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): 远程会话恢复草稿时剥离 Host capability 芯片

Shinku3c6730db83ff
diff --git a/apps/desktop/src/renderer/__tests__/composerExternalDraftEmptyDoc.test.ts b/apps/desktop/src/renderer/__tests__/composerExternalDraftEmptyDoc.test.ts
index 3482b677d9..e68d37409d 100644
--- a/apps/desktop/src/renderer/__tests__/composerExternalDraftEmptyDoc.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerExternalDraftEmptyDoc.test.ts
@@ -33,7 +33,7 @@ describe('external draft notification empty-document handling', () => {
expect(end).toBeGreaterThan(start);
const block = chatInput.slice(start, end);
- expect(block).toContain('normalizeComposerDocumentJSON(draft.text)');
+ expect(block).toContain('normalizeRestoredComposerDraft(draft.text)');
// 判空必须走共享实现,与 draftHasContent / isEditorEmpty 同源。
expect(block).toContain('tiptapDocHasContent(');
expect(block).not.toMatch(/const normalizedDraftText = draft\.text\s*\n?\s*\?/);
diff --git a/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts b/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts
index 4c12ebdb0b..c6914b6520 100644
--- a/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts
+++ b/apps/desktop/src/renderer/__tests__/composerListDocument.test.ts
@@ -4,6 +4,7 @@ import {
insertComposerDocumentForRestore,
normalizeComposerDocumentJSON,
plainTextToComposerDocument,
+ stripHostCapabilityChips,
} from '@/lib/composerListDocument';
const bulletListDocument = (text: string) => ({
@@ -435,3 +436,104 @@ describe('composer list document normalization', () => {
]);
});
});
+
+describe('stripHostCapabilityChips', () => {
+ const hostCapabilityChip = () => ({
+ type: 'mentionChip',
+ attrs: {
+ kind: 'plugin-capability',
+ label: 'iOS Simulator',
+ path: 'ios-simulator',
+ pluginId: 'cindy-ios-simulator',
+ sourceLabel: 'iOS Simulator',
+ },
+ });
+
+ it('removes host capability chips while preserving surrounding text', () => {
+ const doc = {
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ hostCapabilityChip(),
+ { type: 'text', text: ' ' },
+ { type: 'text', text: '打开模拟器' },
+ ],
+ },
+ ],
+ };
+
+ expect(stripHostCapabilityChips(doc)).toEqual({
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ { type: 'text', text: ' ' },
+ { type: 'text', text: '打开模拟器' },
+ ],
+ },
+ ],
+ });
+ });
+
+ it('leaves non-capability chips (slash / session / plugin-resource) untouched', () => {
+ const doc = {
+ type: 'doc',
+ content: [
+ {
+ type: 'paragraph',
+ content: [
+ { type: 'mentionChip', attrs: { kind: 'slash', label: '/plan', path: '/plan' } },
+ { type: 'text', text: '继续' },
+ ],
+ },
+ ],
+ };
+
+ expect(stripHostCapabilityChips(doc)).toEqual(doc);
+ });
+
+ it('recursively strips chips nested inside lists', () => {
+ const doc = {
+ type: 'doc',
+ content: [
+ {
+ type: 'bulletList',
+ content: [
+ {
+ type: 'listItem',
+ content: [
+ {
+ type: 'paragraph',
+ content: [hostCapabilityChip(), { type: 'text', text: '第一条' }],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ };
+
+ expect(stripHostCapabilityChips(doc)).toEqual({
+ type: 'doc',
+ content: [
+ {
+ type: 'bulletList',
+ content: [
+ {
+ type: 'listItem',
+ content: [
+ {
+ type: 'paragraph',
+ content: [{ type: 'text', text: '第一条' }],
+ },
+ ],
+ },
+ ],
+ },
+ ],
+ });
+ });
+});
diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
index 60dcb8637d..f061f0a208 100644
--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx
@@ -271,6 +271,7 @@ import {
composerDocumentContainsList,
normalizeComposerDocumentJSON,
plainTextToComposerDocument,
+ stripHostCapabilityChips,
} from '@/lib/composerListDocument';
import { useAgentCapabilities, type AgentKind } from '@/hooks/useAgentCapabilities';
import { useConnectedSource } from '@/hooks/useConnectedSource';
@@ -1463,6 +1464,20 @@ export function ChatInput({
remoteHostIdRef.current = remoteHostId;
const deviceLinkDeviceIdRef = useRef<string | null | undefined>(deviceLinkDeviceId);
deviceLinkDeviceIdRef.current = deviceLinkDeviceId;
+ // Host capability 芯片只在「已确认本机」的 composer 里有效:SSH(remoteHostId)或
+ // device-link(deviceLinkDeviceId !== null,含未解析)会话若恢复本地草稿里序列化的芯片,
+ // 发送路径会因 TARGET_UNAVAILABLE 中断、逼用户手动删芯片。这里把「归一化 + 非本机剥芯片」
+ // 收口成一个入口,供草稿恢复路径统一复用(与 pendingHostCapabilityGhostId 的
+ // canPlaceHostCapability 同口径:仅已确认本机保留芯片,否则静默丢弃芯片意图)。
+ const normalizeRestoredComposerDraft = (
+ draftText: JSONContent | null | undefined,
+ ): JSONContent | null => {
+ if (!draftText) return null;
+ const normalized = normalizeComposerDocumentJSON(draftText);
+ return remoteHostIdRef.current || deviceLinkDeviceIdRef.current !== null
+ ? stripHostCapabilityChips(normalized)
+ : normalized;
+ };
const tRef = useRef(t);
tRef.current = t;
// 长文本粘贴 chip 的点击编辑目标。保存时用 nodePos + originalText 双重校验,
@@ -3343,7 +3358,7 @@ export function ChatInput({
if (draftDocument && composerDocIsEmpty(editor.state.doc)) {
isRestoringRef.current = true;
try {
- editor.commands.setContent(normalizeComposerDocumentJSON(draftDocument));
+ editor.commands.setContent(normalizeRestoredComposerDraft(draftDocument));
} finally {
isRestoringRef.current = false;
}
@@ -3403,7 +3418,7 @@ export function ChatInput({
try {
const draft = storageKey !== undefined ? getComposerDraft(storageKey) : undefined;
if (draft?.text) {
- editor.commands.setContent(normalizeComposerDocumentJSON(draft.text));
+ editor.commands.setContent(normalizeRestoredComposerDraft(draft.text));
} else {
editor.commands.clearContent();
}
@@ -3483,7 +3498,7 @@ export function ChatInput({
// "编辑器为空"一律折叠成 null。两侧判空口径必须一致,否则每次外部草稿通知都
// 会拿一份空文档整段 setContent:doc 被原地重建,所有按位置存活的状态(语音
// 草稿锚点等)被迫跨整篇映射(#720 后语音录音时首行多一个空行的成因)。
- const draftDocument = draft.text ? normalizeComposerDocumentJSON(draft.text) : null;
+ const draftDocument = normalizeRestoredComposerDraft(draft.text);
const normalizedDraftText =
draftDocument && tiptapDocHasContent(draftDocument) ? draftDocument : null;
const textUnchanged =
diff --git a/apps/desktop/src/renderer/lib/composerListDocument.ts b/apps/desktop/src/renderer/lib/composerListDocument.ts
index 45d6be295c..e93cd84606 100644
--- a/apps/desktop/src/renderer/lib/composerListDocument.ts
+++ b/apps/desktop/src/renderer/lib/composerListDocument.ts
@@ -307,6 +307,35 @@ export function normalizeComposerDocumentJSON(document: JSONContent): JSONConten
return { ...document, content: normalized };
}
+/**
+ * Remove Host capability chips (`mentionChip` with `attrs.kind === 'plugin-capability'`)
+ * from a composer document, recursively.
+ *
+ * Host capability chips carry routing intent that only makes sense on a
+ * confirmed-local composer. They are serialized into the saved draft's `text`,
+ * so when a draft that was authored locally is restored into a remote/unresolved
+ * session (SSH `remoteHostId`, or device-link `deviceLinkDeviceId !== null`), the
+ * generic `setContent` restore path would otherwise bring the chip back and make
+ * the send path abort with TARGET_UNAVAILABLE — forcing the user to delete it by
+ * hand. Stripping the chip at restore time keeps that path consistent with the
+ * `pendingHostCapabilityGhostId` guard, which already refuses to place the chip
+ * for non-local sessions.
+ */
+export function stripHostCapabilityChips(document: JSONContent): JSONContent {
+ if (!document || typeof document !== 'object' || !Array.isArray(document.content)) {
+ return document;
+ }
+ const nextContent = document.content
+ .map((node) => {
+ if (node?.type === 'mentionChip' && node.attrs?.kind === 'plugin-capability') {
+ return null;
+ }
+ return stripHostCapabilityChips(node);
+ })
+ .filter((node): node is JSONContent => node !== null);
+ return { ...document, content: nextContent };
+}
+
export interface ComposerRestoreInsertion {
document: JSONContent;
insertedBlocks: JSONContent[];