From e2a56bb628c476847040908d35d885bdf24c0b05 Mon Sep 17 00:00:00 2001 From: Aljaz Date: Thu, 29 May 2025 14:53:43 +0200 Subject: [PATCH] Add session summarization endpoint --- crates/goose-api/README.md | 39 +++++++++++++++++++ crates/goose-api/src/handlers.rs | 66 ++++++++++++++++++++++++++++++++ crates/goose-api/src/routes.rs | 10 ++++- 3 files changed, 114 insertions(+), 1 deletion(-) diff --git a/crates/goose-api/README.md b/crates/goose-api/README.md index b7fb63a5..2191e609 100644 --- a/crates/goose-api/README.md +++ b/crates/goose-api/README.md @@ -236,6 +236,31 @@ By default, the server runs on `127.0.0.1:8080`. You can modify this using confi } ``` +### 7. Summarize Session + +**Endpoint**: `POST /session/summarize` + +**Description**: Summarizes the full conversation for a given session. + +**Request**: +- Headers: + - Content-Type: application/json + - x-api-key: [your-api-key] +- Body: +```json +{ + "session_id": "" +} +``` + +**Response**: +```json +{ + "message": "", + "status": "success" +} +``` + ## Session Management Sessions created via the API are stored in the same location as the CLI @@ -279,6 +304,12 @@ curl -X POST http://localhost:8080/extensions/remove \ # Get provider configuration curl -X GET http://localhost:8080/provider/config \ -H "x-api-key: your_secure_api_key" + +# Summarize a session +curl -X POST http://localhost:8080/session/summarize \ + -H "Content-Type: application/json" \ + -H "x-api-key: your_secure_api_key" \ + -d '{"session_id": "your-session-id"}' ``` ### Using Python @@ -332,6 +363,14 @@ print(response.json()) # Get provider configuration response = requests.get(f"{API_URL}/provider/config", headers=HEADERS) print(response.json()) + +# Summarize a session +response = requests.post( + f"{API_URL}/session/summarize", + headers=HEADERS, + json={"session_id": "your-session-id"} +) +print(response.json()) ``` ## Troubleshooting diff --git a/crates/goose-api/src/handlers.rs b/crates/goose-api/src/handlers.rs index 096e7193..94fd4abf 100644 --- a/crates/goose-api/src/handlers.rs +++ b/crates/goose-api/src/handlers.rs @@ -43,6 +43,11 @@ pub struct EndSessionRequest { pub session_id: Uuid, } +#[derive(Debug, Serialize, Deserialize)] +pub struct SummarizeSessionRequest { + pub session_id: Uuid, +} + #[derive(Debug, Serialize, Deserialize)] pub struct ExtensionsResponse { pub extensions: Vec, @@ -284,6 +289,67 @@ pub async fn end_session_handler( } } +pub async fn summarize_session_handler( + req: SummarizeSessionRequest, + _api_key: String, +) -> Result { + info!("Summarizing session: {}", req.session_id); + + let agent = AGENT.lock().await; + + let session_name = req.session_id.to_string(); + let session_path = session::get_path(Identifier::Name(session_name.clone())); + + let messages = match session::read_messages(&session_path) { + Ok(m) => m, + Err(_) => { + let response = ApiResponse { + message: "Session not found".to_string(), + status: "error".to_string(), + }; + return Ok(warp::reply::with_status( + warp::reply::json(&response), + warp::http::StatusCode::NOT_FOUND, + )); + } + }; + + let provider = agent.provider().await.ok(); + + match agent.summarize_context(&messages).await { + Ok((summarized_messages, _)) => { + let summary_text = summarized_messages + .first() + .map(|m| m.as_concat_text()) + .unwrap_or_default(); + + if let Err(e) = session::persist_messages(&session_path, &summarized_messages, provider.clone()).await { + warn!("Failed to persist session {}: {}", session_name, e); + } + + let resp = ApiResponse { + message: summary_text, + status: "success".to_string(), + }; + Ok(warp::reply::with_status( + warp::reply::json(&resp), + warp::http::StatusCode::OK, + )) + } + Err(e) => { + error!("Failed to summarize session: {}", e); + let resp = ApiResponse { + message: format!("Failed to summarize session: {}", e), + status: "error".to_string(), + }; + Ok(warp::reply::with_status( + warp::reply::json(&resp), + warp::http::StatusCode::INTERNAL_SERVER_ERROR, + )) + } + } +} + pub async fn list_extensions_handler() -> Result { info!("Listing extensions"); diff --git a/crates/goose-api/src/routes.rs b/crates/goose-api/src/routes.rs index 759786c3..5762bcdd 100644 --- a/crates/goose-api/src/routes.rs +++ b/crates/goose-api/src/routes.rs @@ -4,7 +4,7 @@ use tracing::{info, warn, error}; use crate::handlers::{ add_extension_handler, end_session_handler, get_provider_config_handler, list_extensions_handler, remove_extension_handler, reply_session_handler, - start_session_handler, with_api_key, + start_session_handler, summarize_session_handler, with_api_key, }; use crate::config::{ initialize_extensions, initialize_provider_config, load_configuration, @@ -26,6 +26,13 @@ pub fn build_routes(api_key: String) -> impl Filter impl Filter