Skip to content

replace gpt with openrouter, users can add api key in settings #61

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 16 commits into from
Apr 4, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
update fix code prompt, use qwen model
  • Loading branch information
zubyj committed Mar 14, 2025
commit ed2a0a8cfe0f278d9f2fcbdfa100f0530ffe8cbd
2 changes: 1 addition & 1 deletion src/background/openrouter/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class OpenRouterProvider implements AIProvider {
private readonly apiKey: string;
private readonly model: string;

constructor(apiKey: string, model: string = 'openai/gpt-3.5-turbo') {
constructor(apiKey: string, model: string = 'qwen/qwen-2-7b-instruct') {
this.apiKey = apiKey;
this.model = model;
}
Expand Down
30 changes: 18 additions & 12 deletions src/popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ function formatResponseText(text: string): string {
.replace(/space/gi, '<span style="color: lightgreen;">space complexity</span>');
}

function stripMarkdownCodeBlock(text: string): string {
// Remove any text between the first set of ```
text = text.replace(/```[^\n]*\n/, '');
// Remove trailing ```
text = text.replace(/```$/, '');
return text;
}

function processCode(
chatGPTProvider: AIProvider,
codeText: string,
Expand All @@ -148,19 +156,18 @@ function processCode(
if (fixCodeContainer) fixCodeContainer.classList.add('hidden');
}
else if (action === 'fix') {

// Prompt for generating solution code
prompt = `
As a coding professional, I need your expertise with a specific LeetCode problem named ${problemTitle}.
Please follow the instructions:
1. If no code is provided: Generate an efficient and accurate solution for the problem.
2. If code is provided and contains errors: Identify the issues, correct them, and optimize the code if possible.
3. If the provided code is already correct and optimized: Simply return it as-is.
IMPORTANT: Your response should only include the function definition and code solution in plain text format (no backticks, code blocks, or additional formatting).
Do not explain your solution or provide any additional information other than the code.
Here's the problem description and code:\n
You are a LeetCode solution generator. STRICTLY follow these rules for the Leetcode problem "${problemTitle}":

- Provide ONLY raw solution code with NO markdown (Do NOT include "\`\`\`" or the language name).
- Include ONLY the exact solution class and function definition LeetCode expects.
- NO explanations, comments, markdown formatting, examples, or test cases.
- The solution MUST be directly copy-pastable into LeetCode's editor WITHOUT modification.

Problem and initial code structure:
${codeText}
`
`;
if (infoMessage) infoMessage.textContent = 'Generating solution code ...';
analyzeCodeResponse && analyzeCodeResponse.classList.add('hidden');
fixCodeContainer && fixCodeContainer.classList.remove('hidden');
Expand All @@ -174,9 +181,8 @@ function processCode(
if (event.type === 'answer' && event.data) {
if (action === 'fix' && fixCodeResponse) {
response += event.data.text;
fixCodeResponse.textContent = response;
fixCodeResponse.textContent = stripMarkdownCodeBlock(response); // Strip markdown code blocks
(window as any).Prism.highlightAll();

}
else if (action === 'analyze' && analyzeCodeResponse) {
response += formatResponseText(event.data.text); // Use the helper function here
Expand Down