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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/ARCHITECTURE.md +43 -0
  2. package/LICENSE +202 -0
  3. package/README.md +142 -2
  4. package/assets/openllm-light.svg +18 -0
  5. package/assets/openllm.svg +18 -0
  6. package/client/chat.ts +140 -0
  7. package/client/devices.ts +269 -0
  8. package/client/http.ts +72 -0
  9. package/client/index.ts +129 -0
  10. package/client/relay-mux.ts +547 -0
  11. package/client/types.ts +51 -0
  12. package/client/vault.ts +239 -0
  13. package/connect/chat.ts +58 -0
  14. package/connect/client.ts +240 -0
  15. package/connect/config.ts +47 -0
  16. package/connect/errors.ts +121 -0
  17. package/connect/index.ts +23 -0
  18. package/connect/oauth.ts +265 -0
  19. package/connect/routes.ts +234 -0
  20. package/connect/store.ts +107 -0
  21. package/mcp/handle.ts +24 -0
  22. package/mcp/index.ts +76 -0
  23. package/mcp/server.ts +319 -0
  24. package/mcp/tools/marketplace.ts +439 -0
  25. package/mcp/tools/memory-client.ts +297 -0
  26. package/mcp/tools/memory-config.ts +32 -0
  27. package/mcp/tools/memory.ts +369 -0
  28. package/mcp/tools/model-priority.ts +88 -0
  29. package/mcp/tools/openllm.ts +883 -0
  30. package/mcp/types.ts +28 -0
  31. package/openapi/call.ts +90 -0
  32. package/openapi/index.ts +8 -0
  33. package/openapi/openapi.json +80213 -0
  34. package/openapi/operations.ts +2075 -0
  35. package/package.json +39 -4
  36. package/protocol/CONTRIBUTING.md +21 -0
  37. package/protocol/README.md +134 -0
  38. package/protocol/access.ts +186 -0
  39. package/protocol/agent-prompt-text.ts +32 -0
  40. package/protocol/agent.ts +991 -0
  41. package/protocol/api-key-syntax.ts +105 -0
  42. package/protocol/assets/openllm-light.svg +18 -0
  43. package/protocol/assets/openllm.svg +18 -0
  44. package/protocol/audio.ts +29 -0
  45. package/protocol/auth.ts +142 -0
  46. package/protocol/changelog.ts +155 -0
  47. package/protocol/chat.ts +458 -0
  48. package/protocol/client-registry.ts +356 -0
  49. package/protocol/config.ts +345 -0
  50. package/protocol/control-channel.ts +115 -0
  51. package/protocol/cooldown-reason.ts +78 -0
  52. package/protocol/cost.ts +10 -0
  53. package/protocol/daemon-port.ts +31 -0
  54. package/protocol/daemon.ts +1524 -0
  55. package/protocol/default-chains.ts +66 -0
  56. package/protocol/device-state.ts +85 -0
  57. package/protocol/doctor-report-local.ts +443 -0
  58. package/protocol/doctor-report-policy.ts +119 -0
  59. package/protocol/doctor-report.ts +947 -0
  60. package/protocol/errors.ts +14 -0
  61. package/protocol/execution-selection.ts +141 -0
  62. package/protocol/hop-grant.ts +33 -0
  63. package/protocol/image-edit-parse.ts +263 -0
  64. package/protocol/images.ts +298 -0
  65. package/protocol/index.ts +74 -0
  66. package/protocol/inference-headers.ts +3 -0
  67. package/protocol/keepalive.ts +69 -0
  68. package/protocol/local-auth.ts +300 -0
  69. package/protocol/local-device-control.ts +133 -0
  70. package/protocol/local-provider.ts +288 -0
  71. package/protocol/local-session.ts +118 -0
  72. package/protocol/marketplace.ts +187 -0
  73. package/protocol/media-attempt.ts +81 -0
  74. package/protocol/media-bytes.ts +28 -0
  75. package/protocol/media-default.ts +357 -0
  76. package/protocol/media-dimensions.ts +132 -0
  77. package/protocol/media-input.ts +61 -0
  78. package/protocol/media-model.ts +142 -0
  79. package/protocol/media-option-issue.ts +90 -0
  80. package/protocol/media-types.ts +70 -0
  81. package/protocol/model-match.ts +1051 -0
  82. package/protocol/model-settings.ts +327 -0
  83. package/protocol/models.ts +767 -0
  84. package/protocol/mux.ts +722 -0
  85. package/protocol/plan-surface.ts +59 -0
  86. package/protocol/provider-models.ts +16 -0
  87. package/protocol/provider-status.ts +206 -0
  88. package/protocol/provider-tier.ts +67 -0
  89. package/protocol/provider-usage.ts +263 -0
  90. package/protocol/providers.ts +901 -0
  91. package/protocol/public-usage-index.ts +354 -0
  92. package/protocol/realtime-generated.ts +31 -0
  93. package/protocol/realtime.ts +324 -0
  94. package/protocol/registry-pointer.ts +118 -0
  95. package/protocol/relay.ts +702 -0
  96. package/protocol/replay-session.ts +11 -0
  97. package/protocol/responses.ts +280 -0
  98. package/protocol/semver.ts +182 -0
  99. package/protocol/setup-mode.ts +22 -0
  100. package/protocol/sha256-hex.ts +98 -0
  101. package/protocol/stats.ts +406 -0
  102. package/protocol/streaming.ts +19 -0
  103. package/protocol/subscription-daemon-floors.ts +33 -0
  104. package/protocol/subscription-provider-compat.ts +104 -0
  105. package/protocol/subscription-provider.ts +32 -0
  106. package/protocol/team-grant.ts +17 -0
  107. package/protocol/team-shareable.ts +61 -0
  108. package/protocol/transcription-encoding.ts +76 -0
  109. package/protocol/url-locality-dns.ts +37 -0
  110. package/protocol/url-locality.ts +288 -0
  111. package/protocol/usage-snapshots.ts +9 -0
  112. package/protocol/vendor-cli-install.ts +29 -0
  113. package/protocol/video-job-plan.ts +50 -0
  114. package/protocol/videos.ts +227 -0
  115. package/tsconfig.json +14 -0
  116. package/tunnel/ARCHITECTURE.md +111 -0
  117. package/tunnel/LICENSE +33 -0
  118. package/tunnel/README.md +45 -0
  119. package/tunnel/assets/openllm-light.svg +18 -0
  120. package/tunnel/assets/openllm.svg +18 -0
  121. package/tunnel/channel-envelope.ts +139 -0
  122. package/tunnel/codec.ts +191 -0
  123. package/tunnel/device-anchor.ts +229 -0
  124. package/tunnel/device-control.ts +118 -0
  125. package/tunnel/device-grant.ts +173 -0
  126. package/tunnel/fleet.ts +77 -0
  127. package/tunnel/index.ts +22 -0
  128. package/tunnel/mux.ts +580 -0
  129. package/tunnel/peer-link.ts +390 -0
  130. package/tunnel/rtc-auth.ts +415 -0
  131. package/tunnel/rtc-duplex.ts +187 -0
  132. package/tunnel/rtc-ice.ts +136 -0
  133. package/tunnel/secure.ts +1298 -0
  134. package/tunnel/stream-reset-error.ts +14 -0
  135. package/tunnel/streams.ts +1430 -0
  136. package/tunnel/team-grant.ts +302 -0
  137. package/tunnel/wire-bytes.ts +102 -0
  138. package/vault/ARCHITECTURE.md +470 -0
  139. package/vault/LICENSE +33 -0
  140. package/vault/api-key.ts +190 -0
  141. package/vault/argon2.ts +106 -0
  142. package/vault/cli-auth-seal.ts +157 -0
  143. package/vault/cred-seal.ts +146 -0
  144. package/vault/credential.ts +55 -0
  145. package/vault/crypto.ts +210 -0
  146. package/vault/daemon-attest.ts +229 -0
  147. package/vault/derive.ts +113 -0
  148. package/vault/device-access.ts +87 -0
  149. package/vault/device-anchor.ts +185 -0
  150. package/vault/device-control.ts +174 -0
  151. package/vault/handoff.ts +396 -0
  152. package/vault/index.ts +21 -0
  153. package/vault/passkey.ts +243 -0
  154. package/vault/phrase.ts +65 -0
  155. package/vault/pin.ts +153 -0
  156. package/vault/public-key-seal.ts +131 -0
  157. package/vault/seed.ts +272 -0
  158. package/vault/team.ts +355 -0
  159. package/vault/vault-item.ts +131 -0
  160. package/vault/vault-migrate-digest.ts +29 -0
  161. package/vault/wrapped.ts +77 -0
  162. package/wire/ARCHITECTURE.md +90 -0
  163. package/wire/CONTRIBUTING.md +19 -0
  164. package/wire/LICENSE +33 -0
  165. package/wire/README.md +51 -0
  166. package/wire/adapters/messages/anthropic-map.ts +37 -0
  167. package/wire/adapters/messages/index.ts +7 -0
  168. package/wire/adapters/messages/reasoning-from-items.ts +38 -0
  169. package/wire/adapters/messages/reasoning-signature.ts +243 -0
  170. package/wire/adapters/messages/request.ts +574 -0
  171. package/wire/adapters/messages/response.ts +215 -0
  172. package/wire/adapters/messages/streaming.ts +1027 -0
  173. package/wire/adapters/messages/usage.ts +43 -0
  174. package/wire/adapters/responses/index.ts +3 -0
  175. package/wire/adapters/responses/request.ts +395 -0
  176. package/wire/adapters/responses/response.ts +88 -0
  177. package/wire/adapters/responses/streaming.ts +344 -0
  178. package/wire/assets/openllm-light.svg +18 -0
  179. package/wire/assets/openllm.svg +18 -0
  180. package/wire/features/account-rank.ts +166 -0
  181. package/wire/features/compaction/compact-request.ts +862 -0
  182. package/wire/features/compaction/compaction-text.ts +43 -0
  183. package/wire/features/context-demote.ts +47 -0
  184. package/wire/features/context-overflow-strategy.ts +26 -0
  185. package/wire/features/context-skip.ts +170 -0
  186. package/wire/features/conversation-affinity.ts +144 -0
  187. package/wire/features/max-tokens-backfill.ts +141 -0
  188. package/wire/features/model-caps.ts +53 -0
  189. package/wire/features/model-settings.ts +24 -0
  190. package/wire/features/output-limit-clamp.ts +64 -0
  191. package/wire/features/quota-gate.ts +85 -0
  192. package/wire/index.ts +44 -0
  193. package/wire/lib/attempt-outcome.ts +43 -0
  194. package/wire/lib/canonical/content-part.ts +38 -0
  195. package/wire/lib/canonical/count-tokens-capability.ts +138 -0
  196. package/wire/lib/canonical/encoding-select.ts +82 -0
  197. package/wire/lib/canonical/image-signature.ts +202 -0
  198. package/wire/lib/canonical/image-sse.ts +216 -0
  199. package/wire/lib/canonical/message.ts +43 -0
  200. package/wire/lib/canonical/token-estimate.ts +266 -0
  201. package/wire/lib/encrypted-content.ts +66 -0
  202. package/wire/lib/error-class.ts +154 -0
  203. package/wire/lib/error-envelope.ts +56 -0
  204. package/wire/lib/forwarded-headers.ts +180 -0
  205. package/wire/lib/provider-error-rules.ts +48 -0
  206. package/wire/lib/refusal.ts +38 -0
  207. package/wire/lib/responses-apply-patch.ts +104 -0
  208. package/wire/lib/responses-namespace.ts +53 -0
  209. package/wire/lib/secret-scrub.ts +101 -0
  210. package/wire/lib/streaming/accumulate.ts +221 -0
  211. package/wire/lib/streaming/finish-reason.ts +33 -0
  212. package/wire/lib/streaming/peek.ts +154 -0
  213. package/wire/lib/streaming/provider-decode.ts +259 -0
  214. package/wire/lib/streaming/response-stream.ts +71 -0
  215. package/wire/lib/streaming/sse.ts +699 -0
  216. package/wire/lib/streaming/strip-tool-calls.ts +95 -0
  217. package/wire/lib/streaming/upstream-error.ts +58 -0
  218. package/wire/lib/strip-subagent-isolation.ts +284 -0
  219. package/wire/lib/thinking-signature.ts +156 -0
  220. package/wire/lib/token-ruler/bpe-core.ts +426 -0
  221. package/wire/lib/token-ruler/index.ts +2 -0
  222. package/wire/lib/token-ruler/ruler.ts +79 -0
  223. package/wire/lib/token-ruler/vocab/claude-ranks.ts +16 -0
  224. package/wire/lib/token-ruler/vocab/claude.ts +22 -0
  225. package/wire/lib/token-ruler/vocab/o200k-ranks.ts +18 -0
  226. package/wire/lib/token-ruler/vocab/o200k.ts +19 -0
  227. package/wire/lib/tool-name-map.ts +271 -0
  228. package/wire/lib/tool-schema.ts +353 -0
  229. package/wire/lib/usage-observer.ts +125 -0
  230. package/wire/providers/anthropic/adaptive-thinking.ts +291 -0
  231. package/wire/providers/anthropic/beta-headers.ts +165 -0
  232. package/wire/providers/anthropic/index.ts +10 -0
  233. package/wire/providers/anthropic/request.ts +607 -0
  234. package/wire/providers/anthropic/response.ts +155 -0
  235. package/wire/providers/anthropic/streaming.ts +315 -0
  236. package/wire/providers/anthropic/web-search-domains.ts +49 -0
  237. package/wire/providers/chatgpt/common.ts +136 -0
  238. package/wire/providers/chatgpt/index.ts +9 -0
  239. package/wire/providers/chatgpt/request.ts +959 -0
  240. package/wire/providers/chatgpt/streaming.ts +1007 -0
  241. package/wire/providers/chatgpt/web-search.ts +63 -0
  242. package/wire/providers/grok/web-search.ts +87 -0
  243. package/wire/providers/kimi/web-search.ts +145 -0
  244. package/wire/providers/upstream-deny.ts +355 -0
  245. package/wire/providers/upstream-request.ts +619 -0
@@ -0,0 +1,300 @@
1
+ /** Dependency-free local auth command descriptors, mirrored into the standalone CLI. */
2
+ export const AUTH_LOCAL_PATH = "/local/auth";
3
+ export const AUTH_LOCAL_VERSION = 1;
4
+ export const AUTH_LOCAL_MAX_BODY_BYTES = 16_384;
5
+ export const AUTH_CODE_MAX_BYTES = 8_192;
6
+ /** Longest a `status` long-poll (`wait_ms`) may hold before replying unchanged. */
7
+ export const AUTH_WAIT_MAX_MS = 60_000;
8
+ /** The same long-poll over the relay `auth` command: kept well under the
9
+ * browser's command-result wait so a held status never reads as a timeout. */
10
+ export const AUTH_RELAY_WAIT_MAX_MS = 20_000;
11
+
12
+ /** What the daemon acks for a relayed `auth` command: the loopback reply
13
+ * verbatim, with the HTTP status it would have carried. */
14
+ export type TAuthRelayResult = {
15
+ readonly status: number;
16
+ readonly body: Record<string, unknown>;
17
+ };
18
+
19
+ export const AUTH_COMMANDS = [
20
+ {
21
+ name: "providers",
22
+ args: "",
23
+ description: "Discover this daemon provider login methods and actions",
24
+ mutating: false,
25
+ provider: "none",
26
+ fields: [],
27
+ },
28
+ {
29
+ name: "status",
30
+ args: "[provider] [--account <n>] [--flow-id <id>] [--since <revision> --wait-ms <ms>] [--watch]",
31
+ description:
32
+ "Read local provider state and explicitly check an originating login flow; with since + wait_ms, wait until it differs from that revision",
33
+ mutating: false,
34
+ provider: "optional",
35
+ fields: ["account", "flow_id", "since", "wait_ms"],
36
+ },
37
+ {
38
+ name: "usage",
39
+ args: "[provider] [--account <n>]",
40
+ description: "Read cached usage without refreshing credentials or quota",
41
+ mutating: false,
42
+ provider: "optional",
43
+ fields: ["account"],
44
+ },
45
+ {
46
+ name: "refresh",
47
+ args: "[provider] [--account <n>]",
48
+ description:
49
+ "Explicitly refresh usage using the existing manual refresh policy",
50
+ mutating: true,
51
+ provider: "optional",
52
+ fields: ["account"],
53
+ },
54
+ {
55
+ name: "login",
56
+ args: "<provider> [--account <n>] [--method browser|device]",
57
+ description:
58
+ "Start local provider login using its native CLI and vendor consent",
59
+ mutating: true,
60
+ provider: "required",
61
+ fields: ["account", "method"],
62
+ },
63
+ {
64
+ name: "submit-code",
65
+ args: "<provider> [--account <n>] --flow-id <id>",
66
+ description:
67
+ "Submit a paste-back authorization code to the originating login via stdin",
68
+ mutating: true,
69
+ provider: "required",
70
+ fields: ["account", "flow_id", "code"],
71
+ },
72
+ {
73
+ name: "cancel",
74
+ args: "<provider> [--account <n>] [--flow-id <id>]",
75
+ description: "Cancel a pending local provider login",
76
+ mutating: true,
77
+ provider: "required",
78
+ fields: ["account", "flow_id"],
79
+ },
80
+ {
81
+ name: "logout",
82
+ args: "<provider> [--account <n>]",
83
+ description:
84
+ "Sign out of a provider on this machine not the OpenLLM account",
85
+ mutating: true,
86
+ provider: "required",
87
+ fields: ["account"],
88
+ },
89
+ {
90
+ name: "add-account",
91
+ args: "<provider> [--label <label>]",
92
+ description:
93
+ "Add another account slot for a provider on this machine, then log in with --account",
94
+ mutating: true,
95
+ provider: "required",
96
+ fields: ["label"],
97
+ },
98
+ {
99
+ name: "remove-account",
100
+ args: "<provider> --account <n>",
101
+ description:
102
+ "Sign out and delete an extra account slot (account 0 is only logged out)",
103
+ mutating: true,
104
+ provider: "required",
105
+ fields: ["account"],
106
+ },
107
+ {
108
+ name: "pin-account",
109
+ args: "<provider> --account <n>",
110
+ description:
111
+ "Try this account first when routing; others still serve as fallback",
112
+ mutating: true,
113
+ provider: "required",
114
+ fields: ["account"],
115
+ },
116
+ {
117
+ name: "unpin-account",
118
+ args: "<provider>",
119
+ description:
120
+ "Clear the pinned account; routing returns to least-remaining-quota order",
121
+ mutating: true,
122
+ provider: "required",
123
+ fields: [],
124
+ },
125
+ ] as const;
126
+ export type TAuthOperation = (typeof AUTH_COMMANDS)[number]["name"];
127
+ /** Every operation name, as a tuple for literal schemas. */
128
+ export const AUTH_OPERATIONS = AUTH_COMMANDS.map((one) => one.name) as [
129
+ TAuthOperation,
130
+ ...TAuthOperation[],
131
+ ];
132
+ export type TLocalAuthMethod = "browser" | "device";
133
+ export type TLocalAuthRequest = {
134
+ readonly operation: TAuthOperation;
135
+ readonly provider?: string;
136
+ readonly method?: TLocalAuthMethod;
137
+ readonly flow_id?: string;
138
+ readonly code?: string;
139
+ /** Device-local account slot (multi-account); absent ⇒ account 0. */
140
+ readonly account?: number;
141
+ readonly label?: string;
142
+ /** `status` only: the `revision` the caller already holds. */
143
+ readonly since?: string;
144
+ /** `status` only: hold up to this long for a reply whose revision differs from `since`. */
145
+ readonly wait_ms?: number;
146
+ };
147
+
148
+ export const AUTH_ACCOUNT_MAX = 1023;
149
+ export const AUTH_LABEL_MAX_LENGTH = 64;
150
+
151
+ /** Strict on every surface; provider existence is checked against live daemon delegates. */
152
+ export const parseLocalAuthRequest = (input: unknown): TLocalAuthRequest => {
153
+ const invalid = (message = "Invalid local auth request"): never => {
154
+ throw new Error(message);
155
+ };
156
+ if (input === null || typeof input !== "object" || Array.isArray(input))
157
+ return invalid();
158
+ const record = input as Record<string, unknown>;
159
+ const descriptor = AUTH_COMMANDS.find(
160
+ (item) => item.name === record.operation,
161
+ );
162
+ if (descriptor === undefined) return invalid(`Unknown auth operation`);
163
+ const allowed = [
164
+ "operation",
165
+ ...(descriptor.provider !== "none" ? ["provider"] : []),
166
+ ...descriptor.fields,
167
+ ];
168
+ if (Object.keys(record).some((key) => !allowed.includes(key)))
169
+ return invalid(`Unsupported field for ${descriptor.name}`);
170
+ if (descriptor.provider === "required" && record.provider === undefined)
171
+ return invalid(`${descriptor.name} requires a provider`);
172
+ if (
173
+ record.provider !== undefined &&
174
+ (typeof record.provider !== "string" ||
175
+ !/^[a-z][a-z0-9_]{0,63}$/.test(record.provider))
176
+ )
177
+ return invalid("Invalid provider");
178
+ if (
179
+ record.method !== undefined &&
180
+ record.method !== "browser" &&
181
+ record.method !== "device"
182
+ )
183
+ return invalid("--method must be browser or device");
184
+ if (
185
+ record.flow_id !== undefined &&
186
+ (typeof record.flow_id !== "string" ||
187
+ !/^[a-zA-Z0-9_-]{1,128}$/.test(record.flow_id))
188
+ )
189
+ return invalid("Invalid --flow-id");
190
+ if (record.flow_id !== undefined && record.provider === undefined)
191
+ return invalid("--flow-id requires a provider");
192
+ if (
193
+ record.account !== undefined &&
194
+ (typeof record.account !== "number" ||
195
+ !Number.isSafeInteger(record.account) ||
196
+ record.account < 0 ||
197
+ record.account > AUTH_ACCOUNT_MAX)
198
+ )
199
+ return invalid(`Invalid --account (0–${AUTH_ACCOUNT_MAX})`);
200
+ if (
201
+ descriptor.name === "remove-account" &&
202
+ (typeof record.account !== "number" || record.account < 1)
203
+ )
204
+ return invalid("remove-account requires --account ≥ 1");
205
+ if (descriptor.name === "pin-account" && record.account === undefined)
206
+ return invalid("pin-account requires --account");
207
+ if (
208
+ record.label !== undefined &&
209
+ (typeof record.label !== "string" ||
210
+ record.label.length === 0 ||
211
+ record.label.length > AUTH_LABEL_MAX_LENGTH ||
212
+ Array.from(record.label).some(
213
+ (character) =>
214
+ character.charCodeAt(0) < 32 || character.charCodeAt(0) === 127,
215
+ ))
216
+ )
217
+ return invalid("Invalid --label");
218
+ if (
219
+ record.since !== undefined &&
220
+ (typeof record.since !== "string" || !/^[a-f0-9]{1,64}$/.test(record.since))
221
+ )
222
+ return invalid("--since must be a hex revision");
223
+ if (
224
+ record.wait_ms !== undefined &&
225
+ (typeof record.wait_ms !== "number" ||
226
+ !Number.isSafeInteger(record.wait_ms) ||
227
+ record.wait_ms < 0 ||
228
+ record.wait_ms > AUTH_WAIT_MAX_MS)
229
+ )
230
+ return invalid(
231
+ `--wait-ms must be an integer from 0 to ${AUTH_WAIT_MAX_MS}`,
232
+ );
233
+ if (descriptor.name === "submit-code") {
234
+ if (
235
+ record.flow_id === undefined ||
236
+ typeof record.code !== "string" ||
237
+ record.code.length === 0 ||
238
+ new TextEncoder().encode(record.code).length > AUTH_CODE_MAX_BYTES ||
239
+ Array.from(record.code).some(
240
+ (character) =>
241
+ character.charCodeAt(0) <= 32 || character.charCodeAt(0) === 127,
242
+ )
243
+ )
244
+ return invalid(
245
+ "submit-code requires --flow-id and a single code on stdin; never put login codes in arguments",
246
+ );
247
+ }
248
+ return record as TLocalAuthRequest;
249
+ };
250
+
251
+ export const authHelp = (binary: string): string =>
252
+ `${AUTH_COMMANDS.map((item) => `${binary} auth ${item.name}${item.args ? ` ${item.args}` : ""} [--json]\n ${item.description}`).join("\n")}\n\nstatus --watch prints one JSON snapshot per line (NDJSON with --json) each time the state changes, until the flow ends or Ctrl-C.\nLogin acceptance is not authentication success. Follow vendor consent and check status explicitly.\nsubmit-code reads the code from stdin, never an argument. No remote devices or cloud fallback.\n`;
253
+
254
+ const AUTH_OPTION_KEYS = {
255
+ "--method": "method",
256
+ "--flow-id": "flow_id",
257
+ "--account": "account",
258
+ "--label": "label",
259
+ "--since": "since",
260
+ "--wait-ms": "wait_ms",
261
+ } as const;
262
+ type TAuthOption = keyof typeof AUTH_OPTION_KEYS;
263
+
264
+ export const parseAuthArgs = (
265
+ args: readonly string[],
266
+ code?: string,
267
+ ): TLocalAuthRequest => {
268
+ const request: Record<string, unknown> = { operation: args[0] };
269
+ for (let index = 1; index < args.length; index += 1) {
270
+ const arg = args[index];
271
+ if (arg === "--json") continue;
272
+ if (arg === "--watch") {
273
+ if (args[0] !== "status") throw new Error("--watch is status only");
274
+ continue;
275
+ }
276
+ const key =
277
+ arg !== undefined && Object.hasOwn(AUTH_OPTION_KEYS, arg)
278
+ ? AUTH_OPTION_KEYS[arg as TAuthOption]
279
+ : undefined;
280
+ if (key !== undefined) {
281
+ const value = args[index + 1];
282
+ if (request[key] !== undefined || value === undefined)
283
+ throw new Error("Missing or repeated auth option");
284
+ index += 1;
285
+ request[key] =
286
+ (key === "account" && /^[0-9]{1,4}$/.test(value)) ||
287
+ (key === "wait_ms" && /^[0-9]{1,5}$/.test(value))
288
+ ? Number(value)
289
+ : value;
290
+ } else if (
291
+ arg !== undefined &&
292
+ !arg.startsWith("-") &&
293
+ request.provider === undefined
294
+ )
295
+ request.provider = arg;
296
+ else throw new Error("Unknown or repeated auth argument");
297
+ }
298
+ if (code !== undefined) request.code = code;
299
+ return parseLocalAuthRequest(request);
300
+ };
@@ -0,0 +1,133 @@
1
+ /**
2
+ * Loopback device-control surface — the TOFU-free way to pin a daemon's
3
+ * device-control key and read its X25519 identity.
4
+ *
5
+ * The desktop app (or a local CLI) reaches these on `127.0.0.1` with the
6
+ * boot's local control capability, the same admission as `/local/agent`.
7
+ * Because the caller is on the machine, neither value crosses the cloud, so
8
+ * neither is trust-on-first-use:
9
+ *
10
+ * - `GET /local/identity` → the daemon's X25519 key (`dpk`) for the
11
+ * browser to attest with its control key;
12
+ * - `POST /local/device-control` → pin the account control public key
13
+ * directly (`via: "local"`, an anchored pin).
14
+ * - `POST /local/device-anchor` → install or remove the device trust anchor
15
+ * (sessions on == anchor present).
16
+ */
17
+ import { Schema as S } from "effect";
18
+
19
+ export const IDENTITY_LOCAL_PATH = "/local/identity";
20
+ export const DEVICE_CONTROL_LOCAL_PATH = "/local/device-control";
21
+ export const DEVICE_CONTROL_LOCAL_MAX_BODY_BYTES = 1024;
22
+
23
+ /** The daemon's device-control pin as reported locally / in `DaemonStatus`. */
24
+ export const DeviceAccessPinReport = S.Struct({
25
+ fp: S.String.check(S.isMaxLength(64)),
26
+ kind: S.Literals(["control", "legacy"]),
27
+ via: S.optional(S.Literals(["anchor", "tofu", "local", "rotation"])),
28
+ });
29
+ export type TDeviceAccessPinReport = S.Schema.Type<
30
+ typeof DeviceAccessPinReport
31
+ >;
32
+
33
+ /**
34
+ * `DaemonStatus.device_sessions` — whether this device can host agent sessions
35
+ * and the daemon's proof of its X25519 key.
36
+ *
37
+ * - `anchored`: the pin descends from a local trust root (install / login /
38
+ * desktop loopback anchor). True == device sessions are on. False → agent
39
+ * sessions fail closed with `device_not_anchored`; offer "Set up device
40
+ * sessions".
41
+ * - `dpk_proof`: `HMAC-SHA256(pair_secret, "openllm-dpk-proof-v1" ||
42
+ * key_id || pubkey)`, base64url. Verify with `@openllmsh/sdk/vault`
43
+ * `verifyDpkProof` BEFORE using `DaemonStatus.pubkey` as `dpk`. Absent when
44
+ * the daemon holds no anchor for the key it runs on.
45
+ */
46
+ export const DeviceSessionsReport = S.Struct({
47
+ anchored: S.Boolean,
48
+ dpk_proof: S.optional(S.String.check(S.isMaxLength(64))),
49
+ });
50
+ export type TDeviceSessionsReport = S.Schema.Type<typeof DeviceSessionsReport>;
51
+
52
+ /** `GET /local/identity` response. */
53
+ export const LocalIdentityResponse = S.Struct({
54
+ /** This daemon's X25519 public key (SPKI DER, base64) — `DaemonStatus.pubkey`. */
55
+ pubkey: S.String,
56
+ /** `keyFingerprint(pubkey)` — what `openllm daemon identity` prints. */
57
+ fingerprint: S.String,
58
+ device_access_pin: S.NullOr(DeviceAccessPinReport),
59
+ /** Same as `DaemonStatus.device_sessions`. Absent on older daemons. */
60
+ device_sessions: S.optional(DeviceSessionsReport),
61
+ });
62
+ export type TLocalIdentityResponse = S.Schema.Type<
63
+ typeof LocalIdentityResponse
64
+ >;
65
+
66
+ /** `POST /local/device-control` body. */
67
+ export const LocalDeviceControlRequest = S.Struct({
68
+ /** The account control public key (Ed25519 SPKI DER, base64). */
69
+ device_control_pubkey: S.String.check(S.isMinLength(1), S.isMaxLength(64)),
70
+ /** Its generation (`users.device_control_version`), when known. */
71
+ version: S.optional(
72
+ S.NullOr(S.Number.check(S.isInt(), S.isGreaterThanOrEqualTo(1))),
73
+ ),
74
+ });
75
+ export type TLocalDeviceControlRequest = S.Schema.Type<
76
+ typeof LocalDeviceControlRequest
77
+ >;
78
+
79
+ /**
80
+ * `POST /local/device-anchor` — install or remove this machine's trust anchor
81
+ * over loopback (the desktop app's Device sessions toggle). Same admission as
82
+ * `/local/device-control`. Install refuses unless `key_id` is the key this
83
+ * daemon runs on, writes `OPENLLM_DEVICE_ANCHOR` with the same writer as
84
+ * `openllm daemon anchor`, and re-derives its pin. Remove blanks that line.
85
+ * The anchor is never logged or echoed back. Sessions on == anchor present.
86
+ */
87
+ export const DEVICE_ANCHOR_LOCAL_PATH = "/local/device-anchor";
88
+ export const DEVICE_ANCHOR_LOCAL_MAX_BODY_BYTES = 512;
89
+
90
+ /** `POST /local/device-anchor` body — install or remove. */
91
+ export const LocalDeviceAnchorRequest = S.Union([
92
+ S.Struct({
93
+ /** The API key id the anchor was derived for (`sk-llm-<key_id>.…`). */
94
+ key_id: S.String.check(S.isMinLength(1), S.isMaxLength(64)),
95
+ /** The 86-char base64url anchor (`buildDeviceAnchor(seed, key_id)`). */
96
+ anchor: S.String.check(S.isMinLength(1), S.isMaxLength(128)),
97
+ }),
98
+ S.Struct({
99
+ key_id: S.String.check(S.isMinLength(1), S.isMaxLength(64)),
100
+ /** Blank `OPENLLM_DEVICE_ANCHOR` — sessions off. */
101
+ remove: S.Literal(true),
102
+ }),
103
+ ]);
104
+ export type TLocalDeviceAnchorRequest = S.Schema.Type<
105
+ typeof LocalDeviceAnchorRequest
106
+ >;
107
+
108
+ /** Why `POST /local/device-anchor` refused. */
109
+ export const LocalDeviceAnchorRefusal = S.Literals([
110
+ "invalid_body",
111
+ "body_too_large",
112
+ "malformed_anchor",
113
+ "key_id_mismatch",
114
+ "persist_failed",
115
+ ]);
116
+ export type TLocalDeviceAnchorRefusal = S.Schema.Type<
117
+ typeof LocalDeviceAnchorRefusal
118
+ >;
119
+
120
+ /** `POST /local/device-anchor` response. */
121
+ export const LocalDeviceAnchorResponse = S.Union([
122
+ S.Struct({
123
+ ok: S.Literal(true),
124
+ device_sessions: DeviceSessionsReport,
125
+ }),
126
+ S.Struct({
127
+ ok: S.Literal(false),
128
+ error: LocalDeviceAnchorRefusal,
129
+ }),
130
+ ]);
131
+ export type TLocalDeviceAnchorResponse = S.Schema.Type<
132
+ typeof LocalDeviceAnchorResponse
133
+ >;