@aithos/sdk 0.1.0-alpha.5 → 0.1.0-alpha.51
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.
- package/README.md +245 -7
- package/dist/src/apps.d.ts +224 -0
- package/dist/src/apps.js +432 -0
- package/dist/src/assets.d.ts +209 -0
- package/dist/src/assets.js +534 -0
- package/dist/src/auth-api.d.ts +219 -0
- package/dist/src/auth-api.js +248 -0
- package/dist/src/auth.d.ts +543 -0
- package/dist/src/auth.js +937 -31
- package/dist/src/compute.d.ts +464 -6
- package/dist/src/compute.js +746 -20
- package/dist/src/data-schema-contacts-v1.d.ts +14 -0
- package/dist/src/data-schema-contacts-v1.js +28 -0
- package/dist/src/data.d.ts +342 -0
- package/dist/src/data.js +1002 -0
- package/dist/src/endpoints.d.ts +25 -0
- package/dist/src/endpoints.js +7 -0
- package/dist/src/ethos.d.ts +85 -0
- package/dist/src/ethos.js +463 -7
- package/dist/src/index.d.ts +17 -6
- package/dist/src/index.js +25 -3
- package/dist/src/internal/delegate-bundle.js +7 -2
- package/dist/src/internal/envelope.d.ts +93 -0
- package/dist/src/internal/envelope.js +59 -0
- package/dist/src/mandates.d.ts +111 -2
- package/dist/src/mandates.js +150 -7
- package/dist/src/react/AithosAsset.d.ts +66 -0
- package/dist/src/react/AithosAsset.js +67 -0
- package/dist/src/react/context.d.ts +29 -0
- package/dist/src/react/context.js +31 -0
- package/dist/src/react/index.d.ts +29 -0
- package/dist/src/react/index.js +31 -0
- package/dist/src/react/use-aithos-asset.d.ts +39 -0
- package/dist/src/react/use-aithos-asset.js +118 -0
- package/dist/src/react/use-transcribe-pending.d.ts +21 -0
- package/dist/src/react/use-transcribe-pending.js +47 -0
- package/dist/src/sdk.d.ts +10 -0
- package/dist/src/sdk.js +22 -0
- package/dist/src/transcribe-resilience.d.ts +57 -0
- package/dist/src/transcribe-resilience.js +203 -0
- package/dist/src/web.d.ts +279 -0
- package/dist/src/web.js +186 -0
- package/dist/test/auth-j3.test.js +32 -1
- package/dist/test/canonical-conformance.test.d.ts +2 -0
- package/dist/test/canonical-conformance.test.js +86 -0
- package/dist/test/compute-delegate-path.test.d.ts +2 -0
- package/dist/test/compute-delegate-path.test.js +183 -0
- package/dist/test/compute.test.js +4 -0
- package/dist/test/endpoints.test.js +30 -1
- package/dist/test/envelope-core-conformance.test.d.ts +2 -0
- package/dist/test/envelope-core-conformance.test.js +75 -0
- package/dist/test/envelope.test.d.ts +2 -0
- package/dist/test/envelope.test.js +318 -0
- package/dist/test/ethos-first-edition.test.d.ts +2 -0
- package/dist/test/ethos-first-edition.test.js +371 -0
- package/dist/test/mandates-compute.test.d.ts +2 -0
- package/dist/test/mandates-compute.test.js +256 -0
- package/dist/test/sdk.test.js +11 -2
- package/dist/test/signup-bootstrap.test.d.ts +2 -0
- package/dist/test/signup-bootstrap.test.js +311 -0
- package/dist/test/transcribe-invoke.test.d.ts +2 -0
- package/dist/test/transcribe-invoke.test.js +204 -0
- package/dist/test/transcribe.test.d.ts +2 -0
- package/dist/test/transcribe.test.js +186 -0
- package/dist/test/web.test.d.ts +2 -0
- package/dist/test/web.test.js +270 -0
- package/package.json +20 -3
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
import type { AithosAuth } from "./auth.js";
|
|
2
|
+
import { type AithosSdkEndpoints } from "./endpoints.js";
|
|
3
|
+
/** Opt-in scope a mandate must carry to invoke `aithos.web_extract`. */
|
|
4
|
+
export declare const WEB_EXTRACT_SCOPE: "web.extract";
|
|
5
|
+
export interface ExtractArgs {
|
|
6
|
+
/**
|
|
7
|
+
* Mandate ID under which this call should be attributed.
|
|
8
|
+
*
|
|
9
|
+
* - **Owner sessions**: optional. The SDK uses the owner's own DID
|
|
10
|
+
* as a sentinel "self" mandate id — the proxy skips mandate checks
|
|
11
|
+
* when the envelope is owner-signed.
|
|
12
|
+
* - **Delegate sessions**: required. Must reference the imported
|
|
13
|
+
* mandate bundle the SDK signs with; the proxy enforces the
|
|
14
|
+
* `web.extract` scope.
|
|
15
|
+
*/
|
|
16
|
+
readonly mandateId?: string;
|
|
17
|
+
/** Absolute http(s) URL to extract. */
|
|
18
|
+
readonly url: string;
|
|
19
|
+
/**
|
|
20
|
+
* Playwright `waitUntil` strategy passed straight through to the
|
|
21
|
+
* server-side navigation. Defaults to `"networkidle"` server-side
|
|
22
|
+
* if omitted.
|
|
23
|
+
*/
|
|
24
|
+
readonly waitUntil?: "load" | "domcontentloaded" | "networkidle";
|
|
25
|
+
/** Navigation timeout in ms. Server validates [1000, 60000]. */
|
|
26
|
+
readonly timeoutMs?: number;
|
|
27
|
+
/** Reserved for audit-level deduplication; the proxy currently does not
|
|
28
|
+
* enforce idempotency keys for extractions. */
|
|
29
|
+
readonly idempotencyKey?: string;
|
|
30
|
+
/** Abort signal to cancel the request. */
|
|
31
|
+
readonly signal?: AbortSignal;
|
|
32
|
+
}
|
|
33
|
+
export interface ExtractMeta {
|
|
34
|
+
readonly title: string | null;
|
|
35
|
+
readonly description: string | null;
|
|
36
|
+
readonly lang: string | null;
|
|
37
|
+
readonly charset: string | null;
|
|
38
|
+
readonly viewport: string | null;
|
|
39
|
+
readonly canonical: string | null;
|
|
40
|
+
readonly og: Readonly<Record<string, string>>;
|
|
41
|
+
}
|
|
42
|
+
export interface ExtractHeading {
|
|
43
|
+
readonly level: 1 | 2 | 3 | 4 | 5 | 6;
|
|
44
|
+
readonly text: string;
|
|
45
|
+
readonly id: string | null;
|
|
46
|
+
}
|
|
47
|
+
export interface ExtractSection {
|
|
48
|
+
readonly tag: string;
|
|
49
|
+
readonly role: string | null;
|
|
50
|
+
readonly html: string;
|
|
51
|
+
readonly text_len: number;
|
|
52
|
+
}
|
|
53
|
+
export interface ExtractLink {
|
|
54
|
+
readonly label: string;
|
|
55
|
+
readonly href: string;
|
|
56
|
+
readonly internal: boolean;
|
|
57
|
+
}
|
|
58
|
+
export interface ExtractImage {
|
|
59
|
+
readonly src: string;
|
|
60
|
+
readonly alt: string | null;
|
|
61
|
+
readonly role: string | null;
|
|
62
|
+
}
|
|
63
|
+
export interface ExtractFormField {
|
|
64
|
+
readonly type: string;
|
|
65
|
+
readonly name: string | null;
|
|
66
|
+
readonly required: boolean;
|
|
67
|
+
}
|
|
68
|
+
export interface ExtractForm {
|
|
69
|
+
readonly action: string | null;
|
|
70
|
+
readonly method: string;
|
|
71
|
+
readonly fields: readonly ExtractFormField[];
|
|
72
|
+
}
|
|
73
|
+
export interface ExtractStructure {
|
|
74
|
+
readonly headings: readonly ExtractHeading[];
|
|
75
|
+
readonly sections: readonly ExtractSection[];
|
|
76
|
+
readonly nav_links: readonly ExtractLink[];
|
|
77
|
+
readonly forms: readonly ExtractForm[];
|
|
78
|
+
}
|
|
79
|
+
export interface ExtractContent {
|
|
80
|
+
readonly main_html: string;
|
|
81
|
+
readonly main_text: string;
|
|
82
|
+
readonly images: readonly ExtractImage[];
|
|
83
|
+
readonly links: {
|
|
84
|
+
readonly internal: readonly ExtractLink[];
|
|
85
|
+
readonly external: readonly ExtractLink[];
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
export interface ExtractStyles {
|
|
89
|
+
readonly css: string;
|
|
90
|
+
readonly inline_styles_count: number;
|
|
91
|
+
}
|
|
92
|
+
export interface PaletteEntry {
|
|
93
|
+
readonly hex: string;
|
|
94
|
+
readonly weight: number;
|
|
95
|
+
readonly role: "background" | "text" | "accent" | "other";
|
|
96
|
+
}
|
|
97
|
+
export interface ComponentStyle {
|
|
98
|
+
readonly count: number;
|
|
99
|
+
readonly bg: string | null;
|
|
100
|
+
readonly fg: string | null;
|
|
101
|
+
readonly border: string | null;
|
|
102
|
+
readonly radius: string | null;
|
|
103
|
+
readonly padding: string | null;
|
|
104
|
+
readonly font_size: string | null;
|
|
105
|
+
readonly font_weight: string | null;
|
|
106
|
+
}
|
|
107
|
+
export interface VisualSignature {
|
|
108
|
+
readonly colors: {
|
|
109
|
+
readonly palette: readonly PaletteEntry[];
|
|
110
|
+
readonly background: string | null;
|
|
111
|
+
readonly text: string | null;
|
|
112
|
+
readonly primary: string | null;
|
|
113
|
+
readonly link: string | null;
|
|
114
|
+
};
|
|
115
|
+
readonly typography: {
|
|
116
|
+
readonly heading_font: string | null;
|
|
117
|
+
readonly body_font: string | null;
|
|
118
|
+
readonly size_scale: readonly number[];
|
|
119
|
+
readonly base_size_px: number | null;
|
|
120
|
+
readonly base_line_height: number | null;
|
|
121
|
+
};
|
|
122
|
+
readonly radii: {
|
|
123
|
+
readonly button: string | null;
|
|
124
|
+
readonly input: string | null;
|
|
125
|
+
readonly card: string | null;
|
|
126
|
+
};
|
|
127
|
+
readonly spacing: {
|
|
128
|
+
readonly base_unit_px: number | null;
|
|
129
|
+
readonly common_gaps_px: readonly number[];
|
|
130
|
+
};
|
|
131
|
+
readonly layout: {
|
|
132
|
+
readonly max_content_width_px: number | null;
|
|
133
|
+
readonly mode: "flex" | "grid" | "block" | null;
|
|
134
|
+
};
|
|
135
|
+
readonly components: {
|
|
136
|
+
readonly buttons: readonly ComponentStyle[];
|
|
137
|
+
readonly inputs: readonly ComponentStyle[];
|
|
138
|
+
readonly cards: readonly ComponentStyle[];
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
export interface ExtractIconDeclaration {
|
|
142
|
+
/** href as written in the HTML (relative or absolute). */
|
|
143
|
+
readonly href: string;
|
|
144
|
+
/** rel value, lowercased: "icon", "apple-touch-icon", "shortcut icon", ... */
|
|
145
|
+
readonly rel: string;
|
|
146
|
+
/** Declared `sizes` attribute, e.g. "180x180" or "any" or null. */
|
|
147
|
+
readonly sizes: string | null;
|
|
148
|
+
/** Declared mime type, e.g. "image/svg+xml" or null. */
|
|
149
|
+
readonly type: string | null;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Logo asset resolved server-side. The Lambda picks the best
|
|
153
|
+
* symbol-only asset available on the page — declared <link rel="icon"|
|
|
154
|
+
* "apple-touch-icon"> declarations + conventional well-known paths
|
|
155
|
+
* (/apple-touch-icon.png, /favicon.svg, /favicon.ico) — in that
|
|
156
|
+
* order of expected quality. Null when nothing resolves.
|
|
157
|
+
*
|
|
158
|
+
* Favicons are symbol-only by construction (no designer ships a
|
|
159
|
+
* wordmark inside a 16-180 px icon), which sidesteps the
|
|
160
|
+
* lockup-vs-symbol problem callers used to handle client-side with
|
|
161
|
+
* a vision model.
|
|
162
|
+
*/
|
|
163
|
+
export interface ExtractLogo {
|
|
164
|
+
/** Absolute URL of the asset that was successfully fetched. */
|
|
165
|
+
readonly url: string;
|
|
166
|
+
/** Which source produced the winner. */
|
|
167
|
+
readonly source: "link-icon-svg" | "link-apple-touch-icon" | "link-icon-large" | "link-icon" | "link-shortcut-icon" | "well-known-apple-180" | "well-known-apple" | "well-known-svg" | "well-known-png-large" | "well-known-ico";
|
|
168
|
+
readonly content_type: string;
|
|
169
|
+
readonly size_bytes: number;
|
|
170
|
+
/** Base64-encoded asset bytes (no `data:` prefix). Build a data
|
|
171
|
+
* URI with `data:${content_type};base64,${base64}`. */
|
|
172
|
+
readonly base64: string;
|
|
173
|
+
}
|
|
174
|
+
export interface ExtractData {
|
|
175
|
+
readonly url: string;
|
|
176
|
+
readonly final_url: string;
|
|
177
|
+
readonly fetched_at: string;
|
|
178
|
+
readonly render_ms: number;
|
|
179
|
+
readonly meta: ExtractMeta;
|
|
180
|
+
readonly structure: ExtractStructure;
|
|
181
|
+
readonly content: ExtractContent;
|
|
182
|
+
readonly styles: ExtractStyles;
|
|
183
|
+
readonly visual_signature: VisualSignature;
|
|
184
|
+
/**
|
|
185
|
+
* Best logo asset resolved by the lambda — null when no
|
|
186
|
+
* <link rel="icon"> declaration and no conventional favicon
|
|
187
|
+
* path produced a usable image. Callers should then let the
|
|
188
|
+
* operator upload the logo manually rather than treat this as
|
|
189
|
+
* a fatal error.
|
|
190
|
+
*/
|
|
191
|
+
readonly logo: ExtractLogo | null;
|
|
192
|
+
}
|
|
193
|
+
export interface ExtractResult {
|
|
194
|
+
/** Cleaned extraction payload. */
|
|
195
|
+
readonly data: ExtractData;
|
|
196
|
+
/** Microcredits charged for this call (1 on success, 0 on refunded failures). */
|
|
197
|
+
readonly creditsCharged: number;
|
|
198
|
+
/** Wallet balance after the (possibly refunded) debit. */
|
|
199
|
+
readonly walletBalance: number;
|
|
200
|
+
/** Audit log id for traceability. */
|
|
201
|
+
readonly auditId: string;
|
|
202
|
+
}
|
|
203
|
+
export interface FetchAssetArgs {
|
|
204
|
+
/** Absolute http(s) URL of the asset to fetch. */
|
|
205
|
+
readonly url: string;
|
|
206
|
+
/** Mandate id under which this call should be attributed. */
|
|
207
|
+
readonly mandateId?: string;
|
|
208
|
+
/** Abort signal. */
|
|
209
|
+
readonly signal?: AbortSignal;
|
|
210
|
+
}
|
|
211
|
+
export interface FetchAssetResult {
|
|
212
|
+
/** Asset payload. */
|
|
213
|
+
readonly data: {
|
|
214
|
+
/** URL we asked the proxy to fetch. */
|
|
215
|
+
readonly url: string;
|
|
216
|
+
/** URL after the proxy followed any redirects. */
|
|
217
|
+
readonly final_url: string;
|
|
218
|
+
/** Content-Type reported by the upstream server. */
|
|
219
|
+
readonly content_type: string;
|
|
220
|
+
/** Size of the fetched body in bytes. */
|
|
221
|
+
readonly size_bytes: number;
|
|
222
|
+
/** Base64-encoded body (no `data:` prefix). Build a data URI
|
|
223
|
+
* with `data:${content_type};base64,${base64}`. */
|
|
224
|
+
readonly base64: string;
|
|
225
|
+
};
|
|
226
|
+
/** Microcredits charged for this call. */
|
|
227
|
+
readonly creditsCharged: number;
|
|
228
|
+
readonly walletBalance: number;
|
|
229
|
+
readonly auditId: string;
|
|
230
|
+
}
|
|
231
|
+
export interface WebNamespaceDeps {
|
|
232
|
+
readonly auth: AithosAuth;
|
|
233
|
+
readonly appDid: string;
|
|
234
|
+
readonly endpoints: AithosSdkEndpoints;
|
|
235
|
+
readonly fetch: typeof fetch;
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* `sdk.web` namespace — Aithos's web extraction primitive.
|
|
239
|
+
*
|
|
240
|
+
* Designed so a downstream agent can read the static content of any
|
|
241
|
+
* public page (HTML, purged CSS, computed visual signature) without
|
|
242
|
+
* involving an LLM — saving ~30× over a Bedrock-based extraction in
|
|
243
|
+
* both latency and cost.
|
|
244
|
+
*
|
|
245
|
+
* @throws {AithosSDKError} — same error taxonomy as `sdk.compute`,
|
|
246
|
+
* including `-32071` (insufficient balance with `{required, available}`
|
|
247
|
+
* in `data`) and `-32042` (mandate scope mismatch).
|
|
248
|
+
*/
|
|
249
|
+
export declare class WebNamespace {
|
|
250
|
+
#private;
|
|
251
|
+
constructor(deps: WebNamespaceDeps);
|
|
252
|
+
/**
|
|
253
|
+
* Extract a public webpage. Returns the cleaned HTML, purged CSS and
|
|
254
|
+
* a deterministic visual signature (palette, typography, dominant
|
|
255
|
+
* radii, spacing, layout mode, component digests).
|
|
256
|
+
*/
|
|
257
|
+
extract(args: ExtractArgs): Promise<ExtractResult>;
|
|
258
|
+
/**
|
|
259
|
+
* Fetch a single asset (image / font / css / json …) server-side,
|
|
260
|
+
* bypassing browser CORS. Returns the bytes as base64 + content-type.
|
|
261
|
+
*
|
|
262
|
+
* Use when `fetch(url, {mode: "cors"})` and `<img crossOrigin>`
|
|
263
|
+
* canvas readback both fail because the asset server doesn't return
|
|
264
|
+
* Access-Control-Allow-Origin headers — typical for production
|
|
265
|
+
* sites' logos hosted on the main domain.
|
|
266
|
+
*
|
|
267
|
+
* For the common "logo of a webpage" case the lambda already
|
|
268
|
+
* resolves and embeds the best symbol-only logo in
|
|
269
|
+
* {@link extract}'s `data.logo` field; you only need fetchAsset
|
|
270
|
+
* when extract's logo doesn't fit, when picking up secondary
|
|
271
|
+
* assets (og:image, hero image, document download), or when
|
|
272
|
+
* fetching an asset on a page you haven't extracted.
|
|
273
|
+
*
|
|
274
|
+
* Costs 1 mc per successful fetch, full refund on failure. Server
|
|
275
|
+
* caps: 15 s timeout, 10 MB body, http/https only.
|
|
276
|
+
*/
|
|
277
|
+
fetchAsset(args: FetchAssetArgs): Promise<FetchAssetResult>;
|
|
278
|
+
}
|
|
279
|
+
//# sourceMappingURL=web.d.ts.map
|
package/dist/src/web.js
ADDED
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Mathieu Colla
|
|
3
|
+
// Web namespace — `aithos.web_extract` through the web-extractor proxy.
|
|
4
|
+
//
|
|
5
|
+
// Same JSON-RPC + signed-envelope protocol as the compute namespace, but
|
|
6
|
+
// against a separate Aithos service (`extract.aithos.be`). Pricing is a
|
|
7
|
+
// flat 1 microcredit per successful extraction (refunded on failure).
|
|
8
|
+
//
|
|
9
|
+
// The mandate scope is `web.extract` (exported as {@link WEB_EXTRACT_SCOPE}
|
|
10
|
+
// for owner mint-time use). A delegate that holds only this scope can read
|
|
11
|
+
// pages on the owner's behalf without gaining LLM-spend authority.
|
|
12
|
+
//
|
|
13
|
+
// Signing follows the same owner-vs-delegate logic as the compute namespace
|
|
14
|
+
// (see ComputeNamespace.#resolveSigner). The duplication is bounded — both
|
|
15
|
+
// namespaces' `#signAndPost` helpers can later move into a shared internal
|
|
16
|
+
// once a third primitive arrives.
|
|
17
|
+
import { buildSignedEnvelope, } from "@aithos/protocol-client";
|
|
18
|
+
import { webInvokeUrl, } from "./endpoints.js";
|
|
19
|
+
import { delegateKeyPair, ownerKeyPair, } from "./internal/protocol-client-bridge.js";
|
|
20
|
+
import { AithosSDKError } from "./types.js";
|
|
21
|
+
/** Opt-in scope a mandate must carry to invoke `aithos.web_extract`. */
|
|
22
|
+
export const WEB_EXTRACT_SCOPE = "web.extract";
|
|
23
|
+
/**
|
|
24
|
+
* `sdk.web` namespace — Aithos's web extraction primitive.
|
|
25
|
+
*
|
|
26
|
+
* Designed so a downstream agent can read the static content of any
|
|
27
|
+
* public page (HTML, purged CSS, computed visual signature) without
|
|
28
|
+
* involving an LLM — saving ~30× over a Bedrock-based extraction in
|
|
29
|
+
* both latency and cost.
|
|
30
|
+
*
|
|
31
|
+
* @throws {AithosSDKError} — same error taxonomy as `sdk.compute`,
|
|
32
|
+
* including `-32071` (insufficient balance with `{required, available}`
|
|
33
|
+
* in `data`) and `-32042` (mandate scope mismatch).
|
|
34
|
+
*/
|
|
35
|
+
export class WebNamespace {
|
|
36
|
+
#deps;
|
|
37
|
+
constructor(deps) {
|
|
38
|
+
this.#deps = deps;
|
|
39
|
+
}
|
|
40
|
+
/**
|
|
41
|
+
* Extract a public webpage. Returns the cleaned HTML, purged CSS and
|
|
42
|
+
* a deterministic visual signature (palette, typography, dominant
|
|
43
|
+
* radii, spacing, layout mode, component digests).
|
|
44
|
+
*/
|
|
45
|
+
async extract(args) {
|
|
46
|
+
const { endpoints, fetch: fetchImpl } = this.#deps;
|
|
47
|
+
const choice = this.#resolveSigner(args.mandateId);
|
|
48
|
+
const url = webInvokeUrl(endpoints);
|
|
49
|
+
const params = {
|
|
50
|
+
app_did: this.#deps.appDid,
|
|
51
|
+
mandate_id: this.#resolveMandateIdForWire(args.mandateId, choice),
|
|
52
|
+
url: args.url,
|
|
53
|
+
};
|
|
54
|
+
if (args.waitUntil !== undefined)
|
|
55
|
+
params.waitUntil = args.waitUntil;
|
|
56
|
+
if (args.timeoutMs !== undefined)
|
|
57
|
+
params.timeoutMs = args.timeoutMs;
|
|
58
|
+
if (args.idempotencyKey !== undefined) {
|
|
59
|
+
params.idempotencyKey = args.idempotencyKey;
|
|
60
|
+
}
|
|
61
|
+
return await this.#signAndPost({
|
|
62
|
+
url,
|
|
63
|
+
method: "aithos.web_extract",
|
|
64
|
+
params,
|
|
65
|
+
choice,
|
|
66
|
+
fetchImpl,
|
|
67
|
+
signal: args.signal,
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/**
|
|
71
|
+
* Fetch a single asset (image / font / css / json …) server-side,
|
|
72
|
+
* bypassing browser CORS. Returns the bytes as base64 + content-type.
|
|
73
|
+
*
|
|
74
|
+
* Use when `fetch(url, {mode: "cors"})` and `<img crossOrigin>`
|
|
75
|
+
* canvas readback both fail because the asset server doesn't return
|
|
76
|
+
* Access-Control-Allow-Origin headers — typical for production
|
|
77
|
+
* sites' logos hosted on the main domain.
|
|
78
|
+
*
|
|
79
|
+
* For the common "logo of a webpage" case the lambda already
|
|
80
|
+
* resolves and embeds the best symbol-only logo in
|
|
81
|
+
* {@link extract}'s `data.logo` field; you only need fetchAsset
|
|
82
|
+
* when extract's logo doesn't fit, when picking up secondary
|
|
83
|
+
* assets (og:image, hero image, document download), or when
|
|
84
|
+
* fetching an asset on a page you haven't extracted.
|
|
85
|
+
*
|
|
86
|
+
* Costs 1 mc per successful fetch, full refund on failure. Server
|
|
87
|
+
* caps: 15 s timeout, 10 MB body, http/https only.
|
|
88
|
+
*/
|
|
89
|
+
async fetchAsset(args) {
|
|
90
|
+
const { endpoints, fetch: fetchImpl } = this.#deps;
|
|
91
|
+
const choice = this.#resolveSigner(args.mandateId);
|
|
92
|
+
const url = webInvokeUrl(endpoints);
|
|
93
|
+
const params = {
|
|
94
|
+
app_did: this.#deps.appDid,
|
|
95
|
+
mandate_id: this.#resolveMandateIdForWire(args.mandateId, choice),
|
|
96
|
+
url: args.url,
|
|
97
|
+
};
|
|
98
|
+
return await this.#signAndPost({
|
|
99
|
+
url,
|
|
100
|
+
method: "aithos.web_fetch_asset",
|
|
101
|
+
params,
|
|
102
|
+
choice,
|
|
103
|
+
fetchImpl,
|
|
104
|
+
signal: args.signal,
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
/* ----------------------------- internals ----------------------------- */
|
|
108
|
+
#resolveSigner(mandateId) {
|
|
109
|
+
const { auth } = this.#deps;
|
|
110
|
+
const owner = auth._getOwnerSigners();
|
|
111
|
+
const ownerLoaded = owner !== null && !owner.destroyed;
|
|
112
|
+
if (ownerLoaded) {
|
|
113
|
+
const publicKp = ownerKeyPair(owner, "public");
|
|
114
|
+
return {
|
|
115
|
+
kind: "owner",
|
|
116
|
+
iss: owner.did,
|
|
117
|
+
verificationMethod: `${owner.did}#public`,
|
|
118
|
+
signer: publicKp,
|
|
119
|
+
mandate: undefined,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
if (mandateId === undefined || mandateId.length === 0) {
|
|
123
|
+
throw new AithosSDKError("sdk_no_signer", "no owner signed in and no mandateId provided — pass a mandateId for a delegate session, or sign in as an owner first.");
|
|
124
|
+
}
|
|
125
|
+
const actor = auth._getDelegateActor(mandateId);
|
|
126
|
+
if (!actor || actor.destroyed) {
|
|
127
|
+
throw new AithosSDKError("sdk_no_delegate_for_mandate", `no owner signed in and no imported delegate mandate matches '${mandateId}'. Sign in as an owner, or import a delegate bundle for that mandate via auth.importMandate.`);
|
|
128
|
+
}
|
|
129
|
+
const kp = delegateKeyPair(actor);
|
|
130
|
+
return {
|
|
131
|
+
kind: "delegate",
|
|
132
|
+
iss: actor.subjectDid,
|
|
133
|
+
verificationMethod: actor.granteePubkeyMultibase,
|
|
134
|
+
signer: kp,
|
|
135
|
+
mandate: actor.mandate,
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
#resolveMandateIdForWire(explicit, choice) {
|
|
139
|
+
if (explicit && explicit.length > 0)
|
|
140
|
+
return explicit;
|
|
141
|
+
if (choice.kind === "delegate")
|
|
142
|
+
return choice.mandate.id;
|
|
143
|
+
return `${choice.iss}#self`;
|
|
144
|
+
}
|
|
145
|
+
async #signAndPost(opts) {
|
|
146
|
+
const { url, method, params, choice, fetchImpl, signal } = opts;
|
|
147
|
+
const envelope = buildSignedEnvelope({
|
|
148
|
+
iss: choice.iss,
|
|
149
|
+
aud: url,
|
|
150
|
+
method,
|
|
151
|
+
verificationMethod: choice.verificationMethod,
|
|
152
|
+
params,
|
|
153
|
+
signer: choice.signer,
|
|
154
|
+
...(choice.kind === "delegate" ? { mandate: choice.mandate } : {}),
|
|
155
|
+
});
|
|
156
|
+
let res;
|
|
157
|
+
try {
|
|
158
|
+
res = await fetchImpl(url, {
|
|
159
|
+
method: "POST",
|
|
160
|
+
headers: { "content-type": "application/json" },
|
|
161
|
+
body: JSON.stringify({
|
|
162
|
+
jsonrpc: "2.0",
|
|
163
|
+
id: method,
|
|
164
|
+
method,
|
|
165
|
+
params: { ...params, _envelope: envelope },
|
|
166
|
+
}),
|
|
167
|
+
...(signal ? { signal } : {}),
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
catch (e) {
|
|
171
|
+
throw new AithosSDKError("network", e.message);
|
|
172
|
+
}
|
|
173
|
+
if (!res.ok) {
|
|
174
|
+
throw new AithosSDKError("http", `HTTP ${res.status} ${res.statusText}`, { status: res.status });
|
|
175
|
+
}
|
|
176
|
+
const body = (await res.json());
|
|
177
|
+
if (body.error) {
|
|
178
|
+
throw new AithosSDKError(String(body.error.code), body.error.message, body.error.data ? { data: body.error.data } : undefined);
|
|
179
|
+
}
|
|
180
|
+
if (!body.result) {
|
|
181
|
+
throw new AithosSDKError("empty", "empty result from web extractor proxy");
|
|
182
|
+
}
|
|
183
|
+
return body.result;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
//# sourceMappingURL=web.js.map
|
|
@@ -90,7 +90,7 @@ describe("recovery file: parse + serialize", () => {
|
|
|
90
90
|
/* parseDelegateBundle */
|
|
91
91
|
/* -------------------------------------------------------------------------- */
|
|
92
92
|
describe("delegate bundle: parse", () => {
|
|
93
|
-
it("parses a well-formed bundle", () => {
|
|
93
|
+
it("parses a well-formed bundle (legacy subject_did field)", () => {
|
|
94
94
|
const text = delegateBundleText({
|
|
95
95
|
mandateId: "mandate:01H8XYZ",
|
|
96
96
|
subjectDid: "did:aithos:zCarol",
|
|
@@ -103,6 +103,37 @@ describe("delegate bundle: parse", () => {
|
|
|
103
103
|
assert.equal(parsed.granteeId, "urn:aithos:agent:bob1");
|
|
104
104
|
assert.equal(parsed.delegateSeedHex.length, 64);
|
|
105
105
|
});
|
|
106
|
+
it("parses a bundle minted by mintDelegateBundle (issuer field)", () => {
|
|
107
|
+
// Real wire shape emitted by `mintDelegateBundle` in protocol-client:
|
|
108
|
+
// SignedMandate carries the subject's DID under `issuer`, NOT
|
|
109
|
+
// `subject_did`. Regression test for the import flow that broke
|
|
110
|
+
// every freshly-minted mandate before this fix.
|
|
111
|
+
const text = JSON.stringify({
|
|
112
|
+
aithos_delegate_version: "0.1.0",
|
|
113
|
+
mandate: {
|
|
114
|
+
"aithos-mandate": "0.1",
|
|
115
|
+
id: "mandate:01H8ISSUER",
|
|
116
|
+
issuer: "did:aithos:zCarol",
|
|
117
|
+
issued_by_key: "did:aithos:zCarol#root",
|
|
118
|
+
grantee: {
|
|
119
|
+
id: "urn:aithos:agent:bob1",
|
|
120
|
+
pubkey: "z6MkqGenericPubKey",
|
|
121
|
+
},
|
|
122
|
+
actor_sphere: "self",
|
|
123
|
+
scopes: ["ethos.read.public", "ethos.write.public"],
|
|
124
|
+
not_before: "2026-05-10T00:00:00Z",
|
|
125
|
+
not_after: "2026-05-11T00:00:00Z",
|
|
126
|
+
issued_at: "2026-05-10T00:00:00Z",
|
|
127
|
+
nonce: "abc",
|
|
128
|
+
signature: { alg: "ed25519", key: "...", value: "..." },
|
|
129
|
+
},
|
|
130
|
+
delegate_seed_hex: "11".repeat(32),
|
|
131
|
+
});
|
|
132
|
+
const parsed = parseDelegateBundle(text);
|
|
133
|
+
assert.equal(parsed.subjectDid, "did:aithos:zCarol");
|
|
134
|
+
assert.equal(parsed.mandateId, "mandate:01H8ISSUER");
|
|
135
|
+
assert.equal(parsed.granteeId, "urn:aithos:agent:bob1");
|
|
136
|
+
});
|
|
106
137
|
it("readDelegateBundleText accepts string passthrough", async () => {
|
|
107
138
|
const text = delegateBundleText({
|
|
108
139
|
mandateId: "m",
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
// Copyright 2026 Mathieu Colla
|
|
3
|
+
/**
|
|
4
|
+
* Safety gate for the canonicalization refactor.
|
|
5
|
+
*
|
|
6
|
+
* The SDK historically carried three hand-rolled copies of `jcsCanonicalize`
|
|
7
|
+
* (internal/envelope.ts, data.ts, apps.ts). data.ts uses it to canonicalize a
|
|
8
|
+
* record payload BEFORE encryption (it feeds the AAD/plaintext), so a single
|
|
9
|
+
* differing byte vs. the canonicalization used at decrypt/verify time would
|
|
10
|
+
* silently corrupt data. Before swapping those copies onto
|
|
11
|
+
* `@aithos/protocol-core`'s `canonicalize`, this test proves the two produce
|
|
12
|
+
* identical output across a representative corpus.
|
|
13
|
+
*
|
|
14
|
+
* Known, accepted divergence: lone UTF-16 surrogates. `JSON.stringify` escapes
|
|
15
|
+
* them (\uXXXX) whereas core emits the raw code unit. Lone surrogates never
|
|
16
|
+
* appear in Aithos payloads (record fields are well-formed JSON strings), so
|
|
17
|
+
* the corpus excludes them by design.
|
|
18
|
+
*/
|
|
19
|
+
import { describe, test } from "node:test";
|
|
20
|
+
import { strict as assert } from "node:assert";
|
|
21
|
+
import { canonicalize } from "@aithos/protocol-core/canonical";
|
|
22
|
+
/** Exact copy of the SDK's legacy jcsCanonicalize (pre-refactor reference). */
|
|
23
|
+
function jcsCanonicalize(value) {
|
|
24
|
+
if (value === null)
|
|
25
|
+
return "null";
|
|
26
|
+
if (value === undefined)
|
|
27
|
+
throw new Error("Cannot canonicalize undefined");
|
|
28
|
+
if (typeof value === "boolean")
|
|
29
|
+
return value ? "true" : "false";
|
|
30
|
+
if (typeof value === "number") {
|
|
31
|
+
if (!Number.isFinite(value))
|
|
32
|
+
throw new Error("non-finite number");
|
|
33
|
+
return value.toString();
|
|
34
|
+
}
|
|
35
|
+
if (typeof value === "string")
|
|
36
|
+
return JSON.stringify(value);
|
|
37
|
+
if (Array.isArray(value)) {
|
|
38
|
+
return "[" + value.map(jcsCanonicalize).join(",") + "]";
|
|
39
|
+
}
|
|
40
|
+
if (typeof value === "object") {
|
|
41
|
+
const obj = value;
|
|
42
|
+
const keys = Object.keys(obj).sort();
|
|
43
|
+
return ("{" +
|
|
44
|
+
keys
|
|
45
|
+
.map((k) => JSON.stringify(k) + ":" + jcsCanonicalize(obj[k]))
|
|
46
|
+
.join(",") +
|
|
47
|
+
"}");
|
|
48
|
+
}
|
|
49
|
+
throw new Error(`Cannot canonicalize ${typeof value}`);
|
|
50
|
+
}
|
|
51
|
+
const corpus = [
|
|
52
|
+
null,
|
|
53
|
+
true,
|
|
54
|
+
false,
|
|
55
|
+
0,
|
|
56
|
+
-1,
|
|
57
|
+
42,
|
|
58
|
+
Number.MAX_SAFE_INTEGER,
|
|
59
|
+
"",
|
|
60
|
+
"hello",
|
|
61
|
+
"with \"quotes\" and \\ backslash",
|
|
62
|
+
"tab\tnewline\nreturn\rbackspace\bform\f",
|
|
63
|
+
"control end",
|
|
64
|
+
"accented éàùçö and emoji 😀🚀 and 漢字",
|
|
65
|
+
"slash / and at @ and unicode nbsp",
|
|
66
|
+
[],
|
|
67
|
+
[1, 2, 3],
|
|
68
|
+
["z", "a", "m"],
|
|
69
|
+
[{ b: 1, a: 2 }, [3, [4, 5]]],
|
|
70
|
+
{},
|
|
71
|
+
{ b: 2, a: 1, c: 3 },
|
|
72
|
+
{ z: { y: { x: [1, "two", false, null] } } },
|
|
73
|
+
{ "key with spaces": 1, "weird:char": 2, "": "empty key" },
|
|
74
|
+
{ émoji: "😀", "漢字": 1, A: 0, a: 0 }, // mixed-case + non-ascii keys (UTF-16 order)
|
|
75
|
+
{
|
|
76
|
+
record: { name: "Aïko", tags: ["x", "y"], n: 7, active: true, meta: null },
|
|
77
|
+
},
|
|
78
|
+
];
|
|
79
|
+
describe("canonicalize conformance — core vs legacy SDK jcsCanonicalize", () => {
|
|
80
|
+
for (const [i, value] of corpus.entries()) {
|
|
81
|
+
test(`corpus[${i}] is byte-identical`, () => {
|
|
82
|
+
assert.equal(canonicalize(value), jcsCanonicalize(value));
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
});
|
|
86
|
+
//# sourceMappingURL=canonical-conformance.test.js.map
|