@omnicross/core 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/NOTICE +57 -0
- package/README.md +15 -0
- package/dist/ApiKeyPoolService-BmMkau07.d.cts +170 -0
- package/dist/ApiKeyPoolService-BmMkau07.d.ts +170 -0
- package/dist/ProviderProxy-f_8ziIhW.d.cts +120 -0
- package/dist/ProviderProxy-vjt8sQQk.d.ts +120 -0
- package/dist/SubscriptionAuthSource-Cr4fVEYY.d.cts +264 -0
- package/dist/SubscriptionAuthSource-D89zmiSS.d.ts +264 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.cjs +218 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.d.cts +68 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.d.ts +68 -0
- package/dist/auth/GeminiCodeAssistProjectResolver.js +189 -0
- package/dist/completion/ApiKeyPoolService.cjs +331 -0
- package/dist/completion/ApiKeyPoolService.d.cts +2 -0
- package/dist/completion/ApiKeyPoolService.d.ts +2 -0
- package/dist/completion/ApiKeyPoolService.js +306 -0
- package/dist/completion.cjs +4027 -0
- package/dist/completion.d.cts +17 -0
- package/dist/completion.d.ts +17 -0
- package/dist/completion.js +3983 -0
- package/dist/index-BTSmc9Sm.d.ts +645 -0
- package/dist/index-DXazdTzZ.d.cts +645 -0
- package/dist/index.cjs +10428 -0
- package/dist/index.d.cts +128 -0
- package/dist/index.d.ts +128 -0
- package/dist/index.js +10339 -0
- package/dist/outbound-api/subscriptionRegistryPort.cjs +38 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.cts +73 -0
- package/dist/outbound-api/subscriptionRegistryPort.d.ts +73 -0
- package/dist/outbound-api/subscriptionRegistryPort.js +12 -0
- package/dist/outbound-api.cjs +5264 -0
- package/dist/outbound-api.d.cts +320 -0
- package/dist/outbound-api.d.ts +320 -0
- package/dist/outbound-api.js +5218 -0
- package/dist/pipeline/SubscriptionAuthSource.cjs +131 -0
- package/dist/pipeline/SubscriptionAuthSource.d.cts +3 -0
- package/dist/pipeline/SubscriptionAuthSource.d.ts +3 -0
- package/dist/pipeline/SubscriptionAuthSource.js +103 -0
- package/dist/pipeline/SubscriptionAuthStrategy.cjs +18 -0
- package/dist/pipeline/SubscriptionAuthStrategy.d.cts +61 -0
- package/dist/pipeline/SubscriptionAuthStrategy.d.ts +61 -0
- package/dist/pipeline/SubscriptionAuthStrategy.js +0 -0
- package/dist/ports/gemini-code-assist-resolver.cjs +38 -0
- package/dist/ports/gemini-code-assist-resolver.d.cts +26 -0
- package/dist/ports/gemini-code-assist-resolver.d.ts +26 -0
- package/dist/ports/gemini-code-assist-resolver.js +12 -0
- package/dist/ports.cjs +18 -0
- package/dist/ports.d.cts +15 -0
- package/dist/ports.d.ts +15 -0
- package/dist/ports.js +0 -0
- package/dist/provider-proxy/ingress/providerProxyShared.cjs +2958 -0
- package/dist/provider-proxy/ingress/providerProxyShared.d.cts +77 -0
- package/dist/provider-proxy/ingress/providerProxyShared.d.ts +77 -0
- package/dist/provider-proxy/ingress/providerProxyShared.js +2925 -0
- package/dist/provider-proxy/matchText.cjs +73 -0
- package/dist/provider-proxy/matchText.d.cts +47 -0
- package/dist/provider-proxy/matchText.d.ts +47 -0
- package/dist/provider-proxy/matchText.js +45 -0
- package/dist/provider-proxy/types.cjs +18 -0
- package/dist/provider-proxy/types.d.cts +12 -0
- package/dist/provider-proxy/types.d.ts +12 -0
- package/dist/provider-proxy/types.js +0 -0
- package/dist/provider-proxy.cjs +4667 -0
- package/dist/provider-proxy.d.cts +69 -0
- package/dist/provider-proxy.d.ts +69 -0
- package/dist/provider-proxy.js +4636 -0
- package/dist/serializeError.cjs +82 -0
- package/dist/serializeError.d.cts +24 -0
- package/dist/serializeError.d.ts +24 -0
- package/dist/serializeError.js +57 -0
- package/dist/sse-parser.cjs +456 -0
- package/dist/sse-parser.d.cts +143 -0
- package/dist/sse-parser.d.ts +143 -0
- package/dist/sse-parser.js +430 -0
- package/dist/transformer/TransformerChainExecutor.cjs +321 -0
- package/dist/transformer/TransformerChainExecutor.d.cts +104 -0
- package/dist/transformer/TransformerChainExecutor.d.ts +104 -0
- package/dist/transformer/TransformerChainExecutor.js +294 -0
- package/dist/transformer/TransformerService.cjs +290 -0
- package/dist/transformer/TransformerService.d.cts +138 -0
- package/dist/transformer/TransformerService.d.ts +138 -0
- package/dist/transformer/TransformerService.js +265 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.cjs +1115 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.d.cts +102 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.d.ts +102 -0
- package/dist/transformer/transformers/GeminiCodeAssistTransformer.js +1085 -0
- package/dist/transformer/transformers/GeminiTransformer.cjs +1013 -0
- package/dist/transformer/transformers/GeminiTransformer.d.cts +70 -0
- package/dist/transformer/transformers/GeminiTransformer.d.ts +70 -0
- package/dist/transformer/transformers/GeminiTransformer.js +986 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.cjs +538 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.d.cts +53 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.d.ts +53 -0
- package/dist/transformer/transformers/OpenAIResponseTransformer.js +513 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.cjs +73 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.d.cts +51 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.d.ts +51 -0
- package/dist/transformer/transformers/OpenCodeGoTransformer.js +48 -0
- package/dist/transformer/types.cjs +18 -0
- package/dist/transformer/types.d.cts +405 -0
- package/dist/transformer/types.d.ts +405 -0
- package/dist/transformer/types.js +0 -0
- package/dist/transformer.cjs +3736 -0
- package/dist/transformer.d.cts +33 -0
- package/dist/transformer.d.ts +33 -0
- package/dist/transformer.js +3712 -0
- package/dist/types-CGGrKqC_.d.cts +142 -0
- package/dist/types-CbCN2NQP.d.ts +142 -0
- package/dist/types-DCzHkhJt.d.ts +467 -0
- package/dist/types-DZIQbgp0.d.cts +467 -0
- package/dist/usage-event-sink-BX7FE1NL.d.cts +59 -0
- package/dist/usage-event-sink-BX7FE1NL.d.ts +59 -0
- package/package.json +62 -0
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { OpenCodeGoTokenConfig, OpenCodeGoScenario, OpenCodeGoModelEntry } from '@omnicross/contracts/subscription-types';
|
|
2
|
+
import { AuthStrategy } from './pipeline/SubscriptionAuthStrategy.cjs';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* AuthSource — unified outbound-authentication strategy for the provider
|
|
6
|
+
* request pipeline.
|
|
7
|
+
*
|
|
8
|
+
* Phase 2 of the `provider-request-pipeline` OpenSpec change (design D3).
|
|
9
|
+
*
|
|
10
|
+
* The three consumer paths (the wire-format proxy handler, the host engine
|
|
11
|
+
* adapter, TransformerHandler) each authenticate differently:
|
|
12
|
+
*
|
|
13
|
+
* - provider-key (+ ApiKeyPool failover) — LLM-config provider rows
|
|
14
|
+
* - subscription `AuthStrategy` (+ 401 refresh + fallback) — code-cli OAuth
|
|
15
|
+
* - OAuth pass-through — SDK forwards its own Bearer
|
|
16
|
+
*
|
|
17
|
+
* `AuthSource` is the single contract that unifies these. THIS PHASE ONLY
|
|
18
|
+
* DEFINES the interface and provides three behavior-preserving WRAPPERS
|
|
19
|
+
* (`LlmConfigProviderAuth`, `SubscriptionAuthSource`, `OAuthPassThroughAuth`)
|
|
20
|
+
* around the existing logic. NO caller is routed through it yet — the core
|
|
21
|
+
* (`executeProviderCall`) is NOT changed to consume `ctx.auth`, and
|
|
22
|
+
* the host handler's branch structure is UNCHANGED. Wiring (and
|
|
23
|
+
* the `onResult` pool-rotation integration) is Phase 3.
|
|
24
|
+
*
|
|
25
|
+
* @module pipeline/AuthSource
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Hints an `AuthSource` may need to vary header formatting per request.
|
|
29
|
+
*
|
|
30
|
+
* Mirrors the subscription-side `AuthApplyHints` (which uses optional
|
|
31
|
+
* `upstreamUrl` / `resolvedModel`) but with REQUIRED fields, since the
|
|
32
|
+
* pipeline always knows both at the point it applies headers. Adapters that
|
|
33
|
+
* wrap the looser subscription shape map these across at the boundary.
|
|
34
|
+
*/
|
|
35
|
+
interface AuthApplyHints {
|
|
36
|
+
/** Resolved upstream URL the request will be sent to. */
|
|
37
|
+
upstreamUrl: string;
|
|
38
|
+
/** Resolved model id for the request. */
|
|
39
|
+
model: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result of {@link AuthSource.onResult}.
|
|
43
|
+
*
|
|
44
|
+
* `rebound` is `true` when the auth source rotated to a different credential
|
|
45
|
+
* (e.g. ApiKeyPool re-bound the session to a new key after a 429). `newKey`
|
|
46
|
+
* carries the freshly-resolved key string when a rotation produced one, so a
|
|
47
|
+
* caller MAY retry the same call once with it. Mirrors the
|
|
48
|
+
* `{ newKey | null }` contract of `ApiKeyPoolService.reportError` lifted into
|
|
49
|
+
* a structured shape.
|
|
50
|
+
*/
|
|
51
|
+
interface AuthResultOutcome {
|
|
52
|
+
/** Whether the credential was rotated/re-bound as a result of this status. */
|
|
53
|
+
rebound: boolean;
|
|
54
|
+
/** The new resolved key when a rotation produced one; omitted otherwise. */
|
|
55
|
+
newKey?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A pluggable outbound-authentication source for one provider request.
|
|
59
|
+
*
|
|
60
|
+
* Implementations OWN exactly the auth concern: which headers carry the
|
|
61
|
+
* credential, how to recover from a 401, where the request should be sent
|
|
62
|
+
* (when the credential dictates the endpoint), and how to react to a final
|
|
63
|
+
* HTTP status (pool rotation / refresh). The pipeline core composes the rest.
|
|
64
|
+
*/
|
|
65
|
+
interface AuthSource {
|
|
66
|
+
/**
|
|
67
|
+
* Inject the authentication headers for this request, mutating `headers`
|
|
68
|
+
* in place. Implementations MAY refresh expiring tokens here.
|
|
69
|
+
*
|
|
70
|
+
* NOTE (Phase 2 boundary, see report): exactly WHAT this owns vs what the
|
|
71
|
+
* ingress assembles (content-type, OpenRouter app headers, proxy auth
|
|
72
|
+
* stripping) is intentionally left to the caller this phase. The wrappers
|
|
73
|
+
* preserve their source's CURRENT header behavior verbatim.
|
|
74
|
+
*/
|
|
75
|
+
applyHeaders(headers: Record<string, string>, hints: AuthApplyHints): Promise<void> | void;
|
|
76
|
+
/**
|
|
77
|
+
* Called when the upstream returns 401. Return `true` to ask the caller to
|
|
78
|
+
* retry once with freshly-applied headers; `false` to surface the 401.
|
|
79
|
+
* Optional — sources without a refresh notion omit it.
|
|
80
|
+
*/
|
|
81
|
+
onUnauthorized?(): Promise<boolean>;
|
|
82
|
+
/**
|
|
83
|
+
* Resolve the upstream URL the request should target, when the credential
|
|
84
|
+
* dictates it (e.g. a subscription profile's per-model endpoint). Returns
|
|
85
|
+
* `undefined` when the source does not override the URL (the ingress keeps
|
|
86
|
+
* its own URL logic). Optional.
|
|
87
|
+
*/
|
|
88
|
+
resolveUpstreamUrl?(model: string): string | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* React to a final HTTP status (pool participation seam — design D5).
|
|
91
|
+
*
|
|
92
|
+
* For the provider-key source this reports the status to the
|
|
93
|
+
* `ApiKeyPoolService` (cooldown / disable / re-bind) and returns whether it
|
|
94
|
+
* rotated plus any new key. THIS PHASE ONLY: `onResult` is implemented and
|
|
95
|
+
* unit-tested in isolation; NO caller invokes it yet. Phase 3 wires it into
|
|
96
|
+
* `fetchWithRetry`. Optional.
|
|
97
|
+
*
|
|
98
|
+
* @param status The final HTTP status, or `null` for non-HTTP failures
|
|
99
|
+
* (e.g. network errors). Non-reportable statuses no-op.
|
|
100
|
+
*/
|
|
101
|
+
onResult?(status: number | null): Promise<AuthResultOutcome>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* SubscriptionAuthSource — `AuthSource` wrapping a subscription `AuthStrategy`.
|
|
106
|
+
*
|
|
107
|
+
* Phase 2 of the `provider-request-pipeline` OpenSpec change (design D3, task 4.3).
|
|
108
|
+
*
|
|
109
|
+
* Re-expresses the subscription auth used by `SubscriptionDispatcher` behind
|
|
110
|
+
* the unified `AuthSource` contract, delegating to the existing
|
|
111
|
+
* `AuthStrategy` so the OAuth refresh / token formatting logic is NOT
|
|
112
|
+
* rewritten:
|
|
113
|
+
*
|
|
114
|
+
* - `applyHeaders` → `stripAuthHeaders(headers)` then
|
|
115
|
+
* `authStrategy.applyHeaders(headers, { upstreamUrl, resolvedModel })`,
|
|
116
|
+
* wrapped in the same best-effort try/catch as
|
|
117
|
+
* `SubscriptionDispatcher.applyHeadersWithRetry` (a thrown
|
|
118
|
+
* `applyHeaders` is logged + swallowed, NOT propagated — preserved
|
|
119
|
+
* verbatim).
|
|
120
|
+
* - `onUnauthorized` → `authStrategy.onUnauthorized()`.
|
|
121
|
+
* - `resolveUpstreamUrl` → `profile.resolveUpstreamUrl?.(model)`.
|
|
122
|
+
*
|
|
123
|
+
* IMPORTANT (Phase 2 scope): this WRAPS the strategy + strip behavior but does
|
|
124
|
+
* NOT re-route `SubscriptionDispatcher` through it. The dispatcher's fragile
|
|
125
|
+
* 401-refresh + fallback loop is left UNCHANGED (task 4.5 deferred). This
|
|
126
|
+
* class is unit-tested in isolation only.
|
|
127
|
+
*
|
|
128
|
+
* @module pipeline/SubscriptionAuthSource
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Lightweight summary derived from the inbound request body — consumed by a
|
|
133
|
+
* profile's `modelMapper` (scenario routing). Structural mirror of
|
|
134
|
+
* `provider-proxy/types.ts`'s `SubscriptionRequestSummary`, declared here so
|
|
135
|
+
* `SubscriptionAuthProfile.modelMapper` is type-identical WITHOUT a circular
|
|
136
|
+
* import back into `provider-proxy/types` (which imports THIS module).
|
|
137
|
+
*/
|
|
138
|
+
interface SubscriptionRequestSummary {
|
|
139
|
+
messageCount: number;
|
|
140
|
+
estimatedInputTokens: number;
|
|
141
|
+
/**
|
|
142
|
+
* OPTIONAL bounded per-message match-text slice — kept structurally identical
|
|
143
|
+
* to the canonical declaration in `provider-proxy/types.ts` (the two are
|
|
144
|
+
* deliberate mirrors, not one shared import). Consumed only by the OpenCodeGo
|
|
145
|
+
* keyword matcher in `@omnicross/subscriptions`; core writes, never reads it.
|
|
146
|
+
*/
|
|
147
|
+
matchText?: string[];
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The subset of a `SubscriptionDispatchProfile` the subscription paths need.
|
|
151
|
+
* Kept structural (not the full profile type) so the wrapper does not pull in
|
|
152
|
+
* the whole registry surface. The full `SubscriptionDispatchProfile` (which IS
|
|
153
|
+
* what gets passed here) satisfies this shape; the optional `mode` + `modelMapper`
|
|
154
|
+
* are read by the built-in `/v1/messages` subscription path (RT2.1) to decide the
|
|
155
|
+
* verbatim-vs-transformer shape and to apply opencodego scenario routing.
|
|
156
|
+
*/
|
|
157
|
+
interface SubscriptionAuthProfile {
|
|
158
|
+
readonly authStrategy: AuthStrategy;
|
|
159
|
+
/** Per-model upstream URL resolver (transformer profiles). Optional for
|
|
160
|
+
* pass-through profiles that hard-code their endpoint upstream. The OPTIONAL
|
|
161
|
+
* 2nd `config` arg (opencodego `baseUrl` override, D1) is additive — existing
|
|
162
|
+
* one-arg callers compile unchanged; mirrors the full
|
|
163
|
+
* `SubscriptionDispatchProfile.resolveUpstreamUrl` so the registry profile
|
|
164
|
+
* stays assignable. */
|
|
165
|
+
readonly resolveUpstreamUrl?: (resolvedModel: string, config?: OpenCodeGoTokenConfig) => string;
|
|
166
|
+
/**
|
|
167
|
+
* Names of provider-level transformers (registered in `TransformerService`)
|
|
168
|
+
* to run on the subscription chain — re-encode Unified → the upstream's wire.
|
|
169
|
+
* The full `SubscriptionDispatchProfile` (which IS what gets passed here)
|
|
170
|
+
* carries these; exposing them on the narrow structural type lets the
|
|
171
|
+
* Responses ingress build the profile's REAL chain (cross-vendor route-to,
|
|
172
|
+
* task #29) instead of hard-coding `['openai-response']`. Absent/empty →
|
|
173
|
+
* the ingress falls back to its endpoint transformer (codex byte-identity).
|
|
174
|
+
*/
|
|
175
|
+
readonly providerTransformerNames?: readonly string[];
|
|
176
|
+
/** Names of model-specific transformers — usually empty. See above. */
|
|
177
|
+
readonly modelTransformerNames?: readonly string[];
|
|
178
|
+
/**
|
|
179
|
+
* OPTIONAL shape-aware provider transformer-name resolver (opencodego zen).
|
|
180
|
+
* Type-identical to `SubscriptionDispatchProfile.resolveProviderTransformerNames`
|
|
181
|
+
* so the registry profile stays assignable. The built-in `/v1/messages`
|
|
182
|
+
* subscription path (Phase 3) consults it via
|
|
183
|
+
* `profile.resolveProviderTransformerNames?.(model, route.subscriptionConfig)`
|
|
184
|
+
* to pick the right zen chain per resolved shape. `config` is `unknown` (core
|
|
185
|
+
* opaque-config discipline — no `@omnicross/subscriptions` import). When ABSENT
|
|
186
|
+
* the path reads the static `providerTransformerNames` exactly as today (codex
|
|
187
|
+
* byte-identity).
|
|
188
|
+
*/
|
|
189
|
+
readonly resolveProviderTransformerNames?: (model: string, config?: unknown) => readonly string[];
|
|
190
|
+
/**
|
|
191
|
+
* Pass-through (claude) vs transformer (codex / opencodego / gemini). The
|
|
192
|
+
* built-in `/v1/messages` subscription path (RT2.1) reads this for its
|
|
193
|
+
* core-local same-format signal (pass-through ⇒ always verbatim relay).
|
|
194
|
+
* Optional on the narrow type — partial profiles built by other route-minting
|
|
195
|
+
* sites may omit it.
|
|
196
|
+
*/
|
|
197
|
+
readonly mode?: 'pass-through' | 'transformer';
|
|
198
|
+
/**
|
|
199
|
+
* Optional model placeholder rewriter — only set for OpenCodeGo. Type-identical
|
|
200
|
+
* to `SubscriptionDispatchProfile.modelMapper` so the registry profile stays
|
|
201
|
+
* assignable. Applied by the built-in `/v1/messages` subscription path BEFORE
|
|
202
|
+
* resolving the upstream URL (so scenario-based shape routing picks the right
|
|
203
|
+
* Anthropic-shape vs OpenAI-shape upstream).
|
|
204
|
+
*/
|
|
205
|
+
readonly modelMapper?: (sdkModel: string, summary: SubscriptionRequestSummary, config: OpenCodeGoTokenConfig | undefined) => {
|
|
206
|
+
resolvedModel: string;
|
|
207
|
+
scenario: OpenCodeGoScenario;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Optional fallback resolver — only set for OpenCodeGo. Type-identical to
|
|
211
|
+
* `SubscriptionDispatchProfile.nextFallback` so the registry profile stays
|
|
212
|
+
* assignable. Read by the built-in `/v1/messages` fallback loop (D6b) to pick
|
|
213
|
+
* the next model after an unrecoverable upstream failure; returns `null` when
|
|
214
|
+
* exhausted (claude / codex / gemini omit it → no fallback attempted).
|
|
215
|
+
*/
|
|
216
|
+
readonly nextFallback?: (scenario: OpenCodeGoScenario, attempted: readonly string[], config: OpenCodeGoTokenConfig | undefined) => OpenCodeGoModelEntry | null;
|
|
217
|
+
/**
|
|
218
|
+
* Optional circuit-breaker admission gate for the PRIMARY (mapped) model (D5
|
|
219
|
+
* primary-gating). Type-identical to `SubscriptionDispatchProfile.allowModel`.
|
|
220
|
+
* Only OpenCodeGo sets it; the core `/v1/messages` loop consults it for the
|
|
221
|
+
* primary before its first attempt and jumps to the first admitting
|
|
222
|
+
* `nextFallback` candidate when the primary's circuit is open. Absent ⇒ the
|
|
223
|
+
* primary is always admitted (claude / codex / gemini — no breaker).
|
|
224
|
+
*/
|
|
225
|
+
readonly allowModel?: (modelId: string) => boolean;
|
|
226
|
+
/**
|
|
227
|
+
* Optional record-outcome callback (D5 record seam). Type-identical to
|
|
228
|
+
* `SubscriptionDispatchProfile.recordModelOutcome`. The core `/v1/messages`
|
|
229
|
+
* loop calls `profile.recordModelOutcome?.(model, ok)` THROUGH this optional
|
|
230
|
+
* field after each attempt, so `@omnicross/core` gains NO import of
|
|
231
|
+
* `@omnicross/subscriptions` (the cross-layer litmus stays 0 — exactly the
|
|
232
|
+
* `nextFallback` precedent). `ok: true` on `2xx`; `ok: false` on
|
|
233
|
+
* thrown/`5xx`/`429`; a non-429 `4xx` is NEUTRAL (the loop does NOT call it).
|
|
234
|
+
* Absent for claude / codex / gemini ⇒ no-op.
|
|
235
|
+
*/
|
|
236
|
+
readonly recordModelOutcome?: (modelId: string, ok: boolean) => void;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Drop auth headers the transformer chain may have set so the `AuthStrategy`
|
|
240
|
+
* is the single source of truth for outbound authentication. Byte-identical
|
|
241
|
+
* to `SubscriptionDispatcher.stripAuthHeaders`.
|
|
242
|
+
*/
|
|
243
|
+
declare function stripAuthHeaders(headers: Record<string, string>): void;
|
|
244
|
+
declare class SubscriptionAuthSource implements AuthSource {
|
|
245
|
+
private readonly profile;
|
|
246
|
+
constructor(profile: SubscriptionAuthProfile);
|
|
247
|
+
/**
|
|
248
|
+
* Strip any transformer-set auth headers, then delegate to the bound
|
|
249
|
+
* strategy. Mirrors `SubscriptionDispatcher`'s
|
|
250
|
+
* `stripAuthHeaders(...)` + `applyHeadersWithRetry(...)` sequence, including
|
|
251
|
+
* the best-effort swallow-and-warn around a throwing `applyHeaders`.
|
|
252
|
+
*
|
|
253
|
+
* The strategy's looser `AuthApplyHints` (optional `upstreamUrl` /
|
|
254
|
+
* `resolvedModel`) is fed from the pipeline's required `{ upstreamUrl,
|
|
255
|
+
* model }` at the boundary.
|
|
256
|
+
*/
|
|
257
|
+
applyHeaders(headers: Record<string, string>, hints: AuthApplyHints): Promise<void>;
|
|
258
|
+
/** Delegate the 401-refresh decision to the bound strategy. */
|
|
259
|
+
onUnauthorized(): Promise<boolean>;
|
|
260
|
+
/** Resolve the upstream URL from the profile, when it provides one. */
|
|
261
|
+
resolveUpstreamUrl(model: string): string | undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export { type AuthSource as A, type SubscriptionAuthProfile as S, SubscriptionAuthSource as a, type SubscriptionRequestSummary as b, stripAuthHeaders as s };
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { OpenCodeGoTokenConfig, OpenCodeGoScenario, OpenCodeGoModelEntry } from '@omnicross/contracts/subscription-types';
|
|
2
|
+
import { AuthStrategy } from './pipeline/SubscriptionAuthStrategy.js';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* AuthSource — unified outbound-authentication strategy for the provider
|
|
6
|
+
* request pipeline.
|
|
7
|
+
*
|
|
8
|
+
* Phase 2 of the `provider-request-pipeline` OpenSpec change (design D3).
|
|
9
|
+
*
|
|
10
|
+
* The three consumer paths (the wire-format proxy handler, the host engine
|
|
11
|
+
* adapter, TransformerHandler) each authenticate differently:
|
|
12
|
+
*
|
|
13
|
+
* - provider-key (+ ApiKeyPool failover) — LLM-config provider rows
|
|
14
|
+
* - subscription `AuthStrategy` (+ 401 refresh + fallback) — code-cli OAuth
|
|
15
|
+
* - OAuth pass-through — SDK forwards its own Bearer
|
|
16
|
+
*
|
|
17
|
+
* `AuthSource` is the single contract that unifies these. THIS PHASE ONLY
|
|
18
|
+
* DEFINES the interface and provides three behavior-preserving WRAPPERS
|
|
19
|
+
* (`LlmConfigProviderAuth`, `SubscriptionAuthSource`, `OAuthPassThroughAuth`)
|
|
20
|
+
* around the existing logic. NO caller is routed through it yet — the core
|
|
21
|
+
* (`executeProviderCall`) is NOT changed to consume `ctx.auth`, and
|
|
22
|
+
* the host handler's branch structure is UNCHANGED. Wiring (and
|
|
23
|
+
* the `onResult` pool-rotation integration) is Phase 3.
|
|
24
|
+
*
|
|
25
|
+
* @module pipeline/AuthSource
|
|
26
|
+
*/
|
|
27
|
+
/**
|
|
28
|
+
* Hints an `AuthSource` may need to vary header formatting per request.
|
|
29
|
+
*
|
|
30
|
+
* Mirrors the subscription-side `AuthApplyHints` (which uses optional
|
|
31
|
+
* `upstreamUrl` / `resolvedModel`) but with REQUIRED fields, since the
|
|
32
|
+
* pipeline always knows both at the point it applies headers. Adapters that
|
|
33
|
+
* wrap the looser subscription shape map these across at the boundary.
|
|
34
|
+
*/
|
|
35
|
+
interface AuthApplyHints {
|
|
36
|
+
/** Resolved upstream URL the request will be sent to. */
|
|
37
|
+
upstreamUrl: string;
|
|
38
|
+
/** Resolved model id for the request. */
|
|
39
|
+
model: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Result of {@link AuthSource.onResult}.
|
|
43
|
+
*
|
|
44
|
+
* `rebound` is `true` when the auth source rotated to a different credential
|
|
45
|
+
* (e.g. ApiKeyPool re-bound the session to a new key after a 429). `newKey`
|
|
46
|
+
* carries the freshly-resolved key string when a rotation produced one, so a
|
|
47
|
+
* caller MAY retry the same call once with it. Mirrors the
|
|
48
|
+
* `{ newKey | null }` contract of `ApiKeyPoolService.reportError` lifted into
|
|
49
|
+
* a structured shape.
|
|
50
|
+
*/
|
|
51
|
+
interface AuthResultOutcome {
|
|
52
|
+
/** Whether the credential was rotated/re-bound as a result of this status. */
|
|
53
|
+
rebound: boolean;
|
|
54
|
+
/** The new resolved key when a rotation produced one; omitted otherwise. */
|
|
55
|
+
newKey?: string;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* A pluggable outbound-authentication source for one provider request.
|
|
59
|
+
*
|
|
60
|
+
* Implementations OWN exactly the auth concern: which headers carry the
|
|
61
|
+
* credential, how to recover from a 401, where the request should be sent
|
|
62
|
+
* (when the credential dictates the endpoint), and how to react to a final
|
|
63
|
+
* HTTP status (pool rotation / refresh). The pipeline core composes the rest.
|
|
64
|
+
*/
|
|
65
|
+
interface AuthSource {
|
|
66
|
+
/**
|
|
67
|
+
* Inject the authentication headers for this request, mutating `headers`
|
|
68
|
+
* in place. Implementations MAY refresh expiring tokens here.
|
|
69
|
+
*
|
|
70
|
+
* NOTE (Phase 2 boundary, see report): exactly WHAT this owns vs what the
|
|
71
|
+
* ingress assembles (content-type, OpenRouter app headers, proxy auth
|
|
72
|
+
* stripping) is intentionally left to the caller this phase. The wrappers
|
|
73
|
+
* preserve their source's CURRENT header behavior verbatim.
|
|
74
|
+
*/
|
|
75
|
+
applyHeaders(headers: Record<string, string>, hints: AuthApplyHints): Promise<void> | void;
|
|
76
|
+
/**
|
|
77
|
+
* Called when the upstream returns 401. Return `true` to ask the caller to
|
|
78
|
+
* retry once with freshly-applied headers; `false` to surface the 401.
|
|
79
|
+
* Optional — sources without a refresh notion omit it.
|
|
80
|
+
*/
|
|
81
|
+
onUnauthorized?(): Promise<boolean>;
|
|
82
|
+
/**
|
|
83
|
+
* Resolve the upstream URL the request should target, when the credential
|
|
84
|
+
* dictates it (e.g. a subscription profile's per-model endpoint). Returns
|
|
85
|
+
* `undefined` when the source does not override the URL (the ingress keeps
|
|
86
|
+
* its own URL logic). Optional.
|
|
87
|
+
*/
|
|
88
|
+
resolveUpstreamUrl?(model: string): string | undefined;
|
|
89
|
+
/**
|
|
90
|
+
* React to a final HTTP status (pool participation seam — design D5).
|
|
91
|
+
*
|
|
92
|
+
* For the provider-key source this reports the status to the
|
|
93
|
+
* `ApiKeyPoolService` (cooldown / disable / re-bind) and returns whether it
|
|
94
|
+
* rotated plus any new key. THIS PHASE ONLY: `onResult` is implemented and
|
|
95
|
+
* unit-tested in isolation; NO caller invokes it yet. Phase 3 wires it into
|
|
96
|
+
* `fetchWithRetry`. Optional.
|
|
97
|
+
*
|
|
98
|
+
* @param status The final HTTP status, or `null` for non-HTTP failures
|
|
99
|
+
* (e.g. network errors). Non-reportable statuses no-op.
|
|
100
|
+
*/
|
|
101
|
+
onResult?(status: number | null): Promise<AuthResultOutcome>;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* SubscriptionAuthSource — `AuthSource` wrapping a subscription `AuthStrategy`.
|
|
106
|
+
*
|
|
107
|
+
* Phase 2 of the `provider-request-pipeline` OpenSpec change (design D3, task 4.3).
|
|
108
|
+
*
|
|
109
|
+
* Re-expresses the subscription auth used by `SubscriptionDispatcher` behind
|
|
110
|
+
* the unified `AuthSource` contract, delegating to the existing
|
|
111
|
+
* `AuthStrategy` so the OAuth refresh / token formatting logic is NOT
|
|
112
|
+
* rewritten:
|
|
113
|
+
*
|
|
114
|
+
* - `applyHeaders` → `stripAuthHeaders(headers)` then
|
|
115
|
+
* `authStrategy.applyHeaders(headers, { upstreamUrl, resolvedModel })`,
|
|
116
|
+
* wrapped in the same best-effort try/catch as
|
|
117
|
+
* `SubscriptionDispatcher.applyHeadersWithRetry` (a thrown
|
|
118
|
+
* `applyHeaders` is logged + swallowed, NOT propagated — preserved
|
|
119
|
+
* verbatim).
|
|
120
|
+
* - `onUnauthorized` → `authStrategy.onUnauthorized()`.
|
|
121
|
+
* - `resolveUpstreamUrl` → `profile.resolveUpstreamUrl?.(model)`.
|
|
122
|
+
*
|
|
123
|
+
* IMPORTANT (Phase 2 scope): this WRAPS the strategy + strip behavior but does
|
|
124
|
+
* NOT re-route `SubscriptionDispatcher` through it. The dispatcher's fragile
|
|
125
|
+
* 401-refresh + fallback loop is left UNCHANGED (task 4.5 deferred). This
|
|
126
|
+
* class is unit-tested in isolation only.
|
|
127
|
+
*
|
|
128
|
+
* @module pipeline/SubscriptionAuthSource
|
|
129
|
+
*/
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Lightweight summary derived from the inbound request body — consumed by a
|
|
133
|
+
* profile's `modelMapper` (scenario routing). Structural mirror of
|
|
134
|
+
* `provider-proxy/types.ts`'s `SubscriptionRequestSummary`, declared here so
|
|
135
|
+
* `SubscriptionAuthProfile.modelMapper` is type-identical WITHOUT a circular
|
|
136
|
+
* import back into `provider-proxy/types` (which imports THIS module).
|
|
137
|
+
*/
|
|
138
|
+
interface SubscriptionRequestSummary {
|
|
139
|
+
messageCount: number;
|
|
140
|
+
estimatedInputTokens: number;
|
|
141
|
+
/**
|
|
142
|
+
* OPTIONAL bounded per-message match-text slice — kept structurally identical
|
|
143
|
+
* to the canonical declaration in `provider-proxy/types.ts` (the two are
|
|
144
|
+
* deliberate mirrors, not one shared import). Consumed only by the OpenCodeGo
|
|
145
|
+
* keyword matcher in `@omnicross/subscriptions`; core writes, never reads it.
|
|
146
|
+
*/
|
|
147
|
+
matchText?: string[];
|
|
148
|
+
}
|
|
149
|
+
/**
|
|
150
|
+
* The subset of a `SubscriptionDispatchProfile` the subscription paths need.
|
|
151
|
+
* Kept structural (not the full profile type) so the wrapper does not pull in
|
|
152
|
+
* the whole registry surface. The full `SubscriptionDispatchProfile` (which IS
|
|
153
|
+
* what gets passed here) satisfies this shape; the optional `mode` + `modelMapper`
|
|
154
|
+
* are read by the built-in `/v1/messages` subscription path (RT2.1) to decide the
|
|
155
|
+
* verbatim-vs-transformer shape and to apply opencodego scenario routing.
|
|
156
|
+
*/
|
|
157
|
+
interface SubscriptionAuthProfile {
|
|
158
|
+
readonly authStrategy: AuthStrategy;
|
|
159
|
+
/** Per-model upstream URL resolver (transformer profiles). Optional for
|
|
160
|
+
* pass-through profiles that hard-code their endpoint upstream. The OPTIONAL
|
|
161
|
+
* 2nd `config` arg (opencodego `baseUrl` override, D1) is additive — existing
|
|
162
|
+
* one-arg callers compile unchanged; mirrors the full
|
|
163
|
+
* `SubscriptionDispatchProfile.resolveUpstreamUrl` so the registry profile
|
|
164
|
+
* stays assignable. */
|
|
165
|
+
readonly resolveUpstreamUrl?: (resolvedModel: string, config?: OpenCodeGoTokenConfig) => string;
|
|
166
|
+
/**
|
|
167
|
+
* Names of provider-level transformers (registered in `TransformerService`)
|
|
168
|
+
* to run on the subscription chain — re-encode Unified → the upstream's wire.
|
|
169
|
+
* The full `SubscriptionDispatchProfile` (which IS what gets passed here)
|
|
170
|
+
* carries these; exposing them on the narrow structural type lets the
|
|
171
|
+
* Responses ingress build the profile's REAL chain (cross-vendor route-to,
|
|
172
|
+
* task #29) instead of hard-coding `['openai-response']`. Absent/empty →
|
|
173
|
+
* the ingress falls back to its endpoint transformer (codex byte-identity).
|
|
174
|
+
*/
|
|
175
|
+
readonly providerTransformerNames?: readonly string[];
|
|
176
|
+
/** Names of model-specific transformers — usually empty. See above. */
|
|
177
|
+
readonly modelTransformerNames?: readonly string[];
|
|
178
|
+
/**
|
|
179
|
+
* OPTIONAL shape-aware provider transformer-name resolver (opencodego zen).
|
|
180
|
+
* Type-identical to `SubscriptionDispatchProfile.resolveProviderTransformerNames`
|
|
181
|
+
* so the registry profile stays assignable. The built-in `/v1/messages`
|
|
182
|
+
* subscription path (Phase 3) consults it via
|
|
183
|
+
* `profile.resolveProviderTransformerNames?.(model, route.subscriptionConfig)`
|
|
184
|
+
* to pick the right zen chain per resolved shape. `config` is `unknown` (core
|
|
185
|
+
* opaque-config discipline — no `@omnicross/subscriptions` import). When ABSENT
|
|
186
|
+
* the path reads the static `providerTransformerNames` exactly as today (codex
|
|
187
|
+
* byte-identity).
|
|
188
|
+
*/
|
|
189
|
+
readonly resolveProviderTransformerNames?: (model: string, config?: unknown) => readonly string[];
|
|
190
|
+
/**
|
|
191
|
+
* Pass-through (claude) vs transformer (codex / opencodego / gemini). The
|
|
192
|
+
* built-in `/v1/messages` subscription path (RT2.1) reads this for its
|
|
193
|
+
* core-local same-format signal (pass-through ⇒ always verbatim relay).
|
|
194
|
+
* Optional on the narrow type — partial profiles built by other route-minting
|
|
195
|
+
* sites may omit it.
|
|
196
|
+
*/
|
|
197
|
+
readonly mode?: 'pass-through' | 'transformer';
|
|
198
|
+
/**
|
|
199
|
+
* Optional model placeholder rewriter — only set for OpenCodeGo. Type-identical
|
|
200
|
+
* to `SubscriptionDispatchProfile.modelMapper` so the registry profile stays
|
|
201
|
+
* assignable. Applied by the built-in `/v1/messages` subscription path BEFORE
|
|
202
|
+
* resolving the upstream URL (so scenario-based shape routing picks the right
|
|
203
|
+
* Anthropic-shape vs OpenAI-shape upstream).
|
|
204
|
+
*/
|
|
205
|
+
readonly modelMapper?: (sdkModel: string, summary: SubscriptionRequestSummary, config: OpenCodeGoTokenConfig | undefined) => {
|
|
206
|
+
resolvedModel: string;
|
|
207
|
+
scenario: OpenCodeGoScenario;
|
|
208
|
+
};
|
|
209
|
+
/**
|
|
210
|
+
* Optional fallback resolver — only set for OpenCodeGo. Type-identical to
|
|
211
|
+
* `SubscriptionDispatchProfile.nextFallback` so the registry profile stays
|
|
212
|
+
* assignable. Read by the built-in `/v1/messages` fallback loop (D6b) to pick
|
|
213
|
+
* the next model after an unrecoverable upstream failure; returns `null` when
|
|
214
|
+
* exhausted (claude / codex / gemini omit it → no fallback attempted).
|
|
215
|
+
*/
|
|
216
|
+
readonly nextFallback?: (scenario: OpenCodeGoScenario, attempted: readonly string[], config: OpenCodeGoTokenConfig | undefined) => OpenCodeGoModelEntry | null;
|
|
217
|
+
/**
|
|
218
|
+
* Optional circuit-breaker admission gate for the PRIMARY (mapped) model (D5
|
|
219
|
+
* primary-gating). Type-identical to `SubscriptionDispatchProfile.allowModel`.
|
|
220
|
+
* Only OpenCodeGo sets it; the core `/v1/messages` loop consults it for the
|
|
221
|
+
* primary before its first attempt and jumps to the first admitting
|
|
222
|
+
* `nextFallback` candidate when the primary's circuit is open. Absent ⇒ the
|
|
223
|
+
* primary is always admitted (claude / codex / gemini — no breaker).
|
|
224
|
+
*/
|
|
225
|
+
readonly allowModel?: (modelId: string) => boolean;
|
|
226
|
+
/**
|
|
227
|
+
* Optional record-outcome callback (D5 record seam). Type-identical to
|
|
228
|
+
* `SubscriptionDispatchProfile.recordModelOutcome`. The core `/v1/messages`
|
|
229
|
+
* loop calls `profile.recordModelOutcome?.(model, ok)` THROUGH this optional
|
|
230
|
+
* field after each attempt, so `@omnicross/core` gains NO import of
|
|
231
|
+
* `@omnicross/subscriptions` (the cross-layer litmus stays 0 — exactly the
|
|
232
|
+
* `nextFallback` precedent). `ok: true` on `2xx`; `ok: false` on
|
|
233
|
+
* thrown/`5xx`/`429`; a non-429 `4xx` is NEUTRAL (the loop does NOT call it).
|
|
234
|
+
* Absent for claude / codex / gemini ⇒ no-op.
|
|
235
|
+
*/
|
|
236
|
+
readonly recordModelOutcome?: (modelId: string, ok: boolean) => void;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Drop auth headers the transformer chain may have set so the `AuthStrategy`
|
|
240
|
+
* is the single source of truth for outbound authentication. Byte-identical
|
|
241
|
+
* to `SubscriptionDispatcher.stripAuthHeaders`.
|
|
242
|
+
*/
|
|
243
|
+
declare function stripAuthHeaders(headers: Record<string, string>): void;
|
|
244
|
+
declare class SubscriptionAuthSource implements AuthSource {
|
|
245
|
+
private readonly profile;
|
|
246
|
+
constructor(profile: SubscriptionAuthProfile);
|
|
247
|
+
/**
|
|
248
|
+
* Strip any transformer-set auth headers, then delegate to the bound
|
|
249
|
+
* strategy. Mirrors `SubscriptionDispatcher`'s
|
|
250
|
+
* `stripAuthHeaders(...)` + `applyHeadersWithRetry(...)` sequence, including
|
|
251
|
+
* the best-effort swallow-and-warn around a throwing `applyHeaders`.
|
|
252
|
+
*
|
|
253
|
+
* The strategy's looser `AuthApplyHints` (optional `upstreamUrl` /
|
|
254
|
+
* `resolvedModel`) is fed from the pipeline's required `{ upstreamUrl,
|
|
255
|
+
* model }` at the boundary.
|
|
256
|
+
*/
|
|
257
|
+
applyHeaders(headers: Record<string, string>, hints: AuthApplyHints): Promise<void>;
|
|
258
|
+
/** Delegate the 401-refresh decision to the bound strategy. */
|
|
259
|
+
onUnauthorized(): Promise<boolean>;
|
|
260
|
+
/** Resolve the upstream URL from the profile, when it provides one. */
|
|
261
|
+
resolveUpstreamUrl(model: string): string | undefined;
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
export { type AuthSource as A, type SubscriptionAuthProfile as S, SubscriptionAuthSource as a, type SubscriptionRequestSummary as b, stripAuthHeaders as s };
|