tinyharness_lib/provider/
mod.rs1pub mod llama_cpp;
2pub mod ollama;
3pub mod openai_compat;
4pub mod openai_compat_provider;
5pub mod sockudo;
6pub mod vllm;
7
8use std::fmt::Display;
9use std::future::Future;
10use std::pin::Pin;
11
12use serde::{Deserialize, Serialize};
13
14use crate::config::OllamaThinkType;
15use crate::image::ImageAttachment;
16
17#[derive(Debug, Clone, Serialize, Deserialize)]
18pub struct ToolDefinition {
19 pub name: String,
20 pub description: String,
21 pub parameters: schemars::Schema,
22}
23
24#[derive(Debug, Clone, Serialize, Deserialize)]
25pub struct ToolCall {
26 #[serde(default, skip_serializing_if = "Option::is_none")]
32 pub id: Option<String>,
33 pub function: ToolCallFunction,
34}
35
36#[derive(Debug, Clone, Serialize, Deserialize)]
37pub struct ToolCallFunction {
38 pub name: String,
39 pub arguments: serde_json::Value,
40 #[serde(skip_serializing_if = "Option::is_none", default)]
44 pub thought_signature: Option<String>,
45}
46
47#[derive(Debug, Clone, Serialize, Deserialize)]
48pub struct ChatMessage {
49 pub content: String,
50 #[serde(default)]
51 pub tool_calls: Vec<ToolCall>,
52 #[serde(default)]
55 pub thinking: Option<String>,
56}
57
58#[derive(Debug, Clone, Deserialize)]
59pub struct ChatMessageResponse {
60 pub message: ChatMessage,
61 pub done: bool,
62 #[serde(default)]
63 pub is_error: bool,
64 #[serde(default)]
65 pub usage: Option<TokenUsage>,
66}
67
68#[derive(Debug, Clone, Default, Deserialize, Serialize)]
70pub struct TokenUsage {
71 pub prompt_tokens: u32,
72 pub completion_tokens: u32,
73 pub total_tokens: u32,
74}
75
76#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
77pub enum Role {
78 System,
79 User,
80 Assistant,
81 Tool,
82}
83
84impl Display for Role {
85 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
86 match self {
87 Role::System => f.write_str("System"),
88 Role::User => f.write_str("User"),
89 Role::Assistant => f.write_str("Assistant"),
90 Role::Tool => f.write_str("Tool"),
91 }
92 }
93}
94
95#[derive(Debug, Clone, Serialize, Deserialize)]
96pub struct Message {
97 pub role: Role,
98 pub content: String,
99 pub tool_calls: Vec<ToolCall>,
100 #[serde(default, skip_serializing_if = "Option::is_none")]
106 pub tool_call_id: Option<String>,
107 #[serde(default, skip_serializing_if = "Vec::is_empty")]
110 pub images: Vec<ImageAttachment>,
111}
112
113impl Default for Message {
114 fn default() -> Self {
115 Message::simple(Role::User, "")
116 }
117}
118
119impl Message {
120 pub fn simple(role: Role, content: impl Into<String>) -> Self {
122 Message {
123 role,
124 content: content.into(),
125 tool_calls: vec![],
126 tool_call_id: None,
127 images: vec![],
128 }
129 }
130}
131
132pub trait Provider: Send + Sync {
133 fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
135
136 fn list_models(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>>;
137
138 fn select_model(&mut self, name: String);
139
140 fn current_model(&self) -> Option<String>;
141
142 fn chat(
152 &mut self,
153 messages: Vec<Message>,
154 tools: Vec<ToolDefinition>,
155 ) -> Pin<
156 Box<
157 dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
158 + Send,
159 >,
160 >;
161
162 fn set_timeout(&mut self, _timeout_secs: u64) {}
164
165 fn set_retries(&mut self, _max_retries: u32) {}
167
168 fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
170}