The Open Framework for Building Agents
test(eve): reproduce Telegram ask_question button presses not answering (#4108)
e0-gh-vercel-connect[bot]b3fc8421d2dc
diff --git a/.changeset/telegram-ask-question-buttons.md b/.changeset/telegram-ask-question-buttons.mdnew file mode 100644index 000000000..944813892--- /dev/null+++ b/.changeset/telegram-ask-question-buttons.md@@ -0,0 +1,5 @@+---+"eve": patch+---++Pressing a Telegram inline button for an `ask_question` question now answers it. Since `[email protected]` the press was dropped and the agent kept waiting for an answer.diff --git a/packages/eve/src/execution/proxied-deliver-step.ts b/packages/eve/src/execution/proxied-deliver-step.tsindex 05f462d1b..d3260cc2e 100644--- a/packages/eve/src/execution/proxied-deliver-step.ts+++ b/packages/eve/src/execution/proxied-deliver-step.ts@@ -1,6 +1,9 @@import type { SessionInboxAddress } from "#execution/session-inbox/address.js";import { hasDelegatedSessionContext } from "#execution/delegated-session-context.js";+import { buildAdapterContext } from "#channel/adapter-context.js";import type { DeliverHookPayload, DeliverPayload } from "#channel/types.js";+import { AuthKey } from "#context/keys.js";+import { setChannelContext } from "#execution/channel-context.js";import { coalesceDeliverPayloads } from "#execution/deliver-payloads.js";import {type DurableSessionState,@@ -12,8 +15,8 @@ import {withSessionStateDelta,type WithSessionStateDelta,} from "#execution/session/state-delta.js";-import { deserializeContext } from "#context/serialize.js";-import { BundleKey } from "#runtime/sessions/runtime-context-keys.js";+import { deserializeContext, serializeContext } from "#context/serialize.js";+import { BundleKey, ChannelKey } from "#runtime/sessions/runtime-context-keys.js";import {resolveRemoteAgentStreamHeaders,respondToRemoteAgentSession,@@ -31,7 +34,8 @@ import {type InputResolution,type UnstampedMessageStreamEvent,} from "#protocol/message.js";-import { retireProxyInputRequests } from "#harness/proxy-input-requests.js";+import { getProxyInputRequests, retireProxyInputRequests } from "#harness/proxy-input-requests.js";+import type { InputResponse } from "#shared/input.js";export type RoutedDeliverResult =| {@@ -75,14 +79,14 @@ export async function routeProxiedDeliverStep(async function routeProxiedDeliver(input: SessionStepState & { readonly delivery: DeliverHookPayload },): Promise<RoutedDeliverResult> {+ const { delivery: sourceDelivery, serializedContext } = await deliverChannelInputResponses(input);let durableSession = readDurableSession(input.sessionState);- const sourceDelivery = input.delivery;const parentPayloads = new Map<number, DeliverPayload>();const children = new Map<string, ChildBucket>();let parentAction: { readonly kind: "cancel-turn" } | undefined;// Only a person's own message may answer or skip a pending question.const resolveMessage =- !hasDelegatedSessionContext(input.serializedContext) && sourceDelivery.caller === undefined;+ !hasDelegatedSessionContext(serializedContext) && sourceDelivery.caller === undefined;// Every payload routes against the same state, so a `ctx.ask()` question// resolved by an earlier payload is hidden from later ones; its run takes// one answer, and later messages must reach the parent instead.@@ -157,7 +161,7 @@ async function routeProxiedDeliver(};const remote = child.remote;if (remote !== undefined) {- const ctx = await deserializeContext(input.serializedContext);+ const ctx = await deserializeContext(serializedContext);const headers = await resolveRemoteAgentStreamHeaders({bundle: ctx.require(BundleKey),name: remote.name,@@ -190,7 +194,7 @@ async function routeProxiedDeliver(const context = await relaySessionEvents({- serializedContext: input.serializedContext,+ serializedContext,sessionState: retired? replaceDurableSessionSnapshot({ session: durableSession, state: input.sessionState }): input.sessionState,@@ -215,3 +219,64 @@ async function routeProxiedDeliver(};return { ...context, kind: "continue", remainder };}++/**+ * Maps each input response this session cannot route as sent through the+ * channel's `deliver` hook, and routes what it maps to a proxied request.+ * Telegram buttons, for example, carry compact callback ids that only its hook+ * resolves against channel state. Every other response stays as sent for the+ * turn's own `deliver` call.+ */+async function deliverChannelInputResponses(+ input: SessionStepState & { readonly delivery: DeliverHookPayload },+): Promise<{+ readonly delivery: DeliverHookPayload;+ readonly serializedContext: Record<string, unknown>;+}> {+ const requests = getProxyInputRequests(readDurableSession(input.sessionState).state);+ const routable = (response: InputResponse) => requests.has(response.requestId);+ const unrouted = input.delivery.payloads.some(+ (payload) => payload.inputResponses?.some((response) => !routable(response)) === true,+ );+ if (!unrouted) return input;+ const ctx = await deserializeContext(input.serializedContext);+ const adapter = ctx.require(ChannelKey);+ if (adapter.deliver === undefined) return input;++ // The hook sees this delivery's caller, as it does in the turn.+ if (input.delivery.auth !== undefined) ctx.set(AuthKey, input.delivery.auth ?? null);+ const adapterCtx = buildAdapterContext(adapter, ctx);+ let mapped = false;+ const payloads: DeliverPayload[] = [];+ for (const payload of input.delivery.payloads) {+ if (payload.inputResponses === undefined) {+ payloads.push(payload);+ continue;+ }+ const responses: InputResponse[] = [];+ for (const response of payload.inputResponses) {+ if (routable(response)) {+ responses.push(response);+ continue;+ }+ const result = await adapter.deliver(+ { ...payload, inputResponses: [response], message: undefined },+ adapterCtx,+ );+ const routed = result?.inputResponses?.filter(routable) ?? [];+ mapped ||= routed.length > 0;+ responses.push(...(routed.length > 0 ? routed : [response]));+ }+ payloads.push({ ...payload, inputResponses: responses });+ }+ if (!mapped) return input;++ // Only the channel state the mapping consumed carries over; the turn applies+ // the rest of this delivery, such as its caller, itself.+ const session = await deserializeContext(input.serializedContext);+ setChannelContext(session, { ...adapter, state: { ...adapterCtx.state } });+ return {+ delivery: { ...input.delivery, payloads },+ serializedContext: serializeContext(session),+ };+}diff --git a/packages/eve/src/public/channels/telegram/telegramChannel.integration.test.ts b/packages/eve/src/public/channels/telegram/telegramChannel.integration.test.tsnew file mode 100644index 000000000..b13e60e64--- /dev/null+++ b/packages/eve/src/public/channels/telegram/telegramChannel.integration.test.ts@@ -0,0 +1,174 @@+import { describe, expect, it } from "vitest";++import { createChannelOperations } from "#channel/channel-operations.js";+import { isCompiledChannel } from "#channel/compiled-channel.js";+import { isHttpRouteDefinition } from "#channel/routes.js";+import { workflowEntry } from "#execution/session/entry.js";+import { createWorkflowRuntime } from "#execution/workflow-runtime.js";+import { createTestRuntime } from "#internal/testing/app-harness.js";+import { captureTurnEvents, filterEventsByType } from "#internal/testing/events.js";+import { buildWorkflowToolSerializedContext } from "#internal/testing/workflow-tool-run-harness.js";+import { start } from "#internal/workflow/runtime.js";+import { telegramContinuationToken } from "#public/channels/telegram/api.js";+import { telegramChannel } from "#public/channels/telegram/index.js";+import { initialTelegramState } from "#public/channels/telegram/state.js";+import { createBundledRuntimeCompiledArtifactsSource } from "#runtime/compiled-artifacts-source.js";+import { getCompiledRuntimeAgentBundle } from "#runtime/sessions/compiled-agent-cache.js";+import { askQuestion } from "#tools/provided/ask-question.js";++const SECRET = "telegram-secret";+const CHAT_ID = "42";++interface TelegramApiCall {+ readonly body: Record<string, unknown>;+ readonly method: string;+}++async function withTimeout<T>(promise: Promise<T>, label: string): Promise<T> {+ let timer: ReturnType<typeof setTimeout> | undefined;+ const timeout = new Promise<never>((_, reject) => {+ timer = setTimeout(() => reject(new Error(`Timed out waiting for ${label}.`)), 15_000);+ });+ try {+ return await Promise.race([promise, timeout]);+ } finally {+ clearTimeout(timer);+ }+}++describe("telegram channel", () => {+ it("answers an ask_question question when its inline button is pressed", async () => {+ const calls: TelegramApiCall[] = [];+ const channel = telegramChannel({+ api: {+ fetch: async (input, init) => {+ const method = String(input).split("/").at(-1)!;+ calls.push({ body: JSON.parse(String(init?.body ?? "{}")), method });+ return Response.json({+ ok: true,+ result: { chat: { id: 42, type: "private" }, date: 0, message_id: calls.length },+ });+ },+ },+ credentials: { botToken: "bot-token", webhookSecretToken: SECRET },+ });+ if (!isCompiledChannel(channel)) throw new Error("Expected a compiled Telegram channel.");+ const route = channel.routes.find((candidate) => candidate.method === "POST");+ if (route === undefined || !isHttpRouteDefinition(route)) {+ throw new Error("Expected the Telegram webhook route.");+ }+ const runtime = await createTestRuntime({+ agent: { name: "telegram-ask-question" },+ modules: [+ {+ logicalPath: "tools/ask_question.ts",+ loadNamespace: async () => ({ default: askQuestion() }),+ },+ { logicalPath: "channels/telegram.ts", loadNamespace: async () => ({ default: channel }) },+ ],+ });++ await runtime.run(async () => {+ const bundle = await getCompiledRuntimeAgentBundle({+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),+ });+ const adapter = bundle.graph.root.channels.find(+ (entry) => entry.name === "telegram",+ )?.adapter;+ if (adapter === undefined) throw new Error("Expected the Telegram adapter.");+ // Channel addresses namespace their token with the channel name.+ const continuationToken = `telegram:${telegramContinuationToken({ chatId: CHAT_ID })}`;+ const run = await start(workflowEntry, [+ {+ kind: "initial",+ ownerDeploymentId: "dpl_inline",+ input: {+ message:+ 'Use ask_question and set question to: "When?" with label "Saturday" and label "Sunday".',+ },+ serializedContext: {+ ...buildWorkflowToolSerializedContext({ continuationToken, requestInput: true }),+ "eve.channel": {+ kind: adapter.kind,+ state: {+ ...initialTelegramState(undefined),+ chatId: CHAT_ID,+ chatType: "private",+ triggeringUserId: CHAT_ID,+ },+ },+ },+ },+ ]);+ const stream = captureTurnEvents(run);++ try {+ const asked = await withTimeout(stream.nextTurn(), "the question");+ expect(filterEventsByType(asked, "input.requested")).toHaveLength(1);+ expect(asked.at(-1)?.type).toBe("turn.waiting");++ const question = calls.find(+ (call) => call.method === "sendMessage" && call.body.text === "When?",+ );+ const keyboard = (+ question?.body.reply_markup as+ | { inline_keyboard: { callback_data: string; text: string }[][] }+ | undefined+ )?.inline_keyboard.flat();+ const saturday = keyboard?.find((button) => button.text === "Saturday");+ expect(saturday?.callback_data).toMatch(/^eve:/);++ const operations = createChannelOperations<unknown>({+ adapter,+ channelName: "telegram",+ runtime: createWorkflowRuntime({+ compiledArtifactsSource: createBundledRuntimeCompiledArtifactsSource(),+ }),+ });+ const pending: Promise<unknown>[] = [];+ const response = await route.handler(+ new Request("https://agent.example.com/eve/v1/telegram", {+ body: JSON.stringify({+ callback_query: {+ data: saturday!.callback_data,+ from: { id: 42, is_bot: false },+ id: "cb-saturday",+ message: { chat: { id: 42, type: "private" }, date: 0, message_id: 1 },+ },+ update_id: 2,+ }),+ headers: {+ "content-type": "application/json",+ "x-telegram-bot-api-secret-token": SECRET,+ },+ method: "POST",+ }),+ {+ ...operations,+ attachSession: (() => undefined) as never,+ params: {},+ requestIp: null,+ to: (() => undefined) as never,+ waitUntil: (task) => void pending.push(task),+ },+ );+ expect(response.status).toBe(200);+ await Promise.all(pending);++ const answered = await withTimeout(stream.nextTurn(), "the pressed answer");+ const result = filterEventsByType(answered, "action.result").find(+ (event) =>+ event.data.result.kind === "tool-result" &&+ event.data.result.toolName === "ask_question",+ );+ expect(result?.data.result.output).toEqual({+ answer: "Saturday",+ status: "answered",+ });+ } finally {+ stream.dispose();+ await run.cancel();+ }+ });+ }, 60_000);+});