@elytracms/studio 0.0.80 → 0.0.82

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 (196) hide show
  1. package/convex/contract-parity.test.ts +1 -1
  2. package/convex/functions.ts +13 -3
  3. package/convex/http-routes.test.ts +143 -0
  4. package/convex/http.ts +19 -9
  5. package/convex/redirect-revalidate.test.ts +130 -0
  6. package/convex/revalidate.ts +9 -7
  7. package/convex/schema.ts +0 -19
  8. package/convex/triggers.ts +33 -3
  9. package/convex/users.ts +1 -1
  10. package/dist/components/ai/ai-chat-panel.js +26 -4
  11. package/dist/components/ai/ai-chat-panel.js.map +1 -1
  12. package/dist/components/ai/proposal-card.d.ts +27 -0
  13. package/dist/components/ai/proposal-card.js +31 -3
  14. package/dist/components/ai/proposal-card.js.map +1 -1
  15. package/dist/components/ai/proposal-issues.d.ts +55 -0
  16. package/dist/components/ai/proposal-issues.js +69 -0
  17. package/dist/components/ai/proposal-issues.js.map +1 -0
  18. package/dist/components/ai/translate-document-dialog.js +38 -3
  19. package/dist/components/ai/translate-document-dialog.js.map +1 -1
  20. package/dist/components/app-views/default-modules.d.ts +141 -44
  21. package/dist/components/app-views/default-modules.js +229 -65
  22. package/dist/components/app-views/default-modules.js.map +1 -1
  23. package/dist/components/app-views/module.d.ts +38 -0
  24. package/dist/components/app-views/module.js.map +1 -1
  25. package/dist/components/app-views/sidebar.d.ts +5 -2
  26. package/dist/components/app-views/sidebar.js +16 -5
  27. package/dist/components/app-views/sidebar.js.map +1 -1
  28. package/dist/components/app-views/studio-config.d.ts +28 -1
  29. package/dist/components/app-views/studio-config.js +1 -1
  30. package/dist/components/app-views/studio-config.js.map +1 -1
  31. package/dist/components/builder/canvas-render-context.js +10 -2
  32. package/dist/components/builder/canvas-render-context.js.map +1 -1
  33. package/dist/components/builder/clipboard-actions.js +5 -3
  34. package/dist/components/builder/clipboard-actions.js.map +1 -1
  35. package/dist/components/builder/reuse-context.js +7 -1
  36. package/dist/components/builder/reuse-context.js.map +1 -1
  37. package/dist/components/builder/reuse.d.ts +94 -16
  38. package/dist/components/builder/reuse.js +201 -19
  39. package/dist/components/builder/reuse.js.map +1 -1
  40. package/dist/components/cms/bulk-actions.js +10 -5
  41. package/dist/components/cms/bulk-actions.js.map +1 -1
  42. package/dist/components/cms/cell-edit.d.ts +18 -3
  43. package/dist/components/cms/cell-edit.js +29 -7
  44. package/dist/components/cms/cell-edit.js.map +1 -1
  45. package/dist/components/cms/cms-store.d.ts +54 -14
  46. package/dist/components/cms/cms-store.js +142 -45
  47. package/dist/components/cms/cms-store.js.map +1 -1
  48. package/dist/components/cms/collection-table.js +2 -2
  49. package/dist/components/cms/collection-table.js.map +1 -1
  50. package/dist/components/cms/detail-editor.js +15 -14
  51. package/dist/components/cms/detail-editor.js.map +1 -1
  52. package/dist/components/cms/document-editor-context.d.ts +12 -1
  53. package/dist/components/cms/document-editor-context.js.map +1 -1
  54. package/dist/components/cms/field-control.js +22 -8
  55. package/dist/components/cms/field-control.js.map +1 -1
  56. package/dist/components/cms/field-renderer.js +8 -1
  57. package/dist/components/cms/field-renderer.js.map +1 -1
  58. package/dist/components/cms/gate-validation.d.ts +36 -0
  59. package/dist/components/cms/gate-validation.js +17 -0
  60. package/dist/components/cms/gate-validation.js.map +1 -0
  61. package/dist/components/cms/import-export.js +2 -1
  62. package/dist/components/cms/import-export.js.map +1 -1
  63. package/dist/components/cms/inline-field-errors.d.ts +8 -0
  64. package/dist/components/cms/inline-field-errors.js +52 -0
  65. package/dist/components/cms/inline-field-errors.js.map +1 -0
  66. package/dist/components/cms/issues-pane.d.ts +4 -2
  67. package/dist/components/cms/issues-pane.js +13 -7
  68. package/dist/components/cms/issues-pane.js.map +1 -1
  69. package/dist/components/cms/slug-collision-report.d.ts +41 -0
  70. package/dist/components/cms/slug-collision-report.js +52 -0
  71. package/dist/components/cms/slug-collision-report.js.map +1 -0
  72. package/dist/components/components/transfer-dialog.js +10 -12
  73. package/dist/components/components/transfer-dialog.js.map +1 -1
  74. package/dist/components/deployment-setup-gate.d.ts +24 -0
  75. package/dist/components/deployment-setup-gate.js +29 -4
  76. package/dist/components/deployment-setup-gate.js.map +1 -1
  77. package/dist/components/issue-severity.d.ts +73 -0
  78. package/dist/components/issue-severity.js +56 -0
  79. package/dist/components/issue-severity.js.map +1 -0
  80. package/dist/components/nav-issues.d.ts +68 -0
  81. package/dist/components/nav-issues.js +78 -0
  82. package/dist/components/nav-issues.js.map +1 -0
  83. package/dist/components/operation-error.js +3 -3
  84. package/dist/components/operation-error.js.map +1 -1
  85. package/dist/components/project-list.d.ts +9 -3
  86. package/dist/components/project-list.js +11 -5
  87. package/dist/components/project-list.js.map +1 -1
  88. package/dist/components/project-switcher.d.ts +3 -1
  89. package/dist/components/project-switcher.js +4 -2
  90. package/dist/components/project-switcher.js.map +1 -1
  91. package/dist/components/redirects/redirects-store.d.ts +21 -0
  92. package/dist/components/redirects/redirects-store.js +17 -4
  93. package/dist/components/redirects/redirects-store.js.map +1 -1
  94. package/dist/components/section-nav.js +75 -10
  95. package/dist/components/section-nav.js.map +1 -1
  96. package/dist/customization.d.ts +7 -0
  97. package/dist/customization.js.map +1 -1
  98. package/dist/lib/ai/apply-content.js +6 -7
  99. package/dist/lib/ai/apply-content.js.map +1 -1
  100. package/dist/lib/ai/chat-context.d.ts +32 -26
  101. package/dist/lib/ai/chat-context.js +81 -9
  102. package/dist/lib/ai/chat-context.js.map +1 -1
  103. package/dist/lib/ai/constraints.d.ts +42 -0
  104. package/dist/lib/ai/constraints.js +259 -0
  105. package/dist/lib/ai/constraints.js.map +1 -0
  106. package/dist/lib/ai/index.d.ts +2 -0
  107. package/dist/lib/ai/index.js +2 -0
  108. package/dist/lib/ai/index.js.map +1 -1
  109. package/dist/lib/ai/proposal-validation.d.ts +26 -0
  110. package/dist/lib/ai/proposal-validation.js +17 -0
  111. package/dist/lib/ai/proposal-validation.js.map +1 -0
  112. package/dist/lib/ai/shape.js +60 -13
  113. package/dist/lib/ai/shape.js.map +1 -1
  114. package/dist/lib/ai/tools.d.ts +8 -4
  115. package/dist/lib/ai/tools.js +43 -14
  116. package/dist/lib/ai/tools.js.map +1 -1
  117. package/dist/lib/ai/translate-document.d.ts +35 -1
  118. package/dist/lib/ai/translate-document.js +16 -1
  119. package/dist/lib/ai/translate-document.js.map +1 -1
  120. package/dist/lib/ai/types.d.ts +11 -6
  121. package/dist/lib/ai/types.js.map +1 -1
  122. package/dist/lib/ai/update-fields.d.ts +8 -6
  123. package/dist/lib/ai/update-fields.js +9 -22
  124. package/dist/lib/ai/update-fields.js.map +1 -1
  125. package/dist/lib/clipboard/paste-issues.d.ts +5 -0
  126. package/dist/lib/clipboard/paste-issues.js +16 -6
  127. package/dist/lib/clipboard/paste-issues.js.map +1 -1
  128. package/dist/lib/cms/document-gate.d.ts +37 -0
  129. package/dist/lib/cms/document-gate.js +40 -0
  130. package/dist/lib/cms/document-gate.js.map +1 -0
  131. package/dist/lib/cms-views/view-model.d.ts +5 -0
  132. package/dist/lib/cms-views/view-model.js +10 -4
  133. package/dist/lib/cms-views/view-model.js.map +1 -1
  134. package/dist/lib/data/collection-content.d.ts +6 -1
  135. package/dist/lib/data/collection-content.js +51 -15
  136. package/dist/lib/data/collection-content.js.map +1 -1
  137. package/dist/lib/i18n/i18n.d.ts +51 -16
  138. package/dist/lib/i18n/i18n.js +119 -43
  139. package/dist/lib/i18n/i18n.js.map +1 -1
  140. package/dist/lib/i18n/index.d.ts +8 -1
  141. package/dist/lib/i18n/index.js +1 -1
  142. package/dist/lib/i18n/index.js.map +1 -1
  143. package/dist/lib/i18n/messages.de.js +200 -0
  144. package/dist/lib/i18n/messages.de.js.map +1 -1
  145. package/dist/lib/i18n/messages.en.d.ts +162 -0
  146. package/dist/lib/i18n/messages.en.js +226 -0
  147. package/dist/lib/i18n/messages.en.js.map +1 -1
  148. package/dist/lib/i18n/types.d.ts +15 -9
  149. package/dist/lib/i18n/types.js.map +1 -1
  150. package/dist/lib/locale/binding-fallback.d.ts +8 -1
  151. package/dist/lib/locale/binding-fallback.js +8 -1
  152. package/dist/lib/locale/binding-fallback.js.map +1 -1
  153. package/dist/lib/locale/locale-store.d.ts +41 -9
  154. package/dist/lib/locale/locale-store.js +34 -15
  155. package/dist/lib/locale/locale-store.js.map +1 -1
  156. package/dist/lib/locale/locale-switcher.d.ts +3 -1
  157. package/dist/lib/locale/locale-switcher.js +4 -2
  158. package/dist/lib/locale/locale-switcher.js.map +1 -1
  159. package/dist/lib/locale/use-studio-locale.d.ts +9 -4
  160. package/dist/lib/locale/use-studio-locale.js.map +1 -1
  161. package/dist/lib/operations/studio-operations.js +19 -2
  162. package/dist/lib/operations/studio-operations.js.map +1 -1
  163. package/dist/lib/persistence/convex-adapter.d.ts +1 -3
  164. package/dist/lib/persistence/convex-adapter.js +0 -55
  165. package/dist/lib/persistence/convex-adapter.js.map +1 -1
  166. package/dist/lib/persistence/deployment-readiness.js +10 -0
  167. package/dist/lib/persistence/deployment-readiness.js.map +1 -1
  168. package/dist/lib/persona/labels.d.ts +85 -9
  169. package/dist/lib/persona/labels.js +198 -58
  170. package/dist/lib/persona/labels.js.map +1 -1
  171. package/dist/lib/routes/route-issues.d.ts +9 -0
  172. package/dist/lib/routes/route-issues.js +117 -0
  173. package/dist/lib/routes/route-issues.js.map +1 -0
  174. package/dist/lib/webhooks/dispatcher.d.ts +5 -2
  175. package/dist/lib/webhooks/dispatcher.js.map +1 -1
  176. package/dist/lib/webhooks/tags.js +150 -17
  177. package/dist/lib/webhooks/tags.js.map +1 -1
  178. package/dist/lib/workspace/config-issues.d.ts +56 -0
  179. package/dist/lib/workspace/config-issues.js +72 -0
  180. package/dist/lib/workspace/config-issues.js.map +1 -0
  181. package/dist/lib/workspace/index.d.ts +24 -8
  182. package/dist/lib/workspace/index.js +28 -18
  183. package/dist/lib/workspace/index.js.map +1 -1
  184. package/dist/lib/workspace/switcher-source.d.ts +20 -3
  185. package/dist/lib/workspace/switcher-source.js.map +1 -1
  186. package/dist/lib/workspace/workspace-source.js +27 -0
  187. package/dist/lib/workspace/workspace-source.js.map +1 -1
  188. package/dist/previews/manifest-format.d.ts +13 -6
  189. package/dist/previews/manifest-format.js +9 -2
  190. package/dist/previews/manifest-format.js.map +1 -1
  191. package/dist/router/root.d.ts +36 -1
  192. package/dist/router/root.js +54 -7
  193. package/dist/router/root.js.map +1 -1
  194. package/package.json +2 -2
  195. package/convex/cliTokens.ts +0 -63
  196. package/convex/sync.ts +0 -297
@@ -23,27 +23,57 @@
23
23
  *
24
24
  * Localized fields stay flat in the shape (the model authors one locale at a time);
25
25
  * the engine routes their values under `localized[locale]`.
26
+ *
27
+ * EC-473 — the shape now consumes the ONE constraint projection, so a rule declared
28
+ * in config actually reaches the model:
29
+ *
30
+ * - `collectionJsonSchema` merges declarative rules with the fragment derived from
31
+ * a schema-form `validation.validate`, so `{ min: 2 }` and `z.string().min(2)`
32
+ * arrive identically (and a declared DATE RANGE arrives at all — it previously
33
+ * reached no consumer whatsoever);
34
+ * - a rich-text field no longer OVERWRITES its constraints: the markdown schema is
35
+ * merged over them instead of replacing them (measured drift, EC-474 §2);
36
+ * - a `pattern` is dropped wherever a `format` explains the same thing (a
37
+ * 200-character email regex teaches a model nothing and costs real tokens);
38
+ * - a field whose projection is knowingly partial (`schemaComplete: false`) SAYS SO
39
+ * in its description rather than presenting the partial rule as the whole rule.
40
+ *
41
+ * EC-505 — one further narrowing of what the model is TOLD (never of what the engine
42
+ * accepts): a `date` property is `type: 'string'` + `format: 'date-time'` rather than
43
+ * the `['string','number']` union the general projection carries, because the model is
44
+ * asked to author an ISO instant (EC-472) and some providers reject a union `type`.
26
45
  */
27
- import { collectionJsonSchema, resolveLabel } from '@elytracms/core/cms-core';
46
+ import { collectionJsonSchema, fieldJsonSchema, resolveLabel } from '@elytracms/core/cms-core';
28
47
  import { MARKDOWN_PROSE_DESCRIPTION } from '@elytracms/core/rich-text';
48
+ import { constraintSentence, stripRedundantPattern, toModelFacingSchema } from './constraints.js';
29
49
  /**
30
- * A rich-text field's model-facing schema (EC-379): a MARKDOWN string. The
31
- * description carries the allowed subset (from the engine converter — one source
32
- * of truth), so the model has something concrete to aim at; the engine converts
33
- * the markdown into the `{ version, doc }` storage envelope at proposal-build
34
- * time and rejects out-of-subset constructs with explicit issues.
50
+ * A rich-text field's model-facing schema (EC-379): a MARKDOWN string, MERGED over
51
+ * the field's own constraint projection (EC-473 — it used to replace it, silently
52
+ * dropping a declared `min`/`max`/`pattern`). The description carries the allowed
53
+ * subset (from the engine converter — one source of truth), so the model has
54
+ * something concrete to aim at; the engine converts the markdown into the
55
+ * `{ version, doc }` storage envelope at proposal-build time and rejects
56
+ * out-of-subset constructs with explicit issues.
35
57
  */
36
- function richTextMarkdownSchema() {
58
+ function richTextMarkdownSchema(field) {
59
+ const constraints = stripRedundantPattern(fieldJsonSchema(field));
37
60
  return {
61
+ ...constraints,
38
62
  type: 'string',
39
63
  description: `Rich text, written as ${MARKDOWN_PROSE_DESCRIPTION} Write genuine, publishable prose.`,
40
64
  };
41
65
  }
42
- /** A field's model-facing description: resolved label + optional `ai.hint`. */
66
+ /**
67
+ * A field's model-facing description: resolved label + optional `ai.hint` + the
68
+ * constraint clause (EC-473). The constraints are stated in PROSE because that is
69
+ * what a model acts on — the same facts also ride as JSON-Schema keywords for the
70
+ * structured-output validator, which is a different reader.
71
+ */
43
72
  function describe(field, locale) {
44
73
  const label = resolveLabel(field.form?.label, locale) ?? field.name;
45
74
  const hint = field.ai?.hint;
46
- const parts = [label, hint].filter((p) => typeof p === 'string' && p.length > 0);
75
+ const constraints = constraintSentence(field);
76
+ const parts = [label, hint, constraints].filter((p) => typeof p === 'string' && p.length > 0);
47
77
  return parts.length > 0 ? parts.join(' — ') : undefined;
48
78
  }
49
79
  /**
@@ -116,14 +146,31 @@ export function buildTargetShape(input) {
116
146
  assetFields.push({ fieldName: field.name, multiple: field.multiple, candidateIds: ids });
117
147
  }
118
148
  else if (field.type === 'richText') {
119
- properties[field.name] = richTextMarkdownSchema();
149
+ properties[field.name] = richTextMarkdownSchema(field);
120
150
  }
121
151
  const prop = properties[field.name];
122
152
  if (prop) {
153
+ // EC-473: a `pattern` next to a `format` is 200+ characters that teach the
154
+ // model nothing — the format (and the prose constraints below) say it better.
155
+ // EC-505: and a `date`'s `['string','number']` union becomes the ISO string
156
+ // the model is actually asked to author.
157
+ const stripped = toModelFacingSchema(prop);
123
158
  const desc = describe(field, input.locale);
124
- // Preserve an existing description (e.g. relation enum / rich-text note).
125
- if (desc && typeof prop.description !== 'string')
126
- prop.description = desc;
159
+ // Preserve an existing description (e.g. relation enum / rich-text note), but
160
+ // append the constraint clause to it — the enum leash and the value rules are
161
+ // different facts and the model needs both.
162
+ const next = stripped === prop ? prop : { ...stripped };
163
+ if (desc !== undefined) {
164
+ const existing = typeof next.description === 'string' ? next.description : undefined;
165
+ const constraints = constraintSentence(field);
166
+ next.description =
167
+ existing === undefined
168
+ ? desc
169
+ : constraints !== undefined
170
+ ? `${existing} — ${constraints}`
171
+ : existing;
172
+ }
173
+ properties[field.name] = next;
127
174
  }
128
175
  }
129
176
  const requiredIn = Array.isArray(base.required) ? base.required : [];
@@ -1 +1 @@
1
- {"version":3,"file":"shape.js","sourceRoot":"","sources":["../../../src/lib/ai/shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,OAAO,EAAE,oBAAoB,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAE7E,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAA;AA6CtE;;;;;;GAMG;AACH,SAAS,sBAAsB;IAC7B,OAAO;QACL,IAAI,EAAE,QAAQ;QACd,WAAW,EAAE,yBAAyB,0BAA0B,oCAAoC;KACrG,CAAA;AACH,CAAC;AAED,+EAA+E;AAC/E,SAAS,QAAQ,CAAC,KAAe,EAAE,MAAe;IAChD,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,IAAI,EAAE,KAAK,EAAE,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AAC,CAAA;QACnC,IAAI,IAAI,EAAE,CAAC;YACT,MAAM,IAAI,GAAG,QAAQ,CAAC,KAAK,EAAE,KAAK,CAAC,MAAM,CAAC,CAAA;YAC1C,0EAA0E;YAC1E,IAAI,IAAI,IAAI,OAAO,IAAI,CAAC,WAAW,KAAK,QAAQ;gBAAE,IAAI,CAAC,WAAW,GAAG,IAAI,CAAA;QAC3E,CAAC;IACH,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAE,IAAI,CAAC,QAAqB,CAAC,CAAC,CAAC,EAAE,CAAA;IAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAA;IAE/F,MAAM,MAAM,GAAe;QACzB,IAAI,EAAE,QAAQ;QACd,UAAU;QACV,oBAAoB,EAAE,KAAK;QAC3B,GAAG,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC7C,CAAA;IACD,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,CAAA;AAC3E,CAAC","sourcesContent":["/**\n * Target-shape derivation for `applyContentWithShape` (EC-331).\n *\n * Reuses the existing collection→JSON-Schema derivation (`collectionJsonSchema`,\n * cms-core) rather than writing a new one, then applies the three leashes the AI\n * engine adds on top:\n *\n * - `ai.exclude` fields are removed entirely (the model never sees them → cannot\n * write them).\n * - relation fields become an ENUM over the provided candidate ids (or are omitted\n * when no candidates exist) — so a hallucinated relation id is not representable\n * as valid structured output.\n * - asset/image fields become an ENUM over the searched library asset ids (or are\n * omitted when none were searched) — EC-381, the SAME leash as relations: a\n * proposed asset id must be a real, `listAssets`-returned library asset, and a\n * create shape with no searched assets simply has no asset property (empty beats\n * wrong; the assistant never generates or uploads).\n * - rich-text fields are MARKDOWN STRINGS (EC-379): the description names the\n * allowed subset, and the engine converts md → the `{ version, doc }` storage\n * envelope at proposal-build time. The field's `allow` config leashes component\n * EMBEDS only — markdown cannot express an embed, so the leash is satisfied by\n * construction (and still enforced downstream by `validateRichTextEmbeds`).\n *\n * Localized fields stay flat in the shape (the model authors one locale at a time);\n * the engine routes their values under `localized[locale]`.\n */\nimport { collectionJsonSchema, resolveLabel } from '@elytracms/core/cms-core'\nimport type { CollectionDef, FieldDef, JsonSchema } from '@elytracms/core/cms-core'\nimport { MARKDOWN_PROSE_DESCRIPTION } from '@elytracms/core/rich-text'\nimport type { AssetCandidates, RelationCandidates } from './types'\n\n/** A relation field's resolved candidate set — how the engine maps ids → reference envelopes. */\nexport interface TargetRelationField {\n fieldName: string\n /** The relation's target collection id. */\n target: string\n multiple: boolean\n /** The allowed candidate ids (the enum the model chose from). */\n candidateIds: string[]\n}\n\n/** An asset field's resolved candidate set — how the engine maps ids → asset envelopes (EC-381). */\nexport interface TargetAssetField {\n fieldName: string\n multiple: boolean\n /** The allowed library asset ids (the enum the model chose from). */\n candidateIds: string[]\n}\n\n/** The derived target shape + the metadata the engine needs to post-process the output. */\nexport interface TargetShape {\n /** The JSON Schema the structured output must satisfy. */\n schema: JsonSchema\n /** Field names removed by `ai.exclude` (untouchable). */\n excluded: string[]\n /** Field names whose values route under `localized[locale]`. */\n localizedFields: string[]\n /** Relation fields with their candidate enums. */\n relationFields: TargetRelationField[]\n /** Asset fields with their candidate enums (EC-381). */\n assetFields: TargetAssetField[]\n}\n\nexport interface BuildTargetShapeInput {\n collection: CollectionDef\n /** Relation candidate ids by field — a relation field with none is omitted from the shape. */\n candidates?: RelationCandidates\n /** Asset candidate ids by field — an asset field with none is omitted from the shape (EC-381). */\n assetCandidates?: AssetCandidates\n /** UI locale for resolving field labels into descriptions. */\n locale?: string\n}\n\n/**\n * A rich-text field's model-facing schema (EC-379): a MARKDOWN string. The\n * description carries the allowed subset (from the engine converter — one source\n * of truth), so the model has something concrete to aim at; the engine converts\n * the markdown into the `{ version, doc }` storage envelope at proposal-build\n * time and rejects out-of-subset constructs with explicit issues.\n */\nfunction richTextMarkdownSchema(): JsonSchema {\n return {\n type: 'string',\n description: `Rich text, written as ${MARKDOWN_PROSE_DESCRIPTION} Write genuine, publishable prose.`,\n }\n}\n\n/** A field's model-facing description: resolved label + optional `ai.hint`. */\nfunction describe(field: FieldDef, locale?: string): string | undefined {\n const label = resolveLabel(field.form?.label, locale) ?? field.name\n const hint = field.ai?.hint\n const parts = [label, hint].filter((p): p is string => typeof p === 'string' && p.length > 0)\n return parts.length > 0 ? parts.join(' — ') : undefined\n}\n\n/**\n * Build the leash-aware target shape from a collection + candidates. Deterministic;\n * never throws. Reuses `collectionJsonSchema` as the base, then rewrites relations /\n * rich text and strips excluded fields.\n */\nexport function buildTargetShape(input: BuildTargetShapeInput): TargetShape {\n const base = collectionJsonSchema(input.collection)\n const properties: Record<string, JsonSchema> = {\n ...((base.properties as Record<string, JsonSchema>) ?? {}),\n }\n const excluded: string[] = []\n const localizedFields: string[] = []\n const relationFields: TargetRelationField[] = []\n const assetFields: TargetAssetField[] = []\n\n for (const field of input.collection.fields) {\n if (field.localized) localizedFields.push(field.name)\n\n if (field.ai?.exclude) {\n excluded.push(field.name)\n delete properties[field.name]\n continue\n }\n\n if (field.type === 'relation') {\n const candidates = input.candidates?.[field.name] ?? []\n if (candidates.length === 0) {\n // No candidate ids ⇒ omit the field: the model cannot emit a relation value\n // at all, so a hallucinated id is structurally impossible.\n delete properties[field.name]\n continue\n }\n const ids = candidates.map((c) => c.id)\n const label = candidates\n .map((c) => (c.label ? `${c.id} (${c.label})` : c.id))\n .join(', ')\n const idSchema: JsonSchema = {\n type: 'string',\n enum: ids,\n description: `Choose one of: ${label}`,\n }\n properties[field.name] = field.multiple ? { type: 'array', items: idSchema } : idSchema\n relationFields.push({\n fieldName: field.name,\n target: field.target,\n multiple: field.multiple,\n candidateIds: ids,\n })\n } else if (field.type === 'asset') {\n // EC-381 — same leash as relations: an asset field is an enum over the ids\n // the model actually searched in the media library (`listAssets`). No\n // searched assets ⇒ omit the field, so the proposal simply leaves it empty\n // (an invented / to-be-uploaded id is structurally unrepresentable).\n const candidates = input.assetCandidates?.[field.name] ?? []\n if (candidates.length === 0) {\n delete properties[field.name]\n continue\n }\n const ids = candidates.map((c) => c.id)\n const label = candidates\n .map((c) => (c.label ? `${c.id} (${c.label})` : c.id))\n .join(', ')\n const idSchema: JsonSchema = {\n type: 'string',\n enum: ids,\n description: `Choose an EXISTING library asset id, one of: ${label}. Leave empty if none fits.`,\n }\n properties[field.name] = field.multiple ? { type: 'array', items: idSchema } : idSchema\n assetFields.push({ fieldName: field.name, multiple: field.multiple, candidateIds: ids })\n } else if (field.type === 'richText') {\n properties[field.name] = richTextMarkdownSchema()\n }\n\n const prop = properties[field.name]\n if (prop) {\n const desc = describe(field, input.locale)\n // Preserve an existing description (e.g. relation enum / rich-text note).\n if (desc && typeof prop.description !== 'string') prop.description = desc\n }\n }\n\n const requiredIn = Array.isArray(base.required) ? (base.required as string[]) : []\n const required = requiredIn.filter((n) => !excluded.includes(n) && properties[n] !== undefined)\n\n const schema: JsonSchema = {\n type: 'object',\n properties,\n additionalProperties: false,\n ...(required.length > 0 ? { required } : {}),\n }\n return { schema, excluded, localizedFields, relationFields, assetFields }\n}\n"]}
1
+ {"version":3,"file":"shape.js","sourceRoot":"","sources":["../../../src/lib/ai/shape.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4CG;AACH,OAAO,EAAE,oBAAoB,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,0BAA0B,CAAA;AAE9F,OAAO,EAAE,0BAA0B,EAAE,MAAM,2BAA2B,CAAA;AACtE,OAAO,EAAE,kBAAkB,EAAE,q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C,WAAW,KAAK,SAAS;4BACzB,CAAC,CAAC,GAAG,QAAQ,MAAM,WAAW,EAAE;4BAChC,CAAC,CAAC,QAAQ,CAAA;YAClB,CAAC;YACD,UAAU,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,IAAI,CAAA;QAC/B,CAAC;IACH,CAAC;IAED,MAAM,UAAU,GAAG,KAAK,CAAC,OAAO,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC,CAAC,CAAE,IAAI,CAAC,QAAqB,CAAC,CAAC,CAAC,EAAE,CAAA;IAClF,MAAM,QAAQ,GAAG,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,KAAK,SAAS,CAAC,CAAA;IAE/F,MAAM,MAAM,GAAe;QACzB,IAAI,EAAE,QAAQ;QACd,UAAU;QACV,oBAAoB,EAAE,KAAK;QAC3B,GAAG,CAAC,QAAQ,CAAC,MAAM,GAAG,CAAC,CAAC,CAAC,CAAC,EAAE,QAAQ,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC7C,CAAA;IACD,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,eAAe,EAAE,cAAc,EAAE,WAAW,EAAE,CAAA;AAC3E,CAAC","sourcesContent":["/**\n * Target-shape derivation for `applyContentWithShape` (EC-331).\n *\n * Reuses the existing collection→JSON-Schema derivation (`collectionJsonSchema`,\n * cms-core) rather than writing a new one, then applies the three leashes the AI\n * engine adds on top:\n *\n * - `ai.exclude` fields are removed entirely (the model never sees them → cannot\n * write them).\n * - relation fields become an ENUM over the provided candidate ids (or are omitted\n * when no candidates exist) — so a hallucinated relation id is not representable\n * as valid structured output.\n * - asset/image fields become an ENUM over the searched library asset ids (or are\n * omitted when none were searched) — EC-381, the SAME leash as relations: a\n * proposed asset id must be a real, `listAssets`-returned library asset, and a\n * create shape with no searched assets simply has no asset property (empty beats\n * wrong; the assistant never generates or uploads).\n * - rich-text fields are MARKDOWN STRINGS (EC-379): the description names the\n * allowed subset, and the engine converts md → the `{ version, doc }` storage\n * envelope at proposal-build time. The field's `allow` config leashes component\n * EMBEDS only — markdown cannot express an embed, so the leash is satisfied by\n * construction (and still enforced downstream by `validateRichTextEmbeds`).\n *\n * Localized fields stay flat in the shape (the model authors one locale at a time);\n * the engine routes their values under `localized[locale]`.\n *\n * EC-473 — the shape now consumes the ONE constraint projection, so a rule declared\n * in config actually reaches the model:\n *\n * - `collectionJsonSchema` merges declarative rules with the fragment derived from\n * a schema-form `validation.validate`, so `{ min: 2 }` and `z.string().min(2)`\n * arrive identically (and a declared DATE RANGE arrives at all — it previously\n * reached no consumer whatsoever);\n * - a rich-text field no longer OVERWRITES its constraints: the markdown schema is\n * merged over them instead of replacing them (measured drift, EC-474 §2);\n * - a `pattern` is dropped wherever a `format` explains the same thing (a\n * 200-character email regex teaches a model nothing and costs real tokens);\n * - a field whose projection is knowingly partial (`schemaComplete: false`) SAYS SO\n * in its description rather than presenting the partial rule as the whole rule.\n *\n * EC-505 — one further narrowing of what the model is TOLD (never of what the engine\n * accepts): a `date` property is `type: 'string'` + `format: 'date-time'` rather than\n * the `['string','number']` union the general projection carries, because the model is\n * asked to author an ISO instant (EC-472) and some providers reject a union `type`.\n */\nimport { collectionJsonSchema, fieldJsonSchema, resolveLabel } from '@elytracms/core/cms-core'\nimport type { CollectionDef, FieldDef, JsonSchema } from '@elytracms/core/cms-core'\nimport { MARKDOWN_PROSE_DESCRIPTION } from '@elytracms/core/rich-text'\nimport { constraintSentence, stripRedundantPattern, toModelFacingSchema } from './constraints'\nimport type { AssetCandidates, RelationCandidates } from './types'\n\n/** A relation field's resolved candidate set — how the engine maps ids → reference envelopes. */\nexport interface TargetRelationField {\n fieldName: string\n /** The relation's target collection id. */\n target: string\n multiple: boolean\n /** The allowed candidate ids (the enum the model chose from). */\n candidateIds: string[]\n}\n\n/** An asset field's resolved candidate set — how the engine maps ids → asset envelopes (EC-381). */\nexport interface TargetAssetField {\n fieldName: string\n multiple: boolean\n /** The allowed library asset ids (the enum the model chose from). */\n candidateIds: string[]\n}\n\n/** The derived target shape + the metadata the engine needs to post-process the output. */\nexport interface TargetShape {\n /** The JSON Schema the structured output must satisfy. */\n schema: JsonSchema\n /** Field names removed by `ai.exclude` (untouchable). */\n excluded: string[]\n /** Field names whose values route under `localized[locale]`. */\n localizedFields: string[]\n /** Relation fields with their candidate enums. */\n relationFields: TargetRelationField[]\n /** Asset fields with their candidate enums (EC-381). */\n assetFields: TargetAssetField[]\n}\n\nexport interface BuildTargetShapeInput {\n collection: CollectionDef\n /** Relation candidate ids by field — a relation field with none is omitted from the shape. */\n candidates?: RelationCandidates\n /** Asset candidate ids by field — an asset field with none is omitted from the shape (EC-381). */\n assetCandidates?: AssetCandidates\n /** UI locale for resolving field labels into descriptions. */\n locale?: string\n}\n\n/**\n * A rich-text field's model-facing schema (EC-379): a MARKDOWN string, MERGED over\n * the field's own constraint projection (EC-473 — it used to replace it, silently\n * dropping a declared `min`/`max`/`pattern`). The description carries the allowed\n * subset (from the engine converter — one source of truth), so the model has\n * something concrete to aim at; the engine converts the markdown into the\n * `{ version, doc }` storage envelope at proposal-build time and rejects\n * out-of-subset constructs with explicit issues.\n */\nfunction richTextMarkdownSchema(field: FieldDef): JsonSchema {\n const constraints = stripRedundantPattern(fieldJsonSchema(field))\n return {\n ...constraints,\n type: 'string',\n description: `Rich text, written as ${MARKDOWN_PROSE_DESCRIPTION} Write genuine, publishable prose.`,\n }\n}\n\n/**\n * A field's model-facing description: resolved label + optional `ai.hint` + the\n * constraint clause (EC-473). The constraints are stated in PROSE because that is\n * what a model acts on — the same facts also ride as JSON-Schema keywords for the\n * structured-output validator, which is a different reader.\n */\nfunction describe(field: FieldDef, locale?: string): string | undefined {\n const label = resolveLabel(field.form?.label, locale) ?? field.name\n const hint = field.ai?.hint\n const constraints = constraintSentence(field)\n const parts = [label, hint, constraints].filter(\n (p): p is string => typeof p === 'string' && p.length > 0,\n )\n return parts.length > 0 ? parts.join(' — ') : undefined\n}\n\n/**\n * Build the leash-aware target shape from a collection + candidates. Deterministic;\n * never throws. Reuses `collectionJsonSchema` as the base, then rewrites relations /\n * rich text and strips excluded fields.\n */\nexport function buildTargetShape(input: BuildTargetShapeInput): TargetShape {\n const base = collectionJsonSchema(input.collection)\n const properties: Record<string, JsonSchema> = {\n ...((base.properties as Record<string, JsonSchema>) ?? {}),\n }\n const excluded: string[] = []\n const localizedFields: string[] = []\n const relationFields: TargetRelationField[] = []\n const assetFields: TargetAssetField[] = []\n\n for (const field of input.collection.fields) {\n if (field.localized) localizedFields.push(field.name)\n\n if (field.ai?.exclude) {\n excluded.push(field.name)\n delete properties[field.name]\n continue\n }\n\n if (field.type === 'relation') {\n const candidates = input.candidates?.[field.name] ?? []\n if (candidates.length === 0) {\n // No candidate ids ⇒ omit the field: the model cannot emit a relation value\n // at all, so a hallucinated id is structurally impossible.\n delete properties[field.name]\n continue\n }\n const ids = candidates.map((c) => c.id)\n const label = candidates\n .map((c) => (c.label ? `${c.id} (${c.label})` : c.id))\n .join(', ')\n const idSchema: JsonSchema = {\n type: 'string',\n enum: ids,\n description: `Choose one of: ${label}`,\n }\n properties[field.name] = field.multiple ? { type: 'array', items: idSchema } : idSchema\n relationFields.push({\n fieldName: field.name,\n target: field.target,\n multiple: field.multiple,\n candidateIds: ids,\n })\n } else if (field.type === 'asset') {\n // EC-381 — same leash as relations: an asset field is an enum over the ids\n // the model actually searched in the media library (`listAssets`). No\n // searched assets ⇒ omit the field, so the proposal simply leaves it empty\n // (an invented / to-be-uploaded id is structurally unrepresentable).\n const candidates = input.assetCandidates?.[field.name] ?? []\n if (candidates.length === 0) {\n delete properties[field.name]\n continue\n }\n const ids = candidates.map((c) => c.id)\n const label = candidates\n .map((c) => (c.label ? `${c.id} (${c.label})` : c.id))\n .join(', ')\n const idSchema: JsonSchema = {\n type: 'string',\n enum: ids,\n description: `Choose an EXISTING library asset id, one of: ${label}. Leave empty if none fits.`,\n }\n properties[field.name] = field.multiple ? { type: 'array', items: idSchema } : idSchema\n assetFields.push({ fieldName: field.name, multiple: field.multiple, candidateIds: ids })\n } else if (field.type === 'richText') {\n properties[field.name] = richTextMarkdownSchema(field)\n }\n\n const prop = properties[field.name]\n if (prop) {\n // EC-473: a `pattern` next to a `format` is 200+ characters that teach the\n // model nothing — the format (and the prose constraints below) say it better.\n // EC-505: and a `date`'s `['string','number']` union becomes the ISO string\n // the model is actually asked to author.\n const stripped = toModelFacingSchema(prop)\n const desc = describe(field, input.locale)\n // Preserve an existing description (e.g. relation enum / rich-text note), but\n // append the constraint clause to it — the enum leash and the value rules are\n // different facts and the model needs both.\n const next: JsonSchema = stripped === prop ? prop : { ...stripped }\n if (desc !== undefined) {\n const existing = typeof next.description === 'string' ? next.description : undefined\n const constraints = constraintSentence(field)\n next.description =\n existing === undefined\n ? desc\n : constraints !== undefined\n ? `${existing} — ${constraints}`\n : existing\n }\n properties[field.name] = next\n }\n }\n\n const requiredIn = Array.isArray(base.required) ? (base.required as string[]) : []\n const required = requiredIn.filter((n) => !excluded.includes(n) && properties[n] !== undefined)\n\n const schema: JsonSchema = {\n type: 'object',\n properties,\n additionalProperties: false,\n ...(required.length > 0 ? { required } : {}),\n }\n return { schema, excluded, localizedFields, relationFields, assetFields }\n}\n"]}
@@ -249,10 +249,14 @@ export interface ChatToolExecutorOptions {
249
249
  locale?: string;
250
250
  /** Locale config — required for `translateDocument`. */
251
251
  localeConfig?: LocaleConfig;
252
- /** Injected leashes / store for `applyContentWithShape`'s validation. */
253
- validateRichTextEmbeds?: ValidateDocumentOptions['validateRichTextEmbeds'];
254
- validateComposition?: ValidateDocumentOptions['validateComposition'];
255
- store?: ValidateDocumentOptions['store'];
252
+ /**
253
+ * THE validation option bag (EC-497) — the CMS session's
254
+ * `gateValidateOptions(...)`, forwarded whole to every engine below. EC-473
255
+ * made the assistant's gate the editor's gate; passing one bag instead of five
256
+ * keys makes it stay that way, because a key can no longer be dropped by being
257
+ * forgotten at one of the three call sites this replaced.
258
+ */
259
+ validateOptions?: ValidateDocumentOptions;
256
260
  /** Max repair attempts inside `applyContentWithShape`. */
257
261
  maxRepairAttempts?: number;
258
262
  /** Per-search result cap (default {@link DEFAULT_SEARCH_LIMIT}). */
@@ -118,7 +118,10 @@ export const chatToolDefinitions = {
118
118
  .describe('Document id. Omit to target the currently open document.'),
119
119
  changes: z
120
120
  .array(z.object({
121
- field: z.string().describe('Field name from the collection schema.'),
121
+ field: z
122
+ .string()
123
+ .describe('Field name from the collection schema. The catalog in the system context ' +
124
+ 'lists every field with its type and the rules its value must satisfy.'),
122
125
  locale: z
123
126
  .string()
124
127
  .optional()
@@ -134,7 +137,12 @@ export const chatToolDefinitions = {
134
137
  value: z
135
138
  .unknown()
136
139
  .optional()
137
- .describe('The new value (a rich-text field takes a markdown string). null clears the field/prop.'),
140
+ .describe('The new value (a rich-text field takes a markdown string). null clears the ' +
141
+ "field/prop. It MUST satisfy that field's rules as listed in the catalog " +
142
+ '(length bounds, format, allowed values, date range…); the studio validates ' +
143
+ 'the merged document and rejects the proposal with the failing rules if it ' +
144
+ 'does not. Where the catalog says further rules apply, keep the value ' +
145
+ 'conventional and plausible.'),
138
146
  }))
139
147
  .min(1)
140
148
  .describe('The field changes — only what changes, never the whole document.'),
@@ -275,6 +283,19 @@ export function projectRichTextAsMarkdown(document, collection) {
275
283
  */
276
284
  export function createChatToolExecutor(options) {
277
285
  const searchLimit = options.searchLimit ?? DEFAULT_SEARCH_LIMIT;
286
+ /**
287
+ * EC-497 — the ONE bag every engine below is handed, assembled in exactly one
288
+ * place instead of at each of the three former call sites.
289
+ *
290
+ * The executor's own `localeConfig` is a FLOOR, never an override: the session's
291
+ * bag wins wherever it speaks, but a bag that omits the project's locales must
292
+ * not be able to make the gate locale-blind — a localized bound would then go
293
+ * unchecked, which is precisely the class of hole this task closed.
294
+ */
295
+ const validateOptions = {
296
+ ...(options.localeConfig ? { localeConfig: options.localeConfig } : {}),
297
+ ...(options.validateOptions ?? {}),
298
+ };
278
299
  function collectionById(id) {
279
300
  return options.collections.find((c) => c.id === id);
280
301
  }
@@ -350,12 +371,7 @@ export function createChatToolExecutor(options) {
350
371
  candidates: options.candidates.candidatesFor(collection),
351
372
  assetCandidates: options.candidates.assetCandidatesFor(collection),
352
373
  ...(options.locale ? { locale: options.locale } : {}),
353
- ...(options.localeConfig ? { localeConfig: options.localeConfig } : {}),
354
- ...(options.validateRichTextEmbeds
355
- ? { validateRichTextEmbeds: options.validateRichTextEmbeds }
356
- : {}),
357
- ...(options.validateComposition ? { validateComposition: options.validateComposition } : {}),
358
- ...(options.store ? { store: options.store } : {}),
374
+ validateOptions,
359
375
  });
360
376
  if (!result.ok) {
361
377
  return {
@@ -418,11 +434,7 @@ export function createChatToolExecutor(options) {
418
434
  // EC-381 — asset/image fields are leashed to searched library assets.
419
435
  assetCandidates: options.candidates.assetCandidatesFor(collection),
420
436
  ...(options.locale ? { locale: options.locale } : {}),
421
- ...(options.validateRichTextEmbeds
422
- ? { validateRichTextEmbeds: options.validateRichTextEmbeds }
423
- : {}),
424
- ...(options.validateComposition ? { validateComposition: options.validateComposition } : {}),
425
- ...(options.store ? { store: options.store } : {}),
437
+ validateOptions,
426
438
  ...(options.maxRepairAttempts !== undefined
427
439
  ? { maxRepairAttempts: options.maxRepairAttempts }
428
440
  : {}),
@@ -477,6 +489,10 @@ export function createChatToolExecutor(options) {
477
489
  localeConfig: options.localeConfig,
478
490
  ...(input.locale ? { targetLocales: [input.locale] } : {}),
479
491
  ...(knowledge ? { knowledge } : {}),
492
+ // EC-497: the engine gates its own output; both callers hand it the session
493
+ // registry + THE bag so its verdict is the editor's verdict.
494
+ registry: options.registry,
495
+ validateOptions,
480
496
  });
481
497
  if (result.total === 0) {
482
498
  return {
@@ -490,6 +506,18 @@ export function createChatToolExecutor(options) {
490
506
  if (result.translated === 0) {
491
507
  return { ok: false, errorText: 'The translation did not produce any content.' };
492
508
  }
509
+ // EC-473 established that a translated draft is validated before it is proposed
510
+ // (this path once proposed unvalidated content). EC-497 moved the gate INTO the
511
+ // engine so the detail-editor's header action — the other caller — inherits it
512
+ // instead of writing model output straight to the document. Only errors the
513
+ // translation INTRODUCED block it; pre-existing brokenness is not the
514
+ // assistant's doing.
515
+ if (result.introducedErrors.length > 0) {
516
+ return {
517
+ ok: false,
518
+ errorText: `The translation did not pass validation: ${issueSummary(result.introducedErrors)}`,
519
+ };
520
+ }
493
521
  options.onProposal(toolCallId, {
494
522
  origin: 'translate',
495
523
  collection: collection.id,
@@ -502,7 +530,8 @@ export function createChatToolExecutor(options) {
502
530
  },
503
531
  provenance: result.provenance,
504
532
  attempts: 1,
505
- issues: [],
533
+ // Warnings ride onto the proposal like every other path's (EC-496 renders them).
534
+ issues: [...result.issues],
506
535
  before: {
507
536
  values: document.values,
508
537
  ...(document.localized ? { localized: document.localized } : {}),
@@ -1 +1 @@
1
- {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../../src/lib/ai/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAA;AAU/D,OAAO,EAAE,kBAAkB,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAA;AAE5F,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AA0C3C,gFAAgF;AAChF,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAA;AAetC;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,eAAe,EAAE;QACf,WAAW,EAC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per the ratified AD, tools execute CLIENT-side against the operations layer with\n * the user's own session. This module carries the three pieces of that contract:\n *\n * 1. {@link chatToolDefinitions} — the SDK-native tool DEFINITIONS the route's\n * chat branch declares in `streamText`, deliberately WITHOUT `execute`, so the\n * SDK forwards every call to the client (the route stays a zero-data-access\n * proxy). Plain `{ description, inputSchema }` objects with zod schemas —\n * structurally the SDK's `FunctionTool`, with no SDK import (bundle discipline).\n * 2. {@link ChatToolReaders} — the operations-backed read interface the studio\n * binds client-side (search / list / get). Reads only — the engine never gains\n * a write path; apply is a separate, human-gated draft write.\n * 3. {@link createChatToolExecutor} — the client executor map `useChat`'s\n * `onToolCall` dispatches into. Readers pass through; the terminal tools run\n * the deterministic engines (`applyContentWithShape` / `translateDocument` /\n * `buildUpdateProposal`) and return a COMPACT proposal summary as the tool\n * output — the full proposal goes to the review UI via\n * `onProposal(toolCallId, …)`, never to the model.\n *\n * EC-378: `updateFields` is the edit-flow terminal tool — the model authors\n * per-field changes directly (it has `getDocument` output + the conversation as\n * context); the executor reads the CURRENT document, builds a validated update\n * proposal (`buildUpdateProposal`), and hands it to the review sink. Its target\n * (and `getDocument`'s) may be omitted to mean the document currently open in\n * the editor — resolved via the {@link ChatToolExecutorOptions.currentDocument}\n * seam ({@link resolveDocumentTarget}); the context block the panel injects is\n * ADVISORY, the resolution here is what makes \"this document\" deterministic.\n *\n * The EC-199 planner enum/schema (`plannerSchema` / `parsePlannerAction`) retired\n * with the hand-rolled orchestrator (EC-334 slice 2).\n */\nimport { z } from 'zod'\nimport { resolveDocumentTitle } from '@elytracms/core/cms-core'\nimport type {\n CmsDocument,\n CollectionDef,\n CollectionRegistry,\n CmsValidationIssue,\n FieldValues,\n LocaleConfig,\n ValidateDocumentOptions,\n} from '@elytracms/core/cms-core'\nimport { parseRichTextValue, richTextValueToMarkdownProse } from '@elytracms/core/rich-text'\nimport type { ComponentManifest } from '@elytracms/core/component-registry'\nimport { applyContentWithShape } from './apply-content'\nimport { translateDocument } from './translate-document'\nimport { buildUpdateProposal } from './update-fields'\nimport type { UpdateFieldChange } from './update-fields'\nimport { aiProvenance } from './provenance'\nimport type { ChatProposal } from './chat-state'\nimport type {\n AiProvider,\n AssetCandidate,\n AssetCandidates,\n RelationCandidate,\n RelationCandidates,\n ResolvedKnowledge,\n} from './types'\n\n/** A document the model may reference as a relation target (from a `searchDocuments` read). */\nexport interface DocumentSearchResult {\n id: string\n title?: string\n}\n\n/** An asset the model may reference (from a `listAssets` read). */\nexport interface AssetSearchResult {\n id: string\n title?: string\n}\n\n/**\n * The client-side, operations-backed readers the executor calls. The studio binds\n * these to the user's authenticated operations client; tests bind an in-memory fake.\n * Reads only — there is no write tool (apply is a separate, human-gated draft write).\n */\nexport interface ChatToolReaders {\n searchDocuments(input: {\n collection: string\n query?: string\n limit?: number\n }): Promise<DocumentSearchResult[]>\n listAssets(input: { query?: string; limit?: number }): Promise<AssetSearchResult[]>\n /**\n * Fetch a full document (values + localized) — the translate flow and the model's\n * `getDocument` tool. Optional: a studio that only wires create can omit it.\n */\n getDocument?(input: { collection: string; id: string }): Promise<CmsDocument | null>\n}\n\n/** Per-search result cap (protects the wire — results become model context). */\nexport const DEFAULT_SEARCH_LIMIT = 20\n\n// --- SDK-native chat tool definitions (EC-334) --------------------------------------\n\n/**\n * One model-facing tool definition for the route's `streamText` call. Structurally\n * the SDK's `FunctionTool` WITHOUT `execute` (execution is client-side, always) —\n * typed locally so this module needs no SDK import.\n */\nexport interface ChatToolDefinition {\n description: string\n /** Zod input schema (the AI SDK accepts zod schemas as `inputSchema` directly). */\n inputSchema: z.ZodType\n}\n\n/**\n * The tool set the native chat branch declares (EC-334). No `execute` on any entry:\n * the SDK forwards each call to the client, which executes reads against the\n * operations layer with the user's session and posts the result back; the terminal\n * tools (`createDocument` / `translateDocument`) run the deterministic engines\n * client-side and return a compact proposal summary as the tool output. The route\n * itself never executes anything.\n *\n * Collections are NOT enum-leashed here — the route has zero data access, so it has\n * no catalog; the catalog reaches the model through the request `context` (system\n * prompt), and every id a proposal references must come from a tool RESULT\n * (client-validated; `applyContentWithShape` still enum-leashes relation ids).\n */\nexport const chatToolDefinitions = {\n searchDocuments: {\n description:\n 'Search documents in a collection. Use it to find the REAL ids a relation may ' +\n 'reference, or to locate an existing document — never invent an id. Returns ' +\n '{ id, title } summaries.',\n inputSchema: z.object({\n collection: z\n .string()\n .describe('Target collection id (from the catalog in the system context).'),\n query: z.string().optional().describe('Optional search text (title match).'),\n limit: z.number().int().positive().optional().describe('Max results.'),\n }),\n },\n listAssets: {\n description:\n 'List or search assets (images, files). Use it to find REAL asset ids — never ' +\n 'invent one. Returns { id, title } summaries.',\n inputSchema: z.object({\n query: z.string().optional().describe('Optional search text.'),\n limit: z.number().int().positive().optional().describe('Max results.'),\n }),\n },\n getDocument: {\n description:\n 'Fetch one full document (values + localized values + block ids) by collection and ' +\n 'id. Omit both to read the document currently open in the editor. Rich-text field ' +\n 'values are returned as markdown strings where faithfully expressible.',\n inputSchema: z.object({\n collection: z\n .string()\n .optional()\n .describe('Collection id. Omit to target the currently open document.'),\n id: z\n .string()\n .optional()\n .describe(\n 'Document id (from a searchDocuments result). Omit to target the currently open document.',\n ),\n }),\n },\n createDocument: {\n description:\n 'Author a NEW document in a collection as a reviewable draft proposal. Terminal ' +\n 'step of a creation request — call it once you have gathered the real ids you ' +\n 'need. Returns a compact proposal summary.',\n inputSchema: z.object({\n collection: z.string().describe('Target collection id.'),\n instruction: z.string().describe('What to write, refining the user request.'),\n }),\n },\n updateFields: {\n description:\n 'Change specific fields of an EXISTING document as a reviewable proposal — THE ' +\n 'tool for every edit request (change / fix / remove / replace / rewrite wording, ' +\n 'values, or a page-builder element). Call getDocument first to see the current ' +\n 'values (and block ids), then pass ONLY the fields that change. A block prop ' +\n '(e.g. a Hero headline) is addressed as field + blockId + prop. Omit collection ' +\n 'and id to target the document currently open in the editor. A rich-text field ' +\n 'takes the FULL new value as a MARKDOWN string (headings, lists, links, bold/italic, ' +\n 'blockquotes — no images, code, tables, or HTML).',\n inputSchema: z.object({\n collection: z\n .string()\n .optional()\n .describe('Collection id. Omit to target the currently open document.'),\n id: z\n .string()\n .optional()\n .describe('Document id. Omit to target the currently open document.'),\n changes: z\n .array(\n z.object({\n field: z.string().describe('Field name from the collection schema.'),\n locale: z\n .string()\n .optional()\n .describe(\n \"Locale of a LOCALIZED field (defaults to the editor's working locale).\",\n ),\n blockId: z\n .string()\n .optional()\n .describe('For a blocks field: the id of the block to change (from getDocument).'),\n prop: z\n .string()\n .optional()\n .describe('With blockId: the component prop to change.'),\n value: z\n .unknown()\n .optional()\n .describe(\n 'The new value (a rich-text field takes a markdown string). null clears the field/prop.',\n ),\n }),\n )\n .min(1)\n .describe('The field changes — only what changes, never the whole document.'),\n }),\n },\n translateDocument: {\n description:\n 'Fill the EMPTY locale values of an existing document (search for it first to ' +\n 'get its id). Only for translation requests — never for edits (use updateFields ' +\n 'to change existing content). Returns a compact proposal summary.',\n inputSchema: z.object({\n collection: z.string().describe('Collection id.'),\n id: z.string().describe('Document id (from a searchDocuments result).'),\n locale: z.string().optional().describe('Target locale (defaults to the active one).'),\n }),\n },\n} satisfies Record<string, ChatToolDefinition>\n\n// --- Client executor map (EC-334) ----------------------------------------------------\n\n/** One forwarded tool call, as `useChat`'s `onToolCall` receives it. */\nexport interface ChatToolCall {\n toolCallId: string\n toolName: string\n input: unknown\n}\n\n/** The coordinates of the document currently open in the editor (EC-378 seam). */\nexport interface CurrentDocumentRef {\n collection: string\n id: string\n}\n\n/**\n * Resolve a tool call's target document from its (optional) input coordinates\n * and the current-document seam (EC-378). Deterministic:\n *\n * - both given → use them verbatim;\n * - both omitted → the currently open document (error when none is open);\n * - `collection` given with `id` omitted → the open document ONLY when the\n * collections match — a named collection must never silently cross onto a\n * different open document.\n *\n * Pure — tested directly; `getDocument` and `updateFields` share it.\n */\nexport function resolveDocumentTarget(\n input: { collection?: string; id?: string },\n current: CurrentDocumentRef | null | undefined,\n): { ok: true; ref: CurrentDocumentRef } | { ok: false; errorText: string } {\n if (input.collection !== undefined && input.id !== undefined) {\n return { ok: true, ref: { collection: input.collection, id: input.id } }\n }\n if (!current) {\n return {\n ok: false,\n errorText:\n 'No target document — pass collection and id, or ask the user to open the document in the editor.',\n }\n }\n if (input.collection !== undefined && input.collection !== current.collection) {\n return {\n ok: false,\n errorText: `The open document is in collection \"${current.collection}\", not \"${input.collection}\" — pass the document id explicitly.`,\n }\n }\n return { ok: true, ref: { collection: current.collection, id: input.id ?? current.id } }\n}\n\n/**\n * A tool execution outcome: an `output` posted back as the tool result, or an\n * `errorText` posted as an `output-error` part (the model sees the failure and\n * can react — never a thrown exception into the stream).\n */\nexport type ChatToolOutcome = { ok: true; output: unknown } | { ok: false; errorText: string }\n\n/**\n * The per-conversation candidate store: every `searchDocuments` result is recorded\n * per relation target collection, every `listAssets` result into one flat library\n * pool (EC-381), and the terminal tools may only reference the ids gathered here\n * (the `applyContentWithShape` / `buildUpdateProposal` enum + drop/loud leash makes\n * a hallucinated relation OR asset id structurally unrepresentable / fail loud).\n * Held by the panel per conversation — a new conversation (EC-380 clear) starts an\n * empty store.\n */\nexport interface ChatCandidateStore {\n record(collection: string, results: readonly DocumentSearchResult[]): void\n /** Record every `listAssets` result into the flat library pool (EC-381). */\n recordAssets(results: readonly AssetSearchResult[]): void\n /** Candidates for a target collection's relation fields, keyed by field name. */\n candidatesFor(collection: CollectionDef): RelationCandidates\n /**\n * Candidates for a target collection's asset/image fields (EC-381), keyed by\n * field name — every asset field shares the one flat searched-asset pool\n * (`listAssets` has no field dimension). Empty when nothing was searched, so the\n * asset field is then omitted from the shape (empty beats wrong).\n */\n assetCandidatesFor(collection: CollectionDef): AssetCandidates\n}\n\nexport function createChatCandidateStore(): ChatCandidateStore {\n const searched = new Map<string, Map<string, DocumentSearchResult>>()\n const searchedAssets = new Map<string, AssetSearchResult>()\n return {\n record(collection, results) {\n const byId = searched.get(collection) ?? new Map<string, DocumentSearchResult>()\n for (const result of results) byId.set(result.id, result)\n searched.set(collection, byId)\n },\n recordAssets(results) {\n for (const result of results) searchedAssets.set(result.id, result)\n },\n candidatesFor(collection) {\n const candidates: RelationCandidates = {}\n for (const field of collection.fields) {\n if (field.type !== 'relation') continue\n const found = searched.get(field.target)\n if (!found || found.size === 0) continue\n candidates[field.name] = [...found.values()].map(\n (r): RelationCandidate => ({ id: r.id, ...(r.title ? { label: r.title } : {}) }),\n )\n }\n return candidates\n },\n assetCandidatesFor(collection) {\n const candidates: AssetCandidates = {}\n if (searchedAssets.size === 0) return candidates\n const pool = [...searchedAssets.values()].map(\n (a): AssetCandidate => ({ id: a.id, ...(a.title ? { label: a.title } : {}) }),\n )\n for (const field of collection.fields) {\n if (field.type !== 'asset') continue\n candidates[field.name] = pool\n }\n return candidates\n },\n }\n}\n\nexport interface ChatToolExecutorOptions {\n /** The (route) provider the terminal engines proxy their model calls through. */\n provider: AiProvider\n registry: CollectionRegistry\n /** The collections the assistant may target (unknown ids fail loud). */\n collections: readonly CollectionDef[]\n /** Operations-backed readers (client-side, user's session). */\n readers: ChatToolReaders\n /** Mint a fresh document id for a new draft (studio: the CMS store's minter). */\n mintDocumentId: (collectionId: string) => string\n /** Per-conversation relation-candidate store (see {@link createChatCandidateStore}). */\n candidates: ChatCandidateStore\n /**\n * Full proposal sink, keyed by `toolCallId` — the review UI's entry point.\n * The tool OUTPUT stays a compact summary; the patch never reaches the model.\n */\n onProposal: (toolCallId: string, proposal: ChatProposal) => void\n /**\n * The document currently open in the editor (EC-378 seam) — a thunk read at\n * EXECUTION time, so \"this document\" resolves against what is open NOW.\n * `getDocument` / `updateFields` fall back to it when the model omits the\n * target (see {@link resolveDocumentTarget}).\n */\n currentDocument?: () => CurrentDocumentRef | null | undefined\n /** Current knowledge snapshot (thunk — the body thunk refreshes it per request). */\n knowledge?: () => ResolvedKnowledge | undefined\n /** Component manifests — context for `allow`-leashed richText/blocks fields. */\n components?: readonly ComponentManifest[]\n /** Target locale for localized fields. */\n locale?: string\n /** Locale config — required for `translateDocument`. */\n localeConfig?: LocaleConfig\n /** Injected leashes / store for `applyContentWithShape`'s validation. */\n validateRichTextEmbeds?: ValidateDocumentOptions['validateRichTextEmbeds']\n validateComposition?: ValidateDocumentOptions['validateComposition']\n store?: ValidateDocumentOptions['store']\n /** Max repair attempts inside `applyContentWithShape`. */\n maxRepairAttempts?: number\n /** Per-search result cap (default {@link DEFAULT_SEARCH_LIMIT}). */\n searchLimit?: number\n}\n\n/** The executor `useChat`'s `onToolCall` dispatches into. */\nexport interface ChatToolExecutor {\n execute(call: ChatToolCall): Promise<ChatToolOutcome>\n}\n\nfunction issueSummary(issues: readonly CmsValidationIssue[]): string {\n return issues\n .slice(0, 5)\n .map((issue) => issue.message)\n .join('; ')\n}\n\n/**\n * The MODEL-facing view of a document (EC-379): rich-text field values are\n * projected to markdown strings — but ONLY when the engine's loss-guarded\n * inverse can express them faithfully (subset-only content, round-trip proven).\n * A document carrying embeds, internal `ref` links, or out-of-subset marks keeps\n * its `{ version, doc }` envelope, so nothing the model later echoes back can\n * silently lose structure. Applies to the `getDocument` TOOL OUTPUT only; the\n * proposal/apply paths always read the raw stored document.\n */\nexport function projectRichTextAsMarkdown(\n document: CmsDocument,\n collection: CollectionDef,\n): CmsDocument {\n const richTextFields = collection.fields\n .filter((field) => field.type === 'richText')\n .map((field) => field.name)\n if (richTextFields.length === 0) return document\n const mapBucket = (bucket: FieldValues): FieldValues => {\n let next = bucket\n for (const name of richTextFields) {\n const value = next[name]\n if (value === undefined || value === null) continue\n const parsed = parseRichTextValue(value)\n if (!parsed.ok) continue\n const markdown = richTextValueToMarkdownProse(parsed.value)\n if (markdown === undefined) continue // not faithfully expressible → keep the envelope\n if (next === bucket) next = { ...bucket }\n next[name] = markdown\n }\n return next\n }\n const values = mapBucket(document.values)\n const localized = document.localized\n ? Object.fromEntries(\n Object.entries(document.localized).map(([locale, bucket]) => [locale, mapBucket(bucket)]),\n )\n : undefined\n return { ...document, values, ...(localized ? { localized } : {}) }\n}\n\n/**\n * Build the client executor map. Every entry validates its input against\n * {@link chatToolDefinitions}' zod schema (the route validates UIMessages only\n * structurally — deep input validation is the client's), executes with the\n * user's session, and returns a JSON-safe outcome. EC-378's `updateFields`\n * lands as one more case in this dispatch.\n */\nexport function createChatToolExecutor(options: ChatToolExecutorOptions): ChatToolExecutor {\n const searchLimit = options.searchLimit ?? DEFAULT_SEARCH_LIMIT\n\n function collectionById(id: string): CollectionDef | undefined {\n return options.collections.find((c) => c.id === id)\n }\n\n async function searchDocuments(input: {\n collection: string\n query?: string\n limit?: number\n }): Promise<ChatToolOutcome> {\n if (!collectionById(input.collection)) {\n return { ok: false, errorText: `Unknown collection \"${input.collection}\" — use a collection id from the catalog.` }\n }\n const limit = Math.min(input.limit ?? searchLimit, searchLimit)\n const results = (\n await options.readers.searchDocuments({\n collection: input.collection,\n ...(input.query ? { query: input.query } : {}),\n limit,\n })\n ).slice(0, limit)\n options.candidates.record(input.collection, results)\n return { ok: true, output: results }\n }\n\n async function listAssets(input: { query?: string; limit?: number }): Promise<ChatToolOutcome> {\n const limit = Math.min(input.limit ?? searchLimit, searchLimit)\n const results = (\n await options.readers.listAssets({\n ...(input.query ? { query: input.query } : {}),\n limit,\n })\n ).slice(0, limit)\n // EC-381 — leash asset fields to what the model actually searched, exactly as\n // searchDocuments feeds the relation store.\n options.candidates.recordAssets(results)\n return { ok: true, output: results }\n }\n\n async function getDocument(input: {\n collection?: string\n id?: string\n }): Promise<ChatToolOutcome> {\n if (!options.readers.getDocument) {\n return { ok: false, errorText: 'Reading documents is not available here.' }\n }\n const target = resolveDocumentTarget(input, options.currentDocument?.())\n if (!target.ok) return { ok: false, errorText: target.errorText }\n const document = await options.readers.getDocument(target.ref)\n if (!document) {\n return {\n ok: false,\n errorText: `Document \"${target.ref.collection}/${target.ref.id}\" was not found.`,\n }\n }\n // EC-379: the model reads (and re-authors) rich text as markdown.\n const collection = collectionById(target.ref.collection)\n return { ok: true, output: collection ? projectRichTextAsMarkdown(document, collection) : document }\n }\n\n /** EC-378 — the edit-flow terminal tool: current doc + model-authored changes → validated proposal. */\n async function updateFields(\n toolCallId: string,\n input: { collection?: string; id?: string; changes: UpdateFieldChange[] },\n ): Promise<ChatToolOutcome> {\n if (!options.readers.getDocument) {\n return { ok: false, errorText: 'Reading documents is not available here.' }\n }\n const target = resolveDocumentTarget(input, options.currentDocument?.())\n if (!target.ok) return { ok: false, errorText: target.errorText }\n const collection = collectionById(target.ref.collection)\n if (!collection) {\n return {\n ok: false,\n errorText: `Unknown collection \"${target.ref.collection}\" — use a collection id from the catalog.`,\n }\n }\n const document = await options.readers.getDocument(target.ref)\n if (!document) {\n return {\n ok: false,\n errorText: `Document \"${target.ref.collection}/${target.ref.id}\" was not found.`,\n }\n }\n const result = buildUpdateProposal({\n registry: options.registry,\n collection,\n document,\n changes: input.changes,\n candidates: options.candidates.candidatesFor(collection),\n assetCandidates: options.candidates.assetCandidatesFor(collection),\n ...(options.locale ? { locale: options.locale } : {}),\n ...(options.localeConfig ? { localeConfig: options.localeConfig } : {}),\n ...(options.validateRichTextEmbeds\n ? { validateRichTextEmbeds: options.validateRichTextEmbeds }\n : {}),\n ...(options.validateComposition ? { validateComposition: options.validateComposition } : {}),\n ...(options.store ? { store: options.store } : {}),\n })\n if (!result.ok) {\n return {\n ok: false,\n errorText: `The proposed changes did not pass validation: ${issueSummary(result.issues)}`,\n }\n }\n if (result.kind === 'no-change') {\n return {\n ok: true,\n output: {\n status: 'no-change',\n note: 'The requested change matches the current values — there is nothing to update.',\n },\n }\n }\n options.onProposal(toolCallId, {\n origin: 'update',\n collection: collection.id,\n documentId: document.id,\n patch: result.patch,\n provenance: aiProvenance(options.provider),\n attempts: 1,\n issues: result.issues,\n before: result.before,\n })\n // Compact summary only — the full patch goes to the review UI, not the model.\n const titleValues = {\n ...result.patch.values,\n ...(options.locale ? result.patch.localized?.[options.locale] : {}),\n }\n return {\n ok: true,\n output: {\n status: 'proposed',\n collection: collection.id,\n documentId: document.id,\n title: resolveDocumentTitle(collection, titleValues, document.id),\n changedFields: result.changedFields,\n note: 'Update proposal created; the editor reviews and applies it in the studio. Summarize the change briefly — do not repeat the content.',\n },\n }\n }\n\n async function createDocument(\n toolCallId: string,\n input: { collection: string; instruction: string },\n ): Promise<ChatToolOutcome> {\n const collection = collectionById(input.collection)\n if (!collection) {\n return { ok: false, errorText: `Unknown collection \"${input.collection}\" — use a collection id from the catalog.` }\n }\n const documentId = options.mintDocumentId(collection.id)\n const knowledge = options.knowledge?.()\n const result = await applyContentWithShape({\n provider: options.provider,\n registry: options.registry,\n collection,\n documentId,\n instruction: input.instruction,\n ...(knowledge ? { knowledge } : {}),\n ...(options.components ? { components: options.components } : {}),\n candidates: options.candidates.candidatesFor(collection),\n // EC-381 — asset/image fields are leashed to searched library assets.\n assetCandidates: options.candidates.assetCandidatesFor(collection),\n ...(options.locale ? { locale: options.locale } : {}),\n ...(options.validateRichTextEmbeds\n ? { validateRichTextEmbeds: options.validateRichTextEmbeds }\n : {}),\n ...(options.validateComposition ? { validateComposition: options.validateComposition } : {}),\n ...(options.store ? { store: options.store } : {}),\n ...(options.maxRepairAttempts !== undefined\n ? { maxRepairAttempts: options.maxRepairAttempts }\n : {}),\n })\n if (!result.ok) {\n return {\n ok: false,\n errorText: `The drafted ${collection.id} did not pass validation: ${issueSummary(result.issues)}`,\n }\n }\n options.onProposal(toolCallId, {\n origin: 'create',\n collection: collection.id,\n documentId,\n patch: result.patch,\n provenance: result.provenance,\n attempts: result.attempts,\n issues: result.issues,\n })\n // Compact summary only — the full patch goes to the review UI, not the model.\n return {\n ok: true,\n output: {\n status: 'proposed',\n collection: collection.id,\n documentId,\n title: resolveDocumentTitle(collection, result.patch.values, documentId),\n note: 'Draft proposal created; the editor reviews and applies it in the studio. Summarize briefly — do not repeat the content.',\n },\n }\n }\n\n async function translate(\n toolCallId: string,\n input: { collection: string; id: string; locale?: string },\n ): Promise<ChatToolOutcome> {\n const collection = collectionById(input.collection)\n if (!collection) {\n return { ok: false, errorText: `Unknown collection \"${input.collection}\" — use a collection id from the catalog.` }\n }\n if (!options.readers.getDocument || !options.localeConfig) {\n return { ok: false, errorText: 'Translation is not available here.' }\n }\n const document = await options.readers.getDocument({\n collection: input.collection,\n id: input.id,\n })\n if (!document) {\n return { ok: false, errorText: `Document \"${input.collection}/${input.id}\" was not found.` }\n }\n const knowledge = options.knowledge?.()\n const result = await translateDocument({\n provider: options.provider,\n collection,\n document,\n localeConfig: options.localeConfig,\n ...(input.locale ? { targetLocales: [input.locale] } : {}),\n ...(knowledge ? { knowledge } : {}),\n })\n if (result.total === 0) {\n return {\n ok: true,\n output: {\n status: 'nothing-to-translate',\n note: 'Every locale value is already filled — there is nothing to translate.',\n },\n }\n }\n if (result.translated === 0) {\n return { ok: false, errorText: 'The translation did not produce any content.' }\n }\n options.onProposal(toolCallId, {\n origin: 'translate',\n collection: collection.id,\n documentId: document.id,\n patch: {\n collection: collection.id,\n id: document.id,\n values: result.document.values,\n ...(result.document.localized ? { localized: result.document.localized } : {}),\n },\n provenance: result.provenance,\n attempts: 1,\n issues: [],\n before: {\n values: document.values,\n ...(document.localized ? { localized: document.localized } : {}),\n },\n })\n return {\n ok: true,\n output: {\n status: 'proposed',\n collection: collection.id,\n documentId: document.id,\n translatedUnits: result.translated,\n note: 'Translation proposal created; the editor reviews and applies it in the studio.',\n },\n }\n }\n\n return {\n async execute(call: ChatToolCall): Promise<ChatToolOutcome> {\n const definition = (chatToolDefinitions as Record<string, ChatToolDefinition>)[call.toolName]\n if (!definition) {\n return { ok: false, errorText: `Unknown tool \"${call.toolName}\".` }\n }\n const parsed = definition.inputSchema.safeParse(call.input)\n if (!parsed.success) {\n return { ok: false, errorText: `Invalid ${call.toolName} input: ${parsed.error.message}` }\n }\n try {\n switch (call.toolName) {\n case 'searchDocuments':\n return await searchDocuments(parsed.data as { collection: string; query?: string; limit?: number })\n case 'listAssets':\n return await listAssets(parsed.data as { query?: string; limit?: number })\n case 'getDocument':\n return await getDocument(parsed.data as { collection?: string; id?: string })\n case 'createDocument':\n return await createDocument(\n call.toolCallId,\n parsed.data as { collection: string; instruction: string },\n )\n case 'updateFields':\n return await updateFields(\n call.toolCallId,\n parsed.data as { collection?: string; id?: string; changes: UpdateFieldChange[] },\n )\n case 'translateDocument':\n return await translate(\n call.toolCallId,\n parsed.data as { collection: string; id: string; locale?: string },\n )\n default:\n return { ok: false, errorText: `Unknown tool \"${call.toolName}\".` }\n }\n } catch (error) {\n return {\n ok: false,\n errorText: `The ${call.toolName} step failed: ${error instanceof Error ? error.message : 'unknown error'}`,\n }\n }\n },\n }\n}\n"]}
1
+ {"version":3,"file":"tools.js","sourceRoot":"","sources":["../../../src/lib/ai/tools.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAgCG;AACH,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,oBAAoB,EAAE,MAAM,0BAA0B,CAAA;AAU/D,OAAO,EAAE,kBAAkB,EAAE,4BAA4B,EAAE,MAAM,2BAA2B,CAAA;AAE5F,OAAO,EAAE,qBAAqB,EAAE,MAAM,iBAAiB,CAAA;AACvD,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAA;AACxD,OAAO,EAAE,mBAAmB,EAAE,MAAM,iBAAiB,CAAA;AAErD,OAAO,EAAE,YAAY,EAAE,MAAM,cAAc,CAAA;AA0C3C,gFAAgF;AAChF,MAAM,CAAC,MAAM,oBAAoB,GAAG,EAAE,CAAA;AAetC;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,eAAe,EAAE;QACf,WAAW,EACT,+EAA+E;YAC/E,6EAA6E;YAC7E,0BAA0B;QAC5B,WAAW,EAAE,CAAC,CAAC,MAAM,CAAC;YACpB,UAAU,EAAE,CAAC;iBACV,MAAM,EAAE;iBACR,QAAQ,CAAC,gEAAgE,CAAC;YAC7E,KAAK,EAAE,CAAC,CAAC,MAAM,EAAE,CAAC,QAAQ,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AA0C,CAAC,CAAA;oBAC5E,KAAK,aAAa;wBAChB,OAAO,MAAM,WAAW,CAAC,MAAM,CAAC,IAA4C,CAAC,CAAA;oBAC/E,KAAK,gBAAgB;wBACnB,OAAO,MAAM,cAAc,CACzB,IAAI,CAAC,UAAU,EACf,MAAM,CAAC,IAAmD,CAC3D,CAAA;oBACH,KAAK,cAAc;wBACjB,OAAO,MAAM,YAAY,CACvB,IAAI,CAAC,UAAU,EACf,MAAM,CAAC,IAA0E,CAClF,CAAA;oBACH,KAAK,mBAAmB;wBACtB,OAAO,MAAM,SAAS,CACpB,IAAI,CAAC,UAAU,EACf,MAAM,CAAC,IAA2D,CACnE,CAAA;oBACH;wBACE,OAAO,EAAE,EAAE,EAAE,KAAK,EAAE,SAAS,EAAE,iBAAiB,IAAI,CAAC,QAAQ,IAAI,EAAE,CAAA;gBACvE,CAAC;YACH,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,OAAO;oBACL,EAAE,EAAE,KAAK;oBACT,SAAS,EAAE,OAAO,IAAI,CAAC,QAAQ,iBAAiB,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,eAAe,EAAE;iBAC3G,CAAA;YACH,CAAC;QACH,CAAC;KACF,CAAA;AACH,CAAC","sourcesContent":["/**\n * The narrow tool set the assistant works against (EC-199, reshaped by EC-334).\n *\n * Per the ratified AD, tools execute CLIENT-side against the operations layer with\n * the user's own session. This module carries the three pieces of that contract:\n *\n * 1. {@link chatToolDefinitions} — the SDK-native tool DEFINITIONS the route's\n * chat branch declares in `streamText`, deliberately WITHOUT `execute`, so the\n * SDK forwards every call to the client (the route stays a zero-data-access\n * proxy). Plain `{ description, inputSchema }` objects with zod schemas —\n * structurally the SDK's `FunctionTool`, with no SDK import (bundle discipline).\n * 2. {@link ChatToolReaders} — the operations-backed read interface the studio\n * binds client-side (search / list / get). Reads only — the engine never gains\n * a write path; apply is a separate, human-gated draft write.\n * 3. {@link createChatToolExecutor} — the client executor map `useChat`'s\n * `onToolCall` dispatches into. Readers pass through; the terminal tools run\n * the deterministic engines (`applyContentWithShape` / `translateDocument` /\n * `buildUpdateProposal`) and return a COMPACT proposal summary as the tool\n * output — the full proposal goes to the review UI via\n * `onProposal(toolCallId, …)`, never to the model.\n *\n * EC-378: `updateFields` is the edit-flow terminal tool — the model authors\n * per-field changes directly (it has `getDocument` output + the conversation as\n * context); the executor reads the CURRENT document, builds a validated update\n * proposal (`buildUpdateProposal`), and hands it to the review sink. Its target\n * (and `getDocument`'s) may be omitted to mean the document currently open in\n * the editor — resolved via the {@link ChatToolExecutorOptions.currentDocument}\n * seam ({@link resolveDocumentTarget}); the context block the panel injects is\n * ADVISORY, the resolution here is what makes \"this document\" deterministic.\n *\n * The EC-199 planner enum/schema (`plannerSchema` / `parsePlannerAction`) retired\n * with the hand-rolled orchestrator (EC-334 slice 2).\n */\nimport { z } from 'zod'\nimport { resolveDocumentTitle } from '@elytracms/core/cms-core'\nimport type {\n CmsDocument,\n CollectionDef,\n CollectionRegistry,\n CmsValidationIssue,\n FieldValues,\n LocaleConfig,\n ValidateDocumentOptions,\n} from '@elytracms/core/cms-core'\nimport { parseRichTextValue, richTextValueToMarkdownProse } from '@elytracms/core/rich-text'\nimport type { ComponentManifest } from '@elytracms/core/component-registry'\nimport { applyContentWithShape } from './apply-content'\nimport { translateDocument } from './translate-document'\nimport { buildUpdateProposal } from './update-fields'\nimport type { UpdateFieldChange } from './update-fields'\nimport { aiProvenance } from './provenance'\nimport type { ChatProposal } from './chat-state'\nimport type {\n AiProvider,\n AssetCandidate,\n AssetCandidates,\n RelationCandidate,\n RelationCandidates,\n ResolvedKnowledge,\n} from './types'\n\n/** A document the model may reference as a relation target (from a `searchDocuments` read). */\nexport interface DocumentSearchResult {\n id: string\n title?: string\n}\n\n/** An asset the model may reference (from a `listAssets` read). */\nexport interface AssetSearchResult {\n id: string\n title?: string\n}\n\n/**\n * The client-side, operations-backed readers the executor calls. The studio binds\n * these to the user's authenticated operations client; tests bind an in-memory fake.\n * Reads only — there is no write tool (apply is a separate, human-gated draft write).\n */\nexport interface ChatToolReaders {\n searchDocuments(input: {\n collection: string\n query?: string\n limit?: number\n }): Promise<DocumentSearchResult[]>\n listAssets(input: { query?: string; limit?: number }): Promise<AssetSearchResult[]>\n /**\n * Fetch a full document (values + localized) — the translate flow and the model's\n * `getDocument` tool. Optional: a studio that only wires create can omit it.\n */\n getDocument?(input: { collection: string; id: string }): Promise<CmsDocument | null>\n}\n\n/** Per-search result cap (protects the wire — results become model context). */\nexport const DEFAULT_SEARCH_LIMIT = 20\n\n// --- SDK-native chat tool definitions (EC-334) --------------------------------------\n\n/**\n * One model-facing tool definition for the route's `streamText` call. Structurally\n * the SDK's `FunctionTool` WITHOUT `execute` (execution is client-side, always) —\n * typed locally so this module needs no SDK import.\n */\nexport interface ChatToolDefinition {\n description: string\n /** Zod input schema (the AI SDK accepts zod schemas as `inputSchema` directly). */\n inputSchema: z.ZodType\n}\n\n/**\n * The tool set the native chat branch declares (EC-334). No `execute` on any entry:\n * the SDK forwards each call to the client, which executes reads against the\n * operations layer with the user's session and posts the result back; the terminal\n * tools (`createDocument` / `translateDocument`) run the deterministic engines\n * client-side and return a compact proposal summary as the tool output. The route\n * itself never executes anything.\n *\n * Collections are NOT enum-leashed here — the route has zero data access, so it has\n * no catalog; the catalog reaches the model through the request `context` (system\n * prompt), and every id a proposal references must come from a tool RESULT\n * (client-validated; `applyContentWithShape` still enum-leashes relation ids).\n */\nexport const chatToolDefinitions = {\n searchDocuments: {\n description:\n 'Search documents in a collection. Use it to find the REAL ids a relation may ' +\n 'reference, or to locate an existing document — never invent an id. Returns ' +\n '{ id, title } summaries.',\n inputSchema: z.object({\n collection: z\n .string()\n .describe('Target collection id (from the catalog in the system context).'),\n query: z.string().optional().describe('Optional search text (title match).'),\n limit: z.number().int().positive().optional().describe('Max results.'),\n }),\n },\n listAssets: {\n description:\n 'List or search assets (images, files). Use it to find REAL asset ids — never ' +\n 'invent one. Returns { id, title } summaries.',\n inputSchema: z.object({\n query: z.string().optional().describe('Optional search text.'),\n limit: z.number().int().positive().optional().describe('Max results.'),\n }),\n },\n getDocument: {\n description:\n 'Fetch one full document (values + localized values + block ids) by collection and ' +\n 'id. Omit both to read the document currently open in the editor. Rich-text field ' +\n 'values are returned as markdown strings where faithfully expressible.',\n inputSchema: z.object({\n collection: z\n .string()\n .optional()\n .describe('Collection id. Omit to target the currently open document.'),\n id: z\n .string()\n .optional()\n .describe(\n 'Document id (from a searchDocuments result). Omit to target the currently open document.',\n ),\n }),\n },\n createDocument: {\n description:\n 'Author a NEW document in a collection as a reviewable draft proposal. Terminal ' +\n 'step of a creation request — call it once you have gathered the real ids you ' +\n 'need. Returns a compact proposal summary.',\n inputSchema: z.object({\n collection: z.string().describe('Target collection id.'),\n instruction: z.string().describe('What to write, refining the user request.'),\n }),\n },\n updateFields: {\n description:\n 'Change specific fields of an EXISTING document as a reviewable proposal — THE ' +\n 'tool for every edit request (change / fix / remove / replace / rewrite wording, ' +\n 'values, or a page-builder element). Call getDocument first to see the current ' +\n 'values (and block ids), then pass ONLY the fields that change. A block prop ' +\n '(e.g. a Hero headline) is addressed as field + blockId + prop. Omit collection ' +\n 'and id to target the document currently open in the editor. A rich-text field ' +\n 'takes the FULL new value as a MARKDOWN string (headings, lists, links, bold/italic, ' +\n 'blockquotes — no images, code, tables, or HTML).',\n inputSchema: z.object({\n collection: z\n .string()\n .optional()\n .describe('Collection id. Omit to target the currently open document.'),\n id: z\n .string()\n .optional()\n .describe('Document id. Omit to target the currently open document.'),\n changes: z\n .array(\n z.object({\n field: z\n .string()\n .describe(\n 'Field name from the collection schema. The catalog in the system context ' +\n 'lists every field with its type and the rules its value must satisfy.',\n ),\n locale: z\n .string()\n .optional()\n .describe(\n \"Locale of a LOCALIZED field (defaults to the editor's working locale).\",\n ),\n blockId: z\n .string()\n .optional()\n .describe('For a blocks field: the id of the block to change (from getDocument).'),\n prop: z\n .string()\n .optional()\n .describe('With blockId: the component prop to change.'),\n value: z\n .unknown()\n .optional()\n .describe(\n 'The new value (a rich-text field takes a markdown string). null clears the ' +\n \"field/prop. It MUST satisfy that field's rules as listed in the catalog \" +\n '(length bounds, format, allowed values, date range…); the studio validates ' +\n 'the merged document and rejects the proposal with the failing rules if it ' +\n 'does not. Where the catalog says further rules apply, keep the value ' +\n 'conventional and plausible.',\n ),\n }),\n )\n .min(1)\n .describe('The field changes — only what changes, never the whole document.'),\n }),\n },\n translateDocument: {\n description:\n 'Fill the EMPTY locale values of an existing document (search for it first to ' +\n 'get its id). Only for translation requests — never for edits (use updateFields ' +\n 'to change existing content). Returns a compact proposal summary.',\n inputSchema: z.object({\n collection: z.string().describe('Collection id.'),\n id: z.string().describe('Document id (from a searchDocuments result).'),\n locale: z.string().optional().describe('Target locale (defaults to the active one).'),\n }),\n },\n} satisfies Record<string, ChatToolDefinition>\n\n// --- Client executor map (EC-334) ----------------------------------------------------\n\n/** One forwarded tool call, as `useChat`'s `onToolCall` receives it. */\nexport interface ChatToolCall {\n toolCallId: string\n toolName: string\n input: unknown\n}\n\n/** The coordinates of the document currently open in the editor (EC-378 seam). */\nexport interface CurrentDocumentRef {\n collection: string\n id: string\n}\n\n/**\n * Resolve a tool call's target document from its (optional) input coordinates\n * and the current-document seam (EC-378). Deterministic:\n *\n * - both given → use them verbatim;\n * - both omitted → the currently open document (error when none is open);\n * - `collection` given with `id` omitted → the open document ONLY when the\n * collections match — a named collection must never silently cross onto a\n * different open document.\n *\n * Pure — tested directly; `getDocument` and `updateFields` share it.\n */\nexport function resolveDocumentTarget(\n input: { collection?: string; id?: string },\n current: CurrentDocumentRef | null | undefined,\n): { ok: true; ref: CurrentDocumentRef } | { ok: false; errorText: string } {\n if (input.collection !== undefined && input.id !== undefined) {\n return { ok: true, ref: { collection: input.collection, id: input.id } }\n }\n if (!current) {\n return {\n ok: false,\n errorText:\n 'No target document — pass collection and id, or ask the user to open the document in the editor.',\n }\n }\n if (input.collection !== undefined && input.collection !== current.collection) {\n return {\n ok: false,\n errorText: `The open document is in collection \"${current.collection}\", not \"${input.collection}\" — pass the document id explicitly.`,\n }\n }\n return { ok: true, ref: { collection: current.collection, id: input.id ?? current.id } }\n}\n\n/**\n * A tool execution outcome: an `output` posted back as the tool result, or an\n * `errorText` posted as an `output-error` part (the model sees the failure and\n * can react — never a thrown exception into the stream).\n */\nexport type ChatToolOutcome = { ok: true; output: unknown } | { ok: false; errorText: string }\n\n/**\n * The per-conversation candidate store: every `searchDocuments` result is recorded\n * per relation target collection, every `listAssets` result into one flat library\n * pool (EC-381), and the terminal tools may only reference the ids gathered here\n * (the `applyContentWithShape` / `buildUpdateProposal` enum + drop/loud leash makes\n * a hallucinated relation OR asset id structurally unrepresentable / fail loud).\n * Held by the panel per conversation — a new conversation (EC-380 clear) starts an\n * empty store.\n */\nexport interface ChatCandidateStore {\n record(collection: string, results: readonly DocumentSearchResult[]): void\n /** Record every `listAssets` result into the flat library pool (EC-381). */\n recordAssets(results: readonly AssetSearchResult[]): void\n /** Candidates for a target collection's relation fields, keyed by field name. */\n candidatesFor(collection: CollectionDef): RelationCandidates\n /**\n * Candidates for a target collection's asset/image fields (EC-381), keyed by\n * field name — every asset field shares the one flat searched-asset pool\n * (`listAssets` has no field dimension). Empty when nothing was searched, so the\n * asset field is then omitted from the shape (empty beats wrong).\n */\n assetCandidatesFor(collection: CollectionDef): AssetCandidates\n}\n\nexport function createChatCandidateStore(): ChatCandidateStore {\n const searched = new Map<string, Map<string, DocumentSearchResult>>()\n const searchedAssets = new Map<string, AssetSearchResult>()\n return {\n record(collection, results) {\n const byId = searched.get(collection) ?? new Map<string, DocumentSearchResult>()\n for (const result of results) byId.set(result.id, result)\n searched.set(collection, byId)\n },\n recordAssets(results) {\n for (const result of results) searchedAssets.set(result.id, result)\n },\n candidatesFor(collection) {\n const candidates: RelationCandidates = {}\n for (const field of collection.fields) {\n if (field.type !== 'relation') continue\n const found = searched.get(field.target)\n if (!found || found.size === 0) continue\n candidates[field.name] = [...found.values()].map(\n (r): RelationCandidate => ({ id: r.id, ...(r.title ? { label: r.title } : {}) }),\n )\n }\n return candidates\n },\n assetCandidatesFor(collection) {\n const candidates: AssetCandidates = {}\n if (searchedAssets.size === 0) return candidates\n const pool = [...searchedAssets.values()].map(\n (a): AssetCandidate => ({ id: a.id, ...(a.title ? { label: a.title } : {}) }),\n )\n for (const field of collection.fields) {\n if (field.type !== 'asset') continue\n candidates[field.name] = pool\n }\n return candidates\n },\n }\n}\n\nexport interface ChatToolExecutorOptions {\n /** The (route) provider the terminal engines proxy their model calls through. */\n provider: AiProvider\n registry: CollectionRegistry\n /** The collections the assistant may target (unknown ids fail loud). */\n collections: readonly CollectionDef[]\n /** Operations-backed readers (client-side, user's session). */\n readers: ChatToolReaders\n /** Mint a fresh document id for a new draft (studio: the CMS store's minter). */\n mintDocumentId: (collectionId: string) => string\n /** Per-conversation relation-candidate store (see {@link createChatCandidateStore}). */\n candidates: ChatCandidateStore\n /**\n * Full proposal sink, keyed by `toolCallId` — the review UI's entry point.\n * The tool OUTPUT stays a compact summary; the patch never reaches the model.\n */\n onProposal: (toolCallId: string, proposal: ChatProposal) => void\n /**\n * The document currently open in the editor (EC-378 seam) — a thunk read at\n * EXECUTION time, so \"this document\" resolves against what is open NOW.\n * `getDocument` / `updateFields` fall back to it when the model omits the\n * target (see {@link resolveDocumentTarget}).\n */\n currentDocument?: () => CurrentDocumentRef | null | undefined\n /** Current knowledge snapshot (thunk — the body thunk refreshes it per request). */\n knowledge?: () => ResolvedKnowledge | undefined\n /** Component manifests — context for `allow`-leashed richText/blocks fields. */\n components?: readonly ComponentManifest[]\n /** Target locale for localized fields. */\n locale?: string\n /** Locale config — required for `translateDocument`. */\n localeConfig?: LocaleConfig\n /**\n * THE validation option bag (EC-497) — the CMS session's\n * `gateValidateOptions(...)`, forwarded whole to every engine below. EC-473\n * made the assistant's gate the editor's gate; passing one bag instead of five\n * keys makes it stay that way, because a key can no longer be dropped by being\n * forgotten at one of the three call sites this replaced.\n */\n validateOptions?: ValidateDocumentOptions\n /** Max repair attempts inside `applyContentWithShape`. */\n maxRepairAttempts?: number\n /** Per-search result cap (default {@link DEFAULT_SEARCH_LIMIT}). */\n searchLimit?: number\n}\n\n/** The executor `useChat`'s `onToolCall` dispatches into. */\nexport interface ChatToolExecutor {\n execute(call: ChatToolCall): Promise<ChatToolOutcome>\n}\n\nfunction issueSummary(issues: readonly CmsValidationIssue[]): string {\n return issues\n .slice(0, 5)\n .map((issue) => issue.message)\n .join('; ')\n}\n\n/**\n * The MODEL-facing view of a document (EC-379): rich-text field values are\n * projected to markdown strings — but ONLY when the engine's loss-guarded\n * inverse can express them faithfully (subset-only content, round-trip proven).\n * A document carrying embeds, internal `ref` links, or out-of-subset marks keeps\n * its `{ version, doc }` envelope, so nothing the model later echoes back can\n * silently lose structure. Applies to the `getDocument` TOOL OUTPUT only; the\n * proposal/apply paths always read the raw stored document.\n */\nexport function projectRichTextAsMarkdown(\n document: CmsDocument,\n collection: CollectionDef,\n): CmsDocument {\n const richTextFields = collection.fields\n .filter((field) => field.type === 'richText')\n .map((field) => field.name)\n if (richTextFields.length === 0) return document\n const mapBucket = (bucket: FieldValues): FieldValues => {\n let next = bucket\n for (const name of richTextFields) {\n const value = next[name]\n if (value === undefined || value === null) continue\n const parsed = parseRichTextValue(value)\n if (!parsed.ok) continue\n const markdown = richTextValueToMarkdownProse(parsed.value)\n if (markdown === undefined) continue // not faithfully expressible → keep the envelope\n if (next === bucket) next = { ...bucket }\n next[name] = markdown\n }\n return next\n }\n const values = mapBucket(document.values)\n const localized = document.localized\n ? Object.fromEntries(\n Object.entries(document.localized).map(([locale, bucket]) => [locale, mapBucket(bucket)]),\n )\n : undefined\n return { ...document, values, ...(localized ? { localized } : {}) }\n}\n\n/**\n * Build the client executor map. Every entry validates its input against\n * {@link chatToolDefinitions}' zod schema (the route validates UIMessages only\n * structurally — deep input validation is the client's), executes with the\n * user's session, and returns a JSON-safe outcome. EC-378's `updateFields`\n * lands as one more case in this dispatch.\n */\nexport function createChatToolExecutor(options: ChatToolExecutorOptions): ChatToolExecutor {\n const searchLimit = options.searchLimit ?? DEFAULT_SEARCH_LIMIT\n\n /**\n * EC-497 — the ONE bag every engine below is handed, assembled in exactly one\n * place instead of at each of the three former call sites.\n *\n * The executor's own `localeConfig` is a FLOOR, never an override: the session's\n * bag wins wherever it speaks, but a bag that omits the project's locales must\n * not be able to make the gate locale-blind — a localized bound would then go\n * unchecked, which is precisely the class of hole this task closed.\n */\n const validateOptions: ValidateDocumentOptions = {\n ...(options.localeConfig ? { localeConfig: options.localeConfig } : {}),\n ...(options.validateOptions ?? {}),\n }\n\n function collectionById(id: string): CollectionDef | undefined {\n return options.collections.find((c) => c.id === id)\n }\n\n async function searchDocuments(input: {\n collection: string\n query?: string\n limit?: number\n }): Promise<ChatToolOutcome> {\n if (!collectionById(input.collection)) {\n return { ok: false, errorText: `Unknown collection \"${input.collection}\" — use a collection id from the catalog.` }\n }\n const limit = Math.min(input.limit ?? searchLimit, searchLimit)\n const results = (\n await options.readers.searchDocuments({\n collection: input.collection,\n ...(input.query ? { query: input.query } : {}),\n limit,\n })\n ).slice(0, limit)\n options.candidates.record(input.collection, results)\n return { ok: true, output: results }\n }\n\n async function listAssets(input: { query?: string; limit?: number }): Promise<ChatToolOutcome> {\n const limit = Math.min(input.limit ?? searchLimit, searchLimit)\n const results = (\n await options.readers.listAssets({\n ...(input.query ? { query: input.query } : {}),\n limit,\n })\n ).slice(0, limit)\n // EC-381 — leash asset fields to what the model actually searched, exactly as\n // searchDocuments feeds the relation store.\n options.candidates.recordAssets(results)\n return { ok: true, output: results }\n }\n\n async function getDocument(input: {\n collection?: string\n id?: string\n }): Promise<ChatToolOutcome> {\n if (!options.readers.getDocument) {\n return { ok: false, errorText: 'Reading documents is not available here.' }\n }\n const target = resolveDocumentTarget(input, options.currentDocument?.())\n if (!target.ok) return { ok: false, errorText: target.errorText }\n const document = await options.readers.getDocument(target.ref)\n if (!document) {\n return {\n ok: false,\n errorText: `Document \"${target.ref.collection}/${target.ref.id}\" was not found.`,\n }\n }\n // EC-379: the model reads (and re-authors) rich text as markdown.\n const collection = collectionById(target.ref.collection)\n return { ok: true, output: collection ? projectRichTextAsMarkdown(document, collection) : document }\n }\n\n /** EC-378 — the edit-flow terminal tool: current doc + model-authored changes → validated proposal. */\n async function updateFields(\n toolCallId: string,\n input: { collection?: string; id?: string; changes: UpdateFieldChange[] },\n ): Promise<ChatToolOutcome> {\n if (!options.readers.getDocument) {\n return { ok: false, errorText: 'Reading documents is not available here.' }\n }\n const target = resolveDocumentTarget(input, options.currentDocument?.())\n if (!target.ok) return { ok: false, errorText: target.errorText }\n const collection = collectionById(target.ref.collection)\n if (!collection) {\n return {\n ok: false,\n errorText: `Unknown collection \"${target.ref.collection}\" — use a collection id from the catalog.`,\n }\n }\n const document = await options.readers.getDocument(target.ref)\n if (!document) {\n return {\n ok: false,\n errorText: `Document \"${target.ref.collection}/${target.ref.id}\" was not found.`,\n }\n }\n const result = buildUpdateProposal({\n registry: options.registry,\n collection,\n document,\n changes: input.changes,\n candidates: options.candidates.candidatesFor(collection),\n assetCandidates: options.candidates.assetCandidatesFor(collection),\n ...(options.locale ? { locale: options.locale } : {}),\n validateOptions,\n })\n if (!result.ok) {\n return {\n ok: false,\n errorText: `The proposed changes did not pass validation: ${issueSummary(result.issues)}`,\n }\n }\n if (result.kind === 'no-change') {\n return {\n ok: true,\n output: {\n status: 'no-change',\n note: 'The requested change matches the current values — there is nothing to update.',\n },\n }\n }\n options.onProposal(toolCallId, {\n origin: 'update',\n collection: collection.id,\n documentId: document.id,\n patch: result.patch,\n provenance: aiProvenance(options.provider),\n attempts: 1,\n issues: result.issues,\n before: result.before,\n })\n // Compact summary only — the full patch goes to the review UI, not the model.\n const titleValues = {\n ...result.patch.values,\n ...(options.locale ? result.patch.localized?.[options.locale] : {}),\n }\n return {\n ok: true,\n output: {\n status: 'proposed',\n collection: collection.id,\n documentId: document.id,\n title: resolveDocumentTitle(collection, titleValues, document.id),\n changedFields: result.changedFields,\n note: 'Update proposal created; the editor reviews and applies it in the studio. Summarize the change briefly — do not repeat the content.',\n },\n }\n }\n\n async function createDocument(\n toolCallId: string,\n input: { collection: string; instruction: string },\n ): Promise<ChatToolOutcome> {\n const collection = collectionById(input.collection)\n if (!collection) {\n return { ok: false, errorText: `Unknown collection \"${input.collection}\" — use a collection id from the catalog.` }\n }\n const documentId = options.mintDocumentId(collection.id)\n const knowledge = options.knowledge?.()\n const result = await applyContentWithShape({\n provider: options.provider,\n registry: options.registry,\n collection,\n documentId,\n instruction: input.instruction,\n ...(knowledge ? { knowledge } : {}),\n ...(options.components ? { components: options.components } : {}),\n candidates: options.candidates.candidatesFor(collection),\n // EC-381 — asset/image fields are leashed to searched library assets.\n assetCandidates: options.candidates.assetCandidatesFor(collection),\n ...(options.locale ? { locale: options.locale } : {}),\n validateOptions,\n ...(options.maxRepairAttempts !== undefined\n ? { maxRepairAttempts: options.maxRepairAttempts }\n : {}),\n })\n if (!result.ok) {\n return {\n ok: false,\n errorText: `The drafted ${collection.id} did not pass validation: ${issueSummary(result.issues)}`,\n }\n }\n options.onProposal(toolCallId, {\n origin: 'create',\n collection: collection.id,\n documentId,\n patch: result.patch,\n provenance: result.provenance,\n attempts: result.attempts,\n issues: result.issues,\n })\n // Compact summary only — the full patch goes to the review UI, not the model.\n return {\n ok: true,\n output: {\n status: 'proposed',\n collection: collection.id,\n documentId,\n title: resolveDocumentTitle(collection, result.patch.values, documentId),\n note: 'Draft proposal created; the editor reviews and applies it in the studio. Summarize briefly — do not repeat the content.',\n },\n }\n }\n\n async function translate(\n toolCallId: string,\n input: { collection: string; id: string; locale?: string },\n ): Promise<ChatToolOutcome> {\n const collection = collectionById(input.collection)\n if (!collection) {\n return { ok: false, errorText: `Unknown collection \"${input.collection}\" — use a collection id from the catalog.` }\n }\n if (!options.readers.getDocument || !options.localeConfig) {\n return { ok: false, errorText: 'Translation is not available here.' }\n }\n const document = await options.readers.getDocument({\n collection: input.collection,\n id: input.id,\n })\n if (!document) {\n return { ok: false, errorText: `Document \"${input.collection}/${input.id}\" was not found.` }\n }\n const knowledge = options.knowledge?.()\n const result = await translateDocument({\n provider: options.provider,\n collection,\n document,\n localeConfig: options.localeConfig,\n ...(input.locale ? { targetLocales: [input.locale] } : {}),\n ...(knowledge ? { knowledge } : {}),\n // EC-497: the engine gates its own output; both callers hand it the session\n // registry + THE bag so its verdict is the editor's verdict.\n registry: options.registry,\n validateOptions,\n })\n if (result.total === 0) {\n return {\n ok: true,\n output: {\n status: 'nothing-to-translate',\n note: 'Every locale value is already filled — there is nothing to translate.',\n },\n }\n }\n if (result.translated === 0) {\n return { ok: false, errorText: 'The translation did not produce any content.' }\n }\n // EC-473 established that a translated draft is validated before it is proposed\n // (this path once proposed unvalidated content). EC-497 moved the gate INTO the\n // engine so the detail-editor's header action — the other caller — inherits it\n // instead of writing model output straight to the document. Only errors the\n // translation INTRODUCED block it; pre-existing brokenness is not the\n // assistant's doing.\n if (result.introducedErrors.length > 0) {\n return {\n ok: false,\n errorText: `The translation did not pass validation: ${issueSummary(result.introducedErrors)}`,\n }\n }\n options.onProposal(toolCallId, {\n origin: 'translate',\n collection: collection.id,\n documentId: document.id,\n patch: {\n collection: collection.id,\n id: document.id,\n values: result.document.values,\n ...(result.document.localized ? { localized: result.document.localized } : {}),\n },\n provenance: result.provenance,\n attempts: 1,\n // Warnings ride onto the proposal like every other path's (EC-496 renders them).\n issues: [...result.issues],\n before: {\n values: document.values,\n ...(document.localized ? { localized: document.localized } : {}),\n },\n })\n return {\n ok: true,\n output: {\n status: 'proposed',\n collection: collection.id,\n documentId: document.id,\n translatedUnits: result.translated,\n note: 'Translation proposal created; the editor reviews and applies it in the studio.',\n },\n }\n }\n\n return {\n async execute(call: ChatToolCall): Promise<ChatToolOutcome> {\n const definition = (chatToolDefinitions as Record<string, ChatToolDefinition>)[call.toolName]\n if (!definition) {\n return { ok: false, errorText: `Unknown tool \"${call.toolName}\".` }\n }\n const parsed = definition.inputSchema.safeParse(call.input)\n if (!parsed.success) {\n return { ok: false, errorText: `Invalid ${call.toolName} input: ${parsed.error.message}` }\n }\n try {\n switch (call.toolName) {\n case 'searchDocuments':\n return await searchDocuments(parsed.data as { collection: string; query?: string; limit?: number })\n case 'listAssets':\n return await listAssets(parsed.data as { query?: string; limit?: number })\n case 'getDocument':\n return await getDocument(parsed.data as { collection?: string; id?: string })\n case 'createDocument':\n return await createDocument(\n call.toolCallId,\n parsed.data as { collection: string; instruction: string },\n )\n case 'updateFields':\n return await updateFields(\n call.toolCallId,\n parsed.data as { collection?: string; id?: string; changes: UpdateFieldChange[] },\n )\n case 'translateDocument':\n return await translate(\n call.toolCallId,\n parsed.data as { collection: string; id: string; locale?: string },\n )\n default:\n return { ok: false, errorText: `Unknown tool \"${call.toolName}\".` }\n }\n } catch (error) {\n return {\n ok: false,\n errorText: `The ${call.toolName} step failed: ${error instanceof Error ? error.message : 'unknown error'}`,\n }\n }\n },\n }\n}\n"]}
@@ -1,4 +1,4 @@
1
- import type { CmsDocument, CollectionDef, Locale, LocaleConfig } from '@elytracms/core/cms-core';
1
+ import type { CmsDocument, CmsValidationIssue, CollectionDef, CollectionRegistry, Locale, LocaleConfig, ValidateDocumentOptions } from '@elytracms/core/cms-core';
2
2
  import type { AiProvenance, AiProvider, ResolvedKnowledge } from './types.js';
3
3
  /** Default translatable units per `generate` call. */
4
4
  export declare const DEFAULT_TRANSLATE_BATCH_SIZE = 12;
@@ -28,6 +28,22 @@ export interface TranslateDocumentInput {
28
28
  onProgress?: (progress: TranslateProgress) => void;
29
29
  /** Injected clock for deterministic provenance timestamps in tests. */
30
30
  now?: string;
31
+ /**
32
+ * The registry the OUTPUT is judged against (EC-497). Optional: with none, a
33
+ * single-collection registry over {@link TranslateDocumentInput.collection} is
34
+ * used. That is sound rather than a shortcut, because the verdict this engine
35
+ * publishes is a DELTA (`introducedErrors`, before vs after) and cross-collection
36
+ * findings appear identically on both sides — and a translation only ever writes
37
+ * localized text / rich-text / derived-slug values, never a relation. Both real
38
+ * callers pass the session registry anyway.
39
+ */
40
+ registry?: CollectionRegistry;
41
+ /**
42
+ * THE validation option bag (EC-497) — the studio session's
43
+ * `gateValidateOptions(...)`. Absent ⇒ store-dependent rules simply do not fire,
44
+ * the documented conditionality; everything else still runs.
45
+ */
46
+ validateOptions?: ValidateDocumentOptions;
31
47
  }
32
48
  export interface TranslateProgress {
33
49
  completed: number;
@@ -49,5 +65,23 @@ export interface TranslateDocumentResult {
49
65
  /** Slug fields derived per target locale (not model-translated). */
50
66
  slugsDerived: number;
51
67
  provenance: AiProvenance;
68
+ /**
69
+ * EC-497 — errors this translation INTRODUCED, i.e. present on the produced
70
+ * draft and not on the input. **Non-empty means do not write this document.**
71
+ *
72
+ * The gate lives HERE, in the engine, rather than in each caller: EC-473 put it
73
+ * in the chat tool's executor, and the detail-editor's header action — a second
74
+ * caller of the same engine — therefore upserted model-generated content with no
75
+ * validation at all (`translate-document-dialog.tsx:93`). A gate a caller has to
76
+ * remember is a gate that one caller will not have.
77
+ *
78
+ * It is a DELTA, not "is the document valid": a document may already be broken
79
+ * for reasons that are not the assistant's doing, and refusing to translate it
80
+ * until someone repairs an unrelated field is how an assistant becomes unusable
81
+ * on real content (errors gate publish, not drafts).
82
+ */
83
+ introducedErrors: readonly CmsValidationIssue[];
84
+ /** Surviving non-error findings on the produced draft — carried onto a proposal. */
85
+ issues: readonly CmsValidationIssue[];
52
86
  }
53
87
  export declare function translateDocument(input: TranslateDocumentInput): Promise<TranslateDocumentResult>;