@sanity/client 7.3.0 → 7.4.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 +122 -1
- package/dist/_chunks-cjs/isRecord.cjs +6 -0
- package/dist/_chunks-cjs/isRecord.cjs.map +1 -0
- package/dist/_chunks-cjs/resolveEditInfo.cjs +3 -5
- package/dist/_chunks-cjs/resolveEditInfo.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaClean.cjs +4 -0
- package/dist/_chunks-cjs/stegaClean.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +2 -5
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
- package/dist/_chunks-es/isRecord.js +7 -0
- package/dist/_chunks-es/isRecord.js.map +1 -0
- package/dist/_chunks-es/resolveEditInfo.js +1 -3
- package/dist/_chunks-es/resolveEditInfo.js.map +1 -1
- package/dist/_chunks-es/stegaClean.js +4 -0
- package/dist/_chunks-es/stegaClean.js.map +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +1 -4
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
- package/dist/index.browser.cjs +155 -32
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +473 -68
- package/dist/index.browser.d.ts +473 -68
- package/dist/index.browser.js +156 -33
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +157 -34
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +473 -68
- package/dist/index.d.ts +473 -68
- package/dist/index.js +157 -33
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +473 -68
- package/dist/stega.browser.d.ts +473 -68
- package/dist/stega.d.cts +473 -68
- package/dist/stega.d.ts +473 -68
- package/package.json +1 -1
- package/src/agent/actions/AgentActionsClient.ts +29 -2
- package/src/agent/actions/commonTypes.ts +57 -17
- package/src/agent/actions/generate.ts +36 -2
- package/src/agent/actions/patch.ts +136 -0
- package/src/agent/actions/prompt.ts +145 -0
- package/src/agent/actions/transform.ts +27 -4
- package/src/agent/actions/translate.ts +5 -2
- package/src/csm/walkMap.ts +1 -1
- package/src/data/eventsource.ts +16 -7
- package/src/data/listen.ts +10 -4
- package/src/data/live.ts +13 -5
- package/src/defineCreateClient.ts +7 -1
- package/src/http/errors.ts +92 -27
- package/src/http/request.ts +3 -3
- package/src/types.ts +25 -10
- package/src/util/codeFrame.ts +174 -0
- package/src/{csm → util}/isRecord.ts +1 -1
- package/umd/sanityClient.js +158 -35
- package/umd/sanityClient.min.js +2 -2
package/package.json
CHANGED
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import {lastValueFrom, type Observable} from 'rxjs'
|
|
2
2
|
|
|
3
3
|
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
4
|
-
import type {Any, HttpRequest, IdentifiedSanityDocumentStub
|
|
4
|
+
import type {Any, HttpRequest, IdentifiedSanityDocumentStub} from '../../types'
|
|
5
5
|
import {_generate, type GenerateInstruction} from './generate'
|
|
6
|
+
import {_patch, type PatchDocument} from './patch'
|
|
7
|
+
import {_prompt, type PromptRequest} from './prompt'
|
|
6
8
|
import {_transform, type TransformDocument} from './transform'
|
|
7
|
-
import {_translate} from './translate'
|
|
9
|
+
import {_translate, type TranslateDocument} from './translate'
|
|
8
10
|
|
|
9
11
|
/** @public */
|
|
10
12
|
export class ObservableAgentsActionClient {
|
|
@@ -108,4 +110,29 @@ export class AgentActionsClient {
|
|
|
108
110
|
> {
|
|
109
111
|
return lastValueFrom(_translate(this.#client, this.#httpRequest, request))
|
|
110
112
|
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Run a raw instruction and return the result either as text or json
|
|
116
|
+
* @param request - prompt request
|
|
117
|
+
*/
|
|
118
|
+
prompt<const DocumentShape extends Record<string, Any>>(
|
|
119
|
+
request: PromptRequest<DocumentShape>,
|
|
120
|
+
): Promise<(typeof request)['format'] extends 'json' ? DocumentShape : string> {
|
|
121
|
+
return lastValueFrom(_prompt(this.#client, this.#httpRequest, request))
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/**
|
|
125
|
+
* Patch a document using a schema aware API.
|
|
126
|
+
* Does not use an LLM, but uses the schema to ensure paths and values matches the schema.
|
|
127
|
+
* @param request - instruction request
|
|
128
|
+
*/
|
|
129
|
+
patch<DocumentShape extends Record<string, Any>>(
|
|
130
|
+
request: PatchDocument<DocumentShape>,
|
|
131
|
+
): Promise<
|
|
132
|
+
(typeof request)['async'] extends true
|
|
133
|
+
? {_id: string}
|
|
134
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
135
|
+
> {
|
|
136
|
+
return lastValueFrom(_patch(this.#client, this.#httpRequest, request))
|
|
137
|
+
}
|
|
111
138
|
}
|
|
@@ -35,6 +35,16 @@ export interface ConstantAgentActionParam {
|
|
|
35
35
|
value: string
|
|
36
36
|
}
|
|
37
37
|
|
|
38
|
+
type DocIdParam<TParamConfig extends {docIdRequired: boolean} = {docIdRequired: false}> =
|
|
39
|
+
TParamConfig['docIdRequired'] extends true
|
|
40
|
+
? {documentId: string}
|
|
41
|
+
: {
|
|
42
|
+
/**
|
|
43
|
+
* If omitted, implicitly uses the documentId of the instruction target
|
|
44
|
+
*/
|
|
45
|
+
documentId?: string
|
|
46
|
+
}
|
|
47
|
+
|
|
38
48
|
/**
|
|
39
49
|
*
|
|
40
50
|
*
|
|
@@ -58,17 +68,15 @@ export interface ConstantAgentActionParam {
|
|
|
58
68
|
*
|
|
59
69
|
* @beta
|
|
60
70
|
* */
|
|
61
|
-
export
|
|
71
|
+
export type FieldAgentActionParam<
|
|
72
|
+
TParamConfig extends {docIdRequired: boolean} = {docIdRequired: false},
|
|
73
|
+
> = {
|
|
62
74
|
type: 'field'
|
|
63
75
|
/**
|
|
64
76
|
* Examples: 'title', ['array', \{_key: 'arrayItemKey'\}, 'field']
|
|
65
77
|
*/
|
|
66
78
|
path: AgentActionPathSegment | AgentActionPath
|
|
67
|
-
|
|
68
|
-
* If omitted, implicitly uses the documentId of the instruction target
|
|
69
|
-
*/
|
|
70
|
-
documentId?: string
|
|
71
|
-
}
|
|
79
|
+
} & DocIdParam<TParamConfig>
|
|
72
80
|
|
|
73
81
|
/**
|
|
74
82
|
*
|
|
@@ -90,13 +98,11 @@ export interface FieldAgentActionParam {
|
|
|
90
98
|
*
|
|
91
99
|
* @beta
|
|
92
100
|
* */
|
|
93
|
-
export
|
|
101
|
+
export type DocumentAgentActionParam<
|
|
102
|
+
TParamConfig extends {docIdRequired: boolean} = {docIdRequired: false},
|
|
103
|
+
> = {
|
|
94
104
|
type: 'document'
|
|
95
|
-
|
|
96
|
-
* If omitted, implicitly uses the documentId of the instruction target
|
|
97
|
-
*/
|
|
98
|
-
documentId?: string
|
|
99
|
-
}
|
|
105
|
+
} & DocIdParam<TParamConfig>
|
|
100
106
|
|
|
101
107
|
/**
|
|
102
108
|
* Includes a LLM-friendly version of GROQ query result in the instruction
|
|
@@ -209,21 +215,52 @@ export interface AgentActionTarget {
|
|
|
209
215
|
}
|
|
210
216
|
|
|
211
217
|
/** @beta */
|
|
212
|
-
export type AgentActionParam
|
|
218
|
+
export type AgentActionParam<
|
|
219
|
+
TParamConfig extends {docIdRequired: boolean} = {docIdRequired: false},
|
|
220
|
+
> =
|
|
213
221
|
| string
|
|
214
222
|
| ConstantAgentActionParam
|
|
215
|
-
| FieldAgentActionParam
|
|
216
|
-
| DocumentAgentActionParam
|
|
223
|
+
| FieldAgentActionParam<TParamConfig>
|
|
224
|
+
| DocumentAgentActionParam<TParamConfig>
|
|
217
225
|
| GroqAgentActionParam
|
|
218
226
|
|
|
219
227
|
/** @beta */
|
|
220
|
-
export type AgentActionParams
|
|
228
|
+
export type AgentActionParams<
|
|
229
|
+
TParamConfig extends {docIdRequired: boolean} = {docIdRequired: false},
|
|
230
|
+
> = Record<string, AgentActionParam<TParamConfig>>
|
|
221
231
|
|
|
222
232
|
/** @beta */
|
|
223
|
-
export interface
|
|
233
|
+
export interface AgentActionSchema {
|
|
224
234
|
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
225
235
|
schemaId: string
|
|
226
236
|
|
|
237
|
+
/**
|
|
238
|
+
* ### forcePublishedWrite: false (default)
|
|
239
|
+
* By default, agent actions will never write to a published document.
|
|
240
|
+
*
|
|
241
|
+
* Instead, they will force the use of a draft ID ("drafts.some-id") instead of the published ID ("some-id"),
|
|
242
|
+
* even when a published ID is provided.
|
|
243
|
+
*
|
|
244
|
+
* Actions will use state from an existing draft if it exists,
|
|
245
|
+
* or use the published document to create a draft, if no draft exists.
|
|
246
|
+
*
|
|
247
|
+
* Successful responses contains the _id that was mutated by the action.
|
|
248
|
+
*
|
|
249
|
+
*
|
|
250
|
+
* ### forcePublishedWrite: true
|
|
251
|
+
*
|
|
252
|
+
* When forcePublishedWrite: true an agent action will write to the exact id provided.
|
|
253
|
+
* The action will also not fallback to published state for draft ids.
|
|
254
|
+
*
|
|
255
|
+
*
|
|
256
|
+
* ### Versioned ids (releases)
|
|
257
|
+
*
|
|
258
|
+
* When an ID on the form "versions.<release>.some-id" is provided, agent actions will
|
|
259
|
+
* always behave as if `forcePublishedWrite: true`.
|
|
260
|
+
* That is, only the exact document state of the id provided is considered and mutated.
|
|
261
|
+
* */
|
|
262
|
+
forcePublishedWrite?: boolean
|
|
263
|
+
|
|
227
264
|
/**
|
|
228
265
|
* When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
|
|
229
266
|
*
|
|
@@ -254,7 +291,10 @@ export interface AgentActionRequestBase {
|
|
|
254
291
|
hidden: boolean
|
|
255
292
|
}[]
|
|
256
293
|
}
|
|
294
|
+
}
|
|
257
295
|
|
|
296
|
+
/** @beta */
|
|
297
|
+
export interface AgentActionRequestBase extends AgentActionSchema {
|
|
258
298
|
/**
|
|
259
299
|
* When localeSettings is provided on the request, instruct can write to date and datetime fields.
|
|
260
300
|
* Otherwise, such fields will be ignored.
|
|
@@ -25,7 +25,7 @@ export interface GenerateRequestBase extends AgentActionRequestBase {
|
|
|
25
25
|
*
|
|
26
26
|
* The LLM only has access to information in the instruction, plus the target schema.
|
|
27
27
|
*
|
|
28
|
-
*
|
|
28
|
+
* String template with support for $variable from `instructionParams`.
|
|
29
29
|
* */
|
|
30
30
|
instruction: string
|
|
31
31
|
/**
|
|
@@ -121,6 +121,21 @@ export interface GenerateRequestBase extends AgentActionRequestBase {
|
|
|
121
121
|
* - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
|
|
122
122
|
* It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
123
123
|
*
|
|
124
|
+
* ## Generating images
|
|
125
|
+
*
|
|
126
|
+
* Generate will generate images the same was as AI Assist, for images that have been configured using
|
|
127
|
+
* [AI Assist schema options](https://github.com/sanity-io/assist/tree/main/plugin#image-generation).
|
|
128
|
+
*
|
|
129
|
+
* To generate images _without_ changing the schema, directly target an image asset path.
|
|
130
|
+
*
|
|
131
|
+
* For example, all the following will generate an image into the provided asset:
|
|
132
|
+
* * `target: {path: ['image', 'asset'] }`
|
|
133
|
+
* * `target: {path: 'image', include: ['asset'] }`
|
|
134
|
+
*
|
|
135
|
+
* Image generation can be combined with regular content targets:
|
|
136
|
+
* * `target: [{path: ['image', 'asset'] }, {include: ['title', 'description']}]`
|
|
137
|
+
*
|
|
138
|
+
* Since Generate happens in a single LLM pass, the image will be contextually related to other generated content.
|
|
124
139
|
* @see AgentActionRequestBase#conditionalPaths
|
|
125
140
|
*/
|
|
126
141
|
target?: GenerateTarget | GenerateTarget[]
|
|
@@ -179,6 +194,7 @@ export interface GenerateTarget extends AgentActionTarget {
|
|
|
179
194
|
* @see #AgentActionTargetInclude.operation
|
|
180
195
|
* @see #include
|
|
181
196
|
* @see #AgentActionTargetInclude.include
|
|
197
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
182
198
|
*/
|
|
183
199
|
operation?: GenerateOperation
|
|
184
200
|
|
|
@@ -196,22 +212,34 @@ export interface GenerateTarget extends AgentActionTarget {
|
|
|
196
212
|
export type GenerateTargetDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
197
213
|
| {
|
|
198
214
|
operation: 'edit'
|
|
215
|
+
/**
|
|
216
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
217
|
+
*/
|
|
199
218
|
_id: string
|
|
200
219
|
}
|
|
201
220
|
| {
|
|
202
221
|
operation: 'create'
|
|
222
|
+
/**
|
|
223
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
224
|
+
*/
|
|
203
225
|
_id?: string
|
|
204
226
|
_type: string
|
|
205
227
|
initialValues?: T
|
|
206
228
|
}
|
|
207
229
|
| {
|
|
208
230
|
operation: 'createIfNotExists'
|
|
231
|
+
/**
|
|
232
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
233
|
+
*/
|
|
209
234
|
_id: string
|
|
210
235
|
_type: string
|
|
211
236
|
initialValues?: T
|
|
212
237
|
}
|
|
213
238
|
| {
|
|
214
239
|
operation: 'createOrReplace'
|
|
240
|
+
/**
|
|
241
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
242
|
+
*/
|
|
215
243
|
_id: string
|
|
216
244
|
_type: string
|
|
217
245
|
initialValues?: T
|
|
@@ -222,8 +250,11 @@ export type GenerateTargetDocument<T extends Record<string, Any> = Record<string
|
|
|
222
250
|
* @beta
|
|
223
251
|
*/
|
|
224
252
|
interface GenerateExistingDocumentRequest {
|
|
253
|
+
/**
|
|
254
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
255
|
+
*/
|
|
225
256
|
documentId: string
|
|
226
|
-
|
|
257
|
+
targetDocument?: never
|
|
227
258
|
}
|
|
228
259
|
|
|
229
260
|
/**
|
|
@@ -231,6 +262,9 @@ interface GenerateExistingDocumentRequest {
|
|
|
231
262
|
* @beta
|
|
232
263
|
*/
|
|
233
264
|
interface GenerateTargetDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
|
|
265
|
+
/**
|
|
266
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
267
|
+
*/
|
|
234
268
|
targetDocument: GenerateTargetDocument<T>
|
|
235
269
|
documentId?: never
|
|
236
270
|
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
import {type Observable} from 'rxjs'
|
|
2
|
+
|
|
3
|
+
import {_request} from '../../data/dataMethods'
|
|
4
|
+
import type {ObservableSanityClient, SanityClient} from '../../SanityClient'
|
|
5
|
+
import type {
|
|
6
|
+
AgentActionPath,
|
|
7
|
+
AgentActionPathSegment,
|
|
8
|
+
Any,
|
|
9
|
+
GenerateTargetDocument,
|
|
10
|
+
HttpRequest,
|
|
11
|
+
IdentifiedSanityDocumentStub,
|
|
12
|
+
} from '../../types'
|
|
13
|
+
import {hasDataset} from '../../validators'
|
|
14
|
+
import type {AgentActionAsync, AgentActionSchema, AgentActionSync} from './commonTypes'
|
|
15
|
+
|
|
16
|
+
/** @beta */
|
|
17
|
+
export type PatchOperation = 'set' | 'append' | 'mixed' | 'unset'
|
|
18
|
+
|
|
19
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
20
|
+
type AnyNonNullable = Exclude<any, null | undefined>
|
|
21
|
+
|
|
22
|
+
/** @beta */
|
|
23
|
+
export interface PatchRequestBase extends AgentActionSchema {
|
|
24
|
+
/**
|
|
25
|
+
* Target defines which parts of the document will be affected by the instruction.
|
|
26
|
+
* It can be an array, so multiple parts of the document can be separately configured in detail.
|
|
27
|
+
*
|
|
28
|
+
* Omitting target implies that the document itself is the root.
|
|
29
|
+
*
|
|
30
|
+
* Notes:
|
|
31
|
+
* - instruction can only affect fields up to `maxPathDepth`
|
|
32
|
+
* - when multiple targets are provided, they will be coalesced into a single target sharing a common target root.
|
|
33
|
+
* It is therefore an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
34
|
+
*
|
|
35
|
+
* @see AgentActionRequestBase#conditionalPaths
|
|
36
|
+
*/
|
|
37
|
+
target: PatchTarget | PatchTarget[]
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** @beta */
|
|
41
|
+
export type PatchTarget = {
|
|
42
|
+
/**
|
|
43
|
+
* Determines how the target path will be patched.
|
|
44
|
+
*
|
|
45
|
+
* ### Operation types
|
|
46
|
+
* - `'set'` – an *overwriting* operation: sets the full field value for primitive targets, and merges the provided value with existing values for objects
|
|
47
|
+
* - `'append'`:
|
|
48
|
+
* – array fields: appends new items to the end of the array,
|
|
49
|
+
* - string fields: '"existing content" "new content"'
|
|
50
|
+
* - text fields: '"existing content"\\n"new content"'
|
|
51
|
+
* - number fields: existing + new
|
|
52
|
+
* - other field types not mentioned will set instead (dates, url)
|
|
53
|
+
* - `'mixed'` – sets non-array fields, and appends to array fields
|
|
54
|
+
* - `'unset'` – removes whatever value is on the target path
|
|
55
|
+
*
|
|
56
|
+
* All operations except unset requires a `value`.
|
|
57
|
+
*
|
|
58
|
+
* #### Appending in the middle of arrays
|
|
59
|
+
* To append to an array, use the 'append' operation, and provide an array value with one or more array items.
|
|
60
|
+
*
|
|
61
|
+
* `target: {path: ['array'], operation: 'append', value: [{_type: 'item' _key: 'a'}]}` will append the items in the value to the existing array.
|
|
62
|
+
*
|
|
63
|
+
* To insert in the middle of the array, use `target: {path: ['array', {_key: 'appendAfterKey'}], operation: 'append', value: [{_type: 'item' _key: 'a'}]}`.
|
|
64
|
+
* Here, `{_type: 'item' _key: 'a'}` will be appended after the array item with key `'appendAfterKey'`
|
|
65
|
+
*
|
|
66
|
+
* It is optional to provide a _key for inserted array items; if one isn't provided, it will be generated.
|
|
67
|
+
*/
|
|
68
|
+
operation: PatchOperation
|
|
69
|
+
|
|
70
|
+
path: AgentActionPathSegment | AgentActionPath
|
|
71
|
+
} & (
|
|
72
|
+
| {operation: 'unset'; value?: never}
|
|
73
|
+
| {operation: Exclude<PatchOperation, 'unset'>; value: AnyNonNullable}
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Patches an existing document
|
|
78
|
+
* @beta
|
|
79
|
+
*/
|
|
80
|
+
interface PatchExistingDocumentRequest {
|
|
81
|
+
/**
|
|
82
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
83
|
+
*/
|
|
84
|
+
documentId: string
|
|
85
|
+
targetDocument?: never
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Create a new document, then patch it
|
|
90
|
+
* @beta
|
|
91
|
+
*/
|
|
92
|
+
interface PatchTargetDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
|
|
93
|
+
/**
|
|
94
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
95
|
+
*/
|
|
96
|
+
targetDocument: GenerateTargetDocument<T>
|
|
97
|
+
documentId?: never
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/** @beta */
|
|
101
|
+
export type PatchDocumentSync<T extends Record<string, Any> = Record<string, Any>> = (
|
|
102
|
+
| PatchExistingDocumentRequest
|
|
103
|
+
| PatchTargetDocumentRequest<T>
|
|
104
|
+
) &
|
|
105
|
+
PatchRequestBase &
|
|
106
|
+
AgentActionSync
|
|
107
|
+
|
|
108
|
+
/** @beta */
|
|
109
|
+
export type PatchDocumentAsync<T extends Record<string, Any> = Record<string, Any>> = (
|
|
110
|
+
| PatchExistingDocumentRequest
|
|
111
|
+
| PatchTargetDocumentRequest<T>
|
|
112
|
+
) &
|
|
113
|
+
PatchRequestBase &
|
|
114
|
+
AgentActionAsync
|
|
115
|
+
|
|
116
|
+
/** @beta */
|
|
117
|
+
export type PatchDocument<T extends Record<string, Any> = Record<string, Any>> =
|
|
118
|
+
| PatchDocumentSync<T>
|
|
119
|
+
| PatchDocumentAsync<T>
|
|
120
|
+
|
|
121
|
+
export function _patch<DocumentShape extends Record<string, Any>>(
|
|
122
|
+
client: SanityClient | ObservableSanityClient,
|
|
123
|
+
httpRequest: HttpRequest,
|
|
124
|
+
request: PatchDocument<DocumentShape>,
|
|
125
|
+
): Observable<
|
|
126
|
+
(typeof request)['async'] extends true
|
|
127
|
+
? {_id: string}
|
|
128
|
+
: IdentifiedSanityDocumentStub & DocumentShape
|
|
129
|
+
> {
|
|
130
|
+
const dataset = hasDataset(client.config())
|
|
131
|
+
return _request(client, httpRequest, {
|
|
132
|
+
method: 'POST',
|
|
133
|
+
uri: `/agent/action/patch/${dataset}`,
|
|
134
|
+
body: request,
|
|
135
|
+
})
|
|
136
|
+
}
|
|
@@ -0,0 +1,145 @@
|
|
|
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} from '../../types'
|
|
6
|
+
import {hasDataset} from '../../validators'
|
|
7
|
+
|
|
8
|
+
/** @beta */
|
|
9
|
+
export interface PromptRequestBase {
|
|
10
|
+
/**
|
|
11
|
+
* Instruct the LLM how it should generate content. Be as specific and detailed as needed.
|
|
12
|
+
*
|
|
13
|
+
* The LLM only has access to information in the instruction, plus the target schema.
|
|
14
|
+
*
|
|
15
|
+
* String template with support for $variable from `instructionParams`.
|
|
16
|
+
* */
|
|
17
|
+
instruction: string
|
|
18
|
+
/**
|
|
19
|
+
* param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable"
|
|
20
|
+
*
|
|
21
|
+
* ### Examples
|
|
22
|
+
*
|
|
23
|
+
* #### Constant
|
|
24
|
+
*
|
|
25
|
+
* ##### Shorthand
|
|
26
|
+
* ```ts
|
|
27
|
+
* client.agent.action.prompt({
|
|
28
|
+
* instruction: 'Give the following topic:\n $topic \n ---\nReturns some facts about it',
|
|
29
|
+
* instructionParams: {
|
|
30
|
+
* topic: 'Grapefruit'
|
|
31
|
+
* },
|
|
32
|
+
* })
|
|
33
|
+
* ```
|
|
34
|
+
* ##### Object-form
|
|
35
|
+
*
|
|
36
|
+
* ```ts
|
|
37
|
+
* client.agent.action.prompt({
|
|
38
|
+
* instruction: 'Give the following topic:\n $topic \n ---\nReturns some facts about it.',
|
|
39
|
+
* instructionParams: {
|
|
40
|
+
* topic: {
|
|
41
|
+
* type: 'constant',
|
|
42
|
+
* value: 'Grapefruit'
|
|
43
|
+
* },
|
|
44
|
+
* },
|
|
45
|
+
* })
|
|
46
|
+
* ```
|
|
47
|
+
* #### Field
|
|
48
|
+
* ```ts
|
|
49
|
+
* client.agent.action.prompt({
|
|
50
|
+
* instruction: 'Give the following field value:\n $pte \n ---\nGenerate keywords.',
|
|
51
|
+
* instructionParams: {
|
|
52
|
+
* pte: {
|
|
53
|
+
* type: 'field',
|
|
54
|
+
* path: ['pteField'],
|
|
55
|
+
* documentId: 'someSanityDocId'
|
|
56
|
+
* },
|
|
57
|
+
* },
|
|
58
|
+
* })
|
|
59
|
+
* ```
|
|
60
|
+
* #### Document
|
|
61
|
+
* ```ts
|
|
62
|
+
* client.agent.action.prompt({
|
|
63
|
+
* json: true,
|
|
64
|
+
* instruction: 'Given the following document:$document\nCreate a JSON string[] array with keywords describing it.',
|
|
65
|
+
* instructionParams: {
|
|
66
|
+
* document: {
|
|
67
|
+
* type: 'document',
|
|
68
|
+
* documentId: 'someSanityDocId'
|
|
69
|
+
* },
|
|
70
|
+
* },
|
|
71
|
+
* })
|
|
72
|
+
* ```
|
|
73
|
+
*
|
|
74
|
+
* #### GROQ
|
|
75
|
+
* ```ts
|
|
76
|
+
* client.agent.action.prompt({
|
|
77
|
+
* instruction: 'Return the best title amongst these: $titles.',
|
|
78
|
+
* instructionParams: {
|
|
79
|
+
* titles: {
|
|
80
|
+
* type: 'groq',
|
|
81
|
+
* query: '* [_type==$type].title',
|
|
82
|
+
* params: {type: 'article'}
|
|
83
|
+
* },
|
|
84
|
+
* },
|
|
85
|
+
* })
|
|
86
|
+
* ```
|
|
87
|
+
* */
|
|
88
|
+
instructionParams?: AgentActionParams<{docIdRequired: true}>
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Controls how much variance the instructions will run with.
|
|
92
|
+
*
|
|
93
|
+
* Value must be in the range [0, 1] (inclusive).
|
|
94
|
+
*
|
|
95
|
+
* Default: 0.3
|
|
96
|
+
*/
|
|
97
|
+
temperature?: number
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @beta
|
|
102
|
+
*/
|
|
103
|
+
// need the unused generic here to allow for optional callsite casting
|
|
104
|
+
// eslint-disable-next-line unused-imports/no-unused-vars
|
|
105
|
+
interface PromptJsonResponse<T extends Record<string, Any> = Record<string, Any>> {
|
|
106
|
+
/**
|
|
107
|
+
*
|
|
108
|
+
* When true, the response body will be json according to the instruction.
|
|
109
|
+
* When false, the response is the raw text response to the instruction.
|
|
110
|
+
*
|
|
111
|
+
* Note: In addition to setting this to true, `instruction` MUST include the word 'JSON', or 'json' for this to work.
|
|
112
|
+
*/
|
|
113
|
+
format: 'json'
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
interface PromptTextResponse {
|
|
117
|
+
/**
|
|
118
|
+
*
|
|
119
|
+
* When true, the response body will be json according to the instruction.
|
|
120
|
+
* When false, the response is the raw text response to the instruction.
|
|
121
|
+
*
|
|
122
|
+
* Note: In addition to setting this to true, `instruction` MUST include the word 'JSON', or 'json' for this to work.
|
|
123
|
+
*/
|
|
124
|
+
format?: 'text'
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
/** @beta */
|
|
128
|
+
export type PromptRequest<T extends Record<string, Any> = Record<string, Any>> = (
|
|
129
|
+
| PromptTextResponse
|
|
130
|
+
| PromptJsonResponse<T>
|
|
131
|
+
) &
|
|
132
|
+
PromptRequestBase
|
|
133
|
+
|
|
134
|
+
export function _prompt<const DocumentShape extends Record<string, Any>>(
|
|
135
|
+
client: SanityClient | ObservableSanityClient,
|
|
136
|
+
httpRequest: HttpRequest,
|
|
137
|
+
request: PromptRequest<DocumentShape>,
|
|
138
|
+
): Observable<(typeof request)['format'] extends 'json' ? DocumentShape : string> {
|
|
139
|
+
const dataset = hasDataset(client.config())
|
|
140
|
+
return _request(client, httpRequest, {
|
|
141
|
+
method: 'POST',
|
|
142
|
+
uri: `/agent/action/prompt/${dataset}`,
|
|
143
|
+
body: request,
|
|
144
|
+
})
|
|
145
|
+
}
|
|
@@ -20,6 +20,8 @@ export interface TransformRequestBase extends AgentActionRequestBase {
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* The source document the transformation will use as input.
|
|
23
|
+
*
|
|
24
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
23
25
|
*/
|
|
24
26
|
documentId: string
|
|
25
27
|
|
|
@@ -28,13 +30,15 @@ export interface TransformRequestBase extends AgentActionRequestBase {
|
|
|
28
30
|
* then it is transformed according to the instruction.
|
|
29
31
|
*
|
|
30
32
|
* When omitted, the source document (documentId) is also the target document.
|
|
33
|
+
*
|
|
34
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
31
35
|
*/
|
|
32
36
|
targetDocument?: TransformTargetDocument
|
|
33
37
|
|
|
34
38
|
/**
|
|
35
39
|
* Instruct the LLM how to transform the input to th output.
|
|
36
40
|
*
|
|
37
|
-
* String template
|
|
41
|
+
* String template with support for $variable from `instructionParams`.
|
|
38
42
|
*
|
|
39
43
|
* Capped to 2000 characters, after variables has been injected.
|
|
40
44
|
* */
|
|
@@ -134,12 +138,31 @@ export interface TransformRequestBase extends AgentActionRequestBase {
|
|
|
134
138
|
* It is therefor an error to provide conflicting include/exclude across targets (ie, include title in one, and exclude it in another)
|
|
135
139
|
*
|
|
136
140
|
* Default max depth for transform: 12
|
|
141
|
+
*
|
|
142
|
+
* ## Transforming images
|
|
143
|
+
*
|
|
144
|
+
* To transform an existing image, directly target an image asset path.
|
|
145
|
+
*
|
|
146
|
+
* For example, all the following will transform the image into the provided asset:
|
|
147
|
+
* * `target: {path: ['image', 'asset'] }`
|
|
148
|
+
* * `target: {path: 'image', include: ['asset'] }`
|
|
149
|
+
*
|
|
150
|
+
* Image transform can be combined with regular content targets:
|
|
151
|
+
* * `target: [{path: ['image', 'asset'] }, {include: ['title', 'description']}]`
|
|
152
|
+
*
|
|
153
|
+
* Image transform can have per-path instructions, just like any other target paths:
|
|
154
|
+
* * `target: [{path: ['image', 'asset'], instruction: 'Make the sky blue' }`
|
|
155
|
+
*
|
|
137
156
|
* @see AgentActionRequestBase#conditionalPaths
|
|
138
157
|
*/
|
|
139
158
|
target?: TransformTarget | TransformTarget[]
|
|
140
159
|
}
|
|
141
160
|
|
|
142
|
-
/**
|
|
161
|
+
/**
|
|
162
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
163
|
+
*
|
|
164
|
+
* @beta
|
|
165
|
+
*/
|
|
143
166
|
export type TransformTargetDocument =
|
|
144
167
|
| {operation: 'edit'; _id: string}
|
|
145
168
|
| {operation: 'create'; _id?: string}
|
|
@@ -151,7 +174,7 @@ export interface TransformTargetInclude extends AgentActionTargetInclude {
|
|
|
151
174
|
/**
|
|
152
175
|
* Specifies a tailored instruction of this target.
|
|
153
176
|
*
|
|
154
|
-
*
|
|
177
|
+
* String template with support for $variable from `instructionParams`. */
|
|
155
178
|
instruction?: string
|
|
156
179
|
|
|
157
180
|
/**
|
|
@@ -169,7 +192,7 @@ export interface TransformTarget extends AgentActionTarget {
|
|
|
169
192
|
/**
|
|
170
193
|
* Specifies a tailored instruction of this target.
|
|
171
194
|
*
|
|
172
|
-
*
|
|
195
|
+
* String template with support for $variable from `instructionParams`.
|
|
173
196
|
* */
|
|
174
197
|
instruction?: string
|
|
175
198
|
|
|
@@ -27,6 +27,7 @@ export interface TranslateRequestBase extends AgentActionRequestBase {
|
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* The source document the transformation will use as input.
|
|
30
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
30
31
|
*/
|
|
31
32
|
documentId: string
|
|
32
33
|
|
|
@@ -35,6 +36,8 @@ export interface TranslateRequestBase extends AgentActionRequestBase {
|
|
|
35
36
|
* then it is translated according to the instruction.
|
|
36
37
|
*
|
|
37
38
|
* When omitted, the source document (documentId) is also the target document.
|
|
39
|
+
*
|
|
40
|
+
* @see #AgentActionSchema.forcePublishedWrite
|
|
38
41
|
*/
|
|
39
42
|
targetDocument?: TransformTargetDocument
|
|
40
43
|
|
|
@@ -105,7 +108,7 @@ export interface TranslateLanguage {
|
|
|
105
108
|
|
|
106
109
|
/** @beta */
|
|
107
110
|
export interface TranslateTargetInclude extends AgentActionTargetInclude {
|
|
108
|
-
/**
|
|
111
|
+
/** String template using $variable from styleGuideParams. */
|
|
109
112
|
styleGuide?: string
|
|
110
113
|
|
|
111
114
|
/**
|
|
@@ -120,7 +123,7 @@ export interface TranslateTargetInclude extends AgentActionTargetInclude {
|
|
|
120
123
|
|
|
121
124
|
/** @beta */
|
|
122
125
|
export interface TranslateTarget extends AgentActionTarget {
|
|
123
|
-
/**
|
|
126
|
+
/** String template using $variable from styleGuideParams. */
|
|
124
127
|
styleGuide?: string
|
|
125
128
|
|
|
126
129
|
/**
|