use std::cell::RefCell;
use std::rc::Rc;
use std::sync::LazyLock;
use std::time::Duration;
use deno_core::{op2, OpState};
use deno_error::JsErrorBox;
use serde::{Deserialize, Serialize};
use crate::secrets::{store as secret_store, SecretsService, ANTHROPIC_API_KEY_NAME};
pub const DEFAULT_MODEL: &str = "claude-sonnet-5";
pub const DEFAULT_MAX_TOKENS: u32 = 4096;
pub const ANTHROPIC_URL: &str = "https://api.anthropic.com/v1/messages";
pub const ANTHROPIC_VERSION: &str = "2023-06-01";
const ANTHROPIC_TIMEOUT_SECS: u64 = 90;
static ANTHROPIC_CLIENT: LazyLock<reqwest::Client> = LazyLock::new(|| {
reqwest::Client::builder()
.timeout(Duration::from_secs(ANTHROPIC_TIMEOUT_SECS))
.build()
.expect("failed to build reqwest client for Anthropic")
});
#[derive(Serialize, Deserialize, Clone, Debug)]
#[serde(rename_all = "lowercase")]
pub enum Role {
User,
Assistant,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Message {
pub role: Role,
pub content: String,
}
#[derive(Serialize)]
struct RequestBody<'a> {
model: &'a str,
max_tokens: u32,
#[serde(skip_serializing_if = "Option::is_none")]
system: Option<&'a str>,
messages: &'a [Message],
}
#[derive(Deserialize)]
struct ResponseContent {
#[serde(rename = "type")]
_type: String,
text: String,
}
#[derive(Deserialize)]
struct ResponseBody {
content: Vec<ResponseContent>,
}
pub async fn complete(
api_key: &str,
model: Option<&str>,
system: Option<&str>,
messages: &[Message],
) -> Result<String, String> {
let body = RequestBody {
model: model.unwrap_or(DEFAULT_MODEL),
max_tokens: DEFAULT_MAX_TOKENS,
system,
messages,
};
let response = ANTHROPIC_CLIENT
.post(ANTHROPIC_URL)
.header("x-api-key", api_key)
.header("anthropic-version", ANTHROPIC_VERSION)
.header("content-type", "application/json")
.json(&body)
.send()
.await
.map_err(|e| format!("http request failed: {e}"))?;
let status = response.status();
let text = response
.text()
.await
.map_err(|e| format!("failed to read response body: {e}"))?;
if !status.is_success() {
return Err(format!("anthropic API error {status}: {text}"));
}
let parsed: ResponseBody = serde_json::from_str(&text)
.map_err(|e| format!("failed to parse anthropic response: {e} — body: {text}"))?;
parsed
.content
.into_iter()
.next()
.map(|c| c.text)
.ok_or_else(|| "anthropic response had no content".to_string())
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct CompleteArgs {
prompt: String,
#[serde(default)]
system: Option<String>,
#[serde(default)]
model: Option<String>,
}
#[op2(async)]
#[string]
pub async fn op_chamberlain_ai_complete(
state: Rc<RefCell<OpState>>,
#[serde] args: CompleteArgs,
) -> Result<String, JsErrorBox> {
let api_key = {
let state = state.borrow();
let service = state.borrow::<SecretsService>().0.clone();
secret_store::get(&service, ANTHROPIC_API_KEY_NAME)
.map_err(|e| JsErrorBox::generic(format!("failed to read anthropic_api_key: {e}")))?
.ok_or_else(|| JsErrorBox::generic("anthropic_api_key is not set"))?
};
let messages = vec![Message {
role: Role::User,
content: args.prompt,
}];
complete(
&api_key,
args.model.as_deref(),
args.system.as_deref(),
&messages,
)
.await
.map_err(JsErrorBox::generic)
}