@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.1

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 (245) hide show
  1. package/ARCHITECTURE.md +43 -0
  2. package/LICENSE +202 -0
  3. package/README.md +142 -2
  4. package/assets/openllm-light.svg +18 -0
  5. package/assets/openllm.svg +18 -0
  6. package/client/chat.ts +140 -0
  7. package/client/devices.ts +269 -0
  8. package/client/http.ts +72 -0
  9. package/client/index.ts +129 -0
  10. package/client/relay-mux.ts +547 -0
  11. package/client/types.ts +51 -0
  12. package/client/vault.ts +239 -0
  13. package/connect/chat.ts +58 -0
  14. package/connect/client.ts +240 -0
  15. package/connect/config.ts +47 -0
  16. package/connect/errors.ts +121 -0
  17. package/connect/index.ts +23 -0
  18. package/connect/oauth.ts +265 -0
  19. package/connect/routes.ts +234 -0
  20. package/connect/store.ts +107 -0
  21. package/mcp/handle.ts +24 -0
  22. package/mcp/index.ts +76 -0
  23. package/mcp/server.ts +319 -0
  24. package/mcp/tools/marketplace.ts +439 -0
  25. package/mcp/tools/memory-client.ts +297 -0
  26. package/mcp/tools/memory-config.ts +32 -0
  27. package/mcp/tools/memory.ts +369 -0
  28. package/mcp/tools/model-priority.ts +88 -0
  29. package/mcp/tools/openllm.ts +883 -0
  30. package/mcp/types.ts +28 -0
  31. package/openapi/call.ts +90 -0
  32. package/openapi/index.ts +8 -0
  33. package/openapi/openapi.json +80213 -0
  34. package/openapi/operations.ts +2075 -0
  35. package/package.json +39 -4
  36. package/protocol/CONTRIBUTING.md +21 -0
  37. package/protocol/README.md +134 -0
  38. package/protocol/access.ts +186 -0
  39. package/protocol/agent-prompt-text.ts +32 -0
  40. package/protocol/agent.ts +991 -0
  41. package/protocol/api-key-syntax.ts +105 -0
  42. package/protocol/assets/openllm-light.svg +18 -0
  43. package/protocol/assets/openllm.svg +18 -0
  44. package/protocol/audio.ts +29 -0
  45. package/protocol/auth.ts +142 -0
  46. package/protocol/changelog.ts +155 -0
  47. package/protocol/chat.ts +458 -0
  48. package/protocol/client-registry.ts +356 -0
  49. package/protocol/config.ts +345 -0
  50. package/protocol/control-channel.ts +115 -0
  51. package/protocol/cooldown-reason.ts +78 -0
  52. package/protocol/cost.ts +10 -0
  53. package/protocol/daemon-port.ts +31 -0
  54. package/protocol/daemon.ts +1524 -0
  55. package/protocol/default-chains.ts +66 -0
  56. package/protocol/device-state.ts +85 -0
  57. package/protocol/doctor-report-local.ts +443 -0
  58. package/protocol/doctor-report-policy.ts +119 -0
  59. package/protocol/doctor-report.ts +947 -0
  60. package/protocol/errors.ts +14 -0
  61. package/protocol/execution-selection.ts +141 -0
  62. package/protocol/hop-grant.ts +33 -0
  63. package/protocol/image-edit-parse.ts +263 -0
  64. package/protocol/images.ts +298 -0
  65. package/protocol/index.ts +74 -0
  66. package/protocol/inference-headers.ts +3 -0
  67. package/protocol/keepalive.ts +69 -0
  68. package/protocol/local-auth.ts +300 -0
  69. package/protocol/local-device-control.ts +133 -0
  70. package/protocol/local-provider.ts +288 -0
  71. package/protocol/local-session.ts +118 -0
  72. package/protocol/marketplace.ts +187 -0
  73. package/protocol/media-attempt.ts +81 -0
  74. package/protocol/media-bytes.ts +28 -0
  75. package/protocol/media-default.ts +357 -0
  76. package/protocol/media-dimensions.ts +132 -0
  77. package/protocol/media-input.ts +61 -0
  78. package/protocol/media-model.ts +142 -0
  79. package/protocol/media-option-issue.ts +90 -0
  80. package/protocol/media-types.ts +70 -0
  81. package/protocol/model-match.ts +1051 -0
  82. package/protocol/model-settings.ts +327 -0
  83. package/protocol/models.ts +767 -0
  84. package/protocol/mux.ts +722 -0
  85. package/protocol/plan-surface.ts +59 -0
  86. package/protocol/provider-models.ts +16 -0
  87. package/protocol/provider-status.ts +206 -0
  88. package/protocol/provider-tier.ts +67 -0
  89. package/protocol/provider-usage.ts +263 -0
  90. package/protocol/providers.ts +901 -0
  91. package/protocol/public-usage-index.ts +354 -0
  92. package/protocol/realtime-generated.ts +31 -0
  93. package/protocol/realtime.ts +324 -0
  94. package/protocol/registry-pointer.ts +118 -0
  95. package/protocol/relay.ts +702 -0
  96. package/protocol/replay-session.ts +11 -0
  97. package/protocol/responses.ts +280 -0
  98. package/protocol/semver.ts +182 -0
  99. package/protocol/setup-mode.ts +22 -0
  100. package/protocol/sha256-hex.ts +98 -0
  101. package/protocol/stats.ts +406 -0
  102. package/protocol/streaming.ts +19 -0
  103. package/protocol/subscription-daemon-floors.ts +33 -0
  104. package/protocol/subscription-provider-compat.ts +104 -0
  105. package/protocol/subscription-provider.ts +32 -0
  106. package/protocol/team-grant.ts +17 -0
  107. package/protocol/team-shareable.ts +61 -0
  108. package/protocol/transcription-encoding.ts +76 -0
  109. package/protocol/url-locality-dns.ts +37 -0
  110. package/protocol/url-locality.ts +288 -0
  111. package/protocol/usage-snapshots.ts +9 -0
  112. package/protocol/vendor-cli-install.ts +29 -0
  113. package/protocol/video-job-plan.ts +50 -0
  114. package/protocol/videos.ts +227 -0
  115. package/tsconfig.json +14 -0
  116. package/tunnel/ARCHITECTURE.md +111 -0
  117. package/tunnel/LICENSE +33 -0
  118. package/tunnel/README.md +45 -0
  119. package/tunnel/assets/openllm-light.svg +18 -0
  120. package/tunnel/assets/openllm.svg +18 -0
  121. package/tunnel/channel-envelope.ts +139 -0
  122. package/tunnel/codec.ts +191 -0
  123. package/tunnel/device-anchor.ts +229 -0
  124. package/tunnel/device-control.ts +118 -0
  125. package/tunnel/device-grant.ts +173 -0
  126. package/tunnel/fleet.ts +77 -0
  127. package/tunnel/index.ts +22 -0
  128. package/tunnel/mux.ts +580 -0
  129. package/tunnel/peer-link.ts +390 -0
  130. package/tunnel/rtc-auth.ts +415 -0
  131. package/tunnel/rtc-duplex.ts +187 -0
  132. package/tunnel/rtc-ice.ts +136 -0
  133. package/tunnel/secure.ts +1298 -0
  134. package/tunnel/stream-reset-error.ts +14 -0
  135. package/tunnel/streams.ts +1430 -0
  136. package/tunnel/team-grant.ts +302 -0
  137. package/tunnel/wire-bytes.ts +102 -0
  138. package/vault/ARCHITECTURE.md +470 -0
  139. package/vault/LICENSE +33 -0
  140. package/vault/api-key.ts +190 -0
  141. package/vault/argon2.ts +106 -0
  142. package/vault/cli-auth-seal.ts +157 -0
  143. package/vault/cred-seal.ts +146 -0
  144. package/vault/credential.ts +55 -0
  145. package/vault/crypto.ts +210 -0
  146. package/vault/daemon-attest.ts +229 -0
  147. package/vault/derive.ts +113 -0
  148. package/vault/device-access.ts +87 -0
  149. package/vault/device-anchor.ts +185 -0
  150. package/vault/device-control.ts +174 -0
  151. package/vault/handoff.ts +396 -0
  152. package/vault/index.ts +21 -0
  153. package/vault/passkey.ts +243 -0
  154. package/vault/phrase.ts +65 -0
  155. package/vault/pin.ts +153 -0
  156. package/vault/public-key-seal.ts +131 -0
  157. package/vault/seed.ts +272 -0
  158. package/vault/team.ts +355 -0
  159. package/vault/vault-item.ts +131 -0
  160. package/vault/vault-migrate-digest.ts +29 -0
  161. package/vault/wrapped.ts +77 -0
  162. package/wire/ARCHITECTURE.md +90 -0
  163. package/wire/CONTRIBUTING.md +19 -0
  164. package/wire/LICENSE +33 -0
  165. package/wire/README.md +51 -0
  166. package/wire/adapters/messages/anthropic-map.ts +37 -0
  167. package/wire/adapters/messages/index.ts +7 -0
  168. package/wire/adapters/messages/reasoning-from-items.ts +38 -0
  169. package/wire/adapters/messages/reasoning-signature.ts +243 -0
  170. package/wire/adapters/messages/request.ts +574 -0
  171. package/wire/adapters/messages/response.ts +215 -0
  172. package/wire/adapters/messages/streaming.ts +1027 -0
  173. package/wire/adapters/messages/usage.ts +43 -0
  174. package/wire/adapters/responses/index.ts +3 -0
  175. package/wire/adapters/responses/request.ts +395 -0
  176. package/wire/adapters/responses/response.ts +88 -0
  177. package/wire/adapters/responses/streaming.ts +344 -0
  178. package/wire/assets/openllm-light.svg +18 -0
  179. package/wire/assets/openllm.svg +18 -0
  180. package/wire/features/account-rank.ts +166 -0
  181. package/wire/features/compaction/compact-request.ts +862 -0
  182. package/wire/features/compaction/compaction-text.ts +43 -0
  183. package/wire/features/context-demote.ts +47 -0
  184. package/wire/features/context-overflow-strategy.ts +26 -0
  185. package/wire/features/context-skip.ts +170 -0
  186. package/wire/features/conversation-affinity.ts +144 -0
  187. package/wire/features/max-tokens-backfill.ts +141 -0
  188. package/wire/features/model-caps.ts +53 -0
  189. package/wire/features/model-settings.ts +24 -0
  190. package/wire/features/output-limit-clamp.ts +64 -0
  191. package/wire/features/quota-gate.ts +85 -0
  192. package/wire/index.ts +44 -0
  193. package/wire/lib/attempt-outcome.ts +43 -0
  194. package/wire/lib/canonical/content-part.ts +38 -0
  195. package/wire/lib/canonical/count-tokens-capability.ts +138 -0
  196. package/wire/lib/canonical/encoding-select.ts +82 -0
  197. package/wire/lib/canonical/image-signature.ts +202 -0
  198. package/wire/lib/canonical/image-sse.ts +216 -0
  199. package/wire/lib/canonical/message.ts +43 -0
  200. package/wire/lib/canonical/token-estimate.ts +266 -0
  201. package/wire/lib/encrypted-content.ts +66 -0
  202. package/wire/lib/error-class.ts +154 -0
  203. package/wire/lib/error-envelope.ts +56 -0
  204. package/wire/lib/forwarded-headers.ts +180 -0
  205. package/wire/lib/provider-error-rules.ts +48 -0
  206. package/wire/lib/refusal.ts +38 -0
  207. package/wire/lib/responses-apply-patch.ts +104 -0
  208. package/wire/lib/responses-namespace.ts +53 -0
  209. package/wire/lib/secret-scrub.ts +101 -0
  210. package/wire/lib/streaming/accumulate.ts +221 -0
  211. package/wire/lib/streaming/finish-reason.ts +33 -0
  212. package/wire/lib/streaming/peek.ts +154 -0
  213. package/wire/lib/streaming/provider-decode.ts +259 -0
  214. package/wire/lib/streaming/response-stream.ts +71 -0
  215. package/wire/lib/streaming/sse.ts +699 -0
  216. package/wire/lib/streaming/strip-tool-calls.ts +95 -0
  217. package/wire/lib/streaming/upstream-error.ts +58 -0
  218. package/wire/lib/strip-subagent-isolation.ts +284 -0
  219. package/wire/lib/thinking-signature.ts +156 -0
  220. package/wire/lib/token-ruler/bpe-core.ts +426 -0
  221. package/wire/lib/token-ruler/index.ts +2 -0
  222. package/wire/lib/token-ruler/ruler.ts +79 -0
  223. package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
  224. package/wire/lib/token-ruler/vocab/claude.ts +22 -0
  225. package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
  226. package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
  227. package/wire/lib/tool-name-map.ts +271 -0
  228. package/wire/lib/tool-schema.ts +353 -0
  229. package/wire/lib/usage-observer.ts +125 -0
  230. package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
  231. package/wire/providers/anthropic/beta-headers.ts +165 -0
  232. package/wire/providers/anthropic/index.ts +10 -0
  233. package/wire/providers/anthropic/request.ts +607 -0
  234. package/wire/providers/anthropic/response.ts +155 -0
  235. package/wire/providers/anthropic/streaming.ts +315 -0
  236. package/wire/providers/anthropic/web-search-domains.ts +49 -0
  237. package/wire/providers/chatgpt/common.ts +136 -0
  238. package/wire/providers/chatgpt/index.ts +9 -0
  239. package/wire/providers/chatgpt/request.ts +959 -0
  240. package/wire/providers/chatgpt/streaming.ts +1007 -0
  241. package/wire/providers/chatgpt/web-search.ts +63 -0
  242. package/wire/providers/grok/web-search.ts +87 -0
  243. package/wire/providers/kimi/web-search.ts +145 -0
  244. package/wire/providers/upstream-deny.ts +355 -0
  245. package/wire/providers/upstream-request.ts +619 -0
@@ -0,0 +1,266 @@
1
+ /**
2
+ * The TWO input-token estimators, together in one module so the choice between
3
+ * them is visible at the point of use. They are not interchangeable — each is
4
+ * wrong for the other's job:
5
+ *
6
+ * - {@link estimateBodyTokens} — the ROUTING gate. Every string in the body
7
+ * counts, so it OVER-estimates (discriminators, tool names, a base64 image
8
+ * payload). That is the point: it decides whether a hop's context window can
9
+ * hold the request, where over-shooting costs one extra chain step and
10
+ * under-shooting ships a request that 400s.
11
+ * - {@link estimateAnthropicInputTokens} — the CLIENT-FACING preflight
12
+ * (`/v1/messages/count_tokens`). Counts only what the model actually reads,
13
+ * so a client's context-window indicator isn't inflated by transport noise.
14
+ * Always uses a real BPE (default {@link DEFAULT_ENCODING} / o200k). Never
15
+ * use the routing estimator here: base64 image data alone would make
16
+ * Claude Code believe a fresh session was nearly full.
17
+ */
18
+
19
+ /**
20
+ * Rough input-token estimator over a request body, format-agnostic.
21
+ *
22
+ * Walks the JSON value summing string lengths (everything else
23
+ * contributes zero) and divides by 4. Works for both canonical
24
+ * OpenAI `ChatCompletionRequest` and raw Anthropic Messages bodies
25
+ * because both encode all user-visible text inside string values.
26
+ *
27
+ * Used pre-fetch by the chain orchestrator to skip a hop whose
28
+ * provider model can't fit the request — preventing the otherwise
29
+ * inevitable 400 + waste of the chain entry. The estimate is
30
+ * intentionally conservative: in borderline cases we'd rather burn
31
+ * an extra chain step than ship an oversized request that 400s.
32
+ */
33
+
34
+ import type {
35
+ TRulerCache,
36
+ TTokenCounter,
37
+ TTokenEncoding,
38
+ } from "./encoding-select";
39
+ import {
40
+ DEFAULT_ENCODING,
41
+ getTokenCounter,
42
+ peekTokenCounter,
43
+ } from "./encoding-select";
44
+
45
+ /**
46
+ * Count a single string as tokens for `encoding`, using the real BPE ruler when
47
+ * that family's tokenizer is already warm on this isolate, and falling back to
48
+ * the `chars/4` heuristic when it isn't. The synchronous fallback is what lets
49
+ * every caller stay synchronous; warm the ruler at request entry
50
+ * (`getTokenCounter`) to make the accurate path the common one. `max(1, …)` so
51
+ * any non-empty text counts as at least one token.
52
+ */
53
+ const rulerTokens = (text: string, encoding: TTokenEncoding): number => {
54
+ if (text.length === 0) return 0;
55
+ const counter = peekTokenCounter(encoding);
56
+ if (counter !== null) return Math.max(1, counter.count(text));
57
+ return Math.max(1, Math.ceil(text.length / 4));
58
+ };
59
+
60
+ const stringCharsFromAny = (v: unknown): number => {
61
+ if (typeof v === "string") return v.length;
62
+ if (Array.isArray(v)) {
63
+ let n = 0;
64
+ for (const x of v) n += stringCharsFromAny(x);
65
+ return n;
66
+ }
67
+ if (v !== null && typeof v === "object") {
68
+ let n = 0;
69
+ for (const k of Object.keys(v as Record<string, unknown>)) {
70
+ n += stringCharsFromAny((v as Record<string, unknown>)[k]);
71
+ }
72
+ return n;
73
+ }
74
+ return 0;
75
+ };
76
+
77
+ /**
78
+ * Collect every string in a JSON value, joined by "\n" (a token boundary the
79
+ * BPE won't merge across), so the ruler counts them as one pass instead of
80
+ * summing chars. Mirrors {@link stringCharsFromAny}'s walk exactly.
81
+ */
82
+ const collectStrings = (v: unknown, out: string[]): void => {
83
+ if (typeof v === "string") {
84
+ out.push(v);
85
+ return;
86
+ }
87
+ if (Array.isArray(v)) {
88
+ for (const x of v) collectStrings(x, out);
89
+ return;
90
+ }
91
+ if (v !== null && typeof v === "object") {
92
+ for (const k of Object.keys(v as Record<string, unknown>)) {
93
+ collectStrings((v as Record<string, unknown>)[k], out);
94
+ }
95
+ }
96
+ };
97
+
98
+ /**
99
+ * The ROUTING gate — ALWAYS the `chars/4` heuristic, never the BPE.
100
+ *
101
+ * This runs on every request (twice in the cloud handler, once per daemon walk),
102
+ * so it is deliberately arithmetic-only: a string-length walk with no
103
+ * tokenizer, no allocation of the joined body, and no dependence on whether
104
+ * some earlier request happened to warm a ruler on this isolate.
105
+ *
106
+ * It used to consult {@link peekTokenCounter} and silently upgrade itself to a
107
+ * real BPE pass once any ruler was warm. That made the hot path's cost — and
108
+ * its ANSWER — a function of process history: the first oversized request
109
+ * warmed the module-global counter cache and every later request on that
110
+ * isolate then paid full tokenization. Accuracy is now opt-in via
111
+ * {@link estimateBodyTokensExact}, taken only where it changes a decision
112
+ * (compaction) rather than ambiently everywhere.
113
+ */
114
+ export const estimateBodyTokens = (body: unknown): number =>
115
+ Math.ceil(stringCharsFromAny(body) / 4);
116
+
117
+ /** Count the serialized wire body for cap holds. On a failed or cold ruler,
118
+ * preserve the historical bytes/4 estimate instead of refusing the request.
119
+ * The reserve/admit modes bracket both estimates so differently warmed direct
120
+ * leg peers cannot reject an honest caller's signed allowance.
121
+ */
122
+ export const estimateCapInputTokens = (
123
+ body: Uint8Array,
124
+ mode: "reserve" | "admit" = "reserve",
125
+ cache?: TRulerCache,
126
+ ): number => {
127
+ const fallback = Math.ceil(body.byteLength / 4);
128
+ const counter = peekTokenCounter(DEFAULT_ENCODING, cache);
129
+ const claude = mode === "reserve" ? peekTokenCounter("claude", cache) : null;
130
+ if (counter === null && claude === null) return fallback;
131
+ const text = new TextDecoder().decode(body);
132
+ if (mode === "admit")
133
+ return Math.min(counter?.count(text) ?? fallback, fallback);
134
+ return Math.max(
135
+ fallback,
136
+ counter?.count(text) ?? 0,
137
+ claude?.count(text) ?? 0,
138
+ );
139
+ };
140
+
141
+ /**
142
+ * Warm the rulers {@link estimateCapInputTokens} uses for `mode`, each
143
+ * independently (a failed load leaves the other usable). Admission only reads
144
+ * o200k, so a supplier never pays for the Claude ruler.
145
+ */
146
+ export const warmCapTokenCounter = async (
147
+ mode: "reserve" | "admit" = "reserve",
148
+ ): Promise<void> => {
149
+ await Promise.allSettled([
150
+ getTokenCounter(DEFAULT_ENCODING),
151
+ ...(mode === "reserve" ? [getTokenCounter("claude")] : []),
152
+ ]);
153
+ };
154
+
155
+ /**
156
+ * The ACCURATE body estimate — the real BPE ruler when `encoding`'s family is
157
+ * warm, `chars/4` when it isn't. Opt-in and explicit: use it only where the
158
+ * extra precision changes an outcome and the caller has warmed the ruler
159
+ * deliberately (the compaction seams do, via `getTokenCounter`).
160
+ *
161
+ * Never call this on the per-request routing path — that is
162
+ * {@link estimateBodyTokens}, and it must stay arithmetic-only.
163
+ */
164
+ export const estimateBodyTokensExact = (
165
+ body: unknown,
166
+ encoding: TTokenEncoding = DEFAULT_ENCODING,
167
+ ): number => {
168
+ if (peekTokenCounter(encoding) === null) {
169
+ // Cold — the ruler was never warmed, so fall back to the cheap heuristic.
170
+ return Math.ceil(stringCharsFromAny(body) / 4);
171
+ }
172
+ const parts: string[] = [];
173
+ collectStrings(body, parts);
174
+ return rulerTokens(parts.join("\n"), encoding);
175
+ };
176
+
177
+ /**
178
+ * The text an Anthropic content value actually puts in front of the model:
179
+ * `text` blocks, a `tool_use`'s serialized input, a `tool_result`'s body
180
+ * (recursively — it may itself be a block array), and prior `thinking`. Block
181
+ * types the model does not read as text (image/document sources) contribute
182
+ * nothing, which is exactly why this is not `estimateBodyTokens`.
183
+ */
184
+ const anthropicContentText = (content: unknown): string => {
185
+ if (typeof content === "string") return content;
186
+ if (!Array.isArray(content)) return "";
187
+ const parts: string[] = [];
188
+ for (const block of content) {
189
+ if (block === null || typeof block !== "object") continue;
190
+ const b = block as Record<string, unknown>;
191
+ if (b.type === "text" && typeof b.text === "string") parts.push(b.text);
192
+ else if (b.type === "tool_use") {
193
+ // The name is billed alongside the arguments — both are in the transcript
194
+ // the model reads back.
195
+ if (typeof b.name === "string") parts.push(b.name);
196
+ parts.push(JSON.stringify(b.input ?? {}));
197
+ } else if (b.type === "tool_result") {
198
+ const inner = b.content;
199
+ if (typeof inner === "string") parts.push(inner);
200
+ else if (Array.isArray(inner)) parts.push(anthropicContentText(inner));
201
+ } else if (b.type === "thinking" && typeof b.thinking === "string") {
202
+ parts.push(b.thinking);
203
+ }
204
+ }
205
+ return parts.join("\n");
206
+ };
207
+
208
+ const visibleTranscriptOf = (body: unknown): string => {
209
+ if (body === null || typeof body !== "object") return "";
210
+ const b = body as Record<string, unknown>;
211
+ const sections: string[] = [];
212
+ const system = b.system;
213
+ if (typeof system === "string") sections.push(system);
214
+ else if (Array.isArray(system)) sections.push(anthropicContentText(system));
215
+ if (Array.isArray(b.messages)) {
216
+ for (const m of b.messages) {
217
+ if (m === null || typeof m !== "object") continue;
218
+ sections.push(
219
+ anthropicContentText((m as Record<string, unknown>).content),
220
+ );
221
+ }
222
+ }
223
+ if (Array.isArray(b.tools) && b.tools.length > 0) {
224
+ sections.push(JSON.stringify(b.tools));
225
+ }
226
+ return sections.filter((s) => s.length > 0).join("\n");
227
+ };
228
+
229
+ const countTranscript = (joined: string, counter: TTokenCounter): number =>
230
+ joined.length === 0 ? 0 : Math.max(1, counter.count(joined));
231
+
232
+ /**
233
+ * Estimated `input_tokens` for an Anthropic-shaped Messages body — the answer
234
+ * `/v1/messages/count_tokens` serves ONLY as a fallback, when the vendor's own
235
+ * endpoint isn't reachable (no Anthropic-wire hop at the head of the chain, no
236
+ * local credential, or an upstream refusal). Whenever the vendor can be asked,
237
+ * its exact count is relayed instead and this function is not consulted.
238
+ *
239
+ * Always acquires a BPE counter — never a silent `chars/4` heuristic. Default
240
+ * encoding is {@link DEFAULT_ENCODING} (o200k) regardless of inbound Messages
241
+ * wire shape. Pass `"claude"` only when the caller is explicitly counting for
242
+ * an Anthropic-family hop.
243
+ *
244
+ * Single-sourced here because BOTH preflight paths need the same number: the
245
+ * cloud handler (`@openllm/api/handlers/count-tokens`) and the daemon's local
246
+ * surface (`runCountTokens` in the walker).
247
+ *
248
+ * KNOWN LIMIT — text only. Images and documents contribute nothing, so an
249
+ * image-heavy (or image-only) request UNDER-counts, materially. This is
250
+ * deliberate: Anthropic prices an image at roughly `(w × h) / 750` tokens, and
251
+ * neither dimension is recoverable from a base64 blob or a URL without decoding
252
+ * or fetching it — so any number we put there would be fabricated, and a
253
+ * fabricated count is worse than a knowingly-low one for a fallback whose
254
+ * consumers use it as a context-window indicator. Counting the blob's own length
255
+ * (what {@link estimateBodyTokens} does) is not a substitute: it tracks encoding
256
+ * size, not token cost, and overstates by orders of magnitude.
257
+ */
258
+ export const estimateAnthropicInputTokens = async (
259
+ body: unknown,
260
+ encoding: TTokenEncoding = DEFAULT_ENCODING,
261
+ ): Promise<number> => {
262
+ const joined = visibleTranscriptOf(body);
263
+ if (joined.length === 0) return 0;
264
+ const counter = await getTokenCounter(encoding);
265
+ return countTranscript(joined, counter);
266
+ };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * Recovery for a replayed `reasoning.encrypted_content` the upstream can't
3
+ * decrypt. The OpenAI Responses backend (ChatGPT/Codex) and the Grok chat
4
+ * proxy only decrypt encrypted reasoning THEY minted for that
5
+ * account+model+session. On a fallback that switches account/model, the prior
6
+ * hop's encrypted reasoning replays into a hop that can't decrypt it and the
7
+ * backend 400s. Stripping the resumable state and retrying makes the request
8
+ * succeed (the model just re-reasons) instead of surfacing a hard error — the
9
+ * exact recovery the official partner client performs
10
+ * (audit 2026-07-14-codex-upstream-wire §F2).
11
+ */
12
+
13
+ /** Does a 400 body signal an undecryptable replayed reasoning item? Matched
14
+ * loosely across the OpenAI + xAI phrasings (`invalid_encrypted_content`,
15
+ * `thinking_signature_invalid`, "could not decrypt", "failed to decrypt"). */
16
+ const DECRYPT_FAILURE_RE =
17
+ /encrypted[_\s-]?content|thinking[_\s-]?signature|(?:could|failed|unable)[^.]*?decrypt|decrypt[^.]*?fail/i;
18
+
19
+ export const isEncryptedContentError = (rawBody: string): boolean =>
20
+ rawBody.length > 0 && DECRYPT_FAILURE_RE.test(rawBody);
21
+
22
+ /** True when a built Responses body carries a `reasoning` input item with
23
+ * non-empty `encrypted_content` — i.e. stripping it would change the request
24
+ * (so a retry is worth attempting). */
25
+ export const responsesBodyHasEncryptedContent = (body: unknown): boolean => {
26
+ if (body === null || typeof body !== "object") return false;
27
+ const input = (body as { input?: unknown }).input;
28
+ if (!Array.isArray(input)) return false;
29
+ return input.some(
30
+ (item) =>
31
+ item !== null &&
32
+ typeof item === "object" &&
33
+ (item as { type?: unknown }).type === "reasoning" &&
34
+ typeof (item as { encrypted_content?: unknown }).encrypted_content ===
35
+ "string" &&
36
+ ((item as { encrypted_content: string }).encrypted_content.length ?? 0) >
37
+ 0,
38
+ );
39
+ };
40
+
41
+ /** Return a copy of a built Responses body with `encrypted_content` removed
42
+ * from every `reasoning` input item (the item itself stays — its `summary`
43
+ * keeps the visible chain). The orphan `id` is dropped too: with
44
+ * `store: false` Codex treats a named reasoning item as a persisted lookup
45
+ * and 400s ("Item not found") when the ciphertext is gone. */
46
+ export const stripResponsesEncryptedContent = (body: unknown): unknown => {
47
+ if (body === null || typeof body !== "object") return body;
48
+ const b = body as { input?: unknown };
49
+ if (!Array.isArray(b.input)) return body;
50
+ const input = b.input.map((item) => {
51
+ if (
52
+ item === null ||
53
+ typeof item !== "object" ||
54
+ (item as { type?: unknown }).type !== "reasoning"
55
+ ) {
56
+ return item;
57
+ }
58
+ const {
59
+ encrypted_content: _dropped,
60
+ id: _removedId,
61
+ ...rest
62
+ } = item as Record<string, unknown>;
63
+ return rest;
64
+ });
65
+ return { ...(body as Record<string, unknown>), input };
66
+ };
@@ -0,0 +1,154 @@
1
+ import type { TCooldownReason, TErrorEnvelope } from "@openllmsh/sdk/protocol";
2
+ import {
3
+ errorRulesFor,
4
+ providerRuleMatches,
5
+ type TProviderErrorRule,
6
+ } from "./provider-error-rules";
7
+
8
+ /**
9
+ * Classifies an uncommitted upstream hop failure. Lives in
10
+ * `@openllmsh/sdk/wire` so the cloud orchestrator and coreless daemon walker
11
+ * share one policy.
12
+ *
13
+ * Once the gateway has validated the inbound request, an upstream rejection
14
+ * cannot prove that every other provider/model will reject the canonical
15
+ * request. Therefore every non-abort failure is classified as `transient`
16
+ * and the cooldown-policy action (`walk_no_cool` / `cool_and_advance` /
17
+ * `retry_in_place` / `surface`) decides whether the walk continues. Only
18
+ * request-deterministic reasons (`request_invalid`, `unprocessable`) surface
19
+ * immediately; capability / unfamiliar 4xx (`upstream_rejection`) and
20
+ * oversized payloads (`payload_too_large`) walk without cooling so a sibling
21
+ * model can still serve. This decision is deliberately independent of
22
+ * provider names for the defaults; optional `provider` rules may refine the
23
+ * reason tag before those defaults run.
24
+ *
25
+ * A committed response never reaches this classifier. After output has been
26
+ * committed, the serving transport owns any failure and cannot restart on a
27
+ * different model safely.
28
+ *
29
+ * Some Envoy edge-connect failures wear a 5xx status despite describing a
30
+ * network failure between the edge and its upstream. The narrow conjunction
31
+ * below keeps those failures from cooling a model, while ordinary vendor 5xx
32
+ * responses remain `server_error` and retain their cooldown.
33
+ */
34
+ export type TFallbackClass =
35
+ | { readonly kind: "transient"; readonly reason: TCooldownReason }
36
+ | { readonly kind: "abort" };
37
+
38
+ export type TClassifierInput = {
39
+ readonly status: number;
40
+ readonly envelope?: TErrorEnvelope | undefined;
41
+ readonly providerFormat: "openai" | "anthropic";
42
+ readonly aborted: boolean;
43
+ readonly provider?: string | undefined;
44
+ };
45
+
46
+ const QUOTA_BODY =
47
+ /usage limit|usage balance|balance exhausted|quota|billing cycle|upgrade your plan|out of credits|purchase extra usage|insufficient_quota|insufficient (?:credits?|balance|funds)|credit balance|payment required|top[\s-]?up (?:your )?(?:credits?|balance|account)/i;
48
+
49
+ /** Request-shape failures that every model would reject identically.
50
+ * Capability / model-local refusals ("Unsupported parameter: …",
51
+ * "Unknown parameter: …") stay out — those walk as `upstream_rejection`. */
52
+ const REQUEST_INVALID_BODY =
53
+ /missing required parameter|could not parse the json|invalid json body|extra inputs are not permitted/i;
54
+
55
+ /** True when an error body says the account is out of quota (the same
56
+ * phrasing that upgrades a 401/402/403/429 to `quota_exhausted`). */
57
+ export const isQuotaExhaustedText = (text: string): boolean =>
58
+ QUOTA_BODY.test(text);
59
+ /**
60
+ * The vendor error phrasings that mean "request too large for this model's
61
+ * context window". Exported so callers that classify a raw upstream body — e.g.
62
+ * the daemon walker's last-resort compaction trigger — reuse the SAME matcher
63
+ * instead of maintaining a drifting copy.
64
+ */
65
+ export const CONTEXT_OVERFLOW_BODY =
66
+ /maximum (prompt|context) length|exceeds the context window|too many tokens|reduce the length of the messages|prompt is too long|exceeds? (?:the )?model token limit|token limit: [\d,]+/i;
67
+ // Envoy's first sentence is version-invariant; the trailing reset-reason
68
+ // clause drifts between Envoy versions ("reset reason: …" vs "retried and the
69
+ // latest reset reason: …"), so the matcher pins only the first sentence.
70
+ const ENVOY_CONNECT_ERROR_BODY = [
71
+ /upstream connect error/i,
72
+ /disconnect\/reset before headers/i,
73
+ ] as const;
74
+
75
+ const isEnvoyConnectError = (message: string): boolean =>
76
+ ENVOY_CONNECT_ERROR_BODY.every((pattern) => pattern.test(message));
77
+
78
+ /**
79
+ * Pure reason tagging. Accepts an optional `rules` override so tests can
80
+ * exercise provider rules without mutating the empty shipped table.
81
+ */
82
+ export const reasonFor = (
83
+ i: TClassifierInput,
84
+ rules: readonly TProviderErrorRule[] = errorRulesFor(i.provider),
85
+ ): TCooldownReason => {
86
+ if (i.envelope?.error?.code === "content_filter") return "content_filter";
87
+ // A 2xx the runner still couldn't use (an undecodable 200): the provider
88
+ // ACCEPTED the request and may have billed it, so walking on would send —
89
+ // and bill — it again. Only an explicit in-band refusal (content_filter,
90
+ // above) walks from a 2xx; no body matcher or provider rule may turn an
91
+ // accepted request into a walk. 3xx falls through to the normal rules
92
+ // (typically `upstream_rejection` → walk).
93
+ if (i.status >= 200 && i.status < 300) return "accepted_unreadable";
94
+ const message = i.envelope?.error?.message ?? "";
95
+ if (CONTEXT_OVERFLOW_BODY.test(message)) return "context_overflow";
96
+
97
+ for (const rule of rules) {
98
+ if (
99
+ providerRuleMatches(rule, {
100
+ status: i.status,
101
+ code: i.envelope?.error?.code,
102
+ message,
103
+ })
104
+ ) {
105
+ return rule.reason;
106
+ }
107
+ }
108
+
109
+ if (i.status === 0) return "network";
110
+ if (i.status === 408) return "timeout";
111
+ if (i.status === 429) {
112
+ return QUOTA_BODY.test(message) ? "quota_exhausted" : "rate_limit";
113
+ }
114
+ if (i.status >= 500) {
115
+ return isEnvoyConnectError(message) ? "network" : "server_error";
116
+ }
117
+ // 426 Upgrade Required: the upstream refuses THIS client (xAI's CLI proxy
118
+ // gates on the device's installed grok version). Like an auth failure it is
119
+ // hop-local — another provider/device can serve — so it cools and advances
120
+ // instead of falling into a catch-all that used to surface and halt the chain.
121
+ if (
122
+ i.status === 401 ||
123
+ i.status === 403 ||
124
+ i.status === 407 ||
125
+ i.status === 426
126
+ ) {
127
+ return QUOTA_BODY.test(message) ? "quota_exhausted" : "auth";
128
+ }
129
+ if (i.status === 402) {
130
+ return QUOTA_BODY.test(message) ? "quota_exhausted" : "payment";
131
+ }
132
+ if (i.status === 404) return "not_found";
133
+ if (i.status === 413) return "payload_too_large";
134
+ if (i.status === 422) return "unprocessable";
135
+ // Any remaining 4xx: quota / request-invalid body matchers before the
136
+ // catch-all. A credits-exhausted OpenRouter 400 must cool-and-advance, not
137
+ // stop the walk; a missing-parameter 400 still surfaces.
138
+ if (i.status >= 400 && i.status < 500) {
139
+ if (QUOTA_BODY.test(message)) return "quota_exhausted";
140
+ if (REQUEST_INVALID_BODY.test(message)) return "request_invalid";
141
+ }
142
+ return "upstream_rejection";
143
+ };
144
+
145
+ export const classifyHopError = (
146
+ i: TClassifierInput,
147
+ rules?: readonly TProviderErrorRule[],
148
+ ): TFallbackClass =>
149
+ i.aborted
150
+ ? { kind: "abort" }
151
+ : {
152
+ kind: "transient",
153
+ reason: reasonFor(i, rules ?? errorRulesFor(i.provider)),
154
+ };
@@ -0,0 +1,56 @@
1
+ import type { TErrorEnvelope } from "@openllmsh/sdk/protocol";
2
+
3
+ /** Keep enough of a non-JSON body for classification (quota phrases can sit
4
+ * past a short display snippet). Display/header callers pass their own max. */
5
+ const CLASSIFY_BODY_SNIPPET_CHARS = 500;
6
+
7
+ /**
8
+ * Short single-line snippet from an upstream body (max chars, with ellipsis).
9
+ * Shared by the envelope parser (classification) and the daemon walker
10
+ * (trail/UI headers) so the trim/ellipsis rules cannot drift.
11
+ */
12
+ export const bodySnippet = (raw: string, max = 200): string => {
13
+ const line = raw.replace(/\s+/g, " ").trim();
14
+ if (line.length === 0) return "";
15
+ return line.length > max ? `${line.slice(0, max)}…` : line;
16
+ };
17
+
18
+ /**
19
+ * Best-effort error-envelope extraction for reason tagging. Parsing is never
20
+ * a routing gate: an unknown provider body still represents an uncommitted
21
+ * candidate failure and therefore walks.
22
+ *
23
+ * A STRING-valued `error` is lifted into the canonical envelope: xAI's CLI
24
+ * proxy answers an exhausted balance with `{"error":"Grok Build usage balance
25
+ * exhausted"}`, and dropping that prose left the classifier with an empty
26
+ * message — tagging the hop `payment` (60s) instead of `quota_exhausted`.
27
+ * Non-JSON bodies become a truncated snippet with type `upstream_error`
28
+ * (500 chars so a late quota phrase still classifies).
29
+ */
30
+ export const errorEnvelopeFrom = (raw: string): TErrorEnvelope | undefined => {
31
+ try {
32
+ const json = JSON.parse(raw) as { error?: unknown };
33
+ if (typeof json.error === "string") {
34
+ return { error: { message: json.error, type: "upstream_error" } };
35
+ }
36
+ if (json.error === null || typeof json.error !== "object") {
37
+ return undefined;
38
+ }
39
+ const e = json.error as Record<string, unknown>;
40
+ return {
41
+ error: {
42
+ message:
43
+ typeof e.message === "string"
44
+ ? e.message
45
+ : "upstream error (unparseable message)",
46
+ type: typeof e.type === "string" ? e.type : "upstream_error",
47
+ ...(typeof e.code === "string" ? { code: e.code } : {}),
48
+ },
49
+ };
50
+ } catch {
51
+ const message = bodySnippet(raw, CLASSIFY_BODY_SNIPPET_CHARS);
52
+ return message.length > 0
53
+ ? { error: { message, type: "upstream_error" } }
54
+ : undefined;
55
+ }
56
+ };