The Open Framework for Building Agents
[2/7] fix(eve): answer stale Telegram presses and every pending Discord request (#4241)
Ben Pankowb842fe05668c
diff --git a/.changeset/discord-two-question-presses.md b/.changeset/discord-two-question-presses.mdnew file mode 100644index 000000000..d3b2c022f--- /dev/null+++ b/.changeset/discord-two-question-presses.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Discord buttons now answer every pending request when a turn asks more than one at once, such as two questions, two tool approvals, or a question beside an approval. Before, only the first request's buttons reached the session; pressing another request's buttons failed with "the target session was not found via continuation token".diff --git a/.changeset/telegram-repeated-button-press.md b/.changeset/telegram-repeated-button-press.mdnew file mode 100644index 000000000..0febfa530--- /dev/null+++ b/.changeset/telegram-repeated-button-press.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Pressing a Telegram question button again after the question is answered now sends that option to the agent as a new message, as it does on other channels. Before, the press was acknowledged and dropped. Button presses also act as the person who pressed them instead of an anonymous caller.diff --git a/packages/eve/src/internal/testing/channel-conformance/conformance.ts b/packages/eve/src/internal/testing/channel-conformance/conformance.tsindex 25906acdf..1691f61e0 100644--- a/packages/eve/src/internal/testing/channel-conformance/conformance.ts+++ b/packages/eve/src/internal/testing/channel-conformance/conformance.ts@@ -33,12 +33,6 @@ interface ConformanceChannel {readonly unsupported?: Partial<Record<HitlRule, string>>;}-const DISCORD_ALIAS: BrokenCell = {- reason:- "a press finds the session by its message id, and only the first message the bot posts is aliased to the session",- symptom: /Timed out waiting for \w+ to (return|run or be denied) on discord/,-};-const TUI_TYPED_APPROVAL ="the approval drawer holds the keyboard; a person answers it with y or n";@@ -56,33 +50,13 @@ const TUI_TYPED_APPROVAL =*/const hitlConformance = {"chat-sdk": [{ driver: chatSdkDriver }, { driver: chatSdkTextDriver }],- discord: [- {- driver: discordDriver,- broken: {- "pressing options of two pending questions answers each with its own option": DISCORD_ALIAS,- "pressing Approve on one of two pending approvals runs only that tool": DISCORD_ALIAS,- "answering an approval and a question pending together settles both": DISCORD_ALIAS,- },- },- ],+ discord: [{ driver: discordDriver }],github: [{ driver: githubDriver }],linear: [{ driver: linearDriver }],linq: [{ driver: linqDriver }],slack: [{ driver: slackDriver }],teams: [{ driver: teamsDriver }],- telegram: [- {- driver: telegramDriver,- broken: {- "pressing an option of an answered question sends it to the agent as new input": {- reason:- "the first press consumes the button's callback id, so a later press is acknowledged and dropped",- symptom: /Timed out waiting for a reply to "Saturday" on telegram/,- },- },- },- ],+ telegram: [{ driver: telegramDriver }],tui: [{driver: tuiDriver,diff --git a/packages/eve/src/public/channels/discord/discordChannel.ts b/packages/eve/src/public/channels/discord/discordChannel.tsindex 3ea17bdf9..c8040559d 100644--- a/packages/eve/src/public/channels/discord/discordChannel.ts+++ b/packages/eve/src/public/channels/discord/discordChannel.ts@@ -336,10 +336,18 @@ function buildDiscordHandle(input: {const state = input.state;const credentials = mergeCredentials(input.config.credentials, state);- function anchor(posted: DiscordPostedMessage): void {- if (!posted.id || state.hasMessageAnchor) return;- state.conversationId = posted.id;- state.hasMessageAnchor = true;+ /**+ * The first posted message anchors the conversation. A press finds its+ * session by the id of the message it is on, so every later message with+ * components, such as a second pending question, is aliased too.+ */+ function anchor(posted: DiscordPostedMessage, body: DiscordMessageBody): void {+ if (!posted.id) return;+ if (state.hasMessageAnchor && body.components === undefined) return;+ if (!state.hasMessageAnchor) {+ state.conversationId = posted.id;+ state.hasMessageAnchor = true;+ }if (state.channelId) {input.session?.continuation?.alias(discordContinuationToken(state.channelId, posted.id));}@@ -350,14 +358,15 @@ function buildDiscordHandle(input: {): Promise<DiscordPostedMessage> {const channelId = state.channelId ?? "";if (!channelId) throw new Error("discordChannel: missing channel id for outbound message.");+ const body = normalizePostInput(message);const posted = await sendDiscordChannelMessage({apiBaseUrl: api?.apiBaseUrl,- body: normalizePostInput(message),+ body,credentials,fetch: api?.fetch,channelId,});- anchor(posted);+ anchor(posted, body);return posted;}@@ -366,15 +375,16 @@ function buildDiscordHandle(input: {if (!interactionToken) {throw new Error("discordChannel: missing interaction token for original response edit.");}+ const body = normalizePostInput(message);const posted = await editDiscordOriginalResponse({apiBaseUrl: api?.apiBaseUrl,- body: normalizePostInput(message),+ body,credentials,fetch: api?.fetch,interactionToken,});state.initialResponseSent = true;- anchor(posted);+ anchor(posted, body);return posted;}@@ -383,14 +393,15 @@ function buildDiscordHandle(input: {if (!interactionToken) {throw new Error("discordChannel: missing interaction token for followup message.");}+ const body = normalizePostInput(message);const posted = await createDiscordFollowupMessage({apiBaseUrl: api?.apiBaseUrl,- body: normalizePostInput(message),+ body,credentials,fetch: api?.fetch,interactionToken,});- anchor(posted);+ anchor(posted, body);return posted;}diff --git a/packages/eve/src/public/channels/telegram/defaults.ts b/packages/eve/src/public/channels/telegram/defaults.tsindex d7af7b8d2..3c2d24034 100644--- a/packages/eve/src/public/channels/telegram/defaults.ts+++ b/packages/eve/src/public/channels/telegram/defaults.ts@@ -20,8 +20,13 @@ import type {const log = createLogger("telegram.defaults");-/** Default auth projection for Telegram webhook actors. */-export function defaultTelegramAuth(message: TelegramMessage): SessionAuthContext | null {+/**+ * Default auth projection for Telegram webhook actors. A button press passes+ * the message it was on with the presser as `from`.+ */+export function defaultTelegramAuth(+ message: Pick<TelegramMessage, "chat" | "from" | "messageId" | "messageThreadId">,+): SessionAuthContext | null {const user = message.from;if (!user) return null;diff --git a/packages/eve/src/public/channels/telegram/hitl.test.ts b/packages/eve/src/public/channels/telegram/hitl.test.tsindex 790e35817..c40ca8652 100644--- a/packages/eve/src/public/channels/telegram/hitl.test.ts+++ b/packages/eve/src/public/channels/telegram/hitl.test.ts@@ -63,7 +63,7 @@ describe("renderTelegramInputRequest", () => {});describe("resolveTelegramInputResponses", () => {- it("maps compact callback ids back to real request ids", () => {+ it("maps a compact callback id to its request on every press", () => {const state: TelegramHitlState = {hitlCallbacks: {[`${TELEGRAM_HITL_CALLBACK_PREFIX}0`]: {@@ -78,7 +78,11 @@ describe("resolveTelegramInputResponses", () => {telegramCallbackInputResponse(`${TELEGRAM_HITL_CALLBACK_PREFIX}0`),]),).toEqual([{ optionId: "approve", requestId: "call_1" }]);- expect(state.hitlCallbacks).toEqual({});+ expect(+ resolveTelegramInputResponses(state, [+ telegramCallbackInputResponse(`${TELEGRAM_HITL_CALLBACK_PREFIX}0`),+ ]),+ ).toEqual([{ optionId: "approve", requestId: "call_1" }]);});it("maps replies to ForceReply prompts back to freeform answers", () => {diff --git a/packages/eve/src/public/channels/telegram/hitl.ts b/packages/eve/src/public/channels/telegram/hitl.tsindex 1c769d13e..28a6ca132 100644--- a/packages/eve/src/public/channels/telegram/hitl.ts+++ b/packages/eve/src/public/channels/telegram/hitl.ts@@ -157,11 +157,10 @@ export function resolveTelegramInputResponses(for (const response of responses) {if (response.requestId.startsWith(TELEGRAM_CALLBACK_RESPONSE_PREFIX)) {const callbackData = response.requestId.slice(TELEGRAM_CALLBACK_RESPONSE_PREFIX.length);+ // The mapping outlives the press: a later press of the same button still+ // reaches the session, which reads an answered request's option as new input.const mapped = state.hitlCallbacks?.[callbackData];- if (mapped !== undefined) {- resolved.push(mapped);- delete state.hitlCallbacks?.[callbackData];- }+ if (mapped !== undefined) resolved.push(mapped);continue;}diff --git a/packages/eve/src/public/channels/telegram/telegramChannel.ts b/packages/eve/src/public/channels/telegram/telegramChannel.tsindex e5d8787c6..e086f4f36 100644--- a/packages/eve/src/public/channels/telegram/telegramChannel.ts+++ b/packages/eve/src/public/channels/telegram/telegramChannel.ts@@ -33,6 +33,7 @@ import {import {defaultEvents,defaultOnMessage,+ defaultTelegramAuth,isTelegramBotMentioned,} from "#public/channels/telegram/defaults.js";import {@@ -631,8 +632,10 @@ async function dispatchCallbackQuery(input: {if (!input.query.message || !state.chatId) return;try {const source = input.from(telegramContinuationTokenFromState(state));+ // The presser acts as themselves, so a press that starts a turn lets+ // their next message steer it.await source.respond([telegramCallbackInputResponse(input.query.data)], {- auth: null,+ auth: defaultTelegramAuth({ ...input.query.message, from: input.query.from }),});} catch (error) {log.error("callback query delivery failed", { error });