aimux_core/language_model.rs
1//! The `LanguageModel` trait — the provider-facing interface.
2//!
3//! Every LLM provider implements this trait. Users never call `do_generate` /
4//! `do_stream` directly; they call `generate_text` / `stream_text` instead.
5
6use async_trait::async_trait;
7
8use crate::error::AiMuxError;
9use crate::options::CallOptions;
10use crate::result::{GenerateResult, StreamResult};
11
12/// The unified language model trait (provider-facing).
13///
14/// Aligned with Vercel AI SDK `LanguageModelV4`.
15///
16/// # Implementation notes
17///
18/// - `do_generate` / `do_stream` are **not** user-facing API. Users call
19/// `generate_text` / `stream_text` (free functions in [`crate::generate`]).
20/// - Providers must emit `StreamStart` as the first stream part, and `Finish`
21/// as the last (before any `Error`).
22/// - Unsupported `CallOptions` fields must produce a `Warning::Unsupported`
23/// rather than being silently dropped.
24#[async_trait]
25pub trait LanguageModel: Send + Sync {
26 /// Specification version (always `"v4"`).
27 fn specification_version(&self) -> &'static str {
28 "v4"
29 }
30
31 /// Provider name, e.g. `"openai"`.
32 fn provider(&self) -> &str;
33
34 /// Model identifier, e.g. `"gpt-4o"`.
35 fn model_id(&self) -> &str;
36
37 /// Generate a complete (non-streaming) response.
38 async fn do_generate(&self, options: &CallOptions) -> Result<GenerateResult, AiMuxError>;
39
40 /// Generate a streaming response.
41 async fn do_stream(&self, options: &CallOptions) -> Result<StreamResult, AiMuxError>;
42}