artificial_core/generic.rs
1//! Generic message and role types used by the *artificial-core* crate.
2//!
3//! They deliberately mirror the concepts exposed by most provider APIs:
4//! “system”, “user”, “assistant”, and “tool”. By staying minimal and
5//! provider-agnostic we can:
6//!
7//! * convert them into provider-specific structs via a simple `From`/`Into`,
8//! * serialize them without pulling in heavyweight dependencies, and
9//! * use them in unit tests without mocking a full transport layer.
10//!
11//! ## When to add more fields?
12//!
13//! Only if the additional data is **required by multiple back-ends** or
14//! **fundamentally provider-independent**. Otherwise extend the
15//! provider-specific message type instead of bloating this one.
16use std::fmt::Display;
17
18/// Lightweight container representing a single chat message that is
19/// independent of any specific LLM provider.
20///
21/// * `message` – the raw UTF-8 content. Markdown is fine, but keep newlines
22/// and indentation portable.
23/// * `role` – see [`GenericRole`] for permitted values.
24#[derive(Debug, Clone)]
25pub struct GenericMessage {
26 pub message: String,
27 pub role: GenericRole,
28}
29
30impl GenericMessage {
31 /// Convenience constructor mirroring the field order used by common HTTP
32 /// APIs (`role`, then `content`).
33 ///
34 /// ```rust
35 /// use artificial_core::generic::{GenericMessage, GenericRole};
36 ///
37 /// let sys = GenericMessage::new("You are a helpful bot.".into(),
38 /// GenericRole::System);
39 /// ```
40 pub fn new(message: String, role: GenericRole) -> Self {
41 Self { message, role }
42 }
43}
44
45/// High-level chat roles recognised by most LLM providers.
46///
47/// The `Display` implementation renders the canonical lowercase name so you
48/// can feed it directly into JSON without extra mapping logic.
49#[derive(Debug, Clone, Copy, PartialEq, Eq)]
50pub enum GenericRole {
51 /// “System” messages define global behaviour and style guidelines.
52 System,
53 /// Messages produced by the assistant / model.
54 Assistant,
55 /// Messages originating from the human user.
56 User,
57 /// Special role used when a **tool call** or similar structured result is
58 /// injected into the conversation.
59 Tool,
60}
61
62impl Display for GenericRole {
63 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64 match self {
65 GenericRole::System => write!(f, "system"),
66 GenericRole::Assistant => write!(f, "assistant"),
67 GenericRole::User => write!(f, "user"),
68 GenericRole::Tool => write!(f, "tool"),
69 }
70 }
71}