Repository navigation
Expand file tree
/
Copy pathmodels.rs
More file actions
250 lines (212 loc) · 6.4 KB
/
Copy pathmodels.rs
File metadata and controls
250 lines (212 loc) · 6.4 KB
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
//! Model response types.
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
/// Items that can appear in a model response.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ResponseItem {
/// A message from the model or user.
Message {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
parent_id: Option<String>,
role: String,
content: Vec<ContentItem>,
},
/// A function/tool call from the model.
FunctionCall {
id: String,
call_id: String,
name: String,
arguments: String,
},
/// Output from a function/tool call.
FunctionCallOutput {
id: String,
call_id: String,
output: String,
},
/// Reasoning output from the model.
Reasoning {
#[serde(skip_serializing_if = "Option::is_none")]
id: Option<String>,
content: Vec<ReasoningContent>,
},
/// File citation.
FileCitation {
file_id: String,
#[serde(skip_serializing_if = "Option::is_none")]
quote: Option<String>,
},
/// Web search result.
WebSearchResult {
id: String,
url: String,
title: String,
#[serde(skip_serializing_if = "Option::is_none")]
snippet: Option<String>,
},
}
/// Content items within a message.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ContentItem {
/// Text input from user.
InputText { text: String },
/// Text output from model.
OutputText { text: String },
/// Image input.
InputImage {
image_url: String,
#[serde(skip_serializing_if = "Option::is_none")]
detail: Option<String>,
},
/// Base64 encoded image input.
InputImageBase64 {
data: String,
media_type: String,
#[serde(skip_serializing_if = "Option::is_none")]
detail: Option<String>,
},
/// Model refusal.
Refusal { refusal: String },
/// Tool use request.
ToolUse {
id: String,
name: String,
input: serde_json::Value,
},
/// Tool result.
ToolResult {
tool_use_id: String,
content: String,
#[serde(default)]
is_error: bool,
},
}
/// Reasoning content from the model.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ReasoningContent {
/// Summary of reasoning.
Summary { text: String },
/// Raw thinking content.
Thinking { text: String },
}
/// Local shell action for tool calls.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum LocalShellAction {
/// Execute a command.
Exec(LocalShellExecAction),
/// Read a file.
ReadFile { path: String },
/// Write a file.
WriteFile { path: String, content: String },
/// List directory.
ListDir { path: String },
}
/// Shell execution action.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
pub struct LocalShellExecAction {
pub command: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub workdir: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timeout_ms: Option<u64>,
}
/// Status of a local shell operation.
#[derive(Debug, Clone, Serialize, Deserialize, JsonSchema)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum LocalShellStatus {
/// Command completed successfully.
Success {
exit_code: i32,
stdout: String,
stderr: String,
},
/// Command failed.
Error {
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
exit_code: Option<i32>,
},
/// Command timed out.
Timeout { stdout: String, stderr: String },
/// Waiting for approval.
PendingApproval,
/// Denied by user.
Denied,
}
impl ContentItem {
/// Create a text input content item.
pub fn input_text(text: impl Into<String>) -> Self {
Self::InputText { text: text.into() }
}
/// Create a text output content item.
pub fn output_text(text: impl Into<String>) -> Self {
Self::OutputText { text: text.into() }
}
/// Extract text content if this is a text item.
pub fn as_text(&self) -> Option<&str> {
match self {
Self::InputText { text } | Self::OutputText { text } => Some(text),
_ => None,
}
}
}
impl ResponseItem {
/// Check if this is an assistant message.
pub fn is_assistant_message(&self) -> bool {
matches!(self, Self::Message { role, .. } if role == "assistant")
}
/// Check if this is a user message.
pub fn is_user_message(&self) -> bool {
matches!(self, Self::Message { role, .. } if role == "user")
}
/// Check if this is a function call.
pub fn is_function_call(&self) -> bool {
matches!(self, Self::FunctionCall { .. })
}
/// Get the text content of a message.
pub fn get_text_content(&self) -> Option<String> {
match self {
Self::Message { content, .. } => {
let texts: Vec<&str> = content.iter().filter_map(|c| c.as_text()).collect();
if texts.is_empty() {
None
} else {
Some(texts.join(""))
}
}
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_response_item_message() {
let item = ResponseItem::Message {
id: Some("msg_1".to_string()),
parent_id: None,
role: "assistant".to_string(),
content: vec![ContentItem::output_text("Hello!")],
};
assert!(item.is_assistant_message());
assert!(!item.is_user_message());
assert_eq!(item.get_text_content(), Some("Hello!".to_string()));
}
#[test]
fn test_content_item_serde() {
let item = ContentItem::InputImage {
image_url: "https://example.com/image.png".to_string(),
detail: Some("high".to_string()),
};
let json = serde_json::to_string(&item).expect("serialize");
assert!(json.contains("input_image"));
assert!(json.contains("image_url"));
}
}