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
impl ScriptedProvider
Sourcepub fn builder(
provider: impl Into<ProviderKey>,
model: impl Into<ModelKey>,
) -> ScriptedProviderBuilder
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.
Sourcepub fn narrating(text: impl Into<String>) -> Self
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.
Sourcepub fn profile_ref(&self) -> &ModelProfile
pub fn profile_ref(&self) -> &ModelProfile
The routing profile, as configured.
Sourcepub fn candidate(provider: Arc<Self>) -> ProviderCandidate
pub fn candidate(provider: Arc<Self>) -> ProviderCandidate
Wraps a shared provider as a healthy routing candidate.
Sourcepub fn calls(&self) -> Vec<RecordedCall>
pub fn calls(&self) -> Vec<RecordedCall>
Every request received, in order.
Sourcepub fn call_count(&self) -> usize
pub fn call_count(&self) -> usize
How many requests were received.
Sourcepub fn nth_call(&self, index: usize) -> Option<RecordedCall>
pub fn nth_call(&self, index: usize) -> Option<RecordedCall>
The index-th request received.
Sourcepub fn last_call(&self) -> Option<RecordedCall>
pub fn last_call(&self) -> Option<RecordedCall>
The most recent request received.
Sourcepub fn calls_for(&self, purpose: ModelPurpose) -> Vec<RecordedCall>
pub fn calls_for(&self, purpose: ModelPurpose) -> Vec<RecordedCall>
Every request made for one purpose, in order.
Sourcepub fn remaining_steps(&self) -> usize
pub fn remaining_steps(&self) -> usize
How many steps are still unused.
Sourcepub fn violations(&self) -> Vec<ScriptViolation>
pub fn violations(&self) -> Vec<ScriptViolation>
Every violation the script observed, in order.
Sourcepub fn verify(&self) -> Result<(), ScriptViolation>
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.
Sourcepub fn push(&self, step: ScriptStep)
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.
Sourcepub fn clear_calls(&self)
pub fn clear_calls(&self)
Forgets the recorded calls and violations, keeping the remaining script.
Trait Implementations§
Source§impl Debug for ScriptedProvider
impl Debug for ScriptedProvider
Source§impl ModelProvider for ScriptedProvider
impl ModelProvider for ScriptedProvider
Source§fn provider_key(&self) -> ProviderKey
fn provider_key(&self) -> ProviderKey
"openai". Labels every metric,
attempt record and replay entry this provider produces.Source§fn capabilities(&self) -> ProviderCapabilities
fn capabilities(&self) -> ProviderCapabilities
Source§fn profile(&self) -> ModelProfile
fn profile(&self) -> ModelProfile
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,
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,
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,
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,
Source§fn reference(&self) -> ModelRef
fn reference(&self) -> ModelRef
Source§fn supports(
&self,
requirements: &CapabilityRequirements,
) -> Result<(), CapabilityMismatch>
fn supports( &self, requirements: &CapabilityRequirements, ) -> Result<(), CapabilityMismatch>
requirements. Read more