1use pe_core::state::State;
11use std::marker::PhantomData;
12use std::sync::Arc;
13
14pub struct InjectedState<S: State> {
32 state: S,
33 _phantom: PhantomData<S>,
34}
35
36impl<S: State> InjectedState<S> {
37 pub fn new(state: S) -> Self {
39 Self {
40 state,
41 _phantom: PhantomData,
42 }
43 }
44
45 pub fn get(&self) -> &S {
47 &self.state
48 }
49
50 pub fn into_inner(self) -> S {
52 self.state
53 }
54}
55
56pub struct InjectedStore {
77 store: Arc<dyn std::any::Any + Send + Sync>,
78}
79
80impl InjectedStore {
81 pub fn new(store: Arc<dyn std::any::Any + Send + Sync>) -> Self {
83 Self { store }
84 }
85
86 pub fn get(&self) -> &Arc<dyn std::any::Any + Send + Sync> {
89 &self.store
90 }
91}
92
93#[cfg(test)]
94mod tests {
95 use super::*;
96 use pe_core::message::Message;
97 use pe_core::state::{CoreState, ExecutionContext, StateUpdate};
98 use serde::{Deserialize, Serialize};
99
100 #[derive(Debug, Clone, Serialize, Deserialize)]
101 struct TestState {
102 messages: Vec<Message>,
103 iterations: u32,
104 thread_id: String,
105 context: ExecutionContext,
106 }
107
108 #[derive(Debug, Clone, Default, Serialize, Deserialize)]
109 struct TestUpdate;
110 impl StateUpdate for TestUpdate {}
111
112 impl pe_core::state::State for TestState {
113 type Update = TestUpdate;
114 fn apply(&mut self, _update: Self::Update) {}
115 }
116
117 impl CoreState for TestState {
118 fn messages(&self) -> &[Message] {
119 &self.messages
120 }
121 fn messages_mut(&mut self) -> &mut Vec<Message> {
122 &mut self.messages
123 }
124 fn iterations(&self) -> u32 {
125 self.iterations
126 }
127 fn set_iterations(&mut self, n: u32) {
128 self.iterations = n;
129 }
130 fn thread_id(&self) -> &str {
131 &self.thread_id
132 }
133 fn context(&self) -> &ExecutionContext {
134 &self.context
135 }
136 fn context_mut(&mut self) -> &mut ExecutionContext {
137 &mut self.context
138 }
139 }
140
141 #[test]
142 fn injected_state_wraps_and_unwraps() {
143 let state = TestState {
144 messages: vec![Message::human("hello")],
145 iterations: 0,
146 thread_id: "t1".into(),
147 context: ExecutionContext::new("agent-1"),
148 };
149
150 let injected = InjectedState::new(state.clone());
151 assert_eq!(injected.get().messages().len(), 1);
152 assert_eq!(injected.get().thread_id(), "t1");
153
154 let inner = injected.into_inner();
155 assert_eq!(inner.iterations(), 0);
156 }
157
158 #[test]
159 fn injected_store_wraps_any() {
160 let data: Arc<dyn std::any::Any + Send + Sync> = Arc::new(42_u32);
161 let injected = InjectedStore::new(data);
162 let store = injected.get();
163 let value = store.downcast_ref::<u32>().unwrap();
164 assert_eq!(*value, 42);
165 }
166}