fix(agents): default proxy completions tool choice

This commit is contained in:
Speed-maker
2026-04-25 15:40:27 +08:00
committed by Peter Steinberger
parent 6a71c19839
commit 9a6b769e6e
2 changed files with 40 additions and 1 deletions

View File

@@ -2318,7 +2318,7 @@ describe("openai transport stream", () => {
expect(functionCall?.arguments).toBe("not valid json");
});
it("does not send tool_choice when tools are provided but toolChoice option is not set", () => {
it("defaults tool_choice to auto for proxy-like openai-completions endpoints", () => {
const params = buildOpenAICompletionsParams(
{
id: "test-model",
@@ -2346,6 +2346,38 @@ describe("openai transport stream", () => {
undefined,
);
expect(params).toHaveProperty("tools");
expect(params).toHaveProperty("tool_choice", "auto");
});
it("does not send tool_choice by default for native openai-completions endpoints", () => {
const params = buildOpenAICompletionsParams(
{
id: "gpt-5.4",
name: "GPT-5.4",
api: "openai-completions",
provider: "openai",
baseUrl: "https://api.openai.com/v1",
reasoning: false,
input: ["text"],
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
contextWindow: 4096,
maxTokens: 2048,
} satisfies Model<"openai-completions">,
{
systemPrompt: "You are a helpful assistant",
messages: [],
tools: [
{
name: "get_weather",
description: "Get weather information",
parameters: { type: "object", properties: {} },
},
],
} as never,
undefined,
);
expect(params).toHaveProperty("tools");
expect(params).not.toHaveProperty("tool_choice");
});

View File

@@ -1728,6 +1728,7 @@ export function buildOpenAICompletionsParams(
options: OpenAICompletionsOptions | undefined,
) {
const compat = getCompat(model);
const compatDetection = detectOpenAICompletionsCompat(model);
const completionsContext = context.systemPrompt
? {
...context,
@@ -1765,6 +1766,12 @@ export function buildOpenAICompletionsParams(
params.tools = convertTools(context.tools, compat, model);
if (options?.toolChoice) {
params.tool_choice = options.toolChoice;
} else if (
compatDetection.capabilities.usesExplicitProxyLikeEndpoint &&
Array.isArray(params.tools) &&
params.tools.length > 0
) {
params.tool_choice = "auto";
}
} else if (hasToolHistory(context.messages)) {
params.tools = [];