operonx 0.6.2

High-performance Rust execution backend for Operon workflows
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
419
420
421
422
423
424
425
426
//! `Media` / `MediaRef` — trace-extractable media wrappers.
//!
//! Mirrors Python [`operonx/core/media.py`](../../../../operonx/core/media.py).
//!
//! Producer ops wrap media values at the source:
//!
//! ```rust,ignore
//! #[op]
//! fn tts(text: String) -> Map<String, Value> {
//!     Map::from_iter([(
//!         "audio".into(),
//!         Media::bytes(synthesize(&text), "audio/mp3").into_value(),
//!     )])
//! }
//! ```
//!
//! Downstream consumers read the raw value — [`BaseOp::get_inputs`] auto-unwraps
//! `Media` instances so schemas stay plain. The collector walks state directly
//! (not via input binding), so it sees the wrapper intact and extracts blobs
//! into a parallel `node.media` list before flushing to a tracer backend.
//!
//! # JSON representation
//! Rust represents `Media` as a plain JSON object with a reserved marker
//! field `"__media__": true`. This lets it round-trip through [`serde_json::Value`]
//! without a custom deserializer on every downstream struct.
//!
//! [`BaseOp::get_inputs`]: crate::core::ops::base::BaseOp

use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};

/// Marker key identifying a [`Media`] payload encoded inside a `Value::Object`.
pub const MEDIA_MARKER: &str = "__media__";

/// Media payload — raw bytes or a URL/path reference + MIME type.
///
/// Matches Python's `Media(data, mime_type)` dataclass; `data` may be either
/// a `bytes` payload (base64 on the wire) or a URL/path `String`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Media {
    /// Raw bytes, a `data:` URL, an http(s) URL, or a file path.
    pub data: MediaData,
    /// MIME type like `image/png`, `audio/mp3`, `video/webm`.
    pub mime_type: String,
}

/// Two-variant payload matching Python's `Union[bytes, str]`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(untagged)]
pub enum MediaData {
    /// Raw bytes.
    Bytes(#[serde(with = "base64_bytes")] Vec<u8>),
    /// URL (http/https, data:), file path, or any string reference.
    String(String),
}

impl Media {
    /// Build a `Media` wrapping raw bytes.
    pub fn bytes(data: impl Into<Vec<u8>>, mime_type: impl Into<String>) -> Self {
        Self {
            data: MediaData::Bytes(data.into()),
            mime_type: mime_type.into(),
        }
    }

    /// Build a `Media` wrapping a URL or file-path reference.
    pub fn reference(data: impl Into<String>, mime_type: impl Into<String>) -> Self {
        Self {
            data: MediaData::String(data.into()),
            mime_type: mime_type.into(),
        }
    }

    /// Encode as a JSON object with the `__media__` marker.
    pub fn into_value(self) -> Value {
        let mut obj = Map::new();
        obj.insert(MEDIA_MARKER.into(), Value::Bool(true));
        match self.data {
            MediaData::Bytes(b) => {
                obj.insert("data".into(), Value::String(base64_encode(&b)));
                obj.insert("encoding".into(), Value::String("base64".into()));
            }
            MediaData::String(s) => {
                obj.insert("data".into(), Value::String(s));
            }
        }
        obj.insert("mime_type".into(), Value::String(self.mime_type));
        Value::Object(obj)
    }

    /// Decode a `Value` back into a `Media`, if it carries the marker.
    pub fn from_value(v: &Value) -> Option<Self> {
        let Value::Object(obj) = v else { return None };
        if obj.get(MEDIA_MARKER) != Some(&Value::Bool(true)) {
            return None;
        }
        let mime_type = obj.get("mime_type")?.as_str()?.to_string();
        let data_val = obj.get("data")?.as_str()?;
        let data = match obj.get("encoding").and_then(|v| v.as_str()) {
            Some("base64") => MediaData::Bytes(base64_decode(data_val).ok()?),
            _ => MediaData::String(data_val.to_string()),
        };
        Some(Self { data, mime_type })
    }

    /// Size in bytes — `data.len()` for both variants.
    pub fn size_bytes(&self) -> usize {
        match &self.data {
            MediaData::Bytes(b) => b.len(),
            MediaData::String(s) => s.len(),
        }
    }
}

/// Collector-emitted reference to an extracted media blob.
///
/// The collector replaces each `Media` instance in a node's trace I/O with a
/// `<media:N>` placeholder string and appends a `MediaRef` to the node's
/// `media` list. Tracers walk the list, upload or drop each blob per their
/// backend's capabilities, then substitute the placeholder in the I/O dict
/// at `field_path`.
///
/// `field_path` uses dotted JSONPath-style rooted at `"inputs"` or
/// `"outputs"`, e.g. `"inputs.messages[0].content[1].image_url.url"`.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct MediaRef {
    pub field_path: String,
    pub data: MediaData,
    pub mime_type: String,
    pub size_bytes: usize,
}

impl MediaRef {
    /// Build a [`MediaRef`] from a [`Media`] + its path in the I/O tree.
    pub fn from_media(media: Media, field_path: impl Into<String>) -> Self {
        let size = media.size_bytes();
        Self {
            field_path: field_path.into(),
            data: media.data,
            mime_type: media.mime_type,
            size_bytes: size,
        }
    }
}

// ── Walk + extract / substitute helpers ──────────────────────────────────

/// Recursively replace `Media` payloads inside `io` with `<media:N>` strings.
///
/// `root` is the field-path prefix — pass `"inputs"` or `"outputs"`. Returns
/// the stripped dict + the list of extracted refs in discovery order.
pub fn extract_media(io: &Map<String, Value>, root: &str) -> (Map<String, Value>, Vec<MediaRef>) {
    let mut refs: Vec<MediaRef> = Vec::new();
    let stripped = io
        .iter()
        .map(|(k, v)| {
            let child_path = format!("{}.{}", root, k);
            (k.clone(), walk(v, &child_path, &mut refs))
        })
        .collect();
    (stripped, refs)
}

fn walk(value: &Value, path: &str, refs: &mut Vec<MediaRef>) -> Value {
    if let Some(media) = Media::from_value(value) {
        let idx = refs.len();
        refs.push(MediaRef::from_media(media, path));
        return Value::String(format!("<media:{}>", idx));
    }
    match value {
        Value::Object(m) => {
            let mapped = m
                .iter()
                .map(|(k, v)| (k.clone(), walk(v, &format!("{}.{}", path, k), refs)))
                .collect();
            Value::Object(mapped)
        }
        Value::Array(a) => {
            let mapped = a
                .iter()
                .enumerate()
                .map(|(i, v)| walk(v, &format!("{}[{}]", path, i), refs))
                .collect();
            Value::Array(mapped)
        }
        other => other.clone(),
    }
}

/// Walk `io` along `field_path` and replace the leaf value with `replacement`.
///
/// Returns `true` on successful substitution, `false` if the path no longer
/// exists (tracer should log and skip). Field paths include the root
/// (`"inputs"`/`"outputs"`) as the first segment — it is ignored since
/// `io` is already at that level.
pub fn substitute_placeholder(
    io: &mut Map<String, Value>,
    field_path: &str,
    replacement: Value,
) -> bool {
    let segments = split_path(field_path);
    if segments.len() < 2 {
        return false;
    }
    // Drop the `"inputs"` / `"outputs"` root.
    let mut iter = segments.into_iter();
    iter.next();
    let last = match iter.next_back() {
        Some(s) => s,
        None => return false,
    };
    let rest: Vec<PathSeg> = iter.collect();

    let mut cursor: &mut Value = match rest.first() {
        Some(PathSeg::Key(k)) => match io.get_mut(k) {
            Some(v) => v,
            None => return false,
        },
        Some(PathSeg::Index(_)) => return false, // root is a map, not a list
        None => {
            // Direct top-level write: the leaf sits in `io`.
            return set_top(io, last, replacement);
        }
    };

    for seg in rest.iter().skip(1) {
        cursor = match step_mut(cursor, seg) {
            Some(c) => c,
            None => return false,
        };
    }
    set(cursor, &last, replacement)
}

// ── Path parsing ─────────────────────────────────────────────────────────

#[derive(Debug, Clone, PartialEq, Eq)]
enum PathSeg {
    Key(String),
    Index(usize),
}

fn split_path(path: &str) -> Vec<PathSeg> {
    let mut out = Vec::new();
    let mut buf = String::new();
    let bytes = path.as_bytes();
    let mut i = 0;
    while i < bytes.len() {
        let ch = bytes[i] as char;
        if ch == '.' {
            if !buf.is_empty() {
                out.push(PathSeg::Key(std::mem::take(&mut buf)));
            }
            i += 1;
        } else if ch == '[' {
            if !buf.is_empty() {
                out.push(PathSeg::Key(std::mem::take(&mut buf)));
            }
            let end_rel = match path[i..].find(']') {
                Some(p) => p,
                None => return Vec::new(),
            };
            let end = i + end_rel;
            let idx: usize = match path[i + 1..end].parse() {
                Ok(n) => n,
                Err(_) => return Vec::new(),
            };
            out.push(PathSeg::Index(idx));
            i = end + 1;
        } else {
            buf.push(ch);
            i += 1;
        }
    }
    if !buf.is_empty() {
        out.push(PathSeg::Key(buf));
    }
    out
}

fn step_mut<'a>(cursor: &'a mut Value, seg: &PathSeg) -> Option<&'a mut Value> {
    match (cursor, seg) {
        (Value::Object(m), PathSeg::Key(k)) => m.get_mut(k),
        (Value::Array(a), PathSeg::Index(i)) => a.get_mut(*i),
        _ => None,
    }
}

fn set(cursor: &mut Value, seg: &PathSeg, replacement: Value) -> bool {
    match (cursor, seg) {
        (Value::Object(m), PathSeg::Key(k)) if m.contains_key(k) => {
            m.insert(k.clone(), replacement);
            true
        }
        (Value::Array(a), PathSeg::Index(i)) if *i < a.len() => {
            a[*i] = replacement;
            true
        }
        _ => false,
    }
}

fn set_top(io: &mut Map<String, Value>, seg: PathSeg, replacement: Value) -> bool {
    match seg {
        PathSeg::Key(k) if io.contains_key(&k) => {
            io.insert(k, replacement);
            true
        }
        _ => false,
    }
}

// ── base64 helpers (zero-dep alternative uses the `base64` crate already in deps) ──

fn base64_encode(bytes: &[u8]) -> String {
    use base64::{engine::general_purpose::STANDARD, Engine as _};
    STANDARD.encode(bytes)
}

fn base64_decode(s: &str) -> Result<Vec<u8>, base64::DecodeError> {
    use base64::{engine::general_purpose::STANDARD, Engine as _};
    STANDARD.decode(s)
}

mod base64_bytes {
    use base64::{engine::general_purpose::STANDARD, Engine as _};
    use serde::{Deserialize, Deserializer, Serializer};

    pub fn serialize<S: Serializer>(bytes: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&STANDARD.encode(bytes))
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> {
        let s = String::deserialize(d)?;
        STANDARD.decode(s).map_err(serde::de::Error::custom)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[test]
    fn media_roundtrip_via_value() {
        let m = Media::bytes(vec![0x01, 0x02, 0x03], "image/png");
        let v = m.clone().into_value();
        let back = Media::from_value(&v).unwrap();
        assert_eq!(back, m);
    }

    #[test]
    fn media_url_reference() {
        let m = Media::reference("https://example.com/a.png", "image/png");
        let v = m.clone().into_value();
        let back = Media::from_value(&v).unwrap();
        assert_eq!(back, m);
    }

    #[test]
    fn extract_media_strips_and_collects() {
        let audio = Media::reference("https://example.com/a.mp3", "audio/mp3").into_value();
        let img = Media::bytes(vec![0xaa, 0xbb], "image/png").into_value();

        let mut io = Map::new();
        io.insert(
            "payload".into(),
            json!({
                "audio": audio,
                "images": [img],
                "text": "hello",
            }),
        );

        let (stripped, refs) = extract_media(&io, "inputs");
        assert_eq!(refs.len(), 2);
        assert_eq!(
            stripped.get("payload").unwrap().get("audio"),
            Some(&Value::String("<media:0>".into()))
        );
        assert_eq!(
            stripped
                .get("payload")
                .unwrap()
                .get("images")
                .unwrap()
                .get(0),
            Some(&Value::String("<media:1>".into()))
        );
        assert_eq!(refs[0].field_path, "inputs.payload.audio");
        assert_eq!(refs[1].field_path, "inputs.payload.images[0]");
        assert_eq!(refs[1].size_bytes, 2);
    }

    #[test]
    fn substitute_replaces_placeholder() {
        let mut io = Map::new();
        io.insert(
            "payload".into(),
            json!({
                "audio": "<media:0>",
                "images": ["<media:1>"],
            }),
        );
        assert!(substitute_placeholder(
            &mut io,
            "inputs.payload.audio",
            Value::String("lf:audio-1".into())
        ));
        assert!(substitute_placeholder(
            &mut io,
            "inputs.payload.images[0]",
            Value::String("lf:img-1".into())
        ));
        assert_eq!(
            io.get("payload").unwrap().get("audio"),
            Some(&Value::String("lf:audio-1".into()))
        );
        assert_eq!(
            io.get("payload").unwrap().get("images").unwrap().get(0),
            Some(&Value::String("lf:img-1".into()))
        );
    }
}