@sanity/assist 4.4.5 → 4.4.6

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.esm.js CHANGED
@@ -1,5 +1,5 @@
1
1
  import { jsx, jsxs, Fragment } from "react/jsx-runtime";
2
- import { pathToString, getVersionFromId, getPublishedId, isVersionId, useEditState, useCurrentUser, useClient, typed, isObjectSchemaType, stringToPath, isKeySegment, FormFieldHeaderText, PatchEvent, unset, isArraySchemaType, useSchema, getVersionId, getDraftId, useColorSchemeValue, useFormCallbacks, useDocumentStore, useDocumentPresence, createPatchChannel, FormBuilder, fromMutationPatches, StatusButton, PresenceOverlay, VirtualizerScrollInstanceProvider, isDocumentSchemaType, useSyncState, set, useWorkspaceSchemaId, MemberFieldError, FormCallbacksProvider, FormInput, setIfMissing, insert, ObjectInputMember, isArrayOfObjectsSchemaType, defineType, defineField, defineArrayMember, definePlugin } from "sanity";
2
+ import { pathToString, getVersionFromId, getPublishedId, isVersionId, useEditState, useCurrentUser, useClient, typed, isObjectSchemaType, stringToPath, isKeySegment, isArraySchemaType, useSchema, FormFieldHeaderText, PatchEvent, unset, getVersionId, getDraftId, useColorSchemeValue, useFormCallbacks, useDocumentStore, useDocumentPresence, createPatchChannel, FormBuilder, fromMutationPatches, StatusButton, PresenceOverlay, VirtualizerScrollInstanceProvider, isDocumentSchemaType, useSyncState, set, useWorkspaceSchemaId, MemberFieldError, FormCallbacksProvider, FormInput, setIfMissing, insert, ObjectInputMember, isArrayOfObjectsSchemaType, defineType, defineField, defineArrayMember, definePlugin } from "sanity";
3
3
  import { useToast, useLayer, Dialog, Stack, Flex, Tooltip, Text, TextArea, Button, Badge, Popover, Card, Box, ErrorBoundary, focusFirstDescendant, Spinner, Container, Autocomplete, Breadcrumbs, useClickOutside, useGlobalKeyDown, useTheme, rgba, Radio, Checkbox, ThemeProvider, MenuButton, Menu, MenuItem, Switch, Label } from "@sanity/ui";
4
4
  import { useRef, useState, useEffect, useMemo, createContext, useContext, useCallback, useId, forwardRef, createElement, useReducer } from "react";
5
5
  import { useDocumentPane, usePaneRouter, DocumentInspectorHeader, DocumentPaneProvider } from "sanity/structure";
@@ -485,6 +485,128 @@ function useAiPaneRouter() {
485
485
  [paneRouter]
486
486
  );
487
487
  }
488
+ const hiddenTypes = [
489
+ "any",
490
+ "array",
491
+ "block",
492
+ "boolean",
493
+ "crossDatasetReference",
494
+ "date",
495
+ "datetime",
496
+ "document",
497
+ "email",
498
+ "file",
499
+ "globalDocumentReference",
500
+ "image",
501
+ "number",
502
+ "object",
503
+ "reference",
504
+ "span",
505
+ "string",
506
+ "text",
507
+ "url",
508
+ "slug",
509
+ "geopoint",
510
+ "sanity.assetSourceData",
511
+ "sanity.imageAsset",
512
+ "sanity.fileAsset",
513
+ "sanity.imageCrop",
514
+ "sanity.imageHotspot",
515
+ "sanity.imageMetadata",
516
+ "sanity.imageDimensions",
517
+ "sanity.imagePalette",
518
+ "sanity.imagePaletteSwatch",
519
+ assistSerializedTypeName,
520
+ assistSerializedFieldTypeName,
521
+ "sanity-agent.job.document"
522
+ ], inlineTypes = ["document", "object", "image", "file"];
523
+ function serializeSchema(schema, options2) {
524
+ 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));
525
+ return list.sort((a, b) => (a?.name ?? "").localeCompare(b?.name ?? "")), list;
526
+ }
527
+ function getSchemaStub(schemaType, schema, options2) {
528
+ if (!schemaType.type?.name)
529
+ throw console.error("Missing type name", schemaType.type), new Error("Type is missing name!");
530
+ const baseSchema = {
531
+ // we dont need type or id when we send using POST, so leave these out to save bandwidth
532
+ ...options2?.leanFormat ? {} : { _id: `${assistSchemaIdPrefix}${schemaType.name}`, _type: assistSerializedTypeName },
533
+ name: schemaType.name,
534
+ title: schemaType.title,
535
+ type: schemaType.type.name,
536
+ ...getBaseFields(schema, schemaType, schemaType.type.name, options2)
537
+ };
538
+ return removeUndef(baseSchema);
539
+ }
540
+ function getBaseFields(schema, type, typeName, options2) {
541
+ const schemaOptions = removeUndef({
542
+ imagePromptField: type.options?.aiAssist?.imageInstructionField,
543
+ embeddingsIndex: type.options?.aiAssist?.embeddingsIndex
544
+ });
545
+ return removeUndef({
546
+ options: Object.keys(schemaOptions).length ? schemaOptions : void 0,
547
+ values: Array.isArray(type?.options?.list) ? type?.options?.list.map(
548
+ (v) => typeof v == "string" ? v : v.value ?? `${v.title}`
549
+ ) : void 0,
550
+ of: "of" in type && typeName === "array" ? arrayOf(type, schema, options2) : void 0,
551
+ to: "to" in type && typeName === "reference" ? refToTypeNames(type) : void 0,
552
+ fields: "fields" in type && inlineTypes.includes(typeName) ? serializeFields(schema, type, options2) : void 0,
553
+ annotations: typeName === "block" && "fields" in type ? serializeAnnotations(type, schema, options2) : void 0,
554
+ inlineOf: typeName === "block" && "fields" in type ? serializeInlineOf(type, schema, options2) : void 0,
555
+ hidden: typeof type.hidden == "function" ? "function" : type.hidden ? !0 : void 0,
556
+ readOnly: typeof type.readOnly == "function" ? "function" : type.readOnly ? !0 : void 0
557
+ });
558
+ }
559
+ function serializeFields(schema, schemaType, options2) {
560
+ return (schemaType.fieldsets ? schemaType.fieldsets.flatMap(
561
+ (fs) => fs.single ? fs.field : fs.fields.map((f) => ({
562
+ ...f,
563
+ type: {
564
+ ...f.type,
565
+ // if fieldset is (conditionally) hidden, the field must be considered the same way
566
+ // ie, if the field does not show up in conditionalMembers, it is hidden
567
+ // regardless of weather or not it is the field function or the fieldset function that hides it
568
+ hidden: typeof fs.hidden == "function" ? fs.hidden : fs.hidden ? !0 : f.type.hidden
569
+ }
570
+ }))
571
+ ) : 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));
572
+ }
573
+ function serializeMember(schema, type, name, options2) {
574
+ const typeName = schema.get(type?.name) ? type.name : type.type?.name ?? "";
575
+ return removeUndef({
576
+ ...options2?.leanFormat ? {} : { _type: assistSerializedFieldTypeName },
577
+ name,
578
+ type: typeName,
579
+ title: type.title,
580
+ ...getBaseFields(schema, type, typeName, options2)
581
+ });
582
+ }
583
+ function serializeInlineOf(blockSchemaType, schema, options2) {
584
+ const childrenType = blockSchemaType.fields.find((f) => f.name === "children")?.type;
585
+ if (!(!childrenType || !isArraySchemaType(childrenType)))
586
+ return arrayOf(
587
+ {
588
+ of: childrenType.of.filter((t) => !isType(t, "span"))
589
+ },
590
+ schema,
591
+ options2
592
+ );
593
+ }
594
+ function serializeAnnotations(blockSchemaType, schema, options2) {
595
+ const marksType = blockSchemaType.fields.find((f) => f.name === "markDefs")?.type;
596
+ if (!(!marksType || !isArraySchemaType(marksType)))
597
+ return arrayOf(marksType, schema, options2);
598
+ }
599
+ function arrayOf(arrayType, schema, options2) {
600
+ return arrayType.of.filter((type) => isAssistSupported(type)).map((t) => serializeMember(schema, t, t.name, options2));
601
+ }
602
+ function refToTypeNames(type) {
603
+ return type.to.map((t) => ({
604
+ type: typed(t.name)
605
+ }));
606
+ }
607
+ function removeUndef(obj) {
608
+ return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
609
+ }
488
610
  const AiAssistanceConfigContext = createContext({});
489
611
  function useAiAssistanceConfig() {
490
612
  const context = useContext(AiAssistanceConfigContext);
@@ -492,8 +614,11 @@ function useAiAssistanceConfig() {
492
614
  throw new Error("Missing AiAssistanceConfigContext");
493
615
  return context;
494
616
  }
617
+ function useSerializedTypes() {
618
+ return useAiAssistanceConfig().serializedTypes;
619
+ }
495
620
  function AiAssistanceConfigProvider(props) {
496
- const [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
621
+ const [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient), schema = useSchema(), serializedTypes = useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]);
497
622
  useEffect(() => {
498
623
  getInstructStatus().then((s) => setStatus(s)).catch((e) => {
499
624
  console.error(e), setError(e);
@@ -514,8 +639,9 @@ function AiAssistanceConfigProvider(props) {
514
639
  statusLoading,
515
640
  init,
516
641
  initLoading,
517
- error
518
- }), [config, status, init, statusLoading, initLoading, error]);
642
+ error,
643
+ serializedTypes
644
+ }), [config, status, init, statusLoading, initLoading, error, serializedTypes]);
519
645
  return /* @__PURE__ */ jsx(AiAssistanceConfigContext.Provider, { value: context, children });
520
646
  }
521
647
  const basePath = "/assist/tasks/instruction", API_VERSION_WITH_EXTENDED_TYPES = "2025-04-01";
@@ -896,138 +1022,13 @@ function useDraftDelayedTask(args) {
896
1022
  [setQueuedArgs, documentOnChange]
897
1023
  );
898
1024
  }
899
- const hiddenTypes = [
900
- "any",
901
- "array",
902
- "block",
903
- "boolean",
904
- "crossDatasetReference",
905
- "date",
906
- "datetime",
907
- "document",
908
- "email",
909
- "file",
910
- "globalDocumentReference",
911
- "image",
912
- "number",
913
- "object",
914
- "reference",
915
- "span",
916
- "string",
917
- "text",
918
- "url",
919
- "slug",
920
- "geopoint",
921
- "sanity.assetSourceData",
922
- "sanity.imageAsset",
923
- "sanity.fileAsset",
924
- "sanity.imageCrop",
925
- "sanity.imageHotspot",
926
- "sanity.imageMetadata",
927
- "sanity.imageDimensions",
928
- "sanity.imagePalette",
929
- "sanity.imagePaletteSwatch",
930
- assistSerializedTypeName,
931
- assistSerializedFieldTypeName,
932
- "sanity-agent.job.document"
933
- ], inlineTypes = ["document", "object", "image", "file"];
934
- function serializeSchema(schema, options2) {
935
- 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));
936
- return list.sort((a, b) => (a?.name ?? "").localeCompare(b?.name ?? "")), list;
937
- }
938
- function getSchemaStub(schemaType, schema, options2) {
939
- if (!schemaType.type?.name)
940
- throw console.error("Missing type name", schemaType.type), new Error("Type is missing name!");
941
- const baseSchema = {
942
- // we dont need type or id when we send using POST, so leave these out to save bandwidth
943
- ...options2?.leanFormat ? {} : { _id: `${assistSchemaIdPrefix}${schemaType.name}`, _type: assistSerializedTypeName },
944
- name: schemaType.name,
945
- title: schemaType.title,
946
- type: schemaType.type.name,
947
- ...getBaseFields(schema, schemaType, schemaType.type.name, options2)
948
- };
949
- return removeUndef(baseSchema);
950
- }
951
- function getBaseFields(schema, type, typeName, options2) {
952
- const schemaOptions = removeUndef({
953
- imagePromptField: type.options?.aiAssist?.imageInstructionField,
954
- embeddingsIndex: type.options?.aiAssist?.embeddingsIndex
955
- });
956
- return removeUndef({
957
- options: Object.keys(schemaOptions).length ? schemaOptions : void 0,
958
- values: Array.isArray(type?.options?.list) ? type?.options?.list.map(
959
- (v) => typeof v == "string" ? v : v.value ?? `${v.title}`
960
- ) : void 0,
961
- of: "of" in type && typeName === "array" ? arrayOf(type, schema, options2) : void 0,
962
- to: "to" in type && typeName === "reference" ? refToTypeNames(type) : void 0,
963
- fields: "fields" in type && inlineTypes.includes(typeName) ? serializeFields(schema, type, options2) : void 0,
964
- annotations: typeName === "block" && "fields" in type ? serializeAnnotations(type, schema, options2) : void 0,
965
- inlineOf: typeName === "block" && "fields" in type ? serializeInlineOf(type, schema, options2) : void 0,
966
- hidden: typeof type.hidden == "function" ? "function" : type.hidden ? !0 : void 0,
967
- readOnly: typeof type.readOnly == "function" ? "function" : type.readOnly ? !0 : void 0
968
- });
969
- }
970
- function serializeFields(schema, schemaType, options2) {
971
- return (schemaType.fieldsets ? schemaType.fieldsets.flatMap(
972
- (fs) => fs.single ? fs.field : fs.fields.map((f) => ({
973
- ...f,
974
- type: {
975
- ...f.type,
976
- // if fieldset is (conditionally) hidden, the field must be considered the same way
977
- // ie, if the field does not show up in conditionalMembers, it is hidden
978
- // regardless of weather or not it is the field function or the fieldset function that hides it
979
- hidden: typeof fs.hidden == "function" ? fs.hidden : fs.hidden ? !0 : f.type.hidden
980
- }
981
- }))
982
- ) : 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));
983
- }
984
- function serializeMember(schema, type, name, options2) {
985
- const typeName = schema.get(type?.name) ? type.name : type.type?.name ?? "";
986
- return removeUndef({
987
- ...options2?.leanFormat ? {} : { _type: assistSerializedFieldTypeName },
988
- name,
989
- type: typeName,
990
- title: type.title,
991
- ...getBaseFields(schema, type, typeName, options2)
992
- });
993
- }
994
- function serializeInlineOf(blockSchemaType, schema, options2) {
995
- const childrenType = blockSchemaType.fields.find((f) => f.name === "children")?.type;
996
- if (!(!childrenType || !isArraySchemaType(childrenType)))
997
- return arrayOf(
998
- {
999
- of: childrenType.of.filter((t) => !isType(t, "span"))
1000
- },
1001
- schema,
1002
- options2
1003
- );
1004
- }
1005
- function serializeAnnotations(blockSchemaType, schema, options2) {
1006
- const marksType = blockSchemaType.fields.find((f) => f.name === "markDefs")?.type;
1007
- if (!(!marksType || !isArraySchemaType(marksType)))
1008
- return arrayOf(marksType, schema, options2);
1009
- }
1010
- function arrayOf(arrayType, schema, options2) {
1011
- return arrayType.of.filter((type) => isAssistSupported(type)).map((t) => serializeMember(schema, t, t.name, options2));
1012
- }
1013
- function refToTypeNames(type) {
1014
- return type.to.map((t) => ({
1015
- type: typed(t.name)
1016
- }));
1017
- }
1018
- function removeUndef(obj) {
1019
- return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
1020
- }
1021
- function useSerializedTypes() {
1022
- return useAssistDocumentContext().serializedTypes;
1023
- }
1024
1025
  function useAssistDocumentContextValue(documentId, documentType) {
1025
1026
  const schema = useSchema(), documentSchemaType = useMemo(() => {
1026
1027
  const schemaType = schema.get(documentType);
1027
1028
  if (!schemaType)
1028
1029
  throw new Error(`Schema type "${documentType}" not found`);
1029
1030
  return schemaType;
1030
- }, [documentType, schema]), serializedTypes = useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), { fieldRefs, fieldRefsByTypePath } = useMemo(() => {
1031
+ }, [documentType, schema]), { fieldRefs, fieldRefsByTypePath } = useMemo(() => {
1031
1032
  const fieldRefs2 = getFieldRefs(documentSchemaType), fieldRefsByTypePath2 = asFieldRefsByTypePath(fieldRefs2);
1032
1033
  return {
1033
1034
  fieldRefs: fieldRefs2,
@@ -1061,8 +1062,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1061
1062
  addSyntheticTask,
1062
1063
  removeSyntheticTask,
1063
1064
  fieldRefs,
1064
- fieldRefsByTypePath,
1065
- serializedTypes
1065
+ fieldRefsByTypePath
1066
1066
  };
1067
1067
  return assistDocument ? {
1068
1068
  ...base,
@@ -1084,8 +1084,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1084
1084
  addSyntheticTask,
1085
1085
  removeSyntheticTask,
1086
1086
  fieldRefs,
1087
- fieldRefsByTypePath,
1088
- serializedTypes
1087
+ fieldRefsByTypePath
1089
1088
  ]);
1090
1089
  }
1091
1090
  function useSyntheticTasks(assistableDocumentId) {