opi-agent
Agent runtime — tool calling, hook lifecycle, and queue polling — for opi. A Rust port of pi's agent core.
Status (v0.2.0)
The Phase 1 runtime ships with the full turn lifecycle, tool execution
(parallel + sequential batching), validated arguments, cancellation, hook
points, and steering / follow-up message queues. Built on
opi-ai for provider streaming.
The Transport trait is reserved for stdio / SSE tool servers but is not yet
wired into the agent loop.
Core abstractions
Hook signatures below are abbreviated; see
hooks.rsfor the fullPin<Box<dyn Future<...>>>return types.
Agent loop
agent_loop()
├── for each turn (up to max_turns):
│ transform_context → convert_to_llm → provider.stream(Request)
│ ├── accumulate AssistantStreamEvent into AssistantContent
│ ├── detect tool calls
│ │ ├── validate args against JSON Schema (jsonschema crate)
│ │ ├── before_tool_call hook (Allow / Deny)
│ │ ├── execute (parallel when all tools are Parallel,
│ │ │ sequential if any tool is Sequential)
│ │ ├── after_tool_call hook (Keep / Replace)
│ │ ├── early stop if ALL results have terminate=true
│ │ └── should_stop_after_turn → stop or continue
│ └── prepare_next_turn → may inject extra messages
├── drain steering queue (mode: All)
└── pop one follow_up message (mode: OneAtATime) when no tools pending
Quick example
use ;
use ;
use Arc;
;
Wire it up with an opi_ai::Provider and AgentHooks impl via Agent::new,
then call agent.prompt("...").
Modules
| Module | Purpose |
|---|---|
agent |
Agent wrapper with prompt, continue_, abort, subscribe |
(root agent_loop) |
Async function that drives one full conversation |
tool |
Tool trait, ToolResult, ToolError, ExecutionMode |
hooks |
AgentHooks trait + per-hook context / result types |
event |
AgentEvent (start / message / tool / turn / queue / end) |
state |
AgentState (conversation state holder) |
message |
AgentMessage (LLM message + custom variants) |
loop_types |
AgentLoopContext, AgentLoopConfig, AgentError |
validation |
jsonschema-backed argument validation |
transport |
Placeholder Transport trait (not yet wired in) |
License
MIT — see workspace LICENSE.