use std::sync::Arc;
use agent_framework_core::error::{Error, Result};
use agent_framework_core::types::Content;
use agent_framework_core::workflow::{compare_checkpoints, CheckpointStorage, WorkflowCheckpoint};
use async_trait::async_trait;
use serde_json::{Map, Value};
use crate::config::FoundryHostConfig;
use crate::scope::{store_name, FoundryRequestContext, FoundryRequestScope};
use crate::storage::{FoundryStateStore, FoundryStorage, StorageError, StoreOptions};
pub fn store_scope(
config: &FoundryHostConfig,
context: &FoundryRequestContext,
) -> Result<Option<FoundryRequestScope>> {
if !config.is_hosted {
return Ok(None);
}
FoundryRequestScope::from_context(config, context, None).map(Some)
}
pub(crate) async fn open(
storage: &FoundryStorage,
name: &str,
context: &FoundryRequestContext,
) -> Result<FoundryStateStore> {
Ok(storage
.get_or_create(
name,
StoreOptions {
user_id: context.user_id.clone(),
call_id: context.call_id.clone(),
..Default::default()
},
)
.await?)
}
pub(crate) fn object(value: Value, what: &str) -> Result<Map<String, Value>> {
match value {
Value::Object(map) => Ok(map),
_ => Err(Error::Serialization(format!(
"{what} did not serialize to a JSON object"
))),
}
}
pub struct FoundryCheckpointStore {
storage: FoundryStorage,
name: String,
context: FoundryRequestContext,
}
impl std::fmt::Debug for FoundryCheckpointStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FoundryCheckpointStore")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl FoundryCheckpointStore {
pub const DEFAULT_ROOT: &'static str = "checkpoints";
pub fn new(
storage: FoundryStorage,
context: FoundryRequestContext,
context_id: &str,
scope: Option<&FoundryRequestScope>,
) -> Result<Self> {
if context_id.is_empty() {
return Err(Error::Configuration(
"context_id must be provided to initialize a FoundryCheckpointStore.".into(),
));
}
Ok(Self {
storage,
name: store_name(Self::DEFAULT_ROOT, scope, Some(context_id)),
context,
})
}
pub fn store_name(&self) -> &str {
&self.name
}
}
#[async_trait]
impl CheckpointStorage for FoundryCheckpointStore {
async fn save(&self, checkpoint: WorkflowCheckpoint) -> Result<String> {
let id = checkpoint.checkpoint_id.clone();
let value = object(serde_json::to_value(&checkpoint)?, "checkpoint")?;
let store = open(&self.storage, &self.name, &self.context).await?;
store.set_item(&id, value, None).await?;
Ok(id)
}
async fn load(&self, checkpoint_id: &str) -> Result<Option<WorkflowCheckpoint>> {
let store = open(&self.storage, &self.name, &self.context).await?;
match store.get_item(checkpoint_id).await? {
None => Ok(None),
Some(item) => Ok(Some(serde_json::from_value(Value::Object(item.value))?)),
}
}
async fn list(&self, workflow_id: Option<&str>) -> Result<Vec<WorkflowCheckpoint>> {
let store = open(&self.storage, &self.name, &self.context).await?;
let mut out = Vec::new();
for key in store.list_keys().await? {
if let Some(item) = store.get_item(&key).await? {
let checkpoint: WorkflowCheckpoint =
serde_json::from_value(Value::Object(item.value))?;
if workflow_id.is_none_or(|w| checkpoint.workflow_id == w) {
out.push(checkpoint);
}
}
}
out.sort_by(compare_checkpoints);
Ok(out)
}
async fn delete(&self, checkpoint_id: &str) -> Result<bool> {
let store = open(&self.storage, &self.name, &self.context).await?;
match store.delete_item(checkpoint_id, None).await {
Ok(()) => Ok(true),
Err(StorageError::NotFound(_)) => Ok(false),
Err(e) => Err(e.into()),
}
}
}
#[async_trait]
pub trait FunctionApprovalStore: Send + Sync {
async fn save_approval_request(&self, id: &str, request: &Content) -> Result<()>;
async fn load_approval_request(&self, id: &str) -> Result<Content>;
}
#[derive(Debug, Default)]
pub struct InMemoryFunctionApprovalStore {
requests: std::sync::Mutex<std::collections::HashMap<String, Content>>,
}
#[async_trait]
impl FunctionApprovalStore for InMemoryFunctionApprovalStore {
async fn save_approval_request(&self, id: &str, request: &Content) -> Result<()> {
let mut requests = self.requests.lock().unwrap();
if requests.contains_key(id) {
return Err(agent_framework_core::error::Error::Other(format!(
"Approval request with ID '{id}' already exists."
)));
}
requests.insert(id.to_string(), request.clone());
Ok(())
}
async fn load_approval_request(&self, id: &str) -> Result<Content> {
self.requests
.lock()
.unwrap()
.get(id)
.cloned()
.ok_or_else(|| {
agent_framework_core::error::Error::Other(format!(
"Approval request with ID '{id}' does not exist."
))
})
}
}
pub struct FoundryFunctionApprovalStore {
storage: FoundryStorage,
name: String,
context: FoundryRequestContext,
}
impl std::fmt::Debug for FoundryFunctionApprovalStore {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FoundryFunctionApprovalStore")
.field("name", &self.name)
.finish_non_exhaustive()
}
}
impl FoundryFunctionApprovalStore {
pub const DEFAULT_ROOT: &'static str = "function_approvals";
pub fn new(
storage: FoundryStorage,
context: FoundryRequestContext,
scope: Option<&FoundryRequestScope>,
) -> Self {
Self {
storage,
name: store_name(Self::DEFAULT_ROOT, scope, None),
context,
}
}
}
#[async_trait]
impl FunctionApprovalStore for FoundryFunctionApprovalStore {
async fn save_approval_request(&self, id: &str, request: &Content) -> Result<()> {
let value = object(serde_json::to_value(request)?, "approval request")?;
let store = open(&self.storage, &self.name, &self.context).await?;
match store.create_item(id, value).await {
Ok(_) => Ok(()),
Err(StorageError::Conflict(_)) => Err(Error::Configuration(format!(
"Approval request with ID '{id}' already exists."
))),
Err(e) => Err(e.into()),
}
}
async fn load_approval_request(&self, id: &str) -> Result<Content> {
let store = open(&self.storage, &self.name, &self.context).await?;
match store.get_item(id).await? {
Some(item) => Ok(serde_json::from_value(Value::Object(item.value))?),
None => Err(Error::Configuration(format!(
"Approval request with ID '{id}' does not exist."
))),
}
}
}
pub trait FunctionApprovalStoreProvider: Send + Sync {
fn approval_store(
&self,
config: &FoundryHostConfig,
context: &FoundryRequestContext,
) -> Result<Arc<dyn FunctionApprovalStore>>;
}
impl FunctionApprovalStoreProvider for FoundryStorage {
fn approval_store(
&self,
config: &FoundryHostConfig,
context: &FoundryRequestContext,
) -> Result<Arc<dyn FunctionApprovalStore>> {
let scope = store_scope(config, context)?;
Ok(Arc::new(FoundryFunctionApprovalStore::new(
self.clone(),
context.clone(),
scope.as_ref(),
)))
}
}
pub trait CheckpointStoreProvider: Send + Sync {
fn checkpoint_store(
&self,
config: &FoundryHostConfig,
context: &FoundryRequestContext,
context_id: &str,
) -> Result<Arc<dyn CheckpointStorage>>;
}
impl CheckpointStoreProvider for FoundryStorage {
fn checkpoint_store(
&self,
config: &FoundryHostConfig,
context: &FoundryRequestContext,
context_id: &str,
) -> Result<Arc<dyn CheckpointStorage>> {
let scope = store_scope(config, context)?;
Ok(Arc::new(FoundryCheckpointStore::new(
self.clone(),
context.clone(),
context_id,
scope.as_ref(),
)?))
}
}