tianshu 0.1.0

Async Rust workflow engine — define workflows as trait implementations and run them on a pluggable scheduler
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
// Copyright 2026 Desicool
//
// SPDX-License-Identifier: Apache-2.0

use anyhow::Result;
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use serde_json::Value as JsonValue;
use std::sync::Arc;
use std::time::Instant;

use crate::llm::{LlmProvider, LlmRequest, LlmResponse, LlmUsage};

// ── Data types ────────────────────────────────────────────────────────────────

/// Record of a single step execution.
///
/// Emitted by `WorkflowContext::step()` via `Observer::on_step()`.
/// `cached: true` means the step was restored from a prior checkpoint —
/// useful for RLHF filtering (keep only `cached: false` for original executions).
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StepRecord {
    pub case_key: String,
    pub workflow_code: String,
    pub step_name: String,
    /// Snapshot of `case.resource_data` immediately before the step ran.
    pub input_resource_data: Option<JsonValue>,
    /// Serialized step result. `None` if the step returned an error.
    pub output: Option<JsonValue>,
    pub duration_ms: u64,
    pub timestamp: DateTime<Utc>,
    /// `true` if the result was restored from a checkpoint (replay), `false` if freshly executed.
    pub cached: bool,
    pub error: Option<String>,
}

/// Record emitted when a workflow finishes via `ctx.finish()`.
///
/// `steps` contains all steps that ran (or replayed) in the current tick.
/// Because of the coroutine-like replay model, the final tick always
/// re-runs every step — so `steps` is the complete list for the workflow.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct WorkflowRecord {
    pub case_key: String,
    pub session_id: String,
    pub workflow_code: String,
    /// `case.resource_data` captured when the WorkflowContext was created.
    pub input_resource_data: Option<JsonValue>,
    /// `case.resource_data` at the moment `finish()` was called.
    pub output_resource_data: Option<JsonValue>,
    pub finished_type: Option<String>,
    pub finished_description: Option<String>,
    /// All steps from this tick (includes replayed cached steps).
    pub steps: Vec<StepRecord>,
    pub total_duration_ms: u64,
    pub started_at: DateTime<Utc>,
    pub finished_at: DateTime<Utc>,
}

/// Record of a single tool call execution.
///
/// Emitted by `run_tool_loop()` via `Observer::on_tool_call()`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolCallRecord {
    pub case_key: String,
    pub step_name: Option<String>,
    pub tool_name: String,
    pub call_id: String,
    pub input: JsonValue,
    pub output: Option<String>,
    pub is_error: bool,
    pub duration_ms: u64,
    pub timestamp: DateTime<Utc>,
}

/// Record of a retry attempt.
///
/// Emitted via `Observer::on_retry()` when the retry loop retries after an error.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RetryRecord {
    pub case_key: String,
    pub step_name: Option<String>,
    pub attempt: u32,
    pub error_class: String,
    pub error_message: String,
    pub timestamp: DateTime<Utc>,
}

/// Record of a single LLM call.
///
/// Emitted via `observe_llm_call()` or `ObservedLlmProvider`.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LlmCallRecord {
    pub case_key: String,
    pub step_name: Option<String>,
    pub model: String,
    pub request: LlmRequest,
    /// Response text. `None` if the call errored.
    pub response_content: Option<String>,
    pub usage: Option<LlmUsage>,
    pub duration_ms: u64,
    pub timestamp: DateTime<Utc>,
    pub error: Option<String>,
}

// ── Observer trait ────────────────────────────────────────────────────────────

/// Pluggable sink for workflow observability events.
///
/// Implement this trait to route events to any backend:
/// in-memory buffers, JSONL files, remote tracing services, etc.
///
/// All methods have default no-op implementations so you only override
/// what you need.
///
/// # Example
///
/// ```rust,ignore
/// struct PrintObserver;
///
/// #[async_trait]
/// impl Observer for PrintObserver {
///     async fn on_step(&self, record: &StepRecord) {
///         println!("step: {} -> {:?}", record.step_name, record.output);
///     }
///     async fn on_workflow_complete(&self, record: &WorkflowRecord) {
///         println!("workflow done: {}", record.case_key);
///     }
/// }
/// ```
#[async_trait]
pub trait Observer: Send + Sync {
    /// Called after each step execution (cached or fresh).
    async fn on_step(&self, record: &StepRecord) {
        let _ = record;
    }

    /// Called when a workflow calls `ctx.finish()`.
    async fn on_workflow_complete(&self, record: &WorkflowRecord) {
        let _ = record;
    }

    /// Called after each LLM call tracked via `observe_llm_call` or `ObservedLlmProvider`.
    async fn on_llm_call(&self, record: &LlmCallRecord) {
        let _ = record;
    }

    /// Called after each tool call execution in a tool-use loop.
    async fn on_tool_call(&self, record: &ToolCallRecord) {
        let _ = record;
    }

    /// Called when a retry occurs during error recovery.
    async fn on_retry(&self, record: &RetryRecord) {
        let _ = record;
    }

    /// Flush any buffered writes. Called by `WorkflowContext::finish()`.
    /// Default is a no-op; buffered implementations (e.g. JSONL) should override.
    async fn flush(&self) {}
}

// ── Free function ─────────────────────────────────────────────────────────────

/// Execute an LLM call and record it via `observer`.
///
/// Drop-in replacement for `provider.complete(request)` when you have
/// an observer and want to capture timing + input/output.
///
/// # Example
///
/// ```rust,ignore
/// let response = observe_llm_call(
///     observer.as_ref(),
///     &provider,
///     request,
///     &ctx.case.case_key,
///     Some("my_step"),
/// ).await?;
/// ```
pub async fn observe_llm_call(
    observer: &dyn Observer,
    provider: &dyn LlmProvider,
    request: LlmRequest,
    case_key: &str,
    step_name: Option<&str>,
) -> Result<LlmResponse> {
    let start = Instant::now();
    let timestamp = Utc::now();
    let result = provider.complete(request.clone()).await;
    let duration_ms = start.elapsed().as_millis() as u64;

    let record = match &result {
        Ok(resp) => LlmCallRecord {
            case_key: case_key.to_string(),
            step_name: step_name.map(str::to_string),
            model: request.model.clone(),
            request,
            response_content: Some(resp.content.clone()),
            usage: Some(resp.usage.clone()),
            duration_ms,
            timestamp,
            error: None,
        },
        Err(e) => LlmCallRecord {
            case_key: case_key.to_string(),
            step_name: step_name.map(str::to_string),
            model: request.model.clone(),
            request,
            response_content: None,
            usage: None,
            duration_ms,
            timestamp,
            error: Some(e.to_string()),
        },
    };

    observer.on_llm_call(&record).await;
    result
}

// ── ObservedLlmProvider ───────────────────────────────────────────────────────

/// A `LlmProvider` wrapper that records every call via an `Observer`.
///
/// Useful for transparent instrumentation — wrap your provider once at
/// construction time and every call is automatically observed.
///
/// ```rust,ignore
/// let provider = ObservedLlmProvider::new(
///     Arc::new(OpenAiProvider::new(key, model)),
///     observer.clone(),
///     ctx.case.case_key.clone(),
/// );
/// // Now use `provider` as a normal LlmProvider inside the workflow.
/// let response = provider.complete(request).await?;
/// ```
pub struct ObservedLlmProvider {
    inner: Arc<dyn LlmProvider>,
    observer: Arc<dyn Observer>,
    case_key: String,
    step_name: Option<String>,
}

impl ObservedLlmProvider {
    pub fn new(
        inner: Arc<dyn LlmProvider>,
        observer: Arc<dyn Observer>,
        case_key: impl Into<String>,
    ) -> Self {
        Self {
            inner,
            observer,
            case_key: case_key.into(),
            step_name: None,
        }
    }

    /// Associate this provider with a specific step name for richer records.
    pub fn with_step(mut self, step_name: impl Into<String>) -> Self {
        self.step_name = Some(step_name.into());
        self
    }
}

#[async_trait]
impl LlmProvider for ObservedLlmProvider {
    async fn complete(&self, request: LlmRequest) -> Result<LlmResponse> {
        observe_llm_call(
            self.observer.as_ref(),
            self.inner.as_ref(),
            request,
            &self.case_key,
            self.step_name.as_deref(),
        )
        .await
    }
}

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::*;
    use async_trait::async_trait;
    use serde_json::json;
    use std::sync::{Arc, Mutex};

    // ── Helpers ──────────────────────────────────────────────────────────────

    struct CollectingObserver {
        steps: Mutex<Vec<StepRecord>>,
        workflows: Mutex<Vec<WorkflowRecord>>,
        llm_calls: Mutex<Vec<LlmCallRecord>>,
    }

    impl CollectingObserver {
        fn new() -> Arc<Self> {
            Arc::new(Self {
                steps: Mutex::new(vec![]),
                workflows: Mutex::new(vec![]),
                llm_calls: Mutex::new(vec![]),
            })
        }
    }

    #[async_trait]
    impl Observer for CollectingObserver {
        async fn on_step(&self, record: &StepRecord) {
            self.steps.lock().unwrap().push(record.clone());
        }
        async fn on_workflow_complete(&self, record: &WorkflowRecord) {
            self.workflows.lock().unwrap().push(record.clone());
        }
        async fn on_llm_call(&self, record: &LlmCallRecord) {
            self.llm_calls.lock().unwrap().push(record.clone());
        }
    }

    struct OkLlmProvider;
    #[async_trait]
    impl LlmProvider for OkLlmProvider {
        async fn complete(&self, request: LlmRequest) -> Result<LlmResponse> {
            Ok(LlmResponse {
                content: format!("response to {}", request.model),
                usage: LlmUsage {
                    prompt_tokens: 10,
                    completion_tokens: 5,
                },
                finish_reason: "stop".into(),

                tool_calls: None,
            })
        }
    }

    struct ErrLlmProvider;
    #[async_trait]
    impl LlmProvider for ErrLlmProvider {
        async fn complete(&self, _request: LlmRequest) -> Result<LlmResponse> {
            Err(anyhow::anyhow!("provider unavailable"))
        }
    }

    fn make_request() -> LlmRequest {
        LlmRequest {
            model: "gpt-4".into(),
            system_prompt: Some("You are helpful".into()),
            messages: vec![crate::llm::LlmMessage {
                role: "user".into(),
                content: "Hello".into(),

                tool_calls: None,

                tool_call_id: None,
            }],
            temperature: Some(0.7),
            max_tokens: Some(100),

            tools: None,
        }
    }

    // ── Serialization roundtrip ──────────────────────────────────────────────

    #[test]
    fn step_record_serde_roundtrip() {
        let record = StepRecord {
            case_key: "case_1".into(),
            workflow_code: "approval".into(),
            step_name: "fetch_data".into(),
            input_resource_data: Some(json!({"doc": "test"})),
            output: Some(json!("result")),
            duration_ms: 42,
            timestamp: Utc::now(),
            cached: false,
            error: None,
        };
        let json = serde_json::to_string(&record).unwrap();
        let back: StepRecord = serde_json::from_str(&json).unwrap();
        assert_eq!(back.case_key, "case_1");
        assert_eq!(back.step_name, "fetch_data");
        assert!(!back.cached);
    }

    #[test]
    fn workflow_record_serde_roundtrip() {
        let record = WorkflowRecord {
            case_key: "case_2".into(),
            session_id: "sess_1".into(),
            workflow_code: "approval".into(),
            input_resource_data: Some(json!({"doc_id": "abc"})),
            output_resource_data: Some(json!({"result": "approved"})),
            finished_type: Some("success".into()),
            finished_description: Some("approved".into()),
            steps: vec![],
            total_duration_ms: 500,
            started_at: Utc::now(),
            finished_at: Utc::now(),
        };
        let json = serde_json::to_string(&record).unwrap();
        let back: WorkflowRecord = serde_json::from_str(&json).unwrap();
        assert_eq!(back.case_key, "case_2");
        assert_eq!(back.finished_type.as_deref(), Some("success"));
    }

    #[test]
    fn llm_call_record_serde_roundtrip() {
        let record = LlmCallRecord {
            case_key: "case_3".into(),
            step_name: Some("classify".into()),
            model: "gpt-4".into(),
            request: make_request(),
            response_content: Some("classification result".into()),
            usage: Some(LlmUsage {
                prompt_tokens: 20,
                completion_tokens: 10,
            }),
            duration_ms: 300,
            timestamp: Utc::now(),
            error: None,
        };
        let json = serde_json::to_string(&record).unwrap();
        let back: LlmCallRecord = serde_json::from_str(&json).unwrap();
        assert_eq!(back.case_key, "case_3");
        assert_eq!(back.model, "gpt-4");
        assert!(back.error.is_none());
    }

    // ── observe_llm_call ─────────────────────────────────────────────────────

    #[tokio::test]
    async fn observe_llm_call_records_success() {
        let obs = CollectingObserver::new();
        let provider = OkLlmProvider;
        let request = make_request();

        let result = observe_llm_call(obs.as_ref(), &provider, request, "case_x", Some("step1"))
            .await
            .unwrap();

        assert_eq!(result.finish_reason, "stop");

        let calls = obs.llm_calls.lock().unwrap();
        assert_eq!(calls.len(), 1);
        let call = &calls[0];
        assert_eq!(call.case_key, "case_x");
        assert_eq!(call.step_name.as_deref(), Some("step1"));
        assert!(call.response_content.is_some());
        assert!(call.error.is_none());
        assert!(call.duration_ms < 1000); // sanity
    }

    #[tokio::test]
    async fn observe_llm_call_records_error() {
        let obs = CollectingObserver::new();
        let provider = ErrLlmProvider;

        let result =
            observe_llm_call(obs.as_ref(), &provider, make_request(), "case_y", None).await;
        assert!(result.is_err());

        let calls = obs.llm_calls.lock().unwrap();
        assert_eq!(calls.len(), 1);
        let call = &calls[0];
        assert!(call.response_content.is_none());
        assert!(call.usage.is_none());
        assert!(call.error.as_deref().unwrap().contains("unavailable"));
    }

    #[tokio::test]
    async fn observe_llm_call_no_step_name() {
        let obs = CollectingObserver::new();
        observe_llm_call(obs.as_ref(), &OkLlmProvider, make_request(), "case_z", None)
            .await
            .unwrap();
        let calls = obs.llm_calls.lock().unwrap();
        assert!(calls[0].step_name.is_none());
    }

    // ── ObservedLlmProvider ──────────────────────────────────────────────────

    #[tokio::test]
    async fn observed_llm_provider_records_call() {
        let obs = CollectingObserver::new();
        let provider = ObservedLlmProvider::new(Arc::new(OkLlmProvider), obs.clone(), "case_p");

        provider.complete(make_request()).await.unwrap();

        let calls = obs.llm_calls.lock().unwrap();
        assert_eq!(calls.len(), 1);
        assert_eq!(calls[0].case_key, "case_p");
        assert!(calls[0].step_name.is_none());
    }

    #[tokio::test]
    async fn observed_llm_provider_with_step() {
        let obs = CollectingObserver::new();
        let provider = ObservedLlmProvider::new(Arc::new(OkLlmProvider), obs.clone(), "case_q")
            .with_step("summarize");

        provider.complete(make_request()).await.unwrap();

        let calls = obs.llm_calls.lock().unwrap();
        assert_eq!(calls[0].step_name.as_deref(), Some("summarize"));
    }

    #[tokio::test]
    async fn observed_llm_provider_error_still_records() {
        let obs = CollectingObserver::new();
        let provider = ObservedLlmProvider::new(Arc::new(ErrLlmProvider), obs.clone(), "case_err");

        let result = provider.complete(make_request()).await;
        assert!(result.is_err());

        let calls = obs.llm_calls.lock().unwrap();
        assert_eq!(calls.len(), 1);
        assert!(calls[0].error.is_some());
    }

    // ── Default Observer methods ─────────────────────────────────────────────

    #[tokio::test]
    async fn default_observer_methods_are_no_ops() {
        struct NullObserver;
        #[async_trait]
        impl Observer for NullObserver {}

        let obs = NullObserver;
        let step = StepRecord {
            case_key: "k".into(),
            workflow_code: "wf".into(),
            step_name: "s".into(),
            input_resource_data: None,
            output: None,
            duration_ms: 0,
            timestamp: Utc::now(),
            cached: false,
            error: None,
        };
        obs.on_step(&step).await; // must not panic
        obs.flush().await;
    }
}