Check my work (third homework mode) - One model call with its own system prompt marks the student's own attempt: verdict, what's right, each issue with why + fix, and the corrected version that keeps everything the student got right. Unasked-for extras are never errors. The attempt is typed/pasted (photos of handwriting are documented as unsupported) and stored in the record so Redo re-checks the same work. - Server: mode "check" + solution on POST /api/homework (zod, 400 server.needSolution without one) and the same NDJSON stream; new checkHomework() pipeline with repair-once validation. Follow-ups - New POST /api/homework/refine (plain JSON, RATE_LIMIT_REFINE, zod, auth before body): shorter / more detail / explain, plain text back, language preserved. - Shorter + More detail + Original rewrite the finished answer and the corrected version in place (FollowUpBar); every guide step has Explain step N differently; failed calls say so without losing text. Turn into a study deck - Any result hands its content (answer, corrected version or assembled guide) to New deck through a one-shot sessionStorage seed with the options step pre-filled. UI - Phone step 2 gains the third card: tapping it reveals the solution field + submit; desktop has three cards and the solution field shows with the mode. Homework history labels and share/card previews cover the new mode. Verified: new phase-3 suite 20/20 against a stub Ollama (check payload, missing-solution 400, refine validation, phone + desktop check flows, shorter follow-up with restore, guide explain, deck hand-off) plus regression 74/74, mobile 52/52, declutter 116/116, wizard 80/80, animation 8/8, native 35/35, phase1 38/38, phase2 43/43, i18n 25/25. Key parity 734/734. Lint + build clean. SW freezmo-v27.
197 lines
6.2 KiB
TypeScript
197 lines
6.2 KiB
TypeScript
"use client";
|
|
|
|
/**
|
|
* Homework results backed by localStorage. Same tiny observable store as the
|
|
* deck library, but deliberately smaller: no folders, no attempts — just the
|
|
* assignment text (so “Make it again” works) and the result.
|
|
*
|
|
* Original photos/documents are never stored; they were reduced to text on the
|
|
* client before this point.
|
|
*/
|
|
|
|
import {
|
|
homeworkRecordSchema,
|
|
type HomeworkGuideRecord,
|
|
type HomeworkMode,
|
|
type HomeworkRecord,
|
|
type HomeworkStep,
|
|
} from "./schemas";
|
|
import { markSyncDirty } from "./sync-state";
|
|
import { uid } from "./utils";
|
|
|
|
const STORAGE_KEY = "freezmo.homework.v1";
|
|
const EMPTY: HomeworkRecord[] = [];
|
|
/** Enough to regenerate from, while staying well under the storage quota. */
|
|
export const MAX_STORED_SOURCE = 24_000;
|
|
|
|
type Listener = () => void;
|
|
|
|
let records: HomeworkRecord[] | null = null;
|
|
let cached: HomeworkRecord[] = EMPTY;
|
|
const listeners = new Set<Listener>();
|
|
|
|
function read(): HomeworkRecord[] {
|
|
if (typeof window === "undefined") return EMPTY;
|
|
try {
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) return EMPTY;
|
|
const parsed: unknown = JSON.parse(raw);
|
|
if (!Array.isArray(parsed)) return EMPTY;
|
|
const valid: HomeworkRecord[] = [];
|
|
for (const entry of parsed) {
|
|
const result = homeworkRecordSchema.safeParse(entry);
|
|
if (result.success) valid.push(result.data as HomeworkRecord);
|
|
else console.warn("[freezmo] skipping invalid homework record", result.error.issues);
|
|
}
|
|
return valid;
|
|
} catch (error) {
|
|
console.warn("[freezmo] could not read the homework history", error);
|
|
return EMPTY;
|
|
}
|
|
}
|
|
|
|
function ensureLoaded(): void {
|
|
if (records !== null) return;
|
|
records = read();
|
|
cached = records;
|
|
}
|
|
|
|
function write(next: HomeworkRecord[]): void {
|
|
records = next;
|
|
cached = next;
|
|
try {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(next));
|
|
} catch (error) {
|
|
console.warn("[freezmo] could not save the homework history (storage full?)", error);
|
|
}
|
|
markSyncDirty();
|
|
listeners.forEach((listener) => listener());
|
|
}
|
|
|
|
/** Used by backup import and sync: wholesale replacement of the history. */
|
|
export function replaceHomework(next: HomeworkRecord[]): void {
|
|
const valid: HomeworkRecord[] = [];
|
|
for (const entry of next) {
|
|
const result = homeworkRecordSchema.safeParse(entry);
|
|
if (result.success) valid.push(result.data as HomeworkRecord);
|
|
else console.warn("[freezmo] skipping invalid homework record in import", result.error.issues);
|
|
}
|
|
write(valid);
|
|
}
|
|
|
|
export const homeworkStore = {
|
|
subscribe(listener: Listener): () => void {
|
|
ensureLoaded();
|
|
listeners.add(listener);
|
|
const onStorage = (event: StorageEvent) => {
|
|
if (event.key === STORAGE_KEY) {
|
|
records = null;
|
|
ensureLoaded();
|
|
listeners.forEach((l) => l());
|
|
}
|
|
};
|
|
window.addEventListener("storage", onStorage);
|
|
return () => {
|
|
listeners.delete(listener);
|
|
window.removeEventListener("storage", onStorage);
|
|
};
|
|
},
|
|
getSnapshot(): HomeworkRecord[] {
|
|
ensureLoaded();
|
|
return cached;
|
|
},
|
|
getServerSnapshot(): HomeworkRecord[] {
|
|
return EMPTY;
|
|
},
|
|
};
|
|
|
|
/** Newest first. */
|
|
export function sortHomework(list: HomeworkRecord[]): HomeworkRecord[] {
|
|
return [...list].sort((a, b) => b.updatedAt - a.updatedAt);
|
|
}
|
|
|
|
export function getHomework(id: string): HomeworkRecord | undefined {
|
|
return homeworkStore.getSnapshot().find((record) => record.id === id);
|
|
}
|
|
|
|
/** Everything needed to store a result (the route's payload plus the source). */
|
|
export type NewHomeworkResult =
|
|
| { mode: "make"; title: string; language?: string; sourceText: string; answer: string }
|
|
| { mode: "guide"; title: string; language?: string; sourceText: string; steps: HomeworkStep[] }
|
|
| {
|
|
mode: "check";
|
|
title: string;
|
|
language?: string;
|
|
sourceText: string;
|
|
verdict: string;
|
|
correct: string[];
|
|
wrong: Array<{ issue: string; why: string; fix: string }>;
|
|
corrected: string;
|
|
attempt?: string;
|
|
};
|
|
|
|
function buildRecord(
|
|
base: { id: string; createdAt: number; updatedAt: number },
|
|
input: NewHomeworkResult,
|
|
): HomeworkRecord {
|
|
const shared = {
|
|
...base,
|
|
title: input.title.slice(0, 200),
|
|
language: input.language,
|
|
sourceText: input.sourceText.slice(0, MAX_STORED_SOURCE),
|
|
};
|
|
if (input.mode === "make") return { ...shared, mode: "make", answer: input.answer };
|
|
if (input.mode === "check") {
|
|
return {
|
|
...shared,
|
|
mode: "check",
|
|
verdict: input.verdict,
|
|
correct: input.correct,
|
|
wrong: input.wrong,
|
|
corrected: input.corrected,
|
|
attempt: input.attempt?.slice(0, 20_000),
|
|
};
|
|
}
|
|
return { ...shared, mode: "guide", steps: input.steps };
|
|
}
|
|
|
|
export function saveHomework(input: NewHomeworkResult): HomeworkRecord {
|
|
ensureLoaded();
|
|
const now = Date.now();
|
|
const record = buildRecord({ id: uid("hw"), createdAt: now, updatedAt: now }, input);
|
|
write([record, ...homeworkStore.getSnapshot()]);
|
|
return record;
|
|
}
|
|
|
|
/** “Make it again” keeps the same history row. */
|
|
export function updateHomework(id: string, input: NewHomeworkResult): HomeworkRecord | undefined {
|
|
ensureLoaded();
|
|
const existing = homeworkStore.getSnapshot().find((record) => record.id === id);
|
|
if (!existing) return undefined;
|
|
const updated = buildRecord(
|
|
{ id: existing.id, createdAt: existing.createdAt, updatedAt: Date.now() },
|
|
input,
|
|
);
|
|
write(homeworkStore.getSnapshot().map((record) => (record.id === id ? updated : record)));
|
|
return updated;
|
|
}
|
|
|
|
export function deleteHomework(id: string): void {
|
|
ensureLoaded();
|
|
write(homeworkStore.getSnapshot().filter((record) => record.id !== id));
|
|
}
|
|
|
|
/** The final screen's text: the step answers assembled into one solution. */
|
|
export function assembleSolution(record: HomeworkGuideRecord): string {
|
|
const parts = record.steps.map((step, index) => `${index + 1}. ${step.title}\n${step.answer}`);
|
|
return [record.title, ...parts].join("\n\n");
|
|
}
|
|
|
|
/** Label for a row in the history list. */
|
|
export function homeworkModeLabelKey(mode: HomeworkMode): string {
|
|
if (mode === "make") return "homework.history.made";
|
|
if (mode === "check") return "homework.history.checked";
|
|
return "homework.history.guide";
|
|
}
|
|
|
|
export type { HomeworkGuideRecord };
|