An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Handle atomic skill deletion in agent composers https://github.com/siyuan-note/siyuan/issues/20498
Daniel74925810d667
diff --git a/app/src/layout/dock/agent/AgentComposer.ts b/app/src/layout/dock/agent/AgentComposer.tsindex a7da3c8b0c..523eebe81b 100644--- a/app/src/layout/dock/agent/AgentComposer.ts+++ b/app/src/layout/dock/agent/AgentComposer.ts@@ -10,7 +10,8 @@ import {uploadFiles} from "../../../protyle/upload";import {previewImages} from "../../../protyle/preview/image";import {removeCompressURL} from "../../../util/image";import {mountProtyleLiteFragment} from "../../../protyle/lite/fragmentEditor";-import {AGENT_SKILL_SELECTOR, expandAgentSkillSelection, normalizeAgentSkills} from "./agentSkill";+import {AGENT_SKILL_SELECTOR, deleteAgentSkill, expandAgentSkillSelection, normalizeAgentSkills} from "./agentSkill";+import {focusByRange} from "../../../protyle/util/selectionOffsets";export interface AgentComposerData {text: string;@@ -260,9 +261,30 @@ export function mountComposer(host: HTMLElement, onSend: () => void, onChange?:}}};- ["beforeinput", "cut", "paste"].forEach(type => {+ let skillDeletionRange: Range;+ const handleSkillDeletion = (event: InputEvent) => {+ if (!deleteAgentSkill(p, event)) {+ return false;+ }+ skillDeletionRange = getSelection().getRangeAt(0).cloneRange();+ return true;+ };+ wysiwyg.element.addEventListener("beforeinput", (event: InputEvent) => {+ if (!handleSkillDeletion(event)) {+ prepareSkillSelection();+ }+ }, true);+ ["cut", "paste"].forEach(type => {wysiwyg.element.addEventListener(type, prepareSkillSelection, true);});+ wysiwyg.element.addEventListener("keyup", (event: KeyboardEvent) => {+ // Android 软键盘可能在退格后的 keyup 再次清空选区,恢复本次删除后的光标。+ if (event.key === "Unidentified" && skillDeletionRange && getSelection().rangeCount === 0 &&+ wysiwyg.element.contains(skillDeletionRange.startContainer)) {+ focusByRange(skillDeletionRange);+ }+ skillDeletionRange = undefined;+ });// 智能体输入框没有文档 ID,直接预览输入框中的图片,避免走依赖文档资源列表的默认逻辑。wysiwyg.element.addEventListener("dblclick", (event: MouseEvent) => {@@ -284,9 +306,17 @@ export function mountComposer(host: HTMLElement, onSend: () => void, onChange?:// capture 阶段拦截 hint 选择、发送快捷键、历史翻页;undo/redo 交给 protyle 的 keydown(调 LocalUndo)。wysiwyg.element.addEventListener("keydown", (event: KeyboardEvent) => {+ skillDeletionRange = undefined;if (event.isComposing) {return;}+ if (!event.ctrlKey && !event.metaKey && !event.altKey && ["Backspace", "Delete"].includes(event.key) &&+ handleSkillDeletion(new InputEvent("beforeinput", {cancelable: true,+ inputType: event.key === "Backspace" ? "deleteContentBackward" : "deleteContentForward"}))) {+ event.preventDefault();+ event.stopImmediatePropagation();+ return;+ }prepareSkillSelection();// hint 面板可见时,Enter/方向键主动调 hint.select 完成选择,避免 capture 与冒泡的时序问题。const hintEl = p.hint?.element;diff --git a/app/src/layout/dock/agent/agentSkill.test.ts b/app/src/layout/dock/agent/agentSkill.test.tsindex b6fd8a688f..677ced2254 100644--- a/app/src/layout/dock/agent/agentSkill.test.ts+++ b/app/src/layout/dock/agent/agentSkill.test.ts@@ -5,7 +5,7 @@ import {tmpdir} from "node:os";import * as path from "node:path";import {test} from "node:test";import {promisify} from "node:util";-import {createSourceFile, isClassDeclaration, isMethodDeclaration, isVariableStatement, ScriptTarget, transpileModule} from "typescript";+import {createSourceFile, isClassDeclaration, isExpressionStatement, isFunctionDeclaration, isMethodDeclaration, isVariableStatement, ScriptTarget, transpileModule} from "typescript";const browserCases = async (source: string) => {const check: typeof assert = require("node:assert/strict");@@ -14,6 +14,7 @@ const browserCases = async (source: string) => {const insertItems: IHintData[] = [{id: "bold", value: "strong", html: "Bold"}];let inserted = "";let marked = "";+ const deletions: {before: string; after: string}[] = [];const dependencies = {hintSlash: () => insertItems,getFrequentSlashItems: (): IHintData[] => [],@@ -30,14 +31,21 @@ const browserCases = async (source: string) => {return element.innerHTML;},Constants: {BLOCK_HINT_KEYS: ["((", "[["], INLINE_TYPE: ["strong"], ZWSP: "\u200b"},- hideElements: () => {}, getUndoFocusContext: (): undefined => undefined, focusByRange: () => {},+ hideElements: () => {}, getUndoFocusContext: (): undefined => undefined,+ focusByRange: (range: Range) => {+ getSelection().removeAllRanges();+ getSelection().addRange(range);+ },+ updateTransaction: (_protyle: IProtyle, block: Element, before: string) => {+ deletions.push({before, after: block.outerHTML});+ },isProtyleListItemFragment: () => false,hasClosestBlock: (node: Node) => (node instanceof Element ? node : node.parentElement).closest("[data-node-id]"),insertHTML: (html: string) => { inserted = html; },};const api = new Function(...Object.keys(dependencies), source +- "\nreturn {hintSkill, mountLiteSlashMenu, normalizeAgentSkills, expandAgentSkillSelection, " +- "AGENT_SKILL_SELECTOR, Hint, isBuiltinSlashHint};")(...Object.values(dependencies));+ "\nreturn {hintSkill, mountLiteSlashMenu, normalizeAgentSkills, expandAgentSkillSelection, deleteAgentSkill, " ++ "AGENT_SKILL_SELECTOR, Hint, isBuiltinSlashHint, bindComposerInput};")(...Object.values(dependencies));const element = document.createElement("div");element.className = "protyle-hint fn__none";document.body.appendChild(element);@@ -150,6 +158,72 @@ const browserCases = async (source: string) => {document.execCommand("insertText", false, "suffix");check.equal(skill.textContent, "test111");check.ok(editor.textContent.includes("test111suffix"));++ const deleteSkill = (inputType = "deleteContentBackward", isComposing = false) => {+ const event = new InputEvent("beforeinput", {inputType, isComposing, cancelable: true});+ const handled = api.deleteAgentSkill(protyle, event);+ check.equal(event.defaultPrevented, handled);+ return handled;+ };+ for (const forward of [false, true]) {+ for (const position of ["element", "text", "placeholder", "inside"]) {+ skill = prepare();+ const parent = skill.parentNode;+ const index = Array.from(parent.childNodes).indexOf(skill);+ if (position === "inside") {+ select(skill.firstChild, 3);+ } else if (position === "element") {+ select(parent, index + (forward ? 0 : 1));+ } else {+ const text = document.createTextNode(position === "placeholder" ? "\u200b\ufeff" : "");+ parent.insertBefore(text, forward ? skill : skill.nextSibling);+ select(text, forward ? 0 : text.length);+ }+ check.equal(deleteSkill(forward ? "deleteContentForward" : "deleteContentBackward"), true, position);+ check.equal(editor.querySelector(api.AGENT_SKILL_SELECTOR), null);+ check.ok(editor.textContent.includes("before") && editor.textContent.includes("after"));+ check.ok(editor.contains(getSelection().anchorNode));+ const operation = deletions.at(-1);+ editor.innerHTML = operation.before;+ api.normalizeAgentSkills(editor);+ check.equal(editor.querySelector(api.AGENT_SKILL_SELECTOR).textContent, "test111", "undo snapshot preserves skill");+ editor.innerHTML = operation.after;+ check.equal(editor.querySelector(api.AGENT_SKILL_SELECTOR), null, "redo snapshot removes skill");+ }+ }+ skill = prepare();+ select(skill.firstChild, 2, skill.firstChild, 4);+ check.equal(deleteSkill(), true, "partial touch selections delete the whole skill");+ check.equal(editor.querySelector(api.AGENT_SKILL_SELECTOR), null);+ skill = prepare();+ select(skill.nextSibling, 1);+ check.equal(deleteSkill(), false, "ordinary space is deleted before the skill");+ select(skill.parentNode, Array.from(skill.parentNode.childNodes).indexOf(skill) + 1);+ check.equal(deleteSkill("deleteContentBackward", true), false, "composition remains native");+ check.equal(deleteSkill("insertText"), false, "typing remains native");+ protyle.disabled = true;+ check.equal(deleteSkill(), false, "disabled composers cannot delete skills");+ protyle.disabled = false;+ getSelection().removeAllRanges();+ check.equal(deleteSkill(), false, "missing selection remains with the editor recovery handler");+ api.bindComposerInput(protyle);+ for (const input of ["soft", "hardware", "recovered"]) {+ skill = prepare();+ select(skill.parentNode, Array.from(skill.parentNode.childNodes).indexOf(skill) + 1);+ const event = input === "soft" ? new InputEvent("beforeinput", {+ bubbles: true, cancelable: true, inputType: "deleteContentBackward",+ }) : new KeyboardEvent("keydown", {bubbles: true, cancelable: true, key: "Backspace"});+ editor.dispatchEvent(event);+ check.equal(event.defaultPrevented, true);+ check.equal(editor.querySelector(api.AGENT_SKILL_SELECTOR), null);+ if (input !== "hardware") {+ // 模拟 Android 在 Unidentified 的 keyup 中清空选区,及编辑器恢复后派发 Backspace 的路径。+ getSelection().removeAllRanges();+ editor.dispatchEvent(new KeyboardEvent("keyup", {key: "Unidentified", bubbles: true}));+ check.equal(getSelection().rangeCount, 1);+ check.ok(editor.contains(getSelection().anchorNode));+ }+ }return "Agent skill cases passed";};@@ -163,13 +237,23 @@ test("agent mobile commands coexist with skills and skill names remain atomic",const composerSource = composer.statements.filter(statement => isVariableStatement(statement) &&statement.declarationList.declarations.some(declaration => selected.has(declaration.name.getText(composer)))).map(statement => statement.getText(composer)).join("\n");+ const mount = composer.statements.find(statement => isFunctionDeclaration(statement) && statement.name?.text === "mountComposer");+ assert.ok(mount && isFunctionDeclaration(mount));+ const inputBindings = mount.body.statements.filter(statement => {+ if (isVariableStatement(statement)) {+ return statement.declarationList.declarations.some(declaration =>+ ["prepareSkillSelection", "skillDeletionRange", "handleSkillDeletion"].includes(declaration.name.getText(composer)));+ }+ return isExpressionStatement(statement) && /^wysiwyg\.element\.addEventListener\("(?:beforeinput|keydown|keyup)"/.test(statement.getText(composer));+ }).map(statement => statement.getText(composer)).join("\n");const hint = createSourceFile("hint.ts", read("../../../protyle/hint/index.ts"), ScriptTarget.Latest, true);const hintSource = hint.statements.find(isClassDeclaration).members.filter(member => isMethodDeclaration(member) &&["fill", "getHTMLByData"].includes(member.name.getText(hint))).map(member => member.getText(hint)).join("\n");const source = transpileModule([read("../../../protyle/hint/builtinSlash.ts"),read("../../../protyle/hint/blockHintRange.ts"), read("agentHintState.ts"),read("agentSkill.ts"), read("../../../mobile/util/liteSlashMenu.ts"), composerSource,- "class Hint {" + hintSource + "}"].join("\n").replace(/^import .*;\r?\n/gm, "").replace(/^export /gm, ""), {+ "class Hint {" + hintSource + "}",+ "const bindComposerInput = p => { const wysiwyg = p.wysiwyg; " + inputBindings + "};"].join("\n").replace(/^import .*;\r?\n/gm, "").replace(/^export /gm, ""), {compilerOptions: {target: ScriptTarget.ES2021},}).outputText;const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-agent-skill-test-"));diff --git a/app/src/layout/dock/agent/agentSkill.ts b/app/src/layout/dock/agent/agentSkill.tsindex 201d4ba333..0c6e62bce1 100644--- a/app/src/layout/dock/agent/agentSkill.ts+++ b/app/src/layout/dock/agent/agentSkill.ts@@ -1,5 +1,13 @@+import {hasClosestBlock} from "../../../protyle/util/hasClosest";+import {getUndoFocusContext} from "../../../protyle/util/selection";+import {focusByRange} from "../../../protyle/util/selectionOffsets";+import {updateTransaction} from "../../../protyle/wysiwyg/transaction";+export const AGENT_SKILL_SELECTOR = '[data-type~="text"][custom-agent-skill="true"]';+const getAgentSkill = (node: Node) => (node.nodeType === Node.ELEMENT_NODE ? node as Element : node.parentElement)+ ?.closest(AGENT_SKILL_SELECTOR);+export const normalizeAgentSkills = (element: ParentNode) => {// 渲染、撤销和草稿恢复后继续将技能视为整体,兼容已有的技能标记。element.querySelectorAll<HTMLElement>(AGENT_SKILL_SELECTOR).forEach(skill => {@@ -8,10 +16,8 @@ export const normalizeAgentSkills = (element: ParentNode) => {};export const expandAgentSkillSelection = (range: Range) => {- const getSkill = (node: Node) => (node.nodeType === Node.ELEMENT_NODE ? node as Element : node.parentElement)- ?.closest(AGENT_SKILL_SELECTOR);- const start = getSkill(range.startContainer);- const end = getSkill(range.endContainer);+ const start = getAgentSkill(range.startContainer);+ const end = getAgentSkill(range.endContainer);if (range.collapsed) {if (start) {range.setStartAfter(start);@@ -27,3 +33,87 @@ export const expandAgentSkillSelection = (range: Range) => {range.setEndAfter(end);}};++export const deleteAgentSkill = (protyle: IProtyle, event: InputEvent) => {+ if (protyle.disabled || !event.cancelable || event.isComposing ||+ !["deleteContentBackward", "deleteContentForward"].includes(event.inputType)) {+ return false;+ }+ const selection = getSelection();+ const root = protyle.wysiwyg.element;+ if (!selection?.rangeCount || !root.contains(selection.getRangeAt(0).commonAncestorContainer)) {+ return false;+ }+ const range = selection.getRangeAt(0).cloneRange();+ const block = hasClosestBlock(range.startContainer);+ if (!block || block !== hasClosestBlock(range.endContainer)) {+ return false;+ }+ if (range.collapsed) {+ const backward = event.inputType === "deleteContentBackward";+ let skill = getAgentSkill(range.startContainer);+ if (!skill) {+ let node = range.startContainer;+ const element = node.nodeType === Node.ELEMENT_NODE ? node as Element : node.parentElement;+ const editable = element.closest('[contenteditable="true"]');+ if (!editable || !root.contains(editable)) {+ return false;+ }+ let offset = range.startOffset;+ // 只跨过光标占位符,普通空格和文字仍交给原生删除。+ while (node) {+ if (node.nodeType === Node.TEXT_NODE) {+ const remaining = backward ? node.textContent.slice(0, offset) : node.textContent.slice(offset);+ if (!/^[\u200b\ufeff]*$/.test(remaining)) {+ return false;+ }+ } else {+ const child = node.childNodes[backward ? offset - 1 : offset];+ if (child) {+ if (child instanceof Element && child.matches(AGENT_SKILL_SELECTOR)) {+ skill = child;+ break;+ }+ if (child instanceof Element && (child.tagName === "BR" || child.getAttribute("contenteditable") === "false")) {+ return false;+ }+ node = child;+ offset = backward ? (node.nodeType === Node.TEXT_NODE ? node.textContent.length : node.childNodes.length) : 0;+ continue;+ }+ }+ if (node === editable) {+ return false;+ }+ offset = Array.from(node.parentNode.childNodes).indexOf(node as ChildNode) + (backward ? 0 : 1);+ node = node.parentNode;+ }+ }+ if (!skill) {+ return false;+ }+ if (getAgentSkill(range.startContainer)) {+ range.selectNode(skill);+ } else if (backward) {+ range.setStartBefore(skill);+ } else {+ range.setEndAfter(skill);+ }+ } else {+ if (!Array.from(block.querySelectorAll(AGENT_SKILL_SELECTOR)).some(skill => range.intersectsNode(skill))) {+ return false;+ }+ expandAgentSkillSelection(range);+ }+ const oldHTML = block.outerHTML;+ const undoContext = getUndoFocusContext(root, selection.getRangeAt(0));+ event.preventDefault();+ event.stopImmediatePropagation();+ range.deleteContents();+ range.collapse(true);+ focusByRange(range);+ updateTransaction(protyle, block, oldHTML, undoContext, {+ doOperations: [], undoOperations: [], context: getUndoFocusContext(root, range),+ });+ return true;+};