@sanity/client 6.28.4-beta.0 → 6.28.4-generate.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/_chunks-cjs/config.cjs +2 -13
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-es/config.js +2 -13
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/index.browser.cjs +46 -15
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +404 -0
- package/dist/index.browser.d.ts +404 -0
- package/dist/index.browser.js +46 -15
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +45 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +393 -0
- package/dist/index.d.ts +393 -0
- package/dist/index.js +45 -3
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +393 -0
- package/dist/stega.browser.d.ts +393 -0
- package/dist/stega.d.cts +393 -0
- package/dist/stega.d.ts +393 -0
- package/package.json +22 -22
- package/src/SanityClient.ts +13 -0
- package/src/agent/actions/AgentActionsClient.ts +74 -0
- package/src/agent/actions/generate.ts +24 -0
- package/src/agent/actions/types.ts +373 -0
- package/src/config.ts +6 -23
- package/src/types.ts +10 -0
- package/umd/sanityClient.js +46 -15
- package/umd/sanityClient.min.js +2 -2
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
//Request shape
|
|
2
|
+
|
|
3
|
+
import type {Any, SanityDocumentStub} from '@sanity/client'
|
|
4
|
+
|
|
5
|
+
/** @beta */
|
|
6
|
+
export interface GenerateConstantInstructionParam {
|
|
7
|
+
type: 'constant'
|
|
8
|
+
value: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
*
|
|
13
|
+
* Includes a LLM-friendly version of the field value in the instruction
|
|
14
|
+
* @beta
|
|
15
|
+
* */
|
|
16
|
+
export interface GenerateFieldInstructionParam {
|
|
17
|
+
type: 'field'
|
|
18
|
+
/**
|
|
19
|
+
* Examples: 'title', 'array[_key=="key"].field'
|
|
20
|
+
*/
|
|
21
|
+
path: string
|
|
22
|
+
/**
|
|
23
|
+
* If omitted, implicitly uses the documentId of the instruction target
|
|
24
|
+
*/
|
|
25
|
+
documentId?: string
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
*
|
|
30
|
+
* Includes a LLM-friendly version of the document in the instruction
|
|
31
|
+
* @beta
|
|
32
|
+
* */
|
|
33
|
+
export interface GenerateDocumentInstructionParam {
|
|
34
|
+
type: 'document'
|
|
35
|
+
/**
|
|
36
|
+
* If omitted, implicitly uses the documentId of the instruction target
|
|
37
|
+
*/
|
|
38
|
+
documentId?: string
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* Includes a LLM-friendly version of GROQ query result in the instruction
|
|
43
|
+
* @beta
|
|
44
|
+
* */
|
|
45
|
+
export interface GenerateGroqInstructionParam {
|
|
46
|
+
type: 'groq'
|
|
47
|
+
query: string
|
|
48
|
+
params?: Record<string, string>
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type GenerateTypeConfig =
|
|
52
|
+
| {include: string[]; exclude?: never}
|
|
53
|
+
| {exclude: string[]; include?: never}
|
|
54
|
+
|
|
55
|
+
export type GeneratePathSegment = string | {_key: string}
|
|
56
|
+
export type GeneratePath = GeneratePathSegment[]
|
|
57
|
+
export type GenerateOperation = 'set' | 'append' | 'mixed'
|
|
58
|
+
|
|
59
|
+
export interface GenerateTargetInclude {
|
|
60
|
+
path: GeneratePathSegment | GeneratePath
|
|
61
|
+
|
|
62
|
+
/**
|
|
63
|
+
* Sets the operation for this path, and all its children.
|
|
64
|
+
* This overrides any operation set parents or the root target.
|
|
65
|
+
* @see #GenerateTarget.operation
|
|
66
|
+
* @see #include
|
|
67
|
+
*/
|
|
68
|
+
operation?: GenerateOperation
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
72
|
+
*
|
|
73
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
74
|
+
*
|
|
75
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
76
|
+
*/
|
|
77
|
+
include?: (GeneratePathSegment | GenerateTargetInclude)[]
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
81
|
+
* Fields or array items not on the exclude list, are implicitly included.
|
|
82
|
+
*/
|
|
83
|
+
exclude?: GeneratePathSegment[]
|
|
84
|
+
|
|
85
|
+
/**
|
|
86
|
+
* Types can be used to exclude array item types or all fields directly under the target path of a certain type.
|
|
87
|
+
* If you do exclude: ['string'] all string fields under the target will be excluded, for instance.
|
|
88
|
+
*
|
|
89
|
+
* `types.include` and `types.exclude` are mutually exclusive.
|
|
90
|
+
*/
|
|
91
|
+
types?: GenerateTypeConfig
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* @beta
|
|
96
|
+
*/
|
|
97
|
+
export interface GenerateTarget {
|
|
98
|
+
/**
|
|
99
|
+
* Root target path.
|
|
100
|
+
*
|
|
101
|
+
* Use this to have the instruction only affect a part of the document.
|
|
102
|
+
*
|
|
103
|
+
* To further control the behavior of individual paths under the root, use `include`, `exclude`, `types.include`
|
|
104
|
+
* and `types.exclude`.
|
|
105
|
+
*
|
|
106
|
+
* Example:
|
|
107
|
+
*
|
|
108
|
+
* `path: ['body', {_key: 'someKey'}, 'nestedObject']`
|
|
109
|
+
*
|
|
110
|
+
* Here, the instruction will only write to fields under the nestedObject.
|
|
111
|
+
*
|
|
112
|
+
* Default: [] = the document itself
|
|
113
|
+
*
|
|
114
|
+
* @see #GeneratePathSegment
|
|
115
|
+
* @see #GeneratePath
|
|
116
|
+
* */
|
|
117
|
+
path?: GeneratePathSegment | GeneratePath
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Sets the default operation for all paths in the target.
|
|
121
|
+
* Generate runs in `'mixed'` operation mode by default:
|
|
122
|
+
* Changes are set in all non-array fields, and append to all array fields.
|
|
123
|
+
*
|
|
124
|
+
* ### Operation types
|
|
125
|
+
* - `'set'` – an *overwriting* operation, and replaces the full field value.
|
|
126
|
+
* - `'append'`:
|
|
127
|
+
* – array fields: appends new items to the end of the array,
|
|
128
|
+
* - string fields: '<existing content> <new content>'
|
|
129
|
+
* - text fields: '<existing content>\n<new content>'
|
|
130
|
+
* - number fields: existing + new
|
|
131
|
+
* - other field types not mentioned will set instead (dates, url)
|
|
132
|
+
* - `'mixed'` – (default) sets non-array fields, and appends to array fields
|
|
133
|
+
*
|
|
134
|
+
* The default operation can be overridden on a per-path basis using `include`.
|
|
135
|
+
*
|
|
136
|
+
* Nested fields inherit the operation specified by their parent and falls back to the
|
|
137
|
+
* top level target operation if not otherwise specified.
|
|
138
|
+
*
|
|
139
|
+
* Use `include` to change the `operation` of individual fields or items.
|
|
140
|
+
*
|
|
141
|
+
* #### Appending in the middle of arrays
|
|
142
|
+
* `target: {path: ['array'], operation: 'append'}` will append the output of the instruction to the end of the array.
|
|
143
|
+
*
|
|
144
|
+
* To insert in the middle of the array, use `target: {path: ['array', {_key: 'appendAfterKey'}], operation: 'append'}`.
|
|
145
|
+
* Here, the output of the instruction will be appended after the array item with key `'appendAfterKey'`.
|
|
146
|
+
*
|
|
147
|
+
* @see #GenerateTargetInclude.operation
|
|
148
|
+
* @see #include
|
|
149
|
+
* @see #GenerateTargetInclude.include
|
|
150
|
+
*/
|
|
151
|
+
operation?: GenerateOperation
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* maxPathDepth controls how deep into the schema from the target root the instruction will affect.
|
|
155
|
+
*
|
|
156
|
+
* Depth is based on path segments:
|
|
157
|
+
* - `title` has depth 1
|
|
158
|
+
* - `array[_key="no"].title` has depth 3
|
|
159
|
+
*
|
|
160
|
+
* Be careful not to set this too high in studios with recursive document schemas, as it could have
|
|
161
|
+
* negative impact on performance; both for runtime and quality of responses.
|
|
162
|
+
*
|
|
163
|
+
* Default: 4
|
|
164
|
+
*/
|
|
165
|
+
maxPathDepth?: number
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
169
|
+
*
|
|
170
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
171
|
+
*
|
|
172
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
173
|
+
*/
|
|
174
|
+
include?: (GeneratePathSegment | GenerateTargetInclude)[]
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
178
|
+
* Fields or array items not on the exclude list, are implicitly included.
|
|
179
|
+
*/
|
|
180
|
+
exclude?: GeneratePathSegment[]
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Types can be used to exclude array item types or all fields directly under the target path of a certain type.
|
|
184
|
+
* If you do exclude: ['string'] all string fields under the target will be excluded, for instance.
|
|
185
|
+
*
|
|
186
|
+
* `types.include` and `types.exclude` are mutually exclusive.
|
|
187
|
+
*/
|
|
188
|
+
types?: GenerateTypeConfig
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/** @beta */
|
|
192
|
+
export type GenerateInstructionParam =
|
|
193
|
+
| string
|
|
194
|
+
| GenerateConstantInstructionParam
|
|
195
|
+
| GenerateFieldInstructionParam
|
|
196
|
+
| GenerateDocumentInstructionParam
|
|
197
|
+
| GenerateGroqInstructionParam
|
|
198
|
+
|
|
199
|
+
/** @beta */
|
|
200
|
+
export type GenerateInstructionParams = Record<string, GenerateInstructionParam>
|
|
201
|
+
|
|
202
|
+
interface GenerateRequestBase {
|
|
203
|
+
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
204
|
+
schemaId: string
|
|
205
|
+
/** string template using $variable – more on this below under "Dynamic instruction" */
|
|
206
|
+
instruction: string
|
|
207
|
+
/** param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable" */
|
|
208
|
+
instructionParams?: GenerateInstructionParams
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Target defines which parts of the document will be affected by the instruction.
|
|
212
|
+
* It can be an array, so multiple parts of the document can be separately configured in detail.
|
|
213
|
+
*
|
|
214
|
+
* Omitting target implies that the document itself is the root.
|
|
215
|
+
*
|
|
216
|
+
* Notes:
|
|
217
|
+
* - instruction can only affect fields up to `maxPathDepth`
|
|
218
|
+
* - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
|
|
219
|
+
* It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
220
|
+
*
|
|
221
|
+
* @see GenerateRequestBase#conditionalPaths
|
|
222
|
+
*/
|
|
223
|
+
target?: GenerateTarget | GenerateTarget[]
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
|
|
227
|
+
*
|
|
228
|
+
* By default, Generate will not output to conditional `readOnly` and `hidden` fields,
|
|
229
|
+
* ie, they are considered to resolve to `readOnly: true` / `hidden: true`.
|
|
230
|
+
*
|
|
231
|
+
* `conditionalPaths` param allows setting the default conditional value for
|
|
232
|
+
* `hidden` and `readOnly` to false,
|
|
233
|
+
* or individually set `hidden` and `readOnly` state for individual document paths.
|
|
234
|
+
*
|
|
235
|
+
*
|
|
236
|
+
* Note: fields and types with explicit readOnly: true or hidden: true in the schema, are not available to Generate,
|
|
237
|
+
* and cannot be changed via conditionalPaths
|
|
238
|
+
*
|
|
239
|
+
* conditionalPaths state only apply to fields and types that have conditional `hidden` or `readOnly` in their schema definition.
|
|
240
|
+
*
|
|
241
|
+
* Consider using `hidden: () => true` in schema config, if a field should be writeable only by Generate and never
|
|
242
|
+
* visible in the studio – then make the field visible to the Generate using `conditionalPaths`.
|
|
243
|
+
*
|
|
244
|
+
* @see GenerateRequestBase#target
|
|
245
|
+
*/
|
|
246
|
+
conditionalPaths?: {
|
|
247
|
+
defaultReadOnly?: boolean
|
|
248
|
+
defaultHidden?: boolean
|
|
249
|
+
paths?: {
|
|
250
|
+
/** path here is not a relative path: it must be the full document path, regardless of `path` param used in targets */
|
|
251
|
+
path: GeneratePath
|
|
252
|
+
readOnly: boolean
|
|
253
|
+
hidden: boolean
|
|
254
|
+
}[]
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
/**
|
|
258
|
+
* When localeSettings is provided on the request, instruct can write to date and datetime fields.
|
|
259
|
+
* Otherwise, such fields will be ignored.
|
|
260
|
+
*/
|
|
261
|
+
localeSettings?: {
|
|
262
|
+
/**
|
|
263
|
+
* A valid Unicode BCP 47 locale identifier used to interpret and format
|
|
264
|
+
* natural language inputs and date output. Examples include "en-US", "fr-FR", or "ja-JP".
|
|
265
|
+
*
|
|
266
|
+
* This affects how phrases like "next Friday" or "in two weeks" are parsed,
|
|
267
|
+
* and how resulting dates are presented (e.g., 12-hour vs 24-hour format).
|
|
268
|
+
*
|
|
269
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales
|
|
270
|
+
*/
|
|
271
|
+
locale: string
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* A valid IANA time zone identifier used to resolve relative and absolute
|
|
275
|
+
* date expressions to a specific point in time. Examples include
|
|
276
|
+
* "America/New_York", "Europe/Paris", or "Asia/Tokyo".
|
|
277
|
+
*
|
|
278
|
+
* This ensures phrases like "tomorrow at 9am" are interpreted correctly
|
|
279
|
+
* based on the user's local time.
|
|
280
|
+
*
|
|
281
|
+
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
282
|
+
*/
|
|
283
|
+
timeZone: string
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/**
|
|
287
|
+
* Controls how much variance the instructions will run with.
|
|
288
|
+
*
|
|
289
|
+
* Value must be in the range [0, 1] (inclusive).
|
|
290
|
+
*
|
|
291
|
+
* Default: 0.3
|
|
292
|
+
*/
|
|
293
|
+
temperature?: number
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
interface Sync {
|
|
297
|
+
/**
|
|
298
|
+
* By default, skipWrite: false.
|
|
299
|
+
* Write enabled operations will mutate the target document, and emit AI presence in the studio.
|
|
300
|
+
*
|
|
301
|
+
* When skipWrite: true, the api will not mutate any documents nor emit presence.
|
|
302
|
+
* Ie, when true, no changes will be made to content-lake
|
|
303
|
+
*
|
|
304
|
+
* skipWrite: true is incompatible with async: true,
|
|
305
|
+
* as skipWrite implies that you will use the return value of the operation
|
|
306
|
+
*/
|
|
307
|
+
skipWrite?: boolean
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
|
|
311
|
+
* The instruction operation will carry on in the background.
|
|
312
|
+
*
|
|
313
|
+
* When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
|
|
314
|
+
*
|
|
315
|
+
* async: true is incompatible with skipWrite: true, as async: true does not return the resulting document
|
|
316
|
+
*/
|
|
317
|
+
async?: false
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
interface Async {
|
|
321
|
+
/**
|
|
322
|
+
* When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
|
|
323
|
+
* The instruction operation will carry on in the background.
|
|
324
|
+
*
|
|
325
|
+
* When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
|
|
326
|
+
*
|
|
327
|
+
* async: true is incompatible with skipWrite, as async: true does not return the resulting document
|
|
328
|
+
*/
|
|
329
|
+
async: true
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/**
|
|
333
|
+
* Instruction for an existing document.
|
|
334
|
+
* @beta
|
|
335
|
+
*/
|
|
336
|
+
interface ExistingDocumentRequest {
|
|
337
|
+
documentId: string
|
|
338
|
+
createDocument?: never
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
/**
|
|
342
|
+
* Instruction to create a new document
|
|
343
|
+
* @beta
|
|
344
|
+
*/
|
|
345
|
+
interface CreateDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
|
|
346
|
+
createDocument: {
|
|
347
|
+
/** if no _id is provided, one will be generated. _id is always returned when the requests succeed */
|
|
348
|
+
_id?: string
|
|
349
|
+
_type: string
|
|
350
|
+
} & SanityDocumentStub<T>
|
|
351
|
+
documentId?: never
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
/** @beta */
|
|
355
|
+
export type GenerateSyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
356
|
+
| ExistingDocumentRequest
|
|
357
|
+
| CreateDocumentRequest<T>
|
|
358
|
+
) &
|
|
359
|
+
GenerateRequestBase &
|
|
360
|
+
Sync
|
|
361
|
+
|
|
362
|
+
/** @beta */
|
|
363
|
+
export type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
364
|
+
| ExistingDocumentRequest
|
|
365
|
+
| CreateDocumentRequest<T>
|
|
366
|
+
) &
|
|
367
|
+
GenerateRequestBase &
|
|
368
|
+
Async
|
|
369
|
+
|
|
370
|
+
/** @beta */
|
|
371
|
+
export type GenerateInstruction<T extends Record<string, Any> = Record<string, Any>> =
|
|
372
|
+
| GenerateSyncInstruction<T>
|
|
373
|
+
| GenerateAsyncInstruction<T>
|
package/src/config.ts
CHANGED
|
@@ -28,30 +28,15 @@ function validateApiVersion(apiVersion: string) {
|
|
|
28
28
|
}
|
|
29
29
|
}
|
|
30
30
|
|
|
31
|
-
const VALID_PERSPECTIVE = /^[a-z0-9_]+$/i
|
|
32
|
-
|
|
33
31
|
/**
|
|
34
32
|
* @internal - it may have breaking changes in any release
|
|
35
33
|
*/
|
|
36
34
|
export function validateApiPerspective(
|
|
37
35
|
perspective: unknown,
|
|
38
36
|
): asserts perspective is ClientPerspective {
|
|
39
|
-
if (Array.isArray(perspective)) {
|
|
40
|
-
if (perspective.includes('raw')) {
|
|
41
|
-
throw new TypeError(
|
|
42
|
-
`Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives`,
|
|
43
|
-
)
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
const invalid = (Array.isArray(perspective) ? perspective : [perspective]).filter(
|
|
48
|
-
(perspectiveName) =>
|
|
49
|
-
typeof perspectiveName !== 'string' || !VALID_PERSPECTIVE.test(perspectiveName),
|
|
50
|
-
)
|
|
51
|
-
if (invalid.length > 0) {
|
|
52
|
-
const formatted = invalid.map((v) => JSON.stringify(v))
|
|
37
|
+
if (Array.isArray(perspective) && perspective.length > 1 && perspective.includes('raw')) {
|
|
53
38
|
throw new TypeError(
|
|
54
|
-
`Invalid API perspective value
|
|
39
|
+
`Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives`,
|
|
55
40
|
)
|
|
56
41
|
}
|
|
57
42
|
}
|
|
@@ -123,11 +108,9 @@ export const initConfig = (
|
|
|
123
108
|
const isLocalhost = isBrowser && isLocal(window.location.hostname)
|
|
124
109
|
|
|
125
110
|
const hasToken = Boolean(newConfig.token)
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (withCredentials && hasToken) {
|
|
111
|
+
if (newConfig.withCredentials && hasToken) {
|
|
129
112
|
warnings.printCredentialedTokenWarning()
|
|
130
|
-
withCredentials = false
|
|
113
|
+
newConfig.withCredentials = false
|
|
131
114
|
}
|
|
132
115
|
|
|
133
116
|
if (isBrowser && isLocalhost && hasToken && newConfig.ignoreBrowserTokenWarning !== true) {
|
|
@@ -154,12 +137,12 @@ export const initConfig = (
|
|
|
154
137
|
newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, '')
|
|
155
138
|
newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost
|
|
156
139
|
|
|
157
|
-
if (newConfig.useCdn === true && withCredentials) {
|
|
140
|
+
if (newConfig.useCdn === true && newConfig.withCredentials) {
|
|
158
141
|
warnings.printCdnAndWithCredentialsWarning()
|
|
159
142
|
}
|
|
160
143
|
|
|
161
144
|
// If `useCdn` is undefined, we treat it as `true`
|
|
162
|
-
newConfig.useCdn = newConfig.useCdn !== false && !withCredentials
|
|
145
|
+
newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials
|
|
163
146
|
|
|
164
147
|
validateApiVersion(newConfig.apiVersion)
|
|
165
148
|
|
package/src/types.ts
CHANGED
|
@@ -1336,6 +1336,16 @@ export type ClientReturn<
|
|
|
1336
1336
|
Fallback = Any,
|
|
1337
1337
|
> = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
|
|
1338
1338
|
|
|
1339
|
+
export type {
|
|
1340
|
+
GenerateAsyncInstruction,
|
|
1341
|
+
GenerateConstantInstructionParam,
|
|
1342
|
+
GenerateFieldInstructionParam,
|
|
1343
|
+
GenerateGroqInstructionParam,
|
|
1344
|
+
GenerateInstruction,
|
|
1345
|
+
GenerateInstructionParam,
|
|
1346
|
+
GenerateInstructionParams,
|
|
1347
|
+
GenerateSyncInstruction,
|
|
1348
|
+
} from './agent/actions/types'
|
|
1339
1349
|
export type {
|
|
1340
1350
|
ContentSourceMapParsedPath,
|
|
1341
1351
|
ContentSourceMapParsedPathKeyedSegment,
|
package/umd/sanityClient.js
CHANGED
|
@@ -2109,21 +2109,11 @@
|
|
|
2109
2109
|
if (!(/^\d{4}-\d{2}-\d{2}$/.test(apiVersion) && apiDate instanceof Date && apiDate.getTime() > 0))
|
|
2110
2110
|
throw new Error("Invalid API version string, expected `1` or date in format `YYYY-MM-DD`");
|
|
2111
2111
|
}
|
|
2112
|
-
const VALID_PERSPECTIVE = /^[a-z0-9_]+$/i;
|
|
2113
2112
|
function validateApiPerspective(perspective) {
|
|
2114
|
-
if (Array.isArray(perspective) && perspective.includes("raw"))
|
|
2113
|
+
if (Array.isArray(perspective) && perspective.length > 1 && perspective.includes("raw"))
|
|
2115
2114
|
throw new TypeError(
|
|
2116
2115
|
'Invalid API perspective value: "raw". The raw-perspective can not be combined with other perspectives'
|
|
2117
2116
|
);
|
|
2118
|
-
const invalid = (Array.isArray(perspective) ? perspective : [perspective]).filter(
|
|
2119
|
-
(perspectiveName) => typeof perspectiveName != "string" || !VALID_PERSPECTIVE.test(perspectiveName)
|
|
2120
|
-
);
|
|
2121
|
-
if (invalid.length > 0) {
|
|
2122
|
-
const formatted = invalid.map((v) => JSON.stringify(v));
|
|
2123
|
-
throw new TypeError(
|
|
2124
|
-
`Invalid API perspective value${invalid.length === 1 ? "" : "s"}: ${formatted.join(", ")}, expected \`published\`, \`drafts\`, \`raw\` or a release identifier string`
|
|
2125
|
-
);
|
|
2126
|
-
}
|
|
2127
2117
|
}
|
|
2128
2118
|
const initConfig = (config, prevConfig) => {
|
|
2129
2119
|
const specifiedConfig = {
|
|
@@ -2162,8 +2152,7 @@
|
|
|
2162
2152
|
`stega.studioUrl must be a string or a function, received ${newConfig.stega.studioUrl}`
|
|
2163
2153
|
);
|
|
2164
2154
|
const isBrowser = typeof window < "u" && window.location && window.location.hostname, isLocalhost = isBrowser && isLocal(window.location.hostname), hasToken = !!newConfig.token;
|
|
2165
|
-
|
|
2166
|
-
withCredentials && hasToken && (printCredentialedTokenWarning(), withCredentials = false), isBrowser && isLocalhost && hasToken && newConfig.ignoreBrowserTokenWarning !== true ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn === true && withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== false && !withCredentials, validateApiVersion(newConfig.apiVersion);
|
|
2155
|
+
newConfig.withCredentials && hasToken && (printCredentialedTokenWarning(), newConfig.withCredentials = false), isBrowser && isLocalhost && hasToken && newConfig.ignoreBrowserTokenWarning !== true ? printBrowserTokenWarning() : typeof newConfig.useCdn > "u" && printCdnWarning(), projectBased && projectId(newConfig.projectId), newConfig.dataset && dataset(newConfig.dataset), "requestTagPrefix" in newConfig && (newConfig.requestTagPrefix = newConfig.requestTagPrefix ? requestTag(newConfig.requestTagPrefix).replace(/\.+$/, "") : void 0), newConfig.apiVersion = `${newConfig.apiVersion}`.replace(/^v/, ""), newConfig.isDefaultApi = newConfig.apiHost === defaultConfig.apiHost, newConfig.useCdn === true && newConfig.withCredentials && printCdnAndWithCredentialsWarning(), newConfig.useCdn = newConfig.useCdn !== false && !newConfig.withCredentials, validateApiVersion(newConfig.apiVersion);
|
|
2167
2156
|
const hostParts = newConfig.apiHost.split("://", 2), protocol = hostParts[0], host = hostParts[1], cdnHost = newConfig.isDefaultApi ? defaultCdnHost : host;
|
|
2168
2157
|
return newConfig.useProjectHostname ? (newConfig.url = `${protocol}://${newConfig.projectId}.${host}/v${newConfig.apiVersion}`, newConfig.cdnUrl = `${protocol}://${newConfig.projectId}.${cdnHost}/v${newConfig.apiVersion}`) : (newConfig.url = `${newConfig.apiHost}/v${newConfig.apiVersion}`, newConfig.cdnUrl = newConfig.url), newConfig;
|
|
2169
2158
|
};
|
|
@@ -2834,6 +2823,42 @@ ${selectionOpts}`);
|
|
|
2834
2823
|
const error = new Error(signal?.reason ?? "The operation was aborted.");
|
|
2835
2824
|
return error.name = "AbortError", error;
|
|
2836
2825
|
}
|
|
2826
|
+
function _generate(client, httpRequest, request) {
|
|
2827
|
+
const dataset2 = hasDataset(client.config());
|
|
2828
|
+
return _request(client, httpRequest, {
|
|
2829
|
+
method: "POST",
|
|
2830
|
+
uri: `/agent/action/generate/${dataset2}`,
|
|
2831
|
+
body: request
|
|
2832
|
+
});
|
|
2833
|
+
}
|
|
2834
|
+
class ObservableAgentsActionClient {
|
|
2835
|
+
#client;
|
|
2836
|
+
#httpRequest;
|
|
2837
|
+
constructor(client, httpRequest) {
|
|
2838
|
+
this.#client = client, this.#httpRequest = httpRequest;
|
|
2839
|
+
}
|
|
2840
|
+
/**
|
|
2841
|
+
* Run an instruction to generate content in a target document.
|
|
2842
|
+
* @param request instruction request
|
|
2843
|
+
*/
|
|
2844
|
+
generate(request) {
|
|
2845
|
+
return _generate(this.#client, this.#httpRequest, request);
|
|
2846
|
+
}
|
|
2847
|
+
}
|
|
2848
|
+
class AgentActionsClient {
|
|
2849
|
+
#client;
|
|
2850
|
+
#httpRequest;
|
|
2851
|
+
constructor(client, httpRequest) {
|
|
2852
|
+
this.#client = client, this.#httpRequest = httpRequest;
|
|
2853
|
+
}
|
|
2854
|
+
/**
|
|
2855
|
+
* Run an instruction to generate content in a target document.
|
|
2856
|
+
* @param request instruction request
|
|
2857
|
+
*/
|
|
2858
|
+
generate(request) {
|
|
2859
|
+
return lastValueFrom(_generate(this.#client, this.#httpRequest, request));
|
|
2860
|
+
}
|
|
2861
|
+
}
|
|
2837
2862
|
class ObservableAssetsClient {
|
|
2838
2863
|
#client;
|
|
2839
2864
|
#httpRequest;
|
|
@@ -3228,6 +3253,7 @@ ${selectionOpts}`);
|
|
|
3228
3253
|
live;
|
|
3229
3254
|
projects;
|
|
3230
3255
|
users;
|
|
3256
|
+
agent;
|
|
3231
3257
|
/**
|
|
3232
3258
|
* Private properties
|
|
3233
3259
|
*/
|
|
@@ -3238,7 +3264,9 @@ ${selectionOpts}`);
|
|
|
3238
3264
|
*/
|
|
3239
3265
|
listen = _listen;
|
|
3240
3266
|
constructor(httpRequest, config = defaultConfig) {
|
|
3241
|
-
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)
|
|
3267
|
+
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.agent = {
|
|
3268
|
+
action: new ObservableAgentsActionClient(this, this.#httpRequest)
|
|
3269
|
+
};
|
|
3242
3270
|
}
|
|
3243
3271
|
/**
|
|
3244
3272
|
* Clone the client - returns a new instance
|
|
@@ -3377,6 +3405,7 @@ ${selectionOpts}`);
|
|
|
3377
3405
|
live;
|
|
3378
3406
|
projects;
|
|
3379
3407
|
users;
|
|
3408
|
+
agent;
|
|
3380
3409
|
/**
|
|
3381
3410
|
* Observable version of the Sanity client, with the same configuration as the promise-based one
|
|
3382
3411
|
*/
|
|
@@ -3391,7 +3420,9 @@ ${selectionOpts}`);
|
|
|
3391
3420
|
*/
|
|
3392
3421
|
listen = _listen;
|
|
3393
3422
|
constructor(httpRequest, config = defaultConfig) {
|
|
3394
|
-
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.
|
|
3423
|
+
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.agent = {
|
|
3424
|
+
action: new AgentActionsClient(this, this.#httpRequest)
|
|
3425
|
+
}, this.observable = new ObservableSanityClient(httpRequest, config);
|
|
3395
3426
|
}
|
|
3396
3427
|
/**
|
|
3397
3428
|
* Clone the client - returns a new instance
|