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 theTOOL_CALL_CHUNKthat 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_SNAPSHOTor theRUN_STARTEDecho.
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§
Trait Implementations§
Source§impl Clone for FilterToolCalls
impl Clone for FilterToolCalls
Source§impl Debug for FilterToolCalls
impl Debug for FilterToolCalls
Source§impl StreamTransformer for FilterToolCalls
impl StreamTransformer for FilterToolCalls
Source§fn seed(&mut self, history: &[Message])
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])
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.