refactor(providers): compose provider stream wrappers

This commit is contained in:
Vincent Koc
2026-04-04 11:02:56 +09:00
parent 85c76e83b7
commit 30479b4ee0
5 changed files with 89 additions and 27 deletions

View File

@@ -7,6 +7,7 @@ import {
import { createProviderApiKeyAuthMethod } from "openclaw/plugin-sdk/provider-auth-api-key";
import { DEFAULT_CONTEXT_TOKENS } from "openclaw/plugin-sdk/provider-model-shared";
import {
composeProviderStreamWrappers,
getOpenRouterModelCapabilities,
loadOpenRouterModelCapabilities,
createOpenRouterSystemCacheWrapper,
@@ -145,20 +146,21 @@ export default definePluginEntry({
buildReplayPolicy: ({ modelId }) => buildOpenRouterReplayPolicy(modelId),
isModernModelRef: () => true,
wrapStreamFn: (ctx) => {
let streamFn = ctx.streamFn;
const providerRouting =
ctx.extraParams?.provider != null && typeof ctx.extraParams.provider === "object"
? (ctx.extraParams.provider as Record<string, unknown>)
: undefined;
if (providerRouting) {
streamFn = injectOpenRouterRouting(streamFn, providerRouting);
}
const skipReasoningInjection =
ctx.modelId === "auto" || isProxyReasoningUnsupported(ctx.modelId);
const openRouterThinkingLevel = skipReasoningInjection ? undefined : ctx.thinkingLevel;
streamFn = createOpenRouterWrapper(streamFn, openRouterThinkingLevel);
streamFn = createOpenRouterSystemCacheWrapper(streamFn);
return streamFn;
return composeProviderStreamWrappers(
ctx.streamFn,
providerRouting
? (streamFn) => injectOpenRouterRouting(streamFn, providerRouting)
: undefined,
(streamFn) => createOpenRouterWrapper(streamFn, openRouterThinkingLevel),
(streamFn) => createOpenRouterSystemCacheWrapper(streamFn),
);
},
isCacheTtlEligible: (ctx) => isOpenRouterCacheTtlModel(ctx.modelId),
});