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 @@
|
||||
// Package handlers is the site's REST API: the problem list and pages (public
|
||||
// for solved problems, everything for the admin), the admin's edits, and the
|
||||
// Gitea webhook that tells the server to pull.
|
||||
// for solved problems, everything for the admin), the admin's edits, the
|
||||
// Claude explanations (explain.go), and the Gitea webhook that tells the
|
||||
// server to pull.
|
||||
package handlers
|
||||
|
||||
import (
|
||||
@@ -24,6 +25,7 @@ import (
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"adam-french.co.uk/usaco-site/internal/auth"
|
||||
"adam-french.co.uk/usaco-site/internal/explain"
|
||||
"adam-french.co.uk/usaco-site/internal/index"
|
||||
"adam-french.co.uk/usaco-site/internal/repo"
|
||||
)
|
||||
@@ -41,8 +43,15 @@ type Server struct {
|
||||
// Pull asks for a sync. It must not block: the webhook answers at once
|
||||
// and the sync runs in the background.
|
||||
Pull func()
|
||||
// Claude writes the explanations; nil when CLAUDE_API_KEY is not set,
|
||||
// which turns only that feature off.
|
||||
Claude explain.Asker
|
||||
// Statements reads a statement from the problem's judge; nil means the
|
||||
// real judges.
|
||||
Statements Fetcher
|
||||
|
||||
idx atomic.Pointer[index.Index]
|
||||
idx atomic.Pointer[index.Index]
|
||||
jobs explain.Jobs
|
||||
}
|
||||
|
||||
// SetIndex swaps in a freshly built index. repo.Repo calls it (through
|
||||
@@ -73,6 +82,9 @@ func (s *Server) Routes(api *gin.RouterGroup) {
|
||||
api.GET("/problems/:name", s.get)
|
||||
admin.PUT("/problems/:name/meta", s.updateMeta)
|
||||
admin.POST("/problems/:name/status", s.setStatus)
|
||||
admin.GET("/problems/:name/statement/fetch", s.fetchStatement)
|
||||
admin.POST("/problems/:name/explain", s.startExplain)
|
||||
admin.GET("/problems/:name/explain", s.explainStatus)
|
||||
api.POST("/hooks/gitea", s.webhook)
|
||||
}
|
||||
|
||||
@@ -144,9 +156,20 @@ func (s *Server) lookup(ctx *gin.Context, admin bool) (*index.Problem, bool) {
|
||||
}
|
||||
|
||||
func (s *Server) get(ctx *gin.Context) {
|
||||
if p, ok := s.lookup(ctx, s.isAdmin(ctx)); ok {
|
||||
ctx.JSON(http.StatusOK, p)
|
||||
admin := s.isAdmin(ctx)
|
||||
p, ok := s.lookup(ctx, admin)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
// The statement is the judge's text, kept for the admin and for Claude;
|
||||
// the public page links to the judge instead. The index is shared, so
|
||||
// this blanks a copy.
|
||||
if !admin && p.Statement != "" {
|
||||
c := *p
|
||||
c.Statement = ""
|
||||
p = &c
|
||||
}
|
||||
ctx.JSON(http.StatusOK, p)
|
||||
}
|
||||
|
||||
// requestError is an edit that cannot be made, with the status to send.
|
||||
@@ -194,8 +217,9 @@ func findOnDisk(dir, name string) (status string, err error) {
|
||||
}
|
||||
|
||||
type metaBody struct {
|
||||
URL *string `json:"url"`
|
||||
Notes *string `json:"notes"`
|
||||
URL *string `json:"url"`
|
||||
Notes *string `json:"notes"`
|
||||
Statement *string `json:"statement"`
|
||||
}
|
||||
|
||||
const maxNotes = 64 << 10
|
||||
@@ -225,6 +249,10 @@ func (s *Server) updateMeta(ctx *gin.Context) {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "notes are too long"})
|
||||
return
|
||||
}
|
||||
if body.Statement != nil && len(*body.Statement) > maxNotes {
|
||||
ctx.JSON(http.StatusBadRequest, gin.H{"error": "the statement is too long"})
|
||||
return
|
||||
}
|
||||
|
||||
name := p.Name
|
||||
s.mutate(ctx, name, "Update "+name, func(dir string) error {
|
||||
@@ -244,6 +272,11 @@ func (s *Server) updateMeta(ctx *gin.Context) {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if body.Statement != nil {
|
||||
if err := writeOrRemove(filepath.Join(pdir, "problem.md"), *body.Statement); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user