From 4bdd3a1177229a2ac87e88a5da53a456926501c1 Mon Sep 17 00:00:00 2001 From: Steve Dempsey Date: Thu, 12 Nov 2026 15:03:37 -0500 Subject: [PATCH 1/2] Let a Telegram connection mirror only the first and last text of a turn Telegram got every text block of every mirrored turn, including narration Steve has no use for. A notify setting on the Telegram platform picks bookends (the default), all (the old behaviour) or cards (no turn text). Cards, questions, views and approvals are never filtered. client.js still needs a rebuild; not pushed. --- ds-bot/src/host/im/index.js | 39 +++++++++++-- ds-bot/src/shared/im-platforms.js | 6 +- ds-bot/test/im.test.js | 92 +++++++++++++++++++++++++++++++ 3 files changed, 131 insertions(+), 6 deletions(-) diff --git a/ds-bot/src/host/im/index.js b/ds-bot/src/host/im/index.js index c5e207fbf6..09a4422167 100644 --- a/ds-bot/src/host/im/index.js +++ b/ds-bot/src/host/im/index.js @@ -1157,22 +1157,51 @@ export function install(rt) { } // ----------------------------------------------------------------- outbound + // How much turn text a connection mirrors. Only a platform that declares the + // `notify` field filters; every other platform keeps sending everything. + const notifyOf = (conn) => { + const field = imPlatform(conn.platform)?.fields.find(entry => entry.key === 'notify') + if (field === undefined) return 'all' + return field.options.some(([id]) => id === conn.settings?.notify) ? conn.settings.notify : field.options[0][0] + } + const mirror = (botId, body, wants) => { + for (const conn of Object.values(connections())) { + if (!runners.has(conn.id) || !wants(notifyOf(conn))) continue + for (const { chatKey, chat } of listeners(conn, { botId })) sendTo(conn, chatKey, { key: chatKey, kind: chat.kind, title: chat.title }, body) + } + } + // Per Session, the running turn's first and last text block, sent at turn/end to + // `bookends` connections. Held in memory only and dropped at the next turn/start, + // so a turn that never ended cannot carry its text into the next one. + const bookends = new Map() ctx.on('session/event', (session, event) => { const botId = rt.chatOwners.get(session.id) if (botId === undefined) { imVia.delete(session.id) + bookends.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] }) } + if (event.type === 'turn/start') bookends.delete(session.id) 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 ?? '') - for (const conn of Object.values(connections())) { - if (!runners.has(conn.id)) continue - for (const { chatKey, chat } of listeners(conn, { botId })) sendTo(conn, chatKey, { key: chatKey, kind: chat.kind, title: chat.title }, body) + if (item.kind === 'view') { + mirror(botId, viewCardText({ title: item.title, view: item.view }) || (item.title ?? ''), () => true) + continue + } + if (item.kind !== 'bot') continue + mirror(botId, item.text, mode => mode === 'all') + const held = bookends.get(session.id) + if (held === undefined) bookends.set(session.id, { botId, first: item.text }) + else held.last = item.text + } + if (event.type === 'turn/end') { + const held = bookends.get(session.id) + bookends.delete(session.id) + if (held !== undefined) { + for (const body of held.last === undefined ? [held.first] : [held.first, held.last]) mirror(held.botId, body, mode => mode === 'bookends') } } }) diff --git a/ds-bot/src/shared/im-platforms.js b/ds-bot/src/shared/im-platforms.js index c2f6efdd9a..47650457e4 100644 --- a/ds-bot/src/shared/im-platforms.js +++ b/ds-bot/src/shared/im-platforms.js @@ -7,7 +7,11 @@ export const IM_PLATFORMS = [ { id: 'telegram', name: 'Telegram', - fields: [{ key: 'token', label: 'Bot token', secret: true }], + fields: [ + { key: 'token', label: 'Bot token', secret: true }, + // Turn text mirrored to the chat; cards always go. The first option is the default. + { key: 'notify', label: 'Messages', options: [['bookends', 'First and last'], ['all', 'Everything'], ['cards', 'Cards only']] }, + ], buttons: true, groups: true, help: 'https://t.me/BotFather', diff --git a/ds-bot/test/im.test.js b/ds-bot/test/im.test.js index 45c997a992..feb91763c8 100644 --- a/ds-bot/test/im.test.js +++ b/ds-bot/test/im.test.js @@ -201,6 +201,7 @@ test('outbound: assistant text and member lines go out; hidden turns, user and s t.after(s.stop) const conn = await add(s) const one = s.rt.state.im.connections[conn.id] + one.settings.notify = 'all' one.paired.push({ id: 'u1', name: 'U1', at: 1 }) await s.rt.save() await dm(s, 'u1', 'hi') @@ -224,6 +225,96 @@ test('outbound: assistant text and member lines go out; hidden turns, user and s assert.equal(s.fake.sent[1].chat.key, 'g1') }) +// A paired DM on a fresh Telegram connection, with `notify` set when given. +async function notifyChat(notify) { + const s = await start() + const conn = await add(s) + const one = s.rt.state.im.connections[conn.id] + if (notify !== undefined) one.settings.notify = notify + one.paired.push({ id: 'u1', name: 'U1', at: 1 }) + await s.rt.save() + await dm(s, 'u1', 'hi') + s.fake.sent.length = 0 + return { s, one } +} +const say = (s, ...texts) => s.emitEvent('a', 'assistant/message', { message: { content: texts.map(text => ({ type: 'text', text })) } }) +const texts = s => s.fake.sent.filter(entry => entry.buttons === undefined).map(entry => entry.text) + +test('notify bookends, the default: a turn sends its first and last text at turn/end and drops the middle', async (t) => { + const { s, one } = await notifyChat() + t.after(s.stop) + assert.equal(one.settings.notify, 'bookends', 'a new Telegram connection starts on bookends') + s.emitEvent('a', 'turn/start', { turn: 1 }) + say(s, 'Starting on it.', 'Reading the files.') + say(s, 'Running the tests.') + say(s, 'Done: all green.') + await s.flush() + assert.deepEqual(texts(s), [], 'nothing goes out while the turn runs') + s.emitEvent('a', 'turn/end', { turn: 1, reason: 'stop' }) + await s.flush() + assert.deepEqual(texts(s), ['Starting on it.', 'Done: all green.']) +}) + +test('notify bookends: a turn with one text block sends it once', async (t) => { + const { s } = await notifyChat('bookends') + t.after(s.stop) + s.emitEvent('a', 'turn/start', { turn: 1 }) + say(s, 'Only this.') + s.emitEvent('a', 'turn/end', { turn: 1, reason: 'stop' }) + await s.flush() + assert.deepEqual(texts(s), ['Only this.']) +}) + +test('notify bookends: a turn that never ended does not carry its text into the next', async (t) => { + const { s } = await notifyChat('bookends') + t.after(s.stop) + s.emitEvent('a', 'turn/start', { turn: 1 }) + say(s, 'Old turn text.') + s.emitEvent('a', 'turn/start', { turn: 2 }) + say(s, 'New turn text.') + s.emitEvent('a', 'turn/end', { turn: 2, reason: 'stop' }) + await s.flush() + assert.deepEqual(texts(s), ['New turn text.']) +}) + +test('notify all: every text block goes out as it arrives, as before', async (t) => { + const { s } = await notifyChat('all') + t.after(s.stop) + s.emitEvent('a', 'turn/start', { turn: 1 }) + say(s, 'one', 'two') + say(s, 'three') + await s.flush() + assert.deepEqual(texts(s), ['one', 'two', 'three']) + s.emitEvent('a', 'turn/end', { turn: 1, reason: 'stop' }) + await s.flush() + assert.deepEqual(texts(s), ['one', 'two', 'three'], 'turn/end adds nothing') +}) + +test('notify cards: no turn text is sent', async (t) => { + const { s } = await notifyChat('cards') + t.after(s.stop) + s.emitEvent('a', 'turn/start', { turn: 1 }) + say(s, 'one', 'two') + s.emitEvent('a', 'turn/end', { turn: 1, reason: 'stop' }) + await s.flush() + assert.deepEqual(texts(s), []) +}) + +for (const notify of ['bookends', 'cards']) { + test(`notify ${notify}: a card opened mid turn still goes out at once`, async (t) => { + const { s } = await notifyChat(notify) + t.after(s.stop) + s.emitEvent('a', 'turn/start', { turn: 1 }) + say(s, 'Asking you now.') + s.rt.state.questions.a = { id: 'q1', question: 'Ship it?', options: [{ label: 'Ship' }, { label: 'Hold' }] } + await s.rt.save() + await s.flush() + const card = s.fake.sent.find(entry => entry.buttons !== undefined) + assert.ok(card, 'the card is sent before the turn ends') + assert.match(card.text, /Ship it\?/) + }) +} + 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) @@ -772,6 +863,7 @@ test('a permanent send error shows in the status and is not retried; a network e t.after(s.stop) const conn = await add(s) const one = s.rt.state.im.connections[conn.id] + one.settings.notify = 'all' one.paired.push({ id: 'u1', name: 'U1', at: 1 }) await s.rt.save() await dm(s, 'u1', 'hi') From 155aab762ece798fa7ff6edbf7c5c48df3e5d1df Mon Sep 17 00:00:00 2001 From: Steve Dempsey Date: Thu, 12 Nov 2026 15:58:03 -0500 Subject: [PATCH 2/2] Rebuild client.js for the Telegram notify field The settings page draws the IM add form from src/shared/im-platforms.js, which client.js bundles. --- ds-bot/client.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ds-bot/client.js b/ds-bot/client.js index f07b950368..56ad8dda6b 100644 --- a/ds-bot/client.js +++ b/ds-bot/client.js @@ -25374,7 +25374,11 @@ var IM_PLATFORMS = [ { id: "telegram", name: "Telegram", - fields: [{ key: "token", label: "Bot token", secret: true }], + fields: [ + { key: "token", label: "Bot token", secret: true }, + // Turn text mirrored to the chat; cards always go. The first option is the default. + { key: "notify", label: "Messages", options: [["bookends", "First and last"], ["all", "Everything"], ["cards", "Cards only"]] } + ], buttons: true, groups: true, help: "https://t.me/BotFather"