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 AI editor reasoning streams and copy state https://github.com/siyuan-note/siyuan/issues/10540
Daniel9ec26e9a16aa
diff --git a/app/src/ai/editor.ts b/app/src/ai/editor.tsindex 8975158d90..2c19bdee2a 100644--- a/app/src/ai/editor.ts+++ b/app/src/ai/editor.ts@@ -45,6 +45,7 @@ interface IAIEditorTask {status: TAIEditorTaskStatus;content: string;notice: string;+ reasoning: boolean;panel: HTMLElement;body: HTMLElement;modelElement: HTMLElement;@@ -359,19 +360,19 @@ const scheduleTaskPreview = (task: IAIEditorTask) => {const updateTaskActions = (task: IAIEditorTask) => {const conflict = getSourceConflict(task);const sourceButton = task.actionsElement.querySelector<HTMLButtonElement>('[data-action="source"]');+ const copyButton = task.actionsElement.querySelector<HTMLButtonElement>('[data-action="copy"]');if (sourceButton) {sourceButton.disabled = !task.content || !task.source.insertSupported || conflict.missing ||(task.source.kind === "writing" && conflict.changed);}- task.actionsElement.querySelectorAll<HTMLButtonElement>("button").forEach(item => {- if (item.dataset.action !== "retry" && !task.content) {- item.disabled = true;- }- });+ if (copyButton) {+ copyButton.disabled = !task.content;+ }if (conflict.missing || conflict.changed) {task.statusElement.textContent = window.siyuan.languages.aiSourceChanged;} else if (task.status === "streaming") {- task.statusElement.textContent = window.siyuan.languages.loading;+ task.statusElement.textContent = task.reasoning ?+ window.siyuan.languages.agentThinking : window.siyuan.languages.loading;} else if (task.notice) {task.statusElement.textContent = task.notice;} else if (task.status === "stopped") {@@ -411,7 +412,12 @@ const stopTask = (task: IAIEditorTask) => {};const handleTaskEvent = (task: IAIEditorTask, event: TAIEditorSSEEvent) => {- if (event.type === "content") {+ if (event.type === "reasoning") {+ task.reasoning = true;+ task.statusElement.textContent = window.siyuan.languages.agentThinking;+ } else if (event.type === "content") {+ task.reasoning = false;+ task.statusElement.textContent = window.siyuan.languages.loading;task.content += event.token;scheduleTaskPreview(task);} else if (event.type === "truncated") {@@ -434,6 +440,7 @@ const startTaskStream = (task: IAIEditorTask) => {task.status = "streaming";task.content = "";task.notice = "";+ task.reasoning = false;task.body.textContent = "";updateTaskModel(task);task.statusElement.textContent = window.siyuan.languages.loading;@@ -506,6 +513,7 @@ const createTask = (protyle: IProtyle, source: IAIEditorSource) => {status: "streaming",content: "",notice: "",+ reasoning: false,panel,body: panel.querySelector(".ai-editor-panel__body") as HTMLElement,modelElement: panel.querySelector(".ai-editor-panel__model") as HTMLElement,diff --git a/app/src/ai/editorSSE.test.ts b/app/src/ai/editorSSE.test.tsindex 7ae7ad95c9..bcc619c69a 100644--- a/app/src/ai/editorSSE.test.ts+++ b/app/src/ai/editorSSE.test.ts@@ -23,3 +23,12 @@ test("skips malformed AI editor SSE events", () => {token: "ok",}]);});++test("parses AI editor reasoning events", () => {+ const state = createAIEditorSSEParserState();+ assert.deepEqual(parseAIEditorSSE(state,+ "event:reasoning\ndata:{\"token\":\"thinking\"}\n\n"), [{+ type: "reasoning",+ token: "thinking",+ }]);+});diff --git a/app/src/ai/editorSSE.ts b/app/src/ai/editorSSE.tsindex f0f794a917..a89ea07a02 100644--- a/app/src/ai/editorSSE.ts+++ b/app/src/ai/editorSSE.ts@@ -14,6 +14,9 @@ export interface IAIEditorChatRequest {export type TAIEditorSSEEvent = {type: "start";taskID: string;+} | {+ type: "reasoning";+ token: string;} | {type: "content";token: string;@@ -41,6 +44,8 @@ const buildEvent = (event: string, data: Record<string, unknown>): TAIEditorSSEEreturn {type: "start", taskID: String(data.taskID || "")};case "content":return {type: "content", token: String(data.token || "")};+ case "reasoning":+ return {type: "reasoning", token: String(data.token || "")};case "truncated":return {type: "truncated", message: String(data.message || "")};case "error":diff --git a/kernel/api/ai.go b/kernel/api/ai.goindex 1068316cc8..3162d0eff4 100644--- a/kernel/api/ai.go+++ b/kernel/api/ai.go@@ -160,6 +160,12 @@ func aiEditorChat(c *gin.Context) {return}for _, choice := range response.Choices {+ if "" != choice.Delta.ReasoningContent {+ if err = writeSSEEvent(c, "reasoning", map[string]string{"token": choice.Delta.ReasoningContent}); nil != err {+ return+ }+ flusher.Flush()+ }if "" != choice.Delta.Content {if err = writeSSEEvent(c, "content", map[string]string{"token": choice.Delta.Content}); nil != err {return