tinyharness_lib/provider/
mod.rs1pub mod llama_cpp;
2pub mod ollama;
3pub mod openai_compat;
4pub mod vllm;
5
6use std::fmt::Display;
7use std::future::Future;
8use std::pin::Pin;
9
10use serde::{Deserialize, Serialize};
11
12use crate::config::OllamaThinkType;
13use crate::image::ImageAttachment;
14
15#[derive(Debug, Clone, Serialize, Deserialize)]
16pub struct ToolDefinition {
17 pub name: String,
18 pub description: String,
19 pub parameters: schemars::Schema,
20}
21
22#[derive(Debug, Clone, Serialize, Deserialize)]
23pub struct ToolCall {
24 pub function: ToolCallFunction,
25}
26
27#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct ToolCallFunction {
29 pub name: String,
30 pub arguments: serde_json::Value,
31 #[serde(skip_serializing_if = "Option::is_none", default)]
35 pub thought_signature: Option<String>,
36}
37
38#[derive(Debug, Clone, Serialize, Deserialize)]
39pub struct ChatMessage {
40 pub content: String,
41 #[serde(default)]
42 pub tool_calls: Vec<ToolCall>,
43 #[serde(default)]
46 pub thinking: Option<String>,
47}
48
49#[derive(Debug, Clone, Deserialize)]
50pub struct ChatMessageResponse {
51 pub message: ChatMessage,
52 pub done: bool,
53 #[serde(default)]
54 pub is_error: bool,
55 #[serde(default)]
56 pub usage: Option<TokenUsage>,
57}
58
59#[derive(Debug, Clone, Default, Deserialize, Serialize)]
61pub struct TokenUsage {
62 pub prompt_tokens: u32,
63 pub completion_tokens: u32,
64 pub total_tokens: u32,
65}
66
67#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)]
68pub enum Role {
69 System,
70 User,
71 Assistant,
72 Tool,
73}
74
75impl Display for Role {
76 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
77 match self {
78 Role::System => f.write_str("System"),
79 Role::User => f.write_str("User"),
80 Role::Assistant => f.write_str("Assistant"),
81 Role::Tool => f.write_str("Tool"),
82 }
83 }
84}
85
86#[derive(Debug, Clone, Serialize, Deserialize)]
87pub struct Message {
88 pub role: Role,
89 pub content: String,
90 pub tool_calls: Vec<ToolCall>,
91 #[serde(default, skip_serializing_if = "Vec::is_empty")]
94 pub images: Vec<ImageAttachment>,
95}
96
97impl Message {
98 pub fn simple(role: Role, content: impl Into<String>) -> Self {
100 Message {
101 role,
102 content: content.into(),
103 tool_calls: vec![],
104 images: vec![],
105 }
106 }
107}
108
109pub trait Provider: Send + Sync {
110 fn health_check(&self) -> Pin<Box<dyn Future<Output = Result<(), String>> + Send>>;
112
113 fn list_models(&self) -> Pin<Box<dyn Future<Output = Vec<String>> + Send>>;
114
115 fn select_model(&mut self, name: String);
116
117 fn current_model(&self) -> Option<String>;
118
119 fn chat(
129 &mut self,
130 messages: Vec<Message>,
131 tools: Vec<ToolDefinition>,
132 ) -> Pin<
133 Box<
134 dyn Future<Output = Result<tokio::sync::mpsc::Receiver<ChatMessageResponse>, String>>
135 + Send,
136 >,
137 >;
138
139 fn set_timeout(&mut self, _timeout_secs: u64) {}
141
142 fn set_retries(&mut self, _max_retries: u32) {}
144
145 fn set_think_type(&mut self, _think_type: OllamaThinkType) {}
147}