Skip to main content

graphrt_llm/
mock.rs

1//! Scripted model for tests and offline examples.
2
3use std::collections::VecDeque;
4use std::sync::{Mutex, MutexGuard};
5
6use futures::future::BoxFuture;
7use graphrt_core::event::Usage;
8
9use crate::error::LlmError;
10use crate::types::{ChatDelta, ChatModel, ChatRequest, ChatStream, FinishReason};
11
12/// A model that replays scripted responses in order and records every request.
13#[derive(Debug, Default)]
14pub struct MockModel {
15    script: Mutex<VecDeque<Result<Vec<ChatDelta>, LlmError>>>,
16    requests: Mutex<Vec<ChatRequest>>,
17}
18
19fn lock<T>(m: &Mutex<T>) -> MutexGuard<'_, T> {
20    m.lock().unwrap_or_else(std::sync::PoisonError::into_inner)
21}
22
23impl MockModel {
24    /// One entry per expected `chat` call.
25    pub fn new(responses: Vec<Vec<ChatDelta>>) -> Self {
26        Self { script: Mutex::new(responses.into_iter().map(Ok).collect()), requests: Mutex::default() }
27    }
28
29    /// Script a response consisting of plain text.
30    pub fn text(text: &str) -> Vec<ChatDelta> {
31        vec![
32            ChatDelta::Text(text.to_owned()),
33            ChatDelta::Usage(Usage { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }),
34            ChatDelta::Finish(FinishReason::Stop),
35        ]
36    }
37
38    /// Script a response containing tool calls `(id, name, arguments)`.
39    pub fn tool_calls(calls: &[(&str, &str, &str)]) -> Vec<ChatDelta> {
40        let mut out: Vec<ChatDelta> = calls
41            .iter()
42            .enumerate()
43            .map(|(index, (id, name, args))| ChatDelta::ToolCall {
44                index,
45                id: Some((*id).to_owned()),
46                name: Some((*name).to_owned()),
47                arguments: (*args).to_owned(),
48            })
49            .collect();
50        out.push(ChatDelta::Usage(Usage { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 }));
51        out.push(ChatDelta::Finish(FinishReason::ToolCalls));
52        out
53    }
54
55    /// Append a scripted response.
56    pub fn push(&self, deltas: Vec<ChatDelta>) {
57        lock(&self.script).push_back(Ok(deltas));
58    }
59
60    /// Append a scripted error.
61    pub fn push_error(&self, error: LlmError) {
62        lock(&self.script).push_back(Err(error));
63    }
64
65    /// Requests received so far.
66    pub fn requests(&self) -> Vec<ChatRequest> {
67        lock(&self.requests).clone()
68    }
69}
70
71impl ChatModel for MockModel {
72    fn chat(&self, request: ChatRequest) -> BoxFuture<'_, Result<ChatStream, LlmError>> {
73        lock(&self.requests).push(request);
74        let next = lock(&self.script).pop_front();
75        Box::pin(async move {
76            let deltas = next.unwrap_or_else(|| Err(LlmError::Config("mock model script exhausted".into())))?;
77            let stream: ChatStream = Box::pin(futures::stream::iter(deltas.into_iter().map(Ok)));
78            Ok(stream)
79        })
80    }
81}