Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: https://github.com/siyuan-note/siyuan/issues/18813

Vanessac86addc92c7c
diff --git a/app/src/layout/dock/agent/AgentComposer.ts b/app/src/layout/dock/agent/AgentComposer.ts
index 0b57b2d740..6260000cb9 100644
--- a/app/src/layout/dock/agent/AgentComposer.ts
+++ b/app/src/layout/dock/agent/AgentComposer.ts
@@ -8,6 +8,7 @@ import {genEmptyElement} from "../../../block/util";
import {blockRender} from "../../../protyle/render/blockRender";
import {focusBlock} from "../../../protyle/util/selection";
import {matchHotKey} from "../../../protyle/util/hotKey";
+import {isSkillHintRequestActive, shouldYieldSkillHint} from "./agentHintState";
export interface AgentComposerData {
text: string;
@@ -32,6 +33,7 @@ interface ComposerHandle {
type OnChangeCallback = () => void;
const AGENT_HINT_OVERLAY_CLASS = "protyle-hint--agent-overlay";
+const skillHintRequestIDs = new WeakMap<IProtyle, number>();
interface ComposerOptions {
initialContent?: string;
@@ -67,9 +69,28 @@ const hintAgentRef = (key: string, protyle: IProtyle, source: THintSource): IHin
// / 技能菜单:异步拉取 lsSkills,选中后把技能名作为纯文本插入(value 即技能名)。
// 返回 [] 占位,数据在 fetch 回调里通过 protyle.hint.genHTML 填充(与 hintRef 异步模式一致)。
const hintSkill = (key: string, protyle: IProtyle): IHintData[] => {
+ const requestID = (skillHintRequestIDs.get(protyle) || 0) + 1;
+ skillHintRequestIDs.set(protyle, requestID);
+ if (shouldYieldSkillHint(key, protyle.options.hint.extend.map((item) => item.key))) {
+ protyle.hint.enableExtend = false;
+ protyle.hint.genHTML([], protyle, true, "hint");
+ return [];
+ }
prepareAgentHint(protyle);
protyle.hint.genLoading(protyle);
fetchPost("/api/ai/agent/lsSkills", {}, (response) => {
+ // 异步响应返回时输入状态可能已变化,避免 Esc 或其他提示触发后重新打开旧菜单。
+ if (!isSkillHintRequestActive({
+ requestID,
+ currentRequestID: skillHintRequestIDs.get(protyle),
+ enableExtend: protyle.hint.enableExtend,
+ enableSlash: protyle.hint.enableSlash,
+ splitChar: protyle.hint.splitChar,
+ hidden: protyle.hint.element.classList.contains("fn__none"),
+ connected: protyle.hint.element.isConnected,
+ })) {
+ return;
+ }
const rawSkills = (response && response.data) ? response.data : [];
const q = key.toLowerCase();
const dataList: IHintData[] = rawSkills
diff --git a/app/src/layout/dock/agent/agentHintState.test.ts b/app/src/layout/dock/agent/agentHintState.test.ts
new file mode 100644
index 0000000000..66f2782afa
--- /dev/null
+++ b/app/src/layout/dock/agent/agentHintState.test.ts
@@ -0,0 +1,32 @@
+import * as assert from "node:assert/strict";
+import test from "node:test";
+import {isSkillHintRequestActive, shouldYieldSkillHint} from "./agentHintState";
+
+const activeState = {
+ requestID: 2,
+ currentRequestID: 2,
+ enableExtend: true,
+ enableSlash: true,
+ splitChar: "/",
+ hidden: false,
+ connected: true,
+};
+
+test("skill hint renders the current active request", () => {
+ assert.equal(isSkillHintRequestActive(activeState), true);
+});
+
+test("skill hint ignores a response after Escape", () => {
+ assert.equal(isSkillHintRequestActive({...activeState, enableExtend: false, hidden: true}), false);
+});
+
+test("skill hint ignores stale responses and changed triggers", () => {
+ assert.equal(isSkillHintRequestActive({...activeState, currentRequestID: 3}), false);
+ assert.equal(isSkillHintRequestActive({...activeState, splitChar: "[["}), false);
+});
+
+test("skill hint yields to a multi-character hint prefix before requesting", () => {
+ const hintKeys = ["((", "[[", "/", "、"];
+ assert.equal(shouldYieldSkillHint("(", hintKeys), true);
+ assert.equal(shouldYieldSkillHint("query", hintKeys), false);
+});
diff --git a/app/src/layout/dock/agent/agentHintState.ts b/app/src/layout/dock/agent/agentHintState.ts
new file mode 100644
index 0000000000..c8a6cccdbe
--- /dev/null
+++ b/app/src/layout/dock/agent/agentHintState.ts
@@ -0,0 +1,20 @@
+import {endsWithMultiCharHintPrefix} from "../../../protyle/hint/blockHintRange";
+
+interface ISkillHintRequestState {
+ requestID: number;
+ currentRequestID: number | undefined;
+ enableExtend: boolean;
+ enableSlash: boolean;
+ splitChar: string;
+ hidden: boolean;
+ connected: boolean;
+}
+
+export const isSkillHintRequestActive = (state: ISkillHintRequestState) => {
+ return state.currentRequestID === state.requestID && state.enableExtend && state.enableSlash &&
+ ["/", "、"].includes(state.splitChar) && !state.hidden && state.connected;
+};
+
+export const shouldYieldSkillHint = (key: string, hintKeys: string[]) => {
+ return endsWithMultiCharHintPrefix(key, hintKeys);
+};
diff --git a/app/src/protyle/hint/index.ts b/app/src/protyle/hint/index.ts
index c7dc0d08ce..36e8a5f1e5 100644
--- a/app/src/protyle/hint/index.ts
+++ b/app/src/protyle/hint/index.ts
@@ -279,37 +279,36 @@ export class Hint {
// https://github.com/siyuan-note/siyuan/issues/5083
if (this.splitChar === "/" || this.splitChar === "、") {
clearTimeout(this.timeId);
+ const blockElement = hasClosestBlock(protyle.toolbar.range.startContainer);
+ if (!this.enableSlash || !blockElement || isInEmbedBlock(blockElement)) {
+ return;
+ }
if (protyle.lite) {
- if (this.enableSlash) {
- protyle.options.hint.extend.find((item) => {
- if (item.key === "/" && item.hint) {
- item.hint(key, protyle, "hint");
- return true;
- }
- });
- }
- } else {
- const blockElement = hasClosestBlock(protyle.toolbar.range.startContainer);
- if (this.enableSlash && !isMobile() && blockElement && !isInEmbedBlock(blockElement)) {
- const slashData = hintSlash(key, protyle);
- if (slashData.length === 0) {
- if (endsWithMultiCharHintPrefix(key, protyle.options.hint.extend.map((item) => item.key))) {
- this.enableExtend = false;
- }
- this.genHTML(slashData, protyle, true, "hint");
- return;
+ protyle.options.hint.extend.find((item) => {
+ if (item.key === "/" && item.hint) {
+ item.hint(key, protyle, "hint");
+ return true;
}
- const createTarget = this.prepareCreateTarget(protyle, "doc");
- if (createTarget.result !== undefined) {
- this.genHTML(hintSlash(key, protyle, createTarget.result), protyle, true, "hint");
- } else {
- this.genLoading(protyle);
- createTarget.promise.then((isCurrentSubDoc) => {
- if (createTarget.isCurrent()) {
- this.genHTML(hintSlash(key, protyle, isCurrentSubDoc), protyle, true, "hint");
- }
- });
+ });
+ } else if (!isMobile()) {
+ const slashData = hintSlash(key, protyle);
+ if (slashData.length === 0) {
+ if (endsWithMultiCharHintPrefix(key, protyle.options.hint.extend.map((item) => item.key))) {
+ this.enableExtend = false;
}
+ this.genHTML(slashData, protyle, true, "hint");
+ return;
+ }
+ const createTarget = this.prepareCreateTarget(protyle, "doc");
+ if (createTarget.result !== undefined) {
+ this.genHTML(hintSlash(key, protyle, createTarget.result), protyle, true, "hint");
+ } else {
+ this.genLoading(protyle);
+ createTarget.promise.then((isCurrentSubDoc) => {
+ if (createTarget.isCurrent()) {
+ this.genHTML(hintSlash(key, protyle, isCurrentSubDoc), protyle, true, "hint");
+ }
+ });
}
}
return;