cognate-core 0.1.0

Core traits, types, and HTTP client abstractions for the Cognate LLM framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Cognate Core — HTTP client, traits, and base types for LLM providers.
//!
//! This crate provides the foundational abstractions for building
//! provider-agnostic LLM applications with type-safe interfaces and
//! zero-cost abstractions.
//!
//! # Quick start
//!
//! ```rust,no_run
//! use cognate_core::{Provider, Request, Message};
//!
//! async fn run<P: Provider>(provider: &P) -> cognate_core::Result<()> {
//!     let response = provider
//!         .complete(
//!             Request::new()
//!                 .with_model("gpt-4o-mini")
//!                 .with_message(Message::user("Hello!")),
//!         )
//!         .await?;
//!     println!("{}", response.content());
//!     Ok(())
//! }
//! ```
#![warn(missing_docs)]

use async_trait::async_trait;
use futures::stream::BoxStream;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

pub mod error;
pub mod middleware;
pub mod mock;
pub mod ratelimit;
pub mod types;

pub use error::{Error, Result};
pub use middleware::{Layer, Middleware, ProviderExt};
pub use mock::MockProvider;
pub use ratelimit::TokenBucket;

// ─── Provider trait ────────────────────────────────────────────────────────

/// Core trait for all LLM providers.
///
/// Implement this trait to add support for a new LLM provider.
/// The trait is object-safe and supports both full completion and
/// streaming responses.
///
/// # Example
///
/// ```rust,no_run
/// use cognate_core::{Provider, Request, Message};
///
/// async fn example<P: Provider>(provider: &P) -> cognate_core::Result<()> {
///     let request = Request::new()
///         .with_model("gpt-4o")
///         .with_messages(vec![
///             Message::system("You are a helpful assistant"),
///             Message::user("Hello!"),
///         ]);
///
///     let response = provider.complete(request).await?;
///     println!("{}", response.content());
///     Ok(())
/// }
/// ```
#[async_trait]
pub trait Provider: Send + Sync {
    /// Send a completion request and wait for the full response.
    async fn complete(&self, req: Request) -> Result<Response>;

    /// Send a completion request and return a streaming response.
    ///
    /// Returns a stream of [`Chunk`]s as they are generated by the provider.
    ///
    /// # Example
    ///
    /// ```rust,no_run
    /// use cognate_core::{Provider, Request, Message};
    /// use futures::StreamExt;
    ///
    /// async fn stream_example<P: Provider>(provider: &P) -> cognate_core::Result<()> {
    ///     let mut stream = provider
    ///         .stream(Request::new().with_model("gpt-4o").with_message(Message::user("Hi")))
    ///         .await?;
    ///     while let Some(chunk) = stream.next().await {
    ///         print!("{}", chunk?.content());
    ///     }
    ///     Ok(())
    /// }
    /// ```
    async fn stream(&self, req: Request) -> Result<BoxStream<'static, Result<Chunk>>>;
}

/// Trait for providers that can generate embedding vectors.
///
/// Implement this alongside [`Provider`] if your backend supports embeddings.
///
/// # Example
///
/// ```rust,no_run
/// use cognate_core::EmbeddingProvider;
///
/// async fn embed<E: EmbeddingProvider>(embedder: &E) -> cognate_core::Result<()> {
///     let vecs = embedder.embed(vec!["Hello world".to_string()]).await?;
///     println!("Embedding dimension: {}", vecs[0].len());
///     Ok(())
/// }
/// ```
#[async_trait]
pub trait EmbeddingProvider: Send + Sync {
    /// Generate embedding vectors for the given list of input strings.
    ///
    /// Returns one vector per input in the same order as `inputs`.
    async fn embed(&self, inputs: Vec<String>) -> Result<Vec<Vec<f32>>>;
}

// ─── Message / Role ────────────────────────────────────────────────────────

/// A single message in a conversation.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct Message {
    /// The role of the message sender.
    pub role: Role,
    /// The text content of the message.
    ///
    /// For assistant messages that contain only tool calls, this may be empty.
    pub content: String,
    /// Optional name used to distinguish multiple participants of the same role.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Tool calls requested by the assistant, if any.
    ///
    /// Present on messages with `role = Assistant` when the model wants to
    /// invoke one or more tools.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_calls: Option<Vec<ToolCall>>,
    /// The tool-call ID this message is responding to.
    ///
    /// Must be set on messages with `role = Tool` so the provider can
    /// correlate the result with the originating call.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub tool_call_id: Option<String>,
}

/// The role of a message sender.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum Role {
    /// A high-level instruction that shapes the assistant's behaviour.
    System,
    /// A message from the human end of the conversation.
    User,
    /// A message generated by the assistant.
    Assistant,
    /// A legacy function-call result (OpenAI function-calling v1).
    #[serde(rename = "function")]
    Function,
    /// A tool-call result sent back by the client.
    Tool,
}

impl Message {
    /// Create a system message.
    pub fn system(content: impl Into<String>) -> Self {
        Self {
            role: Role::System,
            content: content.into(),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Create a user message.
    pub fn user(content: impl Into<String>) -> Self {
        Self {
            role: Role::User,
            content: content.into(),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Create an assistant message.
    pub fn assistant(content: impl Into<String>) -> Self {
        Self {
            role: Role::Assistant,
            content: content.into(),
            name: None,
            tool_calls: None,
            tool_call_id: None,
        }
    }

    /// Create a tool-result message.
    ///
    /// `tool_call_id` must match the `id` of the [`ToolCall`] being answered.
    pub fn tool_result(content: impl Into<String>, tool_call_id: impl Into<String>) -> Self {
        Self {
            role: Role::Tool,
            content: content.into(),
            name: None,
            tool_calls: None,
            tool_call_id: Some(tool_call_id.into()),
        }
    }
}

// ─── Tool calling ──────────────────────────────────────────────────────────

/// A tool invocation requested by the model.
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ToolCall {
    /// Unique identifier for this call, used to correlate the result.
    pub id: String,
    /// The type of call — currently always `"function"`.
    #[serde(rename = "type")]
    pub call_type: String,
    /// The function the model wants to call.
    pub function: ToolCallFunction,
}

/// The function component of a [`ToolCall`].
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
pub struct ToolCallFunction {
    /// Name of the function to invoke.
    pub name: String,
    /// JSON-encoded arguments string, e.g. `"{\"query\":\"Rust\"}"`.
    pub arguments: String,
}

// ─── Request ───────────────────────────────────────────────────────────────

/// A completion request sent to a provider.
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Request {
    /// The model identifier, e.g. `"gpt-4o"` or `"claude-3-5-sonnet-20241022"`.
    pub model: String,
    /// The conversation history, including any system prompt.
    pub messages: Vec<Message>,
    /// Sampling temperature in `[0.0, 2.0]`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub temperature: Option<f32>,
    /// Maximum tokens to generate.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub max_tokens: Option<u32>,
    /// Nucleus sampling parameter.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub top_p: Option<f32>,
    /// Frequency penalty in `[-2.0, 2.0]` (OpenAI only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub frequency_penalty: Option<f32>,
    /// Presence penalty in `[-2.0, 2.0]` (OpenAI only).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub presence_penalty: Option<f32>,
    /// Stop sequences.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stop: Option<Vec<String>>,
    /// Whether to stream the response. Providers handle this internally.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub stream: Option<bool>,
    /// Structured output format (`json_object` etc.).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub response_format: Option<ResponseFormat>,
    /// Provider-specific extra parameters (e.g. `tools`, `tool_choice`).
    #[serde(skip_serializing_if = "HashMap::is_empty", default)]
    pub extra: HashMap<String, serde_json::Value>,
}

impl Request {
    /// Create a new empty request.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set the model identifier.
    pub fn with_model(mut self, model: impl Into<String>) -> Self {
        self.model = model.into();
        self
    }

    /// Set the full message list.
    pub fn with_messages(mut self, messages: Vec<Message>) -> Self {
        self.messages = messages;
        self
    }

    /// Append a single message.
    pub fn with_message(mut self, message: Message) -> Self {
        self.messages.push(message);
        self
    }

    /// Set the sampling temperature.
    pub fn with_temperature(mut self, temperature: f32) -> Self {
        self.temperature = Some(temperature);
        self
    }

    /// Set the maximum number of tokens to generate.
    pub fn with_max_tokens(mut self, max_tokens: u32) -> Self {
        self.max_tokens = Some(max_tokens);
        self
    }

    /// Set the `top_p` nucleus sampling parameter.
    pub fn with_top_p(mut self, top_p: f32) -> Self {
        self.top_p = Some(top_p);
        self
    }

    /// Enable JSON mode (structured output).
    pub fn with_json_mode(mut self) -> Self {
        self.response_format = Some(ResponseFormat::json_object());
        self
    }

    /// Insert a provider-specific extra parameter.
    pub fn with_extra(
        mut self,
        key: impl Into<String>,
        value: impl Into<serde_json::Value>,
    ) -> Self {
        self.extra.insert(key.into(), value.into());
        self
    }
}

// ─── ResponseFormat ────────────────────────────────────────────────────────

/// Structured output format specifier.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ResponseFormat {
    /// The format type — e.g. `"json_object"`.
    #[serde(rename = "type")]
    pub format_type: String,
}

impl ResponseFormat {
    /// Request JSON object output.
    pub fn json_object() -> Self {
        Self {
            format_type: "json_object".to_string(),
        }
    }
}

// ─── Response / Choice / Usage / Chunk ────────────────────────────────────

/// A completed response from a provider.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Response {
    /// Provider-assigned response identifier.
    pub id: String,
    /// Model that generated the response.
    pub model: String,
    /// One or more completion choices (usually one).
    pub choices: Vec<Choice>,
    /// Token usage statistics, if the provider returned them.
    pub usage: Option<Usage>,
    /// Unix timestamp of when the response was created.
    pub created: Option<u64>,
}

impl Response {
    /// Return the text content of the first choice.
    ///
    /// Returns an empty string if there are no choices.
    pub fn content(&self) -> &str {
        self.choices
            .first()
            .map(|c| c.message.content.as_str())
            .unwrap_or("")
    }

    /// Return token usage statistics, if available.
    pub fn usage(&self) -> Option<&Usage> {
        self.usage.as_ref()
    }

    /// Return the tool calls from the first choice, if any.
    pub fn tool_calls(&self) -> Option<&Vec<ToolCall>> {
        self.choices
            .first()
            .and_then(|c| c.message.tool_calls.as_ref())
    }
}

/// A single completion choice within a [`Response`].
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Choice {
    /// Zero-based index of this choice.
    pub index: u32,
    /// The message generated for this choice.
    pub message: Message,
    /// Reason the model stopped generating, e.g. `"stop"` or `"tool_calls"`.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finish_reason: Option<String>,
}

/// Token usage statistics for a request.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Usage {
    /// Number of tokens in the prompt.
    pub prompt_tokens: u32,
    /// Number of tokens generated.
    pub completion_tokens: u32,
    /// Total tokens consumed (`prompt_tokens + completion_tokens`).
    pub total_tokens: u32,
}

impl Usage {
    /// Calculate the USD cost of this request.
    ///
    /// `prompt_price` and `completion_price` are expressed as USD per 1 000 tokens.
    pub fn calculate_cost(&self, prompt_price: f64, completion_price: f64) -> f64 {
        let prompt_cost = (self.prompt_tokens as f64 / 1000.0) * prompt_price;
        let completion_cost = (self.completion_tokens as f64 / 1000.0) * completion_price;
        prompt_cost + completion_cost
    }
}

/// A single chunk in a streaming response.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Chunk {
    /// Provider-assigned response identifier.
    pub id: String,
    /// Model that generated this chunk.
    pub model: String,
    /// The incremental content delta.
    pub delta: Delta,
    /// Set on the final chunk — e.g. `"stop"` or `"tool_calls"`.
    pub finish_reason: Option<String>,
}

impl Chunk {
    /// Return the incremental text content of this chunk.
    pub fn content(&self) -> &str {
        &self.delta.content
    }

    /// Return `true` if this is the terminal chunk of the stream.
    pub fn is_finished(&self) -> bool {
        self.finish_reason.is_some()
    }
}

/// The incremental content delta inside a [`Chunk`].
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct Delta {
    /// Role of the speaker, present only in the first chunk of a response.
    pub role: Option<Role>,
    /// Incremental text content generated since the previous chunk.
    pub content: String,
}

// ─── ProviderConfig ────────────────────────────────────────────────────────

/// Configuration shared by all provider clients.
#[derive(Debug, Clone)]
pub struct ProviderConfig {
    /// API key used to authenticate requests.
    pub api_key: String,
    /// Override for the provider's default base URL.
    pub base_url: String,
    /// Request timeout in seconds.
    pub timeout_seconds: u64,
    /// Maximum number of automatic retries on transient errors.
    pub max_retries: u32,
}

impl ProviderConfig {
    /// Create a minimal config with only an API key.
    pub fn new(api_key: impl Into<String>) -> Self {
        Self {
            api_key: api_key.into(),
            base_url: String::new(),
            timeout_seconds: 60,
            max_retries: 3,
        }
    }

    /// Override the default base URL (useful for proxies or local servers).
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = url.into();
        self
    }

    /// Set the request timeout in seconds.
    pub fn with_timeout(mut self, seconds: u64) -> Self {
        self.timeout_seconds = seconds;
        self
    }

    /// Set the maximum number of automatic retries.
    pub fn with_max_retries(mut self, retries: u32) -> Self {
        self.max_retries = retries;
        self
    }
}