@devries-cameron20 / t3code Mirror · read-only / commits / c2ff84a4bb
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 22:15 UTC

fix(client): trim model-reference delimiters in linear time (#34311)

Romil committed Nov 12, 2026 at 10:23 UTC c2ff84a4bbafe5b86422e27d985165cd7220baff
2 files changed +24 -11
packages/client-runtime/src/composerModelMentions.test.ts
+10 -4
@@ -1,4 +1,4 @@
1 -import { describe, expect, it } from "vite-plus/test";
1 +import { describe, expect, it, vi } from "vite-plus/test";
2
3 import {
4 collectComposerModelMentions,
@@ -85,10 +85,16 @@ describe("collectComposerModelMentions", () => {
85 ["model:codex/gpt-6.1,model:claudeAgent/sonnet", ["codex/gpt-6.1", "claudeAgent/sonnet"]],
86 ["model:codex/gpt-6.1's output", ["codex/gpt-6.1"]],
87 ["model:opencode/foo(bar)", ["opencode/foo(bar)"]],
88 + ["(model:codex/GPT-6.1-Sol[1m]).", ["codex/GPT-6.1-Sol[1m]"]],
89 + ["model:codex/gpt-6.1" + ")".repeat(128), ["codex/gpt-6.1"]],
90 ])("bounds the reference in %s", (text, expected) => {
89 - expect(
90 - collectComposerModelMentions(text).map(({ instanceId, model }) => `${instanceId}/${model}`),
91 - ).toEqual(expected);
91 + const { mentions, rescans } = (() => {
92 + using split = vi.spyOn(String.prototype, "split");
93 + const mentions = collectComposerModelMentions(text);
94 + return { mentions, rescans: split.mock.calls.length };
95 + })();
96 + expect(mentions.map(({ instanceId, model }) => `${instanceId}/${model}`)).toEqual(expected);
97 + expect(rescans).toBeLessThanOrEqual(expected.length * 4);
98 });
99
100 it.each(["remodel:codex/gpt", "model:codex", "model:/gpt", "model:codex/.", "model:1x/gpt"])(
packages/client-runtime/src/composerModelMentions.ts
+14 -7
@@ -68,15 +68,22 @@ function countOf(text: string, char: string): number {
68
69 // Slugs like `foo(bar)` or `GPT-6.1-Sol[1m]` keep balanced brackets; prose wrapping them does not.
70 function trimModelSlug(slug: string): string {
71 - let trimmed = slug;
72 - while (trimmed.length > 0) {
73 - const last = trimmed.at(-1)!;
71 + let end = slug.length;
72 + const unmatched = new Map<string, number>();
73 + while (end > 0) {
74 + const last = slug[end - 1]!;
75 + if (SENTENCE_PUNCTUATION.includes(last)) {
76 + end -= 1;
77 + continue;
78 + }
79 const opening = CLOSING_BRACKETS[last];
75 - const unbalanced = opening !== undefined && countOf(trimmed, opening) < countOf(trimmed, last);
76 - if (!SENTENCE_PUNCTUATION.includes(last) && !unbalanced) break;
77 - trimmed = trimmed.slice(0, -1);
80 + if (opening === undefined) break;
81 + const excess = unmatched.get(last) ?? countOf(slug, last) - countOf(slug, opening);
82 + if (excess <= 0) break;
83 + unmatched.set(last, excess - 1);
84 + end -= 1;
85 }
79 - return trimmed;
86 + return slug.slice(0, end);
87 }
88
89 /** Finds the `model:<instance>/<model>` references that `formatComposerModelMention` writes. */