Skip to content

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

Tag IM messages with their platform in live chats and group logs

Fei2411c1130bd8241b
diff --git a/ds-bot/client.js b/ds-bot/client.js
index 1bac0030..db7ff162 100644
--- a/ds-bot/client.js
+++ b/ds-bot/client.js
@@ -8626,6 +8626,7 @@ var IM_PLATFORMS = [
}
];
var imPlatform = (id) => IM_PLATFORMS.find((platform) => platform.id === id);
+var viaOf = (source) => typeof source?.rpcId === "string" && source.rpcId.startsWith("im:") ? source.rpcId.split(":")[1] : void 0;
// src/client/user-message.js
var import_react19 = require("react");
@@ -8694,7 +8695,7 @@ function CopyButton({ text }) {
return (0, import_react19.createElement)("button", { type: "button", className: "bt-tool", "aria-label": copied ? t("Copied") : t("Copy"), title: copied ? t("Copied") : t("Copy"), onClick: copy2 }, copied ? (0, import_react19.createElement)(CheckIcon) : (0, import_react19.createElement)(CopyIcon));
}
var clock2 = (time) => typeof time === "number" ? new Date(time).toLocaleTimeString(dateLocale(), { hour: "numeric", minute: "2-digit" }) : void 0;
-var viaOf = (source) => typeof source?.rpcId === "string" && source.rpcId.startsWith("im:") ? source.rpcId.split(":")[1] : void 0;
+var viaOf2 = (source) => typeof source?.rpcId === "string" && source.rpcId.startsWith("im:") ? source.rpcId.split(":")[1] : void 0;
var ViaTag = ({ via }) => (0, import_react19.createElement)("span", { className: "bt-uvia" }, t("via {platform}", { platform: imPlatform(via)?.name ?? via }));
function UserMessageCell({ node, sessionId, useRoster, actions, renderMessageImages, openFile, openSkill }) {
const roster = useRoster((value) => value);
@@ -8704,7 +8705,7 @@ function UserMessageCell({ node, sessionId, useRoster, actions, renderMessageIma
const references = openFile && openSkill ? { openFile, openSkill } : void 0;
const steering = node.kind === "steering";
const sentAt = Number.isFinite(data.time) ? messageClock(data.time) : void 0;
- const via = viaOf(data.source);
+ const via = viaOf2(data.source) ?? roster.imVia?.[sessionId]?.[node.anchorSeq];
const { shown, folded, control } = useFold(text, { markdown: false });
const body = (0, import_react19.createElement)(UserText, { text: shown, bots: mentionableBots(roster, sessionId), referenceLabels, skillNames: data.skillNames ?? [], references, actions });
const row = (0, import_react19.useRef)(null);
@@ -24245,6 +24246,7 @@ function apply(ctx) {
if (value.btwCards !== void 0 && JSON.stringify(value.btwCards) !== JSON.stringify(current.btwCards ?? {})) live2.btwCards = value.btwCards;
if (value.sideChats !== void 0 && JSON.stringify(value.sideChats) !== JSON.stringify(current.sideChats ?? {})) live2.sideChats = value.sideChats;
if (value.imStatus !== void 0 && JSON.stringify(value.imStatus) !== JSON.stringify(current.imStatus ?? {})) live2.imStatus = value.imStatus;
+ if (value.imVia !== void 0 && JSON.stringify(value.imVia) !== JSON.stringify(current.imVia ?? {})) live2.imVia = value.imVia;
if (Object.keys(live2).length > 0 && current.ready) roster.set({ ...current, ...live2 });
if (value.unchanged || value.revision === current.revision && value.modelsKey === current.modelsKey) return;
if (Object.keys(seenAt).length === 0) leaveVisits([...value.bots, ...value.rooms].map((entry) => entry.id));
diff --git a/ds-bot/src/client/index.js b/ds-bot/src/client/index.js
index a5d799f6..3384281a 100644
--- a/ds-bot/src/client/index.js
+++ b/ds-bot/src/client/index.js
@@ -186,6 +186,7 @@ export function apply(ctx) {
if (value.btwCards !== undefined && JSON.stringify(value.btwCards) !== JSON.stringify(current.btwCards ?? {})) live.btwCards = value.btwCards
if (value.sideChats !== undefined && JSON.stringify(value.sideChats) !== JSON.stringify(current.sideChats ?? {})) live.sideChats = value.sideChats
if (value.imStatus !== undefined && JSON.stringify(value.imStatus) !== JSON.stringify(current.imStatus ?? {})) live.imStatus = value.imStatus
+ if (value.imVia !== undefined && JSON.stringify(value.imVia) !== JSON.stringify(current.imVia ?? {})) live.imVia = value.imVia
if (Object.keys(live).length > 0 && current.ready) roster.set({ ...current, ...live })
if (value.unchanged || (value.revision === current.revision && value.modelsKey === current.modelsKey)) return
// On a browser with no visit record yet, everything already there counts as seen.
diff --git a/ds-bot/src/client/user-message.js b/ds-bot/src/client/user-message.js
index 0968f3b7..2eb51926 100644
--- a/ds-bot/src/client/user-message.js
+++ b/ds-bot/src/client/user-message.js
@@ -96,7 +96,7 @@ export function UserMessageCell({ node, sessionId, useRoster, actions, renderMes
const references = openFile && openSkill ? { openFile, openSkill } : undefined
const steering = node.kind === 'steering'
const sentAt = Number.isFinite(data.time) ? messageClock(data.time) : undefined
- const via = viaOf(data.source)
+ const via = viaOf(data.source) ?? roster.imVia?.[sessionId]?.[node.anchorSeq]
const { shown, folded, control } = useFold(text, { markdown: false })
const body = h(UserText, { text: shown, bots: mentionableBots(roster, sessionId), referenceLabels, skillNames: data.skillNames ?? [], references, actions })
const row = useRef(null)
diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js
index 7eb51e8f..720b7c38 100644
--- a/ds-bot/src/host/channel.js
+++ b/ds-bot/src/host/channel.js
@@ -101,6 +101,7 @@ export function install(rt) {
connectors: rt.connectorsView(),
capabilityStatuses: rt.capabilityStatusesView(),
imStatus: rt.imStatusView?.() ?? {},
+ imVia: rt.imViaView?.() ?? {},
})
// The live fields' sources are spread across the host with no single change
// counter, so the serialized answer itself is the detector: a poll whose `live`
diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.js
index 8430c6bc..9c6d5651 100644
--- a/ds-bot/src/host/groups.js
+++ b/ds-bot/src/host/groups.js
@@ -24,6 +24,7 @@ import { createUserMessage } from '@deepseek-ai/dsh-llm'
import { defineTool } from '@deepseek-ai/dsh-tools'
import { GROUP_LINE_READ, ROOM_IDLE_RELAY_LIMIT, ROOM_REPLY_TIMEOUT_MS, ROOM_STEP_GAP_MS, ROOM_WORK_GRACE_MS, SOURCE_KIND, TEAM_TOOLS, TEXT_KEPT } from './constants.js'
import { escapeRegExp, headOf, splitRef, textOf } from './text.js'
+import { viaOf } from '../shared/im-platforms.js'
import { mentionSpans, speakerOf } from '../shared/mentions.js'
import { GROUP_REF, output } from './tools.js'
import { failureLine } from './turns.js'
@@ -647,7 +648,7 @@ export function install(rt) {
const content = Array.isArray(message.content) ? message.content : []
const text = textOf(content)
if (text === '' || content.some(block => block?.type !== 'text') || text.includes('dsh-session:')) break
- lines.push({ id: message.id, text })
+ lines.push({ id: message.id, text, ...(viaOf(message.source) ? { via: viaOf(message.source) } : {}) })
}
return lines
})
@@ -666,8 +667,8 @@ export function install(rt) {
rt.goalUserMessage?.(room.id)
const members = room.members.map(rt.botOf).filter(Boolean)
for (const line of lines) {
- appendRoom(room, { who: 'User', text: line.text })
- round.replies.push({ kind: 'user', text: line.text, time: Date.now() })
+ appendRoom(room, { who: 'User', text: line.text, ...(line.via ? { via: line.via } : {}) })
+ round.replies.push({ kind: 'user', text: line.text, time: Date.now(), ...(line.via ? { via: line.via } : {}) })
const { all, found } = mentionsIn(line.text, members)
const note = all || found.length > 0 ? 'the user mentioned you' : 'new message from the user'
const { speakers, ordered } = openingCall(room, { text: line.text })
@@ -921,6 +922,7 @@ export function install(rt) {
...(post.botId ? { who: rt.botOf(post.botId)?.name ?? post.name ?? 'Bot', botId: post.botId } : { who: 'User' }),
text,
...(post.files === undefined ? {} : { files: post.files }),
+ ...(post.via === undefined ? {} : { via: post.via }),
})
}
await rt.save()
diff --git a/ds-bot/src/host/im/index.js b/ds-bot/src/host/im/index.js
index 8649371c..ed5317ad 100644
--- a/ds-bot/src/host/im/index.js
+++ b/ds-bot/src/host/im/index.js
@@ -855,12 +855,27 @@ export function install(rt) {
return result
}
+ // ------------------------------------------------------------------ im via
+ // IM-originated user messages, keyed by session and event seq, so the client
+ // can tag a message with the platform it came from. A Session's entries go
+ // when it stops being the chat's current part.
+ const imVia = new Map()
+ const imViaView = () => {
+ for (const sessionId of imVia.keys()) if (!rt.chatOwners.has(sessionId)) imVia.delete(sessionId)
+ return Object.fromEntries([...imVia.entries()].map(([sessionId, seqs]) => [sessionId, { ...seqs }]))
+ }
+
// ----------------------------------------------------------------- outbound
- // Prepended so the feed stays the last session/event listener, as its tests and
- // the delivery order around it assume.
ctx.on('session/event', (session, event) => {
const botId = rt.chatOwners.get(session.id)
- if (botId === undefined) return
+ if (botId === undefined) {
+ imVia.delete(session.id)
+ return
+ }
+ const rpcId = event.type === 'user/message' ? event.data?.source?.rpcId : undefined
+ if (typeof rpcId === 'string' && rpcId.startsWith('im:') && Number.isInteger(event.seq)) {
+ imVia.set(session.id, { ...imVia.get(session.id), [event.seq]: rpcId.split(':')[1] })
+ }
for (const item of rt.displayItems(session.id, [event])) {
if (item.kind !== 'bot' && item.kind !== 'view') continue
const body = item.kind === 'bot' ? item.text : viewCardText({ title: item.title, view: item.view }) || (item.title ?? '')
@@ -927,5 +942,5 @@ export function install(rt) {
const imView = () => ({ connections: Object.values(connections()).map(viewOf), platforms: IM_PLATFORMS.filter(platform => adaptersFor().includes(platform.id)).map(platform => platform.id) })
const imStatusView = () => Object.fromEntries([...statuses.entries()].map(([id, status]) => [id, { state: status.state, ...(status.error !== undefined ? { error: status.error } : {}), at: status.at }]))
- Object.assign(rt, { imView, imStatusView, imEndpoint })
+ Object.assign(rt, { imView, imStatusView, imViaView, imEndpoint })
}
diff --git a/ds-bot/src/host/parts.js b/ds-bot/src/host/parts.js
index 66658a44..5ef8d6f4 100644
--- a/ds-bot/src/host/parts.js
+++ b/ds-bot/src/host/parts.js
@@ -22,6 +22,7 @@ import { parseReplies, stackReplies } from '../shared/group-replies.js'
import { 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 { viaOf } from '../shared/im-platforms.js'
import { checkShownView, normalizeView, readViewReply, viewCardText, viewReply, viewReplyText } from 'deep-view/core'
// An ask_user card as the user saw it. The user's reply is often just an option label,
@@ -1364,7 +1365,7 @@ export function install(rt) {
// the result event's seq, which the chat keys a post's anchor by.
const item = { time: time ?? reply.time ?? event.time }
if (turn !== undefined) item.turn = turn
- if (reply.kind === 'user') items.push({ kind: 'user', text: reply.text, ...item })
+ if (reply.kind === 'user') items.push({ kind: 'user', text: reply.text, ...(reply.via ? { via: reply.via } : {}), ...item })
else if (reply.kind === undefined) items.push({ kind: 'post', botId: reply.botId ?? null, name: reply.name ?? null, texts, ...item, ...(duration === undefined ? {} : { duration }) })
else items.push({ kind: 'relay', reply, ...item })
}
@@ -1375,8 +1376,7 @@ export function install(rt) {
if (source?.kind === 'user') {
const content = Array.isArray(event.data.content) ? event.data.content : []
const images = content.filter(block => block?.type === 'image').length
- // A message that came in over IM carries im:<platform>:<uuid> as its requestId.
- const via = typeof source.rpcId === 'string' && source.rpcId.startsWith('im:') ? source.rpcId.split(':')[1] : undefined
+ const via = viaOf(source)
stamp(event, { kind: 'user', text: textOf(content), ...(images > 0 ? { images } : {}), ...(via ? { via } : {}), time: event.time })
} else if (source?.kind === REPLY_KIND) {
const text = textOf(event.data.content)
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index e314506a..641d7b31 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -8,6 +8,7 @@ import { HANDOFF_ACK, MESSAGE_INLINE_MAX, REFERENCE_KIND, REPLY_KIND, SOURCE_KIN
import { cleanText } from './diagnostics.js'
import { absoluteLinks } from './links.js'
import { splitRef, textOf } from './text.js'
+import { viaOf } from '../shared/im-platforms.js'
import { reminderText } from './schedules.js'
// Failures only the user can fix: a key, a sign-in, a balance. They block the Bot
@@ -838,7 +839,7 @@ export function install(rt) {
const content = Array.isArray(event.data.content) ? event.data.content : []
const images = content.filter(block => block?.type === 'image' && !block.offloaded).map(block => block.attachment)
const files = content.filter(block => block?.type === 'file' && block.attachment !== undefined).map(block => block.attachment)
- queueRoomMessage(rt.roomOf(id).id, { text: textOf(event.data.content), ...(images.length > 0 ? { images } : {}), ...(files.length > 0 ? { files } : {}) })
+ queueRoomMessage(rt.roomOf(id).id, { text: textOf(event.data.content), ...(images.length > 0 ? { images } : {}), ...(files.length > 0 ? { files } : {}), ...(viaOf(source) ? { via: viaOf(source) } : {}) })
}
const own = rt.chatOwners.get(id)
if (own !== undefined) rt.userWrote(own)
diff --git a/ds-bot/src/shared/im-platforms.js b/ds-bot/src/shared/im-platforms.js
index 59209bca..fe97b446 100644
--- a/ds-bot/src/shared/im-platforms.js
+++ b/ds-bot/src/shared/im-platforms.js
@@ -81,3 +81,6 @@ export const IM_PLATFORMS = [
]
export const imPlatform = id => IM_PLATFORMS.find(platform => platform.id === id)
+
+// The platform a user message came in over: IM messages carry im:<platform>:<uuid> as their requestId.
+export const viaOf = source => (typeof source?.rpcId === 'string' && source.rpcId.startsWith('im:') ? source.rpcId.split(':')[1] : undefined)
diff --git a/ds-bot/test/im.test.js b/ds-bot/test/im.test.js
index 58b5b4b9..276d0316 100644
--- a/ds-bot/test/im.test.js
+++ b/ds-bot/test/im.test.js
@@ -224,6 +224,22 @@ test('outbound: assistant text and member lines go out; hidden turns, user and s
assert.equal(s.fake.sent[1].chat.key, 'g1')
})
+test('IM user messages are tagged by seq for the current part; a part that is no longer current drops out', async (t) => {
+ const s = await start()
+ t.after(s.stop)
+ const fire = (sessionId, seq, rpcId) => {
+ for (const listener of s.ctx.listeners.get('session/event') ?? []) {
+ if (listener.length >= 2) listener({ id: sessionId }, { type: 'user/message', seq, data: { source: { kind: 'user', rpcId }, content: [{ type: 'text', text: 'x' }] }, time: Date.now() })
+ }
+ }
+ fire('a', 5, 'im:telegram:1')
+ fire('a', 6, 'web-request')
+ fire('a', 7, 'im:feishu:2')
+ assert.deepEqual(s.rt.imViaView(), { a: { 5: 'telegram', 7: 'feishu' } })
+ s.rt.chatOwners.delete('a')
+ assert.deepEqual(s.rt.imViaView(), {})
+})
+
test('question card: buttons answer it, a stranger press is ignored, settled elsewhere edits the card', async (t) => {
const s = await start()
t.after(s.stop)