Skip to main content

agent_runtime/llm/
mod.rs

1use async_trait::async_trait;
2use futures::stream::Stream;
3use std::pin::Pin;
4
5pub mod provider;
6pub mod types;
7
8pub use provider::{LlamaClient, OpenAIClient};
9pub use types::{ChatMessage, ChatRequest, ChatResponse, Role};
10
11/// Result type for LLM operations
12pub type LlmResult<T> = Result<T, LlmError>;
13
14/// Stream of text chunks from LLM
15pub type TextStream = Pin<Box<dyn Stream<Item = LlmResult<String>> + Send>>;
16
17/// Errors that can occur during LLM operations
18#[derive(Debug, thiserror::Error)]
19pub enum LlmError {
20    #[error("API error: {0}")]
21    ApiError(String),
22
23    #[error("Network error: {0}")]
24    NetworkError(String),
25
26    #[error("Invalid request: {0}")]
27    InvalidRequest(String),
28
29    #[error("Rate limit exceeded")]
30    RateLimitExceeded,
31
32    #[error("Authentication failed: {0}")]
33    AuthenticationFailed(String),
34
35    #[error("Response parsing error: {0}")]
36    ParseError(String),
37}
38
39/// Generic trait for LLM chat clients
40#[async_trait]
41pub trait ChatClient: Send + Sync {
42    /// Send a chat completion request
43    async fn chat(&self, request: ChatRequest) -> LlmResult<ChatResponse>;
44
45    /// Stream a chat completion request (yields text chunks as they arrive)
46    async fn chat_stream(&self, request: ChatRequest) -> LlmResult<TextStream>;
47
48    /// Get the model name this client uses
49    fn model(&self) -> &str;
50
51    /// Get the provider name (e.g., "openai", "llama.cpp")
52    fn provider(&self) -> &str;
53}