Skip to content

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

Send a reply to a group Session's message back to that Session

Fei2411447e93e43c12
diff --git a/ds-bot/src/host/delivery.js b/ds-bot/src/host/delivery.js
index 8ba2d6de..ad62cb85 100644
--- a/ds-bot/src/host/delivery.js
+++ b/ds-bot/src/host/delivery.js
@@ -36,8 +36,9 @@ export function install(rt) {
// `retried` carry on a turn that a restart cut off or that wrote its tool call as
// 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).
- 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 }) {
+ // 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 }) {
const from = rt.botOf(fromId)
const to = rt.botOf(toId)
if (to === undefined) throw new Error(`Unknown Bot ${toId}`)
@@ -59,6 +60,7 @@ export function install(rt) {
...(resumed ? { resumed: true } : {}),
...(retried ? { retried: true } : {}),
...(askedBy ? { askedBy } : {}),
+ ...(fromRoomId ? { senderRoomId: fromRoomId } : {}),
}
if (EXCHANGE_ROLES.has(role) && !deferred && !resumed && !retried && !resent) {
state.exchanges.push({ id: exchangeId, from: fromId, to: toId, text, role, time: Date.now(), ...(replyTo ? { replyTo } : {}), ...(audience === 'user' ? { audience } : {}) })
diff --git a/ds-bot/src/host/resume.js b/ds-bot/src/host/resume.js
index b17cf913..821df2ee 100644
--- a/ds-bot/src/host/resume.js
+++ b/ds-bot/src/host/resume.js
@@ -31,6 +31,7 @@ const triggerOf = info => ({
...(typeof info.hop === 'number' ? { hop: info.hop } : {}),
...(info.audience ? { audience: info.audience } : {}),
...(info.askedBy ? { askedBy: info.askedBy } : {}),
+ ...(info.senderRoomId ? { senderRoomId: info.senderRoomId } : {}),
})
const readNotes = (file) => {
@@ -179,7 +180,7 @@ export function install(rt) {
const trigger = record.trigger
if ((trigger?.role === 'request' || trigger?.role === 'stopped') && trigger.senderSessionId && trigger.audience !== 'user' && rt.botOf(trigger.senderSessionId)) {
// The sender still waits for this turn's answer, so it goes on under the same exchange.
- await rt.deliver({ fromId: trigger.senderSessionId, toId: bot.id, role: trigger.role, hop: trigger.hop ?? 0, ...(trigger.exchangeId ? { exchangeId: trigger.exchangeId } : {}), resumed: true, text })
+ await rt.deliver({ fromId: trigger.senderSessionId, toId: bot.id, role: trigger.role, hop: trigger.hop ?? 0, ...(trigger.exchangeId ? { exchangeId: trigger.exchangeId } : {}), ...(trigger.senderRoomId ? { fromRoomId: trigger.senderRoomId } : {}), resumed: true, text })
return
}
await rt.deliver({ toId: bot.id, role: 'resume', resumed: true, text })
diff --git a/ds-bot/src/host/tools.js b/ds-bot/src/host/tools.js
index d012926a..ecb47972 100644
--- a/ds-bot/src/host/tools.js
+++ b/ds-bot/src/host/tools.js
@@ -17,6 +17,10 @@ export function install(rt) {
const { ctx, state } = rt
const callerBot = (exec) => rt.botOf(rt.selfOf(exec.agent?.id))
+ const callerRoom = (exec) => {
+ const roomId = rt.groupOwners.get(exec.agent?.id)?.roomId
+ return roomId ? { fromRoomId: roomId } : {}
+ }
const roomLine = (room, self) => {
const members = room.members.map(rt.botOf).filter(Boolean)
@@ -100,7 +104,7 @@ export function install(rt) {
const toUser = args.reply_to === 'user'
const working = rt.live.get(rt.chatOf(target.id))?.turn !== undefined
const steer = args.when !== 'queue' && !toUser
- await rt.deliver({ fromId: sender.id, toId: target.id, text: args.message, role: 'request', hop, steer, ...(toUser ? { audience: 'user' } : {}) })
+ await rt.deliver({ fromId: sender.id, toId: target.id, text: args.message, role: 'request', hop, steer, ...(toUser ? { audience: 'user' } : callerRoom(exec)) })
const sent = toUser
? `Delivered to ${target.name}. It will answer the user in its own chat; nothing comes back to you, so point the user to ${target.name}'s chat instead of relaying.`
: working && steer
@@ -142,7 +146,7 @@ export function install(rt) {
agent.cancel({ kind: 'hook', reason: 'ds-bot/stopped' })
}
const why = String(args.reason ?? '').trim() || 'Stop what you are doing; the previous turn was ended on purpose. Acknowledge briefly.'
- await rt.deliver({ fromId: sender.id, toId: target.id, text: why, role: 'stopped' })
+ await rt.deliver({ fromId: sender.id, toId: target.id, text: why, role: 'stopped', ...callerRoom(exec) })
return `Stopped ${target.name} in ${running.map(whereOf).join(' and ')}. It is told why in a new turn, whose reply comes back to you.`
},
}))
diff --git a/ds-bot/src/host/turns.js b/ds-bot/src/host/turns.js
index 951a41b0..80fcf66a 100644
--- a/ds-bot/src/host/turns.js
+++ b/ds-bot/src/host/turns.js
@@ -59,6 +59,7 @@ export function install(rt) {
...(keeps && info.exchangeId ? { exchangeId: info.exchangeId } : {}),
...(keeps && info.audience ? { audience: info.audience } : {}),
...(keeps && info.askedBy ? { askedBy: info.askedBy } : {}),
+ ...(keeps && info.senderRoomId ? { fromRoomId: info.senderRoomId } : {}),
note: 'your tool call did not run',
retried: true,
text: RETRY_TEXT,
@@ -394,11 +395,15 @@ export function install(rt) {
const answer = (source, reply) => {
if (!expectsReply(source) || answersUser(source)) return
const name = rt.botOf(rt.selfOf(id))?.name ?? 'The other Bot'
+ // A sender that wrote from a group gets the reply in its Session there, while
+ // it is still a member.
+ const back = rt.roomOf(source.senderRoomId)?.sessions?.[source.senderSessionId]
if (failure !== undefined) {
// The sender was told a reply comes later; without this it would wait forever.
void rt.deliver({
fromId: rt.selfOf(id),
toId: source.senderSessionId,
+ ...(back ? { sessionId: back } : {}),
text: `${name} could not answer your message: its model call failed (${failureLine(failure)}). Tell the user if it matters; do not retry right away.`,
role: 'reply',
hop: (source.hop ?? 0) + 1,
@@ -410,6 +415,7 @@ export function install(rt) {
void rt.deliver({
fromId: rt.selfOf(id),
toId: source.senderSessionId,
+ ...(back ? { sessionId: back } : {}),
text: reply !== '' ? reply : `${name} was stopped before it could answer your message.`,
role: 'reply',
hop: (source.hop ?? 0) + 1,
@@ -455,6 +461,7 @@ export function install(rt) {
role: 'request',
hop: request.info.hop ?? 0,
exchangeId: request.info.exchangeId,
+ ...(request.info.senderRoomId ? { fromRoomId: request.info.senderRoomId } : {}),
deferred: true,
}).catch(error => rt.warn('ds-bot: deferred request delivery failed: %s', error))
}
diff --git a/ds-bot/test/group-work.test.js b/ds-bot/test/group-work.test.js
index 38ac82ac..db1376af 100644
--- a/ds-bot/test/group-work.test.js
+++ b/ds-bot/test/group-work.test.js
@@ -203,10 +203,14 @@ test('the late round limit makes a late post quiet', async (t) => {
assert.equal(host.takePost().source.quiet, true)
})
-test('a group Session\'s turn that takes a reply posts what it ends with in the group', async (t) => {
+test('a reply to a message a group Session sent comes back there, and its turn posts in the group', async (t) => {
const host = await start(t)
- host.emit('gb', 'turn/start', { turn: 1 })
- host.emit('gb', 'user/message', { content: [{ type: 'text', text: '[Reply from Tester]\nok' }], source: { kind: 'bot', role: 'reply', exchangeId: 'x1', senderName: 'Tester', senderSessionId: 'c', hop: 1 } })
+ await host.tool('message_bot', { to: 'Tester', message: '能跑通吗?' }, 'gb')
+ assert.equal(host.begin('c').source.senderRoomId, 'r1')
+ host.finish('c', 'ok')
+ await settle(() => host.agentOf('gb').calls.length === 1)
+ assert.deepEqual(host.agentOf('b').calls, [])
+ assert.match(text(host.begin('gb')), /^\[Reply from Tester\]\nok/)
host.finish('gb', 'Tester 说可以,我继续。')
await settle(() => host.agentOf('r1').calls.length === 1)
const post = host.takePost()