pub enum ToolResult {
Success {
text: String,
telemetry: Option<Box<ToolTelemetry>>,
},
Error {
message: String,
recoverable: bool,
},
}Expand description
Structured result of a tool execution.
Why: Hard-failing the LLM loop on every tool error is brittle — the model
often can recover (retry with different args, fall back to another tool, or
explain the failure in its final answer). A structured error lets the caller
surface failure back to the LLM as a tool_result with is_error: true
while keeping the loop running.
What: Success carries a successful textual result plus (UI Phase 1) an
optional ToolTelemetry side-channel; Error carries a message plus a
recoverable flag.
Test: ToolResult::err(...).is_error() is true; ok(...).content() returns
the success string. Exercised in tools::registry and delegate tests.
Variants§
Success
Tool executed successfully.
text is the textual result fed back to the model. telemetry
(UI Phase 1) is the structured account of what the tool actually did,
which the agent loop forwards to its ToolEventSink — see
crate::tools::telemetry for why this rides on the result rather than
each tool holding a sink. None for every tool that has nothing
structured to report, which is all but the retrieval tools.
Error
Tool failed; message describes why; recoverable advises the LLM loop.
Implementations§
Source§impl ToolResult
impl ToolResult
Sourcepub fn ok(s: impl Into<String>) -> Self
pub fn ok(s: impl Into<String>) -> Self
Construct a successful result with no telemetry.
Why: Single canonical happy-path constructor used by every tool. Tools
with structured telemetry chain Self::with_telemetry onto this
rather than needing a second constructor.
What: Wraps s in Success with telemetry: None.
Test: Trivially exercised by every successful tool execute().
Sourcepub fn with_telemetry(self, telemetry: ToolTelemetry) -> Self
pub fn with_telemetry(self, telemetry: ToolTelemetry) -> Self
Attach structured telemetry to a successful result (UI Phase 1).
Why: lets a tool report what it actually did (real search lane, which
memories entered context) without knowing which agent runs it or
owning an event sink — see crate::tools::telemetry’s module docs.
What: builder-style; a no-op on an Error result, because the event
shapes this feeds describe work that produced results, and an errored
tool has none to describe — its failure is already narrated by
ToolFinished/ToolError.
Test: tests::tool_result_carries_optional_telemetry,
tests::with_telemetry_is_a_no_op_on_error.
Sourcepub fn telemetry(&self) -> Option<&ToolTelemetry>
pub fn telemetry(&self) -> Option<&ToolTelemetry>
The structured telemetry this result carries, if any (UI Phase 1).
Why: the agent loop reads this after every dispatch to decide whether
to emit a structured event alongside the generic tool events.
What: None for errors and for any tool that reported none.
Test: tests::tool_result_carries_optional_telemetry.
Sourcepub fn err(msg: impl Into<String>) -> Self
pub fn err(msg: impl Into<String>) -> Self
Construct a recoverable error.
Why: Most tool failures are non-fatal — wrong arg, transient network,
empty result. The LLM should see the error and decide.
What: Wraps msg with recoverable = true.
Test: Exercised by delegate and registry tests.
Sourcepub fn fatal(msg: impl Into<String>) -> Self
pub fn fatal(msg: impl Into<String>) -> Self
Construct a fatal (non-recoverable) error.
Why: Some failures (invariant violations, credential rejection) should
not be retried; callers should bail.
What: Wraps msg with recoverable = false.
Test: Checked by is_fatal predicates in tests.
Sourcepub fn is_error(&self) -> bool
pub fn is_error(&self) -> bool
Whether this result is an error variant.
Why: Dispatch paths need a cheap predicate to log/branch on failure.
What: Returns true for any Error, false for Success.
Test: ToolResult::err("x").is_error() is true.
Sourcepub fn is_fatal(&self) -> bool
pub fn is_fatal(&self) -> bool
Whether this error is fatal (not recoverable). false for Success.
Why: Callers that distinguish fatal-vs-recoverable need this to decide
whether to retry or bail.
What: True only for Error { recoverable: false, .. }.
Test: ToolResult::fatal("x").is_fatal() is true.
Sourcepub fn content(&self) -> &str
pub fn content(&self) -> &str
Extract the message string for both variants.
Why: Callers like the LLM loop need the raw string regardless of
success/failure to pass back as tool_result content.
What: Returns the payload string for both Success and Error.
Test: ToolResult::ok("hi").content() == “hi”;
ToolResult::err("oops").content() == “oops”.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ToolResult
impl RefUnwindSafe for ToolResult
impl Send for ToolResult
impl Sync for ToolResult
impl Unpin for ToolResult
impl UnsafeUnpin for ToolResult
impl UnwindSafe for ToolResult
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