1use ai_sdk_provider::Error as ProviderError;
2
3pub type Result<T> = std::result::Result<T, Error>;
5
6pub type Error = Box<dyn std::error::Error + Send + Sync>;
8
9#[derive(thiserror::Error, Debug)]
11pub enum GenerateError {
12 #[error("Missing model - must call .model() before execute()")]
14 MissingModel,
15
16 #[error("Missing prompt - must call .prompt() before execute()")]
18 MissingPrompt,
19
20 #[error("Provider error: {0}")]
22 ProviderError(#[from] ProviderError),
23
24 #[error("Tool execution error: {0}")]
26 ToolError(#[from] ToolError),
27
28 #[error("Maximum steps reached without completion")]
30 MaxStepsReached,
31
32 #[error("Invalid parameters: {0}")]
34 InvalidParameters(String),
35
36 #[error("Stream error: {0}")]
38 StreamError(String),
39
40 #[error("Model error: {0}")]
42 ModelError(String),
43}
44
45#[derive(thiserror::Error, Debug)]
47pub enum EmbedError {
48 #[error("Missing model - must call .model() before execute()")]
50 MissingModel,
51
52 #[error("Missing value - must call .value() before execute()")]
54 MissingValue,
55
56 #[error("Empty response from embedding model")]
58 EmptyResponse,
59
60 #[error("Provider error: {0}")]
62 ProviderError(#[from] ProviderError),
63}
64
65#[derive(thiserror::Error, Debug)]
67pub enum ToolError {
68 #[error("Tool execution failed: {0}")]
70 ExecutionError(String),
71
72 #[error("Tool not found: {0}")]
74 ToolNotFound(String),
75
76 #[error("Invalid tool input: {0}")]
78 InvalidInput(String),
79
80 #[error("Tool execution denied")]
82 ExecutionDenied,
83}
84
85impl ToolError {
86 pub fn execution(msg: impl Into<String>) -> Self {
88 ToolError::ExecutionError(msg.into())
89 }
90
91 pub fn not_found(name: impl Into<String>) -> Self {
93 ToolError::ToolNotFound(name.into())
94 }
95
96 pub fn invalid_input(msg: impl Into<String>) -> Self {
98 ToolError::InvalidInput(msg.into())
99 }
100}
101
102#[derive(Debug, thiserror::Error)]
104pub enum GenerateObjectError {
105 #[error("Model is required")]
107 MissingModel,
108
109 #[error("Prompt is required")]
111 MissingPrompt,
112
113 #[error("Output strategy is required")]
115 MissingOutputStrategy,
116
117 #[error("Provider error: {0}")]
119 ProviderError(#[from] ProviderError),
120
121 #[error("Validation failed: {0}")]
123 ValidationFailed(String),
124
125 #[error("JSON parsing error: {0}")]
127 JsonError(#[from] serde_json::Error),
128
129 #[error("No text content in model response")]
131 NoTextContent,
132
133 #[error("Model error: {0}")]
135 ModelError(Box<dyn std::error::Error + Send + Sync>),
136}
137
138#[derive(Debug, thiserror::Error)]
140pub enum StreamObjectError {
141 #[error("Model is required")]
143 MissingModel,
144
145 #[error("Prompt is required")]
147 MissingPrompt,
148
149 #[error("Output strategy is required")]
151 MissingOutputStrategy,
152
153 #[error("Provider error: {0}")]
155 ProviderError(#[from] ProviderError),
156
157 #[error("Validation failed: {0}")]
159 ValidationFailed(String),
160
161 #[error("Stream error: {0}")]
163 StreamError(String),
164
165 #[error("Model error: {0}")]
167 ModelError(Box<dyn std::error::Error + Send + Sync>),
168}
169
170#[derive(Debug, thiserror::Error)]
172pub enum RegistryError {
173 #[error("No such provider: {provider_id} (available providers: {available_providers:?})")]
175 NoSuchProvider {
176 provider_id: String,
178 model_type: String,
180 available_providers: Vec<String>,
182 },
183
184 #[error("No such model: {model_id} (type: {model_type})")]
186 NoSuchModel {
187 model_id: String,
189 model_type: String,
191 },
192
193 #[error("Invalid model ID: {message}")]
195 InvalidModelId {
196 model_id: String,
198 model_type: String,
200 message: String,
202 },
203}