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 26a41b1..c0214ac 100644 --- a/citti.nomad +++ b/citti.nomad @@ -97,12 +97,15 @@ job "citti" { HOST = "0.0.0.0" } - # Claude key for DA translation + AI offer matching (same account as - # BlaaAi). keyOrDefault so a missing KV never blocks deploy — without it - # offers simply stay German and the token heuristic handles 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 = <