Skip to main content

vtcode_core/orchestrator/
executor.rs

1use anyhow::Result;
2use async_trait::async_trait;
3use serde_json::Value;
4use std::sync::Arc;
5use std::time::Duration;
6use tokio::time::sleep;
7
8use super::ScheduledWork;
9
10/// Executor describes a target runtime that can handle scheduled work.
11#[async_trait]
12pub trait WorkExecutor: Send + Sync {
13    async fn execute(&self, work: ScheduledWork) -> Result<Value>;
14}
15
16/// Minimal local executor that simulates work for orchestration flows.
17#[derive(Debug, Default)]
18pub struct LocalExecutor;
19
20#[async_trait]
21impl WorkExecutor for LocalExecutor {
22    async fn execute(&self, work: ScheduledWork) -> Result<Value> {
23        // Simulate asynchronous execution; caller can measure latency.
24        sleep(Duration::from_millis(10)).await;
25        Ok(serde_json::json!({
26            "work_id": work.id,
27            "target": work.target.to_string(),
28            "metadata": work.metadata,
29        }))
30    }
31}
32
33/// Registry of available executors keyed by logical target.
34#[derive(Default, Clone)]
35pub struct ExecutorRegistry {
36    executors: hashbrown::HashMap<String, Arc<dyn WorkExecutor>>,
37}
38
39impl ExecutorRegistry {
40    pub fn register(&mut self, target: impl Into<String>, executor: Arc<dyn WorkExecutor>) {
41        self.executors.insert(target.into(), executor);
42    }
43
44    pub fn get(&self, target: &str) -> Option<Arc<dyn WorkExecutor>> {
45        self.executors.get(target).cloned()
46    }
47}