use adk_core::{AdkError, ErrorCategory, ErrorComponent, Part, Result};
use async_trait::async_trait;
pub const USER_SCOPE_SESSION_ID: &str = "user";
pub(crate) fn check_session_id(session_id: &str) -> Result<()> {
if session_id == USER_SCOPE_SESSION_ID {
return Err(AdkError::new(
ErrorComponent::Artifact,
ErrorCategory::InvalidInput,
"artifact.reserved_session_id",
format!(
"invalid artifact session id '{USER_SCOPE_SESSION_ID}': it names the scope that \
`user:` artifacts share across sessions; use another session id"
),
));
}
Ok(())
}
pub(crate) fn artifact_not_found(file_name: &str, version: Option<i64>) -> AdkError {
let message = match version {
Some(version) => format!(
"artifact '{file_name}' has no version {version}; list its versions to see which exist"
),
None => format!("artifact '{file_name}' was not found; save it before loading it"),
};
AdkError::not_found(ErrorComponent::Artifact, "artifact.not_found", message)
}
pub(crate) fn invalid_file_name(file_name: &str, rule: &str) -> AdkError {
AdkError::new(
ErrorComponent::Artifact,
ErrorCategory::InvalidInput,
"artifact.invalid_name",
format!("invalid artifact file name '{file_name}': {rule}"),
)
}
pub(crate) fn invalid_identifier(field: &str, value: &str, rule: &str) -> AdkError {
AdkError::new(
ErrorComponent::Artifact,
ErrorCategory::InvalidInput,
"artifact.invalid_identifier",
format!("invalid artifact {field} '{value}': {rule}"),
)
}
pub(crate) fn version_exists(file_name: &str, version: i64) -> AdkError {
AdkError::new(
ErrorComponent::Artifact,
ErrorCategory::Conflict,
"artifact.version_exists",
format!(
"artifact '{file_name}' already has version {version}; versions are never \
replaced, so save without a version to append a new one"
),
)
}
#[derive(Debug, Clone)]
pub struct SaveRequest {
pub app_name: String,
pub user_id: String,
pub session_id: String,
pub file_name: String,
pub part: Part,
pub version: Option<i64>,
}
#[derive(Debug, Clone)]
pub struct SaveResponse {
pub version: i64,
}
#[derive(Debug, Clone)]
pub struct LoadRequest {
pub app_name: String,
pub user_id: String,
pub session_id: String,
pub file_name: String,
pub version: Option<i64>,
}
#[derive(Debug, Clone)]
pub struct LoadResponse {
pub part: Part,
}
#[derive(Debug, Clone)]
pub struct DeleteRequest {
pub app_name: String,
pub user_id: String,
pub session_id: String,
pub file_name: String,
pub version: Option<i64>,
}
#[derive(Debug, Clone)]
pub struct ListRequest {
pub app_name: String,
pub user_id: String,
pub session_id: String,
}
#[derive(Debug, Clone)]
pub struct ListResponse {
pub file_names: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct VersionsRequest {
pub app_name: String,
pub user_id: String,
pub session_id: String,
pub file_name: String,
}
#[derive(Debug, Clone)]
pub struct VersionsResponse {
pub versions: Vec<i64>,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct UserArtifact {
pub session_id: Option<String>,
pub file_name: String,
pub version: i64,
pub mime_type: Option<String>,
pub size: u64,
}
pub(crate) fn part_summary(part: &Part) -> (Option<String>, u64) {
match part {
Part::Text { text } => (Some("text/plain".to_string()), text.len() as u64),
Part::InlineData { mime_type, data, .. } => (Some(mime_type.clone()), data.len() as u64),
Part::FileData { mime_type, .. } => (Some(mime_type.clone()), 0),
Part::Thinking { .. }
| Part::FunctionCall { .. }
| Part::FunctionResponse { .. }
| Part::ServerToolCall { .. }
| Part::ServerToolResponse { .. }
| Part::EmbeddedResource { .. } => {
(None, serde_json::to_vec(part).map_or(0, |json| json.len() as u64))
}
}
}
#[async_trait]
pub trait ArtifactService: Send + Sync {
async fn save(&self, req: SaveRequest) -> Result<SaveResponse>;
async fn load(&self, req: LoadRequest) -> Result<LoadResponse>;
async fn delete(&self, req: DeleteRequest) -> Result<()>;
async fn list(&self, req: ListRequest) -> Result<ListResponse>;
async fn versions(&self, req: VersionsRequest) -> Result<VersionsResponse>;
async fn list_user(&self, app_name: &str, user_id: &str) -> Result<Vec<UserArtifact>> {
let _ = (app_name, user_id);
Err(adk_core::subject::unsupported_error("artifacts", "list"))
}
async fn delete_user(&self, app_name: &str, user_id: &str) -> Result<u64> {
let _ = (app_name, user_id);
Err(adk_core::subject::unsupported_error("artifacts", "delete"))
}
async fn health_check(&self) -> Result<()> {
Ok(())
}
}