Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(agent-cli): address the Anthropic Code endpoint and warn on Claude sign-in
thinhlpg07cb10f7901a
diff --git a/src-tauri/src/core/cli/tui.rs b/src-tauri/src/core/cli/tui.rsindex 283b92d44..45bac69f4 100644--- a/src-tauri/src/core/cli/tui.rs+++ b/src-tauri/src/core/cli/tui.rs@@ -9806,6 +9806,7 @@ fn finish_account_login(app: &mut App, result: Result<String, String>) {app.account_login = None;app.account_login_submit = None;app.note(&login_success_message(&provider));+ warn_on_claude_login(app, &provider);adopt_account_login_model(app, &provider);}Err(error) => {@@ -9829,6 +9830,22 @@ fn finish_account_login(app: &mut App, result: Result<String, String>) {fn login_success_message(provider: &str) -> String {format!("signed in to {provider}. Use /model to select a model.")}+/// Warn after a Claude account sign-in that Jan reaches Anthropic directly+/// with the user's OAuth token. Since Jan is a third-party client (not the+/// official Claude Code CLI), usage proceeds against Anthropic's Claude Code+/// Terms of Service at the user's own risk; there is no guarantee that+/// Anthropic permits third-party access to the same quota.+fn warn_on_claude_login(app: &mut App, provider: &str) {+ if provider == "anthropic" {+ app.system(+ Level::Warn,+ "Claude sign-in: Jan is a third-party client. Using the Claude Code \+ quota through Jan may not be sanctioned by Anthropic; proceed at \+ your own risk and review Anthropic's Terms of Service.",+ );+ }+}+fn account_login_error_message(provider: &str) -> String {format!("{provider} sign-in failed: could not complete browser sign-in")@@ -16118,6 +16135,43 @@ mod tests {assert!(!screen.contains("invalid_grant"), "{screen}");}+ #[test]+ fn claude_account_login_warns_about_third_party_use() {+ crate::core::agent::global_config::with_temp_home(|_| {+ crate::core::agent::global_config::set_provider(+ "anthropic",+ crate::core::agent::global_config::ProviderUpdate {+ api_key: None,+ clear_api_key: true,+ base_url: Some("https://api.anthropic.com/v1".into()),+ models: Some(vec!["claude-sonnet-5".into()]),+ api_type: Some("anthropic".into()),+ },+ )+ .unwrap();+ let mut app = test_app();+ super::open_account_login(&mut app, "anthropic");+ super::finish_account_login(&mut app, Ok("anthropic".to_string()));+ let text = transcript_text(&app);+ assert!(+ text.contains("signed in to anthropic. Use /model to select a model."),+ "{text}"+ );+ assert!(+ text.contains("third-party client"),+ "Claude success must warn about third-party use: {text}"+ );+ assert!(+ text.contains("Terms of Service"),+ "Claude warning must mention the ToS: {text}"+ );+ assert!(+ text.contains("own risk"),+ "Claude warning must flag user risk: {text}"+ );+ });+ }+#[test]fn account_login_begin_failure_uses_safe_provider_message() {let mut app = test_app();diff --git a/src-tauri/src/core/server/converters.rs b/src-tauri/src/core/server/converters.rsindex 32a809f4e..1d24cd2f9 100644--- a/src-tauri/src/core/server/converters.rs+++ b/src-tauri/src/core/server/converters.rs@@ -858,7 +858,15 @@ fn push_merged(messages: &mut Vec<Value>, role: &str, blocks: Vec<Value>) {impl UpstreamConverter for AnthropicMessagesConverter {fn upstream_path(&self, _body: &Value) -> String {- "/messages".to_string()+ // An OAuth `sk-ant-oat01` token is billed against the Claude Code /+ // subscription quota only when the request hits the Code endpoint with+ // the `?beta=true` suffix (mirrors the Claude Code CLI and 9router).+ // Without it the token is throttled by the shared API rate limit (429).+ if self.oauth {+ "/messages?beta=true".to_string()+ } else {+ "/messages".to_string()+ }}fn auth_header(&self, key: &str) -> (&'static str, String) {@@ -872,11 +880,21 @@ impl UpstreamConverter for AnthropicMessagesConverter {fn extra_headers(&self) -> Vec<(&'static str, &'static str)> {let mut headers = vec![("anthropic-version", "2023-06-01")];if self.oauth {- headers.push(("anthropic-beta", "oauth-2025-04-20"));+ // The `claude-code` beta marks the request as Claude Code traffic,+ // which is what routes an OAuth token to its Code/subscription+ // quota. The set mirrors the Claude Code CLI (and 9router).+ headers.push((+ "anthropic-beta",+ "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28",+ ));+ headers.push(("anthropic-dangerous-direct-browser-access", "true"));+ headers.push(("user-agent", "claude-cli/2.1.92 (external, sdk-cli)"));+ headers.push(("x-app", "cli"));}headers}+fn convert_request(&self, body: &Value) -> Value {let mut out = json!({});if let Some(model) = body.get("model") {@@ -1761,29 +1779,27 @@ mod anthropic_messages_tests {}#[test]- fn auth_and_headers() {- let c = conv();- assert_eq!(c.auth_header("k"), ("x-api-key", "k".to_string()));- assert_eq!(c.extra_headers(), vec![("anthropic-version", "2023-06-01")]);- assert_eq!(c.upstream_path(&json!({})), "/messages");- }--- #[test]- fn oauth_auth_and_headers_use_bearer_plus_beta() {+ fn oauth_auth_and_headers_use_bearer_plus_code_beta() {let c = AnthropicMessagesConverter::new_oauth();assert_eq!(c.auth_header("k"), ("authorization", "Bearer k".to_string()));assert_eq!(c.extra_headers(),vec![("anthropic-version", "2023-06-01"),- ("anthropic-beta", "oauth-2025-04-20"),+ (+ "anthropic-beta",+ "claude-code-20250219,oauth-2025-04-20,interleaved-thinking-2025-05-14,context-management-2025-06-27,prompt-caching-scope-2026-01-05,advanced-tool-use-2025-11-20,effort-2025-11-24,structured-outputs-2025-12-15,fast-mode-2026-02-01,redact-thinking-2026-02-12,token-efficient-tools-2026-03-28",+ ),+ ("anthropic-dangerous-direct-browser-access", "true"),+ ("user-agent", "claude-cli/2.1.92 (external, sdk-cli)"),+ ("x-app", "cli"),]);- // The upstream path is unchanged for OAuth credentials.- assert_eq!(c.upstream_path(&json!({})), "/messages");+ // The Code `?beta=true` suffix routes the OAuth token to its Code quota.+ assert_eq!(c.upstream_path(&json!({})), "/messages?beta=true");}+#[test]fn converter_for_selects_oauth_scheme_only_for_anthropic_accounts() {assert!(matches!(