Skip to content

Commit 7ac8e2e

Browse files
authored
fix(telegram): preserve UTF-16 model labels (#104037)
1 parent 070b792 commit 7ac8e2e

2 files changed

Lines changed: 35 additions & 7 deletions

File tree

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,33 @@ describe("buildModelsKeyboard", () => {
397397
}
398398
});
399399

400+
it("does not split surrogate pairs when truncating model labels", () => {
401+
const longLabel = `a😀${"b".repeat(36)}`;
402+
const cases = [
403+
{
404+
name: "model ID fallback",
405+
model: longLabel,
406+
},
407+
{
408+
name: "configured display name",
409+
model: "short-model-id",
410+
modelNames: new Map([["test/short-model-id", longLabel]]),
411+
},
412+
] as const;
413+
414+
for (const testCase of cases) {
415+
const result = buildModelsKeyboard({
416+
provider: "test",
417+
models: [testCase.model],
418+
currentPage: 1,
419+
totalPages: 1,
420+
modelNames: "modelNames" in testCase ? testCase.modelNames : undefined,
421+
});
422+
423+
expect(result[0]?.[0]?.text, testCase.name).toBe(`…${"b".repeat(36)}`);
424+
}
425+
});
426+
400427
it("uses compact selection callback when provider/model callback exceeds 64 bytes", () => {
401428
const model = "us.anthropic.claude-3-5-sonnet-20240620-v1:0";
402429
const result = buildModelsKeyboard({

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
* - mdl_back - back to providers list
1010
*/
1111
import { parseStrictPositiveInteger } from "openclaw/plugin-sdk/number-runtime";
12+
import { sliceUtf16Safe } from "openclaw/plugin-sdk/text-utility-runtime";
1213
import { fitsTelegramCallbackData } from "./approval-callback-data.js";
1314

1415
export type ButtonRow = Array<{ text: string; callback_data: string }>;
@@ -41,6 +42,7 @@ export type ModelsKeyboardParams = {
4142
};
4243

4344
const MODELS_PAGE_SIZE = 8;
45+
const MODEL_BUTTON_LABEL_MAX_LENGTH = 38;
4446
const CALLBACK_PREFIX = {
4547
providers: "mdl_prov",
4648
back: "mdl_back",
@@ -220,7 +222,7 @@ export function buildModelsKeyboard(params: ModelsKeyboardParams): ButtonRow[] {
220222
const isCurrentModel = isCurrentModelSelection({ currentModel, provider, model });
221223
const fallbackLabel = model.includes("/") ? `${provider}/${model}` : model;
222224
const displayLabel = modelNames?.get(`${provider}/${model}`) ?? fallbackLabel;
223-
const displayText = truncateModelId(displayLabel, 38);
225+
const displayText = truncateModelLabel(displayLabel, MODEL_BUTTON_LABEL_MAX_LENGTH);
224226
const text = isCurrentModel ? `${displayText} ✓` : displayText;
225227

226228
rows.push([
@@ -271,14 +273,13 @@ export function buildBrowseProvidersButton(): ButtonRow[] {
271273
}
272274

273275
/**
274-
* Truncate model ID for display, preserving end if too long.
276+
* Truncate a model label for display, preserving its end if too long.
275277
*/
276-
function truncateModelId(modelId: string, maxLen: number): string {
277-
if (modelId.length <= maxLen) {
278-
return modelId;
278+
function truncateModelLabel(modelLabel: string, maxLen: number): string {
279+
if (modelLabel.length <= maxLen) {
280+
return modelLabel;
279281
}
280-
// Show last part with ellipsis prefix
281-
return `…${modelId.slice(-(maxLen - 1))}`;
282+
return `…${sliceUtf16Safe(modelLabel, -(maxLen - 1))}`;
282283
}
283284

284285
/**

0 commit comments

Comments
 (0)