From b4056005aa0c62c95252d2857a13489d10efaae5 Mon Sep 17 00:00:00 2001 From: Roman Pavlovskyi <634881+rpavlovs@users.noreply.github.com> Date: Wed, 11 Nov 2026 19:08:25 +0100 Subject: [PATCH] Deliver selected OpenUI replies to Telegram --- server/index.ts | 47 ++++- server/telegram-openui.ts | 259 ++++++++++++++++++++++++++++ server/telegram-progress.ts | 37 +++- test/helpers/telegram-native.ts | 33 +++- test/telegram-openui-native.test.ts | 233 +++++++++++++++++++++++++ test/telegram-openui.test.ts | 239 +++++++++++++++++++++++++ test/telegram-progress.test.ts | 58 +++++++ 7 files changed, 893 insertions(+), 13 deletions(-) create mode 100644 server/telegram-openui.ts create mode 100644 test/telegram-openui-native.test.ts create mode 100644 test/telegram-openui.test.ts diff --git a/server/index.ts b/server/index.ts index 52ea0093..0564628b 100755 --- a/server/index.ts +++ b/server/index.ts @@ -34,6 +34,7 @@ import { DEVICE_CATEGORIES, noStore, readReport, serveSettingsRead } from "./set import { settingsCatalog } from "./settings-catalog.ts"; import { inspectionRedactor } from "./settings-redaction.ts"; import { telegramReply, type TelegramText } from "./telegram-reply.ts"; +import { telegramOpenUi } from "./telegram-openui.ts"; import { TurnProgress } from "./telegram-progress.ts"; import { inboxState, legacyBinding, migrateTelegram, ownerPairing, pairedChats, replaceTelegramToken, telegramBotName, telegramStatus, type AgentTelegram, type TelegramConfig } from "./telegram-bots.ts"; import { isPrivateTelegramHuman, knockTelegram, telegramPersonName, migrateTelegramPeople, sameTelegramDelegate, telegramDelegateCurrent, telegramNamesakes, telegramPersonHandle, telegramSender, telegramSenderCurrent, telegramOwnerGroupCurrent, telegramReplySenderCurrent, telegramAudience, telegramSenderLabel, TELEGRAM_PEOPLE_UPGRADE, type TelegramPerson, type TelegramSender, type TelegramOwnerGroup, type TelegramDestination, type TelegramReplySender } from "./telegram-people.ts"; @@ -4197,6 +4198,7 @@ function showHeldErrors(bot: BotRecord, laneId: string, held: string[], why?: st * (cancelRecovery). */ function askAgain(bot: BotRecord, laneId: string, recovery: Recovery, delay: number) { + for (const waiter of telegramWorking.get(laneId) ?? []) waiter.progress.excludeComponentsOnContinuation(); // who asked for the turn that failed, before its end clears that const requester = laneRequester.get(laneId) ?? askedLast.get(laneId); recoveries.set(laneId, recovery); @@ -6235,6 +6237,7 @@ async function onRuntimeEvent(event: RuntimeEvent) { !retriedForContext.has(event.threadId) && !outsider(laneRequester.get(event.threadId)) && !person ) { + for (const waiter of telegramWorking.get(event.threadId) ?? []) waiter.progress.excludeComponentsOnContinuation(); retriedForContext.add(event.threadId); // what this turn was asked, not words still waiting for the next one const said = [...store.messagesFor(event.threadId)] @@ -6518,7 +6521,7 @@ async function onRuntimeEvent(event: RuntimeEvent) { } if (!pickingUp && !handedOver) telegramReturns.finish(event.threadId, event.preInputRefusal?.reason); if ((!pickingUp || rebuiltEnds) && !handedOver && !refolding) { - endTelegramWaits(event.threadId, event.preInputRefusal?.reason ?? (rebuilt === "gone" ? engineGoneNotice(bot.name) : undefined)); + endTelegramWaits(event.threadId, event.preInputRefusal?.reason ?? (rebuilt === "gone" ? engineGoneNotice(bot.name) : undefined), undefined, event.ok !== false && !rebuilt); } const heldPerson = laneTelegramPerson.get(event.threadId); if (heldPerson && event.ok === false && !handedOver && event.stopReason !== "interrupted" && !rebuilt) { @@ -7964,6 +7967,9 @@ async function startClaimedTurn( ): Promise { const bot = store.bot(botId); if (!bot) throw Object.assign(new Error("no such agent"), { status: 404 }); + if (opts.retry || opts.fallback || opts.carriedOn) { + for (const waiter of telegramWorking.get(opts.taskId ?? bot.threadId) ?? []) waiter.progress.excludeComponentsOnContinuation(); + } if (opts.serviceMessages) { if (!opts.presetMessage || (opts.roomId && opts.roomId !== opts.taskId) || opts.from || opts.routine || opts.rehearsal || (opts.requester && opts.requester !== "owner")) throw Object.assign(new Error("service wakes require their admitted private owner input"), { status: 409 }); assertServiceTurn(botId, opts.taskId, opts.serviceMessages); @@ -17078,11 +17084,11 @@ function forgetTelegramWaiter(waiter: TelegramWaiter): void { /** A lane's turn has ended: every request it took captures its answer. * `only`, when given, limits it to these requests. */ -function endTelegramWaits(laneId: string, failure?: string, only?: readonly string[]): void { +function endTelegramWaits(laneId: string, failure?: string, only?: readonly string[], componentEligible = false): void { // Signal's waits end at the same moments, for the same reasons. for (const waiter of [...(telegramWorking.get(laneId) ?? []), ...(signalWorking.get(laneId) ?? [])]) { if (only && !only.includes(waiter.progress.request ?? "")) continue; - waiter.progress.ended(failure); + waiter.progress.ended(failure, componentEligible); } // and a change to the home it asked for is for this turn alone if (!only) closeHomeRequests(laneId); @@ -17632,8 +17638,8 @@ const telegramReturns = new TelegramReturns( /** A reply to a chat. A failed send is not worth a crash. Only what an * agent wrote is read as Markdown: the bot's own lines and a card's * summary of a command are shown exactly as they are. */ -async function telegramSay(runtime: TelegramRuntime, chatId: number, text: string, markdown = false): Promise { - if (currentTelegram(runtime)) await telegram.send(runtime.token, chatId, text, markdown, undefined, () => Boolean(currentTelegram(runtime))).catch(() => {}); +async function telegramSay(runtime: TelegramRuntime, chatId: number, text: string, markdown = false, still?: () => boolean): Promise { + if (currentTelegram(runtime)) await telegram.send(runtime.token, chatId, text, markdown, undefined, () => Boolean(currentTelegram(runtime)) && (!still || still())).catch(() => {}); } /** Silent notes in this chat, paused while a card waits on the person. */ @@ -17867,7 +17873,11 @@ function deliverOverTelegram(runtime: TelegramRuntime, chatId: number, text: str // access taken back while it ran: the turn was stopped, and // nothing more of it goes to them if (!current()) return; - await telegramSay(runtime, chatId, answer.text, answer.markdown); + // This check and the call into the existing sender have no await + // between them. A changed row falls back to the ordinary answer. + const component = answer.component?.current() ? answer.component.reply() : undefined; + await telegramSay(runtime, chatId, component?.text ?? answer.text, component?.markdown ?? answer.markdown, + component ? () => current() && Boolean(answer.component?.current()) : undefined); await telegramFiles(runtime, chatId, answer.files, delegate); }); runtime.turns.set(chatId, turn); @@ -18163,11 +18173,12 @@ async function answerOverTelegram(botId: string, text: string, chatId: number, r const sent = steered ?? queueOnLane(botId, laneId, text, who); requestId = sent.id; progress.joined(Boolean(steered)); + if (!steered) progress.allowComponentsFor(sent.id); progress.observe(); // the turn may have ended while its engine was asked if (!steered) drainSteer(laneId); await answeredAfter(botId, laneId, sent.id, progress, runtime.abort.signal); - return replyIn(botId, progress); + return replyIn(botId, progress, !steered); } finally { await settled(); } @@ -18188,15 +18199,33 @@ async function answerOverTelegram(botId: string, text: string, chatId: number, r /** A Telegram turn's reply: what the agent said, and what it left. */ interface TelegramAnswer extends TelegramText { files: telegram.Deliverable[]; + component?: { reply: () => TelegramText | null; current: () => boolean }; } -function replyIn(botId: string, progress: TurnProgress): TelegramAnswer { +function replyIn(botId: string, progress: TurnProgress, componentEligible = false): TelegramAnswer { // A turn that could not run says why, as a request held through a // restart is told (telegramReturns), and left nothing to send. const failure = progress.failure; if (failure !== undefined) return { text: redactSecrets(failure), markdown: false, files: [] }; const messages = progress.answerSpan(); - return { ...telegramReply(messages, redactSecrets), files: deliverablesIn(botId, messages) }; + const answer: TelegramAnswer = { ...telegramReply(messages, redactSecrets), files: deliverablesIn(botId, messages) }; + if (!componentEligible) return answer; + const source = progress.componentSource(); + const bot = store.bot(botId); + if (source && bot) { + const disabled = [...(bot.withoutComponents ?? [])]; + const current = () => { + const latest = store.bot(botId); + return Boolean(latest && JSON.stringify(latest.withoutComponents ?? []) === JSON.stringify(disabled) && source.current()); + }; + answer.component = { + current, + // Render only at handoff, using the current secret redactor. An + // unexpected rendering failure leaves the ordinary reply intact. + reply: () => { try { return telegramOpenUi(source.message, disabled, redactSecrets); } catch { return null; } }, + }; + } + return answer; } /** diff --git a/server/telegram-openui.ts b/server/telegram-openui.ts new file mode 100644 index 00000000..4b50df04 --- /dev/null +++ b/server/telegram-openui.ts @@ -0,0 +1,259 @@ +// OpenUI inside one already-selected Telegram answer. Never reads other rows. +import { createParser } from "@openuidev/lang-core"; +import { extractUi, MAX_FENCES, MAX_SOURCE, MAX_STRING, safeHttpUrl, withheldShapes, type UiNode, type UiValue } from "./genui.ts"; +import { GENUI_ROOT, genuiShapes, isOpenUiLang } from "./genui-library.ts"; +import { messages } from "./telegram-format.ts"; +import type { Message } from "./store.ts"; +import type { TelegramText } from "./telegram-reply.ts"; + +const FENCE = /^\s*(```|~~~)\s*([\w+#.-]*)\s*$/; +const LIMITATION = "Some of this interface could not be displayed here."; +const INTERACTIVE = "These controls cannot be used here."; +let parser: ReturnType | undefined; + +/** Protect displayed data from being interpreted as a new Markdown block. */ +const literal = (s: string) => s.replace(/[\\`*_{}\[\]()#+\-.!|>~]/g, "\\$&"); +const node = (v: unknown): v is UiNode => Boolean(v && typeof v === "object" && !Array.isArray(v) && typeof (v as UiNode).type === "string" && (v as UiNode).props && typeof (v as UiNode).props === "object"); +const array = (v: unknown): UiValue[] => Array.isArray(v) ? v : []; + +/** The formatter already knows how to preserve a Markdown link's target in + * its plain fallback. Use a single uncut field here, then escape it as data. */ +function shownMarkdown(s: string): string { + // The ordinary formatter collapses consecutive blank prose lines. A + // component value can contain them as data, so read its lines separately. + return s.split("\n").map((line) => messages(line, true, Number.MAX_SAFE_INTEGER).map((part) => part.plain).join("\n")).join("\n"); +} + +function project(root: UiNode, truncated: boolean, disabled: Set, redact: (text: string) => string): string { + const lines: string[] = []; + let limited = truncated; + let controls = false; + const safe = (s: string) => literal(redact(s)); + const codeBlock = (s: string) => { + const longest = Math.max(2, ...s.split("\n").map((line) => /^\s*(`+)\s*$/.exec(line)?.[1]?.length ?? 0)); + const marker = "`".repeat(longest + 1); + return `${marker}\n${s}\n${marker}`; + }; + const emit = (s: string, label?: string) => { + const value = redact(s); + const heading = label === undefined ? undefined : redact(label); + if (value.includes("\r") || heading?.includes("\r")) limited = true; + const apart = heading !== undefined && (heading.includes("\n") || value.includes("\n") || heading.length + 2 + value.length > 4_000); + if (apart) { + if (heading.includes("\n")) { + if (heading.trim()) lines.push(codeBlock(heading)); else limited = true; + } else lines.push(`${literal(heading)}${heading.length < 4_000 ? ":" : ""}`); + } + if (value.includes("\n")) { + if (value.trim()) lines.push(codeBlock(value)); + else limited = true; + } else if (apart) { + // A value fits on its own, but the label could make the formatter + // cut at and discard one of the value's literal spaces. + lines.push(literal(value)); + } else { + lines.push(heading === undefined ? literal(value) : `${literal(heading)}: ${literal(value)}`); + } + }; + const string = (v: unknown): string | undefined => { + if (typeof v !== "string") return undefined; + // toTree clips at this exact length. A fence-wide flag cannot tell which + // field was cut, so do not show or link a potentially incomplete value. + if (truncated && v.length >= MAX_STRING) { limited = true; return undefined; } + return v; + }; + const say = (v: unknown, markdown = false) => { + const s = string(v); + if (s !== undefined && s !== "") emit(markdown ? shownMarkdown(s) : s); + }; + const labelValue = (label: string, value: unknown, markdown = false) => { + const s = string(value); + if (s !== undefined) emit(markdown ? shownMarkdown(s) : s, label); + else limited = true; + }; + const visibleValue = (v: unknown): string | undefined => { + if (typeof v === "number" && Number.isFinite(v)) return String(v); + if (typeof v === "boolean") return v ? "true" : "false"; + return string(v); + }; + const walk = (value: unknown): void => { + if (!node(value)) { if (value !== undefined) limited = true; return; } + if (disabled.has(value.type)) { limited = true; return; } + const p = value.props; + const children = (items: unknown) => { for (const child of array(items)) walk(child); }; + const fields = (...names: string[]) => { for (const name of names) say(p[name]); }; + switch (value.type) { + case "Card": children(p.children); break; + case "CardHeader": fields("title", "subtitle"); break; + case "TextContent": say(p.text, true); break; + case "Callout": fields("title"); say(p.description, true); break; + case "Separator": break; + case "Image": { + say(p.alt); + // An image source is not necessarily a displayed link, and the + // picture itself cannot be reconstructed from its alt text here. + if (p.src) limited = true; + break; + } + case "CodeBlock": { + const language = string(p.language); + const code = string(p.codeString); + if (language) say(language); + if (code === undefined) { limited = true; break; } + // Preserve code as code through the existing formatter, even when + // it contains its own fence. A longer marker cannot close within it. + const shownCode = redact(code); + lines.push(codeBlock(shownCode)); + if (code.includes("\r")) limited = true; + break; + } + case "Quote": fields("text", "from", "where"); break; + case "Tag": fields("text"); break; + case "TagBlock": for (const tag of array(p.tags)) say(tag); break; + case "MetricIndicator": fields("label", "value", "change", "trend"); break; + case "Table": { + const cols = array(p.columns); + if (!cols.length || cols.some((c) => !node(c) || c.type !== "Col")) { limited = true; break; } + const columns = cols as UiNode[]; + const data = columns.map((c) => array(c.props.data)); + const headings = columns.map((c, i) => string(c.props.label) ?? `Column ${i + 1}`); + const count = Math.max(0, ...data.map((c) => c.length)); + if (!count) headings.forEach((heading, i) => labelValue(`Column ${i + 1}`, heading)); + for (let r = 0; r < count; r++) { + lines.push(`Row ${r + 1}`); + for (let c = 0; c < columns.length; c++) { + const label = headings[c] || `Column ${c + 1} (blank heading)`; + const entry = data[c][r]; + const text = visibleValue(entry); + if (text === undefined) { limited = true; continue; } + emit(typeof entry === "string" ? shownMarkdown(text) : text, label); + } + } + break; + } + case "BarChart": + case "LineChart": + case "PieChart": { + fields("xLabel", "yLabel"); + const labels = array(p.labels); + const series = value.type === "PieChart" ? [{ type: "Series", props: { category: "", values: p.values } } as UiNode] : array(p.series); + if (!labels.length || !series.length) limited = true; + for (const item of series) { + if (!node(item) || item.type !== "Series") { limited = true; continue; } + say(item.props.category); + const values = array(item.props.values); + if (values.length !== labels.length) { + limited = true; + labels.forEach((name, i) => labelValue(`Label ${i + 1}`, name)); + values.forEach((amount, i) => labelValue(`Value ${i + 1}`, visibleValue(amount))); + continue; + } + const count = Math.max(labels.length, values.length); + for (let i = 0; i < count; i++) { + const name = string(labels[i]); + const amount = visibleValue(values[i]); + if (name !== undefined && amount !== undefined) emit(amount, name); + else { + limited = true; + if (name !== undefined) labelValue(`Label ${i + 1}`, name); + if (amount !== undefined) labelValue(`Value ${i + 1}`, amount); + } + } + } + break; + } + case "Steps": children(p.items); break; + case "StepsItem": fields("title", "details", "state"); break; + case "ListBlock": children(p.items); break; + case "ListItem": { + fields("title", "subtitle"); + if (p.image && typeof p.image === "object" && !Array.isArray(p.image)) say((p.image as Record).alt); + if (p.actionLabel !== undefined) { controls = true; say(p.actionLabel); } + break; + } + case "OptionCards": fields("title"); children(p.items); fields("note"); controls = true; break; + case "OptionCard": fields("title", "subtitle"); if (p.recommended === true) lines.push("Recommended"); controls = true; break; + case "Sources": + for (const item of array(p.items)) { + if (!item || typeof item !== "object" || Array.isArray(item)) { limited = true; continue; } + const source = item as Record; + say(source.title); say(source.sourceName); + const url = string(source.url); + if (url && safeHttpUrl(url) && redact(url) === url) lines.push(safe(url)); else if (source.url) limited = true; + } + break; + case "Tabs": case "Accordion": children(p.items); break; + case "TabItem": case "AccordionItem": fields("trigger"); children(p.content); break; + case "Buttons": children(p.buttons); controls = true; break; + case "Button": fields("label"); controls = true; break; + case "FollowUpBlock": children(p.items); controls = true; break; + case "FollowUpItem": fields("text"); controls = true; break; + case "Form": children(p.fields); walk(p.buttons); controls = true; break; + case "FormControl": fields("label"); walk(p.input); fields("hint"); controls = true; break; + case "Input": case "TextArea": fields("placeholder"); controls = true; break; + case "Select": fields("placeholder"); children(p.items); controls = true; break; + case "SelectItem": fields("label"); controls = true; break; + case "RadioGroup": case "CheckBoxGroup": children(p.items); controls = true; break; + case "RadioItem": case "CheckBoxItem": fields("label", "description"); controls = true; break; + case "Slider": { + fields("label"); + for (const name of ["min", "max", "step"]) if (typeof p[name] === "number" && Number.isFinite(p[name])) lines.push(`${safe(name)}: ${p[name]}`); + controls = true; break; + } + case "DatePicker": controls = true; break; + default: limited = true; + } + }; + walk(root); + if (controls) lines.push(INTERACTIVE); + if (limited) lines.push(LIMITATION); + return lines.join("\n"); +} + +/** Null means preserve the baseline reply exactly. */ +export function telegramOpenUi(message: Message, without: string[] | undefined, redact: (text: string) => string = (text) => text): TelegramText | null { + if (!message.text) return null; + const lines = message.text.split("\n"); + const out: string[] = []; + const disabled = new Set(withheldShapes(without)); + let fenceIndex = 0; + let found = false; + for (let i = 0; i < lines.length; i++) { + const open = lines[i]!.match(FENCE); + if (!open) { out.push(lines[i]!); continue; } + let end = i + 1; + while (end < lines.length && lines[end]!.trim() !== open[1]) end++; + if (!isOpenUiLang(open[2]!)) { + out.push(...lines.slice(i, Math.min(end + 1, lines.length))); + i = end; + continue; + } + found = true; + const code = lines.slice(i + 1, end).join("\n"); + const index = fenceIndex++; + const kept = message.ui?.find((entry) => entry.fence === index); + if (without?.includes("openui") || end >= lines.length || !code.trim() || code.length > MAX_SOURCE) { + out.push(LIMITATION); + } else { + try { + parser ??= createParser(genuiShapes.toJSONSchema(), GENUI_ROOT); + const parsed = parser.parse(code); + // Parse each fence separately: stored UI keeps only the first six. + const fresh = extractUi(`\`\`\`openui\n${code}\n\`\`\``)?.[0]; + const entry = index < MAX_FENCES ? kept : fresh; + if (!entry || !fresh || (kept && JSON.stringify({ ...kept, fence: 0 }) !== JSON.stringify(fresh))) { + out.push(LIMITATION); + } else { + const rendered = project(entry.tree, Boolean(entry.truncated), disabled, redact); + out.push(rendered || LIMITATION); + if (parsed.meta.errors.length && !rendered.includes(LIMITATION)) out.push(LIMITATION); + } + } catch { + out.push(LIMITATION); + } + } + i = end; + } + if (!found) return null; + return { text: out.join("\n").trim() || LIMITATION, markdown: true }; +} diff --git a/server/telegram-progress.ts b/server/telegram-progress.ts index e28de0ea..68afcb53 100644 --- a/server/telegram-progress.ts +++ b/server/telegram-progress.ts @@ -15,6 +15,11 @@ export class TurnProgress { private failed?: string; /** Which of the lane's turns the captured end was. */ private endedIn?: number; + /** A component may be added only to the exact request and selected text + * captured at this completion, never a later re-selection. */ + private componentCapture?: { request: string; reply: string; requestId: string; replyId: string }; + private componentsExcluded = false; + private componentRequest?: string; private join?: { after?: string; accepted: boolean }; private progress: Progress; private messages: () => readonly Message[]; @@ -50,6 +55,7 @@ export class TurnProgress { // the old turn did not consume this request this.finished = undefined; this.failed = undefined; + this.componentCapture = undefined; return; } this.join.accepted = true; @@ -109,7 +115,7 @@ export class TurnProgress { /** Capture before another turn can start, even if the waiter or the * answer send itself has to wait. A still-queued request is not this turn. * `failure` is why the turn could not run, said instead of its span. */ - ended(failure?: string): void { + ended(failure?: string, componentEligible = true): void { // While a steer is still being asked, a later end replaces an earlier // one: joined() keeps only the end of the turn the words went into. const tentative = this.join?.accepted === false; @@ -117,6 +123,11 @@ export class TurnProgress { this.finished = this.span(); this.failed = failure; this.endedIn = this.turn?.(); + const request = this.taken(); + const reply = this.finished.findLast(botWords); + if (!tentative && componentEligible && !this.componentsExcluded && request && request.id === this.componentRequest && !request.midTurn && reply) { + this.componentCapture = { request: JSON.stringify(request), reply: JSON.stringify(reply), requestId: request.id, replyId: reply.id }; + } // A refusal discards this tentative end and queues a different turn. if (!tentative) void this.stop(); } @@ -136,6 +147,30 @@ export class TurnProgress { return this.finished ?? this.span(); } + /** Only the explicitly identified queued waiter may capture a component. */ + allowComponentsFor(requestId: string): void { + this.componentRequest = requestId; + } + + /** A retry/pickup of a request already taken keeps its ordinary answer, + * but does not gain the narrower component addition. */ + excludeComponentsOnContinuation(): void { + if (!this.join && this.taken()) this.componentsExcluded = true; + } + + componentSource(): { message: Message; current: () => boolean } | null { + const captured = this.componentCapture; + if (!captured || !this.completed || this.failed !== undefined || this.componentsExcluded) return null; + const current = () => { + const rows = this.messages(); + const request = rows.find((m) => m.id === captured.requestId); + const reply = rows.find((m) => m.id === captured.replyId); + return Boolean(request && reply && !request.deleted && !request.queued && !request.unsent && !request.midTurn && + botWords(reply) && JSON.stringify(request) === captured.request && JSON.stringify(reply) === captured.reply); + }; + return current() ? { message: JSON.parse(captured.reply) as Message, current } : null; + } + stop(): Promise { this.closed = true; this.pending = undefined; diff --git a/test/helpers/telegram-native.ts b/test/helpers/telegram-native.ts index 6e076fc4..0340cba8 100644 --- a/test/helpers/telegram-native.ts +++ b/test/helpers/telegram-native.ts @@ -159,7 +159,7 @@ createInterface({ input: process.stdin }).on("line", async (line) => { if (rapid === "RAPID_INPUT") writeFileSync(join(deliverables(), "rapid.txt"), "rapid request work"); const receipt = rapid === "RAPID_REFUSE" ? { id: m.id, error: { code: -32000, message: "fixture cannot steer" } } : { id: m.id, result: { turnId: turn } }; const frames = [receipt]; - if (rapid !== "RAPID_EMPTY") frames.push({ method: "item/completed", params: { threadId: thread, turnId: turn, item: { type: "agentMessage", id: "rapid-final", text: rapid === "RAPID_REFUSE" ? "EARLIER RESULT" : "RAPID FINAL" } } }); + if (rapid !== "RAPID_EMPTY") frames.push({ method: "item/completed", params: { threadId: thread, turnId: turn, item: { type: "agentMessage", id: "rapid-final", text: rapid === "RAPID_REFUSE" ? "EARLIER RESULT" : existsSync(root + "/rapid-answer.json") ? JSON.parse(readFileSync(root + "/rapid-answer.json", "utf8")) : "RAPID FINAL" } } }); frames.push({ method: "turn/completed", params: { threadId: thread, turn: { id: turn, status: "completed" } } }); // One atomic pipe write: the receipt and completion precede the // awaiting caller's microtask that saves its accepted input row. @@ -179,8 +179,26 @@ createInterface({ input: process.stdin }).on("line", async (line) => { await nap(21_500); return finish("**Before**\\n\\n| Item | Price | Note |\\n| --- | --- | --- |\\n| [Sample A](https://example.com/a-(b)) | **12** | |\\n\\n- After"); } + if (text.includes("OPENUI_EMPTY")) return finish(null); + if (text.includes("OPENUI_FALLBACK")) return finish(null, "rate limit exceeded"); + if (text.includes("OPENUI_FAILED")) { + say("failed-ui", JSON.parse(readFileSync(root + "/openui-answer.json", "utf8"))); + return finish(null, "tool_stalled"); + } + if (text.includes("OPENUI_SAMPLE")) { + writeFileSync(root + "/openui-started", "1"); + if (existsSync(root + "/openui-progress")) { + say("openui-note", "Checking the sample."); + tool("openui-tool", "sample-check"); + await nap(21_500); + } + if (existsSync(root + "/openui-hold")) { + for (let i = 0; i < 3000 && !existsSync(root + "/openui-release"); i++) await nap(20); + } + return finish(JSON.parse(readFileSync(root + "/openui-answer.json", "utf8"))); + } if (text.includes("DELIVERY_BARRIER")) return finish("DELIVERY DONE"); - if (text.includes("RAPID_REFUSE")) return finish("QUEUED RESULT"); + if (text.includes("RAPID_REFUSE")) return finish(existsSync(root + "/rapid-queued-answer.json") ? JSON.parse(readFileSync(root + "/rapid-queued-answer.json", "utf8")) : "QUEUED RESULT"); if (text.includes("TIMEOUT_")) { if (text.includes("TIMEOUT_RETRY") && !existsSync(root + "/timeout-retried")) { writeFileSync(root + "/timeout-retried", "1"); @@ -208,6 +226,7 @@ createInterface({ input: process.stdin }).on("line", async (line) => { } for (let i = 0; i < 3000 && !existsSync(root + "/timeout-release"); i++) await nap(20); if (text.includes("TIMEOUT_EMPTY")) return finish(null); + if (text.includes("TIMEOUT_RETRY") && existsSync(root + "/retry-ui.json")) return finish(JSON.parse(readFileSync(root + "/retry-ui.json", "utf8"))); return finish("REQUEST RESULT"); } if (text.includes("CTX_HOLD")) { writeFileSync(root+"/ctx-started","1"); for(let i=0;i<1000&&!existsSync(root+"/ctx-release");i++)await nap(20);return finish("CTX DONE"); } @@ -365,13 +384,21 @@ function fakeClaude(root: string): string { const cli = join(root, "fake-claude.mjs"); writeFileSync(cli, `#!${process.execPath} import { createConnection } from "node:net"; -import { readFileSync } from "node:fs"; +import { readFileSync, writeFileSync } from "node:fs"; const args = process.argv.slice(2); +const root = ${JSON.stringify(root)}; if (args[0] === "--version") { console.log("2.1.299 (Claude Code)"); process.exit(0); } if (args[0] === "auth") { console.log(JSON.stringify({ loggedIn: true })); process.exit(0); } const out = (frame) => console.log(JSON.stringify(frame)); process.stdin.once("data", async (input) => { out({ type: "system", subtype: "init", session_id: "sess-native", model: "claude-sonnet-5" }); + if (String(input).includes("OPENUI_FALLBACK")) { + const text = JSON.parse(readFileSync(root + "/fallback-ui.json", "utf8")); + writeFileSync(root + "/fallback-started", "1"); + out({ type: "assistant", message: { content: [{ type: "text", text }] } }); + out({ type: "result", subtype: "success", is_error: false, num_turns: 1, duration_api_ms: 100, total_cost_usd: 0, session_id: "sess-native", result: text }); + return setTimeout(() => process.exit(0), 50); + } if (String(input).includes("CLAUDE_THINKING")) { out({ type: "system", subtype: "thinking_tokens", estimated_tokens: 32, text: "PRIVATE_THINKING" }); out({ type: "assistant", message: { content: [{ type: "thinking", thinking: "PRIVATE_THINKING" }, { type: "text", text: "A visible note." }] } }); diff --git a/test/telegram-openui-native.test.ts b/test/telegram-openui-native.test.ts new file mode 100644 index 00000000..77edabd1 --- /dev/null +++ b/test/telegram-openui-native.test.ts @@ -0,0 +1,233 @@ +// The actual intake, queue, waiter, selector and sender, with a fake engine +// and loopback Telegram. No provider, account or private transcript is used. +import assert from "node:assert/strict"; +import { existsSync, writeFileSync } from "node:fs"; +import { join } from "node:path"; +import { test } from "node:test"; +import { CHAT, STRANGER, fixture, heardContext, inputWords, sleep, waitFor } from "./helpers/telegram-native.ts"; + +type Fixture = Awaited>; +const sentTo = (f: Fixture, chat: number) => f.tg.calls("sendMessage").filter((c) => c.body.chat_id === chat && !c.body.disable_notification); +const table = (prose: boolean) => { + const labels = Array.from({ length: 30 }, (_, i) => `Item ${i + 1}`); + const values = Array.from({ length: 30 }, (_, i) => `${i + 1} kg`); + labels[9] = "[Source](https://example.com/a-(b))"; + const code = `root = Card([Table([Col("Name", ${JSON.stringify(labels)}), Col("Weight", ${JSON.stringify(values)})]), TextContent("Keep the note. fixture-stored-secret-123")])`; + return `${prose ? "Before the table.\n\n" : ""}\`\`\`openui\n${code}\n\`\`\`${prose ? "\n\nCaution after the table." : ""}`; +}; +async function grantDelegate(f: Fixture) { + f.tg.say("Hello there", undefined, STRANGER); + const person = await waitFor(async () => (await f.h.json(`/api/bots/${f.bot.id}/telegram`)).people.find((p: any) => p.name === "Lee"), "no delegate knock"); + await f.post(`/api/bots/${f.bot.id}/telegram`, { personId: person.id, action: "allow" }); + await waitFor(() => sentTo(f, STRANGER).some((c) => c.body.text.startsWith("This agent cannot answer from Telegram yet")), "no restricted notice"); + const response = await f.h.fetch(`/api/bots/${f.bot.id}/telegram`, { method: "POST", body: JSON.stringify({ personId: person.id, action: "full" }) }); + assert.equal(response.status, 200); + return person; +} + +for (const delegate of [false, true]) for (const prose of [false, true]) { + test(`${delegate ? "delegate" : "owner"} queued waiter receives ${prose ? "prose and" : "only"} selected OpenUI table`, async (t) => { + const f = await fixture(t); + if (delegate) await grantDelegate(f); + const chat = delegate ? STRANGER : CHAT; + writeFileSync(join(f.root, "openui-answer.json"), JSON.stringify(table(prose))); + // The phone request waits behind unrelated work; the fake engine refuses + // the steer, then the real queue starts its own consuming turn. + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, chat).length; + f.tg.say("OPENUI_SAMPLE", undefined, chat); + await waitFor(async () => (await f.messages()).some((m) => m.text === "OPENUI_SAMPLE" && m.queued), "request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => sentTo(f, chat).slice(before).some((c) => c.body.text.includes("Row 30")), "selected table was not sent"); + const answers = sentTo(f, chat).slice(before); + const text = answers.map((c) => c.body.text).join("\n"); + for (let i = 1; i <= 30; i++) { + assert.ok(text.includes(`Row ${i}`), `missing row ${i}`); + assert.ok(text.includes(`${i} kg`), `missing value ${i}`); + } + assert.ok(text.includes("Source (https://example.com/a-(b))")); + assert.ok(text.includes("Keep the note.")); + assert.ok(text.includes("[redacted]")); + assert.ok(!text.includes("fixture-stored-secret-123")); + assert.equal(text.includes("Before the table."), prose); + assert.equal(text.includes("Caution after the table."), prose); + assert.ok(!text.includes("EARLIER FINAL")); + assert.ok(!text.includes("root = Card")); + assert.equal(heardContext(f).filter((c) => c.method === "turn/start" && inputWords(c).includes("OPENUI_SAMPLE")).length, 1); + assert.equal(sentTo(f, delegate ? CHAT : STRANGER).some((c) => c.body.text.includes("Row 30")), false); + await sleep(250); + assert.equal(sentTo(f, chat).slice(before).filter((c) => c.body.text.includes("Row 30")).length, 1); + }); +} + +test("deleting the selected row during cleanup keeps the ordinary reply and never exports its component", async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "openui-answer.json"), JSON.stringify(table(false))); + writeFileSync(join(f.root, "openui-progress"), "1"); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("OPENUI_SAMPLE", undefined, CHAT); + await waitFor(async () => (await f.messages()).some((m) => m.text === "OPENUI_SAMPLE" && m.queued), "request did not queue"); + f.tg.state.holdDelete = true; + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => f.tg.state.deleteReleases.length > 0, "progress cleanup did not hold", 40_000); + const selected = await waitFor(async () => (await f.messages()).find((m) => m.role === "bot" && m.text?.includes("```openui")), "final row missing"); + const deleted = await f.h.fetch(`/api/threads/${f.bot.threadId}/messages/${selected.id}`, { method: "DELETE" }); + assert.equal(deleted.status, 200); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_LATER" }); + await waitFor(async () => (await f.messages()).some((m) => m.text === "LATER FINAL"), "later turn did not finish"); + f.tg.state.holdDelete = false; + f.tg.state.deleteReleases.splice(0).forEach((release) => release()); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("interactive")), "ordinary fallback did not arrive"); + const text = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!text.includes("Row 30")); + assert.ok(!text.includes("LATER FINAL")); +}); + +test("two queued requests share their consuming turn without borrowing adjacent turns", async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "openui-answer.json"), JSON.stringify(table(true))); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("OPENUI_SAMPLE", undefined, CHAT); + await waitFor(async () => (await f.messages()).some((m) => m.text === "OPENUI_SAMPLE" && m.queued), "first request did not queue"); + const second = await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SECOND_BURST" }); + assert.equal(second.queued, true); + await waitFor(async () => (await f.messages()).some((m) => m.text === "SECOND_BURST" && m.queued), "second request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("Row 30")), "table did not arrive"); + const starts = heardContext(f).filter((c) => c.method === "turn/start" && inputWords(c).includes("OPENUI_SAMPLE")); + assert.equal(starts.length, 1); + assert.ok(inputWords(starts[0]).includes("SECOND_BURST")); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("EARLIER FINAL")); + assert.equal(sentTo(f, CHAT).slice(before).filter((c) => c.body.text.includes("Row 30")).length, 1); +}); + +test("a separate show row during the eligible turn is not collected or announced", async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "openui-answer.json"), JSON.stringify(table(false))); + writeFileSync(join(f.root, "openui-hold"), "1"); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("OPENUI_SAMPLE", undefined, CHAT); + await waitFor(async () => (await f.messages()).some((m) => m.text === "OPENUI_SAMPLE" && m.queued), "request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => existsSync(join(f.root, "openui-started")), "consuming turn did not start"); + const shown = await f.h.fetch(`/api/bots/${f.bot.id}/show`, { method: "POST", body: JSON.stringify({ kind: "chart", data: { title: "SEPARATE_SHOW_MARKER", bars: [{ label: "Separate", value: 42 }] } }) }); + assert.equal(shown.status, 201); + assert.ok((await f.messages()).some((m) => m.kind === "component" && JSON.stringify(m.component).includes("SEPARATE_SHOW_MARKER"))); + writeFileSync(join(f.root, "openui-release"), "1"); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("Row 30")), "selected table did not arrive"); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("SEPARATE_SHOW_MARKER")); + assert.ok(!output.includes("could not be displayed"), "an unrelated show row must not generate a limitation notice"); +}); + +test("an accepted mid-turn steer keeps the ordinary interactive fallback", async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "rapid-answer.json"), JSON.stringify(table(false))); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "CTX_HOLD" }); + await waitFor(() => existsSync(join(f.root, "ctx-started")), "prior turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("RAPID_INPUT", undefined, CHAT); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("interactive")), "ordinary interactive answer did not arrive"); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("Row 30")); + assert.ok(heardContext(f).some((c) => c.method === "turn/steer" && inputWords(c).includes("RAPID_INPUT"))); +}); + +test("revoking a full-access grant before completion prevents the added component reaching that chat", async (t) => { + const f = await fixture(t); + const person = await grantDelegate(f); + writeFileSync(join(f.root, "openui-answer.json"), JSON.stringify(table(false))); + writeFileSync(join(f.root, "openui-hold"), "1"); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, STRANGER).length; + f.tg.say("OPENUI_SAMPLE", undefined, STRANGER); + await waitFor(async () => (await f.messages()).some((m) => m.text === "OPENUI_SAMPLE" && m.queued), "delegate request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => existsSync(join(f.root, "openui-started")), "delegate turn did not start"); + const changed = await f.h.fetch(`/api/bots/${f.bot.id}/telegram`, { method: "POST", body: JSON.stringify({ personId: person.id, action: "restricted" }) }); + assert.equal(changed.status, 200); + writeFileSync(join(f.root, "openui-release"), "1"); + await waitFor(async () => !(await f.h.json("/api/bots?messages=0")).bots.find((b: any) => b.id === f.bot.id).busy, "delegate turn did not settle"); + await sleep(500); + assert.ok(sentTo(f, STRANGER).slice(before).every((c) => !c.body.text.includes("Row 30"))); + assert.ok(sentTo(f, CHAT).every((c) => !c.body.text.includes("Row 30"))); +}); + +test("a queued request retried after failure keeps the ordinary interactive fallback", async (t) => { + const f = await fixture(t, "codex", true); + writeFileSync(join(f.root, "retry-ui.json"), JSON.stringify(table(false))); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("TIMEOUT_RETRY", undefined, CHAT); + await waitFor(async () => (await f.messages()).some((m) => m.text === "TIMEOUT_RETRY" && m.queued), "retry request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => existsSync(join(f.root, "timeout-retried")), "first attempt did not fail"); + await waitFor(() => existsSync(join(f.root, "timeout-started")), "ordinary retry did not start"); + writeFileSync(join(f.root, "timeout-release"), "1"); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("interactive")), "ordinary fallback did not arrive"); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("Row 30")); + assert.equal(heardContext(f).filter((c) => c.method === "turn/start" && inputWords(c).includes("TIMEOUT_RETRY")).length, 2); +}); + +test("a queued request handed to a backup keeps the ordinary interactive fallback", async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "fallback-ui.json"), JSON.stringify(table(false))); + const settings = await f.h.fetch(`/api/bots/${f.bot.id}`, { method: "PATCH", body: JSON.stringify({ backupSelection: { instanceId: "claude", model: "claude-sonnet-5" } }) }); + assert.equal(settings.status, 200); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("OPENUI_FALLBACK", undefined, CHAT); + await waitFor(async () => (await f.messages()).some((m) => m.text === "OPENUI_FALLBACK" && m.queued), "backup request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + await waitFor(() => existsSync(join(f.root, "fallback-started")), "backup did not start"); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("interactive")), "ordinary fallback did not arrive"); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("Row 30")); + assert.equal(heardContext(f).filter((c) => c.method === "turn/start" && inputWords(c).includes("OPENUI_FALLBACK")).length, 1); +}); + +test("a refused steer whose old turn ended before the receipt uses only the later consuming answer", async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "rapid-queued-answer.json"), JSON.stringify(table(false))); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "CTX_HOLD" }); + await waitFor(() => existsSync(join(f.root, "ctx-started")), "prior turn did not start"); + const before = sentTo(f, CHAT).length; + f.tg.say("RAPID_REFUSE", undefined, CHAT); + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes("Row 30")), "queued table did not arrive"); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("EARLIER RESULT")); + assert.equal(heardContext(f).filter((c) => c.method === "turn/start" && inputWords(c).includes("RAPID_REFUSE")).length, 1); +}); + +for (const kind of ["EMPTY", "FAILED"] as const) test(`a ${kind.toLowerCase()} consuming turn keeps its ordinary answer when a later turn succeeds`, async (t) => { + const f = await fixture(t); + writeFileSync(join(f.root, "openui-answer.json"), JSON.stringify(table(false))); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_MAIN_HOLD" }); + await waitFor(() => existsSync(join(f.root, "scope-force-queue")), "earlier turn did not start"); + const before = sentTo(f, CHAT).length; + const request = `OPENUI_${kind}`; + f.tg.say(request, undefined, CHAT); + await waitFor(async () => (await f.messages()).some((m) => m.text === request && m.queued), "request did not queue"); + writeFileSync(join(f.root, "main-release"), "1"); + const expected = kind === "EMPTY" ? "finished without saying anything" : "interactive"; + await waitFor(() => sentTo(f, CHAT).slice(before).some((c) => c.body.text.includes(expected)), "ordinary answer did not arrive"); + await f.post(`/api/bots/${f.bot.id}/messages`, { text: "SCOPED_LATER" }); + await waitFor(async () => (await f.messages()).some((m) => m.text === "LATER FINAL"), "later turn did not succeed"); + await sleep(250); + const output = sentTo(f, CHAT).slice(before).map((c) => c.body.text).join("\n"); + assert.ok(!output.includes("Row 30")); + assert.ok(!output.includes("LATER FINAL")); + assert.equal(heardContext(f).filter((c) => c.method === "turn/start" && inputWords(c).includes(request)).length, 1); +}); diff --git a/test/telegram-openui.test.ts b/test/telegram-openui.test.ts new file mode 100644 index 00000000..bb159f89 --- /dev/null +++ b/test/telegram-openui.test.ts @@ -0,0 +1,239 @@ +import assert from "node:assert/strict"; +import { test } from "node:test"; +import { extractUi, MAX_STRING } from "../server/genui.ts"; +import { deliveries } from "../server/telegram-format.ts"; +import { telegramOpenUi } from "../server/telegram-openui.ts"; +import { send } from "../server/telegram.ts"; +import type { Message } from "../server/store.ts"; + +const fence = (s: string) => `\`\`\`openui\n${s}\n\`\`\``; +const message = (text: string): Message => ({ id: "answer", at: 1, role: "bot", kind: "text", text, ui: extractUi(text) }); +const plain = (text: string) => deliveries(text, true).flatMap((d) => d.chunks.map((c) => c.plain)).join("\n"); +const table = () => { + const labels = Array.from({ length: 30 }, (_, i) => `Item ${i + 1}`); + const values = Array.from({ length: 30 }, (_, i) => `${i + 1} kg`); + labels[4] = "A | B\nsecond line"; + labels[9] = "[Source](https://example.com/a-(b))"; + return `root = Card([t, n])\nt = Table([Col("Name", ${JSON.stringify(labels)}), Col("Weight", ${JSON.stringify(values)})])\nn = TextContent("Caution: keep all rows.")`; +}; + +test("selected answer retains every table row, prose, literal data and link in real plain fallback", () => { + const input = `Before.\n\n${fence(table())}\n\nAfter.`; + const result = telegramOpenUi(message(input), []); + assert.ok(result); + const text = plain(result.text); + assert.ok(text.indexOf("Before.") < text.indexOf("Row 1")); + assert.ok(text.indexOf("Caution: keep all rows.") < text.indexOf("After.")); + for (let i = 1; i <= 30; i++) { + assert.ok(text.includes(`Row ${i}`), `missing row ${i}`); + assert.ok(text.includes(`${i} kg`), `missing value ${i}`); + } + assert.match(text, /A \| B\nsecond line/); + assert.ok(text.includes("Source (https://example.com/a-(b))")); + assert.doesNotMatch(text, /root = Card/); +}); + +test("literal blank lines and a long spaced cell survive actual delivery parts", () => { + const long = `${"a".repeat(3920)} ${"b".repeat(70)}`; + const label = "A very long heading ".repeat(4); + const code = `root = Card([Table([Col(${JSON.stringify(label)}, ${JSON.stringify(["first | part\n\n\nlast", long])})])])`; + const result = telegramOpenUi(message(fence(code)), []); + assert.ok(result); + const parts = deliveries(result.text, true).flatMap((d) => d.chunks.map((c) => c.plain)); + assert.ok(parts.join("").includes("first | part\n\n\nlast")); + assert.ok(parts.join("").includes(long), "no literal space may be lost at a message boundary"); +}); + +test("capped cells and unresolved values retain independent content with a limitation", () => { + const values = Array.from({ length: 201 }, (_, i) => `value ${i + 1}`); + const code = [ + 'root = Card([h, t, u])', + 'h = TextContent("Independent")', + `t = Table([Col("Values", ${JSON.stringify(values)})])`, + 'u = TextContent(missingReference)', + ].join("\n"); + const result = telegramOpenUi(message(fence(code)), []); + assert.ok(result); + const text = plain(result.text); + assert.ok(text.includes("Independent")); + assert.ok(text.includes("value 200")); + assert.ok(!text.includes("value 201")); + assert.ok(text.includes("could not be displayed")); + assert.ok(!text.includes("missingReference")); +}); + +test("an unresolved reference leaves its valid sibling and a local limitation", () => { + const code = 'root = Card([TextContent("Independent"), TextContent(missingReference)])'; + const result = telegramOpenUi(message(fence(code)), []); + assert.ok(result); + const text = plain(result.text); + assert.ok(text.includes("Independent")); + assert.ok(text.includes("could not be displayed")); + assert.ok(!text.includes("missingReference")); +}); + +test("possibly clipped cell and heading are omitted while an independent column survives", () => { + const clipped = "z".repeat(MAX_STRING + 1); + const code = `root = Card([Table([Col(${JSON.stringify(clipped)}, [${JSON.stringify(clipped)}]), Col("Safe heading", ["Safe value"])])])`; + const result = telegramOpenUi(message(fence(code)), []); + assert.ok(result); + const text = plain(result.text); + assert.ok(text.includes("Safe heading: Safe value")); + assert.ok(text.includes("could not be displayed")); + assert.ok(!text.includes("z".repeat(100))); +}); + +test("more than six independent fences are kept, with malformed siblings limited", () => { + const fences = Array.from({ length: 8 }, (_, i) => fence(`root = Card([TextContent("Part ${i + 1}")])`)); + fences.splice(3, 0, fence('root = Card([TextContent("Safe"), Dashboard("secret")])')); + const text = plain(telegramOpenUi(message(fences.join("\n\n")), [])!.text); + for (let i = 1; i <= 8; i++) assert.ok(text.includes(`Part ${i}`)); + assert.ok(text.includes("Safe")); + assert.ok(text.includes("could not be displayed")); + assert.ok(!text.includes("secret")); +}); + +test("ordinary code fences are preserved and an unclosed OpenUI source is not dumped", () => { + const source = ['```text', '```openui', 'not an interface', '```', '', fence('root = Card([TextContent("Visible")])'), '', '```openui', 'root = Card([TextContent("private source")])'].join("\n"); + const result = telegramOpenUi(message(source), []); + assert.ok(result); + assert.ok(result.text.includes("not an interface")); + assert.ok(result.text.includes("Visible")); + assert.ok(result.text.includes("could not be displayed")); + assert.ok(!result.text.includes("private source")); +}); + +test("truncated data and uneven chart arrays get honest limits without action payloads", () => { + const code = [ + 'root = Card([h, chart, b])', + `h = TextContent(${JSON.stringify("x".repeat(MAX_STRING + 10))})`, + 'chart = BarChart(["A", "B"], [Series("Weight", [11])])', + 'b = Buttons([Button("Act", @ToAssistant("HIDDEN_ACTION", {secret: "HIDDEN_STATE"}))])', + ].join("\n"); + const text = plain(telegramOpenUi(message(fence(code)), [])!.text); + assert.ok(text.includes("Label 1: A")); + assert.ok(text.includes("Label 2: B")); + assert.ok(text.includes("Value 1: 11")); + assert.ok(!text.includes("A: 11")); + assert.ok(text.includes("controls cannot be used")); + assert.ok(text.includes("could not be displayed")); + assert.ok(!text.includes("HIDDEN_ACTION")); + assert.ok(!text.includes("HIDDEN_STATE")); + assert.ok(!text.includes("x".repeat(100))); +}); + +test("clipped source links and disabled nodes do not hide independent text", () => { + const code = [ + 'root = Card([t, s, chart])', + 't = TextContent("Independent")', + `s = Sources([{title: "Source", url: ${JSON.stringify("https://example.com/" + "a".repeat(MAX_STRING))}}])`, + 'chart = BarChart(["A"], [Series("S", [1])])', + ].join("\n"); + const text = plain(telegramOpenUi(message(fence(code)), ["chart"])!.text); + assert.ok(text.includes("Independent")); + assert.ok(text.includes("Source")); + assert.ok(!text.includes("https://example.com/")); + assert.ok(text.includes("could not be displayed")); +}); + +test("newly projected values pass through redaction before formatting and unsafe links are omitted", () => { + const secret = "sk_fixture_SECRET_12345"; + const code = [ + 'root = Card([text, table, src, block])', + `text = TextContent(${JSON.stringify(`Text ${secret}`)})`, + `table = Table([Col("Value", [${JSON.stringify(secret)}])])`, + `src = Sources([{title: "Source", url: ${JSON.stringify(`https://example.com/${secret}`)}}])`, + `block = CodeBlock("text", ${JSON.stringify(`code ${secret}`)})`, + ].join("\n"); + const rendered = telegramOpenUi(message(fence(code)), [], (s) => s.replaceAll(secret, "[redacted]")); + assert.ok(rendered); + const text = plain(rendered.text); + assert.doesNotMatch(text, /sk_fixture_SECRET_12345/); + assert.ok(text.includes("Text [redacted]")); + assert.ok(text.includes("Value: [redacted]")); + assert.ok(text.includes("code [redacted]")); + assert.ok(!text.includes("https://example.com/")); + assert.ok(text.includes("could not be displayed")); +}); + +test("ordinary text keeps its original path; a disabled OpenUI fence has a local limitation", () => { + assert.equal(telegramOpenUi(message("ordinary"), []), null); + const disabled = telegramOpenUi(message(fence(table())), ["openui"]); + assert.ok(disabled); + assert.ok(disabled.text.includes("could not be displayed")); + assert.ok(!disabled.text.includes("Row 1")); +}); + +test("a missing stored tree inside the retained fence range is not synthesized", () => { + const selected = message(fence('root = Card([TextContent("Do not synthesize")])')); + selected.ui = undefined; + const result = telegramOpenUi(selected, []); + assert.ok(result); + assert.ok(!result.text.includes("Do not synthesize")); + assert.ok(result.text.includes("could not be displayed")); +}); + +test("visible component families retain labels and descriptions but never hidden form or action data", () => { + const code = [ + 'root = Card([h, c, q, tags, metric, steps, list, opt, tabs, form, sources, img, block])', + 'h = CardHeader("Heading", "Subtitle")', + 'c = Callout("warning", "Warning", "A caution")', + 'q = Quote("Quoted words", "Speaker", "Book")', + 'tags = TagBlock(["First tag", "Second tag"])', + 'metric = MetricIndicator("Speed", "42 km/h", "+2 km/h", "up")', + 'steps = Steps([StepsItem("Step one", "Step detail", "done")])', + 'list = ListBlock([ListItem("List title", "List detail", {src: "https://example.com/image", alt: "List image"}, "Open", @ToAssistant("HIDDEN_ACTION"))])', + 'opt = OptionCards([OptionCard("HIDDEN_ID", "Choice label", "Choice detail", true)], "Choose", "Choice note")', + 'tabs = Tabs([TabItem("HIDDEN_TAB", "Tab label", [TextContent("Tab content")])])', + 'form = Form("HIDDEN_FORM", Buttons([Button("Submit", @ToAssistant("HIDDEN_SUBMIT"))]), [FormControl("Name label", Input("HIDDEN_NAME", "Enter name", "text"), "Name hint"), FormControl("Pick label", Select("HIDDEN_SELECT", [SelectItem("HIDDEN_VALUE", "Choice A")]))])', + 'sources = Sources([{title: "Source title", url: "https://example.com/ref", sourceName: "Source name"}])', + 'img = Image("An image", "https://example.com/image")', + `block = CodeBlock("js", ${JSON.stringify('const x = "value";\n```\nnext')})`, + ].join("\n"); + const text = plain(telegramOpenUi(message(fence(code)), [])!.text); + for (const expected of ["Heading", "Subtitle", "Warning", "A caution", "Quoted words", "Speaker", "Book", "First tag", "Speed", "42 km/h", "Step detail", "List image", "Choice label", "Choice detail", "Recommended", "Tab label", "Tab content", "Name label", "Enter name", "Name hint", "Pick label", "Choice A", "Source title", "https://example.com/ref", "An image", "const x = \"value\";\n```\nnext"]) { + assert.ok(text.includes(expected), `missing ${expected}`); + } + assert.ok(text.includes("controls cannot be used")); + assert.doesNotMatch(text, /HIDDEN_/); +}); + +test("actual sender's definite formatting refusal keeps every row and link in plain fallback", async (t) => { + const result = telegramOpenUi(message(fence(table())), [])!; + const calls: Array> = []; + t.mock.method(globalThis, "fetch", async (_url: unknown, init?: RequestInit) => { + const body = JSON.parse(String(init?.body)); + calls.push(body); + if (body.parse_mode) return Response.json({ ok: false, error_code: 400, description: "fixture rejects formatting" }, { status: 400 }); + return Response.json({ ok: true, result: { message_id: 1 } }); + }); + await send("TEST", 10101, result.text, result.markdown); + const fallback = calls.filter((c) => !c.parse_mode).map((c) => c.text).join("\n"); + const rich = calls.filter((c) => c.parse_mode === "HTML").map((c) => c.text).join("\n"); + for (const output of [rich, fallback]) { + let previous = -1; + for (let i = 1; i <= 30; i++) { + const at = output.indexOf(`${i} kg`, previous + 1); + assert.ok(at > previous, `missing or reordered value ${i}`); + previous = at; + if (i !== 5 && i !== 10) assert.ok(output.includes(`Item ${i}`), `missing label ${i}`); + } + assert.ok(output.includes("A | B\nsecond line")); + assert.ok(output.includes("https://example.com/a-(b)")); + } + assert.ok(calls.some((c) => c.parse_mode === "HTML")); +}); + +test("actual sender stops after an uncertain partial result without retry", async (t) => { + const values = Array.from({ length: 30 }, (_, i) => `${i + 1} ${"v".repeat(220)}`); + const code = `root = Card([Table([Col("Values", ${JSON.stringify(values)})])])`; + const result = telegramOpenUi(message(fence(code)), [])!; + const calls: Array> = []; + t.mock.method(globalThis, "fetch", async (_url: unknown, init?: RequestInit) => { + calls.push(JSON.parse(String(init?.body))); + if (calls.length === 2) throw new TypeError("fixture connection lost"); + return Response.json({ ok: true, result: { message_id: 1 } }); + }); + await assert.rejects(send("TEST", 10101, result.text, result.markdown)); + assert.equal(calls.length, 2, "an uncertain part must not be retried"); +}); diff --git a/test/telegram-progress.test.ts b/test/telegram-progress.test.ts index c63dd7d6..386afbdf 100644 --- a/test/telegram-progress.test.ts +++ b/test/telegram-progress.test.ts @@ -271,3 +271,61 @@ test("a turn that could not run answers with why, and a refused steer forgets it assert.equal(g.turn.completed, false); await g.turn.stop(); }); + +test("component source is the exact non-joined request and final row captured at completion", async () => { + const rows = [row("older", "user"), row("OLD", "bot")]; + const f = fixture(rows); + f.turn.joining(); + rows.push(row("OLD END", "bot")); + f.turn.ended(); + const request = row("request", "user", { queued: true }); + rows.push(request); + f.turn.joined(false); + f.turn.allowComponentsFor(request.id); + request.queued = false; + const answer = row("OUR ANSWER", "bot", { ui: [{ fence: 0, tree: { type: "Card", props: {} } }] }); + rows.push(row("earlier note", "bot"), answer); + f.turn.ended(); + rows.push(row("next", "user"), row("OTHER ANSWER", "bot")); + const captured = f.turn.componentSource(); + assert.equal(captured?.message.id, answer.id); + assert.equal(captured?.current(), true); + answer.ui![0]!.tree.props.changed = "new"; + assert.equal(captured?.current(), false, "a same-ID changed UI must not pass at handoff"); + assert.equal(f.turn.componentSource(), null); + await f.turn.stop(); +}); + +test("component source rejects changed or deleted request and reply at handoff", async () => { + for (const which of ["request", "reply"] as const) for (const change of ["deleted", "text", "quiet"] as const) { + const request = row("request", "user"); + const answer = row("answer", "bot"); + const rows = [request, answer]; + const f = fixture(rows); + f.turn.allowComponentsFor(request.id); + f.turn.ended(); + const source = f.turn.componentSource(); + assert.ok(source); + const target = which === "request" ? request : answer; + if (change === "text") target.text = "changed"; + else if (change === "deleted") target.deleted = true; + else target.quiet = true; + assert.equal(source.current(), false, `${which} ${change}`); + await f.turn.stop(); + } +}); + +test("empty, failed, joined and continued turns never borrow a later component source", async () => { + for (const kind of ["empty", "failed", "joined", "continued"] as const) { + const request = row("request", "user", kind === "joined" ? { midTurn: true } : {}); + const rows = [request]; + const f = fixture(rows); + f.turn.allowComponentsFor(request.id); + if (kind !== "empty") rows.push(row("current", "bot")); + if (kind === "continued") f.turn.excludeComponentsOnContinuation(); + f.turn.ended(undefined, kind !== "failed"); + rows.push(row("later", "user"), row("later answer", "bot")); + assert.equal(f.turn.componentSource(), null, kind); + await f.turn.stop(); + } +});