1use crate::agent_runs::remote_control::{
10 AdapterKind, PermissionPolicy, RunnerKind, WorkspacePolicy,
11};
12use serde_json::Value;
13
14#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
16pub enum AgentRunStatus {
17 #[default]
19 Queued,
20 Running,
21 Completed,
22 Failed,
23 Cancelled,
24}
25
26impl AgentRunStatus {
27 pub fn from_wire(s: Option<&str>) -> Self {
29 match s {
30 Some("running") => Self::Running,
31 Some("completed") => Self::Completed,
32 Some("failed") => Self::Failed,
33 Some("cancelled") => Self::Cancelled,
34 _ => Self::Queued,
35 }
36 }
37
38 pub fn as_str(&self) -> &'static str {
39 match self {
40 Self::Queued => "queued",
41 Self::Running => "running",
42 Self::Completed => "completed",
43 Self::Failed => "failed",
44 Self::Cancelled => "cancelled",
45 }
46 }
47}
48
49pub const AGENT_RUN_RUNTIME_STANDARD: &str = "standard";
51pub const AGENT_RUN_RUNTIME_CRABCODE_REMOTE: &str = "crabcode_remote";
52
53pub const AGENT_RUN_META_TITLE: &str = "title";
55pub const AGENT_RUN_META_WORKSPACE: &str = "workspace";
56
57#[derive(Debug, Clone, Default)]
59pub struct AgentRunLocalContextPolicy {
60 pub enabled: Option<bool>,
61 pub readonly: Option<bool>,
62 pub max_bytes: Option<i64>,
63 pub allowed_tools: Option<Vec<String>>,
64}
65
66#[derive(Debug, Clone, Default)]
68pub struct AgentRunArtifactPolicy {
69 pub enabled: Option<bool>,
70 pub max_files: Option<i64>,
71}
72
73#[derive(Debug, Clone, Default)]
75pub struct AgentRunCreateRequest {
76 pub app_id: String,
77 pub mode: Option<String>,
78 pub session_id: Option<String>,
79 pub input: String,
80 pub messages: Option<Vec<Value>>,
81 pub model: Option<String>,
82 pub active_skill_ids: Option<Vec<String>>,
83 pub knowledge_base_ids: Option<Vec<String>>,
84 pub metadata: Option<std::collections::HashMap<String, String>>,
85 pub local_context_policy: Option<AgentRunLocalContextPolicy>,
86 pub artifact_policy: Option<AgentRunArtifactPolicy>,
87
88 pub runtime: Option<String>,
90 pub runner: Option<RunnerKind>,
91 pub adapter: Option<AdapterKind>,
92 pub permission_policy: Option<PermissionPolicy>,
93 pub workspace_policy: Option<WorkspacePolicy>,
94 pub byok_credential_ref: Option<String>,
96}
97
98#[derive(Debug, Clone, Default)]
100pub struct AgentRun {
101 pub run_id: String,
102 pub session_id: String,
103 pub app_id: Option<String>,
104 pub mode: Option<String>,
105 pub status: Option<AgentRunStatus>,
106 pub created_at: Option<String>,
107 pub started_at: Option<String>,
108 pub completed_at: Option<String>,
109 pub error: Option<AgentRunErrorPayload>,
110 pub metadata: Option<std::collections::HashMap<String, String>>,
111 pub runtime: Option<String>,
113 pub runner: Option<String>,
114 pub adapter: Option<String>,
115}
116
117#[derive(Debug, Clone, Default)]
119pub struct AgentRunListOptions {
120 pub runtime: Option<String>,
121 pub status: Option<String>,
122 pub page: Option<i64>,
123 pub page_size: Option<i64>,
124}
125
126#[derive(Debug, Clone)]
128pub struct AgentRunListResult {
129 pub records: Vec<AgentRun>,
130 pub total: i64,
131 pub page: i64,
132 pub page_size: i64,
133}
134
135#[derive(Debug, Clone, Default)]
137pub struct AgentRunCreateResponse {
138 pub run_id: String,
139 pub session_id: String,
140 pub status: AgentRunStatus,
141}
142
143#[derive(Debug, Clone, Default)]
145pub struct AgentRunArtifact {
146 pub id: String,
147 pub filename: String,
148 pub content_type: Option<String>,
149 pub size: Option<i64>,
150 pub r#type: Option<String>,
151 pub metadata: Option<std::collections::HashMap<String, String>>,
152}
153
154#[derive(Debug, Clone)]
156pub struct AgentRunDownload {
157 pub data: Vec<u8>,
158 pub filename: String,
159 pub content_type: Option<String>,
160}
161
162#[derive(Debug, Clone, Default)]
164pub struct AgentRunUsage {
165 pub input_tokens: Option<i64>,
166 pub output_tokens: Option<i64>,
167 pub total_tokens: Option<i64>,
168 pub cache_read_tokens: Option<i64>,
169 pub cache_create_tokens: Option<i64>,
170 pub exact: Option<bool>,
171 pub source: Option<String>,
172 pub raw: Value,
174}
175
176#[derive(Debug, Clone, Default)]
178pub struct AgentRunSettlement {
179 pub request_id: Option<String>,
180 pub status: Option<String>,
181 pub consume_status: Option<String>,
182 pub input_tokens: Option<i64>,
183 pub output_tokens: Option<i64>,
184 pub total_tokens: Option<i64>,
185 pub cache_read_tokens: Option<i64>,
186 pub cache_create_tokens: Option<i64>,
187 pub token_remaining: Option<i64>,
188 pub call_remaining: Option<i64>,
189 pub retry_queued: Option<bool>,
190 pub exact: Option<bool>,
191 pub raw: Value,
192}
193
194#[derive(Debug, Clone, Default)]
196pub struct AgentRunErrorPayload {
197 pub code: Option<String>,
198 pub message: String,
199 pub stage: Option<String>,
200 pub retryable: Option<bool>,
201 pub raw: Option<Value>,
202}
203
204#[derive(Debug, Clone, Default)]
206pub struct AgentRunLocalToolResult {
207 pub request_id: String,
208 pub ok: bool,
209 pub content: Option<Value>,
210 pub error: Option<String>,
211}
212
213#[derive(Debug, Clone)]
215pub struct AgentRunStreamOptions {
216 pub throw_on_error: bool,
218}
219
220impl Default for AgentRunStreamOptions {
221 fn default() -> Self {
222 Self {
223 throw_on_error: true,
224 }
225 }
226}
227
228pub type AgentRunRunOptions = AgentRunStreamOptions;
230
231#[derive(Debug, Clone)]
233pub struct AgentRunWithLocalToolsOptions {
234 pub throw_on_error: bool,
235 pub timeout_ms: u64,
237}
238
239impl Default for AgentRunWithLocalToolsOptions {
240 fn default() -> Self {
241 Self {
242 throw_on_error: true,
243 timeout_ms: 30_000,
244 }
245 }
246}
247
248#[derive(Debug, Clone)]
253pub enum AgentRunStreamEvent {
254 RunStarted {
255 run_id: String,
256 session_id: String,
257 },
258 Status {
259 status: String,
260 message: Option<String>,
261 },
262 TextDelta {
263 text: String,
264 },
265 ReasoningDelta {
266 text: String,
267 },
268 ToolCall {
269 id: String,
270 name: String,
271 input: Option<Value>,
272 },
273 ToolResult {
274 id: String,
275 name: Option<String>,
276 result: Option<Value>,
277 error: Option<String>,
278 },
279 LocalToolRequest {
280 request_id: String,
281 name: String,
282 input: Value,
283 },
284 Artifact {
285 artifact: AgentRunArtifact,
286 },
287 Sources {
288 sources: Value,
289 },
290 Usage {
291 usage: AgentRunUsage,
292 },
293 Settle {
294 settlement: AgentRunSettlement,
295 },
296 Error {
297 error: AgentRunErrorPayload,
298 },
299 Done {
300 run_id: String,
301 status: String,
302 },
303}
304
305impl AgentRunStreamEvent {
306 pub fn type_str(&self) -> &'static str {
308 match self {
309 Self::RunStarted { .. } => "run_started",
310 Self::Status { .. } => "status",
311 Self::TextDelta { .. } => "text_delta",
312 Self::ReasoningDelta { .. } => "reasoning_delta",
313 Self::ToolCall { .. } => "tool_call",
314 Self::ToolResult { .. } => "tool_result",
315 Self::LocalToolRequest { .. } => "local_tool_request",
316 Self::Artifact { .. } => "artifact",
317 Self::Sources { .. } => "sources",
318 Self::Usage { .. } => "usage",
319 Self::Settle { .. } => "settle",
320 Self::Error { .. } => "error",
321 Self::Done { .. } => "done",
322 }
323 }
324}