potato_agent/agents/
error.rs

1use potato_prompt::PromptError;
2use pyo3::exceptions::PyRuntimeError;
3use pyo3::PyErr;
4use reqwest::StatusCode;
5use thiserror::Error;
6use tracing::error;
7
8#[derive(Error, Debug)]
9pub enum AgentError {
10    #[error("Error: {0}")]
11    Error(String),
12
13    #[error("Failed to create header value for the agent client")]
14    CreateHeaderValueError(#[from] reqwest::header::InvalidHeaderValue),
15
16    #[error("Failed to create header name for the agent client")]
17    CreateHeaderNameError(#[from] reqwest::header::InvalidHeaderName),
18
19    #[error("Failed to create agent client: {0}")]
20    CreateClientError(#[source] reqwest::Error),
21
22    #[error("Request failed: {0}")]
23    RequestError(#[from] reqwest::Error),
24
25    #[error("Failed to serialize chat request: {0}")]
26    SerializationError(#[from] serde_json::Error),
27
28    #[error("Failed to get response: {0} with status code {1}")]
29    CompletionError(String, StatusCode),
30
31    #[error("Failed to downcast Python object: {0}")]
32    DowncastError(String),
33
34    #[error("Failed to get environment variable: {0}")]
35    EnvVarError(#[from] std::env::VarError),
36
37    #[error("Failed to retrieve GEMINI_API_KEY from the environment")]
38    MissingGeminiApiKeyError,
39
40    #[error("Failed to retrieve OPENAI_API_KEY from the environment")]
41    MissingOpenAIApiKeyError,
42
43    #[error("Failed to extract client: {0}")]
44    ClientExtractionError(String),
45
46    #[error("Client did not provide response")]
47    ClientNoResponseError,
48
49    #[error("No ready tasks found but pending tasks remain. Possible circular dependency.")]
50    NoTaskFoundError,
51
52    #[error("Unsupported content type")]
53    UnsupportedContentTypeError,
54
55    #[error("Failed to create runtime: {0}")]
56    CreateRuntimeError(#[source] std::io::Error),
57
58    #[error(transparent)]
59    PromptError(#[from] PromptError),
60
61    #[error(transparent)]
62    UtilError(#[from] potato_util::UtilError),
63
64    #[error(transparent)]
65    TypeError(#[from] potato_type::error::TypeError),
66
67    #[error("Invalid output type: {0}")]
68    InvalidOutputType(String),
69
70    #[error("Failed to create tokio runtime: {0}")]
71    RuntimeError(String),
72
73    #[error("Undefined error: {0}")]
74    UndefinedError(String),
75
76    #[error("Failed to create tool: {0}")]
77    ToolCreationError(String),
78
79    #[error("Invalid tool definition")]
80    InvalidToolDefinitionError,
81
82    #[error("{0}")]
83    InvalidInput(String),
84
85    #[error("Provider mismatch: prompt provider {0}, agent provider {1}")]
86    ProviderMismatch(String, String),
87
88    #[error("Invalid response type")]
89    InvalidResponseType(String),
90
91    #[error("Failed to extract embedding config. Check provider and config compatibility: {0}")]
92    EmbeddingConfigExtractionError(String),
93
94    #[error("Provider not supported: {0}")]
95    ProviderNotSupportedError(String),
96
97    #[error("No embeddings found in the response")]
98    NoEmbeddingsFound,
99}
100
101impl<'a> From<pyo3::DowncastError<'a, 'a>> for AgentError {
102    fn from(err: pyo3::DowncastError) -> Self {
103        AgentError::DowncastError(err.to_string())
104    }
105}
106
107impl From<AgentError> for PyErr {
108    fn from(err: AgentError) -> PyErr {
109        let msg = err.to_string();
110        error!("{}", msg);
111        PyRuntimeError::new_err(msg)
112    }
113}
114
115impl From<PyErr> for AgentError {
116    fn from(err: PyErr) -> Self {
117        AgentError::Error(err.to_string())
118    }
119}