agent-escrow 0.1.0 → 0.1.1

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/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "agent-escrow",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "Decentralized marketplace for AI agents — post tasks, bid, escrow payments via Lightning, build trust via ai.wot",
5
5
  "main": "src/index.cjs",
6
+ "types": "src/index.d.cts",
6
7
  "bin": {
7
8
  "agent-escrow": "src/cli.cjs"
8
9
  },
@@ -0,0 +1,213 @@
1
+ // Type definitions for agent-escrow
2
+
3
+ export interface Task {
4
+ eventId: string;
5
+ taskId: string;
6
+ poster: string;
7
+ title: string;
8
+ description: string;
9
+ budget: number;
10
+ deadline: number | null;
11
+ status: TaskStatus;
12
+ capabilities: string[];
13
+ minTrust: number | null;
14
+ lightningAddress: string | null;
15
+ worker: string | null;
16
+ createdAt: number;
17
+ raw: NostrEvent;
18
+ }
19
+
20
+ export interface Bid {
21
+ eventId: string;
22
+ bidder: string;
23
+ taskEventId: string;
24
+ posterPubkey: string;
25
+ amount: number;
26
+ lightningAddress: string;
27
+ eta: number | null;
28
+ message: string;
29
+ createdAt: number;
30
+ raw: NostrEvent;
31
+ }
32
+
33
+ export interface Delivery {
34
+ eventId: string;
35
+ worker: string;
36
+ taskEventId: string;
37
+ posterPubkey: string;
38
+ result: string;
39
+ hash: string | null;
40
+ createdAt: number;
41
+ raw: NostrEvent;
42
+ }
43
+
44
+ export interface Resolution {
45
+ eventId: string;
46
+ poster: string;
47
+ taskEventId: string;
48
+ workerPubkey: string;
49
+ type: ResolutionType;
50
+ paymentBolt11: string | null;
51
+ preimage: string | null;
52
+ message: string;
53
+ createdAt: number;
54
+ raw: NostrEvent;
55
+ }
56
+
57
+ export interface NostrEvent {
58
+ id: string;
59
+ pubkey: string;
60
+ kind: number;
61
+ created_at: number;
62
+ tags: string[][];
63
+ content: string;
64
+ sig: string;
65
+ }
66
+
67
+ export type TaskStatus = 'open' | 'claimed' | 'delivered' | 'completed' | 'cancelled' | 'disputed' | 'expired';
68
+ export type ResolutionType = 'approve' | 'reject' | 'dispute';
69
+
70
+ export interface MarketplaceOptions {
71
+ relays?: string[];
72
+ secretKey: string | Uint8Array;
73
+ nwcUrl?: string;
74
+ lightningAddress?: string;
75
+ defaultMinTrust?: number;
76
+ connectTimeoutMs?: number;
77
+ }
78
+
79
+ export interface PostTaskOptions {
80
+ title: string;
81
+ description?: string;
82
+ budget: number;
83
+ deadline?: number;
84
+ capabilities?: string[];
85
+ minTrust?: number;
86
+ lightningAddress?: string;
87
+ taskId?: string;
88
+ }
89
+
90
+ export interface BrowseOptions {
91
+ status?: TaskStatus | TaskStatus[];
92
+ capabilities?: string | string[];
93
+ poster?: string | string[];
94
+ minBudget?: number;
95
+ maxBudget?: number;
96
+ limit?: number;
97
+ since?: number;
98
+ timeoutMs?: number;
99
+ }
100
+
101
+ export interface SubmitBidOptions {
102
+ taskEventId: string;
103
+ posterPubkey: string;
104
+ amount: number;
105
+ lightningAddress?: string;
106
+ eta?: number;
107
+ message?: string;
108
+ }
109
+
110
+ export interface DeliverOptions {
111
+ taskEventId: string;
112
+ posterPubkey: string;
113
+ result: string;
114
+ includeHash?: boolean;
115
+ }
116
+
117
+ export interface ApproveOptions {
118
+ taskId: string;
119
+ workerPubkey: string;
120
+ workerLightningAddress?: string;
121
+ amount: number;
122
+ message?: string;
123
+ }
124
+
125
+ export interface DisputeOptions {
126
+ taskId: string;
127
+ workerPubkey: string;
128
+ reason: string;
129
+ }
130
+
131
+ export interface ApproveResult {
132
+ resolution: Resolution;
133
+ payment: { bolt11?: string; preimage?: string; amountSats: number; error?: string; manual?: boolean };
134
+ task: Task;
135
+ }
136
+
137
+ export interface DisputeResult {
138
+ resolution: Resolution;
139
+ task: Task;
140
+ }
141
+
142
+ export interface DeliveryVerification {
143
+ valid: boolean;
144
+ reason?: string;
145
+ expected?: string;
146
+ actual?: string;
147
+ }
148
+
149
+ export interface Marketplace {
150
+ pubkey: string;
151
+ postTask(opts: PostTaskOptions): Promise<Task>;
152
+ cancelTask(taskId: string): Promise<Task>;
153
+ browseTasks(opts?: BrowseOptions): Promise<Task[]>;
154
+ fetchTask(posterPubkey: string, taskId: string): Promise<Task | null>;
155
+ myTasks(opts?: { status?: TaskStatus | TaskStatus[]; limit?: number; timeoutMs?: number }): Promise<Task[]>;
156
+ myJobs(opts?: { status?: TaskStatus | TaskStatus[]; limit?: number; timeoutMs?: number }): Promise<Task[]>;
157
+ submitBid(opts: SubmitBidOptions): Promise<Bid>;
158
+ getBids(taskEventId: string): Promise<Bid[]>;
159
+ myBids(opts?: { limit?: number; since?: number; timeoutMs?: number }): Promise<Bid[]>;
160
+ acceptBid(taskId: string, bid: Bid): Promise<Task>;
161
+ deliver(opts: DeliverOptions): Promise<Delivery>;
162
+ getDeliveries(taskEventId: string): Promise<Delivery[]>;
163
+ approve(opts: ApproveOptions): Promise<ApproveResult>;
164
+ dispute(opts: DisputeOptions): Promise<DisputeResult>;
165
+ close(): Promise<void>;
166
+ }
167
+
168
+ export function createMarketplace(opts: MarketplaceOptions): Marketplace;
169
+
170
+ export const KINDS: {
171
+ TASK: 30950;
172
+ BID: 950;
173
+ DELIVERY: 951;
174
+ RESOLUTION: 952;
175
+ };
176
+
177
+ export const TASK_STATUS: {
178
+ OPEN: 'open';
179
+ CLAIMED: 'claimed';
180
+ DELIVERED: 'delivered';
181
+ COMPLETED: 'completed';
182
+ CANCELLED: 'cancelled';
183
+ DISPUTED: 'disputed';
184
+ EXPIRED: 'expired';
185
+ };
186
+
187
+ export const RESOLUTION_TYPES: {
188
+ APPROVE: 'approve';
189
+ REJECT: 'reject';
190
+ DISPUTE: 'dispute';
191
+ };
192
+
193
+ export function createTaskEvent(opts: PostTaskOptions): object;
194
+ export function createBidEvent(opts: SubmitBidOptions & { lightningAddress: string }): object;
195
+ export function createDeliveryEvent(opts: DeliverOptions): object;
196
+ export function createResolutionEvent(opts: { taskEventId: string; workerPubkey: string; type: ResolutionType; paymentBolt11?: string; preimage?: string; message?: string }): object;
197
+
198
+ export function parseTask(event: NostrEvent): Task;
199
+ export function parseBid(event: NostrEvent): Bid;
200
+ export function parseDelivery(event: NostrEvent): Delivery;
201
+ export function parseResolution(event: NostrEvent): Resolution;
202
+
203
+ export function findTasks(relays: any[], opts?: BrowseOptions): Promise<Task[]>;
204
+ export function findBids(relays: any[], taskEventId: string): Promise<Bid[]>;
205
+ export function findDeliveries(relays: any[], taskEventId: string): Promise<Delivery[]>;
206
+ export function findResolutions(relays: any[], taskEventId: string): Promise<Resolution[]>;
207
+ export function getTask(relays: any[], posterPubkey: string, taskId: string): Promise<Task | null>;
208
+
209
+ export function verifyDelivery(delivery: Delivery): DeliveryVerification;
210
+ export function trustAvailable(): boolean;
211
+ export function getTrustScore(pubkey: string, opts?: { relays?: string[] }): Promise<any | null>;
212
+ export function meetsTrustThreshold(pubkey: string, minScore: number, opts?: { relays?: string[] }): Promise<boolean>;
213
+ export function paymentsAvailable(): boolean;
@@ -122,6 +122,43 @@ function createMarketplace(opts) {
122
122
  return getTask(r, posterPubkey, taskId);
123
123
  }
124
124
 
125
+ /**
126
+ * List my posted tasks (tasks I created)
127
+ */
128
+ async function myTasks(opts = {}) {
129
+ const r = await ensureConnected();
130
+ return findTasks(r, {
131
+ poster: pubkey,
132
+ status: opts.status,
133
+ limit: opts.limit || 50,
134
+ timeoutMs: opts.timeoutMs || 8000
135
+ });
136
+ }
137
+
138
+ /**
139
+ * List tasks assigned to me (I'm the worker)
140
+ */
141
+ async function myJobs(opts = {}) {
142
+ const r = await ensureConnected();
143
+ // Query all tasks that tag our pubkey
144
+ const filter = {
145
+ kinds: [KINDS.TASK],
146
+ '#p': [pubkey]
147
+ };
148
+ if (opts.limit) filter.limit = opts.limit;
149
+ const { queryRelays } = require('./nostr.cjs');
150
+ const events = await queryRelays(r, filter, opts.timeoutMs || 8000);
151
+ let tasks = events.map(parseTask);
152
+ // Filter to only tasks where we're the assigned worker (not poster)
153
+ tasks = tasks.filter(t => t.worker === pubkey && t.poster !== pubkey);
154
+ if (opts.status) {
155
+ const statuses = Array.isArray(opts.status) ? opts.status : [opts.status];
156
+ tasks = tasks.filter(t => statuses.includes(t.status));
157
+ }
158
+ tasks.sort((a, b) => b.createdAt - a.createdAt);
159
+ return tasks;
160
+ }
161
+
125
162
  // ── Bid operations ────────────────────────────────────
126
163
 
127
164
  /**
@@ -157,6 +194,18 @@ function createMarketplace(opts) {
157
194
  return findBids(r, taskEventId);
158
195
  }
159
196
 
197
+ /**
198
+ * List my submitted bids
199
+ */
200
+ async function myBids(opts = {}) {
201
+ const r = await ensureConnected();
202
+ return findMyBids(r, pubkey, {
203
+ limit: opts.limit || 50,
204
+ since: opts.since,
205
+ timeoutMs: opts.timeoutMs || 8000
206
+ });
207
+ }
208
+
160
209
  /**
161
210
  * Accept a bid — claims the task for the bidder
162
211
  */
@@ -340,10 +389,13 @@ function createMarketplace(opts) {
340
389
  cancelTask,
341
390
  browseTasks,
342
391
  fetchTask,
392
+ myTasks,
393
+ myJobs,
343
394
 
344
395
  // Bids
345
396
  submitBid,
346
397
  getBids,
398
+ myBids,
347
399
  acceptBid,
348
400
 
349
401
  // Delivery