@sanity/assist 4.4.3 → 4.4.5

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
@@ -511,128 +511,6 @@ function AiAssistanceConfigProvider(props) {
511
511
  }), [config, status, init, statusLoading, initLoading, error]);
512
512
  return /* @__PURE__ */ jsxRuntime.jsx(AiAssistanceConfigContext.Provider, { value: context, children });
513
513
  }
514
- const hiddenTypes = [
515
- "any",
516
- "array",
517
- "block",
518
- "boolean",
519
- "crossDatasetReference",
520
- "date",
521
- "datetime",
522
- "document",
523
- "email",
524
- "file",
525
- "globalDocumentReference",
526
- "image",
527
- "number",
528
- "object",
529
- "reference",
530
- "span",
531
- "string",
532
- "text",
533
- "url",
534
- "slug",
535
- "geopoint",
536
- "sanity.assetSourceData",
537
- "sanity.imageAsset",
538
- "sanity.fileAsset",
539
- "sanity.imageCrop",
540
- "sanity.imageHotspot",
541
- "sanity.imageMetadata",
542
- "sanity.imageDimensions",
543
- "sanity.imagePalette",
544
- "sanity.imagePaletteSwatch",
545
- assistSerializedTypeName,
546
- assistSerializedFieldTypeName,
547
- "sanity-agent.job.document"
548
- ], inlineTypes = ["document", "object", "image", "file"];
549
- function serializeSchema(schema, options2) {
550
- const list = schema.getTypeNames().filter((t) => !(hiddenTypes.includes(t) || t.startsWith("sanity."))).map((t) => schema.get(t)).filter((t) => !!t).map((t) => getSchemaStub(t, schema, options2)).filter((t) => !("to" in t && t.to && !t.to.length || "of" in t && t.of && !t.of.length || "fields" in t && t.fields && !t.fields.length));
551
- return list.sort((a, b) => (a?.name ?? "").localeCompare(b?.name ?? "")), list;
552
- }
553
- function getSchemaStub(schemaType, schema, options2) {
554
- if (!schemaType.type?.name)
555
- throw console.error("Missing type name", schemaType.type), new Error("Type is missing name!");
556
- const baseSchema = {
557
- // we dont need type or id when we send using POST, so leave these out to save bandwidth
558
- ...options2?.leanFormat ? {} : { _id: `${assistSchemaIdPrefix}${schemaType.name}`, _type: assistSerializedTypeName },
559
- name: schemaType.name,
560
- title: schemaType.title,
561
- type: schemaType.type.name,
562
- ...getBaseFields(schema, schemaType, schemaType.type.name, options2)
563
- };
564
- return removeUndef(baseSchema);
565
- }
566
- function getBaseFields(schema, type, typeName, options2) {
567
- const schemaOptions = removeUndef({
568
- imagePromptField: type.options?.aiAssist?.imageInstructionField,
569
- embeddingsIndex: type.options?.aiAssist?.embeddingsIndex
570
- });
571
- return removeUndef({
572
- options: Object.keys(schemaOptions).length ? schemaOptions : void 0,
573
- values: Array.isArray(type?.options?.list) ? type?.options?.list.map(
574
- (v) => typeof v == "string" ? v : v.value ?? `${v.title}`
575
- ) : void 0,
576
- of: "of" in type && typeName === "array" ? arrayOf(type, schema, options2) : void 0,
577
- to: "to" in type && typeName === "reference" ? refToTypeNames(type) : void 0,
578
- fields: "fields" in type && inlineTypes.includes(typeName) ? serializeFields(schema, type, options2) : void 0,
579
- annotations: typeName === "block" && "fields" in type ? serializeAnnotations(type, schema, options2) : void 0,
580
- inlineOf: typeName === "block" && "fields" in type ? serializeInlineOf(type, schema, options2) : void 0,
581
- hidden: typeof type.hidden == "function" ? "function" : type.hidden ? !0 : void 0,
582
- readOnly: typeof type.readOnly == "function" ? "function" : type.readOnly ? !0 : void 0
583
- });
584
- }
585
- function serializeFields(schema, schemaType, options2) {
586
- return (schemaType.fieldsets ? schemaType.fieldsets.flatMap(
587
- (fs) => fs.single ? fs.field : fs.fields.map((f) => ({
588
- ...f,
589
- type: {
590
- ...f.type,
591
- // if fieldset is (conditionally) hidden, the field must be considered the same way
592
- // ie, if the field does not show up in conditionalMembers, it is hidden
593
- // regardless of weather or not it is the field function or the fieldset function that hides it
594
- hidden: typeof fs.hidden == "function" ? fs.hidden : fs.hidden ? !0 : f.type.hidden
595
- }
596
- }))
597
- ) : schemaType.fields).filter((f) => !["sanity.imageHotspot", "sanity.imageCrop"].includes(f.type?.name ?? "")).filter((f) => isAssistSupported(f.type)).map((field) => serializeMember(schema, field.type, field.name, options2));
598
- }
599
- function serializeMember(schema, type, name, options2) {
600
- const typeName = schema.get(type?.name) ? type.name : type.type?.name ?? "";
601
- return removeUndef({
602
- ...options2?.leanFormat ? {} : { _type: assistSerializedFieldTypeName },
603
- name,
604
- type: typeName,
605
- title: type.title,
606
- ...getBaseFields(schema, type, typeName, options2)
607
- });
608
- }
609
- function serializeInlineOf(blockSchemaType, schema, options2) {
610
- const childrenType = blockSchemaType.fields.find((f) => f.name === "children")?.type;
611
- if (!(!childrenType || !sanity.isArraySchemaType(childrenType)))
612
- return arrayOf(
613
- {
614
- of: childrenType.of.filter((t) => !isType(t, "span"))
615
- },
616
- schema,
617
- options2
618
- );
619
- }
620
- function serializeAnnotations(blockSchemaType, schema, options2) {
621
- const marksType = blockSchemaType.fields.find((f) => f.name === "markDefs")?.type;
622
- if (!(!marksType || !sanity.isArraySchemaType(marksType)))
623
- return arrayOf(marksType, schema, options2);
624
- }
625
- function arrayOf(arrayType, schema, options2) {
626
- return arrayType.of.filter((type) => isAssistSupported(type)).map((t) => serializeMember(schema, t, t.name, options2));
627
- }
628
- function refToTypeNames(type) {
629
- return type.to.map((t) => ({
630
- type: sanity.typed(t.name)
631
- }));
632
- }
633
- function removeUndef(obj) {
634
- return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
635
- }
636
514
  const basePath = "/assist/tasks/instruction", API_VERSION_WITH_EXTENDED_TYPES = "2025-04-01";
637
515
  function canUseAssist(status) {
638
516
  return status?.enabled && status.initialized && status.validToken;
@@ -645,7 +523,7 @@ function useApiClient(customApiClient) {
645
523
  );
646
524
  }
647
525
  function useTranslate(apiClient) {
648
- const [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), schema = sanity.useSchema(), types = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), toast = ui.useToast(), translate = react.useCallback(
526
+ const [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), types = useSerializedTypes(), toast = ui.useToast(), translate = react.useCallback(
649
527
  ({
650
528
  documentId,
651
529
  languagePath,
@@ -694,7 +572,7 @@ function useTranslate(apiClient) {
694
572
  );
695
573
  }
696
574
  function useGenerateCaption(apiClient) {
697
- const [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), schema = sanity.useSchema(), types = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), toast = ui.useToast(), generateCaption = react.useCallback(
575
+ const [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), types = useSerializedTypes(), toast = ui.useToast(), generateCaption = react.useCallback(
698
576
  ({ path, documentId }) => (setLoading(!0), apiClient.request({
699
577
  method: "POST",
700
578
  url: `/assist/tasks/generate-caption/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
@@ -726,7 +604,7 @@ function useGenerateCaption(apiClient) {
726
604
  );
727
605
  }
728
606
  function useGenerateImage(apiClient) {
729
- const [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), schema = sanity.useSchema(), types = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), toast = ui.useToast(), generateImage = react.useCallback(
607
+ const [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), types = useSerializedTypes(), toast = ui.useToast(), generateImage = react.useCallback(
730
608
  ({ path, documentId }) => (setLoading(!0), apiClient.request({
731
609
  method: "POST",
732
610
  url: `/assist/tasks/generate-image/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
@@ -788,7 +666,7 @@ function useInitInstruct(apiClient) {
788
666
  };
789
667
  }
790
668
  function useRunInstructionApi(apiClient) {
791
- const toast = ui.useToast(), [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), schema = sanity.useSchema(), types = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), {
669
+ const toast = ui.useToast(), [loading, setLoading] = react.useState(!1), user = sanity.useCurrentUser(), types = useSerializedTypes(), {
792
670
  config: { assist: assistConfig }
793
671
  } = useAiAssistanceConfig(), runInstruction = react.useCallback(
794
672
  (request) => {
@@ -1011,13 +889,144 @@ function useDraftDelayedTask(args) {
1011
889
  [setQueuedArgs, documentOnChange]
1012
890
  );
1013
891
  }
892
+ const hiddenTypes = [
893
+ "any",
894
+ "array",
895
+ "block",
896
+ "boolean",
897
+ "crossDatasetReference",
898
+ "date",
899
+ "datetime",
900
+ "document",
901
+ "email",
902
+ "file",
903
+ "globalDocumentReference",
904
+ "image",
905
+ "number",
906
+ "object",
907
+ "reference",
908
+ "span",
909
+ "string",
910
+ "text",
911
+ "url",
912
+ "slug",
913
+ "geopoint",
914
+ "sanity.assetSourceData",
915
+ "sanity.imageAsset",
916
+ "sanity.fileAsset",
917
+ "sanity.imageCrop",
918
+ "sanity.imageHotspot",
919
+ "sanity.imageMetadata",
920
+ "sanity.imageDimensions",
921
+ "sanity.imagePalette",
922
+ "sanity.imagePaletteSwatch",
923
+ assistSerializedTypeName,
924
+ assistSerializedFieldTypeName,
925
+ "sanity-agent.job.document"
926
+ ], inlineTypes = ["document", "object", "image", "file"];
927
+ function serializeSchema(schema, options2) {
928
+ const list = schema.getTypeNames().filter((t) => !(hiddenTypes.includes(t) || t.startsWith("sanity."))).map((t) => schema.get(t)).filter((t) => !!t).map((t) => getSchemaStub(t, schema, options2)).filter((t) => !("to" in t && t.to && !t.to.length || "of" in t && t.of && !t.of.length || "fields" in t && t.fields && !t.fields.length));
929
+ return list.sort((a, b) => (a?.name ?? "").localeCompare(b?.name ?? "")), list;
930
+ }
931
+ function getSchemaStub(schemaType, schema, options2) {
932
+ if (!schemaType.type?.name)
933
+ throw console.error("Missing type name", schemaType.type), new Error("Type is missing name!");
934
+ const baseSchema = {
935
+ // we dont need type or id when we send using POST, so leave these out to save bandwidth
936
+ ...options2?.leanFormat ? {} : { _id: `${assistSchemaIdPrefix}${schemaType.name}`, _type: assistSerializedTypeName },
937
+ name: schemaType.name,
938
+ title: schemaType.title,
939
+ type: schemaType.type.name,
940
+ ...getBaseFields(schema, schemaType, schemaType.type.name, options2)
941
+ };
942
+ return removeUndef(baseSchema);
943
+ }
944
+ function getBaseFields(schema, type, typeName, options2) {
945
+ const schemaOptions = removeUndef({
946
+ imagePromptField: type.options?.aiAssist?.imageInstructionField,
947
+ embeddingsIndex: type.options?.aiAssist?.embeddingsIndex
948
+ });
949
+ return removeUndef({
950
+ options: Object.keys(schemaOptions).length ? schemaOptions : void 0,
951
+ values: Array.isArray(type?.options?.list) ? type?.options?.list.map(
952
+ (v) => typeof v == "string" ? v : v.value ?? `${v.title}`
953
+ ) : void 0,
954
+ of: "of" in type && typeName === "array" ? arrayOf(type, schema, options2) : void 0,
955
+ to: "to" in type && typeName === "reference" ? refToTypeNames(type) : void 0,
956
+ fields: "fields" in type && inlineTypes.includes(typeName) ? serializeFields(schema, type, options2) : void 0,
957
+ annotations: typeName === "block" && "fields" in type ? serializeAnnotations(type, schema, options2) : void 0,
958
+ inlineOf: typeName === "block" && "fields" in type ? serializeInlineOf(type, schema, options2) : void 0,
959
+ hidden: typeof type.hidden == "function" ? "function" : type.hidden ? !0 : void 0,
960
+ readOnly: typeof type.readOnly == "function" ? "function" : type.readOnly ? !0 : void 0
961
+ });
962
+ }
963
+ function serializeFields(schema, schemaType, options2) {
964
+ return (schemaType.fieldsets ? schemaType.fieldsets.flatMap(
965
+ (fs) => fs.single ? fs.field : fs.fields.map((f) => ({
966
+ ...f,
967
+ type: {
968
+ ...f.type,
969
+ // if fieldset is (conditionally) hidden, the field must be considered the same way
970
+ // ie, if the field does not show up in conditionalMembers, it is hidden
971
+ // regardless of weather or not it is the field function or the fieldset function that hides it
972
+ hidden: typeof fs.hidden == "function" ? fs.hidden : fs.hidden ? !0 : f.type.hidden
973
+ }
974
+ }))
975
+ ) : schemaType.fields).filter((f) => !["sanity.imageHotspot", "sanity.imageCrop"].includes(f.type?.name ?? "")).filter((f) => isAssistSupported(f.type)).map((field) => serializeMember(schema, field.type, field.name, options2));
976
+ }
977
+ function serializeMember(schema, type, name, options2) {
978
+ const typeName = schema.get(type?.name) ? type.name : type.type?.name ?? "";
979
+ return removeUndef({
980
+ ...options2?.leanFormat ? {} : { _type: assistSerializedFieldTypeName },
981
+ name,
982
+ type: typeName,
983
+ title: type.title,
984
+ ...getBaseFields(schema, type, typeName, options2)
985
+ });
986
+ }
987
+ function serializeInlineOf(blockSchemaType, schema, options2) {
988
+ const childrenType = blockSchemaType.fields.find((f) => f.name === "children")?.type;
989
+ if (!(!childrenType || !sanity.isArraySchemaType(childrenType)))
990
+ return arrayOf(
991
+ {
992
+ of: childrenType.of.filter((t) => !isType(t, "span"))
993
+ },
994
+ schema,
995
+ options2
996
+ );
997
+ }
998
+ function serializeAnnotations(blockSchemaType, schema, options2) {
999
+ const marksType = blockSchemaType.fields.find((f) => f.name === "markDefs")?.type;
1000
+ if (!(!marksType || !sanity.isArraySchemaType(marksType)))
1001
+ return arrayOf(marksType, schema, options2);
1002
+ }
1003
+ function arrayOf(arrayType, schema, options2) {
1004
+ return arrayType.of.filter((type) => isAssistSupported(type)).map((t) => serializeMember(schema, t, t.name, options2));
1005
+ }
1006
+ function refToTypeNames(type) {
1007
+ return type.to.map((t) => ({
1008
+ type: sanity.typed(t.name)
1009
+ }));
1010
+ }
1011
+ function removeUndef(obj) {
1012
+ return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
1013
+ }
1014
+ function useSerializedTypes() {
1015
+ return useAssistDocumentContext().serializedTypes;
1016
+ }
1014
1017
  function useAssistDocumentContextValue(documentId, documentType) {
1015
1018
  const schema = sanity.useSchema(), documentSchemaType = react.useMemo(() => {
1016
1019
  const schemaType = schema.get(documentType);
1017
1020
  if (!schemaType)
1018
1021
  throw new Error(`Schema type "${documentType}" not found`);
1019
1022
  return schemaType;
1020
- }, [documentType, schema]), {
1023
+ }, [documentType, schema]), serializedTypes = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), { fieldRefs, fieldRefsByTypePath } = react.useMemo(() => {
1024
+ const fieldRefs2 = getFieldRefs(documentSchemaType), fieldRefsByTypePath2 = asFieldRefsByTypePath(fieldRefs2);
1025
+ return {
1026
+ fieldRefs: fieldRefs2,
1027
+ fieldRefsByTypePath: fieldRefsByTypePath2
1028
+ };
1029
+ }, [documentSchemaType]), {
1021
1030
  openInspector,
1022
1031
  closeInspector,
1023
1032
  inspector,
@@ -1029,7 +1038,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1029
1038
  } = 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({
1030
1039
  documentId: assistableDocumentId,
1031
1040
  schemaType: documentSchemaType
1032
- }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId), fieldRefs = getFieldRefs(documentSchemaType), fieldRefsByTypePath = asFieldRefsByTypePath(fieldRefs);
1041
+ }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId);
1033
1042
  return react.useMemo(() => {
1034
1043
  const base = {
1035
1044
  assistableDocumentId,
@@ -1045,7 +1054,8 @@ function useAssistDocumentContextValue(documentId, documentType) {
1045
1054
  addSyntheticTask,
1046
1055
  removeSyntheticTask,
1047
1056
  fieldRefs,
1048
- fieldRefsByTypePath
1057
+ fieldRefsByTypePath,
1058
+ serializedTypes
1049
1059
  };
1050
1060
  return assistDocument ? {
1051
1061
  ...base,
@@ -1065,7 +1075,10 @@ function useAssistDocumentContextValue(documentId, documentType) {
1065
1075
  selectedPath,
1066
1076
  syntheticTasks,
1067
1077
  addSyntheticTask,
1068
- removeSyntheticTask
1078
+ removeSyntheticTask,
1079
+ fieldRefs,
1080
+ fieldRefsByTypePath,
1081
+ serializedTypes
1069
1082
  ]);
1070
1083
  }
1071
1084
  function useSyntheticTasks(assistableDocumentId) {