@aithos/sdk 0.1.0-alpha.4 → 0.1.0-alpha.6
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 +45 -0
- package/dist/src/auth.d.ts +103 -134
- package/dist/src/auth.js +532 -157
- package/dist/src/compute.d.ts +8 -6
- package/dist/src/compute.js +19 -11
- package/dist/src/ethos.d.ts +117 -1
- package/dist/src/ethos.js +417 -16
- package/dist/src/index.d.ts +8 -5
- package/dist/src/index.js +18 -6
- package/dist/src/internal/delegate-bundle.d.ts +18 -0
- package/dist/src/internal/delegate-bundle.js +89 -0
- package/dist/src/internal/delegate-state.d.ts +45 -0
- package/dist/src/internal/delegate-state.js +120 -0
- package/dist/src/internal/owner-signers.d.ts +78 -0
- package/dist/src/internal/owner-signers.js +179 -0
- package/dist/src/internal/protocol-client-bridge.d.ts +8 -0
- package/dist/src/internal/protocol-client-bridge.js +20 -0
- package/dist/src/internal/recovery-file.d.ts +29 -0
- package/dist/src/internal/recovery-file.js +98 -0
- package/dist/src/internal/signer.d.ts +59 -0
- package/dist/src/internal/signer.js +86 -0
- package/dist/src/key-store.d.ts +128 -0
- package/dist/src/key-store.js +244 -0
- package/dist/src/mandates.d.ts +151 -1
- package/dist/src/mandates.js +285 -8
- package/dist/src/sdk.d.ts +36 -3
- package/dist/src/sdk.js +27 -23
- package/dist/src/wallet.d.ts +4 -6
- package/dist/src/wallet.js +18 -8
- package/dist/test/auth-j3.test.d.ts +2 -0
- package/dist/test/auth-j3.test.js +360 -0
- package/dist/test/compute.test.js +22 -11
- package/dist/test/ethos.test.d.ts +2 -0
- package/dist/test/ethos.test.js +219 -0
- package/dist/test/key-store.test.d.ts +2 -0
- package/dist/test/key-store.test.js +161 -0
- package/dist/test/mandates-compute.test.d.ts +2 -0
- package/dist/test/mandates-compute.test.js +256 -0
- package/dist/test/mandates.test.d.ts +2 -0
- package/dist/test/mandates.test.js +93 -0
- package/dist/test/sdk.test.js +64 -30
- package/dist/test/signer.test.d.ts +2 -0
- package/dist/test/signer.test.js +117 -0
- package/dist/test/wallet.test.js +20 -9
- package/package.json +2 -1
package/dist/src/mandates.d.ts
CHANGED
|
@@ -1,2 +1,152 @@
|
|
|
1
|
-
|
|
1
|
+
import type { AithosAuth } from "./auth.js";
|
|
2
|
+
import type { AithosSdkEndpoints } from "./endpoints.js";
|
|
3
|
+
/** Capability scope the SDK accepts. Server-side ultimately decides.
|
|
4
|
+
*
|
|
5
|
+
* Note: `compute.invoke` is intentionally NOT in this union. The token-
|
|
6
|
+
* spending capability is opt-in via the dedicated {@link CreateMandateInput.compute}
|
|
7
|
+
* namespace — see {@link MandatesNamespace.create}. Passing `compute.invoke`
|
|
8
|
+
* directly in `scopes` is rejected at runtime; the compiler can't enforce
|
|
9
|
+
* it (callers who up-cast to string[] would slip through), so the runtime
|
|
10
|
+
* check is the real gate. */
|
|
11
|
+
export type Scope = "ethos.read.public" | "ethos.read.circle" | "ethos.read.self" | "ethos.write.public" | "ethos.write.circle" | "ethos.write.self";
|
|
12
|
+
/**
|
|
13
|
+
* The opt-in scope that authorizes a delegate to spend the subject's
|
|
14
|
+
* compute credits via the Aithos compute proxy. Mirror of
|
|
15
|
+
* `COMPUTE_INVOKE_SCOPE` in `@aithos/protocol-core` v0.4.0.
|
|
16
|
+
*
|
|
17
|
+
* The SDK's `mandates.create()` injects this scope automatically when
|
|
18
|
+
* the caller passes a `compute` namespace, and refuses to mint a
|
|
19
|
+
* mandate where the caller put it directly into `scopes` — this is
|
|
20
|
+
* what makes "compute is a separate, conscious decision" hold at the
|
|
21
|
+
* API surface.
|
|
22
|
+
*/
|
|
23
|
+
export declare const COMPUTE_INVOKE_SCOPE: "compute.invoke";
|
|
24
|
+
/**
|
|
25
|
+
* Which sphere of the owner signs the mandate. Bounds the upper-most
|
|
26
|
+
* scope set the mandate can carry.
|
|
27
|
+
*/
|
|
28
|
+
export type ActorSphere = "public" | "circle" | "self";
|
|
29
|
+
/**
|
|
30
|
+
* Compute-spending capability — opt-in only, never implied by ethos
|
|
31
|
+
* scopes.
|
|
32
|
+
*
|
|
33
|
+
* When `compute` is set on {@link CreateMandateInput}, the SDK:
|
|
34
|
+
* 1. Adds the `compute.invoke` scope to the minted mandate.
|
|
35
|
+
* 2. Maps the caller's caps onto `constraints.compute` in the
|
|
36
|
+
* protocol's snake_case shape (= what the verifier reads).
|
|
37
|
+
* 3. Forbids the caller from passing `compute.invoke` in `scopes`
|
|
38
|
+
* directly — that would let an app slip the scope past a
|
|
39
|
+
* consent UI that only reviews `compute`.
|
|
40
|
+
*
|
|
41
|
+
* At least one of `dailyCapMicrocredits` or `totalCapMicrocredits` MUST
|
|
42
|
+
* be set: an unbounded compute mandate is the kind of bearer-token
|
|
43
|
+
* footgun this whole namespace exists to prevent. Validation happens
|
|
44
|
+
* at the SDK boundary (here) AND at the protocol layer (the
|
|
45
|
+
* server-side verifier rejects capless 0.4.0 mandates), so a bug in
|
|
46
|
+
* either tier still fails closed.
|
|
47
|
+
*
|
|
48
|
+
* `maxCreditsPerCall` is a per-invocation safety net for runaway
|
|
49
|
+
* single requests. `allowedModels`, when set, restricts which Bedrock
|
|
50
|
+
* model ids the delegate may target (the proxy's own allowlist still
|
|
51
|
+
* applies on top).
|
|
52
|
+
*/
|
|
53
|
+
export interface CreateMandateComputeInput {
|
|
54
|
+
/** Hard cap on credits debited per UTC day under this mandate. */
|
|
55
|
+
readonly dailyCapMicrocredits?: number;
|
|
56
|
+
/** Hard cap on credits debited over the whole mandate lifetime. */
|
|
57
|
+
readonly totalCapMicrocredits?: number;
|
|
58
|
+
/** Hard cap on credits debited by any single invocation. */
|
|
59
|
+
readonly maxCreditsPerCall?: number;
|
|
60
|
+
/** Allowlist of Bedrock model ids the delegate may invoke. */
|
|
61
|
+
readonly allowedModels?: readonly string[];
|
|
62
|
+
}
|
|
63
|
+
export interface CreateMandateInput {
|
|
64
|
+
/** Grantee URN — usually `urn:aithos:agent:<extension-id>` or similar. */
|
|
65
|
+
readonly granteeId: string;
|
|
66
|
+
/** Optional human-readable label for the grantee. */
|
|
67
|
+
readonly granteeLabel?: string;
|
|
68
|
+
/**
|
|
69
|
+
* Sphere of the owner that issues the mandate. Defaults to the
|
|
70
|
+
* highest-numbered sphere covered by `scopes` (most permissive
|
|
71
|
+
* common ancestor): `"self"` if any scope ends in `.self`, else
|
|
72
|
+
* `"circle"` if any ends in `.circle`, else `"public"`.
|
|
73
|
+
*/
|
|
74
|
+
readonly actorSphere?: ActorSphere;
|
|
75
|
+
/** Capability set granted by the mandate. */
|
|
76
|
+
readonly scopes: readonly Scope[];
|
|
77
|
+
/** Lifetime in seconds. */
|
|
78
|
+
readonly ttlSeconds: number;
|
|
79
|
+
/**
|
|
80
|
+
* Opt-in compute (token-spending) capability — adds the
|
|
81
|
+
* `compute.invoke` scope and a bounded `constraints.compute` budget
|
|
82
|
+
* to the mandate. See {@link CreateMandateComputeInput}.
|
|
83
|
+
*
|
|
84
|
+
* NEVER add `compute.invoke` to `scopes` directly — the SDK rejects
|
|
85
|
+
* that path so the caller has to pass through this typed namespace,
|
|
86
|
+
* which is what a consent UI can review.
|
|
87
|
+
*/
|
|
88
|
+
readonly compute?: CreateMandateComputeInput;
|
|
89
|
+
}
|
|
90
|
+
export interface MintedMandate {
|
|
91
|
+
/** Unique mandate id (matches `mandate.id` inside the bundle). */
|
|
92
|
+
readonly mandateId: string;
|
|
93
|
+
/** Subject DID — the owner who issued it. */
|
|
94
|
+
readonly subjectDid: string;
|
|
95
|
+
/** Grantee URN. */
|
|
96
|
+
readonly granteeId: string;
|
|
97
|
+
readonly scopes: readonly Scope[];
|
|
98
|
+
/** ISO-8601 (UTC) — `null` if the mandate has no `not_after`. */
|
|
99
|
+
readonly expiresAt: string | null;
|
|
100
|
+
/** Shareable `.aithos-delegate.json` Blob. Hand this to the grantee. */
|
|
101
|
+
readonly bundle: Blob;
|
|
102
|
+
/** Suggested filename for the bundle. */
|
|
103
|
+
readonly filename: string;
|
|
104
|
+
}
|
|
105
|
+
export interface OwnedMandate {
|
|
106
|
+
readonly mandateId: string;
|
|
107
|
+
readonly issuerDid: string;
|
|
108
|
+
readonly actorDid: string;
|
|
109
|
+
readonly scopes: readonly Scope[];
|
|
110
|
+
readonly notBefore: number | null;
|
|
111
|
+
readonly notAfter: number | null;
|
|
112
|
+
readonly createdAt: number;
|
|
113
|
+
}
|
|
114
|
+
export interface MandatesNamespaceDeps {
|
|
115
|
+
readonly auth: AithosAuth;
|
|
116
|
+
readonly endpoints: AithosSdkEndpoints;
|
|
117
|
+
readonly fetch: typeof fetch;
|
|
118
|
+
}
|
|
119
|
+
export declare class MandatesNamespace {
|
|
120
|
+
#private;
|
|
121
|
+
constructor(deps: MandatesNamespaceDeps);
|
|
122
|
+
/**
|
|
123
|
+
* Mint, sign, publish, and package a fresh delegate bundle. The
|
|
124
|
+
* grantee's keypair is generated inside this call and never
|
|
125
|
+
* persisted on the owner's machine — the seed flows out via the
|
|
126
|
+
* returned Blob and only via that Blob.
|
|
127
|
+
*/
|
|
128
|
+
create(input: CreateMandateInput): Promise<MintedMandate>;
|
|
129
|
+
/**
|
|
130
|
+
* List mandates issued by the signed-in owner. Pages through
|
|
131
|
+
* `aithos.list_mandates` until exhausted (or until 5 pages have
|
|
132
|
+
* been crawled — an owner with more than 1000 active mandates is
|
|
133
|
+
* out of scope today).
|
|
134
|
+
*/
|
|
135
|
+
list(): Promise<readonly OwnedMandate[]>;
|
|
136
|
+
/**
|
|
137
|
+
* Publish a §4.2 revocation for `mandateId`. The mandate stops
|
|
138
|
+
* authorizing future actions (artifacts dated before `revoked_at`
|
|
139
|
+
* remain valid — revocation is not retroactive).
|
|
140
|
+
*
|
|
141
|
+
* Server-side: handled by `aithos.publish_revocation`. The envelope
|
|
142
|
+
* is signed by the owner's `#public` sphere — the spec also accepts
|
|
143
|
+
* `#root`, but `#public` is what the existing app does.
|
|
144
|
+
*
|
|
145
|
+
* Throws {@link AithosSDKError} on backend errors. Note that
|
|
146
|
+
* server-side support is in-flight; this method may surface a
|
|
147
|
+
* `mandates_-32601` (method not found) until the auth platform
|
|
148
|
+
* lands the corresponding write handler.
|
|
149
|
+
*/
|
|
150
|
+
revoke(mandateId: string): Promise<void>;
|
|
151
|
+
}
|
|
2
152
|
//# sourceMappingURL=mandates.d.ts.map
|
package/dist/src/mandates.js
CHANGED
|
@@ -1,13 +1,290 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// Copyright 2026 Mathieu Colla
|
|
3
|
-
//
|
|
3
|
+
// `sdk.mandates` namespace — owner-side mandate lifecycle.
|
|
4
4
|
//
|
|
5
|
-
//
|
|
6
|
-
//
|
|
7
|
-
//
|
|
8
|
-
//
|
|
5
|
+
// Three verbs:
|
|
6
|
+
// create(input) → mints + publishes + returns the
|
|
7
|
+
// shareable .aithos-delegate.json bundle
|
|
8
|
+
// list() → mandates the owner has issued
|
|
9
|
+
// revoke(mandateId) → publishes a §4.2 revocation
|
|
9
10
|
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
|
|
11
|
+
// Capability boundary: every method requires an owner signed in. We
|
|
12
|
+
// reach the OwnerSigners through `auth._getOwnerSigners()` and project
|
|
13
|
+
// to a `StoredIdentity` for protocol-client interop. When
|
|
14
|
+
// protocol-client gains a Signer-shaped API the projection step
|
|
15
|
+
// disappears.
|
|
16
|
+
import { buildSignedEnvelope, mintDelegateBundle, readRpc, } from "@aithos/protocol-client";
|
|
17
|
+
import { ownerKeyPair } from "./internal/protocol-client-bridge.js";
|
|
18
|
+
import { AithosSDKError } from "./types.js";
|
|
19
|
+
/**
|
|
20
|
+
* The opt-in scope that authorizes a delegate to spend the subject's
|
|
21
|
+
* compute credits via the Aithos compute proxy. Mirror of
|
|
22
|
+
* `COMPUTE_INVOKE_SCOPE` in `@aithos/protocol-core` v0.4.0.
|
|
23
|
+
*
|
|
24
|
+
* The SDK's `mandates.create()` injects this scope automatically when
|
|
25
|
+
* the caller passes a `compute` namespace, and refuses to mint a
|
|
26
|
+
* mandate where the caller put it directly into `scopes` — this is
|
|
27
|
+
* what makes "compute is a separate, conscious decision" hold at the
|
|
28
|
+
* API surface.
|
|
29
|
+
*/
|
|
30
|
+
export const COMPUTE_INVOKE_SCOPE = "compute.invoke";
|
|
31
|
+
export class MandatesNamespace {
|
|
32
|
+
#deps;
|
|
33
|
+
constructor(deps) {
|
|
34
|
+
this.#deps = deps;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Mint, sign, publish, and package a fresh delegate bundle. The
|
|
38
|
+
* grantee's keypair is generated inside this call and never
|
|
39
|
+
* persisted on the owner's machine — the seed flows out via the
|
|
40
|
+
* returned Blob and only via that Blob.
|
|
41
|
+
*/
|
|
42
|
+
async create(input) {
|
|
43
|
+
const owner = this.#requireOwner();
|
|
44
|
+
// A mandate must carry at least one capability — either ethos
|
|
45
|
+
// scopes, the compute namespace, or both. A "compute-only" mandate
|
|
46
|
+
// (`scopes: []` + `compute: { ... }`) is legitimate: it gives the
|
|
47
|
+
// grantee no access to the subject's ethos data, only the right
|
|
48
|
+
// to spend a bounded amount of compute credits in their name.
|
|
49
|
+
// Useful for creative assistants, brainstorming agents, etc.
|
|
50
|
+
if (input.scopes.length === 0 && input.compute === undefined) {
|
|
51
|
+
throw new AithosSDKError("mandates_invalid_scopes", "scopes must be a non-empty list (or pass `compute` for a compute-only mandate)");
|
|
52
|
+
}
|
|
53
|
+
if (input.ttlSeconds <= 0) {
|
|
54
|
+
throw new AithosSDKError("mandates_invalid_ttl", "ttlSeconds must be > 0");
|
|
55
|
+
}
|
|
56
|
+
// Forbid `compute.invoke` smuggled in via `scopes[]`. The whole
|
|
57
|
+
// point of the dedicated `compute` namespace is that adding token-
|
|
58
|
+
// spending capability requires a typed, named, reviewable input —
|
|
59
|
+
// not a string lost in a generic list. Type-checking can't catch
|
|
60
|
+
// this (the union doesn't include the literal, but callers can
|
|
61
|
+
// up-cast); the runtime check is what holds.
|
|
62
|
+
if (input.scopes.some((s) => s === COMPUTE_INVOKE_SCOPE)) {
|
|
63
|
+
throw new AithosSDKError("mandates_invalid_scopes", `Pass token-spending capability via the dedicated 'compute' namespace, ` +
|
|
64
|
+
`not by adding "${COMPUTE_INVOKE_SCOPE}" to scopes[]. The namespace forces ` +
|
|
65
|
+
`an explicit budget and is what a consent UI reviews.`);
|
|
66
|
+
}
|
|
67
|
+
// Validate + project the compute namespace if present, then derive
|
|
68
|
+
// the final scopes/constraints to send to the protocol layer.
|
|
69
|
+
const computeProjection = projectCompute(input.compute);
|
|
70
|
+
const projectedScopes = [...input.scopes];
|
|
71
|
+
if (computeProjection) {
|
|
72
|
+
projectedScopes.push(COMPUTE_INVOKE_SCOPE);
|
|
73
|
+
}
|
|
74
|
+
const actorSphere = input.actorSphere ?? defaultSphereFromScopes(input.scopes);
|
|
75
|
+
const ownerStored = owner._unsafeStoredIdentity();
|
|
76
|
+
const result = await mintDelegateBundle({
|
|
77
|
+
owner: ownerStored,
|
|
78
|
+
granteeId: input.granteeId,
|
|
79
|
+
...(input.granteeLabel ? { granteeLabel: input.granteeLabel } : {}),
|
|
80
|
+
actorSphere,
|
|
81
|
+
scopes: projectedScopes,
|
|
82
|
+
ttlSeconds: input.ttlSeconds,
|
|
83
|
+
// protocol-client v0.1.0-alpha.11 ships MandateConstraints without
|
|
84
|
+
// the `compute` field; the wire format accepts it though (the
|
|
85
|
+
// canonicalizer just serializes whatever's in the object). We
|
|
86
|
+
// up-cast through `unknown` to bypass the structural check until
|
|
87
|
+
// protocol-client picks up protocol-core 0.4.0 types.
|
|
88
|
+
...(computeProjection
|
|
89
|
+
? {
|
|
90
|
+
constraints: {
|
|
91
|
+
compute: computeProjection,
|
|
92
|
+
},
|
|
93
|
+
}
|
|
94
|
+
: {}),
|
|
95
|
+
});
|
|
96
|
+
const mandate = result.mandate;
|
|
97
|
+
return {
|
|
98
|
+
mandateId: mandate.id,
|
|
99
|
+
subjectDid: mandate.subject_did,
|
|
100
|
+
granteeId: input.granteeId,
|
|
101
|
+
scopes: projectedScopes,
|
|
102
|
+
expiresAt: mandate.not_after ?? null,
|
|
103
|
+
bundle: result.bundleBlob,
|
|
104
|
+
filename: `aithos-delegate-${mandate.id}.json`,
|
|
105
|
+
};
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* List mandates issued by the signed-in owner. Pages through
|
|
109
|
+
* `aithos.list_mandates` until exhausted (or until 5 pages have
|
|
110
|
+
* been crawled — an owner with more than 1000 active mandates is
|
|
111
|
+
* out of scope today).
|
|
112
|
+
*/
|
|
113
|
+
async list() {
|
|
114
|
+
const owner = this.#requireOwner();
|
|
115
|
+
const out = [];
|
|
116
|
+
let cursor;
|
|
117
|
+
for (let i = 0; i < 5; i++) {
|
|
118
|
+
const page = await readRpc("aithos.list_mandates", {
|
|
119
|
+
issuer_did: owner.did,
|
|
120
|
+
limit: 200,
|
|
121
|
+
...(cursor ? { cursor } : {}),
|
|
122
|
+
});
|
|
123
|
+
for (const it of page.items) {
|
|
124
|
+
out.push(toOwnedMandate(it));
|
|
125
|
+
}
|
|
126
|
+
if (!page.next_cursor)
|
|
127
|
+
break;
|
|
128
|
+
cursor = page.next_cursor;
|
|
129
|
+
}
|
|
130
|
+
return out;
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Publish a §4.2 revocation for `mandateId`. The mandate stops
|
|
134
|
+
* authorizing future actions (artifacts dated before `revoked_at`
|
|
135
|
+
* remain valid — revocation is not retroactive).
|
|
136
|
+
*
|
|
137
|
+
* Server-side: handled by `aithos.publish_revocation`. The envelope
|
|
138
|
+
* is signed by the owner's `#public` sphere — the spec also accepts
|
|
139
|
+
* `#root`, but `#public` is what the existing app does.
|
|
140
|
+
*
|
|
141
|
+
* Throws {@link AithosSDKError} on backend errors. Note that
|
|
142
|
+
* server-side support is in-flight; this method may surface a
|
|
143
|
+
* `mandates_-32601` (method not found) until the auth platform
|
|
144
|
+
* lands the corresponding write handler.
|
|
145
|
+
*/
|
|
146
|
+
async revoke(mandateId) {
|
|
147
|
+
const owner = this.#requireOwner();
|
|
148
|
+
const ownerStored = owner._unsafeStoredIdentity();
|
|
149
|
+
// TODO(post-alpha): once @aithos/protocol-client exposes a public
|
|
150
|
+
// configuration API for the `api` endpoint, route this through the
|
|
151
|
+
// same channel as `readRpc` / `publishZoneEdit` so self-hosters
|
|
152
|
+
// can override. For now, target the production write surface.
|
|
153
|
+
const url = "https://api.aithos.be/mcp/primitives/write";
|
|
154
|
+
const params = {
|
|
155
|
+
mandate_id: mandateId,
|
|
156
|
+
revoked_at: new Date().toISOString(),
|
|
157
|
+
};
|
|
158
|
+
// Reach into the owner's #public signer for envelope signing via
|
|
159
|
+
// the centralized migration bridge (will go away when
|
|
160
|
+
// protocol-client accepts Signer-shaped objects).
|
|
161
|
+
const publicKp = ownerKeyPair(owner, "public");
|
|
162
|
+
const envelope = buildSignedEnvelope({
|
|
163
|
+
iss: ownerStored.did,
|
|
164
|
+
aud: url,
|
|
165
|
+
method: "aithos.publish_revocation",
|
|
166
|
+
verificationMethod: `${ownerStored.did}#public`,
|
|
167
|
+
params,
|
|
168
|
+
signer: publicKp,
|
|
169
|
+
});
|
|
170
|
+
let res;
|
|
171
|
+
try {
|
|
172
|
+
res = await this.#deps.fetch(url, {
|
|
173
|
+
method: "POST",
|
|
174
|
+
headers: { "content-type": "application/json" },
|
|
175
|
+
body: JSON.stringify({
|
|
176
|
+
jsonrpc: "2.0",
|
|
177
|
+
id: "aithos.publish_revocation",
|
|
178
|
+
method: "aithos.publish_revocation",
|
|
179
|
+
params: { ...params, _envelope: envelope },
|
|
180
|
+
}),
|
|
181
|
+
});
|
|
182
|
+
}
|
|
183
|
+
catch (e) {
|
|
184
|
+
throw new AithosSDKError("network", e.message);
|
|
185
|
+
}
|
|
186
|
+
const body = (await res.json().catch(() => null));
|
|
187
|
+
if (!body) {
|
|
188
|
+
throw new AithosSDKError("http", `HTTP ${res.status} ${res.statusText} — non-JSON response`, { status: res.status });
|
|
189
|
+
}
|
|
190
|
+
if (body.error) {
|
|
191
|
+
throw new AithosSDKError(`mandates_${body.error.code}`, body.error.message, body.error.data ? { data: body.error.data } : undefined);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
/* ------------------------------------------------------------------------ */
|
|
195
|
+
/* Internals */
|
|
196
|
+
/* ------------------------------------------------------------------------ */
|
|
197
|
+
#requireOwner() {
|
|
198
|
+
const owner = this.#deps.auth._getOwnerSigners();
|
|
199
|
+
if (!owner || owner.destroyed) {
|
|
200
|
+
throw new AithosSDKError("mandates_no_owner", "no owner signed in; sign in first");
|
|
201
|
+
}
|
|
202
|
+
return owner;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
/* -------------------------------------------------------------------------- */
|
|
206
|
+
/* Helpers */
|
|
207
|
+
/* -------------------------------------------------------------------------- */
|
|
208
|
+
function defaultSphereFromScopes(scopes) {
|
|
209
|
+
if (scopes.some((s) => s.endsWith(".self")))
|
|
210
|
+
return "self";
|
|
211
|
+
if (scopes.some((s) => s.endsWith(".circle")))
|
|
212
|
+
return "circle";
|
|
213
|
+
return "public";
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Validate the SDK-side `compute` namespace and project it onto the
|
|
217
|
+
* snake_case shape the protocol layer canonicalizes into the mandate.
|
|
218
|
+
*
|
|
219
|
+
* Returns `null` if the caller passed nothing — that's the "no compute
|
|
220
|
+
* authorization" path. Throws {@link AithosSDKError} on any structural
|
|
221
|
+
* problem (camelCase mirror of the rules `validateComputeAuthorization`
|
|
222
|
+
* enforces server-side; we duplicate them here so a misuse fails at the
|
|
223
|
+
* SDK boundary with a precise error rather than only blowing up at mint).
|
|
224
|
+
*/
|
|
225
|
+
function projectCompute(c) {
|
|
226
|
+
if (c === undefined)
|
|
227
|
+
return null;
|
|
228
|
+
const hasDaily = typeof c.dailyCapMicrocredits === "number";
|
|
229
|
+
const hasTotal = typeof c.totalCapMicrocredits === "number";
|
|
230
|
+
if (!hasDaily && !hasTotal) {
|
|
231
|
+
throw new AithosSDKError("mandates_invalid_compute", "compute namespace requires at least one of dailyCapMicrocredits " +
|
|
232
|
+
"or totalCapMicrocredits — an unbounded compute mandate is a bearer " +
|
|
233
|
+
"token to drain the subject's wallet.");
|
|
234
|
+
}
|
|
235
|
+
for (const [field, value] of [
|
|
236
|
+
["dailyCapMicrocredits", c.dailyCapMicrocredits],
|
|
237
|
+
["totalCapMicrocredits", c.totalCapMicrocredits],
|
|
238
|
+
["maxCreditsPerCall", c.maxCreditsPerCall],
|
|
239
|
+
]) {
|
|
240
|
+
if (value === undefined)
|
|
241
|
+
continue;
|
|
242
|
+
if (!Number.isInteger(value) || value <= 0) {
|
|
243
|
+
throw new AithosSDKError("mandates_invalid_compute", `compute.${field} must be a positive integer (got ${value}).`);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
if (c.allowedModels !== undefined) {
|
|
247
|
+
if (!Array.isArray(c.allowedModels)) {
|
|
248
|
+
throw new AithosSDKError("mandates_invalid_compute", "compute.allowedModels must be an array of strings.");
|
|
249
|
+
}
|
|
250
|
+
for (const m of c.allowedModels) {
|
|
251
|
+
if (typeof m !== "string" || m.length === 0) {
|
|
252
|
+
throw new AithosSDKError("mandates_invalid_compute", `compute.allowedModels entries must be non-empty strings (got ${JSON.stringify(m)}).`);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
const wire = {};
|
|
257
|
+
if (hasDaily)
|
|
258
|
+
wire.daily_cap_microcredits = c.dailyCapMicrocredits;
|
|
259
|
+
if (hasTotal)
|
|
260
|
+
wire.total_cap_microcredits = c.totalCapMicrocredits;
|
|
261
|
+
if (c.maxCreditsPerCall !== undefined)
|
|
262
|
+
wire.max_credits_per_call = c.maxCreditsPerCall;
|
|
263
|
+
if (c.allowedModels !== undefined)
|
|
264
|
+
wire.allowed_models = [...c.allowedModels];
|
|
265
|
+
return wire;
|
|
266
|
+
}
|
|
267
|
+
function toOwnedMandate(it) {
|
|
268
|
+
return {
|
|
269
|
+
mandateId: requireString(it, "mandate_id"),
|
|
270
|
+
issuerDid: requireString(it, "issuer_did"),
|
|
271
|
+
actorDid: requireString(it, "actor_did"),
|
|
272
|
+
scopes: filterScopeArray(it["scopes"]),
|
|
273
|
+
notBefore: typeof it["not_before"] === "number" ? it["not_before"] : null,
|
|
274
|
+
notAfter: typeof it["not_after"] === "number" ? it["not_after"] : null,
|
|
275
|
+
createdAt: typeof it["created_at"] === "number" ? it["created_at"] : 0,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
function requireString(o, key) {
|
|
279
|
+
const v = o[key];
|
|
280
|
+
if (typeof v !== "string") {
|
|
281
|
+
throw new AithosSDKError("mandates_response_malformed", `list_mandates row missing string field ${key}`);
|
|
282
|
+
}
|
|
283
|
+
return v;
|
|
284
|
+
}
|
|
285
|
+
function filterScopeArray(v) {
|
|
286
|
+
if (!Array.isArray(v))
|
|
287
|
+
return [];
|
|
288
|
+
return v.filter((s) => typeof s === "string");
|
|
289
|
+
}
|
|
13
290
|
//# sourceMappingURL=mandates.js.map
|
package/dist/src/sdk.d.ts
CHANGED
|
@@ -1,18 +1,51 @@
|
|
|
1
|
+
import type { AithosAuth } from "./auth.js";
|
|
1
2
|
import { ComputeNamespace } from "./compute.js";
|
|
2
3
|
import { type AithosSdkEndpoints } from "./endpoints.js";
|
|
4
|
+
import { EthosNamespace } from "./ethos.js";
|
|
5
|
+
import { MandatesNamespace } from "./mandates.js";
|
|
3
6
|
import { WalletNamespace } from "./wallet.js";
|
|
4
|
-
|
|
7
|
+
export interface AithosSDKConfig {
|
|
8
|
+
/**
|
|
9
|
+
* The {@link AithosAuth} instance the SDK reads sign-in state from.
|
|
10
|
+
* Constructed and managed by the app — typically a singleton at the
|
|
11
|
+
* top of the application tree.
|
|
12
|
+
*/
|
|
13
|
+
readonly auth: AithosAuth;
|
|
14
|
+
/**
|
|
15
|
+
* Application DID — identifies the calling app in mandates, audit
|
|
16
|
+
* logs, billing splits. Issued at developer onboarding (will be
|
|
17
|
+
* self-service before 0.1.0 stable).
|
|
18
|
+
*/
|
|
19
|
+
readonly appDid: string;
|
|
20
|
+
/**
|
|
21
|
+
* Optional endpoint overrides. Production defaults point at the
|
|
22
|
+
* Aithos infrastructure; pass overrides for staging, self-hosting,
|
|
23
|
+
* tests.
|
|
24
|
+
*/
|
|
25
|
+
readonly endpoints?: Partial<AithosSdkEndpoints>;
|
|
26
|
+
/**
|
|
27
|
+
* Optional `fetch` implementation. Defaults to the global `fetch`.
|
|
28
|
+
* Used by tests to inject a mock without monkeypatching globals.
|
|
29
|
+
*/
|
|
30
|
+
readonly fetch?: typeof fetch;
|
|
31
|
+
}
|
|
5
32
|
export declare class AithosSDK {
|
|
6
33
|
/** Resolved endpoint configuration (defaults + caller overrides). */
|
|
7
34
|
readonly endpoints: AithosSdkEndpoints;
|
|
8
35
|
/** Application DID (as declared in mandates). */
|
|
9
36
|
readonly appDid: string;
|
|
10
|
-
/**
|
|
11
|
-
readonly
|
|
37
|
+
/** The same auth instance the app constructed and passed in. */
|
|
38
|
+
readonly auth: AithosAuth;
|
|
12
39
|
/** Compute proxy namespace — Bedrock invocation. */
|
|
13
40
|
readonly compute: ComputeNamespace;
|
|
14
41
|
/** Wallet namespace — Stripe top-up. */
|
|
15
42
|
readonly wallet: WalletNamespace;
|
|
43
|
+
/** Ethos editing namespace — load, mutate (staged), publish. */
|
|
44
|
+
readonly ethos: EthosNamespace;
|
|
45
|
+
/** Mandate lifecycle namespace — create / list / revoke. */
|
|
46
|
+
readonly mandates: MandatesNamespace;
|
|
16
47
|
constructor(config: AithosSDKConfig);
|
|
48
|
+
/** DID of the currently signed-in owner, or null if no owner is loaded. */
|
|
49
|
+
get userDid(): string | null;
|
|
17
50
|
}
|
|
18
51
|
//# sourceMappingURL=sdk.d.ts.map
|
package/dist/src/sdk.js
CHANGED
|
@@ -1,58 +1,62 @@
|
|
|
1
1
|
// SPDX-License-Identifier: Apache-2.0
|
|
2
2
|
// Copyright 2026 Mathieu Colla
|
|
3
|
-
// AithosSDK — top-level developer surface.
|
|
4
|
-
//
|
|
5
|
-
// One construction, namespaced methods. The SDK takes the user's identity
|
|
6
|
-
// (a `BrowserIdentity` from `@aithos/protocol-client`) and the calling
|
|
7
|
-
// app's DID, then exposes:
|
|
8
|
-
//
|
|
9
|
-
// sdk.compute — Bedrock invocation through the compute proxy.
|
|
10
|
-
// sdk.wallet — Stripe Checkout for credit-pack top-ups.
|
|
11
|
-
// sdk.ethos — re-exports from protocol-client (zone editor, signers).
|
|
12
|
-
// sdk.onboarding / sdk.mandates — likewise.
|
|
13
|
-
//
|
|
14
|
-
// The class is intentionally thin: the heavy lifting lives in dedicated
|
|
15
|
-
// namespace classes (`ComputeNamespace`, `WalletNamespace`) so each can be
|
|
16
|
-
// tested in isolation with a mock fetch and so an advanced caller could
|
|
17
|
-
// instantiate just one of them if needed.
|
|
18
3
|
import { ComputeNamespace } from "./compute.js";
|
|
19
4
|
import { resolveEndpoints } from "./endpoints.js";
|
|
5
|
+
import { EthosNamespace } from "./ethos.js";
|
|
6
|
+
import { MandatesNamespace } from "./mandates.js";
|
|
20
7
|
import { WalletNamespace } from "./wallet.js";
|
|
21
8
|
export class AithosSDK {
|
|
22
9
|
/** Resolved endpoint configuration (defaults + caller overrides). */
|
|
23
10
|
endpoints;
|
|
24
11
|
/** Application DID (as declared in mandates). */
|
|
25
12
|
appDid;
|
|
26
|
-
/**
|
|
27
|
-
|
|
13
|
+
/** The same auth instance the app constructed and passed in. */
|
|
14
|
+
auth;
|
|
28
15
|
/** Compute proxy namespace — Bedrock invocation. */
|
|
29
16
|
compute;
|
|
30
17
|
/** Wallet namespace — Stripe top-up. */
|
|
31
18
|
wallet;
|
|
19
|
+
/** Ethos editing namespace — load, mutate (staged), publish. */
|
|
20
|
+
ethos;
|
|
21
|
+
/** Mandate lifecycle namespace — create / list / revoke. */
|
|
22
|
+
mandates;
|
|
32
23
|
constructor(config) {
|
|
33
|
-
if (!config.
|
|
34
|
-
throw new TypeError("AithosSDK: config.
|
|
24
|
+
if (!config.auth) {
|
|
25
|
+
throw new TypeError("AithosSDK: config.auth is required");
|
|
35
26
|
}
|
|
36
27
|
if (!config.appDid || typeof config.appDid !== "string") {
|
|
37
28
|
throw new TypeError("AithosSDK: config.appDid is required (string)");
|
|
38
29
|
}
|
|
39
30
|
this.endpoints = resolveEndpoints(config.endpoints);
|
|
40
31
|
this.appDid = config.appDid;
|
|
41
|
-
this.
|
|
32
|
+
this.auth = config.auth;
|
|
42
33
|
const fetchImpl = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
43
34
|
this.compute = new ComputeNamespace({
|
|
44
|
-
|
|
35
|
+
auth: config.auth,
|
|
45
36
|
appDid: config.appDid,
|
|
46
37
|
endpoints: this.endpoints,
|
|
47
38
|
fetch: fetchImpl,
|
|
48
39
|
});
|
|
49
40
|
this.wallet = new WalletNamespace({
|
|
50
|
-
|
|
41
|
+
auth: config.auth,
|
|
51
42
|
appDid: config.appDid,
|
|
52
|
-
userDid: config.identity.did,
|
|
53
43
|
endpoints: this.endpoints,
|
|
54
44
|
fetch: fetchImpl,
|
|
55
45
|
});
|
|
46
|
+
this.ethos = new EthosNamespace({
|
|
47
|
+
auth: config.auth,
|
|
48
|
+
endpoints: this.endpoints,
|
|
49
|
+
fetch: fetchImpl,
|
|
50
|
+
});
|
|
51
|
+
this.mandates = new MandatesNamespace({
|
|
52
|
+
auth: config.auth,
|
|
53
|
+
endpoints: this.endpoints,
|
|
54
|
+
fetch: fetchImpl,
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/** DID of the currently signed-in owner, or null if no owner is loaded. */
|
|
58
|
+
get userDid() {
|
|
59
|
+
return this.auth.getOwnerInfo()?.did ?? null;
|
|
56
60
|
}
|
|
57
61
|
}
|
|
58
62
|
//# sourceMappingURL=sdk.js.map
|
package/dist/src/wallet.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import type { AithosAuth } from "./auth.js";
|
|
2
2
|
import { type AithosSdkEndpoints } from "./endpoints.js";
|
|
3
3
|
/**
|
|
4
4
|
* Canonical credit-pack identifiers. Pricing and microcredit amounts are
|
|
@@ -44,12 +44,10 @@ export interface GetBalanceResult {
|
|
|
44
44
|
readonly exists: boolean;
|
|
45
45
|
}
|
|
46
46
|
export interface WalletNamespaceDeps {
|
|
47
|
-
/**
|
|
48
|
-
readonly
|
|
47
|
+
/** Auth instance — the wallet reads the active owner DID + signing key from here. */
|
|
48
|
+
readonly auth: AithosAuth;
|
|
49
49
|
/** App DID — sent as audit attribution alongside the balance request. */
|
|
50
50
|
readonly appDid: string;
|
|
51
|
-
/** Pre-resolved DID convenience accessor (mirrors identity.did). */
|
|
52
|
-
readonly userDid: string;
|
|
53
51
|
readonly endpoints: AithosSdkEndpoints;
|
|
54
52
|
readonly fetch: typeof fetch;
|
|
55
53
|
}
|
|
@@ -64,7 +62,7 @@ export declare class WalletNamespace {
|
|
|
64
62
|
* hosted URL — the caller is responsible for redirecting the user (e.g.
|
|
65
63
|
* `window.location.href = result.checkoutUrl`).
|
|
66
64
|
*
|
|
67
|
-
* On success, the Stripe webhook will credit
|
|
65
|
+
* On success, the Stripe webhook will credit the user's wallet once the
|
|
68
66
|
* payment clears. Wallet balances are shared across all Aithos apps that
|
|
69
67
|
* use the same DID.
|
|
70
68
|
*/
|