@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
package/vault/team.ts ADDED
@@ -0,0 +1,355 @@
1
+ /**
2
+ * User-held team encryption (docs/plan/2026-10-26-teams.md §9).
3
+ *
4
+ * Key graph (one-way only — a team key never opens personal material):
5
+ *
6
+ * personal DEK ──seal──► team_members.sealed_team_key (T)
7
+ * invite secret ─HKDF(auth info)──► auth (base64url) ──sha256──► token_hash
8
+ * invite secret ─HKDF(wrap info)──► wrap key ──seal──► team_invites.wrapped_team_key (T)
9
+ * T ─HKDF──► K_mem ──seal──► embeddings.content (team rows)
10
+ * T ─HKDF──► K_cred ──seal──► team share credential (inner)
11
+ * ▲
12
+ * server K_srv wraps that as outer layer
13
+ *
14
+ * Domain separation:
15
+ * - invite auth info: {@link TEAM_INVITE_AUTH_HKDF_INFO} (browser-only → server)
16
+ * - invite wrap info: {@link TEAM_INVITE_HKDF_INFO} (browser-only; never sent)
17
+ * - memory content info: {@link TEAM_MEM_HKDF_INFO}
18
+ * - credential content info: {@link TEAM_CRED_HKDF_INFO}
19
+ *
20
+ * The raw fragment secret and the wrap key never leave the browser. Only the
21
+ * derived `auth` value is posted; the server stores `sha256(auth)` as
22
+ * `token_hash` and cannot unwrap T from anything it sees or stores.
23
+ *
24
+ * API-key share credentials use a two-layer envelope: the browser seals the
25
+ * plaintext under K_cred with AAD binding (team_id, share_id, provider,
26
+ * key_version); the server then wraps that ciphertext under a per-share
27
+ * K_srv (HMAC of the host signing secret). Neither layer alone opens the
28
+ * credential. Callers `zero()` T, K_mem, and K_cred after use.
29
+ *
30
+ * Personal-row paths must never receive T, K_mem, or K_cred; team-row paths
31
+ * must never receive the personal DEK.
32
+ */
33
+
34
+ import {
35
+ aesGcmDecrypt,
36
+ aesGcmEncrypt,
37
+ fromB64,
38
+ hkdfSha256,
39
+ randomBytes,
40
+ toB64,
41
+ toB64Url,
42
+ zero,
43
+ } from "./crypto";
44
+ import { openVaultBytes, sealVaultBytes, VAULT_ITEM_TAG } from "./vault-item";
45
+ import { joinSealed, splitSealed } from "./wrapped";
46
+
47
+ /** ASCII `k1:` — same tag vault-item puts on a tree-sealed nonce string. */
48
+ const TEAM_KEY_TREE_TAG = new TextEncoder().encode(VAULT_ITEM_TAG);
49
+
50
+ const startsWithTeamKeyTreeTag = (sealed: Uint8Array): boolean => {
51
+ if (sealed.length < TEAM_KEY_TREE_TAG.length + NONCE_BYTES + 16) {
52
+ return false;
53
+ }
54
+ for (let i = 0; i < TEAM_KEY_TREE_TAG.length; i++) {
55
+ if (sealed[i] !== TEAM_KEY_TREE_TAG[i]) return false;
56
+ }
57
+ return true;
58
+ };
59
+
60
+ const NONCE_BYTES = 12;
61
+ const TEAM_KEY_BYTES = 32;
62
+
63
+ /** HKDF info for the invite-fragment wrap of T (browser-only). */
64
+ export const TEAM_INVITE_HKDF_INFO = "openllm-team-invite-v1";
65
+
66
+ /**
67
+ * HKDF info for the invite auth token posted to the server.
68
+ * Distinct from {@link TEAM_INVITE_HKDF_INFO} so seeing `auth` never yields
69
+ * the wrap key.
70
+ */
71
+ export const TEAM_INVITE_AUTH_HKDF_INFO = "openllm-team-invite-auth-v1";
72
+
73
+ /** HKDF info for the team-memory content key derived from T. */
74
+ export const TEAM_MEM_HKDF_INFO = "openllm-team-mem-v1";
75
+
76
+ /**
77
+ * HKDF info for the team-credential content key derived from T.
78
+ * Distinct from {@link TEAM_MEM_HKDF_INFO} so K_mem never opens share
79
+ * credentials and K_cred never opens team memories.
80
+ */
81
+ export const TEAM_CRED_HKDF_INFO = "openllm-team-cred-v1";
82
+
83
+ /** Fresh random team root key T. Caller owns and must eventually `zero()` it. */
84
+ export const generateTeamKey = (): Uint8Array => randomBytes(TEAM_KEY_BYTES);
85
+
86
+ /**
87
+ * Seal T under the personal vault's `teams` branch → bytes for
88
+ * `team_members.sealed_team_key`.
89
+ *
90
+ * Wire layout (single bytea, no separate nonce column):
91
+ * - tree (current): `ASCII "k1:" || nonce(12) || ct` — AES-GCM under the
92
+ * `teams` branch key derived from the root (DEK today);
93
+ * - legacy: `nonce(12) || ct` — AES-GCM under the root DEK directly.
94
+ * {@link openTeamKey} reads both; a legacy blob whose first three bytes
95
+ * happen to match the tag is tried as tree first and falls back to the
96
+ * DEK on auth failure.
97
+ */
98
+ export const sealTeamKey = async (
99
+ dek: Uint8Array,
100
+ teamKey: Uint8Array,
101
+ ): Promise<Uint8Array> => {
102
+ const item = await sealVaultBytes(dek, { branch: "teams" }, teamKey);
103
+ const nonce = fromB64(item.nonce.slice(VAULT_ITEM_TAG.length));
104
+ const ct = fromB64(item.ciphertext);
105
+ const out = new Uint8Array(
106
+ TEAM_KEY_TREE_TAG.length + nonce.length + ct.length,
107
+ );
108
+ out.set(TEAM_KEY_TREE_TAG, 0);
109
+ out.set(nonce, TEAM_KEY_TREE_TAG.length);
110
+ out.set(ct, TEAM_KEY_TREE_TAG.length + nonce.length);
111
+ return out;
112
+ };
113
+
114
+ /** Open a `sealed_team_key` blob under the personal root key. Caller zeros T. */
115
+ export const openTeamKey = async (
116
+ dek: Uint8Array,
117
+ sealed: Uint8Array,
118
+ ): Promise<Uint8Array> => {
119
+ if (startsWithTeamKeyTreeTag(sealed)) {
120
+ const nonce = sealed.slice(
121
+ TEAM_KEY_TREE_TAG.length,
122
+ TEAM_KEY_TREE_TAG.length + NONCE_BYTES,
123
+ );
124
+ const ciphertext = sealed.slice(TEAM_KEY_TREE_TAG.length + NONCE_BYTES);
125
+ try {
126
+ return await openVaultBytes(
127
+ dek,
128
+ { branch: "teams" },
129
+ {
130
+ nonce: `${VAULT_ITEM_TAG}${toB64(nonce)}`,
131
+ ciphertext: toB64(ciphertext),
132
+ },
133
+ );
134
+ } catch {
135
+ // Tag collision on a legacy blob — fall through.
136
+ }
137
+ }
138
+ const { nonce, ciphertext } = splitSealed(sealed, "sealed_team_key");
139
+ return aesGcmDecrypt(dek, nonce, ciphertext);
140
+ };
141
+
142
+ const inviteSalt = (inviteId: string): Uint8Array =>
143
+ new TextEncoder().encode(inviteId);
144
+
145
+ /**
146
+ * Derive the invite auth token posted to the server:
147
+ * HKDF(secret, salt=invite_id, info=openllm-team-invite-auth-v1) → base64url.
148
+ * The raw fragment secret never leaves the browser.
149
+ */
150
+ export const deriveTeamInviteAuth = async (
151
+ inviteSecret: string,
152
+ inviteId: string,
153
+ ): Promise<string> => {
154
+ const ikm = new TextEncoder().encode(inviteSecret);
155
+ try {
156
+ const authBytes = await hkdfSha256(
157
+ ikm,
158
+ inviteSalt(inviteId),
159
+ TEAM_INVITE_AUTH_HKDF_INFO,
160
+ TEAM_KEY_BYTES,
161
+ );
162
+ try {
163
+ return toB64Url(authBytes);
164
+ } finally {
165
+ zero(authBytes);
166
+ }
167
+ } finally {
168
+ zero(ikm);
169
+ }
170
+ };
171
+
172
+ /**
173
+ * Derive the invite wrap key: HKDF(invite_secret, salt=invite_id,
174
+ * info=openllm-team-invite-v1). Caller zeros the returned key. Never send
175
+ * this key or the raw secret to the server.
176
+ */
177
+ export const deriveTeamInviteWrapKey = async (
178
+ inviteSecret: string,
179
+ inviteId: string,
180
+ ): Promise<Uint8Array> => {
181
+ const ikm = new TextEncoder().encode(inviteSecret);
182
+ try {
183
+ return await hkdfSha256(
184
+ ikm,
185
+ inviteSalt(inviteId),
186
+ TEAM_INVITE_HKDF_INFO,
187
+ TEAM_KEY_BYTES,
188
+ );
189
+ } finally {
190
+ zero(ikm);
191
+ }
192
+ };
193
+
194
+ /** Wrap T under the invite secret for storage on `team_invites`. */
195
+ export const wrapTeamKeyForInvite = async (
196
+ inviteSecret: string,
197
+ inviteId: string,
198
+ teamKey: Uint8Array,
199
+ ): Promise<Uint8Array> => {
200
+ const wrapKey = await deriveTeamInviteWrapKey(inviteSecret, inviteId);
201
+ try {
202
+ const nonce = randomBytes(NONCE_BYTES);
203
+ const ct = await aesGcmEncrypt(wrapKey, nonce, teamKey);
204
+ return joinSealed(nonce, ct);
205
+ } finally {
206
+ zero(wrapKey);
207
+ }
208
+ };
209
+
210
+ /** Unwrap T from an invite's `wrapped_team_key`. Caller zeros T. */
211
+ export const unwrapTeamKeyFromInvite = async (
212
+ inviteSecret: string,
213
+ inviteId: string,
214
+ wrapped: Uint8Array,
215
+ ): Promise<Uint8Array> => {
216
+ const wrapKey = await deriveTeamInviteWrapKey(inviteSecret, inviteId);
217
+ try {
218
+ const { nonce, ciphertext } = splitSealed(wrapped, "wrapped_team_key");
219
+ return await aesGcmDecrypt(wrapKey, nonce, ciphertext);
220
+ } finally {
221
+ zero(wrapKey);
222
+ }
223
+ };
224
+
225
+ /**
226
+ * Derive K_mem = HKDF(T, salt=empty, info=openllm-team-mem-v1).
227
+ * Memory code receives ONLY this key for team rows. Caller zeros it.
228
+ */
229
+ export const deriveTeamMemKey = async (
230
+ teamKey: Uint8Array,
231
+ ): Promise<Uint8Array> =>
232
+ hkdfSha256(teamKey, new Uint8Array(0), TEAM_MEM_HKDF_INFO, TEAM_KEY_BYTES);
233
+
234
+ export type TEncryptedTeamMemory = {
235
+ readonly ciphertext: string;
236
+ readonly nonce: string;
237
+ };
238
+
239
+ /** Encrypt memory content under K_mem (same wire shape as credentials). */
240
+ export const encryptTeamMemory = async (
241
+ memKey: Uint8Array,
242
+ plaintext: string,
243
+ ): Promise<TEncryptedTeamMemory> => {
244
+ const nonce = randomBytes(NONCE_BYTES);
245
+ const ct = await aesGcmEncrypt(
246
+ memKey,
247
+ nonce,
248
+ new TextEncoder().encode(plaintext),
249
+ );
250
+ return { ciphertext: toB64(ct), nonce: toB64(nonce) };
251
+ };
252
+
253
+ /** Decrypt a team memory row under K_mem. */
254
+ export const decryptTeamMemory = async (
255
+ memKey: Uint8Array,
256
+ ciphertextB64: string,
257
+ nonceB64: string,
258
+ ): Promise<string> => {
259
+ const pt = await aesGcmDecrypt(
260
+ memKey,
261
+ fromB64(nonceB64),
262
+ fromB64(ciphertextB64),
263
+ );
264
+ return new TextDecoder().decode(pt);
265
+ };
266
+
267
+ /**
268
+ * Derive K_cred = HKDF(T, salt=empty, info=openllm-team-cred-v1).
269
+ * Credential-share code receives ONLY this key for the inner envelope.
270
+ * Caller zeros it.
271
+ */
272
+ export const deriveTeamCredKey = async (
273
+ teamKey: Uint8Array,
274
+ ): Promise<Uint8Array> =>
275
+ hkdfSha256(teamKey, new Uint8Array(0), TEAM_CRED_HKDF_INFO, TEAM_KEY_BYTES);
276
+
277
+ /**
278
+ * Public facts the inner credential ciphertext is bound to as AES-GCM AAD.
279
+ * A row relabelled under another team/share/provider/key_version fails its
280
+ * auth tag instead of handing back a key for the wrong share.
281
+ */
282
+ export type TTeamCredBinding = {
283
+ readonly teamId: string;
284
+ readonly shareId: string;
285
+ readonly provider: string;
286
+ readonly keyVersion: number;
287
+ };
288
+
289
+ const teamCredAad = (binding: TTeamCredBinding): Uint8Array =>
290
+ new TextEncoder().encode(
291
+ `${TEAM_CRED_HKDF_INFO}|${binding.teamId}|${binding.shareId}|${binding.provider}|${binding.keyVersion}`,
292
+ );
293
+
294
+ export type TEncryptedTeamCredential = {
295
+ readonly ciphertext: string;
296
+ readonly nonce: string;
297
+ };
298
+
299
+ /**
300
+ * Encrypt a credential plaintext under K_cred with AAD binding.
301
+ * Returns the same wire shape as personal credentials (`ciphertext`/`nonce`
302
+ * base64). The browser posts this as the inner layer; the server wraps it
303
+ * under K_srv before storage.
304
+ */
305
+ export const encryptTeamCredential = async (
306
+ credKey: Uint8Array,
307
+ plaintext: string,
308
+ binding: TTeamCredBinding,
309
+ ): Promise<TEncryptedTeamCredential> => {
310
+ const nonce = randomBytes(NONCE_BYTES);
311
+ const ct = await aesGcmEncrypt(
312
+ credKey,
313
+ nonce,
314
+ new TextEncoder().encode(plaintext),
315
+ teamCredAad(binding),
316
+ );
317
+ return { ciphertext: toB64(ct), nonce: toB64(nonce) };
318
+ };
319
+
320
+ /** Decrypt an inner team-credential layer under K_cred + the same AAD. */
321
+ export const decryptTeamCredential = async (
322
+ credKey: Uint8Array,
323
+ ciphertextB64: string,
324
+ nonceB64: string,
325
+ binding: TTeamCredBinding,
326
+ ): Promise<string> => {
327
+ const pt = await aesGcmDecrypt(
328
+ credKey,
329
+ fromB64(nonceB64),
330
+ fromB64(ciphertextB64),
331
+ teamCredAad(binding),
332
+ );
333
+ return new TextDecoder().decode(pt);
334
+ };
335
+
336
+ /**
337
+ * Pack an inner `{ciphertext, nonce}` pair into `IV || ct` bytes for the
338
+ * server's outer wrap. Inverse of {@link unpackTeamCredInner}.
339
+ */
340
+ export const packTeamCredInner = (
341
+ sealed: TEncryptedTeamCredential,
342
+ ): Uint8Array => joinSealed(fromB64(sealed.nonce), fromB64(sealed.ciphertext));
343
+
344
+ /** Unpack `IV || ct` bytes back to the inner `{ciphertext, nonce}` pair. */
345
+ export const unpackTeamCredInner = (
346
+ packed: Uint8Array,
347
+ ): TEncryptedTeamCredential => {
348
+ const { nonce, ciphertext } = splitSealed(packed, "team_cred_inner");
349
+ return { ciphertext: toB64(ciphertext), nonce: toB64(nonce) };
350
+ };
351
+
352
+ /** Browser/API helpers: b64 ↔ sealed bytes. */
353
+ export const sealedTeamKeyToB64 = (sealed: Uint8Array): string => toB64(sealed);
354
+
355
+ export const sealedTeamKeyFromB64 = (b64: string): Uint8Array => fromB64(b64);
@@ -0,0 +1,131 @@
1
+ import {
2
+ aesGcmDecrypt,
3
+ aesGcmEncrypt,
4
+ fromB64,
5
+ randomBytes,
6
+ toB64,
7
+ zero,
8
+ } from "./crypto";
9
+ import type { TVaultBranch } from "./seed";
10
+ import { deriveBranchKey, deriveLeafKey } from "./seed";
11
+
12
+ /**
13
+ * Vault data encrypted under the key tree (docs/plans/seed-only-vault.md §2a).
14
+ *
15
+ * Every item says which key it was sealed under, in its nonce:
16
+ *
17
+ * - `k1:<base64 nonce>` — under the branch key, or the leaf key when the
18
+ * item names a leaf, derived from the vault root key;
19
+ * - a bare `<base64 nonce>` — legacy, sealed directly under the root key.
20
+ * Read-only, and only until the 1-week sunset; nothing writes it any more.
21
+ *
22
+ * Self-describing on purpose: a caller says only WHAT it is reading
23
+ * ({@link TVaultItemRef}), never which format the user is in, so items of both
24
+ * formats can sit side by side (mid-migration, or a server write racing a
25
+ * browser one) and every holder of the root key reads them all — by default
26
+ * every key reads what any other wrote.
27
+ */
28
+
29
+ /** Which part of the tree an item belongs to. */
30
+ export type TVaultItemRef = {
31
+ readonly branch: TVaultBranch;
32
+ /** Stable id of the item's leaf (session id, memory scope); none = branch key. */
33
+ readonly leaf?: string;
34
+ };
35
+
36
+ export type TEncryptedVaultItem = {
37
+ readonly ciphertext: string;
38
+ readonly nonce: string;
39
+ };
40
+
41
+ /** Nonce prefix of an item sealed under the key tree. */
42
+ export const VAULT_ITEM_TAG = "k1:";
43
+
44
+ const NONCE_BYTES = 12;
45
+
46
+ const itemKey = async (
47
+ rootKey: Uint8Array,
48
+ ref: TVaultItemRef,
49
+ ): Promise<Uint8Array> => {
50
+ const branch = await deriveBranchKey(rootKey, ref.branch);
51
+ if (ref.leaf === undefined) return branch;
52
+ try {
53
+ return await deriveLeafKey(branch, ref.branch, ref.leaf);
54
+ } finally {
55
+ zero(branch);
56
+ }
57
+ };
58
+
59
+ /** `true` when an item was sealed under the key tree, not the bare root. */
60
+ export const isTreeSealed = (nonce: string): boolean =>
61
+ nonce.startsWith(VAULT_ITEM_TAG);
62
+
63
+ /**
64
+ * Seal bytes for the vault at `ref`, from the root key. Never zeroes its
65
+ * inputs. Always the tree format.
66
+ */
67
+ export const sealVaultBytes = async (
68
+ rootKey: Uint8Array,
69
+ ref: TVaultItemRef,
70
+ plaintext: Uint8Array,
71
+ ): Promise<TEncryptedVaultItem> => {
72
+ const key = await itemKey(rootKey, ref);
73
+ try {
74
+ const nonce = randomBytes(NONCE_BYTES);
75
+ const ct = await aesGcmEncrypt(key, nonce, plaintext);
76
+ return { ciphertext: toB64(ct), nonce: `${VAULT_ITEM_TAG}${toB64(nonce)}` };
77
+ } finally {
78
+ zero(key);
79
+ }
80
+ };
81
+
82
+ /**
83
+ * Open an item of either format from the root key. Throws on a wrong key or
84
+ * a tampered item (auth tag). The caller zeroes the result if it is secret.
85
+ */
86
+ export const openVaultBytes = async (
87
+ rootKey: Uint8Array,
88
+ ref: TVaultItemRef,
89
+ item: TEncryptedVaultItem,
90
+ ): Promise<Uint8Array> => {
91
+ if (!isTreeSealed(item.nonce)) {
92
+ // Legacy: sealed directly under the root key (removed at the sunset).
93
+ return aesGcmDecrypt(
94
+ rootKey,
95
+ fromB64(item.nonce),
96
+ fromB64(item.ciphertext),
97
+ );
98
+ }
99
+ const key = await itemKey(rootKey, ref);
100
+ try {
101
+ return await aesGcmDecrypt(
102
+ key,
103
+ fromB64(item.nonce.slice(VAULT_ITEM_TAG.length)),
104
+ fromB64(item.ciphertext),
105
+ );
106
+ } finally {
107
+ zero(key);
108
+ }
109
+ };
110
+
111
+ /** {@link sealVaultBytes} for a string. */
112
+ export const sealVaultText = (
113
+ rootKey: Uint8Array,
114
+ ref: TVaultItemRef,
115
+ plaintext: string,
116
+ ): Promise<TEncryptedVaultItem> =>
117
+ sealVaultBytes(rootKey, ref, new TextEncoder().encode(plaintext));
118
+
119
+ /** {@link openVaultBytes} for a string. */
120
+ export const openVaultText = async (
121
+ rootKey: Uint8Array,
122
+ ref: TVaultItemRef,
123
+ item: TEncryptedVaultItem,
124
+ ): Promise<string> => {
125
+ const bytes = await openVaultBytes(rootKey, ref, item);
126
+ try {
127
+ return new TextDecoder().decode(bytes);
128
+ } finally {
129
+ zero(bytes);
130
+ }
131
+ };
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Digest of a vault row's ciphertext fields for the one-time seed-vault
3
+ * migration (docs/plans/seed-only-vault.md §4). The browser and the server
4
+ * must compute the same value: SHA-256 over the fields joined by `\n`
5
+ * (null → empty string), returned as standard base64.
6
+ */
7
+
8
+ import { toB64 } from "./crypto";
9
+
10
+ const subtle = (): SubtleCrypto => {
11
+ const s = globalThis.crypto?.subtle;
12
+ if (s === undefined) {
13
+ throw new Error("WebCrypto SubtleCrypto is not available in this runtime");
14
+ }
15
+ return s;
16
+ };
17
+
18
+ /**
19
+ * SHA-256 of `fields` joined by `"\n"` (`null` → `""`), as standard base64.
20
+ */
21
+ export const vaultMigrateDigest = async (
22
+ fields: ReadonlyArray<string | null | undefined>,
23
+ ): Promise<string> => {
24
+ const joined = fields.map((f) => (f == null ? "" : f)).join("\n");
25
+ const digest = new Uint8Array(
26
+ await subtle().digest("SHA-256", new TextEncoder().encode(joined)),
27
+ );
28
+ return toB64(digest);
29
+ };
@@ -0,0 +1,77 @@
1
+ import type { TKdfVersion } from "./argon2";
2
+ import { CURRENT_KDF_VERSION, LEGACY_KDF_VERSION } from "./argon2";
3
+ import { fromB64, toB64 } from "./crypto";
4
+
5
+ /**
6
+ * The on-disk shape of everything this package seals with AES-GCM.
7
+ *
8
+ * One layout, written down once, because it was previously written down three
9
+ * times (`derive.ts`, `pin.ts`, `api-key.ts`) with three copies of the same
10
+ * off-by-one-able join/split and three slightly different error strings.
11
+ */
12
+
13
+ const NONCE_BYTES = 12;
14
+ const GCM_TAG_BYTES = 16;
15
+
16
+ /** `IV (12) || ciphertext+tag`. */
17
+ export const joinSealed = (
18
+ nonce: Uint8Array,
19
+ ciphertext: Uint8Array,
20
+ ): Uint8Array => {
21
+ const out = new Uint8Array(nonce.length + ciphertext.length);
22
+ out.set(nonce, 0);
23
+ out.set(ciphertext, nonce.length);
24
+ return out;
25
+ };
26
+
27
+ export const splitSealed = (
28
+ sealed: Uint8Array,
29
+ label: string,
30
+ ): { readonly nonce: Uint8Array; readonly ciphertext: Uint8Array } => {
31
+ if (sealed.length < NONCE_BYTES + GCM_TAG_BYTES) {
32
+ throw new Error(`${label} too short to contain IV + ciphertext + tag`);
33
+ }
34
+ return {
35
+ nonce: sealed.slice(0, NONCE_BYTES),
36
+ ciphertext: sealed.slice(NONCE_BYTES),
37
+ };
38
+ };
39
+
40
+ /**
41
+ * A blob whose key came from a KDF, encoded so it says which KDF parameters
42
+ * produced it: `v<n>:<base64(IV || ciphertext+tag)>`.
43
+ *
44
+ * The stamp is the whole point. Without it, re-tuning Argon2id is
45
+ * indistinguishable from destroying every vault, because nothing on disk
46
+ * records what the bytes were derived with (see `./argon2.ts`).
47
+ *
48
+ * `:` and `v` are outside the base64 alphabet, so a value either carries the
49
+ * prefix or it doesn't — there is no ambiguity and no length coupling. A bare
50
+ * base64 value is a pre-stamp blob and means {@link LEGACY_KDF_VERSION}.
51
+ */
52
+ const STAMP_PATTERN = /^v(\d{1,3}):(.*)$/s;
53
+
54
+ export type TKdfWrapped = {
55
+ readonly version: number;
56
+ readonly nonce: Uint8Array;
57
+ readonly ciphertext: Uint8Array;
58
+ };
59
+
60
+ export const encodeKdfWrapped = (
61
+ nonce: Uint8Array,
62
+ ciphertext: Uint8Array,
63
+ version: TKdfVersion = CURRENT_KDF_VERSION,
64
+ ): string => `v${version}:${toB64(joinSealed(nonce, ciphertext))}`;
65
+
66
+ /**
67
+ * Read a wrapped blob and the version it declares. The version is returned
68
+ * unvalidated — `kdfParamsFor` is what rejects one this build can't derive,
69
+ * so the failure names the real problem instead of surfacing as a bad auth tag.
70
+ */
71
+ export const decodeKdfWrapped = (value: string, label: string): TKdfWrapped => {
72
+ const stamped = STAMP_PATTERN.exec(value);
73
+ const version =
74
+ stamped === null ? LEGACY_KDF_VERSION : Number.parseInt(stamped[1], 10);
75
+ const payload = stamped === null ? value : stamped[2];
76
+ return { version, ...splitSealed(fromB64(payload), label) };
77
+ };
@@ -0,0 +1,90 @@
1
+ # `packages/sdk/wire` Architecture
2
+
3
+ > `@openllmsh/sdk/wire` — the **wire-format transforms** shared by the cloud
4
+ > proxy pipeline (`packages/core`) and the local subscription daemon
5
+ > (`packages/cli/daemon`). Depends on `@openllmsh/sdk/protocol` and `effect`'s
6
+ > `Schema` decoder only: **no Effect-DI, no provider registry, no `fetch`, no
7
+ > HTTP framework, no DB.** It provides pure request/response transforms and
8
+ > self-contained stream transforms over protocol wire types.
9
+ >
10
+ > Extracted from `packages/core` per
11
+ > [`docs/proposals/coreless-daemon-passthrough.md`](../../docs/proposals/coreless-daemon-passthrough.md)
12
+ > §4 + §7.1. Referenced from the root
13
+ > [`ARCHITECTURE.md`](../../ARCHITECTURE.md) §3.
14
+
15
+ ---
16
+
17
+ ## Why this package exists
18
+
19
+ The daemon must adapt a request to each hop's wire, encode the response
20
+ back, and synthesize usage — **without** linking the whole `core`
21
+ pipeline (the runner + provider graph + Effect-DI layers). Those transforms
22
+ were extracted from `core` into a protocol- and Schema-only package so the
23
+ daemon can import them directly. The gateway re-imports the shared client
24
+ adapters, while `core` re-imports provider-wire transforms, SSE utilities, and
25
+ policy helpers; provider registration and runner orchestration remain outside
26
+ this package. Shared transform behavior is pinned by the existing
27
+ `tests/{inbound,outbound}-*` and matrix suites.
28
+
29
+ ## Modules
30
+
31
+ ```
32
+ wire/
33
+ index.ts root barrel for shared adapters, features, canonical,
34
+ streaming, refusal, encrypted-content, and thinking helpers
35
+ adapters/
36
+ messages/ Anthropic Messages ↔ canonical request/response/SSE;
37
+ Anthropic mapping and reasoning-signature/item helpers
38
+ responses/ OpenAI Responses ↔ canonical request/response/SSE
39
+ providers/
40
+ upstream-request.ts one request body/header recipe for every client/upstream wire pair
41
+ anthropic/ request/response/SSE transforms, adaptive thinking, beta headers
42
+ chatgpt/ Codex request/SSE transforms, auth/endpoints, native web search
43
+ grok|kimi/web-search.ts native web-search request transforms
44
+ features/
45
+ compaction/{compact-request,
46
+ compaction-text}.ts last-resort cache-aware request compaction and visible-text safety
47
+ context-{demote,overflow-strategy,
48
+ skip}.ts context ladder selection, overflow policy, and fit targets
49
+ max-tokens-backfill.ts output-token cap normalization
50
+ quota-gate.ts subscription-quota admission decision
51
+ lib/
52
+ canonical/{content-part,message,
53
+ encoding-select,token-estimate}.ts image detection, canonical utilities, and BPE token estimates
54
+ token-ruler/ lazy Claude/o200k BPE counters and vocabularies
55
+ streaming/{sse,provider-decode,
56
+ response-stream,accumulate,peek,
57
+ strip-tool-calls,upstream-error}.ts SSE framing/liveness, generic provider decode,
58
+ canonical chunk conversion/collection, peeking, and errors
59
+ {encrypted-content,error-class,
60
+ error-envelope,provider-error-rules,
61
+ forwarded-headers,refusal,
62
+ thinking-signature,tool-schema}.ts request/error/header and provider-compatibility helpers
63
+ ```
64
+
65
+ The root barrel intentionally does not expose every provider or streaming helper;
66
+ package export subpaths expose `adapters/messages`, `adapters/responses`,
67
+ `providers/anthropic`, `providers/chatgpt`, and individual source modules.
68
+
69
+ ## Layering rules
70
+
71
+ - Depends only on `@openllmsh/sdk/protocol` plus `effect`'s `Schema` decoder. It
72
+ has no `@openllm/core`, `@openllm/api`, `db`, or `vault` dependency; it does
73
+ not use Effect `Layer`, `Context`, or `Effect`, nor Next/Vercel, `fetch`, or
74
+ a provider registry.
75
+ - Transforms do not perform network or persistence I/O and have no DI. The
76
+ lazy BPE ruler cache and debug-only stream logging are local implementation
77
+ details; request/response transforms run identically in the cloud pipeline
78
+ and compiled daemon.
79
+ - `wire` owns client adapters (Messages and Responses), upstream request
80
+ composition, provider-specific Anthropic/ChatGPT wire transforms, canonical
81
+ SSE framing, generic provider-SSE decoding, and canonical-chunk encoding.
82
+ `core` retains `providerEventStream`, which binds a `TChatProviderSpec` to
83
+ the same SSE/Schema mechanics, plus provider registration, fallback and
84
+ runner orchestration. The daemon remains `core`-free: its walker composes
85
+ wire transforms with local subscription delegation and transport.
86
+ - Compaction helpers in `features/compaction` are last-resort, immutable
87
+ request rewriting after the fallback chain is exhausted. Context sizing,
88
+ overflow strategy, output-cap backfill, and quota decisions are wire policy
89
+ helpers; executing fallback or a compaction attempt is owned by `core` or the
90
+ daemon walker.