|
| 1 | +use crate::auth::AuthProvider; |
| 2 | +use crate::common::MemoryTraceSummarizeInput; |
| 3 | +use crate::common::MemoryTraceSummaryOutput; |
| 4 | +use crate::endpoint::session::EndpointSession; |
| 5 | +use crate::error::ApiError; |
| 6 | +use crate::provider::Provider; |
| 7 | +use codex_client::HttpTransport; |
| 8 | +use codex_client::RequestTelemetry; |
| 9 | +use http::HeaderMap; |
| 10 | +use http::Method; |
| 11 | +use serde::Deserialize; |
| 12 | +use serde_json::to_value; |
| 13 | +use std::sync::Arc; |
| 14 | + |
| 15 | +pub struct MemoriesClient<T: HttpTransport, A: AuthProvider> { |
| 16 | + session: EndpointSession<T, A>, |
| 17 | +} |
| 18 | + |
| 19 | +impl<T: HttpTransport, A: AuthProvider> MemoriesClient<T, A> { |
| 20 | + pub fn new(transport: T, provider: Provider, auth: A) -> Self { |
| 21 | + Self { |
| 22 | + session: EndpointSession::new(transport, provider, auth), |
| 23 | + } |
| 24 | + } |
| 25 | + |
| 26 | + pub fn with_telemetry(self, request: Option<Arc<dyn RequestTelemetry>>) -> Self { |
| 27 | + Self { |
| 28 | + session: self.session.with_request_telemetry(request), |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + fn path() -> &'static str { |
| 33 | + "memories/trace_summarize" |
| 34 | + } |
| 35 | + |
| 36 | + pub async fn trace_summarize( |
| 37 | + &self, |
| 38 | + body: serde_json::Value, |
| 39 | + extra_headers: HeaderMap, |
| 40 | + ) -> Result<Vec<MemoryTraceSummaryOutput>, ApiError> { |
| 41 | + let resp = self |
| 42 | + .session |
| 43 | + .execute(Method::POST, Self::path(), extra_headers, Some(body)) |
| 44 | + .await?; |
| 45 | + let parsed: TraceSummarizeResponse = |
| 46 | + serde_json::from_slice(&resp.body).map_err(|e| ApiError::Stream(e.to_string()))?; |
| 47 | + Ok(parsed.output) |
| 48 | + } |
| 49 | + |
| 50 | + pub async fn trace_summarize_input( |
| 51 | + &self, |
| 52 | + input: &MemoryTraceSummarizeInput, |
| 53 | + extra_headers: HeaderMap, |
| 54 | + ) -> Result<Vec<MemoryTraceSummaryOutput>, ApiError> { |
| 55 | + let body = to_value(input).map_err(|e| { |
| 56 | + ApiError::Stream(format!( |
| 57 | + "failed to encode memory trace summarize input: {e}" |
| 58 | + )) |
| 59 | + })?; |
| 60 | + self.trace_summarize(body, extra_headers).await |
| 61 | + } |
| 62 | +} |
| 63 | + |
| 64 | +#[derive(Debug, Deserialize)] |
| 65 | +struct TraceSummarizeResponse { |
| 66 | + output: Vec<MemoryTraceSummaryOutput>, |
| 67 | +} |
| 68 | + |
| 69 | +#[cfg(test)] |
| 70 | +mod tests { |
| 71 | + use super::*; |
| 72 | + use async_trait::async_trait; |
| 73 | + use codex_client::Request; |
| 74 | + use codex_client::Response; |
| 75 | + use codex_client::StreamResponse; |
| 76 | + use codex_client::TransportError; |
| 77 | + |
| 78 | + #[derive(Clone, Default)] |
| 79 | + struct DummyTransport; |
| 80 | + |
| 81 | + #[async_trait] |
| 82 | + impl HttpTransport for DummyTransport { |
| 83 | + async fn execute(&self, _req: Request) -> Result<Response, TransportError> { |
| 84 | + Err(TransportError::Build("execute should not run".to_string())) |
| 85 | + } |
| 86 | + |
| 87 | + async fn stream(&self, _req: Request) -> Result<StreamResponse, TransportError> { |
| 88 | + Err(TransportError::Build("stream should not run".to_string())) |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + #[derive(Clone, Default)] |
| 93 | + struct DummyAuth; |
| 94 | + |
| 95 | + impl AuthProvider for DummyAuth { |
| 96 | + fn bearer_token(&self) -> Option<String> { |
| 97 | + None |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + #[test] |
| 102 | + fn path_is_memories_trace_summarize() { |
| 103 | + assert_eq!( |
| 104 | + MemoriesClient::<DummyTransport, DummyAuth>::path(), |
| 105 | + "memories/trace_summarize" |
| 106 | + ); |
| 107 | + } |
| 108 | +} |
0 commit comments