@cloudhal/mcp-server 0.8.0 → 0.10.0

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.
@@ -6,16 +6,27 @@
6
6
  * translation holds only the values sent (absent localized ones fall back to the original's) and
7
7
  * never a copy of a shared value; a language edition copies the source's values (§5: the other
8
8
  * site's definitions may differ).
9
+ *
10
+ * RFC-0045 §3: a localized repeater's text and rich-text sub-values are translated by item path
11
+ * (`speakers/0/talk`), and folded into a copy of the source's list (`translatedItemValues`), so
12
+ * each item keeps its other sub-values. Once the translation holds its own list, that repeater
13
+ * is sent whole: `get_translation_source` hands over the source's whole list for it and names it
14
+ * in `wholeLists`, and a path for it is `translation_shape_mismatch`. A list given whole together
15
+ * with paths must have the source's length; a whole list sent alone is the translation's own and
16
+ * is stored as sent (RFC-0045 "Amended in 7a"). A path names an item by index, so a save with
17
+ * paths names the source revision it was read from (`sourceRev`, `conflict` when it moved on).
9
18
  */
10
-
19
+
11
20
  import {
12
21
  fieldDefinitions,
22
+ itemPathRepeater,
13
23
  refuseFieldIssues,
14
24
  sharedFieldIssues,
15
25
  sharedFieldKeys,
16
- translatableFieldValues,
26
+ translatedItemValues,
27
+ translationSourceValues,
17
28
  } from "@cloudhal/core-ts/fields";
18
-
29
+ import { ToolError } from "./common.js";
19
30
 
20
31
 
21
32
 
@@ -23,33 +34,109 @@ import {
23
34
  export const TRANSLATED_FIELDS = {
24
35
  type: "object",
25
36
  description:
26
- "Entries: your translation of each value in get_translation_source's `fields` (localized text and rich-text fields), by key. Shared fields (`sharedFields`) are read from the original and refused here",
37
+ "Entries: your translation of each value in get_translation_source's `fields` (localized text and rich-text fields), by the same key: a field key, or a repeater item's path such as speakers/0/talk (the item's other values are kept). A repeater named in get_translation_source's `wholeLists` is sent whole instead: the list it returned, its text and rich-text sub-values translated and every other sub-value kept, in the same order, under the repeater's key; item paths for it are refused (translation_shape_mismatch). A whole list sent alone is stored as sent. Shared fields (`sharedFields`) are read from the original and refused here",
27
38
  };
28
39
 
29
- /** What `get_translation_source` returns about an entry's typed fields (nothing for a page). */
40
+ /** `save_translation`'s `sourceRev`. */
41
+ export const SOURCE_REV_INPUT = {
42
+ type: "integer",
43
+ minimum: 0,
44
+ description:
45
+ "Entries: get_translation_source's sourceRev. Required when fields has repeater item paths, which name items by index: refused (conflict) when the source changed since. Send it with a repeater's whole list (wholeLists) too: given, it is checked the same way, so a list read before the source changed is not saved as current",
46
+ };
47
+
48
+ /** True when `values` names a repeater item (`speakers/0/talk`); other keys with `/` are plain. */
49
+ const hasItemPaths = (defs , values ) =>
50
+ !!values &&
51
+ typeof values === "object" &&
52
+ Object.keys(values).some((k) => itemPathRepeater(defs, k) !== undefined);
53
+
54
+ /** True when `values` gives a repeater's list whole (`wholeLists`). */
55
+ const hasWholeLists = (defs , values ) =>
56
+ !!values &&
57
+ typeof values === "object" &&
58
+ Object.entries(values).some(
59
+ ([k, v]) => Array.isArray(v) && defs.some((d) => d.key === k && d.type === "repeater"),
60
+ );
61
+
62
+ /**
63
+ * Item paths need the source revision they were read from, unchanged (RFC-0045 §3). A whole
64
+ * repeater list is checked against `sourceRev` when one is given (#221), so a list read before the
65
+ * source changed is not linked to its new revision; without one it is saved as before.
66
+ */
67
+ export function checkSourceRev(
68
+ a ,
69
+ site ,
70
+ source ,
71
+ ) {
72
+ if (source.collection === undefined) return;
73
+ const defs = fieldDefinitions(site, source.collection);
74
+ const paths = hasItemPaths(defs, a.fields);
75
+ if (!paths && !(typeof a.sourceRev === "number" && hasWholeLists(defs, a.fields))) return;
76
+ if (typeof a.sourceRev !== "number")
77
+ throw new ToolError(
78
+ "invalid",
79
+ "fields has repeater item paths: pass sourceRev from get_translation_source",
80
+ );
81
+ if (a.sourceRev !== source.rev)
82
+ throw new ToolError(
83
+ "conflict",
84
+ `${source.id} is at rev ${source.rev}, its items were read at rev ${a.sourceRev}; call get_translation_source again and translate its current items`,
85
+ );
86
+ }
87
+
88
+ /**
89
+ * What `get_translation_source` returns about an entry's typed fields (nothing for a page). A
90
+ * localized repeater the existing translation (`existing`, the one `save_translation` writes)
91
+ * already holds its own list of comes whole, named in `wholeLists` (#218).
92
+ */
30
93
  export function translationSourceFields(
31
94
  site ,
32
95
  source ,
33
- ) {
96
+ existing ,
97
+ )
98
+
99
+
100
+
101
+
102
+ {
34
103
  if (!("collection" in source)) return { source };
35
104
  const entry = source ;
36
105
  const defs = fieldDefinitions(site, entry.collection);
37
106
  if (defs.length === 0) return { source };
38
107
  const { fields, ...rest } = entry;
39
108
  const shared = sharedFieldKeys(defs);
109
+ const own = existing && "fields" in existing ? (existing ).fields : undefined;
110
+ const values = translationSourceValues(defs, fields, own);
40
111
  return {
41
112
  source: rest,
42
- fields: translatableFieldValues(defs, fields),
113
+ fields: values.fields,
114
+ ...(values.whole.length > 0 ? { wholeLists: values.whole } : {}),
43
115
  ...(shared.length > 0 ? { sharedFields: shared } : {}),
44
116
  };
45
117
  }
46
118
 
47
- /** The precheck: translated values of shared fields are refused, before anything is written. */
48
- export function refuseSharedFields(site , source , values ) {
119
+ /**
120
+ * The precheck: translated values of shared fields, and item paths that do not fit (see
121
+ * `translatedItemValues`), are refused before anything is written.
122
+ */
123
+ export function refuseSharedFields(
124
+ site ,
125
+ source ,
126
+ values ,
127
+ existing ,
128
+ ) {
49
129
  if (!values || typeof values !== "object") return;
50
130
  const defs = fieldDefinitions(site, source.collection);
51
131
  const original = source.translation?.group ?? source.id;
52
- refuseFieldIssues(sharedFieldIssues(defs, values , original));
132
+ const shared = sharedFieldIssues(defs, values , original);
133
+ const input = {
134
+ source: source.fields,
135
+ existing: existing?.fields,
136
+ };
137
+ const items = translatedItemValues(defs, input, values ).issues;
138
+ const sharedPaths = new Set(shared.map((i) => i.path));
139
+ refuseFieldIssues([...shared, ...items.filter((i) => !sharedPaths.has(i.path))]);
53
140
  }
54
141
 
55
142
  /** The `fields` of a translated entry: the sent values on what the translation may start from. */
@@ -64,5 +151,14 @@ export function translatedFields(input
64
151
  const { site, source, existing, given, cross } = input;
65
152
  const defined = fieldDefinitions(site, source.collection).length > 0;
66
153
  const base = existing?.fields ?? (cross || !defined ? (source.fields ?? {}) : {});
67
- return { ...base, ...(given ?? {}) };
154
+ if (!given) return { ...base };
155
+ const defs = fieldDefinitions(site, source.collection);
156
+ const lists = {
157
+ source: source.fields,
158
+ existing: existing?.fields,
159
+ };
160
+ // The source may have changed since the precheck: refuse, never drop a path.
161
+ const folded = translatedItemValues(defs, lists, given);
162
+ refuseFieldIssues(folded.issues);
163
+ return { ...base, ...folded.values };
68
164
  }
@@ -36,7 +36,9 @@ import {
36
36
  } from "./common.js";
37
37
  import { ENTRY, mcpWriter, PAGE, SNAPSHOT, sectionInput } from "./content-shared.js";
38
38
  import {
39
+ checkSourceRev,
39
40
  refuseSharedFields,
41
+ SOURCE_REV_INPUT,
40
42
  TRANSLATED_FIELDS,
41
43
  translatedFields,
42
44
  translationSourceFields,
@@ -107,7 +109,7 @@ const getSourceTool = {
107
109
  name: "get_translation_source",
108
110
  title: "Get translation source",
109
111
  description:
110
- "What you need to translate a page or entry into `locale`: the source (translate its title, text, summary and SEO; keep section ids, kinds, links, numbers, images and bindings), for an entry with typed fields the localized text and rich-text values to translate (`fields`) and the shared fields a translation reads from the original (`sharedFields`, never translated), the glossary terms it uses (doNotTranslate terms stay as written, given translations are used), and the current translation in that locale if there is one (with `stale` when the source changed since, and for a linked page whose source changed its layout, `layout`: the translation's sections on the new layout, with the `untranslated` ones to translate). Then call save_translation.",
112
+ "What you need to translate a page or entry into `locale`: the source (translate its title, text, summary and SEO; keep section ids, kinds, links, numbers, images and bindings), for an entry with typed fields the localized text and rich-text values to translate (`fields`, repeater items by path such as speakers/0/talk; a localized repeater whose translation already holds its own list comes whole under its key and is named in `wholeLists`: send it back whole, with sourceRev) and the shared fields a translation reads from the original (`sharedFields`, never translated), the glossary terms it uses (doNotTranslate terms stay as written, given translations are used), and the current translation in that locale if there is one (with `stale` when the source changed since, and for a linked page whose source changed its layout, `layout`: the translation's sections on the new layout, with the `untranslated` ones to translate). Then call save_translation.",
111
113
  inputSchema: obj(
112
114
  {
113
115
  siteId: id,
@@ -128,7 +130,18 @@ const getSourceTool = {
128
130
  fields: {
129
131
  type: "object",
130
132
  description:
131
- "Entries: the localized text and rich-text field values to translate, by key; send them translated as save_translation's `fields`",
133
+ "Entries: the localized text and rich-text field values to translate, by key, and a localized repeater's by item path (speakers/0/talk); send them translated, under the same keys, as save_translation's `fields`. A repeater named in `wholeLists` comes whole instead: the source's list under its key, in the source's order, with every sub-value",
134
+ },
135
+ wholeLists: {
136
+ type: "array",
137
+ items: { type: "string" },
138
+ description:
139
+ "Entries: the localized repeaters the existing translation already holds its own list of. Item paths cannot name their items, so `fields` has the source's whole list for each: translate its text and rich-text sub-values, keep the others, and send the whole list back under the same key, in the same order, with sourceRev (item paths for it are refused, translation_shape_mismatch)",
140
+ },
141
+ sourceRev: {
142
+ type: "integer",
143
+ description:
144
+ "Entries: the source's revision these values were read at; save_translation takes it back with repeater item paths",
132
145
  },
133
146
  sharedFields: {
134
147
  type: "array",
@@ -155,10 +168,8 @@ const getSourceTool = {
155
168
  if (!target.locales.includes(locale))
156
169
  throw new ToolError("locale_not_enabled", `${locale} is not one of the site's locales`);
157
170
  const source = await nodeOf(ctx, site.id, a.nodeId );
158
- const group = await ctx.store.translationGroup(site, source.translation?.group ?? source.id);
159
- const member = group.members.find((m) => m.locale === locale && m.siteId === target.id);
160
- const existing = member ? await nodeOf(ctx, target.id, member.id) : undefined;
161
- const typed = translationSourceFields(site, source);
171
+ const { member, existing } = await translationOf(ctx, { site, target, source, locale });
172
+ const typed = translationSourceFields(site, source, existing);
162
173
  const text = JSON.stringify(
163
174
  "sections" in source ? source.sections : [source.body.value, typed.fields ?? {}],
164
175
  );
@@ -172,6 +183,7 @@ const getSourceTool = {
172
183
  : undefined;
173
184
  return {
174
185
  ...typed,
186
+ ...("collection" in source ? { sourceRev: source.rev } : {}),
175
187
  glossary: glossaryFor(
176
188
  await ctx.store.glossary.get(site.id),
177
189
  `${source.title} ${text}`,
@@ -187,7 +199,7 @@ const saveTranslationTool = {
187
199
  name: "save_translation",
188
200
  title: "Save translation",
189
201
  description:
190
- "Save your translation of a page or entry into `locale` as a draft linked to the source's current revision, with your model as its provenance. Pages: send title, path and sections (same ids, kinds and order as the source; translated text). Entries: title, slug, body, summary and the translated `fields` (only the localized text and rich-text ones get_translation_source returns; shared fields are refused with field_not_localized). The checks (structure, links, numbers, glossary, length) run and are returned; a failed check keeps it from being published until fixed. A person reviews it in Studio before it goes live where the site asks for review. Refused for a translation a person wrote or reviewed. Snapshots afterwards.",
202
+ "Save your translation of a page or entry into `locale` as a draft linked to the source's current revision, with your model as its provenance. Pages: send title, path and sections (same ids, kinds and order as the source; translated text). Entries: title, slug, body, summary and the translated `fields` (only the localized text and rich-text ones get_translation_source returns, under its keys, repeater items by path with get_translation_source's sourceRev, or a repeater named in its `wholeLists` as the whole list; shared fields are refused with field_not_localized). The checks (structure, links, numbers, glossary, length) run and are returned; a failed check keeps it from being published until fixed. A person reviews it in Studio before it goes live where the site asks for review. Refused for a translation a person wrote or reviewed. Snapshots afterwards.",
191
203
  inputSchema: obj(
192
204
  {
193
205
  siteId: id,
@@ -200,6 +212,7 @@ const saveTranslationTool = {
200
212
  body: str("Entries: the translated markdown body"),
201
213
  summary: str("Entries: the translated summary"),
202
214
  fields: TRANSLATED_FIELDS,
215
+ sourceRev: SOURCE_REV_INPUT,
203
216
  coverAlt: str(
204
217
  "Entries: the cover image's alt text in `locale`. Without it a new translation leaves the cover alt empty, so the image's own alt text in `locale` (update_asset) is shown",
205
218
  { maxLength: 500 },
@@ -234,7 +247,12 @@ const saveTranslationTool = {
234
247
  if (a.fields === undefined) return;
235
248
  const site = await ctx.store.getSite(a.siteId );
236
249
  const source = await ctx.store.getContentEntry(a.nodeId );
237
- if (site && source?.siteId === site.id) refuseSharedFields(site, source, a.fields);
250
+ if (!site || source?.siteId !== site.id) return;
251
+ checkSourceRev(a, site, source);
252
+ const target = await targetOf(ctx, site, a.targetSiteId);
253
+ const locale = a.locale ;
254
+ const { existing } = await translationOf(ctx, { site, target, source, locale });
255
+ refuseSharedFields(site, source, a.fields, existing );
238
256
  },
239
257
  handler: async (a, ctx) => {
240
258
  const site = await requireSite(ctx, a.siteId );
@@ -246,6 +264,8 @@ const saveTranslationTool = {
246
264
  let source = await nodeOf(ctx, site.id, a.nodeId );
247
265
  if (source.locale === locale)
248
266
  throw new ToolError("invalid", "the translation's locale differs from its source's");
267
+ // RFC-0045 §3: item paths were read at a revision; checked before marking the original.
268
+ checkSourceRev(a, site, source );
249
269
  // The source becomes the group's original the first time it is translated (a new revision).
250
270
  if (!source.translation) source = await markOriginal(ctx, source);
251
271
  const group = await ctx.store.translationGroup(site, source.translation?.group ?? source.id);
@@ -331,6 +351,18 @@ const saveTranslationTool = {
331
351
  * The site a translation lives on: `siteId`'s own, or with `targetSiteId` another site of the same
332
352
  * project (a language edition, RFC-0034 §6) the caller may write to.
333
353
  */
354
+ /** The group member in `locale` on `target`, and the node, when the translation exists. */
355
+ async function translationOf(
356
+ ctx ,
357
+ at ,
358
+ ) {
359
+ const { site, target, source, locale } = at;
360
+ const group = await ctx.store.translationGroup(site, source.translation?.group ?? source.id);
361
+ const member = group.members.find((m) => m.locale === locale && m.siteId === target.id);
362
+ const existing = member ? await nodeOf(ctx, target.id, member.id) : undefined;
363
+ return { group, member, existing };
364
+ }
365
+
334
366
  async function targetOf(ctx , site , targetSiteId ) {
335
367
  if (typeof targetSiteId !== "string" || targetSiteId === site.id) return site;
336
368
  const target = await ctx.store.getSite(targetSiteId);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cloudhal/mcp-server",
3
- "version": "0.8.0",
3
+ "version": "0.10.0",
4
4
  "description": "Cloudhal MCP server: semantic site tools (pages, articles, snapshots, preview, publish) governed by the policy engine",
5
5
  "license": "Apache-2.0",
6
6
  "homepage": "https://cloudhal.dev",
@@ -56,15 +56,15 @@
56
56
  "README.md"
57
57
  ],
58
58
  "dependencies": {
59
- "@cloudhal/analytics": "0.8.0",
60
- "@cloudhal/agent-runtime": "0.8.0",
61
- "@cloudhal/astro-renderer": "0.8.0",
62
- "@cloudhal/comments": "0.8.0",
63
- "@cloudhal/core-contracts": "0.8.0",
64
- "@cloudhal/core-ts": "0.8.0",
65
- "@cloudhal/storage": "0.8.0",
59
+ "@cloudhal/analytics": "0.10.0",
60
+ "@cloudhal/agent-runtime": "0.10.0",
61
+ "@cloudhal/astro-renderer": "0.10.0",
62
+ "@cloudhal/comments": "0.10.0",
63
+ "@cloudhal/core-contracts": "0.10.0",
64
+ "@cloudhal/core-ts": "0.10.0",
65
+ "@cloudhal/storage": "0.10.0",
66
66
  "@modelcontextprotocol/server": "2.3.1",
67
- "@cloudhal/schedules": "0.8.0"
67
+ "@cloudhal/schedules": "0.10.0"
68
68
  },
69
69
  "devDependencies": {
70
70
  "@cloudflare/workers-types": "^5.20261007.1",