Vendor OpenClaw source as Adolf fork baseline
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Some checks failed
ClawSweeper Dispatch / dispatch (push) Has been cancelled
CodeQL / Security High (actions) (push) Has been cancelled
CodeQL / Security High (channel-runtime-boundary) (push) Has been cancelled
CodeQL / Security High (core-auth-secrets) (push) Has been cancelled
CodeQL / Security High (mcp-process-tool-boundary) (push) Has been cancelled
CodeQL / Security High (network-ssrf-boundary) (push) Has been cancelled
CodeQL / Security High (plugin-trust-boundary) (push) Has been cancelled
CodeQL / Security High (process-exec-boundary) (push) Has been cancelled
Docs Sync Publish Repo / sync-publish-repo (push) Has been cancelled
Docs / docs (push) Has been cancelled
OpenClaw Stable Main Closeout / Resolve stable release closeout inputs (push) Has been cancelled
OpenClaw Stable Main Closeout / Verify stable main closeout (push) Has been cancelled
Workflow Sanity / no-tabs (push) Has been cancelled
Workflow Sanity / actionlint (push) Has been cancelled
Workflow Sanity / generated-doc-baselines (push) Has been cancelled
CI / runner-admission (push) Has been cancelled
CI / preflight (push) Has been cancelled
CI / security-fast (push) Has been cancelled
CI / pnpm-store-warmup (push) Has been cancelled
CI / build-artifacts (push) Has been cancelled
CI / native-i18n (push) Has been cancelled
CI / ${{ matrix.check_name }} (push) Has been cancelled
CI / ${{ matrix.checkName }} (push) Has been cancelled
CI / checks-node-compat-node22 (push) Has been cancelled
CI / check-bundled-channel-config-metadata (push) Has been cancelled
CI / check-dependencies (push) Has been cancelled
CI / check-guards (push) Has been cancelled
CI / check-lint (push) Has been cancelled
CI / check-prod-types (push) Has been cancelled
CI / check-shrinkwrap (push) Has been cancelled
CI / check-test-types (push) Has been cancelled
CI / check-additional-boundaries-a (push) Has been cancelled
CI / check-additional-boundaries-bcd (push) Has been cancelled
CI / check-additional-extension-bundled (push) Has been cancelled
CI / check-additional-extension-channels (push) Has been cancelled
CI / check-additional-extension-package-boundary (push) Has been cancelled
CI / check-additional-runtime-topology-architecture (push) Has been cancelled
CI / check-session-accessor-boundary (push) Has been cancelled
CI / check-session-transcript-reader-boundary (push) Has been cancelled
CI / check-docs (push) Has been cancelled
CI / skills-python (push) Has been cancelled
CI / macos-swift (push) Has been cancelled
CI / ios-build (push) Has been cancelled
CI / ci-timings-summary (push) Has been cancelled
Native App Locale Refresh / Refresh native fa (push) Has been cancelled
Native App Locale Refresh / Refresh native fr (push) Has been cancelled
Native App Locale Refresh / Refresh native hi (push) Has been cancelled
Native App Locale Refresh / Refresh native id (push) Has been cancelled
Native App Locale Refresh / Refresh native it (push) Has been cancelled
Native App Locale Refresh / Refresh native ja-JP (push) Has been cancelled
Control UI Locale Refresh / plan (push) Has been cancelled
Control UI Locale Refresh / Refresh ${{ matrix.locale }} (push) Has been cancelled
Control UI Locale Refresh / Commit control UI locale refresh (push) Has been cancelled
Live Media Runner Image / Build live media runner image (push) Has been cancelled
Native App Locale Refresh / Refresh native ar (push) Has been cancelled
Native App Locale Refresh / Refresh native de (push) Has been cancelled
Native App Locale Refresh / Refresh native es (push) Has been cancelled
Native App Locale Refresh / Refresh native ko (push) Has been cancelled
Native App Locale Refresh / Refresh native nl (push) Has been cancelled
Native App Locale Refresh / Refresh native pl (push) Has been cancelled
Native App Locale Refresh / Refresh native pt-BR (push) Has been cancelled
Native App Locale Refresh / Refresh native ru (push) Has been cancelled
Native App Locale Refresh / Refresh native sv (push) Has been cancelled
Native App Locale Refresh / Refresh native th (push) Has been cancelled
Native App Locale Refresh / Refresh native tr (push) Has been cancelled
Native App Locale Refresh / Refresh native uk (push) Has been cancelled
Native App Locale Refresh / Refresh native vi (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-CN (push) Has been cancelled
Native App Locale Refresh / Refresh native zh-TW (push) Has been cancelled
Native App Locale Refresh / Commit native locale refresh (push) Has been cancelled
Plugin Init Scaffold Validation / Validate provider scaffold (push) Has been cancelled
Plugin NPM Release / preview_plugins_npm (push) Has been cancelled
Plugin NPM Release / Validate release publish approval (push) Has been cancelled
Plugin NPM Release / preview_plugin_pack (push) Has been cancelled
Plugin NPM Release / publish_plugins_npm (push) Has been cancelled
Sandbox Common Smoke / sandbox-common-smoke (push) Has been cancelled
Website Installer Sync / static (push) Has been cancelled
Website Installer Sync / linux-docker (push) Has been cancelled
Website Installer Sync / macos-installer (push) Has been cancelled
Website Installer Sync / windows-installer (push) Has been cancelled
Website Installer Sync / sync-website (push) Has been cancelled
Adolf is a fork/vendored clone of github.com/openclaw/openclaw (v2026.6.11), free to diverge. Tree copied sans upstream .git; upstream remote added for future syncs. Node pinned to 24 (.nvmrc); engines already require >=22.19. Preserves docs/ARCHITECTURE.md. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LeqyaxJF2nbRXJtae2kNB2
This commit is contained in:
484
extensions/openrouter/stream.ts
Normal file
484
extensions/openrouter/stream.ts
Normal file
@@ -0,0 +1,484 @@
|
||||
// Openrouter plugin module implements stream behavior.
|
||||
import type { StreamFn } from "openclaw/plugin-sdk/agent-core";
|
||||
import {
|
||||
createAssistantMessageEventStream,
|
||||
type AssistantMessage,
|
||||
type AssistantMessageEvent,
|
||||
} from "openclaw/plugin-sdk/llm";
|
||||
import type { ProviderWrapStreamFnContext } from "openclaw/plugin-sdk/plugin-entry";
|
||||
import {
|
||||
assertOkOrThrowHttpError,
|
||||
fetchWithTimeoutGuarded,
|
||||
readProviderJsonResponse,
|
||||
} from "openclaw/plugin-sdk/provider-http";
|
||||
import { OPENROUTER_THINKING_STREAM_HOOKS } from "openclaw/plugin-sdk/provider-stream-family";
|
||||
import { createPayloadPatchStreamWrapper } from "openclaw/plugin-sdk/provider-stream-shared";
|
||||
import { createSubsystemLogger } from "openclaw/plugin-sdk/runtime-env";
|
||||
import { isOpenRouterDeepSeekV4ModelId } from "./models.js";
|
||||
import {
|
||||
isOpenRouterProxyReasoningUnsupportedModel,
|
||||
normalizeOpenRouterBaseUrl,
|
||||
OPENROUTER_BASE_URL,
|
||||
} from "./provider-catalog.js";
|
||||
|
||||
const log = createSubsystemLogger("openrouter-stream");
|
||||
const OPENROUTER_GENERATION_LOOKUP_TIMEOUT_MS = 2_000;
|
||||
|
||||
type OpenRouterGenerationResponse = {
|
||||
data?: {
|
||||
total_cost?: unknown;
|
||||
};
|
||||
};
|
||||
|
||||
function readString(value: unknown): string | undefined {
|
||||
return typeof value === "string" ? value.trim() : undefined;
|
||||
}
|
||||
|
||||
function isOpenRouterAnthropicModelId(modelId: unknown): boolean {
|
||||
const normalized = readString(modelId)?.toLowerCase();
|
||||
return (
|
||||
normalized?.startsWith("anthropic/") === true ||
|
||||
normalized?.startsWith("openrouter/anthropic/") === true
|
||||
);
|
||||
}
|
||||
|
||||
function isVerifiedOpenRouterRoute(model: Parameters<StreamFn>[0]): boolean {
|
||||
const provider = readString(model.provider)?.toLowerCase();
|
||||
const baseUrl = readString(model.baseUrl);
|
||||
if (baseUrl) {
|
||||
return normalizeOpenRouterBaseUrl(baseUrl) === OPENROUTER_BASE_URL;
|
||||
}
|
||||
return provider === "openrouter";
|
||||
}
|
||||
|
||||
function shouldPatchAnthropicOpenRouterPayload(model: Parameters<StreamFn>[0]): boolean {
|
||||
const api = readString(model.api);
|
||||
return (
|
||||
(api === undefined || api === "openai-completions") &&
|
||||
isOpenRouterAnthropicModelId(model.id) &&
|
||||
isVerifiedOpenRouterRoute(model)
|
||||
);
|
||||
}
|
||||
|
||||
function shouldPatchDeepSeekV4OpenRouterPayload(model: Parameters<StreamFn>[0]): boolean {
|
||||
const api = readString(model.api);
|
||||
return (
|
||||
(api === undefined || api === "openai-completions") &&
|
||||
isOpenRouterDeepSeekV4ModelId(model.id) &&
|
||||
isVerifiedOpenRouterRoute(model)
|
||||
);
|
||||
}
|
||||
|
||||
function shouldPatchOpenRouterRoutingPayload(model: Parameters<StreamFn>[0]): boolean {
|
||||
const api = readString(model.api);
|
||||
return (api === undefined || api === "openai-completions") && isVerifiedOpenRouterRoute(model);
|
||||
}
|
||||
|
||||
function resolveOpenRouterGenerationUrl(
|
||||
model: Parameters<StreamFn>[0],
|
||||
responseId: string,
|
||||
): string {
|
||||
const baseUrl = readString(model.baseUrl) || OPENROUTER_BASE_URL;
|
||||
const url = new URL("generation", `${baseUrl.replace(/\/$/, "")}/`);
|
||||
url.searchParams.set("id", responseId);
|
||||
return url.href;
|
||||
}
|
||||
|
||||
function readOpenRouterTotalCost(payload: OpenRouterGenerationResponse): number | undefined {
|
||||
const totalCost = payload.data?.total_cost;
|
||||
if (typeof totalCost !== "number" || !Number.isFinite(totalCost) || totalCost < 0) {
|
||||
return undefined;
|
||||
}
|
||||
return totalCost;
|
||||
}
|
||||
|
||||
function isDoneEvent(
|
||||
event: AssistantMessageEvent,
|
||||
): event is Extract<AssistantMessageEvent, { type: "done" }> {
|
||||
return event.type === "done";
|
||||
}
|
||||
|
||||
async function fetchOpenRouterGenerationTotalCost(params: {
|
||||
apiKey: string;
|
||||
model: Parameters<StreamFn>[0];
|
||||
responseId: string;
|
||||
}): Promise<number | undefined> {
|
||||
const url = resolveOpenRouterGenerationUrl(params.model, params.responseId);
|
||||
const { response, release } = await fetchWithTimeoutGuarded(
|
||||
url,
|
||||
{
|
||||
method: "GET",
|
||||
headers: {
|
||||
Authorization: `Bearer ${params.apiKey}`,
|
||||
"HTTP-Referer": "https://openclaw.ai",
|
||||
"X-OpenRouter-Title": "OpenClaw",
|
||||
},
|
||||
},
|
||||
OPENROUTER_GENERATION_LOOKUP_TIMEOUT_MS,
|
||||
fetch,
|
||||
{ auditContext: "openrouter-generation-cost" },
|
||||
);
|
||||
try {
|
||||
await assertOkOrThrowHttpError(response, "OpenRouter generation metadata request failed");
|
||||
return readOpenRouterTotalCost(
|
||||
await readProviderJsonResponse<OpenRouterGenerationResponse>(
|
||||
response,
|
||||
"openrouter.generation-cost",
|
||||
),
|
||||
);
|
||||
} finally {
|
||||
await release();
|
||||
}
|
||||
}
|
||||
|
||||
async function applyOpenRouterBilledCost(params: {
|
||||
apiKey: string | undefined;
|
||||
message: AssistantMessage;
|
||||
model: Parameters<StreamFn>[0];
|
||||
}): Promise<void> {
|
||||
const apiKey = readString(params.apiKey);
|
||||
const responseId = readString((params.message as { responseId?: unknown }).responseId);
|
||||
if (!apiKey || !responseId || !params.message.usage?.cost) {
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const totalCost = await fetchOpenRouterGenerationTotalCost({
|
||||
apiKey,
|
||||
model: params.model,
|
||||
responseId,
|
||||
});
|
||||
if (totalCost !== undefined) {
|
||||
params.message.usage.cost.total = totalCost;
|
||||
}
|
||||
} catch (error) {
|
||||
log.debug?.(
|
||||
`kept streamed OpenRouter cost estimate because generation metadata lookup failed: ${error instanceof Error ? error.message : String(error)}`,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
function createOpenRouterBilledCostWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
): StreamFn | undefined {
|
||||
if (!baseStreamFn) {
|
||||
return baseStreamFn;
|
||||
}
|
||||
return async (model, context, options) => {
|
||||
const source = await baseStreamFn(model, context, options);
|
||||
if (!isVerifiedOpenRouterRoute(model)) {
|
||||
return source;
|
||||
}
|
||||
const output = createAssistantMessageEventStream();
|
||||
const stream = output as unknown as { push(event: unknown): void; end(): void };
|
||||
void (async () => {
|
||||
try {
|
||||
for await (const event of source as AsyncIterable<AssistantMessageEvent>) {
|
||||
if (isDoneEvent(event)) {
|
||||
await applyOpenRouterBilledCost({
|
||||
apiKey: options?.apiKey,
|
||||
message: event.message,
|
||||
model,
|
||||
});
|
||||
}
|
||||
stream.push(event);
|
||||
}
|
||||
} catch (error) {
|
||||
stream.push({
|
||||
type: "error",
|
||||
reason: "error",
|
||||
error: {
|
||||
role: "assistant",
|
||||
content: [],
|
||||
stopReason: "error",
|
||||
errorMessage: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
});
|
||||
} finally {
|
||||
stream.end();
|
||||
}
|
||||
})();
|
||||
return output as ReturnType<StreamFn>;
|
||||
};
|
||||
}
|
||||
|
||||
function mergeOpenRouterAuthHeaders(options: Parameters<StreamFn>[2]): Parameters<StreamFn>[2] {
|
||||
const apiKey = readString(options?.apiKey);
|
||||
if (!apiKey) {
|
||||
return options;
|
||||
}
|
||||
const headers = new Headers((options as { headers?: HeadersInit } | undefined)?.headers);
|
||||
if (!headers.has("authorization")) {
|
||||
headers.set("Authorization", `Bearer ${apiKey}`);
|
||||
}
|
||||
if (!headers.has("http-referer")) {
|
||||
headers.set("HTTP-Referer", "https://openclaw.ai");
|
||||
}
|
||||
if (!headers.has("x-openrouter-title")) {
|
||||
headers.set("X-OpenRouter-Title", "OpenClaw");
|
||||
}
|
||||
return {
|
||||
...options,
|
||||
headers: Object.fromEntries(headers.entries()),
|
||||
} as Parameters<StreamFn>[2];
|
||||
}
|
||||
|
||||
function createOpenRouterAuthHeaderWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
): StreamFn | undefined {
|
||||
if (!baseStreamFn) {
|
||||
return baseStreamFn;
|
||||
}
|
||||
return (model, context, options) =>
|
||||
baseStreamFn(
|
||||
model,
|
||||
context,
|
||||
isVerifiedOpenRouterRoute(model) ? mergeOpenRouterAuthHeaders(options) : options,
|
||||
);
|
||||
}
|
||||
|
||||
function assistantMessageHasOpenAIToolCalls(message: Record<string, unknown>): boolean {
|
||||
return Array.isArray(message.tool_calls) && message.tool_calls.length > 0;
|
||||
}
|
||||
|
||||
function isAnthropicToolCallContentBlock(value: unknown): boolean {
|
||||
return (
|
||||
value !== null &&
|
||||
typeof value === "object" &&
|
||||
((value as { type?: unknown }).type === "tool_use" ||
|
||||
(value as { type?: unknown }).type === "toolCall")
|
||||
);
|
||||
}
|
||||
|
||||
function assistantMessageHasAnthropicToolUse(message: Record<string, unknown>): boolean {
|
||||
const content = message.content;
|
||||
return Array.isArray(content) && content.some(isAnthropicToolCallContentBlock);
|
||||
}
|
||||
|
||||
function shouldStripOpenRouterTrailingMessage(value: unknown): boolean {
|
||||
if (!value || typeof value !== "object") {
|
||||
return false;
|
||||
}
|
||||
const message = value as Record<string, unknown>;
|
||||
return (
|
||||
message.role === "assistant" &&
|
||||
!assistantMessageHasOpenAIToolCalls(message) &&
|
||||
!assistantMessageHasAnthropicToolUse(message)
|
||||
);
|
||||
}
|
||||
|
||||
function stripTrailingOpenRouterAssistantPrefillMessages(payload: Record<string, unknown>): number {
|
||||
const messages = payload.messages;
|
||||
if (!Array.isArray(messages)) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
let keep = messages.length;
|
||||
while (keep > 0 && shouldStripOpenRouterTrailingMessage(messages[keep - 1])) {
|
||||
keep -= 1;
|
||||
}
|
||||
if (keep === messages.length) {
|
||||
return 0;
|
||||
}
|
||||
const stripped = messages.length - keep;
|
||||
messages.splice(keep);
|
||||
return stripped;
|
||||
}
|
||||
|
||||
function isEnabledReasoningValue(value: unknown): boolean {
|
||||
if (value === undefined || value === null || value === false) {
|
||||
return false;
|
||||
}
|
||||
if (typeof value === "string") {
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return normalized !== "" && normalized !== "off" && normalized !== "none";
|
||||
}
|
||||
if (typeof value === "object" && !Array.isArray(value)) {
|
||||
const effort = (value as Record<string, unknown>).effort;
|
||||
if (typeof effort === "string") {
|
||||
const normalized = effort.trim().toLowerCase();
|
||||
return normalized !== "" && normalized !== "off" && normalized !== "none";
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function isOpenRouterReasoningPayloadEnabled(payload: Record<string, unknown>): boolean {
|
||||
return (
|
||||
isEnabledReasoningValue(payload.reasoning) || isEnabledReasoningValue(payload.reasoning_effort)
|
||||
);
|
||||
}
|
||||
|
||||
function stripOpenRouterDeepSeekV4ReasoningContent(payload: Record<string, unknown>): void {
|
||||
if (!Array.isArray(payload.messages)) {
|
||||
return;
|
||||
}
|
||||
for (const message of payload.messages) {
|
||||
if (!message || typeof message !== "object") {
|
||||
continue;
|
||||
}
|
||||
delete (message as Record<string, unknown>).reasoning_content;
|
||||
}
|
||||
}
|
||||
|
||||
function backfillOpenRouterDeepSeekV4ReasoningContent(payload: Record<string, unknown>): void {
|
||||
if (!Array.isArray(payload.messages)) {
|
||||
return;
|
||||
}
|
||||
for (const message of payload.messages) {
|
||||
if (!message || typeof message !== "object") {
|
||||
continue;
|
||||
}
|
||||
const record = message as Record<string, unknown>;
|
||||
if (
|
||||
record.role === "assistant" &&
|
||||
!assistantMessageHasOpenAIToolCalls(record) &&
|
||||
!("reasoning_content" in record)
|
||||
) {
|
||||
record.reasoning_content = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function injectOpenRouterRouting(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
providerRouting?: Record<string, unknown>,
|
||||
): StreamFn | undefined {
|
||||
if (!providerRouting) {
|
||||
return baseStreamFn;
|
||||
}
|
||||
const routedStreamFn: StreamFn = (model, context, options) =>
|
||||
(
|
||||
baseStreamFn ??
|
||||
((nextModel) => {
|
||||
throw new Error(
|
||||
`OpenRouter routing wrapper requires an underlying streamFn for ${nextModel.id}.`,
|
||||
);
|
||||
})
|
||||
)(
|
||||
{
|
||||
...model,
|
||||
compat: { ...model.compat, openRouterRouting: providerRouting },
|
||||
} as typeof model,
|
||||
context,
|
||||
options,
|
||||
);
|
||||
return createPayloadPatchStreamWrapper(
|
||||
routedStreamFn,
|
||||
({ payload }) => {
|
||||
if (payload.provider === undefined) {
|
||||
payload.provider = providerRouting;
|
||||
}
|
||||
},
|
||||
{
|
||||
shouldPatch: ({ model }) => shouldPatchOpenRouterRoutingPayload(model),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function createOpenRouterAnthropicPrefillWrapper(baseStreamFn: StreamFn | undefined): StreamFn {
|
||||
return createPayloadPatchStreamWrapper(
|
||||
baseStreamFn,
|
||||
({ payload }) => {
|
||||
if (!isOpenRouterReasoningPayloadEnabled(payload)) {
|
||||
return;
|
||||
}
|
||||
const stripped = stripTrailingOpenRouterAssistantPrefillMessages(payload);
|
||||
if (stripped > 0) {
|
||||
log.warn(
|
||||
`removed ${stripped} trailing assistant prefill message${stripped === 1 ? "" : "s"} because OpenRouter-routed Anthropic reasoning requires conversations to end with a user turn`,
|
||||
);
|
||||
}
|
||||
},
|
||||
{
|
||||
shouldPatch: ({ model }) => shouldPatchAnthropicOpenRouterPayload(model),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
function resolveOpenRouterDeepSeekV4ReasoningEffort(
|
||||
thinkingLevel: ProviderWrapStreamFnContext["thinkingLevel"],
|
||||
): "high" | "xhigh" | undefined {
|
||||
if (thinkingLevel === "off") {
|
||||
return undefined;
|
||||
}
|
||||
if (thinkingLevel === "xhigh" || thinkingLevel === "max") {
|
||||
return "xhigh";
|
||||
}
|
||||
return "high";
|
||||
}
|
||||
|
||||
function applyOpenRouterDeepSeekV4ReasoningEffort(
|
||||
payload: Record<string, unknown>,
|
||||
thinkingLevel: ProviderWrapStreamFnContext["thinkingLevel"],
|
||||
): boolean {
|
||||
const effort = resolveOpenRouterDeepSeekV4ReasoningEffort(thinkingLevel);
|
||||
if (!effort) {
|
||||
delete payload.reasoning;
|
||||
return false;
|
||||
}
|
||||
const reasoning =
|
||||
payload.reasoning && typeof payload.reasoning === "object" && !Array.isArray(payload.reasoning)
|
||||
? (payload.reasoning as Record<string, unknown>)
|
||||
: {};
|
||||
reasoning.effort = effort;
|
||||
payload.reasoning = reasoning;
|
||||
return true;
|
||||
}
|
||||
|
||||
function createOpenRouterDeepSeekV4ReplayWrapper(
|
||||
baseStreamFn: StreamFn | undefined,
|
||||
thinkingLevel: ProviderWrapStreamFnContext["thinkingLevel"],
|
||||
): StreamFn {
|
||||
return createPayloadPatchStreamWrapper(
|
||||
baseStreamFn,
|
||||
({ payload }) => {
|
||||
delete payload.thinking;
|
||||
delete payload.reasoning_effort;
|
||||
if (!applyOpenRouterDeepSeekV4ReasoningEffort(payload, thinkingLevel)) {
|
||||
stripOpenRouterDeepSeekV4ReasoningContent(payload);
|
||||
return;
|
||||
}
|
||||
backfillOpenRouterDeepSeekV4ReasoningContent(payload);
|
||||
},
|
||||
{
|
||||
shouldPatch: ({ model }) => shouldPatchDeepSeekV4OpenRouterPayload(model),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
export function wrapOpenRouterProviderStream(
|
||||
ctx: ProviderWrapStreamFnContext,
|
||||
): StreamFn | null | undefined {
|
||||
const providerRouting =
|
||||
ctx.extraParams?.provider != null && typeof ctx.extraParams.provider === "object"
|
||||
? (ctx.extraParams.provider as Record<string, unknown>)
|
||||
: undefined;
|
||||
const routedStreamFn = providerRouting
|
||||
? injectOpenRouterRouting(ctx.streamFn, providerRouting)
|
||||
: ctx.streamFn;
|
||||
const wrapStreamFn = OPENROUTER_THINKING_STREAM_HOOKS.wrapStreamFn ?? undefined;
|
||||
if (!wrapStreamFn) {
|
||||
return createOpenRouterBilledCostWrapper(
|
||||
createOpenRouterAnthropicPrefillWrapper(
|
||||
createOpenRouterAuthHeaderWrapper(
|
||||
createOpenRouterDeepSeekV4ReplayWrapper(routedStreamFn, ctx.thinkingLevel),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
const wrappedStreamFn =
|
||||
wrapStreamFn({
|
||||
...ctx,
|
||||
streamFn: routedStreamFn,
|
||||
thinkingLevel: isOpenRouterProxyReasoningUnsupportedModel(ctx.modelId)
|
||||
? undefined
|
||||
: ctx.thinkingLevel,
|
||||
}) ?? undefined;
|
||||
return createOpenRouterBilledCostWrapper(
|
||||
createOpenRouterAnthropicPrefillWrapper(
|
||||
createOpenRouterAuthHeaderWrapper(
|
||||
createOpenRouterDeepSeekV4ReplayWrapper(wrappedStreamFn, ctx.thinkingLevel),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user