From 1dec3b7bd8301a89bde7acd600c15d78ead4cca5 Mon Sep 17 00:00:00 2001 From: Henrik Jess Nielsen Date: Fri, 30 Oct 2026 11:46:57 +0100 Subject: [PATCH] fix(offers): support Claude OAuth token (citti/claude_oauth_token) The key was provisioned as an OAuth token, not an API key. Read it from Consul into ANTHROPIC_AUTH_TOKEN (the SDK uses it as a Bearer credential) and add the required anthropic-beta: oauth-2025-04-20 header on the messages calls. Credential guards now accept a key or a token. The job no longer sets ANTHROPIC_API_KEY, since even an empty value would shadow the token. --- app.py | 27 ++++++++++++++++++++------- citti.nomad | 13 +++++++------ 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/app.py b/app.py index bba73e7..2e59c86 100644 --- a/app.py +++ b/app.py @@ -300,13 +300,25 @@ def fetch_offers(): 'offers_total': total, 'translated': translated, 'matched': matched} +def _claude_mode(): + """'key' for an API key, 'oauth' for a Bearer auth token, else None.""" + if os.environ.get("ANTHROPIC_API_KEY"): + return "key" + if os.environ.get("ANTHROPIC_AUTH_TOKEN"): + return "oauth" + return None + + def _claude_json(client, model, max_tokens, prompt): """One Claude call returning text. Thinking is disabled (these are mechanical JSON tasks and thinking would eat the token budget); if a model rejects the disabled flag we retry without it, and the large max_tokens still fits the - output alongside thinking.""" + output alongside thinking. OAuth tokens require the oauth beta header on + /v1/messages, so add it when authenticating with a token.""" + extra = {"anthropic-beta": "oauth-2025-04-20"} if _claude_mode() == "oauth" else {} kwargs = dict(model=model, max_tokens=max_tokens, - messages=[{"role": "user", "content": prompt}]) + messages=[{"role": "user", "content": prompt}], + extra_headers=extra) try: msg = client.messages.create(thinking={"type": "disabled"}, **kwargs) except Exception as e: @@ -338,9 +350,10 @@ def _translate_batch(client, headings): def translate_missing(): - """Translate offer headings that have no Danish cache entry yet. Needs - ANTHROPIC_API_KEY (as in BlaaAi); without it, headings stay German.""" - if not os.environ.get("ANTHROPIC_API_KEY"): + """Translate offer headings that have no Danish cache entry yet. Needs a + Claude credential (ANTHROPIC_API_KEY or ANTHROPIC_AUTH_TOKEN); without one, + headings stay German.""" + if not _claude_mode(): return 0 with get_db() as c: todo = [r['heading'] for r in c.execute( @@ -387,9 +400,9 @@ def attach_da(tmap, *offer_lists): def match_offers_ai(): """Ask Claude which offer (if any) is the same product as each fixed item. - Stores results in offer_matches. Needs ANTHROPIC_API_KEY; on no key or + Stores results in offer_matches. Needs a Claude credential; on none or failure the heuristic matcher remains the fallback.""" - if not os.environ.get("ANTHROPIC_API_KEY"): + if not _claude_mode(): return 0 with get_db() as c: offers = [dict(r) for r in c.execute( diff --git a/citti.nomad b/citti.nomad index 4537558..c0214ac 100644 --- a/citti.nomad +++ b/citti.nomad @@ -97,14 +97,15 @@ job "citti" { HOST = "0.0.0.0" } - # Claude key for DA translation + AI offer matching (same account as - # BlaaAi). Use citti's own Consul key if set, else fall back to the - # existing blaaai/anthropic_api_key. `or` keeps it empty (not a deploy - # failure) if neither exists — offers then stay German with heuristic - # matching. + # Claude OAuth token for DA translation + AI offer matching. Set as + # ANTHROPIC_AUTH_TOKEN (the SDK reads it as a Bearer token); the app adds + # the required anthropic-beta: oauth-2025-04-20 header. We deliberately do + # NOT set ANTHROPIC_API_KEY — even an empty value would shadow the token. + # keyOrDefault keeps it empty (not a deploy failure) if the KV is unset — + # offers then stay German with heuristic matching. template { data = <