agtrace-sdk 0.7.1

Public SDK for building observability tools on top of agtrace
Documentation
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
//! Session query types for search, list, and get operations.

use chrono::{DateTime, Utc};
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;

use super::Provider;
use super::filters::EventType;
use crate::types::{AgentEvent, AgentSession, EventPayload, truncate};

// ============================================================================
// Cursor Pagination
// ============================================================================

#[derive(Debug, Serialize, Deserialize)]
pub struct Cursor {
    pub offset: usize,
}

impl Cursor {
    pub fn encode(&self) -> String {
        let json = serde_json::to_string(self).unwrap_or_default();
        base64::Engine::encode(&base64::engine::general_purpose::STANDARD, json.as_bytes())
    }

    pub fn decode(cursor: &str) -> Option<Self> {
        let bytes =
            base64::Engine::decode(&base64::engine::general_purpose::STANDARD, cursor).ok()?;
        let json = String::from_utf8(bytes).ok()?;
        serde_json::from_str(&json).ok()
    }
}

// ============================================================================
// Search Events API
// ============================================================================

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct SearchEventsArgs {
    pub query: String,
    pub session_id: Option<String>,
    #[serde(default)]
    pub limit: Option<usize>,
    #[serde(default)]
    pub cursor: Option<String>,
    pub provider: Option<Provider>,
    pub event_type: Option<EventType>,
    pub project_root: Option<String>,
    pub project_hash: Option<String>,
}

impl SearchEventsArgs {
    pub fn limit(&self) -> usize {
        self.limit.unwrap_or(20).min(50)
    }
}

#[derive(Debug, Serialize)]
pub struct SearchEventsResponse {
    pub matches: Vec<EventMatch>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct EventMatch {
    pub session_id: String,
    pub event_index: usize,
    pub turn_index: usize,
    pub step_index: usize,
    pub event_type: EventType,
    pub preview: String,
    pub timestamp: DateTime<Utc>,
}

impl EventMatch {
    pub fn new(
        session_id: String,
        event_index: usize,
        turn_index: usize,
        step_index: usize,
        event: &AgentEvent,
    ) -> Self {
        let event_type = EventType::from_payload(&event.payload);
        let preview = Self::extract_preview(&event.payload);

        Self {
            session_id,
            event_index,
            turn_index,
            step_index,
            event_type,
            preview,
            timestamp: event.timestamp,
        }
    }

    fn extract_preview(payload: &EventPayload) -> String {
        let text = match payload {
            EventPayload::ToolCall(tc) => {
                serde_json::to_string(tc).unwrap_or_else(|_| String::new())
            }
            EventPayload::ToolResult(tr) => {
                serde_json::to_string(tr).unwrap_or_else(|_| String::new())
            }
            EventPayload::User(u) => u.text.clone(),
            EventPayload::Message(m) => m.text.clone(),
            EventPayload::Reasoning(r) => r.text.clone(),
            EventPayload::TokenUsage(tu) => {
                format!("tokens: in={} out={}", tu.input.total(), tu.output.total())
            }
            EventPayload::Notification(n) => {
                serde_json::to_string(n).unwrap_or_else(|_| String::new())
            }
            EventPayload::SlashCommand(sc) => {
                if let Some(args) = &sc.args {
                    format!("{} {}", sc.name, args)
                } else {
                    sc.name.clone()
                }
            }
            EventPayload::QueueOperation(qo) => {
                format!(
                    "{}{}",
                    qo.operation,
                    qo.content
                        .as_ref()
                        .map(|c| format!(": {}", c))
                        .unwrap_or_default()
                )
            }
            EventPayload::Summary(s) => s.summary.clone(),
        };

        if text.len() > 200 {
            let truncated: String = text.chars().take(200).collect();
            format!("{}...", truncated)
        } else {
            text
        }
    }
}

// ============================================================================
// List Turns API
// ============================================================================

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct ListTurnsArgs {
    pub session_id: String,
    #[serde(default)]
    pub limit: Option<usize>,
    #[serde(default)]
    pub cursor: Option<String>,
}

impl ListTurnsArgs {
    pub fn limit(&self) -> usize {
        self.limit.unwrap_or(50).min(100)
    }
}

#[derive(Debug, Serialize)]
pub struct ListTurnsResponse {
    pub session_id: String,
    pub total_turns: usize,
    pub turns: Vec<TurnMetadata>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct TurnMetadata {
    pub turn_index: usize,
    pub user_content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slash_command: Option<SlashCommandDetail>,
    pub status: TurnStatus,
    pub step_count: usize,
    pub duration_ms: u64,
    pub total_tokens: u64,
    pub tools_used: HashMap<String, usize>,
}

#[derive(Debug, Serialize, PartialEq)]
#[serde(rename_all = "lowercase")]
pub enum TurnStatus {
    Completed,
    Failed,
}

impl ListTurnsResponse {
    pub fn new(
        session: AgentSession,
        offset: usize,
        limit: usize,
        next_cursor: Option<String>,
    ) -> Self {
        let total_turns = session.turns.len();
        let turns: Vec<_> = session
            .turns
            .into_iter()
            .enumerate()
            .skip(offset)
            .take(limit)
            .map(|(idx, turn)| {
                let step_count = turn.steps.len();
                let duration_ms = turn.stats.duration_ms as u64;
                let total_tokens = turn.stats.total_tokens as u64;

                let mut tools_used: HashMap<String, usize> = HashMap::new();
                for step in &turn.steps {
                    for tool in &step.tools {
                        *tools_used
                            .entry(tool.call.content.name().to_string())
                            .or_insert(0) += 1;
                    }
                }

                let status = if turn
                    .steps
                    .iter()
                    .any(|s| s.tools.iter().any(|t| t.is_error))
                {
                    TurnStatus::Failed
                } else {
                    TurnStatus::Completed
                };

                let user_content = truncate(&turn.user.content.text, 100);

                let slash_command =
                    turn.user
                        .slash_command
                        .as_ref()
                        .map(|cmd| SlashCommandDetail {
                            name: cmd.name.clone(),
                            args: cmd.args.clone(),
                        });

                TurnMetadata {
                    turn_index: idx,
                    user_content,
                    slash_command,
                    status,
                    step_count,
                    duration_ms,
                    total_tokens,
                    tools_used,
                }
            })
            .collect();

        Self {
            session_id: session.session_id.to_string(),
            total_turns,
            turns,
            next_cursor,
        }
    }
}

// ============================================================================
// Get Turns API
// ============================================================================

// Data-driven defaults based on actual distribution analysis:
// - 3,000 chars covers P90 (2,552 bytes) without truncation for most events
// - 30 steps covers P50 (15) comfortably and approaches P75 (38), allowing ~70% of turns to be read fully
// Worst case: 3,000 × 30 = 90k chars ≈ 22k tokens (still safe for 1-2 turns per request)
// Average case: 1,176 × 30 ≈ 35k chars ≈ 8k tokens (very safe)
const DEFAULT_MAX_CHARS_PER_FIELD: usize = 3_000;
const DEFAULT_MAX_STEPS_LIMIT: usize = 30;

#[derive(Debug, Serialize, Deserialize, JsonSchema)]
pub struct GetTurnsArgs {
    pub session_id: String,
    pub turn_indices: Vec<usize>,
    #[serde(default = "default_true")]
    pub truncate: Option<bool>,
    #[serde(default)]
    pub max_chars_per_field: Option<usize>,
    #[serde(default)]
    pub max_steps_limit: Option<usize>,
}

fn default_true() -> Option<bool> {
    Some(true)
}

impl GetTurnsArgs {
    pub fn should_truncate(&self) -> bool {
        self.truncate.unwrap_or(true)
    }

    pub fn max_chars(&self) -> usize {
        self.max_chars_per_field
            .unwrap_or(DEFAULT_MAX_CHARS_PER_FIELD)
    }

    pub fn max_steps(&self) -> usize {
        self.max_steps_limit.unwrap_or(DEFAULT_MAX_STEPS_LIMIT)
    }
}

#[derive(Debug, Serialize)]
pub struct GetTurnsResponse {
    pub turns: Vec<TurnDetail>,
}

#[derive(Debug, Serialize)]
pub struct TurnDetail {
    pub turn_index: usize,
    pub user_content: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub slash_command: Option<SlashCommandDetail>,
    pub steps: Vec<StepDetail>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub steps_truncated: Option<bool>,
}

#[derive(Debug, Serialize)]
pub struct SlashCommandDetail {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub args: Option<String>,
}

#[derive(Debug, Serialize)]
pub struct StepDetail {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub reasoning: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
    pub tools: Vec<ToolDetail>,
    pub is_failed: bool,
}

#[derive(Debug, Serialize)]
pub struct ToolDetail {
    pub name: String,
    pub args: String,
    pub result: Option<String>,
    pub is_error: bool,
}

impl GetTurnsResponse {
    pub fn new(session: AgentSession, args: &GetTurnsArgs) -> Result<Self, String> {
        let should_truncate = args.should_truncate();
        let max_chars = args.max_chars();
        let max_steps = args.max_steps();

        let mut turns = Vec::new();

        for &turn_index in &args.turn_indices {
            if turn_index >= session.turns.len() {
                return Err(format!(
                    "Turn index {} out of range (session has {} turns)",
                    turn_index,
                    session.turns.len()
                ));
            }

            let turn = &session.turns[turn_index];
            let user_content = if should_truncate {
                truncate(&turn.user.content.text, max_chars)
            } else {
                turn.user.content.text.clone()
            };

            let total_steps = turn.steps.len();
            let steps_to_process = if should_truncate && total_steps > max_steps {
                &turn.steps[..max_steps]
            } else {
                &turn.steps[..]
            };

            let steps: Vec<StepDetail> = steps_to_process
                .iter()
                .map(|step| {
                    let reasoning = step.reasoning.as_ref().map(|r| {
                        if should_truncate {
                            truncate(&r.content.text, max_chars)
                        } else {
                            r.content.text.clone()
                        }
                    });

                    let message = step.message.as_ref().map(|m| {
                        if should_truncate {
                            truncate(&m.content.text, max_chars)
                        } else {
                            m.content.text.clone()
                        }
                    });

                    let tools: Vec<ToolDetail> = step
                        .tools
                        .iter()
                        .map(|tool| {
                            let args_json = serde_json::to_string(&tool.call.content)
                                .unwrap_or_else(|_| String::from("{}"));
                            let result_json = tool.result.as_ref().map(|r| {
                                serde_json::to_string(r).unwrap_or_else(|_| String::from("{}"))
                            });

                            ToolDetail {
                                name: tool.call.content.name().to_string(),
                                args: if should_truncate {
                                    truncate(&args_json, max_chars)
                                } else {
                                    args_json
                                },
                                result: result_json.map(|r| {
                                    if should_truncate {
                                        truncate(&r, max_chars)
                                    } else {
                                        r
                                    }
                                }),
                                is_error: tool.is_error,
                            }
                        })
                        .collect();

                    StepDetail {
                        reasoning,
                        message,
                        tools,
                        is_failed: step.is_failed,
                    }
                })
                .collect();

            let steps_truncated = if should_truncate && total_steps > max_steps {
                Some(true)
            } else {
                None
            };

            let slash_command = turn
                .user
                .slash_command
                .as_ref()
                .map(|cmd| SlashCommandDetail {
                    name: cmd.name.clone(),
                    args: cmd.args.clone(),
                });

            turns.push(TurnDetail {
                turn_index,
                user_content,
                slash_command,
                steps,
                steps_truncated,
            });
        }

        Ok(Self { turns })
    }
}