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。

Merge main and preserve stale mention errors with send receipts

Chris74cb7b37ebc5
diff --cc apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
index 6c5b696648,4887a191ae..07c97929b1
--- a/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
+++ b/apps/desktop/src/main/maker-ipc/__tests__/chatServer.test.ts
@@@ -858,6 -810,89 +858,90 @@@ describe('Chat Server result delivery a
});
});
+
+ describe('Chat Server directed sends', () => {
+ const roomId = '10000000-0000-4000-8000-000000000001';
+ const botId = '20000000-0000-4000-8000-000000000001';
+ const selfId = '30000000-0000-4000-8000-000000000001';
+ const humanId = '40000000-0000-4000-8000-000000000001';
+ const rootId = '50000000-0000-4000-8000-000000000001';
+ let service: BotGroupChatService;
+ let responseMode: 'all' | 'mentioned';
++ let nextSendId: number;
+ let members: Array<{ id: string; kind: string; state: string }>;
+ beforeEach(() => {
+ vi.useFakeTimers();
- fixture.packaged = true; responseMode = 'all';
++ fixture.packaged = true; responseMode = 'all'; nextSendId = 0;
+ fixture.profiles = [{ id: 'local-bot', displayName: 'Bot', status: 'active' }];
+ members = [{ id: selfId, kind: 'human', state: 'joined' },
+ { id: botId, kind: 'bot', state: 'joined' }, { id: humanId, kind: 'human', state: 'joined' }];
+ fixture.handle.mockImplementation((route, method) => {
+ if (route === '/me') return { body: { actor: { id: selfId } } };
+ if (route === '/actors') return { body: [{ id: botId, kind: 'bot', externalId: 'local-bot', name: 'Bot' }] };
+ if (route.endsWith('/members')) return { body: members };
+ if (route.endsWith('/snapshot')) return { body: { room: { id: roomId, response_mode: responseMode }, members, messages: [], cursor: '0' } };
+ if (route.endsWith('/messages') && method === 'POST') return { body: { id: 'posted-message' } };
+ return { body: [] };
+ });
+ service = withChatServer({ listGroups: async () => ({ ok: true, groups: [] }), dispose: vi.fn() } as unknown as BotGroupChatService,
+ { dispatch: vi.fn(), onChanged: vi.fn() } as unknown as BotGroupChatServiceDeps);
+ });
+ afterEach(() => { service.dispose(); vi.useRealTimers(); vi.clearAllMocks(); });
+ const posts = () => fixture.handle.mock.calls.filter(([route, method]) => route.endsWith('/messages') && method === 'POST');
+ const send = (entry: string, mentions = { all: false, botIds: ['local-bot'] }) => {
- const input = { groupId: roomId, text: '@Bot hello', clientId: 'directed-send-1', mentions };
++ const input = { groupId: roomId, text: '@Bot hello', clientId: `directed-send-${++nextSendId}`, mentions };
+ return entry === 'thread' ? service.chatServer!.reply({ ...input, rootId }) : service.sendMessage(input);
+ };
+
+ it.each(['main', 'thread'])('rejects a selected bot that left before the %s send instead of posting empty mentions', async entry => {
+ await service.chatServer!.ownedBots();
+ members[1]!.state = 'left';
+ expect(await send(entry)).toMatchObject({ ok: false, errorCode: 'MENTION_UNAVAILABLE' });
+ expect(posts()).toEqual([]);
+ });
+
+ it.each(['main', 'thread'])('preserves actor/local-bot/human targets and excludes self in %s wire payloads', async entry => {
+ for (const all of [false, true]) {
+ for (const targets of [[botId], ['local-bot'], [humanId], [selfId], [selfId, humanId, 'local-bot', botId]]) {
+ expect(await send(entry, { all, botIds: targets })).toMatchObject({ ok: true });
+ const ids = all ? [botId, humanId]
+ : [...new Set(targets.map(target => target === 'local-bot' ? botId : target))].filter(target => target !== selfId);
+ expect(posts().at(-1)![2]).toMatchObject({ mentions: ids,
+ ...(entry === 'thread' ? { threadRootId: rootId } : {}) });
+ }
+ }
+ });
+
+ it.each(['main', 'thread'])('keeps Everyone and ordinary unaddressed %s messages working', async entry => {
+ for (const mode of ['all', 'mentioned'] as const) {
+ responseMode = mode;
+ for (const mentions of [{ all: true, botIds: [] }, { all: false, botIds: [] }]) {
+ expect(await send(entry, mentions)).toMatchObject({ ok: true });
+ expect(posts().at(-1)![2].mentions).toEqual(mentions.all ? [botId, humanId] : []);
+ }
+ }
+ });
+
+ it.each(['main', 'thread'])('refuses unknown, removed, and mixed stale targets in %s without posting', async entry => {
+ for (const state of ['left', 'removed', 'banned', 'invited', 'missing']) {
+ members = state === 'missing' ? members.filter(m => m.id !== botId) : members.map(m => m.id === botId ? { ...m, state } : m);
+ for (const mentions of [{ all: false, botIds: [botId] }, { all: false, botIds: [humanId, 'local-bot'] },
+ { all: true, botIds: [botId] }, { all: false, botIds: [selfId, 'local-bot'] },
+ { all: true, botIds: [selfId, 'local-bot'] }, { all: false, botIds: ['unknown-target'] }]) {
+ expect(await send(entry, mentions)).toMatchObject({ ok: false, errorCode: 'MENTION_UNAVAILABLE' });
+ }
+ }
+ expect(posts()).toEqual([]);
+ });
+
+ it.each(['main', 'thread'])('refuses a departed human target in %s', async entry => {
+ members[2]!.state = 'left';
+ expect(await send(entry, { all: false, botIds: [humanId] })).toMatchObject({ ok: false, errorCode: 'MENTION_UNAVAILABLE' });
+ expect(posts()).toEqual([]);
+ });
+
+ });
+
describe('Chat authentication recovery and polling backoff', () => {
let service: BotGroupChatService;
const denied = (code = 'INVALID_TOKEN') => ({ status: 401, body: { error: { code } } });
diff --cc apps/desktop/src/main/maker-ipc/chatServerErrors.ts
index e1e445f0c4,0000000000..9d6ff0458b
mode 100644,000000..100644
--- a/apps/desktop/src/main/maker-ipc/chatServerErrors.ts
+++ b/apps/desktop/src/main/maker-ipc/chatServerErrors.ts
@@@ -1,31 -1,0 +1,31 @@@
+import type { BotGroupErrorCode, BotGroupFailure } from '../../shared/botGroupChat.js';
+
+/** Keep diagnostic codes and HTTP status, never exception text, URLs, paths or response bodies. */
+export function chatErrorDiagnostic(error: unknown): { code: string; status?: number } {
+ const value = error as { code?: unknown; message?: unknown; status?: unknown; name?: unknown } | null;
+ const candidate = typeof value?.code === 'string' ? value.code : value?.message;
+ const code = typeof candidate === 'string' && /^[A-Z][A-Z0-9_]{0,63}$/.test(candidate)
+ ? candidate : value?.name === 'TimeoutError' || value?.name === 'AbortError' ? 'REQUEST_TIMEOUT' : 'UNKNOWN';
+ return { code, ...(typeof value?.status === 'number' && Number.isInteger(value.status) ? { status: value.status } : {}) };
+}
+
+/** Stable localized categories for desktop and remote controllers; raw errors stay private. */
+export function chatGroupFailure(error: unknown): BotGroupFailure {
+ const { code, status } = chatErrorDiagnostic(error);
+ let errorCode: BotGroupErrorCode;
- if (['PLAN_OPEN', 'PLAN_CLOSED', 'MEMBER_LIMIT', 'MEMBER_UNAVAILABLE', 'HOST_NOT_READY',
++ if (['PLAN_OPEN', 'PLAN_CLOSED', 'MENTION_UNAVAILABLE', 'MEMBER_LIMIT', 'MEMBER_UNAVAILABLE', 'HOST_NOT_READY',
+ 'INVALID_ATTACHMENT', 'ATTACHMENT_UNAVAILABLE', 'ATTACHMENT_TOO_LARGE', 'MEDIA_UPLOAD_FAILED',
+ 'AUTH_REQUIRED', 'IMPORT_PENDING', 'REQUEST_TIMEOUT'].includes(code)) errorCode = code as BotGroupErrorCode;
+ else if (code === 'CONVERSATION_NOT_FOUND' || code === 'OWNER_CHANGED') errorCode = 'NOT_FOUND';
+ else if (['ROLE_REQUIRED', 'ACTOR_NOT_OWNED', 'OWNER_REQUIRED'].includes(code) || status === 403) errorCode = 'PERMISSION_DENIED';
+ else if (status === 401) errorCode = 'AUTH_REQUIRED';
+ else if (code === 'CHAT_ENDPOINT_UNAVAILABLE' || code === 'INVALID_CHAT_ENDPOINT' || ['ECONNREFUSED', 'ENOTFOUND', 'EAI_AGAIN'].includes(code)) errorCode = 'CHAT_UNAVAILABLE';
+ else if (code === 'ETIMEDOUT' || code === 'ECONNRESET') errorCode = 'REQUEST_TIMEOUT';
+ else if (code.startsWith('FILE_PEER_') || code.startsWith('DEVICE_LINK_')) errorCode = 'ATTACHMENT_UNAVAILABLE';
+ else if (['INVALID_MEDIA_URL', 'MEDIA_STORAGE_UNAVAILABLE', 'UPLOAD_QUOTA', 'UPLOAD_MISMATCH', 'MEDIA_NOT_FOUND', 'UPLOAD_EXPIRED'].includes(code)) errorCode = 'MEDIA_UPLOAD_FAILED';
+ else if (code === 'ENOENT' || code === 'EACCES') errorCode = 'INVALID_ATTACHMENT';
+ else if (code === 'CONVERSATION_ARCHIVED') errorCode = 'GROUP_ARCHIVED';
+ else if (code === 'INVALID_INPUT' || code === 'INVALID_PARAMS') errorCode = 'INVALID_PARAMS';
+ else errorCode = 'SERVICE_ERROR';
+ return { ok: false, errorCode, message: errorCode };
+}
diff --cc apps/mobile/src/session/botGroupRemote.ts
index 28e912cbe5,49af8d74b9..b17bcd1ae9
--- a/apps/mobile/src/session/botGroupRemote.ts
+++ b/apps/mobile/src/session/botGroupRemote.ts
@@@ -364,19 -358,9 +364,20 @@@ export function botGroupChatDataFromRes
/** Specific codes first: the transport code (INVALID_PARAMS / NOT_FOUND) wraps them. */
const ERROR_CODES: readonly BotGroupErrorCode[] = [
+ 'INVALID_ATTACHMENT',
+ 'ATTACHMENT_UNAVAILABLE',
+ 'ATTACHMENT_TOO_LARGE',
+ 'MEDIA_UPLOAD_FAILED',
+ 'AUTH_REQUIRED',
+ 'CHAT_UNAVAILABLE',
+ 'IMPORT_PENDING',
+ 'REQUEST_TIMEOUT',
+ 'PERMISSION_DENIED',
+ 'SERVICE_ERROR',
+ 'GROUP_ARCHIVED',
'MEMBER_LIMIT',
'MEMBER_UNAVAILABLE',
+ 'MENTION_UNAVAILABLE',
'PLAN_OPEN',
'PLAN_CLOSED',
'HOST_NOT_READY',
diff --cc packages/maker-shared/src/botGroupPresentation.ts
index 350158b3ec,718b1d7dcc..fa3b05f17b
--- a/packages/maker-shared/src/botGroupPresentation.ts
+++ b/packages/maker-shared/src/botGroupPresentation.ts
@@@ -73,38 -73,18 +73,40 @@@ export function mergeBotGroupMessages
/** Copy variant for a refused group action; null keeps the caller's own fallback. */
export type BotGroupErrorVariant =
+ | 'invalidAttachment'
+ | 'attachmentUnavailable'
+ | 'attachmentTooLarge'
+ | 'mediaUploadFailed'
+ | 'authRequired'
+ | 'chatUnavailable'
+ | 'importPending'
+ | 'requestTimeout'
+ | 'permissionDenied'
+ | 'serviceError'
+ | 'groupArchived'
| 'memberLimit'
| 'memberUnavailable'
+ | 'mentionUnavailable'
| 'notFound'
| 'hostNotReady'
| 'planOpen'
| 'planClosed';
const ERROR_VARIANTS: ReadonlyMap<string, BotGroupErrorVariant> = new Map<BotGroupErrorCode, BotGroupErrorVariant>([
+ ['INVALID_ATTACHMENT', 'invalidAttachment'],
+ ['ATTACHMENT_UNAVAILABLE', 'attachmentUnavailable'],
+ ['ATTACHMENT_TOO_LARGE', 'attachmentTooLarge'],
+ ['MEDIA_UPLOAD_FAILED', 'mediaUploadFailed'],
+ ['AUTH_REQUIRED', 'authRequired'],
+ ['CHAT_UNAVAILABLE', 'chatUnavailable'],
+ ['IMPORT_PENDING', 'importPending'],
+ ['REQUEST_TIMEOUT', 'requestTimeout'],
+ ['PERMISSION_DENIED', 'permissionDenied'],
+ ['SERVICE_ERROR', 'serviceError'],
+ ['GROUP_ARCHIVED', 'groupArchived'],
['MEMBER_LIMIT', 'memberLimit'],
['MEMBER_UNAVAILABLE', 'memberUnavailable'],
+ ['MENTION_UNAVAILABLE', 'mentionUnavailable'],
['NOT_FOUND', 'notFound'],
['HOST_NOT_READY', 'hostNotReady'],
['PLAN_OPEN', 'planOpen'],