@sanity/assist 4.0.2 → 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/dist/index.js CHANGED
@@ -635,30 +635,36 @@ function useTranslate(apiClient) {
635
635
  translatePath,
636
636
  fieldLanguageMap,
637
637
  conditionalMembers
638
- }) => (setLoading(!0), apiClient.request({
639
- method: "POST",
640
- url: `/assist/tasks/translate/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
641
- body: {
642
- documentId,
643
- types,
644
- languagePath,
645
- userStyleguide: styleguide,
646
- fieldLanguageMap,
647
- conditionalMembers,
648
- translatePath: translatePath.length === 0 ? documentRootKey : sanity.pathToString(translatePath),
649
- userId: user?.id
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
- }).catch((e) => {
652
- throw toast.push({
653
- status: "error",
654
- title: "Translate failed",
655
- description: e.message
656
- }), setLoading(!1), e;
657
- }).finally(() => {
658
- setTimeout(() => {
659
- setLoading(!1);
660
- }, 2e3);
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({ apiVersion: config?.apiVersion ?? "2022-11-27" }), documentId = fieldTranslationParams?.document?._id, id = react.useId(), selectFromLanguage = react.useCallback(
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,
@@ -3880,11 +3916,7 @@ const instructionForm = [
3880
3916
  contextDocumentSchema
3881
3917
  ], assist = sanity.definePlugin((config) => {
3882
3918
  const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
3883
- if (styleguide.length > 2e3)
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))
3919
+ if (typeof styleguide == "string" && validateStyleguide(styleguide), maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
3888
3920
  throw new Error(
3889
3921
  `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
3890
3922
  );