@sanity/client 7.0.1-canary.2 → 7.1.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,111 @@
1
+ import {lastValueFrom, type Observable} from 'rxjs'
2
+
3
+ import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
4
+ import type {Any, HttpRequest, IdentifiedSanityDocumentStub, TranslateDocument} from '../../types'
5
+ import {_generate, type GenerateInstruction} from './generate'
6
+ import {_transform, type TransformDocument} from './transform'
7
+ import {_translate} from './translate'
8
+
9
+ /** @public */
10
+ export class ObservableAgentsActionClient {
11
+ #client: ObservableSanityClient
12
+ #httpRequest: HttpRequest
13
+ constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
14
+ this.#client = client
15
+ this.#httpRequest = httpRequest
16
+ }
17
+
18
+ /**
19
+ * Run an instruction to generate content in a target document.
20
+ * @param request - instruction request
21
+ */
22
+ generate<DocumentShape extends Record<string, Any>>(
23
+ request: GenerateInstruction<DocumentShape>,
24
+ ): Observable<
25
+ (typeof request)['async'] extends true
26
+ ? {_id: string}
27
+ : IdentifiedSanityDocumentStub & DocumentShape
28
+ > {
29
+ return _generate(this.#client, this.#httpRequest, request)
30
+ }
31
+
32
+ /**
33
+ * Transform a target document based on a source.
34
+ * @param request - translation request
35
+ */
36
+ transform<DocumentShape extends Record<string, Any>>(
37
+ request: TransformDocument<DocumentShape>,
38
+ ): Observable<
39
+ (typeof request)['async'] extends true
40
+ ? {_id: string}
41
+ : IdentifiedSanityDocumentStub & DocumentShape
42
+ > {
43
+ return _transform(this.#client, this.#httpRequest, request)
44
+ }
45
+
46
+ /**
47
+ * Translate a target document based on a source.
48
+ * @param request - translation request
49
+ */
50
+ translate<DocumentShape extends Record<string, Any>>(
51
+ request: TranslateDocument<DocumentShape>,
52
+ ): Observable<
53
+ (typeof request)['async'] extends true
54
+ ? {_id: string}
55
+ : IdentifiedSanityDocumentStub & DocumentShape
56
+ > {
57
+ return _translate(this.#client, this.#httpRequest, request)
58
+ }
59
+ }
60
+
61
+ /** @public */
62
+ export class AgentActionsClient {
63
+ #client: SanityClient
64
+ #httpRequest: HttpRequest
65
+ constructor(client: SanityClient, httpRequest: HttpRequest) {
66
+ this.#client = client
67
+ this.#httpRequest = httpRequest
68
+ }
69
+
70
+ /**
71
+ * Run an instruction to generate content in a target document.
72
+ * @param request - instruction request
73
+ */
74
+ generate<DocumentShape extends Record<string, Any>>(
75
+ request: GenerateInstruction<DocumentShape>,
76
+ ): Promise<
77
+ (typeof request)['async'] extends true
78
+ ? {_id: string}
79
+ : IdentifiedSanityDocumentStub & DocumentShape
80
+ > {
81
+ return lastValueFrom(_generate(this.#client, this.#httpRequest, request))
82
+ }
83
+
84
+ /**
85
+ * Transform a target document based on a source.
86
+ * @param request - translation request
87
+ */
88
+ transform<DocumentShape extends Record<string, Any>>(
89
+ request: TransformDocument<DocumentShape>,
90
+ ): Promise<
91
+ (typeof request)['async'] extends true
92
+ ? {_id: string}
93
+ : IdentifiedSanityDocumentStub & DocumentShape
94
+ > {
95
+ return lastValueFrom(_transform(this.#client, this.#httpRequest, request))
96
+ }
97
+
98
+ /**
99
+ * Translate a target document based on a source.
100
+ * @param request - translation request
101
+ */
102
+ translate<DocumentShape extends Record<string, Any>>(
103
+ request: TranslateDocument<DocumentShape>,
104
+ ): Promise<
105
+ (typeof request)['async'] extends true
106
+ ? {_id: string}
107
+ : IdentifiedSanityDocumentStub & DocumentShape
108
+ > {
109
+ return lastValueFrom(_translate(this.#client, this.#httpRequest, request))
110
+ }
111
+ }
@@ -0,0 +1,336 @@
1
+ /**
2
+ * Include a string in the instruction: do not have to escape $ signs in the string.
3
+ *
4
+ * ```ts
5
+ * client.agent.action.generate({
6
+ * schemaId,
7
+ * documentId,
8
+ * instruction: 'Give the following topic:\n $topic \n ---\nGenerate the full article.',
9
+ * instructionParams: {
10
+ * topic: {
11
+ * type: 'constant',
12
+ * value: 'Grapefruit'
13
+ * },
14
+ * },
15
+ * })
16
+ * ```
17
+ *
18
+ * `type: 'constant'` can also be provided directly as a string, as a shorthand:
19
+ *
20
+ * ```ts
21
+ * client.agent.action.generate({
22
+ * schemaId,
23
+ * documentId,
24
+ * instruction: 'Give the following topic:\n $topic \n ---\nGenerate the full article.',
25
+ * instructionParams: {
26
+ * topic: 'Grapefruit'
27
+ * },
28
+ * })
29
+ * ```
30
+ *
31
+ * @beta
32
+ * */
33
+ export interface ConstantAgentActionParam {
34
+ type: 'constant'
35
+ value: string
36
+ }
37
+
38
+ /**
39
+ *
40
+ *
41
+ * Includes a LLM-friendly version of the field value in the instruction
42
+ *
43
+ * ```ts
44
+ * client.agent.action.generate({
45
+ * schemaId,
46
+ * documentId,
47
+ * instruction: 'Give the following field value:\n $pte \n ---\nGenerate keywords.',
48
+ * instructionParams: {
49
+ * pte: {
50
+ * type: 'field',
51
+ * path: ['pteField'],
52
+ * },
53
+ * },
54
+ * target: {path: 'keywords' }
55
+ * })
56
+ *
57
+ * ```
58
+ *
59
+ * @beta
60
+ * */
61
+ export interface FieldAgentActionParam {
62
+ type: 'field'
63
+ /**
64
+ * Examples: 'title', ['array', \{_key: 'arrayItemKey'\}, 'field']
65
+ */
66
+ path: AgentActionPathSegment | AgentActionPath
67
+ /**
68
+ * If omitted, implicitly uses the documentId of the instruction target
69
+ */
70
+ documentId?: string
71
+ }
72
+
73
+ /**
74
+ *
75
+ * Includes a LLM-friendly version of the document in the instruction
76
+ *
77
+ * ```ts
78
+ * client.agent.action.generate({
79
+ * schemaId,
80
+ * documentId,
81
+ * instruction: 'Give the following document value:\n $document \n ---\nGenerate keywords.',
82
+ * instructionParams: {
83
+ * document: {
84
+ * type: 'document',
85
+ * },
86
+ * },
87
+ * target: {path: 'keywords' }
88
+ * })
89
+ * ```
90
+ *
91
+ * @beta
92
+ * */
93
+ export interface DocumentAgentActionParam {
94
+ type: 'document'
95
+ /**
96
+ * If omitted, implicitly uses the documentId of the instruction target
97
+ */
98
+ documentId?: string
99
+ }
100
+
101
+ /**
102
+ * Includes a LLM-friendly version of GROQ query result in the instruction
103
+ *
104
+ * ```ts
105
+ * client.agent.action.generate({
106
+ * schemaId,
107
+ * documentId,
108
+ * instruction: 'Give the following list of titles:\n $list \n ---\nGenerate a similar title.',
109
+ * instructionParams: {
110
+ * list: {
111
+ * type: 'groq',
112
+ * query: '* [_type==$type].title',
113
+ * params: {type: 'article'}
114
+ * },
115
+ * },
116
+ * target: {path: 'title' }
117
+ * })
118
+ * ```
119
+ * @beta
120
+ * */
121
+ export interface GroqAgentActionParam {
122
+ type: 'groq'
123
+ query: string
124
+ params?: Record<string, string>
125
+ }
126
+
127
+ /** @beta */
128
+ export type AgentActionTypeConfig =
129
+ | {include: string[]; exclude?: never}
130
+ | {exclude: string[]; include?: never}
131
+
132
+ /** @beta */
133
+ export type AgentActionPathSegment = string | {_key: string}
134
+
135
+ /** @beta */
136
+ export type AgentActionPath = AgentActionPathSegment[]
137
+
138
+ /** @beta */
139
+ export interface AgentActionTargetInclude {
140
+ path: AgentActionPathSegment | AgentActionPath
141
+
142
+ /**
143
+ * By default, all children up to `target.maxPathDepth` are included.
144
+ * Fields or array items not on the exclude list, are implicitly included.
145
+ */
146
+ exclude?: AgentActionPathSegment[]
147
+
148
+ /**
149
+ * Types can be used to exclude array item types or all fields directly under the target path of a certain type.
150
+ * If you do exclude: ['string'] all string fields under the target will be excluded, for instance.
151
+ *
152
+ * `types.include` and `types.exclude` are mutually exclusive.
153
+ */
154
+ types?: AgentActionTypeConfig
155
+ }
156
+
157
+ /**
158
+ * @beta
159
+ */
160
+ export interface AgentActionTarget {
161
+ /**
162
+ * Root target path.
163
+ *
164
+ * Use this to have the instruction only affect a part of the document.
165
+ *
166
+ * To further control the behavior of individual paths under the root, use `include`, `exclude`, `types.include`
167
+ * and `types.exclude`.
168
+ *
169
+ * Example:
170
+ *
171
+ * `path: ['body', {_key: 'someKey'}, 'nestedObject']`
172
+ *
173
+ * Here, the instruction will only write to fields under the nestedObject.
174
+ *
175
+ * Default: [] = the document itself
176
+ *
177
+ * @see #AgentActionPathSegment
178
+ * @see #AgentActionPath
179
+ * */
180
+ path?: AgentActionPathSegment | AgentActionPath
181
+
182
+ /**
183
+ * maxPathDepth controls how deep into the schema from the target root the instruction will affect.
184
+ *
185
+ * Depth is based on path segments:
186
+ * - `title` has depth 1
187
+ * - `array[_key="no"].title` has depth 3
188
+ *
189
+ * Be careful not to set this too high in studios with recursive document schemas, as it could have
190
+ * negative impact on performance; both for runtime and quality of responses.
191
+ *
192
+ * Default: 4
193
+ */
194
+ maxPathDepth?: number
195
+
196
+ /**
197
+ * By default, all children up to `target.maxPathDepth` are included.
198
+ * Fields or array items not on the exclude list, are implicitly included.
199
+ */
200
+ exclude?: AgentActionPathSegment[]
201
+
202
+ /**
203
+ * Types can be used to exclude array item types or all fields directly under the target path of a certain type.
204
+ * If you do exclude: ['string'] all string fields under the target will be excluded, for instance.
205
+ *
206
+ * `types.include` and `types.exclude` are mutually exclusive.
207
+ */
208
+ types?: AgentActionTypeConfig
209
+ }
210
+
211
+ /** @beta */
212
+ export type AgentActionParam =
213
+ | string
214
+ | ConstantAgentActionParam
215
+ | FieldAgentActionParam
216
+ | DocumentAgentActionParam
217
+ | GroqAgentActionParam
218
+
219
+ /** @beta */
220
+ export type AgentActionParams = Record<string, AgentActionParam>
221
+
222
+ /** @beta */
223
+ export interface AgentActionRequestBase {
224
+ /** schemaId as reported by sanity deploy / sanity schema store */
225
+ schemaId: string
226
+
227
+ /**
228
+ * When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
229
+ *
230
+ * By default, Generate will not output to conditional `readOnly` and `hidden` fields,
231
+ * ie, they are considered to resolve to `readOnly: true` / `hidden: true`.
232
+ *
233
+ * `conditionalPaths` param allows setting the default conditional value for
234
+ * `hidden` and `readOnly` to false,
235
+ * or individually set `hidden` and `readOnly` state for individual document paths.
236
+ *
237
+ * Note: fields and types with explicit readOnly: true or hidden: true in the schema, are not available to Generate,
238
+ * and cannot be changed via conditionalPaths
239
+ *
240
+ * conditionalPaths state only apply to fields and types that have conditional `hidden` or `readOnly` in their schema definition.
241
+ *
242
+ * Consider using `hidden: () => true` in schema config, if a field should be writeable only by Generate and never
243
+ * visible in the studio – then make the field visible to the Generate using `conditionalPaths`.
244
+ *
245
+ * @see GenerateRequestBase#target
246
+ */
247
+ conditionalPaths?: {
248
+ defaultReadOnly?: boolean
249
+ defaultHidden?: boolean
250
+ paths?: {
251
+ /** path here is not a relative path: it must be the full document path, regardless of `path` param used in targets */
252
+ path: AgentActionPath
253
+ readOnly: boolean
254
+ hidden: boolean
255
+ }[]
256
+ }
257
+
258
+ /**
259
+ * When localeSettings is provided on the request, instruct can write to date and datetime fields.
260
+ * Otherwise, such fields will be ignored.
261
+ */
262
+ localeSettings?: {
263
+ /**
264
+ * A valid Unicode BCP 47 locale identifier used to interpret and format
265
+ * natural language inputs and date output. Examples include "en-US", "fr-FR", or "ja-JP".
266
+ *
267
+ * This affects how phrases like "next Friday" or "in two weeks" are parsed,
268
+ * and how resulting dates are presented (e.g., 12-hour vs 24-hour format).
269
+ *
270
+ * @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales
271
+ */
272
+ locale: string
273
+
274
+ /**
275
+ * A valid IANA time zone identifier used to resolve relative and absolute
276
+ * date expressions to a specific point in time. Examples include
277
+ * "America/New_York", "Europe/Paris", or "Asia/Tokyo".
278
+ *
279
+ * This ensures phrases like "tomorrow at 9am" are interpreted correctly
280
+ * based on the user's local time.
281
+ *
282
+ * @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
283
+ */
284
+ timeZone: string
285
+ }
286
+
287
+ /**
288
+ * Controls how much variance the instructions will run with.
289
+ *
290
+ * Value must be in the range [0, 1] (inclusive).
291
+ *
292
+ * Defaults:
293
+ * - generate: 0.3
294
+ * - translate: 0
295
+ * - transform: 0
296
+ */
297
+ temperature?: number
298
+ }
299
+
300
+ /** @beta */
301
+ export interface AgentActionSync {
302
+ /**
303
+ * By default, noWrite: false.
304
+ * Write enabled operations will mutate the target document, and emit AI presence in the studio.
305
+ *
306
+ * When noWrite: true, the api will not mutate any documents nor emit presence.
307
+ * Ie, when true, no changes will be made to content-lake
308
+ *
309
+ * noWrite: true is incompatible with async: true,
310
+ * as noWrite implies that you will use the return value of the operation
311
+ */
312
+ noWrite?: boolean
313
+
314
+ /**
315
+ * When async: true, requests responds with status 201 and \{_id\} as response body as soon as the request is validated.
316
+ * The instruction operation will carry on in the background.
317
+ *
318
+ * When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
319
+ *
320
+ * async: true is incompatible with noWrite: true, as async: true does not return the resulting document
321
+ */
322
+ async?: false
323
+ }
324
+
325
+ /** @beta */
326
+ export interface AgentActionAsync {
327
+ /**
328
+ * When async: true, requests responds with status 201 and \{_id\} as response body as soon as the request is validated.
329
+ * The instruction operation will carry on in the background.
330
+ *
331
+ * When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
332
+ *
333
+ * async: true is incompatible with noWrite, as async: true does not return the resulting document
334
+ */
335
+ async: true
336
+ }