@dudko.dev/agent-web 0.0.42 → 0.0.44

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.
Files changed (40) hide show
  1. package/README.md +150 -4
  2. package/dist/agent-web.js +155 -155
  3. package/dist/agent-web.js.map +1 -1
  4. package/dist/{chunk-DAREMCXF.js → chunk-236RCVAZ.js} +40 -8
  5. package/dist/chunk-236RCVAZ.js.map +1 -0
  6. package/dist/{chunk-2MLHZ6LQ.js → chunk-Y4MIIBPB.js} +2 -2
  7. package/dist/{chunk-ZSZSSJS4.js → chunk-Y6CD3WOK.js} +176 -10
  8. package/dist/chunk-Y6CD3WOK.js.map +1 -0
  9. package/dist/{chunk-YPCHD6DV.js → chunk-YGWIOO27.js} +31 -12
  10. package/dist/chunk-YGWIOO27.js.map +1 -0
  11. package/dist/index.cjs +1248 -108
  12. package/dist/index.cjs.map +1 -1
  13. package/dist/index.d.cts +1777 -1643
  14. package/dist/index.d.ts +1777 -1643
  15. package/dist/index.js +996 -89
  16. package/dist/index.js.map +1 -1
  17. package/dist/mcp.cjs +136 -5
  18. package/dist/mcp.cjs.map +1 -1
  19. package/dist/mcp.d.cts +5 -2
  20. package/dist/mcp.d.ts +5 -2
  21. package/dist/mcp.js +19 -8
  22. package/dist/mcp.js.map +1 -1
  23. package/dist/pipeline-engine.cjs +1130 -0
  24. package/dist/pipeline-engine.cjs.map +1 -0
  25. package/dist/pipeline-engine.d.cts +226 -0
  26. package/dist/pipeline-engine.d.ts +226 -0
  27. package/dist/pipeline-engine.js +1094 -0
  28. package/dist/pipeline-engine.js.map +1 -0
  29. package/dist/{sandbox-3VRX47UY.js → sandbox-7CGSIIIZ.js} +4 -4
  30. package/dist/sandbox-worker.cjs +30 -11
  31. package/dist/sandbox-worker.cjs.map +1 -1
  32. package/dist/sandbox-worker.js +1 -1
  33. package/dist/{elicitation-DS-84_bF.d.cts → vault-Mar2CY0J.d.cts} +526 -526
  34. package/dist/{elicitation-DS-84_bF.d.ts → vault-Mar2CY0J.d.ts} +526 -526
  35. package/package.json +13 -3
  36. package/dist/chunk-DAREMCXF.js.map +0 -1
  37. package/dist/chunk-YPCHD6DV.js.map +0 -1
  38. package/dist/chunk-ZSZSSJS4.js.map +0 -1
  39. /package/dist/{chunk-2MLHZ6LQ.js.map → chunk-Y4MIIBPB.js.map} +0 -0
  40. /package/dist/{sandbox-3VRX47UY.js.map → sandbox-7CGSIIIZ.js.map} +0 -0
package/README.md CHANGED
@@ -794,7 +794,7 @@ const agent = await createAgent({
794
794
  },
795
795
  })
796
796
  agent.setToolApprovalMode('autopilot') // flip the switch at any time
797
- await agent.compact() // summarise the stored transcript now
797
+ await agent.compact() // summarise the stored transcript now (waits for a run of the session; { wait: false } throws instead)
798
798
  ```
799
799
 
800
800
  With more than 40 tools (several MCP servers) the executor switches to **tool
@@ -809,7 +809,13 @@ tools — a helper can only have less than the run that starts it, and its spend
809
809
  that run's limits. Give `persistence` a store
810
810
  (`new IndexedDBRunStore()`) and a run interrupted by a reload or a crash resumes from its
811
811
  last checkpoint with `run('', { resumeFromRunId })` — the planner does not run again and
812
- no finished step is repeated. `createRunControl()` steers a run that is going: `send()`
812
+ no finished step is repeated; every event of a run carries `seq` and `attempt` (one more
813
+ per resume, recorded by `onRunResume` before the resumed run emits anything), so
814
+ `(attempt, seq)` orders a run's events across reloads. `run(goal, { runId })` starts a fresh
815
+ run under an id of yours (1-128 letters, digits, `_` or `-`) instead of a random one, so that a
816
+ retry finds the same run: refused together with `resumeFromRunId`, and refused when `loadRun`
817
+ already has that run (resume it instead - a run on record is never written over); see
818
+ [Resuming](docs/capabilities.md#resuming-an-interrupted-run). `createRunControl()` steers a run that is going: `send()`
813
819
  puts a message in front of the model at the next boundary (the replanner re-checks the plan
814
820
  against it), `stopSubagent(id)` stops one subagent. `egress` (off by default) gives the model one safe `web_fetch` tool with allow / deny lists. `search` (off by default) adds `web_search` over a function of yours (any search API — recipes for Tavily, Brave and SearXNG are in the docs) or the built-in Google Search through Gemini grounding, which needs only a Gemini key and spends its tokens — the key stays in the credential store. `sandbox` (off by default) gives the model `run_js`: the code it wrote runs in QuickJS-in-WebAssembly inside a Web Worker that is killed at the deadline, so "I ran it" is a tool call you can see. `media` (off by default) mounts `generate_image`, `generate_speech` and `transcribe_audio` — each only when you configure its model — writes what they make into the virtual file system instead of the context, and turns attached audio into text for a model that cannot listen. `learning` (off by default) lets the agent write down what it figured out as skills of its own — in memory, or in the file system across sessions. Details, defaults and events:
815
821
  **[docs/capabilities.md](docs/capabilities.md)**.
@@ -870,6 +876,144 @@ Conditions (`always`, `contains`, `matches`, `route`, `otherwise`), the scheduli
870
876
  join, the loop caps), validation and the events: **[docs/capabilities.md](docs/capabilities.md#pipelines-a-graph-of-agents)**.
871
877
  Loading a definition someone else wrote: [docs/security.md](docs/security.md#pipelines-a-definition-is-data).
872
878
 
879
+ ### Running the graph yourself
880
+
881
+ `createPipeline` is a scheduler with `createAgent` as its node runner. Both halves are public, so a
882
+ host can keep the scheduler and run every node its own way - a worker, a server job, another tab -
883
+ and make the route call where the node ran:
884
+
885
+ ```ts
886
+ import { createAgent, pickPipelineRoute, preparePipeline, runPipelineGraph } from '@dudko.dev/agent-web'
887
+
888
+ const plan = preparePipeline(definition) // validates (PipelineValidationError) and compiles
889
+ const result = await runPipelineGraph(
890
+ plan,
891
+ async (node, input, ctx) => {
892
+ // input: { request, from: [{ nodeId, name?, text }], visit } - renderPipelineNodeInput(input)
893
+ // is the text a createPipeline node is given.
894
+ const text = await worker.run(node.id, input, { signal: ctx.signal }) // your runner
895
+ if (!node.routes?.length) return { text }
896
+ // The route call createPipeline makes: the node's decision model, then its planner model.
897
+ const config = configOf(node) // the node's BrowserAgentConfig
898
+ const { route, usage } = await pickPipelineRoute(node, text, {
899
+ agent: await agentOf(node, config), // createAgent(config), kept per node
900
+ config,
901
+ signal: ctx.signal,
902
+ onEvent: ctx.emit,
903
+ })
904
+ return { text, usage, route }
905
+ },
906
+ { input: 'A release note for v2', signal, onEvent, onAbort: 'resolve' },
907
+ )
908
+ ```
909
+
910
+ The runner's contract:
911
+
912
+ - **When it is called.** Once per visit, never for an activation a cap refused (`plan.maxNodeVisits`
913
+ / `plan.maxRuns`: the definition's `limits` or the defaults). All visits of a wave are started, in
914
+ the order their nodes were first activated, before any is awaited; the next wave starts when all
915
+ of them have settled. A visit's answer is applied (events, trace, edges) when it settles.
916
+ - **What it returns.** `{ text, usage?, route? }`. `text` is what the edges test (not a string
917
+ counts as `''`); `usage` is added to `result.usage`, every numeric field; `route` must be one of
918
+ `node.routes`' labels exactly as written - anything else, or none, is no route. To pick one with a
919
+ model of your own: `PIPELINE_ROUTER_SYSTEM` and `renderPipelineRoutePrompt(node, text)` are the
920
+ prompt, `matchPipelineRoute(node, answer)` turns the answer into a label.
921
+ - **When it fails.** A runner that rejects fails the run with a `PipelineNodeError` (`nodeId`,
922
+ `visit`, `cause`), whatever `onAbort` says: the other visits' `ctx.signal` aborts, and what they
923
+ return afterwards is ignored.
924
+ - **Abort.** `signal` aborts the visits in flight (their `ctx.signal`) and settles the run at once,
925
+ without waiting for them: it rejects with an `AbortError` (`onAbort: 'reject'`, the default, as
926
+ the Node sibling's `createPipeline` does) or resolves with what it has and `stoppedBy: 'aborted'`
927
+ (`'resolve'`, as `createPipeline` does here). Aborted before it starts: no visit at all.
928
+ - **Events.** `onEvent` gets the `pipeline.*` events `createPipeline` sends; `ctx.emit(event)`
929
+ forwards one of the node's own, with `nodeId` and `visit`, until the run is over.
930
+
931
+ `pickPipelineRoute(node, text, { agent, config, signal?, onEvent? })` resolves with
932
+ `{ route?, usage }`: the agent's decision model first (`config.decisionModel`, when it is
933
+ confident of a label), else its planner model - structured output, then plain text - with the
934
+ config's budget, temperature and `chatTimeoutMs`. No label, or a failed call, is no route; an
935
+ abort rejects with an `AbortError`.
936
+
937
+ **Deterministic by contract.** The engine - `preparePipeline`, `runPipelineGraph`, the conditions -
938
+ reads no clock, no randomness and no timer, and does no I/O: the same definition, input and runner
939
+ answers, arriving in the same order (and an abort at the same point), give the same calls, events,
940
+ trace and result. So it can be a deterministic scheduler, replayed from a log of the runner's
941
+ answers. It needs nothing but the JavaScript language: where there is no `AbortController`,
942
+ `ctx.signal` is a stand-in (`aborted`, `reason`, `onabort`, `throwIfAborted()`, `addEventListener` /
943
+ `removeEventListener`), where there is no `DOMException` the abort error is an `Error` named
944
+ `AbortError`, and of your `signal` only `aborted` and `addEventListener` / `removeEventListener`
945
+ for `'abort'` are read. `pickPipelineRoute` calls a model, so it is not part of that: it belongs
946
+ where the node runs. Why: [docs/design.md](docs/design.md#pipelines-waves-a-join-rule-caps).
947
+
948
+ **The engine alone: `@dudko.dev/agent-web/pipeline-engine`.** The main entry loads the whole
949
+ agent - `ai`, `zod`, `idb` -, which a bundled orchestrator cannot import (a workflow sandbox has the
950
+ language and little else). The engine has an entry point of its own, ESM and CommonJS with types,
951
+ whose file imports nothing:
952
+
953
+ ```ts
954
+ // In the workflow: the scheduler only; every node runs outside it (an activity, a job).
955
+ import { preparePipeline, runPipelineGraph } from '@dudko.dev/agent-web/pipeline-engine'
956
+ ```
957
+
958
+ It exports `preparePipeline`, `runPipelineGraph`, `validatePipeline`, `normalizePipeline`,
959
+ `testPipelineCondition`, `renderPipelineNodeInput`, `renderPipelineRoutePrompt`,
960
+ `matchPipelineRoute`, `PIPELINE_ROUTER_SYSTEM`, `PIPELINE_LIMITS`, `PipelineValidationError`,
961
+ `PipelineNodeError` and every pipeline type - the same names as the main entry, and the Node
962
+ sibling's `@dudko.dev/agent/pipeline-engine` is the same file. It is a copy of the engine the main
963
+ entry carries, not the same objects: an error thrown by one is not `instanceof` the other's class -
964
+ compare `error.name`. More: [docs/capabilities.md](docs/capabilities.md#pipelines-a-graph-of-agents).
965
+
966
+ ## Several tabs
967
+
968
+ An installed PWA window and a browser tab of the same site share IndexedDB - the vault, the files,
969
+ the memory. The library keeps them from working against each other (browser APIs only; a no-op
970
+ where the browser lacks them, and nothing for a store in memory):
971
+
972
+ ```ts
973
+ import {
974
+ createAgent,
975
+ createRunControl,
976
+ IndexedDBStore,
977
+ onStorageChange,
978
+ watchSession,
979
+ } from '@dudko.dev/agent-web'
980
+
981
+ // 1. After a write COMMITS, the other tabs hear which thing changed - never its value.
982
+ onStorageChange('agent-web', (c) => console.log(c)) // { kind: 'files', op: 'put', key: '/a.md', namespace: 'default' }
983
+
984
+ // 2. One session, one runner: a second run of `chat-1` (in any tab) waits for this one.
985
+ const agent = await createAgent({ model, memory: new IndexedDBStore(), sessionId: 'chat-1' })
986
+ // Holds the session's Web Lock; with a `control` it also takes messages from the other tabs.
987
+ void agent.run('Summarise the report', { control: createRunControl() })
988
+
989
+ // 3. In another tab: follow that run live, write to it, stop it.
990
+ const watch = watchSession({ sessionId: 'chat-1', onEvent: console.log, onState: console.log })
991
+ await watch.send('only the first chapter')
992
+ await watch.stop()
993
+ ```
994
+
995
+ - **Change notices** (`credentials`, `oauth`, `files`, `session`, `runs`, `skills`, and the React
996
+ package's `chats`): one channel per database name, sent after the transaction completed, never for
997
+ a failed write, never heard by the tab that wrote. A store of your own posts with
998
+ `postStorageChange`. `agent.reloadLearnedSkills()` re-reads the notebook after a `skills` notice.
999
+ - **`sessionLock`** (`'wait'` default, `'fail'`, `'off'`): the session's Web Lock for the run's whole
1000
+ duration, released however it ends (and by the browser when the tab closes). Subagents that share
1001
+ the session run under the parent's hold; helpers and pipeline nodes have sessions of their own.
1002
+ - **A behaviour change** for a host that ran one session in several tabs: the second run now waits
1003
+ for the first (one `logger.warn` the first time, "this run waits for it"). `sessionLock: 'off'`
1004
+ keeps the old behaviour; the wait has no timeout of its own (pass a `signal`);
1005
+ `agent.compact({ wait: false })` throws `SessionBusyError` instead of waiting.
1006
+ - **`sessionRelay`** (`{ control: true }` default, `{ control: false }`, `false`): what the running
1007
+ tab offers the others - events (posted only once a tab asks), and with `control` a message
1008
+ (marked `origin: 'another-tab'`) or a stop.
1009
+ - **`watchSession`**: the run's events (clipped), its state (`running`, `ended: 'done' | 'stopped' |
1010
+ 'gone'`), a replay for a tab that joins late, `send` and `stop`. Tool approvals never cross: they
1011
+ are answered in the tab that runs the tool.
1012
+
1013
+ Details: [Several tabs](docs/capabilities.md#several-tabs) ·
1014
+ [what crosses, and who can post](docs/security.md#several-tabs-what-crosses-and-who-can-post) ·
1015
+ [why not a SharedWorker](docs/design.md#several-tabs-broadcastchannel-and-web-locks-not-a-sharedworker).
1016
+
873
1017
  ## Configuration (highlights)
874
1018
 
875
1019
  | Option | Default | Purpose |
@@ -906,7 +1050,9 @@ Loading a definition someone else wrote: [docs/security.md](docs/security.md#pip
906
1050
  | `describeState` | — | serialize world state into prompt context |
907
1051
  | `memory` | — | `ContextStore` (`IndexedDBStore` / `MemoryStore`); recent turns are read back into the planner prompt |
908
1052
  | `sessionId` | — | names the stored conversation in `memory`, and the bucket `agent.sessionUsage()` counts into |
909
- | `persistence` | — | checkpoint runs (`IndexedDBRunStore` / `MemoryRunStore`) so `run(goal, { resumeFromRunId })` continues after a reload — see [Resuming](docs/capabilities.md#resuming-an-interrupted-run) |
1053
+ | `sessionRelay` | `{ control: true }` | what the session's running tab offers the site's other tabs: its events (posted once a tab asks) and, with `control`, a message (`origin: 'another-tab'`) or a stop. `{ control: false }`: watchable only; `false`: nothing relayed — see [security](docs/security.md#several-tabs-what-crosses-and-who-can-post) |
1054
+ | `sessionLock` | `'wait'` | one run per session across the site's tabs, when `memory` is shared (`IndexedDBStore`): a second run of the session waits for the first (`session.busy` / `session.acquired`), `'fail'` throws `SessionBusyError`, `'off'` = no lock and no relay (the last write wins). The running tab relays the run to the others (`watchSession`) — see [Several tabs](#several-tabs) |
1055
+ | `persistence` | — | checkpoint runs (`IndexedDBRunStore` / `MemoryRunStore`) so `run(goal, { resumeFromRunId })` continues after a reload; hooks `onRunStart` / `onStepComplete` / `onRunComplete` / `onRunResume` (a resume's new `attempt`, before any event) / `loadRun` — see [Resuming](docs/capabilities.md#resuming-an-interrupted-run) |
910
1056
  | `maxIterations` / `maxStepsPerTask` / `maxRevisions` | 8 / 4 / 2 | loop caps |
911
1057
  | `chatTimeoutMs` | 120000 | per-call watchdog; for the executor, the whole step (its tool calls included). Stopped while a tool call waits for a person (an approval, an MCP question): the step goes on with the time it had left - see [Tool consent](docs/capabilities.md#tool-consent-and-autopilot) |
912
1058
  | `temperature` | provider default | sampling temperature for every stage |
@@ -976,7 +1122,7 @@ If you do import it:
976
1122
  - [docs/design.md](docs/design.md) — architecture & rationale.
977
1123
  - [docs/capabilities.md](docs/capabilities.md) — thinking, limits, compaction,
978
1124
  caching, skills, tool consent / autopilot, large MCP catalogues, subagents,
979
- steering a running agent, learning, egress, pipelines, usage, resuming, autonomy, event reference.steering a running agent, learning, egress, usage, resuming, autonomy, event reference.
1125
+ steering a running agent, several tabs, learning, egress, pipelines, usage, resuming, autonomy, event reference.steering a running agent, learning, egress, usage, resuming, autonomy, event reference.
980
1126
  - [docs/providers.md](docs/providers.md) — every provider, CORS & direct-vs-proxy.
981
1127
  - [docs/security.md](docs/security.md) — the token vault, egress, learning and pipelines: threat models.— the token vault, egress and learning: threat models.
982
1128
  - [docs/tasks.md](docs/tasks.md) — status & roadmap.