Skip to content

Commit 57187f8

Browse files
author
Mitsuyuki Osabe
committed
feat: pass through OpenRouter provider routing params
extraParams.provider was silently dropped by createStreamFnWithExtraParams(). This change injects it into model.compat.openRouterRouting so pi-ai's buildParams includes params.provider in the API request body. Enables OpenRouter provider routing options (only, order, allow_fallbacks, data_collection, ignore, sort, quantizations) via model config: ```jsonc "openrouter/model-name": { "params": { "provider": { "only": ["deepinfra", "fireworks"], "allow_fallbacks": false } } } ``` Closes #10869 ✍️ Author: Claude Code with @carrotRakko (AI-written, human-approved)
1 parent 9e2233d commit 57187f8

1 file changed

Lines changed: 28 additions & 3 deletions

File tree

src/agents/pi-embedded-runner/extra-params.ts

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,43 @@ function createStreamFnWithExtraParams(
8989
streamParams.cacheRetention = cacheRetention;
9090
}
9191

92-
if (Object.keys(streamParams).length === 0) {
92+
// Extract OpenRouter provider routing preferences from extraParams.provider.
93+
// Injected into model.compat.openRouterRouting so pi-ai's buildParams sets
94+
// params.provider in the API request body (openai-completions.js L359-362).
95+
// pi-ai's OpenRouterRouting type only declares { only?, order? }, but at
96+
// runtime the full object is forwarded — enabling allow_fallbacks,
97+
// data_collection, ignore, sort, quantizations, etc.
98+
const providerRouting =
99+
provider === "openrouter" &&
100+
extraParams.provider != null &&
101+
typeof extraParams.provider === "object"
102+
? (extraParams.provider as Record<string, unknown>)
103+
: undefined;
104+
105+
if (Object.keys(streamParams).length === 0 && !providerRouting) {
93106
return undefined;
94107
}
95108

96109
log.debug(`creating streamFn wrapper with params: ${JSON.stringify(streamParams)}`);
110+
if (providerRouting) {
111+
log.debug(`OpenRouter provider routing: ${JSON.stringify(providerRouting)}`);
112+
}
97113

98114
const underlying = baseStreamFn ?? streamSimple;
99-
const wrappedStreamFn: StreamFn = (model, context, options) =>
100-
underlying(model, context, {
115+
const wrappedStreamFn: StreamFn = (model, context, options) => {
116+
// When provider routing is configured, inject it into model.compat so
117+
// pi-ai picks it up via model.compat.openRouterRouting.
118+
const effectiveModel = providerRouting
119+
? ({
120+
...model,
121+
compat: { ...model.compat, openRouterRouting: providerRouting },
122+
} as unknown as typeof model)
123+
: model;
124+
return underlying(effectiveModel, context, {
101125
...streamParams,
102126
...options,
103127
});
128+
};
104129

105130
return wrappedStreamFn;
106131
}

0 commit comments

Comments
 (0)