@mupt-ai/dari-gateway 0.0.0-stage → 0.2.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.
Files changed (133) hide show
  1. package/LICENSE +202 -0
  2. package/README.md +135 -2
  3. package/dari-gateway.example.yaml +28 -0
  4. package/package.json +42 -4
  5. package/src/bun.ts +4 -0
  6. package/src/context/eval_catalog.ts +11 -0
  7. package/src/context/interaction_fingerprint.ts +691 -0
  8. package/src/context/interaction_fingerprint_format.ts +189 -0
  9. package/src/context/interaction_lease_depth.ts +150 -0
  10. package/src/context/lease_history.ts +200 -0
  11. package/src/context/pricing.ts +30 -0
  12. package/src/context/provider_prompt_accounting.ts +241 -0
  13. package/src/context/routing_context.ts +337 -0
  14. package/src/http/auth.ts +43 -0
  15. package/src/http/errors.ts +114 -0
  16. package/src/http/handler.ts +110 -0
  17. package/src/http/http2_server.ts +122 -0
  18. package/src/http/serve.ts +119 -0
  19. package/src/index.ts +22 -0
  20. package/src/ingress/anthropic_messages.ts +162 -0
  21. package/src/ingress/anthropic_models.ts +28 -0
  22. package/src/ingress/chat_completions.ts +49 -0
  23. package/src/ingress/claude_code/request_context.ts +36 -0
  24. package/src/ingress/codex.ts +81 -0
  25. package/src/ingress/codex_models.ts +113 -0
  26. package/src/ingress/json.ts +113 -0
  27. package/src/ingress/models.ts +55 -0
  28. package/src/interaction/capabilities.ts +30 -0
  29. package/src/interaction/image.ts +12 -0
  30. package/src/interaction/portable_reasoning_state.ts +25 -0
  31. package/src/interaction/provider_continuation_state.ts +36 -0
  32. package/src/interaction/tool_identity.ts +65 -0
  33. package/src/interaction/types.ts +282 -0
  34. package/src/models/context_limits.ts +8 -0
  35. package/src/models/execution_provider.ts +22 -0
  36. package/src/models/gateways.ts +73 -0
  37. package/src/models/ids.ts +26 -0
  38. package/src/models/provider_defaults.ts +47 -0
  39. package/src/models/runtime_overrides.ts +221 -0
  40. package/src/observability/errors.ts +25 -0
  41. package/src/platform/platform.ts +204 -0
  42. package/src/platform/routing_contract.ts +6 -0
  43. package/src/protocol/input/anthropic_messages.ts +531 -0
  44. package/src/protocol/input/chat_completions.ts +255 -0
  45. package/src/protocol/input/chat_completions_validation.ts +293 -0
  46. package/src/protocol/input/openai_responses.ts +749 -0
  47. package/src/protocol/input/router_model.ts +53 -0
  48. package/src/protocol/output/anthropic_messages.ts +32 -0
  49. package/src/protocol/output/anthropic_messages_output.ts +129 -0
  50. package/src/protocol/output/anthropic_messages_stream.ts +281 -0
  51. package/src/protocol/output/chat_completions.ts +451 -0
  52. package/src/protocol/output/chat_reasoning_detail.ts +19 -0
  53. package/src/protocol/output/delivered_stream_snapshot.ts +31 -0
  54. package/src/protocol/output/openai_responses.ts +30 -0
  55. package/src/protocol/output/openai_responses_output.ts +159 -0
  56. package/src/protocol/output/openai_responses_stream.ts +631 -0
  57. package/src/protocol/output/sse_stream.ts +102 -0
  58. package/src/providers/anthropic_safeguards.ts +115 -0
  59. package/src/providers/aws_assume_role.ts +147 -0
  60. package/src/providers/cancel_estimate.ts +140 -0
  61. package/src/providers/execution.ts +800 -0
  62. package/src/providers/model_capabilities.ts +50 -0
  63. package/src/providers/pi.ts +499 -0
  64. package/src/providers/pi_context.ts +466 -0
  65. package/src/providers/pi_options.ts +416 -0
  66. package/src/providers/pi_output.ts +500 -0
  67. package/src/providers/provider_attempt_runner.ts +153 -0
  68. package/src/providers/provider_call.ts +433 -0
  69. package/src/providers/provider_reported_cost.ts +138 -0
  70. package/src/providers/reasoning.ts +59 -0
  71. package/src/providers/response_capture.ts +65 -0
  72. package/src/providers/stream_finalization.ts +264 -0
  73. package/src/providers/subscription_limits.ts +208 -0
  74. package/src/providers/subscription_usage.ts +99 -0
  75. package/src/runtime/claude_code/auxiliary_routing.ts +80 -0
  76. package/src/runtime/customer_disconnect.ts +66 -0
  77. package/src/runtime/explicit_model.ts +86 -0
  78. package/src/runtime/harness_identity/conversation.ts +87 -0
  79. package/src/runtime/harness_identity/extract.ts +323 -0
  80. package/src/runtime/harness_identity/index.ts +14 -0
  81. package/src/runtime/prepare_routed_completion.ts +224 -0
  82. package/src/runtime/provider_stream_lifecycle.ts +105 -0
  83. package/src/runtime/provider_stream_outcome.ts +83 -0
  84. package/src/runtime/request_tags.ts +34 -0
  85. package/src/runtime/routed_completion.ts +102 -0
  86. package/src/runtime/routed_protocol.ts +33 -0
  87. package/src/runtime/routed_runtime.ts +722 -0
  88. package/src/runtime/routing_lease.ts +273 -0
  89. package/src/runtime/runtime_reporter.ts +115 -0
  90. package/src/self_host/config.ts +173 -0
  91. package/src/self_host/main.ts +104 -0
  92. package/src/self_host/platform.ts +63 -0
  93. package/src/self_host/prefix_store.ts +315 -0
  94. package/src/self_host/resolve.ts +240 -0
  95. package/src/self_host/subscriptions/access.ts +43 -0
  96. package/src/self_host/subscriptions/http.ts +204 -0
  97. package/src/self_host/subscriptions/index.ts +43 -0
  98. package/src/self_host/subscriptions/oauth.ts +395 -0
  99. package/src/self_host/subscriptions/store.ts +339 -0
  100. package/src/strategies/auto_routing_selector.ts +72 -0
  101. package/src/strategies/eval_relevance.ts +57 -0
  102. package/src/strategies/fallback_decision.ts +30 -0
  103. package/src/strategies/priority_selector.ts +40 -0
  104. package/src/strategies/route_decision.ts +106 -0
  105. package/src/strategies/routing.ts +455 -0
  106. package/src/strategies/selector_projection.ts +207 -0
  107. package/src/telemetry/background.ts +40 -0
  108. package/src/telemetry/benchmark_replay.ts +160 -0
  109. package/src/telemetry/billing_finalization.ts +167 -0
  110. package/src/telemetry/ingest_values.ts +15 -0
  111. package/src/telemetry/model_cost.ts +14 -0
  112. package/src/telemetry/prefix_state.ts +183 -0
  113. package/src/telemetry/report.ts +832 -0
  114. package/src/telemetry/requests/interaction_observability.ts +258 -0
  115. package/src/telemetry/requests/retained_interaction_request.ts +20 -0
  116. package/src/telemetry/scrub.ts +38 -0
  117. package/src/telemetry/skills/claude/catalog.ts +119 -0
  118. package/src/telemetry/skills/claude/file_reads.ts +153 -0
  119. package/src/telemetry/skills/claude/observations.ts +450 -0
  120. package/src/telemetry/skills/codex/catalog.ts +171 -0
  121. package/src/telemetry/skills/codex/exec_file_reads.ts +55 -0
  122. package/src/telemetry/skills/codex/explicit.ts +116 -0
  123. package/src/telemetry/skills/codex/file_reads.ts +51 -0
  124. package/src/telemetry/skills/codex/identity.ts +21 -0
  125. package/src/telemetry/skills/observations.ts +94 -0
  126. package/src/telemetry/skills/paths.ts +82 -0
  127. package/src/telemetry/skills/pi.ts +276 -0
  128. package/src/telemetry/skills/shell_file_reads.ts +267 -0
  129. package/src/telemetry/skills/shell_words.ts +202 -0
  130. package/src/telemetry/tools/input.ts +16 -0
  131. package/src/telemetry/tools/observations.ts +349 -0
  132. package/src/telemetry/usage.ts +173 -0
  133. package/src/types.ts +150 -0
@@ -0,0 +1,264 @@
1
+ import type {
2
+ ModelPricing,
3
+ RoutingDecision,
4
+ } from "@mupt-ai/dari-router/policy-engine";
5
+
6
+ import type { InteractionRequest, ProviderIdentity } from "../interaction/types";
7
+ import {
8
+ captureOperationalError,
9
+ type OperationalErrorCode,
10
+ } from "../observability/errors";
11
+ import type { ProviderStreamOutcome } from "../runtime/provider_stream_outcome";
12
+ import type { RoutedProtocol } from "../runtime/routed_protocol";
13
+ import {
14
+ reportedExecution,
15
+ type FrozenProviderTerminal,
16
+ type ProviderAttemptRecord,
17
+ type ReportableProviderOutcome,
18
+ type RoutedRuntimeReporter,
19
+ } from "../runtime/runtime_reporter";
20
+ import type { ProviderKeySource } from "../types";
21
+ import {
22
+ reportRoutingUsage,
23
+ type RoutingUsageContext,
24
+ } from "../telemetry/usage";
25
+ import { customerCanceledProviderMessage } from "../runtime/customer_disconnect";
26
+ import {
27
+ estimatedCancelUsage,
28
+ partialInteractionOutput,
29
+ type DeliveredOutput,
30
+ type PromptTokenEstimate,
31
+ } from "./cancel_estimate";
32
+
33
+ // Structurally a subset of ProviderExecution. The narrowing is deliberate: it
34
+ // keeps apiKey and auth out of the finalizer.
35
+ type StreamExecutionFacts = {
36
+ provider: string;
37
+ model: string;
38
+ providerKeySource: ProviderKeySource;
39
+ conversationId: string;
40
+ usageContext: RoutingUsageContext | undefined;
41
+ pricing: ModelPricing | null;
42
+ promptEstimate: PromptTokenEstimate | undefined;
43
+ };
44
+
45
+ type CreateStreamFinalizerArgs = {
46
+ requestId: string;
47
+ interaction: InteractionRequest;
48
+ decision: RoutingDecision;
49
+ execution: StreamExecutionFacts;
50
+ reporter: RoutedRuntimeReporter;
51
+ protocol: RoutedProtocol;
52
+ providerLatencyMs: () => number;
53
+ role: "primary" | "fallback";
54
+ attempt: number;
55
+ reportBarrier?: Promise<unknown>;
56
+ delivered: () => DeliveredOutput;
57
+ source: ProviderIdentity;
58
+ startedAtMs: number;
59
+ cancelBarrierCapMs?: number;
60
+ };
61
+
62
+ /**
63
+ * A client cancel runs its finalization in the background, where a slow
64
+ * selector would only delay the record and, at shutdown, eat the drain budget.
65
+ * The record still gets the selector's run when it arrives within the cap;
66
+ * otherwise it is written without it. The response-owned path is not capped.
67
+ */
68
+ export const CANCEL_REPORT_BARRIER_CAP_MS = 2_000;
69
+
70
+ export type StreamFinalizer = {
71
+ beforeResponseClose(outcome: ProviderStreamOutcome): Promise<void>;
72
+ afterResponseDetached(outcome: ProviderStreamOutcome): Promise<void>;
73
+ };
74
+
75
+ export function createStreamFinalizer(
76
+ args: CreateStreamFinalizerArgs,
77
+ ): StreamFinalizer {
78
+ let frozen: FrozenStreamTerminal | undefined;
79
+ let responseOwnedCompleted = false;
80
+
81
+ async function finalize(
82
+ outcome: ProviderStreamOutcome,
83
+ path: "response_owned" | "detached",
84
+ ): Promise<void> {
85
+ frozen ??= freezeStreamTerminal(args, outcome, path);
86
+ // Normal close calls onFinal after the response-owned barrier has already
87
+ // succeeded. A cancellation racing that barrier reaches this branch first
88
+ // and settles the same immutable payload.
89
+ if (path === "detached" && responseOwnedCompleted) return;
90
+ await frozen.reporting;
91
+ // The response terminator waits for the fast prefix-state write. A client
92
+ // cancellation has nothing left to hold, so its onFinal path skips this.
93
+ if (path === "response_owned") {
94
+ await args.reporter.waitForPrefixEntryWrite();
95
+ responseOwnedCompleted = true;
96
+ }
97
+ }
98
+
99
+ return {
100
+ beforeResponseClose: (outcome) => finalize(outcome, "response_owned"),
101
+ afterResponseDetached: (outcome) => finalize(outcome, "detached"),
102
+ };
103
+ }
104
+
105
+ type FrozenStreamTerminal = {
106
+ terminal: FrozenProviderTerminal;
107
+ reporting: Promise<void>;
108
+ };
109
+
110
+ function freezeStreamTerminal(
111
+ args: CreateStreamFinalizerArgs,
112
+ outcome: ProviderStreamOutcome,
113
+ path: "response_owned" | "detached",
114
+ ): FrozenStreamTerminal {
115
+ const report = buildStreamTerminalReport(args, outcome);
116
+ args.reporter.recordProviderAttempt(report.attempt);
117
+ const terminal = args.reporter.freezeProviderTerminal(report.providerOutcome);
118
+ return {
119
+ terminal,
120
+ reporting: reportTerminalOnce(args, report, terminal, path),
121
+ };
122
+ }
123
+
124
+ async function reportTerminalOnce(
125
+ args: CreateStreamFinalizerArgs,
126
+ report: StreamTerminalReport,
127
+ terminal: FrozenProviderTerminal,
128
+ path: "response_owned" | "detached",
129
+ ): Promise<void> {
130
+ const capture = (error: unknown, code: OperationalErrorCode): void =>
131
+ captureOperationalError(error, {
132
+ code,
133
+ requestId: args.requestId,
134
+ routerId: args.execution.usageContext?.routerId,
135
+ });
136
+ try {
137
+ await (path === "response_owned"
138
+ ? args.reportBarrier
139
+ : Promise.race([
140
+ args.reportBarrier,
141
+ new Promise((resolve) => setTimeout(resolve, args.cancelBarrierCapMs ?? CANCEL_REPORT_BARRIER_CAP_MS)),
142
+ ]));
143
+ } catch (error) {
144
+ capture(error, "routing_report_barrier_failed");
145
+ }
146
+ try {
147
+ terminal.report();
148
+ if (report.reportManagedUsage && args.execution.usageContext) {
149
+ await reportRoutingUsage(
150
+ args.execution.usageContext,
151
+ report.providerMessage,
152
+ );
153
+ }
154
+ } catch (error) {
155
+ capture(error, "stream_report_finalization_failed");
156
+ }
157
+ }
158
+
159
+ type StreamTerminalReport = {
160
+ attempt: ProviderAttemptRecord;
161
+ providerOutcome: ReportableProviderOutcome;
162
+ providerMessage: Record<string, unknown>;
163
+ reportManagedUsage: boolean;
164
+ };
165
+
166
+ function buildStreamTerminalReport(
167
+ args: CreateStreamFinalizerArgs,
168
+ outcome: ProviderStreamOutcome,
169
+ ): StreamTerminalReport {
170
+ const execution = reportedExecution(args.execution);
171
+ const attemptBase = {
172
+ model: args.decision.selectedModel,
173
+ reasoning_effort: args.decision.reasoningEffort,
174
+ role: args.role,
175
+ attempt: args.attempt,
176
+ execution,
177
+ } as const;
178
+ if (outcome.status === "completed") {
179
+ const responseBody = args.protocol.responseBody({
180
+ id: args.requestId,
181
+ model: args.execution.model,
182
+ output: outcome.output,
183
+ modelCost: args.reporter.modelCost({
184
+ ...attemptBase,
185
+ outcome: "completed",
186
+ message: outcome.message,
187
+ billingOutcome: "completed",
188
+ }),
189
+ request: args.interaction,
190
+ routing: args.decision,
191
+ conversationId: args.execution.conversationId,
192
+ });
193
+ return {
194
+ attempt: {
195
+ ...attemptBase,
196
+ outcome: "completed",
197
+ message: outcome.message,
198
+ billingOutcome: "completed",
199
+ },
200
+ providerOutcome: {
201
+ status: "completed",
202
+ output: outcome.output,
203
+ message: outcome.message,
204
+ responseBody,
205
+ providerLatencyMs: args.providerLatencyMs(),
206
+ execution,
207
+ },
208
+ providerMessage: outcome.message,
209
+ reportManagedUsage: true,
210
+ };
211
+ }
212
+
213
+ if (outcome.status === "provider_error") {
214
+ return {
215
+ attempt: {
216
+ ...attemptBase,
217
+ outcome: "terminal_error",
218
+ message: outcome.message,
219
+ billingOutcome: "provider_failed",
220
+ },
221
+ providerOutcome: {
222
+ status: "provider_error",
223
+ message: outcome.message,
224
+ providerLatencyMs: args.providerLatencyMs(),
225
+ execution,
226
+ },
227
+ providerMessage: outcome.message,
228
+ reportManagedUsage: false,
229
+ };
230
+ }
231
+
232
+ // The client left before the provider finished. The provider call was
233
+ // aborted at that moment, so bill the prompt plus what was delivered.
234
+ const delivered = args.delivered();
235
+ const usage = estimatedCancelUsage({
236
+ pricing: args.execution.pricing,
237
+ promptEstimate: args.execution.promptEstimate,
238
+ delivered,
239
+ });
240
+ const providerMessage = customerCanceledProviderMessage(usage);
241
+ return {
242
+ attempt: {
243
+ ...attemptBase,
244
+ outcome: "canceled",
245
+ message: providerMessage,
246
+ billingOutcome: "customer_canceled",
247
+ },
248
+ providerOutcome: {
249
+ status: "customer_canceled",
250
+ message: providerMessage,
251
+ responseBody: outcome.responseBody,
252
+ providerLatencyMs: args.providerLatencyMs(),
253
+ execution,
254
+ partialOutput: partialInteractionOutput({
255
+ delivered,
256
+ usage,
257
+ source: args.source,
258
+ createdAtMs: args.startedAtMs,
259
+ }),
260
+ },
261
+ providerMessage,
262
+ reportManagedUsage: true,
263
+ };
264
+ }
@@ -0,0 +1,208 @@
1
+ import { isRecord } from "@mupt-ai/dari-router/policy-engine";
2
+
3
+ import { providerFailureMessage } from "../runtime/provider_stream_outcome";
4
+ import type { ProviderKeySource } from "../types";
5
+ import type { CapturedProviderResponse } from "./response_capture";
6
+
7
+ export type SubscriptionOAuthProvider = "anthropic_claude_code" | "openai_codex";
8
+
9
+ export type SubscriptionUsageLimit = {
10
+ oauthProvider: SubscriptionOAuthProvider;
11
+ resetsAt: Date;
12
+ reason: string;
13
+ // The platform made the user's next subscription active, so a retry
14
+ // succeeds without any fallback.
15
+ switched?: boolean;
16
+ };
17
+
18
+ // Neither provider guarantees a reset time on every usage-limit response.
19
+ // Without one, hold the subscription out for an hour rather than hammering an
20
+ // exhausted plan on every request.
21
+ export const DEFAULT_USAGE_LIMIT_WINDOW_MS = 60 * 60 * 1000;
22
+
23
+ const FAILURE_MARKER = "subscription_usage_limit";
24
+
25
+ const SUBSCRIPTION_LABEL: Record<SubscriptionOAuthProvider, string> = {
26
+ anthropic_claude_code: "Claude Code",
27
+ openai_codex: "ChatGPT",
28
+ };
29
+
30
+ export function subscriptionOAuthProviderFor(
31
+ provider: string,
32
+ keySource: ProviderKeySource,
33
+ ): SubscriptionOAuthProvider | null {
34
+ if (keySource !== "subscription") return null;
35
+ if (provider === "anthropic") return "anthropic_claude_code";
36
+ if (provider === "openai" || provider === "openai-codex") return "openai_codex";
37
+ return null;
38
+ }
39
+
40
+ /**
41
+ * Recognize a personal subscription that has run out of quota. Claude Code
42
+ * answers 429 with the unified rate-limit headers; the ChatGPT backend answers
43
+ * 429 with a structured `error.code` and `resets_at`. The Codex WebSocket
44
+ * transport bypasses fetch, so its usage-limit error only survives as message
45
+ * text and is matched there without a reset time.
46
+ */
47
+ export function detectSubscriptionUsageLimit(args: {
48
+ provider: string;
49
+ keySource: ProviderKeySource;
50
+ captured: CapturedProviderResponse | undefined;
51
+ failure: Record<string, unknown>;
52
+ now?: Date;
53
+ }): SubscriptionUsageLimit | null {
54
+ const oauthProvider = subscriptionOAuthProviderFor(args.provider, args.keySource);
55
+ if (oauthProvider === null) return null;
56
+ const now = args.now ?? new Date();
57
+ if (oauthProvider === "anthropic_claude_code") {
58
+ return anthropicUsageLimit(args.captured, now);
59
+ }
60
+ return codexUsageLimit(args.captured, providerFailureMessage(args.failure), now);
61
+ }
62
+
63
+ function anthropicUsageLimit(
64
+ captured: CapturedProviderResponse | undefined,
65
+ now: Date,
66
+ ): SubscriptionUsageLimit | null {
67
+ if (captured === undefined || captured.status !== 429) return null;
68
+ const status = captured.headers.get("anthropic-ratelimit-unified-status");
69
+ const overageStatus = captured.headers.get("anthropic-ratelimit-unified-overage-status");
70
+ // A plain per-minute rate limit is transient; only a rejected unified
71
+ // window means the plan itself is exhausted.
72
+ if (status !== "rejected" || (overageStatus !== null && overageStatus !== "rejected")) {
73
+ return null;
74
+ }
75
+ const claim = captured.headers.get("anthropic-ratelimit-unified-representative-claim");
76
+ return {
77
+ oauthProvider: "anthropic_claude_code",
78
+ resetsAt: resetFromUnixSeconds(captured.headers.get("anthropic-ratelimit-unified-reset"), now),
79
+ reason: claim ? `Claude Code ${claim.replaceAll("_", " ")} usage limit reached.` : "Claude Code usage limit reached.",
80
+ };
81
+ }
82
+
83
+ const CODEX_LIMIT_CODES = new Set(["usage_limit_reached", "usage_not_included"]);
84
+ const CODEX_LIMIT_MESSAGE = /usage_limit_reached|usage_not_included|hit your ChatGPT usage limit|the usage limit has been reached/i;
85
+
86
+ function codexUsageLimit(
87
+ captured: CapturedProviderResponse | undefined,
88
+ failureMessage: string,
89
+ now: Date,
90
+ ): SubscriptionUsageLimit | null {
91
+ if (captured !== undefined && captured.status === 429) {
92
+ const error = codexErrorBody(captured.bodyText);
93
+ const code = typeof error?.code === "string" ? error.code : "";
94
+ if (error !== null && (CODEX_LIMIT_CODES.has(code) || failureMessage.match(CODEX_LIMIT_MESSAGE))) {
95
+ const plan = typeof error.plan_type === "string" ? ` (${error.plan_type.toLowerCase()} plan)` : "";
96
+ return {
97
+ oauthProvider: "openai_codex",
98
+ resetsAt: laterReset(error.resets_at, captured.headers, now),
99
+ reason: `ChatGPT usage limit reached${plan}.`,
100
+ };
101
+ }
102
+ return null;
103
+ }
104
+ if (!CODEX_LIMIT_MESSAGE.test(failureMessage)) return null;
105
+ return {
106
+ oauthProvider: "openai_codex",
107
+ resetsAt: new Date(now.getTime() + DEFAULT_USAGE_LIMIT_WINDOW_MS),
108
+ reason: "ChatGPT usage limit reached.",
109
+ };
110
+ }
111
+
112
+ function codexErrorBody(bodyText: string): Record<string, unknown> | null {
113
+ try {
114
+ const parsed: unknown = JSON.parse(bodyText);
115
+ return isRecord(parsed) && isRecord(parsed.error) ? parsed.error : null;
116
+ } catch {
117
+ return null;
118
+ }
119
+ }
120
+
121
+ // Codex may send both an absolute `resets_at` and Retry-After; the plan is
122
+ // back only once both have passed.
123
+ function laterReset(resetsAt: unknown, headers: Headers, now: Date): Date {
124
+ const deadlines = [unixSecondsDeadline(resetsAt, now), retryAfterDeadline(headers, now)]
125
+ .filter((deadline): deadline is Date => deadline !== null);
126
+ if (deadlines.length === 0) return new Date(now.getTime() + DEFAULT_USAGE_LIMIT_WINDOW_MS);
127
+ return new Date(Math.max(...deadlines.map((deadline) => deadline.getTime())));
128
+ }
129
+
130
+ function retryAfterDeadline(headers: Headers, now: Date): Date | null {
131
+ const milliseconds = Number(headers.get("retry-after-ms") ?? NaN);
132
+ if (Number.isFinite(milliseconds) && milliseconds > 0) {
133
+ return new Date(now.getTime() + milliseconds);
134
+ }
135
+ const value = headers.get("retry-after")?.trim();
136
+ if (!value) return null;
137
+ const seconds = Number(value);
138
+ const deadline = Number.isFinite(seconds)
139
+ ? new Date(now.getTime() + seconds * 1000)
140
+ : new Date(value);
141
+ return deadline.getTime() > now.getTime() ? deadline : null;
142
+ }
143
+
144
+ function unixSecondsDeadline(value: unknown, now: Date): Date | null {
145
+ const seconds = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
146
+ return Number.isFinite(seconds) && seconds * 1000 > now.getTime() ? new Date(seconds * 1000) : null;
147
+ }
148
+
149
+ function resetFromUnixSeconds(value: unknown, now: Date): Date {
150
+ const seconds = typeof value === "number" ? value : typeof value === "string" ? Number(value) : NaN;
151
+ if (Number.isFinite(seconds) && seconds * 1000 > now.getTime()) {
152
+ return new Date(seconds * 1000);
153
+ }
154
+ return new Date(now.getTime() + DEFAULT_USAGE_LIMIT_WINDOW_MS);
155
+ }
156
+
157
+ /** Tag a provider failure so telemetry and the terminal error know why. */
158
+ export function failureWithSubscriptionUsageLimit(
159
+ failure: Record<string, unknown>,
160
+ limit: SubscriptionUsageLimit,
161
+ ): Record<string, unknown> {
162
+ return {
163
+ ...failure,
164
+ [FAILURE_MARKER]: {
165
+ oauth_provider: limit.oauthProvider,
166
+ resets_at: limit.resetsAt.toISOString(),
167
+ reason: limit.reason,
168
+ ...(limit.switched === true ? { switched: true } : {}),
169
+ },
170
+ };
171
+ }
172
+
173
+ export function subscriptionUsageLimitFromFailure(
174
+ failure: Record<string, unknown>,
175
+ ): SubscriptionUsageLimit | null {
176
+ const marker = failure[FAILURE_MARKER];
177
+ if (!isRecord(marker)) return null;
178
+ const oauthProvider = marker.oauth_provider;
179
+ const resetsAt = typeof marker.resets_at === "string" ? new Date(marker.resets_at) : null;
180
+ if (
181
+ (oauthProvider !== "anthropic_claude_code" && oauthProvider !== "openai_codex")
182
+ || resetsAt === null || Number.isNaN(resetsAt.getTime())
183
+ ) {
184
+ return null;
185
+ }
186
+ return {
187
+ oauthProvider,
188
+ resetsAt,
189
+ reason: typeof marker.reason === "string" ? marker.reason : "Subscription usage limit reached.",
190
+ ...(marker.switched === true ? { switched: true } : {}),
191
+ };
192
+ }
193
+
194
+ export function subscriptionUsageLimitMessage(limit: SubscriptionUsageLimit): string {
195
+ return `Your ${SUBSCRIPTION_LABEL[limit.oauthProvider]} subscription usage limit is reached. `
196
+ + `It resets at ${limit.resetsAt.toISOString()}. `
197
+ + "Mark it available early at https://app.dari.dev/credentials.";
198
+ }
199
+
200
+ export function subscriptionSwitchedMessage(limit: SubscriptionUsageLimit): string {
201
+ const label = SUBSCRIPTION_LABEL[limit.oauthProvider];
202
+ return `Your ${label} subscription usage limit is reached. `
203
+ + `Dari switched to your next connected ${label} subscription; retry the request.`;
204
+ }
205
+
206
+ export function retryAfterSeconds(limit: SubscriptionUsageLimit, now = new Date()): number {
207
+ return Math.max(0, Math.ceil((limit.resetsAt.getTime() - now.getTime()) / 1000));
208
+ }
@@ -0,0 +1,99 @@
1
+ import { isRecord, pricingForPrompt, providerMinCacheTokens, type ModelPricing } from "@mupt-ai/dari-router/policy-engine";
2
+
3
+ // The ChatGPT subscription backend reports cached prompt tokens but never
4
+ // cache_write_tokens, so every fresh token lands in the input bucket and is
5
+ // priced at the input rate. The public API bills those tokens as cache writes
6
+ // (the GPT-5.6 family accepts only 24h extended caching, and its responses
7
+ // report a write within a few tokens of the fresh count), so subscription rows
8
+ // otherwise read as $0.00 cache write while the identical API request records
9
+ // the premium. Attribute them here, where the provider identity and the
10
+ // pricing pi-ai used are both known, so telemetry, client usage, and the
11
+ // estimator — which already prices fresh input at the cache-write rate — agree.
12
+ const SUBSCRIPTION_RESPONSES_API = "openai-codex-responses";
13
+
14
+ const MTOK = 1_000_000;
15
+
16
+ export type SubscriptionUsageContext = {
17
+ api: string;
18
+ model: string;
19
+ pricing: ModelPricing | null;
20
+ };
21
+
22
+ export function messageWithSubscriptionCacheWrites(
23
+ message: Record<string, unknown>,
24
+ context: SubscriptionUsageContext
25
+ ): Record<string, unknown> {
26
+ const usage = subscriptionCacheWriteUsage(message.usage, context);
27
+ return usage === message.usage ? message : { ...message, usage };
28
+ }
29
+
30
+ // Every stream consumer reads final usage off the terminal `done` message.
31
+ export async function* eventsWithSubscriptionCacheWrites(
32
+ events: AsyncIterable<Record<string, unknown>>,
33
+ context: SubscriptionUsageContext
34
+ ): AsyncIterable<Record<string, unknown>> {
35
+ for await (const event of events) {
36
+ if (event.type === "done" && isRecord(event.message)) {
37
+ yield { ...event, message: messageWithSubscriptionCacheWrites(event.message, context) };
38
+ continue;
39
+ }
40
+ yield event;
41
+ }
42
+ }
43
+
44
+ export function subscriptionCacheWriteUsage(
45
+ usage: unknown,
46
+ context: SubscriptionUsageContext
47
+ ): unknown {
48
+ if (context.api !== SUBSCRIPTION_RESPONSES_API || !isRecord(usage)) return usage;
49
+ const pricing = context.pricing === null ? null : pricingForPrompt(context.pricing,
50
+ (finiteNumber(usage.input) ?? 0) + (finiteNumber(usage.cacheRead) ?? 0) + (finiteNumber(usage.cacheWrite) ?? 0));
51
+ // Without a write premium the buckets price identically, so moving tokens
52
+ // between them would change the report without changing the cost.
53
+ if (!pricing || !(pricing.cacheWrite > pricing.input)) return usage;
54
+
55
+ const input = finiteNumber(usage.input) ?? 0;
56
+ const cacheRead = finiteNumber(usage.cacheRead) ?? 0;
57
+ const cacheWrite = finiteNumber(usage.cacheWrite) ?? 0;
58
+ // A reported write means this backend did account for the bucket; leave it.
59
+ if (cacheWrite > 0 || input <= 0) return usage;
60
+ // Prompts under the provider minimum are never cached, so their fresh tokens
61
+ // are genuinely plain input. This path is gated to the OpenAI subscription
62
+ // API above, so the execution provider is openai by construction.
63
+ if (input + cacheRead < providerMinCacheTokens(context.model, "openai")) return usage;
64
+
65
+ const cost = isRecord(usage.cost) ? usage.cost : {};
66
+ const outputCost = finiteNumber(cost.output) ?? 0;
67
+ const cacheReadCost = finiteNumber(cost.cacheRead) ?? 0;
68
+ const cacheWriteCost = (input * pricing.cacheWrite) / MTOK;
69
+ return {
70
+ ...usage,
71
+ input: 0,
72
+ cacheWrite: input,
73
+ cost: {
74
+ ...cost,
75
+ input: 0,
76
+ cacheRead: cacheReadCost,
77
+ cacheWrite: cacheWriteCost,
78
+ output: outputCost,
79
+ total: outputCost + cacheReadCost + cacheWriteCost,
80
+ },
81
+ };
82
+ }
83
+
84
+ export function modelPricingFromPiModel(model: Record<string, unknown>): ModelPricing | null {
85
+ const cost = model.cost;
86
+ if (!isRecord(cost)) return null;
87
+ const input = finiteNumber(cost.input);
88
+ const output = finiteNumber(cost.output);
89
+ const cacheRead = finiteNumber(cost.cacheRead);
90
+ const cacheWrite = finiteNumber(cost.cacheWrite);
91
+ if (input === null || output === null || cacheRead === null || cacheWrite === null) return null;
92
+ const tiers = Array.isArray(cost.tiers) ? cost.tiers.filter((tier): tier is NonNullable<ModelPricing["tiers"]>[number] =>
93
+ isRecord(tier) && [tier.inputTokensAbove, tier.input, tier.output, tier.cacheRead, tier.cacheWrite].every((rate) => finiteNumber(rate) !== null)) : [];
94
+ return { input, output, cacheRead, cacheWrite, ...(tiers.length ? { tiers } : {}) };
95
+ }
96
+
97
+ function finiteNumber(value: unknown): number | null {
98
+ return typeof value === "number" && Number.isFinite(value) ? value : null;
99
+ }
@@ -0,0 +1,80 @@
1
+ import {
2
+ routingCandidateKey,
3
+ type CandidateCostEstimate,
4
+ type RoutingDecision,
5
+ } from "@mupt-ai/dari-router/policy-engine";
6
+
7
+ import type { RoutedDecision } from "../../strategies/route_decision";
8
+ import { isBackgroundRequest } from "../../interaction/types";
9
+ import { HttpError } from "../../http/errors";
10
+ import { assertCreditAvailableBeforeRouting } from "../../telemetry/usage";
11
+ import type { PreparedRoutedCompletion } from "../prepare_routed_completion";
12
+ import { leaseFallbackDecision } from "../routing_lease.js";
13
+
14
+ // Explicit background classes bypass the selector and every lease path.
15
+ // Keep the existing deterministic cheapest-input-cost serving policy.
16
+ export function auxiliaryDecision(
17
+ prepared: PreparedRoutedCompletion,
18
+ ): RoutedDecision | null {
19
+ if (!isBackgroundRequest(prepared.interaction)) return null;
20
+ assertCreditAvailableBeforeRouting(prepared.billable, prepared.credentials);
21
+ const requestClass = prepared.interaction.agentContext!.requestClass;
22
+ const cheapest = cheapestCandidate(prepared);
23
+ if (!cheapest) {
24
+ throw new HttpError(503, "Background requests require a routable model with known pricing.",
25
+ "background_model_unavailable");
26
+ }
27
+
28
+ const decision: RoutingDecision = {
29
+ selectedModel: cheapest.model,
30
+ reasoningEffort: cheapest.reasoning_effort,
31
+ reason: `${requestClass} request served by the cheapest routable model without selection.`,
32
+ };
33
+ const fallbackDecision = leaseFallbackDecision({
34
+ candidates: prepared.routing.candidateResolution.candidates,
35
+ decision,
36
+ credentials: prepared.credentials,
37
+ });
38
+ if (fallbackDecision) prepared.reporter.attachFallbackDecision(fallbackDecision);
39
+ return fallbackDecision ? { decision, fallbackDecision } : { decision };
40
+ }
41
+
42
+ /* Ranked on estimated input cost: these requests carry a whole transcript in
43
+ and a sentence out, so the prompt dominates. Estimates cover the configured
44
+ roster, so they are narrowed to the candidates pruning actually left
45
+ routable. Unpriced candidates are not comparable; fail explicitly if no
46
+ priced candidate is available, never fall through to selection. */
47
+ function cheapestCandidate(
48
+ prepared: PreparedRoutedCompletion,
49
+ ): CandidateCostEstimate | null {
50
+ const routable = new Set(
51
+ prepared.routing.candidateResolution.candidates.map(routingCandidateKey),
52
+ );
53
+ let cheapest: CandidateCostEstimate | null = null;
54
+ for (const estimate of prepared.routing.candidateResolution.costEstimates) {
55
+ if (!estimate.pricing_known || estimate.est_input_cost_usd === null) continue;
56
+ if (
57
+ !routable.has(
58
+ routingCandidateKey({
59
+ model: estimate.model,
60
+ reasoningEffort: estimate.reasoning_effort,
61
+ }),
62
+ )
63
+ ) {
64
+ continue;
65
+ }
66
+ if (cheapest === null || cheaperThan(estimate, cheapest)) cheapest = estimate;
67
+ }
68
+ return cheapest;
69
+ }
70
+
71
+ function cheaperThan(
72
+ candidate: CandidateCostEstimate,
73
+ incumbent: CandidateCostEstimate,
74
+ ): boolean {
75
+ const candidateInput = candidate.est_input_cost_usd ?? Infinity;
76
+ const incumbentInput = incumbent.est_input_cost_usd ?? Infinity;
77
+ if (candidateInput !== incumbentInput) return candidateInput < incumbentInput;
78
+ return (candidate.output_cost_per_mtok ?? Infinity)
79
+ < (incumbent.output_cost_per_mtok ?? Infinity);
80
+ }