Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
feat(desktop): 意识指令胶囊支持整体退格删除 (#927)
zhanghengxdc07245918aa9
diff --git a/apps/desktop/src/renderer/__tests__/chatInputListContinuation.test.ts b/apps/desktop/src/renderer/__tests__/chatInputListContinuation.test.tsindex 3e3ddbf056..ec72666eab 100644--- a/apps/desktop/src/renderer/__tests__/chatInputListContinuation.test.ts+++ b/apps/desktop/src/renderer/__tests__/chatInputListContinuation.test.ts@@ -52,7 +52,17 @@ describe('ChatInput list continuation wiring contract', () => {expect(block).toContain('!event.altKey');expect(block).toContain('!event.shiftKey');expect(block).toContain('!event.isComposing');- expect(block).toContain('(handleStructuredListBackspace(view) || applyListBackspace(view))');+ // 三段 fallback 依次尝试:结构化列表 → 旧纯文本列表 → 意识指令胶囊整体删。+ // 顺序即优先级:意识指令排最后,只在自身命中时接管,不抢列表的退格语义。+ expect(block).toContain('handleStructuredListBackspace(view) ||');+ expect(block).toContain('applyListBackspace(view) ||');+ expect(block).toContain('applyGhostCommandBackspace(view)');+ expect(block.indexOf('handleStructuredListBackspace(view)')).toBeLessThan(+ block.indexOf('applyListBackspace(view)'),+ );+ expect(block.indexOf('applyListBackspace(view)')).toBeLessThan(+ block.indexOf('applyGhostCommandBackspace(view)'),+ );});it('never intercepts plain Enter — send semantics stay untouched', () => {diff --git a/apps/desktop/src/renderer/__tests__/ghostCommandDecoration.test.ts b/apps/desktop/src/renderer/__tests__/ghostCommandDecoration.test.tsindex 51138e83ff..7ab7ef1e8f 100644--- a/apps/desktop/src/renderer/__tests__/ghostCommandDecoration.test.ts+++ b/apps/desktop/src/renderer/__tests__/ghostCommandDecoration.test.ts@@ -8,15 +8,20 @@* 2. 前导空白 / 空段落被 trim → 仍亮;正文中段的 `$` 不亮;* 3. chip 在前(序列化 `@...` 前缀)/ 指令词紧贴 chip(run 未断)→ 不亮;* 4. 未装 / 沉睡 / 全角触发符 / 大小写折叠等与 findGhostByCommand 同判;- * 5. roster 经 meta 推入后 plugin 重算(装/卸即时反映,不依赖 docChanged)。+ * 5. roster 经 meta 推入后 plugin 重算(装/卸即时反映,不依赖 docChanged);+ * 6. Backspace 整体删除只在「胶囊亮 + 光标在胶囊外(尾随空格之后)」接管,+ * 胶囊内(含贴着词尾的位置)与其余情况一律落回原生逐字删——那里是编辑位,+ * 改词能力不能被捷径吃掉。*/import { describe, expect, it } from 'vitest';import { Schema, type Node as PMNode } from '@tiptap/pm/model';-import { EditorState } from '@tiptap/pm/state';+import { EditorState, TextSelection } from '@tiptap/pm/state';+import type { EditorView } from '@tiptap/pm/view';import { readFileSync } from 'node:fs';import { resolve } from 'node:path';import {+ applyGhostCommandBackspace,createGhostCommandPlugin,findGhostCommandMatch,ghostPillAttrs,@@ -201,6 +206,103 @@ describe('createGhostCommandPlugin — roster meta 与 decoration 生命周期',});});+describe('applyGhostCommandBackspace — 整体删除的接管边界', () => {+ const META_KEY = 'ghostCommandDecoration';++ /**+ * 在 state 层跑一次 Backspace:推 roster → 放光标 → 调用。view 只需要+ * state / dispatch 两个面,applyGhostCommandBackspace 不碰 DOM。+ */+ function backspaceAt(+ initial: PMNode,+ ghosts: InstalledGhost[],+ caret: number,+ caretTo = caret,+ ): { handled: boolean; text: string; caret: number } {+ const plugin = createGhostCommandPlugin();+ let state = EditorState.create({ schema, doc: initial, plugins: [plugin] });+ state = state.apply(state.tr.setMeta(META_KEY, ghosts));+ state = state.apply(state.tr.setSelection(TextSelection.create(state.doc, caret, caretTo)));+ const view = {+ get state() {+ return state;+ },+ dispatch: (tr: ReturnType<typeof state.tr.delete>) => {+ state = state.apply(tr);+ },+ } as unknown as EditorView;+ const handled = applyGhostCommandBackspace(view);+ return {+ handled,+ text: state.doc.textBetween(0, state.doc.content.size, '\n'),+ caret: state.selection.from,+ };+ }++ // `$画图 一只猫`:$ 在 1,词尾 4,尾随空格 4,正文自 5 起+ const 带正文 = () => doc(p(txt('$画图 一只猫')));++ it('光标在胶囊外(尾随空格之后):整体删,不留孤立空格', () => {+ expect(backspaceAt(带正文(), [画图], 5)).toEqual({ handled: true, text: '一只猫', caret: 1 });+ });++ it('全角触发符(¥)同权整体删', () => {+ expect(backspaceAt(doc(p(txt('¥画图 猫'))), [画图], 5)).toMatchObject({+ handled: true,+ text: '猫',+ });+ });++ it('不接管:光标贴在词尾(视觉上在胶囊右内边距里)—— 那里是编辑位', () => {+ expect(backspaceAt(带正文(), [画图], 4)).toEqual({+ handled: false,+ text: '$画图 一只猫',+ caret: 4,+ });+ });++ it('不接管:没有尾随空格时光标只可能停在胶囊内,一律逐字删', () => {+ expect(backspaceAt(doc(p(txt('$画图'))), [画图], 4)).toMatchObject({+ handled: false,+ text: '$画图',+ });+ // 词尾是 hardBreak:光标在换行后属于下一行行首,退格该合并行而不是删引用+ expect(backspaceAt(doc(p(txt('$画图'), br(), txt('一只猫'))), [画图], 5)).toMatchObject({+ handled: false,+ });+ });++ it('不接管:光标在词中间 / 触发符前 / 正文里 —— 改词能力照旧', () => {+ expect(backspaceAt(带正文(), [画图], 3)).toMatchObject({+ handled: false,+ text: '$画图 一只猫',+ });+ expect(backspaceAt(带正文(), [画图], 1)).toMatchObject({+ handled: false,+ text: '$画图 一只猫',+ });+ expect(backspaceAt(带正文(), [画图], 8)).toMatchObject({+ handled: false,+ text: '$画图 一只猫',+ });+ });++ it('不接管:选区非空(原生删选区)', () => {+ expect(backspaceAt(带正文(), [画图], 1, 5)).toMatchObject({ handled: false });+ });++ it('不接管:胶囊没亮(roster 空 / 未装该指令 / 意识沉睡 / 位置不对)', () => {+ expect(backspaceAt(带正文(), [], 5)).toMatchObject({ handled: false, text: '$画图 一只猫' });+ expect(backspaceAt(doc(p(txt('$不存在 猫'))), [画图], 5)).toMatchObject({ handled: false });+ expect(backspaceAt(带正文(), [makeGhost('画图', { enabled: false })], 5)).toMatchObject({+ handled: false,+ text: '$画图 一只猫',+ });+ // 正文中段的 `$` 不是消息起点指令,不整体删+ expect(backspaceAt(doc(p(txt('帮我 $画图 猫'))), [画图], 8)).toMatchObject({ handled: false });+ });+});+describe('ghost command pill visual contract', () => {it('matches the shared inline reference chip geometry and color tokens', () => {const pillRule = globalsSource.match(/\.ProseMirror \.ghost-cmd-pill \{([\s\S]*?)\n\}/)?.[1];diff --git a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsxindex 153e49067d..aa35862e19 100644--- a/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx+++ b/apps/desktop/src/renderer/components/new-chat/ChatInput.tsx@@ -47,7 +47,11 @@ import {type VoiceInputCaretState,} from './VoiceInputDraftDecoration';import { MentionDragCaretDecoration, setMentionDragCaret } from './MentionDragCaretDecoration';-import { GhostCommandDecoration, setGhostCommandRoster } from './GhostCommandDecoration';+import {+ applyGhostCommandBackspace,+ GhostCommandDecoration,+ setGhostCommandRoster,+} from './GhostCommandDecoration';import {replaceSlashCommandRunWithText,setSlashCommandRoster,@@ -1805,6 +1809,8 @@ export function ChatInput({// Backspace — structured list items exit through the schema command;// legacy plain Markdown rows keep the prefix-deletion fallback so// pasted and restored text remains editable without a migration pass.+ // 意识指令胶囊排最后:只在胶囊亮起且光标停在胶囊外(尾随空格之后)才+ // 接管,胶囊内一律原样落回原生逐字删。if (event.key === 'Backspace' &&!event.metaKey &&@@ -1812,7 +1818,9 @@ export function ChatInput({!event.altKey &&!event.shiftKey &&!event.isComposing &&- (handleStructuredListBackspace(view) || applyListBackspace(view))+ (handleStructuredListBackspace(view) ||+ applyListBackspace(view) ||+ applyGhostCommandBackspace(view))) {event.preventDefault();return true;diff --git a/apps/desktop/src/renderer/components/new-chat/GhostCommandDecoration.ts b/apps/desktop/src/renderer/components/new-chat/GhostCommandDecoration.tsindex 4dd4f09de9..969dff5fb8 100644--- a/apps/desktop/src/renderer/components/new-chat/GhostCommandDecoration.ts+++ b/apps/desktop/src/renderer/components/new-chat/GhostCommandDecoration.ts@@ -21,7 +21,7 @@*/import { Extension, type Editor } from '@tiptap/core';import { Plugin, PluginKey, type EditorState, type Transaction } from '@tiptap/pm/state';-import { Decoration, DecorationSet } from '@tiptap/pm/view';+import { Decoration, DecorationSet, type EditorView } from '@tiptap/pm/view';import type { Node as PMNode } from '@tiptap/pm/model';import {@@ -153,6 +153,43 @@ function buildDecorations(doc: PMNode, ghosts: InstalledGhost[]): DecorationSet]);}+/** 词尾后面紧跟的分隔空白(不含换行——换行在 doc 里是 hardBreak,不是文本)。 */+const TRAILING_SPACE_RE = /^[^\S\r\n]/;++/**+ * Backspace 整体删除:光标停在胶囊**外面**——也就是指令词后那个分隔空格之后+ * ——时,一次删掉 `$指令` 连同那个空格,而不是逐字符啃到胶囊碎掉。+ *+ * **胶囊内不接管,包括贴着词尾的位置**(`$画图| 一只猫`)。那里 caret 视觉上落在+ * 胶囊的右内边距里,是编辑位:用户是想改错字,不是想扔掉整条引用。同理光标落在+ * 词中间(`$画|图`)也不接管——改词的能力是当初不把指令做成 atom chip 的核心+ * 诉求,不能被这条捷径吃掉。没有尾随空格时(`$画图` 后直接是段落尾 / hardBreak)+ * 光标只可能停在胶囊内,因此一律走原生逐字删。+ *+ * 判定与胶囊严格同源——同一个 findGhostCommandMatch、同一份 plugin roster,+ * **胶囊亮才整体删**;没亮(未装/沉睡/拼错/位置不对)返回 false,原生逐字删的+ * 行为一字不变。+ *+ * 纯编辑侧行为:doc 里仍是纯文本,序列化与 expandGhostCommand 发送链路零改动。+ */+export function applyGhostCommandBackspace(view: EditorView): boolean {+ const { state } = view;+ if (!state.selection.empty) return false;+ const ghosts = PLUGIN_KEY.getState(state)?.ghosts;+ if (!ghosts || ghosts.length === 0) return false;+ const match = findGhostCommandMatch(state.doc, ghosts);+ if (!match) return false;++ // 面板选中时 ChatInput.insertSlashCommand 会补一个空格(`$cmd `):它既是+ // 「胶囊外」的唯一落脚点,也随引用一起收走,免得正文前留一个孤立空格。+ const after = state.doc.resolve(match.to).nodeAfter;+ if (!after?.isText || !TRAILING_SPACE_RE.test(after.text ?? '')) return false;+ if (state.selection.from !== match.to + 1) return false;++ view.dispatch(state.tr.delete(match.from, match.to + 1).scrollIntoView());+ return true;+}+/*** 推送最新意识清单(装/卸/唤醒/沉睡即时反映)。同引用去重——* useInstalledGhosts 的 state 引用只在真变更时更新。