Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion ds-bot/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
39 changes: 34 additions & 5 deletions ds-bot/src/host/im/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
}
}
})
Expand Down
6 changes: 5 additions & 1 deletion ds-bot/src/shared/im-platforms.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
92 changes: 92 additions & 0 deletions ds-bot/test/im.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand All @@ -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)
Expand Down Expand Up @@ -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')
Expand Down
Loading