@sanity/sdk-react 2.3.1 → 2.5.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/dist/index.d.ts CHANGED
@@ -1,4 +1,11 @@
1
1
  import {ActionsResult} from '@sanity/sdk'
2
+ import {AgentGenerateOptions} from '@sanity/sdk'
3
+ import {AgentPatchOptions} from '@sanity/sdk'
4
+ import {AgentPatchResult} from '@sanity/sdk'
5
+ import {AgentPromptOptions} from '@sanity/sdk'
6
+ import {AgentPromptResult} from '@sanity/sdk'
7
+ import {AgentTransformOptions} from '@sanity/sdk'
8
+ import {AgentTranslateOptions} from '@sanity/sdk'
2
9
  import {ApplyDocumentActionsOptions} from '@sanity/sdk'
3
10
  import {AuthState} from '@sanity/sdk'
4
11
  import {CanvasResource} from '@sanity/message-protocol'
@@ -11,6 +18,7 @@ import {DocumentEvent} from '@sanity/sdk'
11
18
  import {DocumentHandle} from '@sanity/sdk'
12
19
  import {DocumentOptions} from '@sanity/sdk'
13
20
  import {DocumentPermissionsResult} from '@sanity/sdk'
21
+ import {DocumentSource} from '@sanity/sdk'
14
22
  import {FallbackProps} from 'react-error-boundary'
15
23
  import {FavoriteStatusResponse} from '@sanity/sdk'
16
24
  import {FrameMessage} from '@sanity/sdk'
@@ -44,6 +52,24 @@ import {StudioResource} from '@sanity/message-protocol'
44
52
  import {UserPresence} from '@sanity/sdk'
45
53
  import {WindowMessage} from '@sanity/sdk'
46
54
 
55
+ /**
56
+ * @public
57
+ */
58
+ export declare interface AgentResourceContextOptions {
59
+ /**
60
+ * The project ID of the current context
61
+ */
62
+ projectId: string
63
+ /**
64
+ * The dataset of the current context
65
+ */
66
+ dataset: string
67
+ /**
68
+ * Optional document ID if the user is viewing/editing a specific document
69
+ */
70
+ documentId?: string
71
+ }
72
+
47
73
  /**
48
74
  * A component that handles authentication flow and error boundaries for a
49
75
  * protected section of the application.
@@ -142,6 +168,26 @@ declare interface DashboardResource {
142
168
 
143
169
  export {DatasetsResponse}
144
170
 
171
+ /**
172
+ * Return type for the useDispatchIntent hook
173
+ * @beta
174
+ */
175
+ declare interface DispatchIntent {
176
+ dispatchIntent: () => void
177
+ }
178
+
179
+ /**
180
+ * Document handle that optionally includes a source (e.g., media library source)
181
+ * or projectId and dataset for traditional dataset sources
182
+ * (but now marked optional since it's valid to just use a source)
183
+ * @beta
184
+ */
185
+ declare interface DocumentHandleWithSource extends Omit<DocumentHandle, 'projectId' | 'dataset'> {
186
+ source?: DocumentSource
187
+ projectId?: string
188
+ dataset?: string
189
+ }
190
+
145
191
  declare interface DocumentInteractionHistory {
146
192
  recordEvent: (eventType: 'viewed' | 'edited' | 'created' | 'deleted') => void
147
193
  }
@@ -156,8 +202,8 @@ export declare interface DocumentsOptions<
156
202
  TDocumentType extends string = string,
157
203
  TDataset extends string = string,
158
204
  TProjectId extends string = string,
159
- > extends DatasetHandle<TDataset, TProjectId>,
160
- Pick<QueryOptions, 'perspective' | 'params'> {
205
+ >
206
+ extends DatasetHandle<TDataset, TProjectId>, Pick<QueryOptions, 'perspective' | 'params'> {
161
207
  /**
162
208
  * Filter documents by their `_type`. Can be a single type or an array of types.
163
209
  */
@@ -269,6 +315,12 @@ export declare interface NavigateToStudioResult {
269
315
  navigateToStudioDocument: () => void
270
316
  }
271
317
 
318
+ declare interface Observer<T> {
319
+ next?: (value: T) => void
320
+ error?: (err: unknown) => void
321
+ complete?: () => void
322
+ }
323
+
272
324
  /**
273
325
  * Configuration options for the usePaginatedDocuments hook
274
326
  *
@@ -569,6 +621,19 @@ declare interface StudioWorkspacesResult {
569
621
  error: string | null
570
622
  }
571
623
 
624
+ declare interface Subscribable<T> {
625
+ subscribe(observer: Observer<T>): Subscription
626
+ subscribe(
627
+ next: (value: T) => void,
628
+ error?: (err: unknown) => void,
629
+ complete?: () => void,
630
+ ): Subscription
631
+ }
632
+
633
+ declare interface Subscription {
634
+ unsubscribe(): void
635
+ }
636
+
572
637
  declare type Updater<TValue> = TValue | ((currentValue: TValue) => TValue)
573
638
 
574
639
  /**
@@ -595,6 +660,102 @@ declare type UseActiveReleases = {
595
660
  */
596
661
  export declare const useActiveReleases: UseActiveReleases
597
662
 
663
+ /**
664
+ * @alpha
665
+ * Generates content for a document (or specific fields) via Sanity Agent Actions.
666
+ * - Uses instruction templates with `$variables` and supports `instructionParams` (constants, fields, documents, GROQ queries).
667
+ * - Can target specific paths/fields; supports image generation when targeting image fields.
668
+ * - Supports optional `temperature`, `async`, `noWrite`, and `conditionalPaths`.
669
+ *
670
+ * Returns a stable callback that triggers the action and yields a Subscribable stream.
671
+ */
672
+ export declare const useAgentGenerate: () => (
673
+ options: AgentGenerateOptions,
674
+ ) => Subscribable<unknown>
675
+
676
+ /**
677
+ * @alpha
678
+ * Schema-aware patching with Sanity Agent Actions.
679
+ * - Validates provided paths/values against the document schema and merges object values safely.
680
+ * - Prevents duplicate keys and supports array appends (including after a specific keyed item).
681
+ * - Accepts `documentId` or `targetDocument` (mutually exclusive).
682
+ * - Optional `async`, `noWrite`, `conditionalPaths`.
683
+ *
684
+ * Returns a stable callback that triggers the action and resolves a Promise with the patch result.
685
+ */
686
+ export declare const useAgentPatch: () => (options: AgentPatchOptions) => Promise<AgentPatchResult>
687
+
688
+ /**
689
+ * @alpha
690
+ * Prompts the LLM using the same instruction template format as other actions.
691
+ * - `format`: 'string' or 'json' (instruction must contain the word "json" for JSON responses).
692
+ * - Optional `temperature`.
693
+ *
694
+ * Returns a stable callback that triggers the action and resolves a Promise with the prompt result.
695
+ */
696
+ export declare const useAgentPrompt: () => (
697
+ options: AgentPromptOptions,
698
+ ) => Promise<AgentPromptResult>
699
+
700
+ /**
701
+ * @public
702
+ * Hook for emitting agent resource context updates to the Dashboard.
703
+ * This allows the Agent to understand what resource the user is currently
704
+ * interacting with (e.g., which document they're editing).
705
+ *
706
+ * The hook will automatically emit the context when it changes, and also
707
+ * emit the initial context when the hook is first mounted.
708
+ *
709
+ * @category Agent
710
+ * @param options - The resource context options containing projectId, dataset, and optional documentId
711
+ *
712
+ * @example
713
+ * ```tsx
714
+ * import {useAgentResourceContext} from '@sanity/sdk-react'
715
+ *
716
+ * function MyComponent() {
717
+ * const documentId = 'my-document-id'
718
+ *
719
+ * // Automatically updates the Agent's context whenever the document changes
720
+ * useAgentResourceContext({
721
+ * projectId: 'my-project',
722
+ * dataset: 'production',
723
+ * documentId,
724
+ * })
725
+ *
726
+ * return <div>Editing document: {documentId}</div>
727
+ * }
728
+ * ```
729
+ */
730
+ export declare function useAgentResourceContext(options: AgentResourceContextOptions): void
731
+
732
+ /**
733
+ * @alpha
734
+ * Transforms an existing document or selected fields using Sanity Agent Actions.
735
+ * - Accepts `instruction` and `instructionParams` (constants, fields, documents, GROQ queries).
736
+ * - Can write to the same or a different `targetDocument` (create/edit), and target specific paths.
737
+ * - Supports per-path image transform instructions and image description operations.
738
+ * - Optional `temperature`, `async`, `noWrite`, `conditionalPaths`.
739
+ *
740
+ * Returns a stable callback that triggers the action and yields a Subscribable stream.
741
+ */
742
+ export declare const useAgentTransform: () => (
743
+ options: AgentTransformOptions,
744
+ ) => Subscribable<unknown>
745
+
746
+ /**
747
+ * @alpha
748
+ * Translates documents or fields using Sanity Agent Actions.
749
+ * - Configure `fromLanguage`/`toLanguage`, optional `styleGuide`, and `protectedPhrases`.
750
+ * - Can write into a different `targetDocument`, and/or store language in a field.
751
+ * - Optional `temperature`, `async`, `noWrite`, `conditionalPaths`.
752
+ *
753
+ * Returns a stable callback that triggers the action and yields a Subscribable stream.
754
+ */
755
+ export declare const useAgentTranslate: () => (
756
+ options: AgentTranslateOptions,
757
+ ) => Subscribable<unknown>
758
+
598
759
  /**
599
760
  * @public
600
761
  */
@@ -702,6 +863,37 @@ declare interface UseApplyDocumentActions {
702
863
  * )
703
864
  * }
704
865
  * ```
866
+ *
867
+ * @example Create a document with initial field values
868
+ * ```tsx
869
+ * import {
870
+ * createDocument,
871
+ * createDocumentHandle,
872
+ * useApplyDocumentActions
873
+ * } from '@sanity/sdk-react'
874
+ *
875
+ * function CreateArticleButton() {
876
+ * const apply = useApplyDocumentActions()
877
+ *
878
+ * const handleCreateArticle = () => {
879
+ * const newDocHandle = createDocumentHandle({
880
+ * documentId: crypto.randomUUID(),
881
+ * documentType: 'article'
882
+ * })
883
+ *
884
+ * // Create document with initial values in one action
885
+ * apply(
886
+ * createDocument(newDocHandle, {
887
+ * title: 'New Article',
888
+ * author: 'John Doe',
889
+ * publishedAt: new Date().toISOString(),
890
+ * })
891
+ * )
892
+ * }
893
+ *
894
+ * return <button onClick={handleCreateArticle}>Create Article</button>
895
+ * }
896
+ * ```
705
897
  */
706
898
  export declare const useApplyDocumentActions: UseApplyDocumentActions
707
899
 
@@ -762,7 +954,7 @@ export declare const useAuthToken: () => string | null
762
954
  * @public
763
955
  * @function
764
956
  */
765
- export declare const useClient: (x: ClientOptions) => SanityClient
957
+ export declare const useClient: (options: ClientOptions) => SanityClient
766
958
 
767
959
  declare type UseCurrentUser = {
768
960
  /**
@@ -894,6 +1086,70 @@ declare type UseDatasets = {
894
1086
  */
895
1087
  export declare const useDatasets: UseDatasets
896
1088
 
1089
+ /**
1090
+ * @beta
1091
+ *
1092
+ * A hook for dispatching intent messages to the Dashboard with a document handle.
1093
+ * This allows applications to signal their intent to pass the referenced document to other applications that have registered the ability to perform specific actions on that document.
1094
+ *
1095
+ * @param params - Object containing:
1096
+ * - `action` - Action to perform (currently only 'edit' is supported). Will prompt a picker if multiple handlers are available.
1097
+ * - `intentId` - Specific ID of the intent to dispatch. Either `action` or `intentId` is required.
1098
+ * - `documentHandle` - The document handle containing document ID, type, and either:
1099
+ * - `projectId` and `dataset` for traditional dataset sources, like `{documentId: '123', documentType: 'book', projectId: 'abc123', dataset: 'production'}`
1100
+ * - `source` for media library, canvas, or dataset sources, like `{documentId: '123', documentType: 'sanity.asset', source: mediaLibrarySource('ml123')}` or `{documentId: '123', documentType: 'sanity.canvas.document', source: canvasSource('canvas123')}`
1101
+ * - `paremeters` - Optional parameters to include in the dispatch; will be passed to the resolved intent handler
1102
+ * @returns An object containing:
1103
+ * - `dispatchIntent` - Function to dispatch the intent message
1104
+ *
1105
+ * @example
1106
+ * ```tsx
1107
+ * import {useDispatchIntent} from '@sanity/sdk-react'
1108
+ * import {Button} from '@sanity/ui'
1109
+ * import {Suspense} from 'react'
1110
+ *
1111
+ * function DispatchIntentButton({documentId, documentType, projectId, dataset}) {
1112
+ * const {dispatchIntent} = useDispatchIntent({
1113
+ * action: 'edit',
1114
+ * documentHandle: {documentId, documentType, projectId, dataset},
1115
+ * })
1116
+ *
1117
+ * return (
1118
+ * <Button
1119
+ * onClick={() => dispatchIntent()}
1120
+ * text="Dispatch Intent"
1121
+ * />
1122
+ * )
1123
+ * }
1124
+ *
1125
+ * // Wrap the component with Suspense since the hook may suspend
1126
+ * function MyDocumentAction({documentId, documentType, projectId, dataset}) {
1127
+ * return (
1128
+ * <Suspense fallback={<Button text="Loading..." disabled />}>
1129
+ * <DispatchIntentButton
1130
+ * documentId={documentId}
1131
+ * documentType={documentType}
1132
+ * projectId={projectId}
1133
+ * dataset={dataset}
1134
+ * />
1135
+ * </Suspense>
1136
+ * )
1137
+ * }
1138
+ * ```
1139
+ */
1140
+ export declare function useDispatchIntent(params: UseDispatchIntentParams): DispatchIntent
1141
+
1142
+ /**
1143
+ * Parameters for the useDispatchIntent hook
1144
+ * @beta
1145
+ */
1146
+ declare interface UseDispatchIntentParams {
1147
+ action?: 'edit'
1148
+ intentId?: string
1149
+ documentHandle: DocumentHandleWithSource
1150
+ parameters?: Record<string, unknown>
1151
+ }
1152
+
897
1153
  declare interface UseDocument {
898
1154
  /** @internal */
899
1155
  <TDocumentType extends string, TDataset extends string, TProjectId extends string = string>(
@@ -2499,7 +2755,7 @@ export declare function useUser(options: GetUserOptions): UserResult
2499
2755
  * <address>{user.profile.email}</address>
2500
2756
  * </figure>
2501
2757
  * ))}
2502
- * {hasMore && <button onClick={loadMore}>{isPending ? 'Loading...' : 'Load More'</button>}
2758
+ * {hasMore && <button onClick={loadMore}>{isPending ? 'Loading...' : 'Load More'}</button>}
2503
2759
  * </div>
2504
2760
  * )
2505
2761
  * ```