@sanity/client 6.27.3-canary.2 → 6.28.0-instruct.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 +85 -5
- package/dist/_chunks-cjs/config.cjs +4 -1
- package/dist/_chunks-cjs/config.cjs.map +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs +1 -1
- package/dist/_chunks-cjs/stegaEncodeSourceMap.cjs.map +1 -1
- package/dist/_chunks-es/config.js +4 -1
- package/dist/_chunks-es/config.js.map +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js +1 -1
- package/dist/_chunks-es/stegaEncodeSourceMap.js.map +1 -1
- package/dist/csm.d.cts +6 -1
- package/dist/csm.d.ts +6 -1
- package/dist/index.browser.cjs +46 -5
- package/dist/index.browser.cjs.map +1 -1
- package/dist/index.browser.d.cts +221 -1
- package/dist/index.browser.d.ts +221 -1
- package/dist/index.browser.js +47 -6
- package/dist/index.browser.js.map +1 -1
- package/dist/index.cjs +44 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +210 -1
- package/dist/index.d.ts +210 -1
- package/dist/index.js +46 -7
- package/dist/index.js.map +1 -1
- package/dist/stega.browser.d.cts +215 -1
- package/dist/stega.browser.d.ts +215 -1
- package/dist/stega.browser.js +1 -1
- package/dist/stega.d.cts +215 -1
- package/dist/stega.d.ts +215 -1
- package/dist/stega.js +1 -1
- package/package.json +11 -12
- package/src/SanityClient.ts +5 -1
- package/src/assist/AssistClient.ts +87 -0
- package/src/assist/types.ts +178 -0
- package/src/data/dataMethods.ts +12 -3
- package/src/types.ts +16 -1
- package/src/warnings.ts +5 -1
- package/umd/sanityClient.js +121 -86
- package/umd/sanityClient.min.js +2 -2
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import {lastValueFrom, type Observable} from 'rxjs'
|
|
2
|
+
|
|
3
|
+
import {_request} from '../data/dataMethods'
|
|
4
|
+
import type {ObservableSanityClient, SanityClient} from '../SanityClient'
|
|
5
|
+
import type {
|
|
6
|
+
Any,
|
|
7
|
+
AssistAsyncInstruction,
|
|
8
|
+
AssistInstruction,
|
|
9
|
+
AssistSyncInstruction,
|
|
10
|
+
HttpRequest,
|
|
11
|
+
IdentifiedSanityDocumentStub,
|
|
12
|
+
} from '../types'
|
|
13
|
+
import {hasDataset} from '../validators'
|
|
14
|
+
|
|
15
|
+
function _instruct<
|
|
16
|
+
DocumentShape extends Record<string, Any>,
|
|
17
|
+
Req extends AssistInstruction<DocumentShape>,
|
|
18
|
+
>(
|
|
19
|
+
client: SanityClient | ObservableSanityClient,
|
|
20
|
+
httpRequest: HttpRequest,
|
|
21
|
+
request: Req,
|
|
22
|
+
): Observable<
|
|
23
|
+
Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
|
|
24
|
+
> {
|
|
25
|
+
const dataset = hasDataset(client.config())
|
|
26
|
+
return _request(client, httpRequest, {
|
|
27
|
+
method: 'POST',
|
|
28
|
+
uri: `/assist/tasks/instruct/${dataset}`,
|
|
29
|
+
body: request,
|
|
30
|
+
})
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** @public */
|
|
34
|
+
export class ObservableAssistClient {
|
|
35
|
+
#client: ObservableSanityClient
|
|
36
|
+
#httpRequest: HttpRequest
|
|
37
|
+
constructor(client: ObservableSanityClient, httpRequest: HttpRequest) {
|
|
38
|
+
this.#client = client
|
|
39
|
+
this.#httpRequest = httpRequest
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
instruct(request: AssistAsyncInstruction): Observable<{_id: string}>
|
|
43
|
+
|
|
44
|
+
instruct<DocumentShape extends Record<string, Any>>(
|
|
45
|
+
request: AssistSyncInstruction<DocumentShape>,
|
|
46
|
+
): Observable<IdentifiedSanityDocumentStub & DocumentShape>
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Run an ad-hoc instruction for a target document.
|
|
50
|
+
* @param request instruction request
|
|
51
|
+
*/
|
|
52
|
+
instruct<DocumentShape extends Record<string, Any>, Req extends AssistInstruction<DocumentShape>>(
|
|
53
|
+
request: Req,
|
|
54
|
+
): Observable<
|
|
55
|
+
Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
|
|
56
|
+
> {
|
|
57
|
+
return _instruct(this.#client, this.#httpRequest, request)
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
/** @public */
|
|
62
|
+
export class AssistClient {
|
|
63
|
+
#client: SanityClient
|
|
64
|
+
#httpRequest: HttpRequest
|
|
65
|
+
constructor(client: SanityClient, httpRequest: HttpRequest) {
|
|
66
|
+
this.#client = client
|
|
67
|
+
this.#httpRequest = httpRequest
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
instruct(request: AssistAsyncInstruction): Promise<{_id: string}>
|
|
71
|
+
|
|
72
|
+
instruct<DocumentShape extends Record<string, Any>>(
|
|
73
|
+
request: AssistSyncInstruction<DocumentShape>,
|
|
74
|
+
): Promise<IdentifiedSanityDocumentStub & DocumentShape>
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Run an ad-hoc instruction for a target document.
|
|
78
|
+
* @param request instruction request
|
|
79
|
+
*/
|
|
80
|
+
instruct<DocumentShape extends Record<string, Any>, Req extends AssistInstruction<DocumentShape>>(
|
|
81
|
+
request: Req,
|
|
82
|
+
): Promise<
|
|
83
|
+
Req['async'] extends true ? {_id: string} : IdentifiedSanityDocumentStub & DocumentShape
|
|
84
|
+
> {
|
|
85
|
+
return lastValueFrom(_instruct(this.#client, this.#httpRequest, request))
|
|
86
|
+
}
|
|
87
|
+
}
|
|
@@ -0,0 +1,178 @@
|
|
|
1
|
+
//Request shape
|
|
2
|
+
|
|
3
|
+
import type {Any, SanityDocumentStub} from '@sanity/client'
|
|
4
|
+
|
|
5
|
+
/** @beta */
|
|
6
|
+
export interface ConstantInstructionParam {
|
|
7
|
+
type: 'constant'
|
|
8
|
+
value: string
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** @beta */
|
|
12
|
+
export interface FieldInstructionParam {
|
|
13
|
+
type: 'field'
|
|
14
|
+
path: string
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** @beta */
|
|
18
|
+
export interface GroqInstructionParam {
|
|
19
|
+
type: 'groq'
|
|
20
|
+
query: string
|
|
21
|
+
params?: Record<string, string>
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/** @beta */
|
|
25
|
+
export type InstructionParam =
|
|
26
|
+
| string
|
|
27
|
+
| ConstantInstructionParam
|
|
28
|
+
| FieldInstructionParam
|
|
29
|
+
| GroqInstructionParam
|
|
30
|
+
|
|
31
|
+
/** @beta */
|
|
32
|
+
export type InstructionParams = Record<string, InstructionParam>
|
|
33
|
+
|
|
34
|
+
interface AssistRequestBase {
|
|
35
|
+
/** schemaId as reported by sanity deploy / sanity schema store */
|
|
36
|
+
schemaId: string
|
|
37
|
+
/** string template using $variable – more on this below under "Dynamic instruction" */
|
|
38
|
+
instruction: string
|
|
39
|
+
/** param values for the string template, keys are the variable name, ie if the template has "$variable", one key must be "variable" */
|
|
40
|
+
instructionParams?: InstructionParams
|
|
41
|
+
/**
|
|
42
|
+
* Optional document path output target for the instruction.
|
|
43
|
+
* When provided, the instruction will apply to this path in the document and its children.
|
|
44
|
+
*
|
|
45
|
+
* ## Examples
|
|
46
|
+
* - `path: 'title'` will output to the title field in the document
|
|
47
|
+
* - `path: 'array[_key="xx"]'` will output to the item with `_key: 'xx'` in the array field
|
|
48
|
+
*/
|
|
49
|
+
path?: string
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Controls sub-paths in the document that can be output to.
|
|
53
|
+
*
|
|
54
|
+
* The string-paths are relative to the `path` param
|
|
55
|
+
*
|
|
56
|
+
* Note: these path strings are less strictly validated than the `path` param itself:
|
|
57
|
+
* if an relative-path does not exist or is invalid, it will be silently ignored.
|
|
58
|
+
*
|
|
59
|
+
* @see AssistRequestBase#conditionalPaths
|
|
60
|
+
* @see AssistRequestBase#outputTypes
|
|
61
|
+
*/
|
|
62
|
+
relativeOutputPaths?: {include: string[]} | {exclude: string[]}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Controls which types the instruction is allowed to output to.
|
|
66
|
+
*
|
|
67
|
+
* @see AssistRequestBase#relativeOutputPaths
|
|
68
|
+
* @see AssistRequestBase#conditionalPaths
|
|
69
|
+
*/
|
|
70
|
+
outputTypes?: {include: string[]} | {exclude: string[]}
|
|
71
|
+
|
|
72
|
+
/**
|
|
73
|
+
* When a type or field in the schema has a function set for `hidden` or `readOnly`, it is conditional.
|
|
74
|
+
*
|
|
75
|
+
* By default, AI Assist will not output to conditional `readOnly` and `hidden` fields,
|
|
76
|
+
* ie, they are considered to resolve to `readOnly: true` / `hidden: true`.
|
|
77
|
+
*
|
|
78
|
+
* `conditionalPaths` param allows setting the default conditional value for
|
|
79
|
+
* `hidden` and `readOnly` to false,
|
|
80
|
+
* or individually set `hidden` and `readOnly` state for individual document paths.
|
|
81
|
+
*
|
|
82
|
+
*
|
|
83
|
+
* Note: fields and types with explicit readOnly: true or hidden: true in the schema, are not available to AI Assist,
|
|
84
|
+
* and cannot be changed via conditionalPaths.
|
|
85
|
+
*
|
|
86
|
+
* conditionalPaths state only apply to fields and types that have conditional `hidden` or `readOnly` in their schema definition.
|
|
87
|
+
*
|
|
88
|
+
* @see AssistRequestBase#relativeOutputPaths
|
|
89
|
+
* @see AssistRequestBase#outputTypes
|
|
90
|
+
*/
|
|
91
|
+
conditionalPaths?: {
|
|
92
|
+
defaultReadOnly?: boolean
|
|
93
|
+
defaultHidden?: boolean
|
|
94
|
+
paths?: {
|
|
95
|
+
/** path here is not a relative path: it must be the full document path, regardless of `path` param on the request itself */
|
|
96
|
+
path: string
|
|
97
|
+
readOnly: boolean
|
|
98
|
+
hidden: boolean
|
|
99
|
+
}[]
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
interface Sync {
|
|
104
|
+
/**
|
|
105
|
+
* By default, skipWrite: false.
|
|
106
|
+
* Write enabled operations will mutate the target document, and emit AI presence in the studio.
|
|
107
|
+
*
|
|
108
|
+
* When skipWrite: true, the api will not mutate any documents nor emit presence.
|
|
109
|
+
* Ie, when true, no changes will be made to content-lake
|
|
110
|
+
*
|
|
111
|
+
* skipWrite: true is incompatible with async: true,
|
|
112
|
+
* as skipWrite implies that you will use the return value of the operation
|
|
113
|
+
*/
|
|
114
|
+
skipWrite?: boolean
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
|
|
118
|
+
* The instruction operation will carry on in the background.
|
|
119
|
+
*
|
|
120
|
+
* When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
|
|
121
|
+
*
|
|
122
|
+
* async: true is incompatible with skipWrite: true, as async: true does not return the resulting document
|
|
123
|
+
*/
|
|
124
|
+
async?: false
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
interface Async {
|
|
128
|
+
/**
|
|
129
|
+
* When async: true, requests responds with status 201 and {_id} as response body as soon as the request is validated.
|
|
130
|
+
* The instruction operation will carry on in the background.
|
|
131
|
+
*
|
|
132
|
+
* When async: false (default), requests respond with status 200 and the document value after instruction has been applied.
|
|
133
|
+
*
|
|
134
|
+
* async: true is incompatible with skipWrite, as async: true does not return the resulting document
|
|
135
|
+
*/
|
|
136
|
+
async: true
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* Instruction for an existing document.
|
|
141
|
+
* @beta
|
|
142
|
+
*/
|
|
143
|
+
interface ExistingDocumentRequest {
|
|
144
|
+
documentId: string
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Instruction to create a new document
|
|
149
|
+
* @beta
|
|
150
|
+
*/
|
|
151
|
+
interface CreateDocumentRequest<T extends Record<string, Any> = Record<string, Any>> {
|
|
152
|
+
createDocument: {
|
|
153
|
+
/** if no _id is provided, one will be generated. _id is always returned when the requests succeed */
|
|
154
|
+
_id?: string
|
|
155
|
+
_type: string
|
|
156
|
+
} & SanityDocumentStub<T>
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** @beta */
|
|
160
|
+
export type AssistSyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
161
|
+
| ExistingDocumentRequest
|
|
162
|
+
| CreateDocumentRequest<T>
|
|
163
|
+
) &
|
|
164
|
+
AssistRequestBase &
|
|
165
|
+
Sync
|
|
166
|
+
|
|
167
|
+
/** @beta */
|
|
168
|
+
export type AssistAsyncInstruction<T extends Record<string, Any> = Record<string, Any>> = (
|
|
169
|
+
| ExistingDocumentRequest
|
|
170
|
+
| CreateDocumentRequest<T>
|
|
171
|
+
) &
|
|
172
|
+
AssistRequestBase &
|
|
173
|
+
Async
|
|
174
|
+
|
|
175
|
+
/** @beta */
|
|
176
|
+
export type AssistInstruction<T extends Record<string, Any> = Record<string, Any>> =
|
|
177
|
+
| AssistSyncInstruction<T>
|
|
178
|
+
| AssistAsyncInstruction<T>
|
package/src/data/dataMethods.ts
CHANGED
|
@@ -33,7 +33,7 @@ import type {
|
|
|
33
33
|
import {getSelection} from '../util/getSelection'
|
|
34
34
|
import * as validate from '../validators'
|
|
35
35
|
import * as validators from '../validators'
|
|
36
|
-
import {printCdnPreviewDraftsWarning} from '../warnings'
|
|
36
|
+
import {printCdnPreviewDraftsWarning, printPreviewDraftsDeprecationWarning} from '../warnings'
|
|
37
37
|
import {encodeQueryString} from './encodeQueryString'
|
|
38
38
|
import {ObservablePatch, Patch} from './patch'
|
|
39
39
|
import {ObservableTransaction, Transaction} from './transaction'
|
|
@@ -407,6 +407,9 @@ export function _requestObservable<R>(
|
|
|
407
407
|
}
|
|
408
408
|
const perspectiveOption = options.perspective || config.perspective
|
|
409
409
|
if (typeof perspectiveOption !== 'undefined') {
|
|
410
|
+
if (perspectiveOption === 'previewDrafts') {
|
|
411
|
+
printPreviewDraftsDeprecationWarning()
|
|
412
|
+
}
|
|
410
413
|
validateApiPerspective(perspectiveOption)
|
|
411
414
|
options.query = {
|
|
412
415
|
perspective: Array.isArray(perspectiveOption)
|
|
@@ -414,8 +417,14 @@ export function _requestObservable<R>(
|
|
|
414
417
|
: perspectiveOption,
|
|
415
418
|
...options.query,
|
|
416
419
|
}
|
|
417
|
-
// If the perspective is set to `
|
|
418
|
-
if (
|
|
420
|
+
// If the perspective is set to `drafts` or multiple perspectives we can't use the CDN, the API will throw
|
|
421
|
+
if (
|
|
422
|
+
((Array.isArray(perspectiveOption) && perspectiveOption.length > 0) ||
|
|
423
|
+
// previewDrafts was renamed to drafts, but keep for backwards compat
|
|
424
|
+
perspectiveOption === 'previewDrafts' ||
|
|
425
|
+
perspectiveOption === 'drafts') &&
|
|
426
|
+
useCdn
|
|
427
|
+
) {
|
|
419
428
|
useCdn = false
|
|
420
429
|
printCdnPreviewDraftsWarning()
|
|
421
430
|
}
|
package/src/types.ts
CHANGED
|
@@ -37,12 +37,17 @@ export interface RequestOptions {
|
|
|
37
37
|
*/
|
|
38
38
|
export type ReleaseId = `r${string}`
|
|
39
39
|
|
|
40
|
+
/**
|
|
41
|
+
* @deprecated use 'drafts' instead
|
|
42
|
+
*/
|
|
43
|
+
type DeprecatedPreviewDrafts = 'previewDrafts'
|
|
44
|
+
|
|
40
45
|
/** @public */
|
|
41
46
|
export type StackablePerspective = ('published' | 'drafts' | string) & {}
|
|
42
47
|
|
|
43
48
|
/** @public */
|
|
44
49
|
export type ClientPerspective =
|
|
45
|
-
|
|
|
50
|
+
| DeprecatedPreviewDrafts
|
|
46
51
|
| 'published'
|
|
47
52
|
| 'drafts'
|
|
48
53
|
| 'raw'
|
|
@@ -1315,6 +1320,16 @@ export type ClientReturn<
|
|
|
1315
1320
|
Fallback = Any,
|
|
1316
1321
|
> = GroqString extends keyof SanityQueries ? SanityQueries[GroqString] : Fallback
|
|
1317
1322
|
|
|
1323
|
+
export type {
|
|
1324
|
+
AssistAsyncInstruction,
|
|
1325
|
+
AssistInstruction,
|
|
1326
|
+
AssistSyncInstruction,
|
|
1327
|
+
ConstantInstructionParam,
|
|
1328
|
+
FieldInstructionParam,
|
|
1329
|
+
GroqInstructionParam,
|
|
1330
|
+
InstructionParam,
|
|
1331
|
+
InstructionParams,
|
|
1332
|
+
} from './assist/types'
|
|
1318
1333
|
export type {
|
|
1319
1334
|
ContentSourceMapParsedPath,
|
|
1320
1335
|
ContentSourceMapParsedPathKeyedSegment,
|
package/src/warnings.ts
CHANGED
|
@@ -18,10 +18,14 @@ export const printCdnWarning = createWarningPrinter([
|
|
|
18
18
|
])
|
|
19
19
|
|
|
20
20
|
export const printCdnPreviewDraftsWarning = createWarningPrinter([
|
|
21
|
-
`The Sanity client is configured with the \`perspective\` set to \`previewDrafts\`, which doesn't support the API-CDN.`,
|
|
21
|
+
`The Sanity client is configured with the \`perspective\` set to \`drafts\` or \`previewDrafts\`, which doesn't support the API-CDN.`,
|
|
22
22
|
`The Live API will be used instead. Set \`useCdn: false\` in your configuration to hide this warning.`,
|
|
23
23
|
])
|
|
24
24
|
|
|
25
|
+
export const printPreviewDraftsDeprecationWarning = createWarningPrinter([
|
|
26
|
+
`The \`previewDrafts\` perspective has been renamed to \`drafts\` and will be removed in a future API version`,
|
|
27
|
+
])
|
|
28
|
+
|
|
25
29
|
export const printBrowserTokenWarning = createWarningPrinter([
|
|
26
30
|
'You have configured Sanity client to use a token in the browser. This may cause unintentional security issues.',
|
|
27
31
|
`See ${generateHelpUrl(
|