Skip to content

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

:bug: Avoid zero-second completed thinking labels https://github.com/siyuan-note/siyuan/issues/18576

Daniel64e657aee598
diff --git a/app/src/layout/dock/agent/AgentChat.ts b/app/src/layout/dock/agent/AgentChat.ts
index e0ed69b591..0cfe05b776 100644
--- a/app/src/layout/dock/agent/AgentChat.ts
+++ b/app/src/layout/dock/agent/AgentChat.ts
@@ -22,6 +22,7 @@ import {
applyAgentUserEdit,
buildAgentPresentationEntries,
findAgentUserEntryIndex,
+ getAgentThinkingDisplaySeconds,
getAgentThinkingToolGroups,
hasAgentExecutedToolsAfter,
hasAgentModelSpecificContext,
@@ -2584,15 +2585,12 @@ export class AgentChat extends Model {
}
if (reasoning === "processing" && this.hasInterveningCard) {
- const L = window.siyuan.languages;
// 与 finishActiveThinking 对齐:先把本张思考卡片的耗时算出来,
// 既用于 DOM 显示「已思考 Xs」,也用于落盘 entry.duration(重载后仍能显示正确耗时)。
const durSec = this.currentThinkingDuration ||
(this.requestStartTime ? (Date.now() - this.requestStartTime) / 1000 : 0);
this.currentThinkingDuration = durSec;
- const doneText = durSec > 0
- ? (L.agentThinkingDoneTime ? L.agentThinkingDoneTime.replace("%s", Math.round(durSec) + "s") : (L.agentThinking || "Thinking"))
- : (L.agentThinking || "Thinking");
+ const doneText = this.formatThinkingHeader(durSec);
const oldCards = this.messagesContainer.querySelectorAll(".agent-chat__msg--thinking:not(.agent-chat__msg--thinking-done)");
for (let i = 0; i < oldCards.length; i++) {
const card = oldCards[i] as HTMLElement;
@@ -3643,8 +3641,9 @@ export class AgentChat extends Model {
// 由 duration 经 i18n 生成"已思考:Xs"标题文本;无 duration 时回退到"思考中..."。
private formatThinkingHeader(duration?: number): string {
const L = window.siyuan.languages;
- if (duration && duration > 0) {
- return L.agentThinkingDoneTime ? L.agentThinkingDoneTime.replace("%s", Math.round(duration) + "s") : (L.agentThinking || "Thinking");
+ const displaySeconds = getAgentThinkingDisplaySeconds(duration);
+ if (displaySeconds !== undefined) {
+ return L.agentThinkingDoneTime ? L.agentThinkingDoneTime.replace("%s", displaySeconds + "s") : (L.agentThinking || "Thinking");
}
return L.agentThinking || "Thinking";
}
@@ -3880,13 +3879,10 @@ export class AgentChat extends Model {
private finishActiveThinking() {
this.stopThinkingTimer();
- const L = window.siyuan.languages;
// 耗时存为数值(用于持久化 entry.duration),"已思考:Xs" 文本只在 DOM 显示、不落盘。
const durSec = this.requestStartTime ? (Date.now() - this.requestStartTime) / 1000 : 0;
this.currentThinkingDuration = durSec;
- const doneText = durSec > 0
- ? (L.agentThinkingDoneTime ? L.agentThinkingDoneTime.replace("%s", Math.round(durSec) + "s") : (L.agentThinking || "Thinking"))
- : (L.agentThinking || "Thinking");
+ const doneText = this.formatThinkingHeader(durSec);
const items = this.messagesContainer.querySelectorAll(
".agent-chat__msg--thinking:not(.agent-chat__msg--thinking-done)"
diff --git a/app/src/layout/dock/agent/AgentHistory.test.ts b/app/src/layout/dock/agent/AgentHistory.test.ts
index 864a2d0bbf..93f57d81ac 100644
--- a/app/src/layout/dock/agent/AgentHistory.test.ts
+++ b/app/src/layout/dock/agent/AgentHistory.test.ts
@@ -4,6 +4,7 @@ import {
applyAgentUserEdit,
buildAgentPresentationEntries,
findAgentUserEntryIndex,
+ getAgentThinkingDisplaySeconds,
getAgentThinkingToolGroups,
hasAgentExecutedToolsAfter,
hasAgentModelSpecificContext,
@@ -25,6 +26,16 @@ describe("AgentHistory", () => {
assert.equal(findAgentUserEntryIndex(entries, "missing"), -1);
});
+ it("displays every positive thinking duration as at least one second", () => {
+ assert.equal(getAgentThinkingDisplaySeconds(undefined), undefined);
+ assert.equal(getAgentThinkingDisplaySeconds(0), undefined);
+ assert.equal(getAgentThinkingDisplaySeconds(-0.1), undefined);
+ assert.equal(getAgentThinkingDisplaySeconds(0.001), 1);
+ assert.equal(getAgentThinkingDisplaySeconds(0.499), 1);
+ assert.equal(getAgentThinkingDisplaySeconds(1.499), 1);
+ assert.equal(getAgentThinkingDisplaySeconds(1.5), 2);
+ });
+
it("detects executed tools after the selected user entry", () => {
assert.equal(hasAgentExecutedToolsAfter([
{id: "user-1", type: "user"},
diff --git a/app/src/layout/dock/agent/AgentHistory.ts b/app/src/layout/dock/agent/AgentHistory.ts
index c460868f53..bfc06578a3 100644
--- a/app/src/layout/dock/agent/AgentHistory.ts
+++ b/app/src/layout/dock/agent/AgentHistory.ts
@@ -32,6 +32,13 @@ export const getAgentThinkingToolGroups = (steps: AgentHistoryThinkingStep[]): s
return steps.map(step => (step.toolNames || []).filter(Boolean));
};
+export const getAgentThinkingDisplaySeconds = (duration?: number): number | undefined => {
+ if (duration === undefined || !Number.isFinite(duration) || duration <= 0) {
+ return undefined;
+ }
+ return Math.max(1, Math.round(duration));
+};
+
export type AgentHistoryReference = { id: string; title: string };
export type AgentHistoryUserEntry = {