fix(agent): forward configured tool choice into stream params

Ensure agent model params can force tool_choice through the embedded runner so OpenAI-compatible providers actually receive the required tool policy.

Made-with: Cursor
This commit is contained in:
Alishahed 2026-03-20 22:44:36 -07:00
parent 9fb78453e0
commit fe8f06db4e
3 changed files with 43 additions and 0 deletions

View File

@ -1,5 +1,6 @@
import type { Model } from "@mariozechner/pi-ai";
import { afterEach, describe, expect, it } from "vitest";
import type { OpenClawConfig } from "../../config/config.js";
import { captureEnv } from "../../test-utils/env.js";
import { runExtraParamsCase } from "./extra-params.test-support.js";
@ -93,3 +94,35 @@ describe("extra-params: OpenAI attribution", () => {
});
});
});
describe("extra-params: tool choice forwarding", () => {
it("forwards tool_choice from agent model params into stream options", () => {
const cfg = {
agents: {
defaults: {
models: {
"openai/gpt-5.4": {
params: {
tool_choice: "required",
},
},
},
},
},
} satisfies OpenClawConfig;
const capture = runExtraParamsCase({
applyModelId: "gpt-5.4",
applyProvider: "openai",
cfg,
model: {
api: "openai-completions",
provider: "openai",
id: "gpt-5.4",
} as Model<"openai-completions">,
payload: {},
});
expect((capture.options as { toolChoice?: string } | undefined)?.toolChoice).toBe("required");
});
});

View File

@ -5,6 +5,7 @@ import { applyExtraParamsToAgent } from "./extra-params.js";
export type ExtraParamsCapture<TPayload extends Record<string, unknown>> = {
headers?: Record<string, string>;
options?: SimpleStreamOptions;
payload: TPayload;
};
@ -32,6 +33,7 @@ export function runExtraParamsCase<
const baseStreamFn: StreamFn = (model, _context, options) => {
captured.headers = options?.headers;
captured.options = options;
options?.onPayload?.(params.payload, model);
return {} as ReturnType<StreamFn>;
};

View File

@ -90,6 +90,14 @@ function createStreamFnWithExtraParams(
if (typeof extraParams.maxTokens === "number") {
streamParams.maxTokens = extraParams.maxTokens;
}
const resolvedToolChoice = resolveAliasedParamValue(
[extraParams],
"tool_choice",
"toolChoice",
);
if (resolvedToolChoice !== undefined) {
streamParams.toolChoice = resolvedToolChoice as CacheRetentionStreamOptions["toolChoice"];
}
const transport = extraParams.transport;
if (transport === "sse" || transport === "websocket" || transport === "auto") {
streamParams.transport = transport;