Skip to main content

pi_core/
llm.rs

1use ts_rs::TS;
2use serde::{Deserialize, Serialize};
3
4use crate::message::{AssistantMessage, StopReason};
5use crate::types::{ApiName, ModelId, ModelName, ProviderName, ToolCallId};
6
7/// Describes a concrete LLM model and its provider.
8#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
9#[ts(export)]
10pub struct Model {
11    pub id: ModelId,
12    pub name: ModelName,
13    pub api: ApiName,
14    pub provider: ProviderName,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    pub base_url: Option<String>,
17    pub reasoning: bool,
18    pub context_window: u32,
19    pub max_tokens: u32,
20    #[serde(default)]
21    pub capabilities: ModelCapabilities,
22    #[serde(default)]
23    pub cost: ModelCost,
24}
25
26/// Capabilities advertised by a model.
27#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, TS, Default)]
28pub struct ModelCapabilities {
29    pub vision: bool,
30    pub json_mode: bool,
31    pub function_calling: bool,
32    pub streaming: bool,
33}
34
35/// Per-token cost estimate.
36#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, TS, Default)]
37pub struct ModelCost {
38    pub input: f64,
39    pub output: f64,
40    pub cache_read: f64,
41    pub cache_write: f64,
42}
43
44/// Supported LLM providers.
45#[derive(Debug, Clone, Copy, PartialEq, Serialize, Deserialize, TS)]
46#[serde(rename_all = "snake_case")]
47pub enum ModelProvider {
48    OpenAi,
49    Anthropic,
50    Google,
51    Ollama,
52    Custom,
53}
54
55/// A chunk from an LLM streaming response.
56/// Note: Not exported to TS — uses serde(flatten) which ts-rs can't handle,
57/// and this type is only used internally by the host-side streaming pipeline.
58#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
59#[serde(tag = "kind", rename_all = "snake_case")]
60pub enum LlmChunk {
61    Start {
62        #[serde(flatten)]
63        partial: AssistantMessage,
64    },
65    TextDelta {
66        text: String,
67    },
68    ThinkingDelta {
69        text: String,
70    },
71    ToolCallDelta {
72        tool_call_id: ToolCallId,
73        delta: serde_json::Value,
74    },
75    Done,
76    Error {
77        message: String,
78    },
79}
80
81/// Final result of an LLM stream.
82#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS)]
83pub enum LlmResult {
84    Ok(AssistantMessage),
85    Err { error: LlmError, aborted: bool },
86}
87
88impl LlmResult {
89    pub fn finalize_message(self) -> AssistantMessage {
90        match self {
91            LlmResult::Ok(msg) => msg,
92            LlmResult::Err { error, aborted } => {
93                let mut msg = AssistantMessage::empty();
94                msg.stop_reason = if aborted {
95                    StopReason::Aborted
96                } else {
97                    StopReason::Error
98                };
99                msg.error_message = Some(error.message);
100                msg
101            }
102        }
103    }
104
105    pub fn done() -> Self {
106        LlmResult::Ok(AssistantMessage::empty())
107    }
108}
109
110/// Error from the LLM provider.
111#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, TS, thiserror::Error)]
112#[error("llm error: {message}")]
113pub struct LlmError {
114    pub code: String,
115    pub message: String,
116    #[serde(skip_serializing_if = "Option::is_none")]
117    #[ts(type = "object | undefined")]
118    pub details: Option<serde_json::Value>,
119}