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(im): 按渠道能力限定额度恢复指引

Dashfec448d16009
diff --git a/apps/desktop/src/main/im/shared/__tests__/turnRetryNotice.test.ts b/apps/desktop/src/main/im/shared/__tests__/turnRetryNotice.test.ts
index 06975f9e0b..aaa6dc68e7 100644
--- a/apps/desktop/src/main/im/shared/__tests__/turnRetryNotice.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/turnRetryNotice.test.ts
@@ -296,9 +296,10 @@ describe('terminalErrorText', () => {
describe('额度类终态错误', () => {
const SPEND_CAP =
'You hit your spend cap set by the owner of your workspace. Ask them to raise it to keep using Codex.';
+ const PERSONAL = { surface: 'personal', supportsModelCommand: true } as const;
it('花费上限: 说清哪个供应商、什么原因、怎么处理, 并附上游原文', () => {
- const text = terminalErrorText({ message: SPEND_CAP }, { providerLabel: 'OpenAI' });
+ const text = terminalErrorText({ message: SPEND_CAP }, { ...PERSONAL, providerLabel: 'OpenAI' });
expect(text.startsWith(QUOTA_FAILURE_NOTICE_PREFIX)).toBe(true);
expect(text).toContain('「OpenAI」已达到工作区所有者设置的花费上限');
expect(text).toContain('Cindy 不会自动更换模型或供应商');
@@ -308,7 +309,7 @@ describe('额度类终态错误', () => {
});
it('处理方式按入口指到对的设置: 个人 bot 指自己的卡片, 官方 bot 指工作目录并接 /new', () => {
- const personal = terminalErrorText({ message: SPEND_CAP });
+ const personal = terminalErrorText({ message: SPEND_CAP }, PERSONAL);
expect(personal).toContain('「Telegram 机器人」');
expect(personal).toContain('新对话配置');
expect(personal).not.toContain('工作目录');
@@ -319,31 +320,50 @@ describe('额度类终态错误', () => {
expect(official).not.toContain('Telegram 机器人');
});
+ it.each([false, undefined])('个人渠道未支持 /model (%s) 时只指向 Cindy 当前任务', (supportsModelCommand) => {
+ const text = terminalErrorText({ message: SPEND_CAP }, { surface: 'personal', supportsModelCommand });
+ expect(text).toContain('在 Cindy 里打开这个任务');
+ expect(text).toContain('换一个供应商或模型');
+ expect(text).toContain('回到这里重发这条消息');
+ expect(text).not.toContain('/model');
+ expect(text).not.toContain('新对话配置');
+ expect(text).not.toContain('Telegram');
+ });
+
+ it.each([
+ { message: SPEND_CAP },
+ { message: 'Payment Required', errorStatus: 402 },
+ { message: 'billing', sdkError: 'billing_error' },
+ { message: 'limit', codexErrorInfo: 'usageLimitExceeded' },
+ ])('非 IM 调用方未传入口时保持原文,不混入 bot 指引: $message', (data) => {
+ expect(terminalErrorText(data)).toBe(data.message);
+ });
+
it('拿不到供应商名时退回「当前供应商」', () => {
- expect(terminalErrorText({ message: SPEND_CAP })).toContain('当前供应商已达到工作区所有者设置的花费上限');
+ expect(terminalErrorText({ message: SPEND_CAP }, PERSONAL)).toContain('当前供应商已达到工作区所有者设置的花费上限');
});
it('认上游的结构化信号: 402 / billing_error / provider_quota_limit / usageLimitExceeded', () => {
expect(isQuotaExhaustionFailure({ message: 'Payment Required', errorStatus: 402 })).toBe(true);
expect(isQuotaExhaustionFailure({ message: 'billing', sdkError: 'billing_error' })).toBe(true);
expect(isQuotaExhaustionFailure({ message: 'x', reason: 'provider_quota_limit' })).toBe(true);
- expect(terminalErrorText({ message: 'limit', codexErrorInfo: 'usageLimitExceeded' })).toContain(
+ expect(terminalErrorText({ message: 'limit', codexErrorInfo: 'usageLimitExceeded' }, PERSONAL)).toContain(
'账号用量已达上限',
);
- expect(terminalErrorText({ message: 'insufficient_quota' })).toContain('账号余额或配额不足');
+ expect(terminalErrorText({ message: 'insufficient_quota' }, PERSONAL)).toContain('账号余额或配额不足');
});
it('普通限流 / 其它错误不算额度耗尽, 沿用原有映射', () => {
expect(isQuotaExhaustionFailure({ message: 'rate limit exceeded', errorStatus: 429 })).toBe(false);
expect(isQuotaExhaustionFailure({ message: 'process exited with code 1' })).toBe(false);
expect(isQuotaExhaustionFailure(null)).toBe(false);
- expect(terminalErrorText({ message: 'process exited with code 1' }, { providerLabel: 'OpenAI' })).toBe(
+ expect(terminalErrorText({ message: 'process exited with code 1' }, { ...PERSONAL, providerLabel: 'OpenAI' })).toBe(
'process exited with code 1',
);
});
it('上游原文脱敏后再附上', () => {
- const text = terminalErrorText({ message: 'insufficient_quota for key sk-abcdefghijklmnop' });
+ const text = terminalErrorText({ message: 'insufficient_quota for key sk-abcdefghijklmnop' }, PERSONAL);
expect(text).not.toContain('sk-abcdefghijklmnop');
});
});
diff --git a/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts b/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
index 218f28c6da..95aef8ed55 100644
--- a/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/turnRunnerSendOutcome.test.ts
@@ -2981,6 +2981,40 @@ describe('turnRunner send outcome policy (feishu adapter characterization)', ()
if (first.kind === 'accepted') await first.terminal;
});
+ it.each(['rich-card', 'chunked-text'] as const)('quota recovery respects %s output capabilities', async (kind) => {
+ const previousOutput = fakeAdapter.output;
+ const commitFinal = vi.fn(async (_payload: { text: string }) => undefined);
+ if (kind === 'chunked-text') {
+ fakeAdapter.output = {
+ kind,
+ im: mocks.feishuIm as unknown as ChannelIM,
+ beginReply: vi.fn(async () => undefined),
+ commitFinal,
+ };
+ }
+ try {
+ const h = setupSession(async () => ({ accepted: true }));
+ await runDefaultTurn();
+ h.emit({ type: 'error', data: { message: 'insufficient_quota', isTerminal: true } });
+ await waitForAssertion(() => {
+ const text = kind === 'chunked-text'
+ ? commitFinal.mock.calls[0]?.[0].text
+ : mocks.feishuIm.sendText.mock.calls[0]?.[1];
+ expect(text).toContain('账号余额或配额不足');
+ if (kind === 'chunked-text') {
+ expect(text).toContain('在 Cindy 里打开这个任务');
+ expect(text).not.toContain('/model');
+ expect(text).not.toContain('新对话配置');
+ } else {
+ expect(text).toContain('/model');
+ expect(text).toContain('新对话配置');
+ }
+ });
+ } finally {
+ fakeAdapter.output = previousOutput;
+ }
+ });
+
it('commits one terminal payload for a chunked-text output driver', async () => {
const previousOutput = fakeAdapter.output;
const beginReply = vi.fn(async () => undefined);
diff --git a/apps/desktop/src/main/im/shared/turnRetryNotice.ts b/apps/desktop/src/main/im/shared/turnRetryNotice.ts
index 241506dd4d..fc74bcdcb6 100644
--- a/apps/desktop/src/main/im/shared/turnRetryNotice.ts
+++ b/apps/desktop/src/main/im/shared/turnRetryNotice.ts
@@ -234,22 +234,30 @@ export interface QuotaFailureNoticeOptions {
providerLabel?: string | null;
/**
* 哪个入口的任务 —— 「怎么处理」要指到对的地方:
- * - `personal`(默认): 个人 bot / 个人 IM 渠道, `/model` 直接改这个任务, 默认模型在
+ * - `personal`: 个人 bot / 个人 IM 渠道, 支持时 `/model` 直接改这个任务, 默认模型在
* 这个 bot 自己的卡片(如「Telegram 机器人」)的「新对话配置」里;
* - `official`: 官方 bot, `/model` 与官方卡片工作目录里的模型改的是**目录默认**,
* 已有任务保持原模型, 所以要接着 `/new` 开新任务, 或在 Cindy 里直接给任务换模型。
*/
- surface?: 'personal' | 'official';
+ surface: 'personal' | 'official';
+ /** 个人渠道只有富卡输出支持 /model;未声明时指向 Cindy 内的当前任务。 */
+ supportsModelCommand?: boolean;
}
-function quotaRecoveryHint(surface: 'personal' | 'official'): string {
- if (surface === 'official') {
+function quotaRecoveryHint(options: QuotaFailureNoticeOptions): string {
+ if (options.surface === 'official') {
return (
'Cindy 不会自动更换模型或供应商。可以发 /model,或在 Cindy 的 设置 → IM 机器人 →' +
'「Telegram」等官方 bot 卡片的工作目录里换一个模型(改的是这个目录的默认,已有任务保持原模型),' +
'然后发 /new 开新任务;也可以在 Cindy 里直接给这个任务换模型。额度恢复后也可以直接重发这条消息。'
);
}
+ if (!options.supportsModelCommand) {
+ return (
+ 'Cindy 不会自动更换模型或供应商。可以在 Cindy 里打开这个任务,给它换一个供应商或模型,' +
+ '然后回到这里重发这条消息。额度恢复后也可以直接重发这条消息。'
+ );
+ }
return (
'Cindy 不会自动更换模型或供应商。可以发 /model 给这个任务换一个供应商或模型;' +
'以后新任务的默认模型在 Cindy 的 设置 → IM 机器人 里这个 bot 自己的卡片' +
@@ -259,7 +267,7 @@ function quotaRecoveryHint(surface: 'personal' | 'official'): string {
export function quotaFailureNotice(
data: unknown,
- options?: QuotaFailureNoticeOptions,
+ options: QuotaFailureNoticeOptions,
): string | null {
if (!isQuotaExhaustionFailure(data)) return null;
const { message, codexErrorInfo } = terminalErrorRecord(data);
@@ -268,19 +276,20 @@ export function quotaFailureNotice(
: codexErrorInfo === 'usageLimitExceeded' || USAGE_LIMIT_RE.test(message)
? '账号用量已达上限'
: '账号余额或配额不足';
- const label = options?.providerLabel?.trim();
+ const label = options.providerLabel?.trim();
const who = label ? `「${label}」` : '当前供应商';
const upstream = redactSensitiveText(message.replace(/\s+/g, ' ').trim()).slice(0, 300);
return [
`${QUOTA_FAILURE_NOTICE_PREFIX}:${who}${reason},这个任务暂时无法继续。`,
- quotaRecoveryHint(options?.surface ?? 'personal'),
+ quotaRecoveryHint(options),
...(upstream ? [`上游原文:${upstream}`] : []),
].join('\n');
}
/**
* 终态 error 事件的 data -> 渠道要展示的失败文案: 已知 reason 与过载类使用可操作说明,
- * 其它错误沿用上游原文。
+ * 其它错误沿用上游原文。额度指引须由 IM 调用方显式传入入口选项,避免定时任务的
+ * 运行记录与通知误用 bot 命令和设置路径。
*
* 单独抽出来是因为渠道侧有**两条**终态收口路径 —— 用户 turn 的 handleTurnErrorAsync
* 与调度转播的 finalizeTranspond。此前只有前者做了映射, 定时任务的卡片在重试耗尽
@@ -299,7 +308,7 @@ export function terminalErrorText(data: unknown, options?: QuotaFailureNoticeOpt
return (
toolLoopFailureNotice(data) ??
overloadFailureNotice(message, errorStatus, codexErrorInfo) ??
- quotaFailureNotice(data, options) ??
+ (options ? quotaFailureNotice(data, options) : null) ??
message
);
}
diff --git a/apps/desktop/src/main/im/shared/turnRunner.ts b/apps/desktop/src/main/im/shared/turnRunner.ts
index f14b56bf59..403ce490c9 100644
--- a/apps/desktop/src/main/im/shared/turnRunner.ts
+++ b/apps/desktop/src/main/im/shared/turnRunner.ts
@@ -3574,6 +3574,8 @@ export function createTurnRunner(
if (isQuotaExhaustionFailure(errData)) {
msg = terminalErrorText(errData, {
providerLabel: await resolveSessionProviderLabel(state.makerSession.id),
+ surface: 'personal',
+ supportsModelCommand: richIm !== null,
});
}
if (turn?.streamingHandle) {
diff --git a/apps/desktop/src/main/scheduler-host/__tests__/runnerSendOutcome.test.ts b/apps/desktop/src/main/scheduler-host/__tests__/runnerSendOutcome.test.ts
index beedafa26a..f59f3b0f85 100644
--- a/apps/desktop/src/main/scheduler-host/__tests__/runnerSendOutcome.test.ts
+++ b/apps/desktop/src/main/scheduler-host/__tests__/runnerSendOutcome.test.ts
@@ -616,6 +616,25 @@ describe('MakerScheduleRunner send outcome policy', () => {
expect(ctx.removeAbortListener).toHaveBeenCalledTimes(1);
});
+ it('keeps quota failures in scheduler notifications free of IM recovery instructions', async () => {
+ const h = createSessionHarness(async (_message, opts) => {
+ await opts?.onAccepted?.();
+ return { accepted: true };
+ });
+ const { runner, notifier } = createRunnerHarness(h.session);
+ const message = 'You hit your spend cap set by the owner of your workspace.';
+ const firePromise = runner.fire(baseSchedule(), createFireContext());
+ const rejected = expect(firePromise).rejects.toThrow(message);
+
+ await vi.waitFor(() => expect(h.listenerCount()).toBe(1));
+ h.emit({ type: 'error', data: { message, isTerminal: true } });
+
+ await rejected;
+ const run = latestNotifiedRun(notifier);
+ expect(run.status).toBe('failed');
+ expect(run.errorMsg).toBe(message);
+ });
+
it('projects tool-loop terminal errors to a safe scheduler notification', async () => {
const h = createSessionHarness(async (_message, opts) => {
await opts?.onAccepted?.();
diff --git a/docs/product-rules/telegram-bot-parity.md b/docs/product-rules/telegram-bot-parity.md
index 0aaa9cfce3..4f17919d0a 100644
--- a/docs/product-rules/telegram-bot-parity.md
+++ b/docs/product-rules/telegram-bot-parity.md
@@ -11,6 +11,11 @@ Cindy 有两个 Telegram bot,用户看到的是同一个产品:
两者是**两套架构**,不是同一份代码的两个开关。所以"统一"不等于"让两条代码路径长得
一样",而是:**能同源的同源;不能同源的,差异必须写在这张表里、有裁决、有理由。**
+额度说明的调用边界:IM 入口须显式传入 `surface` 才启用 bot 恢复指引;定时任务运行记录、
+通知与旁路转播未传入口时保留原有错误映射。个人渠道的 `/model` 指引只用于支持富卡的
+输出(包括个人 Telegram);钉钉、企业微信等纯文本输出指向在 Cindy 中打开当前任务换
+供应商或模型,再回原渠道重发,不把仅影响以后新任务的默认配置当成当前任务的恢复方式。
+
2026-09-09 多账号路由补充:官方 bot 的 hook 新任务入口保留目录/草稿中明确选中的
供应商连接;该连接失效时走既有失败提示,不改用另一账号的同名模型。
未指定连接仍解析默认来源。本项只调整官方 hook(及共用该入口的渠道),