Skip to main content

zai_rs/knowledge/
document_reembedding.rs

1use std::collections::BTreeMap;
2
3use serde::{Deserialize, Serialize};
4use validator::Validate;
5
6use crate::ZaiResult;
7use crate::client::ZaiClient;
8
9/// Request body for re-embedding a document
10#[derive(Debug, Clone, Serialize, Deserialize, Validate, Default)]
11pub struct DocumentReembeddingBody {
12    /// Optional callback URL that will be called when re-embedding completes
13    #[serde(skip_serializing_if = "Option::is_none")]
14    #[validate(url)]
15    pub callback_url: Option<String>,
16    /// Optional callback headers key-value pairs
17    #[serde(skip_serializing_if = "Option::is_none")]
18    pub callback_header: Option<BTreeMap<String, String>>,
19}
20
21/// Re-embedding request (POST /llm-application/open/document/embedding/{id})
22///
23/// Credentials and transport live on the [`ZaiClient`], passed to
24/// [`send_via`](Self::send_via).
25pub struct DocumentReembeddingRequest {
26    document_id: String,
27    body: DocumentReembeddingBody,
28}
29
30impl DocumentReembeddingRequest {
31    /// Create a new request for the specified document id
32    pub fn new(document_id: impl Into<String>) -> Self {
33        Self {
34            document_id: document_id.into(),
35            body: DocumentReembeddingBody::default(),
36        }
37    }
38
39    /// Set callback URL
40    pub fn with_callback_url(mut self, url: impl Into<String>) -> Self {
41        self.body.callback_url = Some(url.into());
42        self
43    }
44
45    /// Set callback headers
46    pub fn with_callback_header(mut self, hdr: BTreeMap<String, String>) -> Self {
47        self.body.callback_header = Some(hdr);
48        self
49    }
50
51    /// Send the POST request via a [`ZaiClient`] and parse the typed response.
52    pub async fn send_via(&self, client: &ZaiClient) -> ZaiResult<DocumentReembeddingResponse> {
53        self.body.validate()?;
54        let route = crate::client::routes::DOCUMENTS_REEMBED;
55        let url = client
56            .endpoints()
57            .resolve_route(route, &[&self.document_id])?;
58        client
59            .send_json::<_, DocumentReembeddingResponse>(route.method(), url, &self.body)
60            .await
61    }
62}
63
64/// Simple response envelope: { code, message, timestamp }
65#[derive(Debug, Clone, Serialize, Deserialize, Validate)]
66pub struct DocumentReembeddingResponse {
67    /// Business status code.
68    #[serde(skip_serializing_if = "Option::is_none")]
69    pub code: Option<i64>,
70    /// Human-readable message.
71    #[serde(skip_serializing_if = "Option::is_none")]
72    pub message: Option<String>,
73    /// Server timestamp.
74    #[serde(skip_serializing_if = "Option::is_none")]
75    pub timestamp: Option<u64>,
76}