@sanity/client 7.0.1-canary.2 → 7.2.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/README.md +516 -40
- package/dist/_chunks-cjs/config.cjs +14 -0
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-es/config.js +15 -1
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/index.browser.cjs +850 -7
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +2601 -1622
- package/dist/index.browser.d.ts +2601 -1622
- package/dist/index.browser.js +852 -7
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +839 -8
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2601 -1622
- package/dist/index.d.ts +2601 -1622
- package/dist/index.js +842 -9
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +2612 -1633
- package/dist/stega.browser.d.ts +2612 -1633
- package/dist/stega.d.cts +2612 -1633
- package/dist/stega.d.ts +2612 -1633
- package/package.json +3 -1
- package/src/SanityClient.ts +626 -1106
- package/src/agent/actions/AgentActionsClient.ts +111 -0
- package/src/agent/actions/commonTypes.ts +336 -0
- package/src/agent/actions/generate.ts +274 -0
- package/src/agent/actions/transform.ts +215 -0
- package/src/agent/actions/translate.ts +165 -0
- package/src/assets/AssetsClient.ts +2 -86
- package/src/data/dataMethods.ts +127 -3
- package/src/data/transaction.ts +1 -1
- package/src/datasets/DatasetsClient.ts +2 -64
- package/src/projects/ProjectsClient.ts +4 -46
- package/src/releases/ReleasesClient.ts +687 -0
- package/src/releases/createRelease.ts +53 -0
- package/src/types.ts +245 -1
- package/src/users/UsersClient.ts +2 -24
- package/src/util/createVersionId.ts +79 -0
- package/src/validators.ts +23 -1
- package/umd/sanityClient.js +915 -6
- package/umd/sanityClient.min.js +2 -2
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import {type Observable} from 'rxjs'
|
|
2
|
+
|
|
3
|
+
import {_request} from '../../data/dataMethods'
|
|
4
|
+
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
|
+
import type {AgentActionParams, Any, HttpRequest, IdentifiedSanityDocumentStub} from '../../types'
|
|
6
|
+
import {hasDataset} from '../../validators'
|
|
7
|
+
import type {
|
|
8
|
+
AgentActionAsync,
|
|
9
|
+
AgentActionPathSegment,
|
|
10
|
+
AgentActionRequestBase,
|
|
11
|
+
AgentActionSync,
|
|
12
|
+
AgentActionTarget,
|
|
13
|
+
AgentActionTargetInclude,
|
|
14
|
+
} from './commonTypes'
|
|
15
|
+
|
|
16
|
+
/** @beta */
|
|
17
|
+
export type GenerateOperation = 'set' | 'append' | 'mixed'
|
|
18
|
+
|
|
19
|
+
/** @beta */
|
|
20
|
+
export interface GenerateRequestBase extends AgentActionRequestBase {
|
|
21
|
+
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
22
|
+
schemaId: string
|
|
23
|
+
/**
|
|
24
|
+
* Instruct the LLM how it should generate content. Be as specific and detailed as needed.
|
|
25
|
+
*
|
|
26
|
+
* The LLM only has access to information in the instruction, plus the target schema.
|
|
27
|
+
*
|
|
28
|
+
* string template using $variable
|
|
29
|
+
* */
|
|
30
|
+
instruction: string
|
|
31
|
+
/**
|
|
32
|
+
* param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable"
|
|
33
|
+
*
|
|
34
|
+
* ### Examples
|
|
35
|
+
*
|
|
36
|
+
* #### Constant
|
|
37
|
+
*
|
|
38
|
+
* ##### Shorthand
|
|
39
|
+
* ```ts
|
|
40
|
+
* client.agent.action.generate({
|
|
41
|
+
* schemaId,
|
|
42
|
+
* documentId,
|
|
43
|
+
* instruction: 'Give the following topic:\n $topic \n ---\nGenerate the full article.',
|
|
44
|
+
* instructionParams: {
|
|
45
|
+
* topic: 'Grapefruit'
|
|
46
|
+
* },
|
|
47
|
+
* })
|
|
48
|
+
* ```
|
|
49
|
+
* ##### Object-form
|
|
50
|
+
*
|
|
51
|
+
* ```ts
|
|
52
|
+
* client.agent.action.generate({
|
|
53
|
+
* schemaId,
|
|
54
|
+
* documentId,
|
|
55
|
+
* instruction: 'Give the following topic:\n $topic \n ---\nGenerate the full article.',
|
|
56
|
+
* instructionParams: {
|
|
57
|
+
* topic: {
|
|
58
|
+
* type: 'constant',
|
|
59
|
+
* value: 'Grapefruit'
|
|
60
|
+
* },
|
|
61
|
+
* },
|
|
62
|
+
* })
|
|
63
|
+
* ```
|
|
64
|
+
* #### Field
|
|
65
|
+
* ```ts
|
|
66
|
+
* client.agent.action.generate({
|
|
67
|
+
* schemaId,
|
|
68
|
+
* documentId,
|
|
69
|
+
* instruction: 'Give the following field value:\n $pte \n ---\nGenerate keywords.',
|
|
70
|
+
* instructionParams: {
|
|
71
|
+
* pte: {
|
|
72
|
+
* type: 'field',
|
|
73
|
+
* path: ['pteField'],
|
|
74
|
+
* },
|
|
75
|
+
* },
|
|
76
|
+
* target: {path: 'keywords' }
|
|
77
|
+
* })
|
|
78
|
+
* ```
|
|
79
|
+
* #### Document
|
|
80
|
+
* ```ts
|
|
81
|
+
* client.agent.action.generate({
|
|
82
|
+
* schemaId,
|
|
83
|
+
* documentId,
|
|
84
|
+
* instruction: 'Give the following document value:\n $document \n ---\nGenerate keywords.',
|
|
85
|
+
* instructionParams: {
|
|
86
|
+
* document: {
|
|
87
|
+
* type: 'document',
|
|
88
|
+
* },
|
|
89
|
+
* },
|
|
90
|
+
* target: {path: 'keywords' }
|
|
91
|
+
* })
|
|
92
|
+
* ```
|
|
93
|
+
*
|
|
94
|
+
* #### GROQ
|
|
95
|
+
* ```ts
|
|
96
|
+
* client.agent.action.generate({
|
|
97
|
+
* schemaId,
|
|
98
|
+
* documentId,
|
|
99
|
+
* instruction: 'Give the following list of titles:\n $list \n ---\nGenerate a similar title.',
|
|
100
|
+
* instructionParams: {
|
|
101
|
+
* list: {
|
|
102
|
+
* type: 'groq',
|
|
103
|
+
* query: '* [_type==$type].title',
|
|
104
|
+
* params: {type: 'article'}
|
|
105
|
+
* },
|
|
106
|
+
* },
|
|
107
|
+
* target: {path: 'title' }
|
|
108
|
+
* })
|
|
109
|
+
* ```
|
|
110
|
+
* */
|
|
111
|
+
instructionParams?: AgentActionParams
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* Target defines which parts of the document will be affected by the instruction.
|
|
115
|
+
* It can be an array, so multiple parts of the document can be separately configured in detail.
|
|
116
|
+
*
|
|
117
|
+
* Omitting target implies that the document itself is the root.
|
|
118
|
+
*
|
|
119
|
+
* Notes:
|
|
120
|
+
* - instruction can only affect fields up to `maxPathDepth`
|
|
121
|
+
* - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
|
|
122
|
+
* It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
123
|
+
*
|
|
124
|
+
* @see AgentActionRequestBase#conditionalPaths
|
|
125
|
+
*/
|
|
126
|
+
target?: GenerateTarget | GenerateTarget[]
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/** @beta */
|
|
130
|
+
export interface GenerateTargetInclude extends AgentActionTargetInclude {
|
|
131
|
+
/**
|
|
132
|
+
* Sets the operation for this path, and all its children.
|
|
133
|
+
* This overrides any operation set parents or the root target.
|
|
134
|
+
* @see #GenerateTarget.operation
|
|
135
|
+
* @see #include
|
|
136
|
+
*/
|
|
137
|
+
operation?: GenerateOperation
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
141
|
+
*
|
|
142
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
143
|
+
*
|
|
144
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
145
|
+
*/
|
|
146
|
+
include?: (AgentActionPathSegment | GenerateTargetInclude)[]
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/** @beta */
|
|
150
|
+
export interface GenerateTarget extends AgentActionTarget {
|
|
151
|
+
/**
|
|
152
|
+
* Sets the default operation for all paths in the target.
|
|
153
|
+
* Generate runs in `'mixed'` operation mode by default:
|
|
154
|
+
* Changes are set in all non-array fields, and append to all array fields.
|
|
155
|
+
*
|
|
156
|
+
* ### Operation types
|
|
157
|
+
* - `'set'` – an *overwriting* operation, and replaces the full field value.
|
|
158
|
+
* - `'append'`:
|
|
159
|
+
* – array fields: appends new items to the end of the array,
|
|
160
|
+
* - string fields: '"existing content" "new content"'
|
|
161
|
+
* - text fields: '"existing content"\\n"new content"'
|
|
162
|
+
* - number fields: existing + new
|
|
163
|
+
* - other field types not mentioned will set instead (dates, url)
|
|
164
|
+
* - `'mixed'` – (default) sets non-array fields, and appends to array fields
|
|
165
|
+
*
|
|
166
|
+
* The default operation can be overridden on a per-path basis using `include`.
|
|
167
|
+
*
|
|
168
|
+
* Nested fields inherit the operation specified by their parent and falls back to the
|
|
169
|
+
* top level target operation if not otherwise specified.
|
|
170
|
+
*
|
|
171
|
+
* Use `include` to change the `operation` of individual fields or items.
|
|
172
|
+
*
|
|
173
|
+
* #### Appending in the middle of arrays
|
|
174
|
+
* `target: {path: ['array'], operation: 'append'}` will append the output of the instruction to the end of the array.
|
|
175
|
+
*
|
|
176
|
+
* To insert in the middle of the array, use `target: {path: ['array', {_key: 'appendAfterKey'}], operation: 'append'}`.
|
|
177
|
+
* Here, the output of the instruction will be appended after the array item with key `'appendAfterKey'`.
|
|
178
|
+
*
|
|
179
|
+
* @see #AgentActionTargetInclude.operation
|
|
180
|
+
* @see #include
|
|
181
|
+
* @see #AgentActionTargetInclude.include
|
|
182
|
+
*/
|
|
183
|
+
operation?: GenerateOperation
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
187
|
+
*
|
|
188
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
189
|
+
*
|
|
190
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
191
|
+
*/
|
|
192
|
+
include?: (AgentActionPathSegment | GenerateTargetInclude)[]
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** @beta */
|
|
196
|
+
export type GenerateTargetDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
197
|
+
| {
|
|
198
|
+
operation: 'edit'
|
|
199
|
+
_id: string
|
|
200
|
+
}
|
|
201
|
+
| {
|
|
202
|
+
operation: 'create'
|
|
203
|
+
_id?: string
|
|
204
|
+
_type: string
|
|
205
|
+
initialValues?: T
|
|
206
|
+
}
|
|
207
|
+
| {
|
|
208
|
+
operation: 'createIfNotExists'
|
|
209
|
+
_id: string
|
|
210
|
+
_type: string
|
|
211
|
+
initialValues?: T
|
|
212
|
+
}
|
|
213
|
+
| {
|
|
214
|
+
operation: 'createOrReplace'
|
|
215
|
+
_id: string
|
|
216
|
+
_type: string
|
|
217
|
+
initialValues?: T
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Instruction for an existing document.
|
|
222
|
+
* @beta
|
|
223
|
+
*/
|
|
224
|
+
interface GenerateExistingDocumentRequest {
|
|
225
|
+
documentId: string
|
|
226
|
+
createDocument?: never
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Instruction to create a new document
|
|
231
|
+
* @beta
|
|
232
|
+
*/
|
|
233
|
+
interface GenerateTargetDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
|
|
234
|
+
targetDocument: GenerateTargetDocument<T>
|
|
235
|
+
documentId?: never
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** @beta */
|
|
239
|
+
export type GenerateSyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
240
|
+
| GenerateExistingDocumentRequest
|
|
241
|
+
| GenerateTargetDocumentRequest<T>
|
|
242
|
+
) &
|
|
243
|
+
GenerateRequestBase &
|
|
244
|
+
AgentActionSync
|
|
245
|
+
|
|
246
|
+
/** @beta */
|
|
247
|
+
export type GenerateAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
248
|
+
| GenerateExistingDocumentRequest
|
|
249
|
+
| GenerateTargetDocumentRequest<T>
|
|
250
|
+
) &
|
|
251
|
+
GenerateRequestBase &
|
|
252
|
+
AgentActionAsync
|
|
253
|
+
|
|
254
|
+
/** @beta */
|
|
255
|
+
export type GenerateInstruction<T extends Record<string, Any> = Record<string, Any>> =
|
|
256
|
+
| GenerateSyncInstruction<T>
|
|
257
|
+
| GenerateAsyncInstruction<T>
|
|
258
|
+
|
|
259
|
+
export function _generate<DocumentShape extends Record<string, Any>>(
|
|
260
|
+
client: SanityClient | ObservableSanityClient,
|
|
261
|
+
httpRequest: HttpRequest,
|
|
262
|
+
request: GenerateInstruction<DocumentShape>,
|
|
263
|
+
): Observable<
|
|
264
|
+
(typeof request)['async'] extends true
|
|
265
|
+
? {_id: string}
|
|
266
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
267
|
+
> {
|
|
268
|
+
const dataset = hasDataset(client.config())
|
|
269
|
+
return _request(client, httpRequest, {
|
|
270
|
+
method: 'POST',
|
|
271
|
+
uri: `/agent/action/generate/${dataset}`,
|
|
272
|
+
body: request,
|
|
273
|
+
})
|
|
274
|
+
}
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
import {type Observable} from 'rxjs'
|
|
2
|
+
|
|
3
|
+
import {_request} from '../../data/dataMethods'
|
|
4
|
+
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
|
+
import type {AgentActionParams, Any, HttpRequest, IdentifiedSanityDocumentStub} from '../../types'
|
|
6
|
+
import {hasDataset} from '../../validators'
|
|
7
|
+
import type {
|
|
8
|
+
AgentActionAsync,
|
|
9
|
+
AgentActionPathSegment,
|
|
10
|
+
AgentActionRequestBase,
|
|
11
|
+
AgentActionSync,
|
|
12
|
+
AgentActionTarget,
|
|
13
|
+
AgentActionTargetInclude,
|
|
14
|
+
} from './commonTypes'
|
|
15
|
+
|
|
16
|
+
/** @beta */
|
|
17
|
+
export interface TransformRequestBase extends AgentActionRequestBase {
|
|
18
|
+
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
19
|
+
schemaId: string
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* The source document the transformation will use as input.
|
|
23
|
+
*/
|
|
24
|
+
documentId: string
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The source document's content is first copied to the target,
|
|
28
|
+
* then it is transformed according to the instruction.
|
|
29
|
+
*
|
|
30
|
+
* When omitted, the source document (documentId) is also the target document.
|
|
31
|
+
*/
|
|
32
|
+
targetDocument?: TransformTargetDocument
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Instruct the LLM how to transform the input to th output.
|
|
36
|
+
*
|
|
37
|
+
* String template using $variable from instructionParams.
|
|
38
|
+
*
|
|
39
|
+
* Capped to 2000 characters, after variables has been injected.
|
|
40
|
+
* */
|
|
41
|
+
instruction: string
|
|
42
|
+
/**
|
|
43
|
+
*
|
|
44
|
+
* param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable"
|
|
45
|
+
*
|
|
46
|
+
* ### Examples
|
|
47
|
+
*
|
|
48
|
+
* #### Constant
|
|
49
|
+
*
|
|
50
|
+
* ##### Shorthand
|
|
51
|
+
* ```ts
|
|
52
|
+
* client.agent.action.generate({
|
|
53
|
+
* schemaId,
|
|
54
|
+
* documentId,
|
|
55
|
+
* instruction: 'Give the following topic:\n $topic \n ---\nGenerate the full article.',
|
|
56
|
+
* instructionParams: {
|
|
57
|
+
* topic: 'Grapefruit'
|
|
58
|
+
* },
|
|
59
|
+
* })
|
|
60
|
+
* ```
|
|
61
|
+
* ##### Object-form
|
|
62
|
+
*
|
|
63
|
+
* ```ts
|
|
64
|
+
* client.agent.action.transform({
|
|
65
|
+
* schemaId,
|
|
66
|
+
* documentId,
|
|
67
|
+
* instruction: 'Give the following topic:\n $topic \n ---\nGenerate the full article.',
|
|
68
|
+
* instructionParams: {
|
|
69
|
+
* topic: {
|
|
70
|
+
* type: 'constant',
|
|
71
|
+
* value: 'Grapefruit'
|
|
72
|
+
* },
|
|
73
|
+
* },
|
|
74
|
+
* })
|
|
75
|
+
* ```
|
|
76
|
+
* #### Field
|
|
77
|
+
* ```ts
|
|
78
|
+
* client.agent.action.transform({
|
|
79
|
+
* schemaId,
|
|
80
|
+
* documentId,
|
|
81
|
+
* instruction: 'Give the following field value:\n $pte \n ---\nGenerate keywords.',
|
|
82
|
+
* instructionParams: {
|
|
83
|
+
* pte: {
|
|
84
|
+
* type: 'field',
|
|
85
|
+
* path: ['pteField'],
|
|
86
|
+
* },
|
|
87
|
+
* },
|
|
88
|
+
* target: {path: 'keywords' }
|
|
89
|
+
* })
|
|
90
|
+
* ```
|
|
91
|
+
* #### Document
|
|
92
|
+
* ```ts
|
|
93
|
+
* client.agent.action.transform({
|
|
94
|
+
* schemaId,
|
|
95
|
+
* documentId,
|
|
96
|
+
* instruction: 'Give the following document value:\n $document \n ---\nGenerate keywords.',
|
|
97
|
+
* instructionParams: {
|
|
98
|
+
* document: {
|
|
99
|
+
* type: 'document',
|
|
100
|
+
* },
|
|
101
|
+
* },
|
|
102
|
+
* target: {path: 'keywords' }
|
|
103
|
+
* })
|
|
104
|
+
* ```
|
|
105
|
+
*
|
|
106
|
+
* #### GROQ
|
|
107
|
+
* ```ts
|
|
108
|
+
* client.agent.action.transform({
|
|
109
|
+
* schemaId,
|
|
110
|
+
* documentId,
|
|
111
|
+
* instruction: 'Give the following list of titles:\n $list \n ---\nGenerate a similar title.',
|
|
112
|
+
* instructionParams: {
|
|
113
|
+
* list: {
|
|
114
|
+
* type: 'groq',
|
|
115
|
+
* query: '* [_type==$type].title',
|
|
116
|
+
* params: {type: 'article'}
|
|
117
|
+
* },
|
|
118
|
+
* },
|
|
119
|
+
* target: {path: 'title'}
|
|
120
|
+
* })
|
|
121
|
+
* ```
|
|
122
|
+
* */
|
|
123
|
+
instructionParams?: AgentActionParams
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Target defines which parts of the document will be affected by the instruction.
|
|
127
|
+
* It can be an array, so multiple parts of the document can be separately configured in detail.
|
|
128
|
+
*
|
|
129
|
+
* Omitting target implies that the document itself is the root.
|
|
130
|
+
*
|
|
131
|
+
* Notes:
|
|
132
|
+
* - instruction can only affect fields up to `maxPathDepth`
|
|
133
|
+
* - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
|
|
134
|
+
* It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
135
|
+
*
|
|
136
|
+
* Default max depth for transform: 12
|
|
137
|
+
* @see AgentActionRequestBase#conditionalPaths
|
|
138
|
+
*/
|
|
139
|
+
target?: TransformTarget | TransformTarget[]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** @beta */
|
|
143
|
+
export type TransformTargetDocument =
|
|
144
|
+
| {operation: 'edit'; _id: string}
|
|
145
|
+
| {operation: 'create'; _id?: string}
|
|
146
|
+
| {operation: 'createIfNotExists'; _id: string}
|
|
147
|
+
| {operation: 'createOrReplace'; _id: string}
|
|
148
|
+
|
|
149
|
+
/** @beta */
|
|
150
|
+
export interface TransformTargetInclude extends AgentActionTargetInclude {
|
|
151
|
+
/**
|
|
152
|
+
* Specifies a tailored instruction of this target.
|
|
153
|
+
*
|
|
154
|
+
* string template using $variable from instructionParams */
|
|
155
|
+
instruction?: string
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
159
|
+
*
|
|
160
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
161
|
+
*
|
|
162
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
163
|
+
*/
|
|
164
|
+
include?: (AgentActionPathSegment | TransformTargetInclude)[]
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/** @beta */
|
|
168
|
+
export interface TransformTarget extends AgentActionTarget {
|
|
169
|
+
/**
|
|
170
|
+
* Specifies a tailored instruction of this target.
|
|
171
|
+
*
|
|
172
|
+
* string template using $variable from instructionParams.
|
|
173
|
+
* */
|
|
174
|
+
instruction?: string
|
|
175
|
+
|
|
176
|
+
/**
|
|
177
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
178
|
+
*
|
|
179
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
180
|
+
*
|
|
181
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
182
|
+
*/
|
|
183
|
+
include?: (AgentActionPathSegment | TransformTargetInclude)[]
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/** @beta */
|
|
187
|
+
// need the generics to hold optional call-site response generics
|
|
188
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
189
|
+
export type TransformDocumentSync<T extends Record<string, Any> = Record<string, Any>> =
|
|
190
|
+
TransformRequestBase & AgentActionSync
|
|
191
|
+
|
|
192
|
+
/** @beta */
|
|
193
|
+
export type TransformDocumentAsync = TransformRequestBase & AgentActionAsync
|
|
194
|
+
|
|
195
|
+
/** @beta */
|
|
196
|
+
export type TransformDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
197
|
+
| TransformDocumentSync<T>
|
|
198
|
+
| TransformDocumentAsync
|
|
199
|
+
|
|
200
|
+
export function _transform<DocumentShape extends Record<string, Any>>(
|
|
201
|
+
client: SanityClient | ObservableSanityClient,
|
|
202
|
+
httpRequest: HttpRequest,
|
|
203
|
+
request: TransformDocument<DocumentShape>,
|
|
204
|
+
): Observable<
|
|
205
|
+
(typeof request)['async'] extends true
|
|
206
|
+
? {_id: string}
|
|
207
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
208
|
+
> {
|
|
209
|
+
const dataset = hasDataset(client.config())
|
|
210
|
+
return _request(client, httpRequest, {
|
|
211
|
+
method: 'POST',
|
|
212
|
+
uri: `/agent/action/transform/${dataset}`,
|
|
213
|
+
body: request,
|
|
214
|
+
})
|
|
215
|
+
}
|
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import {type Observable} from 'rxjs'
|
|
2
|
+
|
|
3
|
+
import {_request} from '../../data/dataMethods'
|
|
4
|
+
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
|
+
import type {
|
|
6
|
+
AgentActionParams,
|
|
7
|
+
AgentActionPathSegment,
|
|
8
|
+
AgentActionTarget,
|
|
9
|
+
Any,
|
|
10
|
+
HttpRequest,
|
|
11
|
+
IdentifiedSanityDocumentStub,
|
|
12
|
+
} from '../../types'
|
|
13
|
+
import {hasDataset} from '../../validators'
|
|
14
|
+
import type {
|
|
15
|
+
AgentActionAsync,
|
|
16
|
+
AgentActionPath,
|
|
17
|
+
AgentActionRequestBase,
|
|
18
|
+
AgentActionSync,
|
|
19
|
+
AgentActionTargetInclude,
|
|
20
|
+
} from './commonTypes'
|
|
21
|
+
import type {TransformTargetDocument} from './transform'
|
|
22
|
+
|
|
23
|
+
/** @beta */
|
|
24
|
+
export interface TranslateRequestBase extends AgentActionRequestBase {
|
|
25
|
+
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
26
|
+
schemaId: string
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* The source document the transformation will use as input.
|
|
30
|
+
*/
|
|
31
|
+
documentId: string
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The target document will first get content copied over from the source,
|
|
35
|
+
* then it is translated according to the instruction.
|
|
36
|
+
*
|
|
37
|
+
* When omitted, the source document (documentId) is also the target document.
|
|
38
|
+
*/
|
|
39
|
+
targetDocument?: TransformTargetDocument
|
|
40
|
+
|
|
41
|
+
/**
|
|
42
|
+
* While optional, it is recommended
|
|
43
|
+
*/
|
|
44
|
+
fromLanguage?: TranslateLanguage
|
|
45
|
+
toLanguage: TranslateLanguage
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* `styleGuide` can be used to tailor how the translation should be preformed.
|
|
49
|
+
*
|
|
50
|
+
* String template using $variable from styleGuideParams.
|
|
51
|
+
*
|
|
52
|
+
* Capped to 2000 characters, after variables has been injected.
|
|
53
|
+
*
|
|
54
|
+
* @see #protectedPhrases
|
|
55
|
+
*/
|
|
56
|
+
styleGuide?: string
|
|
57
|
+
/** param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable" */
|
|
58
|
+
styleGuideParams?: AgentActionParams
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* When the input string contains any phrase from `protectedPhrases`, the LLM will be instructed not
|
|
62
|
+
* to translate them.
|
|
63
|
+
*
|
|
64
|
+
* It is recommended to use `protectedPhrases` instead of `styleGuide` for deny-list words and phrases,
|
|
65
|
+
* since it keeps token cost low, resulting in faster responses, and limits how much information the LLM
|
|
66
|
+
* has to process, since only phrases that are actually in the input string will be included in the final prompt.
|
|
67
|
+
*/
|
|
68
|
+
protectedPhrases?: string[]
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* When specified, the `toLanguage.id` will be stored in the specified path in the target document.
|
|
72
|
+
*
|
|
73
|
+
* The file _can_ be hidden: true (unlike other fields in the target, which will be ignored)
|
|
74
|
+
*/
|
|
75
|
+
languageFieldPath?: AgentActionPathSegment | AgentActionPath
|
|
76
|
+
|
|
77
|
+
/**
|
|
78
|
+
* Target defines which parts of the document will be affected by the instruction.
|
|
79
|
+
* It can be an array, so multiple parts of the document can be separately configured in detail.
|
|
80
|
+
*
|
|
81
|
+
* Omitting target implies that the document itself is the root.
|
|
82
|
+
*
|
|
83
|
+
* Notes:
|
|
84
|
+
* - instruction can only affect fields up to `maxPathDepth`
|
|
85
|
+
* - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
|
|
86
|
+
* It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
87
|
+
*
|
|
88
|
+
* @see AgentActionRequestBase#conditionalPaths
|
|
89
|
+
*/
|
|
90
|
+
target?: TranslateTarget | TranslateTarget[]
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/** @beta */
|
|
94
|
+
export interface TranslateLanguage {
|
|
95
|
+
/**
|
|
96
|
+
* Language code
|
|
97
|
+
*/
|
|
98
|
+
id: string
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* While optional, it is recommended to provide a language title
|
|
102
|
+
*/
|
|
103
|
+
title?: string
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** @beta */
|
|
107
|
+
export interface TranslateTargetInclude extends AgentActionTargetInclude {
|
|
108
|
+
/** string template using $variable from instructionParams */
|
|
109
|
+
styleGuide?: string
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
113
|
+
*
|
|
114
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
115
|
+
*
|
|
116
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
117
|
+
*/
|
|
118
|
+
include?: (AgentActionPathSegment | TranslateTargetInclude)[]
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/** @beta */
|
|
122
|
+
export interface TranslateTarget extends AgentActionTarget {
|
|
123
|
+
/** string template using $variable from instructionParams */
|
|
124
|
+
styleGuide?: string
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* By default, all children up to `target.maxPathDepth` are included.
|
|
128
|
+
*
|
|
129
|
+
* When `include` is specified, only segments explicitly listed will be included.
|
|
130
|
+
*
|
|
131
|
+
* Fields or array items not on the include list, are implicitly excluded.
|
|
132
|
+
*/
|
|
133
|
+
include?: (AgentActionPathSegment | TranslateTargetInclude)[]
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** @beta */
|
|
137
|
+
// need the generics to hold optional call-site response generics
|
|
138
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
139
|
+
export type TranslateDocumentSync<T extends Record<string, Any> = Record<string, Any>> =
|
|
140
|
+
TranslateRequestBase & AgentActionSync
|
|
141
|
+
|
|
142
|
+
/** @beta */
|
|
143
|
+
export type TranslateDocumentAsync = TranslateRequestBase & AgentActionAsync
|
|
144
|
+
|
|
145
|
+
/** @beta */
|
|
146
|
+
export type TranslateDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
147
|
+
| TranslateDocumentSync<T>
|
|
148
|
+
| TranslateDocumentAsync
|
|
149
|
+
|
|
150
|
+
export function _translate<DocumentShape extends Record<string, Any>>(
|
|
151
|
+
client: SanityClient | ObservableSanityClient,
|
|
152
|
+
httpRequest: HttpRequest,
|
|
153
|
+
request: TranslateDocument<DocumentShape>,
|
|
154
|
+
): Observable<
|
|
155
|
+
(typeof request)['async'] extends true
|
|
156
|
+
? {_id: string}
|
|
157
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
158
|
+
> {
|
|
159
|
+
const dataset = hasDataset(client.config())
|
|
160
|
+
return _request(client, httpRequest, {
|
|
161
|
+
method: 'POST',
|
|
162
|
+
uri: `/agent/action/translate/${dataset}`,
|
|
163
|
+
body: request,
|
|
164
|
+
})
|
|
165
|
+
}
|