@nakafa/aksara-contracts 0.46.0 → 0.47.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (65) hide show
  1. package/README.md +4 -0
  2. package/dist/content.d.ts +8 -6
  3. package/dist/content.js +8 -3
  4. package/dist/math/arc.js +5 -1
  5. package/dist/math/base.js +6 -6
  6. package/dist/math/collision.d.ts +4 -4
  7. package/dist/math/collision.js +15 -9
  8. package/dist/math/coordinate.d.ts +24 -13
  9. package/dist/math/coordinate.js +16 -2
  10. package/dist/math/extent.d.ts +26 -17
  11. package/dist/math/extent.js +18 -2
  12. package/dist/math/intersection.d.ts +21 -11
  13. package/dist/math/intersection.js +14 -1
  14. package/dist/math/precision.js +3 -3
  15. package/dist/math/proximity.d.ts +11 -7
  16. package/dist/math/proximity.js +21 -4
  17. package/dist/math/rational.d.ts +6 -5
  18. package/dist/math/rational.js +6 -1
  19. package/dist/math/resolution.d.ts +1 -1
  20. package/dist/math/resolution.js +12 -4
  21. package/dist/math/scene.d.ts +22 -3
  22. package/dist/preview/auth.js +2 -1
  23. package/dist/preview/target.js +7 -0
  24. package/dist/program/curriculum.js +2 -1
  25. package/dist/program/snapshot/digest.js +32 -31
  26. package/dist/program/snapshot/hash.js +2 -1
  27. package/dist/program/spec.js +2 -1
  28. package/dist/projection/article.js +2 -1
  29. package/dist/projection/material.js +2 -1
  30. package/dist/projection/page.js +2 -1
  31. package/dist/projection/question.d.ts +544 -17
  32. package/dist/projection/question.js +17 -3
  33. package/dist/projection/verify.d.ts +4 -3
  34. package/dist/projection/verify.js +14 -7
  35. package/dist/question/answer.d.ts +12 -6
  36. package/dist/question/answer.js +17 -2
  37. package/dist/question/item.js +5 -4
  38. package/dist/question/response.js +3 -3
  39. package/dist/question/rubric.js +2 -1
  40. package/dist/quran/identity.js +11 -0
  41. package/dist/quran/notes.js +13 -0
  42. package/dist/quran/provenance.js +3 -2
  43. package/dist/quran/snapshot/digest.js +4 -3
  44. package/dist/quran/snapshot/hash.js +2 -1
  45. package/dist/quran/snapshot/row/hash.js +5 -4
  46. package/dist/quran/snapshot/spec.js +3 -2
  47. package/dist/renderer/compatibility.js +8 -5
  48. package/dist/renderer/contract.js +5 -4
  49. package/dist/runtime/protected/limits.js +2 -1
  50. package/dist/runtime/protected/spec.js +3 -3
  51. package/dist/text/json.d.ts +5 -0
  52. package/dist/text/json.js +5 -0
  53. package/dist/transport/request.d.ts +108 -108
  54. package/dist/tryout/closure/accounting.js +5 -4
  55. package/dist/tryout/closure/placement.js +3 -2
  56. package/dist/tryout/hash/catalog.js +11 -10
  57. package/dist/tryout/hash/content.js +2 -1
  58. package/dist/tryout/hash/placement.js +2 -1
  59. package/dist/tryout/language.js +2 -4
  60. package/dist/tryout/runtime/canonical.js +2 -1
  61. package/dist/tryout/scoring.d.ts +13 -9
  62. package/dist/tryout/scoring.js +17 -8
  63. package/dist/tryout/snapshot/canonical.js +2 -1
  64. package/dist/tryout/snapshot/hash.js +2 -1
  65. package/package.json +5 -1
@@ -1,10 +1,29 @@
1
- import { type SceneCoordinate, type ScenePath } from "#contracts/math/coordinate";
1
+ import { BigDecimal } from "effect";
2
+ import { type ScenePath } from "#contracts/math/coordinate";
2
3
  import type { PlaneLabelAnchor, PlaneMathFrame, PlaneMathObject } from "#contracts/math/plane";
3
4
  import { type RenderThreshold } from "#contracts/math/precision";
4
5
  import type { SpaceLabelAnchor, SpaceMathFrame, SpaceMathObject, SpaceMathView } from "#contracts/math/space";
5
6
  /** Owns one stable path for radial geometry-resolution failures. */
6
7
  export declare function radialGeometryPath(kind: "arc" | "circle", index: number): ScenePath;
7
8
  /** Projects one plane scene into a single ordered exact coordinate stream. */
8
- export declare function planeSceneCoordinates(frame: PlaneMathFrame, objects: readonly PlaneMathObject[], labels: readonly PlaneLabelAnchor[], threshold: RenderThreshold): SceneCoordinate[];
9
+ export declare function planeSceneCoordinates(frame: PlaneMathFrame, objects: readonly PlaneMathObject[], labels: readonly PlaneLabelAnchor[], threshold: RenderThreshold): {
10
+ readonly axis: "x" | "y" | "z";
11
+ readonly error?: BigDecimal.BigDecimal;
12
+ readonly path: (string | number)[];
13
+ readonly reportable: boolean;
14
+ readonly value: {
15
+ readonly denominator: BigDecimal.BigDecimal;
16
+ readonly numerator: BigDecimal.BigDecimal;
17
+ };
18
+ }[];
9
19
  /** Projects one space scene into a single ordered exact coordinate stream. */
10
- export declare function spaceSceneCoordinates(frame: SpaceMathFrame, objects: readonly SpaceMathObject[], labels: readonly SpaceLabelAnchor[], view: SpaceMathView, threshold: RenderThreshold): SceneCoordinate[];
20
+ export declare function spaceSceneCoordinates(frame: SpaceMathFrame, objects: readonly SpaceMathObject[], labels: readonly SpaceLabelAnchor[], view: SpaceMathView, threshold: RenderThreshold): {
21
+ readonly axis: "x" | "y" | "z";
22
+ readonly error?: BigDecimal.BigDecimal;
23
+ readonly path: (string | number)[];
24
+ readonly reportable: boolean;
25
+ readonly value: {
26
+ readonly denominator: BigDecimal.BigDecimal;
27
+ readonly numerator: BigDecimal.BigDecimal;
28
+ };
29
+ }[];
@@ -2,6 +2,7 @@ import { Buffer } from "node:buffer";
2
2
  import { createHmac, timingSafeEqual } from "node:crypto";
3
3
  import { Effect, Schema } from "effect";
4
4
  import { RendererManifestEnvelopeSchema } from "#contracts/renderer/contract";
5
+ import { encodeJsonText } from "#contracts/text/json";
5
6
  const BASE64URL_SHA256_PATTERN = /^[A-Za-z0-9_-]{43}$/u;
6
7
  /** Stable wire format for one authenticated local renderer response. */
7
8
  export const PREVIEW_RENDERER_AUTH_FORMAT = "aksara-renderer-auth-v1";
@@ -22,7 +23,7 @@ export class PreviewRendererAuthError extends Schema.TaggedError()("PreviewRende
22
23
  }
23
24
  /** Canonicalizes one challenge and exact validated renderer identity. */
24
25
  export function canonicalizePreviewRendererAuth(input) {
25
- return JSON.stringify([
26
+ return encodeJsonText([
26
27
  PREVIEW_RENDERER_AUTH_FORMAT,
27
28
  input.nonce,
28
29
  input.manifestHash,
@@ -13,6 +13,13 @@ export const PreviewRouteSchema = Schema.Struct({
13
13
  * Responses remain owned by the canonical question registry and projections.
14
14
  */
15
15
  export const TryoutPreviewPlacementSchema = TryoutPlacementSourceSchema.pipe((schema) => schema.mapFields(Struct.omit(["response"])));
16
+ const TryoutPreviewTargetInputSchema = Schema.Struct({
17
+ exam: TryoutExamSchema,
18
+ placement: TryoutPreviewPlacementSchema,
19
+ section: TryoutSectionSchema,
20
+ set: TryoutSetSchema,
21
+ track: TryoutTrackSchema,
22
+ });
16
23
  /** Checks that every target row belongs to one locale-specific hierarchy. */
17
24
  function hasCoherentTryoutHierarchy(input) {
18
25
  const rows = [input.track, input.set, input.section, input.placement];
@@ -4,6 +4,7 @@ import { AppLocaleSchema } from "#contracts/locale";
4
4
  import { MaterialDomainSchema } from "#contracts/material/domain";
5
5
  import { LearningProgramKeySchema, ProgramNavigationIconKeySchema, ProgramNavigationLevelSchema, } from "#contracts/program/spec";
6
6
  import { MaterialKeySchema } from "#contracts/projection/material";
7
+ import { encodeJsonText } from "#contracts/text/json";
7
8
  import { isLowerKebab } from "#contracts/text/syntax";
8
9
  const CurriculumNamespaceMapSchema = Schema.Struct({
9
10
  de: Schema.Literal("lehrplaene"),
@@ -134,7 +135,7 @@ export const CurriculumRouteSchema = CurriculumRouteDraftSchema.pipe(Schema.chec
134
135
  })));
135
136
  /** Serializes one curriculum route in stable signed field order. */
136
137
  export function canonicalizeCurriculumRoute(route) {
137
- return JSON.stringify({
138
+ return encodeJsonText({
138
139
  appLocale: route.appLocale,
139
140
  ...(route.canonicalPath === undefined
140
141
  ? {}
@@ -1,9 +1,10 @@
1
1
  import { createHash } from "node:crypto";
2
- import { Effect, Schema, Stream } from "effect";
2
+ import { Effect, MutableHashMap, MutableHashSet, Option, Schema, Stream, } from "effect";
3
3
  import { Sha256HashSchema } from "#contracts/ids";
4
4
  import { curriculumNamespace, } from "#contracts/program/curriculum";
5
5
  import { ProgramRowHashError, verifyProgramSnapshotRowHash, } from "#contracts/program/snapshot/hash";
6
6
  import { canonicalizeProgramSnapshotRow, } from "#contracts/program/snapshot/row";
7
+ import { encodeJsonText } from "#contracts/text/json";
7
8
  import { compareCodeUnits } from "#contracts/text/order";
8
9
  const DIGEST_DOMAIN = "nakafa.aksara.program-rows";
9
10
  /** An aggregate program stream is incomplete, duplicated, or tampered. */
@@ -50,12 +51,12 @@ function isExactProgramRoot(row, program) {
50
51
  class ProgramDigestState {
51
52
  #activeAppLocales;
52
53
  #hash = createHash("sha256").update(`${DIGEST_DOMAIN}\n`);
53
- #nodes = new Set();
54
- #paths = new Set();
55
- #programs = new Map();
56
- #routes = new Set();
57
- #roots = new Set();
58
- #slugs = new Set();
54
+ #nodes = MutableHashSet.empty();
55
+ #paths = MutableHashSet.empty();
56
+ #programs = MutableHashMap.empty();
57
+ #routes = MutableHashSet.empty();
58
+ #roots = MutableHashSet.empty();
59
+ #slugs = MutableHashSet.empty();
59
60
  #lastCurriculumKey;
60
61
  #lastProgramOrder = 0;
61
62
  curriculumRowCount = 0;
@@ -72,7 +73,7 @@ class ProgramDigestState {
72
73
  programRowCount: this.programRowCount,
73
74
  rowCount: this.curriculumRowCount + this.programRowCount,
74
75
  sitemapCount: this.sitemapCount,
75
- slugCount: this.#slugs.size,
76
+ slugCount: MutableHashSet.size(this.#slugs),
76
77
  };
77
78
  }
78
79
  /** Adds one verified current row after checking ownership and order. */
@@ -83,31 +84,31 @@ class ProgramDigestState {
83
84
  }
84
85
  /** Finalizes complete catalog, root, locale, and count evidence. */
85
86
  validateComplete(expected) {
86
- const expectedRoots = new Set();
87
- for (const program of this.#programs.values()) {
87
+ const expectedRoots = MutableHashSet.empty();
88
+ for (const program of MutableHashMap.values(this.#programs)) {
88
89
  if (program.navigation.model !== "curriculum-tree") {
89
90
  continue;
90
91
  }
91
92
  for (const locale of this.#activeAppLocales) {
92
- expectedRoots.add(`${program.key}\0${locale}`);
93
+ MutableHashSet.add(expectedRoots, `${program.key}\0${locale}`);
93
94
  }
94
95
  }
95
96
  const counts = this.counts();
96
97
  const expectedCounts = expected ?? counts;
97
98
  if (this.programRowCount > 0 &&
98
- this.#slugs.size ===
99
+ MutableHashSet.size(this.#slugs) ===
99
100
  this.programRowCount * this.#activeAppLocales.length &&
100
- expectedRoots.size === this.#roots.size &&
101
- [...expectedRoots].every((root) => this.#roots.has(root)) &&
101
+ MutableHashSet.size(expectedRoots) === MutableHashSet.size(this.#roots) &&
102
+ [...expectedRoots].every((root) => MutableHashSet.has(this.#roots, root)) &&
102
103
  countIdentity(counts) === countIdentity(expectedCounts)) {
103
104
  return Effect.void;
104
105
  }
105
106
  return Effect.fail(new ProgramDigestError({
106
107
  code: "count",
107
- identity: JSON.stringify({
108
+ identity: encodeJsonText({
108
109
  actual: counts,
109
110
  expected,
110
- rootCount: this.#roots.size,
111
+ rootCount: MutableHashSet.size(this.#roots),
111
112
  }),
112
113
  }));
113
114
  }
@@ -126,19 +127,19 @@ class ProgramDigestState {
126
127
  row.displayOrder <= this.#lastProgramOrder) {
127
128
  return Effect.fail(new ProgramDigestError({ code: "order", identity: row.key }));
128
129
  }
129
- if (this.#programs.has(row.key) ||
130
- JSON.stringify(translationLocales) !==
131
- JSON.stringify(this.#activeAppLocales)) {
130
+ if (MutableHashMap.has(this.#programs, row.key) ||
131
+ encodeJsonText(translationLocales) !==
132
+ encodeJsonText(this.#activeAppLocales)) {
132
133
  return Effect.fail(new ProgramDigestError({ code: "key", identity: row.key }));
133
134
  }
134
135
  for (const translation of row.translations) {
135
136
  const slug = `${translation.appLocale}\0${translation.publicSlug}`;
136
- if (this.#slugs.has(slug)) {
137
+ if (MutableHashSet.has(this.#slugs, slug)) {
137
138
  return Effect.fail(new ProgramDigestError({ code: "slug", identity: slug }));
138
139
  }
139
- this.#slugs.add(slug);
140
+ MutableHashSet.add(this.#slugs, slug);
140
141
  }
141
- this.#programs.set(row.key, row);
142
+ MutableHashMap.set(this.#programs, row.key, row);
142
143
  this.#lastProgramOrder = row.displayOrder;
143
144
  this.programRowCount += 1;
144
145
  return this.#updateDigest(record);
@@ -146,7 +147,7 @@ class ProgramDigestState {
146
147
  /** Adds one localized route after validating identity and ancestry. */
147
148
  #addCurriculum(record) {
148
149
  const { row } = record;
149
- const program = this.#programs.get(row.programKey);
150
+ const program = Option.getOrUndefined(MutableHashMap.get(this.#programs, row.programKey));
150
151
  if (!this.#activeAppLocales.includes(row.appLocale) ||
151
152
  program?.navigation.model !== "curriculum-tree") {
152
153
  return Effect.fail(new ProgramDigestError({ code: "program", identity: row.programKey }));
@@ -159,9 +160,9 @@ class ProgramDigestState {
159
160
  const pathIdentity = `${row.programKey}\0${row.appLocale}\0${row.publicPath}`;
160
161
  const routeIdentity = `${row.appLocale}\0${row.publicPath}`;
161
162
  const nodeIdentity = `${row.programKey}\0${row.appLocale}\0${row.nodeKey}`;
162
- if (this.#nodes.has(nodeIdentity) ||
163
- this.#paths.has(pathIdentity) ||
164
- this.#routes.has(routeIdentity)) {
163
+ if (MutableHashSet.has(this.#nodes, nodeIdentity) ||
164
+ MutableHashSet.has(this.#paths, pathIdentity) ||
165
+ MutableHashSet.has(this.#routes, routeIdentity)) {
165
166
  return Effect.fail(new ProgramDigestError({ code: "route", identity: routeIdentity }));
166
167
  }
167
168
  if (row.parentPath === undefined) {
@@ -169,15 +170,15 @@ class ProgramDigestState {
169
170
  if (!isExactProgramRoot(row, program)) {
170
171
  return Effect.fail(new ProgramDigestError({ code: "root", identity: rootIdentity }));
171
172
  }
172
- this.#roots.add(rootIdentity);
173
+ MutableHashSet.add(this.#roots, rootIdentity);
173
174
  }
174
- else if (!this.#paths.has(`${row.programKey}\0${row.appLocale}\0${row.parentPath}`)) {
175
+ else if (!MutableHashSet.has(this.#paths, `${row.programKey}\0${row.appLocale}\0${row.parentPath}`)) {
175
176
  return Effect.fail(new ProgramDigestError({ code: "parent", identity: nodeIdentity }));
176
177
  }
177
178
  this.#lastCurriculumKey = orderKey;
178
- this.#nodes.add(nodeIdentity);
179
- this.#paths.add(pathIdentity);
180
- this.#routes.add(routeIdentity);
179
+ MutableHashSet.add(this.#nodes, nodeIdentity);
180
+ MutableHashSet.add(this.#paths, pathIdentity);
181
+ MutableHashSet.add(this.#routes, routeIdentity);
181
182
  this.curriculumRowCount += 1;
182
183
  this.sitemapCount += row.sitemap ? 1 : 0;
183
184
  return this.#updateDigest(record);
@@ -3,6 +3,7 @@ import { Effect, Schema } from "effect";
3
3
  import { Sha256HashSchema } from "#contracts/ids";
4
4
  import { CurriculumRouteRecordSchema, canonicalizeProgramSnapshotRow, LearningProgramRecordSchema, } from "#contracts/program/snapshot/row";
5
5
  import { PROGRAM_SNAPSHOT_FORMAT, ProgramSnapshotSchema, } from "#contracts/program/snapshot/spec";
6
+ import { encodeJsonText } from "#contracts/text/json";
6
7
  const SNAPSHOT_DOMAIN = "nakafa.aksara.localized-program-snapshot";
7
8
  const ROW_DOMAIN = "nakafa.aksara.program-row";
8
9
  /** Node could not compute a deterministic program snapshot identity. */
@@ -13,7 +14,7 @@ export class ProgramRowHashError extends Schema.TaggedError()("ProgramRowHashErr
13
14
  }
14
15
  /** Serializes current program facts in stable signed field order. */
15
16
  export function canonicalizeProgramSnapshot(input) {
16
- return JSON.stringify({
17
+ return encodeJsonText({
17
18
  activeAppLocales: input.activeAppLocales,
18
19
  curriculumRowCount: input.curriculumRowCount,
19
20
  format: PROGRAM_SNAPSHOT_FORMAT,
@@ -2,6 +2,7 @@ import { Schema } from "effect";
2
2
  import { CountryCodeSchema } from "#contracts/country";
3
3
  import { DateOnlySchema } from "#contracts/date";
4
4
  import { APP_LOCALE_CODES, AppLocaleSchema } from "#contracts/locale";
5
+ import { encodeJsonText } from "#contracts/text/json";
5
6
  import { isHttpsUrl, isLowerKebab } from "#contracts/text/syntax";
6
7
  /** Canonical language-neutral identity for one learning program. */
7
8
  export const LearningProgramKeySchema = Schema.String.pipe(Schema.check(Schema.makeFilter(isLowerKebab, {
@@ -150,7 +151,7 @@ export const LearningProgramSchema = Schema.Struct({
150
151
  });
151
152
  /** Serializes one program in stable signed field order. */
152
153
  export function canonicalizeLearningProgram(program) {
153
- return JSON.stringify({
154
+ return encodeJsonText({
154
155
  defaultCoverageStatus: program.defaultCoverageStatus,
155
156
  displayOrder: program.displayOrder,
156
157
  iconKey: program.iconKey,
@@ -4,6 +4,7 @@ import { withPublicationDates } from "#contracts/date";
4
4
  import { canonicalizeLearningGraphIdentity, LearningGraphIdentitySchema, } from "#contracts/graph/spec";
5
5
  import { ContentKeySchema, PublicPathSchema } from "#contracts/ids";
6
6
  import { AppLocaleSchema, ArtifactLocaleSchema } from "#contracts/locale";
7
+ import { encodeJsonText } from "#contracts/text/json";
7
8
  import { isLowerKebab } from "#contracts/text/syntax";
8
9
  /** Stable source-owned category segment used below the article route family. */
9
10
  export const ArticleCategorySchema = Schema.String.pipe(Schema.check(Schema.makeFilter(isLowerKebab)), Schema.brand("@NakafaAI/AksaraArticleCategory"));
@@ -115,7 +116,7 @@ export function canonicalizeArticleProjection(projection) {
115
116
  : { description: projection.metadata.description }),
116
117
  title: projection.metadata.title,
117
118
  };
118
- return JSON.stringify({
119
+ return encodeJsonText({
119
120
  appLocale: projection.appLocale,
120
121
  articleRouteSlug: projection.articleRouteSlug,
121
122
  articleSlug: projection.articleSlug,
@@ -4,6 +4,7 @@ import { withPublicationDates } from "#contracts/date";
4
4
  import { canonicalizeLearningGraphIdentity, LearningGraphIdentitySchema, } from "#contracts/graph/spec";
5
5
  import { ContentKeySchema, PublicPathSchema } from "#contracts/ids";
6
6
  import { AppLocaleSchema, ArtifactLocaleSchema, } from "#contracts/locale";
7
+ import { encodeJsonText } from "#contracts/text/json";
7
8
  const MATERIAL_KEY_PATTERN = /^lesson\.[a-z0-9]+(?:-[a-z0-9]+)*\.[a-z0-9]+(?:-[a-z0-9]+)*$/u;
8
9
  const SECTION_KEY_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
9
10
  const SEARCH_TITLE_MAX_LENGTH = 70;
@@ -157,7 +158,7 @@ export function canonicalizeMaterialProjection(projection) {
157
158
  : { subject: projection.metadata.subject }),
158
159
  title: projection.metadata.title,
159
160
  };
160
- return JSON.stringify({
161
+ return encodeJsonText({
161
162
  appLocale: projection.appLocale,
162
163
  artifactLocale: projection.artifactLocale,
163
164
  contentKey: projection.contentKey,
@@ -2,6 +2,7 @@ import { Schema } from "effect";
2
2
  import { withPublicationDates } from "#contracts/date";
3
3
  import { ContentKeySchema, CorpusSourcePathSchema, PublicPathSchema, } from "#contracts/ids";
4
4
  import { AppLocaleSchema, ArtifactLocaleSchema } from "#contracts/locale";
5
+ import { encodeJsonText } from "#contracts/text/json";
5
6
  import { isLowerKebab } from "#contracts/text/syntax";
6
7
  /** Stable source-owned identity for one reviewed public site page. */
7
8
  export const PageKeySchema = Schema.String.pipe(Schema.check(Schema.makeFilter(isLowerKebab)), Schema.brand("@NakafaAI/AksaraPageKey"));
@@ -63,7 +64,7 @@ export function canonicalizePublicPageProjection(projection) {
63
64
  description: projection.metadata.description,
64
65
  title: projection.metadata.title,
65
66
  };
66
- return JSON.stringify({
67
+ return encodeJsonText({
67
68
  appLocale: projection.appLocale,
68
69
  artifactLocale: projection.artifactLocale,
69
70
  contentKey: projection.contentKey,