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

@@ -1,2 +1,2 @@
80a8238588cca3c6e0f89115f4271834b6e18f3497f70bc91dccc9203539cdd9 plugin-sdk-api-baseline.json
68ed97a285d82f995f377db7823104976e8ebab5c6d0750332acb1d3d79288ef plugin-sdk-api-baseline.jsonl
e173ab85a739667d8405ec017c03ec659ca3c9fe5bdfaff9f2ffcdd3f9fbfea1 plugin-sdk-api-baseline.json
7a0e0aa10f565a9e5ba03edb429bc66a7b87181861e55bba542dfa9fb1cd6197 plugin-sdk-api-baseline.jsonl

View File

@@ -1,6 +1,6 @@
import type { StreamFn } from "@mariozechner/pi-agent-core";
import type { OpenClawPluginApi } from "openclaw/plugin-sdk/plugin-entry";
import { buildAnthropicReplayPolicyForModel } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import {
createBedrockNoCacheWrapper,
isAnthropicBedrockModel,
@@ -46,6 +46,9 @@ function createGuardrailWrapStreamFn(
const PROVIDER_ID = "amazon-bedrock";
const CLAUDE_46_MODEL_RE = /claude-(?:opus|sonnet)-4(?:\.|-)6(?:$|[-.])/i;
const ANTHROPIC_BY_MODEL_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "anthropic-by-model",
});
const BEDROCK_CONTEXT_OVERFLOW_PATTERNS = [
/ValidationException.*(?:input is too long|max input token|input token.*exceed)/i,
/ValidationException.*(?:exceeds? the (?:maximum|max) (?:number of )?(?:input )?tokens)/i,
@@ -89,7 +92,7 @@ export async function registerAmazonBedrockPlugin(api: OpenClawPluginApi): Promi
},
},
resolveConfigApiKey: ({ env }) => resolveBedrockConfigApiKey(env),
buildReplayPolicy: ({ modelId }) => buildAnthropicReplayPolicyForModel(modelId),
...ANTHROPIC_BY_MODEL_REPLAY_HOOKS,
wrapStreamFn,
matchesContextOverflowError: ({ errorMessage }) =>
BEDROCK_CONTEXT_OVERFLOW_PATTERNS.some((pattern) => pattern.test(errorMessage)),

View File

@@ -1,5 +1,5 @@
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
import { buildAnthropicReplayPolicyForModel } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import {
mergeImplicitAnthropicVertexProvider,
resolveAnthropicVertexConfigApiKey,
@@ -7,6 +7,9 @@ import {
} from "./api.js";
const PROVIDER_ID = "anthropic-vertex";
const ANTHROPIC_BY_MODEL_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "anthropic-by-model",
});
export default definePluginEntry({
id: PROVIDER_ID,
@@ -36,7 +39,7 @@ export default definePluginEntry({
},
},
resolveConfigApiKey: ({ env }) => resolveAnthropicVertexConfigApiKey(env),
buildReplayPolicy: ({ modelId }) => buildAnthropicReplayPolicyForModel(modelId),
...ANTHROPIC_BY_MODEL_REPLAY_HOOKS,
});
},
});

View File

@@ -2,12 +2,15 @@ import type {
OpenClawPluginApi,
ProviderAuthContext,
ProviderFetchUsageSnapshotContext,
ProviderWrapStreamFnContext,
} from "openclaw/plugin-sdk/plugin-entry";
import { buildOauthProviderAuthResult } from "openclaw/plugin-sdk/provider-auth-result";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import { createGoogleThinkingPayloadWrapper } from "openclaw/plugin-sdk/provider-stream";
import { buildProviderToolCompatFamilyHooks } from "openclaw/plugin-sdk/provider-tools";
import { fetchGeminiUsage } from "openclaw/plugin-sdk/provider-usage";
import { formatGoogleOauthApiKey, parseGoogleUsageToken } from "./oauth-token-shared.js";
import { isModernGoogleModel, resolveGoogle31ForwardCompatModel } from "./provider-models.js";
import { buildGoogleGeminiProviderHooks } from "./replay-policy.js";
const PROVIDER_ID = "google-gemini-cli";
const PROVIDER_LABEL = "Gemini CLI OAuth";
@@ -19,9 +22,12 @@ const ENV_VARS = [
"GEMINI_CLI_OAUTH_CLIENT_SECRET",
];
const GOOGLE_GEMINI_CLI_PROVIDER_HOOKS = buildGoogleGeminiProviderHooks({
includeToolSchemaCompat: true,
});
const GOOGLE_GEMINI_CLI_PROVIDER_HOOKS = {
...buildProviderReplayFamilyHooks({ family: "google-gemini" }),
wrapStreamFn: (ctx: ProviderWrapStreamFnContext) =>
createGoogleThinkingPayloadWrapper(ctx.streamFn, ctx.thinkingLevel),
...buildProviderToolCompatFamilyHooks("gemini"),
};
async function fetchGeminiCliUsage(ctx: ProviderFetchUsageSnapshotContext) {
return await fetchGeminiUsage(ctx.token, ctx.timeoutMs, ctx.fetchFn, PROVIDER_ID);

View File

@@ -5,9 +5,13 @@ import {
type OpenClawPluginApi,
type ProviderAuthContext,
type ProviderFetchUsageSnapshotContext,
type ProviderWrapStreamFnContext,
} from "openclaw/plugin-sdk/plugin-entry";
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import type { ProviderPlugin } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import { createGoogleThinkingPayloadWrapper } from "openclaw/plugin-sdk/provider-stream";
import { buildProviderToolCompatFamilyHooks } from "openclaw/plugin-sdk/provider-tools";
import {
GOOGLE_GEMINI_DEFAULT_MODEL,
applyGoogleGeminiModelDefault,
@@ -18,7 +22,6 @@ import {
import { buildGoogleGeminiCliBackend } from "./cli-backend.js";
import { formatGoogleOauthApiKey } from "./oauth-token-shared.js";
import { isModernGoogleModel, resolveGoogle31ForwardCompatModel } from "./provider-models.js";
import { buildGoogleGeminiProviderHooks } from "./replay-policy.js";
import { createGeminiWebSearchProvider } from "./src/gemini-web-search-provider.js";
const GOOGLE_GEMINI_CLI_PROVIDER_ID = "google-gemini-cli";
@@ -42,10 +45,18 @@ type GoogleMediaUnderstandingProvider = MediaUnderstandingProvider & {
describeVideo: NonNullable<MediaUnderstandingProvider["describeVideo"]>;
};
const GOOGLE_GEMINI_PROVIDER_HOOKS = buildGoogleGeminiProviderHooks();
const GOOGLE_GEMINI_PROVIDER_HOOKS_WITH_TOOL_COMPAT = buildGoogleGeminiProviderHooks({
includeToolSchemaCompat: true,
const GOOGLE_GEMINI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "google-gemini",
});
const GOOGLE_GEMINI_PROVIDER_HOOKS = {
...GOOGLE_GEMINI_REPLAY_HOOKS,
wrapStreamFn: (ctx: ProviderWrapStreamFnContext) =>
createGoogleThinkingPayloadWrapper(ctx.streamFn, ctx.thinkingLevel),
};
const GOOGLE_GEMINI_PROVIDER_HOOKS_WITH_TOOL_COMPAT = {
...GOOGLE_GEMINI_PROVIDER_HOOKS,
...buildProviderToolCompatFamilyHooks("gemini"),
};
async function loadGoogleGeminiCliProvider(): Promise<ProviderPlugin> {
if (!googleGeminiCliProviderPromise) {

View File

@@ -1,38 +0,0 @@
import type {
ProviderNormalizeToolSchemasContext,
ProviderSanitizeReplayHistoryContext,
ProviderWrapStreamFnContext,
} from "openclaw/plugin-sdk/plugin-entry";
import {
buildGoogleGeminiReplayPolicy,
resolveTaggedReasoningOutputMode,
sanitizeGoogleGeminiReplayHistory,
} from "openclaw/plugin-sdk/provider-model-shared";
import { createGoogleThinkingPayloadWrapper } from "openclaw/plugin-sdk/provider-stream";
import {
inspectGeminiToolSchemas,
normalizeGeminiToolSchemas,
} from "openclaw/plugin-sdk/provider-tools";
type GoogleGeminiHookOptions = {
includeToolSchemaCompat?: boolean;
};
export function buildGoogleGeminiProviderHooks(options: GoogleGeminiHookOptions = {}) {
return {
buildReplayPolicy: () => buildGoogleGeminiReplayPolicy(),
wrapStreamFn: (ctx: ProviderWrapStreamFnContext) =>
createGoogleThinkingPayloadWrapper(ctx.streamFn, ctx.thinkingLevel),
sanitizeReplayHistory: (ctx: ProviderSanitizeReplayHistoryContext) =>
sanitizeGoogleGeminiReplayHistory(ctx),
resolveReasoningOutputMode: () => resolveTaggedReasoningOutputMode(),
...(options.includeToolSchemaCompat
? {
normalizeToolSchemas: (ctx: ProviderNormalizeToolSchemasContext) =>
normalizeGeminiToolSchemas(ctx),
inspectToolSchemas: (ctx: ProviderNormalizeToolSchemasContext) =>
inspectGeminiToolSchemas(ctx),
}
: {}),
};
}

View File

@@ -1,5 +1,5 @@
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
import { buildPassthroughGeminiSanitizingReplayPolicy } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import {
createKilocodeWrapper,
isProxyReasoningUnsupported,
@@ -8,6 +8,9 @@ import { applyKilocodeConfig, KILOCODE_DEFAULT_MODEL_REF } from "./onboard.js";
import { buildKilocodeProviderWithDiscovery } from "./provider-catalog.js";
const PROVIDER_ID = "kilocode";
const PASSTHROUGH_GEMINI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "passthrough-gemini",
});
export default defineSingleProviderPluginEntry({
id: PROVIDER_ID,
@@ -32,7 +35,7 @@ export default defineSingleProviderPluginEntry({
catalog: {
buildProvider: buildKilocodeProviderWithDiscovery,
},
buildReplayPolicy: ({ modelId }) => buildPassthroughGeminiSanitizingReplayPolicy(modelId),
...PASSTHROUGH_GEMINI_REPLAY_HOOKS,
wrapStreamFn: (ctx) => {
const thinkingLevel =
ctx.modelId === "kilo/auto" || isProxyReasoningUnsupported(ctx.modelId)

View File

@@ -11,7 +11,7 @@ import {
} from "openclaw/plugin-sdk/provider-auth";
import { buildOauthProviderAuthResult } from "openclaw/plugin-sdk/provider-auth";
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import { buildHybridAnthropicOrOpenAIReplayPolicy } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import { createMinimaxFastModeWrapper } from "openclaw/plugin-sdk/provider-stream";
import { fetchMinimaxUsage } from "openclaw/plugin-sdk/provider-usage";
import { isMiniMaxModernModelId, MINIMAX_DEFAULT_MODEL_ID } from "./api.js";
@@ -34,6 +34,10 @@ const PROVIDER_LABEL = "MiniMax";
const DEFAULT_MODEL = MINIMAX_DEFAULT_MODEL_ID;
const DEFAULT_BASE_URL_CN = "https://api.minimaxi.com/anthropic";
const DEFAULT_BASE_URL_GLOBAL = "https://api.minimax.io/anthropic";
const HYBRID_ANTHROPIC_OPENAI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "hybrid-anthropic-openai",
anthropicModelDropThinkingBlocks: true,
});
function resolveMinimaxReasoningOutputMode(): "native" {
// Keep MiniMax on native reasoning mode. Tagged enforcement previously
@@ -237,10 +241,7 @@ export default definePluginEntry({
});
return apiKey ? { token: apiKey } : null;
},
buildReplayPolicy: (ctx) =>
buildHybridAnthropicOrOpenAIReplayPolicy(ctx, {
anthropicModelDropThinkingBlocks: true,
}),
...HYBRID_ANTHROPIC_OPENAI_REPLAY_HOOKS,
wrapStreamFn: (ctx) =>
createMinimaxFastModeWrapper(ctx.streamFn, ctx.extraParams?.fastMode === true),
resolveReasoningOutputMode: () => resolveMinimaxReasoningOutputMode(),
@@ -293,10 +294,7 @@ export default definePluginEntry({
run: createOAuthHandler("cn"),
},
],
buildReplayPolicy: (ctx) =>
buildHybridAnthropicOrOpenAIReplayPolicy(ctx, {
anthropicModelDropThinkingBlocks: true,
}),
...HYBRID_ANTHROPIC_OPENAI_REPLAY_HOOKS,
wrapStreamFn: (ctx) =>
createMinimaxFastModeWrapper(ctx.streamFn, ctx.extraParams?.fastMode === true),
resolveReasoningOutputMode: () => resolveMinimaxReasoningOutputMode(),

View File

@@ -1,5 +1,5 @@
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
import { buildOpenAICompatibleReplayPolicy } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import {
createMoonshotThinkingWrapper,
resolveMoonshotThinkingType,
@@ -15,6 +15,9 @@ import { buildMoonshotProvider } from "./provider-catalog.js";
import { createKimiWebSearchProvider } from "./src/kimi-web-search-provider.js";
const PROVIDER_ID = "moonshot";
const OPENAI_COMPATIBLE_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "openai-compatible",
});
export default defineSingleProviderPluginEntry({
id: PROVIDER_ID,
@@ -59,7 +62,7 @@ export default defineSingleProviderPluginEntry({
},
applyNativeStreamingUsageCompat: ({ providerConfig }) =>
applyMoonshotNativeStreamingUsageCompat(providerConfig),
buildReplayPolicy: (ctx) => buildOpenAICompatibleReplayPolicy(ctx.modelApi),
...OPENAI_COMPATIBLE_REPLAY_HOOKS,
wrapStreamFn: (ctx) => {
const thinkingType = resolveMoonshotThinkingType({
configuredThinking: ctx.extraParams?.thinking,

View File

@@ -6,7 +6,7 @@ import {
type ProviderAuthResult,
type ProviderDiscoveryContext,
} from "openclaw/plugin-sdk/plugin-entry";
import { buildOpenAICompatibleReplayPolicy } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import {
buildOllamaProvider,
configureOllamaNonInteractive,
@@ -27,6 +27,9 @@ import { createOllamaWebSearchProvider } from "./src/web-search-provider.js";
const PROVIDER_ID = "ollama";
const DEFAULT_API_KEY = "ollama-local";
const OPENAI_COMPATIBLE_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "openai-compatible",
});
function shouldSkipAmbientOllamaDiscovery(env: NodeJS.ProcessEnv): boolean {
return Boolean(env.VITEST) || env.NODE_ENV === "test";
@@ -152,7 +155,7 @@ export default definePluginEntry({
providerBaseUrl: config?.models?.providers?.ollama?.baseUrl,
});
},
buildReplayPolicy: (ctx) => buildOpenAICompatibleReplayPolicy(ctx.modelApi),
...OPENAI_COMPATIBLE_REPLAY_HOOKS,
resolveReasoningOutputMode: () => "native",
wrapStreamFn: (ctx) => {
return createConfiguredOllamaCompatStreamWrapper(ctx);

View File

@@ -1,9 +1,12 @@
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import { buildPassthroughGeminiSanitizingReplayPolicy } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import { applyOpencodeGoConfig, OPENCODE_GO_DEFAULT_MODEL_REF } from "./api.js";
const PROVIDER_ID = "opencode-go";
const PASSTHROUGH_GEMINI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "passthrough-gemini",
});
export default definePluginEntry({
id: PROVIDER_ID,
@@ -44,7 +47,7 @@ export default definePluginEntry({
},
}),
],
buildReplayPolicy: ({ modelId }) => buildPassthroughGeminiSanitizingReplayPolicy(modelId),
...PASSTHROUGH_GEMINI_REPLAY_HOOKS,
isModernModelRef: () => true,
});
},

View File

@@ -1,13 +1,16 @@
import { definePluginEntry } from "openclaw/plugin-sdk/plugin-entry";
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import {
buildPassthroughGeminiSanitizingReplayPolicy,
buildProviderReplayFamilyHooks,
matchesExactOrPrefix,
} from "openclaw/plugin-sdk/provider-model-shared";
import { applyOpencodeZenConfig, OPENCODE_ZEN_DEFAULT_MODEL } from "./api.js";
const PROVIDER_ID = "opencode";
const MINIMAX_MODERN_MODEL_MATCHERS = ["minimax-m2.7"] as const;
const PASSTHROUGH_GEMINI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "passthrough-gemini",
});
function isModernOpencodeModel(modelId: string): boolean {
const lower = modelId.trim().toLowerCase();
@@ -57,7 +60,7 @@ export default definePluginEntry({
},
}),
],
buildReplayPolicy: ({ modelId }) => buildPassthroughGeminiSanitizingReplayPolicy(modelId),
...PASSTHROUGH_GEMINI_REPLAY_HOOKS,
isModernModelRef: ({ modelId }) => isModernOpencodeModel(modelId),
});
},

View File

@@ -21,7 +21,7 @@ export default definePluginEntry({
description: "Bundled OpenRouter provider plugin",
async register(api) {
const {
buildPassthroughGeminiSanitizingReplayPolicy,
buildProviderReplayFamilyHooks,
composeProviderStreamWrappers,
createOpenRouterWrapper,
createProviderApiKeyAuthMethod,
@@ -34,6 +34,9 @@ export default definePluginEntry({
applyOpenrouterConfig,
buildOpenrouterProvider,
} = await import("./register.runtime.js");
const PASSTHROUGH_GEMINI_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "passthrough-gemini",
});
function buildDynamicOpenRouterModel(
ctx: ProviderResolveDynamicModelContext,
@@ -128,7 +131,7 @@ export default definePluginEntry({
prepareDynamicModel: async (ctx) => {
await loadOpenRouterModelCapabilities(ctx.modelId);
},
buildReplayPolicy: ({ modelId }) => buildPassthroughGeminiSanitizingReplayPolicy(modelId),
...PASSTHROUGH_GEMINI_REPLAY_HOOKS,
resolveReasoningOutputMode: () => "native",
isModernModelRef: () => true,
wrapStreamFn: (ctx) => {

View File

@@ -1,6 +1,6 @@
export { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
export {
buildPassthroughGeminiSanitizingReplayPolicy,
buildProviderReplayFamilyHooks,
DEFAULT_CONTEXT_TOKENS,
} from "openclaw/plugin-sdk/provider-model-shared";
export {

View File

@@ -1,7 +1,7 @@
import { Type } from "@sinclair/typebox";
import type { OpenClawConfig } from "openclaw/plugin-sdk/plugin-entry";
import { defineSingleProviderPluginEntry } from "openclaw/plugin-sdk/provider-entry";
import { buildOpenAICompatibleReplayPolicy } from "openclaw/plugin-sdk/provider-model-shared";
import { buildProviderReplayFamilyHooks } from "openclaw/plugin-sdk/provider-model-shared";
import {
composeProviderStreamWrappers,
createToolStreamWrapper,
@@ -30,6 +30,9 @@ import { resolveFallbackXaiAuth } from "./src/tool-auth-shared.js";
import { createXaiWebSearchProvider } from "./web-search.js";
const PROVIDER_ID = "xai";
const OPENAI_COMPATIBLE_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "openai-compatible",
});
function hasResolvableXaiApiKey(config: unknown): boolean {
return Boolean(
@@ -200,7 +203,7 @@ export default defineSingleProviderPluginEntry({
catalog: {
buildProvider: buildXaiProvider,
},
buildReplayPolicy: (ctx) => buildOpenAICompatibleReplayPolicy(ctx.modelApi),
...OPENAI_COMPATIBLE_REPLAY_HOOKS,
prepareExtraParams: (ctx) => {
const extraParams = ctx.extraParams;
if (extraParams && extraParams.tool_stream !== undefined) {

View File

@@ -3,8 +3,6 @@ import {
type ProviderAuthContext,
type ProviderAuthMethod,
type ProviderAuthMethodNonInteractiveContext,
type ProviderReplayPolicy,
type ProviderReplayPolicyContext,
type ProviderResolveDynamicModelContext,
type ProviderRuntimeModel,
} from "openclaw/plugin-sdk/plugin-entry";
@@ -19,7 +17,7 @@ import {
validateApiKeyInput,
} from "openclaw/plugin-sdk/provider-auth-api-key";
import {
buildOpenAICompatibleReplayPolicy,
buildProviderReplayFamilyHooks,
normalizeModelCompat,
} from "openclaw/plugin-sdk/provider-model-shared";
import { createZaiToolStreamWrapper } from "openclaw/plugin-sdk/provider-stream";
@@ -32,10 +30,9 @@ import { applyZaiConfig, applyZaiProviderConfig, ZAI_DEFAULT_MODEL_REF } from ".
const PROVIDER_ID = "zai";
const GLM5_TEMPLATE_MODEL_ID = "glm-4.7";
const PROFILE_ID = "zai:default";
function buildZaiReplayPolicy(ctx: ProviderReplayPolicyContext): ProviderReplayPolicy | undefined {
return buildOpenAICompatibleReplayPolicy(ctx.modelApi);
}
const OPENAI_COMPATIBLE_REPLAY_HOOKS = buildProviderReplayFamilyHooks({
family: "openai-compatible",
});
function resolveGlm5ForwardCompatModel(
ctx: ProviderResolveDynamicModelContext,
@@ -281,7 +278,7 @@ export default definePluginEntry({
}),
],
resolveDynamicModel: (ctx) => resolveGlm5ForwardCompatModel(ctx),
buildReplayPolicy: (ctx) => buildZaiReplayPolicy(ctx),
...OPENAI_COMPATIBLE_REPLAY_HOOKS,
prepareExtraParams: (ctx) => {
if (ctx.extraParams?.tool_stream !== undefined) {
return ctx.extraParams;

View File

@@ -0,0 +1,80 @@
import { describe, expect, it } from "vitest";
import { buildProviderReplayFamilyHooks } from "./provider-model-shared.js";
describe("buildProviderReplayFamilyHooks", () => {
it("maps openai-compatible replay families", () => {
const hooks = buildProviderReplayFamilyHooks({
family: "openai-compatible",
});
expect(
hooks.buildReplayPolicy?.({
provider: "xai",
modelApi: "openai-completions",
modelId: "grok-4",
} as never),
).toMatchObject({
sanitizeToolCallIds: true,
applyAssistantFirstOrderingFix: true,
validateGeminiTurns: true,
});
});
it("maps google-gemini replay families", () => {
const hooks = buildProviderReplayFamilyHooks({
family: "google-gemini",
});
expect(hooks.resolveReasoningOutputMode?.({} as never)).toBe("tagged");
expect(
hooks.buildReplayPolicy?.({
provider: "google",
modelApi: "google-generative-ai",
modelId: "gemini-3.1-pro-preview",
} as never),
).toMatchObject({
validateGeminiTurns: true,
allowSyntheticToolResults: true,
});
const sanitized = hooks.sanitizeReplayHistory?.({
provider: "google",
modelApi: "google-generative-ai",
modelId: "gemini-3.1-pro-preview",
sessionId: "session-1",
messages: [
{
role: "assistant",
content: [{ type: "text", text: "hello" }],
},
],
sessionState: {
getCustomEntries: () => [],
appendCustomEntry: () => {},
},
} as never);
expect(sanitized?.[0]).toMatchObject({
role: "user",
content: "(session bootstrap)",
});
});
it("maps hybrid anthropic/openai replay families", () => {
const hooks = buildProviderReplayFamilyHooks({
family: "hybrid-anthropic-openai",
anthropicModelDropThinkingBlocks: true,
});
expect(
hooks.buildReplayPolicy?.({
provider: "minimax",
modelApi: "anthropic-messages",
modelId: "claude-sonnet-4-6",
} as never),
).toMatchObject({
validateAnthropicTurns: true,
dropThinkingBlocks: true,
});
});
});

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,
}),
};
}
}

View File

@@ -0,0 +1,15 @@
import { describe, expect, it } from "vitest";
import {
buildProviderToolCompatFamilyHooks,
inspectGeminiToolSchemas,
normalizeGeminiToolSchemas,
} from "./provider-tools.js";
describe("buildProviderToolCompatFamilyHooks", () => {
it("maps the gemini family to the shared schema helpers", () => {
const hooks = buildProviderToolCompatFamilyHooks("gemini");
expect(hooks.normalizeToolSchemas).toBe(normalizeGeminiToolSchemas);
expect(hooks.inspectToolSchemas).toBe(inspectGeminiToolSchemas);
});
});

View File

@@ -158,3 +158,20 @@ export function inspectGeminiToolSchemas(
return [{ toolName: tool.name, toolIndex, violations }];
});
}
export type ProviderToolCompatFamily = "gemini";
export function buildProviderToolCompatFamilyHooks(family: ProviderToolCompatFamily): {
normalizeToolSchemas: (ctx: ProviderNormalizeToolSchemasContext) => AnyAgentTool[];
inspectToolSchemas: (
ctx: ProviderNormalizeToolSchemasContext,
) => ProviderToolSchemaDiagnostic[];
} {
switch (family) {
case "gemini":
return {
normalizeToolSchemas: normalizeGeminiToolSchemas,
inspectToolSchemas: inspectGeminiToolSchemas,
};
}
}