@nocode-factory/sanity-kit 2.65.0-rc.1 → 2.65.0-rc.3

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nocode-factory/sanity-kit",
3
- "version": "2.65.0-rc.1",
3
+ "version": "2.65.0-rc.3",
4
4
  "description": "Outils Studio de NoCode Factory : Pages (liste + board), Composants, Historique",
5
5
  "license": "UNLICENSED",
6
6
  "private": false,
@@ -7,7 +7,7 @@ import {useKitClient} from '../hooks/useKitClient'
7
7
  import {hasChanges, propagate, type PropagationResult, type SchemaTypeLike} from '../lib/propagate'
8
8
  import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
9
9
  import {collectTexts, setAtKey, valueAtKey} from '../lib/aiTexts'
10
- import {translateValues} from '../lib/aiTranslate'
10
+ import {translateValuesSettled} from '../lib/aiTranslate'
11
11
  import {createTranslatedVersion, translationsOf} from '../lib/translateDocument'
12
12
  import {isReplaceable, readTrace, recordTrace} from '../lib/translationTrace'
13
13
  import {localizeReferences} from '../lib/localizeReferences'
@@ -204,10 +204,17 @@ function PropagateDialog(props: {
204
204
  const values = toDo.map((path) => valueAtKey(after, path))
205
205
  // Texte vidé dans la version principale : vidé aussi (suit la valeur par défaut).
206
206
  const toSend = values.map((v) => (collectTexts(v).some((s) => s.trim()) ? v : undefined))
207
- const translated = await translateValues(config.history, source, t.language, toSend.map((v) => v ?? ''))
207
+ // Traduction partielle : un texte raté reste « To translate », les autres sont gardés.
208
+ const result = await translateValuesSettled(config.history, source, t.language, toSend.map((v) => v ?? ''))
209
+ const missed = new Set(result.failed)
210
+ if (result.error) {
211
+ failed = true
212
+ aiError = result.error
213
+ }
208
214
  const done = new Set<string>()
209
215
  toDo.forEach((path, n) => {
210
- const value = toSend[n] === undefined ? undefined : translated[n]
216
+ if (missed.has(n)) return
217
+ const value = toSend[n] === undefined ? undefined : result.values[n]
211
218
  if (setAtKey(doc, path, value)) {
212
219
  done.add(path)
213
220
  if (value !== undefined) written.push({path, value})
@@ -6,8 +6,11 @@ export interface TranslateService {
6
6
  projectKey: string
7
7
  }
8
8
 
9
- const MAX_TEXTS = 250
10
- const MAX_CHARS = 30_000
9
+ // Lots MODESTES (kit >= 2.65.0) : au-delà, la réponse de l'IA arrive parfois incomplète ou
10
+ // mal formée (« bad_model_output », vu sur JAF avec 66 textes d'une page) et tout le lot
11
+ // échouait. Un lot qui échoue est redécoupé en deux, jusqu'au texte seul.
12
+ const MAX_TEXTS = 30
13
+ const MAX_CHARS = 6_000
11
14
 
12
15
  const MESSAGES: Record<string, string> = {
13
16
  daily_limit: 'Daily AI translation limit reached for this project.',
@@ -15,6 +18,19 @@ const MESSAGES: Record<string, string> = {
15
18
  bad_key: 'This project is not registered in the NCF kit (npx ncf-kit setup).',
16
19
  not_configured: 'AI translation is not configured on the NCF kit service.',
17
20
  too_long: 'Text too long to translate at once.',
21
+ bad_model_output: 'The AI returned an unreadable answer.',
22
+ }
23
+
24
+ /** Erreurs qui ne se règlent pas en redécoupant le lot (inutile de réessayer texte par texte). */
25
+ const FATAL = new Set(['daily_limit', 'project_inactive', 'bad_key', 'not_configured'])
26
+
27
+ class TranslateError extends Error {
28
+ constructor(
29
+ message: string,
30
+ readonly code: string,
31
+ ) {
32
+ super(message)
33
+ }
18
34
  }
19
35
 
20
36
  async function call(service: TranslateService, from: string, to: string, texts: string[]): Promise<string[]> {
@@ -24,24 +40,52 @@ async function call(service: TranslateService, from: string, to: string, texts:
24
40
  body: JSON.stringify({key: service.projectKey, from, to, texts}),
25
41
  })
26
42
  const body = (await response.json().catch(() => ({}))) as {texts?: string[]; error?: string}
27
- if (!response.ok || !Array.isArray(body.texts) || body.texts.length !== texts.length)
28
- throw new Error(MESSAGES[body.error ?? ''] ?? `AI translation failed (${body.error ?? response.status}).`)
43
+ if (!response.ok || !Array.isArray(body.texts) || body.texts.length !== texts.length) {
44
+ const code = body.error ?? (response.ok ? 'bad_model_output' : String(response.status))
45
+ throw new TranslateError(MESSAGES[code] ?? `AI translation failed (${code}).`, code)
46
+ }
29
47
  return body.texts
30
48
  }
31
49
 
32
50
  /**
33
- * Traduit des valeurs de champs (chaînes ou textes riches) de `from` vers `to` :
34
- * mêmes valeurs, textes traduits. Découpe en lots si besoin.
51
+ * Traduit un lot ; s'il échoue (réponse mal formée, texte trop long...), le redécoupe en
52
+ * deux jusqu'au texte seul. Rend une traduction par texte, ou null pour un texte raté.
35
53
  */
36
- export async function translateValues(service: TranslateService, from: string, to: string, values: readonly unknown[]): Promise<unknown[]> {
54
+ async function translateBatch(service: TranslateService, from: string, to: string, texts: string[], errors: string[]): Promise<(string | null)[]> {
55
+ try {
56
+ return await call(service, from, to, texts)
57
+ } catch (cause) {
58
+ const error = cause instanceof TranslateError ? cause : new TranslateError(cause instanceof Error ? cause.message : String(cause), 'unknown')
59
+ if (FATAL.has(error.code)) throw error
60
+ if (texts.length === 1) {
61
+ errors.push(error.message)
62
+ return [null]
63
+ }
64
+ const half = Math.ceil(texts.length / 2)
65
+ return [...(await translateBatch(service, from, to, texts.slice(0, half), errors)), ...(await translateBatch(service, from, to, texts.slice(half), errors))]
66
+ }
67
+ }
68
+
69
+ /**
70
+ * Traduit des valeurs de champs (chaînes ou textes riches) de `from` vers `to`, en lots,
71
+ * SANS tout perdre si une partie échoue : rend les valeurs traduites, les rangs des
72
+ * valeurs ratées (laissées telles quelles) et la raison de l'échec.
73
+ */
74
+ export async function translateValuesSettled(
75
+ service: TranslateService,
76
+ from: string,
77
+ to: string,
78
+ values: readonly unknown[],
79
+ ): Promise<{values: unknown[]; failed: number[]; error: string | null}> {
37
80
  const perValue = values.map((v) => collectTexts(v))
38
81
  const all = perValue.flat()
39
- const translated: string[] = []
82
+ const translated: (string | null)[] = []
83
+ const errors: string[] = []
40
84
  let batch: string[] = []
41
85
  let size = 0
42
86
  const flush = async () => {
43
87
  if (!batch.length) return
44
- translated.push(...(await call(service, from, to, batch)))
88
+ translated.push(...(await translateBatch(service, from, to, batch, errors)))
45
89
  batch = []
46
90
  size = 0
47
91
  }
@@ -52,10 +96,27 @@ export async function translateValues(service: TranslateService, from: string, t
52
96
  }
53
97
  await flush()
54
98
  let offset = 0
55
- return values.map((value, n) => {
99
+ const failed: number[] = []
100
+ const out = values.map((value, n) => {
56
101
  const count = perValue[n]!.length
57
102
  const slice = translated.slice(offset, offset + count)
58
103
  offset += count
59
- return count ? replaceTexts(value, slice) : value
104
+ if (!count) return value
105
+ if (slice.some((t) => t === null)) {
106
+ failed.push(n)
107
+ return value
108
+ }
109
+ return replaceTexts(value, slice as string[])
60
110
  })
111
+ return {values: out, failed, error: failed.length ? `${failed.length} text(s) not translated: ${[...new Set(errors)].join(' ')}` : null}
112
+ }
113
+
114
+ /**
115
+ * Traduit des valeurs de champs (chaînes ou textes riches) de `from` vers `to` :
116
+ * mêmes valeurs, textes traduits. Échoue si une valeur n'a pas pu être traduite.
117
+ */
118
+ export async function translateValues(service: TranslateService, from: string, to: string, values: readonly unknown[]): Promise<unknown[]> {
119
+ const result = await translateValuesSettled(service, from, to, values)
120
+ if (result.error) throw new Error(result.error)
121
+ return result.values
61
122
  }
@@ -4,7 +4,7 @@ import type {SanityClient} from 'sanity'
4
4
  import type {ResolvedKitConfig} from '../config/types'
5
5
  import {TRANSLATION_TODO_TYPE, translationTodoId} from '../schema/translationTodo'
6
6
  import {collectTexts, setAtKey, valueAtKey} from './aiTexts'
7
- import {translateValues} from './aiTranslate'
7
+ import {translateValuesSettled} from './aiTranslate'
8
8
  import {propagate, type SchemaTypeLike} from './propagate'
9
9
  import {slugify} from './slugify'
10
10
  import {hashValue, recordTrace} from './translationTrace'
@@ -113,9 +113,13 @@ export async function createTranslatedVersion(opts: {
113
113
  try {
114
114
  const values = remaining.map((path) => valueAtKey(source, path))
115
115
  const toSend = values.map((v) => (collectTexts(v).some((s) => s.trim()) ? v : undefined))
116
- const translated = await translateValues(config.history, sourceLanguage, language, toSend.map((v) => v ?? ''))
116
+ // Traduction partielle : un texte raté reste « To translate », les autres sont gardés.
117
+ const result = await translateValuesSettled(config.history, sourceLanguage, language, toSend.map((v) => v ?? ''))
118
+ const failed = new Set(result.failed)
119
+ if (result.error) aiError = result.error
117
120
  remaining = remaining.filter((path, n) => {
118
- const value = toSend[n] === undefined ? undefined : translated[n]
121
+ if (failed.has(n)) return true
122
+ const value = toSend[n] === undefined ? undefined : result.values[n]
119
123
  const ok = setAtKey(doc, path, value)
120
124
  if (ok && value !== undefined) written.push({path, value})
121
125
  return !ok
@@ -182,27 +186,31 @@ export async function fillTranslation(opts: {
182
186
  target: Doc
183
187
  sourceLanguage: string
184
188
  language: string
185
- }): Promise<{document: Doc; translated: number; written: {path: string; value: unknown}[]}> {
186
- const {config, schemaType, source, sourceLanguage, language} = opts
189
+ /** Trace de l'IA (readTrace) : un texte déjà rendu à l'identique n'est pas renvoyé. */
190
+ trace?: Map<string, string>
191
+ }): Promise<{document: Doc; translated: number; written: {path: string; value: unknown}[]; failed: number; error: string | null}> {
192
+ const {config, schemaType, source, sourceLanguage, language, trace} = opts
187
193
  const document = structuredClone(opts.target) as Doc
188
194
  // Blocs (champs de premier niveau) absents de la cible : recopiés tels quels.
189
195
  for (const [key, value] of Object.entries(source)) {
190
196
  if (key.startsWith('_') || key === (config.i18n?.field ?? 'language')) continue
191
197
  if (document[key] === undefined || document[key] === null) document[key] = structuredClone(value)
192
198
  }
193
- const paths = untranslatedPaths(source, document, schemaType)
194
- if (!paths.length || !config.history) return {document, translated: 0, written: []}
195
- const translated = await translateValues(
199
+ const paths = untranslatedPaths(source, document, schemaType, trace)
200
+ if (!paths.length || !config.history) return {document, translated: 0, written: [], failed: 0, error: null}
201
+ // Traduction partielle : un texte raté reste tel quel (reproposé au prochain passage).
202
+ const result = await translateValuesSettled(
196
203
  config.history,
197
204
  sourceLanguage,
198
205
  language,
199
206
  paths.map((p) => valueAtKey(source, p)),
200
207
  )
208
+ const failed = new Set(result.failed)
201
209
  const written: {path: string; value: unknown}[] = []
202
210
  paths.forEach((p, n) => {
203
- if (setAtKey(document, p, translated[n])) written.push({path: p, value: translated[n]})
211
+ if (!failed.has(n) && setAtKey(document, p, result.values[n])) written.push({path: p, value: result.values[n]})
204
212
  })
205
- return {document, translated: written.length, written}
213
+ return {document, translated: written.length, written, failed: failed.size, error: result.error}
206
214
  }
207
215
 
208
216
  /**
@@ -253,9 +261,12 @@ export async function retryTranslatedVersion(opts: {
253
261
  try {
254
262
  const values = remaining.map((path) => valueAtKey(source, path))
255
263
  const toSend = values.map((v) => (collectTexts(v).some((s) => s.trim()) ? v : undefined))
256
- const translated = await translateValues(config.history, todo.sourceLanguage, language, toSend.map((v) => v ?? ''))
264
+ const result = await translateValuesSettled(config.history, todo.sourceLanguage, language, toSend.map((v) => v ?? ''))
265
+ const failed = new Set(result.failed)
266
+ if (result.error) aiError = result.error
257
267
  remaining = remaining.filter((path, n) => {
258
- const value = toSend[n] === undefined ? undefined : translated[n]
268
+ if (failed.has(n)) return true
269
+ const value = toSend[n] === undefined ? undefined : result.values[n]
259
270
  const ok = setAtKey(doc, path, value)
260
271
  if (ok && value !== undefined) written.push({path, value})
261
272
  return !ok
@@ -94,8 +94,8 @@ export function LanguagesSync() {
94
94
  // Versions traduites inachevées : liste « To translate » non vide.
95
95
  const unfinished = await client.fetch<Todo[]>(
96
96
  `*[_type == $todo && count(paths) > 0]{documentId, sourceId, sourceLanguage, paths,
97
- "language": coalesce(*[_id == "drafts." + ^.documentId][0].${field}, *[_id == ^.documentId][0].${field}),
98
- "title": coalesce(*[_id == "drafts." + ^.documentId][0].${TITLE}, *[_id == ^.documentId][0].${TITLE})}`,
97
+ "version": coalesce(*[_id == "drafts." + ^.documentId][0], *[_id == ^.documentId][0]){"language": ${field}, "title": ${TITLE}}}
98
+ {documentId, sourceId, sourceLanguage, paths, "language": version.language, "title": version.title}`,
99
99
  {todo: TRANSLATION_TODO_TYPE},
100
100
  )
101
101
  // Versions qui n'existent qu'en brouillon (traduites, pas encore publiées).
@@ -222,7 +222,15 @@ export function LanguagesSync() {
222
222
  const id = String(published._id)
223
223
  // Brouillon : complété depuis le brouillon en cours s'il existe (rien n'est défait).
224
224
  const base = (await client.getDocument<Doc>(`drafts.${id}`)) ?? published
225
- const filled = await fillTranslation({config, schemaType: typeOf(defaultsType), source: src, target: base, sourceLanguage: d.from, language: d.language})
225
+ const filled = await fillTranslation({
226
+ config,
227
+ schemaType: typeOf(defaultsType),
228
+ source: src,
229
+ target: base,
230
+ sourceLanguage: d.from,
231
+ language: d.language,
232
+ trace: await readTrace(client, id),
233
+ })
226
234
  const {_rev, _updatedAt, _createdAt, ...doc} = filled.document
227
235
  const tx = client.transaction()
228
236
  if (publishNow) {
@@ -233,6 +241,7 @@ export function LanguagesSync() {
233
241
  await recordTrace(client, id, filled.written, {sourceId: String(src._id), sourceLanguage: d.from})
234
242
  // Textes rendus à l'identique par l'IA (noms propres, marques) : signalés à vérifier.
235
243
  const unchanged = filled.written.filter((w) => JSON.stringify(w.value) === JSON.stringify(valueAtKey(src, w.path))).length
244
+ if (filled.error) fail(what, `${filled.error} The other texts were kept; retry to translate the rest.`)
236
245
  lines.push({
237
246
  tone: publishNow ? 'positive' : 'caution',
238
247
  text: `${what}: ${filled.translated} text(s) translated${publishNow ? ' and published' : ' in the draft, to review then publish'}${unchanged ? ` (${unchanged} kept identical by the AI: names, brands... check them)` : ''}`,
package/src/version.ts CHANGED
@@ -3,4 +3,4 @@
3
3
  * quelle version. Recopiée de `package.json` par `npm version` (scripts/sync-version.mjs) ; un test
4
4
  * le vérifie.
5
5
  */
6
- export const KIT_VERSION = '2.65.0-rc.1'
6
+ export const KIT_VERSION = '2.65.0-rc.3'