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(desktop): show unwrapped upstream errors as-is (#3385)

Chris Zhang316e06beea5e
diff --git a/apps/desktop/src/renderer/__tests__/errorBannerStreamInterrupt.test.tsx b/apps/desktop/src/renderer/__tests__/errorBannerStreamInterrupt.test.tsx
index 39f1b48156..c501d6d7db 100644
--- a/apps/desktop/src/renderer/__tests__/errorBannerStreamInterrupt.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/errorBannerStreamInterrupt.test.tsx
@@ -84,3 +84,31 @@ describe('ErrorBanner — LiteLLM 流中断', () => {
expect(screen.queryByTitle('chat.errorBanner.retryTitle')).toBeNull();
});
});
+
+const XAI_REJECTED_RAW = `OpenAI API error (400): ${JSON.stringify({
+ message: `litellm.BadRequestError: XaiException - ${JSON.stringify({
+ error: { message: 'Upstream rejected the request!', type: 'invalid_request_error' },
+ })}`,
+ type: null,
+ param: null,
+ code: '400',
+})}`;
+
+
+describe('ErrorBanner — LiteLLM 外壳拆封', () => {
+ it('展示上游内层原文,协议外壳可展开', () => {
+ render(
+ createElement(ErrorBanner, {
+ error: XAI_REJECTED_RAW,
+ retryText: 'retry-token',
+ onRetry: vi.fn(),
+ agentKind: 'pi',
+ }),
+ );
+
+ expect(screen.getByText('Upstream rejected the request!')).toBeTruthy();
+ expect(screen.queryByText(XAI_REJECTED_RAW)).toBeNull();
+ fireEvent.click(screen.getByText('chat.errorBanner.networkShowRaw'));
+ expect(screen.getByText(XAI_REJECTED_RAW)).toBeTruthy();
+ });
+});
diff --git a/apps/desktop/src/renderer/__tests__/streamInterruptError.test.ts b/apps/desktop/src/renderer/__tests__/streamInterruptError.test.ts
index a0bab1cd10..ee387fd9e5 100644
--- a/apps/desktop/src/renderer/__tests__/streamInterruptError.test.ts
+++ b/apps/desktop/src/renderer/__tests__/streamInterruptError.test.ts
@@ -6,6 +6,20 @@ import {
unwrapProviderErrorDisplay,
} from '@/utils/streamInterruptError';
+/** 与 Pi + LiteLLM + xAI 实锤信封同构,避免手写转义把 JSON 写坏。 */
+function xaiRejectedEnvelope(
+ innerMessage = 'Upstream rejected the request!',
+): string {
+ return `OpenAI API error (400): ${JSON.stringify({
+ message: `litellm.BadRequestError: XaiException - ${JSON.stringify({
+ error: { message: innerMessage, type: 'invalid_request_error' },
+ })}`,
+ type: null,
+ param: null,
+ code: '400',
+ })}`;
+}
+
describe('isStreamInterruptedErrorMessage', () => {
it('accepts the classified reason even when the message has been rewritten', () => {
expect(
@@ -39,6 +53,12 @@ describe('unwrapProviderErrorDisplay', () => {
).toBe('XaiException - too long');
});
+ it('peels the nested xAI invalid_request JSON down to the inner message', () => {
+ expect(unwrapProviderErrorDisplay(xaiRejectedEnvelope())).toBe(
+ 'Upstream rejected the request!',
+ );
+ });
+
it('leaves unrelated messages alone', () => {
expect(unwrapProviderErrorDisplay('Network error: fetch failed')).toBe(
'Network error: fetch failed',
@@ -62,4 +82,14 @@ describe('unwrapProviderErrorDisplay', () => {
unwrapProviderErrorDisplay('OpenAI API error (400): {"message":"invalid_prompt"}'),
).toBe('OpenAI API error (400): {"message":"invalid_prompt"}');
});
+
+ it('does not rewrite the other Pi API error dialects', () => {
+ const anthropic =
+ 'API Error: 400 {"type":"error","error":{"type":"invalid_request_error","message":"prompt is too long"}}';
+ const google = 'GoogleGenerativeAI Error: [400] Invalid JSON payload received';
+ const completions = 'OpenAI API error (429): rate_limit_exceeded';
+ expect(unwrapProviderErrorDisplay(anthropic)).toBe(anthropic);
+ expect(unwrapProviderErrorDisplay(google)).toBe(google);
+ expect(unwrapProviderErrorDisplay(completions)).toBe(completions);
+ });
});
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/ErrorMessageCard.test.tsx b/apps/desktop/src/renderer/components/chat/__tests__/ErrorMessageCard.test.tsx
index f36f539505..fc968ad9c3 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/ErrorMessageCard.test.tsx
+++ b/apps/desktop/src/renderer/components/chat/__tests__/ErrorMessageCard.test.tsx
@@ -48,4 +48,23 @@ describe('ErrorMessageCard', () => {
fireEvent.click(screen.getByText('chat.errorBanner.networkShowRaw'));
expect(screen.getByText(raw)).toBeTruthy();
});
+
+ it('shows the inner upstream message instead of the OpenAI JSON envelope', () => {
+ const raw = `OpenAI API error (400): ${JSON.stringify({
+ message: `litellm.BadRequestError: XaiException - ${JSON.stringify({
+ error: {
+ message: 'Upstream rejected the request!',
+ type: 'invalid_request_error',
+ },
+ })}`,
+ type: null,
+ param: null,
+ code: '400',
+ })}`;
+ render(createElement(ErrorMessageCard, { message: raw }));
+ expect(screen.getByText('Upstream rejected the request!')).toBeTruthy();
+ expect(screen.queryByText(raw)).toBeNull();
+ fireEvent.click(screen.getByText('chat.errorBanner.networkShowRaw'));
+ expect(screen.getByText(raw)).toBeTruthy();
+ });
});
diff --git a/apps/desktop/src/renderer/utils/streamInterruptError.ts b/apps/desktop/src/renderer/utils/streamInterruptError.ts
index df7677ff24..01eeb34597 100644
--- a/apps/desktop/src/renderer/utils/streamInterruptError.ts
+++ b/apps/desktop/src/renderer/utils/streamInterruptError.ts
@@ -2,6 +2,9 @@
* 流式输出中途被掐断(renderer 侧)。ErrorBanner 用它把
* `OpenAI API error (500): … Response API in-stream error` 换成友好文案,
* 原文折叠可查。不跨 bundle 共享 maker-core 代码,与 overload / network 同款惯例。
+ *
+ * 同文件把 LiteLLM / OpenAI Responses 套在厂商错误外的协议外壳剥掉,
+ * 展示上游内层原文。只改展示,不改写原因、不落盘。
*/
export const UPSTREAM_STREAM_INTERRUPTED_REASON = 'upstream-stream-interrupted';
@@ -18,9 +21,11 @@ export function isStreamInterruptedErrorMessage(
const OPENAI_API_ERROR_PREFIX = /^(?:Azure )?OpenAI API error \(\d+\):\s*/i;
const LITELLM_ERROR_PREFIX = /^litellm\.\w+Error:\s*/;
+const VENDOR_JSON_EXCEPTION_RE = /^([A-Za-z][\w.]*)\s*-\s*(\{[\s\S]*\})$/;
+const MAX_UNWRAP_DEPTH = 5;
function isLiteLlmEnvelope(text: string): boolean {
- return /litellm\./i.test(text) || LITELLM_ERROR_PREFIX.test(text);
+ return LITELLM_ERROR_PREFIX.test(text) || /^litellm\./i.test(text);
}
function unwrapLiteLlmInner(text: string): string {
@@ -28,15 +33,60 @@ function unwrapLiteLlmInner(text: string): string {
return stripped.length > 0 ? stripped : text;
}
+function extractErrorMessage(value: unknown, depth = 0): string | null {
+ if (depth > MAX_UNWRAP_DEPTH || value == null) return null;
+ if (typeof value === 'string') {
+ const trimmed = value.trim();
+ if (!trimmed) return null;
+ if (trimmed.startsWith('{')) {
+ try {
+ return extractErrorMessage(JSON.parse(trimmed), depth + 1) ?? trimmed;
+ } catch {
+ return trimmed;
+ }
+ }
+ const peeled = peelVendorJsonPayload(trimmed);
+ if (peeled !== trimmed) {
+ return extractErrorMessage(peeled, depth + 1) ?? peeled;
+ }
+ return trimmed;
+ }
+ if (typeof value === 'object' && !Array.isArray(value)) {
+ const obj = value as Record<string, unknown>;
+ if ('error' in obj) {
+ const fromError = extractErrorMessage(obj.error, depth + 1);
+ if (fromError) return fromError;
+ }
+ if (typeof obj.message === 'string') {
+ return extractErrorMessage(obj.message, depth + 1);
+ }
+ }
+ return null;
+}
+
+/** `XaiException - {json}` → 内层 error.message;非 JSON 后缀原样保留。 */
+function peelVendorJsonPayload(text: string): string {
+ const match = text.match(VENDOR_JSON_EXCEPTION_RE);
+ if (!match) return text;
+ try {
+ const inner = extractErrorMessage(JSON.parse(match[2]));
+ return inner && inner.length > 0 ? inner : text;
+ } catch {
+ return text;
+ }
+}
+
/**
- * 兜底展示时剥掉 **LiteLLM 套在 OpenAI Responses 客户端上的协议外壳**。
+ * 剥掉 LiteLLM 套在 OpenAI Responses 客户端上的协议外壳,留下上游原文。
* Pi 的 Responses 客户端不分厂商,一律写成 `OpenAI API error`;LiteLLM 再套 JSON。
* 真 OpenAI / Azure OpenAI 错误保留前缀与状态码。只改展示,不改落盘。
*/
export function unwrapProviderErrorDisplay(message: string): string {
const text = message.trim();
const prefixMatch = text.match(OPENAI_API_ERROR_PREFIX);
- if (!prefixMatch) return text.length > 0 ? text : message;
+ if (!prefixMatch) {
+ return peelVendorJsonPayload(text.length > 0 ? text : message);
+ }
const rest = text.slice(prefixMatch[0].length).trim();
if (rest.startsWith('{')) {
@@ -50,7 +100,7 @@ export function unwrapProviderErrorDisplay(message: string): string {
) {
const inner = (parsed as { message: string }).message.trim();
if (inner.length > 0 && isLiteLlmEnvelope(inner)) {
- return unwrapLiteLlmInner(inner);
+ return peelVendorJsonPayload(unwrapLiteLlmInner(inner));
}
}
} catch {
@@ -58,6 +108,6 @@ export function unwrapProviderErrorDisplay(message: string): string {
}
}
- if (isLiteLlmEnvelope(rest)) return unwrapLiteLlmInner(rest);
+ if (isLiteLlmEnvelope(rest)) return peelVendorJsonPayload(unwrapLiteLlmInner(rest));
return message;
}