opi-agent 0.5.0

General-purpose agent runtime with tool calling and session management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
//! Behavioral tests for hooks and queues (task 1.8).
//!
//! DoD: "before/after, should-stop, steering, follow-up tested"

use std::future::Future;
use std::pin::Pin;
use std::sync::{Arc, Mutex};

use futures_util::stream;
use opi_agent::agent::Agent;
use opi_agent::event::AgentEvent;
use opi_agent::hooks::{
    AfterToolCallContext, AfterToolCallResult, AgentHooks, BeforeToolCallContext,
    BeforeToolCallResult, ShouldStopAfterTurnContext,
};
use opi_agent::loop_types::{AgentError, AgentLoopConfig};
use opi_agent::message::AgentMessage;
use opi_agent::tool::{ExecutionMode, Tool, ToolError, ToolResult};
use opi_ai::message::{
    AssistantContent, AssistantMessage, InputContent, Message, OutputContent, ToolCall, ToolDef,
};
use opi_ai::provider::{EventStream, Provider, ProviderError, Request};
use opi_ai::stream::{AssistantStreamEvent, StopReason, Usage};
use tokio_util::sync::CancellationToken;

// ---------------------------------------------------------------------------
// Recording mock provider
// ---------------------------------------------------------------------------

struct RecordingProvider {
    responses: Arc<Mutex<Vec<Vec<AssistantStreamEvent>>>>,
    received_messages: Arc<Mutex<Vec<Vec<Message>>>>,
}

impl RecordingProvider {
    fn new(responses: Vec<Vec<AssistantStreamEvent>>) -> Self {
        Self {
            responses: Arc::new(Mutex::new(responses)),
            received_messages: Arc::new(Mutex::new(Vec::new())),
        }
    }
}

impl Provider for RecordingProvider {
    fn id(&self) -> &str {
        "recording"
    }

    fn models(&self) -> &[opi_ai::provider::ModelInfo] {
        &[]
    }

    fn stream(&self, request: Request) -> EventStream {
        self.received_messages
            .lock()
            .unwrap()
            .push(request.messages);
        let events = self.responses.lock().unwrap().remove(0);
        Box::pin(stream::iter(events.into_iter().map(Ok::<_, ProviderError>)))
    }
}

// ---------------------------------------------------------------------------
// Echo tool
// ---------------------------------------------------------------------------

struct EchoTool;

impl Tool for EchoTool {
    fn definition(&self) -> ToolDef {
        ToolDef {
            name: "echo".into(),
            description: "echoes input".into(),
            input_schema: serde_json::json!({
                "type": "object",
                "properties": { "text": { "type": "string" } },
                "required": ["text"]
            }),
        }
    }

    fn execute(
        &self,
        _call_id: &str,
        args: serde_json::Value,
        _signal: CancellationToken,
        _on_update: Option<opi_agent::tool::UpdateCallback>,
    ) -> Pin<Box<dyn Future<Output = Result<ToolResult, ToolError>> + Send>> {
        let text = args["text"].as_str().unwrap_or_default().to_owned();
        Box::pin(async move {
            Ok(ToolResult {
                content: vec![OutputContent::Text { text }],
                details: None,
                is_error: false,
                terminate: false,
            })
        })
    }

    fn execution_mode(&self) -> ExecutionMode {
        ExecutionMode::Sequential
    }
}

// ---------------------------------------------------------------------------
// Recording hooks (records after_tool_call and should_stop contexts)
// ---------------------------------------------------------------------------

struct RecordingHooks {
    after_calls: Arc<Mutex<Vec<AfterToolCallContext>>>,
    stop_calls: Arc<Mutex<Vec<ShouldStopAfterTurnContext>>>,
    stop_result: bool,
}

impl RecordingHooks {
    fn new(stop_result: bool) -> Self {
        Self {
            after_calls: Arc::new(Mutex::new(Vec::new())),
            stop_calls: Arc::new(Mutex::new(Vec::new())),
            stop_result,
        }
    }
}

impl AgentHooks for RecordingHooks {
    fn convert_to_llm(
        &self,
        messages: &[AgentMessage],
    ) -> Result<Vec<opi_ai::message::Message>, AgentError> {
        let mut result = Vec::new();
        for msg in messages {
            if let AgentMessage::Llm(m) = msg {
                result.push(m.clone());
            }
        }
        Ok(result)
    }

    fn should_stop_after_turn(
        &self,
        ctx: ShouldStopAfterTurnContext,
    ) -> Pin<Box<dyn Future<Output = bool> + Send>> {
        let calls = self.stop_calls.clone();
        let stop = self.stop_result;
        Box::pin(async move {
            calls.lock().unwrap().push(ctx);
            stop
        })
    }

    fn before_tool_call(
        &self,
        _ctx: BeforeToolCallContext,
    ) -> Pin<Box<dyn Future<Output = BeforeToolCallResult> + Send>> {
        Box::pin(async { BeforeToolCallResult::Allow })
    }

    fn after_tool_call(
        &self,
        ctx: AfterToolCallContext,
    ) -> Pin<Box<dyn Future<Output = AfterToolCallResult> + Send>> {
        let calls = self.after_calls.clone();
        Box::pin(async move {
            calls.lock().unwrap().push(ctx);
            AfterToolCallResult::Keep
        })
    }
}

// ---------------------------------------------------------------------------
// Replacing hooks (returns AfterToolCallResult::Replace)
// ---------------------------------------------------------------------------

struct ReplacingHooks;

impl AgentHooks for ReplacingHooks {
    fn convert_to_llm(
        &self,
        messages: &[AgentMessage],
    ) -> Result<Vec<opi_ai::message::Message>, AgentError> {
        let mut result = Vec::new();
        for msg in messages {
            if let AgentMessage::Llm(m) = msg {
                result.push(m.clone());
            }
        }
        Ok(result)
    }

    fn should_stop_after_turn(
        &self,
        _ctx: ShouldStopAfterTurnContext,
    ) -> Pin<Box<dyn Future<Output = bool> + Send>> {
        Box::pin(async { false })
    }

    fn before_tool_call(
        &self,
        _ctx: BeforeToolCallContext,
    ) -> Pin<Box<dyn Future<Output = BeforeToolCallResult> + Send>> {
        Box::pin(async { BeforeToolCallResult::Allow })
    }

    fn after_tool_call(
        &self,
        ctx: AfterToolCallContext,
    ) -> Pin<Box<dyn Future<Output = AfterToolCallResult> + Send>> {
        let content_len = ctx.result.content.len();
        Box::pin(async move {
            AfterToolCallResult::Replace(ToolResult {
                content: vec![OutputContent::Text {
                    text: format!("replaced: {content_len}"),
                }],
                details: None,
                is_error: false,
                terminate: false,
            })
        })
    }
}

// ---------------------------------------------------------------------------
// Helpers
// ---------------------------------------------------------------------------

fn base_assistant() -> AssistantMessage {
    AssistantMessage {
        content: vec![],
        api: opi_ai::ApiKind::Anthropic,
        provider: "recording".into(),
        model: "mock-model".into(),
        response_model: None,
        response_id: None,
        usage: Usage::default(),
        stop_reason: StopReason::Stop,
        error_message: None,
        timestamp_ms: 0,
    }
}

fn text_response(text: &str) -> Vec<AssistantStreamEvent> {
    let mut partial = base_assistant();
    partial
        .content
        .push(AssistantContent::Text { text: text.into() });
    vec![
        AssistantStreamEvent::Start {
            partial: base_assistant(),
        },
        AssistantStreamEvent::TextDelta {
            content_index: 0,
            delta: text.into(),
            partial: partial.clone(),
        },
        AssistantStreamEvent::Done {
            reason: StopReason::Stop,
            message: partial,
        },
    ]
}

fn tool_call_response(call_id: &str, tool_name: &str, args: &str) -> Vec<AssistantStreamEvent> {
    let tool_call = ToolCall {
        id: call_id.into(),
        name: tool_name.into(),
        arguments: args.into(),
    };
    let mut partial = base_assistant();
    partial.content.push(AssistantContent::ToolCall {
        tool_call: tool_call.clone(),
    });
    partial.stop_reason = StopReason::ToolUse;
    vec![
        AssistantStreamEvent::Start {
            partial: base_assistant(),
        },
        AssistantStreamEvent::ToolCallEnd {
            content_index: 0,
            tool_call,
            partial: partial.clone(),
        },
        AssistantStreamEvent::Done {
            reason: StopReason::ToolUse,
            message: partial,
        },
    ]
}

fn make_agent(
    provider: RecordingProvider,
    tools: Vec<Box<dyn Tool>>,
    hooks: Box<dyn AgentHooks>,
) -> Agent {
    Agent::new(
        Box::new(provider),
        tools,
        "mock-model".into(),
        None,
        AgentLoopConfig::default(),
        hooks,
    )
}

fn user_text_in_messages(messages: &[Message], text: &str) -> bool {
    messages.iter().any(|m| match m {
        Message::User(u) => u
            .content
            .iter()
            .any(|c| matches!(c, InputContent::Text { text: t } if t == text)),
        _ => false,
    })
}

// ---------------------------------------------------------------------------
// Test 1: after_tool_call receives AfterToolCallContext
// ---------------------------------------------------------------------------

#[tokio::test]
async fn after_tool_call_receives_context() {
    let provider = RecordingProvider::new(vec![
        tool_call_response("c1", "echo", r#"{"text":"hello"}"#),
        text_response("done"),
    ]);

    let hooks = RecordingHooks::new(false);
    let after_calls = hooks.after_calls.clone();

    let mut agent = make_agent(provider, vec![Box::new(EchoTool)], Box::new(hooks));
    agent.prompt("test").await.unwrap();

    let calls = after_calls.lock().unwrap();
    assert_eq!(calls.len(), 1, "after_tool_call should be called once");
    assert_eq!(calls[0].tool_call_id, "c1");
    assert_eq!(calls[0].tool_name, "echo");
    assert!(!calls[0].result.is_error);
}

// ---------------------------------------------------------------------------
// Test 2: after_tool_call Replace modifies tool result
// ---------------------------------------------------------------------------

#[tokio::test]
async fn after_tool_call_replace_result() {
    let provider = RecordingProvider::new(vec![
        tool_call_response("c1", "echo", r#"{"text":"hello"}"#),
        text_response("done"),
    ]);

    let mut agent = make_agent(provider, vec![Box::new(EchoTool)], Box::new(ReplacingHooks));
    let result = agent.prompt("test").await.unwrap();

    let tool_result = result
        .iter()
        .find_map(|m| match m {
            AgentMessage::Llm(Message::ToolResult(tr)) => Some(tr.clone()),
            _ => None,
        })
        .expect("should have a tool result");

    match &tool_result.content[0] {
        OutputContent::Text { text } => assert_eq!(text, "replaced: 1"),
        _ => panic!("expected text content"),
    }
}

// ---------------------------------------------------------------------------
// Test 3: should_stop_after_turn receives ShouldStopAfterTurnContext
// ---------------------------------------------------------------------------

#[tokio::test]
async fn should_stop_receives_context() {
    let provider = RecordingProvider::new(vec![text_response("hello")]);

    let hooks = RecordingHooks::new(false);
    let stop_calls = hooks.stop_calls.clone();

    let mut agent = make_agent(provider, vec![], Box::new(hooks));
    agent.prompt("test").await.unwrap();

    let calls = stop_calls.lock().unwrap();
    assert!(!calls.is_empty(), "should_stop_after_turn should be called");
    assert!(
        !calls[0].messages.is_empty(),
        "context should have messages"
    );
}

// ---------------------------------------------------------------------------
// Test 4: steering queue delivers before next request
// ---------------------------------------------------------------------------

#[tokio::test]
async fn steering_queue_delivered_before_next_request() {
    let provider = RecordingProvider::new(vec![
        tool_call_response("c1", "echo", r#"{"text":"hello"}"#),
        text_response("done"),
    ]);
    let received = provider.received_messages.clone();

    let hooks = RecordingHooks::new(false);

    let mut agent = make_agent(provider, vec![Box::new(EchoTool)], Box::new(hooks));
    agent.steer("focus on quality".into());
    agent.prompt("test").await.unwrap();

    let msgs = received.lock().unwrap();
    assert_eq!(msgs.len(), 2, "provider should be called twice");
    assert!(
        user_text_in_messages(&msgs[1], "focus on quality"),
        "second provider call should include steering message"
    );
}

// ---------------------------------------------------------------------------
// Test 5: follow-up queue delivers when agent would stop
// ---------------------------------------------------------------------------

#[tokio::test]
async fn follow_up_queue_delivered_when_would_stop() {
    let provider = RecordingProvider::new(vec![text_response("hello"), text_response("more")]);
    let received = provider.received_messages.clone();

    let hooks = RecordingHooks::new(false);

    let mut agent = make_agent(provider, vec![], Box::new(hooks));
    agent.follow_up("tell me more".into());
    agent.prompt("test").await.unwrap();

    let msgs = received.lock().unwrap();
    assert_eq!(msgs.len(), 2, "provider should be called twice");
    assert!(
        user_text_in_messages(&msgs[1], "tell me more"),
        "second provider call should include follow-up message"
    );
}

// ---------------------------------------------------------------------------
// Test 6: should_stop true prevents queue polling
// ---------------------------------------------------------------------------

#[tokio::test]
async fn should_stop_prevents_queue_polling() {
    let provider = RecordingProvider::new(vec![tool_call_response(
        "c1",
        "echo",
        r#"{"text":"hello"}"#,
    )]);
    let received = provider.received_messages.clone();

    let hooks = RecordingHooks::new(true);

    let mut agent = make_agent(provider, vec![Box::new(EchoTool)], Box::new(hooks));
    agent.steer("should not be delivered".into());
    agent.prompt("test").await.unwrap();

    let msgs = received.lock().unwrap();
    assert_eq!(msgs.len(), 1, "provider should only be called once");
}

// ---------------------------------------------------------------------------
// Test 7: QueueUpdate event emitted
// ---------------------------------------------------------------------------

#[tokio::test]
async fn queue_update_event_emitted() {
    let provider = RecordingProvider::new(vec![
        tool_call_response("c1", "echo", r#"{"text":"hello"}"#),
        text_response("done"),
    ]);

    let hooks = RecordingHooks::new(false);
    type QueueData = (Vec<String>, Vec<String>);
    let queue_events: Arc<Mutex<Vec<QueueData>>> = Arc::new(Mutex::new(Vec::new()));
    let queue_events_clone = queue_events.clone();

    let mut agent = make_agent(provider, vec![Box::new(EchoTool)], Box::new(hooks));
    agent.steer("redirect".into());
    agent.subscribe(Box::new(move |e| {
        if let AgentEvent::QueueUpdate {
            steering,
            follow_up,
        } = e
        {
            queue_events_clone
                .lock()
                .unwrap()
                .push((steering.clone(), follow_up.clone()));
        }
    }));
    agent.prompt("test").await.unwrap();

    let updates = queue_events.lock().unwrap();
    assert!(!updates.is_empty(), "should emit QueueUpdate event");
    assert!(
        updates[0].0.contains(&"redirect".to_owned()),
        "event should contain steering message"
    );
}