@sanity/assist 4.2.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.
@@ -0,0 +1,107 @@
1
+ import {useRunInstruction} from '../assistLayout/RunInstructionProvider'
2
+
3
+ export type GetUserInput = (args: {
4
+ /**
5
+ * Dialog title
6
+ */
7
+ title: string
8
+ /**
9
+ * One titled input per array item
10
+ */
11
+ inputs: CustomInput[]
12
+ }) => Promise<CustomInputResult[] | undefined>
13
+
14
+ /**
15
+ *
16
+ */
17
+ export interface CustomInput {
18
+ /**
19
+ * Id for the input
20
+ */
21
+ id: string
22
+ /**
23
+ * Title of the input field
24
+ */
25
+ title: string
26
+ /**
27
+ * Additional info that will be displayed over the input
28
+ */
29
+ description?: string
30
+ }
31
+
32
+ export type CustomInputResult = {
33
+ /**
34
+ * Identifies which custom input the `result`belongs to
35
+ */
36
+ input: CustomInput
37
+
38
+ /**
39
+ * The text provided by the user in the input
40
+ */
41
+ result: string
42
+ }
43
+
44
+ /**
45
+ * `useUserInput` returns a function that can be used to await user input.
46
+ *
47
+ * Useful for custom `fieldActions` to get user input for populating Agent Action requests,.
48
+ *
49
+ * ```ts
50
+ * fieldActions: {
51
+ * useFieldActions: (props) => {
52
+ * const {
53
+ * documentSchemaType,
54
+ * schemaId,
55
+ * getDocumentValue,
56
+ * getConditionalPaths,
57
+ * documentIdForAction,
58
+ * } = props
59
+ * const client = useClient({apiVersion: 'vX'})
60
+ * const getUserInput = useUserInput()
61
+ * return useMemo(() => {
62
+ * return [
63
+ * defineAssistFieldAction({
64
+ * title: 'Log user input',
65
+ * icon: UserIcon,
66
+ * onAction: async () => {
67
+ * const input = await getUserInput({
68
+ * title: 'Topic',
69
+ * inputs: [{id: 'about', title: 'What should the article be about?'}],
70
+ * })
71
+ * if (!input) return // user canceled input
72
+ * await client.agent.action.generate({
73
+ * schemaId,
74
+ * targetDocument: {
75
+ * operation: 'createIfNotExists',
76
+ * _id: documentIdForAction,
77
+ * _type: documentSchemaType.name,
78
+ * initialValues: getDocumentValue(),
79
+ * },
80
+ * instruction: `
81
+ * Create a document about the following topic:
82
+ * $about
83
+ * ---
84
+ * `,
85
+ * instructionParams: {about: input[0].result},
86
+ * conditionalPaths: {paths: getConditionalPaths()},
87
+ * })
88
+ * },
89
+ * }),
90
+ * ]
91
+ * }, [
92
+ * client,
93
+ * documentSchemaType,
94
+ * schemaId,
95
+ * getDocumentValue,
96
+ * getConditionalPaths,
97
+ * documentIdForAction,
98
+ * getUserInput,
99
+ * ])
100
+ * },
101
+ * }
102
+ * ```
103
+ */
104
+ export function useUserInput(): GetUserInput {
105
+ const {getUserInput} = useRunInstruction()
106
+ return getUserInput
107
+ }
package/src/index.ts CHANGED
@@ -5,3 +5,20 @@ export {defaultLanguageOutputs} from './translate/paths'
5
5
  export * from './translate/types'
6
6
  export {contextDocumentTypeName} from './types'
7
7
  export * from './assistTypes'
8
+
9
+ export {
10
+ type AssistFieldActionProps,
11
+ type AssistFieldActionGroup,
12
+ type AssistFieldActionItem,
13
+ type AssistFieldActionNode,
14
+ defineAssistFieldAction,
15
+ defineFieldActionDivider,
16
+ defineAssistFieldActionGroup,
17
+ } from './fieldActions/customFieldActions'
18
+
19
+ export {
20
+ type GetUserInput,
21
+ type CustomInput,
22
+ type CustomInputResult,
23
+ useUserInput,
24
+ } from './fieldActions/useUserInput'
package/src/plugin.tsx CHANGED
@@ -21,6 +21,7 @@ import {createAssistDocumentPresence} from './presence/AssistDocumentPresence'
21
21
  import {schemaTypes} from './schemas'
22
22
  import {TranslationConfig} from './translate/types'
23
23
  import {assistDocumentTypeName, AssistPreset} from './types'
24
+ import {AssistFieldActionNode, AssistFieldActionProps} from './fieldActions/customFieldActions'
24
25
 
25
26
  export interface AssistPluginConfig {
26
27
  translate?: TranslationConfig
@@ -30,6 +31,11 @@ export interface AssistPluginConfig {
30
31
  */
31
32
  assist?: AssistConfig
32
33
 
34
+ fieldActions?: {
35
+ title?: string
36
+ useFieldActions?: (props: AssistFieldActionProps) => AssistFieldActionNode[]
37
+ }
38
+
33
39
  /**
34
40
  * @internal
35
41
  */
@@ -13,9 +13,9 @@ export function createAssistDocumentPresence(documentId: string | undefined) {
13
13
  }
14
14
 
15
15
  function AssistDocumentPresence() {
16
- const {assistDocument} = useAssistDocumentContext()
16
+ const {assistDocument, syntheticTasks} = useAssistDocumentContext()
17
17
  const anyPresence = useMemo(() => {
18
- const anyPresence = assistDocument?.tasks
18
+ const anyPresence = [...(assistDocument?.tasks ?? []), ...(syntheticTasks ?? [])]
19
19
  ?.filter((run) => !run.ended && !run.reason)
20
20
  ?.flatMap((run) => run.presence ?? [])
21
21
  .find((f) => f.started && new Date().getTime() - new Date(f.started).getTime() < 30000)
@@ -36,7 +36,7 @@ function AssistDocumentPresence() {
36
36
  [],
37
37
  )
38
38
  : undefined
39
- }, [assistDocument?.tasks])
39
+ }, [assistDocument?.tasks, syntheticTasks])
40
40
 
41
41
  return (
42
42
  <Card>