SPB Git

Repository Public

schema.ts 16.2 KB 351 lines typescript

1/**2 * Canonical ATLAS IA course manifest schema (zod). The manifest is produced by the importer3 * (scripts/import-course.ts) from the curriculum repository merged with the editorial layer4 * (content/publication.json). Nothing in here is trusted until it has been parsed by `Manifest`.5 */6import { z } from "zod";78export const PUBLICATION_STATES = ["planned", "draft", "preview", "published", "archived"] as const;9export const PublicationState = z.enum(PUBLICATION_STATES);10export type PublicationState = z.infer<typeof PublicationState>;1112/** private: never shown · preview: only in the bannered preview mode · public: shown to everyone. */13export const Visibility = z.enum(["private", "preview", "public"]);14export type Visibility = z.infer<typeof Visibility>;1516export const LEVELS = ["débutant", "intermédiaire", "avancé"] as const;17export const Level = z.enum(LEVELS);18export type Level = z.infer<typeof Level>;1920/** A path relative to a storage root: no absolute path, no "..", no backslash, no NUL, no empty segment. */21export const SafeRelPath = z22  .string()23  .min(1)24  .max(300)25  .refine((p) => isSafeRelPath(p), "chemin relatif non sûr");2627export function isSafeRelPath(p: string): boolean {28  if (!p || p.length > 300 || p.includes("\0") || p.includes("\\") || p.startsWith("/")) return false;29  return p.split("/").every((seg) => seg !== "" && seg !== "." && seg !== ".." && /^[A-Za-z0-9._-]+$/.test(seg));30}3132const Sha256 = z.string().regex(/^[a-f0-9]{64}$/);33const SessionId = z.string().regex(/^S\d{2,3}$/);34const Slug = z.string().regex(/^[a-z0-9]+(?:-[a-z0-9]+)*$/).max(120);3536export const SECTION_KEYS = ["quiz", "glossary", "figures", "lab", "script", "bibliography", "slides", "lesson"] as const;37export const SectionKey = z.enum(SECTION_KEYS);38export type SectionKey = z.infer<typeof SectionKey>;3940/** A piece of imported session content (a JSON or Markdown file under content/import/). */41export const SectionRef = z.object({42  path: SafeRelPath,43  sha256: Sha256,44  visibility: Visibility,45  /** Number of items (questions, terms, figures…), when the notion applies. Counted by the importer. */46  count: z.number().int().nonnegative().optional(),47});48export type SectionRef = z.infer<typeof SectionRef>;4950export const RESOURCE_KINDS = ["slides", "sources", "lab", "dataset", "code", "transcript", "captions", "solution", "script", "bibliography"] as const;51export const ResourceKind = z.enum(RESOURCE_KINDS);52export type ResourceKind = z.infer<typeof ResourceKind>;5354/** A downloadable or viewable file. `download` is only honoured when a licence is recorded. */55export const Resource = z.object({56  id: z.string().regex(/^[A-Za-z0-9._-]+$/),57  kind: ResourceKind,58  title: z.string().min(1),59  description: z.string().optional(),60  visibility: Visibility,61  download: z.boolean(),62  license: z.string().nullable(),63  /** Storage key under ATLAS_DATA_DIR/resources. */64  path: SafeRelPath,65  bytes: z.number().int().nonnegative(),66  sha256: Sha256,67  mime: z.string(),68  /** Solutions may be held back until this ISO date even when public. */69  releaseAfter: z.iso.datetime().optional(),70});71export type Resource = z.infer<typeof Resource>;7273export const Capsule = z.object({74  id: Slug,75  title: z.string().min(1),76  order: z.number().int().nonnegative(),77  optional: z.boolean(),78  durationSeconds: z.number().positive(),79  /** True for locally generated technical test media. Never counted or described as course content. */80  synthetic: z.boolean(),81  visibility: Visibility,82  download: z.boolean(),83  media: z.object({84    hls: SafeRelPath.optional(),85    mp4: SafeRelPath.optional(),86    poster: SafeRelPath.optional(),87    captions: z.array(z.object({ lang: z.string(), label: z.string(), path: SafeRelPath })),88    transcript: SafeRelPath.optional(),89    chapters: SafeRelPath.optional(),90  }),91});92export type Capsule = z.infer<typeof Capsule>;9394export const Session = z.object({95  id: SessionId,96  number: z.number().int().positive(),97  slug: Slug,98  /** Former slugs: each one answers with a permanent redirect to `slug`. */99  aliases: z.array(Slug),100  part: z.string().min(1),101  level: Level,102  title: z.string().min(1),103  short: z.string().min(1),104  outcomes: z.array(z.string().min(1)),105  dependsOn: z.array(SessionId),106  tags: z.array(z.string()),107  publication: PublicationState,108  /** What the curriculum repository says. Informational: it never publishes anything by itself. */109  source: z.object({110    status: z.enum(["planned", "draft", "implemented", "validated", "unknown"]),111    lastReviewed: z.string().nullable(),112    videoRecorded: z.boolean(),113    hash: Sha256,114  }),115  effortMinutes: z.record(z.string(), z.tuple([z.number(), z.number()])).nullable(),116  sections: z.partialRecord(SectionKey, SectionRef),117  capsules: z.array(Capsule),118  resources: z.array(Resource),119  revisions: z.array(z.object({ date: z.string(), note: z.string() })),120});121export type Session = z.infer<typeof Session>;122123export const Part = z.object({ id: z.string().min(1), short: z.string().min(1), title: z.string().min(1) });124export type Part = z.infer<typeof Part>;125126export const Redirect = z.object({ from: z.string().startsWith("/"), to: z.string().startsWith("/") });127128export const Manifest = z129  .object({130    schemaVersion: z.literal(1),131    course: z.object({132      id: z.string(),133      title: z.string(),134      shortTitle: z.string(),135      tagline: z.string(),136      subtitle: z.string(),137      language: z.literal("fr-CA"),138      version: z.string(),139      endorsementNotice: z.string(),140      /** null until the owner records a licence. While null, no download is ever offered. */141      license: z.string().nullable(),142    }),143    author: z.object({144      name: z.string(),145      role: z.string(),146      institution: z.string(),147      institutionShort: z.string(),148      department: z.string(),149    }),150    parts: z.array(Part).min(1),151    sessions: z.array(Session),152    /** Diagram-library publication, keyed by diagram id. Absent means private. */153    diagrams: z.record(z.string(), Visibility),154    /** Whether theme tags of not-yet-open sessions are shown publicly. */155    exposeTags: z.boolean(),156    redirects: z.array(Redirect),157    import: z.object({ sourceHash: Sha256, files: z.number().int().nonnegative() }),158  })159  .superRefine((m, ctx) => {160    const ids = new Set<string>(), slugs = new Set<string>(), parts = new Set(m.parts.map((p) => p.id));161    for (const s of m.sessions) {162      if (ids.has(s.id)) ctx.addIssue({ code: "custom", message: `séance en double : ${s.id}` });163      ids.add(s.id);164      for (const slug of [s.slug, ...s.aliases]) {165        if (slugs.has(slug)) ctx.addIssue({ code: "custom", message: `slug en double : ${slug}` });166        slugs.add(slug);167      }168      if (!parts.has(s.part)) ctx.addIssue({ code: "custom", message: `${s.id} : partie inconnue ${s.part}` });169    }170    for (const s of m.sessions) for (const d of s.dependsOn) {171      if (!ids.has(d)) ctx.addIssue({ code: "custom", message: `${s.id} : préalable inconnu ${d}` });172      if (d === s.id) ctx.addIssue({ code: "custom", message: `${s.id} : se cite comme préalable` });173    }174  });175export type Manifest = z.infer<typeof Manifest>;176177/** Editorial layer, kept by a human (and, in Phase 4, by the studio). The importer never writes it. */178export const Publication = z.object({179  exposeTags: z.boolean().default(true),180  license: z.string().nullable().default(null),181  sessions: z.record(SessionId, z.object({182    publication: PublicationState.optional(),183    slug: Slug.optional(),184    aliases: z.array(Slug).optional(),185    sections: z.partialRecord(SectionKey, Visibility).optional(),186    revisions: z.array(z.object({ date: z.string(), note: z.string() })).optional(),187  })).default({}),188  diagrams: z.record(z.string(), Visibility).default({}),189  redirects: z.array(Redirect).default([]),190});191export type Publication = z.infer<typeof Publication>;192193/* ---- Imported section payloads ---- */194195const Option = z.object({ id: z.string(), text: z.string(), feedback: z.string().optional() });196const SlideId = z.string().regex(/^S\d{2,3}-D\d{2,3}$/);197const QBase = {198  id: z.string(), prompt: z.string(), explanation: z.string(), outcome: z.number().int().optional(), level: z.string().optional(),199  /** Slides of the session the question draws on (the course's `slides:` field): « Revoir la diapositive ». */200  slides: z.array(SlideId).max(12).optional(),201};202export const Question = z.discriminatedUnion("type", [203  z.object({ ...QBase, type: z.literal("single"), options: z.array(Option).min(2), answer: z.string() }),204  z.object({ ...QBase, type: z.literal("multiple"), options: z.array(Option).min(2), answers: z.array(z.string()).min(1) }),205  z.object({ ...QBase, type: z.literal("true-false"), answer: z.boolean(), feedbackWrong: z.string().optional() }),206  z.object({ ...QBase, type: z.literal("ordering"), items: z.array(Option).min(2), order: z.array(z.string()).min(2) }),207  z.object({ ...QBase, type: z.literal("matching"), left: z.array(Option).min(2), right: z.array(Option).min(2), pairs: z.record(z.string(), z.string()) }),208  z.object({ ...QBase, type: z.literal("numeric"), answer: z.number(), tolerance: z.number().nonnegative(), unit: z.string().optional() }),209  z.object({ ...QBase, type: z.literal("fill-blank"), /** Text with {{1}}, {{2}}… placeholders. */ text: z.string(), blanks: z.record(z.string(), z.array(z.string()).min(1)) }),210  z.object({ ...QBase, type: z.literal("debug"), language: z.string(), code: z.string(), faultyLine: z.number().int().positive(), fixes: z.array(Option).min(2), fix: z.string() }),211  z.object({ ...QBase, type: z.literal("reflection"), modelAnswer: z.string(), criteria: z.array(z.string()).min(1) }),212]);213export type Question = z.infer<typeof Question>;214215export const Quiz = z.object({216  id: z.string(),217  title: z.string(),218  passingScore: z.number().int().nonnegative().optional(),219  questions: z.array(Question).min(1),220});221export type Quiz = z.infer<typeof Quiz>;222223export const Glossary = z.object({224  terms: z.array(z.object({ id: Slug, term: z.string(), english: z.string().nullable(), definition: z.string(), session: SessionId })),225});226export type Glossary = z.infer<typeof Glossary>;227228export const Figures = z.object({229  figures: z.array(z.object({230    id: z.string(), file: SafeRelPath, sha256: Sha256, caption: z.string(), alt: z.string().min(1), purpose: z.string().optional(),231    width: z.number().positive().optional(), height: z.number().positive().optional(),232  })),233});234export type Figures = z.infer<typeof Figures>;235236export const Bibliography = z.object({237  entries: z.array(z.object({ key: z.string(), type: z.string(), authors: z.string(), title: z.string(), year: z.string(), venue: z.string().nullable(), url: z.url().nullable() })),238});239export type Bibliography = z.infer<typeof Bibliography>;240241export const Chapters = z.array(z.object({ start: z.number().nonnegative(), title: z.string().min(1) })).min(1);242export type Chapters = z.infer<typeof Chapters>;243244export const Transcript = z.object({245  lang: z.string(),246  segments: z.array(z.object({ start: z.number().nonnegative(), end: z.number().positive(), text: z.string().min(1) })),247});248export type Transcript = z.infer<typeof Transcript>;249250/* ---- Slide decks ---- */251252export const SLIDE_KINDS = ["title", "section", "content", "references", "closing"] as const;253export const SLIDE_THEMES = ["dark", "light"] as const;254export type SlideTheme = (typeof SLIDE_THEMES)[number];255const SlideFile = z.object({ path: SafeRelPath, sha256: Sha256, bytes: z.number().int().positive() });256257/**258 * One session's deck as the viewer needs it: for each slide its place, kind, title, the plain text read from259 * the PDF (the text alternative, also indexed by the search) and the description of any figure it carries.260 * Assets are one vector image per slide and per theme plus a small thumbnail, under ATLAS_DATA_DIR/slides/<dir>.261 * Speaker notes are not part of this format, by construction: there is no field for them.262 */263export const Deck = z.object({264  session: SessionId,265  /** Identity of the deck: changes whenever a PDF or the structure changes. */266  hash: Sha256,267  /** Directory of the assets, relative to ATLAS_DATA_DIR/slides: "<session>/<first 12 of hash>". */268  dir: SafeRelPath,269  count: z.number().int().positive().max(500),270  width: z.number().positive(), height: z.number().positive(),271  slides: z.array(z.object({272    n: z.number().int().positive(),273    id: z.string().regex(/^S\d{2,3}-D\d{2,3}$/).nullable(),274    kind: z.enum(SLIDE_KINDS),275    title: z.string().min(1).max(300),276    section: z.string().max(300).nullable(),277    text: z.string().max(20_000),278    figures: z.array(z.object({ id: z.string().max(40), alt: z.string().max(5000) })).max(10),279    files: z.object({ dark: SlideFile, light: SlideFile, thumbDark: SlideFile, thumbLight: SlideFile }),280  })).min(1).max(500),281  warnings: z.array(z.string()).max(50),282}).refine((d) => d.slides.length === d.count && d.slides.every((s, i) => s.n === i + 1), "les diapositives doivent être numérotées de 1 à count");283export type Deck = z.infer<typeof Deck>;284285/* ---- Interactive lesson ---- */286287/** Markdown produced by the lesson builder from the deck sources: inert text, rendered by the `Markdown` component. */288const Md = z.string().max(12_000);289export const LESSON_BLOCK_KINDS = ["lead", "text", "definition", "concept", "takeaway", "warning", "question", "demo", "code", "table", "compare", "figure"] as const;290export const LessonBlock = z.object({291  kind: z.enum(LESSON_BLOCK_KINDS),292  /** Term defined, title of the box, label of the listing, identifier of the figure. */293  title: z.string().max(300).optional(),294  md: Md,295  /** Columns of a side-by-side comparison (kind « compare »). */296  columns: z.array(z.object({ title: z.string().max(200), md: Md })).max(4).optional(),297});298export type LessonBlock = z.infer<typeof LessonBlock>;299300/**301 * The interactive lesson of one session: the deck's sections in order, each with the text of its slides as302 * real text, the questions of the quiz that draw on it, the outcomes it covers, the terms it uses and the303 * explorables and diagrams placed in it. Built from the course repository (see src/server/lessons.ts); the304 * format has no field for speaker notes, solutions or grading answers.305 */306export const Lesson = z.object({307  session: SessionId,308  hash: Sha256,309  title: z.string().min(1).max(300),310  /** One-sentence purpose of the session (the deck's subtitle). */311  intro: z.string().max(1000),312  /** « Simplifications pédagogiques assumées » of the session, as its README states them. */313  simplifications: z.array(z.string().max(1000)).max(20),314  sections: z.array(z.object({315    id: Slug,316    title: z.string().min(1).max(300),317    lead: z.string().max(1000),318    /** One step per content slide, in deck order. */319    steps: z.array(z.object({ slide: SlideId.nullable(), title: z.string().min(1).max(300), blocks: z.array(LessonBlock).max(30) })).max(60),320    /** Numbers of the session outcomes this section covers (1-based), from the README mapping table. */321    outcomes: z.array(z.number().int().positive()).max(20),322    /** Quiz questions checked at the end of this section (ids of the session quiz). */323    questions: z.array(z.string().max(40)).max(40),324    /** Glossary terms used in this section (ids of the session glossary). */325    terms: z.array(Slug).max(60),326    /** Explorables and library diagrams shown in this section (ids of the platform registries), in order. */327    explorables: z.array(Slug).max(8),328    diagrams: z.array(Slug).max(4),329  })).min(1).max(20),330  /** Laboratory starter files, readable online. Never the solution, never the rubric. */331  labStarter: z.array(z.object({ name: z.string().regex(/^[A-Za-z0-9._-]+$/).max(80), language: z.string().max(20), text: z.string().max(80_000) })).max(12),332});333export type Lesson = z.infer<typeof Lesson>;334335/** A lesson pack: what `pnpm lessons:build` writes for one session and `pnpm studio lessons-import` installs. */336export const LessonPack = z.object({337  format: z.literal("atlas-lesson-pack"),338  v: z.literal(1),339  session: SessionId,340  /** SHA-256 of the canonical JSON of { lesson, quiz, glossary, lab }. */341  hash: Sha256,342  lesson: Lesson,343  quiz: Quiz.nullable(),344  glossary: Glossary.nullable(),345  lab: z.string().max(1_500_000).nullable(),346  /** Source files read in the course repository, with their SHA-256. */347  sources: z.record(z.string().max(300), Sha256),348  warnings: z.array(z.string().max(500)).max(100),349});350export type LessonPack = z.infer<typeof LessonPack>;351
Files main
Menu

Theme