|
| 1 | +type OpenRouterExtraParamsContext = { |
| 2 | + config?: { |
| 3 | + models?: { |
| 4 | + providers?: Record< |
| 5 | + string, |
| 6 | + { |
| 7 | + params?: Record<string, unknown>; |
| 8 | + } |
| 9 | + >; |
| 10 | + }; |
| 11 | + }; |
| 12 | + extraParams: Record<string, unknown>; |
| 13 | + provider: string; |
| 14 | + model?: { |
| 15 | + params?: Record<string, unknown>; |
| 16 | + }; |
| 17 | +}; |
| 18 | + |
| 19 | +const BLOCKED_RECORD_KEYS = new Set(["__proto__", "prototype", "constructor"]); |
| 20 | + |
| 21 | +function sanitizeJsonLikeValue(value: unknown): unknown { |
| 22 | + if (value === undefined) { |
| 23 | + return undefined; |
| 24 | + } |
| 25 | + if (Array.isArray(value)) { |
| 26 | + return value.map(sanitizeJsonLikeValue).filter((entry) => entry !== undefined); |
| 27 | + } |
| 28 | + if (!value || typeof value !== "object") { |
| 29 | + return value; |
| 30 | + } |
| 31 | + return sanitizeRecord(value as Record<string, unknown>); |
| 32 | +} |
| 33 | + |
| 34 | +function sanitizeRecord(value: Record<string, unknown>): Record<string, unknown> { |
| 35 | + return Object.fromEntries( |
| 36 | + Object.entries(value) |
| 37 | + .filter(([key, entry]) => !BLOCKED_RECORD_KEYS.has(key) && entry !== undefined) |
| 38 | + .map(([key, entry]) => [key, sanitizeJsonLikeValue(entry)]), |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function readRecord(value: unknown): Record<string, unknown> | undefined { |
| 43 | + if (!value || typeof value !== "object" || Array.isArray(value)) { |
| 44 | + return undefined; |
| 45 | + } |
| 46 | + const sanitized = sanitizeRecord(value as Record<string, unknown>); |
| 47 | + return Object.keys(sanitized).length > 0 ? sanitized : undefined; |
| 48 | +} |
| 49 | + |
| 50 | +function mergeOpenRouterProviderRouting(params: { |
| 51 | + providerParams?: Record<string, unknown>; |
| 52 | + modelParams?: Record<string, unknown>; |
| 53 | + extraParams: Record<string, unknown>; |
| 54 | +}): Record<string, unknown> | undefined { |
| 55 | + const providerRouting = readRecord(params.providerParams?.provider); |
| 56 | + const modelRouting = readRecord(params.modelParams?.provider); |
| 57 | + const extraRouting = readRecord(params.extraParams.provider); |
| 58 | + const merged = { |
| 59 | + ...(providerRouting ?? {}), |
| 60 | + ...(modelRouting ?? {}), |
| 61 | + ...(extraRouting ?? {}), |
| 62 | + }; |
| 63 | + return Object.keys(merged).length > 0 ? merged : undefined; |
| 64 | +} |
| 65 | + |
| 66 | +export function resolveOpenRouterExtraParamsForTransport( |
| 67 | + ctx: OpenRouterExtraParamsContext, |
| 68 | +): { patch?: Record<string, unknown> } | undefined { |
| 69 | + const providerConfigParams = readRecord(ctx.config?.models?.providers?.[ctx.provider]?.params); |
| 70 | + const modelParams = readRecord(ctx.model?.params); |
| 71 | + const providerRouting = mergeOpenRouterProviderRouting({ |
| 72 | + providerParams: providerConfigParams, |
| 73 | + modelParams, |
| 74 | + extraParams: ctx.extraParams, |
| 75 | + }); |
| 76 | + if (!providerConfigParams && !modelParams && !providerRouting) { |
| 77 | + return undefined; |
| 78 | + } |
| 79 | + return { |
| 80 | + patch: { |
| 81 | + ...(providerConfigParams ?? {}), |
| 82 | + ...(modelParams ?? {}), |
| 83 | + ...ctx.extraParams, |
| 84 | + ...(providerRouting ? { provider: providerRouting } : {}), |
| 85 | + }, |
| 86 | + }; |
| 87 | +} |
0 commit comments