@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.js CHANGED
@@ -229,7 +229,7 @@ function isDisabled(type) {
229
229
  return !isSchemaAssistEnabled(type) || isUnsupportedType(type);
230
230
  }
231
231
  function isUnsupportedType(type) {
232
- 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");
232
+ 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");
233
233
  }
234
234
  const FirstAssistedPathContext = react.createContext(void 0);
235
235
  function FirstAssistedPathProvider(props) {
@@ -459,6 +459,39 @@ function useAiPaneRouter() {
459
459
  [paneRouter]
460
460
  );
461
461
  }
462
+ const AiAssistanceConfigContext = react.createContext({});
463
+ function useAiAssistanceConfig() {
464
+ const context = react.useContext(AiAssistanceConfigContext);
465
+ if (!context)
466
+ throw new Error("Missing AiAssistanceConfigContext");
467
+ return context;
468
+ }
469
+ function AiAssistanceConfigProvider(props) {
470
+ const [status, setStatus] = react.useState(), [error, setError] = react.useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
471
+ react.useEffect(() => {
472
+ getInstructStatus().then((s) => setStatus(s)).catch((e) => {
473
+ console.error(e), setError(e);
474
+ });
475
+ }, [getInstructStatus]);
476
+ const init = react.useCallback(async () => {
477
+ setError(void 0);
478
+ try {
479
+ await initInstruct();
480
+ const status2 = await getInstructStatus();
481
+ setStatus(status2);
482
+ } catch (e) {
483
+ console.error("Failed to init ai assistance", e), setError(e);
484
+ }
485
+ }, [initInstruct, getInstructStatus, setStatus]), { config, children } = props, context = react.useMemo(() => ({
486
+ config,
487
+ status,
488
+ statusLoading,
489
+ init,
490
+ initLoading,
491
+ error
492
+ }), [config, status, init, statusLoading, initLoading, error]);
493
+ return /* @__PURE__ */ jsxRuntime.jsx(AiAssistanceConfigContext.Provider, { value: context, children });
494
+ }
462
495
  const hiddenTypes = [
463
496
  "any",
464
497
  "array",
@@ -582,12 +615,12 @@ function refToTypeNames(type) {
582
615
  function removeUndef(obj) {
583
616
  return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
584
617
  }
585
- const basePath = "/assist/tasks/instruction";
618
+ const basePath = "/assist/tasks/instruction", API_VERSION_WITH_EXTENDED_TYPES = "2025-04-01";
586
619
  function canUseAssist(status) {
587
620
  return status?.enabled && status.initialized && status.validToken;
588
621
  }
589
622
  function useApiClient(customApiClient) {
590
- const client = sanity.useClient({ apiVersion: "2023-06-05" });
623
+ const client = sanity.useClient({ apiVersion: API_VERSION_WITH_EXTENDED_TYPES });
591
624
  return react.useMemo(
592
625
  () => customApiClient ? customApiClient(client) : client,
593
626
  [client, customApiClient]
@@ -731,25 +764,40 @@ function useInitInstruct(apiClient) {
731
764
  };
732
765
  }
733
766
  function useRunInstructionApi(apiClient) {
734
- const toast = ui.useToast(), [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), schema = sanity.useSchema(), types = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), runInstruction = react.useCallback(
735
- (request) => (setLoading(!0), apiClient.request({
736
- method: "POST",
737
- url: `${basePath}/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
738
- body: {
739
- ...request,
740
- types,
741
- userId: user?.id
767
+ const toast = ui.useToast(), [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), schema = sanity.useSchema(), types = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), {
768
+ config: { assist: assistConfig }
769
+ } = useAiAssistanceConfig(), runInstruction = react.useCallback(
770
+ (request) => {
771
+ if (!user) {
772
+ toast.push({
773
+ status: "error",
774
+ title: "Unable to get user for instruction."
775
+ });
776
+ return;
742
777
  }
743
- }).catch((e) => {
744
- throw toast.push({
745
- status: "error",
746
- title: "Instruction failed",
747
- description: e.message
748
- }), e;
749
- }).finally(() => {
750
- setLoading(!1);
751
- })),
752
- [apiClient, types, user, toast]
778
+ setLoading(!0);
779
+ const { timeZone, locale } = Intl.DateTimeFormat().resolvedOptions(), defaultLocaleSettings = { timeZone, locale }, localeSettings = assistConfig?.localeSettings?.({ user, defaultSettings: defaultLocaleSettings }) ?? defaultLocaleSettings;
780
+ return apiClient.request({
781
+ method: "POST",
782
+ url: `${basePath}/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
783
+ body: {
784
+ ...request,
785
+ types,
786
+ userId: user?.id,
787
+ localeSettings,
788
+ maxPathDepth: assistConfig?.maxPathDepth
789
+ }
790
+ }).catch((e) => {
791
+ throw toast.push({
792
+ status: "error",
793
+ title: "Instruction failed",
794
+ description: e.message
795
+ }), e;
796
+ }).finally(() => {
797
+ setLoading(!1);
798
+ });
799
+ },
800
+ [apiClient, types, user, toast, assistConfig]
753
801
  );
754
802
  return react.useMemo(
755
803
  () => ({
@@ -759,39 +807,6 @@ function useRunInstructionApi(apiClient) {
759
807
  [runInstruction, loading]
760
808
  );
761
809
  }
762
- const AiAssistanceConfigContext = react.createContext({});
763
- function useAiAssistanceConfig() {
764
- const context = react.useContext(AiAssistanceConfigContext);
765
- if (!context)
766
- throw new Error("Missing AiAssistanceConfigContext");
767
- return context;
768
- }
769
- function AiAssistanceConfigProvider(props) {
770
- const [status, setStatus] = react.useState(), [error, setError] = react.useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
771
- react.useEffect(() => {
772
- getInstructStatus().then((s) => setStatus(s)).catch((e) => {
773
- console.error(e), setError(e);
774
- });
775
- }, [getInstructStatus]);
776
- const init = react.useCallback(async () => {
777
- setError(void 0);
778
- try {
779
- await initInstruct();
780
- const status2 = await getInstructStatus();
781
- setStatus(status2);
782
- } catch (e) {
783
- console.error("Failed to init ai assistance", e), setError(e);
784
- }
785
- }, [initInstruct, getInstructStatus, setStatus]), { config, children } = props, context = react.useMemo(() => ({
786
- config,
787
- status,
788
- statusLoading,
789
- init,
790
- initLoading,
791
- error
792
- }), [config, status, init, statusLoading, initLoading, error]);
793
- return /* @__PURE__ */ jsxRuntime.jsx(AiAssistanceConfigContext.Provider, { value: context, children });
794
- }
795
810
  const NO_INPUT = {}, RunInstructionContext = react.createContext({
796
811
  runInstruction: () => {
797
812
  },
@@ -1968,7 +1983,7 @@ function AssistInspector(props) {
1968
1983
  mode: "ghost",
1969
1984
  disabled: isEmptyPrompt || instructionLoading,
1970
1985
  fontSize: 1,
1971
- icon: instructionLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, {}) : icons.PlayIcon,
1986
+ icon: instructionLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, { style: { marginTop: 3 } }) : icons.PlayIcon,
1972
1987
  onClick: runCurrentInstruction,
1973
1988
  padding: 3,
1974
1989
  text: "Run instruction"
@@ -3864,13 +3879,22 @@ const instructionForm = [
3864
3879
  instructionTask,
3865
3880
  contextDocumentSchema
3866
3881
  ], assist = sanity.definePlugin((config) => {
3867
- const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "";
3882
+ const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
3868
3883
  if (styleguide.length > 2e3)
3869
3884
  throw new Error(
3870
3885
  `[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
3871
3886
  );
3887
+ if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
3888
+ throw new Error(
3889
+ `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
3890
+ );
3891
+ if (temperature !== void 0 && (temperature < 0 || temperature > 1))
3892
+ throw new Error(
3893
+ `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [0,1] inclusive, but was ${temperature}`
3894
+ );
3872
3895
  return {
3873
3896
  name: packageName,
3897
+ handlesGDR: !0,
3874
3898
  schema: {
3875
3899
  types: schemaTypes
3876
3900
  },