vv_agent/runtime/backends/distributed/
dispatch.rs1use serde_json::Value;
2
3use crate::runtime::backends::RuntimeRecipe;
4use crate::types::{AgentResult, AgentTask};
5
6#[derive(Debug, Clone, PartialEq)]
7pub struct CycleDispatchResult {
8 pub finished: bool,
9 pub result: Option<AgentResult>,
10}
11
12impl CycleDispatchResult {
13 pub fn unfinished() -> Self {
14 Self {
15 finished: false,
16 result: None,
17 }
18 }
19
20 pub fn finished(result: AgentResult) -> Self {
21 Self {
22 finished: true,
23 result: Some(result),
24 }
25 }
26
27 pub fn to_dict(&self) -> Value {
28 let mut payload =
29 serde_json::Map::from_iter([("finished".to_string(), Value::Bool(self.finished))]);
30 if let Some(result) = &self.result {
31 payload.insert("result".to_string(), result.to_dict());
32 }
33 Value::Object(payload)
34 }
35
36 pub fn from_dict(data: &Value) -> Result<Self, String> {
37 let object = data
38 .as_object()
39 .ok_or_else(|| "CycleDispatchResult payload must be an object".to_string())?;
40 let finished = object
41 .get("finished")
42 .and_then(Value::as_bool)
43 .unwrap_or(false);
44 let result = object
45 .get("result")
46 .filter(|value| !value.is_null())
47 .map(AgentResult::from_dict)
48 .transpose()?;
49 Ok(Self { finished, result })
50 }
51}
52
53pub trait CycleDispatcher: Send + Sync {
54 fn dispatch_cycle(
55 &self,
56 task: &AgentTask,
57 recipe: &RuntimeRecipe,
58 cycle_name: &str,
59 cycle_index: u32,
60 ) -> Result<CycleDispatchResult, String>;
61}