@openllmsh/sdk 0.0.0-stage → 2.10.12-alpha.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 (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,125 @@
1
+ import type { TAttemptUsage } from "./attempt-outcome";
2
+ import { sseEventStream } from "./streaming/sse";
3
+
4
+ export type TUsageProtocol = "openai" | "anthropic" | "embeddings" | "none";
5
+ export type TUsageTransport = "json" | "sse" | "error";
6
+
7
+ const record = (value: unknown): Record<string, unknown> | null =>
8
+ value !== null && typeof value === "object" && !Array.isArray(value)
9
+ ? (value as Record<string, unknown>)
10
+ : null;
11
+
12
+ const count = (value: unknown): value is number =>
13
+ typeof value === "number" && Number.isFinite(value) && value >= 0;
14
+
15
+ const extract = (
16
+ body: unknown,
17
+ protocol: TUsageProtocol,
18
+ ): TAttemptUsage | null => {
19
+ if (protocol === "none") return null;
20
+ const root = record(body);
21
+ const owner = record(root?.response) ?? root;
22
+ const usage = record(owner?.usage);
23
+ if (usage === null) return null;
24
+ const input =
25
+ protocol === "anthropic"
26
+ ? usage.input_tokens
27
+ : (usage.prompt_tokens ?? usage.input_tokens);
28
+ const output =
29
+ protocol === "embeddings"
30
+ ? 0
31
+ : protocol === "anthropic"
32
+ ? usage.output_tokens
33
+ : (usage.completion_tokens ?? usage.output_tokens);
34
+ if (!count(input) || !count(output)) return null;
35
+ const separateCache =
36
+ protocol === "anthropic" ||
37
+ (usage.prompt_tokens === undefined &&
38
+ usage.input_tokens_details === undefined);
39
+ const read = separateCache ? (usage.cache_read_input_tokens ?? 0) : 0;
40
+ const write = separateCache ? (usage.cache_creation_input_tokens ?? 0) : 0;
41
+ if (!count(read) || !count(write) || !Number.isFinite(input + read + write))
42
+ return null;
43
+ // Fractional counts are floored, per side, like every cap settlement.
44
+ const details =
45
+ record(usage.prompt_tokens_details) ?? record(usage.input_tokens_details);
46
+ const detailCached = details?.cached_tokens;
47
+ const detailCreated = details?.cache_creation_tokens;
48
+ const cached =
49
+ protocol === "anthropic" ? read : count(detailCached) ? detailCached : 0;
50
+ const cacheCreation =
51
+ protocol === "anthropic" ? write : count(detailCreated) ? detailCreated : 0;
52
+ return {
53
+ in: Math.floor(input) + Math.floor(read) + Math.floor(write),
54
+ out: Math.floor(output),
55
+ cached: Math.floor(cached),
56
+ cacheCreation: Math.floor(cacheCreation),
57
+ };
58
+ };
59
+
60
+ /** Incrementally observes already-read objects or a separately drained SSE stream. */
61
+ export const createUsageObserver = (
62
+ protocol: TUsageProtocol,
63
+ transport: TUsageTransport,
64
+ ): {
65
+ observe: (body: unknown) => void;
66
+ observeStream: (stream: ReadableStream<Uint8Array>) => Promise<void>;
67
+ usage: () => TAttemptUsage | null;
68
+ } => {
69
+ let last: TAttemptUsage | null = null;
70
+ const anthropicUsage: Record<string, unknown> = {};
71
+ const observe = (body: unknown): void => {
72
+ let next: TAttemptUsage | null;
73
+ if (protocol === "anthropic" && transport === "sse") {
74
+ const event = record(body);
75
+ const delta = event?.type === "message_delta";
76
+ const incoming = record(
77
+ delta
78
+ ? event?.usage
79
+ : event?.type === "message_start"
80
+ ? record(event.message)?.usage
81
+ : null,
82
+ );
83
+ if (incoming === null) return;
84
+ for (const key of [
85
+ "input_tokens",
86
+ "output_tokens",
87
+ "cache_read_input_tokens",
88
+ "cache_creation_input_tokens",
89
+ ] as const) {
90
+ const value = incoming[key];
91
+ if (count(value)) anthropicUsage[key] = value;
92
+ // NaN (not null) so `?? 0` cannot turn an invalid count into zero.
93
+ else if (value !== null && value !== undefined)
94
+ anthropicUsage[key] = Number.NaN;
95
+ }
96
+ if (!delta || !count(incoming.output_tokens)) return;
97
+ next = extract({ usage: anthropicUsage }, protocol);
98
+ } else {
99
+ next = extract(body, protocol);
100
+ }
101
+ if (next !== null) last = next;
102
+ };
103
+ const observeStream = async (
104
+ stream: ReadableStream<Uint8Array>,
105
+ ): Promise<void> => {
106
+ const reader = sseEventStream(stream).getReader();
107
+ try {
108
+ for (;;) {
109
+ const { value, done } = await reader.read();
110
+ if (done) break;
111
+ if (value.kind !== "data") continue;
112
+ try {
113
+ observe(JSON.parse(value.data));
114
+ } catch {
115
+ /* Ignore malformed events. */
116
+ }
117
+ }
118
+ } catch {
119
+ // Complete usage observed before a stream failure remains authoritative.
120
+ } finally {
121
+ reader.releaseLock();
122
+ }
123
+ };
124
+ return { observe, observeStream, usage: () => last };
125
+ };
@@ -0,0 +1,291 @@
1
+ /**
2
+ * Adaptive-thinking normalisation for Anthropic Messages requests.
3
+ *
4
+ * Anthropic's "adaptive thinking" (`thinking: { type: "adaptive" }` +
5
+ * `output_config.effort`) is only honoured by Claude 4.6+ models. Every
6
+ * other model 400s with
7
+ * "adaptive thinking is not supported on this model"
8
+ * the moment any of the adaptive knobs appear in the body — even though
9
+ * those same models support extended thinking via the older
10
+ * `thinking: { type: "enabled", budget_tokens: N }` shape.
11
+ *
12
+ * This module translates the adaptive knobs into the supported shape so
13
+ * a model like haiku-4-5 still gets thinking (we don't strip it). It is
14
+ * the single source of truth for that translation; the pass-through
15
+ * runner and the count_tokens handler both flow through here.
16
+ *
17
+ * Mirror of LiteLLM's `AnthropicConfig._map_reasoning_effort` +
18
+ * `_translate_reasoning_effort_to_anthropic` (see
19
+ * `ref/.litellm/litellm/litellm/llms/anthropic/chat/transformation.py`
20
+ * and `.../experimental_pass_through/messages/transformation.py`).
21
+ * Numeric budgets match LiteLLM's `DEFAULT_REASONING_EFFORT_*_THINKING_BUDGET`
22
+ * constants in `litellm/constants.py` — keep them in sync if those move.
23
+ */
24
+
25
+ import type { TModelCaps } from "@openllmsh/sdk/protocol";
26
+
27
+ /**
28
+ * Effort → thinking budget. Same numbers LiteLLM uses; single source of
29
+ * truth — do NOT inline these values anywhere else.
30
+ */
31
+ export const EFFORT_TO_BUDGET_TOKENS = {
32
+ minimal: 128,
33
+ low: 1024,
34
+ medium: 2048,
35
+ high: 4096,
36
+ xhigh: 8192,
37
+ max: 16384,
38
+ } as const;
39
+ export type TReasoningEffort = keyof typeof EFFORT_TO_BUDGET_TOKENS;
40
+
41
+ /** Map an Anthropic `thinking` budget to the canonical effort enum (flooring to `minimal`). */
42
+ export const budgetToReasoningEffort = (
43
+ budgetTokens: number,
44
+ ): TReasoningEffort => {
45
+ let effort: TReasoningEffort = "minimal";
46
+ for (const [nextEffort, budget] of Object.entries(EFFORT_TO_BUDGET_TOKENS)) {
47
+ const mappedEffort = readEffort(nextEffort);
48
+ if (budgetTokens >= budget && mappedEffort !== undefined) {
49
+ effort = mappedEffort;
50
+ }
51
+ }
52
+ return effort;
53
+ };
54
+
55
+ /**
56
+ * OpenAI-style `reasoning_effort` → Anthropic `output_config.effort`
57
+ * map. Mirrors LiteLLM's `REASONING_EFFORT_TO_OUTPUT_CONFIG_EFFORT` —
58
+ * `minimal` folds into `low` because Anthropic's effort enum starts at
59
+ * `low`; the rest is identity.
60
+ */
61
+ export const REASONING_EFFORT_TO_OUTPUT_CONFIG_EFFORT = {
62
+ minimal: "low",
63
+ low: "low",
64
+ medium: "medium",
65
+ high: "high",
66
+ xhigh: "xhigh",
67
+ max: "max",
68
+ } as const;
69
+
70
+ /** Anthropic's hard floor on `thinking.budget_tokens`. Below this, 400. */
71
+ export const ANTHROPIC_MIN_THINKING_BUDGET = 1024;
72
+
73
+ /**
74
+ * Which thinking modes the resolved model accepts — read from the model
75
+ * card's `caps.thinkingModes`, never from the model NAME.
76
+ *
77
+ * This used to be two regex tables over model ids. That put model
78
+ * knowledge in the one package that must not have any, and it could not
79
+ * be right for long: support is NON-monotonic within a family (4.5
80
+ * extended-only, 4.6 both, 4.7+ adaptive-only), so no prefix encodes it
81
+ * and every new model needed a wire-package edit. The catalog owns the
82
+ * table now; these helpers only interpret it.
83
+ *
84
+ * ABSENT OR EMPTY IS PERMISSIVE on both questions. A custom endpoint, a
85
+ * passthrough hop, or a model the catalog has not caught up with must
86
+ * never be refused locally for a request the upstream might accept —
87
+ * manufacturing a validation error from missing metadata would be a
88
+ * worse failure than the one it prevents.
89
+ */
90
+ const declaredModes = (
91
+ caps: TModelCaps | undefined,
92
+ ): ReadonlyArray<string> | null => {
93
+ const modes = caps?.thinkingModes;
94
+ return modes !== undefined && modes.length > 0 ? modes : null;
95
+ };
96
+
97
+ /** True unless the card explicitly declares modes WITHOUT `adaptive`. */
98
+ export const supportsAdaptiveThinking = (
99
+ caps: TModelCaps | undefined,
100
+ ): boolean => {
101
+ const modes = declaredModes(caps);
102
+ return modes === null || modes.includes("adaptive");
103
+ };
104
+
105
+ const readEffort = (v: unknown): TReasoningEffort | undefined => {
106
+ if (typeof v !== "string") return undefined;
107
+ return v in EFFORT_TO_BUDGET_TOKENS ? (v as TReasoningEffort) : undefined;
108
+ };
109
+
110
+ const isObject = (v: unknown): v is Record<string, unknown> =>
111
+ v !== null && typeof v === "object";
112
+
113
+ /**
114
+ * Does the body's `tool_choice` force tool use? Anthropic accepts
115
+ * `thinking: { type: "adaptive" }` paired with a forced tool_choice (the
116
+ * model decides whether to think before invoking), but REJECTS
117
+ * `thinking: { type: "enabled", budget_tokens: N }` + forced
118
+ * with: "Thinking may not be enabled when tool_choice forces tool use."
119
+ * So when we are about to downgrade adaptive → enabled on a non-
120
+ * adaptive model (haiku, claude-3.*, claude-instant.*), we must also
121
+ * check this and skip the enable. Forced shapes are `{type:"tool",
122
+ * name}` and `{type:"any"}`; `{type:"auto"}` and `{type:"none"}` are
123
+ * not forcing.
124
+ */
125
+ const toolChoiceForcesTool = (tc: unknown): boolean => {
126
+ if (!isObject(tc)) return false;
127
+ return tc.type === "tool" || tc.type === "any";
128
+ };
129
+
130
+ /**
131
+ * Filter out `clear_thinking_*` strategies from
132
+ * `context_management.edits`. Used only on the edge path where thinking
133
+ * has to be dropped entirely — leaving the strategy in 400s with
134
+ * "clear_thinking_20251015 strategy requires `thinking` to be
135
+ * enabled or adaptive"
136
+ * Other strategies (`clear_tool_uses_*`, …) stay. If filtering empties
137
+ * the edits array, the whole `context_management` field is dropped.
138
+ */
139
+ /**
140
+ * Translate an OpenAI-style `reasoning_effort` to the Anthropic
141
+ * extended-thinking shape for the canonical (`/v1/chat/completions` →
142
+ * Anthropic) path. Returns the two fields the caller should spread onto
143
+ * the outbound `AnthropicRequest`, or `null` to mean "no thinking" (the
144
+ * `reasoning_effort: "none"` case, or the model doesn't have room).
145
+ *
146
+ * Mirrors LiteLLM's `_map_reasoning_effort` +
147
+ * `_translate_reasoning_effort_to_anthropic`:
148
+ * - Adaptive-supporting model: `thinking: { type: "adaptive" }` +
149
+ * `output_config: { effort: <mapped> }` so the model uses the
150
+ * stable adaptive-thinking feature and tunes its own budget.
151
+ * - Non-adaptive model: `thinking: { type: "enabled", budget_tokens }`
152
+ * using the canonical effort table, clamped to `max_tokens - 1`.
153
+ * `null` when the resulting budget would fall below
154
+ * `ANTHROPIC_MIN_THINKING_BUDGET` (max_tokens too small).
155
+ */
156
+ export type TReasoningEffortInput = TReasoningEffort | "none";
157
+ export type TAnthropicMappedEffort =
158
+ (typeof REASONING_EFFORT_TO_OUTPUT_CONFIG_EFFORT)[TReasoningEffort];
159
+ export type TThinkingFromEffort = {
160
+ readonly thinking:
161
+ | { readonly type: "enabled"; readonly budget_tokens: number }
162
+ | { readonly type: "adaptive" };
163
+ readonly output_config?: { readonly effort: TAnthropicMappedEffort };
164
+ };
165
+
166
+ export const mapReasoningEffortToAnthropic = (
167
+ effort: TReasoningEffortInput,
168
+ caps: TModelCaps | undefined,
169
+ maxTokens: number | undefined,
170
+ ): TThinkingFromEffort | null => {
171
+ if (effort === "none") return null;
172
+ if (supportsAdaptiveThinking(caps)) {
173
+ return {
174
+ thinking: { type: "adaptive" },
175
+ output_config: {
176
+ effort: REASONING_EFFORT_TO_OUTPUT_CONFIG_EFFORT[effort],
177
+ },
178
+ };
179
+ }
180
+ const targetBudget = EFFORT_TO_BUDGET_TOKENS[effort];
181
+ const ceiling =
182
+ maxTokens === undefined ? Number.POSITIVE_INFINITY : maxTokens - 1;
183
+ const budget = Math.min(targetBudget, ceiling);
184
+ if (budget < ANTHROPIC_MIN_THINKING_BUDGET) return null;
185
+ return { thinking: { type: "enabled", budget_tokens: budget } };
186
+ };
187
+
188
+ const filterClearThinking = (cm: unknown): unknown | undefined => {
189
+ if (!isObject(cm)) return cm;
190
+ const edits = cm.edits;
191
+ if (!Array.isArray(edits)) return cm;
192
+ const filtered = edits.filter((e) => {
193
+ if (!isObject(e)) return true;
194
+ const t = e.type;
195
+ return typeof t !== "string" || !t.startsWith("clear_thinking");
196
+ });
197
+ if (filtered.length === edits.length) return cm;
198
+ if (filtered.length === 0) return undefined;
199
+ return { ...cm, edits: filtered };
200
+ };
201
+
202
+ /**
203
+ * Normalise the adaptive-thinking knobs on an Anthropic Messages
204
+ * request body so it works against any chat model.
205
+ *
206
+ * On models that support adaptive thinking natively → no-op.
207
+ * On every other model:
208
+ * - Replace `thinking: { type: "adaptive" }` (or a bare
209
+ * `reasoning_effort` / `output_config.effort` / top-level `effort`)
210
+ * with `thinking: { type: "enabled", budget_tokens: N }`, where N
211
+ * comes from the canonical effort table (default `medium` when
212
+ * adaptive was requested without an explicit effort level). The
213
+ * budget is clamped to `max_tokens - 1` so Anthropic's
214
+ * `budget_tokens < max_tokens` invariant holds.
215
+ * - Drop `output_config`, `reasoning_effort`, top-level `effort` —
216
+ * they are adaptive-only and have no analogue under explicit
217
+ * `budget_tokens`.
218
+ * - `thinking: { type: "enabled" }` and unknown other shapes pass
219
+ * through unchanged.
220
+ * - If `max_tokens` is too small to fit `ANTHROPIC_MIN_THINKING_BUDGET`,
221
+ * thinking is dropped and any `clear_thinking_*` strategy in
222
+ * `context_management.edits` is filtered out (else Anthropic
223
+ * rejects the orphaned strategy).
224
+ *
225
+ * Returns the input untouched when no rewrite is needed, and a shallow
226
+ * copy otherwise. Caller-provided `tools`, `system`, `metadata`,
227
+ * `cache_control`, beta headers, etc. are never inspected.
228
+ */
229
+ export const normaliseAdaptiveThinking = (
230
+ body: unknown,
231
+ caps: TModelCaps | undefined,
232
+ ): unknown => {
233
+ if (!isObject(body)) return body;
234
+ if (supportsAdaptiveThinking(caps)) return body;
235
+
236
+ const {
237
+ output_config,
238
+ reasoning_effort,
239
+ effort,
240
+ thinking,
241
+ context_management,
242
+ ...rest
243
+ } = body;
244
+
245
+ const thinkingIsAdaptive = isObject(thinking) && thinking.type === "adaptive";
246
+ const wantsThinking =
247
+ thinkingIsAdaptive ||
248
+ typeof reasoning_effort === "string" ||
249
+ typeof effort === "string" ||
250
+ (isObject(output_config) && typeof output_config.effort === "string");
251
+
252
+ // No adaptive knobs to translate — nothing to rewrite.
253
+ if (!wantsThinking) return body;
254
+
255
+ const effortLevel: TReasoningEffort =
256
+ (isObject(output_config) ? readEffort(output_config.effort) : undefined) ??
257
+ readEffort(reasoning_effort) ??
258
+ readEffort(effort) ??
259
+ "medium";
260
+
261
+ const targetBudget = EFFORT_TO_BUDGET_TOKENS[effortLevel];
262
+ const maxTokens =
263
+ typeof body.max_tokens === "number" &&
264
+ Number.isFinite(body.max_tokens) &&
265
+ body.max_tokens > 0
266
+ ? Math.floor(body.max_tokens)
267
+ : undefined;
268
+ const ceiling =
269
+ maxTokens === undefined ? Number.POSITIVE_INFINITY : maxTokens - 1;
270
+ const budget = Math.min(targetBudget, ceiling);
271
+
272
+ // Anthropic rejects `thinking: enabled` (the downgrade output) when
273
+ // tool_choice forces tool use. Drop thinking entirely in that case,
274
+ // matching the max_tokens-too-small edge path (filter orphaned
275
+ // `clear_thinking_*` strategies; the forced tool still executes,
276
+ // the model just doesn't get a thinking preamble). Adaptive remains
277
+ // legal on adaptive-supporting models, which short-circuit above.
278
+ const forcedTool = toolChoiceForcesTool(body.tool_choice);
279
+
280
+ const out: Record<string, unknown> = { ...rest };
281
+ if (budget >= ANTHROPIC_MIN_THINKING_BUDGET && !forcedTool) {
282
+ out.thinking = { type: "enabled", budget_tokens: budget };
283
+ if (context_management !== undefined) {
284
+ out.context_management = context_management;
285
+ }
286
+ } else if (context_management !== undefined) {
287
+ const filtered = filterClearThinking(context_management);
288
+ if (filtered !== undefined) out.context_management = filtered;
289
+ }
290
+ return out;
291
+ };
@@ -0,0 +1,165 @@
1
+ import type {
2
+ TAnthropicContentBlock,
3
+ TAnthropicRequest,
4
+ } from "@openllmsh/sdk/protocol";
5
+
6
+ /**
7
+ * Anthropic `anthropic-beta` header policy.
8
+ *
9
+ * We forward whatever beta tokens the inbound client sent, with one
10
+ * tiny exception: a denylist of tokens we know cause harm (auth path
11
+ * mismatches, schema conflicts, …). Anything else passes through
12
+ * verbatim.
13
+ *
14
+ * Why deny-list, not allow-list:
15
+ *
16
+ * Anthropic ships new betas on every Claude Code release. The
17
+ * previous allow-list approach was mirrored from LiteLLM's
18
+ * `anthropic_beta_headers_config.json`, but LiteLLM lags Claude
19
+ * Code by weeks-to-months. Every time Anthropic shipped a new beta
20
+ * (`mid-conversation-system-2026-04-07` was the smoking gun for the
21
+ * Opus 4.8 "cuts off mid-task" symptom we hunted in the
22
+ * `claude-harness` diagnostic — opus 4.8 needs that beta to handle
23
+ * the mid-conversation system updates Claude Code v2.1.157+ sends),
24
+ * we silently dropped it for every passthrough request until a
25
+ * human noticed and PR'd it in. That's a regression treadmill:
26
+ * tracker-bug-fix-rinse-repeat. The cost of forwarding an unknown
27
+ * beta is at worst a 400 from Anthropic ("unknown beta"), which the
28
+ * fallback chain already treats as a transient error and walks past;
29
+ * the cost of dropping a real beta is silent quality degradation.
30
+ *
31
+ * Denylist + passthrough is forward-compatible: any beta shipped by
32
+ * any future Claude Code build (or any other Anthropic-format
33
+ * client) flows through unchanged. The denylist only carries tokens
34
+ * we have a concrete reason to block — historical and concrete, not
35
+ * speculative.
36
+ *
37
+ * Today the denylist is empty. If a beta starts breaking the proxy,
38
+ * add it here with a comment naming the failure mode.
39
+ */
40
+ const ANTHROPIC_BLOCKED_BETAS: ReadonlySet<string> = new Set<string>();
41
+
42
+ /** Beta required for the subscription-OAuth (`sk-ant-oat01-…`) path. */
43
+ export const ANTHROPIC_OAUTH_BETA = "oauth-2025-04-20";
44
+
45
+ /**
46
+ * Anthropic SUBSCRIPTION / OAuth credential matcher — the `claude setup-token`
47
+ * and "Sign in with Claude" tokens. These ride `Authorization: Bearer` + the
48
+ * oauth beta header, NOT `x-api-key` (which is for Console keys `sk-ant-api03-`).
49
+ * Anthropic has shipped TWO prefixes over time: the original `sk-ant-oat01-` and
50
+ * the current `sk-ant-at01-` (Claude CLI ≥ 2.1.x) — match BOTH, never a Console
51
+ * key. Single source of truth for the daemon (setup-token capture + validation)
52
+ * and the cloud (auth-header split, terms gate, count-tokens).
53
+ */
54
+ export const ANTHROPIC_OAUTH_TOKEN_RE = /sk-ant-o?at01-[A-Za-z0-9_-]+/;
55
+ export const isAnthropicOAuthToken = (token: string): boolean =>
56
+ /^sk-ant-o?at01-/.test(token);
57
+
58
+ const splitBetaHeader = (raw: string | null): string[] => {
59
+ if (raw === null) return [];
60
+ return raw
61
+ .split(",")
62
+ .map((t) => t.trim())
63
+ .filter((t) => t.length > 0);
64
+ };
65
+
66
+ /** Beta required for Files-API (`file_id`) sources on image/document blocks. */
67
+ export const ANTHROPIC_FILES_API_BETA = "files-api-2025-04-14";
68
+
69
+ /** One content block's direct Files-API requirement. */
70
+ const blockUsesFilesApi = (block: TAnthropicContentBlock): boolean =>
71
+ ((block.type === "image" || block.type === "document") &&
72
+ block.source.type === "file") ||
73
+ block.type === "container_upload";
74
+
75
+ /**
76
+ * True when any content block references an uploaded file (`source:
77
+ * {type:"file"}` on an image/document, or a `container_upload` block) —
78
+ * Anthropic only honours those under the Files API beta. `tool_result.content`
79
+ * can contain one nested schema-supported image/document block; inspect that
80
+ * single level too. (Its nested search_result path is text-only today, so an
81
+ * arbitrary-depth recursion would be unreachable complexity.)
82
+ */
83
+ const usesFilesApi = (req: TAnthropicRequest): boolean => {
84
+ for (const m of req.messages) {
85
+ if (typeof m.content === "string") continue;
86
+ for (const block of m.content) {
87
+ if (blockUsesFilesApi(block)) return true;
88
+ if (block.type === "tool_result" && typeof block.content !== "string") {
89
+ if (block.content.some(blockUsesFilesApi)) return true;
90
+ }
91
+ }
92
+ }
93
+ return false;
94
+ };
95
+
96
+ /**
97
+ * Derive the betas implied by the request body itself, independent of
98
+ * what the client sent. Our `TAnthropicRequest` schema is intentionally
99
+ * minimal, so the safe inferences today are the native server-side
100
+ * `web_search`/`web_fetch` tools and Files-API (`file_id`) content
101
+ * sources (Anthropic requires the matching beta for those to be
102
+ * honoured). Kept as a single chokepoint so future schema fields
103
+ * (context_management, output_format, speed) extend here rather than
104
+ * at every call site — the LiteLLM equivalent is
105
+ * `anthropic_beta_headers_manager.get_anthropic_beta_headers`.
106
+ */
107
+ const derivedBetasFor = (req: TAnthropicRequest): string[] => {
108
+ const out: string[] = [];
109
+ const tools = req.tools ?? [];
110
+ for (const tool of tools) {
111
+ const t = tool.type ?? "";
112
+ if (t.startsWith("web_search")) out.push("web-search-2025-03-05");
113
+ if (t.startsWith("web_fetch")) out.push("web-fetch-2025-09-10");
114
+ }
115
+ if (usesFilesApi(req)) out.push(ANTHROPIC_FILES_API_BETA);
116
+ return out;
117
+ };
118
+
119
+ export type TBetaHeaderInput = {
120
+ /** Raw inbound `anthropic-beta` header value (comma-separated) or null. */
121
+ readonly inboundBeta: string | null;
122
+ /** The Anthropic request being forwarded (for body-derived betas). */
123
+ readonly request: TAnthropicRequest;
124
+ /** True when the credential is a subscription OAuth token. */
125
+ readonly isOAuth: boolean;
126
+ /**
127
+ * Extra betas the caller derived from a NON-Anthropic-shaped inbound
128
+ * body (cross-wire hops, where `request` is an empty stand-in) —
129
+ * e.g. the Files-API beta when a canonical `file_id` part will be
130
+ * encoded to an Anthropic file source.
131
+ */
132
+ readonly extraBetas?: ReadonlyArray<string>;
133
+ };
134
+
135
+ /**
136
+ * Compute the final, de-duplicated `anthropic-beta` token list to send
137
+ * upstream. Returns `undefined` when there is nothing to send so the
138
+ * caller can omit the header entirely.
139
+ *
140
+ * Order of precedence (first occurrence wins for dedupe, order is
141
+ * otherwise stable): inbound betas (deny-list filtered) → body-derived
142
+ * betas → the OAuth beta when authing via a subscription token.
143
+ *
144
+ * Tokens the client sends that aren't in `ANTHROPIC_BLOCKED_BETAS`
145
+ * forward verbatim — see the module header for the rationale.
146
+ */
147
+ export const deriveAnthropicBetaHeader = (
148
+ input: TBetaHeaderInput,
149
+ ): string | undefined => {
150
+ const seen = new Set<string>();
151
+ const out: string[] = [];
152
+ const push = (token: string): void => {
153
+ if (token.length === 0 || seen.has(token)) return;
154
+ if (ANTHROPIC_BLOCKED_BETAS.has(token)) return;
155
+ seen.add(token);
156
+ out.push(token);
157
+ };
158
+
159
+ for (const token of splitBetaHeader(input.inboundBeta)) push(token);
160
+ for (const token of derivedBetasFor(input.request)) push(token);
161
+ for (const token of input.extraBetas ?? []) push(token);
162
+ if (input.isOAuth) push(ANTHROPIC_OAUTH_BETA);
163
+
164
+ return out.length > 0 ? out.join(",") : undefined;
165
+ };
@@ -0,0 +1,10 @@
1
+ // Pure Anthropic wire transforms, extracted from `@openllm/core`'s
2
+ // provider so the coreless daemon can build the Anthropic upstream
3
+ // (coreless proposal §9(a)). The DI-bound spec assembly + auth headers +
4
+ // terms gate stay in `@openllm/core/providers/anthropic`.
5
+ export * from "./adaptive-thinking";
6
+ export * from "./beta-headers";
7
+ export * from "./request";
8
+ export * from "./response";
9
+ export * from "./streaming";
10
+ export * from "./web-search-domains";