Skip to main content

vc_runtime/backend/
context.rs

1//! BuildContext — dependency container for backend construction.
2
3use std::collections::HashMap;
4use std::path::PathBuf;
5use std::sync::Arc;
6
7#[cfg(feature = "comfy-renderer")]
8use crate::services::comfy::ValidatedWorkflowTemplate;
9
10// ============================================================
11// ComfyContext (M2.8B)
12// ============================================================
13
14#[cfg(feature = "comfy-renderer")]
15#[derive(Clone)]
16pub struct ComfyContext {
17    pub workflow: Arc<ValidatedWorkflowTemplate>,
18    pub runtime_handle: tokio::runtime::Handle,
19}
20
21#[cfg(feature = "comfy-renderer")]
22impl ComfyContext {
23    pub fn new(
24        workflow: ValidatedWorkflowTemplate,
25        runtime_handle: tokio::runtime::Handle,
26    ) -> Self {
27        Self { workflow: Arc::new(workflow), runtime_handle }
28    }
29}
30
31// ============================================================
32// PythonContext (M2.8C.1 — diperbaiki untuk menerima Option)
33// ============================================================
34
35/// Context for Python backend.
36/// Contains only external resources, not constructed services.
37#[derive(Clone)]
38pub struct PythonContext {
39    /// Path to the Python interpreter (if explicitly specified).
40    pub interpreter: Option<Arc<PathBuf>>,
41
42    /// Root directory for Python scripts (if explicitly specified).
43    pub script_root: Option<Arc<PathBuf>>,
44
45    /// Environment variables for the Python process (if any).
46    pub environment: Option<Arc<HashMap<String, String>>>,
47}
48
49impl PythonContext {
50    pub fn new(
51        interpreter: Option<PathBuf>,
52        script_root: Option<PathBuf>,
53        environment: Option<HashMap<String, String>>,
54    ) -> Self {
55        Self {
56            interpreter: interpreter.map(Arc::new),
57            script_root: script_root.map(Arc::new),
58            environment: environment.map(Arc::new),
59        }
60    }
61}
62
63// ============================================================
64// BuildContext & Builder
65// ============================================================
66
67#[derive(Clone, Default)]
68pub struct BuildContext {
69    #[cfg(feature = "comfy-renderer")]
70    comfy: Option<ComfyContext>,
71
72    python: Option<PythonContext>,
73    // Future: add other backend contexts here (invoke, local, etc.)
74}
75
76impl BuildContext {
77    pub fn builder() -> BuildContextBuilder {
78        BuildContextBuilder::default()
79    }
80
81    #[cfg(feature = "comfy-renderer")]
82    pub fn comfy(&self) -> Option<&ComfyContext> {
83        self.comfy.as_ref()
84    }
85
86    pub fn python(&self) -> Option<&PythonContext> {
87        self.python.as_ref()
88    }
89}
90
91#[derive(Default)]
92pub struct BuildContextBuilder {
93    #[cfg(feature = "comfy-renderer")]
94    comfy: Option<ComfyContext>,
95
96    python: Option<PythonContext>,
97}
98
99impl BuildContextBuilder {
100    #[cfg(feature = "comfy-renderer")]
101    pub fn with_comfy(mut self, ctx: ComfyContext) -> Self {
102        self.comfy = Some(ctx);
103        self
104    }
105
106    pub fn with_python(mut self, ctx: PythonContext) -> Self {
107        self.python = Some(ctx);
108        self
109    }
110
111    pub fn build(self) -> BuildContext {
112        BuildContext {
113            #[cfg(feature = "comfy-renderer")]
114            comfy: self.comfy,
115            python: self.python,
116        }
117    }
118}