mirror of
https://github.com/aljazceru/goose.git
synced 2025-12-18 14:44:21 +01:00
Add session summarization endpoint
This commit is contained in:
@@ -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": "<uuid>"
|
||||
}
|
||||
```
|
||||
|
||||
**Response**:
|
||||
```json
|
||||
{
|
||||
"message": "<summarized conversation>",
|
||||
"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
|
||||
|
||||
@@ -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<String>,
|
||||
@@ -284,6 +289,67 @@ pub async fn end_session_handler(
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn summarize_session_handler(
|
||||
req: SummarizeSessionRequest,
|
||||
_api_key: String,
|
||||
) -> Result<impl warp::Reply, Rejection> {
|
||||
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<impl warp::Reply, Rejection> {
|
||||
info!("Listing extensions");
|
||||
|
||||
|
||||
@@ -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<Extract = impl warp::Reply,
|
||||
.and(with_api_key(api_key.clone()))
|
||||
.and_then(reply_session_handler);
|
||||
|
||||
let summarize_session = warp::path("session")
|
||||
.and(warp::path("summarize"))
|
||||
.and(warp::post())
|
||||
.and(warp::body::json())
|
||||
.and(with_api_key(api_key.clone()))
|
||||
.and_then(summarize_session_handler);
|
||||
|
||||
let end_session = warp::path("session")
|
||||
.and(warp::path("end"))
|
||||
.and(warp::post())
|
||||
@@ -59,6 +66,7 @@ pub fn build_routes(api_key: String) -> impl Filter<Extract = impl warp::Reply,
|
||||
|
||||
start_session
|
||||
.or(reply_session)
|
||||
.or(summarize_session)
|
||||
.or(end_session)
|
||||
.or(list_extensions)
|
||||
.or(add_extension)
|
||||
|
||||
Reference in New Issue
Block a user