From 734503ea39968556e5b6b1c3b445573d22a1a7a4 Mon Sep 17 00:00:00 2001 From: Steve Dempsey Date: Wed, 11 Nov 2026 18:05:36 -0500 Subject: [PATCH] Answer a single-choice view card from an IM chat A show_view card with one single-choice options component reached Telegram as text only, with no buttons, so the user could see it but not answer it there. Such a view now goes out in the question-card layout, with a button per option where the platform has buttons and a bare number elsewhere. Both answer it through rt.answerView, the view-reply body moved out of the endpoint. Any other view keeps its text card. --- ds-bot/src/host/channel.js | 49 +++++++++++---------- ds-bot/src/host/im/index.js | 76 ++++++++++++++++++++++++++++++++- ds-bot/test/im.test.js | 85 +++++++++++++++++++++++++++++++++++++ 3 files changed, 186 insertions(+), 24 deletions(-) diff --git a/ds-bot/src/host/channel.js b/ds-bot/src/host/channel.js index 3e829d6b08..3c8fe2c0b7 100644 --- a/ds-bot/src/host/channel.js +++ b/ds-bot/src/host/channel.js @@ -576,27 +576,7 @@ export function install(rt) { case 'view-reply': { if (typeof body.sessionId !== 'string') return fail('Nothing to send', 'bot/invalid') await rt.load() - const past = rt.pastOwners.get(body.sessionId) - const sessionId = rt.botOf(body.sessionId) ? rt.chatOf(body.sessionId) : past && past.roomId === undefined ? rt.chatOf(past.botId) : body.sessionId - const pending = rt.own(state.views, sessionId) - if (pending === undefined || pending.id !== body.viewId) return fail('This view is no longer open', 'bot/stale') - const checked = validateViewAnswer(pending.view, body.action, body.values) - if (checked.error !== undefined) return fail(checked.error, 'bot/invalid') - const reply = viewReply(pending, body.action, body.values) - if (reply === undefined) return fail('Nothing to send', 'bot/invalid') - // Claim the view before queueing so a second answer is refused. - delete state.views[sessionId] - try { - await rt.ensureCapabilities(sessionId) - const agent = await rt.resolveAgent(sessionId) - agent.followup(createUserMessage({ content: [{ type: 'text', text: reply.text }], source: reply.source })) - await ctx.sessions.flush(agent.session) - } catch (error) { - state.views[sessionId] ??= pending - throw error - } - await rt.save() - return ok(true) + return ok(await rt.answerView(body.sessionId, body.viewId, body.action, body.values)) } // A photo for the next `send`, one per call: a phone's link carries a few MB per // message, so photos travel one by one and the message names them. @@ -840,7 +820,32 @@ export function install(rt) { return true } - Object.assign(rt, { sendUser, answerQuestion }) + // The user's answer to a show_view card, from the web, the phone or an IM chat. + async function answerView(sessionRef, viewId, action, values) { + const past = rt.pastOwners.get(sessionRef) + const sessionId = rt.botOf(sessionRef) ? rt.chatOf(sessionRef) : past && past.roomId === undefined ? rt.chatOf(past.botId) : sessionRef + const pending = rt.own(state.views, sessionId) + if (pending === undefined || pending.id !== viewId) throw Object.assign(new Error('This view is no longer open'), { code: 'bot/stale' }) + const checked = validateViewAnswer(pending.view, action, values) + if (checked.error !== undefined) throw Object.assign(new Error(checked.error), { code: 'bot/invalid' }) + const reply = viewReply(pending, action, values) + if (reply === undefined) throw Object.assign(new Error('Nothing to send'), { code: 'bot/invalid' }) + // Claim the view before queueing so a second answer is refused. + delete state.views[sessionId] + try { + await rt.ensureCapabilities(sessionId) + const agent = await rt.resolveAgent(sessionId) + agent.followup(createUserMessage({ content: [{ type: 'text', text: reply.text }], source: reply.source })) + await ctx.sessions.flush(agent.session) + } catch (error) { + state.views[sessionId] ??= pending + throw error + } + await rt.save() + return true + } + + Object.assign(rt, { sendUser, answerQuestion, answerView }) // An exact Fetch route on the shared /api channel; the browser authenticates with its session cookie. ctx.effect(() => ctx.connection.fetch.register({ diff --git a/ds-bot/src/host/im/index.js b/ds-bot/src/host/im/index.js index 5085e766ba..d8ff726bf7 100644 --- a/ds-bot/src/host/im/index.js +++ b/ds-bot/src/host/im/index.js @@ -423,6 +423,54 @@ export function install(rt) { } return true } + const viewById = (id) => { + for (const [sessionId, view] of Object.entries(state.views ?? {})) { + if (view.id === id) return { sessionId, view } + } + return undefined + } + // A view a chat can answer with a button or a number: one single-choice + // options component and no inputs. Any other view stays text only. + const choiceOf = (view) => { + const answers = [] + const texts = [] + const walk = (node) => { + if (node === null || typeof node !== 'object') return + if (['options', 'confirm', 'form', 'submit', 'slider', 'choice', 'toggle', 'number'].includes(node.type)) answers.push(node) + if (node.type === 'text' && typeof node.text === 'string' && node.text.trim() !== '') texts.push(node.text.trim()) + for (const child of Array.isArray(node.children) ? node.children : []) walk(child) + } + walk(view) + const node = answers[0] + if (answers.length !== 1 || node.type !== 'options' || node.multi === true || typeof node.id !== 'string') return undefined + const options = (Array.isArray(node.options) ? node.options : []).filter(option => typeof option?.label === 'string') + return options.length === 0 ? undefined : { action: node.id, options, texts } + } + const answerViewCard = async (conn, chat, cardKey, label) => { + const held = openCard(conn, chat.key, cardKey) + if (held === undefined) return false + const found = viewById(held.cardKey.slice(2)) + const choice = found === undefined ? undefined : choiceOf(found.view.view) + if (found === undefined || choice === undefined) { + await sendCardReply(conn, chat, text('stale')) + return true + } + if (!choice.options.some(option => option.label === label)) return false + try { + await rt.answerView(found.sessionId, found.view.id, choice.action, { selected: [label] }) + await settleCard(conn.id, held.cardKey, label) + await settledEdits(conn) + } catch (error) { + if (error?.code === 'bot/stale') { + await settleCard(conn.id, held.cardKey, text('answered')) + await sendCardReply(conn, chat, text('stale')) + await settledEdits(conn) + return true + } + throw error + } + return true + } const answerApprovalCard = async (conn, chat, cardKey, choice) => { const held = openCard(conn, chat.key, cardKey) if (held === undefined) return false @@ -447,7 +495,8 @@ export function install(rt) { const data = runtimes.get(conn.id)?.data if (data === undefined) return undefined for (const [cardKey, entries] of Object.entries(data.cards).reverse()) { - if (!cardKey.startsWith('q:') && !cardKey.startsWith('a:')) continue + const choiceView = cardKey.startsWith('v:') && choiceOf(viewById(cardKey.slice(2))?.view.view) !== undefined + if (!cardKey.startsWith('q:') && !cardKey.startsWith('a:') && !choiceView) continue const entry = entries.find(item => item.chatKey === chatKey && item.answer === undefined) if (entry !== undefined) return { cardKey, entry } } @@ -503,6 +552,9 @@ export function install(rt) { && picked.length === 1 && (picked[0] === 1 || picked[0] === 2) && rt.approvalsView().some(entry => `a:${entry.id}` === held.cardKey)) { if (await answerApprovalCard(conn, msg.chat, held.cardKey, picked[0] === 1 ? 'allow' : 'reject')) return + } else if (held !== undefined && held.cardKey.startsWith('v:') && picked.length === 1) { + const label = choiceOf(viewById(held.cardKey.slice(2))?.view.view)?.options[picked[0] - 1]?.label + if (label !== undefined && await answerViewCard(conn, msg.chat, held.cardKey, label)) return } } if (target === undefined) { @@ -550,6 +602,14 @@ export function install(rt) { if (!(await answerApprovalCard(conn, chat, cardKey, choice))) await sendCardReply(conn, chat, text('stale')) } } + if (data.startsWith('v:') && cut > 2) { + const label = choiceOf(viewById(cardKey.slice(2))?.view.view)?.options[Number(choice)]?.label + if (label === undefined) { + await sendCardReply(conn, chat, text('stale')) + return + } + if (!(await answerViewCard(conn, chat, cardKey, label))) await sendCardReply(conn, chat, text('stale')) + } } // ------------------------------------------------------------------ cards @@ -559,10 +619,16 @@ export function install(rt) { const hint = question.multiSelect === true ? text('numbersMulti') : text('numbers') return `${text('question', { bot })}${question.question}${question.detail ? `\n${question.detail}` : ''}\n${options}\n(${hint})` }, + view: (bot, title, choice) => { + const options = choice.options.map((option, index) => `${index + 1}. ${option.label}${option.description ? ` - ${option.description}` : ''}`).join('\n') + const detail = choice.texts.join('\n') + return `${text('question', { bot })}${title ?? ''}${detail !== '' ? `\n${detail}` : ''}\n${options}\n(${text('numbers')})` + }, approval: (bot, entry) => `${text('approval', { bot })}${entry.tool}${entry.reason ? ` — ${entry.reason}` : ''}${entry.detail?.text ? `\n${entry.detail.text}` : ''}${entry.detail?.code ? `\n${entry.detail.code}` : ''}`, } const cardButtons = { question: (question) => (question.multiSelect === true ? undefined : (question.options ?? []).map((option, index) => ({ data: `q:${question.id}:${index}`, label: option.label }))), + view: (id, choice) => choice.options.map((option, index) => ({ data: `v:${id}:${index}`, label: option.label })), approval: (id) => [{ data: `a:${id}:allow`, label: text('allow') }, { data: `a:${id}:reject`, label: text('reject') }], } @@ -602,13 +668,19 @@ export function install(rt) { await sendCard(conn, cardKey, listeners(conn, { botId }), text('secretAsk', { bot: bot.name, name: request.name })) } } - // Views awaiting a reply: text form only. + // Views awaiting a reply: a single choice is a question card, any other view text only. for (const [sessionId, view] of Object.entries(state.views ?? {})) { const cardKey = `v:${view.id}` if (data.cards[cardKey] !== undefined) continue const botId = rt.chatOwners.get(sessionId) const bot = rt.botOf(botId) if (bot === undefined) continue + const choice = choiceOf(view.view) + if (choice !== undefined) { + const buttons = imPlatform(conn.platform)?.buttons === true ? cardButtons.view(view.id, choice) : undefined + await sendCard(conn, cardKey, listeners(conn, { botId }), cardText.view(bot.name, view.title, choice), buttons) + continue + } const form = viewCardText({ title: view.title, view: view.view }) await sendCard(conn, cardKey, listeners(conn, { botId }), `${text('viewShow', { bot: bot.name })}${form !== '' ? `\n${form}` : view.title ? `\n${view.title}` : ''}`) } diff --git a/ds-bot/test/im.test.js b/ds-bot/test/im.test.js index 30c8692a12..1a48515e25 100644 --- a/ds-bot/test/im.test.js +++ b/ds-bot/test/im.test.js @@ -487,6 +487,91 @@ test('a pending secret request sends text without buttons', async (t) => { assert.equal(card.buttons, undefined) }) +const PICK_VIEW = { id: 'v1', title: 'Ship it?', view: { type: 'stack', children: [{ type: 'text', text: 'The build is green.' }, { type: 'options', id: 'pick', options: [{ label: 'Ship' }, { label: 'Hold', description: 'wait a day' }] }] } } +const pairedChat = async (s) => { + const conn = await add(s) + s.rt.state.im.connections[conn.id].paired.push({ id: 'u1', name: 'U1', at: 1 }) + await s.rt.save() + await dm(s, 'u1', 'hi') + s.fake.sent.length = 0 + return conn +} +const showView = async (s, view = PICK_VIEW) => { + s.rt.state.views = { a: structuredClone(view) } + await s.rt.save() + await s.flush() +} + +test('a single-choice view card gets buttons, and a press answers the view', async (t) => { + const s = await start() + t.after(s.stop) + await pairedChat(s) + await showView(s) + const card = s.fake.sent.find(entry => entry.buttons !== undefined) + assert.ok(card, 'the view card has buttons') + assert.deepEqual(card.buttons, [{ data: 'v:v1:0', label: 'Ship' }, { data: 'v:v1:1', label: 'Hold' }]) + assert.match(card.text, /Chief asks: Ship it\?\nThe build is green\.\n1\. Ship\n2\. Hold - wait a day\n\(reply with the number\)/) + await s.fake.handlers.onAction({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, data: 'v:v1:1', messageId: 1 }) + assert.equal(s.rt.state.views.a, undefined, 'the press answers the view') + assert.equal(s.agents.get('a')?.followed, 1) + await s.flush() + assert.equal(s.fake.edited.length, 1, 'the answered card is edited shut') +}) + +test('a bare number answers the newest open view card', async (t) => { + const s = await start({ platform: 'dingtalk', adapter: fakeAdapter({ buttons: false }) }) + t.after(s.stop) + await pairedChat(s) + await showView(s) + assert.equal(s.fake.sent.at(-1).buttons, undefined) + s.prompts.length = 0 + await dm(s, 'u1', '1') + assert.equal(s.rt.state.views.a, undefined, 'the number answers the view') + assert.equal(s.agents.get('a')?.followed, 1) + assert.equal(s.prompts.length, 0, 'the number is not also sent as a message') +}) + +test('a press on a view that was already answered gets the stale text', async (t) => { + const s = await start() + t.after(s.stop) + await pairedChat(s) + await showView(s) + const press = () => s.fake.handlers.onAction({ chat: { key: 'dm-u1', kind: 'direct', title: 'U1' }, sender: { id: 'u1', name: 'U1' }, data: 'v:v1:0', messageId: 1 }) + await press() + assert.equal(s.agents.get('a')?.followed, 1) + await press() + assert.equal(s.agents.get('a')?.followed, 1, 'a second press answers nothing') + await s.flush() + assert.ok(s.fake.sent.some(entry => /already answered/.test(entry.text ?? '')), 'the late press gets the stale text') +}) + +test('a form view stays a text card and a number goes to the Bot', async (t) => { + const s = await start({ platform: 'dingtalk', adapter: fakeAdapter({ buttons: false }) }) + t.after(s.stop) + await pairedChat(s) + await showView(s, { id: 'v2', title: 'Details', view: { type: 'form', id: 'f', fields: [{ id: 'name', label: 'Name' }] } }) + const card = s.fake.sent.at(-1) + assert.match(card.text, /shared a card/) + assert.equal(card.buttons, undefined) + s.prompts.length = 0 + await dm(s, 'u1', '1') + assert.ok(s.rt.state.views.a !== undefined, 'the form view stays open') + assert.equal(s.prompts.length, 1, 'the number reaches the Bot as a message') +}) + +test('a view answered on the computer settles its IM card', async (t) => { + const s = await start() + t.after(s.stop) + await pairedChat(s) + await showView(s) + assert.ok(s.fake.sent.some(entry => entry.buttons !== undefined)) + await s.rt.answerView('a', 'v1', 'pick', { selected: ['Ship'] }) + await s.rt.save() + await s.flush() + assert.equal(s.fake.edited.length, 1, 'the IM copy is edited shut') + assert.match(s.fake.edited[0].text, /answered/) +}) + test('a read-only host runs no adapter; becoming writable starts it', async (t) => { const s = await start() t.after(s.stop)