Skip to content

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

Add a live feed and a chat endpoint for a window that draws the team itself

Fei2411c3ce9f4390d9
diff --git a/app/src/preload.cjs b/app/src/preload.cjs
deleted file mode 100644
index acc18b39..00000000
--- a/app/src/preload.cjs
+++ /dev/null
@@ -1,9 +0,0 @@
-// Gives the DSH Web entry the boot handshake it uses inside an app window.
-const { contextBridge, ipcRenderer } = require('electron')
-
-if (location.protocol === 'dsh-app:' && location.hostname === 'app') {
- contextBridge.exposeInMainWorld('dshDesktopBoot', {
- ready: () => ipcRenderer.invoke('ds-bot:boot'),
- failed: (message) => ipcRenderer.invoke('ds-bot:boot-failed', String(message)),
- })
-}
diff --git a/ds-bot/index.js b/ds-bot/index.js
index 7ba683cf..59104ef5 100644
--- a/ds-bot/index.js
+++ b/ds-bot/index.js
@@ -31,6 +31,7 @@ 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 channel from './src/host/channel.js'
+import * as feed from './src/host/feed.js'
export { COLORS } from './src/host/constants.js'
@@ -41,7 +42,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, 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, channel]
+const MODULES = [diagnostics, store, models, 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]
export function apply(ctx, config = {}) {
createRuntime(ctx, config)
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js
index 9df58a62..7728a237 100644
--- a/ds-bot/src/host/channel.js
+++ b/ds-bot/src/host/channel.js
@@ -80,7 +80,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', 'earlier', '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', 'earlier', 'chat', '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'])
function setPrefs(body) {
if (body.theme !== undefined) {
@@ -146,6 +146,9 @@ export function install(rt) {
case 'earlier':
await rt.load()
return ok(await rt.earlierPage(String(body.sessionId ?? ''), body.cursor ?? undefined))
+ case 'chat':
+ await rt.load()
+ return ok(await rt.chatPage(String(body.botId ?? ''), body.cursor ?? undefined))
case 'usage':
await rt.load()
return ok(await rt.usageView({ timeZone: typeof body.timeZone === 'string' ? body.timeZone.slice(0, 64) : undefined }))
diff --git a/ds-bot/src/host/feed.js b/ds-bot/src/host/feed.js
new file mode 100644
index 00000000..982a457e
--- /dev/null
+++ b/ds-bot/src/host/feed.js
@@ -0,0 +1,108 @@
+// The live feed: a Server-Sent Events stream of what changes, for a window that draws
+// the team itself (the desktop app) instead of polling.
+// revision the team was saved: ask for `state` again
+// live work changed without a new revision: ask for `state` again
+// message a new entry of a Bot's own chat, as the `chat` endpoint returns it
+// stream the reply a Bot is writing: a start, its text as it comes, an end
+import { ROUTE } from './constants.js'
+
+export const FEED_ROUTE = `${ROUTE}/feed`
+// A comment line now and then keeps an idle connection open.
+const KEEPALIVE_MS = 20_000
+// Status changes come in bursts (a turn starts several Sessions); one notice covers them.
+const LIVE_DELAY_MS = 150
+
+export function install(rt) {
+ const { ctx, state } = rt
+ const encoder = new TextEncoder()
+ /** @type {Set<ReadableStreamDefaultController<Uint8Array>>} */
+ const clients = new Set()
+ const send = (controller, chunk) => {
+ try { controller.enqueue(chunk) } catch { clients.delete(controller) }
+ }
+ const publish = (event, data) => {
+ const chunk = encoder.encode(`event: ${event}\ndata: ${JSON.stringify(data)}\n\n`)
+ for (const controller of clients) send(controller, chunk)
+ }
+
+ rt.stateSaved = () => {
+ if (clients.size > 0) publish('revision', { revision: state.revision })
+ }
+
+ let liveTimer
+ const live = () => {
+ if (clients.size === 0 || liveTimer !== undefined) return
+ liveTimer = setTimeout(() => {
+ liveTimer = undefined
+ publish('live', {})
+ }, LIVE_DELAY_MS)
+ }
+ ctx.on('agent/status', ({ agent }) => {
+ if (rt.selfOf(agent.id) !== undefined) live()
+ })
+
+ ctx.on('session/event', (session, event) => {
+ if (clients.size === 0) return
+ const botId = rt.chatOwners.get(session.id)
+ if (botId === undefined) return
+ for (const item of rt.displayItems(session.id, [event])) publish('message', { botId, sessionId: session.id, item })
+ if (event.type === 'tool/call') live()
+ })
+
+ // Attempts being streamed, by id. A turn that answers another Bot is hidden from the
+ // chat (turns.js marks it before the model is called), so its text is not streamed.
+ const writing = new Map()
+ ctx.on('agent/assistant-stream', ({ agent, frame }) => {
+ if (frame.type === 'start') {
+ const botId = rt.chatOwners.get(agent.id)
+ if (clients.size === 0 || botId === undefined || (state.exchangeTurns[agent.id] ?? []).includes(frame.turn)) return
+ writing.set(frame.attemptId, { botId, sessionId: agent.id })
+ publish('stream', { botId, sessionId: agent.id, attemptId: frame.attemptId, phase: 'start' })
+ return
+ }
+ const target = writing.get(frame.attemptId)
+ if (target === undefined) return
+ if (frame.type === 'chunk' && frame.chunk?.type === 'text-delta') publish('stream', { ...target, attemptId: frame.attemptId, text: frame.chunk.text })
+ else if (frame.type === 'end') {
+ writing.delete(frame.attemptId)
+ publish('stream', { ...target, attemptId: frame.attemptId, phase: 'end' })
+ }
+ })
+
+ const leave = (controller) => {
+ clients.delete(controller)
+ if (clients.size === 0) writing.clear()
+ try { controller.close() } catch { /* already closed or errored */ }
+ }
+
+ ctx.effect(() => ctx.connection.fetch.register({
+ path: FEED_ROUTE,
+ methods: ['GET'],
+ requestBody: 'buffered',
+ fetch: async (request) => {
+ let controller
+ const body = new ReadableStream({
+ start(own) {
+ controller = own
+ clients.add(own)
+ send(own, encoder.encode(`event: hello\ndata: ${JSON.stringify({ revision: state.revision })}\n\n`))
+ },
+ cancel() { leave(controller) },
+ })
+ request.signal.addEventListener('abort', () => leave(controller), { once: true })
+ return new Response(body, { status: 200, headers: { 'content-type': 'text/event-stream; charset=utf-8', 'cache-control': 'no-store' } })
+ },
+ }), 'ds-bot: live feed route')
+
+ ctx.effect(() => {
+ const keepalive = setInterval(() => {
+ if (clients.size > 0) for (const controller of clients) send(controller, encoder.encode(': keepalive\n\n'))
+ }, KEEPALIVE_MS)
+ keepalive.unref?.()
+ return () => {
+ clearInterval(keepalive)
+ clearTimeout(liveTimer)
+ for (const controller of clients) leave(controller)
+ }
+ }, 'ds-bot: live feed')
+}
diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.js
index aedd77b3..1160cd69 100644
--- a/ds-bot/src/host/parts.js
+++ b/ds-bot/src/host/parts.js
@@ -1057,13 +1057,7 @@ export function install(rt) {
return items
}
- async function earlierPage(sessionId, cursor) {
- const past = rt.pastOwners.get(sessionId)
- const botId = rt.chatOwners.get(sessionId) ?? (past?.roomId === undefined ? past?.botId : undefined)
- const parts = botId === undefined ? [] : partsOf(botId)
- const at = parts.findIndex(part => part.sessionId === sessionId)
- const index = Number.isInteger(cursor?.part) ? cursor.part : at - 1
- if (at <= 0 || index < 0 || index >= at) return { items: [], cursor: null, startedAt: parts[at]?.startedAt ?? null }
+ async function partPage(parts, index, cursor) {
const part = parts[index]
const agent = await rt.resolveAgent(part.sessionId)
const page = await ctx.sessionController.page({
@@ -1077,12 +1071,28 @@ export function install(rt) {
const more = page.hasMore && events.length > 0
// The line above a part's first message marks where it began.
if (!more && index > 0) items.unshift({ kind: 'divider', time: part.startedAt })
- return {
- items,
- cursor: more ? { part: index, beforeSeq: events[0].seq } : index > 0 ? { part: index - 1 } : null,
- startedAt: parts[at].startedAt,
- }
+ return { items, cursor: more ? { part: index, beforeSeq: events[0].seq } : index > 0 ? { part: index - 1 } : null }
+ }
+
+ async function earlierPage(sessionId, cursor) {
+ const past = rt.pastOwners.get(sessionId)
+ const botId = rt.chatOwners.get(sessionId) ?? (past?.roomId === undefined ? past?.botId : undefined)
+ const parts = botId === undefined ? [] : partsOf(botId)
+ const at = parts.findIndex(part => part.sessionId === sessionId)
+ const index = Number.isInteger(cursor?.part) ? cursor.part : at - 1
+ if (at <= 0 || index < 0 || index >= at) return { items: [], cursor: null, startedAt: parts[at]?.startedAt ?? null }
+ return { ...(await partPage(parts, index, cursor)), startedAt: parts[at].startedAt }
+ }
+
+ // A Bot's whole own chat, newest page first: the current part, then the earlier
+ // ones, for a window that draws the conversation itself.
+ async function chatPage(botId, cursor) {
+ const parts = partsOf(botId)
+ if (parts.length === 0) throw Object.assign(new Error('Unknown Bot'), { code: 'bot/not-found' })
+ const index = Number.isInteger(cursor?.part) ? cursor.part : parts.length - 1
+ if (index < 0 || index >= parts.length) return { sessionId: parts.at(-1).sessionId, items: [], cursor: null }
+ return { sessionId: parts.at(-1).sessionId, ...(await partPage(parts, index, cursor)) }
}
- Object.assign(rt, { clock, partsOf, historyEntry, partLines, noteText, earlierPage, followFolder, switchJobs: () => switches.values() })
+ Object.assign(rt, { clock, partsOf, historyEntry, partLines, noteText, earlierPage, chatPage, displayItems, followFolder, switchJobs: () => switches.values() })
}
diff --git a/ds-bot/src/host/store.js b/ds-bot/src/host/store.js
index b9e48070..6cd47164 100644
--- a/ds-bot/src/host/store.js
+++ b/ds-bot/src/host/store.js
@@ -296,6 +296,7 @@ export function install(rt) {
await rename(temp, statePath)
seen = stamp()
rt.capabilitiesStateSaved?.()
+ rt.stateSaved?.()
} catch (error) {
await unlink(temp).catch(() => {})
throw error
diff --git a/ds-bot/test/feed.test.js b/ds-bot/test/feed.test.js
new file mode 100644
index 00000000..94639983
--- /dev/null
+++ b/ds-bot/test/feed.test.js
@@ -0,0 +1,149 @@
+import assert from 'node:assert/strict'
+import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
+import { tmpdir } from 'node:os'
+import { join } from 'node:path'
+import { test } from 'node:test'
+import { apply } from '../index.js'
+import { fakeContext } from './fake-ctx.js'
+
+const TEAM = {
+ version: 1,
+ mainBotIds: ['m1'],
+ revision: 3,
+ bots: {
+ m1: { id: 'm1', sessionId: 'm1c', segments: [{ sessionId: 'm1', endedAt: 50 }], name: 'Chief', role: 'Chief of Staff', instructions: '', createdAt: 1 },
+ b2: { id: 'b2', sessionId: 'b2', name: 'Writer', role: 'Writer', instructions: '', createdAt: 2 },
+ },
+ rooms: {},
+ exchanges: [],
+ // Chief's turn 7 answers another Bot, so the chat does not show it.
+ exchangeTurns: { m1c: [7] },
+ questions: {},
+}
+
+const userMessage = (text, time) => ({ type: 'user/message', time, data: { source: { kind: 'user' }, content: [{ type: 'text', text }] } })
+const botMessage = (text, time, turn = 1) => ({ type: 'assistant/message', time, data: { turn, message: { content: [{ type: 'text', text }] } } })
+
+function start({ pages = {} } = {}) {
+ const home = mkdtempSync(join(tmpdir(), 'ds-bot-feed-'))
+ writeFileSync(join(home, 'state.json'), JSON.stringify(TEAM))
+ const ctx = fakeContext()
+ ctx.sessionController = {
+ resolveAgent: async sessionId => ({ agent: { session: { id: sessionId, seq: 100 } } }),
+ // Two pages per Session, newest first, keyed by the cursor that asks for them.
+ page: async ({ address, beforeSeq }) => pages[`${address.sessionId}:${beforeSeq ?? 'end'}`] ?? { records: [], hasMore: false },
+ }
+ const routes = {}
+ const register = ctx.connection.fetch.register
+ ctx.connection.fetch.register = (options) => {
+ routes[options.path] = options
+ return register(options)
+ }
+ apply(ctx, { home })
+ const call = async (endpoint, payload) => {
+ const response = await routes['/api/bot'].fetch(new Request('http://localhost/api/bot', { method: 'POST', body: JSON.stringify({ endpoint, payload }) }))
+ return response.json()
+ }
+ const stop = async () => {
+ await ctx.dispose()
+ rmSync(home, { recursive: true, force: true, maxRetries: 5 })
+ }
+ return { ctx, call, stop, routes }
+}
+
+// Reads Server-Sent Events off a response, one `{ event, data }` per call.
+function reader(response) {
+ const source = response.body.getReader()
+ const decoder = new TextDecoder()
+ let buffer = ''
+ return async function next() {
+ for (;;) {
+ const end = buffer.indexOf('\n\n')
+ if (end >= 0) {
+ const block = buffer.slice(0, end)
+ buffer = buffer.slice(end + 2)
+ if (block.startsWith(':')) continue
+ const field = name => block.split('\n').find(line => line.startsWith(`${name}: `))?.slice(name.length + 2)
+ return { event: field('event'), data: JSON.parse(field('data')) }
+ }
+ const { value, done } = await source.read()
+ if (done) return undefined
+ buffer += decoder.decode(value, { stream: true })
+ }
+ }
+}
+
+test('the live feed tells a window about saves, new chat entries and the reply being written', async (t) => {
+ const { ctx, call, stop, routes } = start()
+ t.after(stop)
+ await call('state')
+ const abort = new AbortController()
+ const response = await routes['/api/bot/feed'].fetch(new Request('http://localhost/api/bot/feed', { signal: abort.signal }))
+ assert.equal(response.headers.get('content-type'), 'text/event-stream; charset=utf-8')
+ const next = reader(response)
+ assert.equal((await next()).event, 'hello')
+
+ await call('set-prefs', { motion: 'lively' })
+ const saved = await next()
+ assert.equal(saved.event, 'revision')
+ assert.equal(saved.data.revision > TEAM.revision, true)
+
+ // The feed's own listener is the last on session/event; the others need a live dsh.
+ const onEvent = ctx.listeners.get('session/event').at(-1)
+ onEvent({ id: 'm1c' }, userMessage('hello', 60))
+ assert.deepEqual(await next(), { event: 'message', data: { botId: 'm1', sessionId: 'm1c', item: { kind: 'user', text: 'hello', time: 60 } } })
+ // A group Session or an earlier part is not a Bot's own chat now.
+ onEvent({ id: 'm1' }, userMessage('old', 1))
+ onEvent({ id: 'm1c' }, botMessage('hidden', 61, 7))
+
+ const [onStream] = ctx.listeners.get('agent/assistant-stream')
+ const agent = { id: 'm1c' }
+ onStream({ agent, frame: { type: 'start', attemptId: 'a7', turn: 7 } })
+ onStream({ agent, frame: { type: 'chunk', attemptId: 'a7', chunk: { type: 'text-delta', text: 'hidden' } } })
+ onStream({ agent, frame: { type: 'start', attemptId: 'a1', turn: 8 } })
+ onStream({ agent, frame: { type: 'chunk', attemptId: 'a1', chunk: { type: 'reasoning-delta', text: 'hmm' } } })
+ onStream({ agent, frame: { type: 'chunk', attemptId: 'a1', chunk: { type: 'text-delta', text: 'Hi ' } } })
+ onStream({ agent, frame: { type: 'chunk', attemptId: 'a1', chunk: { type: 'text-delta', text: 'there' } } })
+ onStream({ agent, frame: { type: 'end', attemptId: 'a1' } })
+ onStream({ agent, frame: { type: 'end', attemptId: 'a7' } })
+ const target = { botId: 'm1', sessionId: 'm1c', attemptId: 'a1' }
+ assert.deepEqual(await next(), { event: 'stream', data: { ...target, phase: 'start' } })
+ assert.deepEqual(await next(), { event: 'stream', data: { ...target, text: 'Hi ' } })
+ assert.deepEqual(await next(), { event: 'stream', data: { ...target, text: 'there' } })
+ assert.deepEqual(await next(), { event: 'stream', data: { ...target, phase: 'end' } })
+
+ // Status changes in a burst come as one notice.
+ const onStatus = ctx.listeners.get('agent/status').at(-1)
+ for (const id of ['b2', 'm1c', 'b2']) onStatus({ agent: { id }, status: 'running' })
+ onStatus({ agent: { id: 'not-a-bot' }, status: 'running' })
+ assert.deepEqual(await next(), { event: 'live', data: {} })
+
+ abort.abort()
+ assert.equal(await next(), undefined)
+})
+
+test('the chat endpoint pages a Bot\'s own chat from the newest part back', async (t) => {
+ const record = (event, seq) => ({ event: { ...event, seq } })
+ const { call, stop } = start({
+ pages: {
+ 'm1c:end': { records: [record(userMessage('second', 70), 41), record(botMessage('reply', 71), 42)], hasMore: true },
+ 'm1c:41': { records: [record(userMessage('first', 60), 40)], hasMore: false },
+ 'm1:end': { records: [record(userMessage('long ago', 5), 3)], hasMore: false },
+ },
+ })
+ t.after(stop)
+ await call('state')
+ const first = (await call('chat', { botId: 'm1' })).value
+ assert.deepEqual(first, {
+ sessionId: 'm1c',
+ items: [{ kind: 'user', text: 'second', time: 70 }, { kind: 'bot', text: 'reply', time: 71 }],
+ cursor: { part: 1, beforeSeq: 41 },
+ })
+ // The current part's first message has the line that marks where the part began.
+ const second = (await call('chat', { botId: 'm1', cursor: first.cursor })).value
+ assert.deepEqual(second.items, [{ kind: 'divider', time: 50 }, { kind: 'user', text: 'first', time: 60 }])
+ assert.deepEqual(second.cursor, { part: 0 })
+ const third = (await call('chat', { botId: 'm1', cursor: second.cursor })).value
+ assert.deepEqual([third.items, third.cursor], [[{ kind: 'user', text: 'long ago', time: 5 }], null])
+ assert.equal((await call('chat', { botId: 'nobody' })).error.code, 'bot/not-found')
+})
diff --git a/ds-bot/test/install.test.js b/ds-bot/test/install.test.js
index 8e6d4b9d..6adcdcf7 100644
--- a/ds-bot/test/install.test.js
+++ b/ds-bot/test/install.test.js
@@ -101,6 +101,12 @@ test('apply registers listeners, tools, and effects in a fixed order', (t) => {
'on llm/stream',
'effect ds-bot: update check',
'effect ds-bot: dsh update install',
+ 'on agent/status',
+ 'on session/event',
+ 'on agent/assistant-stream',
+ 'effect ds-bot: live feed route',
+ 'route GET /api/bot/feed',
+ 'effect ds-bot: live feed',
'effect ds-bot: api route',
'route POST /api/bot',
'effect ds-bot: load state',
diff --git a/ds-bot/test/memory.test.js b/ds-bot/test/memory.test.js
index 099b9ed4..60f77c51 100644
--- a/ds-bot/test/memory.test.js
+++ b/ds-bot/test/memory.test.js
@@ -1009,7 +1009,7 @@ test('a turn whose Session moves to a fresh part before its review is still revi
test('config.memoryReview false leaves memory to the Bots\' own remember calls', async (t) => {
const { ctx, call, section, requests, stop } = start({ config: { memoryReview: false, memoryReviewDelayMs: 0 } })
t.after(() => stop())
- assert.equal(ctx.calls.filter(entry => entry === 'on agent/status').length, 2)
+ assert.equal(ctx.calls.filter(entry => entry === 'on agent/status').length, 3)
await call('state', {})
assert.match(section('b2'), /Call it in that same turn, before you reply/)
ctx.emit('agent/status', { agent: { id: 'b2' }, status: 'idle' })