Skip to main content

vv_agent/runtime/engine/
controls.rs

1use std::collections::{BTreeMap, VecDeque};
2use std::path::PathBuf;
3use std::sync::{Arc, Mutex};
4
5use serde_json::Value;
6
7use crate::model::ModelProvider;
8use crate::runtime::cancellation::CancellationToken;
9use crate::runtime::context::{ExecutionContext, StreamCallback};
10use crate::runtime::sub_task_manager::SubTaskManager;
11use crate::types::Message;
12use crate::workspace::WorkspaceBackend;
13
14pub type RuntimeLogCallback = dyn FnMut(&str, &BTreeMap<String, Value>) + Send + Sync + 'static;
15pub type RuntimeLogHandler = Arc<Mutex<Box<RuntimeLogCallback>>>;
16pub type RuntimeEventHandler = Arc<dyn Fn(&str, &BTreeMap<String, Value>) + Send + Sync + 'static>;
17pub type BeforeCycleMessageProvider =
18    Arc<dyn Fn(u32, &[Message], &BTreeMap<String, Value>) -> Vec<Message> + Send + Sync + 'static>;
19pub type InterruptionMessageProvider = Arc<dyn Fn() -> Vec<Message> + Send + Sync + 'static>;
20
21#[derive(Clone, Default)]
22pub struct RuntimeRunControls {
23    pub log_handler: Option<RuntimeEventHandler>,
24    pub before_cycle_messages: Option<BeforeCycleMessageProvider>,
25    pub interruption_messages: Option<InterruptionMessageProvider>,
26    pub steering_queue: Option<Arc<Mutex<VecDeque<String>>>>,
27    pub cancellation_token: Option<CancellationToken>,
28    pub execution_context: Option<ExecutionContext>,
29    pub workspace: Option<PathBuf>,
30    pub workspace_backend: Option<Arc<dyn WorkspaceBackend>>,
31    pub model_provider: Option<Arc<dyn ModelProvider>>,
32    pub sub_task_manager: Option<SubTaskManager>,
33}
34
35impl RuntimeRunControls {
36    pub(in crate::runtime::engine) fn effective_cancellation_token(
37        &self,
38    ) -> Option<CancellationToken> {
39        self.cancellation_token.clone().or_else(|| {
40            self.execution_context
41                .as_ref()
42                .and_then(|context| context.cancellation_token.clone())
43        })
44    }
45
46    pub(in crate::runtime::engine) fn effective_stream_callback(&self) -> Option<StreamCallback> {
47        self.execution_context
48            .as_ref()
49            .and_then(|context| context.stream_callback.clone())
50    }
51}