Skip to main content

ToolCallExtractor

Struct ToolCallExtractor 

Source
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>

Source

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.

Source

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.

Source

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> 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<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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<Unshared, Shared> IntoShared<Shared> for Unshared
where Shared: FromUnshared<Unshared>,

Source§

fn into_shared(self) -> Shared

Creates a shared type from an unshared type.
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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 = !

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<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