synaptic-middleware 0.4.0

Middleware system for Synaptic agents: AgentMiddleware trait and built-in middlewares
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
mod callback_adapter;
mod circuit_breaker;
mod context_editing;
mod human_in_the_loop;
mod model_call_limit;
mod model_fallback;
mod security;
mod ssrf_guard;
mod summarization;
mod todo_list;
mod tool_call_limit;
mod tool_retry;

pub use callback_adapter::CallbackMiddleware;
pub use circuit_breaker::{CircuitBreakerConfig, CircuitBreakerMiddleware, CircuitState};
pub use context_editing::{ContextEditingMiddleware, ContextStrategy};
pub use human_in_the_loop::{ApprovalCallback, HumanInTheLoopMiddleware};
pub use model_call_limit::ModelCallLimitMiddleware;
pub use model_fallback::ModelFallbackMiddleware;
pub use security::{
    ConfirmationPolicy, RiskLevel, RuleBasedAnalyzer, SecurityAnalyzer,
    SecurityConfirmationCallback, SecurityMiddleware, ThresholdConfirmationPolicy,
};
pub use ssrf_guard::{SsrfGuardConfig, SsrfGuardMiddleware};
pub use summarization::SummarizationMiddleware;
pub use todo_list::TodoListMiddleware;
pub use tool_call_limit::ToolCallLimitMiddleware;
pub use tool_retry::ToolRetryMiddleware;

use std::sync::Arc;

use async_trait::async_trait;
use serde_json::Value;
use synaptic_core::{
    ChatModel, ChatRequest, ChatResponse, Message, SynapticError, TokenUsage, ToolCall, ToolChoice,
    ToolDefinition,
};

// ---------------------------------------------------------------------------
// ModelRequest / ModelResponse — middleware-visible request & response types
// ---------------------------------------------------------------------------

/// A model invocation request visible to middleware.
///
/// Contains all parameters that will be sent to the `ChatModel`, plus
/// the optional system prompt managed by the agent builder.
#[derive(Debug, Clone)]
pub struct ModelRequest {
    pub messages: Vec<Message>,
    pub tools: Vec<ToolDefinition>,
    pub tool_choice: Option<ToolChoice>,
    pub system_prompt: Option<String>,
}

impl ModelRequest {
    /// Convert to a `ChatRequest` suitable for calling a `ChatModel`.
    pub fn to_chat_request(&self) -> ChatRequest {
        let mut messages = Vec::new();
        if let Some(ref prompt) = self.system_prompt {
            messages.push(Message::system(prompt));
        }
        messages.extend(self.messages.clone());
        let mut req = ChatRequest::new(messages).with_tools(self.tools.clone());
        if let Some(ref choice) = self.tool_choice {
            req = req.with_tool_choice(choice.clone());
        }
        req
    }
}

/// A model invocation response visible to middleware.
#[derive(Debug, Clone)]
pub struct ModelResponse {
    pub message: Message,
    pub usage: Option<TokenUsage>,
}

impl From<ChatResponse> for ModelResponse {
    fn from(resp: ChatResponse) -> Self {
        Self {
            message: resp.message,
            usage: resp.usage,
        }
    }
}

// ---------------------------------------------------------------------------
// ToolCallRequest — wrapper around a single tool call
// ---------------------------------------------------------------------------

/// A single tool call request visible to middleware.
#[derive(Debug, Clone)]
pub struct ToolCallRequest {
    pub call: ToolCall,
}

// ---------------------------------------------------------------------------
// File/Shell hook types
// ---------------------------------------------------------------------------

/// Describes a file operation intercepted by middleware.
#[derive(Debug, Clone)]
pub struct FileOp {
    pub path: String,
    pub kind: FileOpKind,
}

/// The kind of file operation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FileOpKind {
    Read,
    Write,
    Delete,
}

/// Result of a file operation.
#[derive(Debug, Clone)]
pub struct FileOpResult {
    pub success: bool,
    pub error: Option<String>,
}

/// Decision for a file operation.
#[derive(Debug, Clone)]
pub enum FileOpDecision {
    /// Allow the operation to proceed.
    Allow,
    /// Deny the operation with a reason.
    Deny(String),
}

/// Describes a shell command intercepted by middleware.
#[derive(Debug, Clone)]
pub struct CommandOp {
    pub command: String,
    pub args: Vec<String>,
    pub working_dir: Option<String>,
}

/// Result of a command execution.
#[derive(Debug, Clone)]
pub struct CommandResult {
    pub exit_code: i32,
    pub stdout: String,
    pub stderr: String,
}

/// Decision for a command execution.
#[derive(Debug, Clone)]
pub enum CommandDecision {
    Allow,
    Deny(String),
}

// ---------------------------------------------------------------------------
// ModelCaller / ToolCaller — "next" in the middleware chain
// ---------------------------------------------------------------------------

/// Trait representing the next step in the model call chain.
///
/// The innermost implementation calls the actual `ChatModel`; outer
/// layers are middleware `wrap_model_call` implementations.
#[async_trait]
pub trait ModelCaller: Send + Sync {
    async fn call(&self, request: ModelRequest) -> Result<ModelResponse, SynapticError>;
}

/// Trait representing the next step in the tool call chain.
#[async_trait]
pub trait ToolCaller: Send + Sync {
    async fn call(&self, request: ToolCallRequest) -> Result<Value, SynapticError>;
}

// ---------------------------------------------------------------------------
// AgentMiddleware trait
// ---------------------------------------------------------------------------

/// Middleware that can intercept and modify agent lifecycle events.
///
/// All methods have default no-op implementations, so middleware only
/// needs to override the hooks it cares about.
///
/// # Lifecycle order
///
/// ```text
/// before_agent
///   loop {
///     before_model  ->  wrap_model_call  ->  after_model
///     for each tool_call { wrap_tool_call }
///   }
/// after_agent
/// ```
#[async_trait]
pub trait AgentMiddleware: Send + Sync {
    /// Called once when the agent starts executing.
    async fn before_agent(&self, _messages: &mut Vec<Message>) -> Result<(), SynapticError> {
        Ok(())
    }

    /// Called once when the agent finishes executing.
    async fn after_agent(&self, _messages: &mut Vec<Message>) -> Result<(), SynapticError> {
        Ok(())
    }

    /// Called before each model invocation. Can modify the request.
    async fn before_model(&self, _request: &mut ModelRequest) -> Result<(), SynapticError> {
        Ok(())
    }

    /// Called after each model invocation. Can modify the response.
    async fn after_model(
        &self,
        _request: &ModelRequest,
        _response: &mut ModelResponse,
    ) -> Result<(), SynapticError> {
        Ok(())
    }

    /// Wraps the model call. Override to intercept or replace the model invocation.
    async fn wrap_model_call(
        &self,
        request: ModelRequest,
        next: &dyn ModelCaller,
    ) -> Result<ModelResponse, SynapticError> {
        next.call(request).await
    }

    /// Wraps a tool call. Override to intercept or replace tool execution.
    async fn wrap_tool_call(
        &self,
        request: ToolCallRequest,
        next: &dyn ToolCaller,
    ) -> Result<Value, SynapticError> {
        next.call(request).await
    }

    /// Called before a file operation. Return `Deny` to block it.
    async fn before_file_op(&self, _op: &FileOp) -> Result<FileOpDecision, SynapticError> {
        Ok(FileOpDecision::Allow)
    }

    /// Called after a file operation completes.
    async fn after_file_op(
        &self,
        _op: &FileOp,
        _result: &FileOpResult,
    ) -> Result<(), SynapticError> {
        Ok(())
    }

    /// Called before a shell command. Return `Deny` to block it.
    async fn before_command(&self, _cmd: &CommandOp) -> Result<CommandDecision, SynapticError> {
        Ok(CommandDecision::Allow)
    }

    /// Called after a shell command completes.
    async fn after_command(
        &self,
        _cmd: &CommandOp,
        _result: &CommandResult,
    ) -> Result<(), SynapticError> {
        Ok(())
    }
}

// ---------------------------------------------------------------------------
// MiddlewareChain — composes multiple middlewares
// ---------------------------------------------------------------------------

/// A chain of middlewares that executes them in order.
pub struct MiddlewareChain {
    middlewares: Vec<Arc<dyn AgentMiddleware>>,
}

impl MiddlewareChain {
    pub fn new(middlewares: Vec<Arc<dyn AgentMiddleware>>) -> Self {
        Self { middlewares }
    }

    pub fn is_empty(&self) -> bool {
        self.middlewares.is_empty()
    }

    pub async fn run_before_agent(&self, messages: &mut Vec<Message>) -> Result<(), SynapticError> {
        for mw in &self.middlewares {
            mw.before_agent(messages).await?;
        }
        Ok(())
    }

    pub async fn run_after_agent(&self, messages: &mut Vec<Message>) -> Result<(), SynapticError> {
        for mw in self.middlewares.iter().rev() {
            mw.after_agent(messages).await?;
        }
        Ok(())
    }

    pub async fn run_before_model(&self, request: &mut ModelRequest) -> Result<(), SynapticError> {
        for mw in &self.middlewares {
            mw.before_model(request).await?;
        }
        Ok(())
    }

    pub async fn run_after_model(
        &self,
        request: &ModelRequest,
        response: &mut ModelResponse,
    ) -> Result<(), SynapticError> {
        for mw in self.middlewares.iter().rev() {
            mw.after_model(request, response).await?;
        }
        Ok(())
    }

    /// Execute a model call through the full middleware chain.
    ///
    /// Runs the complete lifecycle: `before_model` -> `wrap_model_call`
    /// chain -> `after_model`.
    pub async fn call_model(
        &self,
        mut request: ModelRequest,
        base: &dyn ModelCaller,
    ) -> Result<ModelResponse, SynapticError> {
        // Run before_model hooks
        self.run_before_model(&mut request).await?;

        // Build the wrapped call chain (outermost first)
        let mut response = if self.middlewares.is_empty() {
            base.call(request.clone()).await?
        } else {
            let chain = WrapModelChain {
                middlewares: &self.middlewares,
                index: 0,
                base,
            };
            chain.call(request.clone()).await?
        };

        // Run after_model hooks
        self.run_after_model(&request, &mut response).await?;

        Ok(response)
    }

    /// Execute a tool call through the full middleware chain.
    pub async fn call_tool(
        &self,
        request: ToolCallRequest,
        base: &dyn ToolCaller,
    ) -> Result<Value, SynapticError> {
        if self.middlewares.is_empty() {
            base.call(request).await
        } else {
            let chain = WrapToolChain {
                middlewares: &self.middlewares,
                index: 0,
                base,
            };
            chain.call(request).await
        }
    }

    pub async fn run_before_file_op(&self, op: &FileOp) -> Result<FileOpDecision, SynapticError> {
        for mw in &self.middlewares {
            match mw.before_file_op(op).await? {
                FileOpDecision::Allow => continue,
                deny => return Ok(deny),
            }
        }
        Ok(FileOpDecision::Allow)
    }

    pub async fn run_after_file_op(
        &self,
        op: &FileOp,
        result: &FileOpResult,
    ) -> Result<(), SynapticError> {
        for mw in self.middlewares.iter().rev() {
            mw.after_file_op(op, result).await?;
        }
        Ok(())
    }

    pub async fn run_before_command(
        &self,
        cmd: &CommandOp,
    ) -> Result<CommandDecision, SynapticError> {
        for mw in &self.middlewares {
            match mw.before_command(cmd).await? {
                CommandDecision::Allow => continue,
                deny => return Ok(deny),
            }
        }
        Ok(CommandDecision::Allow)
    }

    pub async fn run_after_command(
        &self,
        cmd: &CommandOp,
        result: &CommandResult,
    ) -> Result<(), SynapticError> {
        for mw in self.middlewares.iter().rev() {
            mw.after_command(cmd, result).await?;
        }
        Ok(())
    }
}

// Internal chain helpers for recursive wrap_model_call / wrap_tool_call

struct WrapModelChain<'a> {
    middlewares: &'a [Arc<dyn AgentMiddleware>],
    index: usize,
    base: &'a dyn ModelCaller,
}

#[async_trait]
impl ModelCaller for WrapModelChain<'_> {
    async fn call(&self, request: ModelRequest) -> Result<ModelResponse, SynapticError> {
        if self.index >= self.middlewares.len() {
            self.base.call(request).await
        } else {
            let next = WrapModelChain {
                middlewares: self.middlewares,
                index: self.index + 1,
                base: self.base,
            };
            self.middlewares[self.index]
                .wrap_model_call(request, &next)
                .await
        }
    }
}

struct WrapToolChain<'a> {
    middlewares: &'a [Arc<dyn AgentMiddleware>],
    index: usize,
    base: &'a dyn ToolCaller,
}

#[async_trait]
impl ToolCaller for WrapToolChain<'_> {
    async fn call(&self, request: ToolCallRequest) -> Result<Value, SynapticError> {
        if self.index >= self.middlewares.len() {
            self.base.call(request).await
        } else {
            let next = WrapToolChain {
                middlewares: self.middlewares,
                index: self.index + 1,
                base: self.base,
            };
            self.middlewares[self.index]
                .wrap_tool_call(request, &next)
                .await
        }
    }
}

// ---------------------------------------------------------------------------
// BaseChatModelCaller — calls the actual ChatModel
// ---------------------------------------------------------------------------

/// Wraps a `ChatModel` into a `ModelCaller`.
pub struct BaseChatModelCaller {
    model: Arc<dyn ChatModel>,
}

impl BaseChatModelCaller {
    pub fn new(model: Arc<dyn ChatModel>) -> Self {
        Self { model }
    }
}

#[async_trait]
impl ModelCaller for BaseChatModelCaller {
    async fn call(&self, request: ModelRequest) -> Result<ModelResponse, SynapticError> {
        let chat_request = request.to_chat_request();
        let response = self.model.chat(chat_request).await?;
        Ok(response.into())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::atomic::{AtomicUsize, Ordering};

    struct CountingMiddleware {
        before_count: AtomicUsize,
        after_count: AtomicUsize,
    }

    impl CountingMiddleware {
        fn new() -> Self {
            Self {
                before_count: AtomicUsize::new(0),
                after_count: AtomicUsize::new(0),
            }
        }
    }

    #[async_trait]
    impl AgentMiddleware for CountingMiddleware {
        async fn before_model(&self, _request: &mut ModelRequest) -> Result<(), SynapticError> {
            self.before_count.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }

        async fn after_model(
            &self,
            _request: &ModelRequest,
            _response: &mut ModelResponse,
        ) -> Result<(), SynapticError> {
            self.after_count.fetch_add(1, Ordering::SeqCst);
            Ok(())
        }
    }

    #[test]
    fn middleware_chain_creation() {
        let mw: Arc<dyn AgentMiddleware> = Arc::new(CountingMiddleware::new());
        let chain = MiddlewareChain::new(vec![mw]);
        assert!(!chain.is_empty());
    }

    #[test]
    fn empty_middleware_chain() {
        let chain = MiddlewareChain::new(vec![]);
        assert!(chain.is_empty());
    }

    #[test]
    fn model_request_to_chat_request() {
        let req = ModelRequest {
            messages: vec![Message::human("hello")],
            tools: vec![],
            tool_choice: None,
            system_prompt: Some("You are helpful.".to_string()),
        };
        let chat_req = req.to_chat_request();
        assert_eq!(chat_req.messages.len(), 2);
        assert!(chat_req.messages[0].is_system());
        assert!(chat_req.messages[1].is_human());
    }

    #[test]
    fn model_request_without_system_prompt() {
        let req = ModelRequest {
            messages: vec![Message::human("hello")],
            tools: vec![],
            tool_choice: None,
            system_prompt: None,
        };
        let chat_req = req.to_chat_request();
        assert_eq!(chat_req.messages.len(), 1);
    }

    #[tokio::test]
    async fn file_hook_default_allows() {
        let mw: Arc<dyn AgentMiddleware> = Arc::new(CountingMiddleware::new());
        let chain = MiddlewareChain::new(vec![mw]);
        let op = FileOp {
            path: "/tmp/test".to_string(),
            kind: FileOpKind::Write,
        };
        let decision = chain.run_before_file_op(&op).await.unwrap();
        assert!(matches!(decision, FileOpDecision::Allow));
    }

    #[tokio::test]
    async fn command_hook_default_allows() {
        let mw: Arc<dyn AgentMiddleware> = Arc::new(CountingMiddleware::new());
        let chain = MiddlewareChain::new(vec![mw]);
        let cmd = CommandOp {
            command: "ls".to_string(),
            args: vec![],
            working_dir: None,
        };
        let decision = chain.run_before_command(&cmd).await.unwrap();
        assert!(matches!(decision, CommandDecision::Allow));
    }
}