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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
//! Jobs API client.
//!
//! This module provides methods for submitting, polling, and canceling
//! optimization jobs (GEPA, MIPRO).

use std::collections::HashMap;
use std::time::{Duration, Instant};

use reqwest::header::{HeaderMap, HeaderValue};
use serde::Deserialize;
use serde_json::Value;

use crate::http::HttpError;
use crate::polling::{calculate_backoff, BackoffConfig};
use crate::CoreError;

use super::client::SynthClient;
use super::types::{
    CancelRequest, GepaJobRequest, JobSubmitResponse, MiproJobRequest, PauseRequest,
    PolicyJobStatus, PromptLearningResult,
};

/// Canonical API endpoint root for job status/events.
const JOBS_ENDPOINT: &str = "/api/jobs";

/// Canonical create endpoint for GEPA jobs.
const GEPA_CREATE_ENDPOINT: &str = "/api/jobs/gepa";

/// Canonical create endpoint for MIPRO jobs.
const MIPRO_CREATE_ENDPOINT: &str = "/api/jobs/mipro";

/// Legacy API endpoint for prompt learning job submission (fallback).
const LEGACY_SUBMIT_ENDPOINT: &str = "/api/prompt-learning/online/jobs";
/// Legacy policy-optimization status endpoint (fallback for old backends).
const LEGACY_POLICY_STATUS_ENDPOINT: &str = "/api/policy-optimization/online/jobs";
/// Legacy prompt-learning status endpoint (fallback for old backends).
const LEGACY_PROMPT_STATUS_ENDPOINT: &str = "/api/prompt-learning/online/jobs";

/// Jobs API client.
///
/// Use this to submit, poll, and cancel optimization jobs (GEPA, MIPRO).
pub struct JobsClient<'a> {
    client: &'a SynthClient,
}

impl<'a> JobsClient<'a> {
    /// Create a new Jobs client.
    pub(crate) fn new(client: &'a SynthClient) -> Self {
        Self { client }
    }

    /// Submit a GEPA optimization job.
    ///
    /// # Arguments
    ///
    /// * `request` - The GEPA job configuration
    ///
    /// # Returns
    ///
    /// The job ID on success.
    ///
    /// # Example
    ///
    /// ```ignore
    /// let job_id = client.jobs().submit_gepa(GepaJobRequest {
    ///     container_url: "http://localhost:8000".into(),
    ///     env_name: "default".into(),
    ///     policy: PolicyConfig::default(),
    ///     gepa: GepaConfig::default(),
    ///     ..Default::default()
    /// }).await?;
    /// ```
    pub async fn submit_gepa(&self, request: GepaJobRequest) -> Result<String, CoreError> {
        let worker_token = request.container_worker_token.clone();
        let body = serde_json::to_value(&request)
            .map_err(|e| CoreError::Validation(format!("failed to serialize request: {}", e)))?;

        // Try canonical endpoint first, fall back to legacy on 404/405/non-JSON.
        let canonical_result: Result<JobSubmitResponse, HttpError> = if let Some(ref token) = worker_token {
            let mut headers = HeaderMap::new();
            headers.insert(
                "X-SynthTunnel-Worker-Token",
                HeaderValue::from_str(token).map_err(|_| {
                    CoreError::Validation("invalid SynthTunnel worker token".to_string())
                })?,
            );
            self.client
                .http
                .post_json_with_headers(GEPA_CREATE_ENDPOINT, &body, Some(headers))
                .await
        } else {
            self.client
                .http
                .post_json(GEPA_CREATE_ENDPOINT, &body)
                .await
        };

        match canonical_result {
            Ok(response) => return Ok(response.job_id),
            Err(HttpError::Response(detail)) if detail.status == 404 || detail.status == 405 => {
                // Fall back to legacy endpoint.
            }
            Err(HttpError::JsonParse(_)) => {
                // Backend returned non-JSON response; fall back to legacy.
            }
            Err(err) => return Err(map_http_error(err)),
        }

        // Legacy fallback
        let response: JobSubmitResponse = if let Some(token) = worker_token {
            let mut headers = HeaderMap::new();
            headers.insert(
                "X-SynthTunnel-Worker-Token",
                HeaderValue::from_str(&token).map_err(|_| {
                    CoreError::Validation("invalid SynthTunnel worker token".to_string())
                })?,
            );
            self.client
                .http
                .post_json_with_headers(LEGACY_SUBMIT_ENDPOINT, &body, Some(headers))
                .await
                .map_err(map_http_error)?
        } else {
            self.client
                .http
                .post_json(LEGACY_SUBMIT_ENDPOINT, &body)
                .await
                .map_err(map_http_error)?
        };

        Ok(response.job_id)
    }

    /// Submit a MIPRO optimization job.
    ///
    /// # Arguments
    ///
    /// * `request` - The MIPRO job configuration
    ///
    /// # Returns
    ///
    /// The job ID on success.
    pub async fn submit_mipro(&self, request: MiproJobRequest) -> Result<String, CoreError> {
        let worker_token = request.container_worker_token.clone();
        let body = serde_json::to_value(&request)
            .map_err(|e| CoreError::Validation(format!("failed to serialize request: {}", e)))?;
        let response: JobSubmitResponse = if let Some(token) = worker_token {
            let mut headers = HeaderMap::new();
            headers.insert(
                "X-SynthTunnel-Worker-Token",
                HeaderValue::from_str(&token).map_err(|_| {
                    CoreError::Validation("invalid SynthTunnel worker token".to_string())
                })?,
            );
            self.client
                .http
                .post_json_with_headers(MIPRO_CREATE_ENDPOINT, &body, Some(headers))
                .await
                .map_err(map_http_error)?
        } else {
            self.client
                .http
                .post_json(MIPRO_CREATE_ENDPOINT, &body)
                .await
                .map_err(map_http_error)?
        };

        Ok(response.job_id)
    }

    /// Submit a generic optimization job from a JSON value.
    ///
    /// Use this when you have a pre-built job configuration.
    /// Submit a generic optimization job from a JSON value.
    ///
    /// Use this when you have a pre-built job configuration.
    /// Falls back to the legacy endpoint since the job type is unknown.
    pub async fn submit_raw(&self, request: Value) -> Result<String, CoreError> {
        self.submit_raw_with_worker_token(request, None).await
    }

    /// Submit a generic optimization job with optional SynthTunnel worker token.
    pub async fn submit_raw_with_worker_token(
        &self,
        request: Value,
        worker_token: Option<String>,
    ) -> Result<String, CoreError> {
        let mut headers_opt: Option<HeaderMap> = if let Some(token) = worker_token {
            let mut headers = HeaderMap::new();
            headers.insert(
                "X-SynthTunnel-Worker-Token",
                HeaderValue::from_str(&token).map_err(|_| {
                    CoreError::Validation("invalid SynthTunnel worker token".to_string())
                })?,
            );
            Some(headers)
        } else {
            None
        };

        // Route raw submits to canonical algorithm endpoints when possible.
        // This prevents MIPRO configs from being accidentally dispatched via GEPA paths.
        if let Some(endpoint) = infer_algorithm_endpoint(&request) {
            let canonical_result: Result<JobSubmitResponse, HttpError> =
                if let Some(headers) = headers_opt.clone() {
                    self.client
                        .http
                        .post_json_with_headers(endpoint, &request, Some(headers))
                        .await
                } else {
                    self.client.http.post_json(endpoint, &request).await
                };
            match canonical_result {
                Ok(response) => return Ok(response.job_id),
                Err(HttpError::Response(detail)) if detail.status == 404 || detail.status == 405 => {
                    // Older backends may not expose canonical endpoints yet,
                    // or may return 405 Method Not Allowed.
                }
                Err(HttpError::JsonParse(_)) => {
                    // Backend returned non-JSON response (e.g. plain text "ok").
                    // Fall back to legacy endpoint.
                }
                Err(err) => return Err(map_http_error(err)),
            }
        }

        let response: JobSubmitResponse = if let Some(headers) = headers_opt.take() {
            self.client
                .http
                .post_json_with_headers(LEGACY_SUBMIT_ENDPOINT, &request, Some(headers))
                .await
                .map_err(map_http_error)?
        } else {
            self.client
                .http
                .post_json(LEGACY_SUBMIT_ENDPOINT, &request)
                .await
                .map_err(map_http_error)?
        };

        Ok(response.job_id)
    }

    /// Get the current status of a job.
    ///
    /// # Arguments
    ///
    /// * `job_id` - The job ID to check
    ///
    /// # Returns
    ///
    /// The current job result including status, best score, etc.
    pub async fn get_status(&self, job_id: &str) -> Result<PromptLearningResult, CoreError> {
        let canonical = format!("{}/{}", JOBS_ENDPOINT, job_id);
        match self.client.http.get(&canonical, None).await {
            Ok(payload) => parse_prompt_learning_result(payload, job_id),
            Err(err) if err.status() == Some(404) => {
                let legacy_paths = [
                    format!("{}/{}", LEGACY_POLICY_STATUS_ENDPOINT, job_id),
                    format!("{}/{}", LEGACY_PROMPT_STATUS_ENDPOINT, job_id),
                ];
                let mut last_not_found: Option<HttpError> = Some(err);
                for path in legacy_paths {
                    match self.client.http.get(&path, None).await {
                        Ok(payload) => return parse_prompt_learning_result(payload, job_id),
                        Err(legacy_err) if legacy_err.status() == Some(404) => {
                            last_not_found = Some(legacy_err);
                        }
                        Err(legacy_err) => return Err(map_http_error(legacy_err)),
                    }
                }
                Err(map_http_error(
                    last_not_found.expect("at least canonical 404 error must be set"),
                ))
            }
            Err(err) => Err(map_http_error(err)),
        }
    }

    /// Poll a job until it reaches a terminal state.
    ///
    /// This will repeatedly check the job status until it succeeds, fails,
    /// or the timeout is reached.
    ///
    /// # Arguments
    ///
    /// * `job_id` - The job ID to poll
    /// * `timeout_secs` - Maximum time to wait (in seconds)
    /// * `interval_secs` - Initial polling interval (in seconds)
    ///
    /// # Returns
    ///
    /// The final job result.
    ///
    /// # Example
    ///
    /// ```ignore
    /// // Poll for up to 1 hour, checking every 15 seconds
    /// let result = client.jobs().poll_until_complete(&job_id, 3600.0, 15.0).await?;
    /// if result.status.is_success() {
    ///     println!("Best score: {:?}", result.best_score);
    /// }
    /// ```
    pub async fn poll_until_complete(
        &self,
        job_id: &str,
        timeout_secs: f64,
        interval_secs: f64,
    ) -> Result<PromptLearningResult, CoreError> {
        let start = Instant::now();
        let timeout = Duration::from_secs_f64(timeout_secs);
        let base_interval_ms = (interval_secs * 1000.0) as u64;

        let config = BackoffConfig::new(base_interval_ms, 60000, 4);

        let mut consecutive_errors = 0u32;
        let max_errors = 10u32;

        loop {
            // Check timeout
            if start.elapsed() > timeout {
                return Err(CoreError::Timeout(format!(
                    "job {} did not complete within {:.0} seconds",
                    job_id, timeout_secs
                )));
            }

            // Get status
            match self.get_status(job_id).await {
                Ok(result) => {
                    consecutive_errors = 0;

                    if result.status.is_terminal() {
                        match result.status {
                            PolicyJobStatus::Failed => {
                                let error = result.error.as_deref().unwrap_or("unknown");
                                eprintln!("[synth_ai_core] Job {} FAILED: {}", job_id, error);
                            }
                            PolicyJobStatus::Cancelled => {
                                eprintln!("[synth_ai_core] Job {} was cancelled", job_id);
                            }
                            _ => {}
                        }
                        return Ok(result);
                    }

                    if result.status == PolicyJobStatus::Paused {
                        eprintln!("[synth_ai_core] Job {} is paused", job_id);
                        return Ok(result);
                    }

                    // Wait before next poll
                    let delay = calculate_backoff(&config, 0);
                    tokio::time::sleep(delay).await;
                }
                Err(e) => {
                    consecutive_errors += 1;

                    if consecutive_errors >= max_errors {
                        return Err(CoreError::Internal(format!(
                            "too many consecutive errors polling job {}: {}",
                            job_id, e
                        )));
                    }

                    // Backoff on errors
                    let delay = calculate_backoff(&config, consecutive_errors);
                    tokio::time::sleep(delay).await;
                }
            }
        }
    }

    /// Cancel a running job.
    ///
    /// # Arguments
    ///
    /// * `job_id` - The job ID to cancel
    /// * `reason` - Optional cancellation reason
    pub async fn cancel(&self, job_id: &str, reason: Option<&str>) -> Result<(), CoreError> {
        let path = format!("{}/{}/cancel", JOBS_ENDPOINT, job_id);
        let body = serde_json::to_value(&CancelRequest {
            reason: reason.map(|s| s.to_string()),
        })
        .unwrap_or(Value::Object(serde_json::Map::new()));

        let _: Value = self
            .client
            .http
            .post_json(&path, &body)
            .await
            .map_err(map_http_error)?;

        Ok(())
    }

    /// Pause a running job.
    ///
    /// # Arguments
    ///
    /// * `job_id` - The job ID to pause
    /// * `reason` - Optional pause reason
    pub async fn pause(&self, job_id: &str, reason: Option<&str>) -> Result<(), CoreError> {
        let path = format!("{}/{}/pause", JOBS_ENDPOINT, job_id);
        let body = serde_json::to_value(&PauseRequest {
            reason: reason.map(|s| s.to_string()),
        })
        .unwrap_or(Value::Object(serde_json::Map::new()));

        let _: Value = self
            .client
            .http
            .post_json(&path, &body)
            .await
            .map_err(map_http_error)?;

        Ok(())
    }

    /// Resume a paused job.
    ///
    /// # Arguments
    ///
    /// * `job_id` - The job ID to resume
    /// * `reason` - Optional resume reason
    pub async fn resume(&self, job_id: &str, reason: Option<&str>) -> Result<(), CoreError> {
        let path = format!("{}/{}/resume", JOBS_ENDPOINT, job_id);
        let body = serde_json::to_value(&PauseRequest {
            reason: reason.map(|s| s.to_string()),
        })
        .unwrap_or(Value::Object(serde_json::Map::new()));

        let _: Value = self
            .client
            .http
            .post_json(&path, &body)
            .await
            .map_err(map_http_error)?;

        Ok(())
    }

    /// List recent jobs.
    ///
    /// # Arguments
    ///
    /// * `limit` - Maximum number of jobs to return
    /// * `status` - Optional status filter
    pub async fn list(
        &self,
        limit: Option<i32>,
        status: Option<PolicyJobStatus>,
    ) -> Result<Vec<PromptLearningResult>, CoreError> {
        let mut params = vec![];

        let limit_str;
        if let Some(l) = limit {
            limit_str = l.to_string();
            params.push(("limit", limit_str.as_str()));
        }

        let status_str;
        if let Some(s) = status {
            status_str = s.as_str().to_string();
            params.push(("status", status_str.as_str()));
        }

        let params_ref: Option<&[(&str, &str)]> = if params.is_empty() {
            None
        } else {
            Some(&params)
        };

        self.client
            .http
            .get(JOBS_ENDPOINT, params_ref)
            .await
            .map_err(map_http_error)
    }
}

#[derive(Debug, Deserialize)]
struct PromptLearningResultWire {
    #[serde(default)]
    job_id: Option<String>,
    #[serde(default)]
    status: Option<String>,
    #[serde(default)]
    best_reward: Option<f64>,
    #[serde(default)]
    best_score: Option<f64>,
    #[serde(default)]
    best_candidate: Option<Value>,
    #[serde(default)]
    best_prompt: Option<Value>,
    #[serde(default)]
    lever_summary: Option<Value>,
    #[serde(default)]
    sensor_frames: Vec<Value>,
    #[serde(default)]
    lever_versions: HashMap<String, i64>,
    #[serde(default)]
    best_lever_version: Option<i64>,
    #[serde(default)]
    error: Option<String>,
    #[serde(default)]
    generations_completed: Option<i32>,
    #[serde(default)]
    candidates_evaluated: Option<i32>,
}

fn parse_prompt_learning_result(
    payload: Value,
    fallback_job_id: &str,
) -> Result<PromptLearningResult, CoreError> {
    let wire: PromptLearningResultWire = serde_json::from_value(payload).map_err(|err| {
        CoreError::Protocol(format!("invalid prompt learning status payload: {err}"))
    })?;
    let status = wire
        .status
        .as_deref()
        .and_then(PolicyJobStatus::from_str)
        .unwrap_or(PolicyJobStatus::Pending);
    Ok(PromptLearningResult {
        job_id: wire.job_id.unwrap_or_else(|| fallback_job_id.to_string()),
        status,
        best_reward: wire.best_reward.or(wire.best_score),
        best_candidate: wire.best_candidate.or(wire.best_prompt),
        lever_summary: wire.lever_summary,
        sensor_frames: wire.sensor_frames,
        lever_versions: wire.lever_versions,
        best_lever_version: wire.best_lever_version,
        error: wire.error,
        generations_completed: wire.generations_completed,
        candidates_evaluated: wire.candidates_evaluated,
    })
}

fn infer_algorithm_endpoint(request: &Value) -> Option<&'static str> {
    let from_section = |name: &str| {
        request
            .get(name)
            .and_then(|value| value.get("algorithm"))
            .and_then(|value| value.as_str())
    };
    let algorithm = from_section("prompt_learning")
        .or_else(|| from_section("policy_optimization"))
        .or_else(|| request.get("algorithm").and_then(|value| value.as_str()))?
        .trim()
        .to_ascii_lowercase();
    match algorithm.as_str() {
        "gepa" => Some(GEPA_CREATE_ENDPOINT),
        "mipro" => Some(MIPRO_CREATE_ENDPOINT),
        _ => None,
    }
}

/// Map HTTP errors to CoreError.
fn map_http_error(e: HttpError) -> CoreError {
    match e {
        HttpError::Response(detail) => {
            if detail.status == 401 || detail.status == 403 {
                CoreError::Authentication(format!("authentication failed: {}", detail))
            } else if detail.status == 429 {
                CoreError::UsageLimit(crate::UsageLimitInfo::from_http_429("jobs", &detail))
            } else {
                CoreError::HttpResponse(crate::HttpErrorInfo {
                    status: detail.status,
                    url: detail.url,
                    message: detail.message,
                    body_snippet: detail.body_snippet,
                })
            }
        }
        HttpError::Request(e) => CoreError::Http(e),
        _ => CoreError::Internal(format!("{}", e)),
    }
}

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

    #[test]
    fn test_jobs_endpoint() {
        assert_eq!(JOBS_ENDPOINT, "/api/jobs");
        assert_eq!(GEPA_CREATE_ENDPOINT, "/api/jobs/gepa");
        assert_eq!(MIPRO_CREATE_ENDPOINT, "/api/jobs/mipro");
        assert_eq!(LEGACY_SUBMIT_ENDPOINT, "/api/prompt-learning/online/jobs");
        assert_eq!(
            LEGACY_POLICY_STATUS_ENDPOINT,
            "/api/policy-optimization/online/jobs"
        );
        assert_eq!(
            LEGACY_PROMPT_STATUS_ENDPOINT,
            "/api/prompt-learning/online/jobs"
        );
    }

    #[test]
    fn test_infer_algorithm_endpoint() {
        assert_eq!(
            infer_algorithm_endpoint(&serde_json::json!({
                "prompt_learning": {"algorithm": "mipro"}
            })),
            Some(MIPRO_CREATE_ENDPOINT)
        );
        assert_eq!(
            infer_algorithm_endpoint(&serde_json::json!({
                "policy_optimization": {"algorithm": "GEPA"}
            })),
            Some(GEPA_CREATE_ENDPOINT)
        );
        assert_eq!(
            infer_algorithm_endpoint(&serde_json::json!({
                "algorithm": "unknown"
            })),
            None
        );
    }

    #[test]
    fn test_parse_prompt_learning_result_accepts_both_best_candidate_and_best_prompt() {
        let payload = serde_json::json!({
            "job_id": "pl_test",
            "status": "running",
            "best_score": 0.42,
            "best_candidate": {"instruction": "new"},
            "best_prompt": {"instruction": "legacy"},
            "lever_versions": {"mipro.prompt.sys": 3},
        });
        let parsed =
            parse_prompt_learning_result(payload, "fallback").expect("parse should succeed");
        assert_eq!(parsed.job_id, "pl_test");
        assert_eq!(parsed.status, PolicyJobStatus::Running);
        assert_eq!(parsed.best_reward, Some(0.42));
        assert_eq!(
            parsed
                .best_candidate
                .as_ref()
                .and_then(|v| v.get("instruction"))
                .and_then(|v| v.as_str()),
            Some("new")
        );
        assert_eq!(parsed.lever_versions.get("mipro.prompt.sys"), Some(&3));
    }
}