@sanity/assist 3.2.1 → 4.0.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.mjs CHANGED
@@ -236,7 +236,7 @@ function isDisabled(type) {
236
236
  return !isSchemaAssistEnabled(type) || isUnsupportedType(type);
237
237
  }
238
238
  function isUnsupportedType(type) {
239
- return type.jsonType === "number" || type.name === "sanity.imageCrop" || type.name === "sanity.imageHotspot" || isType(type, "globalDocumentReference") || isType(type, "reference") && !type?.options?.aiAssist?.embeddingsIndex || isType(type, "crossDatasetReference") || isType(type, "slug") || isType(type, "url") || isType(type, "date") || isType(type, "datetime") || isType(type, "file");
239
+ return type.name === "sanity.imageCrop" || type.name === "sanity.imageHotspot" || isType(type, "globalDocumentReference") || isType(type, "reference") && !type?.options?.aiAssist?.embeddingsIndex || isType(type, "crossDatasetReference") || isType(type, "file");
240
240
  }
241
241
  const FirstAssistedPathContext = createContext(void 0);
242
242
  function FirstAssistedPathProvider(props) {
@@ -466,6 +466,39 @@ function useAiPaneRouter() {
466
466
  [paneRouter]
467
467
  );
468
468
  }
469
+ const AiAssistanceConfigContext = createContext({});
470
+ function useAiAssistanceConfig() {
471
+ const context = useContext(AiAssistanceConfigContext);
472
+ if (!context)
473
+ throw new Error("Missing AiAssistanceConfigContext");
474
+ return context;
475
+ }
476
+ function AiAssistanceConfigProvider(props) {
477
+ const [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
478
+ useEffect(() => {
479
+ getInstructStatus().then((s) => setStatus(s)).catch((e) => {
480
+ console.error(e), setError(e);
481
+ });
482
+ }, [getInstructStatus]);
483
+ const init = useCallback(async () => {
484
+ setError(void 0);
485
+ try {
486
+ await initInstruct();
487
+ const status2 = await getInstructStatus();
488
+ setStatus(status2);
489
+ } catch (e) {
490
+ console.error("Failed to init ai assistance", e), setError(e);
491
+ }
492
+ }, [initInstruct, getInstructStatus, setStatus]), { config, children } = props, context = useMemo(() => ({
493
+ config,
494
+ status,
495
+ statusLoading,
496
+ init,
497
+ initLoading,
498
+ error
499
+ }), [config, status, init, statusLoading, initLoading, error]);
500
+ return /* @__PURE__ */ jsx(AiAssistanceConfigContext.Provider, { value: context, children });
501
+ }
469
502
  const hiddenTypes = [
470
503
  "any",
471
504
  "array",
@@ -589,12 +622,12 @@ function refToTypeNames(type) {
589
622
  function removeUndef(obj) {
590
623
  return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
591
624
  }
592
- const basePath = "/assist/tasks/instruction";
625
+ const basePath = "/assist/tasks/instruction", API_VERSION_WITH_EXTENDED_TYPES = "2025-04-01";
593
626
  function canUseAssist(status) {
594
627
  return status?.enabled && status.initialized && status.validToken;
595
628
  }
596
629
  function useApiClient(customApiClient) {
597
- const client = useClient({ apiVersion: "2023-06-05" });
630
+ const client = useClient({ apiVersion: API_VERSION_WITH_EXTENDED_TYPES });
598
631
  return useMemo(
599
632
  () => customApiClient ? customApiClient(client) : client,
600
633
  [client, customApiClient]
@@ -738,25 +771,40 @@ function useInitInstruct(apiClient) {
738
771
  };
739
772
  }
740
773
  function useRunInstructionApi(apiClient) {
741
- const toast = useToast(), [loading, setLoading] = useState(!1), user = useCurrentUser(), schema = useSchema(), types = useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), runInstruction = useCallback(
742
- (request) => (setLoading(!0), apiClient.request({
743
- method: "POST",
744
- url: `${basePath}/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
745
- body: {
746
- ...request,
747
- types,
748
- userId: user?.id
774
+ const toast = useToast(), [loading, setLoading] = useState(!1), user = useCurrentUser(), schema = useSchema(), types = useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), {
775
+ config: { assist: assistConfig }
776
+ } = useAiAssistanceConfig(), runInstruction = useCallback(
777
+ (request) => {
778
+ if (!user) {
779
+ toast.push({
780
+ status: "error",
781
+ title: "Unable to get user for instruction."
782
+ });
783
+ return;
749
784
  }
750
- }).catch((e) => {
751
- throw toast.push({
752
- status: "error",
753
- title: "Instruction failed",
754
- description: e.message
755
- }), e;
756
- }).finally(() => {
757
- setLoading(!1);
758
- })),
759
- [apiClient, types, user, toast]
785
+ setLoading(!0);
786
+ const { timeZone, locale } = Intl.DateTimeFormat().resolvedOptions(), defaultLocaleSettings = { timeZone, locale }, localeSettings = assistConfig?.localeSettings?.({ user, defaultSettings: defaultLocaleSettings }) ?? defaultLocaleSettings;
787
+ return apiClient.request({
788
+ method: "POST",
789
+ url: `${basePath}/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
790
+ body: {
791
+ ...request,
792
+ types,
793
+ userId: user?.id,
794
+ localeSettings,
795
+ maxPathDepth: assistConfig?.maxPathDepth
796
+ }
797
+ }).catch((e) => {
798
+ throw toast.push({
799
+ status: "error",
800
+ title: "Instruction failed",
801
+ description: e.message
802
+ }), e;
803
+ }).finally(() => {
804
+ setLoading(!1);
805
+ });
806
+ },
807
+ [apiClient, types, user, toast, assistConfig]
760
808
  );
761
809
  return useMemo(
762
810
  () => ({
@@ -766,39 +814,6 @@ function useRunInstructionApi(apiClient) {
766
814
  [runInstruction, loading]
767
815
  );
768
816
  }
769
- const AiAssistanceConfigContext = createContext({});
770
- function useAiAssistanceConfig() {
771
- const context = useContext(AiAssistanceConfigContext);
772
- if (!context)
773
- throw new Error("Missing AiAssistanceConfigContext");
774
- return context;
775
- }
776
- function AiAssistanceConfigProvider(props) {
777
- const [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
778
- useEffect(() => {
779
- getInstructStatus().then((s) => setStatus(s)).catch((e) => {
780
- console.error(e), setError(e);
781
- });
782
- }, [getInstructStatus]);
783
- const init = useCallback(async () => {
784
- setError(void 0);
785
- try {
786
- await initInstruct();
787
- const status2 = await getInstructStatus();
788
- setStatus(status2);
789
- } catch (e) {
790
- console.error("Failed to init ai assistance", e), setError(e);
791
- }
792
- }, [initInstruct, getInstructStatus, setStatus]), { config, children } = props, context = useMemo(() => ({
793
- config,
794
- status,
795
- statusLoading,
796
- init,
797
- initLoading,
798
- error
799
- }), [config, status, init, statusLoading, initLoading, error]);
800
- return /* @__PURE__ */ jsx(AiAssistanceConfigContext.Provider, { value: context, children });
801
- }
802
817
  const NO_INPUT = {}, RunInstructionContext = createContext({
803
818
  runInstruction: () => {
804
819
  },
@@ -1975,7 +1990,7 @@ function AssistInspector(props) {
1975
1990
  mode: "ghost",
1976
1991
  disabled: isEmptyPrompt || instructionLoading,
1977
1992
  fontSize: 1,
1978
- icon: instructionLoading ? /* @__PURE__ */ jsx(Spinner, {}) : PlayIcon,
1993
+ icon: instructionLoading ? /* @__PURE__ */ jsx(Spinner, { style: { marginTop: 3 } }) : PlayIcon,
1979
1994
  onClick: runCurrentInstruction,
1980
1995
  padding: 3,
1981
1996
  text: "Run instruction"
@@ -3871,13 +3886,22 @@ const instructionForm = [
3871
3886
  instructionTask,
3872
3887
  contextDocumentSchema
3873
3888
  ], assist = definePlugin((config) => {
3874
- const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "";
3889
+ const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
3875
3890
  if (styleguide.length > 2e3)
3876
3891
  throw new Error(
3877
3892
  `[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
3878
3893
  );
3894
+ if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
3895
+ throw new Error(
3896
+ `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
3897
+ );
3898
+ if (temperature !== void 0 && (temperature < 0 || temperature > 1))
3899
+ throw new Error(
3900
+ `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [0,1] inclusive, but was ${temperature}`
3901
+ );
3879
3902
  return {
3880
3903
  name: packageName,
3904
+ handlesGDR: !0,
3881
3905
  schema: {
3882
3906
  types: schemaTypes
3883
3907
  },