refactor(providers): share xai and replay helpers

This commit is contained in:
Vincent Koc
2026-04-04 04:08:06 +09:00
parent cc1881a838
commit 9224afca3d
9 changed files with 156 additions and 92 deletions

View File

@@ -0,0 +1,27 @@
import { describe, expect, it } from "vitest";
import {
buildOpenAICompatibleReplayPolicy,
buildStrictAnthropicReplayPolicy,
} from "./provider-replay-helpers.js";
describe("provider replay helpers", () => {
it("builds strict openai-completions replay policy", () => {
expect(buildOpenAICompatibleReplayPolicy("openai-completions")).toMatchObject({
sanitizeToolCallIds: true,
toolCallIdMode: "strict",
applyAssistantFirstOrderingFix: true,
validateGeminiTurns: true,
validateAnthropicTurns: true,
});
});
it("builds strict anthropic replay policy", () => {
expect(buildStrictAnthropicReplayPolicy({ dropThinkingBlocks: true })).toMatchObject({
sanitizeMode: "full",
preserveSignatures: true,
repairToolUseResultPairing: true,
allowSyntheticToolResults: true,
dropThinkingBlocks: true,
});
});
});

View File

@@ -0,0 +1,45 @@
import type { ProviderReplayPolicy } from "./types.js";
export function buildOpenAICompatibleReplayPolicy(
modelApi: string | null | undefined,
): ProviderReplayPolicy | undefined {
if (
modelApi !== "openai-completions" &&
modelApi !== "openai-responses" &&
modelApi !== "openai-codex-responses" &&
modelApi !== "azure-openai-responses"
) {
return undefined;
}
return {
sanitizeToolCallIds: true,
toolCallIdMode: "strict",
...(modelApi === "openai-completions"
? {
applyAssistantFirstOrderingFix: true,
validateGeminiTurns: true,
validateAnthropicTurns: true,
}
: {
applyAssistantFirstOrderingFix: false,
validateGeminiTurns: false,
validateAnthropicTurns: false,
}),
};
}
export function buildStrictAnthropicReplayPolicy(
options: { dropThinkingBlocks?: boolean } = {},
): ProviderReplayPolicy {
return {
sanitizeMode: "full",
sanitizeToolCallIds: true,
toolCallIdMode: "strict",
preserveSignatures: true,
repairToolUseResultPairing: true,
validateAnthropicTurns: true,
allowSyntheticToolResults: true,
...(options.dropThinkingBlocks ? { dropThinkingBlocks: true } : {}),
};
}