Communication Architecture
PiCrust uses channels for bidirectional communication between your application and running agents.Input Messages
InputMessage Enum
Messages sent TO the agent:Sending Input
Output Messages
OutputChunk Enum
Messages sent FROM the agent:Receiving Output
Critical Pattern: Subscribe Before Send
Correct Order
Wrong Order
Why This Matters
Agents start processing immediately when they receive input. If you subscribe after sending, you’ll miss:- Early text tokens
- Tool execution notifications
- State changes
- Permission requests
The Foolproof Pattern
Multiple Subscribers
Multiple parts of your application can subscribe to the same agent:Message Processing Patterns
Simple Echo
Collect Full Response
Handle All Events
Multi-Turn Conversations
A single user input can trigger multiple LLM calls: Handle this naturally:Permission Flow
Request-Response Pattern
Implementation
Ask User Questions Flow
Interactive Questions
Implementation
Subagent Flow
Parent-Child Communication
Implementation
Interrupt Flow
Graceful Cancellation
Implementation
Channel Characteristics
Broadcast Channels
Output usestokio::sync::broadcast:
- Multiple subscribers: Many receivers get same messages
- Bounded buffer: Configurable capacity (default: 1024)
- Lagging behavior: Slow subscribers may miss messages
- Clonable: Call
subscribe()multiple times
MPSC Channels
Input usestokio::sync::mpsc:
- Single consumer: Only the agent receives
- Unbounded: Won’t block senders
- Order preserved: FIFO message delivery
- Clonable sender: Multiple parts can send
Performance Considerations
Buffer Size
Default broadcast buffer is 1024 messages:Subscriber Lag
If a subscriber is slow, it may miss messages:Backpressure
The system doesn’t implement backpressure. Fast producers continue regardless of slow consumers. Use fast async processing to keep up.Best Practices
1. Subscribe Early
2. Handle All Cases
3. Don’t Block the Receiver
4. Clean Up Subscribers
5. Use Timeouts
Next Steps
Agent States
Understand all agent states and transitions
Streaming & History
Critical dual-channel architecture pattern
OutputChunk Reference
Complete OutputChunk API documentation
InputMessage Reference
Complete InputMessage API documentation