Add an Explain button that has Claude explain a problem
When stuck, the admin can ask Claude for an intuitive explanation of a problem. It is committed as explain.md and shown as folded sections, so the reader opens only as much as they need: the key observation, hints that get stronger, the approach, a walkthrough with an SVG diagram per step, complexity, pitfalls and a pseudocode sketch. - internal/claude: streamed call with a JSON-schema reply, high effort, the server-side refusal fallback, fixed error messages and logged token usage. CLAUDE_API_KEY and CLAUDE_MODEL (default claude-opus-5-5); without a key only this feature is off. - internal/explain: the prompt, the schema, rendering to markdown, and a per-problem job tracker. The request starts a job and the page polls, since a call outlasts nginx's 120s timeout. - internal/statement: fetches statements from LeetCode's GraphQL API and USACO's problem pages. Others (Codeforces is behind Cloudflare) are pasted. - problem.md holds the statement, editable from the meta editor and shown only to the admin. Diagrams are sanitized with DOMPurify. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+23
-2
@@ -4,7 +4,9 @@ The public face of this repo, served at `/usaco/` on adam-french.co.uk. It shows
|
||||
|
||||
- mark a problem solved, skipped or back in progress
|
||||
- set its link
|
||||
- save the problem statement (pasted, or fetched from a LeetCode or USACO link)
|
||||
- write notes
|
||||
- have Claude write an explanation for a problem they are stuck on
|
||||
|
||||
It is one Go binary that serves a REST API and the built Vue SPA. It runs as one container behind the site's nginx and borrows the site's login, the same arrangement as job-tracker (adamf/job-tracker), which it is modelled on.
|
||||
|
||||
@@ -17,6 +19,8 @@ There is no database. The CLI's layout is the data model (see the root README.md
|
||||
- `tests/N.in` and `N.out`
|
||||
- `.url` for the problem link
|
||||
- `notes.md` for notes
|
||||
- `problem.md` for the statement (shown only to the admin, and sent to Claude)
|
||||
- `explain.md` for a Claude-written explanation
|
||||
|
||||
The server keeps a clone of adamf/usaco in `$DATA_DIR/repo` and indexes it into memory.
|
||||
|
||||
@@ -27,7 +31,7 @@ The server keeps a clone of adamf/usaco in `$DATA_DIR/repo` and indexes it into
|
||||
3. commit as `GIT_AUTHOR_NAME`
|
||||
4. push
|
||||
|
||||
The commit subjects match the CLI's: "Solve <name>", "Skip <name>", "Reopen <name>" and "Update <name>". If someone pushed in between and the push is rejected, the edit is replayed once on the new tip. A second rejection returns 409.
|
||||
The commit subjects match the CLI's: "Solve <name>", "Skip <name>", "Reopen <name>" and "Update <name>". An explanation commits as "Explain <name>". If someone pushed in between and the push is rejected, the edit is replayed once on the new tip. A second rejection returns 409.
|
||||
- **Dates come from git, not mtimes.** A problem's solve date is the commit that moved it into `solved/`. Its first and last dates are its first and last commits.
|
||||
|
||||
The site commits, so the laptop must pull before pushing. `usaco sync` does `pull --rebase --autostash` and then `push`.
|
||||
@@ -40,6 +44,9 @@ internal/auth/ is this request the admin? (asks the site backend; copied fr
|
||||
internal/repo/ the clone: sync, and edit-commit-push with one replay on rejection
|
||||
internal/index/ reads the clone into memory: problems, files, tests, git dates
|
||||
internal/handlers/ REST API, Gitea webhook, SPA file server
|
||||
internal/claude/ the Claude client: streamed call, JSON-schema reply, fixed error messages
|
||||
internal/explain/ the explanation prompt and schema, explain.md rendering, the job tracker
|
||||
internal/statement/ fetches statements from LeetCode (GraphQL) and USACO (the problem page)
|
||||
web/ Vue 3 + Vite SPA, built for base /usaco/
|
||||
```
|
||||
|
||||
@@ -53,7 +60,10 @@ Everything is served under `BASE_PATH` (`/usaco`). nginx proxies the prefix thro
|
||||
| `GET /usaco/api/problems` | open; the admin also gets unsolved problems | the list |
|
||||
| `GET /usaco/api/problems/<name>` | open for solved problems; 404 for others unless admin | one problem, with files, tests and history |
|
||||
| `GET /usaco/api/me` | open | `{admin}`, so the SPA knows whether to show the edit controls |
|
||||
| `PUT /usaco/api/problems/<name>/meta` | admin | `{url?, notes?}`: writes `.url` and `notes.md` (blank removes the file) |
|
||||
| `PUT /usaco/api/problems/<name>/meta` | admin | `{url?, notes?, statement?}`: writes `.url`, `notes.md` and `problem.md` (blank removes the file) |
|
||||
| `GET /usaco/api/problems/<name>/statement/fetch?url=` | admin | the statement behind `url` (default: the saved link) as markdown, for review; saves nothing. 422 for a judge it can't read |
|
||||
| `POST /usaco/api/problems/<name>/explain` | admin | `{note?, includeCode?}`: starts an explanation and answers 202; 409 if one is running, 503 without `CLAUDE_API_KEY` |
|
||||
| `GET /usaco/api/problems/<name>/explain` | admin | the latest job: `{state: idle\|running\|done\|error, error?}` |
|
||||
| `POST /usaco/api/problems/<name>/status` | admin | `{to: solving\|solved\|skipped}`: moves the directory |
|
||||
| `POST /usaco/api/hooks/gitea` | HMAC (`X-Gitea-Signature`) | queue a pull |
|
||||
| `GET /usaco/api/health` | open | 200 once the clone is indexed |
|
||||
@@ -126,8 +136,19 @@ The registry setup is shared with job-tracker (see its README, "One-off: registr
|
||||
|
||||
Gitea only delivers to Docker-network hosts that are on `[webhook] ALLOWED_HOST_LIST`. web_server's compose sets that to `external,usaco`. Use "Test delivery": the response should be `202`, and `docker logs usaco` should show a new `[index]` line.
|
||||
|
||||
## Explanations
|
||||
|
||||
The Explain panel on a problem page (admin only) asks Claude for an intuitive explanation and commits it as `explain.md`.
|
||||
|
||||
- **Input.** The statement (`problem.md`; when it is missing, it is fetched from the link and saved too), up to three saved tests with outputs or the sample input, optionally the main solution file, and the admin's "where I'm stuck" note.
|
||||
- **Output.** A JSON-schema reply (`internal/explain` `Schema`): the key observation, 2 to 4 hints, the approach, a walkthrough whose steps each carry an SVG diagram, complexity, pitfalls and a pseudocode sketch. It is stored as markdown with one `## ` section each, and the page shows every section folded so the reader opens only as much as they need. Diagrams are ```` ```svg ```` fences, sanitized with DOMPurify before they are inserted.
|
||||
- **The call.** `CLAUDE_MODEL` (default `claude-opus-5-5`) at high effort, streamed, with the server-side refusal fallback on. It takes a minute or two and costs roughly $0.10 to $0.60. The request only starts a background job, because nginx gives up after 120 seconds; the page polls. Token usage is logged as `[claude]` lines.
|
||||
- **Statements.** LeetCode and USACO can be fetched. Codeforces sits behind a Cloudflare challenge, and other judges are unknown, so paste those.
|
||||
|
||||
## Things that are easy to get wrong
|
||||
|
||||
- **`BASE_PATH` and Vite's `base` must agree.** The SPA's asset URLs and router base are fixed at build time.
|
||||
- **Edits must decide from disk, not the index.** `repo.Mutate` may run an edit twice, the second time on a newer tip. The handlers re-locate the problem in the clone each time with `findOnDisk`.
|
||||
- **`problem.md` is the judge's text.** `GET /problems/<name>` blanks it for non-admins, so the public site links to the judge instead of republishing it.
|
||||
- **An explanation job lives in memory.** A restart mid-job loses it, and nothing is committed. Start it again.
|
||||
- **`tests/*.out` used to be gitignored** by the root `*.out` rule. The root `.gitignore` now re-includes `**/tests/*.out`. A test whose expected output is shown as "Not saved." predates that change or never had one.
|
||||
|
||||
Reference in New Issue
Block a user