use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use validator::Validate;
use super::create::{BackgroundColor, EmbeddingId, KnowledgeIcon};
use crate::client::ZaiClient;
#[derive(Debug, Clone, Default, Serialize, Deserialize, Validate)]
pub struct UpdateKnowledgeBody {
#[serde(skip_serializing_if = "Option::is_none")]
pub embedding_id: Option<EmbeddingId>,
#[serde(skip_serializing_if = "Option::is_none")]
#[validate(length(min = 1))]
pub name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub background: Option<BackgroundColor>,
#[serde(skip_serializing_if = "Option::is_none")]
pub icon: Option<KnowledgeIcon>,
#[serde(skip_serializing_if = "Option::is_none")]
pub callback_url: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub callback_header: Option<HashMap<String, String>>,
}
impl UpdateKnowledgeBody {
fn is_empty(&self) -> bool {
self.embedding_id.is_none()
&& self.name.is_none()
&& self.description.is_none()
&& self.background.is_none()
&& self.icon.is_none()
&& self.callback_url.is_none()
&& self.callback_header.is_none()
}
}
pub struct KnowledgeUpdateRequest {
id: String,
body: UpdateKnowledgeBody,
}
impl KnowledgeUpdateRequest {
pub fn new(id: impl Into<String>) -> Self {
Self {
id: id.into(),
body: UpdateKnowledgeBody::default(),
}
}
pub fn with_embedding_id(mut self, id: EmbeddingId) -> Self {
self.body.embedding_id = Some(id);
self
}
pub fn with_name(mut self, name: impl Into<String>) -> Self {
self.body.name = Some(name.into());
self
}
pub fn with_description(mut self, desc: impl Into<String>) -> Self {
self.body.description = Some(desc.into());
self
}
pub fn with_background(mut self, bg: BackgroundColor) -> Self {
self.body.background = Some(bg);
self
}
pub fn with_icon(mut self, icon: KnowledgeIcon) -> Self {
self.body.icon = Some(icon);
self
}
pub fn with_callback_url(mut self, url: impl Into<String>) -> Self {
self.body.callback_url = Some(url.into());
self
}
pub fn with_callback_header(mut self, headers: HashMap<String, String>) -> Self {
self.body.callback_header = Some(headers);
self
}
pub async fn send_via(&self, client: &ZaiClient) -> crate::ZaiResult<KnowledgeUpdateResponse> {
if self.body.is_empty() {
return Err(crate::client::error::ZaiError::ApiError {
code: crate::client::error::codes::SDK_VALIDATION,
message: "update body is empty; set at least one field".to_string(),
});
}
self.body.validate()?;
let route = crate::client::routes::KNOWLEDGE_UPDATE;
let url = client.endpoints().resolve_route(route, &[&self.id])?;
client
.send_json::<_, KnowledgeUpdateResponse>(route.method(), url, &self.body)
.await
}
}
#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
pub struct KnowledgeUpdateResponse {
#[serde(skip_serializing_if = "Option::is_none")]
pub code: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub timestamp: Option<u64>,
}