Skip to content

Commit 26fd60a

Browse files
author
Peter Steinberger
committed
fix(telegram): preserve question choices across button rows
1 parent fe81123 commit 26fd60a

10 files changed

Lines changed: 659 additions & 41 deletions

‎extensions/codex/src/app-server/user-input-bridge.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ describe("Codex app-server user input bridge", () => {
9999
});
100100
const payload = vi.mocked(params.onBlockReply!).mock.calls[0]![0];
101101
expect(payload.channelData).toEqual({
102-
askUser: { questionId: (request.params as { id: string }).id },
102+
askUser: {
103+
questionId: (request.params as { id: string }).id,
104+
optionValues: ["Fast", "Deep"],
105+
},
103106
});
104107
expect(payload.presentationTextMode).toBe("fallback");
105108
expect(payload.text).toContain("Reply with the number or option text.");

‎extensions/copilot/src/user-input-bridge.test.ts‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,10 @@ describe("Copilot user input bridge", () => {
8080
});
8181
const payload = vi.mocked(params.onBlockReply!).mock.calls[0]![0];
8282
expect(payload.channelData).toEqual({
83-
askUser: { questionId: (request.params as { id: string }).id },
83+
askUser: {
84+
questionId: (request.params as { id: string }).id,
85+
optionValues: ["Fast", "Deep"],
86+
},
8487
});
8588
expect(payload.presentationTextMode).toBe("fallback");
8689
expect(payload.text).toContain("Reply with the number or option text.");

‎extensions/telegram/src/bot-message-dispatch-reply.ts‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,11 @@ import type { TelegramProgressController } from "./bot-message-dispatch-progress
2121
import { deduplicateBlockSentMedia } from "./bot-message-dispatch.media-dedup.js";
2222
import type { TelegramDispatchTurnState } from "./bot-message-dispatch.types.js";
2323
import type { TelegramStreamMode } from "./bot/types.js";
24-
import { resolveTelegramInlineButtons, type TelegramInlineButtons } from "./button-types.js";
24+
import {
25+
resolveTelegramInlineButtons,
26+
resolveTelegramQuestionOptionIndices,
27+
type TelegramInlineButtons,
28+
} from "./button-types.js";
2529
import {
2630
buildTelegramErrorScopeKey,
2731
isSilentErrorPolicy,
@@ -70,11 +74,14 @@ function resolvePayloadTelegramInlineButtons(
7074
const telegramData = payload.channelData?.telegram as
7175
| { buttons?: TelegramInlineButtons }
7276
| undefined;
73-
return resolveTelegramInlineButtons({
74-
buttons: telegramData?.buttons,
75-
presentation: normalizeMessagePresentation(payload.presentation),
76-
interactive: payload.interactive,
77-
});
77+
return resolveTelegramInlineButtons(
78+
{
79+
buttons: telegramData?.buttons,
80+
presentation: normalizeMessagePresentation(payload.presentation),
81+
interactive: payload.interactive,
82+
},
83+
{ questionOptionIndices: resolveTelegramQuestionOptionIndices(payload) },
84+
);
7885
}
7986

8087
function hasExecApprovalPayload(payload: ReplyPayload): boolean {

‎extensions/telegram/src/button-types.test.ts‎

Lines changed: 195 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
import { buildApprovalResolutionRef } from "openclaw/plugin-sdk/approval-reference-runtime";
33
import { describe, expect, it } from "vitest";
44
import { parseTelegramApprovalCallbackData } from "./approval-callback-data.js";
5-
import { buildTelegramPresentationButtons, resolveTelegramInlineButtons } from "./button-types.js";
5+
import {
6+
buildTelegramPresentationButtons,
7+
resolveTelegramInlineButtons,
8+
type TelegramButtonBuildOptions,
9+
} from "./button-types.js";
610
import { describeTelegramInteractiveButtonBehavior } from "./button-types.test-helpers.js";
711
import {
812
buildTelegramOpaqueCallbackData,
@@ -11,6 +15,24 @@ import {
1115

1216
describeTelegramInteractiveButtonBehavior();
1317

18+
function questionButtonOptions(
19+
questions: ReadonlyArray<{ questionId: string; optionValues: readonly string[] }>,
20+
): TelegramButtonBuildOptions {
21+
return {
22+
questionOptionIndices: new Map(
23+
questions.map(
24+
({ questionId, optionValues }) =>
25+
[
26+
questionId,
27+
new Map(
28+
optionValues.map((value, index) => [value.trim().toLowerCase(), index] as const),
29+
),
30+
] as const,
31+
),
32+
),
33+
};
34+
}
35+
1436
describe("buildTelegramInteractiveButtons callback limits", () => {
1537
it("drops buttons whose callback payload exceeds Telegram limits", () => {
1638
expect(
@@ -56,23 +78,186 @@ describe("buildTelegramPresentationButtons", () => {
5678

5779
it("encodes question buttons by record id and option index", () => {
5880
const questionId = "ask_0123456789abcdef0123456789abcdef";
81+
expect(
82+
buildTelegramPresentationButtons(
83+
{
84+
blocks: [
85+
{
86+
type: "buttons",
87+
buttons: ["Staging", "Production"].map((label) => ({
88+
label,
89+
action: { type: "question" as const, questionId, optionValue: label },
90+
})),
91+
},
92+
],
93+
},
94+
questionButtonOptions([{ questionId, optionValues: ["Staging", "Production"] }]),
95+
),
96+
).toEqual([
97+
[
98+
{ text: "Staging", callback_data: `tgq1:${questionId}:0`, style: undefined },
99+
{ text: "Production", callback_data: `tgq1:${questionId}:1`, style: undefined },
100+
],
101+
]);
102+
});
103+
104+
it("drops question buttons when authoritative Gateway option order is unavailable", () => {
105+
const questionId = "ask_0123456789abcdef0123456789abcdef";
106+
59107
expect(
60108
buildTelegramPresentationButtons({
61109
blocks: [
62110
{
63111
type: "buttons",
64-
buttons: ["Staging", "Production"].map((label) => ({
65-
label,
66-
action: { type: "question" as const, questionId, optionValue: label },
67-
})),
112+
buttons: [
113+
{
114+
label: "Production",
115+
action: {
116+
type: "question" as const,
117+
questionId,
118+
optionValue: "Production",
119+
},
120+
},
121+
],
68122
},
69123
],
70124
}),
71-
).toEqual([
72-
[
73-
{ text: "Staging", callback_data: `tgq1:${questionId}:0`, style: undefined },
74-
{ text: "Production", callback_data: `tgq1:${questionId}:1`, style: undefined },
75-
],
125+
).toBeUndefined();
126+
});
127+
128+
it("keeps question option indices independent and stable across presentation blocks", () => {
129+
const firstQuestionId = "ask_0123456789abcdef0123456789abcdef";
130+
const secondQuestionId = "ask_fedcba9876543210fedcba9876543210";
131+
const questionButton = (questionId: string, optionValue: string) => ({
132+
label: optionValue,
133+
action: { type: "question" as const, questionId, optionValue },
134+
});
135+
136+
const rows = buildTelegramPresentationButtons(
137+
{
138+
blocks: [
139+
{
140+
type: "buttons",
141+
buttons: [
142+
questionButton(firstQuestionId, "東京"),
143+
questionButton(firstQuestionId, "Déployer"),
144+
],
145+
},
146+
{
147+
type: "buttons",
148+
buttons: [
149+
questionButton(secondQuestionId, "東京"),
150+
questionButton(secondQuestionId, "Production"),
151+
],
152+
},
153+
{
154+
type: "buttons",
155+
buttons: [
156+
questionButton(firstQuestionId, "東京"),
157+
questionButton(firstQuestionId, "Production 🚀"),
158+
],
159+
},
160+
],
161+
},
162+
questionButtonOptions([
163+
{
164+
questionId: firstQuestionId,
165+
optionValues: ["東京", "Déployer", "Production 🚀"],
166+
},
167+
{ questionId: secondQuestionId, optionValues: ["東京", "Production"] },
168+
]),
169+
);
170+
171+
expect(rows?.map((row) => row.map((button) => button.callback_data))).toEqual([
172+
[`tgq1:${firstQuestionId}:0`, `tgq1:${firstQuestionId}:1`],
173+
[`tgq1:${secondQuestionId}:0`, `tgq1:${secondQuestionId}:1`],
174+
[`tgq1:${firstQuestionId}:0`, `tgq1:${firstQuestionId}:2`],
175+
]);
176+
});
177+
178+
it("maps repeated rendered question values to their canonical Gateway option indices", () => {
179+
const questionId = "ask_0123456789abcdef0123456789abcdef";
180+
const button = (label: string) => ({
181+
label,
182+
action: { type: "question" as const, questionId, optionValue: label },
183+
});
184+
185+
const rows = buildTelegramPresentationButtons(
186+
{
187+
blocks: [
188+
{ type: "buttons", buttons: [button("A"), button("A")] },
189+
{ type: "buttons", buttons: [button("B"), button("C")] },
190+
],
191+
},
192+
questionButtonOptions([{ questionId, optionValues: ["A", "B", "C"] }]),
193+
);
194+
195+
expect(rows?.flatMap((row) => row.map((entry) => entry.callback_data))).toEqual([
196+
`tgq1:${questionId}:0`,
197+
`tgq1:${questionId}:0`,
198+
`tgq1:${questionId}:1`,
199+
`tgq1:${questionId}:2`,
200+
]);
201+
});
202+
203+
it("normalizes repeated question values with the Gateway trim and lowercase contract", () => {
204+
const questionId = "ask_0123456789abcdef0123456789abcdef";
205+
const questionButton = (optionValue: string) => ({
206+
label: optionValue,
207+
action: { type: "question" as const, questionId, optionValue },
208+
});
209+
210+
const rows = buildTelegramPresentationButtons(
211+
{
212+
blocks: [
213+
{ type: "buttons", buttons: [questionButton(" Deploy "), questionButton("deploy")] },
214+
{ type: "buttons", buttons: [questionButton("Production")] },
215+
],
216+
},
217+
questionButtonOptions([{ questionId, optionValues: [" Deploy ", "Production"] }]),
218+
);
219+
220+
expect(rows?.flatMap((row) => row.map((entry) => entry.callback_data))).toEqual([
221+
`tgq1:${questionId}:0`,
222+
`tgq1:${questionId}:0`,
223+
`tgq1:${questionId}:1`,
224+
]);
225+
});
226+
227+
it("does not consume a question option position when callback encoding fails", () => {
228+
const validQuestionId = "ask_0123456789abcdef0123456789abcdef";
229+
const invalidQuestionId = "not-a-gateway-question";
230+
const rows = buildTelegramPresentationButtons(
231+
{
232+
blocks: [
233+
{
234+
type: "buttons",
235+
buttons: [
236+
{
237+
label: "Invalid",
238+
action: {
239+
type: "question" as const,
240+
questionId: invalidQuestionId,
241+
optionValue: "Invalid",
242+
},
243+
},
244+
{
245+
label: "Valid",
246+
action: {
247+
type: "question" as const,
248+
questionId: validQuestionId,
249+
optionValue: "Valid",
250+
},
251+
},
252+
],
253+
},
254+
],
255+
},
256+
questionButtonOptions([{ questionId: validQuestionId, optionValues: ["Valid", "Other"] }]),
257+
);
258+
259+
expect(rows).toEqual([
260+
[{ text: "Valid", callback_data: `tgq1:${validQuestionId}:0`, style: undefined }],
76261
]);
77262
});
78263

0 commit comments

Comments
 (0)