MCPcopy
hub / github.com/7836246/cursor2api / sendCursorRequest

Function sendCursorRequest

src/cursor-client.ts:48–72  ·  view source on GitHub ↗
(
    req: CursorChatRequest,
    onChunk: (event: CursorSSEEvent) => void,
    externalSignal?: AbortSignal,
)

Source from the content-addressed store, hash-verified

46 * 发送请求到 Cursor /api/chat 并以流式方式处理响应(带重试)
47 */
48export async function sendCursorRequest(
49 req: CursorChatRequest,
50 onChunk: (event: CursorSSEEvent) => void,
51 externalSignal?: AbortSignal,
52): Promise<void> {
53 const maxRetries = 2;
54 for (let attempt = 1; attempt <= maxRetries; attempt++) {
55 try {
56 await sendCursorRequestInner(req, onChunk, externalSignal);
57 return;
58 } catch (err) {
59 // 外部主动中止不重试
60 if (externalSignal?.aborted) throw err;
61 // ★ 退化循环中止不重试 — 已有的内容是有效的,重试也会重蹈覆辙
62 if (err instanceof Error && err.message === 'DEGENERATE_LOOP_ABORTED') return;
63 const msg = err instanceof Error ? err.message : String(err);
64 console.error(`[Cursor] 请求失败 (${attempt}/${maxRetries}): ${msg.substring(0, 100)}`);
65 if (attempt < maxRetries) {
66 await new Promise(r => setTimeout(r, 2000));
67 } else {
68 throw err;
69 }
70 }
71 }
72}
73
74async function sendCursorRequestInner(
75 req: CursorChatRequest,

Callers 7

sendCursorRequestFullFunction · 0.85
executeAttemptFunction · 0.85
executeStreamFunction · 0.85
executeAttemptFunction · 0.85
executeStreamFunction · 0.85
handleStreamFunction · 0.85

Calls 2

sendCursorRequestInnerFunction · 0.85
errorMethod · 0.80

Tested by

no test coverage detected