Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

fix(utility-model): 推理预算重试收到 401 时上报 Cindy AI Key 失效(处理 #6148 Codex review)

fmfsaisaibbda4373e0a5
diff --git a/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts b/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts
index 3a69dac581..cbab2907e7 100644
--- a/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts
+++ b/apps/desktop/src/main/utility-model/__tests__/oneShotCandidates.test.ts
@@ -3105,6 +3105,59 @@ describe('utility one-shot candidates', () => {
expect(reportKeyRejected).toHaveBeenCalledWith('proxy-key-sent');
});
+ it.each([
+ [401, 1],
+ [500, 0],
+ ] as const)('LiteLLM budget retry rejected with HTTP %s reports the sent key %s times', async (status, reports) => {
+ chainState.refs = ['litellm-gpt-5.4-mini'];
+ readKey.mockReturnValue('proxy-key-sent');
+ fetchMock
+ .mockResolvedValueOnce({
+ ok: true,
+ json: async () => ({
+ choices: [{ message: { content: '' }, finish_reason: 'length' }],
+ usage: { completion_tokens: 800 },
+ }),
+ } as never)
+ .mockResolvedValueOnce({ ok: false, status, body: { cancel: vi.fn(async () => undefined) } } as never);
+
+ const result = await requestUtilityText(makerMock(false), 'hello', { maxTokens: 800 });
+
+ expect(result).toMatchObject({ ok: false });
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(reportKeyRejected).toHaveBeenCalledTimes(reports);
+ if (reports > 0) expect(reportKeyRejected).toHaveBeenCalledWith('proxy-key-sent');
+ });
+
+ it.each([
+ [401, 1],
+ [500, 0],
+ ] as const)('explicit XD budget retry rejected with HTTP %s reports the sent key %s times', async (status, reports) => {
+ activeCatalog.mockReturnValue(xdCatalog as never);
+ readKey.mockReturnValue('xd-key-sent');
+ fetchMock
+ .mockResolvedValueOnce({
+ ok: true,
+ text: async () => JSON.stringify({
+ choices: [{ message: { content: '' }, finish_reason: 'length' }],
+ usage: { completion_tokens: 800 },
+ }),
+ } as never)
+ .mockResolvedValueOnce({ ok: false, status, body: { cancel: vi.fn(async () => undefined) } } as never);
+
+ const result = await requestUtilityText(makerMock(false), 'generate', {
+ providerId: 'xd',
+ agentKind: 'claude-code',
+ model: 'gpt-5.5',
+ maxTokens: 800,
+ });
+
+ expect(result).toMatchObject({ ok: false });
+ expect(fetchMock).toHaveBeenCalledTimes(2);
+ expect(reportKeyRejected).toHaveBeenCalledTimes(reports);
+ if (reports > 0) expect(reportKeyRejected).toHaveBeenCalledWith('xd-key-sent');
+ });
+
it('reports the key of the cindy-gateway Auto-review candidate rejected with 401', async () => {
readKey.mockReturnValue('review-key-sent');
fetchMock.mockResolvedValueOnce(unauthorized() as never);
diff --git a/apps/desktop/src/main/utility-model/oneShotCandidates.ts b/apps/desktop/src/main/utility-model/oneShotCandidates.ts
index c26272804e..032ea90697 100644
--- a/apps/desktop/src/main/utility-model/oneShotCandidates.ts
+++ b/apps/desktop/src/main/utility-model/oneShotCandidates.ts
@@ -1479,6 +1479,7 @@ async function requestLiteLlmText(input: {
parsed = await budgeted.json() as ChatCompletionBody;
text = chatCompletionChoicesText(parsed);
} else {
+ if (budgeted.status === 401) reportCindyGatewayKeyRejected(input.apiKey);
await budgeted.body?.cancel().catch(() => undefined);
}
}
@@ -1746,6 +1747,7 @@ async function requestProviderHttpText(input: {
raw = await budgeted.text();
text = wireResponseText(input.wire, raw);
} else {
+ if (budgeted.status === 401) input.onUnauthorized?.();
await budgeted.body?.cancel().catch(() => undefined);
}
}