ai-market 0.1.9 → 0.1.11

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.
Files changed (2) hide show
  1. package/dist/index.js +82 -7
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -31,6 +31,8 @@ function print(data) {
31
31
  function splitList(val) {
32
32
  return val.split(",").map((s) => s.trim()).filter(Boolean);
33
33
  }
34
+ var POLL_INTERVAL = 3e3;
35
+ var CHECKOUT_TIMEOUT = 5 * 60 * 1e3;
34
36
  async function run(fn) {
35
37
  try {
36
38
  await fn();
@@ -79,6 +81,18 @@ function registerCommands(program2) {
79
81
  headers: { Authorization: `Bearer ${token}` }
80
82
  }).catch(() => {
81
83
  });
84
+ const profile = {};
85
+ if (opts.offers) profile.offers = opts.offers;
86
+ if (opts.wants) profile.wants = opts.wants;
87
+ if (opts.description) profile.description = opts.description;
88
+ if (Object.keys(profile).length) {
89
+ await fetch(`${apiUrl}/v1/agents/me`, {
90
+ method: "PUT",
91
+ headers: { Authorization: `Bearer ${token}`, "Content-Type": "application/json" },
92
+ body: JSON.stringify(profile)
93
+ }).catch(() => {
94
+ });
95
+ }
82
96
  console.log(`
83
97
  Registered agent "${opts.name}" (${result.agentId})`);
84
98
  console.log(`Capabilities: ${result.capabilityGrants.map((g) => g.capability).join(", ")}`);
@@ -145,11 +159,12 @@ Capabilities updated:`);
145
159
  print(await api("PUT", "/v1/agents/me", body));
146
160
  })
147
161
  );
148
- program2.command("discover").description("Discover matching agents and listings").option("--wants <items>", "Filter by wants").option("--offers <items>", "Filter by offers").action(
162
+ program2.command("discover").description("Discover listings (default) or agents").option("--wants <items>", "Filter by wants").option("--offers <items>", "Filter by offers").option("--agents", "Also include matching agents in results").action(
149
163
  (opts) => run(async () => {
150
164
  const params = new URLSearchParams();
151
165
  if (opts.wants) params.set("wants", opts.wants);
152
166
  if (opts.offers) params.set("offers", opts.offers);
167
+ if (opts.agents) params.set("agents", "true");
153
168
  const qs = params.toString();
154
169
  print(await api("GET", `/v1/deals/discover${qs ? `?${qs}` : ""}`));
155
170
  })
@@ -176,6 +191,30 @@ Capabilities updated:`);
176
191
  })
177
192
  );
178
193
  listing.command("mine").description("Show your listings").action(() => run(async () => print(await api("GET", "/v1/listings/mine"))));
194
+ listing.command("buy <id>").description("Buy a listing instantly (fixed-price, auto-pay)").action(
195
+ (id) => run(async () => {
196
+ const result = await api("POST", `/v1/listings/${id}/buy`);
197
+ if (result.payment?.method === "wallet" || !result.payment?.checkoutUrl) {
198
+ print(result);
199
+ return;
200
+ }
201
+ console.log(`Checkout URL: ${result.payment.checkoutUrl}`);
202
+ console.error(`Waiting for payment of $${result.payment.amount}... (Ctrl+C to cancel)`);
203
+ const deadline = Date.now() + CHECKOUT_TIMEOUT;
204
+ while (Date.now() < deadline) {
205
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL));
206
+ try {
207
+ const deal2 = await api("GET", `/v1/deals/${result.deal.id}`);
208
+ if (deal2.status !== "payment_pending") {
209
+ print(deal2);
210
+ return;
211
+ }
212
+ } catch {
213
+ }
214
+ }
215
+ die("Checkout timed out (5 minutes). Deal is still payment_pending.");
216
+ })
217
+ );
179
218
  const deal = program2.command("deal").description("Manage deals");
180
219
  deal.command("propose").description("Propose a deal to another agent").requiredOption("--to <agent-id>", "Counterparty agent ID").requiredOption("--give <item>", "What you give").option("--give-amount <n>", "Payment amount in USD").requiredOption("--take <item>", "What you want").option("--listing <id>", "Associated listing ID").action(
181
220
  (opts) => run(async () => {
@@ -205,7 +244,30 @@ Capabilities updated:`);
205
244
  print(await api("POST", `/v1/deals/${id}/respond`, body));
206
245
  })
207
246
  );
208
- deal.command("pay <id>").description("Pay for a deal").action((id) => run(async () => print(await api("POST", `/v1/deals/${id}/pay`))));
247
+ deal.command("pay <id>").description("Pay for a deal").action(
248
+ (id) => run(async () => {
249
+ const result = await api("POST", `/v1/deals/${id}/pay`);
250
+ if (result.method === "wallet" || !result.checkoutUrl) {
251
+ print(result);
252
+ return;
253
+ }
254
+ console.log(`Checkout URL: ${result.checkoutUrl}`);
255
+ console.error(`Wallet balance: $${result.walletBalance}. Waiting for payment of $${result.amount}... (Ctrl+C to cancel)`);
256
+ const deadline = Date.now() + CHECKOUT_TIMEOUT;
257
+ while (Date.now() < deadline) {
258
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL));
259
+ try {
260
+ const deal2 = await api("GET", `/v1/deals/${id}`);
261
+ if (deal2.status !== "payment_pending") {
262
+ print(deal2);
263
+ return;
264
+ }
265
+ } catch {
266
+ }
267
+ }
268
+ die("Checkout timed out (5 minutes). Deal is still payment_pending.");
269
+ })
270
+ );
209
271
  deal.command("confirm <id>").description("Confirm delivery \u2014 releases funds to seller").action(
210
272
  (id) => run(async () => print(await api("POST", `/v1/deals/${id}/confirm-delivery`)))
211
273
  );
@@ -251,9 +313,9 @@ Capabilities updated:`);
251
313
  `Waiting for deal ${dealId} to ${targetStatus ? `reach "${targetStatus}"` : `change from "${startStatus}"`}...`
252
314
  );
253
315
  const deadline = Date.now() + timeout;
254
- const POLL_INTERVAL = 3e3;
316
+ const POLL_INTERVAL2 = 3e3;
255
317
  while (Date.now() < deadline) {
256
- await new Promise((r) => setTimeout(r, POLL_INTERVAL));
318
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL2));
257
319
  const current = await api("GET", `/v1/deals/${dealId}`);
258
320
  if (targetStatus) {
259
321
  if (current.status === targetStatus) {
@@ -320,14 +382,27 @@ Capabilities updated:`);
320
382
  const wallet = program2.command("wallet").description("Wallet operations");
321
383
  wallet.command("balance").description("Check wallet balance").action(() => run(async () => print(await api("GET", "/v1/wallet"))));
322
384
  wallet.action(() => run(async () => print(await api("GET", "/v1/wallet"))));
323
- wallet.command("deposit").description("Deposit funds (returns Stripe Checkout URL)").requiredOption("--amount <n>", "Amount in USD").action(
385
+ wallet.command("deposit").description("Deposit funds (prints Stripe checkout URL and waits for completion)").requiredOption("--amount <n>", "Amount in USD").action(
324
386
  (opts) => run(async () => {
325
387
  const result = await api("POST", "/v1/wallet/deposit", {
326
388
  amount: parseFloat(opts.amount)
327
389
  });
328
390
  console.log(`Checkout URL: ${result.checkoutUrl}`);
329
- console.log(`Amount: $${result.amount}`);
330
- console.log("Open the URL in a browser to complete the deposit.");
391
+ console.error(`Waiting for deposit of $${result.amount}... (Ctrl+C to cancel)`);
392
+ const deadline = Date.now() + CHECKOUT_TIMEOUT;
393
+ while (Date.now() < deadline) {
394
+ await new Promise((r) => setTimeout(r, POLL_INTERVAL));
395
+ try {
396
+ const status = await api("GET", `/v1/wallet/checkout-status/${result.sessionId}`);
397
+ if (status.paymentStatus === "paid") {
398
+ print(await api("GET", "/v1/wallet"));
399
+ return;
400
+ }
401
+ if (status.status === "expired") die("Checkout session expired.");
402
+ } catch {
403
+ }
404
+ }
405
+ die("Deposit timed out (5 minutes).");
331
406
  })
332
407
  );
333
408
  wallet.command("transactions").description("Transaction history").action(
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-market",
3
- "version": "0.1.9",
3
+ "version": "0.1.11",
4
4
  "description": "AI Market — Agent Trading Platform CLI",
5
5
  "type": "module",
6
6
  "bin": {