Mountain/Track/SideCarRequest/DispatchSideCarRequest.rs
1//! # DispatchSideCarRequest (Track)
2//!
3//! ## RESPONSIBILITIES
4//!
5//! This module provides the primary dispatcher for requests originating from
6//! Cocoon sidecars via gRPC. It routes RPC calls to the correct effect-based
7//! implementation.
8//!
9//! ### Core Functions:
10//! - Receive gRPC requests from Cocoon sidecars
11//! - Route requests to the effect creation system
12//! - Execute effects through the ApplicationRunTime
13//! - Return results or errors to the sidecar
14//!
15//! ## ARCHITECTURAL ROLE
16//!
17//! DispatchSideCarRequest acts as the **sidecar gateway** in Track's dispatch
18//! layer:
19//!
20//! ```text
21//! Cocoon (Sidecar) ──► DispatchSideCarRequest ──► CreateEffectForRequest ──► ApplicationRunTime ──► Providers
22//! ```
23//!
24//! ## KEY COMPONENTS
25//!
26//! - **Fn**: Main dispatch function (public async fn Fn<R:Runtime>)
27//!
28//! ## ERROR HANDLING
29//!
30//! - Effect creation failures are caught and logged
31//! - Unknown methods are reported with context
32//! - Errors are propagated to the sidecar with descriptive messages
33//!
34//! ## LOGGING
35//!
36//! - All incoming sidecar requests are logged at debug level with sidecar ID
37//! - Effect creation failures are logged at error level
38//! - Log format: "[Track/SideCarRequest] Dispatching sidecar request from '{}':
39//! {}"
40//!
41//! ## PERFORMANCE CONSIDERATIONS
42//!
43//! - Direct effect execution without intermediate overhead
44//! - Minimal locking to avoid blocking
45//! - Async operations for non-blocking dispatch
46//!
47//! ## TODO
48//!
49//! - [ ] Add request timeout handling
50//! - [ ] Implement request cancellation support (VS Code compatibility)
51//! - [ ] Add request metrics and telemetry
52//! - [ ] Add sidecar authentication/authorization
53
54use std::sync::Arc;
55
56use serde_json::Value;
57use tauri::{AppHandle, Runtime};
58
59use crate::{
60 RunTime::ApplicationRunTime::ApplicationRunTime,
61 Track::Effect::CreateEffectForRequest::Fn as CreateEffectForRequest,
62 dev_log,
63};
64
65/// The primary dispatcher for requests originating from a `Cocoon` sidecar via
66/// gRPC. This routes RPC calls to the correct effect-based implementation.
67pub async fn DispatchSideCarRequest<R:Runtime>(
68 ApplicationHandle:AppHandle<R>,
69
70 RunTime:Arc<ApplicationRunTime>,
71
72 SideCarIdentifier:String,
73
74 MethodName:String,
75
76 Parameters:Value,
77) -> Result<Value, String> {
78 // Per-request dispatch line - fires for every FileSystem.ReadFile /
79 // FileSystem.Stat / Configuration.Inspect round-trip from Cocoon. The
80 // caller-side `[DEV:IPC] invoke:` and `done:` pair already carries the
81 // method + timing (when not in the high-frequency skip list), so this
82 // line adds nothing at the default log level. Route to `grpc-verbose`.
83 dev_log!(
84 "grpc-verbose",
85 "[Track/SideCarRequest] Dispatching sidecar request from '{}': {}",
86 SideCarIdentifier,
87 MethodName
88 );
89
90 match CreateEffectForRequest(&ApplicationHandle, &MethodName, Parameters) {
91 Ok(EffectFn) => EffectFn(RunTime).await,
92
93 Err(Error) => {
94 dev_log!(
95 "ipc",
96 "error: [Track/SideCarRequest] Failed to create effect for sidecar method '{}': {}",
97 MethodName,
98 Error
99 );
100
101 Err(Error)
102 },
103 }
104}