Skip to content

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

Pass files the user sends in a group on to the members

Fei241159a3f9ee5e21
diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.js
index 5b20edcb..cb29d381 100644
--- a/ds-bot/src/host/delivery.js
+++ b/ds-bot/src/host/delivery.js
@@ -38,8 +38,9 @@ export function install(rt) {
// text (resume.js, turns.js), and `resent` is a message a shutdown cancelled; their
// exchange is recorded already too. `askedBy` is the member whose @mention called a
// group turn (groups.js). `fromRoomId`: the group whose Session the sender wrote
- // from; the reply goes back to that Session (turns.js).
- async function deliver({ fromId, toId, sessionId, text, role, hop = 0, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, retried = false, resent = false, askedBy, fromRoomId }) {
+ // from; the reply goes back to that Session (turns.js). `files` go along as file
+ // blocks, which DSH turns into a read-only path the recipient's tools can read.
+ async function deliver({ fromId, toId, sessionId, text, role, hop = 0, replyTo, roomId, audience, note, exchangeId = randomUUID(), steer = false, deferred = false, resumed = false, retried = false, resent = false, askedBy, fromRoomId, files = [] }) {
const from = rt.botOf(fromId)
const to = rt.botOf(toId)
if (to === undefined) throw new Error(`Unknown Bot ${toId}`)
@@ -73,7 +74,10 @@ export function install(rt) {
// A steered message cuts into a running turn at its next step; a follow-up
// queues for the next turn (see turns.js for how each is routed).
const message = createUserMessage({
- content: [{ type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed, retried })}\n${text}` }],
+ content: [
+ { type: 'text', text: `${headerFor(role, fromName, room?.name, audience, note, deferred, { resumed, retried })}\n${text}` },
+ ...files.map(attachment => ({ type: 'file', attachment })),
+ ],
source,
})
if (steer) agent.steer(message)
diff --git a/ds-bot/src/host/groups.js b/ds-bot/src/host/groups.js
index 9f636fd5..664c5b6d 100644
--- a/ds-bot/src/host/groups.js
+++ b/ds-bot/src/host/groups.js
@@ -111,6 +111,8 @@ const roomKickoff = language => `The user just created this group and has not wr
const ROOM_READ_MAX = 30
+const fileLine = file => `[File "${file.name}" (${file.bytes} bytes) attached]`
+
export function install(rt) {
const { ctx } = rt
@@ -142,6 +144,7 @@ export function install(rt) {
// Lines from before the round's opening message are kept apart from this round, so
// an earlier round (an old count-off, say) never reads as part of the current one.
+ // `files` are the files of the lines shown, which go to the member with the prompt.
function roomPrompt(room, member, round) {
const peers = room.members.filter(id => id !== member.id).map(rt.botOf).filter(Boolean)
.map(bot => `${bot.name}${rt.isAdminOf(room, bot.id) ? ' (admin)' : ''}`)
@@ -155,9 +158,9 @@ export function install(rt) {
const after = round.slots.length - index - 1
const members = `Members: the user${peers.length ? `, ${peers.join(', ')}` : ''} and you${leads ? ' (admin)' : ''}. Members speak one at a time; each message is posted before the next speaker starts. ${ROOM_CALLS}`
const notice = room.notice ? `Group notice (shared instructions for every member):\n${room.notice}` : ''
- if (round.kickoff) return [members, notice, leads ? adminNote(room) : '', roomKickoff(rt.uiLanguage?.())].filter(Boolean).join('\n\n')
+ if (round.kickoff) return { text: [members, notice, leads ? adminNote(room) : '', roomKickoff(rt.uiLanguage?.())].filter(Boolean).join('\n\n'), files: [] }
const working = workingIn(room).filter(item => item.bot.id !== member.id)
- return [
+ const text = [
members,
notice,
leads ? adminNote(room) : '',
@@ -171,6 +174,7 @@ export function install(rt) {
`You are number ${index + 1} of ${round.slots.length}. ${before === 0 ? 'Nobody spoke before you' : `${before} went before you`}${after === 0 ? ', and you are the last.' : `; ${after} after you will see your message.`}`,
ROOM_REPLY,
].filter(Boolean).join('\n\n')
+ return { text, files: [...earlier, ...current].flatMap(line => line.files ?? []) }
}
// Members whose turn in this group is running, and since when.
@@ -196,7 +200,7 @@ export function install(rt) {
// member's Session, so a member at work is never cut off. `by` is the member that
// called it, which answers what the turn posts after the round moved on. What the
// member writes before its last step is posted at once and collected in `said`.
- async function roomTurn(room, member, sessionId, text, note, signal, round, by, said = []) {
+ async function roomTurn(room, member, sessionId, { text, files }, note, signal, round, by, said = []) {
if (signal.aborted) return null
const exchangeId = randomUUID()
const turn = { botId: member.id, sessionId, exchangeId }
@@ -230,7 +234,7 @@ export function install(rt) {
signal.addEventListener('abort', () => finish(null), { once: true })
})
try {
- await rt.deliver({ fromId: undefined, toId: member.id, sessionId, role: 'room', roomId: room.id, exchangeId, note, text, hop: round.hop, ...(by ? { askedBy: by } : {}) })
+ await rt.deliver({ fromId: undefined, toId: member.id, sessionId, role: 'room', roomId: room.id, exchangeId, note, text, files, hop: round.hop, ...(by ? { askedBy: by } : {}) })
// Still held by an earlier request: this turn cannot start until the user answers.
if (rt.waitsForApproval?.(sessionId)) rt.roomWaiters.get(exchangeId)?.approval()
// Still at earlier work: the call waits in its inbox and is answered after that.
@@ -356,9 +360,9 @@ export function install(rt) {
}
const prompt = roomPrompt(room, member, round)
round.shown?.push(member.id)
- const text = session.brief === undefined ? prompt : `${session.brief}\n\n${prompt}`
+ const text = session.brief === undefined ? prompt.text : `${session.brief}\n\n${prompt.text}`
const said = []
- const reply = await roomTurn(room, member, session.id, text, slot.note ?? 'your turn', signal, round, slot.by, said)
+ const reply = await roomTurn(room, member, session.id, { text, files: prompt.files }, slot.note ?? 'your turn', signal, round, slot.by, said)
round.speaking = null
const final = typeof reply === 'string' && !isPass(reply) ? reply : undefined
if (reply?.failure !== undefined) {
@@ -417,10 +421,12 @@ export function install(rt) {
const event = item => item.failed || item.resumed || item.stopped
for (const post of posts.filter(item => !item.kickoff && !event(item))) {
// Image readings go into the log only; `@` names are picked from what the user typed.
- const text = post.images === undefined ? post.text : [post.text, ...await rt.imagesForGroup(post.images, room.id)].filter(Boolean).join('\n')
+ // A file stays with its line and reaches each member the line is shown to.
+ const readings = post.images === undefined ? [] : await rt.imagesForGroup(post.images, room.id)
+ const text = [post.text, ...readings, ...(post.files ?? []).map(fileLine)].filter(Boolean).join('\n')
appendRoom(room, post.botId
? { who: rt.botOf(post.botId)?.name ?? post.name ?? 'Bot', botId: post.botId, text }
- : { who: 'User', text })
+ : { who: 'User', text, ...(post.files === undefined ? {} : { files: post.files }) })
}
if (posts.some(post => !post.botId && !post.kickoff)) {
room.botRounds = 0
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index ba4bd434..ea08bc29 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -410,9 +410,10 @@ export function install(rt) {
attachReference(roomInbox.get(id), source, textOf(event.data.content))
} else if (source?.kind === 'user' || source?.kind === REPLY_KIND) {
if (rt.roomOf(id) && source.kind === 'user') {
- const images = (Array.isArray(event.data.content) ? event.data.content : [])
- .filter(block => block?.type === 'image' && !block.offloaded).map(block => block.attachment)
- queueRoomMessage(id, { text: textOf(event.data.content), ...(images.length > 0 ? { images } : {}) })
+ 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(id, { text: textOf(event.data.content), ...(images.length > 0 ? { images } : {}), ...(files.length > 0 ? { files } : {}) })
}
const own = rt.chatOwners.get(id)
if (own !== undefined) rt.dropKickoff(own)
diff --git a/ds-bot/test/rounds.test.js b/ds-bot/test/rounds.test.js
index 495826dc..040e2f21 100644
--- a/ds-bot/test/rounds.test.js
+++ b/ds-bot/test/rounds.test.js
@@ -347,3 +347,34 @@ test('a message sent between rounds still opens a full round', async (t) => {
const result = JSON.parse(await running)
assert.deepEqual(result.replies.map(reply => reply.name ?? 'User'), ['Chief', 'Writer'])
})
+
+// 公开#176: a PDF the user sent in a group reached neither the log nor any member, so
+// every Bot said it could not read it; in a Bot's own chat the same file worked.
+test('a file the user sends in a group reaches each member the line is shown to, once', async (t) => {
+ const { call, emit, relay, answer, stop, sent, rt } = start()
+ t.after(stop)
+ const room = await team(call, sent)
+ const pdf = { attachmentId: 'sha256:39cb3dab00', name: 'report-XYZ176.pdf', bytes: 607 }
+ await emit(room.id, { type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'Read this PDF' }, { type: 'file', attachment: pdf }] } })
+ let running = relay(room.id, 'r1')
+ const first = await turnFor(sent)
+ const files = message => message.content.filter(block => block.type === 'file').map(block => block.attachment)
+ assert.deepEqual(files(first.message), [pdf])
+ assert.match(first.message.content[0].text, /User: Read this PDF\n\[File "report-XYZ176\.pdf" \(607 bytes\) attached\]/)
+ await answer(first.sessionId, first.message.source.exchangeId, 'Reading it.', 1)
+ const second = await turnFor(sent, first)
+ assert.deepEqual(files(second.message), [pdf])
+ await answer(second.sessionId, second.message.source.exchangeId, 'Me too.', 2)
+ await running
+ assert.deepEqual(rt.roomOf(room.id).log.find(line => line.who === 'User').files, [pdf])
+
+ sent.length = 0
+ await emit(room.id, { type: 'user/message', data: { source: { kind: 'user' }, content: [{ type: 'text', text: 'Thanks' }] } })
+ running = relay(room.id, 'r2')
+ const next = await turnFor(sent)
+ assert.deepEqual(files(next.message), [])
+ await answer(next.sessionId, next.message.source.exchangeId, 'OK.', 3)
+ const after = await turnFor(sent, next)
+ await answer(after.sessionId, after.message.source.exchangeId, 'OK.', 4)
+ await running
+})