Skip to main content

gproxy_protocol/protocol/claude/common/server_tools/
errors.rs

1use serde::{Deserialize, Serialize};
2
3use super::super::JsonObject;
4
5#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, gproxy_protocol_macros::WireBuilder)]
6#[non_exhaustive]
7pub struct ServerToolResultError<C, T> {
8    pub error_code: C,
9    #[serde(rename = "type")]
10    pub type_: T,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    pub error_message: Option<String>,
13    #[serde(default, flatten, skip_serializing_if = "JsonObject::is_empty")]
14    pub extra: JsonObject,
15}
16
17macro_rules! error_code {
18    ($outer:ident, $known:ident { $($variant:ident => $wire:literal),+ $(,)? }) => {
19        #[derive(Debug, Clone, PartialEq, Eq, Serialize)]
20        #[serde(untagged)]
21        #[non_exhaustive]
22        pub enum $outer {
23            Known($known),
24            Unknown(String),
25        }
26
27        // Manual Deserialize: a known-variant miss falls back to `Unknown`
28        // without ever formatting an unknown-variant error (see
29        // `protocol::extensible`).
30        impl<'de> serde::Deserialize<'de> for $outer {
31            fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
32                crate::protocol::extensible::deserialize_extensible(d, Self::Known, Self::Unknown)
33            }
34        }
35
36        #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
37        #[non_exhaustive]
38        pub enum $known {
39            $(#[serde(rename = $wire)] $variant,)+
40        }
41    };
42}
43
44macro_rules! error_type {
45    ($name:ident { $variant:ident => $wire:literal }) => {
46        #[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
47        #[non_exhaustive]
48        pub enum $name {
49            #[serde(rename = $wire)]
50            $variant,
51        }
52    };
53}
54
55error_code!(WebSearchToolResultErrorCode, WebSearchToolResultErrorCodeKnown {
56    InvalidToolInput => "invalid_tool_input",
57    Unavailable => "unavailable",
58    MaxUsesExceeded => "max_uses_exceeded",
59    TooManyRequests => "too_many_requests",
60    QueryTooLong => "query_too_long",
61    RequestTooLarge => "request_too_large",
62});
63error_type!(WebSearchToolResultErrorType {
64    WebSearchToolResultError => "web_search_tool_result_error"
65});
66pub type WebSearchToolResultError =
67    ServerToolResultError<WebSearchToolResultErrorCode, WebSearchToolResultErrorType>;
68
69error_code!(WebFetchToolResultErrorCode, WebFetchToolResultErrorCodeKnown {
70    InvalidToolInput => "invalid_tool_input",
71    UrlTooLong => "url_too_long",
72    UrlNotAllowed => "url_not_allowed",
73    UrlNotInPriorContext => "url_not_in_prior_context",
74    UrlNotAccessible => "url_not_accessible",
75    UnsupportedContentType => "unsupported_content_type",
76    TooManyRequests => "too_many_requests",
77    MaxUsesExceeded => "max_uses_exceeded",
78    Unavailable => "unavailable",
79});
80error_type!(WebFetchToolResultErrorType {
81    WebFetchToolResultError => "web_fetch_tool_result_error"
82});
83pub type WebFetchToolResultError =
84    ServerToolResultError<WebFetchToolResultErrorCode, WebFetchToolResultErrorType>;
85
86error_code!(AdvisorToolResultErrorCode, AdvisorToolResultErrorCodeKnown {
87    MaxUsesExceeded => "max_uses_exceeded",
88    PromptTooLong => "prompt_too_long",
89    TooManyRequests => "too_many_requests",
90    Overloaded => "overloaded",
91    Unavailable => "unavailable",
92    ExecutionTimeExceeded => "execution_time_exceeded",
93});
94error_type!(AdvisorToolResultErrorType {
95    AdvisorToolResultError => "advisor_tool_result_error"
96});
97pub type AdvisorToolResultError =
98    ServerToolResultError<AdvisorToolResultErrorCode, AdvisorToolResultErrorType>;
99
100error_code!(CodeExecutionToolResultErrorCode, CodeExecutionToolResultErrorCodeKnown {
101    InvalidToolInput => "invalid_tool_input",
102    Unavailable => "unavailable",
103    TooManyRequests => "too_many_requests",
104    ExecutionTimeExceeded => "execution_time_exceeded",
105});
106error_type!(CodeExecutionToolResultErrorType {
107    CodeExecutionToolResultError => "code_execution_tool_result_error"
108});
109pub type CodeExecutionToolResultError =
110    ServerToolResultError<CodeExecutionToolResultErrorCode, CodeExecutionToolResultErrorType>;
111
112error_code!(
113    BashCodeExecutionToolResultErrorCode,
114    BashCodeExecutionToolResultErrorCodeKnown {
115        InvalidToolInput => "invalid_tool_input",
116        Unavailable => "unavailable",
117        TooManyRequests => "too_many_requests",
118        ExecutionTimeExceeded => "execution_time_exceeded",
119        OutputFileTooLarge => "output_file_too_large",
120    }
121);
122error_type!(BashCodeExecutionToolResultErrorType {
123    BashCodeExecutionToolResultError => "bash_code_execution_tool_result_error"
124});
125pub type BashCodeExecutionToolResultError = ServerToolResultError<
126    BashCodeExecutionToolResultErrorCode,
127    BashCodeExecutionToolResultErrorType,
128>;
129
130error_code!(
131    TextEditorCodeExecutionToolResultErrorCode,
132    TextEditorCodeExecutionToolResultErrorCodeKnown {
133        InvalidToolInput => "invalid_tool_input",
134        Unavailable => "unavailable",
135        TooManyRequests => "too_many_requests",
136        ExecutionTimeExceeded => "execution_time_exceeded",
137        FileNotFound => "file_not_found",
138    }
139);
140error_type!(TextEditorCodeExecutionToolResultErrorType {
141    TextEditorCodeExecutionToolResultError => "text_editor_code_execution_tool_result_error"
142});
143pub type TextEditorCodeExecutionToolResultError = ServerToolResultError<
144    TextEditorCodeExecutionToolResultErrorCode,
145    TextEditorCodeExecutionToolResultErrorType,
146>;
147
148error_code!(ToolSearchToolResultErrorCode, ToolSearchToolResultErrorCodeKnown {
149    InvalidToolInput => "invalid_tool_input",
150    Unavailable => "unavailable",
151    TooManyRequests => "too_many_requests",
152    ExecutionTimeExceeded => "execution_time_exceeded",
153});
154error_type!(ToolSearchToolResultErrorType {
155    ToolSearchToolResultError => "tool_search_tool_result_error"
156});
157pub type ToolSearchToolResultError =
158    ServerToolResultError<ToolSearchToolResultErrorCode, ToolSearchToolResultErrorType>;