refactor(providers): add family replay and tool hooks

This commit is contained in:
Vincent Koc
2026-04-04 19:27:36 +09:00
parent 4e099689c0
commit 39d2a719c9
20 changed files with 273 additions and 85 deletions

View File

@@ -4,6 +4,22 @@
// without recursing through provider-specific facades.
import type { BedrockDiscoveryConfig, ModelDefinitionConfig } from "../config/types.models.js";
import type { ProviderPlugin } from "../plugins/types.js";
import {
buildAnthropicReplayPolicyForModel,
buildGoogleGeminiReplayPolicy,
buildHybridAnthropicOrOpenAIReplayPolicy,
buildOpenAICompatibleReplayPolicy,
buildPassthroughGeminiSanitizingReplayPolicy,
buildStrictAnthropicReplayPolicy,
resolveTaggedReasoningOutputMode,
sanitizeGoogleGeminiReplayHistory,
} from "../plugins/provider-replay-helpers.js";
import type {
ProviderReasoningOutputModeContext,
ProviderReplayPolicyContext,
ProviderSanitizeReplayHistoryContext,
} from "./plugin-entry.js";
export type { ModelApi, ModelProviderConfig } from "../config/types.models.js";
export type {
@@ -38,7 +54,7 @@ export {
resolveTaggedReasoningOutputMode,
sanitizeGoogleGeminiReplayHistory,
buildStrictAnthropicReplayPolicy,
} from "../plugins/provider-replay-helpers.js";
};
export {
createMoonshotThinkingWrapper,
resolveMoonshotThinkingType,
@@ -110,3 +126,62 @@ export function normalizeNativeXaiModelId(id: string): string {
}
return id;
}
export type ProviderReplayFamily =
| "openai-compatible"
| "anthropic-by-model"
| "google-gemini"
| "passthrough-gemini"
| "hybrid-anthropic-openai";
type ProviderReplayFamilyHooks = Pick<
ProviderPlugin,
"buildReplayPolicy" | "sanitizeReplayHistory" | "resolveReasoningOutputMode"
>;
type BuildProviderReplayFamilyHooksOptions =
| { family: "openai-compatible" }
| { family: "anthropic-by-model" }
| { family: "google-gemini" }
| { family: "passthrough-gemini" }
| {
family: "hybrid-anthropic-openai";
anthropicModelDropThinkingBlocks?: boolean;
};
export function buildProviderReplayFamilyHooks(
options: BuildProviderReplayFamilyHooksOptions,
): ProviderReplayFamilyHooks {
switch (options.family) {
case "openai-compatible":
return {
buildReplayPolicy: (ctx: ProviderReplayPolicyContext) =>
buildOpenAICompatibleReplayPolicy(ctx.modelApi),
};
case "anthropic-by-model":
return {
buildReplayPolicy: ({ modelId }: ProviderReplayPolicyContext) =>
buildAnthropicReplayPolicyForModel(modelId),
};
case "google-gemini":
return {
buildReplayPolicy: () => buildGoogleGeminiReplayPolicy(),
sanitizeReplayHistory: (ctx: ProviderSanitizeReplayHistoryContext) =>
sanitizeGoogleGeminiReplayHistory(ctx),
resolveReasoningOutputMode: (_ctx: ProviderReasoningOutputModeContext) =>
resolveTaggedReasoningOutputMode(),
};
case "passthrough-gemini":
return {
buildReplayPolicy: ({ modelId }: ProviderReplayPolicyContext) =>
buildPassthroughGeminiSanitizingReplayPolicy(modelId),
};
case "hybrid-anthropic-openai":
return {
buildReplayPolicy: (ctx: ProviderReplayPolicyContext) =>
buildHybridAnthropicOrOpenAIReplayPolicy(ctx, {
anthropicModelDropThinkingBlocks: options.anthropicModelDropThinkingBlocks,
}),
};
}
}