use crate::http::context::{AppContext, OpenApiResult, bad_request}; use crate::llm::chat; use poem_openapi::{Object, OpenApi, Tags, payload::Json}; use serde::Deserialize; use std::sync::Arc; #[derive(Deserialize, Object)] struct ApiKeyPayload { api_key: String, } #[derive(Tags)] enum AnthropicTags { Anthropic, } pub struct AnthropicApi { ctx: Arc, } impl AnthropicApi { pub fn new(ctx: Arc) -> Self { Self { ctx } } } #[OpenApi(tag = "AnthropicTags::Anthropic")] impl AnthropicApi { /// Check whether an Anthropic API key is stored. /// /// Returns `true` if a non-empty key is present, otherwise `false`. #[oai(path = "/anthropic/key/exists", method = "get")] async fn get_anthropic_api_key_exists(&self) -> OpenApiResult> { let exists = chat::get_anthropic_api_key_exists(self.ctx.store.as_ref()).map_err(bad_request)?; Ok(Json(exists)) } /// Store or update the Anthropic API key used for requests. /// /// Returns `true` when the key is saved successfully. #[oai(path = "/anthropic/key", method = "post")] async fn set_anthropic_api_key( &self, payload: Json, ) -> OpenApiResult> { chat::set_anthropic_api_key(self.ctx.store.as_ref(), payload.0.api_key) .map_err(bad_request)?; Ok(Json(true)) } }