Skip to main content

armillae_core/
message.rs

1use schemars::JsonSchema;
2use serde::{Deserialize, Serialize};
3
4use crate::{ProviderData, ToolCall, ToolResult};
5
6/// A role-bearing message in model-call history.
7#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
8pub struct Message {
9    pub role: Role,
10    pub content: Vec<ContentPart>,
11}
12
13impl Message {
14    pub fn new(role: Role, content: Vec<ContentPart>) -> Self {
15        Self { role, content }
16    }
17
18    pub fn user(text: impl Into<String>) -> Self {
19        Self::new(Role::User, vec![ContentPart::text(text)])
20    }
21
22    pub fn assistant(content: Vec<ContentPart>) -> Self {
23        Self::new(Role::Assistant, content)
24    }
25
26    pub fn tool_result(result: ToolResult) -> Self {
27        Self::new(Role::Tool, vec![ContentPart::ToolResult(result)])
28    }
29}
30
31/// The author or protocol role of a message.
32#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
33#[serde(rename_all = "snake_case")]
34#[non_exhaustive]
35pub enum Role {
36    System,
37    Developer,
38    User,
39    Assistant,
40    Tool,
41}
42
43/// An ordered content item within a message.
44#[derive(Clone, Debug, PartialEq, Serialize, Deserialize, JsonSchema)]
45#[serde(tag = "type", rename_all = "snake_case")]
46#[non_exhaustive]
47pub enum ContentPart {
48    Text(TextContent),
49    ToolCall(ToolCall),
50    ToolResult(ToolResult),
51    ProviderData(ProviderData),
52}
53
54impl ContentPart {
55    pub fn text(text: impl Into<String>) -> Self {
56        Self::Text(TextContent::new(text))
57    }
58}
59
60/// Plain text content.
61#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
62pub struct TextContent {
63    pub text: String,
64}
65
66impl TextContent {
67    pub fn new(text: impl Into<String>) -> Self {
68        Self { text: text.into() }
69    }
70}