@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.js CHANGED
@@ -478,6 +478,128 @@ function useAiPaneRouter() {
478
478
  [paneRouter]
479
479
  );
480
480
  }
481
+ const hiddenTypes = [
482
+ "any",
483
+ "array",
484
+ "block",
485
+ "boolean",
486
+ "crossDatasetReference",
487
+ "date",
488
+ "datetime",
489
+ "document",
490
+ "email",
491
+ "file",
492
+ "globalDocumentReference",
493
+ "image",
494
+ "number",
495
+ "object",
496
+ "reference",
497
+ "span",
498
+ "string",
499
+ "text",
500
+ "url",
501
+ "slug",
502
+ "geopoint",
503
+ "sanity.assetSourceData",
504
+ "sanity.imageAsset",
505
+ "sanity.fileAsset",
506
+ "sanity.imageCrop",
507
+ "sanity.imageHotspot",
508
+ "sanity.imageMetadata",
509
+ "sanity.imageDimensions",
510
+ "sanity.imagePalette",
511
+ "sanity.imagePaletteSwatch",
512
+ assistSerializedTypeName,
513
+ assistSerializedFieldTypeName,
514
+ "sanity-agent.job.document"
515
+ ], inlineTypes = ["document", "object", "image", "file"];
516
+ function serializeSchema(schema, options2) {
517
+ 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));
518
+ return list.sort((a, b) => (a?.name ?? "").localeCompare(b?.name ?? "")), list;
519
+ }
520
+ function getSchemaStub(schemaType, schema, options2) {
521
+ if (!schemaType.type?.name)
522
+ throw console.error("Missing type name", schemaType.type), new Error("Type is missing name!");
523
+ const baseSchema = {
524
+ // we dont need type or id when we send using POST, so leave these out to save bandwidth
525
+ ...options2?.leanFormat ? {} : { _id: `${assistSchemaIdPrefix}${schemaType.name}`, _type: assistSerializedTypeName },
526
+ name: schemaType.name,
527
+ title: schemaType.title,
528
+ type: schemaType.type.name,
529
+ ...getBaseFields(schema, schemaType, schemaType.type.name, options2)
530
+ };
531
+ return removeUndef(baseSchema);
532
+ }
533
+ function getBaseFields(schema, type, typeName, options2) {
534
+ const schemaOptions = removeUndef({
535
+ imagePromptField: type.options?.aiAssist?.imageInstructionField,
536
+ embeddingsIndex: type.options?.aiAssist?.embeddingsIndex
537
+ });
538
+ return removeUndef({
539
+ options: Object.keys(schemaOptions).length ? schemaOptions : void 0,
540
+ values: Array.isArray(type?.options?.list) ? type?.options?.list.map(
541
+ (v) => typeof v == "string" ? v : v.value ?? `${v.title}`
542
+ ) : void 0,
543
+ of: "of" in type && typeName === "array" ? arrayOf(type, schema, options2) : void 0,
544
+ to: "to" in type && typeName === "reference" ? refToTypeNames(type) : void 0,
545
+ fields: "fields" in type && inlineTypes.includes(typeName) ? serializeFields(schema, type, options2) : void 0,
546
+ annotations: typeName === "block" && "fields" in type ? serializeAnnotations(type, schema, options2) : void 0,
547
+ inlineOf: typeName === "block" && "fields" in type ? serializeInlineOf(type, schema, options2) : void 0,
548
+ hidden: typeof type.hidden == "function" ? "function" : type.hidden ? !0 : void 0,
549
+ readOnly: typeof type.readOnly == "function" ? "function" : type.readOnly ? !0 : void 0
550
+ });
551
+ }
552
+ function serializeFields(schema, schemaType, options2) {
553
+ return (schemaType.fieldsets ? schemaType.fieldsets.flatMap(
554
+ (fs) => fs.single ? fs.field : fs.fields.map((f) => ({
555
+ ...f,
556
+ type: {
557
+ ...f.type,
558
+ // if fieldset is (conditionally) hidden, the field must be considered the same way
559
+ // ie, if the field does not show up in conditionalMembers, it is hidden
560
+ // regardless of weather or not it is the field function or the fieldset function that hides it
561
+ hidden: typeof fs.hidden == "function" ? fs.hidden : fs.hidden ? !0 : f.type.hidden
562
+ }
563
+ }))
564
+ ) : 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));
565
+ }
566
+ function serializeMember(schema, type, name, options2) {
567
+ const typeName = schema.get(type?.name) ? type.name : type.type?.name ?? "";
568
+ return removeUndef({
569
+ ...options2?.leanFormat ? {} : { _type: assistSerializedFieldTypeName },
570
+ name,
571
+ type: typeName,
572
+ title: type.title,
573
+ ...getBaseFields(schema, type, typeName, options2)
574
+ });
575
+ }
576
+ function serializeInlineOf(blockSchemaType, schema, options2) {
577
+ const childrenType = blockSchemaType.fields.find((f) => f.name === "children")?.type;
578
+ if (!(!childrenType || !sanity.isArraySchemaType(childrenType)))
579
+ return arrayOf(
580
+ {
581
+ of: childrenType.of.filter((t) => !isType(t, "span"))
582
+ },
583
+ schema,
584
+ options2
585
+ );
586
+ }
587
+ function serializeAnnotations(blockSchemaType, schema, options2) {
588
+ const marksType = blockSchemaType.fields.find((f) => f.name === "markDefs")?.type;
589
+ if (!(!marksType || !sanity.isArraySchemaType(marksType)))
590
+ return arrayOf(marksType, schema, options2);
591
+ }
592
+ function arrayOf(arrayType, schema, options2) {
593
+ return arrayType.of.filter((type) => isAssistSupported(type)).map((t) => serializeMember(schema, t, t.name, options2));
594
+ }
595
+ function refToTypeNames(type) {
596
+ return type.to.map((t) => ({
597
+ type: sanity.typed(t.name)
598
+ }));
599
+ }
600
+ function removeUndef(obj) {
601
+ return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
602
+ }
481
603
  const AiAssistanceConfigContext = react.createContext({});
482
604
  function useAiAssistanceConfig() {
483
605
  const context = react.useContext(AiAssistanceConfigContext);
@@ -485,8 +607,11 @@ function useAiAssistanceConfig() {
485
607
  throw new Error("Missing AiAssistanceConfigContext");
486
608
  return context;
487
609
  }
610
+ function useSerializedTypes() {
611
+ return useAiAssistanceConfig().serializedTypes;
612
+ }
488
613
  function AiAssistanceConfigProvider(props) {
489
- const [status, setStatus] = react.useState(), [error, setError] = react.useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
614
+ const [status, setStatus] = react.useState(), [error, setError] = react.useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient), schema = sanity.useSchema(), serializedTypes = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]);
490
615
  react.useEffect(() => {
491
616
  getInstructStatus().then((s) => setStatus(s)).catch((e) => {
492
617
  console.error(e), setError(e);
@@ -507,8 +632,9 @@ function AiAssistanceConfigProvider(props) {
507
632
  statusLoading,
508
633
  init,
509
634
  initLoading,
510
- error
511
- }), [config, status, init, statusLoading, initLoading, error]);
635
+ error,
636
+ serializedTypes
637
+ }), [config, status, init, statusLoading, initLoading, error, serializedTypes]);
512
638
  return /* @__PURE__ */ jsxRuntime.jsx(AiAssistanceConfigContext.Provider, { value: context, children });
513
639
  }
514
640
  const basePath = "/assist/tasks/instruction", API_VERSION_WITH_EXTENDED_TYPES = "2025-04-01";
@@ -889,138 +1015,13 @@ function useDraftDelayedTask(args) {
889
1015
  [setQueuedArgs, documentOnChange]
890
1016
  );
891
1017
  }
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
- }
1017
1018
  function useAssistDocumentContextValue(documentId, documentType) {
1018
1019
  const schema = sanity.useSchema(), documentSchemaType = react.useMemo(() => {
1019
1020
  const schemaType = schema.get(documentType);
1020
1021
  if (!schemaType)
1021
1022
  throw new Error(`Schema type "${documentType}" not found`);
1022
1023
  return schemaType;
1023
- }, [documentType, schema]), serializedTypes = react.useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), { fieldRefs, fieldRefsByTypePath } = react.useMemo(() => {
1024
+ }, [documentType, schema]), { fieldRefs, fieldRefsByTypePath } = react.useMemo(() => {
1024
1025
  const fieldRefs2 = getFieldRefs(documentSchemaType), fieldRefsByTypePath2 = asFieldRefsByTypePath(fieldRefs2);
1025
1026
  return {
1026
1027
  fieldRefs: fieldRefs2,
@@ -1054,8 +1055,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1054
1055
  addSyntheticTask,
1055
1056
  removeSyntheticTask,
1056
1057
  fieldRefs,
1057
- fieldRefsByTypePath,
1058
- serializedTypes
1058
+ fieldRefsByTypePath
1059
1059
  };
1060
1060
  return assistDocument ? {
1061
1061
  ...base,
@@ -1077,8 +1077,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1077
1077
  addSyntheticTask,
1078
1078
  removeSyntheticTask,
1079
1079
  fieldRefs,
1080
- fieldRefsByTypePath,
1081
- serializedTypes
1080
+ fieldRefsByTypePath
1082
1081
  ]);
1083
1082
  }
1084
1083
  function useSyntheticTasks(assistableDocumentId) {