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
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
463
464
465
466
//! System prompts and dynamic instruction generation.
//!
//! This module provides traits and implementations for generating
//! system prompts and instructions dynamically based on context.

use crate::context::RunContext;
use async_trait::async_trait;
use std::future::Future;
use std::marker::PhantomData;

/// Trait for generating dynamic instructions.
///
/// Instructions are combined with the system prompt to form the
/// complete system message sent to the model.
#[async_trait]
pub trait InstructionFn<Deps>: Send + Sync {
    /// Generate instruction text based on the run context.
    ///
    /// Returns `None` if no instruction should be added.
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String>;
}

/// Trait for generating dynamic system prompts.
///
/// System prompts can be static strings or dynamically generated
/// based on the run context and dependencies.
#[async_trait]
pub trait SystemPromptFn<Deps>: Send + Sync {
    /// Generate system prompt text based on the run context.
    ///
    /// Returns `None` if no prompt should be added.
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String>;
}

// ============================================================================
// Async Function Wrappers
// ============================================================================

/// Wrapper for async instruction functions.
pub struct AsyncInstructionFn<F, Deps, Fut>
where
    F: Fn(&RunContext<Deps>) -> Fut + Send + Sync,
    Fut: Future<Output = Option<String>> + Send,
{
    func: F,
    _phantom: PhantomData<fn(Deps) -> Fut>,
}

impl<F, Deps, Fut> AsyncInstructionFn<F, Deps, Fut>
where
    F: Fn(&RunContext<Deps>) -> Fut + Send + Sync,
    Fut: Future<Output = Option<String>> + Send,
{
    /// Create a new async instruction function.
    pub fn new(func: F) -> Self {
        Self {
            func,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Deps, Fut> InstructionFn<Deps> for AsyncInstructionFn<F, Deps, Fut>
where
    F: Fn(&RunContext<Deps>) -> Fut + Send + Sync,
    Fut: Future<Output = Option<String>> + Send,
    Deps: Send + Sync,
{
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        (self.func)(ctx).await
    }
}

/// Wrapper for async system prompt functions.
pub struct AsyncSystemPromptFn<F, Deps, Fut>
where
    F: Fn(&RunContext<Deps>) -> Fut + Send + Sync,
    Fut: Future<Output = Option<String>> + Send,
{
    func: F,
    _phantom: PhantomData<fn(Deps) -> Fut>,
}

impl<F, Deps, Fut> AsyncSystemPromptFn<F, Deps, Fut>
where
    F: Fn(&RunContext<Deps>) -> Fut + Send + Sync,
    Fut: Future<Output = Option<String>> + Send,
{
    /// Create a new async system prompt function.
    pub fn new(func: F) -> Self {
        Self {
            func,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Deps, Fut> SystemPromptFn<Deps> for AsyncSystemPromptFn<F, Deps, Fut>
where
    F: Fn(&RunContext<Deps>) -> Fut + Send + Sync,
    Fut: Future<Output = Option<String>> + Send,
    Deps: Send + Sync,
{
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        (self.func)(ctx).await
    }
}

// ============================================================================
// Sync Function Wrappers
// ============================================================================

/// Wrapper for sync instruction functions.
pub struct SyncInstructionFn<F, Deps>
where
    F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync,
{
    func: F,
    _phantom: PhantomData<Deps>,
}

impl<F, Deps> SyncInstructionFn<F, Deps>
where
    F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync,
{
    /// Create a new sync instruction function.
    pub fn new(func: F) -> Self {
        Self {
            func,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Deps> InstructionFn<Deps> for SyncInstructionFn<F, Deps>
where
    F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync,
    Deps: Send + Sync,
{
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        (self.func)(ctx)
    }
}

/// Wrapper for sync system prompt functions.
pub struct SyncSystemPromptFn<F, Deps>
where
    F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync,
{
    func: F,
    _phantom: PhantomData<Deps>,
}

impl<F, Deps> SyncSystemPromptFn<F, Deps>
where
    F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync,
{
    /// Create a new sync system prompt function.
    pub fn new(func: F) -> Self {
        Self {
            func,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Deps> SystemPromptFn<Deps> for SyncSystemPromptFn<F, Deps>
where
    F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync,
    Deps: Send + Sync,
{
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        (self.func)(ctx)
    }
}

// ============================================================================
// Static Wrappers
// ============================================================================

/// Static instruction that always returns the same text.
pub struct StaticInstruction {
    text: String,
}

impl StaticInstruction {
    /// Create a new static instruction.
    pub fn new(text: impl Into<String>) -> Self {
        Self { text: text.into() }
    }
}

#[async_trait]
impl<Deps: Send + Sync> InstructionFn<Deps> for StaticInstruction {
    async fn generate(&self, _ctx: &RunContext<Deps>) -> Option<String> {
        Some(self.text.clone())
    }
}

/// Static system prompt that always returns the same text.
pub struct StaticSystemPrompt {
    text: String,
}

impl StaticSystemPrompt {
    /// Create a new static system prompt.
    pub fn new(text: impl Into<String>) -> Self {
        Self { text: text.into() }
    }
}

#[async_trait]
impl<Deps: Send + Sync> SystemPromptFn<Deps> for StaticSystemPrompt {
    async fn generate(&self, _ctx: &RunContext<Deps>) -> Option<String> {
        Some(self.text.clone())
    }
}

// ============================================================================
// Instruction Builder
// ============================================================================

/// Builder for combining multiple instructions.
pub struct InstructionBuilder<Deps> {
    parts: Vec<Box<dyn InstructionFn<Deps>>>,
    separator: String,
}

impl<Deps: Send + Sync + 'static> InstructionBuilder<Deps> {
    /// Create a new instruction builder.
    pub fn new() -> Self {
        Self {
            parts: Vec::new(),
            separator: "\n\n".to_string(),
        }
    }

    /// Set the separator between instruction parts.
    pub fn separator(mut self, sep: impl Into<String>) -> Self {
        self.separator = sep.into();
        self
    }

    /// Add a static instruction.
    #[allow(clippy::should_implement_trait)]
    pub fn add(mut self, text: impl Into<String>) -> Self {
        self.parts.push(Box::new(StaticInstruction::new(text)));
        self
    }

    /// Add a dynamic instruction function.
    pub fn add_fn<F>(mut self, func: F) -> Self
    where
        F: Fn(&RunContext<Deps>) -> Option<String> + Send + Sync + 'static,
    {
        self.parts.push(Box::new(SyncInstructionFn::new(func)));
        self
    }

    /// Add a custom instruction.
    pub fn add_instruction(mut self, instruction: Box<dyn InstructionFn<Deps>>) -> Self {
        self.parts.push(instruction);
        self
    }

    /// Build the combined instruction generator.
    pub fn build(self) -> CombinedInstruction<Deps> {
        CombinedInstruction {
            parts: self.parts,
            separator: self.separator,
        }
    }
}

impl<Deps: Send + Sync + 'static> Default for InstructionBuilder<Deps> {
    fn default() -> Self {
        Self::new()
    }
}

/// Combined instruction from multiple sources.
pub struct CombinedInstruction<Deps> {
    parts: Vec<Box<dyn InstructionFn<Deps>>>,
    separator: String,
}

#[async_trait]
impl<Deps: Send + Sync> InstructionFn<Deps> for CombinedInstruction<Deps> {
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        let mut results = Vec::new();

        for part in &self.parts {
            if let Some(text) = part.generate(ctx).await {
                if !text.is_empty() {
                    results.push(text);
                }
            }
        }

        if results.is_empty() {
            None
        } else {
            Some(results.join(&self.separator))
        }
    }
}

// ============================================================================
// Common Instruction Functions
// ============================================================================

/// Instruction that includes the current date/time.
pub struct DateTimeInstruction {
    format: String,
    prefix: String,
}

impl DateTimeInstruction {
    /// Create with default format.
    pub fn new() -> Self {
        Self {
            format: "%Y-%m-%d %H:%M:%S UTC".to_string(),
            prefix: "Current date and time:".to_string(),
        }
    }

    /// Set custom format.
    pub fn format(mut self, fmt: impl Into<String>) -> Self {
        self.format = fmt.into();
        self
    }

    /// Set prefix text.
    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
        self.prefix = prefix.into();
        self
    }
}

impl Default for DateTimeInstruction {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait]
impl<Deps: Send + Sync> InstructionFn<Deps> for DateTimeInstruction {
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        let formatted = ctx.start_time.format(&self.format).to_string();
        Some(format!("{} {}", self.prefix, formatted))
    }
}

/// Instruction that includes user information.
pub struct UserInfoInstruction<F, Deps>
where
    F: Fn(&Deps) -> Option<String> + Send + Sync,
{
    extractor: F,
    _phantom: PhantomData<Deps>,
}

impl<F, Deps> UserInfoInstruction<F, Deps>
where
    F: Fn(&Deps) -> Option<String> + Send + Sync,
{
    /// Create with a user info extractor.
    pub fn new(extractor: F) -> Self {
        Self {
            extractor,
            _phantom: PhantomData,
        }
    }
}

#[async_trait]
impl<F, Deps> InstructionFn<Deps> for UserInfoInstruction<F, Deps>
where
    F: Fn(&Deps) -> Option<String> + Send + Sync,
    Deps: Send + Sync,
{
    async fn generate(&self, ctx: &RunContext<Deps>) -> Option<String> {
        (self.extractor)(&ctx.deps)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use chrono::Utc;
    use std::sync::Arc;

    fn make_test_context() -> RunContext<()> {
        RunContext {
            deps: Arc::new(()),
            run_id: "test-run".to_string(),
            start_time: Utc::now(),
            model_name: "test-model".to_string(),
            model_settings: Default::default(),
            tool_name: None,
            tool_call_id: None,
            retry_count: 0,
            metadata: None,
        }
    }

    #[tokio::test]
    async fn test_static_instruction() {
        let instruction = StaticInstruction::new("Be helpful.");
        let ctx = make_test_context();
        let result = instruction.generate(&ctx).await;
        assert_eq!(result, Some("Be helpful.".to_string()));
    }

    #[tokio::test]
    async fn test_sync_instruction_fn() {
        let instruction =
            SyncInstructionFn::new(|ctx: &RunContext<()>| Some(format!("Run ID: {}", ctx.run_id)));
        let ctx = make_test_context();
        let result = instruction.generate(&ctx).await;
        assert_eq!(result, Some("Run ID: test-run".to_string()));
    }

    #[tokio::test]
    async fn test_instruction_builder() {
        let instruction = InstructionBuilder::<()>::new()
            .add("First instruction.")
            .add("Second instruction.")
            .build();

        let ctx = make_test_context();
        let result = instruction.generate(&ctx).await.unwrap();

        assert!(result.contains("First instruction."));
        assert!(result.contains("Second instruction."));
    }

    #[tokio::test]
    async fn test_datetime_instruction() {
        let instruction = DateTimeInstruction::new();
        let ctx = make_test_context();
        let result = instruction.generate(&ctx).await.unwrap();

        assert!(result.contains("Current date and time:"));
    }

    #[tokio::test]
    async fn test_combined_instruction_skips_empty() {
        let instruction = InstructionBuilder::<()>::new()
            .add("Has content.")
            .add_fn(|_| None) // Returns None
            .add("") // Empty
            .add("Also has content.")
            .build();

        let ctx = make_test_context();
        let result = instruction.generate(&ctx).await.unwrap();

        let parts: Vec<_> = result.split("\n\n").collect();
        assert_eq!(parts.len(), 2);
    }
}