Explain concepts on the site, and reuse their code from the CLI
Test and publish the site / test (push) Successful in 1m19s
Test and publish the site / publish (push) Successful in 1m31s

/usaco/concepts is a library of techniques Claude explained: the admin
asks by topic ("help explain fenwick trees") or pastes code they could
not follow, and gets an intuitive explanation with SVG diagrams plus a
C++ snippet in contest style. Given code, the walkthrough traces that
code and the snippet is a general version of it. Anyone can read the
library; generating, rewriting and deleting are admin-only.

Each concept is committed as concepts/<slug>/: concept.md (tags in a
header comment, then a # title), snippet.cpp (first line
"// concept: <slug>") and request.md (the question, for rewrites). The
slug is Claude's to pick, so a new concept's job is keyed by an id and
returns {slug} as its result; a taken slug gets -2, -3... rather than
overwriting. Rewriting starts from the saved question and version plus
a note on what to change. The index reads concepts into Index.Concepts,
with their date from git.

internal/concept shares explain's math and diagram rules, now split out
as explain.MathRules and explain.DiagramRules; the explain prompt is
byte-for-byte unchanged. Code fences in explanations are highlighted
with the same highlight.js setup as CodeBlock (web/src/highlight.js).

`usaco snippet` lists concepts, prints one's snippet, or pastes it into
a solution above main (or class Solution), keeping a .bak and refusing
a concept the file already has. In interactive mode it pastes into the
selection.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
2026-10-20 12:58:38 +01:00
co-authored by Claude Opus 5.5
parent d4849859df
commit ccf4e4dd74
25 changed files with 1750 additions and 33 deletions
+226
View File
@@ -0,0 +1,226 @@
<script setup>
/**
* One concept: the snippet to reuse first, since that is what a return
* visit is usually for, then the explanation folded into sections like a
* problem's. The admin can have it rewritten, with a note on what to
* change, or forget it.
*/
import { onUnmounted, ref, watch } from "vue";
import { RouterLink, useRouter } from "vue-router";
import { deleteConcept, getConcept, getMe, rewriteConcept, rewriteStatus } from "@/api";
import { formatDate } from "@/format";
import CodeBlock from "@/components/CodeBlock.vue";
import Explanation from "@/components/Explanation.vue";
const props = defineProps({ slug: { type: String, required: true } });
const router = useRouter();
const concept = ref(null);
const admin = ref(false);
const loading = ref(true);
const error = ref("");
const panel = ref("");
const note = ref("");
const job = ref({ state: "idle" });
const actionError = ref("");
let poll = null;
function stopPolling() {
clearTimeout(poll);
poll = null;
}
onUnmounted(stopPolling);
async function load() {
stopPolling();
loading.value = true;
error.value = "";
panel.value = "";
job.value = { state: "idle" };
try {
const [c, me] = await Promise.all([getConcept(props.slug), getMe().catch(() => ({ admin: false }))]);
concept.value = c;
admin.value = me.admin;
// A rewrite started before a reload is still worth waiting for.
if (me.admin) checkRewrite();
} catch (e) {
error.value = e.message;
} finally {
loading.value = false;
}
}
watch(() => props.slug, load, { immediate: true });
async function checkRewrite() {
stopPolling();
const slug = props.slug;
try {
const s = await rewriteStatus(slug);
if (slug !== props.slug) return;
const finished = job.value.state === "running" && s.state === "done";
job.value = s;
if (s.state === "running") {
poll = setTimeout(checkRewrite, 3000);
} else if (finished) {
concept.value = await getConcept(slug);
}
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function rewrite() {
try {
job.value = await rewriteConcept(props.slug, { note: note.value });
panel.value = "";
note.value = "";
poll = setTimeout(checkRewrite, 3000);
} catch (e) {
job.value = { state: "error", error: e.message };
}
}
async function forget() {
if (!confirm(`Delete “${concept.value.title}” and its snippet?`)) return;
actionError.value = "";
try {
await deleteConcept(props.slug);
router.push({ name: "concepts" });
} catch (e) {
actionError.value = e.message;
}
}
</script>
<template>
<div class="page">
<RouterLink to="/concepts" class="back">← All concepts</RouterLink>
<p v-if="loading && !concept" class="muted">Loading…</p>
<p v-else-if="error" class="error">{{ error }}</p>
<template v-else-if="concept">
<header>
<h1>{{ concept.title }}</h1>
<div class="facts">
<span v-for="t in concept.tags" :key="t" class="badge">{{ t }}</span>
<time v-if="concept.updated" class="muted" :datetime="concept.updated">
Updated {{ formatDate(concept.updated) }}
</time>
</div>
</header>
<section v-if="admin" class="admin">
<div class="admin-row">
<button class="btn" :disabled="job.state === 'running'" @click="panel = panel === 'rewrite' ? '' : 'rewrite'">
{{ job.state === "running" ? "Claude is rewriting…" : "Rewrite" }}
</button>
<button class="btn" :disabled="job.state === 'running'" @click="forget">Delete</button>
</div>
<div v-if="panel === 'rewrite'" class="panel">
<p class="muted">
Claude writes it again from the same question, starting from this version. Say what to change, or leave it
empty for a fresh take.
</p>
<textarea v-model="note" rows="3" placeholder="e.g. step 3's diagram is wrong; add range updates"></textarea>
<div class="admin-row">
<button class="btn primary" @click="rewrite">Rewrite</button>
<button class="btn" @click="panel = ''">Cancel</button>
</div>
</div>
<p v-if="job.state === 'error'" class="error">Rewrite: {{ job.error }}</p>
<p v-if="actionError" class="error">{{ actionError }}</p>
</section>
<section v-if="concept.snippet">
<h2>Code</h2>
<p class="filename muted">snippet.cpp</p>
<CodeBlock :content="concept.snippet.content" lang="cpp" :truncated="concept.snippet.truncated" />
<p class="cli muted">
From the CLI: <code>usaco snippet {{ concept.slug }} &lt;problem&gt;</code> pastes it above
<code>main</code>.
</p>
</section>
<section v-if="concept.content">
<h2>Explanation</h2>
<Explanation :source="concept.content" :open="['Intuition']" />
</section>
</template>
</div>
</template>
<style scoped>
.page {
display: grid;
gap: 24px;
min-width: 0;
}
.page > section {
min-width: 0;
}
.back {
font-size: 0.875rem;
color: var(--color-ink-muted);
text-decoration: none;
}
.back:hover {
color: var(--color-ink);
}
h1 {
font-size: 2.75rem;
margin: 0 0 10px;
overflow-wrap: anywhere;
}
h2 {
font-size: 1.6rem;
margin-bottom: 8px;
}
.facts {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 6px 10px;
font-size: 0.9rem;
}
.admin {
display: grid;
gap: 10px;
}
.admin-row {
display: flex;
flex-wrap: wrap;
align-items: center;
gap: 8px;
}
.panel {
display: grid;
gap: 10px;
}
.panel p {
margin: 0;
font-size: 0.9rem;
}
textarea {
padding: 8px 10px;
border: 1px solid var(--color-line);
border-radius: 4px;
font-family: var(--font-mono);
font-size: 0.85rem;
color: var(--color-ink);
background: var(--color-paper);
}
.filename {
margin: 0 0 4px;
font-family: var(--font-mono);
font-size: 0.8rem;
}
.cli {
margin: 8px 0 0;
font-size: 0.85rem;
}
.cli code {
font-family: var(--font-mono);
}
</style>