Skip to main content

systemprompt_logging/trace/
models.rs

1use chrono::{DateTime, Utc};
2use serde::{Deserialize, Serialize};
3use serde_json::Value;
4use systemprompt_identifiers::{
5    AiRequestId, ArtifactId, ContextId, ExecutionStepId, LogId, McpExecutionId, SessionId, TaskId,
6    TraceId, UserId,
7};
8
9#[derive(Debug, Clone)]
10pub struct TraceListFilter {
11    pub limit: i64,
12    pub since: Option<DateTime<Utc>>,
13    pub agent: Option<String>,
14    pub status: Option<String>,
15    pub tool: Option<String>,
16    pub has_mcp: bool,
17    pub include_system: bool,
18}
19
20impl TraceListFilter {
21    pub const fn new(limit: i64) -> Self {
22        Self {
23            limit,
24            since: None,
25            agent: None,
26            status: None,
27            tool: None,
28            has_mcp: false,
29            include_system: false,
30        }
31    }
32
33    pub const fn with_since(mut self, since: DateTime<Utc>) -> Self {
34        self.since = Some(since);
35        self
36    }
37
38    systemprompt_models::builder_methods! {
39        with_agent(agent) -> String,
40        with_status(status) -> String,
41        with_tool(tool) -> String,
42    }
43
44    pub const fn with_has_mcp(mut self, has_mcp: bool) -> Self {
45        self.has_mcp = has_mcp;
46        self
47    }
48
49    pub const fn with_include_system(mut self, include_system: bool) -> Self {
50        self.include_system = include_system;
51        self
52    }
53}
54
55#[derive(Debug, Clone, Serialize, Deserialize)]
56pub struct TraceListItem {
57    pub trace_id: TraceId,
58    pub first_timestamp: DateTime<Utc>,
59    pub last_timestamp: DateTime<Utc>,
60    pub agent: Option<String>,
61    pub status: Option<String>,
62    pub ai_requests: i64,
63    pub mcp_calls: i64,
64}
65
66#[derive(Debug, Clone)]
67pub struct ToolExecutionFilter {
68    pub limit: i64,
69    pub since: Option<DateTime<Utc>>,
70    pub name: Option<String>,
71    pub server: Option<String>,
72    pub status: Option<String>,
73}
74
75impl ToolExecutionFilter {
76    pub const fn new(limit: i64) -> Self {
77        Self {
78            limit,
79            since: None,
80            name: None,
81            server: None,
82            status: None,
83        }
84    }
85
86    pub const fn with_since(mut self, since: DateTime<Utc>) -> Self {
87        self.since = Some(since);
88        self
89    }
90
91    systemprompt_models::builder_methods! {
92        with_name(name) -> String,
93        with_server(server) -> String,
94        with_status(status) -> String,
95    }
96}
97
98#[derive(Debug, Clone, Serialize, Deserialize)]
99pub struct ToolExecutionItem {
100    pub timestamp: DateTime<Utc>,
101    pub trace_id: TraceId,
102    pub tool_name: String,
103    pub server_name: Option<String>,
104    pub status: String,
105    pub execution_time_ms: Option<i32>,
106}
107
108#[derive(Debug, Clone)]
109pub struct LogSearchFilter {
110    pub pattern: String,
111    pub limit: i64,
112    pub since: Option<DateTime<Utc>>,
113    pub level: Option<String>,
114}
115
116impl LogSearchFilter {
117    pub const fn new(pattern: String, limit: i64) -> Self {
118        Self {
119            pattern,
120            limit,
121            since: None,
122            level: None,
123        }
124    }
125
126    pub const fn with_since(mut self, since: DateTime<Utc>) -> Self {
127        self.since = Some(since);
128        self
129    }
130
131    systemprompt_models::builder_methods! {
132        with_level(level) -> String,
133    }
134}
135
136#[derive(Debug, Clone, Serialize, Deserialize)]
137pub struct LogSearchItem {
138    pub id: LogId,
139    pub trace_id: TraceId,
140    pub timestamp: DateTime<Utc>,
141    pub level: String,
142    pub module: String,
143    pub message: String,
144    pub metadata: Option<String>,
145}
146
147#[derive(Debug, Clone)]
148pub struct AiRequestFilter {
149    pub limit: i64,
150    pub since: Option<DateTime<Utc>>,
151    pub model: Option<String>,
152    pub provider: Option<String>,
153}
154
155impl AiRequestFilter {
156    pub const fn new(limit: i64) -> Self {
157        Self {
158            limit,
159            since: None,
160            model: None,
161            provider: None,
162        }
163    }
164
165    pub const fn with_since(mut self, since: DateTime<Utc>) -> Self {
166        self.since = Some(since);
167        self
168    }
169
170    systemprompt_models::builder_methods! {
171        with_model(model) -> String,
172        with_provider(provider) -> String,
173    }
174}
175
176#[derive(Debug, Clone, Serialize, Deserialize)]
177pub struct AiRequestListItem {
178    pub id: AiRequestId,
179    pub created_at: DateTime<Utc>,
180    pub trace_id: Option<TraceId>,
181    pub provider: String,
182    pub model: String,
183    pub input_tokens: Option<i32>,
184    pub output_tokens: Option<i32>,
185    pub cost_microdollars: i64,
186    pub latency_ms: Option<i32>,
187    pub status: String,
188}
189
190#[derive(Debug, Clone, Serialize, Deserialize)]
191pub struct AiRequestDetail {
192    pub id: AiRequestId,
193    pub provider: String,
194    pub model: String,
195    pub input_tokens: Option<i32>,
196    pub output_tokens: Option<i32>,
197    pub cost_microdollars: i64,
198    pub latency_ms: Option<i32>,
199    pub status: String,
200    pub error_message: Option<String>,
201}
202
203#[derive(Debug, Clone, Default, Serialize, Deserialize)]
204pub struct AiRequestStats {
205    pub total_requests: i64,
206    pub total_input_tokens: i64,
207    pub total_output_tokens: i64,
208    pub total_cost_microdollars: i64,
209    pub avg_latency_ms: i64,
210    pub by_provider: Vec<ProviderStatsRow>,
211    pub by_model: Vec<ModelStatsRow>,
212}
213
214#[derive(Debug, Clone, Serialize, Deserialize)]
215pub struct ProviderStatsRow {
216    pub provider: String,
217    pub request_count: i64,
218    pub total_tokens: i64,
219    pub total_cost_microdollars: i64,
220    pub avg_latency_ms: i64,
221}
222
223#[derive(Debug, Clone, Serialize, Deserialize)]
224pub struct ModelStatsRow {
225    pub model: String,
226    pub provider: String,
227    pub request_count: i64,
228    pub total_tokens: i64,
229    pub total_cost_microdollars: i64,
230    pub avg_latency_ms: i64,
231}
232
233#[derive(Debug, Clone, Serialize, Deserialize)]
234pub struct AuditLookupResult {
235    pub id: AiRequestId,
236    pub provider: String,
237    pub model: String,
238    pub input_tokens: Option<i32>,
239    pub output_tokens: Option<i32>,
240    pub cost_microdollars: i64,
241    pub latency_ms: Option<i32>,
242    pub task_id: Option<TaskId>,
243    pub trace_id: Option<TraceId>,
244}
245
246#[derive(Debug, Clone, Serialize, Deserialize)]
247pub struct AuditToolCallRow {
248    pub tool_name: String,
249    pub tool_input: String,
250    pub sequence_number: i32,
251}
252
253#[derive(Debug, Clone, Serialize, Deserialize)]
254pub struct LinkedMcpCall {
255    pub tool_name: String,
256    pub server_name: String,
257    pub status: String,
258    pub execution_time_ms: Option<i32>,
259}
260
261#[derive(Debug, Clone, Serialize, Deserialize)]
262pub struct TraceEvent {
263    pub event_type: String,
264    pub timestamp: DateTime<Utc>,
265    pub details: String,
266    pub user_id: Option<UserId>,
267    pub session_id: Option<SessionId>,
268    pub task_id: Option<TaskId>,
269    pub context_id: Option<ContextId>,
270    pub metadata: Option<String>,
271}
272
273#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
274pub struct AiRequestSummary {
275    pub total_cost_microdollars: i64,
276    pub total_tokens: i64,
277    pub total_input_tokens: i64,
278    pub total_output_tokens: i64,
279    pub request_count: i64,
280    pub total_latency_ms: i64,
281}
282
283#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
284pub struct McpExecutionSummary {
285    pub execution_count: i64,
286    pub total_execution_time_ms: i64,
287}
288
289#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
290pub struct ExecutionStepSummary {
291    #[serde(rename = "step_count")]
292    pub total: i64,
293    #[serde(rename = "completed_count")]
294    pub completed: i64,
295    #[serde(rename = "failed_count")]
296    pub failed: i64,
297    #[serde(rename = "pending_count")]
298    pub pending: i64,
299}
300
301#[derive(Debug, Clone, Serialize, Deserialize)]
302pub struct TaskInfo {
303    pub task_id: TaskId,
304    pub context_id: ContextId,
305    pub agent_name: Option<String>,
306    pub status: String,
307    pub created_at: DateTime<Utc>,
308    pub started_at: Option<DateTime<Utc>>,
309    pub completed_at: Option<DateTime<Utc>>,
310    pub execution_time_ms: Option<i32>,
311    pub error_message: Option<String>,
312}
313
314#[derive(Debug, Clone, Serialize, Deserialize)]
315pub struct ExecutionStep {
316    pub step_id: ExecutionStepId,
317    pub step_type: Option<String>,
318    pub title: Option<String>,
319    pub status: String,
320    pub duration_ms: Option<i32>,
321    pub error_message: Option<String>,
322}
323
324#[derive(Debug, Clone, Serialize, Deserialize)]
325pub struct AiRequestInfo {
326    pub id: AiRequestId,
327    pub provider: String,
328    pub model: String,
329    pub max_tokens: Option<i32>,
330    pub input_tokens: Option<i32>,
331    pub output_tokens: Option<i32>,
332    pub cost_microdollars: i64,
333    pub latency_ms: Option<i32>,
334}
335
336#[derive(Debug, Clone, Serialize, Deserialize)]
337pub struct McpToolExecution {
338    pub mcp_execution_id: McpExecutionId,
339    pub tool_name: String,
340    pub server_name: String,
341    pub status: String,
342    pub execution_time_ms: Option<i32>,
343    pub error_message: Option<String>,
344    pub input: String,
345    pub output: Option<String>,
346}
347
348#[derive(Debug, Clone, Serialize, Deserialize)]
349pub struct ConversationMessage {
350    pub role: String,
351    pub content: String,
352    pub sequence_number: i32,
353}
354
355#[derive(Debug, Clone, Serialize, Deserialize)]
356pub struct ToolLogEntry {
357    pub timestamp: DateTime<Utc>,
358    pub level: String,
359    pub module: String,
360    pub message: String,
361}
362
363#[derive(Debug, Clone, Serialize, Deserialize)]
364pub struct TaskArtifact {
365    pub artifact_id: ArtifactId,
366    pub artifact_type: String,
367    pub name: Option<String>,
368    pub source: Option<String>,
369    pub tool_name: Option<String>,
370    pub part_kind: Option<String>,
371    pub text_content: Option<String>,
372    pub data_content: Option<Value>,
373}
374
375#[derive(Debug, Clone, Serialize, Deserialize)]
376pub struct LevelCount {
377    pub level: String,
378    pub count: i64,
379}
380
381#[derive(Debug, Clone, Serialize, Deserialize)]
382pub struct ModuleCount {
383    pub module: String,
384    pub count: i64,
385}
386
387#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
388pub struct LogTimeRange {
389    pub earliest: Option<DateTime<Utc>>,
390    pub latest: Option<DateTime<Utc>>,
391}