serdes-ai-agent 0.2.6

Agent implementation for serdes-ai
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
//! Run context and state management.
//!
//! The context contains all information about the current agent run,
//! including dependencies, settings, and execution state.

use chrono::{DateTime, Utc};
use serde_json::Value as JsonValue;
use serdes_ai_core::ModelSettings;
use std::sync::Arc;

/// Context for an agent run.
///
/// The context is passed to tools, instruction functions, and validators.
/// It provides access to dependencies and run metadata.
#[derive(Debug)]
pub struct RunContext<Deps> {
    /// Shared dependencies.
    pub deps: Arc<Deps>,
    /// Unique run identifier.
    pub run_id: String,
    /// Run start time.
    pub start_time: DateTime<Utc>,
    /// Model being used.
    pub model_name: String,
    /// Model settings for this run.
    pub model_settings: ModelSettings,
    /// Current tool being executed (if any).
    pub tool_name: Option<String>,
    /// Current tool call ID (if any).
    pub tool_call_id: Option<String>,
    /// Current retry count.
    pub retry_count: u32,
    /// Custom metadata.
    pub metadata: Option<JsonValue>,
}

impl<Deps> RunContext<Deps> {
    /// Create a new run context.
    pub fn new(deps: Deps, model_name: impl Into<String>) -> Self {
        Self {
            deps: Arc::new(deps),
            run_id: generate_run_id(),
            start_time: Utc::now(),
            model_name: model_name.into(),
            model_settings: ModelSettings::default(),
            tool_name: None,
            tool_call_id: None,
            retry_count: 0,
            metadata: None,
        }
    }

    /// Create with shared dependencies.
    pub fn with_shared_deps(deps: Arc<Deps>, model_name: impl Into<String>) -> Self {
        Self {
            deps,
            run_id: generate_run_id(),
            start_time: Utc::now(),
            model_name: model_name.into(),
            model_settings: ModelSettings::default(),
            tool_name: None,
            tool_call_id: None,
            retry_count: 0,
            metadata: None,
        }
    }

    /// Get a reference to the dependencies.
    pub fn deps(&self) -> &Deps {
        &self.deps
    }

    /// Get elapsed time since run started.
    pub fn elapsed(&self) -> chrono::Duration {
        Utc::now() - self.start_time
    }

    /// Get elapsed time in seconds.
    pub fn elapsed_seconds(&self) -> i64 {
        self.elapsed().num_seconds()
    }

    /// Check if this is a retry.
    pub fn is_retry(&self) -> bool {
        self.retry_count > 0
    }

    /// Check if we're currently in a tool execution.
    pub fn in_tool(&self) -> bool {
        self.tool_name.is_some()
    }

    /// Set metadata value.
    pub fn set_metadata(&mut self, key: &str, value: impl serde::Serialize) {
        let meta = self
            .metadata
            .get_or_insert_with(|| JsonValue::Object(Default::default()));
        if let JsonValue::Object(ref mut map) = meta {
            if let Ok(v) = serde_json::to_value(value) {
                map.insert(key.to_string(), v);
            }
        }
    }

    /// Get metadata value.
    pub fn get_metadata<T: serde::de::DeserializeOwned>(&self, key: &str) -> Option<T> {
        self.metadata
            .as_ref()
            .and_then(|m| m.get(key))
            .and_then(|v| serde_json::from_value(v.clone()).ok())
    }

    /// Clone with a new tool context.
    pub fn for_tool(&self, tool_name: impl Into<String>, tool_call_id: Option<String>) -> Self {
        Self {
            deps: self.deps.clone(),
            run_id: self.run_id.clone(),
            start_time: self.start_time,
            model_name: self.model_name.clone(),
            model_settings: self.model_settings.clone(),
            tool_name: Some(tool_name.into()),
            tool_call_id,
            retry_count: 0,
            metadata: self.metadata.clone(),
        }
    }

    /// Clone for a retry.
    pub fn for_retry(&self) -> Self {
        Self {
            deps: self.deps.clone(),
            run_id: self.run_id.clone(),
            start_time: self.start_time,
            model_name: self.model_name.clone(),
            model_settings: self.model_settings.clone(),
            tool_name: self.tool_name.clone(),
            tool_call_id: self.tool_call_id.clone(),
            retry_count: self.retry_count + 1,
            metadata: self.metadata.clone(),
        }
    }
}

impl<Deps: Default> Default for RunContext<Deps> {
    fn default() -> Self {
        Self::new(Deps::default(), "unknown")
    }
}

impl<Deps> Clone for RunContext<Deps> {
    fn clone(&self) -> Self {
        Self {
            deps: self.deps.clone(),
            run_id: self.run_id.clone(),
            start_time: self.start_time,
            model_name: self.model_name.clone(),
            model_settings: self.model_settings.clone(),
            tool_name: self.tool_name.clone(),
            tool_call_id: self.tool_call_id.clone(),
            retry_count: self.retry_count,
            metadata: self.metadata.clone(),
        }
    }
}

/// Generate a unique run ID.
pub fn generate_run_id() -> String {
    uuid::Uuid::new_v4().to_string()
}

/// Usage tracking for a run.
#[derive(Debug, Clone, Default)]
pub struct RunUsage {
    /// Total request tokens.
    pub request_tokens: u64,
    /// Total response tokens.
    pub response_tokens: u64,
    /// Total tokens.
    pub total_tokens: u64,
    /// Number of model requests.
    pub request_count: u32,
    /// Number of tool calls.
    pub tool_call_count: u32,
    /// Cache creation tokens.
    pub cache_creation_tokens: Option<u64>,
    /// Cache read tokens.
    pub cache_read_tokens: Option<u64>,
}

impl RunUsage {
    /// Create new empty usage.
    pub fn new() -> Self {
        Self::default()
    }

    /// Add usage from a model request.
    pub fn add_request(&mut self, usage: serdes_ai_core::RequestUsage) {
        if let Some(req) = usage.request_tokens {
            self.request_tokens += req;
        }
        if let Some(resp) = usage.response_tokens {
            self.response_tokens += resp;
        }
        if let Some(total) = usage.total_tokens {
            self.total_tokens += total;
        } else {
            self.total_tokens = self.request_tokens + self.response_tokens;
        }
        if let Some(cache) = usage.cache_creation_tokens {
            *self.cache_creation_tokens.get_or_insert(0) += cache;
        }
        if let Some(cache) = usage.cache_read_tokens {
            *self.cache_read_tokens.get_or_insert(0) += cache;
        }
        self.request_count += 1;
    }

    /// Record a tool call.
    pub fn record_tool_call(&mut self) {
        self.tool_call_count += 1;
    }
}

/// Usage limits for a run.
#[derive(Debug, Clone, Default)]
pub struct UsageLimits {
    /// Maximum request tokens.
    pub max_request_tokens: Option<u64>,
    /// Maximum response tokens.
    pub max_response_tokens: Option<u64>,
    /// Maximum total tokens.
    pub max_total_tokens: Option<u64>,
    /// Maximum number of requests.
    pub max_requests: Option<u32>,
    /// Maximum number of tool calls.
    pub max_tool_calls: Option<u32>,
    /// Maximum run time in seconds.
    pub max_time_seconds: Option<u64>,
}

impl UsageLimits {
    /// Create new empty limits.
    pub fn new() -> Self {
        Self::default()
    }

    /// Set max request tokens.
    pub fn request_tokens(mut self, limit: u64) -> Self {
        self.max_request_tokens = Some(limit);
        self
    }

    /// Set max response tokens.
    pub fn response_tokens(mut self, limit: u64) -> Self {
        self.max_response_tokens = Some(limit);
        self
    }

    /// Set max total tokens.
    pub fn total_tokens(mut self, limit: u64) -> Self {
        self.max_total_tokens = Some(limit);
        self
    }

    /// Set max requests.
    pub fn requests(mut self, limit: u32) -> Self {
        self.max_requests = Some(limit);
        self
    }

    /// Set max tool calls.
    pub fn tool_calls(mut self, limit: u32) -> Self {
        self.max_tool_calls = Some(limit);
        self
    }

    /// Set max time in seconds.
    pub fn time_seconds(mut self, limit: u64) -> Self {
        self.max_time_seconds = Some(limit);
        self
    }

    /// Check usage against limits.
    pub fn check(&self, usage: &RunUsage) -> Result<(), crate::errors::UsageLimitError> {
        use crate::errors::UsageLimitError;

        if let Some(limit) = self.max_request_tokens {
            if usage.request_tokens > limit {
                return Err(UsageLimitError::RequestTokens {
                    used: usage.request_tokens,
                    limit,
                });
            }
        }

        if let Some(limit) = self.max_response_tokens {
            if usage.response_tokens > limit {
                return Err(UsageLimitError::ResponseTokens {
                    used: usage.response_tokens,
                    limit,
                });
            }
        }

        if let Some(limit) = self.max_total_tokens {
            if usage.total_tokens > limit {
                return Err(UsageLimitError::TotalTokens {
                    used: usage.total_tokens,
                    limit,
                });
            }
        }

        if let Some(limit) = self.max_requests {
            if usage.request_count > limit {
                return Err(UsageLimitError::RequestCount {
                    count: usage.request_count,
                    limit,
                });
            }
        }

        if let Some(limit) = self.max_tool_calls {
            if usage.tool_call_count > limit {
                return Err(UsageLimitError::ToolCalls {
                    count: usage.tool_call_count,
                    limit,
                });
            }
        }

        Ok(())
    }

    /// Check time limit.
    pub fn check_time(&self, elapsed_seconds: u64) -> Result<(), crate::errors::UsageLimitError> {
        if let Some(limit) = self.max_time_seconds {
            if elapsed_seconds > limit {
                return Err(crate::errors::UsageLimitError::TimeLimit {
                    elapsed_seconds,
                    limit_seconds: limit,
                });
            }
        }
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_run_context_new() {
        let ctx = RunContext::new((), "gpt-4o");
        assert_eq!(ctx.model_name, "gpt-4o");
        assert!(!ctx.run_id.is_empty());
    }

    #[test]
    fn test_run_context_metadata() {
        let mut ctx = RunContext::new((), "gpt-4o");
        ctx.set_metadata("user_id", "12345");

        let user_id: Option<String> = ctx.get_metadata("user_id");
        assert_eq!(user_id, Some("12345".to_string()));
    }

    #[test]
    fn test_run_context_for_tool() {
        let ctx = RunContext::new((), "gpt-4o");
        let tool_ctx = ctx.for_tool("search", Some("call-123".to_string()));

        assert_eq!(tool_ctx.tool_name, Some("search".to_string()));
        assert_eq!(tool_ctx.tool_call_id, Some("call-123".to_string()));
        assert!(tool_ctx.in_tool());
    }

    #[test]
    fn test_run_usage() {
        let mut usage = RunUsage::new();
        usage.add_request(serdes_ai_core::RequestUsage {
            request_tokens: Some(100),
            response_tokens: Some(50),
            total_tokens: Some(150),
            cache_creation_tokens: None,
            cache_read_tokens: None,
            details: None,
        });

        assert_eq!(usage.request_tokens, 100);
        assert_eq!(usage.response_tokens, 50);
        assert_eq!(usage.request_count, 1);
    }

    #[test]
    fn test_usage_limits() {
        let limits = UsageLimits::new().total_tokens(1000).requests(10);

        let mut usage = RunUsage::new();
        usage.total_tokens = 500;
        usage.request_count = 5;

        assert!(limits.check(&usage).is_ok());

        usage.total_tokens = 1500;
        assert!(limits.check(&usage).is_err());
    }
}