Skip to content

Commit 954a931

Browse files
authored
fix(tui): resolve provider-specific thinking labels (#129952)
1 parent 72912b1 commit 954a931

3 files changed

Lines changed: 132 additions & 12 deletions

File tree

‎src/tui/tui-command-handlers.test.ts‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2469,6 +2469,93 @@ describe("tui command handlers", () => {
24692469
expect(addSystem).toHaveBeenNthCalledWith(2, "usage: /think <off|max|default>");
24702470
});
24712471

2472+
it.each([
2473+
{
2474+
name: "provider-specific binary on",
2475+
local: false,
2476+
levels: [
2477+
{ id: "off", label: "off" },
2478+
{ id: "high", label: "on" },
2479+
],
2480+
input: "on",
2481+
expected: "high",
2482+
},
2483+
{
2484+
name: "case-insensitive binary on in embedded mode",
2485+
local: true,
2486+
levels: [{ id: "high", label: "on" }],
2487+
input: "ON",
2488+
expected: "high",
2489+
},
2490+
{
2491+
name: "always-on high profile",
2492+
local: false,
2493+
levels: [{ id: "high", label: "always on" }],
2494+
input: "always on",
2495+
expected: "high",
2496+
},
2497+
{
2498+
name: "always-on off profile",
2499+
local: false,
2500+
levels: [{ id: "off", label: "always on" }],
2501+
input: "always on",
2502+
expected: "off",
2503+
},
2504+
{
2505+
name: "Moonshot binary on",
2506+
local: false,
2507+
levels: [{ id: "low", label: "on" }],
2508+
input: "on",
2509+
expected: "low",
2510+
},
2511+
{
2512+
name: "canonical id ahead of another option's label",
2513+
local: false,
2514+
levels: [
2515+
{ id: "low", label: "high" },
2516+
{ id: "high", label: "turbo" },
2517+
],
2518+
input: "high",
2519+
expected: "high",
2520+
},
2521+
])(
2522+
"resolves $name to its canonical thinking level",
2523+
async ({ local, levels, input, expected }) => {
2524+
const { handleCommand, patchSession, addSystem } = createHarness({
2525+
opts: { local },
2526+
sessionInfo: { thinkingLevels: levels },
2527+
});
2528+
2529+
await handleCommand(`/think ${input}`);
2530+
2531+
expect(patchSession).toHaveBeenCalledWith({
2532+
key: "agent:main:main",
2533+
thinkingLevel: expected,
2534+
});
2535+
expect(addSystem).toHaveBeenCalledWith(`thinking set to ${input}`);
2536+
},
2537+
);
2538+
2539+
it.each([undefined, []])(
2540+
"resolves thinking labels from the provider policy when session levels are %j",
2541+
async (thinkingLevels) => {
2542+
const { handleCommand, patchSession } = createHarness({
2543+
sessionInfo: {
2544+
modelProvider: "opencode-go",
2545+
model: "minimax-m3",
2546+
thinkingLevels,
2547+
},
2548+
});
2549+
2550+
await handleCommand("/think on");
2551+
2552+
expect(patchSession).toHaveBeenCalledWith({
2553+
key: "agent:main:main",
2554+
thinkingLevel: "high",
2555+
});
2556+
},
2557+
);
2558+
24722559
it.each([
24732560
{ command: "verbose", usage: "usage: /verbose <on|off|full>" },
24742561
{ command: "reasoning", usage: "usage: /reasoning <on|off|stream>" },

‎src/tui/tui-command-handlers.ts‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import { modelKey } from "../agents/model-ref-shared.js";
88
import { shouldForwardModelCommandToServer } from "../auto-reply/commands-registry.shared.js";
99
import { normalizeGroupActivation } from "../auto-reply/group-activation.js";
1010
import {
11-
formatThinkingLevels,
1211
isSessionDefaultDirectiveValue,
12+
listThinkingLevelOptions,
1313
normalizeUsageDisplay,
1414
resolveResponseUsageMode,
1515
} from "../auto-reply/thinking.js";
@@ -632,20 +632,20 @@ export function createCommandHandlers(context: CommandHandlerContext) {
632632
},
633633
models: async () => await openModelSelector(),
634634
think: async (args) => {
635+
const { thinkingLevels, modelProvider, model, agentRuntime } = state.sessionInfo;
636+
const levels = thinkingLevels?.length
637+
? thinkingLevels
638+
: listThinkingLevelOptions(modelProvider, model, undefined, agentRuntime?.id);
635639
if (!args) {
636-
const levels =
637-
state.sessionInfo.thinkingLevels?.map((level) => level.label).join("|") ||
638-
formatThinkingLevels(
639-
state.sessionInfo.modelProvider,
640-
state.sessionInfo.model,
641-
"|",
642-
undefined,
643-
state.sessionInfo.agentRuntime?.id,
644-
);
645-
chatLog.addSystem(`usage: /think <${levels}|default>`);
640+
chatLog.addSystem(`usage: /think <${levels.map(({ label }) => label).join("|")}|default>`);
646641
return;
647642
}
648-
const thinkingLevel = isSessionDefaultDirectiveValue(args) ? null : args;
643+
const normalized = args.toLowerCase();
644+
const thinkingLevel = isSessionDefaultDirectiveValue(args)
645+
? null
646+
: (levels.find(({ id }) => id.toLowerCase() === normalized)?.id ??
647+
levels.find(({ label }) => label.toLowerCase() === normalized)?.id ??
648+
args);
649649
await applySessionSetting({ thinkingLevel }, `thinking set to ${args}`, "think failed");
650650
},
651651
verbose: async (args) => {

‎src/tui/tui-session-identity-pty.e2e.test.ts‎

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,39 @@ afterEach(async () => {
7474
await disposeActiveTuiFixtures();
7575
});
7676

77+
it("submits provider-specific thinking labels with one Enter", async () => {
78+
const fixture = await startTuiFixture({
79+
env: {
80+
OPENCLAW_TUI_PTY_THINKING_LABEL: "on",
81+
OPENCLAW_TUI_PTY_SAFE_THINKING_LABEL: "always on",
82+
},
83+
});
84+
85+
try {
86+
await fixture.run.waitForOutput("local ready", STARTUP_TIMEOUT_MS);
87+
88+
for (const [index, { label, id }] of [
89+
{ label: "on", id: "fixture-thinking" },
90+
{ label: "always on", id: "fixture-thinking-safe" },
91+
].entries()) {
92+
await fixture.run.write(`/think ${label}`, { delay: false });
93+
await fixture.run.waitForOutput(`→ ${label}`, STARTUP_TIMEOUT_MS);
94+
await fixture.run.write("\r", { delay: false });
95+
const entries = await waitForLogCount({
96+
logPath: fixture.logPath,
97+
predicate: (entry) => entry.method === "patchSession",
98+
count: index + 1,
99+
});
100+
expect(entries.findLast((entry) => entry.method === "patchSession")?.payload).toMatchObject({
101+
thinkingLevel: id,
102+
});
103+
await fixture.run.waitForOutput(`thinking set to ${label}`, STARTUP_TIMEOUT_MS);
104+
}
105+
} finally {
106+
await fixture.cleanup();
107+
}
108+
}, 65_000);
109+
77110
it("clears the previous display name when the selected session is unnamed", async () => {
78111
const fixture = await startTuiFixture();
79112
try {

0 commit comments

Comments
 (0)