@dudko.dev/agent-web 0.0.42 → 0.0.43

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
@@ -4162,6 +4162,7 @@ var SPAWN_INPUT_SCHEMA = {
4162
4162
  description: "Optional: the names of YOUR tools the helper may use. Only tools you have can be named. Omit it for the default set; pass [] for a helper that only reasons."
4163
4163
  }
4164
4164
  },
4165
+ // A mutable array: JSON Schema 7's `required` is `string[]`, and `as const` would make it readonly.
4165
4166
  required: ["task"],
4166
4167
  additionalProperties: false
4167
4168
  };
@@ -4581,8 +4582,49 @@ var resolveResume = async (persistence, resumeFromRunId) => {
4581
4582
  `Run "${resumeFromRunId}" has no steps left to run (status="${snapshot.status}"); nothing to resume`
4582
4583
  );
4583
4584
  }
4585
+ if (snapshot.attempt !== void 0 && !(Number.isSafeInteger(snapshot.attempt) && snapshot.attempt >= 1)) {
4586
+ throw new Error(
4587
+ `Run "${resumeFromRunId}" has an invalid attempt (${JSON.stringify(snapshot.attempt)}); it must be a positive integer`
4588
+ );
4589
+ }
4584
4590
  return snapshot;
4585
4591
  };
4592
+ var resolveRunId = async (persistence, runId, resumeFromRunId) => {
4593
+ if (runId === void 0) return void 0;
4594
+ if (resumeFromRunId !== void 0) {
4595
+ throw new Error(
4596
+ "runId names a fresh run and cannot be combined with resumeFromRunId: a resume keeps the id of the run it resumes"
4597
+ );
4598
+ }
4599
+ assertSafeRunId(runId, "runId");
4600
+ const snapshot = await persistence?.loadRun?.(runId);
4601
+ if (snapshot) {
4602
+ throw new Error(
4603
+ `Run "${runId}" already exists (status "${snapshot.status}"); resume it with resumeFromRunId instead of starting it again`
4604
+ );
4605
+ }
4606
+ return runId;
4607
+ };
4608
+ var resumeAttemptOf = (snapshot) => (snapshot.attempt ?? 1) + 1;
4609
+ var recordResume = async (persistence, resumed, attempt, warn) => {
4610
+ const hook = persistence?.onRunResume ? "onRunResume" : persistence?.onStepComplete ? "onStepComplete" : void 0;
4611
+ if (!persistence || !hook) {
4612
+ warn(
4613
+ `persistence has neither onRunResume nor onStepComplete: attempt ${attempt} of run ${resumed.runId} is not on record until the run ends`
4614
+ );
4615
+ return;
4616
+ }
4617
+ const { error: _error, completedAt: _completedAt, text: _text, ...rest } = resumed;
4618
+ void [_error, _completedAt, _text];
4619
+ try {
4620
+ await persistence[hook]?.call(persistence, { ...rest, status: "executing", attempt });
4621
+ } catch (err) {
4622
+ throw new Error(
4623
+ `persistence.${hook} threw while recording attempt ${attempt} of run ${resumed.runId}; the resume was refused: ${err instanceof Error ? err.message : String(err)}`,
4624
+ { cause: err }
4625
+ );
4626
+ }
4627
+ };
4586
4628
 
4587
4629
  // src/agent/schemas.ts
4588
4630
  import { z as z5 } from "zod";
@@ -5650,16 +5692,20 @@ ${renderActiveSkills(active)}`
5650
5692
  spentBySession.set(sessionId, addUsage(spentBySession.get(sessionId) ?? emptyUsage(), u));
5651
5693
  };
5652
5694
  const runCore = async (goal, opts, track, port) => {
5695
+ const ownRunId = await resolveRunId(config.persistence, opts.runId, opts.resumeFromRunId);
5653
5696
  const resumed = await resolveResume(config.persistence, opts.resumeFromRunId);
5697
+ const attempt = resumed ? resumeAttemptOf(resumed) : 1;
5698
+ let seq3 = 0;
5654
5699
  const emit = (event) => {
5655
5700
  try {
5656
- opts.onEvent?.(event);
5701
+ opts.onEvent?.({ ...event, seq: ++seq3, attempt });
5657
5702
  } catch {
5658
5703
  }
5659
5704
  };
5705
+ if (resumed) await recordResume(config.persistence, resumed, attempt, (m) => log.warn(m));
5660
5706
  let text = (resumed?.goal ?? goal).trim();
5661
5707
  const sessionId = resumed?.sessionId ?? opts.sessionId ?? cfg.sessionId;
5662
- const runId = resumed?.runId ?? newRunId();
5708
+ const runId = resumed?.runId ?? ownRunId ?? newRunId();
5663
5709
  const startedAt = resumed?.startedAt ?? Date.now();
5664
5710
  let usage = resumed ? { ...resumed.usage } : emptyUsage();
5665
5711
  const result = {
@@ -6044,6 +6090,7 @@ ${renderActiveSkills(active)}`
6044
6090
  ...userUpdates.length ? { userUpdates: userUpdates.map((u) => ({ ...u })) } : {},
6045
6091
  ...escalationCount ? { escalations: escalationCount } : {},
6046
6092
  ...spawnRun && !seed && spawnRun.root.spawned ? { subagentsSpawned: spawnRun.root.spawned } : {},
6093
+ attempt,
6047
6094
  ...extra
6048
6095
  });
6049
6096
  const persist = async (hook, snapshot) => {
@@ -7376,7 +7423,100 @@ var preparePipeline = (definition) => {
7376
7423
  reaches: (from, to) => descendants(from).has(to)
7377
7424
  };
7378
7425
  };
7379
- var abortError2 = () => new DOMException("Aborted", "AbortError");
7426
+ var passReach = (plan) => {
7427
+ const entry = plan.definition.entry;
7428
+ const order = [entry];
7429
+ const preds = /* @__PURE__ */ new Map([[entry, []]]);
7430
+ for (let i = 0; i < order.length; i++) {
7431
+ for (const { edge } of plan.outgoing.get(order[i]) ?? []) {
7432
+ if (!preds.has(edge.to)) {
7433
+ preds.set(edge.to, []);
7434
+ order.push(edge.to);
7435
+ }
7436
+ ;
7437
+ preds.get(edge.to).push(order[i]);
7438
+ }
7439
+ }
7440
+ const dom = /* @__PURE__ */ new Map();
7441
+ for (const nodeId of order) dom.set(nodeId, new Set(nodeId === entry ? [entry] : order));
7442
+ for (let changed = true; changed; ) {
7443
+ changed = false;
7444
+ for (const nodeId of order) {
7445
+ if (nodeId === entry) continue;
7446
+ const [first, ...rest] = preds.get(nodeId).map((p) => dom.get(p));
7447
+ const next = /* @__PURE__ */ new Set([nodeId]);
7448
+ for (const d of first) if (rest.every((set) => set.has(d))) next.add(d);
7449
+ if (next.size < dom.get(nodeId).size) {
7450
+ dom.set(nodeId, next);
7451
+ changed = true;
7452
+ }
7453
+ }
7454
+ }
7455
+ const closure = /* @__PURE__ */ new Map();
7456
+ return (from, to) => {
7457
+ let seen = closure.get(from);
7458
+ if (!seen) {
7459
+ const found = /* @__PURE__ */ new Set();
7460
+ const queue = [from];
7461
+ while (queue.length) {
7462
+ const at = queue.pop();
7463
+ for (const { edge } of plan.outgoing.get(at) ?? []) {
7464
+ if (dom.get(at)?.has(edge.to) || found.has(edge.to)) continue;
7465
+ found.add(edge.to);
7466
+ queue.push(edge.to);
7467
+ }
7468
+ }
7469
+ closure.set(from, seen = found);
7470
+ }
7471
+ return seen.has(to);
7472
+ };
7473
+ };
7474
+ var abortError2 = () => {
7475
+ if (typeof DOMException === "function") return new DOMException("Aborted", "AbortError");
7476
+ const error = new Error("Aborted");
7477
+ error.name = "AbortError";
7478
+ return error;
7479
+ };
7480
+ var createStopper = () => {
7481
+ if (typeof AbortController === "function") {
7482
+ const controller = new AbortController();
7483
+ return { signal: controller.signal, abort: () => controller.abort() };
7484
+ }
7485
+ const listeners = [];
7486
+ const signal = {
7487
+ aborted: false,
7488
+ reason: void 0,
7489
+ onabort: null,
7490
+ addEventListener(type, listener) {
7491
+ if (type === "abort" && listener && !listeners.includes(listener)) listeners.push(listener);
7492
+ },
7493
+ removeEventListener(type, listener) {
7494
+ const index = listener ? listeners.indexOf(listener) : -1;
7495
+ if (type === "abort" && index >= 0) listeners.splice(index, 1);
7496
+ },
7497
+ throwIfAborted() {
7498
+ if (signal.aborted) throw signal.reason;
7499
+ }
7500
+ };
7501
+ const abort = () => {
7502
+ if (signal.aborted) return;
7503
+ signal.aborted = true;
7504
+ signal.reason = abortError2();
7505
+ const event = { type: "abort", target: signal };
7506
+ const call = (fn, thisArg) => {
7507
+ try {
7508
+ fn.call(thisArg, event);
7509
+ } catch {
7510
+ }
7511
+ };
7512
+ for (const listener of listeners.splice(0)) {
7513
+ if (typeof listener === "function") call(listener, signal);
7514
+ else call(listener.handleEvent, listener);
7515
+ }
7516
+ if (typeof signal.onabort === "function") call(signal.onabort, signal);
7517
+ };
7518
+ return { signal, abort };
7519
+ };
7380
7520
  var addUsage2 = (into, usage) => {
7381
7521
  if (!usage) return;
7382
7522
  const sum = into;
@@ -7384,9 +7524,10 @@ var addUsage2 = (into, usage) => {
7384
7524
  if (typeof value === "number" && Number.isFinite(value)) sum[key] = (sum[key] ?? 0) + value;
7385
7525
  }
7386
7526
  };
7387
- var runPipelineGraph = async (plan, runNode, options, onAbort) => {
7527
+ var runPipelineGraph = async (plan, runNode, options) => {
7388
7528
  const { definition } = plan;
7389
- const controller = new AbortController();
7529
+ const onAbort = options.onAbort ?? "reject";
7530
+ const stopper = createStopper();
7390
7531
  let halted;
7391
7532
  let halt = () => {
7392
7533
  };
@@ -7421,7 +7562,7 @@ ${t.text}`).join("\n\n");
7421
7562
  const onOuterAbort = () => {
7422
7563
  if (halted) return;
7423
7564
  halted = "aborted";
7424
- controller.abort();
7565
+ stopper.abort();
7425
7566
  halt(abortError2());
7426
7567
  };
7427
7568
  const visit = async (nodeId, activations) => {
@@ -7437,7 +7578,7 @@ ${t.text}`).join("\n\n");
7437
7578
  node,
7438
7579
  { request: options.input, from: activations.map((a) => ({ ...a.source })), visit: number },
7439
7580
  {
7440
- signal: controller.signal,
7581
+ signal: stopper.signal,
7441
7582
  pipeline: definition,
7442
7583
  emit: (event) => {
7443
7584
  if (!halted) emit({ ...event, nodeId, visit: number });
@@ -7454,7 +7595,7 @@ ${t.text}`).join("\n\n");
7454
7595
  visit: number,
7455
7596
  error: err instanceof Error ? err.message : String(err)
7456
7597
  });
7457
- controller.abort();
7598
+ stopper.abort();
7458
7599
  halt(failure);
7459
7600
  return;
7460
7601
  }
@@ -7510,7 +7651,8 @@ ${t.text}`).join("\n\n");
7510
7651
  if (record2.live === 0) terminals.push(record2);
7511
7652
  }
7512
7653
  };
7513
- const deferred = (nodeId, all) => (plan.nodes.get(nodeId)?.join ?? "all") === "all" && all.some((other) => other !== nodeId && plan.reaches(other, nodeId));
7654
+ const deferred = (nodeId, all, reaches = plan.reaches) => (plan.nodes.get(nodeId)?.join ?? "all") === "all" && all.some((other) => other !== nodeId && reaches(other, nodeId));
7655
+ let inPass;
7514
7656
  emit({ type: "pipeline.started", ...definition.name ? { pipeline: definition.name } : {} });
7515
7657
  options.signal?.addEventListener("abort", onOuterAbort, { once: true });
7516
7658
  try {
@@ -7518,6 +7660,10 @@ ${t.text}`).join("\n\n");
7518
7660
  while (pending.size > 0 && !halted) {
7519
7661
  const waiting = [...pending.keys()];
7520
7662
  let ready = waiting.filter((nodeId) => !deferred(nodeId, waiting));
7663
+ if (ready.length === 0) {
7664
+ const reaches = inPass ??= passReach(plan);
7665
+ ready = waiting.filter((nodeId) => !deferred(nodeId, waiting, reaches));
7666
+ }
7521
7667
  if (ready.length === 0) ready = waiting;
7522
7668
  const starting = [];
7523
7669
  for (const nodeId of ready) {
@@ -7605,6 +7751,21 @@ var pickRouteWithDecisionModel = async ({ agent, config }, node, text, signal, e
7605
7751
  ...outcome.accepted && outcome.choice !== void 0 ? { route: outcome.choice } : {}
7606
7752
  };
7607
7753
  };
7754
+ var pickPipelineRoute = async (node, text, options) => {
7755
+ if (!node.routes?.length) return { usage: normalizeUsage() };
7756
+ const entry = { agent: options.agent, config: options.config };
7757
+ const signal = options.signal ?? new AbortController().signal;
7758
+ const emit = (event) => {
7759
+ try {
7760
+ options.onEvent?.(event);
7761
+ } catch {
7762
+ }
7763
+ };
7764
+ const quick = await pickRouteWithDecisionModel(entry, node, text, signal, emit);
7765
+ if (quick?.route) return quick;
7766
+ const slow = await pickRoute(entry, node, text, signal);
7767
+ return quick ? { ...slow, usage: addUsage(quick.usage, slow.usage) } : slow;
7768
+ };
7608
7769
  var createPipeline = (definition, options) => {
7609
7770
  const plan = preparePipeline(definition);
7610
7771
  const { resolveNode } = options;
@@ -7639,11 +7800,11 @@ var createPipeline = (definition, options) => {
7639
7800
  if (result.stopped || ctx.signal.aborted) throw aborted();
7640
7801
  if (failure !== void 0) throw new Error(failure);
7641
7802
  if (!node.routes?.length) return { text: result.final, usage: result.usage };
7642
- const quick = await pickRouteWithDecisionModel(entry, node, result.final, ctx.signal, ctx.emit);
7643
- const picked = quick?.route ? quick : await pickRoute(entry, node, result.final, ctx.signal).then((slow) => ({
7644
- ...slow,
7645
- usage: quick ? addUsage(quick.usage, slow.usage) : slow.usage
7646
- }));
7803
+ const picked = await pickPipelineRoute(node, result.final, {
7804
+ ...entry,
7805
+ signal: ctx.signal,
7806
+ onEvent: ctx.emit
7807
+ });
7647
7808
  return {
7648
7809
  text: result.final,
7649
7810
  usage: addUsage(result.usage, picked.usage),
@@ -7652,12 +7813,12 @@ var createPipeline = (definition, options) => {
7652
7813
  };
7653
7814
  return {
7654
7815
  definition: plan.definition,
7655
- run: (run) => runPipelineGraph(
7656
- plan,
7657
- options.runNode ?? runWithAgent,
7658
- { input: run.input, signal: run.signal, onEvent: run.onEvent },
7659
- "resolve"
7660
- ),
7816
+ run: (run) => runPipelineGraph(plan, options.runNode ?? runWithAgent, {
7817
+ input: run.input,
7818
+ signal: run.signal,
7819
+ onEvent: run.onEvent,
7820
+ onAbort: "resolve"
7821
+ }),
7661
7822
  close: async () => {
7662
7823
  agents.clear();
7663
7824
  }
@@ -8282,6 +8443,10 @@ var MemoryRunStore = class {
8282
8443
  onStepComplete(snapshot) {
8283
8444
  this.runs.set(snapshot.runId, snapshot);
8284
8445
  }
8446
+ /** A resume started: its attempt is on record before any of its events. */
8447
+ onRunResume(snapshot) {
8448
+ this.runs.set(snapshot.runId, snapshot);
8449
+ }
8285
8450
  onRunComplete(snapshot) {
8286
8451
  if (snapshot.status === "complete" && !this.keepCompleted) this.runs.delete(snapshot.runId);
8287
8452
  else this.runs.set(snapshot.runId, snapshot);
@@ -8332,6 +8497,10 @@ var IndexedDBRunStore = class {
8332
8497
  onStepComplete(snapshot) {
8333
8498
  return this.put(snapshot);
8334
8499
  }
8500
+ /** A resume started: its attempt is on record before any of its events. */
8501
+ onRunResume(snapshot) {
8502
+ return this.put(snapshot);
8503
+ }
8335
8504
  async onRunComplete(snapshot) {
8336
8505
  if (snapshot.status === "complete" && !this.keepCompleted) {
8337
8506
  await this.delete(snapshot.runId);
@@ -8429,6 +8598,7 @@ export {
8429
8598
  MemorySkillStore,
8430
8599
  MemoryStore,
8431
8600
  PIPELINE_LIMITS,
8601
+ PIPELINE_ROUTER_SYSTEM,
8432
8602
  PipelineNodeError,
8433
8603
  PipelineValidationError,
8434
8604
  PlanSchema,
@@ -8547,6 +8717,7 @@ export {
8547
8717
  looksLikeJson,
8548
8718
  markCodeExecution,
8549
8719
  markReadOnly,
8720
+ matchPipelineRoute,
8550
8721
  matchRule,
8551
8722
  mcpAppOrigin,
8552
8723
  mcpSkillEntryRefusal,
@@ -8571,12 +8742,15 @@ export {
8571
8742
  parseReplannerResponse,
8572
8743
  parseSearchResponse,
8573
8744
  parseSkillMarkdown,
8745
+ pickPipelineRoute,
8574
8746
  pickTier,
8575
8747
  preloadWebLLMModel,
8748
+ preparePipeline,
8576
8749
  promptHintOf,
8577
8750
  renderActiveSkills,
8578
8751
  renderCatalog,
8579
8752
  renderPipelineNodeInput,
8753
+ renderPipelineRoutePrompt,
8580
8754
  renderSearchCatalog,
8581
8755
  renderSkillIndex,
8582
8756
  replanWanted,
@@ -8593,6 +8767,7 @@ export {
8593
8767
  resolveResume,
8594
8768
  resolveStage,
8595
8769
  resolveThinking,
8770
+ runPipelineGraph,
8596
8771
  runToolLoop,
8597
8772
  sanitizeSearchResults,
8598
8773
  schemaHintOf,