vtcode_core/utils/session_archive/
message.rs1use serde::{Deserialize, Deserializer, Serialize};
2
3use crate::llm::provider::{AssistantPhase, Message, MessageClearAt, MessageContent, MessageRole, ToolCall};
4
5#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
6pub struct SessionMessage {
7 pub role: MessageRole,
8 pub content: MessageContent,
9 #[serde(
11 default,
12 skip_serializing_if = "Option::is_none",
13 deserialize_with = "deserialize_boxed_non_empty_string_opt"
14 )]
15 pub reasoning: Option<Box<String>>,
16 #[serde(
17 default,
18 skip_serializing_if = "Option::is_none",
19 deserialize_with = "deserialize_boxed_non_empty_vec_opt"
20 )]
21 pub reasoning_details: Option<Box<Vec<serde_json::Value>>>,
22 #[serde(
23 default,
24 skip_serializing_if = "Option::is_none",
25 deserialize_with = "deserialize_boxed_non_empty_vec_opt"
26 )]
27 pub tool_calls: Option<Box<Vec<ToolCall>>>,
28 #[serde(default, deserialize_with = "deserialize_boxed_non_empty_string_opt")]
29 pub tool_call_id: Option<Box<String>>,
30 #[serde(default, skip_serializing_if = "Option::is_none")]
31 pub phase: Option<AssistantPhase>,
32 #[serde(
33 default,
34 skip_serializing_if = "Option::is_none",
35 deserialize_with = "deserialize_boxed_non_empty_string_opt"
36 )]
37 pub origin_tool: Option<Box<String>>,
38 #[serde(
39 default,
40 skip_serializing_if = "Option::is_none",
41 deserialize_with = "deserialize_boxed_metadata_opt"
42 )]
43 pub metadata: Option<Box<crate::core::message_metadata::MessageMetadata>>,
44 #[serde(default, skip_serializing_if = "Option::is_none")]
45 pub clear_at: Option<MessageClearAt>,
46}
47
48impl Eq for SessionMessage {}
49
50#[expect(
51 clippy::box_collection,
52 reason = "Intentional compatibility, platform, test, or API-shape suppression."
53)]
54#[inline]
55fn boxed_non_empty_string(value: Option<String>) -> Option<Box<String>> {
56 value.and_then(|value| (!value.is_empty()).then_some(Box::new(value)))
57}
58
59#[expect(
60 clippy::box_collection,
61 reason = "Intentional compatibility, platform, test, or API-shape suppression."
62)]
63#[inline]
64fn boxed_non_empty_vec<T>(value: Option<Vec<T>>) -> Option<Box<Vec<T>>> {
65 value.and_then(|value| (!value.is_empty()).then_some(Box::new(value)))
66}
67
68fn clone_non_empty_string(value: Option<&str>) -> Option<String> {
69 value.filter(|value| !value.is_empty()).map(str::to_owned)
70}
71
72fn deserialize_boxed_metadata_opt<'de, D>(
73 deserializer: D,
74) -> Result<Option<Box<crate::core::message_metadata::MessageMetadata>>, D::Error>
75where
76 D: Deserializer<'de>,
77{
78 crate::core::message_metadata::MessageMetadata::deserialize(deserializer).map(|m| Some(Box::new(m)))
79}
80
81fn clone_non_empty_vec<T: Clone>(value: Option<&[T]>) -> Option<Vec<T>> {
82 value.filter(|value| !value.is_empty()).map(<[T]>::to_vec)
83}
84
85#[expect(
86 clippy::box_collection,
87 reason = "Intentional compatibility, platform, test, or API-shape suppression."
88)]
89fn deserialize_boxed_non_empty_string_opt<'de, D>(deserializer: D) -> Result<Option<Box<String>>, D::Error>
90where
91 D: Deserializer<'de>,
92{
93 Option::<String>::deserialize(deserializer).map(boxed_non_empty_string)
94}
95
96#[expect(
97 clippy::box_collection,
98 reason = "Intentional compatibility, platform, test, or API-shape suppression."
99)]
100fn deserialize_boxed_non_empty_vec_opt<'de, D, T>(deserializer: D) -> Result<Option<Box<Vec<T>>>, D::Error>
101where
102 D: Deserializer<'de>,
103 T: Deserialize<'de>,
104{
105 Option::<Vec<T>>::deserialize(deserializer).map(boxed_non_empty_vec)
106}
107
108impl SessionMessage {
109 fn base(role: MessageRole, content: MessageContent) -> Self {
110 Self {
111 role,
112 content,
113 reasoning: None,
114 reasoning_details: None,
115 tool_calls: None,
116 tool_call_id: None,
117 phase: None,
118 origin_tool: None,
119 metadata: None,
120 clear_at: None,
121 }
122 }
123
124 pub fn new(role: MessageRole, content: impl Into<String>) -> Self {
125 Self::base(role, MessageContent::Text(content.into()))
126 }
127
128 pub fn with_content(role: MessageRole, content: MessageContent) -> Self {
129 Self::base(role, content)
130 }
131
132 pub fn with_tool_call_id(role: MessageRole, content: impl Into<String>, tool_call_id: Option<String>) -> Self {
133 Self::with_tool_call_id_content(role, MessageContent::Text(content.into()), tool_call_id)
134 }
135
136 pub fn with_tool_call_id_content(role: MessageRole, content: MessageContent, tool_call_id: Option<String>) -> Self {
137 let mut message = Self::base(role, content);
138 message.tool_call_id = boxed_non_empty_string(tool_call_id);
139 message
140 }
141}
142
143impl From<&Message> for SessionMessage {
144 fn from(message: &Message) -> Self {
145 Self {
146 role: message.role,
147 content: message.content.clone(),
148 reasoning: boxed_non_empty_string(message.reasoning.clone()),
149 reasoning_details: boxed_non_empty_vec(message.reasoning_details.clone()),
150 tool_calls: boxed_non_empty_vec(message.tool_calls.clone()),
151 tool_call_id: boxed_non_empty_string(message.tool_call_id.clone()),
152 phase: message.phase,
153 origin_tool: boxed_non_empty_string(message.origin_tool.clone()),
154 metadata: message.metadata.clone().map(Box::new),
155 clear_at: message.clear_at,
156 }
157 }
158}
159
160impl From<&SessionMessage> for Message {
161 fn from(message: &SessionMessage) -> Self {
162 Self {
163 role: message.role,
164 content: message.content.clone(),
165 reasoning: clone_non_empty_string(message.reasoning.as_deref().map(String::as_str)),
166 reasoning_details: clone_non_empty_vec(message.reasoning_details.as_deref().map(Vec::as_slice)),
167 tool_calls: clone_non_empty_vec(message.tool_calls.as_deref().map(Vec::as_slice)),
168 tool_call_id: clone_non_empty_string(message.tool_call_id.as_deref().map(String::as_str)),
169 phase: message.phase,
170 origin_tool: clone_non_empty_string(message.origin_tool.as_deref().map(String::as_str)),
171 metadata: message.metadata.as_deref().cloned(),
172 clear_at: message.clear_at,
173 }
174 }
175}
176
177#[cfg(test)]
178mod tests {
179 use super::SessionMessage;
180 use crate::llm::provider::Message;
181
182 #[test]
183 fn borrowed_archive_conversion_preserves_sparse_metadata_boundaries() {
184 let mut archived = SessionMessage::from(&Message::assistant("body".to_string()));
185 archived.reasoning = Some(Box::new(String::new()));
186 archived.reasoning_details = Some(Box::new(Vec::new()));
187 archived.tool_calls = Some(Box::new(Vec::new()));
188 archived.tool_call_id = Some(Box::new(String::new()));
189 archived.origin_tool = Some(Box::new(String::new()));
190 let empty = Message::from(&archived);
191 assert!(empty.reasoning.is_none());
192 assert!(empty.reasoning_details.is_none());
193 assert!(empty.tool_calls.is_none());
194 assert!(empty.tool_call_id.is_none());
195 assert!(empty.origin_tool.is_none());
196
197 archived.reasoning = Some(Box::new(" \t".to_string()));
198 archived.reasoning_details = Some(Box::new(vec![
199 serde_json::json!({"text": "second"}),
200 serde_json::json!({"text": "first"}),
201 ]));
202 archived.tool_call_id = Some(Box::new("call-7".to_string()));
203 archived.origin_tool = Some(Box::new("read_file".to_string()));
204 let restored = Message::from(&archived);
205 assert_eq!(restored.reasoning.as_deref(), Some(" \t"));
206 assert_eq!(
207 restored.reasoning_details,
208 Some(vec![
209 serde_json::json!({"text": "second"}),
210 serde_json::json!({"text": "first"})
211 ])
212 );
213 assert_eq!(restored.tool_call_id.as_deref(), Some("call-7"));
214 assert_eq!(restored.origin_tool.as_deref(), Some("read_file"));
215 assert!(restored.tool_calls.is_none());
216 }
217}