From edf2f8e03cbb61df385b9b145b46f676ce3b70d5 Mon Sep 17 00:00:00 2001 From: Eureka Date: Sun, 29 Nov 2026 20:25:20 +0000 Subject: [PATCH] claude-connector: Messages API backend for API-key auth (tools, thinking, caching) (#4) --- Cargo.lock | 137 ++- README.md | 11 +- crates/claude-connector/Cargo.toml | 3 +- crates/claude-connector/src/ask.rs | 3 +- crates/claude-connector/src/backend.rs | 2 +- crates/claude-connector/src/backend/tests.rs | 109 +- crates/claude-connector/src/config.rs | 36 +- crates/claude-connector/src/daemon.rs | 63 +- crates/claude-connector/src/launch.rs | 6 +- crates/claude-connector/src/lib.rs | 2 + crates/claude-connector/src/messages.rs | 626 ++++++++++ crates/claude-connector/src/messages/tests.rs | 1095 +++++++++++++++++ crates/claude-connector/tests/ask.rs | 5 +- 13 files changed, 1929 insertions(+), 169 deletions(-) create mode 100644 crates/claude-connector/src/messages.rs create mode 100644 crates/claude-connector/src/messages/tests.rs diff --git a/Cargo.lock b/Cargo.lock index 996b5fc..37286de 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -73,6 +73,12 @@ version = "0.22.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72b3254f16251a8381aa12e40e3c4d2f0199f8c6508fbecb9d91f575e0fbb8c6" +[[package]] +name = "base64" +version = "0.23.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac07cdecf99051d9a5238b80f35af32cdeba5b336e55d957b318b50137e18da5" + [[package]] name = "base64ct" version = "1.8.3" @@ -94,6 +100,12 @@ version = "3.20.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "72f5acc6cb2ba439de613abc23857ec3d78374d8ed5ac84e9d11336e87da8649" +[[package]] +name = "bytes" +version = "1.12.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "776dfebd015e2d8732c684a2c71003674ccb49457b539b9e82151ba38e96dd7c" + [[package]] name = "cc" version = "1.4.4" @@ -151,6 +163,7 @@ dependencies = [ "serde_json", "sha2", "thiserror", + "ureq", "zeroize", ] @@ -232,7 +245,7 @@ source = "git+https://github.com/GameCult/CultLib.git?rev=a4eae9056152335662f111 dependencies = [ "aes-gcm", "anyhow", - "base64", + "base64 0.22.1", "chrono", "cultcache-rs", "ed25519-dalek", @@ -437,6 +450,22 @@ dependencies = [ "digest", ] +[[package]] +name = "http" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "918d3568bebf352712bc2ef3d46a8bcf1a75b373be6539de198e9105cbbf9ce0" +dependencies = [ + "bytes", + "itoa", +] + +[[package]] +name = "httparse" +version = "1.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" + [[package]] name = "iana-time-zone" version = "0.1.65" @@ -511,7 +540,7 @@ version = "0.1.0" dependencies = [ "aes-gcm", "anyhow", - "base64", + "base64 0.22.1", "cultcache-rs", "cultnet-rs", "rand_core 0.6.4", @@ -556,6 +585,12 @@ version = "0.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c08d65885ee38876c4f86fa503fb49d7b507c2b62552df7c70b2fce627e06381" +[[package]] +name = "percent-encoding" +version = "2.3.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" + [[package]] name = "pin-project-lite" version = "0.2.17" @@ -700,6 +735,20 @@ dependencies = [ "syn 3.0.4", ] +[[package]] +name = "ring" +version = "0.17.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a4689e6c2294d81e88dc6261c768b63bc4fcdb852be6d1352498b114f61383b7" +dependencies = [ + "cc", + "cfg-if", + "getrandom 0.2.17", + "libc", + "untrusted", + "windows-sys 0.52.0", +] + [[package]] name = "rmp" version = "0.8.15" @@ -739,6 +788,41 @@ dependencies = [ "semver", ] +[[package]] +name = "rustls" +version = "0.23.46" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" +dependencies = [ + "log", + "once_cell", + "ring", + "rustls-pki-types", + "rustls-webpki", + "subtle", + "zeroize", +] + +[[package]] +name = "rustls-pki-types" +version = "1.16.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "102f35f7de7b06d8c61f1cff6592f5253e0dea431c74867aad5dfd70f44663fb" +dependencies = [ + "zeroize", +] + +[[package]] +name = "rustls-webpki" +version = "0.103.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e3a0760a887c14823fca5a1c970b01d47c328c696c0c64829d1df50e8f853fd5" +dependencies = [ + "ring", + "rustls-pki-types", + "untrusted", +] + [[package]] name = "rustversion" version = "1.0.23" @@ -986,6 +1070,46 @@ dependencies = [ "subtle", ] +[[package]] +name = "untrusted" +version = "0.9.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8ecb6da28b8a351d773b68d5825ac39017e680750f980f3a1a85cd8dd28a47c1" + +[[package]] +name = "ureq" +version = "3.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dea7109cdcd5864d4eeb1b58a1648dc9bf520360d7af16ec26d0a9354bafcfc0" +dependencies = [ + "base64 0.22.1", + "log", + "percent-encoding", + "rustls", + "rustls-pki-types", + "ureq-proto", + "utf8-zero", + "webpki-roots", +] + +[[package]] +name = "ureq-proto" +version = "0.6.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841583fc36089d7d59419568ae157f43a5a03a2e5a81ba87c77d1596ef46e922" +dependencies = [ + "base64 0.23.1", + "http", + "httparse", + "log", +] + +[[package]] +name = "utf8-zero" +version = "0.8.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b8c0a043c9540bae7c578c88f91dda8bd82e59ae27c21baca69c8b191aaf5a6e" + [[package]] name = "uuid" version = "1.25.0" @@ -1063,6 +1187,15 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "webpki-roots" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7265c9301c16a8711c992c2d8da90ee77ef54b1ed51482d6dfeb00dc756b1297" +dependencies = [ + "rustls-pki-types", +] + [[package]] name = "winapi" version = "0.3.9" diff --git a/README.md b/README.md index e094571..eb3a1e3 100644 --- a/README.md +++ b/README.md @@ -50,15 +50,12 @@ Crate `crates/claude-connector` (library and the `claude-connector` binary that - Reasoning state is a session handle (`ClaudeCodeHandle`) authenticated with a connector secret and bound to the caller it was minted for; a handle that cannot be proven to match the request and its caller re-seeds from the caller's transcript, and every resume forks. +- `MessagesBackend` implements `ProviderBackend` for provider `anthropic-messages` when the config document says `auth` is an API key (`config init --auth api-key`; one choice for the whole connector, held in the credential `--token-credential` names). One non-streaming `POST /v1/messages` per call: tools, tool calls and results, thinking blocks (replayed unchanged as reasoning state), `cache_control` (5 minutes, or 1 hour for long retention) and cache read and write usage map natively. The key is read from the systemd credential at every call and travels only as the `x-api-key` header. A request's output schema is `output_config.format`; `Required` tool choice is `tool_choice: any` where the model accepts it and a refusal before sending where it does not (the current Sonnet and Opus reject it). A key has no plan: no meter runs, the plan windows stay empty, and a key document refuses leases. - `ProcessPort` is the one OS seam (`StdProcessPort`, unix, kills the child's whole process group); `SessionStore` and `StderrSink` are the other ports. `ClaudeCodeConfig` is what the - binary hands in; the credential is read from a systemd credential and reaches the child on a - pipe, never in an environment: `CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR` by default, or - `CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR` when the config document says `auth` is an API key - (`config init --auth api-key`; one choice for the whole connector, held in the credential - `--token-credential` names). `QuotaCell` keeps the plan windows the CLI's `rate_limit_event`s - report, for the usage readout and the lease grant; on an API key there is no plan, no meter - runs and the cell stays empty. + binary hands in; the subscription token is read from a systemd credential and reaches the child on a + pipe, never in an environment (`CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR`). `QuotaCell` keeps the + plan windows the CLI's `rate_limit_event`s report, for the usage readout and the lease grant. ## Asking for one no-tools completion diff --git a/crates/claude-connector/Cargo.toml b/crates/claude-connector/Cargo.toml index 3b1d113..73f60cd 100644 --- a/crates/claude-connector/Cargo.toml +++ b/crates/claude-connector/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "claude-connector" -description = "Claude backend for the connector kit: drives the unmodified claude binary in print stream-json mode" +description = "Claude backends for the connector kit: the claude CLI on a subscription token, the Messages API on an API key" version.workspace = true edition.workspace = true license.workspace = true @@ -23,6 +23,7 @@ serde = { version = "1", features = ["derive"] } serde_json = "1" sha2 = "0.10" thiserror = "2" +ureq = { version = "~3.3", default-features = false, features = ["rustls"] } zeroize = "1" [target.'cfg(unix)'.dependencies] diff --git a/crates/claude-connector/src/ask.rs b/crates/claude-connector/src/ask.rs index ec00de7..ddd4e9f 100644 --- a/crates/claude-connector/src/ask.rs +++ b/crates/claude-connector/src/ask.rs @@ -278,7 +278,7 @@ pub fn ask(args: &AskArgs) -> Result { #[cfg(test)] mod tests { use super::*; - use crate::config::{ClaudeAuth, ClaudeCodeConfig, ClaudeCodeLimits}; + use crate::config::{ClaudeCodeConfig, ClaudeCodeLimits}; use crate::handle::{SessionStore, plan}; use model_connector::ToolDefinition; @@ -315,7 +315,6 @@ mod tests { prompt_dir: "/p".into(), token_credential: "t".into(), handle_secret_credential: "h".into(), - auth: ClaudeAuth::OAuthToken, instance: "i".into(), limits: ClaudeCodeLimits { deadline: Duration::from_secs(1), diff --git a/crates/claude-connector/src/backend.rs b/crates/claude-connector/src/backend.rs index 0b02690..71a2c15 100644 --- a/crates/claude-connector/src/backend.rs +++ b/crates/claude-connector/src/backend.rs @@ -261,7 +261,7 @@ impl ClaudeCodeBackend { deadline, &request.model, request.output_schema_json.as_deref(), - self.config.auth.is_metered().then_some(&*self.quota), + Some(&*self.quota), ); child.kill(); child.wait(); diff --git a/crates/claude-connector/src/backend/tests.rs b/crates/claude-connector/src/backend/tests.rs index 43b2e44..bcf5ce8 100644 --- a/crates/claude-connector/src/backend/tests.rs +++ b/crates/claude-connector/src/backend/tests.rs @@ -11,7 +11,7 @@ use model_connector::{ }; use super::*; -use crate::config::{ClaudeAuth, ClaudeCodeLimits}; +use crate::config::ClaudeCodeLimits; use crate::handle::{FsSessionStore, SessionStore}; use crate::launch::ChildEnv; @@ -19,8 +19,6 @@ const CANARY_TOKEN: &str = "canary-oauth-token-value"; /// The variable the connector's own environment must never hand on. const TOKEN_ENV: &str = "CLAUDE_CODE_OAUTH_TOKEN"; const TOKEN_FD_ENV: &str = "CLAUDE_CODE_OAUTH_TOKEN_FILE_DESCRIPTOR"; -const KEY_FD_ENV: &str = "CLAUDE_CODE_API_KEY_FILE_DESCRIPTOR"; -const CANARY_KEY: &str = "canary-api-key-value"; const MODEL: &str = "claude-haiku-5-5"; const INSTANCE: &str = "persona-a"; const SESSION: &str = "11111111-2222-3333-4444-555555555555"; @@ -335,7 +333,6 @@ fn config(dir: &Path, instance: &str, limits: ClaudeCodeLimits) -> ClaudeCodeCon config_dir: PathBuf::from("/cfg"), prompt_dir: dir.join("prompts"), token_credential: "claude-token".to_string(), - auth: ClaudeAuth::OAuthToken, handle_secret_credential: "claude-handle-secret".to_string(), instance: instance.to_string(), limits, @@ -677,90 +674,13 @@ fn child_env_is_cleared_and_names_the_token_descriptor_never_the_token() { assert!(!format!("{result:?}").contains(CANARY_TOKEN)); } -// ---- API key authentication ---- - -/// A rig whose child authenticates with an API key. -fn api_rig(scripts: Vec