Skip to content

A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.

Show the reasoning row while a Bot thinks, with how long it thought

Fei2411d40e2ea1b249
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 2f4be655..0bb06dd8 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -2932,6 +2932,7 @@ var CONVERSATION_CSS = `
.bt-think{font-size:13px;line-height:18px;color:var(--bt-ink-2);margin:2px 0 4px;max-width:min(560px,78%)}
.bt-think summary{cursor:pointer;list-style:none;display:inline-flex;align-items:center;gap:6px}
.bt-think summary::-webkit-details-marker{display:none}
+.bt-think-time{font-variant-numeric:tabular-nums;white-space:nowrap}
/* Five lines of the 18px line height; the rest scrolls inside the box. */
.bt-think-text{white-space:pre-wrap;overflow-wrap:anywhere;margin-top:6px;padding-left:10px;border-left:2px solid var(--bt-line-2);max-height:90px;overflow-y:auto;overscroll-behavior:contain;scrollbar-width:thin;outline:none}
.bt-think-text:focus-visible{border-left-color:var(--bt-ink-2)}
@@ -4183,10 +4184,11 @@ var SECRET_CSS = `
function turnReasoning(steps, current) {
const members = steps.length ? steps : [current];
if (members.find((step) => stepReasoning(step) !== "")?.step !== current.step) return "";
- return members.filter((step) => !thinkingLive(step)).map(stepReasoning).filter(Boolean).join("\n\n");
+ return members.map(stepReasoning).filter(Boolean).join("\n\n");
}
-var texts = (step) => step.blocks.filter((block) => block.kind === "text" && block.text.trim() !== "");
+var thinkingStep = (steps) => steps.find(thinkingLive);
var thinkingLive = (step) => step.status === "running" && texts(step).length === 0 && step.blocks.some((block) => block.kind === "reasoning");
+var texts = (step) => step.blocks.filter((block) => block.kind === "text" && block.text.trim() !== "");
var stepReasoning = (step) => step.blocks.filter((block) => block.kind === "reasoning").map((block) => block.text).join("\n").trim();
// src/client/fold.js
@@ -4397,7 +4399,26 @@ function MessageTools({ text, name, sessionId, react, actions }) {
), document.body) : null
);
}
-function ThinkingRow({ text }) {
+var thinkingTimes = /* @__PURE__ */ new Map();
+function useThinkingMs({ sessionId, turn, settledSteps, skip }) {
+ const key = `${sessionId}:${turn}:${settledSteps}`;
+ const [ms, setMs] = (0, import_react8.useState)(() => thinkingTimes.get(key));
+ (0, import_react8.useEffect)(() => {
+ if (skip || thinkingTimes.has(key)) {
+ setMs(thinkingTimes.get(key));
+ return void 0;
+ }
+ const control = new AbortController();
+ call("thinking", { sessionId, turn }, control.signal).then((value) => {
+ thinkingTimes.set(key, value?.ms);
+ setMs(value?.ms);
+ }).catch(() => {
+ });
+ return () => control.abort();
+ }, [key, skip]);
+ return ms;
+}
+function ThinkingRow({ text, live, at, ms }) {
const box = (0, import_react8.useRef)(null);
const atEnd = (0, import_react8.useRef)(true);
const [open, setOpen] = (0, import_react8.useState)(false);
@@ -4419,11 +4440,17 @@ function ThinkingRow({ text }) {
if (atEnd.current) el.scrollTop = el.scrollHeight;
measure();
}, [text, open]);
- const title = t("Thought it through");
+ const title = live ? t("Thinking") : t("Thought it through");
return (0, import_react8.createElement)(
"details",
{ className: "bt-think", onToggle: toggle },
- (0, import_react8.createElement)("summary", null, title),
+ (0, import_react8.createElement)(
+ "summary",
+ null,
+ title,
+ live && at !== void 0 ? (0, import_react8.createElement)(Elapsed, { at, className: "bt-think-time" }) : null,
+ !live && ms !== void 0 ? (0, import_react8.createElement)("span", { className: "bt-think-time" }, `· ${elapsedLabel(Math.max(1e3, ms))}`) : null
+ ),
(0, import_react8.createElement)("div", { ref: box, className: "bt-think-text", role: "region", "aria-label": title, tabIndex: 0, "data-fade": fade, onScroll: measure }, text)
);
}
@@ -4446,9 +4473,13 @@ function AssistantCell({ node, groupPart, sessionId, openFile, fileMentions, use
const hidden = Boolean(roster.roomsById[sessionId]) || isHiddenTurn(roster, sessionId, data.turn);
const texts2 = groupPart === "reasoning" ? [] : data.blocks.filter((block) => block.kind === "text" && block.text.trim() !== "").map((block) => block.text);
const running = data.status === "running";
+ const members = steps.length ? steps : [data];
+ const reasoning = groupPart === "response" ? "" : turnReasoning(steps, data);
+ const thinking = groupPart === "response" ? void 0 : thinkingStep(members);
+ const ms = useThinkingMs({ sessionId, turn: data.turn, settledSteps: members.filter((step) => step.status !== "running").length, skip: hidden || reasoning === "" });
useLiveNote(actions, sessionId, `text:${node.key ?? data.seq}`, !hidden && running && texts2.length > 0 ? { text: true } : null);
+ useLiveNote(actions, sessionId, `think:${node.key ?? data.seq}`, !hidden && thinking !== void 0 && reasoning !== "" ? { text: true } : null);
if (hidden) return null;
- const reasoning = groupPart === "response" ? "" : turnReasoning(steps, data);
const relays = relayPart(data, groupPart) ? relaysBefore(contexts, steps, data) : [];
if (texts2.length === 0 && !reasoning && relays.length === 0) return null;
const selfId = roster.byId[sessionId]?.id ?? sessionId;
@@ -4468,7 +4499,13 @@ function AssistantCell({ node, groupPart, sessionId, openFile, fileMentions, use
whole: true
});
}),
- reasoning ? (0, import_react8.createElement)(ThinkingRow, { text: reasoning }) : null,
+ reasoning ? (0, import_react8.createElement)(ThinkingRow, {
+ text: reasoning,
+ live: thinking !== void 0,
+ // The live clock counts the settled steps too: backing `at` by the settled
+ // time makes one Elapsed show settled + running.
+ ...thinking !== void 0 ? { at: typeof thinking.time === "number" ? thinking.time - (ms ?? 0) : void 0 } : { ms }
+ }) : null,
texts2.length ? (0, import_react8.createElement)(Bubbles, { texts: texts2, roster, selfId, actions, streaming: running, sessionId, msgKey: `${sessionId}:${data.turn}:${node.key ?? data.seq ?? ""}`, useUi, mentions, fold: FOLD_HUGE }) : null
);
}
diff --git a/ds-bot/index.js b/ds-bot/index.js
index b7dfb260..31a0a815 100644
--- a/ds-bot/index.js
+++ b/ds-bot/index.js
@@ -31,6 +31,7 @@ import * as schedules from './src/host/schedules.js'
import * as usage from './src/host/usage.js'
import * as update from './src/host/update.js'
import * as dshUpdate from './src/host/dsh-update.js'
+import * as thinking from './src/host/thinking.js'
import * as channel from './src/host/channel.js'
import * as feed from './src/host/feed.js'
@@ -43,7 +44,7 @@ export const inject = ['llm', 'tools', 'systemPrompt', 'sessionController', 'ses
// the functions other modules call; they call them through `rt` at run time, never during
// install. Listeners run and tools are listed in registration order, so this order holds:
// usage meters model requests after every listener that rewrites them.
-const MODULES = [diagnostics, store, models, providers, permissions, turns, handoffs, delivery, roster, folders, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, capabilities, schedules, browser, usage, update, dshUpdate, feed, channel]
+const MODULES = [diagnostics, store, models, providers, permissions, turns, handoffs, delivery, roster, folders, agents, prompt, timeContext, tools, chatIndex, groups, approvals, resume, memory, parts, images, secrets, connectors, capabilities, schedules, browser, usage, update, dshUpdate, thinking, feed, channel]
export function apply(ctx, config = {}) {
createRuntime(ctx, config)
diff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.js
index 9694232f..f9ffb47b 100644
--- a/ds-bot/src/client/cells.js
+++ b/ds-bot/src/client/cells.js
@@ -8,8 +8,9 @@ import { colorOf, toolState, BotMark, RoomAvatar } from './mark.js'
import { AnsweredCell, SettledQuestion, PendingQuestion } from './questions.js'
import { SecretToolCell } from './secret-card.js'
import { dateLocale, t } from './i18n.js'
+import { call } from './api.js'
import { failureLabel } from './diagnostics.js'
-import { turnReasoning } from './turn-reasoning.js'
+import { turnReasoning, thinkingStep } from './turn-reasoning.js'
import { FOLD_AT, FOLD_HUGE, stripLongNote, useFold } from './fold.js'
import { heldCardKey } from './sources.js'
@@ -115,10 +116,29 @@ function MessageTools({ text, name, sessionId, react, actions }) {
item(t('Copy'), CopyIcon, copy), item(t('Download'), DownloadIcon, download)), document.body) : null)
}
+// A Turn's settled thinking time, fetched per count of settled steps: while a turn
+// runs each new settled step refetches; scrolling back to a loaded turn reuses it.
+const thinkingTimes = new Map()
+function useThinkingMs({ sessionId, turn, settledSteps, skip }) {
+ const key = `${sessionId}:${turn}:${settledSteps}`
+ const [ms, setMs] = useState(() => thinkingTimes.get(key))
+ useEffect(() => {
+ if (skip || thinkingTimes.has(key)) { setMs(thinkingTimes.get(key)); return undefined }
+ const control = new AbortController()
+ call('thinking', { sessionId, turn }, control.signal)
+ .then(value => { thinkingTimes.set(key, value?.ms); setMs(value?.ms) })
+ .catch(() => {})
+ return () => control.abort()
+ }, [key, skip])
+ return ms
+}
+
// The row starts closed. Few readers go through a whole chain of thought, so when opened
// it shows the last lines and the box scrolls for the rest. It stays at the end as later
-// steps add reasoning, unless the reader has scrolled up.
-export function ThinkingRow({ text }) {
+// steps add reasoning, unless the reader has scrolled up. While a step of the turn is
+// thinking the row shows immediately and ticks (`at` backs the live clock so it counts
+// the settled steps too); settled, it shows the turn's exact thinking time if known.
+export function ThinkingRow({ text, live, at, ms }) {
const box = useRef(null)
const atEnd = useRef(true)
const [open, setOpen] = useState(false)
@@ -140,9 +160,11 @@ export function ThinkingRow({ text }) {
if (atEnd.current) el.scrollTop = el.scrollHeight
measure()
}, [text, open])
- const title = t('Thought it through')
+ const title = live ? t('Thinking') : t('Thought it through')
return h('details', { className: 'bt-think', onToggle: toggle },
- h('summary', null, title),
+ h('summary', null, title,
+ live && at !== undefined ? h(Elapsed, { at, className: 'bt-think-time' }) : null,
+ !live && ms !== undefined ? h('span', { className: 'bt-think-time' }, `· ${elapsedLabel(Math.max(1000, ms))}`) : null),
h('div', { ref: box, className: 'bt-think-text', role: 'region', 'aria-label': title, tabIndex: 0, 'data-fade': fade, onScroll: measure }, text))
}
@@ -168,10 +190,16 @@ export function AssistantCell({ node, groupPart, sessionId, openFile, fileMentio
const hidden = Boolean(roster.roomsById[sessionId]) || isHiddenTurn(roster, sessionId, data.turn)
const texts = groupPart === 'reasoning' ? [] : data.blocks.filter(block => block.kind === 'text' && block.text.trim() !== '').map(block => block.text)
const running = data.status === 'running'
- // A streaming reply stands in for the activity row.
+ const members = steps.length ? steps : [data]
+ const reasoning = groupPart === 'response' ? '' : turnReasoning(steps, data)
+ // A live step of the same Turn keeps the row ticking even when it sits on an
+ // earlier step's cell.
+ const thinking = groupPart === 'response' ? undefined : thinkingStep(members)
+ const ms = useThinkingMs({ sessionId, turn: data.turn, settledSteps: members.filter(step => step.status !== 'running').length, skip: hidden || reasoning === '' })
+ // A streaming reply — or a live reasoning row — stands in for the activity row.
useLiveNote(actions, sessionId, `text:${node.key ?? data.seq}`, !hidden && running && texts.length > 0 ? { text: true } : null)
+ useLiveNote(actions, sessionId, `think:${node.key ?? data.seq}`, !hidden && thinking !== undefined && reasoning !== '' ? { text: true } : null)
if (hidden) return null
- const reasoning = groupPart === 'response' ? '' : turnReasoning(steps, data)
// A relay spliced mid-turn is a 'context' node, hidden from the chat flow on
// DSH 0.2; the step that answers it shows the event line where it was read,
// once, in the step's first cell.
@@ -190,7 +218,12 @@ export function AssistantCell({ node, groupPart, sessionId, openFile, fileMentio
whole: true,
})
}),
- reasoning ? h(ThinkingRow, { text: reasoning }) : null,
+ reasoning ? h(ThinkingRow, {
+ text: reasoning, live: thinking !== undefined,
+ // The live clock counts the settled steps too: backing `at` by the settled
+ // time makes one Elapsed show settled + running.
+ ...(thinking !== undefined ? { at: typeof thinking.time === 'number' ? thinking.time - (ms ?? 0) : undefined } : { ms }),
+ }) : null,
texts.length ? h(Bubbles, { texts, roster, selfId, actions, streaming: running, sessionId, msgKey: `${sessionId}:${data.turn}:${node.key ?? data.seq ?? ''}`, useUi, mentions, fold: FOLD_HUGE }) : null)
}
diff --git a/ds-bot/src/client/styles.js b/ds-bot/src/client/styles.js
index cac722c5..6a0a541b 100644
--- a/ds-bot/src/client/styles.js
+++ b/ds-bot/src/client/styles.js
@@ -807,6 +807,7 @@ export const CONVERSATION_CSS = `
.bt-think{font-size:13px;line-height:18px;color:var(--bt-ink-2);margin:2px 0 4px;max-width:min(560px,78%)}
.bt-think summary{cursor:pointer;list-style:none;display:inline-flex;align-items:center;gap:6px}
.bt-think summary::-webkit-details-marker{display:none}
+.bt-think-time{font-variant-numeric:tabular-nums;white-space:nowrap}
/* Five lines of the 18px line height; the rest scrolls inside the box. */
.bt-think-text{white-space:pre-wrap;overflow-wrap:anywhere;margin-top:6px;padding-left:10px;border-left:2px solid var(--bt-line-2);max-height:90px;overflow-y:auto;overscroll-behavior:contain;scrollbar-width:thin;outline:none}
.bt-think-text:focus-visible{border-left-color:var(--bt-ink-2)}
diff --git a/ds-bot/src/client/turn-reasoning.js b/ds-bot/src/client/turn-reasoning.js
index 8caa04b9..609af74d 100644
--- a/ds-bot/src/client/turn-reasoning.js
+++ b/ds-bot/src/client/turn-reasoning.js
@@ -1,14 +1,17 @@
-// Settled reasoning of every loaded step in a Turn, shown once on the first step that has
-// reasoning: only steps with reasoning get a reasoning part to render it. A step still
-// thinking stays out until it settles; the activity row shows it meanwhile.
+// Settled and live reasoning of every loaded step in a Turn, shown once on the first
+// step that has reasoning: only steps with reasoning get a reasoning part to render it.
export function turnReasoning(steps, current) {
const members = steps.length ? steps : [current]
if (members.find(step => stepReasoning(step) !== '')?.step !== current.step) return ''
- return members.filter(step => !thinkingLive(step)).map(stepReasoning).filter(Boolean).join('\n\n')
+ return members.map(stepReasoning).filter(Boolean).join('\n\n')
}
-const texts = step => step.blocks.filter(block => block.kind === 'text' && block.text.trim() !== '')
+// A step still streaming reasoning with no reply text yet is thinking: the row
+// counts its clock from `step.time`, the time its first visible chunk arrived.
+export const thinkingStep = steps => steps.find(thinkingLive)
const thinkingLive = step => step.status === 'running' && texts(step).length === 0 && step.blocks.some(block => block.kind === 'reasoning')
+const texts = step => step.blocks.filter(block => block.kind === 'text' && block.text.trim() !== '')
+
const stepReasoning = step => step.blocks.filter(block => block.kind === 'reasoning').map(block => block.text).join('\n').trim()
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js
index 306e9e13..f32cb2c2 100644
--- a/ds-bot/src/host/channel.js
+++ b/ds-bot/src/host/channel.js
@@ -82,7 +82,7 @@ export function install(rt) {
})
// What a host that only reads the team may still answer.
// Updates change the dsh profile, not the team, so a reading host may run them too.
- const READS = new Set(['state', 'models', 'efforts', 'providers', 'provider-models', 'earlier', 'chat', 'run-stats', 'room-progress', 'exchange', 'exchange-text', 'file-text', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer'])
+ const READS = new Set(['state', 'models', 'efforts', 'providers', 'provider-models', 'earlier', 'chat', 'run-stats', 'room-progress', 'exchange', 'exchange-text', 'file-text', 'usage', 'schedules', 'memory-view', 'browser-wait', 'browser-result', 'diagnostics', 'diagnostics-log', 'diagnostics-clear', 'update-check', 'update-install', 'dsh-update-install', 'repo-stars', 'approval-answer', 'thinking'])
function setPrefs(body) {
if (body.theme !== undefined) {
@@ -121,6 +121,16 @@ export function install(rt) {
return ok({ modelsKey: modelsKey() })
case 'efforts':
return ok(await rt.reasoningOf(String(body.ref ?? '')) ?? { efforts: [] })
+ // A Turn's exact thinking time, from the timed stream in its events; `ms`
+ // omitted when the stream carries no reasoning records.
+ case 'thinking': {
+ const sessionId = String(body.sessionId ?? '')
+ const turn = Number(body.turn)
+ if (!Number.isFinite(turn) || rt.selfOf(sessionId) === undefined) return ok(null)
+ await rt.load()
+ const ms = await rt.thinkingOf(sessionId, turn)
+ return ok(ms === undefined ? {} : { ms })
+ }
// Model providers and their keys, for a window without DSH's own Models page.
case 'providers':
return ok(await rt.providersView())
diff --git a/ds-bot/src/host/thinking.js b/ds-bot/src/host/thinking.js
new file mode 100644
index 00000000..eca8d650
--- /dev/null
+++ b/ds-bot/src/host/thinking.js
@@ -0,0 +1,55 @@
+// The exact thinking time of a Turn, read from the timed model stream DSH keeps in
+// each assistant/message event: for one step, its last reasoning chunk's time minus
+// its first one's, summed over the turn. assistant/attempt events are skipped on
+// purpose — a failed or retried attempt's thinking never reached the reply.
+import { HISTORY_TIMEOUT_MS } from './constants.js'
+
+export function thinkingMs(events, turn) {
+ let total = 0
+ let timed = false
+ for (const event of events) {
+ if (event.type !== 'assistant/message' || event.data.turn !== turn) continue
+ const span = streamReasoningMs(event.data.stream)
+ if (span === undefined) continue
+ timed = true
+ total += span
+ }
+ return timed ? total : undefined
+}
+
+// One stream's reasoning span. A reasoning-chunks record packs its member times as
+// the first time plus the gaps to each next member, so its last member lands at
+// time0 + sum(dt).
+const streamReasoningMs = (stream) => {
+ let first
+ let last
+ for (const record of stream ?? []) {
+ if (record.type !== 'reasoning-chunks' || record.texts.length === 0) continue
+ const end = record.dt.reduce((time, gap) => time + gap, record.time0)
+ if (first === undefined || record.time0 < first) first = record.time0
+ if (last === undefined || end > last) last = end
+ }
+ return first === undefined ? undefined : last - first
+}
+
+export function install(rt) {
+ // A closed turn's events do not change; cache its answer once turn/end exists.
+ const settled = new Map()
+ rt.thinkingOf = async (sessionId, turn) => {
+ const key = `${sessionId}:${turn}`
+ if (settled.has(key)) return settled.get(key)
+ const events = []
+ // Newest first; events older than the turn close the read.
+ for await (const event of rt.eventsBackwards(sessionId, AbortSignal.timeout(HISTORY_TIMEOUT_MS))) {
+ events.push(event)
+ if (typeof event.data?.turn === 'number' && event.data.turn < turn) break
+ }
+ // The read went through every newer turn whole, so each closed one is answered too.
+ for (const event of events) {
+ if (event.type === 'turn/end' && typeof event.data?.turn === 'number' && event.data.turn >= turn) {
+ settled.set(`${sessionId}:${event.data.turn}`, thinkingMs(events, event.data.turn))
+ }
+ }
+ return settled.has(key) ? settled.get(key) : thinkingMs(events, turn)
+ }
+}
diff --git a/ds-bot/test/chat-rows.test.js b/ds-bot/test/chat-rows.test.js
index 36f2d7e4..9b568a11 100644
--- a/ds-bot/test/chat-rows.test.js
+++ b/ds-bot/test/chat-rows.test.js
@@ -65,3 +65,21 @@ test('the reasoning row is a closed details that opens to the text', () => {
assert.equal(title.children.join(''), 'Thought it through')
assert.equal(box.props.className, 'bt-think-text')
})
+
+test('a live reasoning row titles "Thinking" with a ticking clock', () => {
+ const row = ThinkingRow({ text: 'one\ntwo', live: true, at: Date.now() - 4000 })
+ const title = row.children[0]
+ assert.equal(title.children[0], 'Thinking')
+ assert.equal(title.children[1]?.type?.name, 'Elapsed')
+ assert.equal(title.children[1]?.props.at <= Date.now() - 4000, true)
+})
+
+test('a settled reasoning row shows the turn’s thinking time when known', () => {
+ const timed = ThinkingRow({ text: 'one\ntwo', ms: 12_000 })
+ const [title] = timed.children
+ assert.equal(title.children[0], 'Thought it through')
+ const time = title.children.find(child => child?.type === 'span')
+ assert.equal(time?.children.join(''), '· 12s')
+ const untimed = ThinkingRow({ text: 'one\ntwo' })
+ assert.equal(untimed.children[0].children.join(''), 'Thought it through')
+})
diff --git a/ds-bot/test/thinking.test.js b/ds-bot/test/thinking.test.js
new file mode 100644
index 00000000..f375ed31
--- /dev/null
+++ b/ds-bot/test/thinking.test.js
@@ -0,0 +1,154 @@
+import { test } from 'node:test'
+import assert from 'node:assert/strict'
+import { install as installChannel } from '../src/host/channel.js'
+import { install as installThinking, thinkingMs } from '../src/host/thinking.js'
+
+const reasoningRun = (time0, gaps, texts) => ({ type: 'reasoning-chunks', time0, index: 0, dt: gaps, texts })
+const textRun = (time0, gaps, texts) => ({ type: 'text-chunks', time0, index: 0, dt: gaps, texts })
+const message = (turn, step, stream, extra = {}) => ({ seq: turn * 10 + step, type: 'assistant/message', data: { turn, step, message: { content: [] }, stream, ...extra } })
+
+test('a Turn’s thinking time sums its steps’ reasoning spans', () => {
+ const events = [
+ message(1, 1, [reasoningRun(1000, [500, 200], ['a', 'b', 'c']), textRun(1800, [100], ['reply'])]),
+ message(1, 2, [reasoningRun(3000, [900], ['x', 'y']), textRun(4000, [], ['done'])]),
+ ]
+ // Step 1: 1000+500+200−1000 = 700; step 2: 3000+900−3000 = 900.
+ assert.equal(thinkingMs(events, 1), 1600)
+})
+
+test('a step without reasoning records contributes 0, other turn events are ignored', () => {
+ const events = [
+ message(1, 1, [textRun(1000, [50], ['straight to text'])]),
+ { type: 'tool/call', data: { turn: 1, step: 1 } },
+ message(1, 2, [reasoningRun(3000, [500], ['a', 'b'])]),
+ message(2, 1, [reasoningRun(5000, [1000], ['other', 'turn'])]),
+ ]
+ assert.equal(thinkingMs(events, 1), 500)
+})
+
+test('an interrupted message still counts its streamed reasoning', () => {
+ const events = [message(1, 1, [reasoningRun(1000, [400, 300], ['a', 'b', 'c'])], { interrupted: true })]
+ assert.equal(thinkingMs(events, 1), 700)
+})
+
+test('assistant/attempt streams are skipped', () => {
+ const events = [
+ { type: 'assistant/attempt', data: { turn: 1, step: 1, stream: [reasoningRun(1000, [9000], ['failed', 'try'])] } },
+ message(1, 1, [reasoningRun(2000, [100], ['ok', 'now'])]),
+ ]
+ assert.equal(thinkingMs(events, 1), 100)
+})
+
+test('a turn with reasoning text but no stream records is unknown, not 0', () => {
+ const events = [message(1, 1, undefined)]
+ assert.equal(thinkingMs(events, 1), undefined)
+ assert.equal(thinkingMs([{ type: 'assistant/message', data: { turn: 1, step: 1, message: { content: [{ type: 'reasoning', text: 'old' }] } } }], 1), undefined)
+})
+
+test('several reasoning runs in one step span first to last', () => {
+ const events = [message(1, 1, [
+ reasoningRun(1000, [100], ['a', 'b']),
+ textRun(2000, [50], ['tool intro']),
+ reasoningRun(5000, [200], ['c', 'd']),
+ ])]
+ assert.equal(thinkingMs(events, 1), 4200)
+})
+
+test('thinkingOf caches only a closed turn', async () => {
+ let pages = 0
+ const rt = {
+ eventsBackwards: async function* () {
+ pages += 1
+ for (const event of [...EVENTS].reverse()) yield event
+ },
+ }
+ installThinking(rt)
+ // Open turn: computed each call.
+ assert.equal(await rt.thinkingOf('s', 1), 1000)
+ assert.equal(await rt.thinkingOf('s', 1), 1000)
+ assert.equal(pages, 2)
+ EVENTS.push({ type: 'turn/end', data: { turn: 1 } })
+ assert.equal(await rt.thinkingOf('s', 1), 1000)
+ assert.equal(await rt.thinkingOf('s', 1), 1000)
+ assert.equal(pages, 3)
+})
+
+test('reading an older turn also answers the closed turns read on the way', async () => {
+ const events = [
+ message(1, 1, [reasoningRun(1000, [1000], ['a', 'b'])]), { type: 'turn/end', data: { turn: 1 } },
+ message(2, 1, [reasoningRun(5000, [2000], ['a', 'b'])]), { type: 'turn/end', data: { turn: 2 } },
+ message(3, 1, [reasoningRun(9000, [3000], ['a', 'b'])]),
+ ]
+ let pages = 0
+ const rt = { eventsBackwards: async function* () { pages += 1; for (const event of [...events].reverse()) yield event } }
+ installThinking(rt)
+ assert.equal(await rt.thinkingOf('s', 1), 1000)
+ assert.equal(await rt.thinkingOf('s', 2), 2000)
+ assert.equal(pages, 1)
+ assert.equal(await rt.thinkingOf('s', 3), 3000)
+ assert.equal(pages, 2)
+})
+
+const EVENTS = [message(1, 1, [reasoningRun(1000, [400, 600], ['a', 'b', 'c'])])]
+
+const api = (handlers = {}) => {
+ let route
+ const ctx = {
+ logger: { warn() {}, info() {} },
+ get: () => undefined,
+ on: () => () => {},
+ effect: execute => { execute() },
+ tools: { register: () => () => {}, guard: () => () => {}, schemas: () => [] },
+ systemPrompt: { section: () => () => {} },
+ connection: { fetch: { register(r) { route = r; return () => {} } } },
+ llm: { listProviders: () => [] },
+ }
+ const rt = {
+ ctx,
+ state: { revision: 0, bots: {}, rooms: {}, exchanges: [], questions: {}, agents: {}, secretRequests: {}, prefs: {} },
+ save: async () => {},
+ refresh: async () => {},
+ readOnly: () => undefined,
+ load: async () => {},
+ bootstrap: async () => {},
+ catalog: async () => {},
+ loadSecrets: async () => {},
+ cachedCatalog: () => [],
+ cachedDefault: () => undefined,
+ reasoningOf: () => undefined,
+ providersView: async () => ({}),
+ secretsView: () => ({}),
+ capabilityLibraryView: () => ({}),
+ activityView: () => ({}),
+ workView: () => ({}),
+ roomWorkView: () => ({}),
+ approvalsView: () => ({}),
+ waitingView: () => ({}),
+ turnsView: () => ({}),
+ memoryNews: () => ({}),
+ memoryReviews: () => ({}),
+ connectorsView: () => ({}),
+ capabilityStatusesView: () => ({}),
+ selfOf: id => handlers.selfOf?.(id),
+ thinkingOf: handlers.thinkingOf ?? (async () => undefined),
+ warn() {},
+ }
+ installChannel(rt)
+ const post = async (endpoint, payload) => {
+ const response = await route.fetch({ json: async () => ({ endpoint, payload }), signal: new AbortController().signal })
+ return response.json()
+ }
+ return { post }
+}
+
+test('the thinking endpoint answers ms only for a Bot session', async () => {
+ const { post } = api({ selfOf: id => (id === 'bot-chat' ? 'bot-1' : undefined), thinkingOf: async () => 12000 })
+ assert.deepEqual(await post('thinking', { sessionId: 'bot-chat', turn: 3 }), { ok: true, value: { ms: 12000 } })
+ assert.deepEqual(await post('thinking', { sessionId: 'elsewhere', turn: 3 }), { ok: true, value: null })
+ assert.deepEqual(await post('thinking', { sessionId: 'bot-chat', turn: 'x' }), { ok: true, value: null })
+})
+
+test('the thinking endpoint omits ms when the time is unknown', async () => {
+ const { post } = api({ selfOf: () => 'bot-1', thinkingOf: async () => undefined })
+ assert.deepEqual(await post('thinking', { sessionId: 's', turn: 1 }), { ok: true, value: {} })
+})
diff --git a/ds-bot/test/turn-reasoning.test.js b/ds-bot/test/turn-reasoning.test.js
index 0dcc8995..d77556cb 100644
--- a/ds-bot/test/turn-reasoning.test.js
+++ b/ds-bot/test/turn-reasoning.test.js
@@ -1,6 +1,6 @@
import test from 'node:test'
import assert from 'node:assert/strict'
-import { turnReasoning } from '../src/client/turn-reasoning.js'
+import { thinkingStep, turnReasoning } from '../src/client/turn-reasoning.js'
const step = (n, blocks, status = 'settled') => ({ turn: 1, step: n, status, blocks })
const think = text => ({ kind: 'reasoning', text })
@@ -18,14 +18,25 @@ test('the row sits on the first step with reasoning', () => {
assert.deepEqual(steps.map(current => turnReasoning(steps, current)), ['', 'B\n\nC', ''])
})
-test('reasoning still in progress stays out until its step settles', () => {
+test('reasoning still in progress shows while its step thinks', () => {
const steps = [step(1, [think('A'), tool]), step(2, [think('B')], 'running')]
- assert.equal(turnReasoning(steps, steps[0]), 'A')
- assert.equal(turnReasoning([step(1, [think('A')], 'running')], step(1, [think('A')], 'running')), '')
+ assert.equal(turnReasoning(steps, steps[0]), 'A\n\nB')
+ assert.equal(turnReasoning([step(1, [think('A')], 'running')], step(1, [think('A')], 'running')), 'A')
steps[1].blocks.push({ kind: 'text', text: 'Reply' })
assert.equal(turnReasoning(steps, steps[0]), 'A\n\nB')
})
+test('the live step is the one still thinking', () => {
+ const steps = [step(1, [think('A'), tool]), step(2, [think('B')], 'running')]
+ assert.equal(thinkingStep(steps), steps[1])
+ // Reasoning that moved on to its reply no longer ticks.
+ steps[1].blocks.push({ kind: 'text', text: 'Reply' })
+ assert.equal(thinkingStep(steps), undefined)
+ // A running step without reasoning yet is not thinking either.
+ assert.equal(thinkingStep([step(3, [tool], 'running')]), undefined)
+ assert.equal(thinkingStep(steps.slice(0, 1)), undefined)
+})
+
test('a step outside the loaded Turn list shows only its own reasoning', () => {
const current = step(1, [think(' A ')])
assert.equal(turnReasoning([], current), 'A')