@sanity/assist 4.3.2 → 4.4.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/README.md CHANGED
@@ -999,6 +999,9 @@ prop can be used to parameterize Agent Actions on sanity client.
999
999
 
1000
1000
  #### Agent Action examples
1001
1001
 
1002
+ Below are some examples of agent action integration.
1003
+ For more, see [HOW-TO-USE](../studio/examples/agentActions/HOW-TO-USE.md)
1004
+
1002
1005
  ##### Fix spelling
1003
1006
 
1004
1007
  The following example adds a "Fix spelling" action to all fields and the document itself.
package/dist/index.d.mts CHANGED
@@ -157,6 +157,8 @@ export declare interface AssistFieldActionProps {
157
157
  * },
158
158
  * //...
159
159
  * })
160
+ *
161
+ * ```
160
162
  */
161
163
  schemaId: string
162
164
  /**
@@ -205,6 +207,15 @@ export declare interface AssistFieldActionProps {
205
207
  * ```
206
208
  */
207
209
  schemaType: SchemaType_2
210
+ /**
211
+ * Schema type of the parent field or array item holding this field.
212
+ *
213
+ * This can be undefined if the action was unable to resolve the parent type is excluded from AI Assist.
214
+ *
215
+ * @see schemaType
216
+ * @see documentSchemaType
217
+ */
218
+ parentSchemaType?: SchemaType_2
208
219
  }
209
220
 
210
221
  export declare interface AssistOptions {
@@ -532,7 +543,7 @@ declare interface PresetInstruction {
532
543
  prompt?: PromptTextBlock[]
533
544
  title?: string
534
545
  /**
535
- * String key from @sanity/icons IconMap
546
+ * String key from `@sanity/icons` IconMap
536
547
  */
537
548
  icon?: string
538
549
  /**
package/dist/index.d.ts CHANGED
@@ -157,6 +157,8 @@ export declare interface AssistFieldActionProps {
157
157
  * },
158
158
  * //...
159
159
  * })
160
+ *
161
+ * ```
160
162
  */
161
163
  schemaId: string
162
164
  /**
@@ -205,6 +207,15 @@ export declare interface AssistFieldActionProps {
205
207
  * ```
206
208
  */
207
209
  schemaType: SchemaType_2
210
+ /**
211
+ * Schema type of the parent field or array item holding this field.
212
+ *
213
+ * This can be undefined if the action was unable to resolve the parent type is excluded from AI Assist.
214
+ *
215
+ * @see schemaType
216
+ * @see documentSchemaType
217
+ */
218
+ parentSchemaType?: SchemaType_2
208
219
  }
209
220
 
210
221
  export declare interface AssistOptions {
@@ -532,7 +543,7 @@ declare interface PresetInstruction {
532
543
  prompt?: PromptTextBlock[]
533
544
  title?: string
534
545
  /**
535
- * String key from @sanity/icons IconMap
546
+ * String key from `@sanity/icons` IconMap
536
547
  */
537
548
  icon?: string
538
549
  /**
package/dist/index.esm.js CHANGED
@@ -242,7 +242,8 @@ function isSchemaAssistEnabled(type) {
242
242
  return !type.options?.aiAssist?.exclude;
243
243
  }
244
244
  function isAssistSupported(type) {
245
- return !isSchemaAssistEnabled(type) || isDisabled(type) ? !1 : type.jsonType === "array" ? !type.of.every((t) => isDisabled(t)) : type.jsonType === "object" ? !type.fields.every((field) => isDisabled(field.type)) : !0;
245
+ return !isSchemaAssistEnabled(type) || isDisabled(type) ? !1 : type.jsonType === "array" ? !type.of.every((t) => isDisabled(t)) : type.jsonType === "object" ? !type.fields.every((field) => isDisabled(field.type)) || /* to allow attaching custom actions on fieldless images */
246
+ isType(type, "image") : !0;
246
247
  }
247
248
  function isDisabled(type) {
248
249
  return !isSchemaAssistEnabled(type) || isUnsupportedType(type);
@@ -400,6 +401,12 @@ function getTypeIcon(schemaType) {
400
401
  }
401
402
  return isType(schemaType, "slug") ? LinkIcon : isType(schemaType, "image") ? ImageIcon : schemaType.jsonType === "array" && isPortableTextArray(schemaType) ? BlockContentIcon : schemaType.jsonType === "array" ? OlistIcon : schemaType.jsonType === "object" ? BlockquoteIcon : schemaType.jsonType === "string" ? StringIcon : DocumentIcon;
402
403
  }
404
+ function asFieldRefsByTypePath(fieldRefs) {
405
+ return fieldRefs.reduce(
406
+ (acc, ref) => ({ ...acc, [ref.key]: ref }),
407
+ {}
408
+ );
409
+ }
403
410
  function getFieldRefsWithDocument(schemaType) {
404
411
  const fields = getFieldRefs(schemaType);
405
412
  return [
@@ -1029,7 +1036,7 @@ function useAssistDocumentContextValue(documentId, documentType) {
1029
1036
  } = useDocumentPane(), { draft, published, version } = editState || {}, assistableDocumentId = selectedReleaseId ? getVersionId(documentId, selectedReleaseId) : documentSchemaType.liveEdit ? documentId : 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
1037
  documentId: assistableDocumentId,
1031
1038
  schemaType: documentSchemaType
1032
- }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId);
1039
+ }), { syntheticTasks, addSyntheticTask, removeSyntheticTask } = useSyntheticTasks(assistableDocumentId), fieldRefs = getFieldRefs(documentSchemaType), fieldRefsByTypePath = asFieldRefsByTypePath(fieldRefs);
1033
1040
  return useMemo(() => {
1034
1041
  const base = {
1035
1042
  assistableDocumentId,
@@ -1043,7 +1050,9 @@ function useAssistDocumentContextValue(documentId, documentType) {
1043
1050
  selectedPath,
1044
1051
  syntheticTasks,
1045
1052
  addSyntheticTask,
1046
- removeSyntheticTask
1053
+ removeSyntheticTask,
1054
+ fieldRefs,
1055
+ fieldRefsByTypePath
1047
1056
  };
1048
1057
  return assistDocument ? {
1049
1058
  ...base,
@@ -2959,7 +2968,8 @@ const assistFieldActions = {
2959
2968
  documentOnChange,
2960
2969
  documentSchemaType,
2961
2970
  selectedPath,
2962
- assistableDocumentId
2971
+ assistableDocumentId,
2972
+ fieldRefsByTypePath
2963
2973
  } = useAssistDocumentContext(), { value: docValue, formState } = useDocumentPane(), docValueRef = useRef(docValue), formStateRef = useRef(formState);
2964
2974
  formStateRef.current = formState;
2965
2975
  const currentUser = useCurrentUser(), isHidden = !assistDocument, pathKey = usePathKey(props.path), typePath = useTypePath(docValue, pathKey), assistDocumentId2 = assistDocument?._id, { requestRunInstruction } = useRequestRunInstruction({
@@ -3047,14 +3057,22 @@ const assistFieldActions = {
3047
3057
  path
3048
3058
  };
3049
3059
  }
3050
- ), []), customActions = useCustomFieldActions({
3060
+ ), []), parentSchemaType = useMemo(() => {
3061
+ if (props.path.length) {
3062
+ if (props.path.length === 1)
3063
+ return documentSchemaType;
3064
+ } else return;
3065
+ const parentPath = props.path.slice(0, -1), typePath2 = getTypePath(docValueRef.current, pathToString(parentPath));
3066
+ return typePath2 ? fieldRefsByTypePath[typePath2]?.schemaType : void 0;
3067
+ }, [fieldRefsByTypePath, props.path, documentSchemaType]), customActions = useCustomFieldActions({
3051
3068
  actionType: props.path.length ? "field" : "document",
3052
3069
  documentIdForAction: assistableDocumentId,
3053
3070
  schemaType,
3054
3071
  documentSchemaType,
3055
3072
  path: props.path,
3056
3073
  getDocumentValue,
3057
- getConditionalPaths
3074
+ getConditionalPaths,
3075
+ parentSchemaType
3058
3076
  }), manageInstructionsItem = useMemo(
3059
3077
  () => ({
3060
3078
  type: "action",