@sanity/assist 4.3.0 → 4.3.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
@@ -953,18 +953,20 @@ or other custom actions into the AI Assist document and field action menus, use
953
953
 
954
954
  ```ts
955
955
  assist({
956
- title: 'Custom actions',
957
- useFieldActions: (props: AssistFieldActionProps) => {
958
- return useMemo(() => [
959
- defineAssistFieldAction({
960
- title: 'Do something',
961
- icon: ActionIcon,
962
- onAction: async () => {
963
- // perform an (async) action
964
- // errors will be caught and displayed in a toast
965
- // until the action completes or fails, AI Assist "presence" will show up on the top of the document
966
- },
967
- })], [])
956
+ fieldActions: {
957
+ title: 'Custom actions',
958
+ useFieldActions: (props: AssistFieldActionProps) => {
959
+ return useMemo(() => [
960
+ defineAssistFieldAction({
961
+ title: 'Do something',
962
+ icon: ActionIcon,
963
+ onAction: async () => {
964
+ // perform an (async) action
965
+ // errors will be caught and displayed in a toast
966
+ // until the action completes or fails, AI Assist "presence" will show up on the top of the document
967
+ },
968
+ })], [])
969
+ }
968
970
  }
969
971
  })
970
972
  ```
@@ -989,46 +991,49 @@ It will fix spelling mistakes for the field it is invoked for (and all child fie
989
991
  by calling `client.agent.action.transform`.
990
992
 
991
993
  ```ts
992
- assist({
993
- title: 'Custom actions',
994
- useFieldActions: (props) => {
995
- const {
996
- documentSchemaType,
997
- schemaId,
998
- getDocumentValue,
999
- getConditionalPaths,
1000
- documentIdForAction,
1001
- path,
1002
- } = props
1003
- const client = useClient({apiVersion: 'vX'})
1004
- return useMemo(() => {
1005
- return [
1006
- defineAssistFieldAction({
1007
- title: 'Fix spelling',
1008
- icon: TranslateIcon,
1009
- onAction: async () => {
1010
- await client.agent.action.transform({
1011
- schemaId,
1012
- documentId: documentIdForAction,
1013
- instruction: 'Fix any spelling mistakes',
1014
- instructionParams: {field: {type: 'field', path}},
1015
- // no need to send path for document actions
1016
- target: path.length ? {path} : undefined,
1017
- conditionalPaths: {paths: getConditionalPaths()},
1018
- })
1019
- },
1020
- }),
1021
- ]
1022
- }, [
1023
- client,
1024
- documentSchemaType,
1025
- schemaId,
1026
- getDocumentValue,
1027
- getConditionalPaths,
1028
- documentIdForAction,
1029
- path,
1030
- ])
1031
- }})
994
+ assist({
995
+ fieldActions: {
996
+ title: 'Custom actions',
997
+ useFieldActions: (props) => {
998
+ const {
999
+ documentSchemaType,
1000
+ schemaId,
1001
+ getDocumentValue,
1002
+ getConditionalPaths,
1003
+ documentIdForAction,
1004
+ path,
1005
+ } = props
1006
+ const client = useClient({apiVersion: 'vX'})
1007
+ return useMemo(() => {
1008
+ return [
1009
+ defineAssistFieldAction({
1010
+ title: 'Fix spelling',
1011
+ icon: TranslateIcon,
1012
+ onAction: async () => {
1013
+ await client.agent.action.transform({
1014
+ schemaId,
1015
+ documentId: documentIdForAction,
1016
+ instruction: 'Fix any spelling mistakes',
1017
+ instructionParams: {field: {type: 'field', path}},
1018
+ // no need to send path for document actions
1019
+ target: path.length ? {path} : undefined,
1020
+ conditionalPaths: {paths: getConditionalPaths()},
1021
+ })
1022
+ },
1023
+ }),
1024
+ ]
1025
+ }, [
1026
+ client,
1027
+ documentSchemaType,
1028
+ schemaId,
1029
+ getDocumentValue,
1030
+ getConditionalPaths,
1031
+ documentIdForAction,
1032
+ path,
1033
+ ])
1034
+ },
1035
+ },
1036
+ })
1032
1037
  ```
1033
1038
 
1034
1039
  ##### Fill field (contextually aware)
@@ -1042,43 +1047,44 @@ The action will:
1042
1047
  - output to the field the action started from (`target.path`)
1043
1048
 
1044
1049
  ```ts
1045
- assist({
1046
- title: 'Custom actions',
1047
- useFieldActions: (props: AssistFieldActionProps) => {
1048
- const {
1049
- documentSchemaType,
1050
- actionType,
1051
- schemaId,
1052
- getDocumentValue,
1053
- getConditionalPaths,
1054
- documentIdForAction,
1055
- path,
1056
- schemaType,
1057
- } = props
1058
-
1059
- // hook usage has to happen outside onAction, so preassemble state in useFieldActions and pass to useMemo
1060
- const client = useClient({apiVersion: 'vX'})
1061
-
1062
- return useMemo(() => {
1063
- if (actionType === 'document') {
1064
- // in this case we dont want a document action
1065
- return []
1066
- }
1067
-
1068
- return [
1069
- defineAssistFieldAction({
1070
- title: 'Fill field',
1071
- icon: EditIcon,
1072
- onAction: async () => {
1073
- await client.agent.action.generate({
1074
- schemaId,
1075
- targetDocument: {
1076
- operation: 'createIfNotExists',
1077
- _id: documentIdForAction,
1078
- _type: documentSchemaType.name,
1079
- initialValues: getDocumentValue(),
1080
- },
1081
- instruction: `
1050
+ assist({
1051
+ fieldActions: {
1052
+ title: 'Custom actions',
1053
+ useFieldActions: (props) => {
1054
+ const {
1055
+ documentSchemaType,
1056
+ actionType,
1057
+ schemaId,
1058
+ getDocumentValue,
1059
+ getConditionalPaths,
1060
+ documentIdForAction,
1061
+ path,
1062
+ schemaType,
1063
+ } = props
1064
+
1065
+ // hook usage has to happen outside onAction, so preassemble state in useFieldActions and pass to useMemo
1066
+ const client = useClient({apiVersion: 'vX'})
1067
+
1068
+ return useMemo(() => {
1069
+ if (actionType === 'document') {
1070
+ // in this case we dont want a document action
1071
+ return []
1072
+ }
1073
+
1074
+ return [
1075
+ defineAssistFieldAction({
1076
+ title: 'Fill field',
1077
+ icon: EditIcon,
1078
+ onAction: async () => {
1079
+ await client.agent.action.generate({
1080
+ schemaId,
1081
+ targetDocument: {
1082
+ operation: 'createIfNotExists',
1083
+ _id: documentIdForAction,
1084
+ _type: documentSchemaType.name,
1085
+ initialValues: getDocumentValue(),
1086
+ },
1087
+ instruction: `
1082
1088
  We are generating a new value for a document field.
1083
1089
  The document type is ${documentSchemaType.name}, and the document type title is ${documentSchemaType.title}
1084
1090
  The document language is: "$lang" (use en-US if unspecified)
@@ -1093,33 +1099,34 @@ assist({
1093
1099
  Generate a new field value. The new value should be relevant to the document type and context.
1094
1100
  Keep it interesting. Generate using the document language.
1095
1101
  `,
1096
- instructionParams: {
1097
- doc: {type: 'document'},
1098
- field: {type: 'field', path},
1099
- lang: {type: 'field', path: ['language']},
1100
- },
1101
- target: {
1102
- path,
1103
- },
1104
- conditionalPaths: {
1105
- paths: getConditionalPaths(),
1106
- },
1107
- })
1108
- },
1109
- })
1102
+ instructionParams: {
1103
+ doc: {type: 'document'},
1104
+ field: {type: 'field', path},
1105
+ lang: {type: 'field', path: ['language']},
1106
+ },
1107
+ target: {
1108
+ path,
1109
+ },
1110
+ conditionalPaths: {
1111
+ paths: getConditionalPaths(),
1112
+ },
1113
+ })
1114
+ },
1115
+ }),
1110
1116
  ]
1111
- }, [
1112
- client,
1113
- documentSchemaType,
1114
- schemaId,
1115
- getDocumentValue,
1116
- getConditionalPaths,
1117
- documentIdForAction,
1118
- actionType,
1119
- path,
1120
- schemaType,
1121
- ])
1122
- }
1117
+ }, [
1118
+ client,
1119
+ documentSchemaType,
1120
+ schemaId,
1121
+ getDocumentValue,
1122
+ getConditionalPaths,
1123
+ documentIdForAction,
1124
+ actionType,
1125
+ path,
1126
+ schemaType,
1127
+ ])
1128
+ },
1129
+ },
1123
1130
  })
1124
1131
  ```
1125
1132
 
@@ -1147,20 +1154,17 @@ Adds a group to hold one or more actions (or nested groups).
1147
1154
 
1148
1155
  By default, any actions returned by `useFieldActions` will be grouped under `title`.
1149
1156
  ```ts
1150
- assist({
1151
- title: 'Default group',
1152
- useFieldActions: (props) => {
1153
- return [
1154
- defineAssistFieldAction({/* ... */}),
1155
- defineAssistFieldActionGroup({
1156
- title: 'More actions',
1157
- children: [
1158
- defineAssistFieldAction({/* ... */}),
1159
- ],
1160
- })
1161
- ]
1162
- }
1163
- })
1157
+ useFieldActions: (props) => {
1158
+ return [
1159
+ defineAssistFieldAction({/* ... */}),
1160
+ defineAssistFieldActionGroup({
1161
+ title: 'More actions',
1162
+ children: [
1163
+ defineAssistFieldAction({/* ... */}),
1164
+ ],
1165
+ })
1166
+ ]
1167
+ }
1164
1168
  ```
1165
1169
 
1166
1170
  #### Only groups in `useFieldActions`
@@ -1170,16 +1174,14 @@ If `useFieldActions` _only_ returns groups, the default wrapper group will be om
1170
1174
  Adds a divider between actions or groups. Takes no arguments:
1171
1175
 
1172
1176
  ```ts
1173
- assist({
1174
- title: 'Custom actions',
1175
- useFieldActions: (props) => {
1176
- return useMemo(() => [
1177
- defineAssistFieldAction({/* ... */}),
1178
- defineFieldActionDivider(),
1179
- defineAssistFieldAction({/* ... */}),
1180
- ], [])
1181
- }
1182
- })
1177
+
1178
+ useFieldActions: (props) => {
1179
+ return useMemo(() => [
1180
+ defineAssistFieldAction({/* ... */}),
1181
+ defineFieldActionDivider(),
1182
+ defineAssistFieldAction({/* ... */}),
1183
+ ], [])
1184
+ }
1183
1185
  ```
1184
1186
 
1185
1187
  ### `useUserInput`
@@ -1196,39 +1198,44 @@ When the user completes the dialog, the user inputted text will be available (or
1196
1198
 
1197
1199
 
1198
1200
  ```ts
1199
- assist({
1200
- title: 'Custom actions',
1201
- useFieldActions: (props: AssistFieldActionProps) => {
1202
- const getUserInput = useUserInput()
1203
-
1204
- return useMemo(() => [
1205
- defineAssistFieldAction({
1206
- title: 'Do something with user input',
1207
- icon: ActionIcon,
1208
- onAction: async () => {
1209
- const inputResult = await getUserInput({
1210
- title: 'What do you want to do?', // dialog title
1211
- inputs: [
1212
- {
1213
- id: 'topic',
1214
- title: 'Topic',
1215
- },
1216
- {
1217
- id: 'facts',
1218
- title: 'Facts',
1219
- description: 'Provide additional facts that will be used by the action'
1220
- },
1221
- ],
1222
- })
1223
- if(!inputResult) {
1224
- return // user closed the dialog
1225
- }
1226
-
1227
- //use the result from each input
1228
- //const [{result: topic}, {result: facts}] = inputResult
1229
- },
1230
- })], [getUserInput])
1231
- }
1201
+ ({
1202
+ fieldActions: {
1203
+ title: 'Custom actions',
1204
+ useFieldActions: (props) => {
1205
+ const getUserInput = useUserInput()
1206
+
1207
+ return useMemo(
1208
+ () => [
1209
+ defineAssistFieldAction({
1210
+ title: 'Do something with user input',
1211
+ onAction: async () => {
1212
+ const inputResult = await getUserInput({
1213
+ title: 'What do you want to do?', // dialog title
1214
+ inputs: [
1215
+ {
1216
+ id: 'topic',
1217
+ title: 'Topic',
1218
+ },
1219
+ {
1220
+ id: 'facts',
1221
+ title: 'Facts',
1222
+ description: 'Provide additional facts that will be used by the action',
1223
+ },
1224
+ ],
1225
+ })
1226
+ if (!inputResult) {
1227
+ return // user closed the dialog
1228
+ }
1229
+
1230
+ //use the result from each input
1231
+ //const [{result: topic}, {result: facts}] = inputResult
1232
+ },
1233
+ }),
1234
+ ],
1235
+ [getUserInput],
1236
+ )
1237
+ },
1238
+ },
1232
1239
  })
1233
1240
  ```
1234
1241
 
package/dist/index.d.mts CHANGED
@@ -451,6 +451,14 @@ declare type InlinePromptBlock = PortableTextSpan | FieldRef | UserInputBlock |
451
451
 
452
452
  declare const instructionContextTypeName: 'sanity.assist.instruction.context'
453
453
 
454
+ /**
455
+ * Returns true if the `schemaType` or any of its parent types (`schemaType.type`)` has `name` equal
456
+ * to `typeName`.
457
+ *
458
+ * Useful for checking if `schemaType` is a type alias of `ìmage`, `code` or similar.
459
+ */
460
+ export declare function isType(schemaType: SchemaType, typeName: string): boolean
461
+
454
462
  export declare interface Language {
455
463
  id: string
456
464
  title?: string
package/dist/index.d.ts CHANGED
@@ -451,6 +451,14 @@ declare type InlinePromptBlock = PortableTextSpan | FieldRef | UserInputBlock |
451
451
 
452
452
  declare const instructionContextTypeName: 'sanity.assist.instruction.context'
453
453
 
454
+ /**
455
+ * Returns true if the `schemaType` or any of its parent types (`schemaType.type`)` has `name` equal
456
+ * to `typeName`.
457
+ *
458
+ * Useful for checking if `schemaType` is a type alias of `ìmage`, `code` or similar.
459
+ */
460
+ export declare function isType(schemaType: SchemaType, typeName: string): boolean
461
+
454
462
  export declare interface Language {
455
463
  id: string
456
464
  title?: string
package/dist/index.esm.js CHANGED
@@ -4236,6 +4236,7 @@ export {
4236
4236
  defineAssistFieldAction,
4237
4237
  defineAssistFieldActionGroup,
4238
4238
  defineFieldActionDivider,
4239
+ isType,
4239
4240
  useUserInput
4240
4241
  };
4241
4242
  //# sourceMappingURL=index.esm.js.map