A3S CodeExamples
Streaming
Real-time event stream with AgentEvent
Streaming
Subscribe to AgentEvent for real-time visibility into what the agent is doing — text deltas, tool calls, token usage, and more.
Event Types
Prop
Type
Example
use a3s_code_core::{Agent, AgentEvent, SessionOptions};
let session = agent.session("/my-project", Some(
SessionOptions::new().with_permissive_policy()
))?;
let (mut rx, handle) = session.stream("Refactor the auth module", None).await?;
while let Some(event) = rx.recv().await {
match event {
AgentEvent::TextDelta { text } => print!("{text}"),
AgentEvent::ToolStart { name, .. } => println!("\n🔧 {name}..."),
AgentEvent::ToolEnd { name, exit_code, .. } => {
println!(" ✓ (exit={})", exit_code);
}
AgentEvent::TurnEnd { turn, usage } => {
println!("\n└─ Turn {turn} done ({} tokens)", usage.total_tokens);
}
AgentEvent::End { text, usage } => {
println!("\n■ Done — {} tokens total", usage.total_tokens);
break;
}
_ => {}
}
}
handle.await??;Run: cargo run --example 02_streaming
Source: core/examples/02_streaming.rs
session = agent.session("/my-project", permissive=True)
async for event in session.stream("Refactor the auth module"):
t = event.get("type")
if t == "text_delta":
print(event["text"], end="", flush=True)
elif t == "tool_start":
print(f"\n🔧 {event['name']}...", end="")
elif t == "tool_end":
print(f" ✓ (exit={event['exit_code']})")
elif t == "turn_end":
print(f"\n└─ Turn {event['turn']} done ({event['usage']['total_tokens']} tokens)")
elif t == "end":
print(f"\n■ Done — {event['usage']['total_tokens']} tokens total")
breakconst session = agent.session('/my-project', { permissive: true });
const stream = await session.stream('Refactor the auth module');
for await (const event of stream) {
switch (event.type) {
case 'text_delta':
process.stdout.write(event.text);
break;
case 'tool_start':
process.stdout.write(`\n🔧 ${event.name}...`);
break;
case 'tool_end':
console.log(` ✓ (exit=${event.exitCode})`);
break;
case 'turn_end':
console.log(`\n└─ Turn ${event.turn} done (${event.usage.totalTokens} tokens)`);
break;
case 'end':
console.log(`\n■ Done — ${event.usage.totalTokens} tokens total`);
break;
}
}Sample Output
┌─ Turn 1
🔧 read... ✓ (exit=0)
🔧 write... ✓ (exit=0)
└─ Turn 1 done (1842 tokens)
┌─ Turn 2
The auth module has been refactored. Key changes:
- Extracted JWT validation into a separate function
- Added proper error types
- Improved test coverage
└─ Turn 2 done (2103 tokens)
■ Done — 3945 tokens totalAPI Reference
stream()
Prop
Type
AgentEvent types
Prop
Type