@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,43 @@
1
+ # `@openllmsh/sdk` architecture
2
+
3
+ One workspace package, eight internal layers. Boundaries that used to be
4
+ enforced by separate `package.json` deps are enforced by
5
+ `tests/sdk/layering.test.ts`.
6
+
7
+ ```
8
+ protocol ← wire
9
+ ← tunnel ← vault
10
+ ← openapi
11
+ openapi + protocol ← mcp
12
+ any SDK layer ← client (no node:*)
13
+ vault/crypto + vault/public-key-seal + wire SSE ← connect
14
+ ```
15
+
16
+ | Folder | May import | Detail |
17
+ | --- | --- | --- |
18
+ | `protocol/` | `effect` only | Public Effect-Schema contracts. See `protocol/README.md`. |
19
+ | `wire/` | `effect`, `@openllmsh/sdk/protocol` | Pure wire transforms. See `wire/ARCHITECTURE.md`. |
20
+ | `tunnel/` | `@openllmsh/sdk/protocol`, `effect` | Sans-I/O mux / RTC / Noise. See `tunnel/ARCHITECTURE.md`. |
21
+ | `tunnel/codec*`, `tunnel/channel-envelope` | nothing outside themselves | Relay-safe subpaths. |
22
+ | `vault/` | `@openllmsh/sdk/protocol`, `@openllmsh/sdk/tunnel`, `@noble/ed25519`, `@scure/bip39`, `hash-wasm` | Zero-knowledge crypto. See `vault/ARCHITECTURE.md`. |
23
+ | `openapi/` | `@openllmsh/sdk/protocol` (+ committed JSON) | Spec + operations table + `callOperation`. |
24
+ | `mcp/` | `@openllmsh/sdk/openapi`, `@openllmsh/sdk/protocol`, `@openllmsh/sdk/client`, `@modelcontextprotocol/server` | Tool defs + `createMcpServer`; no `node:*`. |
25
+ | `client/` | any SDK layer; no `node:*` | I/O-injected facade (`createClient`): vault session, HTTP (`callOperation`), device tunnel (relay-mux + open ladder). |
26
+ | `connect/` | `@openllmsh/sdk/vault/crypto`, `@openllmsh/sdk/vault/public-key-seal`, `@openllmsh/sdk/wire/lib/streaming/sse` only | Server-side "Connect with OpenLLM" for third-party apps (`createConnect`, `connectRoutes`, `encryptedConnectionStore`): OAuth + PKCE, key pickup, token refresh, chat streaming, Fetch-API routes. Never the vault barrel (argon2 WASM). |
27
+
28
+ Rules that apply to every folder:
29
+
30
+ - Never `@openllm/{schema,core,db,api}`.
31
+ - No `node:*` outside the allow-list (`protocol/url-locality-dns.ts`,
32
+ `tunnel/rtc-ice.ts`).
33
+ - No nested `package.json` under `packages/sdk/*/`.
34
+ - BUSL folders (`wire/`, `tunnel/`, `vault/`) each keep their own `LICENSE`.
35
+
36
+ Intra-SDK imports use the package's own name (`@openllmsh/sdk/protocol`) so
37
+ the public mirror is byte-identical to `packages/sdk` apart from the version
38
+ stamp. There is no `"."` root export — a root barrel would re-couple the
39
+ layers and drag vault's WASM into the relay bundle.
40
+
41
+ Published as one mirror (`openllmsh/sdk`) by `git subtree split
42
+ --prefix=packages/sdk`. The CLI mirror pins a single
43
+ `"@openllmsh/sdk": "github:openllmsh/sdk#<tag>"`.
package/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
package/README.md CHANGED
@@ -1,3 +1,143 @@
1
- # Temporary Holding Version
1
+ <p align="center">
2
+ <picture>
3
+ <source media="(prefers-color-scheme: light)" srcset="./assets/openllm-light.svg">
4
+ <img alt="OpenLLM" src="./assets/openllm.svg" width="300">
5
+ </picture>
6
+ </p>
2
7
 
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.
8
+ <p align="center"><b>@openllmsh/sdk</b> — everything a client needs to talk to OpenLLM.</p>
9
+
10
+ <p align="center">
11
+ <a href="./LICENSE"><img alt="License: Apache-2.0 AND BUSL-1.1" src="https://img.shields.io/badge/license-Apache--2.0%20%2B%20BUSL--1.1-blue.svg"></a>
12
+ <img alt="runtime" src="https://img.shields.io/badge/Bun%20%C2%B7%20Node%20%C2%B7%20browser-TypeScript-black.svg">
13
+ <img alt="deps: effect" src="https://img.shields.io/badge/deps-effect-blue.svg">
14
+ </p>
15
+
16
+ ---
17
+
18
+ The same code the OpenLLM browser app, the `openllm` CLI and its local daemon
19
+ run, published as one package. Typed contracts, wire-format transforms, the
20
+ device tunnel, client-side vault crypto, the HTTP API spec and the MCP tools,
21
+ under a single version.
22
+
23
+ - **One package, imported by subpath.** No root barrel. Pull in
24
+ `@openllmsh/sdk/protocol`, `/vault` or `/client` and nothing else comes along.
25
+ - **Zero-knowledge by construction.** Provider keys are sealed in the client
26
+ under a key the server never sees in plaintext; `vault` holds the crypto
27
+ (Argon2id, AES-256-GCM, HKDF, X25519 seals) and zeroes key material on close.
28
+ - **I/O injected, runs anywhere.** `client` and `mcp` take `fetch`, `WebSocket`
29
+ and an RTC factory from the host, so the same code runs in the browser, in
30
+ Bun and in Node.
31
+ - **One contract for HTTP and MCP.** `openapi` ships the public spec with a
32
+ typed `callOperation()`; `mcp` derives its tools from the same operations.
33
+
34
+ See [`ARCHITECTURE.md`](./ARCHITECTURE.md) for the layers and which may import
35
+ which.
36
+
37
+ ## Install
38
+
39
+ ```sh
40
+ bun install github:openllmsh/sdk#vX.Y.Z
41
+ ```
42
+
43
+ Releases are tagged with the OpenLLM release version; pin a tag.
44
+
45
+ ## Quickstart
46
+
47
+ ```ts
48
+ import { createClient } from "@openllmsh/sdk/client";
49
+
50
+ const ollm = createClient({
51
+ origin: "https://openllm.sh",
52
+ apiKey, // sk-llm-…
53
+ transport: { fetch, websocket: WebSocket },
54
+ });
55
+
56
+ // Streaming chat: a fetch Response whose body is the SSE stream.
57
+ const res = await ollm.chat.completions.create(
58
+ { model: "plus", messages, stream: true },
59
+ { signal },
60
+ );
61
+
62
+ // Any public API operation, typed from the spec.
63
+ await ollm.http.call("marketplace.launch", {});
64
+
65
+ ollm.close(); // zeroes key material, closes device channels
66
+ ```
67
+
68
+ ### Vault
69
+
70
+ ```ts
71
+ // Unwrap the data key with the server's dek_for_key blob for this API key.
72
+ await ollm.vault.unlockFromApiKey(dekForKeyB64);
73
+
74
+ const sealed = await ollm.vault.sealToDevice(devicePubSpkiB64, secret);
75
+ const encrypted = await ollm.vault.encryptCredential(JSON.stringify(creds));
76
+
77
+ // Borrow the API-key secret; it is zeroed when the callback returns or throws.
78
+ await ollm.vault.withApiKeySecret((secret, id) => use(secret, id));
79
+ ```
80
+
81
+ A browser host that has already unlocked the vault (PIN or passkey) passes
82
+ `{ dek }` to `createClient` instead.
83
+
84
+ ### Devices
85
+
86
+ ```ts
87
+ const ollm = createClient({
88
+ origin,
89
+ apiKey,
90
+ transport: {
91
+ fetch,
92
+ websocket: WebSocket,
93
+ rtc: (config) => new RTCPeerConnection(config),
94
+ },
95
+ devices: { mux: muxDeps, ladder: ladderDeps },
96
+ });
97
+
98
+ const device = ollm.devices.open(deviceId);
99
+ await device.openOverLadder(async (channel, budget, path) => {
100
+ // agentStream(…) over WebRTC, falling back to the relay mux
101
+ });
102
+ ```
103
+
104
+ The SDK owns the relay-mux open path and the WebRTC → mux failover; the host
105
+ still negotiates the WebRTC channel itself.
106
+
107
+ ### Lower layers
108
+
109
+ ```ts
110
+ import { ChatCompletionRequest } from "@openllmsh/sdk/protocol";
111
+ import { fromAnthropicMessagesRequest } from "@openllmsh/sdk/wire/adapters/messages";
112
+ import { isValidMuxFrame } from "@openllmsh/sdk/tunnel/codec";
113
+ import { parseApiKey, sealCliAuth } from "@openllmsh/sdk/vault";
114
+ ```
115
+
116
+ ## Subpaths
117
+
118
+ | Subpath | Contents |
119
+ | --- | --- |
120
+ | `@openllmsh/sdk/protocol` | Effect Schema contracts: request/response, streaming, provider, daemon and relay wire shapes. |
121
+ | `@openllmsh/sdk/wire` | Pure wire-format transforms: Anthropic ↔ OpenAI adapters, SSE, token estimates. Shared by the cloud proxy and the daemon. |
122
+ | `@openllmsh/sdk/tunnel` | Sans-I/O binary mux, device grants, WebRTC helpers, Noise_IK. The relay imports only `/codec` and `/channel-envelope`. |
123
+ | `@openllmsh/sdk/vault` | Client-side crypto, DEK-carrying API keys and X25519 seals. **Only the barrel is stable public API**; `vault/*` files are internal. |
124
+ | `@openllmsh/sdk/openapi` | The committed OpenAPI spec, the operations table and `callOperation()`. |
125
+ | `@openllmsh/sdk/openapi/spec.json` | The OpenAPI document itself, as served at `/api/swagger`. |
126
+ | `@openllmsh/sdk/mcp` | MCP tool definitions (API, memory, marketplace upstreams) and `createMcpServer` over an injected transport. |
127
+ | `@openllmsh/sdk/client` | The facade above: vault session, HTTP and chat, device tunnel. Composition only — no protocol or crypto of its own. |
128
+
129
+ ## License
130
+
131
+ The package root is **Apache-2.0** ([`LICENSE`](./LICENSE)), covering
132
+ `protocol/`, `openapi/`, `mcp/` and `client/`.
133
+
134
+ `wire/`, `tunnel/` and `vault/` carry their own `LICENSE` under the
135
+ **Business Source License 1.1**, which governs those folders instead of the
136
+ root. Importing them brings in the BUSL competing-service clause.
137
+
138
+ | Path | License |
139
+ | --- | --- |
140
+ | `protocol/`, `openapi/`, `mcp/`, `client/` | Apache-2.0 |
141
+ | `wire/`, `tunnel/`, `vault/` | BUSL-1.1 |
142
+
143
+ `package.json` declares `"license": "(Apache-2.0 AND BUSL-1.1)"`.
@@ -0,0 +1,18 @@
1
+ <svg width="382" height="84" viewBox="0 0 382 84" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M225.974 45.3375V63.4219H219.835V29.9968H225.974V39.653C227.596 33.074 232.144 29.0873 238.389 29.0873C245.999 29.0873 250.546 35.0295 250.546 44.7312V63.4219H244.407V44.7312C244.407 38.031 241.572 34.6354 235.827 34.6354C229.657 34.6354 225.974 38.5767 225.974 45.3375Z" fill="#0d1117"/>
3
+ <path d="M209.478 52.3409H215.617C213.889 59.8293 208.098 64.3315 200.276 64.3315C190.438 64.3315 183.844 57.2372 183.844 46.6563C183.844 36.1361 190.363 29.0873 200.079 29.0873C209.644 29.0873 215.769 35.9087 215.769 46.5654C215.769 47.3385 215.738 48.0509 215.663 48.9453H190.105C190.817 55.0694 194.637 58.7833 200.428 58.7833C204.854 58.7833 207.841 56.6005 209.478 52.3409ZM190.226 43.6397H209.417C208.599 37.8339 205.324 34.6354 200.11 34.6354C194.759 34.6354 191.151 38.0158 190.226 43.6397Z" fill="#0d1117"/>
4
+ <path d="M153.97 29.9968V40.2896C155.713 33.4379 160.306 29.0873 166.37 29.0873C174.616 29.0873 180.513 36.4393 180.513 46.7018C180.513 56.9643 174.616 64.3315 166.37 64.3315C160.306 64.3315 155.713 59.9809 153.97 53.114V74.8668H147.83V29.9968H153.97ZM153.97 46.7018C153.97 54.3267 157.85 58.9349 164.278 58.9349C170.826 58.9349 174.373 54.6298 174.373 46.7018C174.373 38.7737 170.826 34.4838 164.278 34.4838C157.85 34.4838 153.97 39.0769 153.97 46.7018Z" fill="#0d1117"/>
5
+ <path d="M102.347 41.7449C102.347 28.2991 110.639 19.1583 122.811 19.1583C134.984 19.1583 143.291 28.2991 143.291 41.7449C143.291 55.1756 134.984 64.3315 122.811 64.3315C110.639 64.3315 102.347 55.1756 102.347 41.7449ZM108.713 41.7449C108.713 52.159 114.004 58.4196 122.811 58.4196C131.618 58.4196 136.924 52.159 136.924 41.7449C136.924 31.3308 131.618 25.0703 122.811 25.0703C114.004 25.0703 108.713 31.3308 108.713 41.7449Z" fill="#0d1117"/>
6
+ <path d="M373.782 20.0679H381.453V63.422H376.784V24.373L360.018 63.422H353.56L336.977 24.3881V63.422H332.399V20.0679H340.084L356.85 59.6323L373.782 20.0679Z" fill="#0d1117"/>
7
+ <path d="M300.083 20.0679H304.752V59.0714H327.945V63.422H300.083V20.0679Z" fill="#0d1117"/>
8
+ <path d="M267.752 20.0677H272.421V59.0713H295.614V63.4219H267.752V20.0677Z" fill="#0d1117"/>
9
+ <path d="M31.3217 2.56513C31.3217 1.14844 32.4702 0 33.8869 0H42.1804C43.5971 0 44.7456 1.14844 44.7456 2.56513V10.8587C44.7456 12.2754 43.5971 13.4238 42.1804 13.4238H33.8869C32.4702 13.4238 31.3217 12.2754 31.3217 10.8587V2.56513Z" fill="#0d1117"/>
10
+ <path d="M0 6.14247C0 4.90684 1.00167 3.90516 2.2373 3.90516C3.47293 3.90516 4.47461 4.90684 4.47461 6.14247C4.47461 7.3781 3.47293 8.37977 2.2373 8.37977C1.00167 8.37977 0 7.3781 0 6.14247Z" fill="#0d1117"/>
11
+ <path d="M46.9829 52.8122C46.9829 51.3955 48.1313 50.247 49.548 50.247H61.6752C63.0918 50.247 64.2403 49.0986 64.2403 47.6819V35.4832C64.2403 34.0665 63.0918 32.9181 61.6752 32.9181H49.548C48.1313 32.9181 46.9829 31.7696 46.9829 30.3529V17.5848C46.9829 16.1681 48.1313 15.0196 49.548 15.0196H62.3162C63.7328 15.0196 64.8813 16.1681 64.8813 17.5848V29.7835C64.8813 31.2002 66.0297 32.3486 67.4464 32.3486H79.5731C80.9898 32.3486 82.1382 33.4971 82.1382 34.9137V47.6819C82.1382 49.0986 80.9898 50.247 79.5731 50.247H67.4464C66.0297 50.247 64.8813 51.3955 64.8813 52.8122V65.5804C64.8813 66.997 63.7328 68.1455 62.3162 68.1455H49.548C48.1313 68.1455 46.9829 66.997 46.9829 65.5804V52.8122Z" fill="#0d1117"/>
12
+ <path d="M15.6611 21.4899C15.6611 20.0732 16.8096 18.9248 18.2262 18.9248H22.0452C23.4619 18.9248 24.6103 20.0732 24.6103 21.4899V25.3089C24.6103 26.7256 23.4619 27.874 22.0452 27.874H18.2262C16.8096 27.874 15.6611 26.7256 15.6611 25.3089V21.4899Z" fill="#0d1117"/>
13
+ <path d="M31.3217 37.1511C31.3217 35.7344 32.4702 34.5859 33.8869 34.5859H42.1804C43.5971 34.5859 44.7456 35.7344 44.7456 37.1511V45.4446C44.7456 46.8613 43.5971 48.0097 42.1804 48.0097H33.8869C32.4702 48.0097 31.3217 46.8613 31.3217 45.4446V37.1511Z" fill="#0d1117"/>
14
+ <path d="M0 41.2978C0 40.0622 1.00167 39.0605 2.2373 39.0605C3.47293 39.0605 4.47461 40.0622 4.47461 41.2978C4.47461 42.5335 3.47293 43.5351 2.2373 43.5351C1.00167 43.5351 0 42.5335 0 41.2978Z" fill="#0d1117"/>
15
+ <path d="M15.6611 57.2868C15.6611 55.8701 16.8096 54.7217 18.2262 54.7217H22.0452C23.4619 54.7217 24.6103 55.8701 24.6103 57.2868V61.1057C24.6103 62.5224 23.4619 63.6709 22.0452 63.6709H18.2262C16.8096 63.6709 15.6611 62.5224 15.6611 61.1057V57.2868Z" fill="#0d1117"/>
16
+ <path d="M33.8869 83.1651C32.4702 83.1651 31.3217 82.0167 31.3217 80.6V72.3064C31.3217 70.8897 32.4702 69.7413 33.8869 69.7413H42.1804C43.5971 69.7413 44.7456 70.8897 44.7456 72.3064V80.6C44.7456 82.0167 43.5971 83.1651 42.1804 83.1651H33.8869Z" fill="#0d1117"/>
17
+ <path d="M0 76.4527C0 75.2171 1.00167 74.2154 2.2373 74.2154C3.47293 74.2154 4.47461 75.2171 4.47461 76.4527C4.47461 77.6883 3.47293 78.69 2.2373 78.69C1.00167 78.69 0 77.6883 0 76.4527Z" fill="#0d1117"/>
18
+ </svg>
@@ -0,0 +1,18 @@
1
+ <svg width="382" height="84" viewBox="0 0 382 84" fill="none" xmlns="http://www.w3.org/2000/svg">
2
+ <path d="M225.974 45.3375V63.4219H219.835V29.9968H225.974V39.653C227.596 33.074 232.144 29.0873 238.389 29.0873C245.999 29.0873 250.546 35.0295 250.546 44.7312V63.4219H244.407V44.7312C244.407 38.031 241.572 34.6354 235.827 34.6354C229.657 34.6354 225.974 38.5767 225.974 45.3375Z" fill="white"/>
3
+ <path d="M209.478 52.3409H215.617C213.889 59.8293 208.098 64.3315 200.276 64.3315C190.438 64.3315 183.844 57.2372 183.844 46.6563C183.844 36.1361 190.363 29.0873 200.079 29.0873C209.644 29.0873 215.769 35.9087 215.769 46.5654C215.769 47.3385 215.738 48.0509 215.663 48.9453H190.105C190.817 55.0694 194.637 58.7833 200.428 58.7833C204.854 58.7833 207.841 56.6005 209.478 52.3409ZM190.226 43.6397H209.417C208.599 37.8339 205.324 34.6354 200.11 34.6354C194.759 34.6354 191.151 38.0158 190.226 43.6397Z" fill="white"/>
4
+ <path d="M153.97 29.9968V40.2896C155.713 33.4379 160.306 29.0873 166.37 29.0873C174.616 29.0873 180.513 36.4393 180.513 46.7018C180.513 56.9643 174.616 64.3315 166.37 64.3315C160.306 64.3315 155.713 59.9809 153.97 53.114V74.8668H147.83V29.9968H153.97ZM153.97 46.7018C153.97 54.3267 157.85 58.9349 164.278 58.9349C170.826 58.9349 174.373 54.6298 174.373 46.7018C174.373 38.7737 170.826 34.4838 164.278 34.4838C157.85 34.4838 153.97 39.0769 153.97 46.7018Z" fill="white"/>
5
+ <path d="M102.347 41.7449C102.347 28.2991 110.639 19.1583 122.811 19.1583C134.984 19.1583 143.291 28.2991 143.291 41.7449C143.291 55.1756 134.984 64.3315 122.811 64.3315C110.639 64.3315 102.347 55.1756 102.347 41.7449ZM108.713 41.7449C108.713 52.159 114.004 58.4196 122.811 58.4196C131.618 58.4196 136.924 52.159 136.924 41.7449C136.924 31.3308 131.618 25.0703 122.811 25.0703C114.004 25.0703 108.713 31.3308 108.713 41.7449Z" fill="white"/>
6
+ <path d="M373.782 20.0679H381.453V63.422H376.784V24.373L360.018 63.422H353.56L336.977 24.3881V63.422H332.399V20.0679H340.084L356.85 59.6323L373.782 20.0679Z" fill="white"/>
7
+ <path d="M300.083 20.0679H304.752V59.0714H327.945V63.422H300.083V20.0679Z" fill="white"/>
8
+ <path d="M267.752 20.0677H272.421V59.0713H295.614V63.4219H267.752V20.0677Z" fill="white"/>
9
+ <path d="M31.3217 2.56513C31.3217 1.14844 32.4702 0 33.8869 0H42.1804C43.5971 0 44.7456 1.14844 44.7456 2.56513V10.8587C44.7456 12.2754 43.5971 13.4238 42.1804 13.4238H33.8869C32.4702 13.4238 31.3217 12.2754 31.3217 10.8587V2.56513Z" fill="white"/>
10
+ <path d="M0 6.14247C0 4.90684 1.00167 3.90516 2.2373 3.90516C3.47293 3.90516 4.47461 4.90684 4.47461 6.14247C4.47461 7.3781 3.47293 8.37977 2.2373 8.37977C1.00167 8.37977 0 7.3781 0 6.14247Z" fill="white"/>
11
+ <path d="M46.9829 52.8122C46.9829 51.3955 48.1313 50.247 49.548 50.247H61.6752C63.0918 50.247 64.2403 49.0986 64.2403 47.6819V35.4832C64.2403 34.0665 63.0918 32.9181 61.6752 32.9181H49.548C48.1313 32.9181 46.9829 31.7696 46.9829 30.3529V17.5848C46.9829 16.1681 48.1313 15.0196 49.548 15.0196H62.3162C63.7328 15.0196 64.8813 16.1681 64.8813 17.5848V29.7835C64.8813 31.2002 66.0297 32.3486 67.4464 32.3486H79.5731C80.9898 32.3486 82.1382 33.4971 82.1382 34.9137V47.6819C82.1382 49.0986 80.9898 50.247 79.5731 50.247H67.4464C66.0297 50.247 64.8813 51.3955 64.8813 52.8122V65.5804C64.8813 66.997 63.7328 68.1455 62.3162 68.1455H49.548C48.1313 68.1455 46.9829 66.997 46.9829 65.5804V52.8122Z" fill="white"/>
12
+ <path d="M15.6611 21.4899C15.6611 20.0732 16.8096 18.9248 18.2262 18.9248H22.0452C23.4619 18.9248 24.6103 20.0732 24.6103 21.4899V25.3089C24.6103 26.7256 23.4619 27.874 22.0452 27.874H18.2262C16.8096 27.874 15.6611 26.7256 15.6611 25.3089V21.4899Z" fill="white"/>
13
+ <path d="M31.3217 37.1511C31.3217 35.7344 32.4702 34.5859 33.8869 34.5859H42.1804C43.5971 34.5859 44.7456 35.7344 44.7456 37.1511V45.4446C44.7456 46.8613 43.5971 48.0097 42.1804 48.0097H33.8869C32.4702 48.0097 31.3217 46.8613 31.3217 45.4446V37.1511Z" fill="white"/>
14
+ <path d="M0 41.2978C0 40.0622 1.00167 39.0605 2.2373 39.0605C3.47293 39.0605 4.47461 40.0622 4.47461 41.2978C4.47461 42.5335 3.47293 43.5351 2.2373 43.5351C1.00167 43.5351 0 42.5335 0 41.2978Z" fill="white"/>
15
+ <path d="M15.6611 57.2868C15.6611 55.8701 16.8096 54.7217 18.2262 54.7217H22.0452C23.4619 54.7217 24.6103 55.8701 24.6103 57.2868V61.1057C24.6103 62.5224 23.4619 63.6709 22.0452 63.6709H18.2262C16.8096 63.6709 15.6611 62.5224 15.6611 61.1057V57.2868Z" fill="white"/>
16
+ <path d="M33.8869 83.1651C32.4702 83.1651 31.3217 82.0167 31.3217 80.6V72.3064C31.3217 70.8897 32.4702 69.7413 33.8869 69.7413H42.1804C43.5971 69.7413 44.7456 70.8897 44.7456 72.3064V80.6C44.7456 82.0167 43.5971 83.1651 42.1804 83.1651H33.8869Z" fill="white"/>
17
+ <path d="M0 76.4527C0 75.2171 1.00167 74.2154 2.2373 74.2154C3.47293 74.2154 4.47461 75.2171 4.47461 76.4527C4.47461 77.6883 3.47293 78.69 2.2373 78.69C1.00167 78.69 0 77.6883 0 76.4527Z" fill="white"/>
18
+ </svg>
package/client/chat.ts ADDED
@@ -0,0 +1,140 @@
1
+ /**
2
+ * `/v1/chat/completions` convenience on the client. Stream and non-stream both
3
+ * use authenticated `request` so signal / headers / timeout behave the same and
4
+ * the caller always gets a live `Response` (SSE body or JSON body).
5
+ */
6
+
7
+ import type { TClientHttp } from "./http";
8
+
9
+ /** Default non-stream request ceiling — matches `openapi/call.ts` `REQUEST_TIMEOUT_MS`. */
10
+ export const DEFAULT_CHAT_TIMEOUT_MS = 120_000;
11
+
12
+ export type TChatCompletionCreateBody = {
13
+ readonly model: string;
14
+ readonly messages: ReadonlyArray<unknown>;
15
+ readonly stream?: boolean;
16
+ readonly [key: string]: unknown;
17
+ };
18
+
19
+ export type TChatCompletions = {
20
+ /**
21
+ * Create a chat completion. Always returns the raw `Response` — when
22
+ * `body.stream === true` the body is an SSE stream; otherwise JSON.
23
+ * Honours `signal` and merges `headers` via `Headers`.
24
+ *
25
+ * Timeouts:
26
+ * - non-stream: `timeoutMs` defaults to {@link DEFAULT_CHAT_TIMEOUT_MS} and
27
+ * covers the whole request (headers + body), matching `callOperation`.
28
+ * - stream: no default timeout (a long SSE must outlive 2 minutes). When the
29
+ * caller passes `timeoutMs`, it applies only until response headers /
30
+ * first byte arrive — never to the body. Matches the pre-SDK browser SSE
31
+ * and CLI `-p` paths, which passed only the caller's signal (or none).
32
+ */
33
+ readonly create: (
34
+ body: TChatCompletionCreateBody,
35
+ options?: {
36
+ readonly signal?: AbortSignal;
37
+ readonly headers?: HeadersInit;
38
+ readonly timeoutMs?: number;
39
+ },
40
+ ) => Promise<Response>;
41
+ };
42
+
43
+ export type TClientChat = {
44
+ readonly completions: TChatCompletions;
45
+ };
46
+
47
+ const mergeHeaders = (base: HeadersInit, extra?: HeadersInit): Headers => {
48
+ const headers = new Headers(base);
49
+ if (extra !== undefined) {
50
+ new Headers(extra).forEach((value, key) => {
51
+ headers.set(key, value);
52
+ });
53
+ }
54
+ return headers;
55
+ };
56
+
57
+ type TResolvedRequestSignal = {
58
+ readonly signal: AbortSignal | undefined;
59
+ /** Clear a connect-only timer once headers have arrived (or the fetch failed). */
60
+ readonly release: () => void;
61
+ };
62
+
63
+ /**
64
+ * Build the fetch abort signal.
65
+ * - non-stream: default {@link DEFAULT_CHAT_TIMEOUT_MS} on the whole request.
66
+ * - stream: caller's signal only by default; optional `timeoutMs` is
67
+ * connect/first-byte only (released after `fetch` resolves).
68
+ */
69
+ const resolveRequestSignal = (
70
+ streaming: boolean,
71
+ timeoutMs: number | undefined,
72
+ caller: AbortSignal | undefined,
73
+ ): TResolvedRequestSignal => {
74
+ if (streaming) {
75
+ if (timeoutMs === undefined) {
76
+ return { signal: caller, release: (): void => {} };
77
+ }
78
+ // Connect/first-byte only: clear the timer once headers arrive so the SSE
79
+ // body is never cut by timeoutMs. The caller's signal stays linked through
80
+ // AbortSignal.any and can still abort the body after release().
81
+ const connect = new AbortController();
82
+ const timer = setTimeout((): void => {
83
+ connect.abort(
84
+ new DOMException(
85
+ "The operation was aborted due to timeout",
86
+ "TimeoutError",
87
+ ),
88
+ );
89
+ }, timeoutMs);
90
+ const signal =
91
+ caller !== undefined
92
+ ? AbortSignal.any([caller, connect.signal])
93
+ : connect.signal;
94
+ return {
95
+ signal,
96
+ release: (): void => {
97
+ clearTimeout(timer);
98
+ },
99
+ };
100
+ }
101
+ const timeout = AbortSignal.timeout(timeoutMs ?? DEFAULT_CHAT_TIMEOUT_MS);
102
+ return {
103
+ signal: caller !== undefined ? AbortSignal.any([caller, timeout]) : timeout,
104
+ release: (): void => {},
105
+ };
106
+ };
107
+
108
+ export const createClientChat = (http: TClientHttp): TClientChat => ({
109
+ completions: {
110
+ create: async (body, options = {}): Promise<Response> => {
111
+ const streaming = body.stream === true;
112
+ const { signal, release } = resolveRequestSignal(
113
+ streaming,
114
+ options.timeoutMs,
115
+ options.signal,
116
+ );
117
+ try {
118
+ const response = await http.request("/v1/chat/completions", {
119
+ method: "POST",
120
+ headers: mergeHeaders(
121
+ {
122
+ "content-type": "application/json",
123
+ ...(streaming ? { accept: "text/event-stream" } : {}),
124
+ },
125
+ options.headers,
126
+ ),
127
+ body: JSON.stringify(body),
128
+ ...(signal !== undefined ? { signal } : {}),
129
+ });
130
+ // Headers (and first byte for HTTP/1.1) are in — drop a stream
131
+ // connect timeout so reading the SSE body is not aborted.
132
+ release();
133
+ return response;
134
+ } catch (error) {
135
+ release();
136
+ throw error;
137
+ }
138
+ },
139
+ },
140
+ });