@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.cjs CHANGED
@@ -3140,6 +3140,7 @@ __export(src_exports, {
3140
3140
  MemorySkillStore: () => MemorySkillStore,
3141
3141
  MemoryStore: () => MemoryStore,
3142
3142
  PIPELINE_LIMITS: () => PIPELINE_LIMITS,
3143
+ PIPELINE_ROUTER_SYSTEM: () => PIPELINE_ROUTER_SYSTEM,
3143
3144
  PipelineNodeError: () => PipelineNodeError,
3144
3145
  PipelineValidationError: () => PipelineValidationError,
3145
3146
  PlanSchema: () => PlanSchema,
@@ -3258,6 +3259,7 @@ __export(src_exports, {
3258
3259
  looksLikeJson: () => looksLikeJson,
3259
3260
  markCodeExecution: () => markCodeExecution,
3260
3261
  markReadOnly: () => markReadOnly,
3262
+ matchPipelineRoute: () => matchPipelineRoute,
3261
3263
  matchRule: () => matchRule,
3262
3264
  mcpAppOrigin: () => mcpAppOrigin,
3263
3265
  mcpSkillEntryRefusal: () => mcpSkillEntryRefusal,
@@ -3282,12 +3284,15 @@ __export(src_exports, {
3282
3284
  parseReplannerResponse: () => parseReplannerResponse,
3283
3285
  parseSearchResponse: () => parseSearchResponse,
3284
3286
  parseSkillMarkdown: () => parseSkillMarkdown,
3287
+ pickPipelineRoute: () => pickPipelineRoute,
3285
3288
  pickTier: () => pickTier,
3286
3289
  preloadWebLLMModel: () => preloadWebLLMModel,
3290
+ preparePipeline: () => preparePipeline,
3287
3291
  promptHintOf: () => promptHintOf,
3288
3292
  renderActiveSkills: () => renderActiveSkills,
3289
3293
  renderCatalog: () => renderCatalog,
3290
3294
  renderPipelineNodeInput: () => renderPipelineNodeInput,
3295
+ renderPipelineRoutePrompt: () => renderPipelineRoutePrompt,
3291
3296
  renderSearchCatalog: () => renderSearchCatalog,
3292
3297
  renderSkillIndex: () => renderSkillIndex,
3293
3298
  replanWanted: () => replanWanted,
@@ -3304,6 +3309,7 @@ __export(src_exports, {
3304
3309
  resolveResume: () => resolveResume,
3305
3310
  resolveStage: () => resolveStage,
3306
3311
  resolveThinking: () => resolveThinking,
3312
+ runPipelineGraph: () => runPipelineGraph,
3307
3313
  runToolLoop: () => runToolLoop,
3308
3314
  sanitizeSearchResults: () => sanitizeSearchResults,
3309
3315
  schemaHintOf: () => schemaHintOf,
@@ -7388,6 +7394,7 @@ var SPAWN_INPUT_SCHEMA = {
7388
7394
  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."
7389
7395
  }
7390
7396
  },
7397
+ // A mutable array: JSON Schema 7's `required` is `string[]`, and `as const` would make it readonly.
7391
7398
  required: ["task"],
7392
7399
  additionalProperties: false
7393
7400
  };
@@ -7807,8 +7814,49 @@ var resolveResume = async (persistence, resumeFromRunId) => {
7807
7814
  `Run "${resumeFromRunId}" has no steps left to run (status="${snapshot.status}"); nothing to resume`
7808
7815
  );
7809
7816
  }
7817
+ if (snapshot.attempt !== void 0 && !(Number.isSafeInteger(snapshot.attempt) && snapshot.attempt >= 1)) {
7818
+ throw new Error(
7819
+ `Run "${resumeFromRunId}" has an invalid attempt (${JSON.stringify(snapshot.attempt)}); it must be a positive integer`
7820
+ );
7821
+ }
7810
7822
  return snapshot;
7811
7823
  };
7824
+ var resolveRunId = async (persistence, runId, resumeFromRunId) => {
7825
+ if (runId === void 0) return void 0;
7826
+ if (resumeFromRunId !== void 0) {
7827
+ throw new Error(
7828
+ "runId names a fresh run and cannot be combined with resumeFromRunId: a resume keeps the id of the run it resumes"
7829
+ );
7830
+ }
7831
+ assertSafeRunId(runId, "runId");
7832
+ const snapshot = await persistence?.loadRun?.(runId);
7833
+ if (snapshot) {
7834
+ throw new Error(
7835
+ `Run "${runId}" already exists (status "${snapshot.status}"); resume it with resumeFromRunId instead of starting it again`
7836
+ );
7837
+ }
7838
+ return runId;
7839
+ };
7840
+ var resumeAttemptOf = (snapshot) => (snapshot.attempt ?? 1) + 1;
7841
+ var recordResume = async (persistence, resumed, attempt, warn) => {
7842
+ const hook = persistence?.onRunResume ? "onRunResume" : persistence?.onStepComplete ? "onStepComplete" : void 0;
7843
+ if (!persistence || !hook) {
7844
+ warn(
7845
+ `persistence has neither onRunResume nor onStepComplete: attempt ${attempt} of run ${resumed.runId} is not on record until the run ends`
7846
+ );
7847
+ return;
7848
+ }
7849
+ const { error: _error, completedAt: _completedAt, text: _text, ...rest } = resumed;
7850
+ void [_error, _completedAt, _text];
7851
+ try {
7852
+ await persistence[hook]?.call(persistence, { ...rest, status: "executing", attempt });
7853
+ } catch (err) {
7854
+ throw new Error(
7855
+ `persistence.${hook} threw while recording attempt ${attempt} of run ${resumed.runId}; the resume was refused: ${err instanceof Error ? err.message : String(err)}`,
7856
+ { cause: err }
7857
+ );
7858
+ }
7859
+ };
7812
7860
 
7813
7861
  // src/agent/planner.ts
7814
7862
  init_context_window();
@@ -8983,16 +9031,20 @@ ${renderActiveSkills(active)}`
8983
9031
  spentBySession.set(sessionId, addUsage(spentBySession.get(sessionId) ?? emptyUsage(), u));
8984
9032
  };
8985
9033
  const runCore = async (goal, opts, track, port) => {
9034
+ const ownRunId = await resolveRunId(config.persistence, opts.runId, opts.resumeFromRunId);
8986
9035
  const resumed = await resolveResume(config.persistence, opts.resumeFromRunId);
9036
+ const attempt = resumed ? resumeAttemptOf(resumed) : 1;
9037
+ let seq4 = 0;
8987
9038
  const emit = (event) => {
8988
9039
  try {
8989
- opts.onEvent?.(event);
9040
+ opts.onEvent?.({ ...event, seq: ++seq4, attempt });
8990
9041
  } catch {
8991
9042
  }
8992
9043
  };
9044
+ if (resumed) await recordResume(config.persistence, resumed, attempt, (m) => log.warn(m));
8993
9045
  let text = (resumed?.goal ?? goal).trim();
8994
9046
  const sessionId = resumed?.sessionId ?? opts.sessionId ?? cfg.sessionId;
8995
- const runId = resumed?.runId ?? newRunId();
9047
+ const runId = resumed?.runId ?? ownRunId ?? newRunId();
8996
9048
  const startedAt = resumed?.startedAt ?? Date.now();
8997
9049
  let usage = resumed ? { ...resumed.usage } : emptyUsage();
8998
9050
  const result = {
@@ -9377,6 +9429,7 @@ ${renderActiveSkills(active)}`
9377
9429
  ...userUpdates.length ? { userUpdates: userUpdates.map((u) => ({ ...u })) } : {},
9378
9430
  ...escalationCount ? { escalations: escalationCount } : {},
9379
9431
  ...spawnRun && !seed && spawnRun.root.spawned ? { subagentsSpawned: spawnRun.root.spawned } : {},
9432
+ attempt,
9380
9433
  ...extra
9381
9434
  });
9382
9435
  const persist = async (hook, snapshot) => {
@@ -10714,7 +10767,100 @@ var preparePipeline = (definition) => {
10714
10767
  reaches: (from, to) => descendants(from).has(to)
10715
10768
  };
10716
10769
  };
10717
- var abortError2 = () => new DOMException("Aborted", "AbortError");
10770
+ var passReach = (plan) => {
10771
+ const entry = plan.definition.entry;
10772
+ const order = [entry];
10773
+ const preds = /* @__PURE__ */ new Map([[entry, []]]);
10774
+ for (let i = 0; i < order.length; i++) {
10775
+ for (const { edge } of plan.outgoing.get(order[i]) ?? []) {
10776
+ if (!preds.has(edge.to)) {
10777
+ preds.set(edge.to, []);
10778
+ order.push(edge.to);
10779
+ }
10780
+ ;
10781
+ preds.get(edge.to).push(order[i]);
10782
+ }
10783
+ }
10784
+ const dom = /* @__PURE__ */ new Map();
10785
+ for (const nodeId of order) dom.set(nodeId, new Set(nodeId === entry ? [entry] : order));
10786
+ for (let changed = true; changed; ) {
10787
+ changed = false;
10788
+ for (const nodeId of order) {
10789
+ if (nodeId === entry) continue;
10790
+ const [first, ...rest] = preds.get(nodeId).map((p) => dom.get(p));
10791
+ const next = /* @__PURE__ */ new Set([nodeId]);
10792
+ for (const d of first) if (rest.every((set) => set.has(d))) next.add(d);
10793
+ if (next.size < dom.get(nodeId).size) {
10794
+ dom.set(nodeId, next);
10795
+ changed = true;
10796
+ }
10797
+ }
10798
+ }
10799
+ const closure = /* @__PURE__ */ new Map();
10800
+ return (from, to) => {
10801
+ let seen = closure.get(from);
10802
+ if (!seen) {
10803
+ const found = /* @__PURE__ */ new Set();
10804
+ const queue = [from];
10805
+ while (queue.length) {
10806
+ const at = queue.pop();
10807
+ for (const { edge } of plan.outgoing.get(at) ?? []) {
10808
+ if (dom.get(at)?.has(edge.to) || found.has(edge.to)) continue;
10809
+ found.add(edge.to);
10810
+ queue.push(edge.to);
10811
+ }
10812
+ }
10813
+ closure.set(from, seen = found);
10814
+ }
10815
+ return seen.has(to);
10816
+ };
10817
+ };
10818
+ var abortError2 = () => {
10819
+ if (typeof DOMException === "function") return new DOMException("Aborted", "AbortError");
10820
+ const error = new Error("Aborted");
10821
+ error.name = "AbortError";
10822
+ return error;
10823
+ };
10824
+ var createStopper = () => {
10825
+ if (typeof AbortController === "function") {
10826
+ const controller = new AbortController();
10827
+ return { signal: controller.signal, abort: () => controller.abort() };
10828
+ }
10829
+ const listeners = [];
10830
+ const signal = {
10831
+ aborted: false,
10832
+ reason: void 0,
10833
+ onabort: null,
10834
+ addEventListener(type, listener) {
10835
+ if (type === "abort" && listener && !listeners.includes(listener)) listeners.push(listener);
10836
+ },
10837
+ removeEventListener(type, listener) {
10838
+ const index = listener ? listeners.indexOf(listener) : -1;
10839
+ if (type === "abort" && index >= 0) listeners.splice(index, 1);
10840
+ },
10841
+ throwIfAborted() {
10842
+ if (signal.aborted) throw signal.reason;
10843
+ }
10844
+ };
10845
+ const abort = () => {
10846
+ if (signal.aborted) return;
10847
+ signal.aborted = true;
10848
+ signal.reason = abortError2();
10849
+ const event = { type: "abort", target: signal };
10850
+ const call = (fn, thisArg) => {
10851
+ try {
10852
+ fn.call(thisArg, event);
10853
+ } catch {
10854
+ }
10855
+ };
10856
+ for (const listener of listeners.splice(0)) {
10857
+ if (typeof listener === "function") call(listener, signal);
10858
+ else call(listener.handleEvent, listener);
10859
+ }
10860
+ if (typeof signal.onabort === "function") call(signal.onabort, signal);
10861
+ };
10862
+ return { signal, abort };
10863
+ };
10718
10864
  var addUsage2 = (into, usage) => {
10719
10865
  if (!usage) return;
10720
10866
  const sum = into;
@@ -10722,9 +10868,10 @@ var addUsage2 = (into, usage) => {
10722
10868
  if (typeof value === "number" && Number.isFinite(value)) sum[key] = (sum[key] ?? 0) + value;
10723
10869
  }
10724
10870
  };
10725
- var runPipelineGraph = async (plan, runNode, options, onAbort) => {
10871
+ var runPipelineGraph = async (plan, runNode, options) => {
10726
10872
  const { definition } = plan;
10727
- const controller = new AbortController();
10873
+ const onAbort = options.onAbort ?? "reject";
10874
+ const stopper = createStopper();
10728
10875
  let halted;
10729
10876
  let halt = () => {
10730
10877
  };
@@ -10759,7 +10906,7 @@ ${t.text}`).join("\n\n");
10759
10906
  const onOuterAbort = () => {
10760
10907
  if (halted) return;
10761
10908
  halted = "aborted";
10762
- controller.abort();
10909
+ stopper.abort();
10763
10910
  halt(abortError2());
10764
10911
  };
10765
10912
  const visit = async (nodeId, activations) => {
@@ -10775,7 +10922,7 @@ ${t.text}`).join("\n\n");
10775
10922
  node,
10776
10923
  { request: options.input, from: activations.map((a) => ({ ...a.source })), visit: number },
10777
10924
  {
10778
- signal: controller.signal,
10925
+ signal: stopper.signal,
10779
10926
  pipeline: definition,
10780
10927
  emit: (event) => {
10781
10928
  if (!halted) emit({ ...event, nodeId, visit: number });
@@ -10792,7 +10939,7 @@ ${t.text}`).join("\n\n");
10792
10939
  visit: number,
10793
10940
  error: err instanceof Error ? err.message : String(err)
10794
10941
  });
10795
- controller.abort();
10942
+ stopper.abort();
10796
10943
  halt(failure);
10797
10944
  return;
10798
10945
  }
@@ -10848,7 +10995,8 @@ ${t.text}`).join("\n\n");
10848
10995
  if (record2.live === 0) terminals.push(record2);
10849
10996
  }
10850
10997
  };
10851
- const deferred = (nodeId, all) => (plan.nodes.get(nodeId)?.join ?? "all") === "all" && all.some((other) => other !== nodeId && plan.reaches(other, nodeId));
10998
+ const deferred = (nodeId, all, reaches = plan.reaches) => (plan.nodes.get(nodeId)?.join ?? "all") === "all" && all.some((other) => other !== nodeId && reaches(other, nodeId));
10999
+ let inPass;
10852
11000
  emit({ type: "pipeline.started", ...definition.name ? { pipeline: definition.name } : {} });
10853
11001
  options.signal?.addEventListener("abort", onOuterAbort, { once: true });
10854
11002
  try {
@@ -10856,6 +11004,10 @@ ${t.text}`).join("\n\n");
10856
11004
  while (pending.size > 0 && !halted) {
10857
11005
  const waiting = [...pending.keys()];
10858
11006
  let ready = waiting.filter((nodeId) => !deferred(nodeId, waiting));
11007
+ if (ready.length === 0) {
11008
+ const reaches = inPass ??= passReach(plan);
11009
+ ready = waiting.filter((nodeId) => !deferred(nodeId, waiting, reaches));
11010
+ }
10859
11011
  if (ready.length === 0) ready = waiting;
10860
11012
  const starting = [];
10861
11013
  for (const nodeId of ready) {
@@ -10943,6 +11095,21 @@ var pickRouteWithDecisionModel = async ({ agent, config }, node, text, signal, e
10943
11095
  ...outcome.accepted && outcome.choice !== void 0 ? { route: outcome.choice } : {}
10944
11096
  };
10945
11097
  };
11098
+ var pickPipelineRoute = async (node, text, options) => {
11099
+ if (!node.routes?.length) return { usage: normalizeUsage() };
11100
+ const entry = { agent: options.agent, config: options.config };
11101
+ const signal = options.signal ?? new AbortController().signal;
11102
+ const emit = (event) => {
11103
+ try {
11104
+ options.onEvent?.(event);
11105
+ } catch {
11106
+ }
11107
+ };
11108
+ const quick = await pickRouteWithDecisionModel(entry, node, text, signal, emit);
11109
+ if (quick?.route) return quick;
11110
+ const slow = await pickRoute(entry, node, text, signal);
11111
+ return quick ? { ...slow, usage: addUsage(quick.usage, slow.usage) } : slow;
11112
+ };
10946
11113
  var createPipeline = (definition, options) => {
10947
11114
  const plan = preparePipeline(definition);
10948
11115
  const { resolveNode } = options;
@@ -10977,11 +11144,11 @@ var createPipeline = (definition, options) => {
10977
11144
  if (result.stopped || ctx.signal.aborted) throw aborted();
10978
11145
  if (failure !== void 0) throw new Error(failure);
10979
11146
  if (!node.routes?.length) return { text: result.final, usage: result.usage };
10980
- const quick = await pickRouteWithDecisionModel(entry, node, result.final, ctx.signal, ctx.emit);
10981
- const picked = quick?.route ? quick : await pickRoute(entry, node, result.final, ctx.signal).then((slow) => ({
10982
- ...slow,
10983
- usage: quick ? addUsage(quick.usage, slow.usage) : slow.usage
10984
- }));
11147
+ const picked = await pickPipelineRoute(node, result.final, {
11148
+ ...entry,
11149
+ signal: ctx.signal,
11150
+ onEvent: ctx.emit
11151
+ });
10985
11152
  return {
10986
11153
  text: result.final,
10987
11154
  usage: addUsage(result.usage, picked.usage),
@@ -10990,12 +11157,12 @@ var createPipeline = (definition, options) => {
10990
11157
  };
10991
11158
  return {
10992
11159
  definition: plan.definition,
10993
- run: (run) => runPipelineGraph(
10994
- plan,
10995
- options.runNode ?? runWithAgent,
10996
- { input: run.input, signal: run.signal, onEvent: run.onEvent },
10997
- "resolve"
10998
- ),
11160
+ run: (run) => runPipelineGraph(plan, options.runNode ?? runWithAgent, {
11161
+ input: run.input,
11162
+ signal: run.signal,
11163
+ onEvent: run.onEvent,
11164
+ onAbort: "resolve"
11165
+ }),
10999
11166
  close: async () => {
11000
11167
  agents.clear();
11001
11168
  }
@@ -11735,6 +11902,10 @@ var MemoryRunStore = class {
11735
11902
  onStepComplete(snapshot) {
11736
11903
  this.runs.set(snapshot.runId, snapshot);
11737
11904
  }
11905
+ /** A resume started: its attempt is on record before any of its events. */
11906
+ onRunResume(snapshot) {
11907
+ this.runs.set(snapshot.runId, snapshot);
11908
+ }
11738
11909
  onRunComplete(snapshot) {
11739
11910
  if (snapshot.status === "complete" && !this.keepCompleted) this.runs.delete(snapshot.runId);
11740
11911
  else this.runs.set(snapshot.runId, snapshot);
@@ -11785,6 +11956,10 @@ var IndexedDBRunStore = class {
11785
11956
  onStepComplete(snapshot) {
11786
11957
  return this.put(snapshot);
11787
11958
  }
11959
+ /** A resume started: its attempt is on record before any of its events. */
11960
+ onRunResume(snapshot) {
11961
+ return this.put(snapshot);
11962
+ }
11788
11963
  async onRunComplete(snapshot) {
11789
11964
  if (snapshot.status === "complete" && !this.keepCompleted) {
11790
11965
  await this.delete(snapshot.runId);
@@ -11908,6 +12083,7 @@ var MCP_APP_MAX_DOWNLOAD_BYTES = 20 * 1024 * 1024;
11908
12083
  MemorySkillStore,
11909
12084
  MemoryStore,
11910
12085
  PIPELINE_LIMITS,
12086
+ PIPELINE_ROUTER_SYSTEM,
11911
12087
  PipelineNodeError,
11912
12088
  PipelineValidationError,
11913
12089
  PlanSchema,
@@ -12026,6 +12202,7 @@ var MCP_APP_MAX_DOWNLOAD_BYTES = 20 * 1024 * 1024;
12026
12202
  looksLikeJson,
12027
12203
  markCodeExecution,
12028
12204
  markReadOnly,
12205
+ matchPipelineRoute,
12029
12206
  matchRule,
12030
12207
  mcpAppOrigin,
12031
12208
  mcpSkillEntryRefusal,
@@ -12050,12 +12227,15 @@ var MCP_APP_MAX_DOWNLOAD_BYTES = 20 * 1024 * 1024;
12050
12227
  parseReplannerResponse,
12051
12228
  parseSearchResponse,
12052
12229
  parseSkillMarkdown,
12230
+ pickPipelineRoute,
12053
12231
  pickTier,
12054
12232
  preloadWebLLMModel,
12233
+ preparePipeline,
12055
12234
  promptHintOf,
12056
12235
  renderActiveSkills,
12057
12236
  renderCatalog,
12058
12237
  renderPipelineNodeInput,
12238
+ renderPipelineRoutePrompt,
12059
12239
  renderSearchCatalog,
12060
12240
  renderSkillIndex,
12061
12241
  replanWanted,
@@ -12072,6 +12252,7 @@ var MCP_APP_MAX_DOWNLOAD_BYTES = 20 * 1024 * 1024;
12072
12252
  resolveResume,
12073
12253
  resolveStage,
12074
12254
  resolveThinking,
12255
+ runPipelineGraph,
12075
12256
  runToolLoop,
12076
12257
  sanitizeSearchResults,
12077
12258
  schemaHintOf,