@sanity/assist 4.0.1 → 4.1.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 +15 -0
- package/dist/index.d.mts +18 -1
- package/dist/index.d.ts +18 -1
- package/dist/index.esm.js +70 -35
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +70 -35
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +70 -35
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/helpers/styleguide.ts +24 -0
- package/src/plugin.tsx +4 -5
- package/src/translate/FieldTranslationProvider.tsx +14 -3
- package/src/translate/translateActions.tsx +16 -5
- package/src/translate/types.ts +18 -2
- package/src/useApiClient.ts +8 -5
package/dist/index.mjs
CHANGED
|
@@ -642,30 +642,36 @@ function useTranslate(apiClient) {
|
|
|
642
642
|
translatePath,
|
|
643
643
|
fieldLanguageMap,
|
|
644
644
|
conditionalMembers
|
|
645
|
-
}) =>
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
645
|
+
}) => {
|
|
646
|
+
setLoading(!0);
|
|
647
|
+
async function run() {
|
|
648
|
+
return apiClient.request({
|
|
649
|
+
method: "POST",
|
|
650
|
+
url: `/assist/tasks/translate/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
|
|
651
|
+
body: {
|
|
652
|
+
documentId,
|
|
653
|
+
types,
|
|
654
|
+
languagePath,
|
|
655
|
+
userStyleguide: await styleguide(),
|
|
656
|
+
fieldLanguageMap,
|
|
657
|
+
conditionalMembers,
|
|
658
|
+
translatePath: translatePath.length === 0 ? documentRootKey : pathToString(translatePath),
|
|
659
|
+
userId: user?.id
|
|
660
|
+
}
|
|
661
|
+
});
|
|
657
662
|
}
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
663
|
+
return run().catch((e) => {
|
|
664
|
+
throw toast.push({
|
|
665
|
+
status: "error",
|
|
666
|
+
title: "Translate failed",
|
|
667
|
+
description: e.message
|
|
668
|
+
}), setLoading(!1), e;
|
|
669
|
+
}).finally(() => {
|
|
670
|
+
setTimeout(() => {
|
|
671
|
+
setLoading(!1);
|
|
672
|
+
}, 2e3);
|
|
673
|
+
});
|
|
674
|
+
},
|
|
669
675
|
[setLoading, apiClient, toast, user, types]
|
|
670
676
|
);
|
|
671
677
|
return useMemo(
|
|
@@ -2220,6 +2226,21 @@ function AssistConnectorsOverlay(props) {
|
|
|
2220
2226
|
DEBUG
|
|
2221
2227
|
] });
|
|
2222
2228
|
}
|
|
2229
|
+
function validateStyleguide(styleguide) {
|
|
2230
|
+
if (styleguide && styleguide.length > 2e3)
|
|
2231
|
+
throw new Error(
|
|
2232
|
+
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, but was ${styleguide.length} characters`
|
|
2233
|
+
);
|
|
2234
|
+
return styleguide;
|
|
2235
|
+
}
|
|
2236
|
+
function createStyleGuideResolver(styleguide, context) {
|
|
2237
|
+
return async () => {
|
|
2238
|
+
if (typeof styleguide != "function")
|
|
2239
|
+
return styleguide;
|
|
2240
|
+
const styleguideResult = await styleguide(context);
|
|
2241
|
+
return validateStyleguide(styleguideResult);
|
|
2242
|
+
};
|
|
2243
|
+
}
|
|
2223
2244
|
const getLanguageParams = (select, document2) => {
|
|
2224
2245
|
if (!select || !document2)
|
|
2225
2246
|
return {};
|
|
@@ -2341,7 +2362,9 @@ function hasValuesToTranslate(fieldLanguageMaps, fromLanguage, basePath2) {
|
|
|
2341
2362
|
function FieldTranslationProvider(props) {
|
|
2342
2363
|
const { config: assistConfig } = useAiAssistanceConfig(), apiClient = useApiClient(assistConfig.__customApiClient), styleguide = assistConfig.translate?.styleguide, config = assistConfig.translate?.field, { translate: runTranslate } = useTranslate(apiClient), [dialogOpen, setDialogOpen] = useState(!1), [fieldTranslationParams, setFieldTranslationParams] = useState(), [languages, setLanguages] = useState(), [fromLanguage, setFromLanguage] = useState(void 0), [toLanguages, setToLanguages] = useState(void 0), [fieldLanguageMaps, setFieldLanguageMaps] = useState(), close = useCallback(() => {
|
|
2343
2364
|
setDialogOpen(!1), setLanguages(void 0), setFieldTranslationParams(void 0);
|
|
2344
|
-
}, []), languageClient = useClient({
|
|
2365
|
+
}, []), languageClient = useClient({
|
|
2366
|
+
apiVersion: config?.apiVersion ?? API_VERSION_WITH_EXTENDED_TYPES
|
|
2367
|
+
}), documentId = fieldTranslationParams?.document?._id, id = useId(), selectFromLanguage = useCallback(
|
|
2345
2368
|
(from, languages2, params) => {
|
|
2346
2369
|
const { document: document2, documentSchema } = params ?? {};
|
|
2347
2370
|
if (setFromLanguage(from), !document2 || !documentSchema || !params || !languages2) {
|
|
@@ -2398,7 +2421,12 @@ function FieldTranslationProvider(props) {
|
|
|
2398
2421
|
fieldLanguageMaps && documentId && translatePath && runTranslate({
|
|
2399
2422
|
documentId,
|
|
2400
2423
|
translatePath,
|
|
2401
|
-
styleguide,
|
|
2424
|
+
styleguide: createStyleGuideResolver(styleguide, {
|
|
2425
|
+
client: languageClient,
|
|
2426
|
+
documentId,
|
|
2427
|
+
schemaType: fieldTranslationParams?.documentSchema,
|
|
2428
|
+
translatePath
|
|
2429
|
+
}),
|
|
2402
2430
|
fieldLanguageMap: fieldLanguageMaps.map((map) => ({
|
|
2403
2431
|
...map,
|
|
2404
2432
|
// eslint-disable-next-line max-nested-callbacks
|
|
@@ -2414,7 +2442,9 @@ function FieldTranslationProvider(props) {
|
|
|
2414
2442
|
close,
|
|
2415
2443
|
toLanguages,
|
|
2416
2444
|
fieldTranslationParams?.translatePath,
|
|
2417
|
-
fieldTranslationParams?.conditionalMembers
|
|
2445
|
+
fieldTranslationParams?.conditionalMembers,
|
|
2446
|
+
fieldTranslationParams?.documentSchema,
|
|
2447
|
+
languageClient
|
|
2418
2448
|
]), runButton = /* @__PURE__ */ jsx(
|
|
2419
2449
|
Button,
|
|
2420
2450
|
{
|
|
@@ -2602,7 +2632,7 @@ function useAssistSupported(path, schemaType) {
|
|
|
2602
2632
|
const translateActions = {
|
|
2603
2633
|
name: "sanity-assist-translate",
|
|
2604
2634
|
useAction(props) {
|
|
2605
|
-
const { config, status } = useAiAssistanceConfig(), apiClient = useApiClient(config?.__customApiClient), {
|
|
2635
|
+
const { config, status } = useAiAssistanceConfig(), apiClient = useApiClient(config?.__customApiClient), client = useClient({ apiVersion: API_VERSION_WITH_EXTENDED_TYPES }), {
|
|
2606
2636
|
schemaType: fieldSchemaType,
|
|
2607
2637
|
path,
|
|
2608
2638
|
documentId,
|
|
@@ -2630,7 +2660,11 @@ const translateActions = {
|
|
|
2630
2660
|
translationApi.loading || !languagePath || !documentId || translate({
|
|
2631
2661
|
languagePath,
|
|
2632
2662
|
translatePath: path,
|
|
2633
|
-
styleguide,
|
|
2663
|
+
styleguide: createStyleGuideResolver(styleguide, {
|
|
2664
|
+
client,
|
|
2665
|
+
documentId,
|
|
2666
|
+
schemaType: documentSchemaType
|
|
2667
|
+
}),
|
|
2634
2668
|
documentId: documentId ?? "",
|
|
2635
2669
|
conditionalMembers: formStateRef.current ? getConditionalMembers(formStateRef.current) : []
|
|
2636
2670
|
});
|
|
@@ -2646,7 +2680,9 @@ const translateActions = {
|
|
|
2646
2680
|
translationApi.loading,
|
|
2647
2681
|
documentTranslationEnabled,
|
|
2648
2682
|
path,
|
|
2649
|
-
readOnly
|
|
2683
|
+
readOnly,
|
|
2684
|
+
client,
|
|
2685
|
+
documentSchemaType
|
|
2650
2686
|
]), fieldTranslate = useFieldTranslation(), openFieldTranslation = useDraftDelayedTask({
|
|
2651
2687
|
documentOnChange,
|
|
2652
2688
|
isDocAssistable: documentIsAssistable ?? !1,
|
|
@@ -2658,7 +2694,10 @@ const translateActions = {
|
|
|
2658
2694
|
title: "Translate fields...",
|
|
2659
2695
|
onAction: () => {
|
|
2660
2696
|
fieldTranslate.translationLoading || !documentId || (formStateRef.current && getConditionalMembers(formStateRef.current), openFieldTranslation({
|
|
2661
|
-
document:
|
|
2697
|
+
document: {
|
|
2698
|
+
...docRef.current,
|
|
2699
|
+
_id: documentId
|
|
2700
|
+
},
|
|
2662
2701
|
documentSchema: documentSchemaType,
|
|
2663
2702
|
translatePath: path,
|
|
2664
2703
|
conditionalMembers: formStateRef.current ? getConditionalMembers(formStateRef.current, maxDepth2) : []
|
|
@@ -3884,11 +3923,7 @@ const instructionForm = [
|
|
|
3884
3923
|
contextDocumentSchema
|
|
3885
3924
|
], assist = definePlugin((config) => {
|
|
3886
3925
|
const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
|
|
3887
|
-
if (styleguide
|
|
3888
|
-
throw new Error(
|
|
3889
|
-
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
|
|
3890
|
-
);
|
|
3891
|
-
if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3926
|
+
if (typeof styleguide == "string" && validateStyleguide(styleguide), maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3892
3927
|
throw new Error(
|
|
3893
3928
|
`[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
|
|
3894
3929
|
);
|