mirror of
https://github.com/bitbaum/heidi.git
synced 2026-12-04 17:57:11 +01:00
* feat(course): every stored record names its course, and today's progress is the Zurich course's A course registry (lib/course/registry.ts) lists the one course there is; lib/variety/active.ts resolves its pack. Browser stores that hold course progress move to an envelope keyed by course under .v2 keys, and a value under the old .v1 key is read as the default course without being rewritten. progress_devices, certificates and entitlements gain a course column that defaults to the Zurich course. Item ids are unique across courses, and the default course's ids are unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * refactor(variety): the pack declares its articles, persons, respellings, voices and prompt lines Article, FormLabel and VocabularyGroup were closed unions of the Zurich values, the answer comparison folded Zurich respellings for every language, word distance lower-cased as German, the voice ranking named two German tags and the prompt builder carried Swiss spelling advice. Each is now a field of the pack, read by the engine; a contract test over every pack refuses a value the pack did not declare, and the leak test also looks for the taught language's own words in engine code. The Ukrainian pack gains one verb with its own pronouns and no articles. The 2,956 published items and the system prompt are byte-identical. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * refactor(practice): an item's level comes from a source the course chooses A course entered from a sibling language measures each item's distance from that language, exactly as before; a course entered from zero reads a declared CEFR band on the word, the line or the topic. levels.ts exposes both as LevelSource and picks by Course.entry. Every published item keeps its level. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * feat(meaning): a meaning can be shown in the learner's language A meaning is the bridge form plus an optional gloss in the interface language. One pure rule (lib/domain/meaning/meaning.ts) decides what is printed from the learner's answer to «Wie gut ist dein Deutsch?»: good shows German only, as before and by default; some shows German and the gloss; none shows the gloss; without a gloss German is always shown. Every exercise, the word list and the scene pages print meanings through one component. The answer is a course store, set in the settings and asked once after the start card, only in an interface language that has glosses. Gloss sets are one file per course and language, loaded on demand. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * content(glosses): English and Russian meanings for every word and scene line; French and Italian for every word Machine drafts from the Zurich form and its German meaning together, marked unreviewed in the data. One file per language beside the pack, keyed by the German the course gives. Tests refuse a key no entry carries, an empty gloss and two glosses for one German form; pnpm run glosses:coverage prints what is covered. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * content(glosses): French and Italian meanings for every scene line Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * docs: courses, pack-declared forms, level sources and glosses, as they now are Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T * refactor(variety): the pack says which grammatical person each of its labels is The gloss check and the prompt's auxiliary table, both new on main, matched the Zurich labels «ich» and «er» by name. The forms pass of the leak test caught it on rebase. The pack now declares personOf, and both read it; the system prompt and the published items are unchanged. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_019MDtmvb6xi489TfZMg7n9T --------- Co-authored-by: Claude <noreply@anthropic.com>
145 lines
5.6 KiB
TypeScript
145 lines
5.6 KiB
TypeScript
import { and, desc, eq, inArray, max, ne, or, sql } from "drizzle-orm";
|
|
import { db } from "../../db/index.ts";
|
|
import { certificates, progressDevices } from "../../db/schema.ts";
|
|
import { decodeModel, type LearnerModel } from "../practice/model.ts";
|
|
import type { SituationStrength } from "../practice/situation-strength.ts";
|
|
import { LEARNER_KEYS, SYNC_KEYS, type SyncKey } from "./sync.ts";
|
|
import type { Course } from "../../course/registry.ts";
|
|
|
|
export type DeviceRecords = {
|
|
device: string;
|
|
values: Partial<Record<SyncKey, unknown>>;
|
|
updatedAt: Partial<Record<SyncKey, string>>;
|
|
};
|
|
|
|
/** This learner's OTHER devices, each with its records. */
|
|
export async function otherDevices(actorId: string, deviceId: string, course: Course): Promise<DeviceRecords[]> {
|
|
const rows = await db
|
|
.select()
|
|
.from(progressDevices)
|
|
.where(
|
|
and(
|
|
eq(progressDevices.actorId, actorId),
|
|
ne(progressDevices.deviceId, deviceId),
|
|
or(eq(progressDevices.course, course.id), inArray(progressDevices.key, [...LEARNER_KEYS])),
|
|
),
|
|
);
|
|
const byDevice = new Map<string, DeviceRecords>();
|
|
for (const r of rows) {
|
|
const key = r.key as SyncKey;
|
|
if (!SYNC_KEYS.includes(key)) continue;
|
|
const d = byDevice.get(r.deviceId) ?? { device: r.deviceId, values: {}, updatedAt: {} };
|
|
d.values[key] = r.value;
|
|
d.updatedAt[key] = r.updatedAt.toISOString();
|
|
byDevice.set(r.deviceId, d);
|
|
}
|
|
return [...byDevice.values()];
|
|
}
|
|
|
|
/**
|
|
* Store this device's records. A value that has not changed keeps its
|
|
* `updated_at` — which is what lets another device tell a saved list edited
|
|
* here from one merely pushed again.
|
|
*/
|
|
export async function putRecords(
|
|
actorId: string,
|
|
deviceId: string,
|
|
course: Course,
|
|
values: Partial<Record<SyncKey, unknown>>,
|
|
): Promise<void> {
|
|
for (const [key, value] of Object.entries(values)) {
|
|
await db
|
|
.insert(progressDevices)
|
|
.values({ actorId, deviceId, key, value, course: course.id })
|
|
.onConflictDoUpdate({
|
|
target: [progressDevices.actorId, progressDevices.deviceId, progressDevices.key],
|
|
set: {
|
|
value,
|
|
course: course.id,
|
|
updatedAt: sql`CASE WHEN ${progressDevices.value} = excluded.value THEN ${progressDevices.updatedAt} ELSE now() END`,
|
|
},
|
|
});
|
|
}
|
|
await keepNewestDevices(actorId, deviceId);
|
|
}
|
|
|
|
/**
|
|
* How many devices one learner's progress is kept for. A phone, a laptop, a
|
|
* work machine and a few reinstalls fit with room. Without a ceiling a new
|
|
* random device id per request stored another five records each time, and
|
|
* every read (`otherDevices`, `deviceModels`, a team's overview) loaded all
|
|
* of them.
|
|
*
|
|
* Past it, the device that has gone longest without a change loses its server
|
|
* copy — never the one writing now. Nothing is refused, and nothing is lost
|
|
* that the device itself does not still hold: it syncs back if it returns.
|
|
*/
|
|
export const MAX_DEVICES = 10;
|
|
|
|
async function keepNewestDevices(actorId: string, current: string): Promise<void> {
|
|
const devices = await db
|
|
.select({ deviceId: progressDevices.deviceId, last: max(progressDevices.updatedAt) })
|
|
.from(progressDevices)
|
|
.where(eq(progressDevices.actorId, actorId))
|
|
.groupBy(progressDevices.deviceId)
|
|
.orderBy(desc(max(progressDevices.updatedAt)));
|
|
if (devices.length <= MAX_DEVICES) return;
|
|
const keep = new Set([current, ...devices.filter((d) => d.deviceId !== current).slice(0, MAX_DEVICES - 1).map((d) => d.deviceId)]);
|
|
const stale = devices.map((d) => d.deviceId).filter((id) => !keep.has(id));
|
|
if (stale.length === 0) return;
|
|
await db.delete(progressDevices).where(and(eq(progressDevices.actorId, actorId), inArray(progressDevices.deviceId, stale)));
|
|
}
|
|
|
|
/** Turning sync off on one device, or "delete everything synced" with no device. */
|
|
export async function forget(actorId: string, deviceId?: string): Promise<void> {
|
|
await db
|
|
.delete(progressDevices)
|
|
.where(deviceId ? and(eq(progressDevices.actorId, actorId), eq(progressDevices.deviceId, deviceId)) : eq(progressDevices.actorId, actorId));
|
|
}
|
|
|
|
/** Every device's learner model for this learner — the certificate's evidence. */
|
|
export async function deviceModels(actorId: string, course: Course): Promise<LearnerModel[]> {
|
|
const rows = await db
|
|
.select({ value: progressDevices.value })
|
|
.from(progressDevices)
|
|
.where(
|
|
and(eq(progressDevices.actorId, actorId), eq(progressDevices.key, "model"), eq(progressDevices.course, course.id)),
|
|
);
|
|
return rows.map((r) => decodeModel(JSON.stringify(r.value)));
|
|
}
|
|
|
|
export type Certificate = typeof certificates.$inferSelect;
|
|
|
|
/**
|
|
* Issue a certificate, or return the one already issued for this situation at
|
|
* this size — asking twice is one certificate, and a scene that has grown
|
|
* since gets a new one that says so.
|
|
*/
|
|
export async function issueCertificate(actorId: string, course: Course, s: SituationStrength): Promise<Certificate> {
|
|
const [existing] = await db
|
|
.select()
|
|
.from(certificates)
|
|
.where(
|
|
and(
|
|
eq(certificates.actorId, actorId),
|
|
eq(certificates.course, course.id),
|
|
eq(certificates.sceneId, s.scene),
|
|
eq(certificates.askable, s.askable),
|
|
),
|
|
)
|
|
.orderBy(desc(certificates.issuedAt))
|
|
.limit(1);
|
|
if (existing) return existing;
|
|
const [row] = await db
|
|
.insert(certificates)
|
|
.values({ actorId, course: course.id, sceneId: s.scene, askable: s.askable, held: s.held, stuck: s.stuck })
|
|
.returning();
|
|
return row;
|
|
}
|
|
|
|
export async function certificateById(id: string): Promise<Certificate | null> {
|
|
if (!/^[0-9a-f-]{36}$/i.test(id)) return null;
|
|
const [row] = await db.select().from(certificates).where(eq(certificates.id, id));
|
|
return row ?? null;
|
|
}
|