@purseragents/sdk 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +24 -2
- package/dist/abi.d.ts +3235 -0
- package/dist/abi.js +254 -0
- package/dist/all.d.ts +2 -0
- package/dist/all.js +3 -0
- package/dist/chain.d.ts +31 -0
- package/dist/chain.js +28 -0
- package/dist/client.d.ts +421 -0
- package/dist/client.js +221 -0
- package/dist/eip712.d.ts +381 -0
- package/dist/eip712.js +132 -0
- package/dist/index.d.ts +73 -0
- package/dist/index.js +66 -0
- package/dist/merchant.d.ts +41 -0
- package/dist/merchant.js +91 -0
- package/dist/schema.d.ts +583 -0
- package/dist/schema.js +11 -0
- package/dist/tools.d.ts +42 -0
- package/dist/tools.js +212 -0
- package/dist/webhooks.d.ts +8 -0
- package/dist/webhooks.js +24 -0
- package/dist/x402.d.ts +100 -0
- package/dist/x402.js +89 -0
- package/package.json +28 -4
package/dist/client.d.ts
ADDED
|
@@ -0,0 +1,421 @@
|
|
|
1
|
+
import type * as S from "./schema.js";
|
|
2
|
+
export interface PurserOptions {
|
|
3
|
+
/** The engine, e.g. http://localhost:8787 */
|
|
4
|
+
baseUrl: string;
|
|
5
|
+
/** An agent API key (prs_...). Needed for paying, selling and everything an agent does. */
|
|
6
|
+
apiKey?: string;
|
|
7
|
+
/** An owner session token (own_...) from sign-in. Needed for keys, webhooks, teams and account settings. */
|
|
8
|
+
ownerToken?: string;
|
|
9
|
+
fetch?: typeof fetch;
|
|
10
|
+
}
|
|
11
|
+
export declare class PurserError extends Error {
|
|
12
|
+
status: number;
|
|
13
|
+
code: string;
|
|
14
|
+
body?: unknown | undefined;
|
|
15
|
+
constructor(status: number, code: string, message: string, body?: unknown | undefined);
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* The Purser engine API, typed. Amounts are base units (USDG has 6 decimals: 1 USDG = 1000000).
|
|
19
|
+
* Methods that move money return the intent, the firewall verdict and the receipt; a blocked payment comes back
|
|
20
|
+
* with status "blocked" and the reasons, it does not throw.
|
|
21
|
+
*/
|
|
22
|
+
export declare class Purser {
|
|
23
|
+
opts: PurserOptions;
|
|
24
|
+
private f;
|
|
25
|
+
constructor(opts: PurserOptions);
|
|
26
|
+
private req;
|
|
27
|
+
health: () => Promise<S.Health>;
|
|
28
|
+
config: () => Promise<Record<string, unknown>>;
|
|
29
|
+
me: () => Promise<{
|
|
30
|
+
safe: S.Address;
|
|
31
|
+
key: S.ApiKey;
|
|
32
|
+
sessionAddress: S.Address | null;
|
|
33
|
+
}>;
|
|
34
|
+
/** Pay someone from the agent's account. */
|
|
35
|
+
pay: (p: {
|
|
36
|
+
to: string;
|
|
37
|
+
amount: bigint | number | string;
|
|
38
|
+
token?: string;
|
|
39
|
+
service?: string;
|
|
40
|
+
memo?: string;
|
|
41
|
+
trigger?: string;
|
|
42
|
+
taskId?: string;
|
|
43
|
+
customer?: string;
|
|
44
|
+
category?: string;
|
|
45
|
+
cosignature?: string;
|
|
46
|
+
}) => Promise<S.PayResult>;
|
|
47
|
+
/** Ask the firewall about a payment without making it. */
|
|
48
|
+
checkPayment: (p: {
|
|
49
|
+
to: string;
|
|
50
|
+
amount: bigint | number | string;
|
|
51
|
+
token?: string;
|
|
52
|
+
service?: string;
|
|
53
|
+
trigger?: string;
|
|
54
|
+
}) => Promise<S.Verdict>;
|
|
55
|
+
/** Send an intent signed by a key the agent keeps itself (see signPayIntent in the SDK). */
|
|
56
|
+
submitIntent: (body: Record<string, unknown>) => Promise<S.PayResult>;
|
|
57
|
+
cosign: (intentId: string, signature: string) => Promise<S.PayResult>;
|
|
58
|
+
intent: (id: string) => Promise<S.Intent>;
|
|
59
|
+
call: (p: {
|
|
60
|
+
target: string;
|
|
61
|
+
data: string;
|
|
62
|
+
value?: bigint | string;
|
|
63
|
+
spendToken?: string;
|
|
64
|
+
spendLimit?: bigint | string;
|
|
65
|
+
memo?: string;
|
|
66
|
+
trigger?: string;
|
|
67
|
+
}) => Promise<S.PayResult>;
|
|
68
|
+
swap: (p: {
|
|
69
|
+
tokenIn: string;
|
|
70
|
+
tokenOut: string;
|
|
71
|
+
amountIn: bigint | string;
|
|
72
|
+
minOut: bigint | string;
|
|
73
|
+
fee?: number;
|
|
74
|
+
}) => Promise<S.PayResult>;
|
|
75
|
+
/** Fetch an x402 URL; if it asks for payment, pay it from the account within the rules. */
|
|
76
|
+
x402Fetch: (url: string, init?: {
|
|
77
|
+
method?: string;
|
|
78
|
+
headers?: Record<string, string>;
|
|
79
|
+
body?: string;
|
|
80
|
+
maxAmount?: bigint | string;
|
|
81
|
+
trigger?: string;
|
|
82
|
+
taskId?: string;
|
|
83
|
+
}) => Promise<{
|
|
84
|
+
status: number;
|
|
85
|
+
paid: boolean;
|
|
86
|
+
body: string;
|
|
87
|
+
headers: Record<string, string>;
|
|
88
|
+
payment: S.PayResult | null;
|
|
89
|
+
settlement: unknown;
|
|
90
|
+
}>;
|
|
91
|
+
/** OpenAI-compatible chat completions, paid from the agent's account. */
|
|
92
|
+
chat: {
|
|
93
|
+
completions: {
|
|
94
|
+
create: (body: {
|
|
95
|
+
model: string;
|
|
96
|
+
messages: {
|
|
97
|
+
role: string;
|
|
98
|
+
content: unknown;
|
|
99
|
+
}[];
|
|
100
|
+
[k: string]: unknown;
|
|
101
|
+
}) => Promise<{
|
|
102
|
+
choices: {
|
|
103
|
+
message: {
|
|
104
|
+
role: string;
|
|
105
|
+
content: string;
|
|
106
|
+
};
|
|
107
|
+
}[];
|
|
108
|
+
usage?: Record<string, number>;
|
|
109
|
+
purser: {
|
|
110
|
+
costUsd: string;
|
|
111
|
+
receipt: string | null;
|
|
112
|
+
paid: boolean;
|
|
113
|
+
};
|
|
114
|
+
}>;
|
|
115
|
+
};
|
|
116
|
+
};
|
|
117
|
+
models: () => Promise<{
|
|
118
|
+
data: {
|
|
119
|
+
id: string;
|
|
120
|
+
}[];
|
|
121
|
+
}>;
|
|
122
|
+
streams: {
|
|
123
|
+
create: (p: {
|
|
124
|
+
payee: string;
|
|
125
|
+
ratePerSecond: bigint | string;
|
|
126
|
+
maxDurationSeconds: number;
|
|
127
|
+
graceSeconds?: number;
|
|
128
|
+
token?: string;
|
|
129
|
+
}) => Promise<S.PayResult & {
|
|
130
|
+
streamId?: string;
|
|
131
|
+
}>;
|
|
132
|
+
stop: (id: string) => Promise<{
|
|
133
|
+
txHash: string;
|
|
134
|
+
}>;
|
|
135
|
+
heartbeat: (id: string, upTo: number, signature: string) => Promise<{
|
|
136
|
+
txHash: string;
|
|
137
|
+
}>;
|
|
138
|
+
get: (id: string) => Promise<S.Stream>;
|
|
139
|
+
list: (safe: string) => Promise<S.Page<S.Stream>>;
|
|
140
|
+
};
|
|
141
|
+
subscriptions: {
|
|
142
|
+
create: (p: {
|
|
143
|
+
payee: string;
|
|
144
|
+
amount: bigint | string;
|
|
145
|
+
periodSeconds: number;
|
|
146
|
+
maxCharges: number;
|
|
147
|
+
token?: string;
|
|
148
|
+
}) => Promise<S.PayResult & {
|
|
149
|
+
subscriptionId?: string;
|
|
150
|
+
}>;
|
|
151
|
+
cancel: (id: string) => Promise<{
|
|
152
|
+
txHash: string;
|
|
153
|
+
}>;
|
|
154
|
+
list: (safe: string) => Promise<S.Page<S.Subscription>>;
|
|
155
|
+
};
|
|
156
|
+
store: {
|
|
157
|
+
create: (p: {
|
|
158
|
+
name: string;
|
|
159
|
+
description?: string;
|
|
160
|
+
model?: "per_call" | "per_minute" | "subscription";
|
|
161
|
+
price: bigint | string;
|
|
162
|
+
periodSeconds?: number;
|
|
163
|
+
upstream?: string;
|
|
164
|
+
response?: string;
|
|
165
|
+
}) => Promise<S.StoreService>;
|
|
166
|
+
update: (id: string, p: {
|
|
167
|
+
active?: boolean;
|
|
168
|
+
price?: bigint | string;
|
|
169
|
+
description?: string;
|
|
170
|
+
}) => Promise<S.StoreService>;
|
|
171
|
+
list: (safe?: string) => Promise<S.Page<S.StoreService>>;
|
|
172
|
+
usage: (id: string) => Promise<{
|
|
173
|
+
service: S.StoreService;
|
|
174
|
+
items: unknown[];
|
|
175
|
+
}>;
|
|
176
|
+
};
|
|
177
|
+
splits: {
|
|
178
|
+
get: (safe: string) => Promise<{
|
|
179
|
+
items: (S.Split & {
|
|
180
|
+
role: string;
|
|
181
|
+
paid: S.Money[];
|
|
182
|
+
})[];
|
|
183
|
+
keptBps: number;
|
|
184
|
+
}>;
|
|
185
|
+
prepare: (safe: string, splits: {
|
|
186
|
+
to: string;
|
|
187
|
+
bps: number;
|
|
188
|
+
label?: string;
|
|
189
|
+
role?: string;
|
|
190
|
+
}[]) => Promise<S.PreparedSafeTx>;
|
|
191
|
+
};
|
|
192
|
+
invoices: {
|
|
193
|
+
create: (p: {
|
|
194
|
+
payer: string;
|
|
195
|
+
amount: bigint | string;
|
|
196
|
+
description: string;
|
|
197
|
+
token?: string;
|
|
198
|
+
dueInSeconds?: number;
|
|
199
|
+
reviewPeriodSeconds?: number;
|
|
200
|
+
arbiter?: string | null;
|
|
201
|
+
}) => Promise<S.Invoice>;
|
|
202
|
+
pay: (id: string) => Promise<S.PayResult & {
|
|
203
|
+
invoice?: S.Invoice;
|
|
204
|
+
}>;
|
|
205
|
+
deliver: (id: string, note?: string) => Promise<{
|
|
206
|
+
invoice: S.Invoice;
|
|
207
|
+
}>;
|
|
208
|
+
release: (id: string) => Promise<{
|
|
209
|
+
invoice: S.Invoice;
|
|
210
|
+
}>;
|
|
211
|
+
dispute: (id: string) => Promise<{
|
|
212
|
+
invoice: S.Invoice;
|
|
213
|
+
}>;
|
|
214
|
+
resolve: (id: string, payeeBps: number) => Promise<{
|
|
215
|
+
invoice: S.Invoice;
|
|
216
|
+
}>;
|
|
217
|
+
claim: (id: string) => Promise<{
|
|
218
|
+
txHash: string;
|
|
219
|
+
invoice: S.Invoice;
|
|
220
|
+
}>;
|
|
221
|
+
refund: (id: string) => Promise<{
|
|
222
|
+
txHash: string;
|
|
223
|
+
invoice: S.Invoice;
|
|
224
|
+
}>;
|
|
225
|
+
get: (id: string) => Promise<S.Invoice>;
|
|
226
|
+
list: (safe: string) => Promise<S.Page<S.Invoice>>;
|
|
227
|
+
};
|
|
228
|
+
treasury: {
|
|
229
|
+
get: (safe: string) => Promise<S.Treasury>;
|
|
230
|
+
vaults: () => Promise<S.Page<S.TreasuryVault>>;
|
|
231
|
+
deposit: (vault: string, amount: bigint | string) => Promise<S.PayResult>;
|
|
232
|
+
withdraw: (vault: string, amount: bigint | string) => Promise<S.PayResult>;
|
|
233
|
+
sweep: (buffer?: bigint | string) => Promise<S.PayResult | {
|
|
234
|
+
moved: string;
|
|
235
|
+
reason: string;
|
|
236
|
+
}>;
|
|
237
|
+
};
|
|
238
|
+
accounts: {
|
|
239
|
+
list: (owner?: string) => Promise<S.Page<S.AccountSummary>>;
|
|
240
|
+
get: (safe: string) => Promise<S.Account>;
|
|
241
|
+
rules: (safe: string) => Promise<Pick<S.Account, "config" | "rules" | "allowlist" | "services" | "treasuryVaults" | "splits">>;
|
|
242
|
+
receipts: (safe: string, q?: {
|
|
243
|
+
direction?: "in" | "out";
|
|
244
|
+
category?: string;
|
|
245
|
+
before?: number;
|
|
246
|
+
limit?: number;
|
|
247
|
+
}) => Promise<S.Page<S.Receipt>>;
|
|
248
|
+
intents: (safe: string, q?: {
|
|
249
|
+
status?: string;
|
|
250
|
+
limit?: number;
|
|
251
|
+
}) => Promise<S.Page<S.Intent>>;
|
|
252
|
+
verdicts: (safe: string, q?: {
|
|
253
|
+
decision?: "allow" | "block";
|
|
254
|
+
limit?: number;
|
|
255
|
+
}) => Promise<S.Page<S.Verdict>>;
|
|
256
|
+
approvals: (safe: string, all?: boolean) => Promise<S.Page<S.PendingApproval>>;
|
|
257
|
+
watchdog: (safe: string) => Promise<{
|
|
258
|
+
baseline: S.WatchdogBaseline;
|
|
259
|
+
events: S.WatchdogEvent[];
|
|
260
|
+
}>;
|
|
261
|
+
pnl: (safe: string, q?: {
|
|
262
|
+
from?: number;
|
|
263
|
+
to?: number;
|
|
264
|
+
}) => Promise<S.Pnl>;
|
|
265
|
+
exportCsv: (safe: string, q?: {
|
|
266
|
+
from?: number;
|
|
267
|
+
to?: number;
|
|
268
|
+
}) => Promise<string>;
|
|
269
|
+
};
|
|
270
|
+
receipt: (id: string) => Promise<S.Receipt>;
|
|
271
|
+
verdict: (id: string) => Promise<S.Verdict>;
|
|
272
|
+
owner: {
|
|
273
|
+
signInMessage: (address: string) => Promise<{
|
|
274
|
+
nonce: string;
|
|
275
|
+
issued: number;
|
|
276
|
+
message: string;
|
|
277
|
+
}>;
|
|
278
|
+
signIn: (p: {
|
|
279
|
+
address: string;
|
|
280
|
+
nonce: string;
|
|
281
|
+
issued: number;
|
|
282
|
+
signature: string;
|
|
283
|
+
}) => Promise<{
|
|
284
|
+
token: string;
|
|
285
|
+
address: string;
|
|
286
|
+
expires: number;
|
|
287
|
+
}>;
|
|
288
|
+
prepareAccount: (p: Record<string, unknown>) => Promise<{
|
|
289
|
+
predictedSafe: S.Address;
|
|
290
|
+
tx: S.PreparedTx;
|
|
291
|
+
}>;
|
|
292
|
+
register: (p: {
|
|
293
|
+
safe: string;
|
|
294
|
+
name: string;
|
|
295
|
+
kind?: string;
|
|
296
|
+
public?: boolean;
|
|
297
|
+
template?: string;
|
|
298
|
+
}) => Promise<S.Account>;
|
|
299
|
+
update: (safe: string, p: {
|
|
300
|
+
name?: string;
|
|
301
|
+
kind?: string;
|
|
302
|
+
public?: boolean;
|
|
303
|
+
}) => Promise<S.Account>;
|
|
304
|
+
prepareRules: (safe: string, change: Record<string, unknown>) => Promise<S.PreparedSafeTx>;
|
|
305
|
+
prepareTemplate: (safe: string, template: string) => Promise<S.PreparedSafeTx>;
|
|
306
|
+
prepareFreeze: (safe: string) => Promise<S.PreparedTx>;
|
|
307
|
+
prepareUnfreeze: (safe: string) => Promise<S.PreparedTx>;
|
|
308
|
+
preparePasskey: (safe: string, x: string, y: string) => Promise<S.PreparedSafeTx>;
|
|
309
|
+
prepareApproval: (intentId: string, decision: "approve" | "reject") => Promise<S.PreparedTx>;
|
|
310
|
+
passkeyDecision: (intentId: string, decision: "approve" | "reject", assertion: Record<string, unknown>) => Promise<{
|
|
311
|
+
txHash: string;
|
|
312
|
+
result: S.PayResult | null;
|
|
313
|
+
}>;
|
|
314
|
+
createKey: (safe: string, p?: {
|
|
315
|
+
name?: string;
|
|
316
|
+
scopes?: S.ApiScope[];
|
|
317
|
+
managed?: boolean;
|
|
318
|
+
}) => Promise<{
|
|
319
|
+
key: S.ApiKey;
|
|
320
|
+
secret: string;
|
|
321
|
+
}>;
|
|
322
|
+
keys: (safe: string) => Promise<S.Page<S.ApiKey>>;
|
|
323
|
+
revokeKey: (safe: string, id: string) => Promise<{
|
|
324
|
+
ok: true;
|
|
325
|
+
}>;
|
|
326
|
+
setAutoFreeze: (safe: string, autoFreeze: boolean) => Promise<{
|
|
327
|
+
autoFreeze: boolean;
|
|
328
|
+
}>;
|
|
329
|
+
addProvider: (safe: string, p: Record<string, unknown>) => Promise<unknown>;
|
|
330
|
+
};
|
|
331
|
+
teams: {
|
|
332
|
+
create: (p: {
|
|
333
|
+
name: string;
|
|
334
|
+
treasury: string;
|
|
335
|
+
}) => Promise<S.Team>;
|
|
336
|
+
get: (id: string) => Promise<S.Team>;
|
|
337
|
+
list: (owner?: string) => Promise<S.Page<S.Team>>;
|
|
338
|
+
org: (id: string) => Promise<S.OrgChartNode>;
|
|
339
|
+
addDepartment: (id: string, p: {
|
|
340
|
+
name: string;
|
|
341
|
+
dailyBudget?: string | null;
|
|
342
|
+
}) => Promise<S.Team>;
|
|
343
|
+
addMember: (id: string, p: {
|
|
344
|
+
safe: string;
|
|
345
|
+
departmentId?: string | null;
|
|
346
|
+
role?: string;
|
|
347
|
+
dailyBudget?: string | null;
|
|
348
|
+
}) => Promise<{
|
|
349
|
+
team: S.Team;
|
|
350
|
+
treasuryTx: S.PreparedSafeTx;
|
|
351
|
+
}>;
|
|
352
|
+
topUp: (id: string, member: string, amount: bigint | string, memo?: string) => Promise<S.PayResult>;
|
|
353
|
+
pnl: (id: string, q?: {
|
|
354
|
+
from?: number;
|
|
355
|
+
to?: number;
|
|
356
|
+
}) => Promise<S.Pnl>;
|
|
357
|
+
};
|
|
358
|
+
webhooks: {
|
|
359
|
+
create: (p: {
|
|
360
|
+
url: string;
|
|
361
|
+
events: S.WebhookEventType[];
|
|
362
|
+
safe?: string | null;
|
|
363
|
+
}) => Promise<{
|
|
364
|
+
webhook: S.Webhook;
|
|
365
|
+
secret: string;
|
|
366
|
+
}>;
|
|
367
|
+
list: () => Promise<S.Page<S.Webhook>>;
|
|
368
|
+
remove: (id: string) => Promise<{
|
|
369
|
+
ok: true;
|
|
370
|
+
}>;
|
|
371
|
+
deliveries: (id: string) => Promise<S.Page<S.WebhookDelivery>>;
|
|
372
|
+
test: (id: string) => Promise<{
|
|
373
|
+
delivered: number;
|
|
374
|
+
}>;
|
|
375
|
+
};
|
|
376
|
+
alerts: {
|
|
377
|
+
list: (safe: string) => Promise<S.Page<S.Alert>>;
|
|
378
|
+
set: (safe: string, alerts: {
|
|
379
|
+
kind: S.AlertKind;
|
|
380
|
+
threshold?: string | null;
|
|
381
|
+
}[]) => Promise<S.Page<S.Alert>>;
|
|
382
|
+
};
|
|
383
|
+
directory: (q?: {
|
|
384
|
+
source?: string;
|
|
385
|
+
limit?: number;
|
|
386
|
+
}) => Promise<S.Page<S.DirectoryEntry>>;
|
|
387
|
+
economy: () => Promise<S.Economy>;
|
|
388
|
+
leaderboard: (q?: {
|
|
389
|
+
limit?: number;
|
|
390
|
+
}) => Promise<S.Page<S.LeaderboardEntry>>;
|
|
391
|
+
publicAgent: (safe: string) => Promise<S.PublicAgent>;
|
|
392
|
+
templates: () => Promise<S.Page<S.Template>>;
|
|
393
|
+
audit: {
|
|
394
|
+
list: (q?: {
|
|
395
|
+
safe?: string;
|
|
396
|
+
limit?: number;
|
|
397
|
+
before?: number;
|
|
398
|
+
}) => Promise<{
|
|
399
|
+
items: S.AuditEntry[];
|
|
400
|
+
signer: string;
|
|
401
|
+
}>;
|
|
402
|
+
export: () => Promise<string>;
|
|
403
|
+
verify: (jsonl: string, signer?: string) => Promise<S.AuditVerifyResult>;
|
|
404
|
+
};
|
|
405
|
+
demo: {
|
|
406
|
+
runFirewall: (paceMs?: number) => Promise<{
|
|
407
|
+
safe: string;
|
|
408
|
+
steps: S.DemoStep[];
|
|
409
|
+
}>;
|
|
410
|
+
lastFirewall: () => Promise<{
|
|
411
|
+
steps: S.DemoStep[];
|
|
412
|
+
safe: string | null;
|
|
413
|
+
}>;
|
|
414
|
+
};
|
|
415
|
+
/** Live events over Server-Sent Events. Returns a function that stops the stream. */
|
|
416
|
+
events(onEvent: (e: S.EngineEvent) => void, opts?: {
|
|
417
|
+
safe?: string;
|
|
418
|
+
types?: S.EngineEventType[];
|
|
419
|
+
since?: number;
|
|
420
|
+
}): () => void;
|
|
421
|
+
}
|
package/dist/client.js
ADDED
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
export class PurserError extends Error {
|
|
2
|
+
status;
|
|
3
|
+
code;
|
|
4
|
+
body;
|
|
5
|
+
constructor(status, code, message, body) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.status = status;
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.body = body;
|
|
10
|
+
this.name = "PurserError";
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
const qs = (q) => {
|
|
14
|
+
if (!q)
|
|
15
|
+
return "";
|
|
16
|
+
const p = Object.entries(q).filter(([, v]) => v !== undefined && v !== null).map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(String(v))}`);
|
|
17
|
+
return p.length ? `?${p.join("&")}` : "";
|
|
18
|
+
};
|
|
19
|
+
const amt = (v) => (typeof v === "bigint" ? v.toString() : String(v));
|
|
20
|
+
/**
|
|
21
|
+
* The Purser engine API, typed. Amounts are base units (USDG has 6 decimals: 1 USDG = 1000000).
|
|
22
|
+
* Methods that move money return the intent, the firewall verdict and the receipt; a blocked payment comes back
|
|
23
|
+
* with status "blocked" and the reasons, it does not throw.
|
|
24
|
+
*/
|
|
25
|
+
export class Purser {
|
|
26
|
+
opts;
|
|
27
|
+
f;
|
|
28
|
+
constructor(opts) {
|
|
29
|
+
this.opts = opts;
|
|
30
|
+
this.f = opts.fetch ?? fetch.bind(globalThis);
|
|
31
|
+
}
|
|
32
|
+
async req(method, path, body, auth = "none", raw = false) {
|
|
33
|
+
const token = auth === "agent" ? this.opts.apiKey : auth === "owner" ? this.opts.ownerToken : undefined;
|
|
34
|
+
if (auth !== "none" && !token)
|
|
35
|
+
throw new PurserError(401, "unauthorized", auth === "agent" ? "This call needs an agent API key (apiKey)." : "This call needs an owner session (ownerToken).");
|
|
36
|
+
const res = await this.f(`${this.opts.baseUrl.replace(/\/$/, "")}${path}`, {
|
|
37
|
+
method,
|
|
38
|
+
headers: { ...(body !== undefined ? { "content-type": "application/json" } : {}), ...(token ? { authorization: `Bearer ${token}` } : {}) },
|
|
39
|
+
body: body === undefined ? undefined : typeof body === "string" ? body : JSON.stringify(body),
|
|
40
|
+
});
|
|
41
|
+
if (raw)
|
|
42
|
+
return (await res.text());
|
|
43
|
+
const text = await res.text();
|
|
44
|
+
const data = text ? JSON.parse(text) : null;
|
|
45
|
+
// 422 = blocked by the rules or the firewall: a normal answer with a verdict
|
|
46
|
+
if (!res.ok && !(res.status === 422 && data && "verdict" in data)) {
|
|
47
|
+
throw new PurserError(res.status, data?.error?.code ?? "http_error", data?.error?.message ?? `HTTP ${res.status}`, data);
|
|
48
|
+
}
|
|
49
|
+
return data;
|
|
50
|
+
}
|
|
51
|
+
// ------------------------------------------------------------------------------------------------ meta
|
|
52
|
+
health = () => this.req("GET", "/v1/health");
|
|
53
|
+
config = () => this.req("GET", "/v1/config");
|
|
54
|
+
me = () => this.req("GET", "/v1/me", undefined, "agent");
|
|
55
|
+
// ------------------------------------------------------------------------------------------------ the agent's money
|
|
56
|
+
/** Pay someone from the agent's account. */
|
|
57
|
+
pay = (p) => this.req("POST", "/v1/pay", { ...p, amount: amt(p.amount) }, "agent");
|
|
58
|
+
/** Ask the firewall about a payment without making it. */
|
|
59
|
+
checkPayment = (p) => this.req("POST", "/v1/firewall/check", { ...p, amount: amt(p.amount) }, "agent");
|
|
60
|
+
/** Send an intent signed by a key the agent keeps itself (see signPayIntent in the SDK). */
|
|
61
|
+
submitIntent = (body) => this.req("POST", "/v1/intents", body);
|
|
62
|
+
cosign = (intentId, signature) => this.req("POST", `/v1/intents/${intentId}/cosign`, { signature });
|
|
63
|
+
intent = (id) => this.req("GET", `/v1/intents/${id}`);
|
|
64
|
+
call = (p) => this.req("POST", "/v1/call", { ...p, value: p.value === undefined ? undefined : amt(p.value), spendLimit: p.spendLimit === undefined ? undefined : amt(p.spendLimit) }, "agent");
|
|
65
|
+
swap = (p) => this.req("POST", "/v1/swap", { ...p, amountIn: amt(p.amountIn), minOut: amt(p.minOut) }, "agent");
|
|
66
|
+
/** Fetch an x402 URL; if it asks for payment, pay it from the account within the rules. */
|
|
67
|
+
x402Fetch = (url, init = {}) => this.req("POST", "/v1/x402/fetch", { url, ...init, maxAmount: init.maxAmount === undefined ? undefined : amt(init.maxAmount) }, "agent");
|
|
68
|
+
/** OpenAI-compatible chat completions, paid from the agent's account. */
|
|
69
|
+
chat = {
|
|
70
|
+
completions: {
|
|
71
|
+
create: (body) => this.req("POST", "/v1/chat/completions", body, "agent"),
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
models = () => this.req("GET", "/v1/models", undefined, "agent");
|
|
75
|
+
streams = {
|
|
76
|
+
create: (p) => this.req("POST", "/v1/streams", { ...p, ratePerSecond: amt(p.ratePerSecond) }, "agent"),
|
|
77
|
+
stop: (id) => this.req("POST", `/v1/streams/${id}/stop`, {}, "agent"),
|
|
78
|
+
heartbeat: (id, upTo, signature) => this.req("POST", `/v1/streams/${id}/heartbeat`, { upTo, signature }),
|
|
79
|
+
get: (id) => this.req("GET", `/v1/streams/${id}`),
|
|
80
|
+
list: (safe) => this.req("GET", `/v1/accounts/${safe}/streams`),
|
|
81
|
+
};
|
|
82
|
+
subscriptions = {
|
|
83
|
+
create: (p) => this.req("POST", "/v1/subscriptions", { ...p, amount: amt(p.amount) }, "agent"),
|
|
84
|
+
cancel: (id) => this.req("POST", `/v1/subscriptions/${id}/cancel`, {}, "agent"),
|
|
85
|
+
list: (safe) => this.req("GET", `/v1/accounts/${safe}/subscriptions`),
|
|
86
|
+
};
|
|
87
|
+
// ------------------------------------------------------------------------------------------------ money in
|
|
88
|
+
store = {
|
|
89
|
+
create: (p) => this.req("POST", "/v1/storefront/services", { ...p, price: amt(p.price) }, "agent"),
|
|
90
|
+
update: (id, p) => this.req("PATCH", `/v1/storefront/services/${id}`, { ...p, price: p.price === undefined ? undefined : amt(p.price) }, "agent"),
|
|
91
|
+
list: (safe) => this.req("GET", `/v1/storefront/services${qs({ safe })}`),
|
|
92
|
+
usage: (id) => this.req("GET", `/v1/storefront/services/${id}/usage`),
|
|
93
|
+
};
|
|
94
|
+
splits = {
|
|
95
|
+
get: (safe) => this.req("GET", `/v1/accounts/${safe}/splits`),
|
|
96
|
+
prepare: (safe, splits) => this.req("POST", `/v1/accounts/${safe}/splits/prepare`, { splits }),
|
|
97
|
+
};
|
|
98
|
+
invoices = {
|
|
99
|
+
create: (p) => this.req("POST", "/v1/invoices", { ...p, amount: amt(p.amount) }, "agent"),
|
|
100
|
+
pay: (id) => this.req("POST", `/v1/invoices/${id}/pay`, {}, "agent"),
|
|
101
|
+
deliver: (id, note) => this.req("POST", `/v1/invoices/${id}/deliver`, { note }, "agent"),
|
|
102
|
+
release: (id) => this.req("POST", `/v1/invoices/${id}/release`, {}, "agent"),
|
|
103
|
+
dispute: (id) => this.req("POST", `/v1/invoices/${id}/dispute`, {}, "agent"),
|
|
104
|
+
resolve: (id, payeeBps) => this.req("POST", `/v1/invoices/${id}/resolve`, { payeeBps }, "agent"),
|
|
105
|
+
claim: (id) => this.req("POST", `/v1/invoices/${id}/claim`, {}),
|
|
106
|
+
refund: (id) => this.req("POST", `/v1/invoices/${id}/refund`, {}),
|
|
107
|
+
get: (id) => this.req("GET", `/v1/invoices/${id}`),
|
|
108
|
+
list: (safe) => this.req("GET", `/v1/accounts/${safe}/invoices`),
|
|
109
|
+
};
|
|
110
|
+
treasury = {
|
|
111
|
+
get: (safe) => this.req("GET", `/v1/accounts/${safe}/treasury`),
|
|
112
|
+
vaults: () => this.req("GET", "/v1/treasury/vaults"),
|
|
113
|
+
deposit: (vault, amount) => this.req("POST", "/v1/treasury/deposit", { vault, amount: amt(amount) }, "agent"),
|
|
114
|
+
withdraw: (vault, amount) => this.req("POST", "/v1/treasury/withdraw", { vault, amount: amt(amount) }, "agent"),
|
|
115
|
+
sweep: (buffer = 0n) => this.req("POST", "/v1/treasury/sweep", { buffer: amt(buffer) }, "agent"),
|
|
116
|
+
};
|
|
117
|
+
// ------------------------------------------------------------------------------------------------ accounts (read)
|
|
118
|
+
accounts = {
|
|
119
|
+
list: (owner) => this.req("GET", `/v1/accounts${qs({ owner })}`),
|
|
120
|
+
get: (safe) => this.req("GET", `/v1/accounts/${safe}`),
|
|
121
|
+
rules: (safe) => this.req("GET", `/v1/accounts/${safe}/rules`),
|
|
122
|
+
receipts: (safe, q) => this.req("GET", `/v1/accounts/${safe}/receipts${qs(q)}`),
|
|
123
|
+
intents: (safe, q) => this.req("GET", `/v1/accounts/${safe}/intents${qs(q)}`),
|
|
124
|
+
verdicts: (safe, q) => this.req("GET", `/v1/accounts/${safe}/verdicts${qs(q)}`),
|
|
125
|
+
approvals: (safe, all = false) => this.req("GET", `/v1/accounts/${safe}/approvals${qs({ all: all ? 1 : undefined })}`),
|
|
126
|
+
watchdog: (safe) => this.req("GET", `/v1/accounts/${safe}/watchdog`),
|
|
127
|
+
pnl: (safe, q) => this.req("GET", `/v1/accounts/${safe}/pnl${qs(q)}`),
|
|
128
|
+
exportCsv: (safe, q) => this.req("GET", `/v1/accounts/${safe}/export.csv${qs(q)}`, undefined, "none", true),
|
|
129
|
+
};
|
|
130
|
+
receipt = (id) => this.req("GET", `/v1/receipts/${id}`);
|
|
131
|
+
verdict = (id) => this.req("GET", `/v1/verdicts/${id}`);
|
|
132
|
+
// ------------------------------------------------------------------------------------------------ owner actions (return transactions to sign)
|
|
133
|
+
owner = {
|
|
134
|
+
signInMessage: (address) => this.req("POST", "/v1/auth/nonce", { address }),
|
|
135
|
+
signIn: (p) => this.req("POST", "/v1/auth/verify", p),
|
|
136
|
+
prepareAccount: (p) => this.req("POST", "/v1/accounts/prepare", p),
|
|
137
|
+
register: (p) => this.req("POST", "/v1/accounts/register", p, "owner"),
|
|
138
|
+
update: (safe, p) => this.req("PATCH", `/v1/accounts/${safe}`, p, "owner"),
|
|
139
|
+
prepareRules: (safe, change) => this.req("POST", `/v1/accounts/${safe}/rules/prepare`, change),
|
|
140
|
+
prepareTemplate: (safe, template) => this.req("POST", `/v1/accounts/${safe}/template/prepare`, { template }),
|
|
141
|
+
prepareFreeze: (safe) => this.req("POST", `/v1/accounts/${safe}/freeze/prepare`, {}),
|
|
142
|
+
prepareUnfreeze: (safe) => this.req("POST", `/v1/accounts/${safe}/unfreeze/prepare`, {}),
|
|
143
|
+
preparePasskey: (safe, x, y) => this.req("POST", `/v1/accounts/${safe}/passkey/prepare`, { x, y }),
|
|
144
|
+
prepareApproval: (intentId, decision) => this.req("POST", `/v1/approvals/${intentId}/prepare`, { decision }),
|
|
145
|
+
passkeyDecision: (intentId, decision, assertion) => this.req("POST", `/v1/approvals/${intentId}/passkey`, { decision, assertion }),
|
|
146
|
+
createKey: (safe, p = {}) => this.req("POST", `/v1/accounts/${safe}/keys`, p, "owner"),
|
|
147
|
+
keys: (safe) => this.req("GET", `/v1/accounts/${safe}/keys`, undefined, "owner"),
|
|
148
|
+
revokeKey: (safe, id) => this.req("DELETE", `/v1/accounts/${safe}/keys/${id}`, undefined, "owner"),
|
|
149
|
+
setAutoFreeze: (safe, autoFreeze) => this.req("PATCH", `/v1/accounts/${safe}/watchdog`, { autoFreeze }, "owner"),
|
|
150
|
+
addProvider: (safe, p) => this.req("POST", `/v1/accounts/${safe}/providers`, p, "owner"),
|
|
151
|
+
};
|
|
152
|
+
teams = {
|
|
153
|
+
create: (p) => this.req("POST", "/v1/teams", p, "owner"),
|
|
154
|
+
get: (id) => this.req("GET", `/v1/teams/${id}`),
|
|
155
|
+
list: (owner) => this.req("GET", `/v1/teams${qs({ owner })}`),
|
|
156
|
+
org: (id) => this.req("GET", `/v1/teams/${id}/org`),
|
|
157
|
+
addDepartment: (id, p) => this.req("POST", `/v1/teams/${id}/departments`, p, "owner"),
|
|
158
|
+
addMember: (id, p) => this.req("POST", `/v1/teams/${id}/members`, p, "owner"),
|
|
159
|
+
topUp: (id, member, amount, memo) => this.req("POST", `/v1/teams/${id}/topups`, { member, amount: amt(amount), memo }, "agent"),
|
|
160
|
+
pnl: (id, q) => this.req("GET", `/v1/teams/${id}/pnl${qs(q)}`),
|
|
161
|
+
};
|
|
162
|
+
webhooks = {
|
|
163
|
+
create: (p) => this.req("POST", "/v1/webhooks", p, "owner"),
|
|
164
|
+
list: () => this.req("GET", "/v1/webhooks", undefined, "owner"),
|
|
165
|
+
remove: (id) => this.req("DELETE", `/v1/webhooks/${id}`, undefined, "owner"),
|
|
166
|
+
deliveries: (id) => this.req("GET", `/v1/webhooks/${id}/deliveries`, undefined, "owner"),
|
|
167
|
+
test: (id) => this.req("POST", `/v1/webhooks/${id}/test`, {}, "owner"),
|
|
168
|
+
};
|
|
169
|
+
alerts = {
|
|
170
|
+
list: (safe) => this.req("GET", `/v1/accounts/${safe}/alerts`),
|
|
171
|
+
set: (safe, alerts) => this.req("PUT", `/v1/accounts/${safe}/alerts`, { alerts }, "owner"),
|
|
172
|
+
};
|
|
173
|
+
// ------------------------------------------------------------------------------------------------ the network
|
|
174
|
+
directory = (q) => this.req("GET", `/v1/directory${qs(q)}`);
|
|
175
|
+
economy = () => this.req("GET", "/v1/economy");
|
|
176
|
+
leaderboard = (q) => this.req("GET", `/v1/leaderboard${qs(q)}`);
|
|
177
|
+
publicAgent = (safe) => this.req("GET", `/v1/agents/${safe}/public`);
|
|
178
|
+
templates = () => this.req("GET", "/v1/templates");
|
|
179
|
+
audit = {
|
|
180
|
+
list: (q) => this.req("GET", `/v1/audit${qs(q)}`),
|
|
181
|
+
export: () => this.req("GET", "/v1/audit/export", undefined, "none", true),
|
|
182
|
+
verify: (jsonl, signer) => this.req("POST", `/v1/audit/verify${qs({ signer })}`, jsonl),
|
|
183
|
+
};
|
|
184
|
+
demo = {
|
|
185
|
+
runFirewall: (paceMs = 0) => this.req("POST", "/v1/demo/firewall/run", { paceMs }),
|
|
186
|
+
lastFirewall: () => this.req("GET", "/v1/demo/firewall"),
|
|
187
|
+
};
|
|
188
|
+
/** Live events over Server-Sent Events. Returns a function that stops the stream. */
|
|
189
|
+
events(onEvent, opts = {}) {
|
|
190
|
+
const ac = new AbortController();
|
|
191
|
+
const url = `${this.opts.baseUrl.replace(/\/$/, "")}/v1/events${qs({ safe: opts.safe, types: opts.types?.join(","), since: opts.since })}`;
|
|
192
|
+
(async () => {
|
|
193
|
+
const res = await this.f(url, { signal: ac.signal, headers: { accept: "text/event-stream" } });
|
|
194
|
+
const reader = res.body.getReader();
|
|
195
|
+
const dec = new TextDecoder();
|
|
196
|
+
let buf = "";
|
|
197
|
+
for (;;) {
|
|
198
|
+
const { value, done } = await reader.read();
|
|
199
|
+
if (done)
|
|
200
|
+
break;
|
|
201
|
+
buf += dec.decode(value, { stream: true });
|
|
202
|
+
let i;
|
|
203
|
+
while ((i = buf.indexOf("\n\n")) >= 0) {
|
|
204
|
+
const chunk = buf.slice(0, i);
|
|
205
|
+
buf = buf.slice(i + 2);
|
|
206
|
+
const data = chunk.split("\n").filter((l) => l.startsWith("data:")).map((l) => l.slice(5).trim()).join("\n");
|
|
207
|
+
const event = chunk.split("\n").find((l) => l.startsWith("event:"))?.slice(6).trim();
|
|
208
|
+
if (data && event !== "ping") {
|
|
209
|
+
try {
|
|
210
|
+
onEvent(JSON.parse(data));
|
|
211
|
+
}
|
|
212
|
+
catch {
|
|
213
|
+
/* not JSON */
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
})().catch(() => undefined);
|
|
219
|
+
return () => ac.abort();
|
|
220
|
+
}
|
|
221
|
+
}
|