fix(ai): port openai responses handoff guard

This commit is contained in:
Markus Ylisiurunen 2026-01-22 18:55:30 +02:00 committed by Mario Zechner
parent 5edec3a40a
commit bd7049b7d1
2 changed files with 16 additions and 2 deletions

View file

@ -6,7 +6,7 @@ if (typeof process !== "undefined" && (process.versions?.node || process.version
});
}
import type { ResponseInput, ResponseStreamEvent, Tool as OpenAITool } from "openai/resources/responses/responses.js";
import type { Tool as OpenAITool, ResponseInput, ResponseStreamEvent } from "openai/resources/responses/responses.js";
import { getEnvApiKey } from "../stream.js";
import type { Api, AssistantMessage, Context, Model, StreamFunction, StreamOptions } from "../types.js";
import { AssistantMessageEventStream } from "../utils/event-stream.js";

View file

@ -135,6 +135,11 @@ export function convertResponsesMessages<TApi extends Api>(
}
} else if (msg.role === "assistant") {
const output: ResponseInput = [];
const assistantMsg = msg as AssistantMessage;
const isDifferentModel =
assistantMsg.model !== model.id &&
assistantMsg.provider === model.provider &&
assistantMsg.api === model.api;
for (const block of msg.content) {
if (block.type === "thinking") {
@ -160,7 +165,16 @@ export function convertResponsesMessages<TApi extends Api>(
} satisfies ResponseOutputMessage);
} else if (block.type === "toolCall") {
const toolCall = block as ToolCall;
const [callId, itemId] = toolCall.id.split("|");
const [callId, itemIdRaw] = toolCall.id.split("|");
let itemId: string | undefined = itemIdRaw;
// For different-model messages, set id to undefined to avoid pairing validation.
// OpenAI tracks which fc_xxx IDs were paired with rs_xxx reasoning items.
// By omitting the id, we avoid triggering that validation (like cross-provider does).
if (isDifferentModel && itemId?.startsWith("fc_")) {
itemId = undefined;
}
output.push({
type: "function_call",
id: itemId,