@autonoma-ai/planner 0.1.5 → 0.1.6

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
  }
@@ -6585,6 +6662,7 @@ function nextPendingStep(state) {
6585
6662
  }
6586
6663
 
6587
6664
  // src/index.ts
6665
+ process.setSourceMapsEnabled(true);
6588
6666
  var PAGES_FILE = "pages.json";
6589
6667
  async function savePages(outputDir, pages) {
6590
6668
  const obj = Object.fromEntries(pages);
@@ -6747,10 +6825,17 @@ async function runStep(step, outputDir, state, config, projectContext, nonIntera
6747
6825
  }
6748
6826
  } catch (err) {
6749
6827
  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.");
6828
+ const known = describeKnownError(err);
6829
+ if (known) {
6830
+ p9.log.error(`Failed: ${label} \u2014 ${known.title}`);
6831
+ p9.log.info(known.hint);
6832
+ } else {
6833
+ const message = err instanceof Error ? err.message : String(err);
6834
+ p9.log.error(`Failed: ${label} \u2014 ${message}`);
6835
+ console.error(`\x1B[2m${formatException(err)}\x1B[0m`);
6836
+ console.error(`\x1B[2m${supportReference({ step })}\x1B[0m`);
6837
+ p9.log.info("If you report this, please include the error output above.");
6838
+ }
6754
6839
  trackError(err, { step, source: "step_exception" });
6755
6840
  }
6756
6841
  track("cli_step_completed", {
@@ -7034,6 +7119,7 @@ Continue?`
7034
7119
  const message = err instanceof Error ? err.message : String(err);
7035
7120
  p9.log.error(`Failed to upload artifacts: ${message}`);
7036
7121
  console.error(`\x1B[2m${formatException(err)}\x1B[0m`);
7122
+ console.error(`\x1B[2m${supportReference({ phase: "artifact_upload" })}\x1B[0m`);
7037
7123
  p9.log.info(`Your artifacts are saved in ${outputDir}. Re-run the CLI to retry the upload.`);
7038
7124
  track("cli_artifacts_upload_failed", { message });
7039
7125
  trackError(err, { source: "artifact_upload" });
@@ -7042,7 +7128,14 @@ Continue?`
7042
7128
  p9.outro("Done");
7043
7129
  }
7044
7130
  main().then(() => flushAnalytics()).catch(async (err) => {
7045
- console.error(err);
7131
+ const known = describeKnownError(err);
7132
+ if (known) {
7133
+ console.error(`\x1B[31m${known.title}\x1B[0m`);
7134
+ console.error(known.hint);
7135
+ } else {
7136
+ console.error(err);
7137
+ console.error(`\x1B[2m${supportReference()}\x1B[0m`);
7138
+ }
7046
7139
  trackError(err, { source: "uncaught" }, false);
7047
7140
  await flushAnalytics();
7048
7141
  process.exit(1);