9a4006cb2b
Add the agent-creation factory seam on ctx.agents (AgentRegistry):
setFactory/create/resume plus the AgentFactory interface and
CreateAgentOptions/ResumeAgentOptions. AgentLoop implements AgentFactory
and registers itself via ctx.agents.setFactory(this), so plugins
create/resume agents through the interface without depending on the
concrete loop package.
- create({ agentId, sessionId, meta?, agentOptions? }) — programmatic
create on a caller-supplied session id (e.g. an ACP-generated id).
- resume({ agentId, resumeSessionId, agentOptions? }) — load a persisted
session via ctx.sessionPersistence (RFC 009) and resume an agent on it;
the live session id is the resumed id, turn numbering and derived
history continue from the loaded log. sessionPersistence is NOT
hard-injected (non-persistent demos still work); resume rejects with a
typed error when it is absent. assertAgentIdFree runs before any
session is created (and again after the load await) so a duplicate id
never leaves an orphaned live session.
Adds the runtime dsh-session-persistence dependency to agent-loop.
68 lines
3.6 KiB
Markdown
68 lines
3.6 KiB
Markdown
# dsh-agent
|
|
|
|
Agent interface, registry, and `agent/*` event vocabulary. Every plugin (UI, hooks, orchestrators) programs against the `Agent` handle defined here — it has zero loop dependency, so the loop is swappable.
|
|
|
|
## Service: `AgentRegistry` (ctx key: `agents`)
|
|
|
|
Tracks live agents so UI, hook, and orchestrator plugins can find them without importing the concrete loop package.
|
|
|
|
### Public API
|
|
|
|
- `ctx.agents.register(agent: Agent): () => void` — record an **already-constructed** agent. Disposed with the calling fiber.
|
|
- `ctx.agents.get(id: string): Agent | undefined`
|
|
- `ctx.agents.list(): Agent[]`
|
|
|
|
#### Factory seam (creation)
|
|
|
|
Agent *creation* is provided by whichever plugin implements `AgentFactory` (phase 1: `dsh-agent-loop`), registered via `setFactory`. This keeps creation on the `dsh-agent` interface so consumers (UI, the ACP bridge) program against `ctx.agents` without depending on the concrete loop package.
|
|
|
|
- `ctx.agents.setFactory(factory: AgentFactory): () => void` — register the creation factory (the loop calls this on construction). Throws on a second factory; the slot clears on dispose.
|
|
- `ctx.agents.create(options: CreateAgentOptions): Agent` — construct, start, AND register a new agent on a caller-supplied `sessionId` (with optional `meta.cwd`). Distinct from `register` (which only records). Throws if no factory is registered.
|
|
- `ctx.agents.resume(options: ResumeAgentOptions): Promise<Agent>` — load a persisted session (RFC 009) and resume an agent on it. Async; rejects if no factory is registered, or if the factory finds session persistence unconfigured.
|
|
|
|
### Events
|
|
|
|
The full `agent/*` event taxonomy is declared via declaration merging in `dsh-agent` (not `dsh-agent-loop`), so plugins depend only on this package.
|
|
|
|
#### Lifecycle (emit)
|
|
|
|
- `agent/created`, `agent/disposed` — registration/deregistration
|
|
- `agent/status` — idle / running / disposed transition
|
|
- `agent/queued` — message entered inbox (source-resolved, steering flag)
|
|
|
|
#### Turn/step boundaries (emit)
|
|
|
|
- `agent/turn-start`, `agent/turn-end` (carries `TurnEndReason`)
|
|
- `agent/step-start`, `agent/step-end`
|
|
|
|
#### Interception seams (waterfall)
|
|
|
|
- `agent/request` — mutate `GenerateOptions` before the model call (hooks, compaction, model switching, tool filtering)
|
|
- `agent/step-result` — post-process the assembled assistant message before tool dispatch (validates what the log records)
|
|
- `agent/turn-continuation` — override the continue/stop decision (force-continue /loop, force-stop budget guard)
|
|
|
|
#### Streaming + tool (emit)
|
|
|
|
- `agent/stream-chunk` — raw chunk from the model (token-level UI/log feed)
|
|
- `agent/steering` — steering content injected mid-turn
|
|
- `agent/error` — step/turn error
|
|
|
|
### Agent interface (`types.ts`)
|
|
|
|
The handle every plugin programs against:
|
|
|
|
- `agent.send(content, options?)` — queue a message; starts a turn when idle
|
|
- `agent.steer(content, options?)` — steer a running turn (inject between steps); behaves like `send` when idle
|
|
- `agent.inject(content, options?)` — inject in-session context without triggering a turn (context/message event); next request sees it
|
|
- `agent.abort(reason?)` — abort the in-flight step
|
|
- `agent.session`, `agent.status`, `agent.options`, `agent.id`
|
|
|
|
### Extension points
|
|
|
|
- Agent creation: `AgentLoop.create()` is the concrete implementation (in `dsh-agent-loop`). Replace the loop by implementing `Agent` and registering via `ctx.agents.register()`.
|
|
- Event listeners: all `agent/*` events are declared here — no dependency on the loop package needed.
|
|
|
|
### What is NOT here (TODO)
|
|
|
|
- **Sub-agent spawn/fork** — seam on `AgentLoop.create()`, semantics deferred.
|