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:
@@ -1,6 +1,7 @@
|
||||
// Command usacosite serves the USACO practice log at /usaco/: the problems
|
||||
// from adamf/usaco on the site's Gitea, their solutions and tests, and a few
|
||||
// admin edits (problem link, notes, status) that it commits back to git.
|
||||
// admin edits (problem link, notes, statement, status) and Claude-written
|
||||
// explanations that it commits back to git.
|
||||
//
|
||||
// It is built to sit behind adam-french.co.uk's nginx and borrow its login
|
||||
// (internal/auth), like the job tracker. There is no database: the server
|
||||
@@ -21,6 +22,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"adam-french.co.uk/usaco-site/internal/auth"
|
||||
"adam-french.co.uk/usaco-site/internal/claude"
|
||||
"adam-french.co.uk/usaco-site/internal/handlers"
|
||||
"adam-french.co.uk/usaco-site/internal/repo"
|
||||
)
|
||||
@@ -66,6 +68,16 @@ func main() {
|
||||
log.Println("[webhook] WEBHOOK_SECRET is not set: the push hook is off, pulls happen on the interval only")
|
||||
}
|
||||
|
||||
// CLAUDE
|
||||
// Optional: without a key the site runs as before and the Explain
|
||||
// button answers 503.
|
||||
if c, err := claude.New(os.Getenv("CLAUDE_API_KEY"), os.Getenv("CLAUDE_MODEL")); err == nil {
|
||||
srv.Claude = c
|
||||
log.Printf("[claude] explanations use %s", c.Model())
|
||||
} else {
|
||||
log.Printf("[claude] %v: explanations are off", err)
|
||||
}
|
||||
|
||||
rp, err := repo.New(repo.Config{
|
||||
Dir: filepath.Join(dataDir, "repo"),
|
||||
Remote: remote,
|
||||
|
||||
Reference in New Issue
Block a user