An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: AI Agent https://github.com/siyuan-note/siyuan/issues/17797
Daniel7accc23f0c44
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.tsindex d8c80bd311..fe494585f2 100644--- a/app/src/layout/dock/agent/AgentChat.ts+++ b/app/src/layout/dock/agent/AgentChat.ts@@ -15,7 +15,8 @@ import {getLute} from "../../../protyle/render/setLute";import {setPanelFocus} from "../../util";import {escapeAriaLabel, escapeHtml} from "../../../util/escape";import {setPosition} from "../../../util/setPosition";-import {fetchPost} from "../../../util/fetch";+import {fetchPost, fetchSyncPost} from "../../../util/fetch";+import {Constants} from "../../../constants";import {confirmDialog} from "../../../dialog/confirmDialog";import {showMessage} from "../../../dialog/message";import * as dayjs from "dayjs";@@ -281,6 +282,8 @@ export class AgentChat extends Model {// 内容变化时刷新发送按钮可用性(含用户输入、IME、程序化 clear 等所有 doc 变更)。this.updateSendButtonState();});+ // 支持从编辑器块标或文档面板拖拽块到输入框,效果等同于 @ 引用。+ this.bindComposerDragDrop();this.sessionPanel = new AgentSessionPanel(this.sessionMenuBtn,this.parent.panelElement,@@ -327,6 +330,65 @@ export class AgentChat extends Model {}}+ // 绑定输入框的块拖拽:从编辑器块标或文档面板拖拽块到输入框,插入 @ 引用(mention chip)。+ // 与 @ 搜索的 label 来源一致——用 getRefText API 按 block ID 查询引用文本。+ private bindComposerDragDrop() {+ this.composerHost.addEventListener("dragover", (e: DragEvent) => {+ if (!e.dataTransfer) { return; }+ // 仅接受块标拖拽(SIYUAN_DROP_GUTTER)和文档面板拖拽(SIYUAN_DROP_FILE)。+ let canDrop = false;+ for (const type of e.dataTransfer.types) {+ if (type.startsWith(Constants.SIYUAN_DROP_GUTTER) || type === Constants.SIYUAN_DROP_FILE) {+ canDrop = true;+ break;+ }+ }+ if (canDrop) {+ e.preventDefault();+ e.dataTransfer.dropEffect = "copy";+ }+ });+ this.composerHost.addEventListener("drop", async (e: DragEvent) => {+ if (!e.dataTransfer || !this.composer) { return; }+ const blockIds: string[] = [];+ // 块标拖拽:从 MIME type key 提取所有 block ID(gutterTypes[2])。+ for (const type of e.dataTransfer.types) {+ if (type.startsWith(Constants.SIYUAN_DROP_GUTTER)) {+ const gutterTypes = type.replace(Constants.SIYUAN_DROP_GUTTER, "").split(Constants.ZWSP);+ const ids = (gutterTypes[2] || "").split(",").filter(Boolean);+ blockIds.push(...ids);+ break;+ }+ }+ // 文档面板拖拽:数据值是逗号分隔的 doc ID,全部收集。+ if (blockIds.length === 0) {+ const fileData = e.dataTransfer.getData(Constants.SIYUAN_DROP_FILE);+ if (fileData) {+ blockIds.push(...fileData.split(",").filter(Boolean));+ }+ }+ if (blockIds.length === 0) { return; }+ e.preventDefault();+ e.stopPropagation();+ // 用 getRefText API 并行获取每个块的引用文本作为 label(与 @ 搜索一致),+ // 失败时回退到 blockId。+ const mentions: Array<{id: string; label: string}> = [];+ await Promise.all(blockIds.map(async (id) => {+ let label = id;+ try {+ const resp = await fetchSyncPost("/api/block/getRefText", {id});+ if (resp && resp.data) {+ label = resp.data;+ }+ } catch {+ label = id;+ }+ mentions.push({id, label});+ }));+ this.composer.insertMentions(mentions);+ });+ }+// 从 window.siyuan.config.ai 重新计算可用模型列表,幂等可重复调用。// 与后端 HasAnyProvider()/GetModel() 判定一致:provider 需 enabled 且 apiKey 非空,model 需 enabled。// 零模型时显式置空 selectedModel(避免 undefined 透传到后端),失效选择自动重置。diff --git a/app/src/layout/dock/agent/AgentComposer.ts b/app/src/layout/dock/agent/AgentComposer.tsindex 20b7896213..3cecd95224 100644--- a/app/src/layout/dock/agent/AgentComposer.ts+++ b/app/src/layout/dock/agent/AgentComposer.ts@@ -25,6 +25,8 @@ interface ComposerHandle {getHistory: () => string[];clearHistory: () => void;restoreHistory: (h: string[]) => void;+ insertMention: (id: string, label: string) => void;+ insertMentions: (mentions: Array<{id: string; label: string}>) => void;}// 内容变化回调(含用户输入、IME、程序化 clearContent 等所有 doc 变更)。@@ -404,5 +406,22 @@ export function mountComposer(host: HTMLElement, onSend: () => void, onChange?:getHistory: function () { return history.slice(); },clearHistory: function () { history.length = 0; historyIdx = -1; },restoreHistory: function (h: string[]) { history.length = 0; history.push(...h); historyIdx = -1; },+ insertMention: function (id: string, label: string) {+ editor.chain().focus().insertContent([+ {type: "mention", attrs: {id, label}},+ {type: "text", text: " "},+ ]).run();+ },+ insertMentions: function (mentions: Array<{id: string; label: string}>) {+ // 批量插入多个 mention chip,一次性 insertContent 避免多次 focus/选择重置。+ const nodes: Array<Record<string, unknown>> = [];+ for (const m of mentions) {+ nodes.push({type: "mention", attrs: {id: m.id, label: m.label}});+ nodes.push({type: "text", text: " "});+ }+ if (nodes.length) {+ editor.chain().focus().insertContent(nodes).run();+ }+ },};}