Skip to main content

trace_weft/
lib.rs

1pub mod builder;
2pub mod capture;
3pub mod context;
4pub mod eval;
5pub mod events;
6pub mod hitl;
7pub mod replay;
8
9pub use builder::{
10    SpanBuilder, agent as build_agent, llm_call as build_llm_call, tool as build_tool,
11};
12pub use capture::{CaptureConfig, capture_enabled, capture_json, init_capture};
13pub use context::{SpanContext, current_span_context, scope_current};
14pub use events::{EventBuilder, event};
15
16pub use hitl::{HitlResponse, get_pending_approvals, register_approval, resolve_approval};
17pub use replay::{ReplayConfig, init_replay};
18
19pub use trace_weft_core::*;
20
21pub use trace_weft_macros::{agent, llm_call, tool};
22pub use trace_weft_recorder::{LocalConfig, LocalRecorder, NullStore, TraceStore};
23
24// Re-export uuid and serde_json so the macros can reference them through the
25// facade without the consumer depending on either crate directly.
26pub use serde_json;
27pub use uuid;
28
29use std::sync::Arc;
30use tokio::sync::OnceCell;
31
32static RECORDER: OnceCell<Arc<dyn TraceStore>> = OnceCell::const_new();
33
34pub async fn init_local(config: LocalConfig) -> anyhow::Result<()> {
35    let policy = config.capture_content;
36    let blob_dir = config.blob_dir.clone();
37
38    let recorder = LocalRecorder::new(config).await?;
39    init_custom(Arc::new(recorder))?;
40
41    init_capture(CaptureConfig {
42        policy,
43        blobs: Arc::new(capture::FsBlobStore::new(blob_dir)),
44        redactor: Arc::new(trace_weft_core::redactor::RegexRedactor::default()),
45        storage_backend: "local_fs".to_string(),
46    })
47}
48
49pub fn init_custom(store: Arc<dyn TraceStore>) -> anyhow::Result<()> {
50    RECORDER
51        .set(store)
52        .map_err(|_| anyhow::anyhow!("Already initialized"))?;
53
54    // Auto-load replay config from environment if present
55    if let Some(replay_config) = ReplayConfig::load_from_env() {
56        init_replay(replay_config);
57    }
58
59    Ok(())
60}
61
62pub async fn record_span(span: SpanRecord) {
63    if let Some(recorder) = RECORDER.get() {
64        let _ = recorder.record_span(span).await;
65    }
66}
67
68pub async fn record_event(event: EventRecord) {
69    if let Some(recorder) = RECORDER.get() {
70        let _ = recorder.record_event(event).await;
71    }
72}