ax 0.8.0 → 0.8.2

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
@@ -1,5 +1,5 @@
1
1
  // src/contract/version.ts
2
- var BUILT_AGAINST = "1.25.0";
2
+ var BUILT_AGAINST = "1.27.0";
3
3
 
4
4
  // src/contract/index.ts
5
5
  function minor(version) {
@@ -42,13 +42,22 @@ function watchdog(ms) {
42
42
  tripped: () => fired
43
43
  };
44
44
  }
45
- async function errorBodyText(res) {
45
+ async function errorBody(res) {
46
+ let payload = null;
46
47
  try {
47
- const payload = await res.json();
48
- return payload.message || payload.error || `HTTP ${res.status}`;
48
+ payload = await res.clone().json();
49
49
  } catch {
50
- return `HTTP ${res.status}`;
51
50
  }
51
+ res.body?.cancel().catch(() => {
52
+ });
53
+ return {
54
+ message: payload?.message || payload?.error || `HTTP ${res.status}`,
55
+ code: payload?.code ?? null,
56
+ payload
57
+ };
58
+ }
59
+ async function errorBodyText(res) {
60
+ return (await errorBody(res)).message;
52
61
  }
53
62
  var pause = (ms) => new Promise((r) => setTimeout(r, ms));
54
63
 
@@ -59,6 +68,14 @@ var POLL_EVERY_MS = 2e3;
59
68
  var POLL_LIMIT = 45;
60
69
  var BAR_CELLS = 14;
61
70
  var AuditApiError = class extends Error {
71
+ code;
72
+ payload;
73
+ constructor(message, options = {}) {
74
+ super(message);
75
+ this.name = "AuditApiError";
76
+ this.code = options.code ?? null;
77
+ this.payload = options.payload;
78
+ }
62
79
  };
63
80
  function authHeaders(apiKey) {
64
81
  return apiKey ? { authorization: `Bearer ${apiKey}` } : {};
@@ -106,7 +123,11 @@ async function consumeScanStream(base, target, options) {
106
123
  }
107
124
  if (!res.ok || !res.body) {
108
125
  dog.disarm();
109
- throw new AuditApiError(`ora audit failed: ${await errorBodyText(res)}`);
126
+ const body = await errorBody(res);
127
+ throw new AuditApiError(`ora audit failed: ${body.message}`, {
128
+ code: body.code,
129
+ payload: body.payload
130
+ });
110
131
  }
111
132
  const narrate = progressNarrator();
112
133
  const utf8 = new TextDecoder();
@@ -136,7 +157,8 @@ async function consumeScanStream(base, target, options) {
136
157
  if (line) options.progress?.(line);
137
158
  if (event.type === "error") {
138
159
  throw new AuditApiError(
139
- `ora audit failed: ${typeof event.message === "string" ? event.message : "scan error"}`
160
+ `ora audit failed: ${typeof event.message === "string" ? event.message : "scan error"}`,
161
+ { code: typeof event.code === "string" ? event.code : null, payload: event }
140
162
  );
141
163
  }
142
164
  if (event.type === "scan_complete" && event.result) {
@@ -243,8 +265,21 @@ var PUBLIC_BASE2 = "https://ora.ai";
243
265
  var STREAM_IDLE_MS2 = 12e4;
244
266
  var POLL_EVERY_MS2 = 5e3;
245
267
  var POLL_LIMIT2 = 180;
268
+ var FOLLOW_BUDGET_MS = 15 * 6e4;
269
+ var MAX_RETRY_AFTER_MS = 6e4;
270
+ var STREAM_REOPENS = 5;
271
+ var STREAM_REOPEN_PAUSE_MS = 2e3;
246
272
  var DeepJourneyApiError = class extends Error {
247
273
  };
274
+ var JourneyStreamHttpError = class extends DeepJourneyApiError {
275
+ constructor(message, status, retryAfterMs) {
276
+ super(message);
277
+ this.status = status;
278
+ this.retryAfterMs = retryAfterMs;
279
+ }
280
+ status;
281
+ retryAfterMs;
282
+ };
248
283
  function apiBase(baseUrl) {
249
284
  return (baseUrl ?? process.env.ORA_API_URL ?? PUBLIC_BASE2).replace(/\/+$/, "");
250
285
  }
@@ -314,7 +349,7 @@ async function triggerRun(base, target, options) {
314
349
  }
315
350
  if (res.status === 429) {
316
351
  const wait = res.headers.get("retry-after");
317
- const caps = options.apiKey ? "keyed allowance: 1000 runs per 24h per key" : "burst: 20/min/IP; daily: 200 runs per 24h per IP \u2014 an ora partner API key raises this to 1000/24h per key: set ORA_PARTNER_API_KEY or pass --api-key";
352
+ const caps = options.apiKey ? "keyed allowance: 1000 runs per 24h per key" : "burst: 20/min/IP; daily: 20 runs per 24h per IP \u2014 an ora partner API key raises this to 1000/24h per key: set ORA_PARTNER_API_KEY or pass --api-key";
318
353
  throw new DeepJourneyApiError(
319
354
  `ora journey caller limit exceeded${wait ? ` \u2014 retry after ${formatWait(Number(wait) * 1e3)}` : ""} (${caps})`
320
355
  );
@@ -360,7 +395,12 @@ async function followJourneyStream(base, streamPath, options) {
360
395
  let res;
361
396
  try {
362
397
  res = await fetch(target, {
363
- headers: { accept: "text/event-stream" },
398
+ headers: {
399
+ accept: "text/event-stream",
400
+ // A recognized key lifts the stream's per-IP limit on a run that
401
+ // already exists, which reopening after a drop relies on.
402
+ ...options.apiKey ? { authorization: `Bearer ${options.apiKey}` } : {}
403
+ },
364
404
  signal: dog.signal
365
405
  });
366
406
  } catch (cause) {
@@ -371,7 +411,12 @@ async function followJourneyStream(base, streamPath, options) {
371
411
  }
372
412
  if (!res.ok || !res.body) {
373
413
  dog.disarm();
374
- throw new DeepJourneyApiError(`ora journey stream failed: ${await errorBodyText(res)}`);
414
+ const retryAfterS = Number(res.headers.get("retry-after"));
415
+ throw new JourneyStreamHttpError(
416
+ `ora journey stream failed (${res.status}): ${await errorBodyText(res)}`,
417
+ res.status,
418
+ Number.isFinite(retryAfterS) && retryAfterS > 0 ? retryAfterS * 1e3 : void 0
419
+ );
375
420
  }
376
421
  const utf8 = new TextDecoder();
377
422
  const collected = {};
@@ -425,14 +470,38 @@ async function followJourneyStream(base, streamPath, options) {
425
470
  }
426
471
  return collected;
427
472
  }
473
+ async function streamToEnd(base, streamPath, options, deadline) {
474
+ const pauseMs = options.reopenPauseMs ?? STREAM_REOPEN_PAUSE_MS;
475
+ let lastFailure;
476
+ let waitMs = 0;
477
+ for (let attempt = 0; attempt <= STREAM_REOPENS && Date.now() < deadline; attempt++) {
478
+ if (attempt > 0) {
479
+ options.progress?.("stream dropped \u2014 reopening");
480
+ await pause(waitMs);
481
+ }
482
+ waitMs = pauseMs;
483
+ try {
484
+ const streamed = await followJourneyStream(base, streamPath, options);
485
+ if (streamed.result || streamed.errorMessage) return streamed.errorMessage;
486
+ lastFailure = new DeepJourneyApiError("ora journey stream closed before the run ended");
487
+ } catch (cause) {
488
+ lastFailure = cause;
489
+ if (cause instanceof JourneyStreamHttpError) {
490
+ if (cause.status !== 429) throw cause;
491
+ waitMs = Math.min(cause.retryAfterMs ?? pauseMs, MAX_RETRY_AFTER_MS);
492
+ }
493
+ }
494
+ }
495
+ throw lastFailure ?? new DeepJourneyApiError("ora journey stream was never opened");
496
+ }
428
497
  function fetchRunDetail(base, runId) {
429
498
  return getJson(`${base}/api/journey/runs/${encodeURIComponent(runId)}`);
430
499
  }
431
- async function awaitByPolling(base, runId, options) {
500
+ async function awaitByPolling(base, runId, options, deadline) {
432
501
  const every = options.pollEveryMs ?? POLL_EVERY_MS2;
433
502
  const limit = options.pollLimit ?? POLL_LIMIT2;
434
503
  let latest = await fetchRunDetail(base, runId);
435
- for (let round = 0; round < limit && latest.status === "running"; round++) {
504
+ for (let round = 0; round < limit && latest.status === "running" && Date.now() < deadline; round++) {
436
505
  options.progress?.("agent working\u2026 (polling)");
437
506
  await pause(every);
438
507
  try {
@@ -454,16 +523,18 @@ async function performDeepJourney(target, options) {
454
523
  options.progress?.("target at its 24h cap \u2014 fetching the most recent stored run");
455
524
  }
456
525
  let engineError;
457
- if (!options.noStream && record.status === "running") {
526
+ let streamFailure;
527
+ if (record.status === "running") {
528
+ const deadline = Date.now() + (options.followBudgetMs ?? FOLLOW_BUDGET_MS);
529
+ const streamOptions = options.noStream ? { ...options, onTrajectory: void 0 } : options;
458
530
  try {
459
- const streamed = await followJourneyStream(base, record.stream_url, options);
460
- engineError = streamed.errorMessage;
461
- } catch {
462
- options.progress?.("stream went quiet \u2014 switching to polling");
463
- await awaitByPolling(base, record.id, options);
531
+ engineError = await streamToEnd(base, record.stream_url, streamOptions, deadline);
532
+ } catch (cause) {
533
+ if (cause instanceof JourneyStreamHttpError && cause.status !== 429) throw cause;
534
+ streamFailure = cause instanceof Error ? cause.message : String(cause);
535
+ options.progress?.(`stream unavailable (${streamFailure}) \u2014 polling`);
536
+ await awaitByPolling(base, record.id, options, deadline);
464
537
  }
465
- } else if (options.noStream && record.status === "running") {
466
- await awaitByPolling(base, record.id, options);
467
538
  }
468
539
  const detail = await awaitDetailSettled(base, record.id, options);
469
540
  if (detail.status === "running") {
@@ -471,7 +542,7 @@ async function performDeepJourney(target, options) {
471
542
  throw new DeepJourneyApiError(`ora journey run failed: ${engineError}`);
472
543
  }
473
544
  throw new DeepJourneyApiError(
474
- `run ${record.id} is still executing \u2014 the ${options.noStream ? "poll budget ran out" : "stream ended"} before a terminal result. Check ${base}/api/journey/runs/${record.id} shortly`
545
+ `run ${record.id} is still executing \u2014 ${streamFailure ? `the stream could not be followed (${streamFailure}) and polling ran out of time` : "the stream ended"} before a terminal result. Check ${base}/api/journey/runs/${record.id} shortly`
475
546
  );
476
547
  }
477
548
  warnOnNewerContract(detail.contractVersion);