@aithos/sdk 0.1.0-alpha.2 → 0.1.0-alpha.21

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 (57) hide show
  1. package/README.md +45 -0
  2. package/dist/src/auth-api.d.ts +50 -0
  3. package/dist/src/auth-api.js +102 -0
  4. package/dist/src/auth.d.ts +253 -0
  5. package/dist/src/auth.js +940 -0
  6. package/dist/src/compute.d.ts +370 -9
  7. package/dist/src/compute.js +369 -16
  8. package/dist/src/ethos.d.ts +164 -1
  9. package/dist/src/ethos.js +729 -16
  10. package/dist/src/index.d.ts +11 -4
  11. package/dist/src/index.js +31 -5
  12. package/dist/src/internal/delegate-bundle.d.ts +18 -0
  13. package/dist/src/internal/delegate-bundle.js +94 -0
  14. package/dist/src/internal/delegate-state.d.ts +45 -0
  15. package/dist/src/internal/delegate-state.js +120 -0
  16. package/dist/src/internal/owner-signers.d.ts +78 -0
  17. package/dist/src/internal/owner-signers.js +179 -0
  18. package/dist/src/internal/protocol-client-bridge.d.ts +8 -0
  19. package/dist/src/internal/protocol-client-bridge.js +20 -0
  20. package/dist/src/internal/recovery-file.d.ts +29 -0
  21. package/dist/src/internal/recovery-file.js +98 -0
  22. package/dist/src/internal/signer.d.ts +59 -0
  23. package/dist/src/internal/signer.js +86 -0
  24. package/dist/src/key-store.d.ts +128 -0
  25. package/dist/src/key-store.js +244 -0
  26. package/dist/src/mandates.d.ts +163 -1
  27. package/dist/src/mandates.js +286 -8
  28. package/dist/src/sdk.d.ts +36 -3
  29. package/dist/src/sdk.js +27 -23
  30. package/dist/src/session-store.d.ts +58 -0
  31. package/dist/src/session-store.js +158 -0
  32. package/dist/src/wallet.d.ts +4 -6
  33. package/dist/src/wallet.js +18 -8
  34. package/dist/test/auth-j3.test.d.ts +2 -0
  35. package/dist/test/auth-j3.test.js +391 -0
  36. package/dist/test/auth.test.d.ts +2 -0
  37. package/dist/test/auth.test.js +175 -0
  38. package/dist/test/compute-delegate-path.test.d.ts +2 -0
  39. package/dist/test/compute-delegate-path.test.js +183 -0
  40. package/dist/test/compute.test.js +184 -11
  41. package/dist/test/ethos-first-edition.test.d.ts +2 -0
  42. package/dist/test/ethos-first-edition.test.js +248 -0
  43. package/dist/test/ethos.test.d.ts +2 -0
  44. package/dist/test/ethos.test.js +219 -0
  45. package/dist/test/key-store.test.d.ts +2 -0
  46. package/dist/test/key-store.test.js +161 -0
  47. package/dist/test/mandates-compute.test.d.ts +2 -0
  48. package/dist/test/mandates-compute.test.js +256 -0
  49. package/dist/test/mandates.test.d.ts +2 -0
  50. package/dist/test/mandates.test.js +93 -0
  51. package/dist/test/sdk.test.js +70 -30
  52. package/dist/test/signer.test.d.ts +2 -0
  53. package/dist/test/signer.test.js +117 -0
  54. package/dist/test/signup-bootstrap.test.d.ts +2 -0
  55. package/dist/test/signup-bootstrap.test.js +222 -0
  56. package/dist/test/wallet.test.js +20 -9
  57. package/package.json +4 -3
@@ -1,16 +1,31 @@
1
- import { type BrowserIdentity } from "@aithos/protocol-client";
1
+ import type { AithosAuth } from "./auth.js";
2
2
  import { type AithosSdkEndpoints } from "./endpoints.js";
3
3
  export interface ComputeMessage {
4
4
  readonly role: "user" | "assistant";
5
5
  readonly content: string;
6
6
  }
7
7
  export interface InvokeBedrockArgs {
8
- /** Mandate ID granting this app the right to spend the user's wallet. */
9
- readonly mandateId: string;
8
+ /**
9
+ * Mandate ID under which this call should be attributed.
10
+ *
11
+ * - **Owner sessions**: optional. The SDK uses the owner's own DID
12
+ * as a sentinel "self" mandate id — the proxy skips all
13
+ * mandate-related checks (scope, allowed_models, caps) when the
14
+ * envelope is owner-signed, so the value is informational only.
15
+ * - **Delegate sessions**: required. Must reference the imported
16
+ * mandate bundle the SDK signs with (the proxy enforces
17
+ * `compute.invoke` scope and any `allowed_models` filter).
18
+ */
19
+ readonly mandateId?: string;
10
20
  /**
11
21
  * Model id. Today the proxy accepts the canonical Aithos identifiers:
12
- * `claude-sonnet-4-6`, `claude-haiku-4-5`, `claude-opus-4-7`.
22
+ * `claude-sonnet-4-6`, `claude-haiku-4-5`, `claude-opus-4-6`.
13
23
  * The proxy maps these to Bedrock cross-region inference profiles.
24
+ *
25
+ * NOTE: `claude-opus-4-7` is also provisioned on the Bedrock account
26
+ * but commercial access is gated behind an AWS Sales unlock (as of
27
+ * May 2026) — InvokeModel returns AccessDeniedException pointing to
28
+ * AWS Sales. Stick with `claude-opus-4-6` until the unlock lands.
14
29
  */
15
30
  readonly model: string;
16
31
  /** Conversation messages (user / assistant turns). */
@@ -44,16 +59,263 @@ export interface InvokeBedrockResult {
44
59
  /** Audit log id for traceability. */
45
60
  readonly auditId: string;
46
61
  }
62
+ /**
63
+ * Stable cross-provider image model ids supported by the Aithos compute
64
+ * proxy. New models can be added on the server side without an SDK
65
+ * release — but tagging the namespaced literal here gives consumers
66
+ * autocomplete + type-checking.
67
+ *
68
+ * The `image:` prefix is part of the wire contract: the server uses it
69
+ * to disambiguate text and image dispatch when a mandate's
70
+ * `allowed_models` mixes both.
71
+ */
72
+ export type ImageModelId = "image:flux-schnell" | "image:flux-dev" | "image:flux-pro-1.1" | "image:flux-pro-1.1-ultra" | "image:imagen-3" | "image:imagen-4" | "image:nano-banana";
73
+ /** Aspect ratios accepted by `invokeImage`. */
74
+ export type ImageAspectRatio = "1:1" | "16:9" | "9:16" | "4:3" | "3:4" | "21:9";
75
+ export interface InvokeImageArgs {
76
+ /**
77
+ * Mandate ID under which this call should be attributed.
78
+ *
79
+ * - **Owner sessions**: optional. The SDK uses the owner's own DID
80
+ * as a sentinel "self" mandate id — the proxy skips all
81
+ * mandate-related checks when the envelope is owner-signed.
82
+ * - **Delegate sessions**: required. Must reference the imported
83
+ * mandate bundle the SDK signs with.
84
+ */
85
+ readonly mandateId?: string;
86
+ /**
87
+ * Image model id. Defaults to `"image:flux-pro-1.1"` on the SDK side
88
+ * if omitted — the server allowlist is the source of truth for which
89
+ * ones actually work.
90
+ */
91
+ readonly model?: ImageModelId;
92
+ /** What to draw. */
93
+ readonly prompt: string;
94
+ /** Optional comma-separated list of things to avoid (e.g. "blurry, watermark"). */
95
+ readonly negativePrompt?: string;
96
+ /** Default 1:1. */
97
+ readonly aspectRatio?: ImageAspectRatio;
98
+ /** Deterministic seed (re-rolls). Omit for random. */
99
+ readonly seed?: number;
100
+ /** Number of images to generate. Default 1, max 4. */
101
+ readonly numberOfImages?: number;
102
+ /** Idempotency key for retries (generated if omitted). */
103
+ readonly idempotencyKey?: string;
104
+ /** Abort signal to cancel the request (network + provider call). */
105
+ readonly signal?: AbortSignal;
106
+ }
107
+ export interface InvokeImageImage {
108
+ /** Base64-encoded image bytes (raw, no data: URI prefix). */
109
+ readonly base64: string;
110
+ /** "image/png" | "image/jpeg". */
111
+ readonly contentType: string;
112
+ readonly width: number;
113
+ readonly height: number;
114
+ }
115
+ export interface InvokeImageResult {
116
+ readonly images: readonly InvokeImageImage[];
117
+ /** Seed actually used by the provider (echoed back even when caller didn't set one). */
118
+ readonly seed: number;
119
+ /** Microcredits debited from the wallet (exact — no reconcile path for images). */
120
+ readonly creditsCharged: number;
121
+ /** Wallet balance after debit. */
122
+ readonly walletBalance: number;
123
+ /** Audit log id for traceability. */
124
+ readonly auditId: string;
125
+ }
126
+ export interface InvokeBedrockVisionArgs {
127
+ readonly mandateId?: string;
128
+ /**
129
+ * Model id. Sonnet 4.6 is the default — it's vision-capable and
130
+ * returns reliable structured JSON when prompted.
131
+ */
132
+ readonly model?: string;
133
+ /** Source image — Blob (recommended) or raw base64. */
134
+ readonly image: Blob | {
135
+ readonly base64: string;
136
+ readonly contentType: string;
137
+ };
138
+ /** Text prompt accompanying the image. */
139
+ readonly prompt: string;
140
+ /** Optional system prompt. */
141
+ readonly system?: string;
142
+ readonly maxTokens?: number;
143
+ readonly temperature?: number;
144
+ readonly idempotencyKey?: string;
145
+ readonly signal?: AbortSignal;
146
+ }
147
+ export interface InvokeBedrockVisionResult {
148
+ readonly content: string;
149
+ readonly stopReason: StopReason;
150
+ readonly usage: {
151
+ readonly inputTokens: number;
152
+ readonly outputTokens: number;
153
+ };
154
+ readonly creditsCharged: number;
155
+ readonly walletBalance: number;
156
+ readonly auditId: string;
157
+ }
158
+ export interface InvokeSegmentationArgs {
159
+ /** Mandate id (optional for owner sessions — see InvokeImageArgs). */
160
+ readonly mandateId?: string;
161
+ /** Source image. Blob (recommended) or raw base64. */
162
+ readonly image: Blob | {
163
+ readonly base64: string;
164
+ readonly contentType: string;
165
+ };
166
+ /**
167
+ * Text phrase describing what to segment. Florence-2 is robust with
168
+ * natural-language descriptions: "the torso of the robot", "the
169
+ * dog's head", "the chest of the character".
170
+ */
171
+ readonly textInput: string;
172
+ readonly idempotencyKey?: string;
173
+ readonly signal?: AbortSignal;
174
+ }
175
+ export interface SegmentPolygon {
176
+ readonly points: ReadonlyArray<{
177
+ readonly x: number;
178
+ readonly y: number;
179
+ }>;
180
+ }
181
+ export interface InvokeSegmentationResult {
182
+ /** All polygons Florence-2 returned (typically 1, sometimes a few when the prompt matches multiple regions). */
183
+ readonly polygons: readonly SegmentPolygon[];
184
+ /** Bbox of the first polygon for callers that only need a coarse target. */
185
+ readonly bbox: {
186
+ readonly left: number;
187
+ readonly top: number;
188
+ readonly right: number;
189
+ readonly bottom: number;
190
+ } | null;
191
+ readonly creditsCharged: number;
192
+ readonly walletBalance: number;
193
+ readonly auditId: string;
194
+ }
195
+ /**
196
+ * Models accepted by `invokeUrlFetch`. These are the Anthropic-API-direct
197
+ * model aliases — the proxy resolves them to the canonical API model id
198
+ * (`claude-haiku-4-5-20251001`, etc.) at dispatch time.
199
+ *
200
+ * Bedrock's compute_invoke supports the same names but routes through
201
+ * Bedrock; url_fetch routes through `api.anthropic.com` directly because
202
+ * the `web_fetch` server-side tool isn't exposed via Bedrock.
203
+ */
204
+ export type UrlFetchModelId = "claude-haiku-4-5" | "claude-sonnet-4-6" | "claude-opus-4-6";
205
+ export interface InvokeUrlFetchArgs {
206
+ /**
207
+ * Mandate ID under which this call should be attributed.
208
+ *
209
+ * - **Owner sessions**: optional. The owner's own DID is used as a
210
+ * sentinel "self" mandate id; the proxy skips all mandate checks
211
+ * for owner-signed envelopes.
212
+ * - **Delegate sessions**: required. Must reference an imported
213
+ * mandate bundle that carries the `compute.url_fetch` scope (NOT
214
+ * the same scope as `compute.invoke` — see the protocol spec).
215
+ */
216
+ readonly mandateId?: string;
217
+ /**
218
+ * Anthropic model alias. Default `"claude-haiku-4-5"` — fastest and
219
+ * cheapest model that supports `web_fetch`. Bump to Sonnet 4.6 for
220
+ * deeper reasoning over the fetched content; Opus 4.6 for the
221
+ * heaviest analyses (priced accordingly via reconcile). Opus 4.7 is
222
+ * provisioned but commercially gated — see the docstring on
223
+ * InvokeBedrockArgs.model for the unlock path.
224
+ */
225
+ readonly model?: UrlFetchModelId;
226
+ /**
227
+ * User prompt — should normally contain the URL(s) the caller wants
228
+ * the model to fetch and analyze. Example:
229
+ * "Voici l'URL https://tata.com — résume le service, identifie
230
+ * le style du site et la couleur primaire. Renvoie en JSON."
231
+ */
232
+ readonly prompt: string;
233
+ /** Optional system prompt (Anthropic-style separate field). */
234
+ readonly system?: string;
235
+ /** Cap on output tokens. Default 2048. */
236
+ readonly maxTokens?: number;
237
+ /** Sampling temperature. Default model-dependent. */
238
+ readonly temperature?: number;
239
+ /**
240
+ * Maximum number of `web_fetch` invocations the model is allowed to
241
+ * make in this call. Default 5; range [1, 10].
242
+ */
243
+ readonly maxFetches?: number;
244
+ /**
245
+ * Maximum tokens of fetched content Anthropic will inject per fetch.
246
+ * Default 100_000; range [1000, 200_000]. Pages larger than this
247
+ * are truncated server-side.
248
+ */
249
+ readonly maxContentTokens?: number;
250
+ /**
251
+ * When `true` (default), the response carries citation spans tying
252
+ * generated text back to fetched documents — useful for displaying
253
+ * "selon X, …" footnotes in the UI without post-processing.
254
+ */
255
+ readonly citations?: boolean;
256
+ /**
257
+ * Optional domain allowlist — if set, the model can only fetch from
258
+ * these domains. Mutually exclusive with `blockedDomains`.
259
+ */
260
+ readonly allowedDomains?: readonly string[];
261
+ /**
262
+ * Optional domain blocklist. Mutually exclusive with `allowedDomains`.
263
+ */
264
+ readonly blockedDomains?: readonly string[];
265
+ /** Idempotency key for retries (generated if omitted). */
266
+ readonly idempotencyKey?: string;
267
+ /** Abort signal to cancel the request. */
268
+ readonly signal?: AbortSignal;
269
+ }
270
+ /**
271
+ * Single citation projection — flattened from Anthropic's wire shape so
272
+ * consumers can render `{cited_text}` next to `{url}` without parsing
273
+ * vendor-specific block types.
274
+ */
275
+ export interface UrlFetchCitation {
276
+ readonly url: string;
277
+ readonly citedText: string;
278
+ readonly documentTitle?: string;
279
+ readonly startCharIndex?: number;
280
+ readonly endCharIndex?: number;
281
+ }
282
+ /** Per-fetch metadata in the order the model performed the fetches. */
283
+ export interface UrlFetchMetadata {
284
+ readonly url: string;
285
+ readonly retrievedAt?: string;
286
+ readonly title?: string;
287
+ }
288
+ export interface InvokeUrlFetchResult {
289
+ /** Final assistant text — typically a JSON string when the prompt asked for structure. */
290
+ readonly content: string;
291
+ /** Citation spans (empty when `citations: false` or no fetches succeeded). */
292
+ readonly citations: readonly UrlFetchCitation[];
293
+ /** Per-URL fetch metadata. */
294
+ readonly urlsFetched: readonly UrlFetchMetadata[];
295
+ readonly stopReason: "end_turn" | "max_tokens" | "tool_use" | "stop_sequence" | "pause_turn" | "refusal";
296
+ readonly usage: {
297
+ readonly inputTokens: number;
298
+ readonly outputTokens: number;
299
+ readonly webFetchInvocations: number;
300
+ };
301
+ /** Microcredits charged after reconcile (already net of any refund). */
302
+ readonly creditsCharged: number;
303
+ /** Wallet balance after the debit + reconcile. */
304
+ readonly walletBalance: number;
305
+ /** Audit log id for traceability. */
306
+ readonly auditId: string;
307
+ }
47
308
  export interface ComputeNamespaceDeps {
48
- readonly identity: BrowserIdentity;
309
+ readonly auth: AithosAuth;
49
310
  readonly appDid: string;
50
311
  readonly endpoints: AithosSdkEndpoints;
51
312
  readonly fetch: typeof fetch;
52
313
  }
53
314
  /**
54
315
  * `sdk.compute` namespace. Constructed once by the {@link AithosSDK}
55
- * constructor; all calls share the SDK's identity, app DID, and endpoint
56
- * configuration.
316
+ * constructor; reads the active owner from the supplied
317
+ * {@link AithosAuth} on every call so signing material follows the
318
+ * latest sign-in/sign-out state.
57
319
  */
58
320
  export declare class ComputeNamespace {
59
321
  #private;
@@ -62,10 +324,109 @@ export declare class ComputeNamespace {
62
324
  * Invoke a Bedrock model through the compute proxy. See
63
325
  * {@link InvokeBedrockArgs} and {@link InvokeBedrockResult}.
64
326
  *
327
+ * Two signer paths are supported:
328
+ *
329
+ * - **Owner**: when the caller is signed in as an owner, the
330
+ * envelope is signed with the owner's `#public` sphere key and
331
+ * no mandate is attached (the proxy resolves the mandate
332
+ * server-side from `params.mandate_id`).
333
+ * - **Delegate**: when the caller is delegate-only (mandate
334
+ * imported via `auth.importMandate`), the envelope is signed
335
+ * with the delegate's bound keypair and the full SignedMandate
336
+ * is attached so the proxy can verify both signature and
337
+ * authorisation in one pass. The mandate must carry the
338
+ * `compute.invoke` scope and the proxy enforces its constraints
339
+ * (caps, allowed models, …) at server-side.
340
+ *
341
+ * Owner takes precedence: if a session has BOTH an owner and a
342
+ * matching delegate session, we use the owner key (more flexible —
343
+ * the mandate is dereferenced via `mandate_id` and there's no
344
+ * lifetime cliff if the delegate seed has been wiped).
345
+ *
65
346
  * @throws {AithosSDKError} on protocol errors. The `code` field is one of
66
- * `network`, `http`, `empty`, or any code returned by the proxy
67
- * (`quota_exceeded`, `mandate_revoked`, `insufficient_credits`, …).
347
+ * `sdk_no_signer`, `sdk_no_delegate_for_mandate`, `network`, `http`,
348
+ * `empty`, or any code returned by the proxy (`quota_exceeded`,
349
+ * `mandate_revoked`, `insufficient_credits`, …).
68
350
  */
69
351
  invokeBedrock(args: InvokeBedrockArgs): Promise<InvokeBedrockResult>;
352
+ /**
353
+ * Multimodal Bedrock invoke — image + text → text response.
354
+ * Default model: `claude-sonnet-4-6` (vision-capable, reliable JSON).
355
+ *
356
+ * Use when you need a VLM to reason about an image: locating
357
+ * features, structured extraction, semantic Q&A. Prompt the model
358
+ * to return JSON if you need structured output (the API itself is
359
+ * unstructured).
360
+ */
361
+ invokeBedrockVision(args: InvokeBedrockVisionArgs): Promise<InvokeBedrockVisionResult>;
362
+ /**
363
+ * Generate one or more images through the Aithos compute proxy
364
+ * (currently powered by fal.ai FLUX models). Spec mirror of
365
+ * {@link invokeBedrock}: same envelope, same wallet path, same
366
+ * mandate-scope gate (`compute.invoke` + `allowed_models`). The
367
+ * separation at the JSON-RPC method level (`aithos.compute_invoke_image`
368
+ * vs `aithos.compute_invoke`) commits each envelope to a specific
369
+ * modality so a stolen text-invoke envelope cannot be replayed
370
+ * against the image endpoint.
371
+ *
372
+ * Default model: `"image:flux-pro-1.1"`. Default aspect ratio: 1:1.
373
+ * Default count: 1 image.
374
+ *
375
+ * Pricing is per image and deterministic (no token-based reconcile):
376
+ * - flux-schnell: 3 000 mc + fee per image
377
+ * - flux-dev: 25 000 mc + fee per image
378
+ * - flux-pro-1.1: 40 000 mc + fee per image
379
+ * - flux-pro-1.1-ultra: 60 000 mc + fee per image
380
+ */
381
+ invokeImage(args: InvokeImageArgs): Promise<InvokeImageResult>;
382
+ /**
383
+ * Run text-prompted segmentation (Florence-2 referring-expression)
384
+ * on a source image. Returns one or more polygons hugging the
385
+ * region matching the text prompt.
386
+ *
387
+ * Use cases: locate the chest/torso area of a generated mascot
388
+ * for logo compositing, find the face zone for a thumbnail crop,
389
+ * extract a product from a marketing shot — anything that needs
390
+ * a precise mask + bbox from natural-language description.
391
+ *
392
+ * Pricing: flat 5 000 mc per call (~$0.005 — Florence-2 is cheap).
393
+ */
394
+ invokeSegmentation(args: InvokeSegmentationArgs): Promise<InvokeSegmentationResult>;
395
+ /**
396
+ * Fetch one or more URLs and have Claude analyze the content. Routes
397
+ * through `api.anthropic.com` with the `web_fetch` server-side tool —
398
+ * NOT through Bedrock — because Bedrock does not expose Anthropic's
399
+ * server-side tools (web_fetch, web_search, etc.). The proxy hides
400
+ * this multi-backend detail from the SDK consumer; the wallet,
401
+ * envelope, and mandate-scope contracts are unchanged.
402
+ *
403
+ * Typical use:
404
+ * ```ts
405
+ * const r = await sdk.compute.invokeUrlFetch({
406
+ * prompt: "Voici l'URL https://tata.com — résume le service, " +
407
+ * "identifie le style et la couleur primaire. JSON.",
408
+ * });
409
+ * console.log(r.content);
410
+ * for (const c of r.citations) console.log(c.url, "→", c.citedText);
411
+ * ```
412
+ *
413
+ * Mandate scope: requires `compute.url_fetch` (distinct from
414
+ * `compute.invoke` — see {@link InvokeUrlFetchArgs.mandateId}).
415
+ *
416
+ * Pricing: same Claude 4.x rates as Bedrock (Anthropic's direct API
417
+ * and Bedrock list prices are identical). Default Haiku 4.5 ≈
418
+ * $0.001/1k input + $0.005/1k output. The proxy pre-debits a
419
+ * conservative upper bound (`maxFetches × maxContentTokens × input
420
+ * rate + maxTokens × output rate`) and reconciles down to the actual
421
+ * usage Anthropic reports — so the wallet is always charged the
422
+ * exact post-call cost.
423
+ *
424
+ * @throws {AithosSDKError} on protocol errors. Notable codes:
425
+ * `sdk_no_signer`, `sdk_no_delegate_for_mandate`, `network`,
426
+ * `http`, `empty`, plus proxy codes `-32042` (mandate scope
427
+ * missing), `-32070`/`-32071` (wallet), `-32074` (fetch blocked
428
+ * by robots.txt / domain filter), `-32050` (rate limit).
429
+ */
430
+ invokeUrlFetch(args: InvokeUrlFetchArgs): Promise<InvokeUrlFetchResult>;
70
431
  }
71
432
  //# sourceMappingURL=compute.d.ts.map