Compare commits

..

3 Commits

Author SHA1 Message Date
ed2121e4f8 Merge pull request 'worktree-citti-arkiver-tur' (#4) from worktree-citti-arkiver-tur into main
All checks were successful
Build and Deploy citti / build-and-deploy (push) Successful in 46s
Reviewed-on: #4
2026-10-30 11:47:44 +01:00
Henrik Jess Nielsen
1dec3b7bd8 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.
2026-10-30 11:46:57 +01:00
Henrik Jess Nielsen
06656ffd4f fix(deploy): reuse blaaai Consul key so citti gets a Claude key
Prod container had no ANTHROPIC_API_KEY (refresh returned 0/0 instantly;
homepage showed only the 9 heuristic matches), so offers stayed German.
The template read citti/anthropic_api_key, a path never set. Fall back to
the existing blaaai Consul key via or(), reusing BlaaAi's key with no new
secret to provision.
2026-10-30 11:28:19 +01:00
2 changed files with 27 additions and 11 deletions

27
app.py
View File

@@ -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(

View File

@@ -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 = <<EOT
ANTHROPIC_API_KEY={{ keyOrDefault "citti/anthropic_api_key" "" }}
ANTHROPIC_AUTH_TOKEN={{ keyOrDefault "citti/claude_oauth_token" "" }}
EOT
destination = "secrets/app.env"
env = true