synth-ai-core 0.3.0

Rust core for Synth AI SDKs - API client, streaming, tracing, and tunnels
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
use crate::errors::CoreError;
use chrono::Utc;
use serde_json::{Map, Value};
use url::Url;
use uuid::Uuid;

fn parse_url(inference_url: &str) -> Option<Url> {
    if let Ok(parsed) = Url::parse(inference_url) {
        return Some(parsed);
    }
    if inference_url.starts_with('/') {
        let candidate = format!("http://localhost{}", inference_url);
        return Url::parse(&candidate).ok();
    }
    if !inference_url.contains("://") {
        let candidate = format!("http://{}", inference_url);
        return Url::parse(&candidate).ok();
    }
    None
}

pub fn extract_trace_correlation_id(inference_url: Option<&str>) -> Option<String> {
    let raw = inference_url?.trim();
    if raw.is_empty() {
        return None;
    }

    let parsed = parse_url(raw)?;
    let path_segments: Vec<&str> = parsed.path().split('/').filter(|s| !s.is_empty()).collect();

    if path_segments.len() >= 3 {
        let tail = &path_segments[path_segments.len() - 2..];
        if tail == ["chat", "completions"] {
            if let Some(segment) = path_segments.get(path_segments.len() - 3) {
                if segment.starts_with("trace_")
                    || segment.starts_with("cid_")
                    || segment.starts_with("eval_")
                {
                    return Some(segment.to_string());
                }
            }
        }
    }

    for segment in path_segments.iter().rev() {
        if segment.starts_with("trace_")
            || segment.starts_with("cid_")
            || segment.starts_with("eval_")
        {
            return Some(segment.to_string());
        }
    }

    let query = parsed.query().unwrap_or("");
    for (key, value) in url::form_urlencoded::parse(query.as_bytes()) {
        let key = key.to_string();
        if key == "cid" || key == "trace_correlation_id" || key == "trace" {
            let val = value.to_string();
            if !val.trim().is_empty() {
                return Some(val);
            }
        }
    }

    None
}

pub fn validate_trace_correlation_id(
    trace_correlation_id: Option<&str>,
    inference_url: Option<&str>,
    fatal: bool,
) -> Result<Option<String>, CoreError> {
    if let Some(cid) = trace_correlation_id {
        if !cid.trim().is_empty() {
            return Ok(Some(cid.to_string()));
        }
    }

    let inference_url = inference_url.unwrap_or("NOT_SET");
    let error_msg = format!(
        "CRITICAL: Cannot extract trace_correlation_id!\n\nID: UNKNOWN\nInference URL: {}\n\nChecked:\n1. inference_url path segments\n2. inference_url query params\n\nTask app CANNOT proceed without trace_correlation_id.\nThis indicates the RL trainer is not sending it correctly.\n\nSee monorepo/trace_creation_and_judgement.txt 'Fatal Guards' section.\n",
        inference_url
    );

    if fatal {
        return Err(CoreError::InvalidInput(error_msg));
    }

    Ok(None)
}

pub fn include_trace_correlation_id_in_response(
    response_data: &Value,
    trace_correlation_id: Option<&str>,
) -> Value {
    let cid = match trace_correlation_id {
        Some(value) if !value.trim().is_empty() => value.trim(),
        _ => return response_data.clone(),
    };

    let mut output = response_data.clone();
    let obj = match output.as_object_mut() {
        Some(obj) => obj,
        None => return output,
    };

    obj.entry("trace_correlation_id".to_string())
        .or_insert_with(|| Value::String(cid.to_string()));

    if let Some(trace_val) = obj.get_mut("trace") {
        if let Some(trace_obj) = trace_val.as_object_mut() {
            let meta = trace_obj
                .entry("metadata".to_string())
                .or_insert_with(|| Value::Object(Map::new()));
            if let Some(meta_obj) = meta.as_object_mut() {
                meta_obj
                    .entry("trace_correlation_id".to_string())
                    .or_insert_with(|| Value::String(cid.to_string()));
                let corr_ids = meta_obj
                    .entry("correlation_ids".to_string())
                    .or_insert_with(|| Value::Object(Map::new()));
                if let Some(corr_map) = corr_ids.as_object_mut() {
                    corr_map
                        .entry("trace_correlation_id".to_string())
                        .or_insert_with(|| Value::String(cid.to_string()));
                }
            }

            if let Some(session_trace) = trace_obj.get_mut("session_trace") {
                if let Some(session_obj) = session_trace.as_object_mut() {
                    let session_meta = session_obj
                        .entry("metadata".to_string())
                        .or_insert_with(|| Value::Object(Map::new()));
                    if let Some(session_meta_obj) = session_meta.as_object_mut() {
                        session_meta_obj
                            .entry("trace_correlation_id".to_string())
                            .or_insert_with(|| Value::String(cid.to_string()));
                    }
                }
            }
        }
    }

    output
}

pub fn build_trace_payload(
    messages: &Value,
    response: Option<&Value>,
    correlation_id: Option<&str>,
    session_id: Option<&str>,
    metadata: Option<&Value>,
) -> Value {
    let messages_value = match messages {
        Value::Array(list) => Value::Array(list.clone()),
        _ => Value::Array(Vec::new()),
    };

    let llm_response = match response {
        Some(Value::Object(obj)) => {
            if obj.contains_key("message") {
                Value::Object(obj.clone())
            } else if let Some(Value::Array(choices)) = obj.get("choices") {
                if let Some(Value::Object(first)) = choices.get(0) {
                    let mut map = Map::new();
                    map.insert(
                        "message".to_string(),
                        first.get("message").cloned().unwrap_or(Value::Null),
                    );
                    map.insert(
                        "usage".to_string(),
                        obj.get("usage")
                            .cloned()
                            .unwrap_or(Value::Object(Map::new())),
                    );
                    map.insert(
                        "finish_reason".to_string(),
                        first.get("finish_reason").cloned().unwrap_or(Value::Null),
                    );
                    Value::Object(map)
                } else {
                    Value::Object(obj.clone())
                }
            } else {
                Value::Object(obj.clone())
            }
        }
        Some(other) => other.clone(),
        None => Value::Object(Map::new()),
    };

    let mut llm_event = Map::new();
    llm_event.insert("type".to_string(), Value::String("lm_call".to_string()));
    llm_event.insert(
        "event_type".to_string(),
        Value::String("lm_call".to_string()),
    );
    llm_event.insert(
        "timestamp".to_string(),
        Value::String(Utc::now().to_rfc3339()),
    );
    let mut llm_request = Map::new();
    llm_request.insert("messages".to_string(), messages_value);
    llm_event.insert("llm_request".to_string(), Value::Object(llm_request));
    llm_event.insert("llm_response".to_string(), llm_response);
    llm_event.insert("api_format".to_string(), Value::String("chat".to_string()));
    if let Some(cid) = correlation_id {
        if !cid.trim().is_empty() {
            llm_event.insert("correlation_id".to_string(), Value::String(cid.to_string()));
        }
    }

    let mut event_history = Vec::new();
    event_history.push(Value::Object(llm_event));

    let mut trace_meta = match metadata {
        Some(Value::Object(obj)) => obj.clone(),
        _ => Map::new(),
    };
    let session_id = session_id
        .map(|s| s.to_string())
        .unwrap_or_else(|| Uuid::new_v4().to_string());
    trace_meta
        .entry("session_id".to_string())
        .or_insert(Value::String(session_id));

    if let Some(cid) = correlation_id {
        if !cid.trim().is_empty() {
            trace_meta
                .entry("trace_correlation_id".to_string())
                .or_insert(Value::String(cid.to_string()));
            let corr_ids = trace_meta
                .entry("correlation_ids".to_string())
                .or_insert_with(|| Value::Object(Map::new()));
            if let Some(corr_map) = corr_ids.as_object_mut() {
                corr_map
                    .entry("trace_correlation_id".to_string())
                    .or_insert(Value::String(cid.to_string()));
            }
        }
    }

    let mut trace = Map::new();
    trace.insert(
        "schema_version".to_string(),
        Value::String("4.0".to_string()),
    );
    trace.insert("event_history".to_string(), Value::Array(event_history));
    trace.insert(
        "markov_blanket_message_history".to_string(),
        Value::Array(Vec::new()),
    );
    trace.insert("metadata".to_string(), Value::Object(trace_meta));

    Value::Object(trace)
}

pub fn build_trajectory_trace(
    messages: &Value,
    response: Option<&Value>,
    correlation_id: Option<&str>,
    session_id: Option<&str>,
    metadata: Option<&Value>,
) -> Value {
    build_trace_payload(messages, response, correlation_id, session_id, metadata)
}

pub fn include_event_history_in_response(
    response_data: &Value,
    messages: Option<&Value>,
    response: Option<&Value>,
    run_id: &str,
    correlation_id: Option<&str>,
) -> Value {
    let mut output = response_data.clone();
    let obj = match output.as_object_mut() {
        Some(obj) => obj,
        None => return output,
    };

    let trace_val = obj
        .entry("trace".to_string())
        .or_insert_with(|| Value::Object(Map::new()));
    let trace_obj = match trace_val.as_object_mut() {
        Some(obj) => obj,
        None => return output,
    };

    let mut event_history = trace_obj.get("event_history");
    if event_history.is_none() {
        if let Some(Value::Object(session_trace)) = trace_obj.get("session_trace") {
            event_history = session_trace.get("event_history");
        }
    }
    if matches!(event_history, Some(Value::Array(list)) if !list.is_empty()) {
        return output;
    }

    let meta_map = {
        let mut map = Map::new();
        map.insert("run_id".to_string(), Value::String(run_id.to_string()));
        Value::Object(map)
    };

    let new_trace = build_trace_payload(
        messages.unwrap_or(&Value::Array(Vec::new())),
        response,
        correlation_id,
        None,
        Some(&meta_map),
    );

    if let Some(new_trace_obj) = new_trace.as_object() {
        let incoming_meta = trace_obj.get("metadata");
        let mut merged_meta = match new_trace_obj.get("metadata") {
            Some(Value::Object(meta)) => meta.clone(),
            _ => Map::new(),
        };
        if let Some(Value::Object(existing)) = incoming_meta {
            for (k, v) in existing.iter() {
                merged_meta.insert(k.clone(), v.clone());
            }
        }
        trace_obj.insert("metadata".to_string(), Value::Object(merged_meta));
        if let Some(schema) = new_trace_obj.get("schema_version") {
            trace_obj
                .entry("schema_version".to_string())
                .or_insert_with(|| schema.clone());
        }
        if let Some(events) = new_trace_obj.get("event_history") {
            trace_obj.insert("event_history".to_string(), events.clone());
        }
        if let Some(blanket) = new_trace_obj.get("markov_blanket_message_history") {
            trace_obj
                .entry("markov_blanket_message_history".to_string())
                .or_insert_with(|| blanket.clone());
        }

        let events_clone = trace_obj.get("event_history").cloned();
        if let Some(Value::Object(session_trace)) = trace_obj.get_mut("session_trace") {
            if !session_trace.contains_key("event_history") {
                if let Some(events) = events_clone {
                    session_trace.insert("event_history".to_string(), events);
                }
            }
        }
    }

    output
}

pub fn include_event_history_in_trajectories(
    response_data: &Value,
    messages_by_trajectory: Option<&Value>,
    responses_by_trajectory: Option<&Value>,
    run_id: &str,
    correlation_id: Option<&str>,
) -> Value {
    let messages = messages_by_trajectory
        .and_then(|value| value.as_array())
        .and_then(|arr| arr.get(0));
    let response = responses_by_trajectory
        .and_then(|value| value.as_array())
        .and_then(|arr| arr.get(0));

    include_event_history_in_response(response_data, messages, response, run_id, correlation_id)
}

pub fn verify_trace_correlation_id_in_response(
    response_data: &Value,
    expected_correlation_id: Option<&str>,
) -> bool {
    let expected = match expected_correlation_id {
        Some(value) if !value.trim().is_empty() => value,
        _ => return false,
    };

    let obj = match response_data.as_object() {
        Some(obj) => obj,
        None => return false,
    };

    if obj.get("trace_correlation_id").and_then(|v| v.as_str()) != Some(expected) {
        return false;
    }

    let trace_val = match obj.get("trace") {
        Some(Value::Object(trace)) => trace,
        _ => return false,
    };

    let mut trace_meta_id = trace_val
        .get("metadata")
        .and_then(|v| v.as_object())
        .and_then(|meta| meta.get("trace_correlation_id"))
        .and_then(|v| v.as_str());

    if trace_meta_id != Some(expected) {
        if let Some(Value::Object(session_trace)) = trace_val.get("session_trace") {
            trace_meta_id = session_trace
                .get("metadata")
                .and_then(|v| v.as_object())
                .and_then(|meta| meta.get("trace_correlation_id"))
                .and_then(|v| v.as_str());
        }
    }

    trace_meta_id == Some(expected)
}