@sanity/assist 4.0.2 → 4.2.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.
- package/README.md +25 -0
- package/dist/index.d.mts +30 -2
- package/dist/index.d.ts +30 -2
- package/dist/index.esm.js +79 -38
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +79 -38
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +79 -38
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/components/ImageContext.tsx +4 -4
- package/src/helpers/styleguide.ts +24 -0
- package/src/helpers/typeUtils.ts +13 -3
- package/src/plugin.tsx +4 -5
- package/src/schemas/typeDefExtensions.ts +12 -1
- package/src/translate/FieldTranslationProvider.tsx +14 -3
- package/src/translate/translateActions.tsx +12 -4
- package/src/translate/types.ts +18 -2
- package/src/useApiClient.ts +8 -5
package/dist/index.js
CHANGED
|
@@ -214,7 +214,16 @@ function isImage(schemaType) {
|
|
|
214
214
|
return isType(schemaType, "image");
|
|
215
215
|
}
|
|
216
216
|
function getDescriptionFieldOption(schemaType) {
|
|
217
|
-
|
|
217
|
+
if (!schemaType)
|
|
218
|
+
return;
|
|
219
|
+
const descriptionField = schemaType.options?.aiAssist?.imageDescriptionField;
|
|
220
|
+
return typeof descriptionField == "string" ? {
|
|
221
|
+
path: descriptionField,
|
|
222
|
+
updateOnImageChange: !0
|
|
223
|
+
} : descriptionField ? {
|
|
224
|
+
path: descriptionField.path,
|
|
225
|
+
updateOnImageChange: descriptionField.updateOnImageChange ?? !0
|
|
226
|
+
} : getDescriptionFieldOption(schemaType.type);
|
|
218
227
|
}
|
|
219
228
|
function getImageInstructionFieldOption(schemaType) {
|
|
220
229
|
return schemaType ? schemaType.options?.aiAssist?.imageInstructionField || getImageInstructionFieldOption(schemaType.type) : void 0;
|
|
@@ -635,30 +644,36 @@ function useTranslate(apiClient) {
|
|
|
635
644
|
translatePath,
|
|
636
645
|
fieldLanguageMap,
|
|
637
646
|
conditionalMembers
|
|
638
|
-
}) =>
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
647
|
+
}) => {
|
|
648
|
+
setLoading(!0);
|
|
649
|
+
async function run() {
|
|
650
|
+
return apiClient.request({
|
|
651
|
+
method: "POST",
|
|
652
|
+
url: `/assist/tasks/translate/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
|
|
653
|
+
body: {
|
|
654
|
+
documentId,
|
|
655
|
+
types,
|
|
656
|
+
languagePath,
|
|
657
|
+
userStyleguide: await styleguide(),
|
|
658
|
+
fieldLanguageMap,
|
|
659
|
+
conditionalMembers,
|
|
660
|
+
translatePath: translatePath.length === 0 ? documentRootKey : sanity.pathToString(translatePath),
|
|
661
|
+
userId: user?.id
|
|
662
|
+
}
|
|
663
|
+
});
|
|
650
664
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
665
|
+
return run().catch((e) => {
|
|
666
|
+
throw toast.push({
|
|
667
|
+
status: "error",
|
|
668
|
+
title: "Translate failed",
|
|
669
|
+
description: e.message
|
|
670
|
+
}), setLoading(!1), e;
|
|
671
|
+
}).finally(() => {
|
|
672
|
+
setTimeout(() => {
|
|
673
|
+
setLoading(!1);
|
|
674
|
+
}, 2e3);
|
|
675
|
+
});
|
|
676
|
+
},
|
|
662
677
|
[setLoading, apiClient, toast, user, types]
|
|
663
678
|
);
|
|
664
679
|
return react.useMemo(
|
|
@@ -2213,6 +2228,21 @@ function AssistConnectorsOverlay(props) {
|
|
|
2213
2228
|
DEBUG
|
|
2214
2229
|
] });
|
|
2215
2230
|
}
|
|
2231
|
+
function validateStyleguide(styleguide) {
|
|
2232
|
+
if (styleguide && styleguide.length > 2e3)
|
|
2233
|
+
throw new Error(
|
|
2234
|
+
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, but was ${styleguide.length} characters`
|
|
2235
|
+
);
|
|
2236
|
+
return styleguide;
|
|
2237
|
+
}
|
|
2238
|
+
function createStyleGuideResolver(styleguide, context) {
|
|
2239
|
+
return async () => {
|
|
2240
|
+
if (typeof styleguide != "function")
|
|
2241
|
+
return styleguide;
|
|
2242
|
+
const styleguideResult = await styleguide(context);
|
|
2243
|
+
return validateStyleguide(styleguideResult);
|
|
2244
|
+
};
|
|
2245
|
+
}
|
|
2216
2246
|
const getLanguageParams = (select, document2) => {
|
|
2217
2247
|
if (!select || !document2)
|
|
2218
2248
|
return {};
|
|
@@ -2334,7 +2364,9 @@ function hasValuesToTranslate(fieldLanguageMaps, fromLanguage, basePath2) {
|
|
|
2334
2364
|
function FieldTranslationProvider(props) {
|
|
2335
2365
|
const { config: assistConfig } = useAiAssistanceConfig(), apiClient = useApiClient(assistConfig.__customApiClient), styleguide = assistConfig.translate?.styleguide, config = assistConfig.translate?.field, { translate: runTranslate } = useTranslate(apiClient), [dialogOpen, setDialogOpen] = react.useState(!1), [fieldTranslationParams, setFieldTranslationParams] = react.useState(), [languages, setLanguages] = react.useState(), [fromLanguage, setFromLanguage] = react.useState(void 0), [toLanguages, setToLanguages] = react.useState(void 0), [fieldLanguageMaps, setFieldLanguageMaps] = react.useState(), close = react.useCallback(() => {
|
|
2336
2366
|
setDialogOpen(!1), setLanguages(void 0), setFieldTranslationParams(void 0);
|
|
2337
|
-
}, []), languageClient = sanity.useClient({
|
|
2367
|
+
}, []), languageClient = sanity.useClient({
|
|
2368
|
+
apiVersion: config?.apiVersion ?? API_VERSION_WITH_EXTENDED_TYPES
|
|
2369
|
+
}), documentId = fieldTranslationParams?.document?._id, id = react.useId(), selectFromLanguage = react.useCallback(
|
|
2338
2370
|
(from, languages2, params) => {
|
|
2339
2371
|
const { document: document2, documentSchema } = params ?? {};
|
|
2340
2372
|
if (setFromLanguage(from), !document2 || !documentSchema || !params || !languages2) {
|
|
@@ -2391,7 +2423,12 @@ function FieldTranslationProvider(props) {
|
|
|
2391
2423
|
fieldLanguageMaps && documentId && translatePath && runTranslate({
|
|
2392
2424
|
documentId,
|
|
2393
2425
|
translatePath,
|
|
2394
|
-
styleguide,
|
|
2426
|
+
styleguide: createStyleGuideResolver(styleguide, {
|
|
2427
|
+
client: languageClient,
|
|
2428
|
+
documentId,
|
|
2429
|
+
schemaType: fieldTranslationParams?.documentSchema,
|
|
2430
|
+
translatePath
|
|
2431
|
+
}),
|
|
2395
2432
|
fieldLanguageMap: fieldLanguageMaps.map((map) => ({
|
|
2396
2433
|
...map,
|
|
2397
2434
|
// eslint-disable-next-line max-nested-callbacks
|
|
@@ -2407,7 +2444,9 @@ function FieldTranslationProvider(props) {
|
|
|
2407
2444
|
close,
|
|
2408
2445
|
toLanguages,
|
|
2409
2446
|
fieldTranslationParams?.translatePath,
|
|
2410
|
-
fieldTranslationParams?.conditionalMembers
|
|
2447
|
+
fieldTranslationParams?.conditionalMembers,
|
|
2448
|
+
fieldTranslationParams?.documentSchema,
|
|
2449
|
+
languageClient
|
|
2411
2450
|
]), runButton = /* @__PURE__ */ jsxRuntime.jsx(
|
|
2412
2451
|
ui.Button,
|
|
2413
2452
|
{
|
|
@@ -2537,8 +2576,8 @@ function ImageContextProvider(props) {
|
|
|
2537
2576
|
), isShowingOlderRevision = !!structure.usePaneRouter().params?.rev;
|
|
2538
2577
|
react.useEffect(() => {
|
|
2539
2578
|
const descriptionField = getDescriptionFieldOption(schemaType);
|
|
2540
|
-
assetRef && assistableDocumentId && descriptionField && assetRef !== assetRefState && !isSyncing && !isShowingOlderRevision && !readOnly && (setAssetRefState(assetRef), canUseAssist(status) && generateCaption({
|
|
2541
|
-
path: sanity.pathToString([...path, descriptionField]),
|
|
2579
|
+
assetRef && assistableDocumentId && descriptionField?.updateOnImageChange && assetRef !== assetRefState && !isSyncing && !isShowingOlderRevision && !readOnly && (setAssetRefState(assetRef), canUseAssist(status) && generateCaption({
|
|
2580
|
+
path: sanity.pathToString([...path, descriptionField.path]),
|
|
2542
2581
|
documentId: assistableDocumentId
|
|
2543
2582
|
}));
|
|
2544
2583
|
}, [
|
|
@@ -2556,7 +2595,7 @@ function ImageContextProvider(props) {
|
|
|
2556
2595
|
const context = react.useMemo(() => {
|
|
2557
2596
|
const descriptionField = getDescriptionFieldOption(schemaType), imageInstructionField = getImageInstructionFieldOption(schemaType);
|
|
2558
2597
|
return {
|
|
2559
|
-
imageDescriptionPath: descriptionField ? sanity.pathToString([...path, descriptionField]) : void 0,
|
|
2598
|
+
imageDescriptionPath: descriptionField?.path ? sanity.pathToString([...path, descriptionField.path]) : void 0,
|
|
2560
2599
|
imageInstructionPath: imageInstructionField ? sanity.pathToString([...path, imageInstructionField]) : void 0,
|
|
2561
2600
|
assetRef
|
|
2562
2601
|
};
|
|
@@ -2595,7 +2634,7 @@ function useAssistSupported(path, schemaType) {
|
|
|
2595
2634
|
const translateActions = {
|
|
2596
2635
|
name: "sanity-assist-translate",
|
|
2597
2636
|
useAction(props) {
|
|
2598
|
-
const { config, status } = useAiAssistanceConfig(), apiClient = useApiClient(config?.__customApiClient), {
|
|
2637
|
+
const { config, status } = useAiAssistanceConfig(), apiClient = useApiClient(config?.__customApiClient), client = sanity.useClient({ apiVersion: API_VERSION_WITH_EXTENDED_TYPES }), {
|
|
2599
2638
|
schemaType: fieldSchemaType,
|
|
2600
2639
|
path,
|
|
2601
2640
|
documentId,
|
|
@@ -2623,7 +2662,11 @@ const translateActions = {
|
|
|
2623
2662
|
translationApi.loading || !languagePath || !documentId || translate({
|
|
2624
2663
|
languagePath,
|
|
2625
2664
|
translatePath: path,
|
|
2626
|
-
styleguide,
|
|
2665
|
+
styleguide: createStyleGuideResolver(styleguide, {
|
|
2666
|
+
client,
|
|
2667
|
+
documentId,
|
|
2668
|
+
schemaType: documentSchemaType
|
|
2669
|
+
}),
|
|
2627
2670
|
documentId: documentId ?? "",
|
|
2628
2671
|
conditionalMembers: formStateRef.current ? getConditionalMembers(formStateRef.current) : []
|
|
2629
2672
|
});
|
|
@@ -2639,7 +2682,9 @@ const translateActions = {
|
|
|
2639
2682
|
translationApi.loading,
|
|
2640
2683
|
documentTranslationEnabled,
|
|
2641
2684
|
path,
|
|
2642
|
-
readOnly
|
|
2685
|
+
readOnly,
|
|
2686
|
+
client,
|
|
2687
|
+
documentSchemaType
|
|
2643
2688
|
]), fieldTranslate = useFieldTranslation(), openFieldTranslation = useDraftDelayedTask({
|
|
2644
2689
|
documentOnChange,
|
|
2645
2690
|
isDocAssistable: documentIsAssistable ?? !1,
|
|
@@ -3880,11 +3925,7 @@ const instructionForm = [
|
|
|
3880
3925
|
contextDocumentSchema
|
|
3881
3926
|
], assist = sanity.definePlugin((config) => {
|
|
3882
3927
|
const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
|
|
3883
|
-
if (styleguide
|
|
3884
|
-
throw new Error(
|
|
3885
|
-
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
|
|
3886
|
-
);
|
|
3887
|
-
if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3928
|
+
if (typeof styleguide == "string" && validateStyleguide(styleguide), maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3888
3929
|
throw new Error(
|
|
3889
3930
|
`[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
|
|
3890
3931
|
);
|