mirror of
https://github.com/openclaw/openclaw.git
synced 2026-07-21 10:16:44 +00:00
fix(lmstudio): honor thinking off for binary reasoning models (#92002)
Scope disabled-thinking payload repair to LM Studio's lightweight provider stream hook. Preserve official OpenAI and Anthropic tool-calling paths. Co-authored-by: David <32288+nxmxbbd@users.noreply.github.com>
This commit is contained in:
@@ -1,2 +1,2 @@
|
||||
85c3572e6ed2bfe3df92c7d53cef465b30d2e861ad9529009faa287cdc5aec71 plugin-sdk-api-baseline.json
|
||||
0d7c7e42d04b97d40519c5a23ba96599b05868c71a997eb913b9fccbc5fb2515 plugin-sdk-api-baseline.jsonl
|
||||
2ac33288de5918da052ee7f1715294135aa99fc829666cc9490d1cd3e17f8a10 plugin-sdk-api-baseline.json
|
||||
a37ed8fc317d439f33425058c2be7114d8278f7e9ed82fb869ef783ae73bca26 plugin-sdk-api-baseline.jsonl
|
||||
|
||||
@@ -515,6 +515,7 @@ API key auth, and dynamic model resolution.
|
||||
|
||||
- `openclaw/plugin-sdk/provider-model-shared` - `ProviderReplayFamily`, `buildProviderReplayFamilyHooks(...)`, and the raw replay builders (`buildOpenAICompatibleReplayPolicy`, `buildAnthropicReplayPolicyForModel`, `buildGoogleGeminiReplayPolicy`, `buildHybridAnthropicOrOpenAIReplayPolicy`). Also exports Gemini replay helpers (`sanitizeGoogleGeminiReplayHistory`, `resolveTaggedReasoningOutputMode`) and endpoint/model helpers (`resolveProviderEndpoint`, `normalizeProviderId`, `normalizeGooglePreviewModelId`).
|
||||
- `openclaw/plugin-sdk/provider-stream` - `ProviderStreamFamily`, `buildProviderStreamFamilyHooks(...)`, `composeProviderStreamWrappers(...)`, plus the shared OpenAI/Codex wrappers (`createOpenAIAttributionHeadersWrapper`, `createOpenAIFastModeWrapper`, `createOpenAIServiceTierWrapper`, `createOpenAIResponsesContextManagementWrapper`, `createCodexNativeWebSearchWrapper`), DeepSeek V4 OpenAI-compatible wrapper (`createDeepSeekV4OpenAICompatibleThinkingWrapper`), Anthropic Messages thinking prefill cleanup (`createAnthropicThinkingPrefillPayloadWrapper`), plain-text tool-call compat (`createPlainTextToolCallCompatWrapper`), and shared proxy/provider wrappers (`createOpenRouterWrapper`, `createToolStreamWrapper`, `createMinimaxFastModeWrapper`).
|
||||
- `openclaw/plugin-sdk/provider-stream-shared` - lightweight payload and event wrappers for hot provider paths, including `createOpenAICompatibleCompletionsThinkingOffWrapper`, `createPayloadPatchStreamWrapper`, and `createPlainTextToolCallCompatWrapper`.
|
||||
- `openclaw/plugin-sdk/provider-tools` - `ProviderToolCompatFamily`, `buildProviderToolCompatFamilyHooks("deepseek" | "gemini" | "openai")`, and underlying provider schema helpers.
|
||||
|
||||
For Gemini-family providers, keep the reasoning-output mode aligned with
|
||||
|
||||
@@ -164,7 +164,7 @@ and pairing-path families.
|
||||
| `plugin-sdk/provider-tools` | `ProviderToolCompatFamily`, `buildProviderToolCompatFamilyHooks`, and DeepSeek/Gemini/OpenAI schema cleanup + diagnostics |
|
||||
| `plugin-sdk/provider-usage` | Provider usage snapshot types, shared usage fetch helpers, and provider fetchers such as `fetchClaudeUsage` |
|
||||
| `plugin-sdk/provider-stream` | `ProviderStreamFamily`, `buildProviderStreamFamilyHooks`, `composeProviderStreamWrappers`, stream wrapper types, plain-text tool-call compat, and shared Anthropic/Bedrock/DeepSeek V4/Google/Kilocode/Moonshot/OpenAI/OpenRouter/Z.A.I/MiniMax/Copilot wrapper helpers |
|
||||
| `plugin-sdk/provider-stream-shared` | Public shared provider stream wrapper helpers including `composeProviderStreamWrappers`, `createPlainTextToolCallCompatWrapper`, `createPayloadPatchStreamWrapper`, `createToolStreamWrapper`, and Anthropic/DeepSeek/OpenAI-compatible stream utilities |
|
||||
| `plugin-sdk/provider-stream-shared` | Public shared provider stream wrapper helpers including `composeProviderStreamWrappers`, `createOpenAICompatibleCompletionsThinkingOffWrapper`, `createPlainTextToolCallCompatWrapper`, `createPayloadPatchStreamWrapper`, `createToolStreamWrapper`, and Anthropic/DeepSeek/OpenAI-compatible stream utilities |
|
||||
| `plugin-sdk/provider-transport-runtime` | Native provider transport helpers such as guarded fetch, transport message transforms, and writable transport event streams |
|
||||
| `plugin-sdk/provider-onboard` | Onboarding config patch helpers |
|
||||
| `plugin-sdk/global-singleton` | Process-local singleton/map/cache helpers |
|
||||
|
||||
@@ -125,7 +125,7 @@ function buildEventStreamFn(events: unknown[]): StreamFn {
|
||||
|
||||
function createWrappedLmstudioStream(
|
||||
baseStream: StreamFn,
|
||||
params?: { baseUrl?: string },
|
||||
params?: { baseUrl?: string; thinkingLevel?: string },
|
||||
): StreamFn {
|
||||
return wrapLmstudioInferencePreload({
|
||||
provider: "lmstudio",
|
||||
@@ -141,9 +141,27 @@ function createWrappedLmstudioStream(
|
||||
},
|
||||
},
|
||||
streamFn: baseStream,
|
||||
thinkingLevel: params?.thinkingLevel,
|
||||
} as never);
|
||||
}
|
||||
|
||||
function buildPayloadStreamFn(payload: Record<string, unknown>): StreamFn {
|
||||
return vi.fn((model, _context, options) => {
|
||||
const stream = createAssistantMessageEventStream();
|
||||
queueMicrotask(() => {
|
||||
options?.onPayload?.(payload, model);
|
||||
stream.push({ type: "done", reason: "stop", message: {} as never });
|
||||
stream.end();
|
||||
});
|
||||
return stream;
|
||||
});
|
||||
}
|
||||
|
||||
const BINARY_REASONING_COMPAT = {
|
||||
supportedReasoningEfforts: ["none", "minimal", "low", "medium", "high", "xhigh"],
|
||||
reasoningEffortMap: { off: "none", none: "none", adaptive: "xhigh", max: "xhigh" },
|
||||
};
|
||||
|
||||
function runWrappedLmstudioStream(
|
||||
wrapped: StreamFn,
|
||||
model: Record<string, unknown>,
|
||||
@@ -682,4 +700,69 @@ describe("lmstudio stream wrapper", () => {
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
it("rewrites reasoning_effort to the disabled effort when thinking is off", async () => {
|
||||
const payload: Record<string, unknown> = {
|
||||
model: "qwen3-8b-instruct",
|
||||
reasoning_effort: "high",
|
||||
};
|
||||
const baseStream = buildPayloadStreamFn(payload);
|
||||
const wrapped = createWrappedLmstudioStream(baseStream, { thinkingLevel: "off" });
|
||||
const events = await collectEvents(
|
||||
runWrappedLmstudioStream(wrapped, { compat: BINARY_REASONING_COMPAT }),
|
||||
);
|
||||
|
||||
expectSingleDoneEvent(events);
|
||||
expect(payload.reasoning_effort).toBe("none");
|
||||
});
|
||||
|
||||
it("drops reasoning_effort on thinking off when the model has no disabled effort", async () => {
|
||||
const payload: Record<string, unknown> = {
|
||||
model: "qwen3-8b-instruct",
|
||||
reasoning_effort: "high",
|
||||
};
|
||||
const baseStream = buildPayloadStreamFn(payload);
|
||||
const wrapped = createWrappedLmstudioStream(baseStream, { thinkingLevel: "off" });
|
||||
const events = await collectEvents(
|
||||
runWrappedLmstudioStream(wrapped, {
|
||||
compat: {
|
||||
supportedReasoningEfforts: ["minimal", "low", "medium", "high", "xhigh"],
|
||||
reasoningEffortMap: { adaptive: "xhigh", max: "xhigh" },
|
||||
},
|
||||
}),
|
||||
);
|
||||
|
||||
expectSingleDoneEvent(events);
|
||||
expect("reasoning_effort" in payload).toBe(false);
|
||||
});
|
||||
|
||||
it("keeps reasoning_effort untouched for enabled thinking levels", async () => {
|
||||
const payload: Record<string, unknown> = {
|
||||
model: "qwen3-8b-instruct",
|
||||
reasoning_effort: "high",
|
||||
};
|
||||
const baseStream = buildPayloadStreamFn(payload);
|
||||
const wrapped = createWrappedLmstudioStream(baseStream, { thinkingLevel: "high" });
|
||||
const events = await collectEvents(
|
||||
runWrappedLmstudioStream(wrapped, { compat: BINARY_REASONING_COMPAT }),
|
||||
);
|
||||
|
||||
expectSingleDoneEvent(events);
|
||||
expect(payload.reasoning_effort).toBe("high");
|
||||
});
|
||||
|
||||
it("keeps reasoning_effort untouched without a thinking level", async () => {
|
||||
const payload: Record<string, unknown> = {
|
||||
model: "qwen3-8b-instruct",
|
||||
reasoning_effort: "high",
|
||||
};
|
||||
const baseStream = buildPayloadStreamFn(payload);
|
||||
const wrapped = createWrappedLmstudioStream(baseStream);
|
||||
const events = await collectEvents(
|
||||
runWrappedLmstudioStream(wrapped, { compat: BINARY_REASONING_COMPAT }),
|
||||
);
|
||||
|
||||
expectSingleDoneEvent(events);
|
||||
expect(payload.reasoning_effort).toBe("high");
|
||||
});
|
||||
});
|
||||
|
||||
@@ -3,7 +3,10 @@ import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
|
||||
import { streamSimple } from "openclaw/plugin-sdk/llm";
|
||||
import { createSubsystemLogger } from "openclaw/plugin-sdk/logging-core";
|
||||
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
|
||||
import { createPlainTextToolCallCompatWrapper } from "openclaw/plugin-sdk/provider-stream-shared";
|
||||
import {
|
||||
createOpenAICompatibleCompletionsThinkingOffWrapper,
|
||||
createPlainTextToolCallCompatWrapper,
|
||||
} from "openclaw/plugin-sdk/provider-stream-shared";
|
||||
import { ssrfPolicyFromHttpBaseUrlAllowedHostname } from "openclaw/plugin-sdk/ssrf-runtime";
|
||||
import { asPositiveSafeInteger } from "openclaw/plugin-sdk/string-coerce-runtime";
|
||||
import { LMSTUDIO_PROVIDER_ID } from "./defaults.js";
|
||||
@@ -175,7 +178,14 @@ async function ensureLmstudioModelLoadedBestEffort(params: {
|
||||
|
||||
export function wrapLmstudioInferencePreload(ctx: ProviderWrapStreamFnContext): StreamFn {
|
||||
const underlying = ctx.streamFn ?? streamSimple;
|
||||
const streamWithPlainTextToolCalls = createPlainTextToolCallCompatWrapper(underlying);
|
||||
// LM Studio does not ride the shared OpenAI provider hook stack, so the
|
||||
// thinking-level payload rewrite must be composed here: without it, thinking
|
||||
// "off" leaves the transport's defaulted reasoning_effort (an enabled level)
|
||||
// in requests to binary-thinking servers.
|
||||
const streamWithThinkingLevel = createOpenAICompatibleCompletionsThinkingOffWrapper(
|
||||
createPlainTextToolCallCompatWrapper(underlying),
|
||||
ctx.thinkingLevel,
|
||||
);
|
||||
return (model, context, options) => {
|
||||
if (model.provider !== LMSTUDIO_PROVIDER_ID) {
|
||||
return underlying(model, context, options);
|
||||
@@ -186,7 +196,7 @@ export function wrapLmstudioInferencePreload(ctx: ProviderWrapStreamFnContext):
|
||||
}
|
||||
const providerConfig = ctx.config?.models?.providers?.[LMSTUDIO_PROVIDER_ID];
|
||||
if (!shouldPreloadLmstudioModels(providerConfig)) {
|
||||
return streamWithPlainTextToolCalls(withLmstudioUsageCompat(model), context, options);
|
||||
return streamWithThinkingLevel(withLmstudioUsageCompat(model), context, options);
|
||||
}
|
||||
const providerBaseUrl = providerConfig?.baseUrl;
|
||||
const resolvedBaseUrl = resolveLmstudioInferenceBase(
|
||||
@@ -261,7 +271,7 @@ export function wrapLmstudioInferencePreload(ctx: ProviderWrapStreamFnContext):
|
||||
// LM Studio uses OpenAI-compatible streaming usage payloads when requested via
|
||||
// `stream_options.include_usage`. Force this compat flag at call time so usage
|
||||
// reporting remains enabled even when catalog entries omitted compat metadata.
|
||||
const stream = streamWithPlainTextToolCalls(withLmstudioUsageCompat(model), context, options);
|
||||
const stream = streamWithThinkingLevel(withLmstudioUsageCompat(model), context, options);
|
||||
const resolvedStream = stream instanceof Promise ? await stream : stream;
|
||||
return resolvedStream;
|
||||
})();
|
||||
|
||||
@@ -2,11 +2,13 @@
|
||||
* Tests provider stream shared helpers and stream hook capture.
|
||||
*/
|
||||
import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
|
||||
import type { Model } from "openclaw/plugin-sdk/llm";
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { createAssistantMessageEventStream } from "../llm/utils/event-stream.js";
|
||||
import {
|
||||
createDeepSeekV4OpenAICompatibleThinkingWrapper,
|
||||
createAnthropicThinkingPrefillPayloadWrapper,
|
||||
createOpenAICompatibleCompletionsThinkingOffWrapper,
|
||||
createPayloadPatchStreamWrapper,
|
||||
createPlainTextToolCallCompatWrapper,
|
||||
defaultToolStreamExtraParams,
|
||||
@@ -16,6 +18,27 @@ import {
|
||||
|
||||
type StreamEvent = { type: string } & Record<string, unknown>;
|
||||
|
||||
const lmstudioBinaryModel = {
|
||||
api: "openai-completions",
|
||||
provider: "lmstudio",
|
||||
id: "google/gemma-4-26b-a4b-qat",
|
||||
baseUrl: "http://127.0.0.1:1234/v1",
|
||||
reasoning: true,
|
||||
compat: {
|
||||
supportsReasoningEffort: true,
|
||||
supportedReasoningEfforts: ["none", "minimal", "low", "medium", "high", "xhigh"],
|
||||
reasoningEffortMap: { off: "none", none: "none", adaptive: "xhigh", max: "xhigh" },
|
||||
},
|
||||
} as unknown as Model<"openai-completions">;
|
||||
|
||||
const lmstudioBareModel = {
|
||||
api: "openai-completions",
|
||||
provider: "lmstudio",
|
||||
id: "qwen3-8b-instruct",
|
||||
baseUrl: "http://127.0.0.1:1234/v1",
|
||||
reasoning: true,
|
||||
} as unknown as Model<"openai-completions">;
|
||||
|
||||
function requireRecord(value: unknown, label: string): Record<string, unknown> {
|
||||
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
||||
throw new Error(`expected ${label} to be a record`);
|
||||
@@ -35,6 +58,20 @@ function createEventStream(events: unknown[]): ReturnType<StreamFn> {
|
||||
return output as ReturnType<StreamFn>;
|
||||
}
|
||||
|
||||
function createPayloadCapture(initialReasoningEffort?: string) {
|
||||
const payloads: Array<Record<string, unknown>> = [];
|
||||
const baseStreamFn: StreamFn = (model, _context, options) => {
|
||||
const payload: Record<string, unknown> = { model: model.id };
|
||||
if (initialReasoningEffort !== undefined) {
|
||||
payload.reasoning_effort = initialReasoningEffort;
|
||||
}
|
||||
options?.onPayload?.(payload, model);
|
||||
payloads.push(structuredClone(payload));
|
||||
return createAssistantMessageEventStream();
|
||||
};
|
||||
return { baseStreamFn, payloads };
|
||||
}
|
||||
|
||||
function createControlledPlainTextToolCallCompatStream() {
|
||||
const source = createAssistantMessageEventStream();
|
||||
const baseStream: StreamFn = () => source as ReturnType<StreamFn>;
|
||||
@@ -198,6 +235,40 @@ describe("createPayloadPatchStreamWrapper", () => {
|
||||
});
|
||||
});
|
||||
|
||||
describe("createOpenAICompatibleCompletionsThinkingOffWrapper", () => {
|
||||
it("maps reasoning_effort to the model's disabled value when thinking is off", () => {
|
||||
const { baseStreamFn, payloads } = createPayloadCapture("high");
|
||||
const wrapped = createOpenAICompatibleCompletionsThinkingOffWrapper(baseStreamFn, "off");
|
||||
void wrapped(lmstudioBinaryModel, { messages: [] }, {});
|
||||
|
||||
expect(payloads[0]?.reasoning_effort).toBe("none");
|
||||
});
|
||||
|
||||
it("drops reasoning_effort when the model has no disabled effort", () => {
|
||||
const { baseStreamFn, payloads } = createPayloadCapture("high");
|
||||
const wrapped = createOpenAICompatibleCompletionsThinkingOffWrapper(baseStreamFn, "off");
|
||||
void wrapped(lmstudioBareModel, { messages: [] }, {});
|
||||
|
||||
expect(payloads[0]).not.toHaveProperty("reasoning_effort");
|
||||
});
|
||||
|
||||
it("does not add reasoning_effort when none was sent", () => {
|
||||
const { baseStreamFn, payloads } = createPayloadCapture();
|
||||
const wrapped = createOpenAICompatibleCompletionsThinkingOffWrapper(baseStreamFn, "off");
|
||||
void wrapped(lmstudioBinaryModel, { messages: [] }, {});
|
||||
|
||||
expect(payloads[0]).not.toHaveProperty("reasoning_effort");
|
||||
});
|
||||
|
||||
it("leaves enabled thinking levels unchanged", () => {
|
||||
const { baseStreamFn, payloads } = createPayloadCapture("high");
|
||||
const wrapped = createOpenAICompatibleCompletionsThinkingOffWrapper(baseStreamFn, "high");
|
||||
void wrapped(lmstudioBinaryModel, { messages: [] }, {});
|
||||
|
||||
expect(payloads[0]?.reasoning_effort).toBe("high");
|
||||
});
|
||||
});
|
||||
|
||||
describe("createPlainTextToolCallCompatWrapper", () => {
|
||||
it("promotes standalone text tool calls into tool-call stream events", async () => {
|
||||
const baseStreamFn: StreamFn = () =>
|
||||
|
||||
@@ -9,7 +9,10 @@ import {
|
||||
type PlainTextToolCallNameMatcher,
|
||||
type PlainTextToolCallMessageNormalization,
|
||||
} from "../../packages/tool-call-repair/src/index.js";
|
||||
import { resolveOpenAIReasoningEffortMap } from "../agents/openai-reasoning-compat.js";
|
||||
import { resolveOpenAIReasoningEffortForModel } from "../agents/openai-reasoning-effort.js";
|
||||
import type { StreamFn } from "../agents/runtime/index.js";
|
||||
import type { ThinkLevel } from "../auto-reply/thinking.js";
|
||||
import { streamWithPayloadPatch } from "../llm/providers/stream-wrappers/stream-payload-utils.js";
|
||||
import { streamSimple } from "../llm/stream.js";
|
||||
import { createAssistantMessageEventStream } from "../llm/utils/event-stream.js";
|
||||
@@ -277,6 +280,44 @@ export function createPayloadPatchStreamWrapper(
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Applies explicit disabled-thinking intent to OpenAI-compatible Chat
|
||||
* Completions payloads without changing enabled reasoning levels.
|
||||
*/
|
||||
export function createOpenAICompatibleCompletionsThinkingOffWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
thinkingLevel?: ThinkLevel,
|
||||
): StreamFn {
|
||||
const underlying = baseStreamFn ?? streamSimple;
|
||||
if (thinkingLevel !== "off") {
|
||||
return underlying;
|
||||
}
|
||||
return (model, context, options) => {
|
||||
if (model.api !== "openai-completions") {
|
||||
return underlying(model, context, options);
|
||||
}
|
||||
return streamWithPayloadPatch(underlying, model, context, options, (payload) => {
|
||||
if (!("reasoning_effort" in payload)) {
|
||||
return;
|
||||
}
|
||||
const disabled = resolveOpenAIReasoningEffortForModel({
|
||||
model,
|
||||
effort: "none",
|
||||
fallbackMap: resolveOpenAIReasoningEffortMap({
|
||||
provider: typeof model.provider === "string" ? model.provider : null,
|
||||
id: typeof model.id === "string" ? model.id : null,
|
||||
compat: model.compat,
|
||||
}),
|
||||
});
|
||||
if (disabled) {
|
||||
payload.reasoning_effort = disabled;
|
||||
} else {
|
||||
delete payload.reasoning_effort;
|
||||
}
|
||||
});
|
||||
};
|
||||
}
|
||||
|
||||
function isAnthropicThinkingEnabled(payload: Record<string, unknown>): boolean {
|
||||
const thinking = payload.thinking;
|
||||
if (!thinking || typeof thinking !== "object") {
|
||||
|
||||
Reference in New Issue
Block a user