|
| 1 | +/** |
| 2 | + * Shared adapter decorator mechanics. Only the JSON-body / extra-header / |
| 3 | + * registry plumbing is shared; the per-adapter semantics stay in each |
| 4 | + * adapter's rewrite or header factory (codex parallel_tool_calls, DeepSeek |
| 5 | + * reasoning strip and V4 quirks, Bifrost virtual-key sentinel, anthropic |
| 6 | + * session header). Every decorator is a null overhead: when its hook bails, |
| 7 | + * the base request's body and headers pass through byte-identical. |
| 8 | + */ |
| 9 | + |
| 10 | +import type { |
| 11 | + AdapterRegistry, |
| 12 | + BuiltRequest, |
| 13 | + ProviderAdapter, |
| 14 | +} from "@intx/inference"; |
| 15 | +import type { |
| 16 | + ConversationTurn, |
| 17 | + InferenceOptions, |
| 18 | + LastCycleSource, |
| 19 | +} from "@intx/types/runtime"; |
| 20 | + |
| 21 | +export type DecoratorContext = { |
| 22 | + readonly messages: readonly ConversationTurn[]; |
| 23 | + readonly model: string; |
| 24 | + readonly options: InferenceOptions; |
| 25 | +}; |
| 26 | + |
| 27 | +/** Parse the built body, apply the rewrite, and re-stringify only when the |
| 28 | + * rewrite reports a change. An optional gate keeps adapters that only |
| 29 | + * sometimes rewrite on their zero-parse fast path. */ |
| 30 | +export function withJsonBodyRewrite( |
| 31 | + adapter: ProviderAdapter, |
| 32 | + rewrite: ( |
| 33 | + body: Record<string, unknown>, |
| 34 | + context: DecoratorContext, |
| 35 | + ) => boolean, |
| 36 | + gate?: (request: BuiltRequest, context: DecoratorContext) => boolean, |
| 37 | +): ProviderAdapter { |
| 38 | + return { |
| 39 | + ...adapter, |
| 40 | + buildRequest: (messages, model, options) => { |
| 41 | + const built = adapter.buildRequest(messages, model, options); |
| 42 | + const context: DecoratorContext = { messages, model, options }; |
| 43 | + if (gate !== undefined && !gate(built, context)) return built; |
| 44 | + let body: Record<string, unknown>; |
| 45 | + try { |
| 46 | + body = JSON.parse(built.body) as Record<string, unknown>; |
| 47 | + } catch { |
| 48 | + return built; |
| 49 | + } |
| 50 | + if (!rewrite(body, context)) return built; |
| 51 | + return { ...built, body: JSON.stringify(body) }; |
| 52 | + }, |
| 53 | + }; |
| 54 | +} |
| 55 | + |
| 56 | +/** Merge the factory's headers over the built request's own; the spread |
| 57 | + * never removes or rewrites a header the base adapter emitted. */ |
| 58 | +export function withExtraHeaders( |
| 59 | + adapter: ProviderAdapter, |
| 60 | + headers: (context: DecoratorContext) => Record<string, string>, |
| 61 | +): ProviderAdapter { |
| 62 | + return { |
| 63 | + ...adapter, |
| 64 | + buildRequest: (messages, model, options) => { |
| 65 | + const built = adapter.buildRequest(messages, model, options); |
| 66 | + const context: DecoratorContext = { messages, model, options }; |
| 67 | + return { |
| 68 | + ...built, |
| 69 | + headers: { ...built.headers, ...headers(context) }, |
| 70 | + }; |
| 71 | + }, |
| 72 | + }; |
| 73 | +} |
| 74 | + |
| 75 | +/** Wrap an adapter registry so every resolved adapter passes through the |
| 76 | + * decorator; membership is delegated untouched. */ |
| 77 | +export function withDecoratedAdapters( |
| 78 | + adapters: AdapterRegistry, |
| 79 | + decorate: ( |
| 80 | + adapter: ProviderAdapter, |
| 81 | + source: LastCycleSource, |
| 82 | + ) => ProviderAdapter, |
| 83 | +): AdapterRegistry { |
| 84 | + return { |
| 85 | + has: (provider) => adapters.has(provider), |
| 86 | + resolve(source, quirks) { |
| 87 | + return decorate(adapters.resolve(source, quirks), source); |
| 88 | + }, |
| 89 | + }; |
| 90 | +} |
0 commit comments