@sanity/assist 3.2.2 → 4.0.1

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
  },
@@ -960,10 +975,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
960
975
  // @ts-ignore this is a valid option available in `corel` - Remove after corel is merged to next
961
976
  selectedReleaseId,
962
977
  editState
963
- } = structure.useDocumentPane(), { draft, published, version } = editState || {};
964
- let assistableDocumentId = version?._id || draft?._id || published?._id;
965
- assistableDocumentId || (assistableDocumentId = selectedReleaseId ? sanity.getVersionId(documentId, selectedReleaseId) : documentSchemaType.liveEdit ? documentId : sanity.getDraftId(documentId));
966
- const documentIsNew = selectedReleaseId ? !version?._id : !draft?._id && !published?._id, documentIsAssistable = selectedReleaseId ? !!version : isDocAssistable(documentSchemaType, published, draft), { params } = useAiPaneRouter(), selectedPath = params[fieldPathParam], assistDocument = useStudioAssistDocument({
978
+ } = structure.useDocumentPane(), { draft, published, version } = editState || {}, assistableDocumentId = selectedReleaseId ? sanity.getVersionId(documentId, selectedReleaseId) : documentSchemaType.liveEdit ? documentId : sanity.getDraftId(documentId), documentIsNew = selectedReleaseId ? !version?._id : !draft?._id && !published?._id, documentIsAssistable = selectedReleaseId ? !!version : isDocAssistable(documentSchemaType, published, draft), { params } = useAiPaneRouter(), selectedPath = params[fieldPathParam], assistDocument = useStudioAssistDocument({
967
979
  documentId: assistableDocumentId,
968
980
  schemaType: documentSchemaType
969
981
  });
@@ -1968,7 +1980,7 @@ function AssistInspector(props) {
1968
1980
  mode: "ghost",
1969
1981
  disabled: isEmptyPrompt || instructionLoading,
1970
1982
  fontSize: 1,
1971
- icon: instructionLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, {}) : icons.PlayIcon,
1983
+ icon: instructionLoading ? /* @__PURE__ */ jsxRuntime.jsx(ui.Spinner, { style: { marginTop: 3 } }) : icons.PlayIcon,
1972
1984
  onClick: runCurrentInstruction,
1973
1985
  padding: 3,
1974
1986
  text: "Run instruction"
@@ -3864,11 +3876,19 @@ const instructionForm = [
3864
3876
  instructionTask,
3865
3877
  contextDocumentSchema
3866
3878
  ], assist = sanity.definePlugin((config) => {
3867
- const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "";
3879
+ const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
3868
3880
  if (styleguide.length > 2e3)
3869
3881
  throw new Error(
3870
3882
  `[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
3871
3883
  );
3884
+ if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
3885
+ throw new Error(
3886
+ `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
3887
+ );
3888
+ if (temperature !== void 0 && (temperature < 0 || temperature > 1))
3889
+ throw new Error(
3890
+ `[${packageName}]: \`assist.maxPathDepth\` must be be in the range [0,1] inclusive, but was ${temperature}`
3891
+ );
3872
3892
  return {
3873
3893
  name: packageName,
3874
3894
  handlesGDR: !0,