Files
stpweb/python/app/claude.py
T
adamfandClaude Opus 5.5 b8670246a8
Deploy with Docker Compose / deploy (push) Successful in 1m31s
Add browser job capture to the job tracker
A Violentmonkey userscript, served by the site and installed from /cv/jobs,
adds the posting open in the browser (LinkedIn, an ATS, any company page) to
the tracker. Claude reads the title, company and location off the page text.

- Three token-guarded REST routes under /api/jobs/capture (status, extract,
  upsert). They take a bearer token, JOB_CAPTURE_TOKEN, because the auth
  cookies are SameSite=Lax and never sent from another origin. An unset token
  switches the routes off.
- The upsert matches on a canonical posting URL, then (company, job title),
  so a capture and the email pipeline do not make two rows for one job.
- The Claude call is a new internal Python route, /internal/jobs/extract,
  alongside the rowing reader; the client and fence stripping they share move
  to python/app/claude.py.
- New "saved" status ranked below "applied", and a description column holding
  the posting text, shown per row on the tracker.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-12 13:33:28 +01:00

60 lines
2.1 KiB
Python

"""The Anthropic client and the reply handling every Claude caller here shares.
`rowing` (a photo in, three numbers out) and `jobs` (page text in, a few
fields out) are the same shape of call: one prompt asking for bare JSON, one
reply to parse. What they have in common lives here so the two cannot drift -
the lazily built client, the fence stripping, and the two exception types
`main` maps to 502 and 503.
Like the modules that use it, this never imports FastAPI.
"""
import functools
import os
import anthropic
MODEL = "claude-haiku-4-5"
# Seconds per attempt. The SDK retries twice on top (429/5xx/connection), so
# the worst case is roughly three of these plus backoff; the Go callers' own
# timeout is set above that so they see this side's error, not their own.
TIMEOUT_S = 30.0
class ReadError(Exception):
"""Claude could not produce the answer; the message is safe to show the admin."""
class NotConfigured(ReadError):
"""CLAUDE_API_KEY is not set in this container."""
@functools.lru_cache(maxsize=1)
def client() -> anthropic.Anthropic:
"""Build the Anthropic client on first use.
Lazy so that a missing key only breaks the Claude features: the hotel
finder must still boot without it. The key is passed explicitly because the
stack's variable is CLAUDE_API_KEY, not the ANTHROPIC_API_KEY the SDK looks
for.
"""
key = os.environ.get("CLAUDE_API_KEY", "")
if not key:
raise NotConfigured("CLAUDE_API_KEY is not set")
return anthropic.Anthropic(api_key=key, timeout=TIMEOUT_S)
def strip_markdown_fence(raw: str) -> str:
"""Remove a surrounding markdown code fence from a model's response.
The prompts ask for bare JSON and the model fences it anyway often enough
to matter. Mirrors services.StripMarkdownFence in the Go backend (still
used by the email pipeline): "```json" is tried before a plain "```", and
unfenced input comes back unchanged apart from surrounding whitespace.
"""
raw = raw.strip()
raw = raw.removeprefix("```json")
raw = raw.removeprefix("```")
raw = raw.removesuffix("```")
return raw.strip()