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