Files
usaco/site/internal/explain/jobs.go
T
adamfandClaude Opus 5.5 995b44a57a
Test and publish the site / publish (push) Successful in 8m15s
Test and publish the site / test (push) Successful in 5m51s
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]>
2026-10-19 20:38:15 +01:00

70 lines
1.6 KiB
Go

package explain
import (
"sync"
"time"
)
// A job's states. Idle means nothing has run since the server started.
const (
Idle = "idle"
Running = "running"
Done = "done"
Failed = "error"
)
// Status is what the page polls for.
type Status struct {
State string `json:"state"`
Error string `json:"error,omitempty"`
Started *time.Time `json:"started,omitempty"`
Finished *time.Time `json:"finished,omitempty"`
}
// Jobs runs at most one explanation per problem at a time. An explanation
// takes a minute or more, longer than nginx waits for a response, so the
// request only starts it and the page polls. State is in memory: a restart
// mid-job loses the job, and nothing is committed until it finishes.
type Jobs struct {
mu sync.Mutex
m map[string]*Status
}
// Start runs fn in the background unless a job for name is running, and
// reports whether it started. fn's error is shown to the admin, so it must
// be a message meant for them.
func (j *Jobs) Start(name string, fn func() error) bool {
j.mu.Lock()
defer j.mu.Unlock()
if j.m == nil {
j.m = map[string]*Status{}
}
if s := j.m[name]; s != nil && s.State == Running {
return false
}
now := time.Now()
j.m[name] = &Status{State: Running, Started: &now}
go func() {
err := fn()
j.mu.Lock()
defer j.mu.Unlock()
end := time.Now()
s := &Status{State: Done, Started: &now, Finished: &end}
if err != nil {
s.State, s.Error = Failed, err.Error()
}
j.m[name] = s
}()
return true
}
// Get is name's latest job.
func (j *Jobs) Get(name string) Status {
j.mu.Lock()
defer j.mu.Unlock()
if s := j.m[name]; s != nil {
return *s
}
return Status{State: Idle}
}