Skip to main content

MockChatAdapter

Struct MockChatAdapter 

Source
pub struct MockChatAdapter { /* private fields */ }
Expand description

A deterministic ChatAdapter: script the model’s turns, then assert on what it was asked.

let adapter = MockChatAdapter::new();
adapter.call_tool("read_order", serde_json::json!({ "id": 7 }));
adapter.reply("Your order shipped.");

One queued turn is consumed per round-trip. A tool-call turn therefore needs another turn queued behind it, because the chat loop calls the adapter again after running the tool — the mistake is common enough that the exhaustion error says so.

Scripting takes &self so a test can queue turns after handing the adapter to a harness.

Implementations§

Source§

impl MockChatAdapter

Source

pub fn new() -> Self

Source

pub fn arc() -> Arc<Self>

Source

pub fn with_fallback_reply(self, text: impl Into<String>) -> Self

Answer anything unscripted with text instead of failing — for tests that care about one turn and not about the rest.

Off by default: a silent stand-in reply turns “the test didn’t script enough” into a confusing assertion failure further down.

Source

pub fn reply(&self, text: impl Into<String>) -> &Self

Queue a turn where the model answers with plain text.

Source

pub fn call_tool( &self, name: impl Into<String>, arguments: impl ToArguments, ) -> &Self

Queue a turn where the model calls a tool. The tool itself runs for real — this only scripts the model’s decision to call it.

Source

pub fn enqueue(&self, turn: ScriptedTurn) -> &Self

Queue a raw turn.

Source

pub fn respond_with( &self, responder: impl Fn(&ChatAdapterRequest) -> Vec<ChatItem> + Send + Sync + 'static, ) -> &Self

Queue a turn computed from the request — for asserting on what the model was given and branching on it in one place.

Source

pub fn requests(&self) -> Vec<RecordedRequest>

Every request the adapter received, oldest first.

Source

pub fn last_request(&self) -> Option<RecordedRequest>

Source

pub fn call_count(&self) -> usize

How many round-trips happened — the cheapest check that a tool loop ran as many times as expected, and no more.

Source

pub fn pending(&self) -> usize

Turns still queued. A test that ends with this above zero scripted more than it exercised, which usually means the conversation took a different path than it assumed.

Trait Implementations§

Source§

impl ChatAdapter for MockChatAdapter

Source§

fn next_items<'life0, 'async_trait>( &'life0 self, request: ChatAdapterRequest, ) -> Pin<Box<dyn Future<Output = Result<ChatAdapterResponse, ChatAdapterError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Source§

impl Default for MockChatAdapter

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<A, B, T> HttpServerConnExec<A, B> for T
where B: Body,

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more