Skip to main content

mermaid_cli/models/
traits.rs

1//! Core Model trait - the single interface for model interactions
2//!
3//! Adapters implement this trait directly. No intermediate layers.
4
5use async_trait::async_trait;
6
7use super::capabilities::ModelCapabilities;
8use super::config::ModelConfig;
9use super::error::{ModelError, Result};
10use super::stream::StreamCallback;
11use super::types::{ChatMessage, ModelResponse};
12
13/// Core trait that all model adapters implement.
14///
15/// `chat()` streams typed `StreamEvent`s (text, reasoning, tool calls,
16/// completion) through the optional callback and returns a final
17/// `ModelResponse`. Adapters own the translation between provider-native
18/// stream shapes and the typed event surface — see
19/// `OllamaAdapter::chat` for the reference impl.
20#[async_trait]
21pub trait Model: Send + Sync {
22    /// Send a chat conversation to the model. If a callback is supplied
23    /// the adapter streams typed events; otherwise it does a single
24    /// blocking request and returns the response.
25    async fn chat(
26        &self,
27        messages: &[ChatMessage],
28        config: &ModelConfig,
29        callback: Option<StreamCallback>,
30    ) -> Result<ModelResponse>;
31
32    /// Capabilities advertised by this model — does it support tools,
33    /// vision, what reasoning controls, max context. Adapters return a
34    /// `ModelCapabilities` populated at construction time.
35    fn capabilities(&self) -> &ModelCapabilities;
36
37    /// Get the model identifier (e.g., "ollama/tinyllama")
38    fn name(&self) -> &str;
39
40    /// List available models from this backend.
41    ///
42    /// Default impl returns `Unsupported` — appropriate for providers
43    /// that have no `/models` endpoint (Anthropic, raw Bedrock). Ollama
44    /// and OpenAI-compatible adapters override.
45    async fn list_models(&self) -> Result<Vec<String>> {
46        Err(ModelError::Unsupported {
47            feature: "list_models".to_string(),
48        })
49    }
50}