@sanity/assist 4.1.0 → 4.3.0
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 +302 -0
- package/dist/index.d.mts +269 -1
- package/dist/index.d.ts +269 -1
- package/dist/index.esm.js +244 -103
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +239 -98
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +244 -103
- package/dist/index.mjs.map +1 -1
- package/package.json +9 -8
- package/src/assistDocument/AssistDocumentContext.tsx +14 -1
- package/src/assistDocument/hooks/useAssistDocumentContextValue.tsx +30 -3
- package/src/assistLayout/RunInstructionProvider.tsx +75 -23
- package/src/components/ImageContext.tsx +4 -4
- package/src/fieldActions/assistFieldActions.tsx +42 -2
- package/src/fieldActions/customFieldActions.tsx +304 -0
- package/src/fieldActions/useUserInput.ts +107 -0
- package/src/helpers/typeUtils.ts +13 -3
- package/src/index.ts +17 -0
- package/src/plugin.tsx +6 -0
- package/src/presence/AssistDocumentPresence.tsx +3 -3
- package/src/schemas/typeDefExtensions.ts +12 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import {AgentActionPath} from '@sanity/client/stega'
|
|
1
2
|
import {CurrentUser} from 'sanity'
|
|
3
|
+
import {DocumentFieldActionDivider} from 'sanity'
|
|
4
|
+
import {DocumentFieldActionGroup} from 'sanity'
|
|
5
|
+
import {DocumentFieldActionItem} from 'sanity'
|
|
2
6
|
import {JSX as JSX_2} from 'react/jsx-runtime'
|
|
3
7
|
import {ObjectSchemaType} from 'sanity'
|
|
4
8
|
import {Path} from 'sanity'
|
|
@@ -8,7 +12,15 @@ import {PortableTextMarkDefinition} from '@portabletext/types'
|
|
|
8
12
|
import {PortableTextSpan} from '@portabletext/types'
|
|
9
13
|
import {SanityClient} from 'sanity'
|
|
10
14
|
import type {SanityClient as SanityClient_2} from '@sanity/client'
|
|
15
|
+
import {SanityDocumentLike} from 'sanity'
|
|
11
16
|
import {SchemaType} from 'sanity'
|
|
17
|
+
import {SchemaType as SchemaType_2} from '@sanity/types'
|
|
18
|
+
|
|
19
|
+
declare interface AgentActionConditionalPath {
|
|
20
|
+
path: AgentActionPath
|
|
21
|
+
readOnly: boolean
|
|
22
|
+
hidden: boolean
|
|
23
|
+
}
|
|
12
24
|
|
|
13
25
|
export declare const assist: Plugin_2<void | AssistPluginConfig>
|
|
14
26
|
|
|
@@ -62,6 +74,135 @@ export declare interface AssistConfig {
|
|
|
62
74
|
temperature?: number
|
|
63
75
|
}
|
|
64
76
|
|
|
77
|
+
export declare type AssistFieldActionGroup = Omit<
|
|
78
|
+
DocumentFieldActionGroup,
|
|
79
|
+
'renderAsButton' | 'expanded' | 'children'
|
|
80
|
+
> & {
|
|
81
|
+
children: AssistFieldActionNode[]
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export declare type AssistFieldActionItem = Omit<
|
|
85
|
+
DocumentFieldActionItem,
|
|
86
|
+
'renderAsButton' | 'selected' | 'onAction'
|
|
87
|
+
> & {
|
|
88
|
+
onAction: () => void | Promise<void>
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export declare type AssistFieldActionNode =
|
|
92
|
+
| AssistFieldActionItem
|
|
93
|
+
| AssistFieldActionGroup
|
|
94
|
+
| DocumentFieldActionDivider
|
|
95
|
+
|
|
96
|
+
export declare interface AssistFieldActionProps {
|
|
97
|
+
/**
|
|
98
|
+
* `actionType` will be `document` for action invoked from the top right document action menu, and
|
|
99
|
+
* `field` when invoked from a field action menu.
|
|
100
|
+
*/
|
|
101
|
+
actionType: 'document' | 'field'
|
|
102
|
+
/**
|
|
103
|
+
* This is the id of the current document pane; it contains `drafts.`or `versions. prefix` ect depending on context.
|
|
104
|
+
* Use this for `documentId` when calling any `client.agent.action`.
|
|
105
|
+
*
|
|
106
|
+
* It is generally recommended to call actions from the studio like this:
|
|
107
|
+
* ```ts
|
|
108
|
+
* await client.agent.action.generate({
|
|
109
|
+
* targetDocument: {
|
|
110
|
+
* operation: 'createIfNotExists',
|
|
111
|
+
* _id: props.documentIdForAction,
|
|
112
|
+
* _type: props.documentSchemaType.name,
|
|
113
|
+
* initialValues: props.getDocumentValue()
|
|
114
|
+
* },
|
|
115
|
+
* //...
|
|
116
|
+
* })
|
|
117
|
+
* ```
|
|
118
|
+
*/
|
|
119
|
+
documentIdForAction: string
|
|
120
|
+
/**
|
|
121
|
+
* Schema type of the current document.
|
|
122
|
+
* @see documentIdForAction
|
|
123
|
+
*/
|
|
124
|
+
documentSchemaType: ObjectSchemaType
|
|
125
|
+
/**
|
|
126
|
+
* Returns the current document value.
|
|
127
|
+
*
|
|
128
|
+
* Prefer passing this function to your hooks instead of passing the document value directly to avoid unnecessary re-renders.
|
|
129
|
+
* @see documentIdForAction
|
|
130
|
+
*/
|
|
131
|
+
getDocumentValue: () => SanityDocumentLike
|
|
132
|
+
/**
|
|
133
|
+
* Returns the current readOnly and hidden state of all conditional members in the current document form.
|
|
134
|
+
*
|
|
135
|
+
* Intended to be passed to agent actions `conditionalPaths.paths`.
|
|
136
|
+
*/
|
|
137
|
+
getConditionalPaths: () => AgentActionConditionalPath[]
|
|
138
|
+
/**
|
|
139
|
+
* `schemaId` for the current workspace.
|
|
140
|
+
*
|
|
141
|
+
* Note: the workspace schema has to be deployed using `sanity schema deploy` or `sanity deploy`.
|
|
142
|
+
*
|
|
143
|
+
* Use this for `schemaId` when calling any `client.agent.action`.
|
|
144
|
+
*
|
|
145
|
+
* It is generally recommended to call actions from the studio like this:
|
|
146
|
+
* ```ts
|
|
147
|
+
* await client.agent.action.generate({
|
|
148
|
+
* targetDocument: {
|
|
149
|
+
* operation: 'createIfNotExists',
|
|
150
|
+
* _id: props.documentIdForAction,
|
|
151
|
+
* _type: props.documentSchemaType.name,
|
|
152
|
+
* initialValues: props.getDocumentValue()
|
|
153
|
+
* },
|
|
154
|
+
* //...
|
|
155
|
+
* })
|
|
156
|
+
*/
|
|
157
|
+
schemaId: string
|
|
158
|
+
/**
|
|
159
|
+
* This is the schema type of the field the actions will be attached to (ie, schemaType for `path`)
|
|
160
|
+
*
|
|
161
|
+
* It can be used with agent actions using `target.path`, to scope the action to a specific field.
|
|
162
|
+
*
|
|
163
|
+
* It is generally recommended to call actions from the studio like this:
|
|
164
|
+
* ```ts
|
|
165
|
+
* await client.agent.action.generate({
|
|
166
|
+
* targetDocument: {
|
|
167
|
+
* operation: 'createIfNotExists',
|
|
168
|
+
* _id: props.documentIdForAction,
|
|
169
|
+
* _type: props.documentSchemaType.name,
|
|
170
|
+
* initialValues: props.getDocumentValue()
|
|
171
|
+
* },
|
|
172
|
+
* target: {
|
|
173
|
+
* path: props.path
|
|
174
|
+
* },
|
|
175
|
+
* })
|
|
176
|
+
* ```
|
|
177
|
+
*/
|
|
178
|
+
path: AgentActionPath
|
|
179
|
+
/**
|
|
180
|
+
* This is the schema type of the field the actions will be attached to (ie, schemaType for `path`).
|
|
181
|
+
*
|
|
182
|
+
* Typically useful to dynamically return different actions based on the schema type of the field.
|
|
183
|
+
*
|
|
184
|
+
* ```ts
|
|
185
|
+
* if(isObjectSchemaType(schemaType)) {
|
|
186
|
+
* return [
|
|
187
|
+
* defineAssistFieldAction({
|
|
188
|
+
* title: 'Fill the object fields',
|
|
189
|
+
* icon: RobotIcon,
|
|
190
|
+
* onAction: () => {
|
|
191
|
+
* //...
|
|
192
|
+
* }
|
|
193
|
+
* })
|
|
194
|
+
* ]
|
|
195
|
+
* }
|
|
196
|
+
* return useMemo(() => {
|
|
197
|
+
*
|
|
198
|
+
*
|
|
199
|
+
* }, [])
|
|
200
|
+
*
|
|
201
|
+
* ```
|
|
202
|
+
*/
|
|
203
|
+
schemaType: SchemaType_2
|
|
204
|
+
}
|
|
205
|
+
|
|
65
206
|
export declare interface AssistOptions {
|
|
66
207
|
aiAssist?: {
|
|
67
208
|
/** Set to true to disable assistance for this field or type */
|
|
@@ -80,6 +221,10 @@ declare interface AssistPluginConfig {
|
|
|
80
221
|
* Config that affects all instructions
|
|
81
222
|
*/
|
|
82
223
|
assist?: AssistConfig
|
|
224
|
+
fieldActions?: {
|
|
225
|
+
title?: string
|
|
226
|
+
useFieldActions?: (props: AssistFieldActionProps) => AssistFieldActionNode[]
|
|
227
|
+
}
|
|
83
228
|
/**
|
|
84
229
|
* @internal
|
|
85
230
|
*/
|
|
@@ -104,6 +249,35 @@ declare interface ContextBlock {
|
|
|
104
249
|
|
|
105
250
|
export declare const contextDocumentTypeName: 'assist.instruction.context'
|
|
106
251
|
|
|
252
|
+
/**
|
|
253
|
+
*
|
|
254
|
+
*/
|
|
255
|
+
export declare interface CustomInput {
|
|
256
|
+
/**
|
|
257
|
+
* Id for the input
|
|
258
|
+
*/
|
|
259
|
+
id: string
|
|
260
|
+
/**
|
|
261
|
+
* Title of the input field
|
|
262
|
+
*/
|
|
263
|
+
title: string
|
|
264
|
+
/**
|
|
265
|
+
* Additional info that will be displayed over the input
|
|
266
|
+
*/
|
|
267
|
+
description?: string
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
export declare type CustomInputResult = {
|
|
271
|
+
/**
|
|
272
|
+
* Identifies which custom input the `result`belongs to
|
|
273
|
+
*/
|
|
274
|
+
input: CustomInput
|
|
275
|
+
/**
|
|
276
|
+
* The text provided by the user in the input
|
|
277
|
+
*/
|
|
278
|
+
result: string
|
|
279
|
+
}
|
|
280
|
+
|
|
107
281
|
/**
|
|
108
282
|
* Default implementation for plugin config `translate.field.translationOutputs`
|
|
109
283
|
*
|
|
@@ -111,6 +285,16 @@ export declare const contextDocumentTypeName: 'assist.instruction.context'
|
|
|
111
285
|
*/
|
|
112
286
|
export declare const defaultLanguageOutputs: TranslationOutputsFunction
|
|
113
287
|
|
|
288
|
+
export declare function defineAssistFieldAction(
|
|
289
|
+
action: Omit<AssistFieldActionItem, 'type'>,
|
|
290
|
+
): AssistFieldActionItem
|
|
291
|
+
|
|
292
|
+
export declare function defineAssistFieldActionGroup(
|
|
293
|
+
group: Omit<AssistFieldActionGroup, 'type'>,
|
|
294
|
+
): AssistFieldActionGroup
|
|
295
|
+
|
|
296
|
+
export declare function defineFieldActionDivider(): DocumentFieldActionDivider
|
|
297
|
+
|
|
114
298
|
export declare interface DocumentMember {
|
|
115
299
|
schemaType: SchemaType
|
|
116
300
|
path: Path
|
|
@@ -252,6 +436,17 @@ export declare interface FieldTranslationConfig {
|
|
|
252
436
|
maxPathDepth?: number
|
|
253
437
|
}
|
|
254
438
|
|
|
439
|
+
export declare type GetUserInput = (args: {
|
|
440
|
+
/**
|
|
441
|
+
* Dialog title
|
|
442
|
+
*/
|
|
443
|
+
title: string
|
|
444
|
+
/**
|
|
445
|
+
* One titled input per array item
|
|
446
|
+
*/
|
|
447
|
+
inputs: CustomInput[]
|
|
448
|
+
}) => Promise<CustomInputResult[] | undefined>
|
|
449
|
+
|
|
255
450
|
declare type InlinePromptBlock = PortableTextSpan | FieldRef | UserInputBlock | ContextBlock
|
|
256
451
|
|
|
257
452
|
declare const instructionContextTypeName: 'sanity.assist.instruction.context'
|
|
@@ -395,6 +590,68 @@ declare interface UserInputBlock {
|
|
|
395
590
|
|
|
396
591
|
declare const userInputTypeName: 'sanity.assist.instruction.userInput'
|
|
397
592
|
|
|
593
|
+
/**
|
|
594
|
+
* `useUserInput` returns a function that can be used to await user input.
|
|
595
|
+
*
|
|
596
|
+
* Useful for custom `fieldActions` to get user input for populating Agent Action requests,.
|
|
597
|
+
*
|
|
598
|
+
* ```ts
|
|
599
|
+
* fieldActions: {
|
|
600
|
+
* useFieldActions: (props) => {
|
|
601
|
+
* const {
|
|
602
|
+
* documentSchemaType,
|
|
603
|
+
* schemaId,
|
|
604
|
+
* getDocumentValue,
|
|
605
|
+
* getConditionalPaths,
|
|
606
|
+
* documentIdForAction,
|
|
607
|
+
* } = props
|
|
608
|
+
* const client = useClient({apiVersion: 'vX'})
|
|
609
|
+
* const getUserInput = useUserInput()
|
|
610
|
+
* return useMemo(() => {
|
|
611
|
+
* return [
|
|
612
|
+
* defineAssistFieldAction({
|
|
613
|
+
* title: 'Log user input',
|
|
614
|
+
* icon: UserIcon,
|
|
615
|
+
* onAction: async () => {
|
|
616
|
+
* const input = await getUserInput({
|
|
617
|
+
* title: 'Topic',
|
|
618
|
+
* inputs: [{id: 'about', title: 'What should the article be about?'}],
|
|
619
|
+
* })
|
|
620
|
+
* if (!input) return // user canceled input
|
|
621
|
+
* await client.agent.action.generate({
|
|
622
|
+
* schemaId,
|
|
623
|
+
* targetDocument: {
|
|
624
|
+
* operation: 'createIfNotExists',
|
|
625
|
+
* _id: documentIdForAction,
|
|
626
|
+
* _type: documentSchemaType.name,
|
|
627
|
+
* initialValues: getDocumentValue(),
|
|
628
|
+
* },
|
|
629
|
+
* instruction: `
|
|
630
|
+
* Create a document about the following topic:
|
|
631
|
+
* $about
|
|
632
|
+
* ---
|
|
633
|
+
* `,
|
|
634
|
+
* instructionParams: {about: input[0].result},
|
|
635
|
+
* conditionalPaths: {paths: getConditionalPaths()},
|
|
636
|
+
* })
|
|
637
|
+
* },
|
|
638
|
+
* }),
|
|
639
|
+
* ]
|
|
640
|
+
* }, [
|
|
641
|
+
* client,
|
|
642
|
+
* documentSchemaType,
|
|
643
|
+
* schemaId,
|
|
644
|
+
* getDocumentValue,
|
|
645
|
+
* getConditionalPaths,
|
|
646
|
+
* documentIdForAction,
|
|
647
|
+
* getUserInput,
|
|
648
|
+
* ])
|
|
649
|
+
* },
|
|
650
|
+
* }
|
|
651
|
+
* ```
|
|
652
|
+
*/
|
|
653
|
+
export declare function useUserInput(): GetUserInput
|
|
654
|
+
|
|
398
655
|
export {}
|
|
399
656
|
|
|
400
657
|
declare module 'sanity' {
|
|
@@ -474,7 +731,18 @@ declare module 'sanity' {
|
|
|
474
731
|
* })
|
|
475
732
|
* ```
|
|
476
733
|
*/
|
|
477
|
-
imageDescriptionField?:
|
|
734
|
+
imageDescriptionField?:
|
|
735
|
+
| string
|
|
736
|
+
| {
|
|
737
|
+
path: string
|
|
738
|
+
/**
|
|
739
|
+
* When updateOnImageChange is true (or undefined), whenever the
|
|
740
|
+
* image asset changes, imageDescriptionField will be regenerated.
|
|
741
|
+
*
|
|
742
|
+
* default: true
|
|
743
|
+
* */
|
|
744
|
+
updateOnImageChange?: boolean
|
|
745
|
+
}
|
|
478
746
|
}
|
|
479
747
|
}
|
|
480
748
|
interface NumberOptions extends AssistOptions {}
|