@nocode-factory/sanity-kit 2.64.0-rc.3 → 2.65.0-rc.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/bin/ncf-kit.mjs +25 -5
- package/dist/index.d.ts +1 -1
- package/dist/index.js +379 -144
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/actions/PropagatePublish.tsx +19 -6
- package/src/lib/translateDocument.ts +71 -7
- package/src/tools/pages/LanguagesSync.tsx +236 -112
- package/src/version.ts +1 -1
package/package.json
CHANGED
|
@@ -153,8 +153,10 @@ function PropagateDialog(props: {
|
|
|
153
153
|
const [busy, setBusy] = useState(false)
|
|
154
154
|
const toast = useToast()
|
|
155
155
|
const [error, setError] = useState<string | null>(null)
|
|
156
|
-
// Traduction par l'IA (service du kit)
|
|
156
|
+
// Traduction par l'IA (service du kit). Publication directe : seulement sur demande
|
|
157
|
+
// (kit >= 2.65.0) ; sinon chaque langue est un BROUILLON à relire puis publier.
|
|
157
158
|
const [ai, setAi] = useState(Boolean(config.history))
|
|
159
|
+
const [publishNow, setPublishNow] = useState(false)
|
|
158
160
|
// Textes modifiés à la main dans une langue : gardés, sauf demande explicite.
|
|
159
161
|
const [overwrite, setOverwrite] = useState(false)
|
|
160
162
|
const editedCount = targets.reduce((n, t) => n + (t.selected ? t.edited.length : 0), 0)
|
|
@@ -180,6 +182,7 @@ function PropagateDialog(props: {
|
|
|
180
182
|
sourceLanguage: source,
|
|
181
183
|
language: t.language,
|
|
182
184
|
ai,
|
|
185
|
+
publish: publishNow,
|
|
183
186
|
})
|
|
184
187
|
if (created.aiError) aiError = created.aiError
|
|
185
188
|
;(created.published ? published : drafted).push(t.language.toUpperCase())
|
|
@@ -217,7 +220,7 @@ function PropagateDialog(props: {
|
|
|
217
220
|
}
|
|
218
221
|
}
|
|
219
222
|
// Publié si l'IA a tout traduit (les textes gardés à la main ne bloquent pas).
|
|
220
|
-
const publish = ai && !failed && remaining.every((p) => kept.includes(p))
|
|
223
|
+
const publish = publishNow && ai && !failed && remaining.every((p) => kept.includes(p))
|
|
221
224
|
const tx = client.transaction()
|
|
222
225
|
if (publish) {
|
|
223
226
|
tx.createOrReplace({...doc, _id: targetId, _type: type} as {_id: string; _type: string})
|
|
@@ -241,9 +244,11 @@ function PropagateDialog(props: {
|
|
|
241
244
|
status: aiError ? 'warning' : 'success',
|
|
242
245
|
title: [published.length ? `Published: ${published.join(', ')}` : '', drafted.length ? `Drafts: ${drafted.join(', ')}` : ''].filter(Boolean).join(' · '),
|
|
243
246
|
description: aiError
|
|
244
|
-
? `AI translation failed (${aiError}): saved as drafts, texts marked "To translate".`
|
|
247
|
+
? `AI translation failed (${aiError}): saved as drafts, texts marked "To translate". Retry later from Pages → Languages (unfinished translations).`
|
|
245
248
|
: drafted.length
|
|
246
|
-
?
|
|
249
|
+
? ai && !publishNow
|
|
250
|
+
? 'Translated with AI as drafts: review each language, then publish it.'
|
|
251
|
+
: 'Translate the fields marked "To translate", then publish each language.'
|
|
247
252
|
: 'Translated with AI and published.',
|
|
248
253
|
duration: 8000,
|
|
249
254
|
})
|
|
@@ -262,13 +267,21 @@ function PropagateDialog(props: {
|
|
|
262
267
|
? `The ${source.toUpperCase()} page is published. Create it in the other languages?`
|
|
263
268
|
: `The ${source.toUpperCase()} version is published. Apply its changes to the other languages?`}{' '}
|
|
264
269
|
{ai
|
|
265
|
-
?
|
|
270
|
+
? publishNow
|
|
271
|
+
? 'Texts are translated with AI and each language is published right away.'
|
|
272
|
+
: 'Texts are translated with AI; each language is saved as a draft to review, then publish.'
|
|
266
273
|
: 'Each language is saved as a draft, texts marked "To translate".'}
|
|
267
274
|
</Text>
|
|
268
275
|
{config.history && hasTexts && (
|
|
269
276
|
<Flex as="label" align="center" gap={2}>
|
|
270
277
|
<Checkbox checked={ai} onChange={() => setAi(!ai)} />
|
|
271
|
-
<Text size={1}>Translate with AI
|
|
278
|
+
<Text size={1}>Translate with AI</Text>
|
|
279
|
+
</Flex>
|
|
280
|
+
)}
|
|
281
|
+
{config.history && hasTexts && ai && (
|
|
282
|
+
<Flex as="label" align="center" gap={2}>
|
|
283
|
+
<Checkbox checked={publishNow} onChange={() => setPublishNow(!publishNow)} />
|
|
284
|
+
<Text size={1}>Publish right away, without review</Text>
|
|
272
285
|
</Flex>
|
|
273
286
|
)}
|
|
274
287
|
{mode === 'update' && editedCount > 0 && (
|
|
@@ -7,7 +7,7 @@ import {collectTexts, setAtKey, valueAtKey} from './aiTexts'
|
|
|
7
7
|
import {translateValues} from './aiTranslate'
|
|
8
8
|
import {propagate, type SchemaTypeLike} from './propagate'
|
|
9
9
|
import {slugify} from './slugify'
|
|
10
|
-
import {recordTrace} from './translationTrace'
|
|
10
|
+
import {hashValue, recordTrace} from './translationTrace'
|
|
11
11
|
|
|
12
12
|
type Doc = Record<string, unknown>
|
|
13
13
|
|
|
@@ -94,8 +94,14 @@ export async function createTranslatedVersion(opts: {
|
|
|
94
94
|
sourceLanguage: string
|
|
95
95
|
language: string
|
|
96
96
|
ai: boolean
|
|
97
|
-
|
|
98
|
-
|
|
97
|
+
/**
|
|
98
|
+
* Publier directement une version entièrement traduite (défaut : false, kit >= 2.65.0).
|
|
99
|
+
* Sinon brouillon à relire : une traduction par l'IA n'est jamais mise en ligne sans
|
|
100
|
+
* qu'un humain l'ait relue, sauf choix explicite.
|
|
101
|
+
*/
|
|
102
|
+
publish?: boolean
|
|
103
|
+
}): Promise<{id: string; published: boolean; complete: boolean; aiError: string | null; remaining: string[]}> {
|
|
104
|
+
const {client, config, type, schemaType, source, sourceId, sourceLanguage, language, ai, publish = false} = opts
|
|
99
105
|
const field = config.i18n?.field ?? 'language'
|
|
100
106
|
const result = propagate({}, {}, source, schemaType)
|
|
101
107
|
const {_id, _rev, _createdAt, _updatedAt, ...doc} = structuredClone(result.document) as Doc
|
|
@@ -143,7 +149,8 @@ export async function createTranslatedVersion(opts: {
|
|
|
143
149
|
// Slug non traduit (titre resté dans la langue source) : marqué « To translate »
|
|
144
150
|
// avec le titre, la règle d'or veut un slug dans la langue de la version.
|
|
145
151
|
if (slugField && remaining.includes(pageType?.titleField ?? 'title') && !remaining.includes(slugField)) remaining = [...remaining, slugField]
|
|
146
|
-
const
|
|
152
|
+
const complete = ai && !remaining.length && !aiError
|
|
153
|
+
const published = complete && publish
|
|
147
154
|
await client.createOrReplace({...doc, _id: published ? id : `drafts.${id}`, _type: type} as {_id: string; _type: string})
|
|
148
155
|
if (remaining.length)
|
|
149
156
|
await client.createOrReplace({
|
|
@@ -159,7 +166,7 @@ export async function createTranslatedVersion(opts: {
|
|
|
159
166
|
// Version publiée : « slug d'origine dans cette langue » redirige vers elle.
|
|
160
167
|
const redirects = published ? translationRedirectOptions(config) : null
|
|
161
168
|
if (redirects) await syncTranslationRedirects(client, redirects, [sourceId]).catch(() => {})
|
|
162
|
-
return {id, published, aiError}
|
|
169
|
+
return {id, published, complete, aiError, remaining}
|
|
163
170
|
}
|
|
164
171
|
|
|
165
172
|
/**
|
|
@@ -202,11 +209,68 @@ export async function fillTranslation(opts: {
|
|
|
202
209
|
* Textes de `source` absents ou non traduits dans `target` (vides, ou identiques au
|
|
203
210
|
* texte source). Un texte déjà traduit, même à la main, n'y figure jamais.
|
|
204
211
|
*/
|
|
205
|
-
export function untranslatedPaths(
|
|
212
|
+
export function untranslatedPaths(
|
|
213
|
+
source: Doc,
|
|
214
|
+
target: Doc | null,
|
|
215
|
+
schemaType: SchemaTypeLike | undefined,
|
|
216
|
+
/** Trace des textes écrits par l'IA (readTrace) : un texte que l'IA a rendu À L'IDENTIQUE
|
|
217
|
+
* (nom propre, marque, « OK ») est traduit, il n'est plus reproposé sans fin. */
|
|
218
|
+
trace?: Map<string, string>,
|
|
219
|
+
): string[] {
|
|
206
220
|
return propagate({}, {}, source, schemaType).toTranslate.filter((path) => {
|
|
207
221
|
const src = valueAtKey(source, path)
|
|
208
222
|
if (!collectTexts(src).some((t) => t.trim())) return false
|
|
209
223
|
const cur = target ? valueAtKey(target, path) : undefined
|
|
210
|
-
|
|
224
|
+
if (!collectTexts(cur).some((t) => t.trim())) return true
|
|
225
|
+
if (JSON.stringify(cur) !== JSON.stringify(src)) return false
|
|
226
|
+
return trace?.get(path) !== hashValue(cur)
|
|
211
227
|
})
|
|
212
228
|
}
|
|
229
|
+
|
|
230
|
+
/**
|
|
231
|
+
* Reprend une version traduite restée INACHEVÉE (textes « To translate » après un échec de
|
|
232
|
+
* l'IA) : retraduit seulement ces textes depuis la version source publiée, dans le
|
|
233
|
+
* brouillon (créé depuis la version publiée s'il n'existe pas). Publie si tout est traduit
|
|
234
|
+
* et `publish` demandé. Rend ce qui reste et l'erreur de l'IA, pour le dire à l'équipe.
|
|
235
|
+
*/
|
|
236
|
+
export async function retryTranslatedVersion(opts: {
|
|
237
|
+
client: SanityClient
|
|
238
|
+
config: ResolvedKitConfig
|
|
239
|
+
todo: {documentId: string; sourceId: string; sourceLanguage: string; paths: string[]}
|
|
240
|
+
language: string
|
|
241
|
+
publish?: boolean
|
|
242
|
+
}): Promise<{published: boolean; remaining: string[]; aiError: string | null}> {
|
|
243
|
+
const {client, config, todo, language, publish = false} = opts
|
|
244
|
+
const id = todo.documentId.replace(/^drafts\./, '')
|
|
245
|
+
const source = await client.getDocument<Doc>(todo.sourceId)
|
|
246
|
+
const current = (await client.getDocument<Doc>(`drafts.${id}`)) ?? (await client.getDocument<Doc>(id))
|
|
247
|
+
if (!source || !current) return {published: false, remaining: todo.paths, aiError: !source ? 'source version not found (unpublished or deleted)' : 'translated version not found'}
|
|
248
|
+
if (!config.history) return {published: false, remaining: todo.paths, aiError: 'AI translation is not configured (kit project key missing)'}
|
|
249
|
+
const {_rev, _createdAt, _updatedAt, ...doc} = structuredClone(current) as Doc
|
|
250
|
+
let remaining = todo.paths
|
|
251
|
+
let aiError: string | null = null
|
|
252
|
+
const written: {path: string; value: unknown}[] = []
|
|
253
|
+
try {
|
|
254
|
+
const values = remaining.map((path) => valueAtKey(source, path))
|
|
255
|
+
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 ?? ''))
|
|
257
|
+
remaining = remaining.filter((path, n) => {
|
|
258
|
+
const value = toSend[n] === undefined ? undefined : translated[n]
|
|
259
|
+
const ok = setAtKey(doc, path, value)
|
|
260
|
+
if (ok && value !== undefined) written.push({path, value})
|
|
261
|
+
return !ok
|
|
262
|
+
})
|
|
263
|
+
} catch (cause) {
|
|
264
|
+
aiError = cause instanceof Error ? cause.message : String(cause)
|
|
265
|
+
}
|
|
266
|
+
const published = publish && !remaining.length && !aiError
|
|
267
|
+
const tx = client.transaction()
|
|
268
|
+
if (published) {
|
|
269
|
+
tx.createOrReplace({...doc, _id: id} as {_id: string; _type: string})
|
|
270
|
+
tx.delete(`drafts.${id}`)
|
|
271
|
+
} else tx.createOrReplace({...doc, _id: `drafts.${id}`} as {_id: string; _type: string})
|
|
272
|
+
tx.patch(translationTodoId(id), (p) => p.set({paths: remaining}))
|
|
273
|
+
await tx.commit()
|
|
274
|
+
await recordTrace(client, id, written, {sourceId: todo.sourceId, sourceLanguage: todo.sourceLanguage})
|
|
275
|
+
return {published, remaining, aiError}
|
|
276
|
+
}
|