@sanity/client 6.29.0-generate.0 → 6.29.0-generate.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/client",
3
- "version": "6.29.0-generate.0",
3
+ "version": "6.29.0-generate.2",
4
4
  "description": "Client for retrieving, creating and patching data from Sanity.io",
5
5
  "keywords": [
6
6
  "sanity",
@@ -1,15 +1,20 @@
1
1
  import {lastValueFrom, type Observable} from 'rxjs'
2
2
 
3
3
  import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
4
- import type {
5
- Any,
6
- GenerateAsyncInstruction,
7
- GenerateInstruction,
8
- GenerateSyncInstruction,
9
- HttpRequest,
10
- IdentifiedSanityDocumentStub,
11
- } from '../../types'
12
- import {_generate} from './generate'
4
+ import type {Any, HttpRequest, IdentifiedSanityDocumentStub, TranslateDocument} from '../../types'
5
+ import {
6
+ _generate,
7
+ type GenerateAsyncInstruction,
8
+ type GenerateInstruction,
9
+ type GenerateSyncInstruction,
10
+ } from './generate'
11
+ import {
12
+ _transform,
13
+ type TransformDocument,
14
+ type TransformDocumentAsync,
15
+ type TransformDocumentSync,
16
+ } from './transform'
17
+ import {_translate, type TranslateDocumentAsync, type TranslateDocumentSync} from './translate'
13
18
 
14
19
  /** @public */
15
20
  export class ObservableAgentsActionClient {
@@ -40,6 +45,42 @@ export class ObservableAgentsActionClient {
40
45
  > {
41
46
  return _generate(this.#client, this.#httpRequest, request)
42
47
  }
48
+
49
+ transform(request: TransformDocumentAsync): Observable<{_id: string}>
50
+
51
+ transform<DocumentShape extends Record<string, Any>>(
52
+ request: TransformDocumentSync,
53
+ ): Observable<IdentifiedSanityDocumentStub & DocumentShape>
54
+
55
+ /**
56
+ * Transform a target document based on a source.
57
+ * @param request translation request
58
+ */
59
+ transform<DocumentShape extends Record<string, Any>, Req extends TransformDocument>(
60
+ request: Req,
61
+ ): Observable<
62
+ Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
63
+ > {
64
+ return _transform(this.#client, this.#httpRequest, request)
65
+ }
66
+
67
+ translate(request: TranslateDocumentAsync): Observable<{_id: string}>
68
+
69
+ translate<DocumentShape extends Record<string, Any>>(
70
+ request: TranslateDocumentSync,
71
+ ): Observable<IdentifiedSanityDocumentStub & DocumentShape>
72
+
73
+ /**
74
+ * Translate a target document based on a source.
75
+ * @param request translation request
76
+ */
77
+ translate<DocumentShape extends Record<string, Any>, Req extends TranslateDocument>(
78
+ request: Req,
79
+ ): Observable<
80
+ Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
81
+ > {
82
+ return _translate(this.#client, this.#httpRequest, request)
83
+ }
43
84
  }
44
85
 
45
86
  /** @public */
@@ -71,4 +112,40 @@ export class AgentActionsClient {
71
112
  > {
72
113
  return lastValueFrom(_generate(this.#client, this.#httpRequest, request))
73
114
  }
115
+
116
+ transform(request: TransformDocumentAsync): Promise<{_id: string}>
117
+
118
+ transform<DocumentShape extends Record<string, Any>>(
119
+ request: TransformDocumentSync,
120
+ ): Promise<IdentifiedSanityDocumentStub & DocumentShape>
121
+
122
+ /**
123
+ * Transform a target document based on a source.
124
+ * @param request translation request
125
+ */
126
+ transform<DocumentShape extends Record<string, Any>, Req extends TransformDocument>(
127
+ request: Req,
128
+ ): Promise<
129
+ Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
130
+ > {
131
+ return lastValueFrom(_transform(this.#client, this.#httpRequest, request))
132
+ }
133
+
134
+ translate(request: TranslateDocumentAsync): Promise<{_id: string}>
135
+
136
+ translate<DocumentShape extends Record<string, Any>>(
137
+ request: TranslateDocumentSync,
138
+ ): Promise<IdentifiedSanityDocumentStub & DocumentShape>
139
+
140
+ /**
141
+ * Translate a target document based on a source.
142
+ * @param request translation request
143
+ */
144
+ translate<DocumentShape extends Record<string, Any>, Req extends TranslateDocument>(
145
+ request: Req,
146
+ ): Promise<
147
+ Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
148
+ > {
149
+ return lastValueFrom(_translate(this.#client, this.#httpRequest, request))
150
+ }
74
151
  }
@@ -1,9 +1,5 @@
1
- //Request shape
2
-
3
- import type {Any, SanityDocumentStub} from '@sanity/client'
4
-
5
1
  /** @beta */
6
- export interface GenerateConstantInstructionParam {
2
+ export interface ConstantAgentActionParam {
7
3
  type: 'constant'
8
4
  value: string
9
5
  }
@@ -13,12 +9,12 @@ export interface GenerateConstantInstructionParam {
13
9
  * Includes a LLM-friendly version of the field value in the instruction
14
10
  * @beta
15
11
  * */
16
- export interface GenerateFieldInstructionParam {
12
+ export interface FieldAgentActionParam {
17
13
  type: 'field'
18
14
  /**
19
15
  * Examples: ['title'], ['array', {_key: 'arrayItemKey'}, 'field']
20
16
  */
21
- path: GeneratePath
17
+ path: AgentActionPath
22
18
  /**
23
19
  * If omitted, implicitly uses the documentId of the instruction target
24
20
  */
@@ -30,7 +26,7 @@ export interface GenerateFieldInstructionParam {
30
26
  * Includes a LLM-friendly version of the document in the instruction
31
27
  * @beta
32
28
  * */
33
- export interface GenerateDocumentInstructionParam {
29
+ export interface DocumentAgentActionParam {
34
30
  type: 'document'
35
31
  /**
36
32
  * If omitted, implicitly uses the documentId of the instruction target
@@ -42,45 +38,32 @@ export interface GenerateDocumentInstructionParam {
42
38
  * Includes a LLM-friendly version of GROQ query result in the instruction
43
39
  * @beta
44
40
  * */
45
- export interface GenerateGroqInstructionParam {
41
+ export interface GroqAgentActionParam {
46
42
  type: 'groq'
47
43
  query: string
48
44
  params?: Record<string, string>
49
45
  }
50
46
 
51
- export type GenerateTypeConfig =
47
+ /** @beta */
48
+ export type AgentActionTypeConfig =
52
49
  | {include: string[]; exclude?: never}
53
50
  | {exclude: string[]; include?: never}
54
51
 
55
- export type GeneratePathSegment = string | {_key: string}
56
- export type GeneratePath = GeneratePathSegment[]
57
- export type GenerateOperation = 'set' | 'append' | 'mixed'
52
+ /** @beta */
53
+ export type AgentActionPathSegment = string | {_key: string}
58
54
 
59
- export interface GenerateTargetInclude {
60
- path: GeneratePathSegment | GeneratePath
55
+ /** @beta */
56
+ export type AgentActionPath = AgentActionPathSegment[]
61
57
 
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)[]
58
+ /** @beta */
59
+ export interface AgentActionTargetInclude {
60
+ path: AgentActionPathSegment | AgentActionPath
78
61
 
79
62
  /**
80
63
  * By default, all children up to `target.maxPathDepth` are included.
81
64
  * Fields or array items not on the exclude list, are implicitly included.
82
65
  */
83
- exclude?: GeneratePathSegment[]
66
+ exclude?: AgentActionPathSegment[]
84
67
 
85
68
  /**
86
69
  * Types can be used to exclude array item types or all fields directly under the target path of a certain type.
@@ -88,13 +71,13 @@ export interface GenerateTargetInclude {
88
71
  *
89
72
  * `types.include` and `types.exclude` are mutually exclusive.
90
73
  */
91
- types?: GenerateTypeConfig
74
+ types?: AgentActionTypeConfig
92
75
  }
93
76
 
94
77
  /**
95
78
  * @beta
96
79
  */
97
- export interface GenerateTarget {
80
+ export interface AgentActionTarget {
98
81
  /**
99
82
  * Root target path.
100
83
  *
@@ -111,44 +94,10 @@ export interface GenerateTarget {
111
94
  *
112
95
  * Default: [] = the document itself
113
96
  *
114
- * @see #GeneratePathSegment
115
- * @see #GeneratePath
97
+ * @see #AgentActionPathSegment
98
+ * @see #AgentActionPath
116
99
  * */
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
100
+ path?: AgentActionPathSegment | AgentActionPath
152
101
 
153
102
  /**
154
103
  * maxPathDepth controls how deep into the schema from the target root the instruction will affect.
@@ -164,20 +113,11 @@ export interface GenerateTarget {
164
113
  */
165
114
  maxPathDepth?: number
166
115
 
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
116
  /**
177
117
  * By default, all children up to `target.maxPathDepth` are included.
178
118
  * Fields or array items not on the exclude list, are implicitly included.
179
119
  */
180
- exclude?: GeneratePathSegment[]
120
+ exclude?: AgentActionPathSegment[]
181
121
 
182
122
  /**
183
123
  * Types can be used to exclude array item types or all fields directly under the target path of a certain type.
@@ -185,42 +125,23 @@ export interface GenerateTarget {
185
125
  *
186
126
  * `types.include` and `types.exclude` are mutually exclusive.
187
127
  */
188
- types?: GenerateTypeConfig
128
+ types?: AgentActionTypeConfig
189
129
  }
190
130
 
191
131
  /** @beta */
192
- export type GenerateInstructionParam =
132
+ export type AgentActionParam =
193
133
  | string
194
- | GenerateConstantInstructionParam
195
- | GenerateFieldInstructionParam
196
- | GenerateDocumentInstructionParam
197
- | GenerateGroqInstructionParam
134
+ | ConstantAgentActionParam
135
+ | FieldAgentActionParam
136
+ | DocumentAgentActionParam
137
+ | GroqAgentActionParam
198
138
 
199
139
  /** @beta */
200
- export type GenerateInstructionParams = Record<string, GenerateInstructionParam>
140
+ export type AgentActionParams = Record<string, AgentActionParam>
201
141
 
202
- interface GenerateRequestBase {
142
+ export interface AgentActionRequestBase {
203
143
  /** schemaId as reported by sanity deploy / sanity schema store */
204
144
  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
145
 
225
146
  /**
226
147
  * When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
@@ -232,7 +153,6 @@ interface GenerateRequestBase {
232
153
  * `hidden` and `readOnly` to false,
233
154
  * or individually set `hidden` and `readOnly` state for individual document paths.
234
155
  *
235
- *
236
156
  * Note: fields and types with explicit readOnly: true or hidden: true in the schema, are not available to Generate,
237
157
  * and cannot be changed via conditionalPaths
238
158
  *
@@ -248,7 +168,7 @@ interface GenerateRequestBase {
248
168
  defaultHidden?: boolean
249
169
  paths?: {
250
170
  /** path here is not a relative path: it must be the full document path, regardless of `path` param used in targets */
251
- path: GeneratePath
171
+ path: AgentActionPath
252
172
  readOnly: boolean
253
173
  hidden: boolean
254
174
  }[]
@@ -288,12 +208,15 @@ interface GenerateRequestBase {
288
208
  *
289
209
  * Value must be in the range [0, 1] (inclusive).
290
210
  *
291
- * Default: 0.3
211
+ * Defaults:
212
+ * - generate: 0.3
213
+ * - translate: 0
214
+ * - transform: 0
292
215
  */
293
216
  temperature?: number
294
217
  }
295
218
 
296
- interface Sync {
219
+ export interface AgentActionSync {
297
220
  /**
298
221
  * By default, noWrite: false.
299
222
  * Write enabled operations will mutate the target document, and emit AI presence in the studio.
@@ -317,7 +240,7 @@ interface Sync {
317
240
  async?: false
318
241
  }
319
242
 
320
- interface Async {
243
+ export interface AgentActionAsync {
321
244
  /**
322
245
  * When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
323
246
  * The instruction operation will carry on in the background.
@@ -328,46 +251,3 @@ interface Async {
328
251
  */
329
252
  async: true
330
253
  }
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>
@@ -1,10 +1,161 @@
1
+ import type {
2
+ AgentActionAsync,
3
+ AgentActionPathSegment,
4
+ AgentActionRequestBase,
5
+ AgentActionSync,
6
+ AgentActionTarget,
7
+ AgentActionTargetInclude,
8
+ } from '@sanity/client/agent/actions/commonTypes'
1
9
  import {type Observable} from 'rxjs'
2
10
 
3
11
  import {_request} from '../../data/dataMethods'
4
12
  import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
5
- import type {Any, GenerateInstruction, HttpRequest, IdentifiedSanityDocumentStub} from '../../types'
13
+ import type {
14
+ AgentActionParams,
15
+ Any,
16
+ HttpRequest,
17
+ IdentifiedSanityDocumentStub,
18
+ SanityDocumentStub,
19
+ } from '../../types'
6
20
  import {hasDataset} from '../../validators'
7
21
 
22
+ /** @beta */
23
+ export type GenerateOperation = 'set' | 'append' | 'mixed'
24
+
25
+ /** @beta */
26
+ export interface GenerateRequestBase extends AgentActionRequestBase {
27
+ /** schemaId as reported by sanity deploy / sanity schema store */
28
+ schemaId: string
29
+ /** string template using $variable */
30
+ instruction: string
31
+ /** param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable" */
32
+ instructionParams?: AgentActionParams
33
+
34
+ /**
35
+ * Target defines which parts of the document will be affected by the instruction.
36
+ * It can be an array, so multiple parts of the document can be separately configured in detail.
37
+ *
38
+ * Omitting target implies that the document itself is the root.
39
+ *
40
+ * Notes:
41
+ * - instruction can only affect fields up to `maxPathDepth`
42
+ * - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
43
+ * It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
44
+ *
45
+ * @see AgentActionRequestBase#conditionalPaths
46
+ */
47
+ target?: GenerateTarget | GenerateTarget[]
48
+ }
49
+
50
+ /** @beta */
51
+ export interface GenerateTargetInclude extends AgentActionTargetInclude {
52
+ /**
53
+ * Sets the operation for this path, and all its children.
54
+ * This overrides any operation set parents or the root target.
55
+ * @see #GenerateTarget.operation
56
+ * @see #include
57
+ */
58
+ operation?: GenerateOperation
59
+
60
+ /**
61
+ * By default, all children up to `target.maxPathDepth` are included.
62
+ *
63
+ * When `include` is specified, only segments explicitly listed will be included.
64
+ *
65
+ * Fields or array items not on the include list, are implicitly excluded.
66
+ */
67
+ include?: (AgentActionPathSegment | GenerateTargetInclude)[]
68
+ }
69
+
70
+ /** @beta */
71
+ export interface GenerateTarget extends AgentActionTarget {
72
+ /**
73
+ * Sets the default operation for all paths in the target.
74
+ * Generate runs in `'mixed'` operation mode by default:
75
+ * Changes are set in all non-array fields, and append to all array fields.
76
+ *
77
+ * ### Operation types
78
+ * - `'set'` – an *overwriting* operation, and replaces the full field value.
79
+ * - `'append'`:
80
+ * – array fields: appends new items to the end of the array,
81
+ * - string fields: '<existing content> <new content>'
82
+ * - text fields: '<existing content>\n<new content>'
83
+ * - number fields: existing + new
84
+ * - other field types not mentioned will set instead (dates, url)
85
+ * - `'mixed'` – (default) sets non-array fields, and appends to array fields
86
+ *
87
+ * The default operation can be overridden on a per-path basis using `include`.
88
+ *
89
+ * Nested fields inherit the operation specified by their parent and falls back to the
90
+ * top level target operation if not otherwise specified.
91
+ *
92
+ * Use `include` to change the `operation` of individual fields or items.
93
+ *
94
+ * #### Appending in the middle of arrays
95
+ * `target: {path: ['array'], operation: 'append'}` will append the output of the instruction to the end of the array.
96
+ *
97
+ * To insert in the middle of the array, use `target: {path: ['array', {_key: 'appendAfterKey'}], operation: 'append'}`.
98
+ * Here, the output of the instruction will be appended after the array item with key `'appendAfterKey'`.
99
+ *
100
+ * @see #AgentActionTargetInclude.operation
101
+ * @see #include
102
+ * @see #AgentActionTargetInclude.include
103
+ */
104
+ operation?: GenerateOperation
105
+
106
+ /**
107
+ * By default, all children up to `target.maxPathDepth` are included.
108
+ *
109
+ * When `include` is specified, only segments explicitly listed will be included.
110
+ *
111
+ * Fields or array items not on the include list, are implicitly excluded.
112
+ */
113
+ include?: (AgentActionPathSegment | GenerateTargetInclude)[]
114
+ }
115
+
116
+ /**
117
+ * Instruction for an existing document.
118
+ * @beta
119
+ */
120
+ interface GenerateExistingDocumentRequest {
121
+ documentId: string
122
+ createDocument?: never
123
+ }
124
+
125
+ /**
126
+ * Instruction to create a new document
127
+ * @beta
128
+ */
129
+ interface GenerateCreateDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
130
+ createDocument: {
131
+ /** if no _id is provided, one will be generated. _id is always returned when the requests succeed */
132
+ _id?: string
133
+ _type: string
134
+ } & SanityDocumentStub<T>
135
+ documentId?: never
136
+ }
137
+
138
+ /** @beta */
139
+ export type GenerateSyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
140
+ | GenerateExistingDocumentRequest
141
+ | GenerateCreateDocumentRequest<T>
142
+ ) &
143
+ GenerateRequestBase &
144
+ AgentActionSync
145
+
146
+ /** @beta */
147
+ export type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
148
+ | GenerateExistingDocumentRequest
149
+ | GenerateCreateDocumentRequest<T>
150
+ ) &
151
+ GenerateRequestBase &
152
+ AgentActionAsync
153
+
154
+ /** @beta */
155
+ export type GenerateInstruction<T extends Record<string, Any> = Record<string, Any>> =
156
+ | GenerateSyncInstruction<T>
157
+ | GenerateAsyncInstruction<T>
158
+
8
159
  export function _generate<
9
160
  DocumentShape extends Record<string, Any>,
10
161
  Req extends GenerateInstruction<DocumentShape>,