Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ These instructions have been tested on a clean machine to ensure a reliable setu

> **Note:** If the repository already includes `.env.example`, you only need to copy it to `.env`. Creating a new `.env.example` is only necessary if the file is missing.


5. **Start the development server:**
```bash
bun dev
Expand Down
62 changes: 28 additions & 34 deletions src/lib/api/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,19 @@ export async function searchIssues(

const issues = data.items
.filter((item) => !item.pull_request)
.map(
(item): Issue => ({
number: item.number,
title: item.title,
body: item.body,
user: item.user,
labels: item.labels,
assignees: item.assignees,
comments_count: item.comments,
created_at: item.created_at,
updated_at: item.updated_at,
html_url: item.html_url,
state: item.state,
}),
);
.map((item): Issue => ({
number: item.number,
title: item.title,
body: item.body,
user: item.user,
labels: item.labels,
assignees: item.assignees,
comments_count: item.comments,
created_at: item.created_at,
updated_at: item.updated_at,
html_url: item.html_url,
state: item.state,
}));

allIssues.push(...issues);

Expand Down Expand Up @@ -190,15 +188,13 @@ export async function fetchIssueComments(

const data = await response.json();

return data.map(
(c: Record<string, unknown>): Comment => ({
id: c.id as number,
user: c.user as Comment['user'],
body: c.body as string,
created_at: c.created_at as string,
updated_at: c.updated_at as string,
}),
);
return data.map((c: Record<string, unknown>): Comment => ({
id: c.id as number,
user: c.user as Comment['user'],
body: c.body as string,
created_at: c.created_at as string,
updated_at: c.updated_at as string,
}));
}

export async function fetchIssueTimeline(
Expand All @@ -217,16 +213,14 @@ export async function fetchIssueTimeline(

const data: Record<string, unknown>[] = await response.json();

return data.map(
(e): TimelineEvent => ({
event: e.event as string,
created_at: e.created_at as string,
actor: e.actor as TimelineEvent['actor'],
source: e.source as TimelineEvent['source'],
commit_id: e.commit_id as string | undefined,
label: e.label as TimelineEvent['label'],
}),
);
return data.map((e): TimelineEvent => ({
event: e.event as string,
created_at: e.created_at as string,
actor: e.actor as TimelineEvent['actor'],
source: e.source as TimelineEvent['source'],
commit_id: e.commit_id as string | undefined,
label: e.label as TimelineEvent['label'],
}));
} catch {
return [];
}
Expand Down
28 changes: 28 additions & 0 deletions src/lib/api/openrouter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,34 @@ describe('OpenRouter API Client', () => {
const out = await callOpenRouter('s', 'u', 'k');
expect(out).toBe('');
});

it('aborts the request when the response exceeds the timeout', async () => {
let abortFn: (() => void) | null = null;
vi.spyOn(globalThis, 'setTimeout').mockImplementation((fn: any) => {
abortFn = fn;
return 0 as any;
});

mockFetch.mockImplementation((_url: string, init: any) => {
const signal = init?.signal;
return new Promise((_resolve, reject) => {
if (signal?.aborted) {
reject(new DOMException('The operation was aborted.', 'AbortError'));
return;
}
signal?.addEventListener('abort', () => {
reject(new DOMException('The operation was aborted.', 'AbortError'));
});
}); // never resolves on its own
});

const promise = callOpenRouter('s', 'u', 'k');

// Simulate the timeout firing
abortFn!();

await expect(promise).rejects.toThrow('timed out');
});
});

describe('provider dispatch', () => {
Expand Down
59 changes: 40 additions & 19 deletions src/lib/api/openrouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ export interface OpenRouterOptions {
baseUrl?: string;
}

// Abort in-flight OpenRouter requests that stall past this window so a hung
// connection cannot block the analysis queue indefinitely.
export const OPENROUTER_TIMEOUT_MS = 60_000;

export async function callOpenRouter(
prompt: string,
userMessage: string,
Expand All @@ -17,25 +21,42 @@ export async function callOpenRouter(
throw new Error('OpenRouter API Key is missing. Please configure it.');
}

const response = await fetch(`${baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
'HTTP-Referer': window.location.origin,
'X-Title': 'Isscope',
},
body: JSON.stringify({
model,
messages: [
{ role: 'system', content: prompt },
{ role: 'user', content: userMessage },
],
temperature: 0.3,
max_tokens: 1024,
response_format: { type: 'json_object' },
}),
});
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), OPENROUTER_TIMEOUT_MS);

let response: Response;

try {
response = await fetch(`${baseUrl}/chat/completions`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${apiKey}`,
'HTTP-Referer': window.location.origin,
'X-Title': 'Isscope',
},
body: JSON.stringify({
model,
messages: [
{ role: 'system', content: prompt },
{ role: 'user', content: userMessage },
],
temperature: 0.3,
max_tokens: 1024,
response_format: { type: 'json_object' },
}),
signal: controller.signal,
});
} catch (err) {
if (controller.signal.aborted) {
throw new Error(`OpenRouter request timed out after ${OPENROUTER_TIMEOUT_MS / 1000}s`, {
cause: err,
});
}
throw new Error(err instanceof Error ? err.message : String(err), { cause: err });
} finally {
clearTimeout(timeoutId);
}

if (response.status === 429) {
throw new Error('RATE_LIMITED');
Expand Down
Loading