mirror of
https://github.com/openai/codex.git
synced 2026-04-29 17:06:51 +00:00
feat: add phase 1 mem client (#10629)
Adding a client on top of https://github.com/openai/openai/pull/672176
This commit is contained in:
108
codex-rs/codex-api/src/endpoint/memories.rs
Normal file
108
codex-rs/codex-api/src/endpoint/memories.rs
Normal file
@@ -0,0 +1,108 @@
|
||||
use crate::auth::AuthProvider;
|
||||
use crate::common::MemoryTraceSummarizeInput;
|
||||
use crate::common::MemoryTraceSummaryOutput;
|
||||
use crate::endpoint::session::EndpointSession;
|
||||
use crate::error::ApiError;
|
||||
use crate::provider::Provider;
|
||||
use codex_client::HttpTransport;
|
||||
use codex_client::RequestTelemetry;
|
||||
use http::HeaderMap;
|
||||
use http::Method;
|
||||
use serde::Deserialize;
|
||||
use serde_json::to_value;
|
||||
use std::sync::Arc;
|
||||
|
||||
pub struct MemoriesClient<T: HttpTransport, A: AuthProvider> {
|
||||
session: EndpointSession<T, A>,
|
||||
}
|
||||
|
||||
impl<T: HttpTransport, A: AuthProvider> MemoriesClient<T, A> {
|
||||
pub fn new(transport: T, provider: Provider, auth: A) -> Self {
|
||||
Self {
|
||||
session: EndpointSession::new(transport, provider, auth),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn with_telemetry(self, request: Option<Arc<dyn RequestTelemetry>>) -> Self {
|
||||
Self {
|
||||
session: self.session.with_request_telemetry(request),
|
||||
}
|
||||
}
|
||||
|
||||
fn path() -> &'static str {
|
||||
"memories/trace_summarize"
|
||||
}
|
||||
|
||||
pub async fn trace_summarize(
|
||||
&self,
|
||||
body: serde_json::Value,
|
||||
extra_headers: HeaderMap,
|
||||
) -> Result<Vec<MemoryTraceSummaryOutput>, ApiError> {
|
||||
let resp = self
|
||||
.session
|
||||
.execute(Method::POST, Self::path(), extra_headers, Some(body))
|
||||
.await?;
|
||||
let parsed: TraceSummarizeResponse =
|
||||
serde_json::from_slice(&resp.body).map_err(|e| ApiError::Stream(e.to_string()))?;
|
||||
Ok(parsed.output)
|
||||
}
|
||||
|
||||
pub async fn trace_summarize_input(
|
||||
&self,
|
||||
input: &MemoryTraceSummarizeInput,
|
||||
extra_headers: HeaderMap,
|
||||
) -> Result<Vec<MemoryTraceSummaryOutput>, ApiError> {
|
||||
let body = to_value(input).map_err(|e| {
|
||||
ApiError::Stream(format!(
|
||||
"failed to encode memory trace summarize input: {e}"
|
||||
))
|
||||
})?;
|
||||
self.trace_summarize(body, extra_headers).await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize)]
|
||||
struct TraceSummarizeResponse {
|
||||
output: Vec<MemoryTraceSummaryOutput>,
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use async_trait::async_trait;
|
||||
use codex_client::Request;
|
||||
use codex_client::Response;
|
||||
use codex_client::StreamResponse;
|
||||
use codex_client::TransportError;
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct DummyTransport;
|
||||
|
||||
#[async_trait]
|
||||
impl HttpTransport for DummyTransport {
|
||||
async fn execute(&self, _req: Request) -> Result<Response, TransportError> {
|
||||
Err(TransportError::Build("execute should not run".to_string()))
|
||||
}
|
||||
|
||||
async fn stream(&self, _req: Request) -> Result<StreamResponse, TransportError> {
|
||||
Err(TransportError::Build("stream should not run".to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Default)]
|
||||
struct DummyAuth;
|
||||
|
||||
impl AuthProvider for DummyAuth {
|
||||
fn bearer_token(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn path_is_memories_trace_summarize() {
|
||||
assert_eq!(
|
||||
MemoriesClient::<DummyTransport, DummyAuth>::path(),
|
||||
"memories/trace_summarize"
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
pub mod aggregate;
|
||||
pub mod compact;
|
||||
pub mod memories;
|
||||
pub mod models;
|
||||
pub mod responses;
|
||||
pub mod responses_websocket;
|
||||
|
||||
Reference in New Issue
Block a user