|
| 1 | +use axum::{ |
| 2 | + extract::Path, |
| 3 | + routing::{delete, get, put}, |
| 4 | + Json, Router, |
| 5 | +}; |
| 6 | +use goose::prompt_template::{ |
| 7 | + get_template, list_templates, reset_template, save_template, Template, |
| 8 | +}; |
| 9 | +use http::StatusCode; |
| 10 | +use serde::{Deserialize, Serialize}; |
| 11 | +use utoipa::ToSchema; |
| 12 | + |
| 13 | +#[derive(Serialize, ToSchema)] |
| 14 | +pub struct PromptsListResponse { |
| 15 | + pub prompts: Vec<Template>, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Serialize, ToSchema)] |
| 19 | +pub struct PromptContentResponse { |
| 20 | + pub name: String, |
| 21 | + pub content: String, |
| 22 | + pub default_content: String, |
| 23 | + pub is_customized: bool, |
| 24 | +} |
| 25 | + |
| 26 | +#[derive(Deserialize, ToSchema)] |
| 27 | +pub struct SavePromptRequest { |
| 28 | + pub content: String, |
| 29 | +} |
| 30 | + |
| 31 | +#[utoipa::path( |
| 32 | + get, |
| 33 | + path = "/config/prompts", |
| 34 | + responses( |
| 35 | + (status = 200, description = "List of all available prompts", body = PromptsListResponse) |
| 36 | + ) |
| 37 | +)] |
| 38 | +pub async fn get_prompts() -> Json<PromptsListResponse> { |
| 39 | + Json(PromptsListResponse { |
| 40 | + prompts: list_templates(), |
| 41 | + }) |
| 42 | +} |
| 43 | + |
| 44 | +#[utoipa::path( |
| 45 | + get, |
| 46 | + path = "/config/prompts/{name}", |
| 47 | + params( |
| 48 | + ("name" = String, Path, description = "Prompt template name (e.g., system.md)") |
| 49 | + ), |
| 50 | + responses( |
| 51 | + (status = 200, description = "Prompt content retrieved successfully", body = PromptContentResponse), |
| 52 | + (status = 404, description = "Prompt not found") |
| 53 | + ) |
| 54 | +)] |
| 55 | +pub async fn get_prompt( |
| 56 | + Path(name): Path<String>, |
| 57 | +) -> Result<Json<PromptContentResponse>, StatusCode> { |
| 58 | + let template = get_template(&name).ok_or(StatusCode::NOT_FOUND)?; |
| 59 | + |
| 60 | + let content = template |
| 61 | + .user_content |
| 62 | + .as_ref() |
| 63 | + .unwrap_or(&template.default_content); |
| 64 | + |
| 65 | + Ok(Json(PromptContentResponse { |
| 66 | + name: template.name, |
| 67 | + content: content.clone(), |
| 68 | + default_content: template.default_content, |
| 69 | + is_customized: template.is_customized, |
| 70 | + })) |
| 71 | +} |
| 72 | + |
| 73 | +#[utoipa::path( |
| 74 | + put, |
| 75 | + path = "/config/prompts/{name}", |
| 76 | + params( |
| 77 | + ("name" = String, Path, description = "Prompt template name (e.g., system.md)") |
| 78 | + ), |
| 79 | + request_body = SavePromptRequest, |
| 80 | + responses( |
| 81 | + (status = 200, description = "Prompt saved successfully", body = String), |
| 82 | + (status = 404, description = "Prompt not found"), |
| 83 | + (status = 500, description = "Failed to save prompt") |
| 84 | + ) |
| 85 | +)] |
| 86 | +pub async fn save_prompt( |
| 87 | + Path(name): Path<String>, |
| 88 | + Json(request): Json<SavePromptRequest>, |
| 89 | +) -> Result<Json<String>, StatusCode> { |
| 90 | + save_template(&name, &request.content).map_err(|e| { |
| 91 | + if e.kind() == std::io::ErrorKind::NotFound { |
| 92 | + StatusCode::NOT_FOUND |
| 93 | + } else { |
| 94 | + tracing::error!("Failed to save prompt {}: {}", name, e); |
| 95 | + StatusCode::INTERNAL_SERVER_ERROR |
| 96 | + } |
| 97 | + })?; |
| 98 | + |
| 99 | + Ok(Json(format!("Saved prompt: {}", name))) |
| 100 | +} |
| 101 | + |
| 102 | +#[utoipa::path( |
| 103 | + delete, |
| 104 | + path = "/config/prompts/{name}", |
| 105 | + params( |
| 106 | + ("name" = String, Path, description = "Prompt template name (e.g., system.md)") |
| 107 | + ), |
| 108 | + responses( |
| 109 | + (status = 200, description = "Prompt reset to default successfully", body = String), |
| 110 | + (status = 404, description = "Prompt not found"), |
| 111 | + (status = 500, description = "Failed to reset prompt") |
| 112 | + ) |
| 113 | +)] |
| 114 | +pub async fn reset_prompt(Path(name): Path<String>) -> Result<Json<String>, StatusCode> { |
| 115 | + reset_template(&name).map_err(|e| { |
| 116 | + if e.kind() == std::io::ErrorKind::NotFound { |
| 117 | + StatusCode::NOT_FOUND |
| 118 | + } else { |
| 119 | + tracing::error!("Failed to reset prompt {}: {}", name, e); |
| 120 | + StatusCode::INTERNAL_SERVER_ERROR |
| 121 | + } |
| 122 | + })?; |
| 123 | + |
| 124 | + Ok(Json(format!("Reset prompt to default: {}", name))) |
| 125 | +} |
| 126 | + |
| 127 | +pub fn routes() -> Router { |
| 128 | + Router::new() |
| 129 | + .route("/config/prompts", get(get_prompts)) |
| 130 | + .route("/config/prompts/{name}", get(get_prompt)) |
| 131 | + .route("/config/prompts/{name}", put(save_prompt)) |
| 132 | + .route("/config/prompts/{name}", delete(reset_prompt)) |
| 133 | +} |
0 commit comments