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 log::{debug, error};
57use serde_json::Value;
58use tauri::{AppHandle, Runtime};
59
60use crate::{RunTime::ApplicationRunTime::ApplicationRunTime, Track::Effect::CreateEffectForRequest};
61
62/// The primary dispatcher for requests originating from a `Cocoon` sidecar via
63/// gRPC. This routes RPC calls to the correct effect-based implementation.
64pub async fn DispatchSideCarRequest<R:Runtime>(
65	ApplicationHandle:AppHandle<R>,
66
67	RunTime:Arc<ApplicationRunTime>,
68
69	SideCarIdentifier:String,
70
71	MethodName:String,
72
73	Parameters:Value,
74) -> Result<Value, String> {
75	debug!(
76		"[Track/SideCarRequest] Dispatching sidecar request from '{}': {}",
77		SideCarIdentifier, MethodName
78	);
79
80	match CreateEffectForRequest(&ApplicationHandle, &MethodName, Parameters) {
81		Ok(EffectFn) => EffectFn(RunTime).await,
82
83		Err(Error) => {
84			error!(
85				"[Track/SideCarRequest] Failed to create effect for sidecar method '{}': {}",
86				MethodName, Error
87			);
88
89			Err(Error)
90		},
91	}
92}