vv_agent/types/
messages.rs1use serde::{de::Error as _, Deserialize, Deserializer, Serialize, Serializer};
2
3use super::{Metadata, TokenUsage, ToolArtifactRef, ToolCall};
4
5#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
6#[serde(rename_all = "lowercase")]
7pub enum MessageRole {
8 System,
9 User,
10 Assistant,
11 Tool,
12}
13
14#[derive(Debug, Clone, PartialEq)]
15pub struct Message {
16 pub role: MessageRole,
17 pub content: String,
18 pub name: Option<String>,
19 pub tool_call_id: Option<String>,
20 pub tool_calls: Vec<ToolCall>,
21 pub reasoning_content: Option<String>,
22 pub image_url: Option<String>,
23 pub metadata: Metadata,
24 pub artifact_ref: Option<ToolArtifactRef>,
25}
26
27impl Message {
28 pub fn new(role: MessageRole, content: impl Into<String>) -> Self {
29 Self {
30 role,
31 content: content.into(),
32 name: None,
33 tool_call_id: None,
34 tool_calls: Vec::new(),
35 reasoning_content: None,
36 image_url: None,
37 metadata: Metadata::new(),
38 artifact_ref: None,
39 }
40 }
41
42 pub fn system(content: impl Into<String>) -> Self {
43 Self::new(MessageRole::System, content)
44 }
45
46 pub fn user(content: impl Into<String>) -> Self {
47 Self::new(MessageRole::User, content)
48 }
49
50 pub fn assistant(content: impl Into<String>) -> Self {
51 Self::new(MessageRole::Assistant, content)
52 }
53
54 pub fn tool(content: impl Into<String>, tool_call_id: impl Into<String>) -> Self {
55 let mut message = Self::new(MessageRole::Tool, content);
56 message.tool_call_id = Some(tool_call_id.into());
57 message
58 }
59}
60
61impl Serialize for Message {
62 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
63 where
64 S: Serializer,
65 {
66 self.to_dict().serialize(serializer)
67 }
68}
69
70impl<'de> Deserialize<'de> for Message {
71 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
72 where
73 D: Deserializer<'de>,
74 {
75 let value = serde_json::Value::deserialize(deserializer)?;
76 Self::from_dict(&value).map_err(D::Error::custom)
77 }
78}
79
80#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
81pub struct LLMResponse {
82 pub content: String,
83 pub tool_calls: Vec<ToolCall>,
84 pub raw: Metadata,
85 pub token_usage: TokenUsage,
86}
87
88impl LLMResponse {
89 pub fn new(content: impl Into<String>) -> Self {
90 Self {
91 content: content.into(),
92 tool_calls: Vec::new(),
93 raw: Metadata::new(),
94 token_usage: TokenUsage::default(),
95 }
96 }
97
98 pub fn with_tool_calls(content: impl Into<String>, tool_calls: Vec<ToolCall>) -> Self {
99 Self {
100 content: content.into(),
101 tool_calls,
102 raw: Metadata::new(),
103 token_usage: TokenUsage::default(),
104 }
105 }
106}