@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.
@@ -57,6 +57,12 @@ export declare type AllDocumentsMutationOptions = BaseMutationOptions & {
57
57
  */
58
58
  export declare type Any = any
59
59
 
60
+ /**
61
+ * Used to tag types that is set to `any` as a temporary measure, but should be replaced with proper typings in the future
62
+ * @internal
63
+ */
64
+ declare type Any_2 = any
65
+
60
66
  /** @internal */
61
67
  export declare interface ApiError {
62
68
  error: string
@@ -116,6 +122,122 @@ export declare class AssetsClient {
116
122
  ): Promise<SanityAssetDocument | SanityImageAssetDocument>
117
123
  }
118
124
 
125
+ /** @beta */
126
+ export declare type AssistAsyncInstruction<
127
+ T extends Record<string, Any_2> = Record<string, Any_2>,
128
+ > = (ExistingDocumentRequest | CreateDocumentRequest<T>) & AssistRequestBase & Async
129
+
130
+ /** @public */
131
+ declare class AssistClient {
132
+ #private
133
+ constructor(client: SanityClient, httpRequest: HttpRequest)
134
+ instruct(request: AssistAsyncInstruction): Promise<{
135
+ _id: string
136
+ }>
137
+ instruct<DocumentShape extends Record<string, Any>>(
138
+ request: AssistSyncInstruction<DocumentShape>,
139
+ ): Promise<IdentifiedSanityDocumentStub & DocumentShape>
140
+ }
141
+
142
+ /** @beta */
143
+ export declare type AssistInstruction<T extends Record<string, Any_2> = Record<string, Any_2>> =
144
+ | AssistSyncInstruction<T>
145
+ | AssistAsyncInstruction<T>
146
+
147
+ declare interface AssistRequestBase {
148
+ /** schemaId as reported by sanity deploy / sanity schema store */
149
+ schemaId: string
150
+ /** string template using $variable – more on this below under "Dynamic instruction" */
151
+ instruction: string
152
+ /** param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable" */
153
+ instructionParams?: InstructionParams
154
+ /**
155
+ * Optional document path output target for the instruction.
156
+ * When provided, the instruction will apply to this path in the document and its children.
157
+ *
158
+ * ## Examples
159
+ * - `path: 'title'` will output to the title field in the document
160
+ * - `path: 'array[_key="xx"]'` will output to the item with `_key: 'xx'` in the array field
161
+ */
162
+ path?: string
163
+ /**
164
+ * Controls sub-paths in the document that can be output to.
165
+ *
166
+ * The string-paths are relative to the `path` param
167
+ *
168
+ * Note: these path strings are less strictly validated than the `path` param itself:
169
+ * if an relative-path does not exist or is invalid, it will be silently ignored.
170
+ *
171
+ * @see AssistRequestBase#conditionalPaths
172
+ * @see AssistRequestBase#outputTypes
173
+ */
174
+ relativeOutputPaths?:
175
+ | {
176
+ include: string[]
177
+ }
178
+ | {
179
+ exclude: string[]
180
+ }
181
+ /**
182
+ * Controls which types the instruction is allowed to output to.
183
+ *
184
+ * @see AssistRequestBase#relativeOutputPaths
185
+ * @see AssistRequestBase#conditionalPaths
186
+ */
187
+ outputTypes?:
188
+ | {
189
+ include: string[]
190
+ }
191
+ | {
192
+ exclude: string[]
193
+ }
194
+ /**
195
+ * When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
196
+ *
197
+ * By default, AI Assist will not output to conditional `readOnly` and `hidden` fields,
198
+ * ie, they are considered to resolve to `readOnly: true` / `hidden: true`.
199
+ *
200
+ * `conditionalPaths` param allows setting the default conditional value for
201
+ * `hidden` and `readOnly` to false,
202
+ * or individually set `hidden` and `readOnly` state for individual document paths.
203
+ *
204
+ *
205
+ * Note: fields and types with explicit readOnly: true or hidden: true in the schema, are not available to AI Assist,
206
+ * and cannot be changed via conditionalPaths.
207
+ *
208
+ * conditionalPaths state only apply to fields and types that have conditional `hidden` or `readOnly` in their schema definition.
209
+ *
210
+ * @see AssistRequestBase#relativeOutputPaths
211
+ * @see AssistRequestBase#outputTypes
212
+ */
213
+ conditionalPaths?: {
214
+ defaultReadOnly?: boolean
215
+ defaultHidden?: boolean
216
+ paths?: {
217
+ /** path here is not a relative path: it must be the full document path, regardless of `path` param on the request itself */
218
+ path: string
219
+ readOnly: boolean
220
+ hidden: boolean
221
+ }[]
222
+ }
223
+ }
224
+
225
+ /** @beta */
226
+ export declare type AssistSyncInstruction<T extends Record<string, Any_2> = Record<string, Any_2>> =
227
+ (ExistingDocumentRequest | CreateDocumentRequest<T>) & AssistRequestBase & Sync
228
+
229
+ declare interface Async {
230
+ /**
231
+ * When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
232
+ * The instruction operation will carry on in the background.
233
+ *
234
+ * When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
235
+ *
236
+ * async: true is incompatible with skipWrite, as async: true does not return the resulting document
237
+ */
238
+ async: true
239
+ }
240
+
119
241
  /** @internal */
120
242
  export declare type AttributeSet = {
121
243
  [key: string]: Any
@@ -342,14 +464,14 @@ export declare interface ClientConfig {
342
464
  /**
343
465
  * What perspective to use for the client. See {@link https://www.sanity.io/docs/perspectives|perspective documentation}
344
466
  * @remarks
345
- * As of API version `v2025-02-19`, the default perspective has changed from `raw` to `published`. {@link https://www.sanity.io/changelog/e93a2d5a-9cee-4801-829e-8d3394bfed85|Changelog}
467
+ * 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}
346
468
  * @defaultValue 'published'
347
469
  */
348
470
  perspective?: ClientPerspective
349
471
  apiHost?: string
350
472
  /**
351
473
  @remarks
352
- * As of API version `v2025-02-19`, the default perspective has changed from `raw` to `published`. {@link https://www.sanity.io/changelog/e93a2d5a-9cee-4801-829e-8d3394bfed85|Changelog}
474
+ * 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}
353
475
  */
354
476
  apiVersion?: string
355
477
  proxy?: string
@@ -455,6 +577,12 @@ export declare class ConnectionFailedError extends Error {
455
577
  readonly name = 'ConnectionFailedError'
456
578
  }
457
579
 
580
+ /** @beta */
581
+ export declare interface ConstantInstructionParam {
582
+ type: 'constant'
583
+ value: string
584
+ }
585
+
458
586
  /** @public */
459
587
  export declare interface ContentSourceMap {
460
588
  mappings: ContentSourceMapMappings
@@ -574,7 +702,19 @@ export declare type CreateAction = {
574
702
  /** @public */
575
703
  export declare const createClient: (config: ClientConfig) => SanityClient
576
704
 
577
- /** @internal */
705
+ /**
706
+ * Instruction to create a new document
707
+ * @beta
708
+ */
709
+ declare interface CreateDocumentRequest<T extends Record<string, Any_2> = Record<string, Any_2>> {
710
+ createDocument: {
711
+ /** if no _id is provided, one will be generated. _id is always returned when the requests succeed */
712
+ _id?: string
713
+ _type: string
714
+ } & SanityDocumentStub_2<T>
715
+ }
716
+
717
+ /** @public */
578
718
  export declare interface CurrentSanityUser {
579
719
  id: string
580
720
  name: string
@@ -583,10 +723,10 @@ export declare interface CurrentSanityUser {
583
723
  role: string
584
724
  }
585
725
 
586
- /** @internal */
726
+ /** @public */
587
727
  export declare type DatasetAclMode = 'public' | 'private' | 'custom'
588
728
 
589
- /** @internal */
729
+ /** @public */
590
730
  export declare type DatasetResponse = {
591
731
  datasetName: string
592
732
  aclMode: DatasetAclMode
@@ -634,7 +774,7 @@ export declare class DatasetsClient {
634
774
  list(): Promise<DatasetsResponse>
635
775
  }
636
776
 
637
- /** @internal */
777
+ /** @public */
638
778
  export declare type DatasetsResponse = {
639
779
  name: string
640
780
  aclMode: DatasetAclMode
@@ -725,6 +865,19 @@ export declare type DisconnectEvent = {
725
865
  reason: string
726
866
  }
727
867
 
868
+ /**
869
+ *
870
+ * Includes a LLM-friendly version of the document in the instruction
871
+ * @beta
872
+ * */
873
+ declare interface DocumentInstructionParam {
874
+ type: 'document'
875
+ /**
876
+ * If omitted, implicitly uses the documentId of the instruction target
877
+ */
878
+ documentId?: string
879
+ }
880
+
728
881
  /**
729
882
  * Modifies an existing draft document.
730
883
  * It applies the given patch to the document referenced by draftId.
@@ -767,6 +920,31 @@ export declare type EventSourceEvent<Name extends string> = ServerSentEvent<Name
767
920
  */
768
921
  export declare type EventSourceInstance = InstanceType<typeof globalThis.EventSource>
769
922
 
923
+ /**
924
+ * Instruction for an existing document.
925
+ * @beta
926
+ */
927
+ declare interface ExistingDocumentRequest {
928
+ documentId: string
929
+ }
930
+
931
+ /**
932
+ *
933
+ * Includes a LLM-friendly version of the field value in the instruction
934
+ * @beta
935
+ * */
936
+ export declare interface FieldInstructionParam {
937
+ type: 'field'
938
+ /**
939
+ * Examples: 'title', 'array[_key=="key"].field
940
+ */
941
+ path: string
942
+ /**
943
+ * If omitted, implicitly uses the documentId of the instruction target
944
+ */
945
+ documentId?: string
946
+ }
947
+
770
948
  /** @public */
771
949
  export declare type FilterDefault = (props: {
772
950
  /**
@@ -833,6 +1011,13 @@ export declare type FirstDocumentMutationOptions = BaseMutationOptions & {
833
1011
  returnDocuments?: true
834
1012
  }
835
1013
 
1014
+ /** @beta */
1015
+ export declare interface GroqInstructionParam {
1016
+ type: 'groq'
1017
+ query: string
1018
+ params?: Record<string, string>
1019
+ }
1020
+
836
1021
  /** @public */
837
1022
  export declare type HttpRequest = {
838
1023
  (options: RequestOptions, requester: Requester): ReturnType<Requester>
@@ -893,6 +1078,17 @@ export declare type InsertPatch =
893
1078
  items: Any[]
894
1079
  }
895
1080
 
1081
+ /** @beta */
1082
+ export declare type InstructionParam =
1083
+ | string
1084
+ | ConstantInstructionParam
1085
+ | FieldInstructionParam
1086
+ | DocumentInstructionParam
1087
+ | GroqInstructionParam
1088
+
1089
+ /** @beta */
1090
+ export declare type InstructionParams = Record<string, InstructionParam>
1091
+
896
1092
  /**
897
1093
  * Set up a listener that will be notified when mutations occur on documents matching the provided query/filter.
898
1094
  *
@@ -964,7 +1160,7 @@ export declare interface ListenOptions {
964
1160
  includePreviousRevision?: boolean
965
1161
  /**
966
1162
  * Whether to include events for drafts and versions. As of API Version >= v2025-02-19, only events
967
- * for published documents will be included by default (see {@link https://www.sanity.io/changelog/e93a2d5a-9cee-4801-829e-8d3394bfed85|Changelog})
1163
+ * for published documents will be included by default (see {@link https://www.sanity.io/changelog/676aaa9d-2da6-44fb-abe5-580f28047c10|Changelog})
968
1164
  * If you need events from drafts and versions, set this to `true`.
969
1165
  * Note: Keep in mind that additional document variants may be introduced in the future, so it's
970
1166
  * recommended to respond to events in a way that's tolerant of potential future variants, e.g. by
@@ -1307,6 +1503,18 @@ export declare class ObservableAssetsClient {
1307
1503
  >
1308
1504
  }
1309
1505
 
1506
+ /** @public */
1507
+ declare class ObservableAssistClient {
1508
+ #private
1509
+ constructor(client: ObservableSanityClient, httpRequest: HttpRequest)
1510
+ instruct(request: AssistAsyncInstruction): Observable<{
1511
+ _id: string
1512
+ }>
1513
+ instruct<DocumentShape extends Record<string, Any>>(
1514
+ request: AssistSyncInstruction<DocumentShape>,
1515
+ ): Observable<IdentifiedSanityDocumentStub & DocumentShape>
1516
+ }
1517
+
1310
1518
  /** @internal */
1311
1519
  export declare class ObservableDatasetsClient {
1312
1520
  #private
@@ -1430,6 +1638,7 @@ export declare class ObservableSanityClient {
1430
1638
  live: LiveClient
1431
1639
  projects: ObservableProjectsClient
1432
1640
  users: ObservableUsersClient
1641
+ assist: ObservableAssistClient
1433
1642
  /**
1434
1643
  * Instance properties
1435
1644
  */
@@ -2349,6 +2558,7 @@ export declare class SanityClient {
2349
2558
  live: LiveClient
2350
2559
  projects: ProjectsClient
2351
2560
  users: UsersClient
2561
+ assist: AssistClient
2352
2562
  /**
2353
2563
  * Observable version of the Sanity client, with the same configuration as the promise-based one
2354
2564
  */
@@ -2887,6 +3097,13 @@ export declare type SanityDocumentStub<T extends Record<string, Any> = Record<st
2887
3097
  _type: string
2888
3098
  }
2889
3099
 
3100
+ /** @public */
3101
+ declare type SanityDocumentStub_2<T extends Record<string, Any_2> = Record<string, Any_2>> = {
3102
+ [P in keyof T]: T[P]
3103
+ } & {
3104
+ _type: string
3105
+ }
3106
+
2890
3107
  /** @internal */
2891
3108
  export declare interface SanityImageAssetDocument extends SanityAssetDocument {
2892
3109
  metadata: {
@@ -2930,7 +3147,7 @@ export declare interface SanityImagePalette {
2930
3147
  title: string
2931
3148
  }
2932
3149
 
2933
- /** @internal */
3150
+ /** @public */
2934
3151
  export declare interface SanityProject {
2935
3152
  id: string
2936
3153
  displayName: string
@@ -2958,7 +3175,7 @@ export declare interface SanityProject {
2958
3175
  }
2959
3176
  }
2960
3177
 
2961
- /** @internal */
3178
+ /** @public */
2962
3179
  export declare interface SanityProjectMember {
2963
3180
  id: string
2964
3181
  role: string
@@ -2974,7 +3191,7 @@ export declare interface SanityReference {
2974
3191
  _ref: string
2975
3192
  }
2976
3193
 
2977
- /** @internal */
3194
+ /** @public */
2978
3195
  export declare interface SanityUser {
2979
3196
  id: string
2980
3197
  projectId: string
@@ -3063,6 +3280,29 @@ export {StudioBaseUrl}
3063
3280
 
3064
3281
  export {StudioUrl}
3065
3282
 
3283
+ declare interface Sync {
3284
+ /**
3285
+ * By default, skipWrite: false.
3286
+ * Write enabled operations will mutate the target document, and emit AI presence in the studio.
3287
+ *
3288
+ * When skipWrite: true, the api will not mutate any documents nor emit presence.
3289
+ * Ie, when true, no changes will be made to content-lake
3290
+ *
3291
+ * skipWrite: true is incompatible with async: true,
3292
+ * as skipWrite implies that you will use the return value of the operation
3293
+ */
3294
+ skipWrite?: boolean
3295
+ /**
3296
+ * When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
3297
+ * The instruction operation will carry on in the background.
3298
+ *
3299
+ * When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
3300
+ *
3301
+ * async: true is incompatible with skipWrite: true, as async: true does not return the resulting document
3302
+ */
3303
+ async?: false
3304
+ }
3305
+
3066
3306
  /** @public */
3067
3307
  export declare type SyncTag = `s1:${string}`
3068
3308
 
@@ -981,6 +981,42 @@ function optionsFromFile(opts, file) {
981
981
  opts
982
982
  );
983
983
  }
984
+ function _instruct(client, httpRequest, request) {
985
+ const dataset2 = hasDataset(client.config());
986
+ return _request(client, httpRequest, {
987
+ method: "POST",
988
+ uri: `/assist/tasks/instruct/${dataset2}`,
989
+ body: request
990
+ });
991
+ }
992
+ class ObservableAssistClient {
993
+ #client;
994
+ #httpRequest;
995
+ constructor(client, httpRequest) {
996
+ this.#client = client, this.#httpRequest = httpRequest;
997
+ }
998
+ /**
999
+ * Run an ad-hoc instruction for a target document.
1000
+ * @param request instruction request
1001
+ */
1002
+ instruct(request) {
1003
+ return _instruct(this.#client, this.#httpRequest, request);
1004
+ }
1005
+ }
1006
+ class AssistClient {
1007
+ #client;
1008
+ #httpRequest;
1009
+ constructor(client, httpRequest) {
1010
+ this.#client = client, this.#httpRequest = httpRequest;
1011
+ }
1012
+ /**
1013
+ * Run an ad-hoc instruction for a target document.
1014
+ * @param request instruction request
1015
+ */
1016
+ instruct(request) {
1017
+ return lastValueFrom(_instruct(this.#client, this.#httpRequest, request));
1018
+ }
1019
+ }
984
1020
  var defaults = (obj, defaults2) => Object.keys(defaults2).concat(Object.keys(obj)).reduce((target, prop) => (target[prop] = typeof obj[prop] > "u" ? defaults2[prop] : obj[prop], target), {});
985
1021
  const pick = (obj, props) => props.reduce((selection, prop) => (typeof obj[prop] > "u" || (selection[prop] = obj[prop]), selection), {}), eventSourcePolyfill = defer(() => import("@sanity/eventsource")).pipe(
986
1022
  map(({ default: EventSource2 }) => EventSource2),
@@ -1042,7 +1078,10 @@ function _shareReplayLatest(config) {
1042
1078
  }),
1043
1079
  share(shareConfig)
1044
1080
  ), emitLatest = new Observable((subscriber) => {
1045
- emitted && subscriber.next(latest), subscriber.complete();
1081
+ emitted && subscriber.next(
1082
+ // this cast is safe because of the emitted check which asserts that we got T from the source
1083
+ latest
1084
+ ), subscriber.complete();
1046
1085
  });
1047
1086
  return merge(wrapped, emitLatest);
1048
1087
  };
@@ -1313,6 +1352,7 @@ class ObservableSanityClient {
1313
1352
  live;
1314
1353
  projects;
1315
1354
  users;
1355
+ assist;
1316
1356
  /**
1317
1357
  * Private properties
1318
1358
  */
@@ -1323,7 +1363,7 @@ class ObservableSanityClient {
1323
1363
  */
1324
1364
  listen = _listen;
1325
1365
  constructor(httpRequest, config = defaultConfig) {
1326
- this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest);
1366
+ this.config(config), this.#httpRequest = httpRequest, this.assets = new ObservableAssetsClient(this, this.#httpRequest), this.datasets = new ObservableDatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ObservableProjectsClient(this, this.#httpRequest), this.users = new ObservableUsersClient(this, this.#httpRequest), this.assist = new ObservableAssistClient(this, this.#httpRequest);
1327
1367
  }
1328
1368
  /**
1329
1369
  * Clone the client - returns a new instance
@@ -1462,6 +1502,7 @@ class SanityClient {
1462
1502
  live;
1463
1503
  projects;
1464
1504
  users;
1505
+ assist;
1465
1506
  /**
1466
1507
  * Observable version of the Sanity client, with the same configuration as the promise-based one
1467
1508
  */
@@ -1476,7 +1517,7 @@ class SanityClient {
1476
1517
  */
1477
1518
  listen = _listen;
1478
1519
  constructor(httpRequest, config = defaultConfig) {
1479
- this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
1520
+ this.config(config), this.#httpRequest = httpRequest, this.assets = new AssetsClient(this, this.#httpRequest), this.datasets = new DatasetsClient(this, this.#httpRequest), this.live = new LiveClient(this), this.projects = new ProjectsClient(this, this.#httpRequest), this.users = new UsersClient(this, this.#httpRequest), this.assist = new AssistClient(this, this.#httpRequest), this.observable = new ObservableSanityClient(httpRequest, config);
1480
1521
  }
1481
1522
  /**
1482
1523
  * Clone the client - returns a new instance