import { React, html, useEffect, useMemo, useState } from "./runtime.js"; import { useApp } from "./store.js"; import { useEngine } from "./engine.js"; import { useCloud, money } from "./cloud.js"; import { Notice, Pill, FeedbackPanel, Icon, Mark, usePersistent, download, SECTIONS } from "./components.js"; import { CREDIT_COSTS, PARAGRAPH_RUBRIC, SUGGESTED_QUESTIONS, TEXTS, reviewText, wordCount } from "./marking.js"; function PageHead({ kicker, title, subtitle, right }) { return html`
${kicker}

${title}

${subtitle && html`

${subtitle}

`}
${right}
`; } // --------------------------------------------------------------------------- // Model answers // --------------------------------------------------------------------------- export function ModelAnswers() { const app = useApp(); const engine = useEngine(); const [textId, setTextId] = usePersistent("explication.modelAnswer.textId", "macbeth"); const [question, setQuestion] = usePersistent("explication.modelAnswer.question", "How does Shakespeare present ambition in Macbeth?"); const [answer, setAnswer] = usePersistent("explication.modelAnswer.answer", ""); const [busy, setBusy] = useState(false); const [error, setError] = useState(""); const text = TEXTS.find((t) => t.id === textId) || TEXTS[0]; const generate = async () => { setError(""); if (!engine.canGenerate) { setError("A model answer needs a model. Open Settings to load one or connect an endpoint."); return; } if (app.credits < CREDIT_COSTS.modelAnswer) { setError("This costs " + CREDIT_COSTS.modelAnswer + " credits. Mark a paragraph to earn more."); return; } app.spend(CREDIT_COSTS.modelAnswer); setBusy(true); setAnswer(""); try { const result = await engine.chat( [ { role: "system", content: [ "You are an experienced GCSE English Literature examiner writing a model answer for study.", "Write one developed paragraph, 150-220 words, in British English, at grade 8-9 standard.", "Use one short embedded quotation from the named text; never invent a quotation.", "Name the method, explain the effect, and finish by returning to the question.", "Return only the paragraph." ].join(" ") }, { role: "user", content: `Text: ${text.title}\nQuestion: ${question}` } ], setAnswer ); if (result && result.trim()) setAnswer(result.trim()); } catch (err) { setError(err && err.message ? err.message : "The model answer could not be written."); app.addCredits(CREDIT_COSTS.modelAnswer); } setBusy(false); }; return html`
<${PageHead} kicker="Model answers" title="See what a strong answer looks like." subtitle="A model answer is a worked example, not your work. It is always labelled, and it costs credits because a model writes it." right=${html`<${Pill} tone="warn">${CREDIT_COSTS.modelAnswer} credits`} />
setQuestion(e.target.value)} /> ${SUGGESTED_QUESTIONS.map((q) => html``)}
${!engine.canGenerate && html`
<${Notice} kind="warn">No model is connected. Open Settings to download a Paragraph model or add an endpoint.
`} ${error && html`
<${Notice} kind="error">${error}
`}
${app.credits} credits available
${answer && html`
AI-written model answer ${wordCount(answer)} words

${answer}

Study material only — do not submit this as your own work.
`}
`; } // --------------------------------------------------------------------------- // Mock exam // --------------------------------------------------------------------------- const EXAM_SECONDS = 25 * 60; export function MockExam() { const app = useApp(); const [textId, setTextId] = usePersistent("explication.mock.textId", "macbeth"); const [question, setQuestion] = usePersistent("explication.mock.question", "How does Shakespeare present ambition in Macbeth?"); const [extract, setExtract] = usePersistent("explication.mock.extract", ""); const [answer, setAnswer] = usePersistent("explication.mock.answer", ""); const [remaining, setRemaining] = useState(EXAM_SECONDS); const [running, setRunning] = useState(false); const [feedback, setFeedback] = useState(null); const [error, setError] = useState(""); const text = TEXTS.find((t) => t.id === textId) || TEXTS[0]; useEffect(() => { if (!running) return; const timer = setInterval(() => { setRemaining((r) => { if (r <= 1) { setRunning(false); return 0; } return r - 1; }); }, 1000); return () => clearInterval(timer); }, [running]); const time = `${String(Math.floor(remaining / 60)).padStart(2, "0")}:${String(remaining % 60).padStart(2, "0")}`; const mark = () => { setError(""); if (wordCount(answer) < 20) { setError("Write at least a couple of sentences before marking."); return; } if (app.credits < CREDIT_COSTS.mockExam) { setError("A marked mock exam costs " + CREDIT_COSTS.mockExam + " credits. Mark a paragraph to earn more."); return; } app.spend(CREDIT_COSTS.mockExam); setRunning(false); const result = reviewText({ text: answer, question, extract, rubric: PARAGRAPH_RUBRIC, title: text.title }); setFeedback(result); app.recordDraft({ mode: "Mock exam", title: text.title, question, extract, text: answer, score: result.total, max: result.max, feedback: result, summary: "Timed mock exam" }); }; return html`
<${PageHead} kicker="Mock exam" title="Sit the paper, then mark it." subtitle="Twenty-five minutes, one question, one paragraph. Marking costs credits, exactly as it does in the app." right=${html`${time}`} />
setQuestion(e.target.value)} /> ${SUGGESTED_QUESTIONS.map((q) => html``)}
25 minutes
${wordCount(answer)} words
${error && html`
<${Notice} kind="error">${error}
`}
${app.credits} credits · marking costs ${CREDIT_COSTS.mockExam}
${feedback && html`
<${FeedbackPanel} feedback=${feedback} />`}
`; } // --------------------------------------------------------------------------- // Process log // --------------------------------------------------------------------------- export function ProcessLog() { const app = useApp(); const [open, setOpen] = useState(null); const [confirmClear, setConfirmClear] = useState(false); const drafts = app.drafts; const exportMarkdown = () => { const lines = ["# My writing process — Explication", "", `Exported ${new Date().toLocaleString("en-GB")}`, ""]; for (const d of [...drafts].reverse()) { lines.push(`## ${d.title || "Untitled"} — ${d.score}/${d.max}`); lines.push(`_${new Date(d.at).toLocaleDateString("en-GB")} · ${d.mode}${d.question ? " · " + d.question : ""}_`); lines.push(""); lines.push(d.text); if (d.feedback && d.feedback.nextStep) { lines.push(""); lines.push(`**Next step:** ${d.feedback.nextStep}`); } lines.push(""); } lines.push("Exported from Explication. Nothing left this browser."); download("explication-writing-process.md", lines.join("\n")); }; const exportJson = () => { download("explication-data.json", JSON.stringify({ settings: app.settings, drafts, credits: app.credits, totalEarned: app.totalEarned, days: app.days }, null, 2), "application/json"); }; return html`
<${PageHead} kicker="Process log" title="How the writing has changed." subtitle="Every marked piece, kept privately in this browser. Read the next steps again before your next attempt." right=${html``} />
Marked
${drafts.length}
Pieces
Average
${app.average}%
Across recent work
Earned
${app.totalEarned}
Credits in total

History

${drafts.length > 0 && html``}
${confirmClear && html`
Delete all ${drafts.length} marked pieces? This cannot be undone.
`}
${drafts.length === 0 ? html`
<${Mark} size=${34} />
Mark your first paragraph and it will appear here.
` : html`
${drafts.map( (d) => html`
${open === d.id && html`
${d.text}
${d.feedback && d.feedback.nextStep && html`
Next step. ${d.feedback.nextStep}
`}
`}
` )}
`}
Your writing never leaves this browser.
`; } // --------------------------------------------------------------------------- // Settings // --------------------------------------------------------------------------- export function Settings() { const app = useApp(); const engine = useEngine(); const cloud = useCloud(); const [baseUrl, setBaseUrl] = useState(engine.endpoint.baseUrl); const [endpointModel, setEndpointModel] = useState(engine.endpoint.model); const [key, setKey] = useState(engine.endpoint.key); const [saved, setSaved] = useState(false); const [confirmReset, setConfirmReset] = useState(false); const saveEndpoint = () => { engine.setEndpoint({ baseUrl, model: endpointModel, key }); setSaved(true); setTimeout(() => setSaved(false), 2000); }; return html`
<${PageHead} kicker="Settings" title="How Explication works here." subtitle="Your name, your exam board and the model behind the feedback. Everything is stored locally in this browser." />

You

app.setSettings({ name: e.target.value })} placeholder="What should the coach call you?" />

Marking

Rule-based review
Paragraph, essay, poetry, improve and mock-exam marking all run here: the rubric, quotation checking and the next-step guidance work with no model and no network.
<${Pill} tone="green">Always on
Paragraph models offline
The real Paragraph family, published by quantilimited. Download a model once and run it in the Mac or iOS app, or any GGUF runner. The weights are not loaded in this browser tab.
<${Pill} tone="green">${cloud.models.filter((m) => m.enabled !== false).length} models
${cloud.models .filter((m) => m.enabled !== false) .map((m) => { const allowed = cloud.canDownload(m.min_tier); return html`
${m.name}
Tier ${m.min_tier}
${m.base_model || m.model_name}${m.params ? " · " + m.params : ""}
${m.download_size}${m.format ? " · " + m.format : ""}${m.essays ? " · " + m.essays + " essays" : ""}

${m.note}

${m.card_url && html`Model card`} ${m.download_url ? allowed ? html`<${Icon} name="download" size=${12} /> Download` : html`<${Icon} name="lock" size=${12} /> Included in a plan · See plans` : html`Safetensors only — no GGUF build yet`}
`; })}
Your OpenAI-compatible endpoint
Optional. Bring any endpoint that speaks POST /chat/completions. The key is kept in this browser only and sent only to that URL.
setBaseUrl(e.target.value)} placeholder="http://127.0.0.1:11434/v1" />
setEndpointModel(e.target.value)} placeholder="qwen2.5:7b" />
setKey(e.target.value)} placeholder="sk-…" autocomplete="off" />
${engine.hasEndpoint ? "Connected: " + engine.endpoint.model : "No endpoint connected yet."}

Hosted models

See plans
${!cloud.configured ? html`
Not connected
Hosted models run on the site owner's API keys and are metered against a plan's budget. Connect Supabase and choose a plan to use them.
Connect Supabase
` : !cloud.user ? html`
Sign in to use hosted models
Your plan's included budget is attached to your account.
Sign in
` : !cloud.plan ? html`
No plan on this account
A subscription includes hosted model usage. You can keep using your own key or the free offline model.
Choose a plan
` : html`
${cloud.plan.name}
${cloud.budget.limit ? Math.max(0, Math.round((cloud.budget.remaining / cloud.budget.limit) * 100)) + "% of your included AI left this period" : "No hosted AI included"}
Manage
${cloud.budget.limit > 0 && html`
${engine.hostedReady ? "In use when no personal endpoint is set." : ""}
`}
`}

Data

Export
Download your drafts, settings and credits as JSON.
Reset progress
Clears drafts, credits and streaks. Settings are kept.
${confirmReset ? html`` : html``}

About

<${Mark} size=${40} />
Explication for the web
Version 1.0 · GCSE English Literature · British English

The same rubric, credit economy, quotation checking and process log as the Mac and iOS app. On the web the marking runs as a deterministic review; connect a model for chat, model answers and rewritten suggestions. No account, no analytics, no uploads.

`; }