@pollinations/cli 0.1.18 → 0.1.19

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/index.js CHANGED
@@ -159,10 +159,18 @@ var printError = (msg) => {
159
159
  process.stderr.write(`${chalk.red.bold("error:")} ${chalk.red(msg)}
160
160
  `);
161
161
  };
162
+ var ExitSignal = class extends Error {
163
+ constructor(code) {
164
+ super(`exit ${code}`);
165
+ this.code = code;
166
+ }
167
+ code;
168
+ };
162
169
  var fail = (message, error) => {
170
+ if (error instanceof ExitSignal) throw error;
163
171
  const detail = error === void 0 ? "" : `: ${error instanceof Error ? error.message : "unknown"}`;
164
172
  printError(`${message}${detail}`);
165
- process.exit(1);
173
+ throw new ExitSignal(1);
166
174
  };
167
175
 
168
176
  // src/lib/api.ts
@@ -224,13 +232,13 @@ function readConfig(path) {
224
232
  printError(
225
233
  `Failed to read agent config: ${error instanceof Error ? error.message : "unknown"}`
226
234
  );
227
- process.exit(1);
235
+ throw new ExitSignal(1);
228
236
  }
229
237
  }
230
238
  function agentBody(configPath3, opts) {
231
239
  if (opts.visibility !== void 0 && opts.visibility !== "private" && opts.visibility !== "public") {
232
240
  printError("--visibility must be 'private' or 'public'");
233
- process.exit(1);
241
+ throw new ExitSignal(1);
234
242
  }
235
243
  return {
236
244
  ...configPath3 && readConfig(configPath3),
@@ -279,7 +287,7 @@ var list = new Command("list").description("List agents owned by your account").
279
287
  printError(
280
288
  `Failed to list agents: ${error instanceof Error ? error.message : "unknown"}`
281
289
  );
282
- process.exit(1);
290
+ throw new ExitSignal(1);
283
291
  }
284
292
  });
285
293
  var get = new Command("get").description("Get an agent owned by your account").argument("<id>", "Agent id").action(async (id) => {
@@ -295,7 +303,7 @@ var get = new Command("get").description("Get an agent owned by your account").a
295
303
  printError(
296
304
  `Failed to get agent: ${error instanceof Error ? error.message : "unknown"}`
297
305
  );
298
- process.exit(1);
306
+ throw new ExitSignal(1);
299
307
  }
300
308
  });
301
309
  var create = new Command("create").description("Create a managed agent").requiredOption(
@@ -306,11 +314,12 @@ var create = new Command("create").description("Create a managed agent").require
306
314
  "Agent visibility: private (default) or public"
307
315
  ).action(async (opts) => {
308
316
  const key = requireKey();
317
+ const body = agentBody(opts.config, opts);
309
318
  try {
310
319
  const agent = await gen("/account/agents", {
311
320
  apiKey: key,
312
321
  method: "POST",
313
- body: agentBody(opts.config, opts)
322
+ body
314
323
  });
315
324
  if (getOutputMode() === "json") printResult(agent);
316
325
  else {
@@ -321,7 +330,7 @@ var create = new Command("create").description("Create a managed agent").require
321
330
  printError(
322
331
  `Failed to create agent: ${error instanceof Error ? error.message : "unknown"}`
323
332
  );
324
- process.exit(1);
333
+ throw new ExitSignal(1);
325
334
  }
326
335
  });
327
336
  var update = new Command("update").description("Update an agent; fields you leave out keep their values").argument("<id>", "Agent id").option(
@@ -329,13 +338,14 @@ var update = new Command("update").description("Update an agent; fields you leav
329
338
  'JSON with only the fields to change, e.g. {"systemPrompt": "..."}'
330
339
  ).option("--name <name>", "Callable model name").option("--title <title>", "Display title shown in the catalog").option("--description <text>", "Agent description; empty clears it").option("--visibility <visibility>", "Agent visibility: private or public").action(async (id, opts) => {
331
340
  const key = requireKey();
341
+ const body = agentBody(opts.config, opts);
332
342
  try {
333
343
  const agent = await gen(
334
344
  `/account/agents/${encodeURIComponent(id)}`,
335
345
  {
336
346
  apiKey: key,
337
347
  method: "PATCH",
338
- body: agentBody(opts.config, opts)
348
+ body
339
349
  }
340
350
  );
341
351
  if (getOutputMode() === "json") printResult(agent);
@@ -347,7 +357,7 @@ var update = new Command("update").description("Update an agent; fields you leav
347
357
  printError(
348
358
  `Failed to update agent: ${error instanceof Error ? error.message : "unknown"}`
349
359
  );
350
- process.exit(1);
360
+ throw new ExitSignal(1);
351
361
  }
352
362
  });
353
363
  var remove = new Command("delete").description("Delete an agent and its model registration").argument("<id>", "Agent id").action(async (id) => {
@@ -363,7 +373,7 @@ var remove = new Command("delete").description("Delete an agent and its model re
363
373
  printError(
364
374
  `Failed to delete agent: ${error instanceof Error ? error.message : "unknown"}`
365
375
  );
366
- process.exit(1);
376
+ throw new ExitSignal(1);
367
377
  }
368
378
  });
369
379
  var sync = new Command("sync").description("Deploy the latest revision of a code agent").argument("<id>", "Agent id").action(async (id) => {
@@ -381,7 +391,7 @@ var sync = new Command("sync").description("Deploy the latest revision of a code
381
391
  printError(
382
392
  `Failed to sync agent: ${error instanceof Error ? error.message : "unknown"}`
383
393
  );
384
- process.exit(1);
394
+ throw new ExitSignal(1);
385
395
  }
386
396
  });
387
397
  var agentsCommand = new Command("agents").description("Manage agents").addCommand(list).addCommand(get).addCommand(create).addCommand(update).addCommand(sync).addCommand(remove);
@@ -426,7 +436,7 @@ function assertApiKey(key) {
426
436
  const trimmed = key.trim();
427
437
  if (!trimmed.startsWith("pk_") && !trimmed.startsWith("sk_")) {
428
438
  printError("Invalid key format. Must start with pk_ or sk_");
429
- process.exit(1);
439
+ throw new ExitSignal(1);
430
440
  }
431
441
  return trimmed;
432
442
  }
@@ -435,7 +445,7 @@ async function readStdinToken() {
435
445
  printError(
436
446
  `--with-token expects a key on stdin, e.g. printf '%s' "$KEY" | polli auth login --with-token`
437
447
  );
438
- process.exit(1);
448
+ throw new ExitSignal(1);
439
449
  }
440
450
  let text = "";
441
451
  for await (const chunk of process.stdin) {
@@ -478,13 +488,13 @@ async function loginWithDeviceFlow(opts = {}) {
478
488
  "Fallback: printf '%s' '<your-key>' | polli auth login --with-token"
479
489
  );
480
490
  printInfo("Get your key at: https://enter.pollinations.ai/keys");
481
- process.exit(1);
491
+ throw new ExitSignal(1);
482
492
  });
483
493
  if (!res.ok) {
484
494
  printError(
485
495
  `Failed to start device flow: ${res.status} ${await res.text()}`
486
496
  );
487
- process.exit(1);
497
+ throw new ExitSignal(1);
488
498
  }
489
499
  const deviceResp = await res.json();
490
500
  printInfo(`
@@ -518,7 +528,7 @@ Your code: ${deviceResp.user_code}
518
528
  errMsg = `Login failed: ${tokenResp.error_description ?? tokenResp.error}`;
519
529
  }
520
530
  printError(errMsg);
521
- process.exit(1);
531
+ throw new ExitSignal(1);
522
532
  }
523
533
  const key = tokenResp.access_token;
524
534
  storeKey(key);
@@ -632,7 +642,7 @@ var docsCommand = new Command3("docs").description("Show Pollinations API docume
632
642
  printInfo(
633
643
  `Available endpoints can be found with: polli docs`
634
644
  );
635
- process.exit(1);
645
+ throw new ExitSignal(1);
636
646
  }
637
647
  content = matches.join("\n");
638
648
  }
@@ -677,7 +687,7 @@ var earningsCommand = new Command4("earnings").description("Show developer earni
677
687
  printError(
678
688
  err instanceof Error ? err.message : "Invalid --days value"
679
689
  );
680
- process.exit(1);
690
+ throw new ExitSignal(1);
681
691
  }
682
692
  try {
683
693
  const data = await gen(
@@ -760,7 +770,7 @@ async function fetchGen(path, init = {}) {
760
770
  }
761
771
  function exitWithError(error) {
762
772
  printError(error instanceof Error ? error.message : "unknown error");
763
- process.exit(1);
773
+ throw new ExitSignal(1);
764
774
  }
765
775
 
766
776
  // src/lib/play.ts
@@ -851,7 +861,7 @@ Examples:
851
861
  printError(
852
862
  "No text provided. Pass as argument or pipe via stdin."
853
863
  );
854
- process.exit(1);
864
+ throw new ExitSignal(1);
855
865
  }
856
866
  const params = new URLSearchParams({ voice: opts.voice });
857
867
  if (opts.format !== "mp3")
@@ -999,7 +1009,8 @@ Chat session started (model: ${opts.model})
999
1009
  if (hint) {
1000
1010
  printError(hint);
1001
1011
  rl.close();
1002
- process.exit(1);
1012
+ process.exitCode = 1;
1013
+ return;
1003
1014
  }
1004
1015
  throw new Error(`${res.status}: ${errText}`);
1005
1016
  }
@@ -1060,7 +1071,7 @@ Session ended. ${totalTokens} tokens used.
1060
1071
  );
1061
1072
  }
1062
1073
  rl.close();
1063
- process.exit(0);
1074
+ process.exitCode = 0;
1064
1075
  }
1065
1076
  if (input === "/clear") {
1066
1077
  messages.length = 0;
@@ -1087,7 +1098,7 @@ Session ended. ${totalTokens} tokens used.
1087
1098
  });
1088
1099
  rl.on("close", () => {
1089
1100
  if (opts.save) saveTranscript(opts.save);
1090
- process.exit(0);
1101
+ process.exitCode = 0;
1091
1102
  });
1092
1103
  });
1093
1104
  }
@@ -1117,7 +1128,7 @@ function createImageCommand() {
1117
1128
  printError(
1118
1129
  `--image requires a public http(s) URL, not a local path: ${bad}`
1119
1130
  );
1120
- process.exit(1);
1131
+ throw new ExitSignal(1);
1121
1132
  }
1122
1133
  params.set("image", opts.image.join("|"));
1123
1134
  }
@@ -1162,7 +1173,7 @@ function createTextCommand() {
1162
1173
  printError(
1163
1174
  "No prompt provided. Pass as argument or pipe via stdin."
1164
1175
  );
1165
- process.exit(1);
1176
+ throw new ExitSignal(1);
1166
1177
  }
1167
1178
  if (promptArg && stdinText) {
1168
1179
  opts.system = opts.system ? `${opts.system}
@@ -1357,7 +1368,7 @@ function createVideoCommand() {
1357
1368
  printError(
1358
1369
  `--image requires a public http(s) URL, not a local path: ${opts.image}`
1359
1370
  );
1360
- process.exit(1);
1371
+ throw new ExitSignal(1);
1361
1372
  }
1362
1373
  params.set("image", opts.image);
1363
1374
  }
@@ -4489,13 +4500,13 @@ var usageCommand = new Command20("usage").description(
4489
4500
  printError(
4490
4501
  "--key, --model, --days and --csv require --history or --daily"
4491
4502
  );
4492
- process.exit(1);
4503
+ throw new ExitSignal(1);
4493
4504
  }
4494
4505
  if (opts.daily && opts.csv && models2.length > 0) {
4495
4506
  printError(
4496
4507
  "--model can't filter the raw --daily --csv export (the endpoint has no models param)"
4497
4508
  );
4498
- process.exit(1);
4509
+ throw new ExitSignal(1);
4499
4510
  }
4500
4511
  if (!opts.history && !opts.daily) {
4501
4512
  try {
@@ -4516,7 +4527,7 @@ var usageCommand = new Command20("usage").description(
4516
4527
  printError(
4517
4528
  `Failed to fetch balance: ${err instanceof Error ? err.message : "unknown"}`
4518
4529
  );
4519
- process.exit(1);
4530
+ throw new ExitSignal(1);
4520
4531
  }
4521
4532
  }
4522
4533
  let days;
@@ -4527,7 +4538,7 @@ var usageCommand = new Command20("usage").description(
4527
4538
  printError(
4528
4539
  err instanceof Error ? err.message : "Invalid --days value"
4529
4540
  );
4530
- process.exit(1);
4541
+ throw new ExitSignal(1);
4531
4542
  }
4532
4543
  }
4533
4544
  let keyIds = [];
@@ -4546,7 +4557,7 @@ var usageCommand = new Command20("usage").description(
4546
4557
  printError(
4547
4558
  err instanceof Error ? err.message : "Failed to resolve keys"
4548
4559
  );
4549
- process.exit(1);
4560
+ throw new ExitSignal(1);
4550
4561
  }
4551
4562
  }
4552
4563
  }
@@ -4582,7 +4593,7 @@ var usageCommand = new Command20("usage").description(
4582
4593
  const limit = Number(opts.limit);
4583
4594
  if (!Number.isInteger(limit) || limit < 1) {
4584
4595
  printError("--limit must be a positive integer");
4585
- process.exit(1);
4596
+ throw new ExitSignal(1);
4586
4597
  }
4587
4598
  params.set("limit", String(limit));
4588
4599
  if (keyIds.length > 0) params.set("api_key_ids", keyIds.join(","));
@@ -4610,7 +4621,7 @@ var usageCommand = new Command20("usage").description(
4610
4621
  printError(
4611
4622
  `Failed to fetch usage: ${err instanceof Error ? err.message : "unknown"}`
4612
4623
  );
4613
- process.exit(1);
4624
+ throw new ExitSignal(1);
4614
4625
  }
4615
4626
  });
4616
4627
 
@@ -4674,8 +4685,12 @@ if (process.argv.length <= 2) {
4674
4685
  program.help();
4675
4686
  }
4676
4687
  program.parseAsync(process.argv).catch((err) => {
4688
+ if (err instanceof ExitSignal) {
4689
+ process.exitCode = err.code;
4690
+ return;
4691
+ }
4677
4692
  process.stderr.write(`${err instanceof Error ? err.message : err}
4678
4693
  `);
4679
- process.exit(1);
4694
+ process.exitCode = 1;
4680
4695
  });
4681
4696
  //# sourceMappingURL=index.js.map