@autonoma-ai/planner 0.1.5 → 0.1.7

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
@@ -65,7 +65,7 @@ var init_context = __esm({
65
65
  import { createOpenRouter } from "@openrouter/ai-sdk-provider";
66
66
  function getProvider() {
67
67
  if (provider == null) {
68
- const apiKey = process.env.OPENROUTER_API_KEY;
68
+ const apiKey = process.env.OPENROUTER_API_KEY?.trim();
69
69
  if (!apiKey) throw new Error("OPENROUTER_API_KEY is required");
70
70
  provider = createOpenRouter({ apiKey });
71
71
  }
@@ -103,6 +103,9 @@ function getIdentity() {
103
103
  const id = process.env.AUTONOMA_DISTINCT_ID?.trim();
104
104
  return id && id.length > 0 ? id : void 0;
105
105
  }
106
+ function getRunId() {
107
+ return RUN_ID;
108
+ }
106
109
  function getDeviceId() {
107
110
  if (cachedDeviceId) return cachedDeviceId;
108
111
  try {
@@ -190,6 +193,60 @@ import { APICallError, RetryError, LoadAPIKeyError, InvalidPromptError, NoSuchMo
190
193
  function sleep(ms) {
191
194
  return new Promise((resolve5) => setTimeout(resolve5, ms));
192
195
  }
196
+ function chainMessages(err) {
197
+ const parts = [];
198
+ let cur = err;
199
+ for (let depth = 0; cur != null && depth < 10; depth++) {
200
+ if (cur instanceof Error) {
201
+ parts.push(cur.message);
202
+ cur = cur.cause;
203
+ } else {
204
+ parts.push(String(cur));
205
+ break;
206
+ }
207
+ }
208
+ return parts.join(" \u2190 ").toLowerCase();
209
+ }
210
+ function describeKnownError(err) {
211
+ const msg = chainMessages(err);
212
+ const status = APICallError.isInstance(err) ? err.statusCode : void 0;
213
+ const looksLikeAuth = msg.includes("missing authentication header") || msg.includes("no auth credentials") || msg.includes("openrouter_api_key") || msg.includes("user not found") || status === 401 || status === 403;
214
+ if (looksLikeAuth) {
215
+ return {
216
+ title: "OpenRouter rejected the request \u2014 your API key looks missing or invalid.",
217
+ hint: "Set a valid OPENROUTER_API_KEY (https://openrouter.ai/keys). If it's already set, the key may be revoked, empty, or have a stray space."
218
+ };
219
+ }
220
+ if (msg.includes("insufficient") || msg.includes("credits") || msg.includes("quota") || status === 402) {
221
+ return {
222
+ title: "OpenRouter ran out of credits for this account.",
223
+ hint: "Top up your balance at https://openrouter.ai/credits, then re-run."
224
+ };
225
+ }
226
+ if (NoSuchModelError.isInstance(err) || msg.includes("not a valid model") || msg.includes("no endpoints found") || msg.includes("model not found")) {
227
+ return {
228
+ title: "The requested model isn't available on OpenRouter.",
229
+ hint: "Check OPENROUTER_MODEL, or unset it to use the default."
230
+ };
231
+ }
232
+ if (msg.includes("rate limit") || msg.includes("too many requests") || status === 429) {
233
+ return {
234
+ title: "OpenRouter is rate-limiting this account.",
235
+ hint: "Wait a minute and re-run. If it persists, your key may be on a low-throughput tier."
236
+ };
237
+ }
238
+ return null;
239
+ }
240
+ function supportReference(extra = {}) {
241
+ const fields = {
242
+ ref: getRunId(),
243
+ version: process.env.npm_package_version,
244
+ node: process.version,
245
+ platform: `${process.platform}-${process.arch}`,
246
+ ...extra
247
+ };
248
+ return Object.entries(fields).filter(([, v]) => v != null && v !== "").map(([k, v]) => `${k}: ${v}`).join(" ");
249
+ }
193
250
  function formatException(err) {
194
251
  if (!(err instanceof Error)) return String(err);
195
252
  let out = err.stack ?? `${err.name}: ${err.message}`;
@@ -223,11 +280,22 @@ function classifyAgentError(err) {
223
280
  }
224
281
  return "transient";
225
282
  }
226
- var FATAL_STATUS_CODES, TRANSIENT_MESSAGE_PATTERNS;
283
+ var AgentError, FATAL_STATUS_CODES, TRANSIENT_MESSAGE_PATTERNS;
227
284
  var init_errors = __esm({
228
285
  "src/core/errors.ts"() {
229
286
  "use strict";
230
287
  init_esm_shims();
288
+ init_analytics();
289
+ AgentError = class extends Error {
290
+ constructor(message, phase, cause) {
291
+ super(message);
292
+ this.phase = phase;
293
+ this.cause = cause;
294
+ this.name = "AgentError";
295
+ }
296
+ phase;
297
+ cause;
298
+ };
231
299
  FATAL_STATUS_CODES = /* @__PURE__ */ new Set([400, 401, 403, 404, 422]);
232
300
  TRANSIENT_MESSAGE_PATTERNS = [
233
301
  "econnreset",
@@ -481,6 +549,15 @@ function buildStepHandler(config) {
481
549
  async function runAgent(config, prompt, extractResult) {
482
550
  const stepTimeout = config.stepTimeoutMs ?? STEP_TIMEOUT_MS;
483
551
  const modelsToTry = [config.model, ...FALLBACK_MODELS.map((id) => getModel(id))];
552
+ const modelIdOf = (m) => typeof m === "string" ? m : m.modelId;
553
+ const fail = (err, model) => {
554
+ const msg = err instanceof Error ? err.message : String(err);
555
+ throw new AgentError(
556
+ `agent "${config.id}" (model ${modelIdOf(model)}) failed: ${msg}`,
557
+ config.id,
558
+ err
559
+ );
560
+ };
484
561
  const YELLOW5 = "\x1B[33m";
485
562
  const RESET8 = "\x1B[0m";
486
563
  for (let modelIdx = 0; modelIdx < modelsToTry.length; modelIdx++) {
@@ -520,7 +597,7 @@ async function runAgent(config, prompt, extractResult) {
520
597
  return extractResult();
521
598
  } catch (err) {
522
599
  const errorClass = classifyAgentError(err);
523
- if (errorClass === "fatal") throw err;
600
+ if (errorClass === "fatal") fail(err, model);
524
601
  const msg = err instanceof Error ? err.message : String(err);
525
602
  if (errorClass === "timeout") {
526
603
  console.log(` ${YELLOW5}[${config.id}] step timed out after ${stepTimeout / 1e3}s${RESET8}`);
@@ -544,7 +621,7 @@ async function runAgent(config, prompt, extractResult) {
544
621
  );
545
622
  break;
546
623
  }
547
- throw err;
624
+ fail(err, model);
548
625
  }
549
626
  }
550
627
  }
@@ -6362,6 +6439,7 @@ var RESET = "\x1B[0m";
6362
6439
  var SHOW_CURSOR = "\x1B[?25h";
6363
6440
  var EXIT_HINT = `${DIM}(press Ctrl+C again to exit)${RESET}`;
6364
6441
  var ARM_WINDOW_MS = 3e3;
6442
+ var FORCE_EXIT_MS = 2500;
6365
6443
  var installed = false;
6366
6444
  var armed = false;
6367
6445
  var armTimer = null;
@@ -6372,11 +6450,19 @@ function disarm() {
6372
6450
  armTimer = null;
6373
6451
  armed = false;
6374
6452
  }
6453
+ function forceExit() {
6454
+ restoreTerminal();
6455
+ process.exit(130);
6456
+ }
6375
6457
  function handleInterrupt() {
6376
- if (quitting) return;
6458
+ if (quitting) {
6459
+ forceExit();
6460
+ return;
6461
+ }
6377
6462
  if (armed) {
6378
6463
  quitting = true;
6379
6464
  disarm();
6465
+ setTimeout(forceExit, FORCE_EXIT_MS).unref?.();
6380
6466
  onExit?.();
6381
6467
  return;
6382
6468
  }
@@ -6585,6 +6671,7 @@ function nextPendingStep(state) {
6585
6671
  }
6586
6672
 
6587
6673
  // src/index.ts
6674
+ process.setSourceMapsEnabled(true);
6588
6675
  var PAGES_FILE = "pages.json";
6589
6676
  async function savePages(outputDir, pages) {
6590
6677
  const obj = Object.fromEntries(pages);
@@ -6747,10 +6834,17 @@ async function runStep(step, outputDir, state, config, projectContext, nonIntera
6747
6834
  }
6748
6835
  } catch (err) {
6749
6836
  state = await markStep(outputDir, state, step, "failed");
6750
- const message = err instanceof Error ? err.message : String(err);
6751
- p9.log.error(`Failed: ${label} \u2014 ${message}`);
6752
- console.error(`\x1B[2m${formatException(err)}\x1B[0m`);
6753
- p9.log.info("If you report this, please include the error output above.");
6837
+ const known = describeKnownError(err);
6838
+ if (known) {
6839
+ p9.log.error(`Failed: ${label} \u2014 ${known.title}`);
6840
+ p9.log.info(known.hint);
6841
+ } else {
6842
+ const message = err instanceof Error ? err.message : String(err);
6843
+ p9.log.error(`Failed: ${label} \u2014 ${message}`);
6844
+ console.error(`\x1B[2m${formatException(err)}\x1B[0m`);
6845
+ console.error(`\x1B[2m${supportReference({ step })}\x1B[0m`);
6846
+ p9.log.info("If you report this, please include the error output above.");
6847
+ }
6754
6848
  trackError(err, { step, source: "step_exception" });
6755
6849
  }
6756
6850
  track("cli_step_completed", {
@@ -7034,6 +7128,7 @@ Continue?`
7034
7128
  const message = err instanceof Error ? err.message : String(err);
7035
7129
  p9.log.error(`Failed to upload artifacts: ${message}`);
7036
7130
  console.error(`\x1B[2m${formatException(err)}\x1B[0m`);
7131
+ console.error(`\x1B[2m${supportReference({ phase: "artifact_upload" })}\x1B[0m`);
7037
7132
  p9.log.info(`Your artifacts are saved in ${outputDir}. Re-run the CLI to retry the upload.`);
7038
7133
  track("cli_artifacts_upload_failed", { message });
7039
7134
  trackError(err, { source: "artifact_upload" });
@@ -7042,7 +7137,14 @@ Continue?`
7042
7137
  p9.outro("Done");
7043
7138
  }
7044
7139
  main().then(() => flushAnalytics()).catch(async (err) => {
7045
- console.error(err);
7140
+ const known = describeKnownError(err);
7141
+ if (known) {
7142
+ console.error(`\x1B[31m${known.title}\x1B[0m`);
7143
+ console.error(known.hint);
7144
+ } else {
7145
+ console.error(err);
7146
+ console.error(`\x1B[2m${supportReference()}\x1B[0m`);
7147
+ }
7046
7148
  trackError(err, { source: "uncaught" }, false);
7047
7149
  await flushAnalytics();
7048
7150
  process.exit(1);