@agenttool/sdk 0.2.2 → 0.2.3
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/dist/economy.d.ts +89 -8
- package/dist/economy.js +173 -18
- package/dist/index.d.ts +1 -1
- package/dist/types.d.ts +33 -1
- package/package.json +1 -1
package/dist/economy.d.ts
CHANGED
|
@@ -1,27 +1,108 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Economy client for the wallet/
|
|
2
|
+
* Economy client for the wallet/escrow API.
|
|
3
3
|
*/
|
|
4
|
-
import type { CreateWalletOptions, Wallet } from "./types.js";
|
|
4
|
+
import type { CreateEscrowOptions, CreateWalletOptions, Escrow, Wallet, WalletPolicy } from "./types.js";
|
|
5
5
|
import type { HttpConfig } from "./memory.js";
|
|
6
6
|
/**
|
|
7
|
-
* Client for the economy
|
|
7
|
+
* Client for the agent-economy API — wallets and escrows.
|
|
8
8
|
*
|
|
9
9
|
* @example
|
|
10
10
|
* ```ts
|
|
11
11
|
* const at = new AgentTool();
|
|
12
|
-
*
|
|
13
|
-
*
|
|
12
|
+
*
|
|
13
|
+
* // Create and fund a wallet
|
|
14
|
+
* const wallet = await at.economy.createWallet({ name: "agent-wallet", agentId: "agent-1" });
|
|
15
|
+
* await at.economy.fundWallet(wallet.id, { amount: 500, description: "Weekly budget" });
|
|
16
|
+
*
|
|
17
|
+
* // Create an escrow for agent-to-agent payment
|
|
18
|
+
* const escrow = await at.economy.createEscrow({
|
|
19
|
+
* creatorWalletId: wallet.id,
|
|
20
|
+
* amount: 100,
|
|
21
|
+
* description: "Summarise 50 research papers",
|
|
22
|
+
* });
|
|
23
|
+
* await at.economy.releaseEscrow(escrow.id);
|
|
14
24
|
* ```
|
|
15
25
|
*/
|
|
16
26
|
export declare class EconomyClient {
|
|
17
27
|
private readonly http;
|
|
18
28
|
/** @internal */
|
|
19
29
|
constructor(http: HttpConfig);
|
|
30
|
+
private url;
|
|
31
|
+
private req;
|
|
20
32
|
/**
|
|
21
33
|
* Create a new wallet.
|
|
22
|
-
*
|
|
23
|
-
* @param options - Wallet creation options (name required).
|
|
24
|
-
* @returns The created Wallet object.
|
|
25
34
|
*/
|
|
26
35
|
createWallet(options: CreateWalletOptions): Promise<Wallet>;
|
|
36
|
+
/**
|
|
37
|
+
* List all wallets for this project.
|
|
38
|
+
*/
|
|
39
|
+
listWallets(): Promise<Wallet[]>;
|
|
40
|
+
/**
|
|
41
|
+
* Get a wallet by ID.
|
|
42
|
+
*/
|
|
43
|
+
getWallet(walletId: string): Promise<Wallet>;
|
|
44
|
+
/**
|
|
45
|
+
* Fund a wallet with credits.
|
|
46
|
+
*/
|
|
47
|
+
fundWallet(walletId: string, options: {
|
|
48
|
+
amount: number;
|
|
49
|
+
description?: string;
|
|
50
|
+
metadata?: Record<string, unknown>;
|
|
51
|
+
}): Promise<Record<string, unknown>>;
|
|
52
|
+
/**
|
|
53
|
+
* Spend credits from a wallet (subject to spending policy).
|
|
54
|
+
*/
|
|
55
|
+
spend(walletId: string, options: {
|
|
56
|
+
amount: number;
|
|
57
|
+
counterparty: string;
|
|
58
|
+
description: string;
|
|
59
|
+
metadata?: Record<string, unknown>;
|
|
60
|
+
}): Promise<Record<string, unknown>>;
|
|
61
|
+
/**
|
|
62
|
+
* Set or update a wallet's spending policy.
|
|
63
|
+
*/
|
|
64
|
+
setPolicy(walletId: string, policy: WalletPolicy): Promise<Record<string, unknown>>;
|
|
65
|
+
/**
|
|
66
|
+
* Freeze a wallet — halts all spending immediately.
|
|
67
|
+
*/
|
|
68
|
+
freezeWallet(walletId: string): Promise<Wallet>;
|
|
69
|
+
/**
|
|
70
|
+
* Unfreeze a wallet to resume normal operation.
|
|
71
|
+
*/
|
|
72
|
+
unfreezeWallet(walletId: string): Promise<Wallet>;
|
|
73
|
+
/**
|
|
74
|
+
* Get paginated transaction history for a wallet.
|
|
75
|
+
*/
|
|
76
|
+
getTransactions(walletId: string, options?: {
|
|
77
|
+
limit?: number;
|
|
78
|
+
offset?: number;
|
|
79
|
+
}): Promise<Record<string, unknown>[]>;
|
|
80
|
+
/**
|
|
81
|
+
* Create an escrow — locks credits until work is released or refunded.
|
|
82
|
+
*/
|
|
83
|
+
createEscrow(options: CreateEscrowOptions): Promise<Escrow>;
|
|
84
|
+
/**
|
|
85
|
+
* List escrows, optionally filtered by status.
|
|
86
|
+
*/
|
|
87
|
+
listEscrows(status?: Escrow["status"]): Promise<Escrow[]>;
|
|
88
|
+
/**
|
|
89
|
+
* Get an escrow by ID.
|
|
90
|
+
*/
|
|
91
|
+
getEscrow(escrowId: string): Promise<Escrow>;
|
|
92
|
+
/**
|
|
93
|
+
* Accept an escrow as the worker.
|
|
94
|
+
*/
|
|
95
|
+
acceptEscrow(escrowId: string, workerWalletId: string): Promise<Escrow>;
|
|
96
|
+
/**
|
|
97
|
+
* Release escrow funds to the worker.
|
|
98
|
+
*/
|
|
99
|
+
releaseEscrow(escrowId: string): Promise<Escrow>;
|
|
100
|
+
/**
|
|
101
|
+
* Refund escrow credits back to the creator.
|
|
102
|
+
*/
|
|
103
|
+
refundEscrow(escrowId: string): Promise<Escrow>;
|
|
104
|
+
/**
|
|
105
|
+
* Flag an escrow as disputed — credits stay locked pending resolution.
|
|
106
|
+
*/
|
|
107
|
+
disputeEscrow(escrowId: string): Promise<Escrow>;
|
|
27
108
|
}
|
package/dist/economy.js
CHANGED
|
@@ -1,15 +1,25 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Economy client for the wallet/
|
|
2
|
+
* Economy client for the wallet/escrow API.
|
|
3
3
|
*/
|
|
4
4
|
import { AgentToolError } from "./errors.js";
|
|
5
5
|
/**
|
|
6
|
-
* Client for the economy
|
|
6
|
+
* Client for the agent-economy API — wallets and escrows.
|
|
7
7
|
*
|
|
8
8
|
* @example
|
|
9
9
|
* ```ts
|
|
10
10
|
* const at = new AgentTool();
|
|
11
|
-
*
|
|
12
|
-
*
|
|
11
|
+
*
|
|
12
|
+
* // Create and fund a wallet
|
|
13
|
+
* const wallet = await at.economy.createWallet({ name: "agent-wallet", agentId: "agent-1" });
|
|
14
|
+
* await at.economy.fundWallet(wallet.id, { amount: 500, description: "Weekly budget" });
|
|
15
|
+
*
|
|
16
|
+
* // Create an escrow for agent-to-agent payment
|
|
17
|
+
* const escrow = await at.economy.createEscrow({
|
|
18
|
+
* creatorWalletId: wallet.id,
|
|
19
|
+
* amount: 100,
|
|
20
|
+
* description: "Summarise 50 research papers",
|
|
21
|
+
* });
|
|
22
|
+
* await at.economy.releaseEscrow(escrow.id);
|
|
13
23
|
* ```
|
|
14
24
|
*/
|
|
15
25
|
export class EconomyClient {
|
|
@@ -18,34 +28,179 @@ export class EconomyClient {
|
|
|
18
28
|
constructor(http) {
|
|
19
29
|
this.http = http;
|
|
20
30
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
31
|
+
// ── helpers ──────────────────────────────────────────────────────────────
|
|
32
|
+
url(path) {
|
|
33
|
+
return `${this.http.baseUrl}${path}`;
|
|
34
|
+
}
|
|
35
|
+
async req(method, path, body, params) {
|
|
36
|
+
let url = this.url(path);
|
|
37
|
+
if (params) {
|
|
38
|
+
const qs = new URLSearchParams(params).toString();
|
|
39
|
+
url = `${url}?${qs}`;
|
|
40
|
+
}
|
|
30
41
|
const resp = await globalThis.fetch(url, {
|
|
31
|
-
method
|
|
42
|
+
method,
|
|
32
43
|
headers: this.http.headers,
|
|
33
|
-
body: JSON.stringify(body),
|
|
44
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
34
45
|
signal: AbortSignal.timeout(this.http.timeout),
|
|
35
46
|
});
|
|
36
47
|
if (resp.status >= 400) {
|
|
37
48
|
let detail;
|
|
38
49
|
try {
|
|
39
50
|
const json = (await resp.json());
|
|
40
|
-
detail = json.detail ?? resp.statusText;
|
|
51
|
+
detail = json.detail ?? json.error ?? resp.statusText;
|
|
41
52
|
}
|
|
42
53
|
catch {
|
|
43
54
|
detail = resp.statusText;
|
|
44
55
|
}
|
|
45
56
|
throw new AgentToolError(`Economy API error (${resp.status}): ${detail}`, {
|
|
46
|
-
hint: "Check
|
|
57
|
+
hint: "Check wallet ID, balance, and spending policy.",
|
|
47
58
|
});
|
|
48
59
|
}
|
|
49
|
-
return
|
|
60
|
+
return resp.json();
|
|
61
|
+
}
|
|
62
|
+
// ── Wallets ───────────────────────────────────────────────────────────────
|
|
63
|
+
/**
|
|
64
|
+
* Create a new wallet.
|
|
65
|
+
*/
|
|
66
|
+
async createWallet(options) {
|
|
67
|
+
const body = { name: options.name };
|
|
68
|
+
if (options.agentId)
|
|
69
|
+
body.agentId = options.agentId;
|
|
70
|
+
if (options.currency)
|
|
71
|
+
body.currency = options.currency;
|
|
72
|
+
const r = await this.req("POST", "/v1/wallets", body);
|
|
73
|
+
return r.data ?? r;
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* List all wallets for this project.
|
|
77
|
+
*/
|
|
78
|
+
async listWallets() {
|
|
79
|
+
const r = await this.req("GET", "/v1/wallets");
|
|
80
|
+
return r.data ?? r;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Get a wallet by ID.
|
|
84
|
+
*/
|
|
85
|
+
async getWallet(walletId) {
|
|
86
|
+
const r = await this.req("GET", `/v1/wallets/${walletId}`);
|
|
87
|
+
return r.data ?? r;
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Fund a wallet with credits.
|
|
91
|
+
*/
|
|
92
|
+
async fundWallet(walletId, options) {
|
|
93
|
+
const body = {
|
|
94
|
+
amount: options.amount,
|
|
95
|
+
description: options.description ?? "Manual fund",
|
|
96
|
+
};
|
|
97
|
+
if (options.metadata)
|
|
98
|
+
body.metadata = options.metadata;
|
|
99
|
+
return this.req("POST", `/v1/wallets/${walletId}/fund`, body);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Spend credits from a wallet (subject to spending policy).
|
|
103
|
+
*/
|
|
104
|
+
async spend(walletId, options) {
|
|
105
|
+
const body = {
|
|
106
|
+
amount: options.amount,
|
|
107
|
+
counterparty: options.counterparty,
|
|
108
|
+
description: options.description,
|
|
109
|
+
};
|
|
110
|
+
if (options.metadata)
|
|
111
|
+
body.metadata = options.metadata;
|
|
112
|
+
return this.req("POST", `/v1/wallets/${walletId}/spend`, body);
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Set or update a wallet's spending policy.
|
|
116
|
+
*/
|
|
117
|
+
async setPolicy(walletId, policy) {
|
|
118
|
+
return this.req("PUT", `/v1/wallets/${walletId}/policy`, policy);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Freeze a wallet — halts all spending immediately.
|
|
122
|
+
*/
|
|
123
|
+
async freezeWallet(walletId) {
|
|
124
|
+
const r = await this.req("POST", `/v1/wallets/${walletId}/freeze`);
|
|
125
|
+
return r.data ?? r;
|
|
126
|
+
}
|
|
127
|
+
/**
|
|
128
|
+
* Unfreeze a wallet to resume normal operation.
|
|
129
|
+
*/
|
|
130
|
+
async unfreezeWallet(walletId) {
|
|
131
|
+
const r = await this.req("POST", `/v1/wallets/${walletId}/unfreeze`);
|
|
132
|
+
return r.data ?? r;
|
|
133
|
+
}
|
|
134
|
+
/**
|
|
135
|
+
* Get paginated transaction history for a wallet.
|
|
136
|
+
*/
|
|
137
|
+
async getTransactions(walletId, options) {
|
|
138
|
+
const params = {};
|
|
139
|
+
if (options?.limit)
|
|
140
|
+
params.limit = String(options.limit);
|
|
141
|
+
if (options?.offset)
|
|
142
|
+
params.offset = String(options.offset);
|
|
143
|
+
const r = await this.req("GET", `/v1/wallets/${walletId}/transactions`, undefined, Object.keys(params).length ? params : undefined);
|
|
144
|
+
return r.data ?? r;
|
|
145
|
+
}
|
|
146
|
+
// ── Escrows ───────────────────────────────────────────────────────────────
|
|
147
|
+
/**
|
|
148
|
+
* Create an escrow — locks credits until work is released or refunded.
|
|
149
|
+
*/
|
|
150
|
+
async createEscrow(options) {
|
|
151
|
+
const body = {
|
|
152
|
+
creatorWalletId: options.creatorWalletId,
|
|
153
|
+
amount: options.amount,
|
|
154
|
+
description: options.description,
|
|
155
|
+
};
|
|
156
|
+
if (options.workerWalletId)
|
|
157
|
+
body.workerWalletId = options.workerWalletId;
|
|
158
|
+
if (options.deadline)
|
|
159
|
+
body.deadline = options.deadline;
|
|
160
|
+
const r = await this.req("POST", "/v1/escrows", body);
|
|
161
|
+
return r.data ?? r;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* List escrows, optionally filtered by status.
|
|
165
|
+
*/
|
|
166
|
+
async listEscrows(status) {
|
|
167
|
+
const params = status ? { status } : undefined;
|
|
168
|
+
const r = await this.req("GET", "/v1/escrows", undefined, params);
|
|
169
|
+
return r.data ?? r;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Get an escrow by ID.
|
|
173
|
+
*/
|
|
174
|
+
async getEscrow(escrowId) {
|
|
175
|
+
const r = await this.req("GET", `/v1/escrows/${escrowId}`);
|
|
176
|
+
return r.data ?? r;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Accept an escrow as the worker.
|
|
180
|
+
*/
|
|
181
|
+
async acceptEscrow(escrowId, workerWalletId) {
|
|
182
|
+
const r = await this.req("POST", `/v1/escrows/${escrowId}/accept`, { workerWalletId });
|
|
183
|
+
return r.data ?? r;
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* Release escrow funds to the worker.
|
|
187
|
+
*/
|
|
188
|
+
async releaseEscrow(escrowId) {
|
|
189
|
+
const r = await this.req("POST", `/v1/escrows/${escrowId}/release`);
|
|
190
|
+
return r.data ?? r;
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Refund escrow credits back to the creator.
|
|
194
|
+
*/
|
|
195
|
+
async refundEscrow(escrowId) {
|
|
196
|
+
const r = await this.req("POST", `/v1/escrows/${escrowId}/refund`);
|
|
197
|
+
return r.data ?? r;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Flag an escrow as disputed — credits stay locked pending resolution.
|
|
201
|
+
*/
|
|
202
|
+
async disputeEscrow(escrowId) {
|
|
203
|
+
const r = await this.req("POST", `/v1/escrows/${escrowId}/dispute`);
|
|
204
|
+
return r.data ?? r;
|
|
50
205
|
}
|
|
51
206
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -12,4 +12,4 @@
|
|
|
12
12
|
export { AgentTool } from "./client.js";
|
|
13
13
|
export { AgentToolError } from "./errors.js";
|
|
14
14
|
export type { Trace, StoreTraceOptions, SearchTracesOptions, TraceSearchResult, TraceChain } from "./traces.js";
|
|
15
|
-
export type { CreateWalletOptions, ExecuteResult, Memory, ScrapeResult, SearchMemoryOptions, SearchResponse, SearchResult, StoreOptions, UsageStats, VerifyResult, Wallet, } from "./types.js";
|
|
15
|
+
export type { CreateEscrowOptions, CreateWalletOptions, Escrow, ExecuteResult, Memory, ScrapeResult, SearchMemoryOptions, SearchResponse, SearchResult, StoreOptions, UsageStats, VerifyResult, Wallet, WalletPolicy, } from "./types.js";
|
package/dist/types.d.ts
CHANGED
|
@@ -76,9 +76,41 @@ export interface Wallet {
|
|
|
76
76
|
id: string;
|
|
77
77
|
name: string;
|
|
78
78
|
balance: number;
|
|
79
|
-
|
|
79
|
+
currency: string;
|
|
80
|
+
frozen: boolean;
|
|
81
|
+
agentId?: string;
|
|
82
|
+
createdAt?: string;
|
|
80
83
|
}
|
|
81
84
|
/** Options for creating a wallet. */
|
|
82
85
|
export interface CreateWalletOptions {
|
|
83
86
|
name: string;
|
|
87
|
+
agentId?: string;
|
|
88
|
+
currency?: string;
|
|
89
|
+
}
|
|
90
|
+
/** A wallet spending policy. */
|
|
91
|
+
export interface WalletPolicy {
|
|
92
|
+
maxPerTransaction?: number | null;
|
|
93
|
+
maxPerHour?: number | null;
|
|
94
|
+
maxPerDay?: number | null;
|
|
95
|
+
allowedRecipients?: string[] | null;
|
|
96
|
+
requiresApprovalAbove?: number | null;
|
|
97
|
+
}
|
|
98
|
+
/** An escrow object. */
|
|
99
|
+
export interface Escrow {
|
|
100
|
+
id: string;
|
|
101
|
+
status: "pending" | "active" | "released" | "refunded" | "disputed";
|
|
102
|
+
amount: number;
|
|
103
|
+
description: string;
|
|
104
|
+
creatorWalletId: string;
|
|
105
|
+
workerWalletId?: string | null;
|
|
106
|
+
deadline?: string | null;
|
|
107
|
+
createdAt?: string;
|
|
108
|
+
}
|
|
109
|
+
/** Options for creating an escrow. */
|
|
110
|
+
export interface CreateEscrowOptions {
|
|
111
|
+
creatorWalletId: string;
|
|
112
|
+
amount: number;
|
|
113
|
+
description: string;
|
|
114
|
+
workerWalletId?: string;
|
|
115
|
+
deadline?: string;
|
|
84
116
|
}
|