@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.js
CHANGED
|
@@ -635,30 +635,36 @@ function useTranslate(apiClient) {
|
|
|
635
635
|
translatePath,
|
|
636
636
|
fieldLanguageMap,
|
|
637
637
|
conditionalMembers
|
|
638
|
-
}) =>
|
|
639
|
-
|
|
640
|
-
|
|
641
|
-
|
|
642
|
-
|
|
643
|
-
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
638
|
+
}) => {
|
|
639
|
+
setLoading(!0);
|
|
640
|
+
async function run() {
|
|
641
|
+
return apiClient.request({
|
|
642
|
+
method: "POST",
|
|
643
|
+
url: `/assist/tasks/translate/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
|
|
644
|
+
body: {
|
|
645
|
+
documentId,
|
|
646
|
+
types,
|
|
647
|
+
languagePath,
|
|
648
|
+
userStyleguide: await styleguide(),
|
|
649
|
+
fieldLanguageMap,
|
|
650
|
+
conditionalMembers,
|
|
651
|
+
translatePath: translatePath.length === 0 ? documentRootKey : sanity.pathToString(translatePath),
|
|
652
|
+
userId: user?.id
|
|
653
|
+
}
|
|
654
|
+
});
|
|
650
655
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
660
|
-
|
|
661
|
-
|
|
656
|
+
return run().catch((e) => {
|
|
657
|
+
throw toast.push({
|
|
658
|
+
status: "error",
|
|
659
|
+
title: "Translate failed",
|
|
660
|
+
description: e.message
|
|
661
|
+
}), setLoading(!1), e;
|
|
662
|
+
}).finally(() => {
|
|
663
|
+
setTimeout(() => {
|
|
664
|
+
setLoading(!1);
|
|
665
|
+
}, 2e3);
|
|
666
|
+
});
|
|
667
|
+
},
|
|
662
668
|
[setLoading, apiClient, toast, user, types]
|
|
663
669
|
);
|
|
664
670
|
return react.useMemo(
|
|
@@ -2213,6 +2219,21 @@ function AssistConnectorsOverlay(props) {
|
|
|
2213
2219
|
DEBUG
|
|
2214
2220
|
] });
|
|
2215
2221
|
}
|
|
2222
|
+
function validateStyleguide(styleguide) {
|
|
2223
|
+
if (styleguide && styleguide.length > 2e3)
|
|
2224
|
+
throw new Error(
|
|
2225
|
+
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, but was ${styleguide.length} characters`
|
|
2226
|
+
);
|
|
2227
|
+
return styleguide;
|
|
2228
|
+
}
|
|
2229
|
+
function createStyleGuideResolver(styleguide, context) {
|
|
2230
|
+
return async () => {
|
|
2231
|
+
if (typeof styleguide != "function")
|
|
2232
|
+
return styleguide;
|
|
2233
|
+
const styleguideResult = await styleguide(context);
|
|
2234
|
+
return validateStyleguide(styleguideResult);
|
|
2235
|
+
};
|
|
2236
|
+
}
|
|
2216
2237
|
const getLanguageParams = (select, document2) => {
|
|
2217
2238
|
if (!select || !document2)
|
|
2218
2239
|
return {};
|
|
@@ -2334,7 +2355,9 @@ function hasValuesToTranslate(fieldLanguageMaps, fromLanguage, basePath2) {
|
|
|
2334
2355
|
function FieldTranslationProvider(props) {
|
|
2335
2356
|
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
2357
|
setDialogOpen(!1), setLanguages(void 0), setFieldTranslationParams(void 0);
|
|
2337
|
-
}, []), languageClient = sanity.useClient({
|
|
2358
|
+
}, []), languageClient = sanity.useClient({
|
|
2359
|
+
apiVersion: config?.apiVersion ?? API_VERSION_WITH_EXTENDED_TYPES
|
|
2360
|
+
}), documentId = fieldTranslationParams?.document?._id, id = react.useId(), selectFromLanguage = react.useCallback(
|
|
2338
2361
|
(from, languages2, params) => {
|
|
2339
2362
|
const { document: document2, documentSchema } = params ?? {};
|
|
2340
2363
|
if (setFromLanguage(from), !document2 || !documentSchema || !params || !languages2) {
|
|
@@ -2391,7 +2414,12 @@ function FieldTranslationProvider(props) {
|
|
|
2391
2414
|
fieldLanguageMaps && documentId && translatePath && runTranslate({
|
|
2392
2415
|
documentId,
|
|
2393
2416
|
translatePath,
|
|
2394
|
-
styleguide,
|
|
2417
|
+
styleguide: createStyleGuideResolver(styleguide, {
|
|
2418
|
+
client: languageClient,
|
|
2419
|
+
documentId,
|
|
2420
|
+
schemaType: fieldTranslationParams?.documentSchema,
|
|
2421
|
+
translatePath
|
|
2422
|
+
}),
|
|
2395
2423
|
fieldLanguageMap: fieldLanguageMaps.map((map) => ({
|
|
2396
2424
|
...map,
|
|
2397
2425
|
// eslint-disable-next-line max-nested-callbacks
|
|
@@ -2407,7 +2435,9 @@ function FieldTranslationProvider(props) {
|
|
|
2407
2435
|
close,
|
|
2408
2436
|
toLanguages,
|
|
2409
2437
|
fieldTranslationParams?.translatePath,
|
|
2410
|
-
fieldTranslationParams?.conditionalMembers
|
|
2438
|
+
fieldTranslationParams?.conditionalMembers,
|
|
2439
|
+
fieldTranslationParams?.documentSchema,
|
|
2440
|
+
languageClient
|
|
2411
2441
|
]), runButton = /* @__PURE__ */ jsxRuntime.jsx(
|
|
2412
2442
|
ui.Button,
|
|
2413
2443
|
{
|
|
@@ -2595,7 +2625,7 @@ function useAssistSupported(path, schemaType) {
|
|
|
2595
2625
|
const translateActions = {
|
|
2596
2626
|
name: "sanity-assist-translate",
|
|
2597
2627
|
useAction(props) {
|
|
2598
|
-
const { config, status } = useAiAssistanceConfig(), apiClient = useApiClient(config?.__customApiClient), {
|
|
2628
|
+
const { config, status } = useAiAssistanceConfig(), apiClient = useApiClient(config?.__customApiClient), client = sanity.useClient({ apiVersion: API_VERSION_WITH_EXTENDED_TYPES }), {
|
|
2599
2629
|
schemaType: fieldSchemaType,
|
|
2600
2630
|
path,
|
|
2601
2631
|
documentId,
|
|
@@ -2623,7 +2653,11 @@ const translateActions = {
|
|
|
2623
2653
|
translationApi.loading || !languagePath || !documentId || translate({
|
|
2624
2654
|
languagePath,
|
|
2625
2655
|
translatePath: path,
|
|
2626
|
-
styleguide,
|
|
2656
|
+
styleguide: createStyleGuideResolver(styleguide, {
|
|
2657
|
+
client,
|
|
2658
|
+
documentId,
|
|
2659
|
+
schemaType: documentSchemaType
|
|
2660
|
+
}),
|
|
2627
2661
|
documentId: documentId ?? "",
|
|
2628
2662
|
conditionalMembers: formStateRef.current ? getConditionalMembers(formStateRef.current) : []
|
|
2629
2663
|
});
|
|
@@ -2639,7 +2673,9 @@ const translateActions = {
|
|
|
2639
2673
|
translationApi.loading,
|
|
2640
2674
|
documentTranslationEnabled,
|
|
2641
2675
|
path,
|
|
2642
|
-
readOnly
|
|
2676
|
+
readOnly,
|
|
2677
|
+
client,
|
|
2678
|
+
documentSchemaType
|
|
2643
2679
|
]), fieldTranslate = useFieldTranslation(), openFieldTranslation = useDraftDelayedTask({
|
|
2644
2680
|
documentOnChange,
|
|
2645
2681
|
isDocAssistable: documentIsAssistable ?? !1,
|
|
@@ -2651,7 +2687,10 @@ const translateActions = {
|
|
|
2651
2687
|
title: "Translate fields...",
|
|
2652
2688
|
onAction: () => {
|
|
2653
2689
|
fieldTranslate.translationLoading || !documentId || (formStateRef.current && getConditionalMembers(formStateRef.current), openFieldTranslation({
|
|
2654
|
-
document:
|
|
2690
|
+
document: {
|
|
2691
|
+
...docRef.current,
|
|
2692
|
+
_id: documentId
|
|
2693
|
+
},
|
|
2655
2694
|
documentSchema: documentSchemaType,
|
|
2656
2695
|
translatePath: path,
|
|
2657
2696
|
conditionalMembers: formStateRef.current ? getConditionalMembers(formStateRef.current, maxDepth2) : []
|
|
@@ -3877,11 +3916,7 @@ const instructionForm = [
|
|
|
3877
3916
|
contextDocumentSchema
|
|
3878
3917
|
], assist = sanity.definePlugin((config) => {
|
|
3879
3918
|
const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
|
|
3880
|
-
if (styleguide
|
|
3881
|
-
throw new Error(
|
|
3882
|
-
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
|
|
3883
|
-
);
|
|
3884
|
-
if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3919
|
+
if (typeof styleguide == "string" && validateStyleguide(styleguide), maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3885
3920
|
throw new Error(
|
|
3886
3921
|
`[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
|
|
3887
3922
|
);
|