Skip to main content

wesichain_core/
serde.rs

1use serde::{Deserialize, Serialize};
2use serde_json::Value;
3use std::collections::HashMap;
4
5/// A serializable representation of a Runnable.
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
7#[serde(tag = "type", rename_all = "snake_case")]
8pub enum SerializableRunnable {
9    Chain {
10        steps: Vec<SerializableRunnable>,
11    },
12    Parallel {
13        steps: HashMap<String, SerializableRunnable>,
14    },
15    Fallbacks {
16        primary: Box<SerializableRunnable>,
17        fallbacks: Vec<SerializableRunnable>,
18    },
19    Llm {
20        model: String,
21        #[serde(default)]
22        params: HashMap<String, Value>,
23    },
24    Parser {
25        kind: String, // "str", "json", "structured"
26        #[serde(default)]
27        target_type: Option<String>,
28    },
29    Prompt {
30        template: String,
31        input_variables: Vec<String>,
32    },
33    Tool {
34        name: String,
35        description: Option<String>,
36        schema: Option<Value>,
37    },
38    Passthrough,
39}
40
41impl SerializableRunnable {
42    pub fn to_json(&self) -> serde_json::Result<String> {
43        serde_json::to_string(self)
44    }
45
46    pub fn from_json(json: &str) -> serde_json::Result<Self> {
47        serde_json::from_str(json)
48    }
49}
50
51// Conversion to actual Runnable trait objects is handled in `src/persistence.rs` via `reconstruct`.