@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 +179 -172
- package/dist/index.d.mts +8 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.esm.js +1 -0
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
- package/src/helpers/typeUtils.ts +6 -0
- package/src/index.ts +2 -0
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
|
-
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
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
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
return
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1021
|
-
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
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
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1052
|
-
|
|
1053
|
-
|
|
1054
|
-
|
|
1055
|
-
|
|
1056
|
-
|
|
1057
|
-
|
|
1058
|
-
|
|
1059
|
-
|
|
1060
|
-
|
|
1061
|
-
|
|
1062
|
-
|
|
1063
|
-
|
|
1064
|
-
|
|
1065
|
-
|
|
1066
|
-
|
|
1067
|
-
|
|
1068
|
-
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
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
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
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
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
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
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
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
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
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
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1229
|
-
|
|
1230
|
-
|
|
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
|