A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Restore display view cards when replaying chat history
klarkxya684c80c3384
diff --git a/ds-bot/client.js b/ds-bot/client.jsindex 662dd1c1..e998db10 100644--- a/ds-bot/client.js+++ b/ds-bot/client.js@@ -5365,6 +5365,9 @@ function chatRow(row, key, { sessionId, selfId, roster, actions, useUi }) {return (0, import_react9.createElement)(EarlierUser, { key, row });case "answer":return (0, import_react9.createElement)(AnsweredReply, { key, node: { data: row } });+ // A display-only view replays from the blueprint the host kept on the call.+ case "view":+ return (0, import_react9.createElement)(WidgetCell, { key, sessionId, callId: row.callId, args: { text: (name) => name === "title" ? row.title : void 0, value: (name) => name === "view" ? row.view : void 0 }, done: true, isError: false, roster, actions });case "bot":return (0, import_react9.createElement)(Bubbles, { key, texts: row.texts, roster, selfId, actions, sessionId: useUi ? sessionId : void 0, msgKey: useUi ? `${sessionId}:${row.time}` : void 0, useUi, fold: FOLD_HUGE });case "event": {diff --git a/ds-bot/dev/mock-llm.mjs b/ds-bot/dev/mock-llm.mjsindex 2ccd4dac..c650e4d8 100644--- a/ds-bot/dev/mock-llm.mjs+++ b/ds-bot/dev/mock-llm.mjs@@ -501,7 +501,9 @@ function decide(body) {})]}if (/^\s*大视图/.test(userText) && tools.has('show_view')) {- return [tool('show_view', { title: 'A long note', view: { type: 'text', text: 'x'.repeat(7800) }, await_reply: false })]+ // Big but valid: every text node stays under the component limit, the whole+ // blueprint just under the byte limit.+ return [tool('show_view', { title: 'A long note', view: { type: 'stack', children: [0, 1, 2, 3].map(() => ({ type: 'text', text: 'x'.repeat(1900) })) }, await_reply: false })]}// Streams slowly, then checks the team: exercises the activity row stepping aside// for a streaming reply and coming back for the tool.diff --git a/ds-bot/dev/smoke.mjs b/ds-bot/dev/smoke.mjsindex d6251b4d..914a2f81 100644--- a/ds-bot/dev/smoke.mjs+++ b/ds-bot/dev/smoke.mjs@@ -217,7 +217,8 @@ await step('the view answer reaches Writer and clears the card', () => {}, (_value, calls) => {const chat = botNamed('Writer').sessionId ?? botNamed('Writer').idreturn expect(state().views?.[chat] === undefined, 'the view is still open')- ?? expect(calls.some(call => call.last?.startsWith('{"kind":"answer_to_pending_view"')), 'the answer did not arrive as the Bot\'s next message')+ // DSH may prepend whitespace to the reply payload.+ ?? expect(calls.some(call => call.last?.trimStart().startsWith('{"kind":"answer_to_pending_view"')), 'the answer did not arrive as the Bot\'s next message')})// A display-only view just under the byte limit lives on the recorded call; re-reading@@ -225,10 +226,10 @@ await step('the view answer reaches Writer and clears the card', () => {await step('Writer shows a view just under the byte limit', () => send(botNamed('Writer').id, '大视图'), (_value, calls) => {return expect(calls.some(call => (call.out ?? []).some(block => block.type === 'tool_use' && block.name === 'show_view')), 'Writer did not call show_view')})-await step('the big view survives a re-read of the part', () => api('earlier', { sessionId: botNamed('Writer').sessionId ?? botNamed('Writer').id }), (page) => {- const found = (page?.items ?? []).find(item => JSON.stringify(item).includes('show_view'))- return expect(found !== undefined, `no show_view call in the part (${JSON.stringify(page?.items?.length)})`)- ?? expect(JSON.stringify(found).length > 7500, `the call came back without its view (${JSON.stringify(found).length} chars)`)+await step('the big view survives a re-read of the part', () => api('chat', { botId: botNamed('Writer').id }), (page) => {+ const found = (page?.items ?? []).find(item => item.kind === 'view' && item.title === 'A long note')+ return expect(found !== undefined, `no view card in the part (${JSON.stringify(page?.items?.length)})`)+ ?? expect(found.view?.children?.length === 4 && found.view.children.every(node => node.type === 'text' && node.text === 'x'.repeat(1900)), 'the view came back without its complete blueprint')})await step('user creates Coder, who keeps the default model', () => api('create-bot', { name: 'Coder', role: 'Engineer', instructions: 'Write code.' }), (bot) => {@@ -305,6 +306,15 @@ await step('Writer searches its own chat across parts', () => send(botNamed('Wri// `earlier` pages backwards from the part the chat shows now.await step('earlier part pages load', () => api('earlier', { sessionId: botNamed('Writer').sessionId }), page => expect(Array.isArray(page?.items) && page.items.some(item => item.kind === 'user'), `no user lines in the part before (${JSON.stringify(page).slice(0, 80)})`))+await step('the big view survives the switch to an earlier part', async () => {+ let cursor+ do {+ const page = await api('earlier', { sessionId: botNamed('Writer').sessionId, cursor })+ const found = page.items.find(item => item.kind === 'view' && item.title === 'A long note')+ if (found) return found+ cursor = page.cursor+ } while (cursor)+}, found => expect(found?.view?.children?.length === 4 && found.view.children.every(node => node.type === 'text' && node.text === 'x'.repeat(1900)), 'the earlier part lost the view blueprint'))// Work still under way (the mock steward reads 阶段未完 in the latest user message): the// part is condensed in place with DS Bot's checkpoint, not switched, and the condenseddiff --git a/ds-bot/src/client/cells.js b/ds-bot/src/client/cells.jsindex 88522089..f3b7535d 100644--- a/ds-bot/src/client/cells.js+++ b/ds-bot/src/client/cells.js@@ -416,6 +416,8 @@ function chatRow(row, key, { sessionId, selfId, roster, actions, useUi }) {case 'divider': return h(PartLine, { key, time: row.time })case 'user': return h(EarlierUser, { key, row })case 'answer': return h(AnsweredReply, { key, node: { data: row } })+ // A display-only view replays from the blueprint the host kept on the call.+ case 'view': return h(WidgetCell, { key, sessionId, callId: row.callId, args: { text: name => name === 'title' ? row.title : undefined, value: name => name === 'view' ? row.view : undefined }, done: true, isError: false, roster, actions })case 'bot': return h(Bubbles, { key, texts: row.texts, roster, selfId, actions, sessionId: useUi ? sessionId : undefined, msgKey: useUi ? `${sessionId}:${row.time}` : undefined, useUi, fold: FOLD_HUGE })case 'event': {if (row.role === 'report') return h(GroupEvent, { key, prefix: t('Replies from'), room: roster.roomsById[row.roomId], name: t('a group chat'), roster, actions })diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.jsindex d7222f58..6cb102e4 100644--- a/ds-bot/src/host/parts.js+++ b/ds-bot/src/host/parts.js@@ -20,7 +20,7 @@ import { HANDOFF_ACK, HISTORY_TIMEOUT_MS, REPLY_KIND, SOURCE_KIND } from './consimport { CHECKPOINT_PROMPT, STEWARD_SYSTEM, checkpointCount, checkpointTail, checkpointTailNote, checkpointText, linesFor, parseDecision, stewardRequest } from './steward.js'import { RESUME_DELAY_MS } from './resume.js'import { clip, createClock, escapeRegExp, estimateTokens, splitRef, textOf } from './text.js'-import { validateViewAnswer, viewSummary } from '../shared/views.js'+import { checkShownView, normalizeView, validateViewAnswer, viewSummary } from '../shared/views.js'// An ask_user card as the user saw it. The user's reply is often just an option label,// which means nothing without the card. Arguments the tool would refuse (no question,@@ -1226,6 +1226,20 @@ export function install(rt) {}continue}+ if (event.type === 'tool/call') {+ // Hidden turns and refused calls never showed a display card. Interactive+ // cards live in the pending dock or on their answer message instead.+ if (event.data?.name === 'show_view' && !hidden.has(event.data?.turn)) {+ let args = event.data?.arguments+ if (typeof args === 'string') { try { args = JSON.parse(args) } catch { args = undefined } }+ const view = normalizeView(args?.view)+ const shown = checkShownView(args?.title, view)+ if (shown.error === undefined && !shown.interactive && args?.await_reply !== true) {+ items.push({ kind: 'view', title: shown.title, view, callId: event.data?.callId ?? null, time: event.time })+ }+ }+ continue+ }if (event.type !== 'user/message') continueconst source = event.data?.sourceif (source?.kind === 'user') {diff --git a/ds-bot/test/chat-rows.test.js b/ds-bot/test/chat-rows.test.jsindex 5bfd01ed..afd0eb7c 100644--- a/ds-bot/test/chat-rows.test.js+++ b/ds-bot/test/chat-rows.test.js@@ -11,7 +11,7 @@ const bundle = await build({})const module = { exports: {} }runInNewContext(bundle.outputFiles[0].text, {- module, exports: module.exports, Intl, Date,+ module, exports: module.exports, Intl, Date, TextEncoder,require: (name) => {const platform = {react: {@@ -43,6 +43,20 @@ test('a Bot\'s messages in a row are one block', () => {assert.deepEqual(shape(ChatRows({ items, sessionId: 'b', roster, actions: {} })), ['EarlierUser', 'bot:one+two', 'EarlierUser', 'bot:three'])})+test('a view row renders as a view card from its blueprint', () => {+ const items = [+ { kind: 'user', text: 'show me', time: MINUTE },+ { kind: 'view', title: 'A long note', view: { type: 'stack', children: [0, 1, 2, 3].map(() => ({ type: 'text', text: 'x'.repeat(1900) })) }, callId: 'c1', time: 2 * MINUTE },+ { kind: 'bot', text: 'done', time: 3 * MINUTE },+ ]+ const rows = [...ChatRows({ items, sessionId: 'b', roster, actions: {} })]+ assert.deepEqual(shape(rows), ['EarlierUser', 'WidgetCell', 'bot:done'])+ // The card draws the blueprint, not nothing: run the cell and look inside.+ const cell = rows[1]+ const rendered = cell.type(cell.props)+ assert.equal(rendered.props.className, 'bt-q-inline')+})+test('with the clock, a time separator sits above the first entry and after each pause of half an hour', () => {const items = [{ kind: 'divider', time: 0 },diff --git a/ds-bot/test/feed.test.js b/ds-bot/test/feed.test.jsindex d664c5d7..ca3a1e7b 100644--- a/ds-bot/test/feed.test.js+++ b/ds-bot/test/feed.test.js@@ -162,6 +162,75 @@ test('the chat endpoint pages a Bot\'s own chat from the newest part back', asynassert.equal((await call('chat', { botId: 'nobody' })).error.code, 'bot/not-found')})+test('a show_view call in a part reads back as a view row with its blueprint', async (t) => {+ const record = (event, seq) => ({ event: { ...event, seq } })+ const showView = (time, args) => ({ type: 'tool/call', time, data: { callId: 'c1', name: 'show_view', arguments: typeof args === 'string' ? args : JSON.stringify(args) } })+ const { call, stop } = start({+ pages: {+ 'm1c:end': {+ records: [+ record(userMessage('look', 70), 41),+ record(showView(71, { title: 'A long note', view: { type: 'stack', children: [0, 1, 2, 3].map(() => ({ type: 'text', text: 'x'.repeat(1900) })) } }), 42),+ // No title: the call showed no card, so it leaves no row. Other tools stay out too.+ record(showView(72, { view: { type: 'text', text: 'y' } }), 43),+ record({ type: 'tool/call', time: 73, data: { callId: 'c3', name: 'list_bots', arguments: '{}' } }, 44),+ ],+ hasMore: false,+ },+ },+ })+ t.after(stop)+ await call('state')+ const page = (await call('chat', { botId: 'm1' })).value+ const row = page.items.find(item => item.kind === 'view')+ assert.equal(row.title, 'A long note')+ assert.equal(row.callId, 'c1')+ assert.equal(row.view.children.length, 4)+ assert.equal(JSON.stringify(row.view).length > 7500, true)+ assert.deepEqual(page.items.map(item => item.kind), ['divider', 'user', 'view'])+})++test('view replay omits hidden, refused and interactive calls in pages and the live feed', async (t) => {+ const view = { type: 'text', text: 'Visible' }+ const cases = [+ { turn: 7, arguments: { title: 'Hidden', view } },+ { arguments: { title: 'Refused wait', view, await_reply: true } },+ { arguments: { title: 'Too long', view: { type: 'text', text: 'x'.repeat(7800) } } },+ { arguments: { title: 'Interactive', view: { type: 'confirm', id: 'ok' } } },+ { arguments: '{invalid' },+ { arguments: { title: ' Accepted ', view: { type: 'progress', percent: '40' } } },+ ]+ const events = cases.map((data, index) => ({+ type: 'tool/call', time: 70 + index, seq: 41 + index,+ data: { turn: 8, callId: `v${index}`, name: 'show_view', ...data },+ }))+ const { ctx, call, stop, routes } = start({ pages: { 'm1c:end': { records: events.map(event => ({ event })), hasMore: false } } })+ t.after(stop)+ await call('state')+ const expected = { kind: 'view', title: 'Accepted', view: { type: 'progress', percent: 40 }, callId: 'v5', time: 75 }+ const page = (await call('chat', { botId: 'm1' })).value+ assert.deepEqual(page.items.filter(item => item.kind === 'view'), [expected])++ const abort = new AbortController()+ t.after(() => abort.abort())+ const response = await routes['/api/bot/feed'].fetch(new Request('http://localhost/api/bot/feed', { signal: abort.signal }))+ const next = reader(response)+ assert.equal((await next()).event, 'hello')+ const onEvent = ctx.listeners.get('session/event').at(-1)+ for (const event of events) onEvent({ id: 'm1c' }, event)+ assert.deepEqual(await next(), { event: 'message', data: { botId: 'm1', sessionId: 'm1c', item: expected } })+})++test('a display view is retained when its part becomes earlier history', async (t) => {+ const view = { type: 'text', text: 'From the first part' }+ const event = { type: 'tool/call', time: 20, seq: 3, data: { turn: 1, callId: 'old-view', name: 'show_view', arguments: JSON.stringify({ title: 'Earlier card', view }) } }+ const { call, stop } = start({ pages: { 'm1:end': { records: [{ event }], hasMore: false } } })+ t.after(stop)+ await call('state')+ const page = (await call('earlier', { sessionId: 'm1c' })).value+ assert.deepEqual(page.items, [{ kind: 'view', title: 'Earlier card', view, callId: 'old-view', time: 20 }])+})+test('the run-stats endpoint reads DSH\'s figures for the current part of a Bot\'s chat', async (t) => {const views = { sessionStats: { turns: 3 }, tokenUsage: { cacheReadTokens: 9 }, contextPressure: { contextWindow: 100 } }const read = []