@caelo-cms/shared 0.10.21 → 0.10.23

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 (241) hide show
  1. package/dist/ai-tools.d.ts +291 -231
  2. package/dist/ai-tools.d.ts.map +1 -1
  3. package/dist/ai-tools.js +349 -281
  4. package/dist/ai-tools.js.map +1 -1
  5. package/dist/auth-forms.d.ts.map +1 -1
  6. package/dist/auth-forms.js +4 -1
  7. package/dist/auth-forms.js.map +1 -1
  8. package/dist/base-css.d.ts +24 -0
  9. package/dist/base-css.d.ts.map +1 -0
  10. package/dist/base-css.js +28 -0
  11. package/dist/base-css.js.map +1 -0
  12. package/dist/build-page.d.ts +330 -0
  13. package/dist/build-page.d.ts.map +1 -0
  14. package/dist/build-page.js +282 -0
  15. package/dist/build-page.js.map +1 -0
  16. package/dist/content.d.ts +322 -9
  17. package/dist/content.d.ts.map +1 -1
  18. package/dist/content.js +354 -11
  19. package/dist/content.js.map +1 -1
  20. package/dist/css-gradient-scan.d.ts +14 -0
  21. package/dist/css-gradient-scan.d.ts.map +1 -0
  22. package/dist/css-gradient-scan.js +81 -0
  23. package/dist/css-gradient-scan.js.map +1 -0
  24. package/dist/css-var-scan.d.ts +56 -0
  25. package/dist/css-var-scan.d.ts.map +1 -0
  26. package/dist/css-var-scan.js +97 -0
  27. package/dist/css-var-scan.js.map +1 -0
  28. package/dist/design-manifest.d.ts +36 -0
  29. package/dist/design-manifest.d.ts.map +1 -0
  30. package/dist/design-manifest.js +90 -0
  31. package/dist/design-manifest.js.map +1 -0
  32. package/dist/fonts.d.ts +89 -0
  33. package/dist/fonts.d.ts.map +1 -0
  34. package/dist/fonts.js +241 -0
  35. package/dist/fonts.js.map +1 -0
  36. package/dist/genesis-inventory.d.ts +32 -0
  37. package/dist/genesis-inventory.d.ts.map +1 -0
  38. package/dist/genesis-inventory.js +186 -0
  39. package/dist/genesis-inventory.js.map +1 -0
  40. package/dist/genesis.d.ts +62 -0
  41. package/dist/genesis.d.ts.map +1 -0
  42. package/dist/genesis.js +78 -0
  43. package/dist/genesis.js.map +1 -0
  44. package/dist/i18n.d.ts +44 -1
  45. package/dist/i18n.d.ts.map +1 -1
  46. package/dist/i18n.js +72 -6
  47. package/dist/i18n.js.map +1 -1
  48. package/dist/index.d.ts +27 -0
  49. package/dist/index.d.ts.map +1 -1
  50. package/dist/index.js +27 -0
  51. package/dist/index.js.map +1 -1
  52. package/dist/interactions.d.ts +23 -0
  53. package/dist/interactions.d.ts.map +1 -0
  54. package/dist/interactions.js +44 -0
  55. package/dist/interactions.js.map +1 -0
  56. package/dist/media.d.ts +101 -16
  57. package/dist/media.d.ts.map +1 -1
  58. package/dist/media.js +126 -15
  59. package/dist/media.js.map +1 -1
  60. package/dist/page-log.d.ts +94 -0
  61. package/dist/page-log.d.ts.map +1 -0
  62. package/dist/page-log.js +111 -0
  63. package/dist/page-log.js.map +1 -0
  64. package/dist/preview-compose.d.ts +79 -0
  65. package/dist/preview-compose.d.ts.map +1 -1
  66. package/dist/preview-compose.js +155 -25
  67. package/dist/preview-compose.js.map +1 -1
  68. package/dist/proposal-status.d.ts +40 -0
  69. package/dist/proposal-status.d.ts.map +1 -0
  70. package/dist/proposal-status.js +34 -0
  71. package/dist/proposal-status.js.map +1 -0
  72. package/dist/responsive-images.d.ts +64 -0
  73. package/dist/responsive-images.d.ts.map +1 -0
  74. package/dist/responsive-images.js +98 -0
  75. package/dist/responsive-images.js.map +1 -0
  76. package/dist/safe-keys.d.ts +9 -0
  77. package/dist/safe-keys.d.ts.map +1 -0
  78. package/dist/safe-keys.js +20 -0
  79. package/dist/safe-keys.js.map +1 -0
  80. package/dist/seo.d.ts +8 -0
  81. package/dist/seo.d.ts.map +1 -1
  82. package/dist/seo.js +3 -1
  83. package/dist/seo.js.map +1 -1
  84. package/dist/skills.d.ts +14 -68
  85. package/dist/skills.d.ts.map +1 -1
  86. package/dist/skills.js +19 -113
  87. package/dist/skills.js.map +1 -1
  88. package/dist/strip-cdata.d.ts +7 -0
  89. package/dist/strip-cdata.d.ts.map +1 -0
  90. package/dist/strip-cdata.js +48 -0
  91. package/dist/strip-cdata.js.map +1 -0
  92. package/dist/structured-sets.d.ts +6 -16
  93. package/dist/structured-sets.d.ts.map +1 -1
  94. package/dist/structured-sets.js +5 -16
  95. package/dist/structured-sets.js.map +1 -1
  96. package/dist/subagents.d.ts +105 -3
  97. package/dist/subagents.d.ts.map +1 -1
  98. package/dist/subagents.js +224 -41
  99. package/dist/subagents.js.map +1 -1
  100. package/dist/template-engine.d.ts +85 -0
  101. package/dist/template-engine.d.ts.map +1 -0
  102. package/dist/template-engine.js +403 -0
  103. package/dist/template-engine.js.map +1 -0
  104. package/dist/theme-importers/auto-detect.d.ts +26 -0
  105. package/dist/theme-importers/auto-detect.d.ts.map +1 -0
  106. package/dist/theme-importers/auto-detect.js +42 -0
  107. package/dist/theme-importers/auto-detect.js.map +1 -0
  108. package/dist/theme-importers/css-comments.d.ts +12 -0
  109. package/dist/theme-importers/css-comments.d.ts.map +1 -0
  110. package/dist/theme-importers/css-comments.js +15 -0
  111. package/dist/theme-importers/css-comments.js.map +1 -0
  112. package/dist/theme-importers/dtcg.d.ts +46 -0
  113. package/dist/theme-importers/dtcg.d.ts.map +1 -0
  114. package/dist/theme-importers/dtcg.js +111 -0
  115. package/dist/theme-importers/dtcg.js.map +1 -0
  116. package/dist/theme-importers/loose.d.ts +3 -0
  117. package/dist/theme-importers/loose.d.ts.map +1 -0
  118. package/dist/theme-importers/loose.js +76 -0
  119. package/dist/theme-importers/loose.js.map +1 -0
  120. package/dist/theme-importers/shadcn.d.ts +24 -0
  121. package/dist/theme-importers/shadcn.d.ts.map +1 -0
  122. package/dist/theme-importers/shadcn.js +135 -0
  123. package/dist/theme-importers/shadcn.js.map +1 -0
  124. package/dist/theme-importers/style-dictionary.d.ts +17 -0
  125. package/dist/theme-importers/style-dictionary.d.ts.map +1 -0
  126. package/dist/theme-importers/style-dictionary.js +125 -0
  127. package/dist/theme-importers/style-dictionary.js.map +1 -0
  128. package/dist/theme-importers/tailwind.d.ts +3 -0
  129. package/dist/theme-importers/tailwind.d.ts.map +1 -0
  130. package/dist/theme-importers/tailwind.js +218 -0
  131. package/dist/theme-importers/tailwind.js.map +1 -0
  132. package/dist/theme-literal-binding.d.ts +37 -0
  133. package/dist/theme-literal-binding.d.ts.map +1 -0
  134. package/dist/theme-literal-binding.js +138 -0
  135. package/dist/theme-literal-binding.js.map +1 -0
  136. package/dist/theme-normalize.d.ts +31 -0
  137. package/dist/theme-normalize.d.ts.map +1 -0
  138. package/dist/theme-normalize.js +587 -0
  139. package/dist/theme-normalize.js.map +1 -0
  140. package/dist/theme-ramp.d.ts +55 -0
  141. package/dist/theme-ramp.d.ts.map +1 -0
  142. package/dist/theme-ramp.js +149 -0
  143. package/dist/theme-ramp.js.map +1 -0
  144. package/dist/theme-render.d.ts +105 -0
  145. package/dist/theme-render.d.ts.map +1 -0
  146. package/dist/theme-render.js +441 -0
  147. package/dist/theme-render.js.map +1 -0
  148. package/dist/themes-errors.d.ts +109 -0
  149. package/dist/themes-errors.d.ts.map +1 -0
  150. package/dist/themes-errors.js +170 -0
  151. package/dist/themes-errors.js.map +1 -0
  152. package/dist/themes.d.ts +343 -0
  153. package/dist/themes.d.ts.map +1 -0
  154. package/dist/themes.js +697 -0
  155. package/dist/themes.js.map +1 -0
  156. package/dist/version.d.ts +7 -4
  157. package/dist/version.d.ts.map +1 -1
  158. package/dist/version.js +6 -3
  159. package/dist/version.js.map +1 -1
  160. package/package.json +10 -2
  161. package/src/__tests__/redos-hardening.test.ts +160 -0
  162. package/src/ai-tools-add-module-modes.test.ts +106 -0
  163. package/src/ai-tools-position.test.ts +134 -0
  164. package/src/ai-tools.test.ts +81 -0
  165. package/src/ai-tools.ts +1179 -0
  166. package/src/auth-forms.ts +36 -0
  167. package/src/base-css.ts +30 -0
  168. package/src/build-page.test.ts +228 -0
  169. package/src/build-page.ts +319 -0
  170. package/src/cap-failures.ts +67 -0
  171. package/src/content.test.ts +170 -0
  172. package/src/content.ts +620 -0
  173. package/src/context.ts +43 -0
  174. package/src/css-gradient-scan.ts +88 -0
  175. package/src/css-var-scan.test.ts +96 -0
  176. package/src/css-var-scan.ts +144 -0
  177. package/src/derive-module-type.test.ts +80 -0
  178. package/src/design-manifest.ts +93 -0
  179. package/src/fonts.test.ts +157 -0
  180. package/src/fonts.ts +296 -0
  181. package/src/genesis-inventory.test.ts +86 -0
  182. package/src/genesis-inventory.ts +215 -0
  183. package/src/genesis-sanitize.test.ts +35 -0
  184. package/src/genesis.ts +87 -0
  185. package/src/i18n.test.ts +274 -0
  186. package/src/i18n.ts +269 -0
  187. package/src/index.test.ts +10 -0
  188. package/src/index.ts +59 -0
  189. package/src/interactions.ts +48 -0
  190. package/src/logger.ts +147 -0
  191. package/src/media.test.ts +160 -0
  192. package/src/media.ts +355 -0
  193. package/src/page-log.test.ts +163 -0
  194. package/src/page-log.ts +124 -0
  195. package/src/preview-compose.test.ts +602 -0
  196. package/src/preview-compose.ts +709 -0
  197. package/src/preview-scanner.test.ts +96 -0
  198. package/src/preview-scanner.ts +214 -0
  199. package/src/proposal-status.test.ts +69 -0
  200. package/src/proposal-status.ts +40 -0
  201. package/src/responsive-images.test.ts +104 -0
  202. package/src/responsive-images.ts +151 -0
  203. package/src/result.ts +29 -0
  204. package/src/safe-keys.ts +21 -0
  205. package/src/seo.test.ts +234 -0
  206. package/src/seo.ts +261 -0
  207. package/src/skills.ts +48 -0
  208. package/src/snapshots.test.ts +80 -0
  209. package/src/snapshots.ts +81 -0
  210. package/src/strip-cdata.test.ts +41 -0
  211. package/src/strip-cdata.ts +50 -0
  212. package/src/structured-sets.ts +180 -0
  213. package/src/subagents.test.ts +262 -0
  214. package/src/subagents.ts +432 -0
  215. package/src/template-engine.test.ts +379 -0
  216. package/src/template-engine.ts +520 -0
  217. package/src/theme-gradient.test.ts +92 -0
  218. package/src/theme-importers/__tests__/proto-pollution.test.ts +54 -0
  219. package/src/theme-importers/auto-detect.ts +84 -0
  220. package/src/theme-importers/css-comments.ts +15 -0
  221. package/src/theme-importers/dtcg.ts +106 -0
  222. package/src/theme-importers/loose.ts +76 -0
  223. package/src/theme-importers/shadcn.ts +133 -0
  224. package/src/theme-importers/style-dictionary.ts +125 -0
  225. package/src/theme-importers/tailwind.ts +217 -0
  226. package/src/theme-literal-binding.test.ts +71 -0
  227. package/src/theme-literal-binding.ts +159 -0
  228. package/src/theme-motion.test.ts +115 -0
  229. package/src/theme-normalize-envelope.test.ts +43 -0
  230. package/src/theme-normalize-gradient.test.ts +135 -0
  231. package/src/theme-normalize.ts +661 -0
  232. package/src/theme-ramp.ts +187 -0
  233. package/src/theme-render-sanitize.test.ts +45 -0
  234. package/src/theme-render.test.ts +119 -0
  235. package/src/theme-render.ts +487 -0
  236. package/src/theme-shadow.test.ts +56 -0
  237. package/src/themes-errors.ts +199 -0
  238. package/src/themes.ts +842 -0
  239. package/src/translation.test.ts +160 -0
  240. package/src/translation.ts +295 -0
  241. package/src/version.ts +66 -0
@@ -0,0 +1,160 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ import { describe, expect, it } from "bun:test";
4
+ import {
5
+ buildModeOnePrompt,
6
+ buildModeTwoPrompt,
7
+ computeBlockDiff,
8
+ type GlossaryEntry,
9
+ type ModuleBlockSlot,
10
+ translationResultPayload,
11
+ } from "./translation.js";
12
+
13
+ const SLOT = (overrides: Partial<ModuleBlockSlot> = {}): ModuleBlockSlot => ({
14
+ blockName: "content",
15
+ position: 0,
16
+ moduleId: "11111111-1111-4111-8111-111111111111",
17
+ moduleSlug: "hero",
18
+ html: "<h1>Welcome</h1>",
19
+ altText: null,
20
+ caption: null,
21
+ ...overrides,
22
+ });
23
+
24
+ describe("computeBlockDiff", () => {
25
+ it("returns empty when source and variant align", () => {
26
+ const slot = SLOT();
27
+ expect(computeBlockDiff([slot], [slot])).toEqual([]);
28
+ });
29
+
30
+ it("emits changed when html differs", () => {
31
+ const source = SLOT({ html: "<h1>Welcome — updated</h1>" });
32
+ const variant = SLOT({ html: "<h1>Willkommen</h1>" });
33
+ const ops = computeBlockDiff([source], [variant]);
34
+ expect(ops.length).toBe(1);
35
+ const [op] = ops;
36
+ if (op?.kind !== "changed") throw new Error("expected changed");
37
+ expect(op.before.html).toBe("<h1>Willkommen</h1>");
38
+ expect(op.after.html).toBe("<h1>Welcome — updated</h1>");
39
+ });
40
+
41
+ it("emits added when source has a slot variant lacks", () => {
42
+ const source = [SLOT({ position: 0 }), SLOT({ position: 1, html: "<p>New</p>" })];
43
+ const variant = [SLOT({ position: 0 })];
44
+ const ops = computeBlockDiff(source, variant);
45
+ expect(ops.length).toBe(1);
46
+ expect(ops[0]?.kind).toBe("added");
47
+ });
48
+
49
+ it("emits removed when variant has a slot source lacks", () => {
50
+ const source = [SLOT({ position: 0 })];
51
+ const variant = [SLOT({ position: 0 }), SLOT({ position: 1, html: "<p>Stale</p>" })];
52
+ const ops = computeBlockDiff(source, variant);
53
+ expect(ops.length).toBe(1);
54
+ expect(ops[0]?.kind).toBe("removed");
55
+ });
56
+
57
+ it("matches by (blockName, position) — same blockName different position is added/removed", () => {
58
+ const source = [SLOT({ position: 0 })];
59
+ const variant = [SLOT({ position: 1 })];
60
+ const ops = computeBlockDiff(source, variant);
61
+ expect(ops.length).toBe(2);
62
+ expect(ops.map((o) => o.kind).sort()).toEqual(["added", "removed"]);
63
+ });
64
+
65
+ it("alt/caption changes count as changed", () => {
66
+ const source = SLOT({ altText: "A cat" });
67
+ const variant = SLOT({ altText: "Eine Katze" });
68
+ const ops = computeBlockDiff([source], [variant]);
69
+ expect(ops[0]?.kind).toBe("changed");
70
+ });
71
+ });
72
+
73
+ const GLOSSARY: GlossaryEntry[] = [
74
+ { sourceTerm: "Caelo", translation: "Caelo", context: "brand name — never translate" },
75
+ { sourceTerm: "CMS", translation: "CMS", context: null },
76
+ ];
77
+
78
+ describe("buildModeOnePrompt", () => {
79
+ it("includes locale codes, structural lock, glossary, style guide", () => {
80
+ const { system, user } = buildModeOnePrompt({
81
+ sourceLocale: "en",
82
+ targetLocale: "de-AT",
83
+ targetLocaleDisplayName: "German (Austria)",
84
+ sourceModules: [SLOT()],
85
+ glossary: GLOSSARY,
86
+ styleGuide: "Use Sie form (formal).",
87
+ });
88
+ expect(system).toContain("Source locale: en");
89
+ expect(system).toContain("Target locale: de-AT (German (Austria))");
90
+ expect(system).toContain("STRUCTURAL LOCK");
91
+ expect(system).toContain("Caelo");
92
+ expect(system).toContain("Use Sie form");
93
+ expect(user).toContain("Welcome");
94
+ });
95
+
96
+ it("omits empty glossary block", () => {
97
+ const { system } = buildModeOnePrompt({
98
+ sourceLocale: "en",
99
+ targetLocale: "de",
100
+ sourceModules: [SLOT()],
101
+ glossary: [],
102
+ styleGuide: null,
103
+ });
104
+ expect(system).not.toContain("## Glossary");
105
+ expect(system).not.toContain("## Style guide");
106
+ });
107
+ });
108
+
109
+ describe("buildModeTwoPrompt", () => {
110
+ it("renders structured diff with before/after for changed blocks", () => {
111
+ const source = SLOT({ html: "<h1>Welcome — Spring sale</h1>" });
112
+ const variant = SLOT({ html: "<h1>Willkommen</h1>" });
113
+ const diff = computeBlockDiff([source], [variant]);
114
+ const { system, user } = buildModeTwoPrompt({
115
+ sourceLocale: "en",
116
+ targetLocale: "de",
117
+ sourceModules: [source],
118
+ variantModules: [variant],
119
+ diff,
120
+ glossary: [],
121
+ styleGuide: null,
122
+ });
123
+ expect(system).toContain("STRUCTURAL LOCK");
124
+ expect(system).toContain("Return ONE entry per CHANGED module");
125
+ expect(user).toContain("Existing translation");
126
+ expect(user).toContain("Willkommen"); // before
127
+ expect(user).toContain("Spring sale"); // after
128
+ expect(user).toContain("CHANGED");
129
+ });
130
+
131
+ it("renders empty diff hint when no ops", () => {
132
+ const slot = SLOT();
133
+ const { user } = buildModeTwoPrompt({
134
+ sourceLocale: "en",
135
+ targetLocale: "de",
136
+ sourceModules: [slot],
137
+ variantModules: [slot],
138
+ diff: [],
139
+ glossary: [],
140
+ styleGuide: null,
141
+ });
142
+ expect(user).toContain("no changes detected");
143
+ });
144
+ });
145
+
146
+ describe("translationResultPayload", () => {
147
+ it("accepts the expected AI output shape", () => {
148
+ const ok = translationResultPayload.safeParse({
149
+ modules: [{ blockName: "content", position: 0, html: "<h1>Willkommen</h1>", altText: null }],
150
+ });
151
+ expect(ok.success).toBe(true);
152
+ });
153
+
154
+ it("rejects unknown fields (.strict)", () => {
155
+ const bad = translationResultPayload.safeParse({
156
+ modules: [{ blockName: "content", position: 0, html: "<h1>x</h1>", extra: 1 }],
157
+ });
158
+ expect(bad.success).toBe(false);
159
+ });
160
+ });
@@ -0,0 +1,295 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ /**
4
+ * P10 — translation primitives shared between the AI prompt builder,
5
+ * the Mode 1 / Mode 2 op handlers, and the dashboard.
6
+ *
7
+ * computeBlockDiff(source, variant) — block-level diff per
8
+ * CMS_REQUIREMENTS §7.6: matches modules by (block_name, position),
9
+ * returns typed `changed` / `added` / `removed` operations. Pure
10
+ * function; the AI receives this as Mode 2 prompt context.
11
+ *
12
+ * buildModeOnePrompt(...) / buildModeTwoPrompt(...) — assemble the
13
+ * §7.6 sample-payload shapes with glossary + style guide injected.
14
+ * Returns the prompt + a structured payload for unit-tests to
15
+ * assert against.
16
+ */
17
+
18
+ import { z } from "zod";
19
+
20
+ export interface ModuleBlockSlot {
21
+ /** The block (e.g. "content", "header") this module sits in. */
22
+ blockName: string;
23
+ /** Position within the block. Blocks are ordered + 0-based. */
24
+ position: number;
25
+ /** Module id — locked across locales (translations share refs). */
26
+ moduleId: string;
27
+ /** Display name shown to the AI for context. */
28
+ moduleSlug: string;
29
+ /** The translatable fields of the module. P10 supports HTML body
30
+ * and three SEO-style metadata fields. Extend as new typed fields
31
+ * land (e.g. structured-content blocks in P12A). */
32
+ html: string;
33
+ altText: string | null;
34
+ caption: string | null;
35
+ }
36
+
37
+ export type BlockDiffOp =
38
+ | {
39
+ kind: "changed";
40
+ blockName: string;
41
+ position: number;
42
+ moduleId: string;
43
+ before: ModuleBlockSlot;
44
+ after: ModuleBlockSlot;
45
+ }
46
+ | { kind: "added"; blockName: string; position: number; module: ModuleBlockSlot }
47
+ | { kind: "removed"; blockName: string; position: number; module: ModuleBlockSlot };
48
+
49
+ /**
50
+ * Diff a source page's modules against a variant's modules. Match by
51
+ * (blockName, position) — the §7.5 contract is that translations
52
+ * share STRUCTURE with the source (block + position alignment) and
53
+ * differ only in CONTENT (html / alt / caption per locale). Anything
54
+ * mis-aligned is `added` (source has it, variant doesn't) or
55
+ * `removed` (variant has it, source doesn't).
56
+ *
57
+ * `changed` rows contain BOTH before + after so the AI prompt can
58
+ * show the structured diff per the §7.6 sample payload.
59
+ */
60
+ export function computeBlockDiff(
61
+ source: readonly ModuleBlockSlot[],
62
+ variant: readonly ModuleBlockSlot[],
63
+ ): BlockDiffOp[] {
64
+ const ops: BlockDiffOp[] = [];
65
+ const variantBy = new Map<string, ModuleBlockSlot>();
66
+ for (const v of variant) variantBy.set(`${v.blockName}|${v.position}`, v);
67
+ const sourceBy = new Map<string, ModuleBlockSlot>();
68
+ for (const s of source) sourceBy.set(`${s.blockName}|${s.position}`, s);
69
+
70
+ for (const s of source) {
71
+ const key = `${s.blockName}|${s.position}`;
72
+ const v = variantBy.get(key);
73
+ if (!v) {
74
+ ops.push({ kind: "added", blockName: s.blockName, position: s.position, module: s });
75
+ continue;
76
+ }
77
+ if (s.html !== v.html || s.altText !== v.altText || s.caption !== v.caption) {
78
+ ops.push({
79
+ kind: "changed",
80
+ blockName: s.blockName,
81
+ position: s.position,
82
+ moduleId: v.moduleId,
83
+ before: v,
84
+ after: s,
85
+ });
86
+ }
87
+ }
88
+ for (const v of variant) {
89
+ const key = `${v.blockName}|${v.position}`;
90
+ if (!sourceBy.has(key)) {
91
+ ops.push({ kind: "removed", blockName: v.blockName, position: v.position, module: v });
92
+ }
93
+ }
94
+ return ops;
95
+ }
96
+
97
+ // ---------------------------------------------------------------------
98
+ // Prompt builders
99
+ // ---------------------------------------------------------------------
100
+
101
+ export interface GlossaryEntry {
102
+ sourceTerm: string;
103
+ translation: string;
104
+ context: string | null;
105
+ }
106
+
107
+ export interface ModeOnePromptInput {
108
+ /** Source-locale code (e.g. 'en'). */
109
+ sourceLocale: string;
110
+ /** Target locale code (e.g. 'de-AT'). */
111
+ targetLocale: string;
112
+ /** Optional locale displayName for context (e.g. "German (Austria)"). */
113
+ targetLocaleDisplayName?: string;
114
+ /** Source-page modules. The AI translates each per (blockName, position). */
115
+ sourceModules: readonly ModuleBlockSlot[];
116
+ glossary: readonly GlossaryEntry[];
117
+ styleGuide: string | null;
118
+ }
119
+
120
+ export interface ModeTwoPromptInput {
121
+ sourceLocale: string;
122
+ targetLocale: string;
123
+ targetLocaleDisplayName?: string;
124
+ sourceModules: readonly ModuleBlockSlot[];
125
+ /** The CURRENT variant — passed in full so the AI keeps the prior
126
+ * translation quality on unchanged blocks. */
127
+ variantModules: readonly ModuleBlockSlot[];
128
+ /** Pre-computed diff (call computeBlockDiff first). */
129
+ diff: readonly BlockDiffOp[];
130
+ glossary: readonly GlossaryEntry[];
131
+ styleGuide: string | null;
132
+ }
133
+
134
+ /**
135
+ * The AI returns this shape for both Mode 1 and Mode 2: one entry per
136
+ * source module slot, with the translated content fields. The handler
137
+ * matches entries back to the variant page's module rows by
138
+ * (blockName, position).
139
+ *
140
+ * Mode 1 returns one entry per source module.
141
+ * Mode 2 returns entries ONLY for changed blocks (preserves prior
142
+ * translation on unchanged ones).
143
+ */
144
+ export const translationResultModule = z
145
+ .object({
146
+ blockName: z.string().min(1),
147
+ position: z.number().int().min(0),
148
+ /** Translated module HTML body. */
149
+ html: z.string(),
150
+ altText: z.string().nullable().optional(),
151
+ caption: z.string().nullable().optional(),
152
+ })
153
+ .strict();
154
+
155
+ export const translationResultPayload = z
156
+ .object({
157
+ modules: z.array(translationResultModule),
158
+ })
159
+ .strict();
160
+ export type TranslationResultPayload = z.infer<typeof translationResultPayload>;
161
+
162
+ function renderGlossaryBlock(glossary: readonly GlossaryEntry[]): string {
163
+ if (glossary.length === 0) return "";
164
+ const lines = glossary.map((g) =>
165
+ g.context
166
+ ? `- "${g.sourceTerm}" → "${g.translation}" (${g.context})`
167
+ : `- "${g.sourceTerm}" → "${g.translation}"`,
168
+ );
169
+ return ["", "## Glossary (use these exact translations)", ...lines].join("\n");
170
+ }
171
+
172
+ function renderStyleGuideBlock(styleGuide: string | null): string {
173
+ if (!styleGuide || styleGuide.trim().length === 0) return "";
174
+ return ["", "## Style guide", styleGuide.trim()].join("\n");
175
+ }
176
+
177
+ function renderSourceModulesBlock(modules: readonly ModuleBlockSlot[]): string {
178
+ const rendered = modules.map((m) => {
179
+ const parts = [
180
+ `### Module ${m.moduleSlug} (block=${m.blockName}, position=${m.position})`,
181
+ "HTML:",
182
+ "```html",
183
+ m.html,
184
+ "```",
185
+ ];
186
+ if (m.altText !== null) parts.push(`alt: ${m.altText}`);
187
+ if (m.caption !== null) parts.push(`caption: ${m.caption}`);
188
+ return parts.join("\n");
189
+ });
190
+ return rendered.join("\n\n");
191
+ }
192
+
193
+ export function buildModeOnePrompt(input: ModeOnePromptInput): {
194
+ system: string;
195
+ user: string;
196
+ } {
197
+ const targetLabel = input.targetLocaleDisplayName ?? input.targetLocale;
198
+ const system = [
199
+ "You are translating a web page from one locale to another.",
200
+ `Source locale: ${input.sourceLocale}.`,
201
+ `Target locale: ${input.targetLocale} (${targetLabel}).`,
202
+ "",
203
+ "STRUCTURAL LOCK — the page's module layout (block names + positions) is identical across locales. You may NOT add, remove, or reorder modules. Translate ONLY the content fields (html, alt, caption) of each existing module.",
204
+ "",
205
+ "Translate every module listed below into the target locale. Preserve every HTML tag, attribute, class, id, href, and inline style verbatim — only the human-readable text inside tags + the alt/caption fields are translated. Numbers, code samples, and untranslatable proper nouns stay as-is.",
206
+ "",
207
+ 'Respond with a JSON object matching: {"modules": [{"blockName": str, "position": int, "html": str, "altText": str|null, "caption": str|null}, ...]}. Return ONE entry per source module — same blockName + position.',
208
+ renderGlossaryBlock(input.glossary),
209
+ renderStyleGuideBlock(input.styleGuide),
210
+ ]
211
+ .filter((s) => s.length > 0)
212
+ .join("\n");
213
+ const user = [
214
+ `# Source page modules (${input.sourceLocale} → ${input.targetLocale})`,
215
+ "",
216
+ renderSourceModulesBlock(input.sourceModules),
217
+ ].join("\n");
218
+ return { system, user };
219
+ }
220
+
221
+ export function buildModeTwoPrompt(input: ModeTwoPromptInput): {
222
+ system: string;
223
+ user: string;
224
+ } {
225
+ const targetLabel = input.targetLocaleDisplayName ?? input.targetLocale;
226
+ const changed = input.diff.filter((d) => d.kind === "changed");
227
+ const added = input.diff.filter((d) => d.kind === "added");
228
+ const removed = input.diff.filter((d) => d.kind === "removed");
229
+
230
+ const system = [
231
+ "You are updating an existing translation of a web page after the source changed.",
232
+ `Source locale: ${input.sourceLocale}.`,
233
+ `Target locale: ${input.targetLocale} (${targetLabel}).`,
234
+ "",
235
+ "STRUCTURAL LOCK — the page's module layout (block names + positions) is identical across locales. You may NOT add, remove, or reorder modules. Translate ONLY the content fields (html, alt, caption) that have CHANGED on the source. Preserve the existing translation verbatim for unchanged modules.",
236
+ "",
237
+ "Numbers, code samples, and untranslatable proper nouns stay as-is. Preserve every HTML tag/attribute/class/id/href/inline style in the source verbatim.",
238
+ "",
239
+ 'Respond with a JSON object matching: {"modules": [{"blockName": str, "position": int, "html": str, "altText": str|null, "caption": str|null}, ...]}. Return ONE entry per CHANGED module — do NOT include unchanged modules in the response.',
240
+ renderGlossaryBlock(input.glossary),
241
+ renderStyleGuideBlock(input.styleGuide),
242
+ ]
243
+ .filter((s) => s.length > 0)
244
+ .join("\n");
245
+
246
+ const userLines: string[] = [
247
+ `# Update translation: ${input.sourceLocale} → ${input.targetLocale}`,
248
+ "",
249
+ "## Current source (full, for context)",
250
+ "",
251
+ renderSourceModulesBlock(input.sourceModules),
252
+ "",
253
+ "## Existing translation (preserve unchanged modules verbatim)",
254
+ "",
255
+ renderSourceModulesBlock(input.variantModules),
256
+ "",
257
+ "## Structured diff",
258
+ ];
259
+ if (changed.length === 0 && added.length === 0 && removed.length === 0) {
260
+ userLines.push("(no changes detected — return an empty `modules` array)");
261
+ }
262
+ for (const c of changed) {
263
+ if (c.kind !== "changed") continue;
264
+ userLines.push("");
265
+ userLines.push(
266
+ `### CHANGED — block=${c.blockName} position=${c.position} module=${c.moduleId}`,
267
+ );
268
+ userLines.push("Before (existing translation):");
269
+ userLines.push("```html");
270
+ userLines.push(c.before.html);
271
+ userLines.push("```");
272
+ userLines.push("Source after (current source HTML to re-translate):");
273
+ userLines.push("```html");
274
+ userLines.push(c.after.html);
275
+ userLines.push("```");
276
+ }
277
+ for (const a of added) {
278
+ if (a.kind !== "added") continue;
279
+ userLines.push("");
280
+ userLines.push(
281
+ `### ADDED in source — block=${a.blockName} position=${a.position} (must translate)`,
282
+ );
283
+ userLines.push("```html");
284
+ userLines.push(a.module.html);
285
+ userLines.push("```");
286
+ }
287
+ for (const r of removed) {
288
+ if (r.kind !== "removed") continue;
289
+ userLines.push("");
290
+ userLines.push(
291
+ `### REMOVED from source — block=${r.blockName} position=${r.position} (drop from the translation)`,
292
+ );
293
+ }
294
+ return { system, user: userLines.join("\n") };
295
+ }
package/src/version.ts ADDED
@@ -0,0 +1,66 @@
1
+ // SPDX-License-Identifier: MPL-2.0
2
+
3
+ /**
4
+ * P17.0 — single source of truth for the Caelo version.
5
+ *
6
+ * Bumped via `bun run scripts/release.ts <new-version>` which:
7
+ * 1. Updates this constant + the root package.json `version`,
8
+ * 2. Re-signs every Tier-1 plugin manifest under packages/plugins/,
9
+ * 3. Builds + writes the release notes via `scripts/release-notes.ts`,
10
+ * 4. Tags the commit as `vX.Y.Z`,
11
+ * 5. Pushes — GitHub Actions takes over for the publish step.
12
+ *
13
+ * Read by:
14
+ * - admin's /security index page (header badge + about-modal),
15
+ * - cms-provision CLI's `version` subcommand,
16
+ * - User-Agent header on plugin-host's outbound HTTP calls,
17
+ * - Tier-1 manifest signing (the version is hashed into the
18
+ * signature payload so a manifest from version 0.5.0 can't be
19
+ * replayed as a 0.6.0 manifest).
20
+ *
21
+ * Format: SemVer 2.0.0. Pre-1.0 minor bumps are breaking; post-1.0
22
+ * follow standard SemVer.
23
+ */
24
+
25
+ export const CAELO_VERSION = "0.10.23";
26
+
27
+ /**
28
+ * Deprecated alias for back-compat — early P17 work spelled this
29
+ * `CALEO_VERSION` (typo of "Caelo"). New code should import
30
+ * `CAELO_VERSION`. Kept as a re-export so external consumers of this
31
+ * published package (plugin authors / embedders) keep compiling;
32
+ * removal is deferred to the next major bump. Knip flags this as a
33
+ * duplicate export — suppressed in `knip.json#ignoreIssues` because the
34
+ * alias is intentional, mirroring `subagents.ts`.
35
+ *
36
+ * @deprecated use CAELO_VERSION
37
+ */
38
+ export const CALEO_VERSION = CAELO_VERSION;
39
+
40
+ /**
41
+ * Parsed shape — major/minor/patch + optional pre-release tag.
42
+ * Stable interface for callers that need to feature-gate (rare —
43
+ * Caelo doesn't do feature flags, but version-gating a deprecation
44
+ * warning is a legitimate use).
45
+ */
46
+ export interface CaeloVersion {
47
+ readonly major: number;
48
+ readonly minor: number;
49
+ readonly patch: number;
50
+ readonly preRelease: string | null;
51
+ readonly raw: string;
52
+ }
53
+
54
+ export function parseVersion(raw: string = CAELO_VERSION): CaeloVersion {
55
+ const m = /^(\d+)\.(\d+)\.(\d+)(?:-(.+))?$/.exec(raw);
56
+ if (!m) {
57
+ throw new Error(`invalid Caelo version: ${raw}`);
58
+ }
59
+ return {
60
+ major: Number.parseInt(m[1] ?? "0", 10),
61
+ minor: Number.parseInt(m[2] ?? "0", 10),
62
+ patch: Number.parseInt(m[3] ?? "0", 10),
63
+ preRelease: m[4] ?? null,
64
+ raw,
65
+ };
66
+ }