@dudko.dev/agent 0.0.39 → 0.0.41

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/README.md CHANGED
@@ -92,19 +92,53 @@ await agent.close()
92
92
 
93
93
  ### Streaming events
94
94
 
95
- Pass an event handler as the second argument to `createAgent`, or per run via `onEvent`. Events include plan deltas, step starts and tool calls, replanner decisions, retries, budget breaches, the streamed final answer, and errors. See [`AgentEvent`](./src/types.ts) for the full union; the newer ones:
95
+ Pass an event handler as the second argument to `createAgent`, or per run via `onEvent`. Every event carries the `runId` of its run, so a handler shared by concurrent runs can tell them apart. The full union is [`AgentEvent`](./src/types.ts); this is the whole list, by stage.
96
+
97
+ **The loop**
96
98
 
97
99
  | Event | When |
98
100
  | --- | --- |
101
+ | `plan.thought-delta` | The planner's thought, streamed. |
102
+ | `plan.step-added` | A step of the plan is coherent enough to show (`step`, `index`). A hint for incremental UIs — it can still change before `plan.created`. |
103
+ | `plan.created` | The canonical plan (`plan`). |
104
+ | `plan.revised` | The replanner replaced the remaining steps (`plan`, `reason`). |
105
+ | `step.start` | A step began (`step`, `index`). |
106
+ | `step.text-delta` | The executor's text for the step. |
99
107
  | `step.reasoning-delta` / `final.reasoning-delta` | The executor's / synthesizer's thoughts, when [thinking](#thinking-reasoning) is on and the provider streams them. |
108
+ | `step.tool-call` / `step.tool-result` | A tool was called (`name`, `input`) / returned (`output`, `ok`). |
109
+ | `step.complete` | The step finished (`result`). |
110
+ | `replan.decision` | The replanner's verdict: `mode: 'continue' \| 'revise' \| 'finish'`, `reason`, and `cause: 'last-step' \| 'clean-step' \| 'llm-decision' \| 'user-message'`. |
111
+ | `revisions.exceeded` | The cap on plan revisions (`cap`) was reached; the run goes on to synthesize. |
112
+ | `final.text-delta` | The final answer, streamed. |
113
+ | `final` | The final answer, whole (`text`). |
114
+ | `retry` | A stage failed and is being retried (`phase`, `attempt`, `error`). |
115
+ | `error` | A stage failed for good (`error`, `phase: 'plan' \| 'execute' \| 'replan' \| 'synthesize' \| 'init'`). |
116
+ | `log` | A log line (`level`, `message`), the same ones `config.logger` gets. |
117
+
118
+ **Budgets, context and cost**
119
+
120
+ | Event | When |
121
+ | --- | --- |
122
+ | `usage` | Per LLM call; `phase` is `'plan' \| 'execute' \| 'replan' \| 'synthesize' \| 'compact' \| 'subagent'`. Counted live into [`agent.usage`](#usage-what-a-session-has-spent). |
100
123
  | `budget.exceeded` | A run cap was crossed; `kind: 'input' \| 'output' \| 'reasoning' \| 'total' \| 'tool-calls'`, `tokens`, `cap`. |
101
- | `context.compacted` | History or trace was compacted (`scope`, `beforeTokens`, `afterTokens`). |
124
+ | `context.compacted` | History, trace or stale tool results were compacted (`scope: 'history' \| 'trace' \| 'tool-results'`, `beforeTokens`, `afterTokens`). |
125
+
126
+ **Tools, skills and consent**
127
+
128
+ | Event | When |
129
+ | --- | --- |
102
130
  | `skill.activated` | A skill became active (`by: 'plan' \| 'tool'`). |
103
131
  | `tools.discovered` | `find_tools` activated tools (`query`, `names`). |
104
132
  | `tool.approval-requested` / `tool.approval-resolved` | The approval gate asked / decided (`automatic` for rule, mode and timeout denials). |
105
- | `subagent.start` / `subagent.event` / `subagent.complete` / `subagent.error` | A subagent tool call's lifecycle and its forwarded child events. |
133
+ | `egress.request` | A `web_fetch` request, allowed or refused by the [egress policy](#egress-what-the-model-may-fetch) (`url` is origin and path only, `allowed`, `status?`, `reason?`). |
134
+ | `skill.learned` / `skill.forgotten` | The agent wrote, rewrote (`update`) or removed a lesson — see [Learning](#learning-lessons-that-outlive-the-task) (`name`, and for a lesson `description`, `chars`, `sources`). |
135
+
136
+ **Subagents and steering**
137
+
138
+ | Event | When |
139
+ | --- | --- |
140
+ | `subagent.start` / `subagent.event` / `subagent.complete` / `subagent.error` | A subagent tool call's lifecycle and its forwarded child events (`error` carries `stopped` when the user stopped it). |
106
141
  | `message.queued` / `message.delivered` | A `RunControl.send()` message is waiting for a boundary / the model has seen it (`at: 'round' \| 'step' \| 'final'`). See [Steering a run](#steering-a-run-that-is-already-going). |
107
- | `usage` | Per LLM call; `phase` is `'plan' \| 'execute' \| 'replan' \| 'synthesize' \| 'compact' \| 'subagent'`. |
108
142
 
109
143
  ```ts
110
144
  const agent = await createAgent(config, (event) => {
@@ -260,6 +294,22 @@ const agent = await createAgent({
260
294
 
261
295
  The run caps are **soft**: they are checked between steps *and* inside an executor step, as an extra `stopWhen` condition that adds the usage of the step's LLM calls to the run so far — so a runaway tool loop stops at its next LLM step instead of at the end of the step. When a cap is crossed the agent emits `budget.exceeded` (`kind: 'input' | 'output' | 'reasoning' | 'total' | 'tool-calls'`), stops executing steps and goes straight to synthesis, which still runs (capped by `perCall.synthesizer`). `checkLimits(usage, limits)` is the pure check, exported.
262
296
 
297
+ **Per-call output caps default to a share of the context window.** `perCall` limits what one LLM call of a stage may write. Leave a stage out and it gets a share of `compaction.contextWindowTokens` (default 128k), kept between a floor and a ceiling, so the cap follows the model instead of being one number that is wrong at one end:
298
+
299
+ | Stage | Share of the window | Floor – ceiling | At 128k |
300
+ | --- | --- | --- | --- |
301
+ | `planner` | 1/64 | 512 – 4,000 | 2,000 |
302
+ | `executor` | 1/16 | 2,048 – 32,000 | 8,000 |
303
+ | `replanner` | 1/128 | 256 – 2,000 | 1,000 |
304
+ | `synthesizer` | 1/32 | 1,024 – 16,000 | 4,000 |
305
+
306
+ (`compaction` keeps its own `summaryMaxTokens`, default 1,024.) A cap never takes more than half of a small window. The executor's share is the widest because a tool call's arguments — a file it writes — count as output.
307
+
308
+ - **Yours wins.** Any `limits.perCall.<stage>` replaces the default, and **`0` means no cap** (the provider's own maximum applies — what Node did before these defaults existed).
309
+ - **A stage that thinks gets no default cap.** On OpenAI and Gemini the reasoning counts against `max_output_tokens`, so a default sized for the answer alone could leave a thinking model no room to write it. Set `perCall` yourself if you want a cap there (Anthropic adds the thinking budget on top of it).
310
+ - **A call the cap cuts off says so.** The executor and synthesizer emit a `log` warning (`[synthesizer] the model's output was cut off at 4000 tokens, the default cap for this stage …`); a plan or a replanner decision cut in half fails with the same reason in its error instead of a bare "could not parse the response".
311
+ - The browser sibling `@dudko.dev/agent-web` derives its caps the same way, with its own, smaller shares (800 / 1,200 / 400 / 3,000 at 128k).
312
+
263
313
  `maxToolCalls`: once reached, further tool calls in the run fail with `tool-call budget exhausted` (the model sees that as the tool result) and the run moves to synthesis with `budget.exceeded` of kind `'tool-calls'`. Built-in tools (`find_tools`, `load_skill`, `read_skill_file`) do not count.
264
314
 
265
315
  The other step caps are unchanged: `maxIterations` (executed steps per run), `maxStepsPerTask` (LLM steps inside one executor call) and `maxRevisions` (replanner revisions per run).
@@ -590,7 +640,7 @@ Consent is the host's job ([tool approval](#tool-approval-and-autopilot)) — th
590
640
  | `maxRevisions` | Cap on `revise` decisions the replanner can make per run. Default `2`. |
591
641
  | `replanAfter` | Replan trigger: `'failure'` (default; blocked step or a tool failure that stayed failed) \| `'always'` \| `(stepResult) => boolean \| Promise<boolean>` (bounded by `llmTimeoutMs`; falls back to `'failure'` on error). |
592
642
  | `maxTotalTokens` | Soft cap on cumulative input + output tokens; checked between steps and triggers an early jump to synthesis when crossed. Legacy shortcut for `limits.maxTotalTokens` (which wins). |
593
- | `limits` | `{ maxInputTokens?, maxOutputTokens?, maxReasoningTokens?, maxTotalTokens?, perCall?: { planner?, executor?, replanner?, synthesizer?, compaction? } }` — cumulative per-run caps (also enforced inside an executor step) and per-call output caps. See [Token limits](#token-limits-and-step-caps). |
643
+ | `limits` | `{ maxInputTokens?, maxOutputTokens?, maxReasoningTokens?, maxTotalTokens?, perCall?: { planner?, executor?, replanner?, synthesizer?, compaction? } }` — cumulative per-run caps (also enforced inside an executor step) and per-call output caps (default: a share of the context window; `0` = none). See [Token limits](#token-limits-and-step-caps). |
594
644
  | `maxToolCalls` | Cap on tool calls per run; further calls fail with "tool-call budget exhausted" and the run goes to synthesis. |
595
645
  | `maxPlanSteps` | Hard cap on plan steps (initial and revised). Default `8`. |
596
646
  | `thinking` / `stageThinking` | Reasoning for every stage / per stage (`planner`, `executor`, `replanner`, `synthesizer`). See [Thinking](#thinking-reasoning). |
@@ -695,9 +745,9 @@ The package ships a REPL CLI as `dd-agent`. After install, npm makes it availabl
695
745
  dd-agent --env-file=.env
696
746
  ```
697
747
 
698
- `--env-file=<path>` is loaded via Node's built-in `process.loadEnvFile`, so no `dotenv` dependency is needed. Without the flag the CLI reads the ambient process env. `-h` / `--help` prints the supported flags and the in-REPL slash commands (`/status`, `/usage`, `/tools`, `/history`, `/reset`, `/compact`, `/autopilot`, `/approval <mode>`, `/reconnect`, `/agents`, `/stop`, `/exit`).
748
+ `--env-file=<path>` is loaded via Node's built-in `process.loadEnvFile`, so no `dotenv` dependency is needed. Without the flag the CLI reads the ambient process env. `-h` / `--help` prints the supported flags and the in-REPL slash commands (`/status`, `/usage`, `/tools`, `/history`, `/reset`, `/compact`, `/autopilot`, `/approval <mode>`, `/reconnect`, `/skills`, `/forget <name>`, `/agents`, `/stop [n]`, `/exit` — `/quit` does the same).
699
749
 
700
- The newer features are env-driven too: `AGENT_THINKING` (`off | minimal | low | medium | high | xhigh | <budget tokens>`), `AGENT_MAX_INPUT_TOKENS` / `AGENT_MAX_OUTPUT_TOKENS` / `AGENT_MAX_REASONING_TOKENS` / `AGENT_MAX_TOTAL_TOKENS`, `AGENT_MAX_TOOL_CALLS`, `AGENT_TOOL_APPROVAL` (`autopilot | ask-writes | ask-all | read-only`), `AGENT_TOOL_RULES` (JSON `{ "<tool or glob*>": "allow" | "ask" | "deny" }`, beats the mode — e.g. `{"github__delete_*":"deny"}`), `AGENT_EGRESS` (`on | off`; with `AGENT_EGRESS_ALLOW`, `AGENT_EGRESS_DENY`, `AGENT_EGRESS_PROTOCOLS`, `AGENT_EGRESS_ALLOW_PRIVATE` — see [Egress](#egress-what-the-model-may-fetch)), `AGENT_SKILLS_DIR`, `AGENT_LEARNING` (`on | off`; `AGENT_LEARNING_DIR` keeps the lessons across sessions — see [Learning](#learning-lessons-that-outlive-the-task)), `AGENT_TOOL_SELECTION_STRATEGY` (`auto | all | plan-narrowed | search`), `AGENT_CONTEXT_WINDOW_TOKENS`, `AGENT_COMPACTION_THRESHOLD` (percent of the window where auto-compaction starts, default 85) and `AGENT_COMPACTION=off`. The REPL prints the model's thoughts dimmed, auto-compacts its history with the same settings, and — when the approval mode asks — prompts `allow? [y]es / [n]o / [a]lways` right in the terminal; `/autopilot` toggles between autopilot and the last asking mode.
750
+ The newer features are env-driven too: `AGENT_THINKING` (`off | minimal | low | medium | high | xhigh | <budget tokens>`), `AGENT_MAX_INPUT_TOKENS` / `AGENT_MAX_OUTPUT_TOKENS` / `AGENT_MAX_REASONING_TOKENS` / `AGENT_MAX_TOTAL_TOKENS`, `AGENT_MAX_TOOL_CALLS`, `AGENT_PER_CALL_TOKENS` (JSON `{"executor":16000,"synthesizer":0}` — per-stage output caps, `0` = no cap, see [Token limits](#token-limits-and-step-caps)), `AGENT_TOOL_APPROVAL` (`autopilot | ask-writes | ask-all | read-only`), `AGENT_TOOL_RULES` (JSON `{ "<tool or glob*>": "allow" | "ask" | "deny" }`, beats the mode — e.g. `{"github__delete_*":"deny"}`), `AGENT_EGRESS` (`on | off`; with `AGENT_EGRESS_ALLOW`, `AGENT_EGRESS_DENY`, `AGENT_EGRESS_PROTOCOLS`, `AGENT_EGRESS_ALLOW_PRIVATE` — see [Egress](#egress-what-the-model-may-fetch)), `AGENT_SKILLS_DIR`, `AGENT_LEARNING` (`on | off`; `AGENT_LEARNING_DIR` keeps the lessons across sessions — see [Learning](#learning-lessons-that-outlive-the-task)), `AGENT_TOOL_SELECTION_STRATEGY` (`auto | all | plan-narrowed | search`), `AGENT_CONTEXT_WINDOW_TOKENS`, `AGENT_COMPACTION_THRESHOLD` (percent of the window where auto-compaction starts, default 85) and `AGENT_COMPACTION=off`. The REPL prints the model's thoughts dimmed, auto-compacts its history with the same settings, and — when the approval mode asks — prompts `allow? [y]es / [n]o / [a]lways` right in the terminal; `/autopilot` toggles between autopilot and the last asking mode.
701
751
 
702
752
  **Talking to the agent while it works.** The REPL keeps reading while a run goes. Text you type then is said to the running agent (it sees it at its next boundary and the replanner re-checks the plan — see [Steering a run](#steering-a-run-that-is-already-going)): the REPL prints `[you->agent] queued` and then `delivered (round|step|final)`, and the message enters the history as a user turn. A message that arrives while the answer is being written is sent as the next prompt (`[follow-up]`). After Ctrl-C nothing is sent behind your back: what the agent never saw is listed as `[not delivered]`. While a run goes, `/agents` lists the running subagents and `/stop <n>` (or an id prefix, or just `/stop` when one runs) stops one — the run goes on; the other REPL commands work between runs. Each subagent's own steps and tool calls are logged as `[name id] ...` lines, so parallel ones can be told apart.
703
753
 
@@ -707,7 +757,18 @@ For local development against the source tree:
707
757
  npm start # node --experimental-strip-types src/cli/start.ts --env-file=.env
708
758
  ```
709
759
 
710
- The CLI source lives in [`src/cli`](./src/cli) — see [`env.example`](./env.example) for the full list of recognized env vars.
760
+ The CLI source lives in [`src/cli`](./src/cli) — [`env.example`](./env.example) is the full, commented list of recognized env vars. The core ones:
761
+
762
+ | Variable | What it sets |
763
+ | --- | --- |
764
+ | `AGENT_PROVIDER_TYPE`, `AGENT_MODEL`, `AGENT_API_KEY`, `AGENT_BASE_URL` | The provider, the executor's model, its key and endpoint (`AGENT_BASE_URL` is required for `openai-compatible` and `azure`). |
765
+ | `AGENT_PLANNER_MODEL`, `AGENT_SYNTHESIZER_MODEL` | A different model for the planner + replanner / the synthesizer; both default to `AGENT_MODEL`. |
766
+ | `AGENT_PLANNER_*`, `AGENT_SYNTHESIZER_*` (`PROVIDER_TYPE`, `BASE_URL`, `API_KEY`, `PROVIDER_OPTIONS`) | Run a stage on another provider (see [Picking models per stage](#picking-models-per-stage)). A key is never inherited across vendors. |
767
+ | `AGENT_PROVIDER_OPTIONS` | JSON spread into the provider factory (`{"region":"us-east-1"}`, Azure's `resourceName`, …). |
768
+ | `AGENT_MAX_ITERATIONS`, `AGENT_MAX_STEPS_PER_TASK`, `AGENT_MAX_REVISIONS` | Loop caps: plan/execute/replan rounds, LLM steps inside one executor call, replanner "revise" decisions. |
769
+ | `AGENT_LLM_TIMEOUT_MS`, `AGENT_LLM_MAX_RETRIES` | Per-call timeout (`0` = off) and retries on 5xx / 429 / network errors (default 2). |
770
+ | `AGENT_AVAILABLE_TOOLS`, `AGENT_EXCLUDED_TOOLS` | Comma lists of MCP tool names to offer / withhold (available wins). |
771
+ | `AGENT_CLIENT_NAME`, `AGENT_LOG_LEVEL` | The `User-Agent` sent on MCP / provider requests; `none \| error \| warn \| info \| debug`. |
711
772
 
712
773
  ## Build & test
713
774