Skip to main content

adk_agent/codeact/
error_map.rs

1//! Error-message helpers for the strings raised into scripts and fed back to
2//! the model.
3//!
4//! The framework is language-agnostic, so these are just message strings. When
5//! a tool fails or is denied, the driver raises an error *into* the script via
6//! [`ResumeWith::Raise`](crate::codeact::ResumeWith); the runtime represents it
7//! however its language does (a Python exception, a JS throw, etc.).
8
9use adk_core::AdkError;
10use serde_json::Value;
11
12/// Message raised into the script when a tool fails.
13pub fn tool_error_message(err: &AdkError) -> String {
14    format!("{}: {}", err.category, err.message)
15}
16
17/// Message raised when the script calls a tool that is not registered.
18pub fn unknown_tool_message(tool_name: &str) -> String {
19    format!("tool '{tool_name}' is not available")
20}
21
22/// Message raised when a human denies a confirmation-gated tool call.
23pub fn denied_message(tool_name: &str) -> String {
24    format!("call to '{tool_name}' was denied by a human reviewer")
25}
26
27/// Render a tool result for inclusion in an observation fed back to the model.
28pub(crate) fn render_value(value: &Value) -> String {
29    serde_json::to_string(value).unwrap_or_else(|_| value.to_string())
30}
31
32#[cfg(test)]
33mod tests {
34    use super::*;
35    use adk_core::{ErrorCategory, ErrorComponent};
36
37    #[test]
38    fn tool_error_includes_category_and_message() {
39        let err = AdkError::new(ErrorComponent::Tool, ErrorCategory::NotFound, "t", "no file");
40        let msg = tool_error_message(&err);
41        assert!(msg.contains("not_found"));
42        assert!(msg.contains("no file"));
43    }
44
45    #[test]
46    fn helpers_name_the_tool() {
47        assert!(unknown_tool_message("foo").contains("foo"));
48        assert!(denied_message("rm").contains("rm"));
49    }
50}