[router] responses api POST and GET with local storage (#10581)

Co-authored-by: key4ng <rukeyang@gmail.com>
This commit is contained in:
Simo Lin
2025-09-23 12:12:02 -04:00
committed by GitHub
parent ddab4fc7c7
commit 98c3b04ff2
14 changed files with 1158 additions and 176 deletions

View File

@@ -74,13 +74,16 @@ impl ResponseStorage for MemoryResponseStorage {
// Store the response
store.responses.insert(response_id.clone(), response);
tracing::info!("memory_store_size" = store.responses.len());
Ok(response_id)
}
async fn get_response(&self, response_id: &ResponseId) -> Result<Option<StoredResponse>> {
let store = self.store.read();
Ok(store.responses.get(response_id).cloned())
let result = store.responses.get(response_id).cloned();
tracing::info!("memory_get_response" = %response_id.0, found = result.is_some());
Ok(result)
}
async fn delete_response(&self, response_id: &ResponseId) -> Result<()> {
@@ -200,6 +203,20 @@ pub struct MemoryStoreStats {
mod tests {
use super::*;
#[tokio::test]
async fn test_store_with_custom_id() {
let store = MemoryResponseStorage::new();
let mut response = StoredResponse::new("Input".to_string(), "Output".to_string(), None);
response.id = ResponseId::from_string("resp_custom".to_string());
store.store_response(response.clone()).await.unwrap();
let retrieved = store
.get_response(&ResponseId::from_string("resp_custom".to_string()))
.await
.unwrap();
assert!(retrieved.is_some());
assert_eq!(retrieved.unwrap().output, "Output");
}
#[tokio::test]
async fn test_memory_store_basic() {
let store = MemoryResponseStorage::new();

View File

@@ -1,5 +1,6 @@
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
@@ -55,6 +56,10 @@ pub struct StoredResponse {
/// Model used for generation
pub model: Option<String>,
/// Raw OpenAI response payload
#[serde(default)]
pub raw_response: Value,
}
impl StoredResponse {
@@ -70,6 +75,7 @@ impl StoredResponse {
created_at: chrono::Utc::now(),
user: None,
model: None,
raw_response: Value::Null,
}
}
}
@@ -175,3 +181,9 @@ pub trait ResponseStorage: Send + Sync {
/// Type alias for shared storage
pub type SharedResponseStorage = Arc<dyn ResponseStorage>;
impl Default for StoredResponse {
fn default() -> Self {
Self::new(String::new(), String::new(), None)
}
}