use crate::error::Result;
use crate::models::run::Run;
use async_trait::async_trait;
#[async_trait]
pub trait TracingStrategy: Send + Sync {
async fn trace_start(&self, run: &Run) -> Result<()>;
async fn trace_end(&self, run: &Run) -> Result<()>;
async fn trace_error(&self, run: &Run, error: &str) -> Result<()>;
}
pub struct AsyncTracingStrategy {
}
impl AsyncTracingStrategy {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl TracingStrategy for AsyncTracingStrategy {
async fn trace_start(&self, run: &Run) -> Result<()> {
use crate::client::LangSmithClient;
let client = LangSmithClient::new()?;
client.post_run(run).await
}
async fn trace_end(&self, run: &Run) -> Result<()> {
use crate::client::LangSmithClient;
use crate::models::run::RunUpdate;
let client = LangSmithClient::new()?;
let updates = RunUpdate::from(run);
client.patch_run(run.id, &updates).await
}
async fn trace_error(&self, run: &Run, error: &str) -> Result<()> {
use crate::client::LangSmithClient;
use crate::models::run::RunUpdate;
let client = LangSmithClient::new()?;
let mut updates = RunUpdate::from(run);
updates.error = Some(error.to_string());
client.patch_run(run.id, &updates).await
}
}
pub struct SyncTracingStrategy {
}
impl SyncTracingStrategy {
pub fn new() -> Self {
Self {}
}
}
#[async_trait]
impl TracingStrategy for SyncTracingStrategy {
async fn trace_start(&self, run: &Run) -> Result<()> {
use crate::client::LangSmithClient;
let rt = tokio::runtime::Runtime::new().unwrap();
let client = LangSmithClient::new()?;
rt.block_on(client.post_run(run))
}
async fn trace_end(&self, run: &Run) -> Result<()> {
use crate::client::LangSmithClient;
use crate::models::run::RunUpdate;
let rt = tokio::runtime::Runtime::new().unwrap();
let client = LangSmithClient::new()?;
let updates = RunUpdate::from(run);
rt.block_on(client.patch_run(run.id, &updates))
}
async fn trace_error(&self, run: &Run, error: &str) -> Result<()> {
use crate::client::LangSmithClient;
use crate::models::run::RunUpdate;
let rt = tokio::runtime::Runtime::new().unwrap();
let client = LangSmithClient::new()?;
let mut updates = RunUpdate::from(run);
updates.error = Some(error.to_string());
rt.block_on(client.patch_run(run.id, &updates))
}
}