akribes-sdk 0.22.2

Rust client SDK for the Akribes workflow server
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
use std::collections::{HashMap, HashSet};
use std::ops::Deref;
use std::sync::Arc;
use std::time::Duration;

use crate::client::{AkribesClient, Inner};
use crate::error::{AkribesError, Result};
use crate::models::*;

/// Pre-dispatch contract validation.
fn validate_contract(
    inner: &Inner,
    script_name: &str,
    document_keys: Option<&[&str]>,
) -> Result<()> {
    if inner.broken_scripts.lock().unwrap().contains(script_name) {
        return Err(AkribesError::ScriptSchemaChanged {
            script_name: script_name.to_string(),
        });
    }

    if let Some(doc_keys) = document_keys {
        let schemas = inner.schema_cache.lock().unwrap();
        if let Some(schema) = schemas.get(script_name) {
            let expected_docs: Vec<&str> = schema
                .iter()
                .filter(|(_, ty)| ty == "document")
                .map(|(name, _)| name.as_str())
                .collect();
            let provided: HashSet<&str> = doc_keys.iter().copied().collect();
            let missing: Vec<String> = expected_docs
                .iter()
                .filter(|n| !provided.contains(**n))
                .map(|n| n.to_string())
                .collect();
            let expected: HashSet<&str> = expected_docs.into_iter().collect();
            let extra: Vec<String> = doc_keys
                .iter()
                .filter(|k| !expected.contains(**k))
                .map(|k| k.to_string())
                .collect();
            if !missing.is_empty() || !extra.is_empty() {
                return Err(AkribesError::ScriptInputMismatch {
                    script_name: script_name.to_string(),
                    missing,
                    extra,
                });
            }
        }
    }

    Ok(())
}

// ── ExecutionsClient (global) ───────────────────────────────────────────────

/// Sub-client for execution operations that are not project-scoped
/// (lookup, resume, cancel, document helpers, await). Obtained via
/// [`AkribesClient::executions()`].
///
/// For project-scoped operations (run, list, etc.), see
/// [`ScopedExecutionsClient`] via [`crate::client::ProjectScope::executions`].
#[derive(Clone, Debug)]
pub struct ExecutionsClient {
    pub(crate) inner: Arc<Inner>,
}

impl ExecutionsClient {
    pub(crate) fn new(inner: Arc<Inner>) -> Self {
        Self { inner }
    }

    fn c(&self) -> AkribesClient {
        AkribesClient {
            inner: Arc::clone(&self.inner),
        }
    }

    /// Resume a suspended checkpoint within a running execution.
    pub async fn resume(
        &self,
        execution_id: &str,
        token: &str,
        data: serde_json::Value,
    ) -> Result<serde_json::Value> {
        let url = format!("{}/executions/{}/resume", self.inner.base_url, execution_id);
        self.c()
            .post(
                &url,
                &ResumeRequest {
                    token: token.to_string(),
                    data,
                },
            )
            .await
    }

    /// Cancel a specific execution by ID; returns `true` if it was cancelled.
    pub async fn cancel(&self, execution_id: &str) -> Result<bool> {
        let url = format!("{}/executions/{}", self.inner.base_url, execution_id);
        self.c().delete(&url).await
    }

    /// List child executions spawned by `execution_id` via the engine's
    /// `spawn_child_execution` callback (#1054). Returns an empty `Vec` when
    /// no children exist (the common case for v1 where parent-linkage
    /// columns are typically NULL). Mirrors TS `executions.children` and
    /// Python `executions.children`.
    pub async fn children(&self, execution_id: &str) -> Result<Vec<ExecutionChildSummary>> {
        let url = format!(
            "{}/executions/{}/children",
            self.inner.base_url, execution_id
        );
        self.c().get_list(&url).await
    }

    /// Fetch the full status record for an execution.
    pub async fn get(&self, execution_id: &str) -> Result<Option<ExecutionStatus>> {
        let url = format!("{}/executions/{}", self.inner.base_url, execution_id);
        self.c().get_opt(&url).await
    }

    /// Per-task cost / token / duration breakdown for an execution
    /// (`GET /executions/{id}/tasks`). Reads from the `execution_tasks`
    /// table populated as `TaskEnd` events arrive, so callers don't have to
    /// parse the event JSON to recover this data. Useful for monolith
    /// workflows where there are no spawned children — every agent
    /// invocation lives in `execution_tasks` keyed by `task_name`.
    ///
    /// 404 → `Ok(None)` (the execution doesn't exist or isn't accessible to
    /// this token), matching `get` / `get_output`. Mirrors the TS SDK's
    /// `executions.tasks`.
    pub async fn tasks(&self, execution_id: &str) -> Result<Option<ExecutionTasksResponse>> {
        let url = format!(
            "{}/executions/{}/tasks",
            self.inner.base_url,
            urlencoding::encode(execution_id)
        );
        self.c().get_opt(&url).await
    }

    /// Fetch only the output (status, error, result) for an execution.
    pub async fn get_output(&self, execution_id: &str) -> Result<Option<ExecutionOutput>> {
        let url = format!("{}/executions/{}/output", self.inner.base_url, execution_id);
        self.c().get_opt(&url).await
    }

    /// Return the persisted event stream for an execution. `types` is an
    /// optional comma-separated allowlist (`"TaskEnd,Error,WorkflowEnd"`)
    /// that lets callers slim down responses on long workflows.
    pub async fn get_events(
        &self,
        execution_id: &str,
        after_id: Option<i64>,
        limit: Option<i64>,
        types: Option<&str>,
    ) -> Result<Option<ExecutionEvents>> {
        #[derive(serde::Serialize)]
        struct Q<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            after_id: Option<i64>,
            #[serde(skip_serializing_if = "Option::is_none")]
            limit: Option<i64>,
            #[serde(skip_serializing_if = "Option::is_none")]
            types: Option<&'a str>,
        }
        let base = format!("{}/executions/{}/events", self.inner.base_url, execution_id);
        let url = AkribesClient::url_with_query(
            &base,
            &Q {
                after_id,
                limit,
                types,
            },
        );
        let res = self.c().send(self.c().inner.http.get(&url)).await?;
        if res.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(None);
        }
        Ok(Some(crate::client::decode_json(res).await?))
    }

    // ── Document helpers ──────────────────────────────────────────────────

    /// Get document metadata by ID.
    pub async fn get_document(&self, document_id: &str) -> Result<Option<DocumentMeta>> {
        let url = format!(
            "{}/documents/{}",
            self.inner.base_url,
            urlencoding::encode(document_id)
        );
        self.c().get_opt(&url).await
    }

    /// Get converted markdown for a document.
    pub async fn get_document_markdown(&self, document_id: &str) -> Result<String> {
        let url = format!(
            "{}/documents/{}/markdown",
            self.inner.base_url,
            urlencoding::encode(document_id)
        );
        let res = self.c().send(self.c().inner.http.get(&url)).await?;
        let body: serde_json::Value = crate::client::decode_json(res).await?;
        // The server contract is `{"markdown": "<string>"}`. A missing or
        // non-string `markdown` field is a server-contract violation, not an
        // "empty document" — surface it rather than silently returning "".
        match body.get("markdown") {
            Some(serde_json::Value::String(s)) => Ok(s.clone()),
            other => Err(AkribesError::Other(format!(
                "GET /documents/{}/markdown returned a malformed response: \
                 expected a string `markdown` field, got {}",
                document_id,
                match other {
                    None => "no `markdown` field".to_string(),
                    Some(v) => format!("{v}"),
                },
            ))),
        }
    }

    /// Get a presigned download URL for the original document file.
    pub async fn get_document_url(&self, document_id: &str) -> Result<String> {
        let url = format!(
            "{}/documents/{}/content",
            self.inner.base_url,
            urlencoding::encode(document_id)
        );
        let resp = self
            .c()
            .send(self.c().inner.http.get(&url).header("Accept", "*/*"))
            .await?;
        Ok(resp
            .headers()
            .get("location")
            .and_then(|v| v.to_str().ok())
            .map(|s| s.to_string())
            .unwrap_or_else(|| resp.url().to_string()))
    }

    /// Retry conversion on a failed document.
    pub async fn reconvert_document(&self, document_id: &str) -> Result<serde_json::Value> {
        let url = format!(
            "{}/documents/{}/convert",
            self.inner.base_url,
            urlencoding::encode(document_id)
        );
        self.c().post(&url, &serde_json::json!({})).await
    }

    /// Run a script from a specific point with pre-seeded environment values.
    ///
    /// Convenience wrapper that doesn't require building a [`ScopedExecutionsClient`]
    /// first — useful for callers (e.g. akribes-mcp) that already know the
    /// `project_id` numerically. Forwards to
    /// [`ScopedExecutionsClient::run_from`].
    ///
    /// `seed_env` carries pre-computed variable values for nodes already
    /// completed by a prior execution; `skip_node_ids` lists those node IDs
    /// so the engine skips re-running them. `channel` defaults to `draft`.
    pub async fn run_from_node(
        &self,
        project_id: i64,
        script_name: &str,
        seed_env: HashMap<String, serde_json::Value>,
        skip_node_ids: Vec<usize>,
        channel: Option<&str>,
        inputs: Option<HashMap<String, serde_json::Value>>,
    ) -> Result<RunResult> {
        let scoped = ScopedExecutionsClient::new(Arc::clone(&self.inner), project_id);
        scoped
            .run_from(script_name, seed_env, skip_node_ids, channel, inputs, None)
            .await
    }

    /// Poll `get_output` until the execution reaches a terminal state.
    pub async fn await_execution(
        &self,
        execution_id: &str,
        timeout_ms: Option<u64>,
        poll_interval_ms: Option<u64>,
    ) -> Result<ExecutionOutput> {
        let interval = Duration::from_millis(poll_interval_ms.unwrap_or(500));
        let deadline = timeout_ms.map(|ms| std::time::Instant::now() + Duration::from_millis(ms));

        loop {
            if let Some(deadline) = deadline {
                if std::time::Instant::now() >= deadline {
                    return Err(AkribesError::Timeout {
                        execution_id: Some(execution_id.to_string()),
                    });
                }
            }

            let output = self.get_output(execution_id).await?;
            if let Some(output) = output {
                match output.status.as_str() {
                    "completed" => return Ok(output),
                    "failed" | "cancelled" => {
                        let msg = output.error.clone().unwrap_or_default();
                        let eid = Some(execution_id.to_string());
                        return Err(match output.error_kind.as_deref() {
                            // #1296: accept the legacy "ServerError" wire
                            // form for back-compat plus the four new
                            // status-specific variants.
                            Some("RateLimit")
                            | Some("ServerError")
                            | Some("ServerError500")
                            | Some("BadGateway502")
                            | Some("ServiceUnavailable503")
                            | Some("GatewayTimeout504")
                            | Some("NetworkError") => {
                                let status = match output.error_kind.as_deref() {
                                    Some("ServerError500") => Some(500u16),
                                    Some("BadGateway502") => Some(502u16),
                                    Some("ServiceUnavailable503") => Some(503u16),
                                    Some("GatewayTimeout504") => Some(504u16),
                                    Some("RateLimit") => Some(429u16),
                                    _ => None,
                                };
                                AkribesError::Transient {
                                    message: msg,
                                    execution_id: eid,
                                    retry_after: None,
                                    status,
                                }
                            }
                            Some("AuthError") | Some("TokenLimit") => AkribesError::Fatal {
                                message: msg,
                                execution_id: eid,
                            },
                            _ => AkribesError::Script {
                                message: msg,
                                execution_id: eid,
                            },
                        });
                    }
                    _ => {}
                }
            }

            tokio::time::sleep(interval).await;
        }
    }

    /// Cross-SDK naming alias for [`await_execution`]. Mirrors the Python
    /// SDK's `await_result`; forwarded verbatim so callers porting examples
    /// across SDKs don't trip on the rename. Refs #109 (item 3:
    /// method-naming consistency).
    pub async fn await_result(
        &self,
        execution_id: &str,
        timeout_ms: Option<u64>,
        poll_interval_ms: Option<u64>,
    ) -> Result<ExecutionOutput> {
        self.await_execution(execution_id, timeout_ms, poll_interval_ms)
            .await
    }
}

// ── ScopedExecutionsClient (project-scoped) ─────────────────────────────────

/// Project-scoped execution operations. Obtained via
/// [`crate::client::ProjectScope::executions`].
///
/// Derefs to [`ExecutionsClient`], so global methods (`get`, `cancel`,
/// `resume`, `await_execution`, document helpers) are available too.
#[derive(Clone, Debug)]
pub struct ScopedExecutionsClient {
    base: ExecutionsClient,
    project_id: i64,
}

impl Deref for ScopedExecutionsClient {
    type Target = ExecutionsClient;
    fn deref(&self) -> &Self::Target {
        &self.base
    }
}

impl ScopedExecutionsClient {
    pub(crate) fn new(inner: Arc<Inner>, project_id: i64) -> Self {
        Self {
            base: ExecutionsClient { inner },
            project_id,
        }
    }

    fn c(&self) -> AkribesClient {
        AkribesClient {
            inner: Arc::clone(&self.base.inner),
        }
    }

    fn project_url(&self) -> String {
        format!("{}/projects/{}", self.base.inner.base_url, self.project_id)
    }

    fn script_url(&self, name: &str) -> String {
        format!(
            "{}/scripts/{}",
            self.project_url(),
            urlencoding::encode(name)
        )
    }

    /// Start building a script run.
    pub fn run(&self, script_name: &str) -> RunBuilder {
        RunBuilder {
            client: self.c(),
            inner: Arc::clone(&self.base.inner),
            project_id: self.project_id,
            script_name: script_name.to_string(),
            channel: "production".to_string(),
            inputs: None,
            triggered_by: None,
            breakpoint_lines: None,
        }
    }

    /// Subscribe to the SSE event stream *first*, then kick off a run, and
    /// return a [`RunStream`] that yields typed [`WorkflowEvent`]s until the
    /// workflow terminates.
    ///
    /// `req` is a [`RunBuilder`] (from [`Self::run`]) carrying the script
    /// name, channel, inputs and other run parameters.
    ///
    /// [`RunStream`]: crate::sub::run_stream::RunStream
    /// [`WorkflowEvent`]: crate::events::WorkflowEvent
    pub async fn run_stream(&self, req: RunBuilder) -> Result<crate::sub::run_stream::RunStream> {
        crate::sub::run_stream::start_run_stream(Arc::clone(&self.base.inner), self.project_id, req)
            .await
    }

    /// Start building an execution list query.
    pub fn list(&self, script_name: &str) -> ListExecutionsBuilder {
        ListExecutionsBuilder {
            client: self.c(),
            script_url: self.script_url(script_name),
            status: None,
            channel: None,
            limit: None,
            offset: None,
        }
    }

    /// Cancel all running executions for a script; returns `true` if any were cancelled.
    pub async fn cancel_run(&self, script_name: &str) -> Result<bool> {
        let url = format!("{}/run", self.script_url(script_name));
        self.c().delete(&url).await
    }

    /// Cross-SDK naming alias for [`cancel_run`]. Mirrors the Python SDK's
    /// `cancel_all`; forwarded verbatim so callers porting examples across
    /// SDKs don't trip on the rename. Refs #109 (item 3: method-naming
    /// consistency).
    pub async fn cancel_all(&self, script_name: &str) -> Result<bool> {
        self.cancel_run(script_name).await
    }

    /// Run a script from a specific point with pre-seeded environment values.
    pub async fn run_from(
        &self,
        script_name: &str,
        seed_env: HashMap<String, serde_json::Value>,
        skip_node_ids: Vec<usize>,
        channel: Option<&str>,
        inputs: Option<HashMap<String, serde_json::Value>>,
        triggered_by: Option<&str>,
    ) -> Result<RunResult> {
        let channel = channel.unwrap_or("draft");
        let url = format!(
            "{}/run/from?channel={}",
            self.script_url(script_name),
            urlencoding::encode(channel)
        );
        let tb = triggered_by
            .map(|s| s.to_string())
            .unwrap_or_else(|| self.base.inner.name.clone());
        self.c()
            .post(
                &url,
                &RunFromRequest {
                    inputs,
                    seed_env,
                    skip_node_ids,
                    triggered_by: Some(tb),
                },
            )
            .await
    }

    /// Get the compiled execution DAG for a script.
    pub async fn get_graph(
        &self,
        script_name: &str,
        version_id: Option<i64>,
    ) -> Result<GraphResponse> {
        #[derive(serde::Serialize)]
        struct Q {
            #[serde(skip_serializing_if = "Option::is_none")]
            version: Option<i64>,
        }
        let base = format!("{}/graph", self.script_url(script_name));
        let url = AkribesClient::url_with_query(
            &base,
            &Q {
                version: version_id,
            },
        );
        let res = self.c().send(self.c().inner.http.get(&url)).await?;
        crate::client::decode_json(res).await
    }

    /// Get cost aggregation for the entire project.
    pub async fn get_project_cost(
        &self,
        since: Option<&str>,
        until: Option<&str>,
    ) -> Result<crate::models::ProjectCost> {
        #[derive(serde::Serialize)]
        struct Q<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            since: Option<&'a str>,
            #[serde(skip_serializing_if = "Option::is_none")]
            until: Option<&'a str>,
        }
        let base = format!("{}/cost", self.project_url());
        let url = AkribesClient::url_with_query(&base, &Q { since, until });
        let res = self.c().send(self.c().inner.http.get(&url)).await?;
        crate::client::decode_json(res).await
    }

    /// Get cost aggregation for a script.
    pub async fn get_cost(&self, script_name: &str) -> Result<CostAggregation> {
        let url = format!("{}/cost", self.script_url(script_name));
        self.c().get_opt::<CostAggregation>(&url).await.map(|o| {
            o.unwrap_or_else(|| CostAggregation {
                total_executions: 0,
                total_cost_usd: 0.0,
                avg_cost_usd: 0.0,
                total_input_tokens: 0,
                total_output_tokens: 0,
                total_tool_tokens: 0,
                by_version: vec![],
            })
        })
    }

    /// Run a script with file uploads that get converted to Markdown via Docling.
    pub async fn run_with_upload(
        &self,
        script_name: &str,
        files: HashMap<String, (String, Vec<u8>)>,
        channel: Option<&str>,
        triggered_by: Option<&str>,
    ) -> Result<RunResult> {
        let channel = channel.unwrap_or("production");
        let url = format!(
            "{}/run/upload?channel={}",
            self.script_url(script_name),
            urlencoding::encode(channel)
        );

        let mut form = reqwest::multipart::Form::new();
        for (input_name, (filename, data)) in files {
            let part = reqwest::multipart::Part::bytes(data)
                .file_name(filename)
                .mime_str("application/octet-stream")
                .expect("valid static MIME type");
            form = form.part(input_name, part);
        }

        let tb = triggered_by
            .map(|s| s.to_string())
            .unwrap_or_else(|| self.base.inner.name.clone());
        let meta = serde_json::json!({ "triggered_by": tb });
        form = form.text("_meta", meta.to_string());

        self.c().post_multipart(&url, form).await
    }

    /// Run a script with documents sourced from S3.
    pub async fn run_with_s3(
        &self,
        script_name: &str,
        inputs: HashMap<String, S3DocumentRef>,
        channel: Option<&str>,
        triggered_by: Option<&str>,
    ) -> Result<RunResult> {
        let url = format!("{}/run/s3", self.script_url(script_name));
        let tb = triggered_by
            .map(|s| s.to_string())
            .unwrap_or_else(|| self.base.inner.name.clone());
        self.c()
            .post(
                &url,
                &RunWithS3Request {
                    inputs,
                    channel: channel.map(|s| s.to_string()),
                    triggered_by: Some(tb),
                },
            )
            .await
    }
}

// ── RunBuilder ──────────────────────────────────────────────────────────────

/// Builder for a script run.
///
/// Inputs may be supplied one-at-a-time with [`input`](Self::input) (and the
/// [`document`](Self::document) / [`documents`](Self::documents) ergonomic
/// shortcuts for doc-id references), or in bulk with [`inputs`](Self::inputs).
/// All merge into the same map; later writes to the same key overwrite earlier
/// ones.
///
/// ```no_run
/// # use akribes_sdk::AkribesClient;
/// # use serde_json::json;
/// # async fn demo() -> akribes_sdk::Result<()> {
/// let client = AkribesClient::builder("http://localhost:8080").token("tok").build();
/// let _run = client.project(1).executions().run("my_script")
///     .input("age", 25)
///     .input("tags", json!(["a", "b"]))
///     .document("resume", "doc_00000000-0000-0000-0000-000000000001")
///     .execute()
///     .await?;
/// # Ok(()) }
/// ```
#[derive(Debug, Clone)]
#[must_use = "a builder does nothing until .execute() is called"]
pub struct RunBuilder {
    client: AkribesClient,
    inner: Arc<Inner>,
    project_id: i64,
    script_name: String,
    channel: String,
    inputs: Option<HashMap<String, serde_json::Value>>,
    triggered_by: Option<String>,
    breakpoint_lines: Option<Vec<usize>>,
}

impl RunBuilder {
    /// The script this builder will run.
    pub fn script_name(&self) -> &str {
        &self.script_name
    }

    fn script_url(&self) -> String {
        format!(
            "{}/projects/{}/scripts/{}",
            self.inner.base_url,
            self.project_id,
            urlencoding::encode(&self.script_name)
        )
    }

    pub fn channel(mut self, channel: impl Into<String>) -> Self {
        self.channel = channel.into();
        self
    }

    /// Replace the inputs map in bulk. Merges into any previously-set inputs —
    /// entries with the same key are overwritten by this call.
    pub fn inputs(mut self, inputs: HashMap<String, serde_json::Value>) -> Self {
        match &mut self.inputs {
            Some(existing) => existing.extend(inputs),
            None => self.inputs = Some(inputs),
        }
        self
    }

    /// Set one input. Overwrites any previous value for the same name.
    pub fn input<V: Into<serde_json::Value>>(mut self, name: impl Into<String>, value: V) -> Self {
        self.inputs
            .get_or_insert_with(HashMap::new)
            .insert(name.into(), value.into());
        self
    }

    /// Convenience for setting a `document`-typed input from a `doc_<uuid>`
    /// reference. The server resolves it to markdown before the workflow runs.
    /// Inline content is no longer supported — use `input name: markdown`
    /// for that.
    pub fn document(self, name: impl Into<String>, doc_id: impl Into<String>) -> Self {
        self.input(name, serde_json::Value::String(doc_id.into()))
    }

    /// Convenience for setting a `list[document]`-typed input from an iterable
    /// of `doc_<uuid>` references. Each is resolved independently.
    pub fn documents<I, S>(self, name: impl Into<String>, doc_ids: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        let arr: Vec<serde_json::Value> = doc_ids
            .into_iter()
            .map(|d| serde_json::Value::String(d.into()))
            .collect();
        self.input(name, serde_json::Value::Array(arr))
    }

    pub fn triggered_by(mut self, triggered_by: impl Into<String>) -> Self {
        self.triggered_by = Some(triggered_by.into());
        self
    }

    pub fn breakpoint_lines(mut self, lines: Vec<usize>) -> Self {
        self.breakpoint_lines = Some(lines);
        self
    }

    pub async fn execute(self) -> Result<RunResult> {
        let input_keys: Vec<&str> = self
            .inputs
            .as_ref()
            .map(|d| d.keys().map(|k| k.as_str()).collect())
            .unwrap_or_default();
        validate_contract(
            &self.inner,
            &self.script_name,
            if input_keys.is_empty() {
                None
            } else {
                Some(&input_keys)
            },
        )?;
        let url = format!(
            "{}/run?channel={}",
            self.script_url(),
            urlencoding::encode(&self.channel)
        );
        let triggered_by = self.triggered_by.unwrap_or_else(|| self.inner.name.clone());
        self.client
            .post(
                &url,
                &RunRequest {
                    inputs: self.inputs,
                    triggered_by: Some(triggered_by),
                    breakpoint_lines: self.breakpoint_lines,
                },
            )
            .await
    }

    pub async fn execute_and_await(
        self,
        timeout_ms: Option<u64>,
    ) -> Result<(String, ExecutionOutput)> {
        let execs = ExecutionsClient {
            inner: Arc::clone(&self.inner),
        };
        let run = self.execute().await?;
        let eid = run.execution_id.clone();
        let output = execs.await_execution(&eid, timeout_ms, None).await?;
        Ok((eid, output))
    }
}

// ── ListExecutionsBuilder ───────────────────────────────────────────────────

#[derive(Debug, Clone)]
#[must_use = "a builder does nothing until .fetch() is called"]
pub struct ListExecutionsBuilder {
    client: AkribesClient,
    script_url: String,
    status: Option<String>,
    channel: Option<String>,
    limit: Option<i64>,
    offset: Option<i64>,
}

impl ListExecutionsBuilder {
    pub fn status(mut self, status: impl Into<String>) -> Self {
        self.status = Some(status.into());
        self
    }

    pub fn channel(mut self, channel: impl Into<String>) -> Self {
        self.channel = Some(channel.into());
        self
    }

    pub fn limit(mut self, limit: i64) -> Self {
        self.limit = Some(limit);
        self
    }

    pub fn offset(mut self, offset: i64) -> Self {
        self.offset = Some(offset);
        self
    }

    pub async fn fetch(self) -> Result<Vec<ExecutionStatus>> {
        #[derive(serde::Serialize)]
        struct Q<'a> {
            #[serde(skip_serializing_if = "Option::is_none")]
            status: Option<&'a str>,
            #[serde(skip_serializing_if = "Option::is_none")]
            channel: Option<&'a str>,
            #[serde(skip_serializing_if = "Option::is_none")]
            limit: Option<i64>,
            #[serde(skip_serializing_if = "Option::is_none")]
            offset: Option<i64>,
        }
        let base = format!("{}/executions", self.script_url);
        let url = AkribesClient::url_with_query(
            &base,
            &Q {
                status: self.status.as_deref(),
                channel: self.channel.as_deref(),
                limit: self.limit,
                offset: self.offset,
            },
        );
        let res = self.client.send(self.client.inner.http.get(&url)).await?;
        if res.status() == reqwest::StatusCode::NOT_FOUND {
            return Ok(vec![]);
        }
        crate::client::decode_json(res).await
    }
}