@sanity/client 6.28.1 → 6.28.3-instruct.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.browser.cjs +44 -3
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +250 -10
- package/dist/index.browser.d.ts +250 -10
- package/dist/index.browser.js +44 -3
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +45 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +240 -11
- package/dist/index.d.ts +240 -11
- package/dist/index.js +45 -4
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +244 -10
- package/dist/stega.browser.d.ts +244 -10
- package/dist/stega.d.cts +244 -10
- package/dist/stega.d.ts +244 -10
- package/package.json +1 -1
- package/src/SanityClient.ts +5 -1
- package/src/assist/AssistClient.ts +87 -0
- package/src/assist/types.ts +203 -0
- package/src/index.ts +1 -1
- package/src/types.ts +20 -10
- package/src/util/shareReplayLatest.ts +4 -1
- package/umd/sanityClient.js +44 -3
- package/umd/sanityClient.min.js +2 -2
package/dist/stega.d.ts
CHANGED
|
@@ -110,6 +110,129 @@ export declare class AssetsClient {
|
|
|
110
110
|
): Promise<SanityAssetDocument | SanityImageAssetDocument>
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
/** @beta */
|
|
114
|
+
export declare type AssistAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
115
|
+
| ExistingDocumentRequest
|
|
116
|
+
| CreateDocumentRequest<T>
|
|
117
|
+
) &
|
|
118
|
+
AssistRequestBase &
|
|
119
|
+
Async
|
|
120
|
+
|
|
121
|
+
/** @public */
|
|
122
|
+
declare class AssistClient {
|
|
123
|
+
#private
|
|
124
|
+
constructor(client: SanityClient, httpRequest: HttpRequest)
|
|
125
|
+
instruct(request: AssistAsyncInstruction): Promise<{
|
|
126
|
+
_id: string
|
|
127
|
+
}>
|
|
128
|
+
instruct<DocumentShape extends Record<string, Any>>(
|
|
129
|
+
request: AssistSyncInstruction<DocumentShape>,
|
|
130
|
+
): Promise<IdentifiedSanityDocumentStub & DocumentShape>
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** @beta */
|
|
134
|
+
export declare type AssistInstruction<T extends Record<string, Any> = Record<string, Any>> =
|
|
135
|
+
| AssistSyncInstruction<T>
|
|
136
|
+
| AssistAsyncInstruction<T>
|
|
137
|
+
|
|
138
|
+
declare interface AssistRequestBase {
|
|
139
|
+
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
140
|
+
schemaId: string
|
|
141
|
+
/** string template using $variable – more on this below under "Dynamic instruction" */
|
|
142
|
+
instruction: string
|
|
143
|
+
/** param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable" */
|
|
144
|
+
instructionParams?: InstructionParams
|
|
145
|
+
/**
|
|
146
|
+
* Optional document path output target for the instruction.
|
|
147
|
+
* When provided, the instruction will apply to this path in the document and its children.
|
|
148
|
+
*
|
|
149
|
+
* ## Examples
|
|
150
|
+
* - `path: 'title'` will output to the title field in the document
|
|
151
|
+
* - `path: 'array[_key="xx"]'` will output to the item with `_key: 'xx'` in the array field
|
|
152
|
+
*/
|
|
153
|
+
path?: string
|
|
154
|
+
/**
|
|
155
|
+
* Controls sub-paths in the document that can be output to.
|
|
156
|
+
*
|
|
157
|
+
* The string-paths are relative to the `path` param
|
|
158
|
+
*
|
|
159
|
+
* Note: these path strings are less strictly validated than the `path` param itself:
|
|
160
|
+
* if an relative-path does not exist or is invalid, it will be silently ignored.
|
|
161
|
+
*
|
|
162
|
+
* @see AssistRequestBase#conditionalPaths
|
|
163
|
+
* @see AssistRequestBase#outputTypes
|
|
164
|
+
*/
|
|
165
|
+
relativeOutputPaths?:
|
|
166
|
+
| {
|
|
167
|
+
include: string[]
|
|
168
|
+
}
|
|
169
|
+
| {
|
|
170
|
+
exclude: string[]
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* Controls which types the instruction is allowed to output to.
|
|
174
|
+
*
|
|
175
|
+
* @see AssistRequestBase#relativeOutputPaths
|
|
176
|
+
* @see AssistRequestBase#conditionalPaths
|
|
177
|
+
*/
|
|
178
|
+
outputTypes?:
|
|
179
|
+
| {
|
|
180
|
+
include: string[]
|
|
181
|
+
}
|
|
182
|
+
| {
|
|
183
|
+
exclude: string[]
|
|
184
|
+
}
|
|
185
|
+
/**
|
|
186
|
+
* When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
|
|
187
|
+
*
|
|
188
|
+
* By default, AI Assist will not output to conditional `readOnly` and `hidden` fields,
|
|
189
|
+
* ie, they are considered to resolve to `readOnly: true` / `hidden: true`.
|
|
190
|
+
*
|
|
191
|
+
* `conditionalPaths` param allows setting the default conditional value for
|
|
192
|
+
* `hidden` and `readOnly` to false,
|
|
193
|
+
* or individually set `hidden` and `readOnly` state for individual document paths.
|
|
194
|
+
*
|
|
195
|
+
*
|
|
196
|
+
* Note: fields and types with explicit readOnly: true or hidden: true in the schema, are not available to AI Assist,
|
|
197
|
+
* and cannot be changed via conditionalPaths.
|
|
198
|
+
*
|
|
199
|
+
* conditionalPaths state only apply to fields and types that have conditional `hidden` or `readOnly` in their schema definition.
|
|
200
|
+
*
|
|
201
|
+
* @see AssistRequestBase#relativeOutputPaths
|
|
202
|
+
* @see AssistRequestBase#outputTypes
|
|
203
|
+
*/
|
|
204
|
+
conditionalPaths?: {
|
|
205
|
+
defaultReadOnly?: boolean
|
|
206
|
+
defaultHidden?: boolean
|
|
207
|
+
paths?: {
|
|
208
|
+
/** path here is not a relative path: it must be the full document path, regardless of `path` param on the request itself */
|
|
209
|
+
path: string
|
|
210
|
+
readOnly: boolean
|
|
211
|
+
hidden: boolean
|
|
212
|
+
}[]
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/** @beta */
|
|
217
|
+
export declare type AssistSyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
218
|
+
| ExistingDocumentRequest
|
|
219
|
+
| CreateDocumentRequest<T>
|
|
220
|
+
) &
|
|
221
|
+
AssistRequestBase &
|
|
222
|
+
Sync
|
|
223
|
+
|
|
224
|
+
declare interface Async {
|
|
225
|
+
/**
|
|
226
|
+
* When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
|
|
227
|
+
* The instruction operation will carry on in the background.
|
|
228
|
+
*
|
|
229
|
+
* When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
|
|
230
|
+
*
|
|
231
|
+
* async: true is incompatible with skipWrite, as async: true does not return the resulting document
|
|
232
|
+
*/
|
|
233
|
+
async: true
|
|
234
|
+
}
|
|
235
|
+
|
|
113
236
|
/** @internal */
|
|
114
237
|
export declare type AttributeSet = {
|
|
115
238
|
[key: string]: Any
|
|
@@ -336,14 +459,14 @@ export declare interface ClientConfig {
|
|
|
336
459
|
/**
|
|
337
460
|
* What perspective to use for the client. See {@link https://www.sanity.io/docs/perspectives|perspective documentation}
|
|
338
461
|
* @remarks
|
|
339
|
-
* As of API version `v2025-02-19`, the default perspective has changed from `raw` to `published`. {@link https://www.sanity.io/changelog/
|
|
462
|
+
* As of API version `v2025-02-19`, the default perspective has changed from `raw` to `published`. {@link https://www.sanity.io/changelog/676aaa9d-2da6-44fb-abe5-580f28047c10|Changelog}
|
|
340
463
|
* @defaultValue 'published'
|
|
341
464
|
*/
|
|
342
465
|
perspective?: ClientPerspective
|
|
343
466
|
apiHost?: string
|
|
344
467
|
/**
|
|
345
468
|
@remarks
|
|
346
|
-
* As of API version `v2025-02-19`, the default perspective has changed from `raw` to `published`. {@link https://www.sanity.io/changelog/
|
|
469
|
+
* As of API version `v2025-02-19`, the default perspective has changed from `raw` to `published`. {@link https://www.sanity.io/changelog/676aaa9d-2da6-44fb-abe5-580f28047c10|Changelog}
|
|
347
470
|
*/
|
|
348
471
|
apiVersion?: string
|
|
349
472
|
proxy?: string
|
|
@@ -455,6 +578,12 @@ export declare class ConnectionFailedError extends Error {
|
|
|
455
578
|
readonly name = 'ConnectionFailedError'
|
|
456
579
|
}
|
|
457
580
|
|
|
581
|
+
/** @beta */
|
|
582
|
+
export declare interface ConstantInstructionParam {
|
|
583
|
+
type: 'constant'
|
|
584
|
+
value: string
|
|
585
|
+
}
|
|
586
|
+
|
|
458
587
|
/** @public */
|
|
459
588
|
export declare interface ContentSourceMap {
|
|
460
589
|
mappings: ContentSourceMapMappings
|
|
@@ -673,7 +802,19 @@ export declare type CreateAction = {
|
|
|
673
802
|
*/
|
|
674
803
|
export declare const createClient: (config: ClientConfig_2) => SanityClient
|
|
675
804
|
|
|
676
|
-
/**
|
|
805
|
+
/**
|
|
806
|
+
* Instruction to create a new document
|
|
807
|
+
* @beta
|
|
808
|
+
*/
|
|
809
|
+
declare interface CreateDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
|
|
810
|
+
createDocument: {
|
|
811
|
+
/** if no _id is provided, one will be generated. _id is always returned when the requests succeed */
|
|
812
|
+
_id?: string
|
|
813
|
+
_type: string
|
|
814
|
+
} & SanityDocumentStub<T>
|
|
815
|
+
}
|
|
816
|
+
|
|
817
|
+
/** @public */
|
|
677
818
|
export declare interface CurrentSanityUser {
|
|
678
819
|
id: string
|
|
679
820
|
name: string
|
|
@@ -682,10 +823,10 @@ export declare interface CurrentSanityUser {
|
|
|
682
823
|
role: string
|
|
683
824
|
}
|
|
684
825
|
|
|
685
|
-
/** @
|
|
826
|
+
/** @public */
|
|
686
827
|
export declare type DatasetAclMode = 'public' | 'private' | 'custom'
|
|
687
828
|
|
|
688
|
-
/** @
|
|
829
|
+
/** @public */
|
|
689
830
|
export declare type DatasetResponse = {
|
|
690
831
|
datasetName: string
|
|
691
832
|
aclMode: DatasetAclMode
|
|
@@ -733,7 +874,7 @@ export declare class DatasetsClient {
|
|
|
733
874
|
list(): Promise<DatasetsResponse>
|
|
734
875
|
}
|
|
735
876
|
|
|
736
|
-
/** @
|
|
877
|
+
/** @public */
|
|
737
878
|
export declare type DatasetsResponse = {
|
|
738
879
|
name: string
|
|
739
880
|
aclMode: DatasetAclMode
|
|
@@ -817,6 +958,19 @@ export declare type DisconnectEvent = {
|
|
|
817
958
|
reason: string
|
|
818
959
|
}
|
|
819
960
|
|
|
961
|
+
/**
|
|
962
|
+
*
|
|
963
|
+
* Includes a LLM-friendly version of the document in the instruction
|
|
964
|
+
* @beta
|
|
965
|
+
* */
|
|
966
|
+
declare interface DocumentInstructionParam {
|
|
967
|
+
type: 'document'
|
|
968
|
+
/**
|
|
969
|
+
* If omitted, implicitly uses the documentId of the instruction target
|
|
970
|
+
*/
|
|
971
|
+
documentId?: string
|
|
972
|
+
}
|
|
973
|
+
|
|
820
974
|
/**
|
|
821
975
|
* Modifies an existing draft document.
|
|
822
976
|
* It applies the given patch to the document referenced by draftId.
|
|
@@ -878,6 +1032,31 @@ export declare type EventSourceEvent<Name extends string> = ServerSentEvent<Name
|
|
|
878
1032
|
*/
|
|
879
1033
|
export declare type EventSourceInstance = InstanceType<typeof globalThis.EventSource>
|
|
880
1034
|
|
|
1035
|
+
/**
|
|
1036
|
+
* Instruction for an existing document.
|
|
1037
|
+
* @beta
|
|
1038
|
+
*/
|
|
1039
|
+
declare interface ExistingDocumentRequest {
|
|
1040
|
+
documentId: string
|
|
1041
|
+
}
|
|
1042
|
+
|
|
1043
|
+
/**
|
|
1044
|
+
*
|
|
1045
|
+
* Includes a LLM-friendly version of the field value in the instruction
|
|
1046
|
+
* @beta
|
|
1047
|
+
* */
|
|
1048
|
+
export declare interface FieldInstructionParam {
|
|
1049
|
+
type: 'field'
|
|
1050
|
+
/**
|
|
1051
|
+
* Examples: 'title', 'array[_key=="key"].field
|
|
1052
|
+
*/
|
|
1053
|
+
path: string
|
|
1054
|
+
/**
|
|
1055
|
+
* If omitted, implicitly uses the documentId of the instruction target
|
|
1056
|
+
*/
|
|
1057
|
+
documentId?: string
|
|
1058
|
+
}
|
|
1059
|
+
|
|
881
1060
|
/** @public */
|
|
882
1061
|
export declare type FilterDefault = (props: {
|
|
883
1062
|
/**
|
|
@@ -993,6 +1172,13 @@ export declare type FirstDocumentMutationOptions = BaseMutationOptions & {
|
|
|
993
1172
|
returnDocuments?: true
|
|
994
1173
|
}
|
|
995
1174
|
|
|
1175
|
+
/** @beta */
|
|
1176
|
+
export declare interface GroqInstructionParam {
|
|
1177
|
+
type: 'groq'
|
|
1178
|
+
query: string
|
|
1179
|
+
params?: Record<string, string>
|
|
1180
|
+
}
|
|
1181
|
+
|
|
996
1182
|
/** @public */
|
|
997
1183
|
export declare type HttpRequest = {
|
|
998
1184
|
(options: RequestOptions, requester: Requester): ReturnType<Requester>
|
|
@@ -1063,6 +1249,17 @@ export declare type InsertPatch =
|
|
|
1063
1249
|
items: Any[]
|
|
1064
1250
|
}
|
|
1065
1251
|
|
|
1252
|
+
/** @beta */
|
|
1253
|
+
export declare type InstructionParam =
|
|
1254
|
+
| string
|
|
1255
|
+
| ConstantInstructionParam
|
|
1256
|
+
| FieldInstructionParam
|
|
1257
|
+
| DocumentInstructionParam
|
|
1258
|
+
| GroqInstructionParam
|
|
1259
|
+
|
|
1260
|
+
/** @beta */
|
|
1261
|
+
export declare type InstructionParams = Record<string, InstructionParam>
|
|
1262
|
+
|
|
1066
1263
|
/**
|
|
1067
1264
|
* Set up a listener that will be notified when mutations occur on documents matching the provided query/filter.
|
|
1068
1265
|
*
|
|
@@ -1134,7 +1331,7 @@ export declare interface ListenOptions {
|
|
|
1134
1331
|
includePreviousRevision?: boolean
|
|
1135
1332
|
/**
|
|
1136
1333
|
* Whether to include events for drafts and versions. As of API Version >= v2025-02-19, only events
|
|
1137
|
-
* for published documents will be included by default (see {@link https://www.sanity.io/changelog/
|
|
1334
|
+
* for published documents will be included by default (see {@link https://www.sanity.io/changelog/676aaa9d-2da6-44fb-abe5-580f28047c10|Changelog})
|
|
1138
1335
|
* If you need events from drafts and versions, set this to `true`.
|
|
1139
1336
|
* Note: Keep in mind that additional document variants may be introduced in the future, so it's
|
|
1140
1337
|
* recommended to respond to events in a way that's tolerant of potential future variants, e.g. by
|
|
@@ -1484,6 +1681,18 @@ export declare class ObservableAssetsClient {
|
|
|
1484
1681
|
>
|
|
1485
1682
|
}
|
|
1486
1683
|
|
|
1684
|
+
/** @public */
|
|
1685
|
+
declare class ObservableAssistClient {
|
|
1686
|
+
#private
|
|
1687
|
+
constructor(client: ObservableSanityClient, httpRequest: HttpRequest)
|
|
1688
|
+
instruct(request: AssistAsyncInstruction): Observable<{
|
|
1689
|
+
_id: string
|
|
1690
|
+
}>
|
|
1691
|
+
instruct<DocumentShape extends Record<string, Any>>(
|
|
1692
|
+
request: AssistSyncInstruction<DocumentShape>,
|
|
1693
|
+
): Observable<IdentifiedSanityDocumentStub & DocumentShape>
|
|
1694
|
+
}
|
|
1695
|
+
|
|
1487
1696
|
/** @internal */
|
|
1488
1697
|
export declare class ObservableDatasetsClient {
|
|
1489
1698
|
#private
|
|
@@ -1607,6 +1816,7 @@ export declare class ObservableSanityClient {
|
|
|
1607
1816
|
live: LiveClient
|
|
1608
1817
|
projects: ObservableProjectsClient
|
|
1609
1818
|
users: ObservableUsersClient
|
|
1819
|
+
assist: ObservableAssistClient
|
|
1610
1820
|
/**
|
|
1611
1821
|
* Instance properties
|
|
1612
1822
|
*/
|
|
@@ -2538,6 +2748,7 @@ export declare class SanityClient {
|
|
|
2538
2748
|
live: LiveClient
|
|
2539
2749
|
projects: ProjectsClient
|
|
2540
2750
|
users: UsersClient
|
|
2751
|
+
assist: AssistClient
|
|
2541
2752
|
/**
|
|
2542
2753
|
* Observable version of the Sanity client, with the same configuration as the promise-based one
|
|
2543
2754
|
*/
|
|
@@ -3119,7 +3330,7 @@ export declare interface SanityImagePalette {
|
|
|
3119
3330
|
title: string
|
|
3120
3331
|
}
|
|
3121
3332
|
|
|
3122
|
-
/** @
|
|
3333
|
+
/** @public */
|
|
3123
3334
|
export declare interface SanityProject {
|
|
3124
3335
|
id: string
|
|
3125
3336
|
displayName: string
|
|
@@ -3147,7 +3358,7 @@ export declare interface SanityProject {
|
|
|
3147
3358
|
}
|
|
3148
3359
|
}
|
|
3149
3360
|
|
|
3150
|
-
/** @
|
|
3361
|
+
/** @public */
|
|
3151
3362
|
export declare interface SanityProjectMember {
|
|
3152
3363
|
id: string
|
|
3153
3364
|
role: string
|
|
@@ -3169,7 +3380,7 @@ export declare interface SanityReference {
|
|
|
3169
3380
|
*/
|
|
3170
3381
|
export declare class SanityStegaClient extends SanityClient {}
|
|
3171
3382
|
|
|
3172
|
-
/** @
|
|
3383
|
+
/** @public */
|
|
3173
3384
|
export declare interface SanityUser {
|
|
3174
3385
|
id: string
|
|
3175
3386
|
projectId: string
|
|
@@ -3321,6 +3532,29 @@ export declare type StudioBaseUrl =
|
|
|
3321
3532
|
/** @alpha */
|
|
3322
3533
|
export declare type StudioUrl = StudioBaseUrl | StudioBaseRoute
|
|
3323
3534
|
|
|
3535
|
+
declare interface Sync {
|
|
3536
|
+
/**
|
|
3537
|
+
* By default, skipWrite: false.
|
|
3538
|
+
* Write enabled operations will mutate the target document, and emit AI presence in the studio.
|
|
3539
|
+
*
|
|
3540
|
+
* When skipWrite: true, the api will not mutate any documents nor emit presence.
|
|
3541
|
+
* Ie, when true, no changes will be made to content-lake
|
|
3542
|
+
*
|
|
3543
|
+
* skipWrite: true is incompatible with async: true,
|
|
3544
|
+
* as skipWrite implies that you will use the return value of the operation
|
|
3545
|
+
*/
|
|
3546
|
+
skipWrite?: boolean
|
|
3547
|
+
/**
|
|
3548
|
+
* When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
|
|
3549
|
+
* The instruction operation will carry on in the background.
|
|
3550
|
+
*
|
|
3551
|
+
* When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
|
|
3552
|
+
*
|
|
3553
|
+
* async: true is incompatible with skipWrite: true, as async: true does not return the resulting document
|
|
3554
|
+
*/
|
|
3555
|
+
async?: false
|
|
3556
|
+
}
|
|
3557
|
+
|
|
3324
3558
|
/** @public */
|
|
3325
3559
|
export declare type SyncTag = `s1:${string}`
|
|
3326
3560
|
|
package/package.json
CHANGED
package/src/SanityClient.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import {lastValueFrom, Observable} from 'rxjs'
|
|
2
2
|
|
|
3
3
|
import {AssetsClient, ObservableAssetsClient} from './assets/AssetsClient'
|
|
4
|
+
import {AssistClient, ObservableAssistClient} from './assist/AssistClient'
|
|
4
5
|
import {defaultConfig, initConfig} from './config'
|
|
5
6
|
import * as dataMethods from './data/dataMethods'
|
|
6
7
|
import {_listen} from './data/listen'
|
|
@@ -65,7 +66,7 @@ export class ObservableSanityClient {
|
|
|
65
66
|
live: LiveClient
|
|
66
67
|
projects: ObservableProjectsClient
|
|
67
68
|
users: ObservableUsersClient
|
|
68
|
-
|
|
69
|
+
assist: ObservableAssistClient
|
|
69
70
|
/**
|
|
70
71
|
* Private properties
|
|
71
72
|
*/
|
|
@@ -87,6 +88,7 @@ export class ObservableSanityClient {
|
|
|
87
88
|
this.live = new LiveClient(this)
|
|
88
89
|
this.projects = new ObservableProjectsClient(this, this.#httpRequest)
|
|
89
90
|
this.users = new ObservableUsersClient(this, this.#httpRequest)
|
|
91
|
+
this.assist = new ObservableAssistClient(this, this.#httpRequest)
|
|
90
92
|
}
|
|
91
93
|
|
|
92
94
|
/**
|
|
@@ -733,6 +735,7 @@ export class SanityClient {
|
|
|
733
735
|
live: LiveClient
|
|
734
736
|
projects: ProjectsClient
|
|
735
737
|
users: UsersClient
|
|
738
|
+
assist: AssistClient
|
|
736
739
|
|
|
737
740
|
/**
|
|
738
741
|
* Observable version of the Sanity client, with the same configuration as the promise-based one
|
|
@@ -760,6 +763,7 @@ export class SanityClient {
|
|
|
760
763
|
this.live = new LiveClient(this)
|
|
761
764
|
this.projects = new ProjectsClient(this, this.#httpRequest)
|
|
762
765
|
this.users = new UsersClient(this, this.#httpRequest)
|
|
766
|
+
this.assist = new AssistClient(this, this.#httpRequest)
|
|
763
767
|
|
|
764
768
|
this.observable = new ObservableSanityClient(httpRequest, config)
|
|
765
769
|
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {lastValueFrom, type Observable} from 'rxjs'
|
|
2
|
+
|
|
3
|
+
import {_request} from '../data/dataMethods'
|
|
4
|
+
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
5
|
+
import type {
|
|
6
|
+
Any,
|
|
7
|
+
AssistAsyncInstruction,
|
|
8
|
+
AssistInstruction,
|
|
9
|
+
AssistSyncInstruction,
|
|
10
|
+
HttpRequest,
|
|
11
|
+
IdentifiedSanityDocumentStub,
|
|
12
|
+
} from '../types'
|
|
13
|
+
import {hasDataset} from '../validators'
|
|
14
|
+
|
|
15
|
+
function _instruct<
|
|
16
|
+
DocumentShape extends Record<string, Any>,
|
|
17
|
+
Req extends AssistInstruction<DocumentShape>,
|
|
18
|
+
>(
|
|
19
|
+
client: SanityClient | ObservableSanityClient,
|
|
20
|
+
httpRequest: HttpRequest,
|
|
21
|
+
request: Req,
|
|
22
|
+
): Observable<
|
|
23
|
+
Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
|
|
24
|
+
> {
|
|
25
|
+
const dataset = hasDataset(client.config())
|
|
26
|
+
return _request(client, httpRequest, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
uri: `/assist/tasks/instruct/${dataset}`,
|
|
29
|
+
body: request,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @public */
|
|
34
|
+
export class ObservableAssistClient {
|
|
35
|
+
#client: ObservableSanityClient
|
|
36
|
+
#httpRequest: HttpRequest
|
|
37
|
+
constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
|
|
38
|
+
this.#client = client
|
|
39
|
+
this.#httpRequest = httpRequest
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
instruct(request: AssistAsyncInstruction): Observable<{_id: string}>
|
|
43
|
+
|
|
44
|
+
instruct<DocumentShape extends Record<string, Any>>(
|
|
45
|
+
request: AssistSyncInstruction<DocumentShape>,
|
|
46
|
+
): Observable<IdentifiedSanityDocumentStub & DocumentShape>
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Run an ad-hoc instruction for a target document.
|
|
50
|
+
* @param request instruction request
|
|
51
|
+
*/
|
|
52
|
+
instruct<DocumentShape extends Record<string, Any>, Req extends AssistInstruction<DocumentShape>>(
|
|
53
|
+
request: Req,
|
|
54
|
+
): Observable<
|
|
55
|
+
Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
|
|
56
|
+
> {
|
|
57
|
+
return _instruct(this.#client, this.#httpRequest, request)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @public */
|
|
62
|
+
export class AssistClient {
|
|
63
|
+
#client: SanityClient
|
|
64
|
+
#httpRequest: HttpRequest
|
|
65
|
+
constructor(client: SanityClient, httpRequest: HttpRequest) {
|
|
66
|
+
this.#client = client
|
|
67
|
+
this.#httpRequest = httpRequest
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
instruct(request: AssistAsyncInstruction): Promise<{_id: string}>
|
|
71
|
+
|
|
72
|
+
instruct<DocumentShape extends Record<string, Any>>(
|
|
73
|
+
request: AssistSyncInstruction<DocumentShape>,
|
|
74
|
+
): Promise<IdentifiedSanityDocumentStub & DocumentShape>
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Run an ad-hoc instruction for a target document.
|
|
78
|
+
* @param request instruction request
|
|
79
|
+
*/
|
|
80
|
+
instruct<DocumentShape extends Record<string, Any>, Req extends AssistInstruction<DocumentShape>>(
|
|
81
|
+
request: Req,
|
|
82
|
+
): Promise<
|
|
83
|
+
Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
|
|
84
|
+
> {
|
|
85
|
+
return lastValueFrom(_instruct(this.#client, this.#httpRequest, request))
|
|
86
|
+
}
|
|
87
|
+
}
|