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

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.
@@ -1,4 +1,4 @@
1
- export declare const VERSION = "0.1.0-alpha.0";
1
+ export declare const VERSION = "0.1.0-alpha.2";
2
2
  export { AithosSDK } from "./sdk.js";
3
3
  export type { AithosSDKConfig } from "./types.js";
4
4
  export { AithosSDKError } from "./types.js";
@@ -6,7 +6,7 @@ export type { AithosSdkEndpoints } from "./endpoints.js";
6
6
  export { DEFAULT_SDK_ENDPOINTS } from "./endpoints.js";
7
7
  export type { ComputeMessage, InvokeBedrockArgs, InvokeBedrockResult, StopReason, } from "./compute.js";
8
8
  export { ComputeNamespace } from "./compute.js";
9
- export type { CreditPackId, CreateTopupSessionArgs, CreateTopupSessionResult, } from "./wallet.js";
9
+ export type { CreditPackId, CreateTopupSessionArgs, CreateTopupSessionResult, GetBalanceArgs, GetBalanceResult, } from "./wallet.js";
10
10
  export { WalletNamespace } from "./wallet.js";
11
11
  export * as ethos from "./ethos.js";
12
12
  export * as onboarding from "./onboarding.js";
package/dist/src/index.js CHANGED
@@ -17,7 +17,7 @@
17
17
  // Public types specific to the SDK (`AithosSDKConfig`, `AithosSDKError`)
18
18
  // are exported from here. Endpoint config (`AithosSdkEndpoints`,
19
19
  // `DEFAULT_SDK_ENDPOINTS`) likewise.
20
- export const VERSION = "0.1.0-alpha.0";
20
+ export const VERSION = "0.1.0-alpha.2";
21
21
  export { AithosSDK } from "./sdk.js";
22
22
  export { AithosSDKError } from "./types.js";
23
23
  export { DEFAULT_SDK_ENDPOINTS } from "./endpoints.js";
package/dist/src/sdk.js CHANGED
@@ -47,6 +47,8 @@ export class AithosSDK {
47
47
  fetch: fetchImpl,
48
48
  });
49
49
  this.wallet = new WalletNamespace({
50
+ identity: config.identity,
51
+ appDid: config.appDid,
50
52
  userDid: config.identity.did,
51
53
  endpoints: this.endpoints,
52
54
  fetch: fetchImpl,
@@ -1,3 +1,4 @@
1
+ import { type BrowserIdentity } from "@aithos/protocol-client";
1
2
  import { type AithosSdkEndpoints } from "./endpoints.js";
2
3
  /**
3
4
  * Canonical credit-pack identifiers. Pricing and microcredit amounts are
@@ -24,7 +25,30 @@ export interface CreateTopupSessionResult {
24
25
  /** Stripe Checkout session id (for diagnostics). */
25
26
  readonly sessionId: string;
26
27
  }
28
+ export interface GetBalanceArgs {
29
+ /** Abort signal to cancel the request. */
30
+ readonly signal?: AbortSignal;
31
+ }
32
+ export interface GetBalanceResult {
33
+ /** Current wallet balance in microcredits. 0 if the wallet does not yet exist. */
34
+ readonly balance: number;
35
+ /**
36
+ * Microcredits spent today. Resets server-side at midnight UTC.
37
+ * Useful for showing "X / daily cap" UI.
38
+ */
39
+ readonly dailySpent: number;
40
+ /**
41
+ * Whether a wallet row exists for this DID. False on a never-funded
42
+ * identity — `balance` will be 0 in that case.
43
+ */
44
+ readonly exists: boolean;
45
+ }
27
46
  export interface WalletNamespaceDeps {
47
+ /** User identity — needed to sign the balance-lookup envelope. */
48
+ readonly identity: BrowserIdentity;
49
+ /** App DID — sent as audit attribution alongside the balance request. */
50
+ readonly appDid: string;
51
+ /** Pre-resolved DID convenience accessor (mirrors identity.did). */
28
52
  readonly userDid: string;
29
53
  readonly endpoints: AithosSdkEndpoints;
30
54
  readonly fetch: typeof fetch;
@@ -45,5 +69,23 @@ export declare class WalletNamespace {
45
69
  * use the same DID.
46
70
  */
47
71
  createTopupSession(args: CreateTopupSessionArgs): Promise<CreateTopupSessionResult>;
72
+ /**
73
+ * Read the user's current wallet balance.
74
+ *
75
+ * Routed through the compute proxy at `${compute}/v1/invoke` because
76
+ * that's where the Aithos-platform DDB IAM lives — keeps the wallet
77
+ * Lambda focused on Stripe and avoids granting read-on-wallet to a
78
+ * second function. The call is gated by the same signed-envelope
79
+ * verification as `invokeBedrock`: only the owner of the user's
80
+ * `#public` key can read their balance.
81
+ *
82
+ * Returns `{ balance: 0, exists: false, dailySpent: 0 }` for an
83
+ * identity that has never been funded — the call still succeeds,
84
+ * the row just doesn't exist yet.
85
+ *
86
+ * @throws {AithosSDKError} on protocol errors (network, http,
87
+ * envelope-verify failures from the proxy).
88
+ */
89
+ getBalance(args?: GetBalanceArgs): Promise<GetBalanceResult>;
48
90
  }
49
91
  //# sourceMappingURL=wallet.d.ts.map
@@ -1,17 +1,20 @@
1
1
  // SPDX-License-Identifier: Apache-2.0
2
2
  // Copyright 2026 Mathieu Colla
3
- // Wallet namespace — Stripe Checkout for credit-pack top-ups.
3
+ // Wallet namespace — credit-pack top-ups (Stripe) and balance lookup.
4
4
  //
5
- // Posts to `${wallet}/v1/wallet/topup/checkout`. The Lambda creates a
6
- // Stripe Checkout session bound to the user's DID via `metadata`, returns
7
- // the hosted URL. The frontend then redirects the user. Stripe webhook
8
- // (server-side) credits the wallet on `checkout.session.completed`.
9
- //
10
- // v0 scope: no envelope verify on the checkout endpoint the link is
11
- // single-use and metadata-bound to `user_did`. Anyone creating a session
12
- // for someone else's DID would just gift them credits, no value to an
13
- // attacker. Envelope-verify gets added in a follow-up if abuse appears.
14
- import { walletTopupCheckoutUrl, } from "./endpoints.js";
5
+ // Two methods:
6
+ // - createTopupSession() posts to `${wallet}/v1/wallet/topup/checkout`
7
+ // to create a Stripe Checkout session. The Lambda binds the session
8
+ // to the user's DID via metadata; the Stripe webhook credits the
9
+ // wallet on `checkout.session.completed`. No envelope verify on this
10
+ // endpoint at v0 (the link is single-use and metadata-boundanyone
11
+ // creating a session for someone else's DID just gifts them credits).
12
+ // - getBalance() JSON-RPC `aithos.wallet_get_balance` against the
13
+ // compute proxy at `${compute}/v1/invoke`. Read-only DDB GetItem on
14
+ // the wallet table, gated by the same signed envelope verification
15
+ // as compute_invoke. Returns the current balance + daily spent.
16
+ import { buildSignedEnvelope, } from "@aithos/protocol-client";
17
+ import { computeInvokeUrl, walletTopupCheckoutUrl, } from "./endpoints.js";
15
18
  import { AithosSDKError } from "./types.js";
16
19
  /**
17
20
  * `sdk.wallet` namespace.
@@ -67,5 +70,67 @@ export class WalletNamespace {
67
70
  }
68
71
  return { checkoutUrl: ok.checkout_url, sessionId: ok.session_id };
69
72
  }
73
+ /**
74
+ * Read the user's current wallet balance.
75
+ *
76
+ * Routed through the compute proxy at `${compute}/v1/invoke` because
77
+ * that's where the Aithos-platform DDB IAM lives — keeps the wallet
78
+ * Lambda focused on Stripe and avoids granting read-on-wallet to a
79
+ * second function. The call is gated by the same signed-envelope
80
+ * verification as `invokeBedrock`: only the owner of the user's
81
+ * `#public` key can read their balance.
82
+ *
83
+ * Returns `{ balance: 0, exists: false, dailySpent: 0 }` for an
84
+ * identity that has never been funded — the call still succeeds,
85
+ * the row just doesn't exist yet.
86
+ *
87
+ * @throws {AithosSDKError} on protocol errors (network, http,
88
+ * envelope-verify failures from the proxy).
89
+ */
90
+ async getBalance(args = {}) {
91
+ const { identity, appDid, endpoints, fetch: fetchImpl } = this.#deps;
92
+ const url = computeInvokeUrl(endpoints);
93
+ const params = { app_did: appDid };
94
+ const envelope = buildSignedEnvelope({
95
+ iss: identity.did,
96
+ aud: url,
97
+ method: "aithos.wallet_get_balance",
98
+ verificationMethod: `${identity.did}#public`,
99
+ params,
100
+ signer: identity.public,
101
+ });
102
+ let res;
103
+ try {
104
+ res = await fetchImpl(url, {
105
+ method: "POST",
106
+ headers: { "content-type": "application/json" },
107
+ body: JSON.stringify({
108
+ jsonrpc: "2.0",
109
+ id: "aithos.wallet_get_balance",
110
+ method: "aithos.wallet_get_balance",
111
+ params: { ...params, _envelope: envelope },
112
+ }),
113
+ ...(args.signal ? { signal: args.signal } : {}),
114
+ });
115
+ }
116
+ catch (e) {
117
+ throw new AithosSDKError("network", e.message);
118
+ }
119
+ if (!res.ok) {
120
+ throw new AithosSDKError("http", `HTTP ${res.status} ${res.statusText}`, { status: res.status });
121
+ }
122
+ const body = (await res.json());
123
+ if (body.error) {
124
+ throw new AithosSDKError(String(body.error.code), body.error.message, body.error.data ? { data: body.error.data } : undefined);
125
+ }
126
+ if (!body.result) {
127
+ throw new AithosSDKError("empty", "empty result from wallet_get_balance");
128
+ }
129
+ return {
130
+ balance: typeof body.result.balance === "number" ? body.result.balance : 0,
131
+ dailySpent: typeof body.result.dailySpent === "number" ? body.result.dailySpent : 0,
132
+ exists: body.result.exists === true,
133
+ };
134
+ }
70
135
  }
71
136
  //# sourceMappingURL=wallet.js.map
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aithos/sdk",
3
- "version": "0.1.0-alpha.1",
3
+ "version": "0.1.0-alpha.2",
4
4
  "description": "Aithos SDK — high-level TypeScript developer kit for building agentic apps on the Aithos protocol. Wraps @aithos/protocol-client and exposes the Aithos compute proxy and wallet (Stripe top-up) endpoints.",
5
5
  "keywords": [
6
6
  "aithos",