Skip to main content

vv_agent/runtime/engine/
construction.rs

1use std::path::PathBuf;
2use std::sync::{Arc, Mutex};
3
4use crate::llm::{LlmClient, LlmError};
5use crate::runtime::backends::RuntimeExecutionBackend;
6use crate::runtime::hooks::RuntimeHook;
7use crate::runtime::lifecycle::AfterCycleHook;
8use crate::tools::{build_default_registry, ToolRegistry};
9use crate::types::{AgentResult, AgentTask};
10use crate::workspace::{LocalWorkspaceBackend, WorkspaceBackend};
11
12use super::{AgentRuntime, RuntimeRunControls};
13
14impl<C: LlmClient> AgentRuntime<C> {
15    pub fn new(llm_client: C) -> Self {
16        Self {
17            llm_client,
18            tool_registry: build_default_registry(),
19            default_workspace: None,
20            event_handler: None,
21            log_preview_chars: None,
22            workspace_backend: Arc::new(LocalWorkspaceBackend::new(PathBuf::from("./workspace"))),
23            hooks: Vec::new(),
24            after_cycle_hooks: Vec::new(),
25            execution_backend: RuntimeExecutionBackend::default(),
26            settings_file: None,
27            default_backend: None,
28            sub_agent_timeout_seconds: 90.0,
29            tool_policy: None,
30            pending_tool_approval: None,
31        }
32    }
33
34    pub fn with_tool_registry(mut self, tool_registry: ToolRegistry) -> Self {
35        self.tool_registry = tool_registry;
36        self
37    }
38
39    pub fn with_default_workspace(mut self, workspace: impl Into<PathBuf>) -> Self {
40        self.default_workspace = Some(workspace.into());
41        self
42    }
43
44    pub fn with_workspace_backend(mut self, backend: Arc<dyn WorkspaceBackend>) -> Self {
45        self.workspace_backend = backend;
46        self
47    }
48
49    pub fn with_hooks(mut self, hooks: Vec<Arc<dyn RuntimeHook>>) -> Self {
50        self.hooks = hooks;
51        self
52    }
53
54    pub fn with_after_cycle_hooks(mut self, hooks: Vec<Arc<dyn AfterCycleHook>>) -> Self {
55        self.after_cycle_hooks = hooks;
56        self
57    }
58
59    pub fn with_execution_backend(
60        mut self,
61        execution_backend: impl Into<RuntimeExecutionBackend>,
62    ) -> Self {
63        self.execution_backend = execution_backend.into();
64        self
65    }
66
67    pub fn with_settings_file(mut self, settings_file: impl Into<PathBuf>) -> Self {
68        self.settings_file = Some(settings_file.into());
69        self
70    }
71
72    pub fn with_default_backend(mut self, default_backend: impl Into<String>) -> Self {
73        self.default_backend = Some(default_backend.into());
74        self
75    }
76
77    pub fn with_log_preview_chars(mut self, log_preview_chars: usize) -> Self {
78        self.log_preview_chars = Some(log_preview_chars);
79        self
80    }
81
82    pub fn with_sub_agent_timeout_seconds(mut self, timeout_seconds: f64) -> Self {
83        self.sub_agent_timeout_seconds = timeout_seconds.max(1.0);
84        self
85    }
86
87    pub(crate) fn with_tool_policy(mut self, tool_policy: crate::tools::ToolPolicy) -> Self {
88        self.tool_policy = Some(tool_policy);
89        self
90    }
91
92    pub(crate) fn with_pending_tool_approval(
93        mut self,
94        pending: Arc<Mutex<Option<crate::result::PendingToolApproval>>>,
95    ) -> Self {
96        self.pending_tool_approval = Some(pending);
97        self
98    }
99}
100
101impl<C: LlmClient + Clone + 'static> AgentRuntime<C> {
102    pub fn set_tool_policy(&mut self, tool_policy: crate::tools::ToolPolicy) {
103        self.tool_policy = Some(tool_policy);
104    }
105
106    pub fn run(&self, task: AgentTask) -> Result<AgentResult, LlmError> {
107        self.run_with_controls(task, RuntimeRunControls::default())
108    }
109}