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): handle DingTalk text-only commands

davidbbf49ce0ba55
diff --git a/apps/desktop/src/main/im/dingtalk/__tests__/uiText.test.ts b/apps/desktop/src/main/im/dingtalk/__tests__/uiText.test.ts
new file mode 100644
index 0000000000..5d4623b812
--- /dev/null
+++ b/apps/desktop/src/main/im/dingtalk/__tests__/uiText.test.ts
@@ -0,0 +1,25 @@
+import { describe, expect, it } from 'vitest';
+
+import { ui } from '../uiText';
+
+describe('DingTalk UI text pack', () => {
+ it('derives the complete shared pack instead of the expired-card fragment', () => {
+ expect(ui.slash.new).toBeTruthy();
+ expect(ui.agent.completedNoText).toBeTruthy();
+ expect(ui.cards.model.title).toBeTruthy();
+ expect(ui.cards.control.title).toBeTruthy();
+ });
+
+ it('does not advertise slash commands that require rich cards', () => {
+ expect(ui.slash.help).not.toContain('/model');
+ expect(ui.slash.help).not.toContain('/permission');
+ expect(ui.slash.help).not.toContain('/ctr');
+ expect(ui.slash.help).not.toContain('/session');
+ expect(ui.slash.interactiveCommandUnsupported?.('/model')).toContain('Cindy 桌面端');
+ });
+
+ it('relabels inherited Telegram copy for DingTalk', () => {
+ expect(ui.slash.detachedBySlash).toContain('钉钉');
+ expect(ui.slash.detachedBySlash).not.toContain('Telegram');
+ });
+});
diff --git a/apps/desktop/src/main/im/dingtalk/uiText.ts b/apps/desktop/src/main/im/dingtalk/uiText.ts
index a2febd0ba4..1730c8cb8c 100644
--- a/apps/desktop/src/main/im/dingtalk/uiText.ts
+++ b/apps/desktop/src/main/im/dingtalk/uiText.ts
@@ -1,24 +1,42 @@
import type { ImUiTextPack } from '../shared/types';
-import { telegramUiText } from '../telegram/uiText';
+import { ui as telegramUi } from '../telegram/uiText';
/**
* Shared IM control copy is channel-neutral apart from a small number of
* route labels. Keep the DingTalk pack derived at the composition boundary so
* fixes to permissions, model selection, and takeover guidance stay aligned.
*/
-export const ui = replaceChannelLabel(telegramUiText) as ImUiTextPack;
+const sharedUi = replaceChannelLabel(telegramUi);
-function replaceChannelLabel(value: unknown): unknown {
+export const ui = {
+ ...sharedUi,
+ slash: {
+ ...sharedUi.slash,
+ help: `🤖 我能帮你做这些:
+
+/new 开个新对话(清掉当前上下文)
+/stop 停止当前任务,并撤掉排队消息
+/help 查看可用命令
+
+模型、权限和远程接管请在 Cindy 桌面端调整。`,
+ unknownCommand: (cmd: string) =>
+ `没认出 \`${cmd}\` 这个命令 🤔\n我能听懂的:/new、/stop、/help`,
+ interactiveCommandUnsupported: (cmd: string) =>
+ `钉钉暂不支持 ${cmd} 的交互选择,请在 Cindy 桌面端完成对应设置。`,
+ },
+} satisfies ImUiTextPack;
+
+function replaceChannelLabel<T>(value: T): T {
if (typeof value === 'string') {
- return value.replaceAll('Telegram', '钉钉').replaceAll('TG', '钉钉');
+ return value.replaceAll('Telegram', '钉钉').replaceAll('TG', '钉钉') as T;
}
if (typeof value === 'function') {
- return (...args: unknown[]) =>
- replaceChannelLabel((value as (...input: unknown[]) => unknown)(...args));
+ return ((...args: unknown[]) =>
+ replaceChannelLabel((value as (...input: unknown[]) => unknown)(...args))) as T;
}
- if (Array.isArray(value)) return value.map(replaceChannelLabel);
+ if (Array.isArray(value)) return value.map(replaceChannelLabel) as T;
if (!value || typeof value !== 'object') return value;
return Object.fromEntries(
Object.entries(value).map(([key, child]) => [key, replaceChannelLabel(child)]),
- );
+ ) as T;
}
diff --git a/apps/desktop/src/main/im/shared/__tests__/slashCommands.test.ts b/apps/desktop/src/main/im/shared/__tests__/slashCommands.test.ts
index d95a8313a8..4a8cf840a8 100644
--- a/apps/desktop/src/main/im/shared/__tests__/slashCommands.test.ts
+++ b/apps/desktop/src/main/im/shared/__tests__/slashCommands.test.ts
@@ -44,7 +44,7 @@ vi.mock('../controlState', () => ({
enterControl: vi.fn(),
}));
-import type { ChannelIM } from '@cindy/im';
+import type { ChannelIM, TextChannelIM } from '@cindy/im';
import { ui } from '../../feishu/uiText';
import { createSlashHandlers } from '../slashCommands';
@@ -277,6 +277,46 @@ describe('IM slash commands', () => {
);
});
+ it('text-only channels explain and skip slash commands that require cards', async () => {
+ const unsupported = (cmd: string) => `unsupported:${cmd}`;
+ const outputIm = {
+ sendMarkdownText: mocks.sendMarkdownText,
+ } as unknown as TextChannelIM;
+ const { handlers, cards, turnRunner } = makeHarness({
+ adapterOverrides: {
+ output: {
+ kind: 'chunked-text',
+ im: outputIm,
+ commitFinal: vi.fn(),
+ },
+ ui: {
+ ...ui,
+ slash: { ...ui.slash, interactiveCommandUnsupported: unsupported },
+ },
+ },
+ });
+
+ for (const cmd of ['/model', '/permission', '/ctr', '/session', '/project']) {
+ await handlers.handleSlashCommand(cmd, {
+ botContextId: 'bot',
+ userId: 'ou_user',
+ });
+ }
+
+ expect(mocks.sendMarkdownText.mock.calls).toEqual(
+ ['/model', '/permission', '/ctr', '/session', '/project'].map((cmd) => [
+ 'ou_user',
+ unsupported(cmd),
+ ]),
+ );
+ expect(mocks.sendInteractiveCard).not.toHaveBeenCalled();
+ expect(cards.buildModelPickerCard).not.toHaveBeenCalled();
+ expect(cards.buildPermissionModePickerCard).not.toHaveBeenCalled();
+ expect(cards.buildControlPickerCard).not.toHaveBeenCalled();
+ expect(cards.buildProjectPickerCard).not.toHaveBeenCalled();
+ expect(turnRunner.resolveRouteTarget).not.toHaveBeenCalled();
+ });
+
describe('/project (projectSwitching channels)', () => {
const projectUi = {
title: 'P',
diff --git a/apps/desktop/src/main/im/shared/slashCommands.ts b/apps/desktop/src/main/im/shared/slashCommands.ts
index f4f83524ae..dd53023f8c 100644
--- a/apps/desktop/src/main/im/shared/slashCommands.ts
+++ b/apps/desktop/src/main/im/shared/slashCommands.ts
@@ -38,6 +38,14 @@ import { bindingStore, executeDetach } from '../binding';
import type { IdentityKey } from '@cindy/im';
import type { ImChannelAdapter } from './types';
+const INTERACTIVE_SLASH_COMMANDS = new Set([
+ '/model',
+ '/permission',
+ '/ctr',
+ '/session',
+ '/project',
+]);
+
/** Quick text-only check; treat anything starting with '/' (no spaces before) as a command. */
export function looksLikeSlashCommand(text: string): boolean {
return text.startsWith('/');
@@ -87,6 +95,14 @@ export function createSlashHandlers(
const [cmd] = text.trim().split(/\s+/);
log.info(`slash cmd=${cmd} userId=...${ctx.userId.slice(-8)}`);
+ if (adapter.output.kind === 'chunked-text' && INTERACTIVE_SLASH_COMMANDS.has(cmd)) {
+ await safeSendText(
+ ctx.userId,
+ ui.slash.interactiveCommandUnsupported?.(cmd) ?? ui.slash.unknownCommand(cmd),
+ );
+ return true;
+ }
+
switch (cmd) {
case '/help':
await safeSendText(ctx.userId, ui.slash.help);
diff --git a/apps/desktop/src/main/im/shared/types.ts b/apps/desktop/src/main/im/shared/types.ts
index 17b46532da..4a3533d9fe 100644
--- a/apps/desktop/src/main/im/shared/types.ts
+++ b/apps/desktop/src/main/im/shared/types.ts
@@ -183,6 +183,11 @@ export interface ImUiTextPack {
*/
start?: string;
unknownCommand: (cmd: string) => string;
+ /**
+ * Text-only channels use this when a slash command requires interactive
+ * cards. Missing copy falls back to unknownCommand.
+ */
+ interactiveCommandUnsupported?: (cmd: string) => string;
detachedBySlash: string;
detachedByRevoke: string;
notAttached: string;