|
1 | 1 | use acp_nats::acp_prefix::AcpPrefix; |
2 | 2 | use acp_nats::client_proxy::NatsClientProxy; |
3 | | -use acp_nats::nats::{AgentMethod, parse_agent_subject}; |
| 3 | +use acp_nats::nats::{ |
| 4 | + GlobalAgentMethod, ParsedAgentSubject, SessionAgentMethod, parse_agent_subject, |
| 5 | +}; |
4 | 6 | use acp_nats::session_id::AcpSessionId; |
5 | 7 | use agent_client_protocol::{ |
6 | 8 | Agent, AuthenticateRequest, CancelNotification, CloseSessionRequest, ExtNotification, |
@@ -163,89 +165,114 @@ async fn dispatch_message<N: PublishClient + FlushClient, A: Agent>( |
163 | 165 | None => return, |
164 | 166 | }; |
165 | 167 |
|
166 | | - let result = match parsed.method { |
167 | | - AgentMethod::Initialize => { |
168 | | - handle_request(&msg, nats, |req: InitializeRequest| agent.initialize(req)).await |
| 168 | + let (result, session_id) = match parsed { |
| 169 | + ParsedAgentSubject::Global(method) => { |
| 170 | + let r = dispatch_global(method, &msg, agent, nats).await; |
| 171 | + (r, None) |
| 172 | + } |
| 173 | + ParsedAgentSubject::Session { session_id, method } => { |
| 174 | + let r = dispatch_session(method, &msg, agent, nats).await; |
| 175 | + (r, Some(session_id)) |
169 | 176 | } |
170 | | - AgentMethod::Authenticate => { |
171 | | - handle_request(&msg, nats, |req: AuthenticateRequest| { |
| 177 | + }; |
| 178 | + |
| 179 | + if let Err(e) = result { |
| 180 | + let sid = session_id.as_ref().map(|s| s.as_str()).unwrap_or("-"); |
| 181 | + warn!(subject, session_id = sid, error = %e, "Error handling agent request"); |
| 182 | + } |
| 183 | +} |
| 184 | + |
| 185 | +async fn dispatch_global<N: PublishClient + FlushClient, A: Agent>( |
| 186 | + method: GlobalAgentMethod, |
| 187 | + msg: &Message, |
| 188 | + agent: &A, |
| 189 | + nats: &N, |
| 190 | +) -> Result<(), DispatchError> { |
| 191 | + match method { |
| 192 | + GlobalAgentMethod::Initialize => { |
| 193 | + handle_request(msg, nats, |req: InitializeRequest| agent.initialize(req)).await |
| 194 | + } |
| 195 | + GlobalAgentMethod::Authenticate => { |
| 196 | + handle_request(msg, nats, |req: AuthenticateRequest| { |
172 | 197 | agent.authenticate(req) |
173 | 198 | }) |
174 | 199 | .await |
175 | 200 | } |
176 | | - AgentMethod::SessionNew => { |
177 | | - handle_request(&msg, nats, |req: NewSessionRequest| agent.new_session(req)).await |
| 201 | + GlobalAgentMethod::SessionNew => { |
| 202 | + handle_request(msg, nats, |req: NewSessionRequest| agent.new_session(req)).await |
178 | 203 | } |
179 | | - AgentMethod::SessionList => { |
180 | | - handle_request(&msg, nats, |req: ListSessionsRequest| { |
| 204 | + GlobalAgentMethod::SessionList => { |
| 205 | + handle_request(msg, nats, |req: ListSessionsRequest| { |
181 | 206 | agent.list_sessions(req) |
182 | 207 | }) |
183 | 208 | .await |
184 | 209 | } |
185 | | - AgentMethod::SessionLoad => { |
186 | | - handle_request(&msg, nats, |req: LoadSessionRequest| { |
187 | | - agent.load_session(req) |
188 | | - }) |
189 | | - .await |
| 210 | + GlobalAgentMethod::Ext(_) => { |
| 211 | + if msg.reply.is_some() { |
| 212 | + handle_request(msg, nats, |req: ExtRequest| agent.ext_method(req)).await |
| 213 | + } else { |
| 214 | + handle_notification(msg, |req: ExtNotification| agent.ext_notification(req)).await |
| 215 | + } |
| 216 | + } |
| 217 | + } |
| 218 | +} |
| 219 | + |
| 220 | +async fn dispatch_session<N: PublishClient + FlushClient, A: Agent>( |
| 221 | + method: SessionAgentMethod, |
| 222 | + msg: &Message, |
| 223 | + agent: &A, |
| 224 | + nats: &N, |
| 225 | +) -> Result<(), DispatchError> { |
| 226 | + match method { |
| 227 | + SessionAgentMethod::Load => { |
| 228 | + handle_request(msg, nats, |req: LoadSessionRequest| agent.load_session(req)).await |
190 | 229 | } |
191 | | - AgentMethod::SessionPrompt => { |
192 | | - handle_request(&msg, nats, |req: PromptRequest| agent.prompt(req)).await |
| 230 | + SessionAgentMethod::Prompt => { |
| 231 | + handle_request(msg, nats, |req: PromptRequest| agent.prompt(req)).await |
193 | 232 | } |
194 | | - AgentMethod::SessionCancel => { |
195 | | - handle_notification(&msg, |req: CancelNotification| agent.cancel(req)).await |
| 233 | + SessionAgentMethod::Cancel => { |
| 234 | + handle_notification(msg, |req: CancelNotification| agent.cancel(req)).await |
196 | 235 | } |
197 | | - AgentMethod::SessionSetMode => { |
198 | | - handle_request(&msg, nats, |req: SetSessionModeRequest| { |
| 236 | + SessionAgentMethod::SetMode => { |
| 237 | + handle_request(msg, nats, |req: SetSessionModeRequest| { |
199 | 238 | agent.set_session_mode(req) |
200 | 239 | }) |
201 | 240 | .await |
202 | 241 | } |
203 | | - AgentMethod::SessionSetConfigOption => { |
204 | | - handle_request(&msg, nats, |req: SetSessionConfigOptionRequest| { |
| 242 | + SessionAgentMethod::SetConfigOption => { |
| 243 | + handle_request(msg, nats, |req: SetSessionConfigOptionRequest| { |
205 | 244 | agent.set_session_config_option(req) |
206 | 245 | }) |
207 | 246 | .await |
208 | 247 | } |
209 | | - AgentMethod::SessionSetModel => { |
210 | | - handle_request(&msg, nats, |req: SetSessionModelRequest| { |
| 248 | + SessionAgentMethod::SetModel => { |
| 249 | + handle_request(msg, nats, |req: SetSessionModelRequest| { |
211 | 250 | agent.set_session_model(req) |
212 | 251 | }) |
213 | 252 | .await |
214 | 253 | } |
215 | | - AgentMethod::SessionFork => { |
216 | | - handle_request(&msg, nats, |req: ForkSessionRequest| { |
217 | | - agent.fork_session(req) |
218 | | - }) |
219 | | - .await |
| 254 | + SessionAgentMethod::Fork => { |
| 255 | + handle_request(msg, nats, |req: ForkSessionRequest| agent.fork_session(req)).await |
220 | 256 | } |
221 | | - AgentMethod::SessionResume => { |
222 | | - handle_request(&msg, nats, |req: ResumeSessionRequest| { |
| 257 | + SessionAgentMethod::Resume => { |
| 258 | + handle_request(msg, nats, |req: ResumeSessionRequest| { |
223 | 259 | agent.resume_session(req) |
224 | 260 | }) |
225 | 261 | .await |
226 | 262 | } |
227 | | - AgentMethod::SessionClose => { |
228 | | - handle_request(&msg, nats, |req: CloseSessionRequest| { |
| 263 | + SessionAgentMethod::Close => { |
| 264 | + handle_request(msg, nats, |req: CloseSessionRequest| { |
229 | 265 | agent.close_session(req) |
230 | 266 | }) |
231 | 267 | .await |
232 | 268 | } |
233 | | - AgentMethod::Ext(_) => { |
| 269 | + SessionAgentMethod::Ext(_) => { |
234 | 270 | if msg.reply.is_some() { |
235 | | - handle_request(&msg, nats, |req: ExtRequest| agent.ext_method(req)).await |
| 271 | + handle_request(msg, nats, |req: ExtRequest| agent.ext_method(req)).await |
236 | 272 | } else { |
237 | | - handle_notification(&msg, |req: ExtNotification| agent.ext_notification(req)).await |
| 273 | + handle_notification(msg, |req: ExtNotification| agent.ext_notification(req)).await |
238 | 274 | } |
239 | 275 | } |
240 | | - }; |
241 | | - |
242 | | - if let Err(e) = result { |
243 | | - let sid = parsed |
244 | | - .session_id |
245 | | - .as_ref() |
246 | | - .map(|s| s.as_str()) |
247 | | - .unwrap_or("-"); |
248 | | - warn!(subject, session_id = sid, error = %e, "Error handling agent request"); |
249 | 276 | } |
250 | 277 | } |
251 | 278 |
|
|
0 commit comments