Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

🐛 fix(aiChat): forward outputJSON error details; add balance limit copy (#22767)

YuTengjingb9ff6759c206
diff --git a/apps/server/src/routers/lambda/_helpers/runtimeErrorCause.test.ts b/apps/server/src/routers/lambda/_helpers/runtimeErrorCause.test.ts
new file mode 100644
index 00000000000..72dc4301d7e
--- /dev/null
+++ b/apps/server/src/routers/lambda/_helpers/runtimeErrorCause.test.ts
@@ -0,0 +1,24 @@
+import { describe, expect, it } from 'vitest';
+
+import { toRuntimeErrorCause } from './runtimeErrorCause';
+
+describe('toRuntimeErrorCause', () => {
+ it('exposes a plain runtime payload as data so the client receives errorData', () => {
+ const budgetError = {
+ budget: { blockingRules: [{ rule: 'overflow.month' }], requiredCredits: 1000 },
+ errorType: 'InsufficientBudgetForModel',
+ message: 'Budget exceeded',
+ };
+
+ expect(toRuntimeErrorCause(budgetError)).toEqual({ ...budgetError, data: budgetError });
+ });
+
+ it('passes Error instances, payloads that already carry data, and primitives through', () => {
+ const error = new Error('boom');
+ const withData = { data: { code: 1 }, errorType: 'X' };
+
+ expect(toRuntimeErrorCause(error)).toBe(error);
+ expect(toRuntimeErrorCause(withData)).toBe(withData);
+ expect(toRuntimeErrorCause('oops')).toBe('oops');
+ });
+});
diff --git a/apps/server/src/routers/lambda/_helpers/runtimeErrorCause.ts b/apps/server/src/routers/lambda/_helpers/runtimeErrorCause.ts
new file mode 100644
index 00000000000..88c373297f7
--- /dev/null
+++ b/apps/server/src/routers/lambda/_helpers/runtimeErrorCause.ts
@@ -0,0 +1,17 @@
+import { toRecord } from '@lobechat/utils/object';
+
+/**
+ * Build the `cause` for a TRPCError raised from a model-runtime error.
+ *
+ * The lambda errorFormatter forwards `cause.data` to the client as
+ * `errorData`. A plain runtime error payload is exposed there, so callers such
+ * as the input-completion alert can read its structured details (for example
+ * which budget rule blocked), the same payload the chat error card receives.
+ * Errors that are `Error` instances or already carry `data` pass through.
+ */
+export const toRuntimeErrorCause = (error: unknown): unknown => {
+ const record = toRecord(error);
+ if (!record || error instanceof Error || 'data' in record) return error;
+
+ return { ...record, data: record };
+};
diff --git a/apps/server/src/routers/lambda/aiChat.ts b/apps/server/src/routers/lambda/aiChat.ts
index f22180fb569..c77f3e6367d 100644
--- a/apps/server/src/routers/lambda/aiChat.ts
+++ b/apps/server/src/routers/lambda/aiChat.ts
@@ -31,6 +31,7 @@ import { markSilentTRPCErrorLog } from '@/libs/trpc/utils/errorLogger';
import { unwrapPgError } from '@/server/modules/AgentRuntime/pgError';
import { assertCanUseConversationTargets } from '@/server/routers/lambda/_helpers/conversationResourceGuard';
import { resolveContext } from '@/server/routers/lambda/_helpers/resolveContext';
+import { toRuntimeErrorCause } from '@/server/routers/lambda/_helpers/runtimeErrorCause';
import { resolveAgentReference } from '@/server/services/agent/resolveReference';
import { AiChatService } from '@/server/services/aiChat';
import { AiGenerationService } from '@/server/services/aiGeneration';
@@ -106,7 +107,7 @@ const createRuntimeTRPCError = (
if (options?.silentHandlerLog && runtimeStatus < 500) markSilentTRPCErrorLog(error);
return new TRPCError({
- cause: error,
+ cause: toRuntimeErrorCause(error),
code: getTRPCErrorCodeFromStatus(runtimeStatus),
message:
typeof errorType === 'string'
diff --git a/locales/en-US/subscription.json b/locales/en-US/subscription.json
index e014a866c83..cd77268878b 100644
--- a/locales/en-US/subscription.json
+++ b/locales/en-US/subscription.json
@@ -347,6 +347,10 @@
"limitation.insufficientBudget.exactDesc": "This request needs more balance. Top up or upgrade your plan.",
"limitation.insufficientBudget.required": "Required amount",
"limitation.insufficientBudget.rescue.desc": "This request needs {{shortfall}} more to continue",
+ "limitation.insufficientBudget.rescue.limit.benefit": "{{spent}} of {{limit}} used this month. Raise the limit to keep going on your balance.",
+ "limitation.insufficientBudget.rescue.limit.benefitPlain": "Raise the limit to keep going on your balance.",
+ "limitation.insufficientBudget.rescue.limit.cta": "Adjust limit",
+ "limitation.insufficientBudget.rescue.limit.title": "Raise your monthly balance limit",
"limitation.insufficientBudget.rescue.overflow.badge": "Enough for this",
"limitation.insufficientBudget.rescue.overflow.benefit": "Spend your {{credits}} balance once plan limits run out. No extra cost.",
"limitation.insufficientBudget.rescue.overflow.cta": "Use balance and continue",
@@ -855,9 +859,16 @@
"usage.window.credits": "Balance",
"usage.window.left": "{{percent}}% left",
"usage.window.notStarted": "Starts with your next request",
+ "usage.window.overflow.belowSpend": "Not above the {{spent}} used this month, so your balance stops covering requests.",
"usage.window.overflow.desc": "When a limit runs out, keep going on your balance.",
"usage.window.overflow.limit": "{{spent}} / {{limit}} this month",
+ "usage.window.overflow.monthlyLimit": "Monthly limit",
+ "usage.window.overflow.monthlyLimitDesc": "The most your balance pays each month once plan limits run out. Leave empty for no limit.",
+ "usage.window.overflow.monthlyLimitTitle": "Monthly balance limit",
+ "usage.window.overflow.noLimit": "No limit",
"usage.window.overflow.off": "Off: requests pause when a limit runs out",
+ "usage.window.overflow.save": "Save",
+ "usage.window.overflow.spent": "{{spent}} used this month",
"usage.window.overflow.title": "Use balance after limits",
"usage.window.overflow.updateFailed": "Update failed. Try again.",
"usage.window.referral": "Referral credits",
diff --git a/locales/zh-CN/subscription.json b/locales/zh-CN/subscription.json
index 702bf3a5de1..c8fdd18dc86 100644
--- a/locales/zh-CN/subscription.json
+++ b/locales/zh-CN/subscription.json
@@ -347,6 +347,10 @@
"limitation.insufficientBudget.exactDesc": "本次请求需要更多余额,请充值或升级套餐。",
"limitation.insufficientBudget.required": "所需金额",
"limitation.insufficientBudget.rescue.desc": "本次请求还差 {{shortfall}},补上就能继续",
+ "limitation.insufficientBudget.rescue.limit.benefit": "本月已用 {{spent}} / {{limit}},调高后可继续使用余额",
+ "limitation.insufficientBudget.rescue.limit.benefitPlain": "调高上限后可继续使用余额",
+ "limitation.insufficientBudget.rescue.limit.cta": "调整上限",
+ "limitation.insufficientBudget.rescue.limit.title": "调高每月余额上限",
"limitation.insufficientBudget.rescue.overflow.badge": "足够本次使用",
"limitation.insufficientBudget.rescue.overflow.benefit": "套餐限额用完后改用 {{credits}} 余额,无需额外付费",
"limitation.insufficientBudget.rescue.overflow.cta": "使用余额并继续",
@@ -855,9 +859,16 @@
"usage.window.credits": "余额",
"usage.window.left": "剩余 {{percent}}%",
"usage.window.notStarted": "下次请求时开始计算",
+ "usage.window.overflow.belowSpend": "不高于本月已用的 {{spent}},余额将无法继续支付请求",
"usage.window.overflow.desc": "限额用完后,继续使用余额。",
"usage.window.overflow.limit": "本月 {{spent}} / {{limit}}",
+ "usage.window.overflow.monthlyLimit": "每月上限",
+ "usage.window.overflow.monthlyLimitDesc": "套餐限额用完后,每月最多用余额支付的金额。留空表示不限。",
+ "usage.window.overflow.monthlyLimitTitle": "每月余额上限",
+ "usage.window.overflow.noLimit": "不限",
"usage.window.overflow.off": "已关闭:限额用完后暂停请求",
+ "usage.window.overflow.save": "保存",
+ "usage.window.overflow.spent": "本月已用 {{spent}}",
"usage.window.overflow.title": "限额用完后使用余额",
"usage.window.overflow.updateFailed": "更新失败,请重试",
"usage.window.referral": "返利额度",
diff --git a/packages/locales/src/default/subscription.ts b/packages/locales/src/default/subscription.ts
index 45439535e91..03f015ce441 100644
--- a/packages/locales/src/default/subscription.ts
+++ b/packages/locales/src/default/subscription.ts
@@ -113,6 +113,12 @@ export default {
'Spend your {{credits}} balance once plan limits run out. No extra cost.',
'limitation.insufficientBudget.rescue.overflow.cta': 'Use balance and continue',
'limitation.insufficientBudget.rescue.overflow.title': 'Use your balance',
+ 'limitation.insufficientBudget.rescue.limit.benefit':
+ '{{spent}} of {{limit}} used this month. Raise the limit to keep going on your balance.',
+ 'limitation.insufficientBudget.rescue.limit.benefitPlain':
+ 'Raise the limit to keep going on your balance.',
+ 'limitation.insufficientBudget.rescue.limit.cta': 'Adjust limit',
+ 'limitation.insufficientBudget.rescue.limit.title': 'Raise your monthly balance limit',
'limitation.insufficientBudget.rescue.topup.benefitOverflow':
"Top-up balance is used after your plan limits run out. We'll turn that on for you.",
'limitation.insufficientBudget.rescue.upgrade.benefitMultiple':
@@ -973,6 +979,15 @@ export default {
'usage.window.overflow.off': 'Off: requests pause when a limit runs out',
'usage.window.overflow.updateFailed': 'Update failed. Try again.',
'usage.window.overflow.title': 'Use balance after limits',
+ 'usage.window.overflow.belowSpend':
+ 'Not above the {{spent}} used this month, so your balance stops covering requests.',
+ 'usage.window.overflow.monthlyLimit': 'Monthly limit',
+ 'usage.window.overflow.monthlyLimitDesc':
+ 'The most your balance pays each month once plan limits run out. Leave empty for no limit.',
+ 'usage.window.overflow.monthlyLimitTitle': 'Monthly balance limit',
+ 'usage.window.overflow.noLimit': 'No limit',
+ 'usage.window.overflow.save': 'Save',
+ 'usage.window.overflow.spent': '{{spent}} used this month',
'usage.window.resetsIn': 'Resets in {{duration}}',
'usage.window.rule.plan5h': '5-hour limit',
'usage.window.rule.plan7d': 'Weekly limit',