swiftide_core/chat_completion/
errors.rs

1use thiserror::Error;
2
3use crate::CommandError;
4
5#[derive(Error, Debug)]
6pub enum ToolError {
7    /// I.e. the llm calls the tool with the wrong arguments
8    #[error("arguments for tool failed to parse: {0:#}")]
9    WrongArguments(#[from] serde_json::Error),
10
11    /// Tool requires arguments but none were provided
12    #[error("arguments missing for tool {0:#}")]
13    MissingArguments(String),
14
15    /// Tool execution failed
16    #[error("tool execution failed: {0:#}")]
17    ExecutionFailed(#[from] CommandError),
18
19    #[error(transparent)]
20    Unknown(#[from] anyhow::Error),
21}
22
23type BoxedError = Box<dyn std::error::Error + Send + Sync>;
24
25#[derive(Error, Debug)]
26pub enum LanguageModelError {
27    #[error("Context length exceeded: {0}")]
28    ContextLengthExceeded(BoxedError),
29    #[error("Permanent error: {0}")]
30    PermanentError(BoxedError),
31    #[error("Transient error: {0}")]
32    TransientError(BoxedError),
33}
34
35impl LanguageModelError {
36    pub fn permanent(e: impl Into<BoxedError>) -> Self {
37        LanguageModelError::PermanentError(e.into())
38    }
39
40    pub fn transient(e: impl Into<BoxedError>) -> Self {
41        LanguageModelError::TransientError(e.into())
42    }
43
44    pub fn context_length_exceeded(e: impl Into<BoxedError>) -> Self {
45        LanguageModelError::ContextLengthExceeded(e.into())
46    }
47}
48
49impl From<BoxedError> for LanguageModelError {
50    fn from(e: BoxedError) -> Self {
51        LanguageModelError::PermanentError(e)
52    }
53}
54
55impl From<anyhow::Error> for LanguageModelError {
56    fn from(e: anyhow::Error) -> Self {
57        LanguageModelError::PermanentError(e.into())
58    }
59}