vv-agent 0.7.1

VectorVein agent runtime, SDK, CLI, tools, and workspace backends
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
use std::path::PathBuf;
use std::sync::Arc;
use std::time::Duration;

use serde_json::Value;

use crate::approval::{ApprovalBroker, ApprovalProvider};
use crate::budget::{HostCostMeter, RunBudgetLimits};
use crate::checkpoint::{CheckpointConfig, CheckpointExtension, ReconciliationProvider};
use crate::context_providers::ContextProvider;
use crate::event_store::RunEventStore;
use crate::execution_mode::ExecutionMode;
use crate::llm::LlmStreamCallback;
use crate::memory::MemoryProvider;
use crate::model::{ModelProvider, ModelRef};
use crate::model_settings::ModelSettings;
use crate::runtime::backends::RuntimeExecutionBackend;
use crate::runtime::{
    BeforeCycleMessageProvider, CancellationToken, InterruptionMessageProvider,
    RuntimeEventHandler, RuntimeHook, SubTaskManager,
};
use crate::sessions::Session;
use crate::tools::{ToolPolicy, ToolRegistry};
use crate::tracing::TraceSink;
use crate::types::{Message, Metadata, NoToolPolicy};
use crate::workspace::WorkspaceBackend;

pub type ToolRegistryFactory = Arc<dyn Fn() -> ToolRegistry + Send + Sync + 'static>;

pub(crate) const INITIAL_BUDGET_USAGE_METADATA_KEY: &str = "_vv_agent_initial_budget_usage";

pub(crate) const MAX_CYCLES_RANGE_ERROR: &str = "max_cycles must be between 1 and 4294967295";

pub(crate) fn validate_max_cycles(max_cycles: u32) -> Result<u32, String> {
    if max_cycles == 0 {
        return Err(MAX_CYCLES_RANGE_ERROR.to_string());
    }
    Ok(max_cycles)
}

#[derive(Clone, Default)]
pub struct RunConfig {
    pub model: Option<ModelRef>,
    pub model_provider: Option<Arc<dyn ModelProvider>>,
    pub model_settings: Option<ModelSettings>,
    pub workspace: Option<PathBuf>,
    pub workspace_backend: Option<Arc<dyn WorkspaceBackend>>,
    pub session: Option<Arc<dyn Session>>,
    pub initial_messages: Option<Vec<Message>>,
    pub max_cycles: Option<u32>,
    pub max_handoffs: Option<u32>,
    pub no_tool_policy: Option<NoToolPolicy>,
    pub tool_policy: ToolPolicy,
    pub execution_backend: Option<RuntimeExecutionBackend>,
    pub cancellation_token: Option<CancellationToken>,
    pub hooks: Vec<Arc<dyn RuntimeHook>>,
    pub trace_sink: Option<Arc<dyn TraceSink>>,
    pub trace_id: Option<String>,
    pub workflow_name: Option<String>,
    pub event_store: Option<Arc<dyn RunEventStore>>,
    pub event_store_fail_closed: bool,
    pub approval_provider: Option<Arc<dyn ApprovalProvider>>,
    pub approval_timeout: Option<Duration>,
    pub approval_broker: Option<ApprovalBroker>,
    pub context_providers: Vec<Arc<dyn ContextProvider>>,
    pub max_context_chars: Option<usize>,
    pub memory_providers: Vec<Arc<dyn MemoryProvider>>,
    pub app_state: Option<Arc<dyn std::any::Any + Send + Sync>>,
    pub initial_shared_state: Metadata,
    pub tool_registry_factory: Option<ToolRegistryFactory>,
    pub log_preview_chars: Option<usize>,
    pub debug_dump_dir: Option<PathBuf>,
    pub before_cycle_messages: Option<BeforeCycleMessageProvider>,
    pub interruption_messages: Option<InterruptionMessageProvider>,
    pub sub_task_manager: Option<SubTaskManager>,
    pub runtime_log_handler: Option<RuntimeEventHandler>,
    pub runtime_stream_callback: Option<LlmStreamCallback>,
    pub budget_limits: Option<RunBudgetLimits>,
    pub host_cost_meter: Option<Arc<dyn HostCostMeter>>,
    pub checkpoint_config: Option<CheckpointConfig>,
    pub checkpoint_extensions: Vec<Arc<dyn CheckpointExtension>>,
    pub reconciliation_provider: Option<Arc<dyn ReconciliationProvider>>,
    pub metadata: Metadata,
}

impl RunConfig {
    pub fn builder() -> RunConfigBuilder {
        RunConfigBuilder::default()
    }

    pub(crate) fn for_background_child(&self, shared_state: Metadata) -> Self {
        let mut child = self.clone();
        child.model = None;
        child.model_settings = None;
        child.session = None;
        child.initial_messages = None;
        child.cancellation_token = self
            .cancellation_token
            .as_ref()
            .map(CancellationToken::child);
        child.initial_shared_state = shared_state;
        child.before_cycle_messages = None;
        child.interruption_messages = None;
        child.sub_task_manager = None;
        child.runtime_log_handler = None;
        child.runtime_stream_callback = None;
        child.host_cost_meter = None;
        child.checkpoint_config = None;
        child.checkpoint_extensions.clear();
        child.reconciliation_provider = None;
        child.metadata.remove(INITIAL_BUDGET_USAGE_METADATA_KEY);
        child
    }
}

#[derive(Default)]
pub struct RunConfigBuilder {
    config: RunConfig,
}

impl RunConfigBuilder {
    pub fn model(mut self, model: ModelRef) -> Self {
        self.config.model = Some(model);
        self
    }

    pub fn model_provider(mut self, provider: impl ModelProvider + 'static) -> Self {
        self.config.model_provider = Some(Arc::new(provider));
        self
    }

    pub fn model_provider_arc(mut self, provider: Arc<dyn ModelProvider>) -> Self {
        self.config.model_provider = Some(provider);
        self
    }

    pub fn model_settings(mut self, settings: ModelSettings) -> Self {
        self.config.model_settings = Some(settings);
        self
    }

    pub fn workspace(mut self, workspace: impl Into<PathBuf>) -> Self {
        self.config.workspace = Some(workspace.into());
        self
    }

    pub fn workspace_backend(mut self, backend: Arc<dyn WorkspaceBackend>) -> Self {
        self.config.workspace_backend = Some(backend);
        self
    }

    pub fn session(mut self, session: impl Session + 'static) -> Self {
        self.config.session = Some(Arc::new(session));
        self
    }

    pub fn session_arc(mut self, session: Arc<dyn Session>) -> Self {
        self.config.session = Some(session);
        self
    }

    pub fn initial_messages(mut self, messages: Vec<Message>) -> Self {
        self.config.initial_messages = Some(messages);
        self
    }

    pub fn max_cycles(mut self, max_cycles: u32) -> Self {
        self.config.max_cycles = Some(max_cycles);
        self
    }

    pub fn max_handoffs(mut self, max_handoffs: u32) -> Self {
        self.config.max_handoffs = Some(max_handoffs);
        self
    }

    pub fn no_tool_policy(mut self, policy: NoToolPolicy) -> Self {
        self.config.no_tool_policy = Some(policy);
        self
    }

    pub fn tool_policy(mut self, tool_policy: ToolPolicy) -> Self {
        self.config.tool_policy = tool_policy;
        self
    }

    pub fn execution_backend(mut self, execution_backend: RuntimeExecutionBackend) -> Self {
        self.config.execution_backend = Some(execution_backend);
        self
    }

    pub fn execution_mode(mut self, execution_mode: ExecutionMode) -> Self {
        self.config.execution_backend = Some(execution_mode.into());
        self
    }

    pub fn cancellation_token(mut self, cancellation_token: CancellationToken) -> Self {
        self.config.cancellation_token = Some(cancellation_token);
        self
    }

    pub fn hook(mut self, hook: Arc<dyn RuntimeHook>) -> Self {
        self.config.hooks.push(hook);
        self
    }

    pub fn trace_sink(mut self, sink: Arc<dyn TraceSink>) -> Self {
        self.config.trace_sink = Some(sink);
        self
    }

    pub fn trace_id(mut self, trace_id: impl Into<String>) -> Self {
        self.config.trace_id = Some(trace_id.into());
        self
    }

    pub fn workflow_name(mut self, workflow_name: impl Into<String>) -> Self {
        self.config.workflow_name = Some(workflow_name.into());
        self
    }

    pub fn event_store(mut self, store: Arc<dyn RunEventStore>) -> Self {
        self.config.event_store = Some(store);
        self
    }

    pub fn event_store_fail_closed(mut self, fail_closed: bool) -> Self {
        self.config.event_store_fail_closed = fail_closed;
        self
    }

    pub fn approval_provider(mut self, provider: Arc<dyn ApprovalProvider>) -> Self {
        self.config.approval_provider = Some(provider);
        self
    }

    pub fn approval_timeout(mut self, timeout: Duration) -> Self {
        self.config.approval_timeout = Some(timeout);
        self
    }

    pub fn approval_broker(mut self, broker: ApprovalBroker) -> Self {
        self.config.approval_broker = Some(broker);
        self
    }

    pub fn context_provider(mut self, provider: Arc<dyn ContextProvider>) -> Self {
        self.config.context_providers.push(provider);
        self
    }

    pub fn max_context_chars(mut self, max_chars: usize) -> Self {
        self.config.max_context_chars = Some(max_chars);
        self
    }

    pub fn memory_provider(mut self, provider: Arc<dyn MemoryProvider>) -> Self {
        self.config.memory_providers.push(provider);
        self
    }

    pub fn app_state<T>(mut self, app_state: T) -> Self
    where
        T: Send + Sync + 'static,
    {
        self.config.app_state = Some(Arc::new(app_state));
        self
    }

    pub fn app_state_arc(mut self, app_state: Arc<dyn std::any::Any + Send + Sync>) -> Self {
        self.config.app_state = Some(app_state);
        self
    }

    pub fn initial_shared_state(mut self, shared_state: Metadata) -> Self {
        self.config.initial_shared_state = shared_state;
        self
    }

    pub fn shared_state(self, shared_state: Metadata) -> Self {
        self.initial_shared_state(shared_state)
    }

    pub fn tool_registry_factory(
        mut self,
        factory: impl Fn() -> ToolRegistry + Send + Sync + 'static,
    ) -> Self {
        self.config.tool_registry_factory = Some(Arc::new(factory));
        self
    }

    pub fn tool_registry_factory_arc(mut self, factory: ToolRegistryFactory) -> Self {
        self.config.tool_registry_factory = Some(factory);
        self
    }

    pub fn log_preview_chars(mut self, max_chars: usize) -> Self {
        self.config.log_preview_chars = Some(max_chars);
        self
    }

    pub fn debug_dump_dir(mut self, path: impl Into<PathBuf>) -> Self {
        self.config.debug_dump_dir = Some(path.into());
        self
    }

    pub fn before_cycle_messages(
        mut self,
        provider: impl Fn(u32, &[Message], &Metadata) -> Vec<Message> + Send + Sync + 'static,
    ) -> Self {
        self.config.before_cycle_messages = Some(Arc::new(provider));
        self
    }

    pub fn before_cycle_messages_arc(mut self, provider: BeforeCycleMessageProvider) -> Self {
        self.config.before_cycle_messages = Some(provider);
        self
    }

    pub fn interruption_messages(
        mut self,
        provider: impl Fn() -> Vec<Message> + Send + Sync + 'static,
    ) -> Self {
        self.config.interruption_messages = Some(Arc::new(provider));
        self
    }

    pub fn interruption_messages_arc(mut self, provider: InterruptionMessageProvider) -> Self {
        self.config.interruption_messages = Some(provider);
        self
    }

    pub fn sub_task_manager(mut self, manager: SubTaskManager) -> Self {
        self.config.sub_task_manager = Some(manager);
        self
    }

    pub fn runtime_log_handler(
        mut self,
        handler: impl Fn(&str, &Metadata) + Send + Sync + 'static,
    ) -> Self {
        self.config.runtime_log_handler = Some(Arc::new(handler));
        self
    }

    pub fn runtime_log_handler_arc(mut self, handler: RuntimeEventHandler) -> Self {
        self.config.runtime_log_handler = Some(handler);
        self
    }

    pub fn runtime_stream_callback(
        mut self,
        callback: impl Fn(&Metadata) + Send + Sync + 'static,
    ) -> Self {
        self.config.runtime_stream_callback = Some(Arc::new(callback));
        self
    }

    pub fn runtime_stream_callback_arc(mut self, callback: LlmStreamCallback) -> Self {
        self.config.runtime_stream_callback = Some(callback);
        self
    }

    pub fn budget_limits(mut self, limits: RunBudgetLimits) -> Self {
        self.config.budget_limits = Some(limits);
        self
    }

    pub fn host_cost_meter(mut self, meter: impl HostCostMeter + 'static) -> Self {
        self.config.host_cost_meter = Some(Arc::new(meter));
        self
    }

    pub fn host_cost_meter_arc(mut self, meter: Arc<dyn HostCostMeter>) -> Self {
        self.config.host_cost_meter = Some(meter);
        self
    }

    pub fn checkpoint_config(mut self, checkpoint_config: CheckpointConfig) -> Self {
        self.config.checkpoint_config = Some(checkpoint_config);
        self
    }

    pub fn checkpoint_extension(mut self, extension: impl CheckpointExtension + 'static) -> Self {
        self.config.checkpoint_extensions.push(Arc::new(extension));
        self
    }

    pub fn checkpoint_extension_arc(mut self, extension: Arc<dyn CheckpointExtension>) -> Self {
        self.config.checkpoint_extensions.push(extension);
        self
    }

    pub fn reconciliation_provider(
        mut self,
        provider: impl ReconciliationProvider + 'static,
    ) -> Self {
        self.config.reconciliation_provider = Some(Arc::new(provider));
        self
    }

    pub fn reconciliation_provider_arc(
        mut self,
        provider: Arc<dyn ReconciliationProvider>,
    ) -> Self {
        self.config.reconciliation_provider = Some(provider);
        self
    }

    pub fn metadata(mut self, key: impl Into<String>, value: Value) -> Self {
        self.config.metadata.insert(key.into(), value);
        self
    }

    pub fn build(self) -> RunConfig {
        self.config
    }
}