Skip to main content

FilterToolCalls

Struct FilterToolCalls 

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

Drops whole tool calls by tool name.

Both halves of a filtered call are removed — TOOL_CALL_START, its TOOL_CALL_ARGS or TOOL_CALL_CHUNKs, the TOOL_CALL_END, the TOOL_CALL_RESULT and the REASONING_ENCRYPTED_VALUE attached to the call — and so is the call where a conversation is restated: in a MESSAGES_SNAPSHOT and in the request echoed on RUN_STARTED.

That is what the filter covers. It does not look into RAW or CUSTOM events, into the rawEvent payload of an event it sends, or into an interrupt that names a filtered call’s id. Keep the tool out of those where they are made.

let mut filter = FilterToolCalls::deny(["secret_tool"]);
assert!(filter.transform(Event::tool_call_start("c1", "secret_tool")).is_empty());
assert!(filter.transform(Event::tool_call_args("c1", "{}")).is_empty());
assert_eq!(filter.transform(Event::tool_call_start("c2", "public")).len(), 1);

§A conversation the stream restates

A MESSAGES_SNAPSHOT, and the request echoed on RUN_STARTED, carry tool calls too, and are filtered by the same names. A filtered call is taken out of the assistant message that made it, with the encrypted value it carries, and the tool message that answers it is taken out of the list. So is:

  • an assistant message that is left with neither content nor calls. It was there to carry the call, and a client that read the run’s events never held it: the events that would have made it were dropped. Whatever else it carried goes with it;
  • a tool message that answers a call nothing named. It has no tool name of its own to judge it by.

The calls a restated conversation carries are judged as they are met, like a TOOL_CALL_START, so a result that follows one goes where the call went.

let restated = Event::messages_snapshot(vec![
    Message::Assistant(AssistantMessage {
        id: "msg-1".into(),
        content: Some("Looking.".into()),
        tool_calls: Some(vec![
            ToolCall::new("c1", "search", "{}"),
            ToolCall::new("c2", "secret_tool", "{}"),
        ]),
        ..Default::default()
    }),
    Message::tool("msg-2", "c2", "the result"),
]);

let mut filter = FilterToolCalls::deny(["secret_tool"]);
let Some(Event::MessagesSnapshot(snapshot)) = filter.transform(restated).pop() else {
    unreachable!("the snapshot is kept");
};
let [Message::Assistant(assistant)] = snapshot.messages.as_slice() else {
    unreachable!("the tool message went with its call");
};
assert_eq!(assistant.tool_calls.as_ref().unwrap().len(), 1);

§How a call is named

A call is judged by the name of its tool, and only what opens a call names it. The filter learns the names from four places:

  • this run’s events: a TOOL_CALL_START, or the TOOL_CALL_CHUNK that opens a call;
  • the request’s messages (seed), for a result a resumed run reports;
  • in a run that another driver began, the events already sent (replay), for a call that driver left open;
  • a conversation restated in a MESSAGES_SNAPSHOT or the RUN_STARTED echo.

An event of a call none of these named is dropped, and the emit that produced it returns Ok: arguments, an end, a result, an encrypted value, a chunk that names no tool. The filter cannot tell such a call from one that was filtered where this filter could not see it: by the filter of an earlier run, so that the client never held the call and the request’s messages do not either, or by the filter of the earlier driver of this run, so that the events sent do not. The result a resumed run reports for such a call, and the end a continuation gives it, go the way the call went.

In a run that another driver began, the filter does not take up the chunk streams the events sent left open. The earlier driver may have been streaming a call its filter removed in the same lane, and the events sent do not show it, so a chunk that names no id cannot be told from one meant for that call. A continuation that keeps streaming a call by chunks names the call’s id on its first chunk, and the chunks after it continue that call, as within any single run. A chunk that names no id before that is dropped.

Implementations§

Source§

impl FilterToolCalls

Source

pub fn allow<I, T>(names: I) -> Self
where I: IntoIterator<Item = T>, T: Into<String>,

Passes only calls to the named tools.

Source

pub fn deny<I, T>(names: I) -> Self
where I: IntoIterator<Item = T>, T: Into<String>,

Passes everything except calls to the named tools.

Trait Implementations§

Source§

impl Clone for FilterToolCalls

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for FilterToolCalls

Source§

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

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

impl StreamTransformer for FilterToolCalls

Source§

fn seed(&mut self, history: &[Message])

A call an earlier run made is judged by its name like a new one, so the result a resumed run reports for it goes the way the call went.

Source§

fn replay(&mut self, sent: &[Event])

A call the earlier driver of the run opened is judged by its name like a new one, so a continuation can finish and answer it.

The chunk streams the events sent left open are not taken up: what was sent is not what the earlier producer had open. A chunk that names no id is dropped until a chunk of this driver names the call it continues.

Source§

fn transform(&mut self, event: Event) -> Vec<Event>

Rewrites one event into zero or more events.

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DynClone for T
where T: Clone,

Source§

fn __clone_box(&self, _: Private) -> *mut ()

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> FromRef<T> for T
where T: Clone,

§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
§

impl<T> Instrument for T

§

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

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

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.

§

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

§

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
§

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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.
§

impl<T> WithSubscriber for T

§

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
§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

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