Skip to main content

ScriptedProvider

Struct ScriptedProvider 

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

A ModelProvider that answers from a script and refuses to improvise.

Steps are consumed in order. When the script runs out, the provider does not fall back to a default answer: it records a ScriptViolation::UnexpectedCall and fails the call with UNEXPECTED_CALL_CODE, so a runtime that calls a model one more time than the test believed cannot pass silently.

use turnframe_provider::prelude::*;
use turnframe_test::providers::{ScriptedProvider, ScriptedReply};

let provider = ScriptedProvider::builder("fake", "m")
    .reply(ScriptedReply::text("una frase"))
    .build();

let request = ModelRequest::new(ModelPurpose::Acknowledge).with_message(Message::user("ciao"));
assert_eq!(provider.generate(request.clone()).await.unwrap().text(), "una frase");

// The second call was never scripted.
assert!(provider.generate(request).await.is_err());
assert!(provider.verify().is_err());

Implementations§

Source§

impl ScriptedProvider

Source

pub fn builder( provider: impl Into<ProviderKey>, model: impl Into<ModelKey>, ) -> ScriptedProviderBuilder

A builder for a provider that declares native JSON Schema output, parallel tool calling and no streaming.

Those are the capabilities a mutation-capable interpretation stage requires (spec §20.4), which is what most tests need; call capabilities to say something else.

Source

pub fn narrating(text: impl Into<String>) -> Self

The one-line common case: a provider that acknowledges one turn with text, passes its review, and refuses everything else.

Source

pub fn profile_ref(&self) -> &ModelProfile

The routing profile, as configured.

Source

pub fn candidate(provider: Arc<Self>) -> ProviderCandidate

Wraps a shared provider as a healthy routing candidate.

Source

pub fn calls(&self) -> Vec<RecordedCall>

Every request received, in order.

Source

pub fn call_count(&self) -> usize

How many requests were received.

Source

pub fn nth_call(&self, index: usize) -> Option<RecordedCall>

The index-th request received.

Source

pub fn last_call(&self) -> Option<RecordedCall>

The most recent request received.

Source

pub fn calls_for(&self, purpose: ModelPurpose) -> Vec<RecordedCall>

Every request made for one purpose, in order.

Source

pub fn remaining_steps(&self) -> usize

How many steps are still unused.

Source

pub fn violations(&self) -> Vec<ScriptViolation>

Every violation the script observed, in order.

Source

pub fn verify(&self) -> Result<(), ScriptViolation>

Checks that the script was followed exactly: no unanticipated call and no unused step.

A call the script did not anticipate already failed at the time it was made; this is how the test learns about it even when the code under test swallowed the error.

§Errors

The first ScriptViolation observed, or ScriptViolation::StepsUnused when steps remain.

Source

pub fn push(&self, step: ScriptStep)

Appends a step to a provider already in use, for a turn whose script depends on identifiers an earlier turn produced.

Source

pub fn clear_calls(&self)

Forgets the recorded calls and violations, keeping the remaining script.

Trait Implementations§

Source§

impl Debug for ScriptedProvider

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl ModelProvider for ScriptedProvider

Source§

fn provider_key(&self) -> ProviderKey

The configured provider key, e.g. "openai". Labels every metric, attempt record and replay entry this provider produces.
Source§

fn model_key(&self) -> ModelKey

The configured model key, e.g. "gpt-4o-2024-08-06".
Source§

fn capabilities(&self) -> ProviderCapabilities

What this provider-model pair can actually do (spec §20.3). Read more
Source§

fn profile(&self) -> ModelProfile

The full routing profile: keys, capabilities, and the cost, region and tags routing filters on. Read more
Source§

fn generate<'life0, 'async_trait>( &'life0 self, request: ModelRequest, ) -> Pin<Box<dyn Future<Output = Result<ModelResponse, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Runs one model call and returns the whole answer. Read more
Source§

fn stream<'life0, 'async_trait>( &'life0 self, request: ModelRequest, ) -> Pin<Box<dyn Future<Output = Result<ModelStream, ProviderError>> + Send + 'async_trait>>
where Self: 'async_trait, 'life0: 'async_trait,

Runs one model call and returns its answer incrementally. Read more
Source§

fn reference(&self) -> ModelRef

The provider-model pair, as routing and health tracking key it.
Source§

fn supports( &self, requirements: &CapabilityRequirements, ) -> Result<(), CapabilityMismatch>

Checks the declared capabilities against requirements. 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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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