Skip to main content

switchyard_libsy/core/
processor.rs

1// SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
2// SPDX-License-Identifier: Apache-2.0
3
4use crate::Result;
5use async_trait::async_trait;
6use switchyard_protocol::{AggLlmResponse, Decision, Request, Signals};
7
8/// An event observed by the algorithm. Events are consumed by [`Processor`] to mutate state.
9///
10/// Request-bearing variants ([`Event::Request`], [`Event::Decision`]) borrow the request
11/// mutably, so a processor may rewrite it in place and the edit propagates to the rest of
12/// the chain and to the model call. The observation-only variants stay immutable.
13pub enum Event<'a> {
14    /// The inbound request that begins a turn.
15    Request(&'a mut Request),
16    /// An out-of-band agentic-stack signal (tool results, budget updates, …).
17    Signal(&'a Signals),
18    /// A routing decision paired with the request that produced it.
19    ///
20    /// The request is rewritable: a processor may add instructions or notes here that
21    /// are bound to the routing outcome (e.g. a model-specific system prompt).
22    Decision {
23        /// The request, rewritable in place.
24        request: &'a mut Request,
25        /// The routing decision produced for `request`.
26        decision: &'a Decision,
27    },
28    /// A buffered response received back from a model.
29    ModelResponse(&'a AggLlmResponse),
30}
31
32/// Collects events as the algorithm runs and mutates the composition's state.
33#[async_trait]
34pub trait Processor<S = ()>: Send + Sync {
35    /// Process an event, accumulating facts into `state`. Request-bearing events
36    /// ([`Event::Request`], [`Event::Decision`]) may also be rewritten in place.
37    async fn process(&self, state: &mut S, event: Event<'_>) -> Result<()>;
38}
39
40#[cfg(test)]
41mod tests {
42    use super::*;
43    use std::collections::HashMap;
44    use switchyard_protocol::{text_request, text_response};
45
46    type TestState = HashMap<&'static str, u32>;
47
48    /// The key each event variant tallies under.
49    fn event_key(event: &Event<'_>) -> &'static str {
50        match event {
51            Event::Request(_) => "requests",
52            Event::Signal(_) => "signals",
53            Event::Decision { .. } => "decisions",
54            Event::ModelResponse(_) => "model_responses",
55        }
56    }
57
58    /// Reads a count, treating a missing key as zero.
59    fn count(state: &TestState, key: &'static str) -> u32 {
60        state.get(key).copied().unwrap_or_default()
61    }
62
63    /// Tallies each event variant under its own key.
64    struct CountingProcessor;
65
66    #[async_trait]
67    impl Processor<TestState> for CountingProcessor {
68        async fn process(&self, state: &mut TestState, event: Event<'_>) -> Result<()> {
69            *state.entry(event_key(&event)).or_default() += 1;
70            Ok(())
71        }
72    }
73
74    fn request() -> Request {
75        Request {
76            llm_request: text_request(Some("auto".to_string()), "hi"),
77            raw_request: None,
78            metadata: None,
79        }
80    }
81
82    #[tokio::test]
83    async fn processor_tallies_each_event_variant_into_state() -> Result<()> {
84        let processor = CountingProcessor;
85        let mut state = TestState::default();
86        let mut req = request();
87        let response = text_response(None, "ok");
88        let decision = Decision::new("test/model", None, true);
89        let signals = Signals {};
90
91        // Feed one of every event variant through the processor.
92        processor
93            .process(&mut state, Event::Request(&mut req))
94            .await?;
95        processor
96            .process(&mut state, Event::ModelResponse(&response))
97            .await?;
98        processor
99            .process(
100                &mut state,
101                Event::Decision {
102                    request: &mut req,
103                    decision: &decision,
104                },
105            )
106            .await?;
107        processor
108            .process(&mut state, Event::Signal(&signals))
109            .await?;
110
111        assert_eq!(count(&state, "requests"), 1);
112        assert_eq!(count(&state, "signals"), 1);
113        assert_eq!(count(&state, "decisions"), 1);
114        assert_eq!(count(&state, "model_responses"), 1);
115        Ok(())
116    }
117
118    #[tokio::test]
119    async fn process_accumulates_state_across_repeated_events() -> Result<()> {
120        let processor = CountingProcessor;
121        let mut state = TestState::default();
122        let mut req = request();
123
124        for _ in 0..3 {
125            processor
126                .process(&mut state, Event::Request(&mut req))
127                .await?;
128        }
129
130        assert_eq!(count(&state, "requests"), 3);
131        Ok(())
132    }
133
134    /// Rewrites the requested model on every request-bearing event.
135    struct RewritingProcessor;
136
137    #[async_trait]
138    impl Processor for RewritingProcessor {
139        async fn process(&self, _state: &mut (), event: Event<'_>) -> Result<()> {
140            match event {
141                Event::Request(request) | Event::Decision { request, .. } => {
142                    request.llm_request.model = Some("rewritten".to_string());
143                }
144                _ => {}
145            }
146            Ok(())
147        }
148    }
149
150    #[tokio::test]
151    async fn processor_rewrites_the_request_in_place() -> Result<()> {
152        let mut state = ();
153        let mut req = request();
154        assert_eq!(req.requested_model(), Some("auto"));
155
156        RewritingProcessor
157            .process(&mut state, Event::Request(&mut req))
158            .await?;
159
160        // The edit outlives the call, so the next component sees the rewritten request.
161        assert_eq!(req.requested_model(), Some("rewritten"));
162        Ok(())
163    }
164}