A team of long-lived bots for DeepSeek Harness, with main bots, bot-to-bot messages and group chats.
Merge remote-tracking branch 'origin/dev' into fix/165-group-work-one-place
Fei24110ed9c00d3d57
diff --cc ds-bot/src/host/tools.jsindex c3152a3e,e890b3c4..7388e252--- a/ds-bot/src/host/tools.js+++ b/ds-bot/src/host/tools.js@@@ -2,9 -2,9 +2,10 @@@// question card. Group rounds and reading chats are in groups.js.import { randomUUID } from 'node:crypto'import { defineTool } from '@deepseek-ai/dsh-tools'- import { COLORS, EXTRA_COLORS, MAX_HOPS, MAX_UNATTENDED_MESSAGES, MAX_UNREAD_REQUESTS, ROOM_BOT_ROUNDS, ROOM_MODES, SOURCE_KIND } from './constants.js'+ import { COLORS, EXTRA_COLORS, MAX_HOPS, MAX_UNATTENDED_MESSAGES, MAX_UNREAD_REQUESTS, MESSAGE_INLINE_MAX, ROOM_BOT_ROUNDS, ROOM_MODES, SOURCE_KIND } from './constants.js'+ import { absoluteLinks } from './links.js'import { unquote } from './text.js'+import { mentionSpans } from '../shared/mentions.js'export const output = { schema: { type: 'string' }, render: (_args, value) => [{ type: 'text', text: String(value) }] }export const GROUP_REF = { type: 'string', required: true, description: 'Exact group chat name from list_bots' }@@@ -143,11 -127,7 +145,13 @@@ export function install(rt)const target = rt.findBot(args.to)if (target === undefined) return `No Bot named ${args.to}. Team: ${Object.values(state.bots).map(bot => bot.name).join(', ')}`if (target.id === sender.id) return 'You cannot message yourself.'+ if (String(args.message ?? '').trim() === '') return 'Write a message to send.'++ const text = absoluteLinks(String(args.message), rt.sessionFolder?.(exec.agent?.session ?? exec.agent))+ const room = rt.roomOf(rt.groupOwners.get(exec.agent?.id)?.roomId)- if (room !== undefined) return callInGroup(exec, room, sender, target, String(args.message ?? '').trim())++ if (room !== undefined) return callInGroup(exec, room, sender, target, text.trim())+ const posted = calledInPosts.get(exec.agent?.id)+ const postedIn = posted !== undefined && posted.turn === turnOf(exec) ? posted.calls.get(target.id) : undefined+ if (postedIn !== undefined) refuse(`Not sent: you called ${target.name} in "${postedIn}" in this turn, and it works on that there. Send each task one way only; to add to it, post in that group.`)const hop = nextHop(exec)if (hop > MAX_HOPS) return CHAIN_TOO_LONGcheckUnattended()@@@ -156,10 -136,13 +160,12 @@@if (unread >= MAX_UNREAD_REQUESTS) {refuse(`Not sent: ${target.name} has not read the last ${unread} messages from Bots yet, so it is most likely stuck, for example waiting for the user. Do not send it more; tell the user that ${target.name} is not responding.`)}- const text = absoluteLinks(String(args.message), rt.sessionFolder?.(exec.agent?.session ?? exec.agent))+ const long = await rt.attachLong(text, `Message from ${sender.name}.txt`)+ if (long === undefined) return tooLong(text)const toUser = args.reply_to === 'user'const working = rt.live.get(chat)?.turn !== undefinedconst 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: long.text, files: long.files, length: long.length, role: 'request', hop, steer, ...(toUser ? { audience: 'user' } : callerRoom(exec)) })++ await rt.deliver({ fromId: sender.id, toId: target.id, text: long.text, files: long.files, length: long.length, role: 'request', hop, steer, ...(toUser ? { audience: 'user' } : {}) })rt.unattended.sent += 1const 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.`diff --cc ds-bot/test/group-work.test.jsindex ce313757,28480ac3..3e59c760--- a/ds-bot/test/group-work.test.js+++ b/ds-bot/test/group-work.test.js@@@ -148,70 -148,68 +148,131 @@@ test('work past the grace lets the rounassert.deepEqual(host.agentOf('gc').calls, [])})+// 公开#165: asked in a group to hand out work, Chief sent it with message_bot. Members+// did it in their own chats, apart from the group (Bots outside the group too), and+// again when called in the group.+test('message_bot from a group to one of its members calls it there, as "@Name" would', async (t) => {+ const host = await start(t)+ const first = host.round('c1', host.say('@Chief 安排一下文档'))+ await settle(() => host.agentOf('ga').calls.length === 1)+ host.begin('ga')+ const sent = await host.tool('message_bot', { to: 'Writer', message: '把文档整理好' }, 'ga')+ assert.match(sent, /^Called Writer in "Ops"/)+ host.finish('ga', '安排好了。')+ await settle(() => host.agentOf('gb').calls.length === 1)+ const called = host.begin('gb')+ assert.equal(called.source.askedBy, 'a')+ assert.match(text(called), /Chief: @Writer 把文档整理好/)+ host.finish('gb', '整理好了。')+ await settle(() => first() !== undefined)+ assert.deepEqual(kinds(first()), [['message', 'Chief'], ['message', 'Chief'], ['message', 'Writer']])+ assert.deepEqual([host.agentOf('b').calls, host.agentOf('gc').calls], [[], []])+})++test('message_bot from a member the round moved on from calls the other member in a round of its own', async (t) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const host = await start(t)+ const first = host.round('c1', host.say('@Writer 整理文档'))+ await settle(() => host.agentOf('gb').calls.length === 1)+ host.begin('gb')+ host.emit('gb', 'tool/call', { callId: 'k1', name: 'write', arguments: '{}' })+ t.mock.timers.tick(ROOM_WORK_GRACE_MS)+ await settle(() => first() !== undefined)+ await host.tool('message_bot', { to: 'Tester', message: '帮我测一下' }, 'gb')+ await settle(() => host.agentOf('r1').calls.length === 1)+ const post = host.takePost()+ assert.deepEqual([post.source.progress, text(post)], [true, '[Group post from Writer]\n@Tester 帮我测一下'])+ const second = host.round('c2', post)+ await settle(() => host.agentOf('gc').calls.length === 1)+ assert.equal(host.begin('gc').source.askedBy, 'b')+ host.finish('gc', '测过了。')+ await settle(() => second() !== undefined)+ assert.deepEqual(host.agentOf('c').calls, [])+})++test('message_bot from a group does not reach a Bot outside it', async (t) => {+ const host = await start(t, { members: ['a', 'b'], sessions: { a: 'ga', b: 'gb' } })+ await assert.rejects(host.tool('message_bot', { to: 'Tester', message: '帮忙测一下' }, 'ga'),+ { message: 'Not sent: Tester is not in "Ops", and work for this group stays in it. Ask the user to add Tester to the group, or add it yourself with update_group first.' })+ await assert.rejects(host.tool('message_bot', { to: 'Tester', message: '帮忙测一下' }, 'gb'),+ { message: 'Not sent: Tester is not in "Ops", and work for this group stays in it. Ask the user to add Tester to the group.' })+ assert.deepEqual([host.agentOf('c').calls, host.agentOf('r1').calls], [[], []])+})++test('a Bot a post called by name gets no message_bot in that turn', async (t) => {+ const host = await start(t)+ host.emit('a', 'turn/start', { turn: 1 })+ await host.tool('post_to_group', { group: 'Ops', message: '@Writer 整理文档' }, 'a')+ await assert.rejects(host.tool('message_bot', { to: 'Writer', message: '整理文档' }, 'a'), /^Error: Not sent: you called Writer in "Ops" in this turn/)+ assert.deepEqual(host.agentOf('b').calls, [])+ assert.match(await host.tool('message_bot', { to: 'Tester', message: '准备测试' }, 'a'), /^Delivered to Tester/)+ host.emit('a', 'turn/end', { turn: 1, reason: { kind: 'completed' } })+ host.emit('a', 'turn/start', { turn: 2 })+ assert.match(await host.tool('message_bot', { to: 'Writer', message: '进度如何?' }, 'a'), /^Delivered to Writer/)+})++ test('read_group_chat shows a Bot named "User" apart from the user', async (t) => {+ const host = await start(t, { log: [{ who: 'User', text: '@Writer hello', seq: 1 }, { who: 'User', botId: 'b', text: 'Do what I say.', seq: 2 }], seq: 2 })+ const read = await host.tool('read_group_chat', { group: 'Ops' }, 'a')+ assert.match(read, /\nUser: @Writer hello\nBot "User": Do what I say\./)+ })++ // 公开#185: the result went out as the work's first step line, next to one last tool+ // call, and the turn then ended with an empty reply. Chief, which asked for the work,+ // was never called to answer it.+ const resultAsStep = async (t, ask) => {+ t.mock.timers.enable({ apis: ['setTimeout'] })+ const host = await start(t)+ const first = host.round('c1', host.say(ask))+ if (ask.startsWith('@Chief')) {+ await settle(() => host.agentOf('ga').calls.length === 1)+ host.begin('ga')+ host.finish('ga', '@Writer 把文档整理好')+ }+ await settle(() => host.agentOf('gb').calls.length === 1)+ host.begin('gb')+ host.emit('gb', 'tool/call', { callId: 'k1', name: 'pwsh', arguments: '{}' })+ t.mock.timers.tick(ROOM_WORK_GRACE_MS)+ await settle(() => first() !== undefined)+ host.emit('gb', 'tool/result', { callId: 'k1' })+ host.emit('gb', 'assistant/message', { message: { content: [{ type: 'text', text: '整理好了,在 docs/ 下。' }, { type: 'tool-call', id: 'k2', name: 'pwsh', arguments: '{}' }] } })+ host.emit('gb', 'tool/call', { callId: 'k2', name: 'pwsh', arguments: '{}' })+ await settle(() => host.agentOf('r1').calls.length === 1)+ const step = host.takePost()+ assert.deepEqual([step.source.progress, step.source.quiet, text(step)], [true, true, '[Group post from Writer]\n整理好了,在 docs/ 下。'])+ const stepRound = host.round('c2', step)+ await settle(() => stepRound() !== undefined)+ host.emit('gb', 'tool/result', { callId: 'k2' })+ host.finish('gb', '')+ return host+ }++ test('late work whose result went out as a step line still tells the member that asked, which answers it', async (t) => {+ const host = await resultAsStep(t, '@Chief 安排一下文档')+ await settle(() => host.agentOf('r1').calls.length === 1)+ const done = host.takePost()+ assert.deepEqual([done.source.done, done.source.late, done.source.askedBy], [true, true, 'a'])+ const third = host.round('c3', done)+ await settle(() => host.agentOf('ga').calls.length === 2)+ const prompt = text(host.agentOf('ga').calls[1])+ assert.match(prompt, /Writer finished the work you asked for/)+ assert.match(prompt, /Writer: 整理好了,在 docs\/ 下。/)+ host.begin('ga')+ host.finish('ga', '收到,辛苦了。')+ await settle(() => third() !== undefined)+ assert.deepEqual(kinds(third()), [['done', 'Writer'], ['message', 'Chief']])+ assert.deepEqual(host.agentOf('gc').calls, [])+ const log = JSON.parse(readFileSync(join(host.home, 'state.json'), 'utf8')).rooms.r1.log.map(line => [line.who, line.kind ?? 'message'])+ assert.deepEqual(log.slice(-3), [['Writer', 'message'], ['Writer', 'done'], ['Chief', 'message']])+ })++ test('late work the user asked for directly ends without a post once its result is out', async (t) => {+ const host = await resultAsStep(t, '@Writer 把文档整理好')+ for (let i = 0; i < 20; i += 1) await new Promise(resolve => setImmediate(resolve))+ assert.deepEqual(host.agentOf('r1').calls, [])+ })+-test('a Bot\'s post keeps its hop through the round, the member\'s late reply and its own messages', async (t) => {+test('a Bot\'s post keeps its hop through the round, the members it calls and the member\'s late reply', async (t) => {t.mock.timers.enable({ apis: ['setTimeout'] })const host = await start(t)host.emit('a', 'turn/start', { turn: 1 })diff --cc ds-bot/test/rounds.test.jsindex 495826dc,9240baac..24562f3d--- a/ds-bot/test/rounds.test.js+++ b/ds-bot/test/rounds.test.js@@@ -347,3 -347,307 +347,308 @@@ test('a message sent between rounds sticonst result = JSON.parse(await running)assert.deepEqual(result.replies.map(reply => reply.name ?? 'User'), ['Chief', 'Writer'])})++ // 公开#163: a member's group Session whose stored log is corrupt could not be resumed;+ // the round counted the member as passed, showed nothing, and tried the same Session+ // again every round.+ test('a member whose group Session cannot be opened shows a failure, and its next turn there starts a new Session', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, rt } = start()+ t.after(stop)+ const room = await team(call, sent)+ const chief = (await call('state', {})).value.mainBotId++ await emit(room.id, userMessage('Report in'))+ let running = relay(room.id, 'r1')+ const first = await turnFor(sent)+ await answer(first.sessionId, first.message.source.exchangeId, 'Chief here.', 1)+ const second = await turnFor(sent, first)+ await answer(second.sessionId, second.message.source.exchangeId, 'Writer here.', 2)+ await running+ const broken = rt.roomOf(room.id).sessions[chief]+ assert.equal(first.sessionId, broken)++ const resolve = ctx.sessionController.resolveAgent+ ctx.sessionController.resolveAgent = async sessionId => (sessionId === broken+ ? { error: { code: 'gateway/internal', message: `resume failed for session "${sessionId}": SessionQueryError: stored session "${sessionId}" is corrupt: released v2 row has seq gap (expected 20, got 16)` } }+ : resolve(sessionId))+ t.mock.method(rt, 'warn', () => {})+ sent.length = 0+ await emit(room.id, userMessage('Again'))+ running = relay(room.id, 'r2')+ const writerTurn = await turnFor(sent)+ assert.equal(writerTurn.sessionId, second.sessionId)+ await answer(writerTurn.sessionId, writerTurn.message.source.exchangeId, 'Still here.', 3)+ const result = JSON.parse(await running)+ const failed = result.replies.filter(reply => reply.kind === 'failed')+ assert.deepEqual(failed.map(reply => [reply.name, reply.code]), [['Chief', 'SESSION_RESET']])+ assert.equal(result.passed.includes('Chief'), false)+ assert.equal(rt.roomOf(room.id).sessions[chief], undefined)++ sent.length = 0+ await emit(room.id, userMessage('Once more'))+ running = relay(room.id, 'r3')+ const fresh = await turnFor(sent)+ assert.notEqual(fresh.sessionId, broken)+ assert.equal(rt.roomOf(room.id).sessions[chief], fresh.sessionId)+ await answer(fresh.sessionId, fresh.message.source.exchangeId, 'Back.', 1)+ const again = await turnFor(sent, fresh)+ await answer(again.sessionId, again.message.source.exchangeId, 'Here.', 4)+ assert.deepEqual(JSON.parse(await running).replies.map(reply => reply.name ?? 'User'), ['Chief', 'Writer'])+ })++ test('a delivery that fails for another reason shows a failure and keeps the Session', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, rt } = start()+ t.after(stop)+ const room = await team(call, sent)+ const chief = (await call('state', {})).value.mainBotId+ await emit(room.id, userMessage('Report in'))+ let running = relay(room.id, 'r1')+ const first = await turnFor(sent)+ await answer(first.sessionId, first.message.source.exchangeId, 'Chief here.', 1)+ const second = await turnFor(sent, first)+ await answer(second.sessionId, second.message.source.exchangeId, 'Writer here.', 2)+ await running++ const resolve = ctx.sessionController.resolveAgent+ ctx.sessionController.resolveAgent = async sessionId => (sessionId === first.sessionId+ ? { error: { code: 'session/writer-held', message: 'another process holds this Session' } }+ : resolve(sessionId))+ t.mock.method(rt, 'warn', () => {})+ sent.length = 0+ await emit(room.id, userMessage('Again'))+ running = relay(room.id, 'r2')+ const writerTurn = await turnFor(sent)+ await answer(writerTurn.sessionId, writerTurn.message.source.exchangeId, 'Still here.', 3)+ const result = JSON.parse(await running)+ assert.deepEqual(result.replies.filter(reply => reply.kind === 'failed').map(reply => [reply.name, reply.code]), [['Chief', 'SESSION']])+ assert.equal(rt.roomOf(room.id).sessions[chief], first.sessionId)+ })++ // #217: DSH opens a relative file link from the folder of the Session that shows it. A+ // member wrote the link in its own group Session, which works in the group's folder; the+ // group's Session and other Bots work elsewhere, so the link reaches them in full.+ test('a relative file link a member writes reaches the group, and another Bot, as a full path', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, rt } = start()+ t.after(stop)+ const folder = realpathSync.native(mkdtempSync(join(tmpdir(), 'ds-bot-links-')))+ t.after(() => rmSync(folder, { recursive: true, force: true }))+ const room = await team(call, sent)+ assert.equal((await call('update-room', { id: room.id, workDir: folder })).ok, true)+ const full = path => join(folder, path).replace(/\\/g, '/').replace(/ /g, '%20')+ await emit(room.id, userMessage('Where is the draft?'))+ const running = relay(room.id, 'relay')+ const first = await turnFor(sent)+ await answer(first.sessionId, first.message.source.exchangeId, 'It is in [a.txt](sub/a.txt).', 1)+ const second = await turnFor(sent, first)+ // The next member reads it in its prompt, and the group shows it, with the full path.+ assert.ok(second.message.content[0].text.includes(`[a.txt](${full('sub/a.txt')})`))+ await answer(second.sessionId, second.message.source.exchangeId, 'Thanks.', 2)+ const result = JSON.parse(await running)+ assert.equal(result.replies[0].text, `It is in [a.txt](${full('sub/a.txt')}).`)+- // A Bot that works in the folder sends the same link to another Bot.++ // A Bot that works in the folder sends the same link to another Bot; from its group++ // Session that calls the Bot in the group (#165).+ const chief = rt.state.mainBotIds[0]+ const writer = rt.state.rooms[room.id].members.find(id => id !== chief)+ sent.length = 0+ await ctx.registeredTools.get('message_bot').execute({ to: rt.botOf(writer).name, message: 'Read [notes](notes.md#L3)', when: 'queue' }, {+ agent: { id: first.sessionId, session: { id: first.sessionId, header: { cwd: folder } }, inbox: { nextStep: [], nextTurn: [] } }, callId: 'c1',+ })- const delivered = await waitFor(() => sent.find(entry => entry.message.source?.role === 'request'))++ const delivered = await waitFor(() => sent.find(entry => entry.sessionId === room.id && entry.message.source?.role === 'post'))+ assert.ok(delivered.message.content.map(block => block.text).join('\n').includes(`[notes](${full('notes.md')}#L3)`))+ })++ // #209: DSH can fail a member's turn before its first step (DSH 0.2.1 does so for a folder+ // that is gone), so the turn never takes the round's call. The round counts the failure at+ // once instead of waiting minutes for a reply that cannot come.+ test('a member turn that fails before it takes the call fails that member in the round at once', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent } = start()+ t.after(stop)+ const room = await team(call, sent)+ await emit(room.id, userMessage('Start'))+ const running = relay(room.id, 'relay')+ const first = await turnFor(sent)+ await emit(first.sessionId, { type: 'turn/start', data: { turn: 1 } })+ await emit(first.sessionId, { type: 'turn/end', data: { reason: { kind: 'error', error: { message: 'working-directory: directory does not exist: "/gone"', code: 'UNKNOWN' } } } })+ const second = await turnFor(sent, first)+ assert.ok(second, 'the next member speaks without waiting for the reply timeout')+ await answer(second.sessionId, second.message.source.exchangeId, 'Writer here.', 1)+ const result = JSON.parse(await running)+ assert.deepEqual(result.replies.map(reply => [reply.kind ?? 'reply', reply.name]), [['failed', 'Chief'], ['reply', 'Writer']])+ })++ // A member deleted while its group Session is made sits the round out; the Session it was+ // getting belongs to no one, so it is archived instead of left over.+ test('a member deleted while its group Session is made is left out, and the Session is not kept', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, rt } = start()+ t.after(stop)+ const room = await team(call, sent)+ const writer = rt.state.rooms[room.id].members.find(id => !rt.isMain(id))+ const archived = []+ ctx.workspaceRegistry.archiveSession = async (sessionId) => { archived.push(sessionId) }+ await emit(room.id, userMessage('Start'))+ const running = relay(room.id, 'relay')+ const first = await turnFor(sent)++ let release+ const gate = new Promise(resolve => { release = resolve })+ let made+ const making = new Promise(resolve => { made = resolve })+ const create = ctx.sessionController.create+ ctx.sessionController.create = async (options) => {+ const result = await create(options)+ made(result.sessionId)+ await gate+ return result+ }+ await answer(first.sessionId, first.message.source.exchangeId, 'Chief here.', 1)+ const sessionId = await making+ assert.equal((await call('delete-bot', { id: writer })).ok, true)+ release()++ const result = JSON.parse(await running)+ assert.deepEqual(result.replies.map(reply => [reply.kind ?? 'reply', reply.name]), [['reply', 'Chief']])+ assert.ok(archived.includes(sessionId))+ assert.equal(rt.state.rooms[room.id].sessions?.[writer], undefined)+ })++ // #206: state.json is written whole on every save, so a long group line keeps its start+ // there and its whole text in a file of its own. Members still read it whole.+ test('a long group line keeps its start in state.json, and members and read_group_chat read it whole', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent, ctx, home, rt } = start()+ t.after(() => rmSync(home, { recursive: true, force: true, maxRetries: 5 }))+ const room = await team(call, sent)+ const long = `${'长'.repeat(9000)}结尾`+ await emit(room.id, userMessage('Write it all out'))+ const running = relay(room.id, 'relay')+ const first = await turnFor(sent)+ await answer(first.sessionId, first.message.source.exchangeId, long, 1)+ const second = await turnFor(sent, first)+ assert.ok(second.message.content[0].text.includes(long), 'the next member reads the line whole')+ await answer(second.sessionId, second.message.source.exchangeId, 'Thanks.', 2)+ assert.equal(JSON.parse(await running).replies[0].text, long)++ await rt.save()+ const saved = JSON.parse(readFileSync(join(home, 'state.json'), 'utf8')).rooms[room.id].log.find(line => line.length !== undefined)+ assert.equal(saved.text.length, 2000)+ assert.equal(saved.length, long.length)+ assert.equal(readFileSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`), 'utf8'), long)+ const read = ctx.registeredTools.get('read_group_chat')+ const exec = { agent: { id: first.sessionId } }+ assert.match(await read.execute({ group: 'R1' }, exec), new RegExp(`cut; ${long.length} characters in all; read_group_chat with seq ${saved.seq} reads it whole`))+ assert.ok((await read.execute({ group: 'R1', seq: saved.seq }, exec)).endsWith(long))+ await stop({ keep: true })++ // The log is full (60 lines) when DSH starts again, the long line second oldest.+ const file = join(home, 'state.json')+ const onDisk = JSON.parse(readFileSync(file, 'utf8'))+ const kept = onDisk.rooms[room.id]+ const fillers = Array.from({ length: 60 - kept.log.length }, (_, at) => ({ who: 'User', text: `line ${at}`, seq: kept.seq + at + 1 }))+ kept.log = [...kept.log, ...fillers]+ kept.seq += fillers.length+ writeFileSync(file, JSON.stringify(onDisk))++ // After a restart the whole line is read back from its file.+ const again = start({ home })+ t.after(() => again.stop({ keep: true }))+ await again.call('state', {})+ const whole = await again.ctx.registeredTools.get('read_group_chat').execute({ group: 'R1', seq: saved.seq }, exec)+ assert.ok(whole.endsWith(long))++ // The file goes with the line once the log moves past it, and with the group.+ await again.emit(room.id, again.userMessage('Next'))+ const next = again.relay(room.id, 'relay-2')+ const one = await turnFor(again.sent)+ await again.answer(one.sessionId, one.message.source.exchangeId, 'One.', 3)+ const two = await turnFor(again.sent, one)+ await again.answer(two.sessionId, two.message.source.exchangeId, 'Two.', 4)+ await next+ await waitFor(() => (existsSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`)) ? undefined : true))+ assert.equal(existsSync(join(home, 'group-lines', room.id, `${saved.seq}.txt`)), false)+ await again.call('delete-room', { id: room.id })+ await waitFor(() => (existsSync(join(home, 'group-lines', room.id)) ? undefined : true))+ assert.equal(existsSync(join(home, 'group-lines', room.id)), false)+ })++ test('a group line longer than a member reads is cut in its prompt, with its length, and its file keeps only that', async (t) => {+ const { call, emit, userMessage, relay, answer, stop, sent, home, rt } = start()+ t.after(stop)+ const room = await team(call, sent)+ await emit(room.id, userMessage('Go'))+ const running = relay(room.id, 'relay')+ const first = await turnFor(sent)+ await answer(first.sessionId, first.message.source.exchangeId, 'x'.repeat(25_000), 1)+ const second = await turnFor(sent, first)+ const prompt = second.message.content[0].text+ assert.ok(prompt.includes(`${'x'.repeat(20_000)}… (cut; 25000 characters in all)`))+ assert.ok(!prompt.includes('x'.repeat(20_001)))+ await answer(second.sessionId, second.message.source.exchangeId, 'Ok.', 2)+ await running+ const line = rt.state.rooms[room.id].log.find(entry => entry.length === 25_000)+ assert.equal(readFileSync(join(home, 'group-lines', room.id, `${line.seq}.txt`), 'utf8').length, 20_000)+ })++ // 公开#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+ })++ // #206: a Bot's post past 20,000 characters goes as a file; the line keeps the file and+ // each member it is shown to gets it with its prompt.+ test('a long post goes to the group as a file, and reaches the member with its prompt', async (t) => {+ const { call, emit, relay, answer, stop, sent, ctx, rt } = start()+ t.after(stop)+ const store = fakeAttachments()+ ctx.get = name => (name === 'attachments' ? store : undefined)+ const room = await team(call, sent)+ const chief = rt.state.mainBotIds[0]+ const long = `${Array.from({ length: 400 }, (_, at) => `第 ${at + 1} 行:${'报告内容。'.repeat(10)}`).join('\n')}\n结尾`+ const posted = await ctx.registeredTools.get('post_to_group').execute({ group: 'R1', message: long }, { agent: { id: rt.chatOf(chief) } })+ assert.match(posted, /^Posted in "R1"/)+ const post = sent.find(entry => entry.sessionId === room.id && entry.message.source.role === 'post')+ const file = post.message.content.find(block => block.type === 'file')?.attachment+ assert.equal(file?.name, 'Post from Chief.txt')+ assert.equal(post.message.source.length, long.length)+ assert.equal(new TextDecoder().decode(store.files.get(file.attachmentId)), long)++ sent.length = 0+ await emit(room.id, { type: 'user/message', data: { source: post.message.source, content: post.message.content } })+ const running = relay(room.id, 'relay')+ const turn = await turnFor(sent)+ assert.deepEqual(turn.message.content.filter(block => block.type === 'file').map(block => block.attachment), [file])+ const prompt = turn.message.content[0].text+ assert.ok(prompt.includes(`Chief: [A long message: ${long.length} characters.`))+ assert.ok(prompt.includes(`[File "Post from Chief.txt" (${file.bytes} bytes) attached]`))+ assert.ok(prompt.length < 6000, `the prompt has ${prompt.length} characters`)+ await answer(turn.sessionId, turn.message.source.exchangeId, 'Read it.', 1)+ await running+ const line = rt.state.rooms[room.id].log.find(entry => entry.botId === chief)+ assert.deepEqual(line.files, [file])+ assert.equal(line.length, undefined, 'the line is short; no file of its own')+ })