Skip to content

Commit 036fffc

Browse files
committed
fix(telegram): add mdl_idx callback fallback for long model names
Telegram inline button callback_data is capped at 64 bytes. Namespaced Ollama tags (e.g. 'xentriom/gemma-4-12B-agentic-fable5-composer2.5-v2:latest') plus the mdl_sel_ or mdl_sel/ prefix overflow that cap, so buildModelsKeyboard silently dropped those rows and the user could never pick the model. Add a third-tier callback encoding mdl_idx/{provider}/{i} that stores the provider name plus the 1-indexed position in the locale-sorted model list. The handler re-derives the same sort order ([...modelSet].toSorted(localeCompare)) to map the index back to a model, so the user's selection still lands on the exact row they pressed. Fixes #98221
1 parent 73e6a62 commit 036fffc

3 files changed

Lines changed: 133 additions & 14 deletions

File tree

‎extensions/telegram/src/bot-handlers.runtime.ts‎

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2795,7 +2795,7 @@ export const registerTelegramHandlers = ({
27952795
}
27962796

27972797
// Model selection callback handler (mdl_prov, mdl_list_*, mdl_sel_*, mdl_back)
2798-
const modelCallback = parseModelCallbackData(data);
2798+
let modelCallback = parseModelCallbackData(data);
27992799
if (modelCallback) {
28002800
if (
28012801
!(await isTelegramModelCallbackAuthorized({
@@ -2937,6 +2937,34 @@ export const registerTelegramHandlers = ({
29372937
return;
29382938
}
29392939

2940+
// mdl_idx/{provider}/{i} — fallback when the model name plus the
2941+
// mdl_sel encoding exceeds Telegram's 64-byte callback_data limit.
2942+
// Re-derive the same locale-sorted list buildModelsKeyboard uses so
2943+
// the 0-based index points at the same row the user pressed.
2944+
if (modelCallback.type === "select-index") {
2945+
const modelSet = byProvider.get(modelCallback.provider);
2946+
const sorted = modelSet
2947+
? [...modelSet].toSorted((left, right) => left.localeCompare(right))
2948+
: [];
2949+
const resolvedModel = sorted[modelCallback.index];
2950+
if (!resolvedModel) {
2951+
try {
2952+
await editMessageWithButtons(
2953+
`❌ Model index ${modelCallback.index + 1} for "${modelCallback.provider}" is no longer available.`,
2954+
[],
2955+
);
2956+
} catch (err) {
2957+
throw new TelegramRetryableCallbackError(err);
2958+
}
2959+
return;
2960+
}
2961+
modelCallback = {
2962+
type: "select",
2963+
provider: modelCallback.provider,
2964+
model: resolvedModel,
2965+
};
2966+
}
2967+
29402968
if (modelCallback.type === "select") {
29412969
const selection = resolveModelSelection({
29422970
callback: modelCallback,

‎extensions/telegram/src/model-buttons.test.ts‎

Lines changed: 58 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ describe("parseModelCallbackData", () => {
3333
"mdl_sel/anthropic/claude-3-7-sonnet",
3434
{ type: "select", model: "anthropic/claude-3-7-sonnet" },
3535
],
36+
["mdl_idx/ollama/3", { type: "select-index", provider: "ollama", index: 2 }],
3637
[" mdl_prov ", { type: "providers" }],
3738
] as const;
3839
for (const [input, expected] of cases) {
@@ -50,6 +51,10 @@ describe("parseModelCallbackData", () => {
5051
"mdl_list_openai_9007199254740993",
5152
"mdl_sel_noslash",
5253
"mdl_sel/",
54+
"mdl_idx/openai",
55+
"mdl_idx/openai/abc",
56+
"mdl_idx//3",
57+
"mdl_idx/openai/0",
5358
];
5459
for (const input of invalid) {
5560
expect(parseModelCallbackData(input), input).toBeNull();
@@ -128,6 +133,30 @@ describe("buildModelSelectionCallbackData", () => {
128133
const tooLongModel = "x".repeat(80);
129134
expect(buildModelSelectionCallbackData({ provider: "openai", model: tooLongModel })).toBeNull();
130135
});
136+
137+
it("falls back to index encoding when standard and compact callbacks exceed the limit", () => {
138+
const tooLongModel = "x".repeat(80);
139+
expect(
140+
buildModelSelectionCallbackData({
141+
provider: "openai",
142+
model: tooLongModel,
143+
sortedIndex: 4,
144+
}),
145+
).toBe("mdl_idx/openai/5");
146+
});
147+
148+
it("returns null when no encoding fits even with an index", () => {
149+
// Provider name alone is so long that even the index callback overflows.
150+
const tooLongProvider = "p".repeat(80);
151+
const tooLongModel = "x".repeat(80);
152+
expect(
153+
buildModelSelectionCallbackData({
154+
provider: tooLongProvider,
155+
model: tooLongModel,
156+
sortedIndex: 0,
157+
}),
158+
).toBeNull();
159+
});
131160
});
132161

133162
describe("buildProviderKeyboard", () => {
@@ -495,23 +524,43 @@ describe("large model lists (OpenRouter-scale)", () => {
495524
}
496525
});
497526

498-
it("skips models that would exceed callback_data limit", () => {
499-
const models = [
500-
"short-model",
501-
"this-is-an-extremely-long-model-name-that-definitely-exceeds-the-sixty-four-byte-limit",
502-
"another-short",
503-
];
527+
it("uses index fallback when a model name exceeds callback_data limit", () => {
528+
const longModel =
529+
"this-is-an-extremely-long-model-name-that-definitely-exceeds-the-sixty-four-byte-limit";
530+
const models = ["short-model", longModel, "another-short"];
504531
const result = buildModelsKeyboard({
505532
provider: "openrouter",
506533
models,
507534
currentPage: 1,
508535
totalPages: 1,
509536
});
510537

511-
// Should have 2 model buttons (skipping the long one) + back
538+
// All three models render. The middle model falls back to a
539+
// mdl_idx/{provider}/{sortedIndex+1} callback because the standard
540+
// (mdl_sel_) and compact (mdl_sel/) encodings both exceed 64 bytes.
512541
const modelButtons = result.filter((row) => !row[0]?.callback_data.startsWith("mdl_back"));
513-
expect(modelButtons.length).toBe(2);
542+
expect(modelButtons.length).toBe(3);
514543
expect(modelButtons[0]?.[0]?.text).toBe("short-model");
515-
expect(modelButtons[1]?.[0]?.text).toBe("another-short");
544+
expect(modelButtons[0]?.[0]?.callback_data).toBe("mdl_sel_openrouter/short-model");
545+
expect(modelButtons[1]?.[0]?.callback_data).toBe("mdl_idx/openrouter/2");
546+
expect(modelButtons[2]?.[0]?.text).toBe("another-short");
547+
expect(modelButtons[2]?.[0]?.callback_data).toBe("mdl_sel_openrouter/another-short");
548+
});
549+
550+
it("drops a model only when every encoding (standard, compact, index) overflows", () => {
551+
const tooLongProvider = "p".repeat(80);
552+
const longModel = "x".repeat(80);
553+
const result = buildModelsKeyboard({
554+
provider: tooLongProvider,
555+
models: ["short", longModel],
556+
currentPage: 1,
557+
totalPages: 1,
558+
});
559+
560+
// Only "short" survives: index fallback still overflows because the
561+
// provider name alone is longer than 64 bytes.
562+
const modelButtons = result.filter((row) => !row[0]?.callback_data.startsWith("mdl_back"));
563+
expect(modelButtons.length).toBe(1);
564+
expect(modelButtons[0]?.[0]?.text).toBe("short");
516565
});
517566
});

‎extensions/telegram/src/model-buttons.ts‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
* - mdl_list_{prov}_{pg} - show models for provider (page N, 1-indexed)
77
* - mdl_sel_{provider/id} - select model (standard)
88
* - mdl_sel/{model} - select model (compact fallback when standard is >64 bytes)
9+
* - mdl_idx/{provider}/{i}- select model by sorted-list index (fallback when
10+
* even the compact encoding exceeds 64 bytes; the
11+
* handler re-derives the same sort order)
912
* - mdl_back - back to providers list
1013
*/
1114
import { parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime";
@@ -17,6 +20,7 @@ export type ParsedModelCallback =
1720
| { type: "providers" }
1821
| { type: "list"; provider: string; page: number }
1922
| { type: "select"; provider?: string; model: string }
23+
| { type: "select-index"; provider: string; index: number }
2024
| { type: "back" };
2125

2226
export type ProviderInfo = {
@@ -47,6 +51,7 @@ const CALLBACK_PREFIX = {
4751
list: "mdl_list_",
4852
selectStandard: "mdl_sel_",
4953
selectCompact: "mdl_sel/",
54+
selectedIndex: "mdl_idx/",
5055
} as const;
5156

5257
/**
@@ -85,6 +90,16 @@ export function parseModelCallbackData(data: string): ParsedModelCallback | null
8590
}
8691
}
8792

93+
// mdl_idx/{provider}/{index} (index fallback for names too long to fit)
94+
const indexSelMatch = trimmed.match(/^mdl_idx\/([^/]+)\/(\d+)$/);
95+
if (indexSelMatch) {
96+
const [, provider, indexStr] = indexSelMatch;
97+
const index = parseStrictPositiveInteger(indexStr);
98+
if (provider && index !== undefined) {
99+
return { type: "select-index", provider, index: index - 1 };
100+
}
101+
}
102+
88103
// mdl_sel_{provider/model}
89104
const selMatch = trimmed.match(/^mdl_sel_(.+)$/);
90105
if (selMatch) {
@@ -107,13 +122,29 @@ export function parseModelCallbackData(data: string): ParsedModelCallback | null
107122
export function buildModelSelectionCallbackData(params: {
108123
provider: string;
109124
model: string;
125+
/**
126+
* 0-based index of the model within the provider's locale-sorted model list.
127+
* When the standard and compact encodings both exceed Telegram's 64-byte
128+
* callback_data limit, an index-based fallback is emitted so the model is
129+
* still selectable. The caller re-derives the same sort order on resolution.
130+
*/
131+
sortedIndex?: number;
110132
}): string | null {
111133
const fullCallbackData = `${CALLBACK_PREFIX.selectStandard}${params.provider}/${params.model}`;
112134
if (fitsTelegramCallbackData(fullCallbackData)) {
113135
return fullCallbackData;
114136
}
115137
const compactCallbackData = `${CALLBACK_PREFIX.selectCompact}${params.model}`;
116-
return fitsTelegramCallbackData(compactCallbackData) ? compactCallbackData : null;
138+
if (fitsTelegramCallbackData(compactCallbackData)) {
139+
return compactCallbackData;
140+
}
141+
if (params.sortedIndex !== undefined && params.sortedIndex >= 0) {
142+
const indexCallbackData = `${CALLBACK_PREFIX.selectedIndex}${params.provider}/${params.sortedIndex + 1}`;
143+
if (fitsTelegramCallbackData(indexCallbackData)) {
144+
return indexCallbackData;
145+
}
146+
}
147+
return null;
117148
}
118149

119150
export function resolveModelSelection(params: {
@@ -210,9 +241,20 @@ export function buildModelsKeyboard(params: ModelsKeyboardParams): ButtonRow[] {
210241
const endIndex = Math.min(startIndex + pageSize, models.length);
211242
const pageModels = models.slice(startIndex, endIndex);
212243

213-
for (const model of pageModels) {
214-
const callbackData = buildModelSelectionCallbackData({ provider, model });
215-
// Skip models that still exceed Telegram's callback_data limit.
244+
for (let pageOffset = 0; pageOffset < pageModels.length; pageOffset++) {
245+
const model = pageModels[pageOffset];
246+
// pageModels is a slice of the sorted `models` list, so the global index
247+
// is startIndex + pageOffset. Used as a 64-byte-safe callback fallback so
248+
// long model names (e.g. namespaced Ollama tags) stay selectable.
249+
const sortedIndex = startIndex + pageOffset;
250+
const callbackData = buildModelSelectionCallbackData({
251+
provider,
252+
model,
253+
sortedIndex,
254+
});
255+
// Only drop if every encoding (standard, compact, index) exceeds the
256+
// Telegram callback_data limit. In practice the index fallback always
257+
// fits, so this branch now only guards against pathological providers.
216258
if (!callbackData) {
217259
continue;
218260
}

0 commit comments

Comments
 (0)