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
send problem title, version to backend
  • Loading branch information
zubyj committed Mar 17, 2025
commit 461d302ecc1d549bf56bcba7eab2d1db54befc21
17 changes: 17 additions & 0 deletions src/background/background.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,24 @@
import { getChatGPTAccessToken } from './chatgpt/chatgpt.js';

// Helper function to get or create user ID
function getRandomToken(): string {
const randomPool = new Uint8Array(32);
crypto.getRandomValues(randomPool);
return Array.from(randomPool)
.map(b => b.toString(16).padStart(2, '0'))
.join('');
}

// Load problem data & default settings on install
chrome.runtime.onInstalled.addListener(() => {
// Generate and store user ID if it doesn't exist
chrome.storage.sync.get('userId', function (items) {
if (!items.userId) {
const userId = getRandomToken();
chrome.storage.sync.set({ userId: userId });
}
});

// Load JSON file of problem data into storage
const leetcodeProblems = chrome.runtime.getURL('src/assets/data/problem_data.json');
fetch(leetcodeProblems)
Expand Down
21 changes: 19 additions & 2 deletions src/background/openrouter/openrouter.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
export interface AIProvider {
generateAnswer(params: {
prompt: string,
action: 'analyze' | 'fix',
onEvent: (arg: { type: string, data?: { text: string } }) => void
}): Promise<void>;
}

export class OpenRouterProvider implements AIProvider {
private readonly apiUrl: string;
private readonly model: string;
private readonly version: string = '1.0.0'; // Add version tracking

constructor(apiKey: string, model: string = 'amazon/nova-micro-v1') {
this.apiUrl = 'https://api.leetcodeapp.com';
this.model = model;
}

async generateAnswer(params: { prompt: string, onEvent: (arg: { type: string, data?: { text: string } }) => void }) {
async generateAnswer(params: {
prompt: string,
action: 'analyze' | 'fix',
onEvent: (arg: { type: string, data?: { text: string } }) => void
}) {
try {
// Get the userId from chrome.storage.sync
const userIdResult = await chrome.storage.sync.get('userId');
const userId = userIdResult.userId;

// Get problem title from storage instead of querying tabs
const titleResult = await chrome.storage.local.get('currentLeetCodeProblemTitle');
const problemTitle = titleResult.currentLeetCodeProblemTitle?.split('-')[0].trim() || '';

const response = await fetch(`${this.apiUrl}/api/generate`, {
method: 'POST',
credentials: 'include',
Expand All @@ -26,7 +40,10 @@ export class OpenRouterProvider implements AIProvider {
body: JSON.stringify({
prompt: params.prompt,
model: this.model,
action: 'fix' // You may want to pass this as a parameter
userId: userId,
version: this.version,
problemTitle: problemTitle,
action: params.action
})
});

Expand Down
3 changes: 2 additions & 1 deletion src/popup/popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ function stripMarkdownCodeBlock(text: string): string {
function processCode(
chatGPTProvider: AIProvider,
codeText: string,
action: string,
action: 'analyze' | 'fix',
): void {
disableAllButtons(true);
clearResponse();
Expand Down Expand Up @@ -178,6 +178,7 @@ function processCode(
Promise.race([
chatGPTProvider.generateAnswer({
prompt: prompt,
action: action,
onEvent: (event: { type: string; data?: { text: string } }) => {
if (event.type === 'answer' && event.data) {
if (action === 'fix' && fixCodeResponse) {
Expand Down