pub struct ToolCallExtractor<'a> { /* private fields */ }Expand description
Recovers and validates tool calls across every supported provider convention.
Why: Bundles the schema-lookup callback so a single instance can be reused
across every attempt of a repair::extract_with_repair loop without
re-threading the tool registry through each call.
What: Holds a boxed closure from tool name to that tool’s raw
OpenAI-function schema Value (see tools::traits::ToolExecutor::schema;
callers typically pass a closure over ToolRegistry::schemas() results
keyed by name). Self::extract is the single-attempt entry point.
Test: tests::*.
Implementations§
Source§impl<'a> ToolCallExtractor<'a>
impl<'a> ToolCallExtractor<'a>
Sourcepub fn new(
schema_lookup: impl Fn(&str) -> Option<Value> + Send + Sync + 'a,
) -> Self
pub fn new( schema_lookup: impl Fn(&str) -> Option<Value> + Send + Sync + 'a, ) -> Self
Build an extractor bound to a schema-lookup callback.
Why: Decouples this module from tools::ToolRegistry directly, so it
can be unit-tested with a plain closure/HashMap lookup.
What: Stores schema_lookup; each tool’s schema is expected to be the
full {"type":"function","function":{"name","parameters",...}}
envelope — Self::extract reads .function.parameters out of it.
Test: tests::* all construct via this constructor.
Sourcepub fn extract(
&self,
response: &ChatResponse,
model_slug: &str,
) -> Result<Vec<ExtractedToolCall>, ToolCallExtractError>
pub fn extract( &self, response: &ChatResponse, model_slug: &str, ) -> Result<Vec<ExtractedToolCall>, ToolCallExtractError>
Attempt one extraction across native + fallback strategies, then validate.
Why: This is the “single attempt” the bounded repair loop iterates:
try native first (authoritative when present), else each fallback
strategy for model_slug in order, then validate whatever candidate
was found against its tool’s schema.
What: If response.first_tool_calls() is non-empty, parses and
validates each as ExtractionStrategy::Native, returning the first
failure encountered (or all calls on full success). Otherwise scans
response.first_text() via strategy_order_for’s fallback order,
stopping at the first strategy that recovers a candidate — even if
that candidate then fails validation, later strategies are NOT tried
(the model committed to a shape; the repair loop, not a different
parse strategy, is the correct next step).
Test: tests::extract_native_success, tests::extract_fenced_json_success,
tests::extract_angle_bracket_success, tests::extract_no_call_found,
tests::extract_native_malformed_arguments,
tests::extract_schema_invalid_arguments, tests::extract_unknown_tool.
Sourcepub fn parse_and_validate(
&self,
name: &str,
raw_arguments: &str,
) -> Result<Value, ToolCallExtractError>
pub fn parse_and_validate( &self, name: &str, raw_arguments: &str, ) -> Result<Value, ToolCallExtractError>
Parse a raw JSON arguments string and validate it against name’s schema.
Why: This is the exact per-call operation Self::extract’s native
branch needs, factored out so a caller that already has a single known
(name, raw_arguments) pair — notably agent_loop::dispatch_all,
which dispatches native ToolCalls one at a time — can validate
without constructing a ChatResponse. This is the #1023 replacement
for the pre-#1023 agent_loop::parse_args, which silently degraded
any unparseable arguments string to {}; a genuine JSON syntax
error now surfaces as a structured, actionable
ToolCallExtractError::MalformedArguments instead.
What: An empty or all-whitespace raw_arguments is treated as {}
(many providers send an empty string for a zero-argument call — this
preserves that legacy leniency) and still runs through schema
validation, so a tool with required arguments correctly reports
SchemaInvalid rather than silently succeeding. A non-empty string
that fails to parse reports MalformedArguments. Returns the
validated arguments value on success.
Test: tests::parse_and_validate_success,
tests::parse_and_validate_empty_string_becomes_empty_object,
tests::parse_and_validate_malformed_json.
Auto Trait Implementations§
impl<'a> !RefUnwindSafe for ToolCallExtractor<'a>
impl<'a> !UnwindSafe for ToolCallExtractor<'a>
impl<'a> Freeze for ToolCallExtractor<'a>
impl<'a> Send for ToolCallExtractor<'a>
impl<'a> Sync for ToolCallExtractor<'a>
impl<'a> Unpin for ToolCallExtractor<'a>
impl<'a> UnsafeUnpin for ToolCallExtractor<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more