@sanity/assist 3.2.1 → 4.0.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 +58 -5
- package/dist/index.d.mts +84 -0
- package/dist/index.d.ts +84 -0
- package/dist/index.esm.js +80 -56
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +80 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +80 -56
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
- package/src/assistInspector/AssistInspector.tsx +1 -1
- package/src/assistTypes.ts +83 -0
- package/src/helpers/assistSupported.ts +0 -5
- package/src/index.ts +1 -0
- package/src/plugin.tsx +23 -1
- package/src/schemas/serialize/serializeSchema.test.ts +8 -6
- package/src/useApiClient.ts +24 -2
package/README.md
CHANGED
|
@@ -12,11 +12,13 @@
|
|
|
12
12
|
- [Enabling the AI Assist API](#enabling-the-ai-assist-api)
|
|
13
13
|
- [Permissions](#permissions)
|
|
14
14
|
- [Conditional user access](#conditional-user-access)
|
|
15
|
+
- [Assist configuration](#assist-configuration-optional)
|
|
15
16
|
- [Schema configuration](#schema-configuration)
|
|
16
17
|
- [Disable AI Assist for a schema type](#disable-ai-assist-for-a-schema-type)
|
|
17
18
|
- [Disable for a field](#disable-for-a-field)
|
|
18
19
|
- [Disable for an array type](#disable-for-an-array-type)
|
|
19
20
|
- [Unsupported types](#unsupported-types)
|
|
21
|
+
- [Date and datetime](#date-and-datetime)
|
|
20
22
|
- [Hidden and readOnly fields](#hidden-and-readonly-fields)
|
|
21
23
|
- [Reference support](#reference-support)
|
|
22
24
|
- [Troubleshooting](#troubleshooting)
|
|
@@ -152,6 +154,26 @@ function isAiAssistAllowed(user?: CurrentUser | null) {
|
|
|
152
154
|
}
|
|
153
155
|
```
|
|
154
156
|
|
|
157
|
+
### Assist configuration (optional)
|
|
158
|
+
|
|
159
|
+
```ts
|
|
160
|
+
assist({
|
|
161
|
+
// Showing default values
|
|
162
|
+
assist: {
|
|
163
|
+
localeSettings: () => Intl.DateTimeFormat().resolvedOptions(),
|
|
164
|
+
maxPathDepth: 4,
|
|
165
|
+
temperature: 0.3
|
|
166
|
+
},
|
|
167
|
+
translate: { /* see sections about document and field translation */}
|
|
168
|
+
})
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
- `localeSettings`: See section on [date and datetime](#date-and-datetime)
|
|
172
|
+
- `maxPathDepth`: The max depth for document paths AI Assist will write to.
|
|
173
|
+
- `temperature`: Influences how much the output of an instruction will vary between runs.
|
|
174
|
+
|
|
175
|
+
For more details, please review the TSDocs of the individual config parameters in [assistTypes.ts](./src/assistTypes.ts)
|
|
176
|
+
|
|
155
177
|
## Schema configuration
|
|
156
178
|
|
|
157
179
|
By default, most string, object, and array field types (including Portable Text!) have AI writing assistance enabled. Your assistant can write to all compatible fields that it detects.
|
|
@@ -215,11 +237,6 @@ defineType({
|
|
|
215
237
|
|
|
216
238
|
The following types are not supported, and behave as excluded types:
|
|
217
239
|
|
|
218
|
-
- [Number](https://www.sanity.io/docs/number-type)
|
|
219
|
-
- [Slug](https://www.sanity.io/docs/slug-type)
|
|
220
|
-
- [Url](https://www.sanity.io/docs/url-type)
|
|
221
|
-
- [Date](https://www.sanity.io/docs/date-type)
|
|
222
|
-
- [Datetime](https://www.sanity.io/docs/datetime-type)
|
|
223
240
|
- [Image](https://www.sanity.io/docs/image-type) (supported when image has custom fields)
|
|
224
241
|
- [File](https://www.sanity.io/docs/file-type) (never supported, even when file has custom fields)
|
|
225
242
|
- [Reference](https://www.sanity.io/docs/reference-type) (supported when configured with embeddingsIndex)
|
|
@@ -229,6 +246,42 @@ Fields with these types will not be changed by the assistant, do not have AI Ass
|
|
|
229
246
|
Objects where all fields are excluded or unsupported and arrays where all member types are excluded or unsupported
|
|
230
247
|
will also be excluded.
|
|
231
248
|
|
|
249
|
+
### Date and datetime
|
|
250
|
+
- [Date](https://www.sanity.io/docs/date-type)
|
|
251
|
+
- [Datetime](https://www.sanity.io/docs/datetime-type)
|
|
252
|
+
|
|
253
|
+
Starting from v3.0.0, AI Assist can write to date and datetime fields. Instructions can use language like "tomorrow at noon" or
|
|
254
|
+
"next year", and when Assist writes to the field, it will be converted to a field-compatible value.
|
|
255
|
+
|
|
256
|
+
Language about time is locale and timeZone dependant. By default instructions will use the locale and timezone provided
|
|
257
|
+
by the browser (`Intl.DateTimeFormat().resolvedOptions()`).
|
|
258
|
+
|
|
259
|
+
Alternatively, you can configure the plugin per user with an `assist.localeSettings` function that should return `LocaleSettings`.
|
|
260
|
+
|
|
261
|
+
##### Example
|
|
262
|
+
```ts
|
|
263
|
+
assist({
|
|
264
|
+
assist: {
|
|
265
|
+
localeSettings: ({currentUser, defaultSettings}) => {
|
|
266
|
+
if (currentUser.roles.includes('admin')) {
|
|
267
|
+
// forces locale and timeZone for admins
|
|
268
|
+
return {
|
|
269
|
+
locale: 'en-US',
|
|
270
|
+
timeZone: 'America/New_York'
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
// defaultSettings is the same as using:
|
|
274
|
+
// const {locale, timeZone} = Intl.DateTimeFormat().resolvedOptions()
|
|
275
|
+
return defaultSettings
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
})
|
|
279
|
+
```
|
|
280
|
+
|
|
281
|
+
For a list of allowed values for these parameters, see the following resources:
|
|
282
|
+
- `locale`: [Mozilla on Intl](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales)
|
|
283
|
+
- `timeZone`: [Wiki on time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)
|
|
284
|
+
|
|
232
285
|
### Hidden and readOnly fields
|
|
233
286
|
|
|
234
287
|
In AI Assist 2.0 and later, conditionally `hidden` and `readOnly` fields can have instructions.
|
package/dist/index.d.mts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {CurrentUser} from 'sanity'
|
|
1
2
|
import {JSX as JSX_2} from 'react/jsx-runtime'
|
|
2
3
|
import {Path} from 'sanity'
|
|
3
4
|
import {Plugin as Plugin_2} from 'sanity'
|
|
@@ -10,6 +11,56 @@ import {SchemaType} from 'sanity'
|
|
|
10
11
|
|
|
11
12
|
export declare const assist: Plugin_2<void | AssistPluginConfig>
|
|
12
13
|
|
|
14
|
+
export declare interface AssistConfig {
|
|
15
|
+
/**
|
|
16
|
+
* As of v3.0 Assist can write to date and datetime fields.
|
|
17
|
+
*
|
|
18
|
+
* If this function is omitted from config, the plugin will use the default timeZone and locale
|
|
19
|
+
* in the browser:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const {timeZone, locale} = Intl.DateTimeFormat().resolvedOptions()
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* The function will be called any time an instruction runs.
|
|
26
|
+
*
|
|
27
|
+
* @see #LocaleSettings.locale
|
|
28
|
+
* @see #LocaleSettings.timeZone
|
|
29
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales
|
|
30
|
+
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
31
|
+
*/
|
|
32
|
+
localeSettings?: (context: LocaleSettingsContext) => LocaleSettings
|
|
33
|
+
/**
|
|
34
|
+
* The max depth for document paths AI Assist will write to.
|
|
35
|
+
*
|
|
36
|
+
* Depth is based on field path segments:
|
|
37
|
+
* - `title` has depth 1
|
|
38
|
+
* - `array[_key="no"].title` has depth 3
|
|
39
|
+
*
|
|
40
|
+
* Be careful not to set this too high in studios with recursive document schemas, as it could have
|
|
41
|
+
* negative impact on performance.
|
|
42
|
+
*
|
|
43
|
+
* Depth will be counted from the field the instruction is run from. For example, if an instruction
|
|
44
|
+
* is attached to depth 6, the count starts from there (at 0, not at 6).
|
|
45
|
+
*
|
|
46
|
+
* Default: 4
|
|
47
|
+
*/
|
|
48
|
+
maxPathDepth?: number
|
|
49
|
+
/**
|
|
50
|
+
* Influences how much the output of an instruction will vary.
|
|
51
|
+
*
|
|
52
|
+
* Min: 0 – re-running an instruction will often produce the same outcomes
|
|
53
|
+
* Max: 1 – re-running an instruction can produce wildly different outcomes
|
|
54
|
+
*
|
|
55
|
+
* This parameter applies to _all_ instructions in the studio.
|
|
56
|
+
*
|
|
57
|
+
* Prior to v3.0, this defaulted to 0
|
|
58
|
+
*
|
|
59
|
+
* Default: 0.3
|
|
60
|
+
*/
|
|
61
|
+
temperature?: number
|
|
62
|
+
}
|
|
63
|
+
|
|
13
64
|
export declare interface AssistOptions {
|
|
14
65
|
aiAssist?: {
|
|
15
66
|
/** Set to true to disable assistance for this field or type */
|
|
@@ -24,6 +75,10 @@ export declare interface AssistOptions {
|
|
|
24
75
|
|
|
25
76
|
declare interface AssistPluginConfig {
|
|
26
77
|
translate?: TranslationConfig
|
|
78
|
+
/**
|
|
79
|
+
* Config that affects all instructions
|
|
80
|
+
*/
|
|
81
|
+
assist?: AssistConfig
|
|
27
82
|
/**
|
|
28
83
|
* @internal
|
|
29
84
|
*/
|
|
@@ -210,6 +265,35 @@ export declare type LanguageCallback = (
|
|
|
210
265
|
selectedLanguageParams: Record<string, unknown>,
|
|
211
266
|
) => Promise<Language[]>
|
|
212
267
|
|
|
268
|
+
export declare interface LocaleSettings {
|
|
269
|
+
/**
|
|
270
|
+
* A valid Unicode BCP 47 locale identifier used to interpret and format
|
|
271
|
+
* natural language inputs and date output. Examples include "en-US", "fr-FR", or "ja-JP".
|
|
272
|
+
*
|
|
273
|
+
* This affects how phrases like "next Friday" or "in two weeks" are parsed,
|
|
274
|
+
* and how resulting dates are presented (e.g., 12-hour vs 24-hour format).
|
|
275
|
+
*
|
|
276
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales
|
|
277
|
+
*/
|
|
278
|
+
locale: string
|
|
279
|
+
/**
|
|
280
|
+
* A valid IANA time zone identifier used to resolve relative and absolute
|
|
281
|
+
* date expressions to a specific point in time. Examples include
|
|
282
|
+
* "America/New_York", "Europe/Paris", or "Asia/Tokyo".
|
|
283
|
+
*
|
|
284
|
+
* This ensures phrases like "tomorrow at 9am" are interpreted correctly
|
|
285
|
+
* based on the user's local time.
|
|
286
|
+
*
|
|
287
|
+
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
288
|
+
*/
|
|
289
|
+
timeZone: string
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export declare interface LocaleSettingsContext {
|
|
293
|
+
user: CurrentUser
|
|
294
|
+
defaultSettings: LocaleSettings
|
|
295
|
+
}
|
|
296
|
+
|
|
213
297
|
declare interface OutputFieldItem {
|
|
214
298
|
_type: typeof outputFieldTypeName
|
|
215
299
|
_key: string
|
package/dist/index.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import {CurrentUser} from 'sanity'
|
|
1
2
|
import {JSX as JSX_2} from 'react/jsx-runtime'
|
|
2
3
|
import {Path} from 'sanity'
|
|
3
4
|
import {Plugin as Plugin_2} from 'sanity'
|
|
@@ -10,6 +11,56 @@ import {SchemaType} from 'sanity'
|
|
|
10
11
|
|
|
11
12
|
export declare const assist: Plugin_2<void | AssistPluginConfig>
|
|
12
13
|
|
|
14
|
+
export declare interface AssistConfig {
|
|
15
|
+
/**
|
|
16
|
+
* As of v3.0 Assist can write to date and datetime fields.
|
|
17
|
+
*
|
|
18
|
+
* If this function is omitted from config, the plugin will use the default timeZone and locale
|
|
19
|
+
* in the browser:
|
|
20
|
+
*
|
|
21
|
+
* ```ts
|
|
22
|
+
* const {timeZone, locale} = Intl.DateTimeFormat().resolvedOptions()
|
|
23
|
+
* ```
|
|
24
|
+
*
|
|
25
|
+
* The function will be called any time an instruction runs.
|
|
26
|
+
*
|
|
27
|
+
* @see #LocaleSettings.locale
|
|
28
|
+
* @see #LocaleSettings.timeZone
|
|
29
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales
|
|
30
|
+
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
31
|
+
*/
|
|
32
|
+
localeSettings?: (context: LocaleSettingsContext) => LocaleSettings
|
|
33
|
+
/**
|
|
34
|
+
* The max depth for document paths AI Assist will write to.
|
|
35
|
+
*
|
|
36
|
+
* Depth is based on field path segments:
|
|
37
|
+
* - `title` has depth 1
|
|
38
|
+
* - `array[_key="no"].title` has depth 3
|
|
39
|
+
*
|
|
40
|
+
* Be careful not to set this too high in studios with recursive document schemas, as it could have
|
|
41
|
+
* negative impact on performance.
|
|
42
|
+
*
|
|
43
|
+
* Depth will be counted from the field the instruction is run from. For example, if an instruction
|
|
44
|
+
* is attached to depth 6, the count starts from there (at 0, not at 6).
|
|
45
|
+
*
|
|
46
|
+
* Default: 4
|
|
47
|
+
*/
|
|
48
|
+
maxPathDepth?: number
|
|
49
|
+
/**
|
|
50
|
+
* Influences how much the output of an instruction will vary.
|
|
51
|
+
*
|
|
52
|
+
* Min: 0 – re-running an instruction will often produce the same outcomes
|
|
53
|
+
* Max: 1 – re-running an instruction can produce wildly different outcomes
|
|
54
|
+
*
|
|
55
|
+
* This parameter applies to _all_ instructions in the studio.
|
|
56
|
+
*
|
|
57
|
+
* Prior to v3.0, this defaulted to 0
|
|
58
|
+
*
|
|
59
|
+
* Default: 0.3
|
|
60
|
+
*/
|
|
61
|
+
temperature?: number
|
|
62
|
+
}
|
|
63
|
+
|
|
13
64
|
export declare interface AssistOptions {
|
|
14
65
|
aiAssist?: {
|
|
15
66
|
/** Set to true to disable assistance for this field or type */
|
|
@@ -24,6 +75,10 @@ export declare interface AssistOptions {
|
|
|
24
75
|
|
|
25
76
|
declare interface AssistPluginConfig {
|
|
26
77
|
translate?: TranslationConfig
|
|
78
|
+
/**
|
|
79
|
+
* Config that affects all instructions
|
|
80
|
+
*/
|
|
81
|
+
assist?: AssistConfig
|
|
27
82
|
/**
|
|
28
83
|
* @internal
|
|
29
84
|
*/
|
|
@@ -210,6 +265,35 @@ export declare type LanguageCallback = (
|
|
|
210
265
|
selectedLanguageParams: Record<string, unknown>,
|
|
211
266
|
) => Promise<Language[]>
|
|
212
267
|
|
|
268
|
+
export declare interface LocaleSettings {
|
|
269
|
+
/**
|
|
270
|
+
* A valid Unicode BCP 47 locale identifier used to interpret and format
|
|
271
|
+
* natural language inputs and date output. Examples include "en-US", "fr-FR", or "ja-JP".
|
|
272
|
+
*
|
|
273
|
+
* This affects how phrases like "next Friday" or "in two weeks" are parsed,
|
|
274
|
+
* and how resulting dates are presented (e.g., 12-hour vs 24-hour format).
|
|
275
|
+
*
|
|
276
|
+
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#getcanonicalocales
|
|
277
|
+
*/
|
|
278
|
+
locale: string
|
|
279
|
+
/**
|
|
280
|
+
* A valid IANA time zone identifier used to resolve relative and absolute
|
|
281
|
+
* date expressions to a specific point in time. Examples include
|
|
282
|
+
* "America/New_York", "Europe/Paris", or "Asia/Tokyo".
|
|
283
|
+
*
|
|
284
|
+
* This ensures phrases like "tomorrow at 9am" are interpreted correctly
|
|
285
|
+
* based on the user's local time.
|
|
286
|
+
*
|
|
287
|
+
* @see https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
|
288
|
+
*/
|
|
289
|
+
timeZone: string
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
export declare interface LocaleSettingsContext {
|
|
293
|
+
user: CurrentUser
|
|
294
|
+
defaultSettings: LocaleSettings
|
|
295
|
+
}
|
|
296
|
+
|
|
213
297
|
declare interface OutputFieldItem {
|
|
214
298
|
_type: typeof outputFieldTypeName
|
|
215
299
|
_key: string
|
package/dist/index.esm.js
CHANGED
|
@@ -236,7 +236,7 @@ function isDisabled(type) {
|
|
|
236
236
|
return !isSchemaAssistEnabled(type) || isUnsupportedType(type);
|
|
237
237
|
}
|
|
238
238
|
function isUnsupportedType(type) {
|
|
239
|
-
return type.
|
|
239
|
+
return type.name === "sanity.imageCrop" || type.name === "sanity.imageHotspot" || isType(type, "globalDocumentReference") || isType(type, "reference") && !type?.options?.aiAssist?.embeddingsIndex || isType(type, "crossDatasetReference") || isType(type, "file");
|
|
240
240
|
}
|
|
241
241
|
const FirstAssistedPathContext = createContext(void 0);
|
|
242
242
|
function FirstAssistedPathProvider(props) {
|
|
@@ -466,6 +466,39 @@ function useAiPaneRouter() {
|
|
|
466
466
|
[paneRouter]
|
|
467
467
|
);
|
|
468
468
|
}
|
|
469
|
+
const AiAssistanceConfigContext = createContext({});
|
|
470
|
+
function useAiAssistanceConfig() {
|
|
471
|
+
const context = useContext(AiAssistanceConfigContext);
|
|
472
|
+
if (!context)
|
|
473
|
+
throw new Error("Missing AiAssistanceConfigContext");
|
|
474
|
+
return context;
|
|
475
|
+
}
|
|
476
|
+
function AiAssistanceConfigProvider(props) {
|
|
477
|
+
const [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
|
|
478
|
+
useEffect(() => {
|
|
479
|
+
getInstructStatus().then((s) => setStatus(s)).catch((e) => {
|
|
480
|
+
console.error(e), setError(e);
|
|
481
|
+
});
|
|
482
|
+
}, [getInstructStatus]);
|
|
483
|
+
const init = useCallback(async () => {
|
|
484
|
+
setError(void 0);
|
|
485
|
+
try {
|
|
486
|
+
await initInstruct();
|
|
487
|
+
const status2 = await getInstructStatus();
|
|
488
|
+
setStatus(status2);
|
|
489
|
+
} catch (e) {
|
|
490
|
+
console.error("Failed to init ai assistance", e), setError(e);
|
|
491
|
+
}
|
|
492
|
+
}, [initInstruct, getInstructStatus, setStatus]), { config, children } = props, context = useMemo(() => ({
|
|
493
|
+
config,
|
|
494
|
+
status,
|
|
495
|
+
statusLoading,
|
|
496
|
+
init,
|
|
497
|
+
initLoading,
|
|
498
|
+
error
|
|
499
|
+
}), [config, status, init, statusLoading, initLoading, error]);
|
|
500
|
+
return /* @__PURE__ */ jsx(AiAssistanceConfigContext.Provider, { value: context, children });
|
|
501
|
+
}
|
|
469
502
|
const hiddenTypes = [
|
|
470
503
|
"any",
|
|
471
504
|
"array",
|
|
@@ -589,12 +622,12 @@ function refToTypeNames(type) {
|
|
|
589
622
|
function removeUndef(obj) {
|
|
590
623
|
return Object.keys(obj).forEach((key) => obj[key] === void 0 ? delete obj[key] : {}), obj;
|
|
591
624
|
}
|
|
592
|
-
const basePath = "/assist/tasks/instruction";
|
|
625
|
+
const basePath = "/assist/tasks/instruction", API_VERSION_WITH_EXTENDED_TYPES = "2025-04-01";
|
|
593
626
|
function canUseAssist(status) {
|
|
594
627
|
return status?.enabled && status.initialized && status.validToken;
|
|
595
628
|
}
|
|
596
629
|
function useApiClient(customApiClient) {
|
|
597
|
-
const client = useClient({ apiVersion:
|
|
630
|
+
const client = useClient({ apiVersion: API_VERSION_WITH_EXTENDED_TYPES });
|
|
598
631
|
return useMemo(
|
|
599
632
|
() => customApiClient ? customApiClient(client) : client,
|
|
600
633
|
[client, customApiClient]
|
|
@@ -738,25 +771,40 @@ function useInitInstruct(apiClient) {
|
|
|
738
771
|
};
|
|
739
772
|
}
|
|
740
773
|
function useRunInstructionApi(apiClient) {
|
|
741
|
-
const toast = useToast(), [loading, setLoading] = useState(!1), user = useCurrentUser(), schema = useSchema(), types = useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]),
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
774
|
+
const toast = useToast(), [loading, setLoading] = useState(!1), user = useCurrentUser(), schema = useSchema(), types = useMemo(() => serializeSchema(schema, { leanFormat: !0 }), [schema]), {
|
|
775
|
+
config: { assist: assistConfig }
|
|
776
|
+
} = useAiAssistanceConfig(), runInstruction = useCallback(
|
|
777
|
+
(request) => {
|
|
778
|
+
if (!user) {
|
|
779
|
+
toast.push({
|
|
780
|
+
status: "error",
|
|
781
|
+
title: "Unable to get user for instruction."
|
|
782
|
+
});
|
|
783
|
+
return;
|
|
749
784
|
}
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
785
|
+
setLoading(!0);
|
|
786
|
+
const { timeZone, locale } = Intl.DateTimeFormat().resolvedOptions(), defaultLocaleSettings = { timeZone, locale }, localeSettings = assistConfig?.localeSettings?.({ user, defaultSettings: defaultLocaleSettings }) ?? defaultLocaleSettings;
|
|
787
|
+
return apiClient.request({
|
|
788
|
+
method: "POST",
|
|
789
|
+
url: `${basePath}/${apiClient.config().dataset}?projectId=${apiClient.config().projectId}`,
|
|
790
|
+
body: {
|
|
791
|
+
...request,
|
|
792
|
+
types,
|
|
793
|
+
userId: user?.id,
|
|
794
|
+
localeSettings,
|
|
795
|
+
maxPathDepth: assistConfig?.maxPathDepth
|
|
796
|
+
}
|
|
797
|
+
}).catch((e) => {
|
|
798
|
+
throw toast.push({
|
|
799
|
+
status: "error",
|
|
800
|
+
title: "Instruction failed",
|
|
801
|
+
description: e.message
|
|
802
|
+
}), e;
|
|
803
|
+
}).finally(() => {
|
|
804
|
+
setLoading(!1);
|
|
805
|
+
});
|
|
806
|
+
},
|
|
807
|
+
[apiClient, types, user, toast, assistConfig]
|
|
760
808
|
);
|
|
761
809
|
return useMemo(
|
|
762
810
|
() => ({
|
|
@@ -766,39 +814,6 @@ function useRunInstructionApi(apiClient) {
|
|
|
766
814
|
[runInstruction, loading]
|
|
767
815
|
);
|
|
768
816
|
}
|
|
769
|
-
const AiAssistanceConfigContext = createContext({});
|
|
770
|
-
function useAiAssistanceConfig() {
|
|
771
|
-
const context = useContext(AiAssistanceConfigContext);
|
|
772
|
-
if (!context)
|
|
773
|
-
throw new Error("Missing AiAssistanceConfigContext");
|
|
774
|
-
return context;
|
|
775
|
-
}
|
|
776
|
-
function AiAssistanceConfigProvider(props) {
|
|
777
|
-
const [status, setStatus] = useState(), [error, setError] = useState(), apiClient = useApiClient(props.config?.__customApiClient), { getInstructStatus, loading: statusLoading } = useGetInstructStatus(apiClient), { initInstruct, loading: initLoading } = useInitInstruct(apiClient);
|
|
778
|
-
useEffect(() => {
|
|
779
|
-
getInstructStatus().then((s) => setStatus(s)).catch((e) => {
|
|
780
|
-
console.error(e), setError(e);
|
|
781
|
-
});
|
|
782
|
-
}, [getInstructStatus]);
|
|
783
|
-
const init = useCallback(async () => {
|
|
784
|
-
setError(void 0);
|
|
785
|
-
try {
|
|
786
|
-
await initInstruct();
|
|
787
|
-
const status2 = await getInstructStatus();
|
|
788
|
-
setStatus(status2);
|
|
789
|
-
} catch (e) {
|
|
790
|
-
console.error("Failed to init ai assistance", e), setError(e);
|
|
791
|
-
}
|
|
792
|
-
}, [initInstruct, getInstructStatus, setStatus]), { config, children } = props, context = useMemo(() => ({
|
|
793
|
-
config,
|
|
794
|
-
status,
|
|
795
|
-
statusLoading,
|
|
796
|
-
init,
|
|
797
|
-
initLoading,
|
|
798
|
-
error
|
|
799
|
-
}), [config, status, init, statusLoading, initLoading, error]);
|
|
800
|
-
return /* @__PURE__ */ jsx(AiAssistanceConfigContext.Provider, { value: context, children });
|
|
801
|
-
}
|
|
802
817
|
const NO_INPUT = {}, RunInstructionContext = createContext({
|
|
803
818
|
runInstruction: () => {
|
|
804
819
|
},
|
|
@@ -1975,7 +1990,7 @@ function AssistInspector(props) {
|
|
|
1975
1990
|
mode: "ghost",
|
|
1976
1991
|
disabled: isEmptyPrompt || instructionLoading,
|
|
1977
1992
|
fontSize: 1,
|
|
1978
|
-
icon: instructionLoading ? /* @__PURE__ */ jsx(Spinner, {}) : PlayIcon,
|
|
1993
|
+
icon: instructionLoading ? /* @__PURE__ */ jsx(Spinner, { style: { marginTop: 3 } }) : PlayIcon,
|
|
1979
1994
|
onClick: runCurrentInstruction,
|
|
1980
1995
|
padding: 3,
|
|
1981
1996
|
text: "Run instruction"
|
|
@@ -3871,13 +3886,22 @@ const instructionForm = [
|
|
|
3871
3886
|
instructionTask,
|
|
3872
3887
|
contextDocumentSchema
|
|
3873
3888
|
], assist = definePlugin((config) => {
|
|
3874
|
-
const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "";
|
|
3889
|
+
const configWithDefaults = config ?? {}, styleguide = configWithDefaults.translate?.styleguide || "", maxPathDepth = configWithDefaults.assist?.maxPathDepth, temperature = configWithDefaults.assist?.temperature;
|
|
3875
3890
|
if (styleguide.length > 2e3)
|
|
3876
3891
|
throw new Error(
|
|
3877
3892
|
`[${packageName}]: \`translate.styleguide\` value is too long. It must be 2000 characters or less, was ${styleguide.length} characters`
|
|
3878
3893
|
);
|
|
3894
|
+
if (maxPathDepth !== void 0 && (maxPathDepth < 1 || maxPathDepth > 12))
|
|
3895
|
+
throw new Error(
|
|
3896
|
+
`[${packageName}]: \`assist.maxPathDepth\` must be be in the range [1,12] inclusive, but was ${maxPathDepth}`
|
|
3897
|
+
);
|
|
3898
|
+
if (temperature !== void 0 && (temperature < 0 || temperature > 1))
|
|
3899
|
+
throw new Error(
|
|
3900
|
+
`[${packageName}]: \`assist.maxPathDepth\` must be be in the range [0,1] inclusive, but was ${temperature}`
|
|
3901
|
+
);
|
|
3879
3902
|
return {
|
|
3880
3903
|
name: packageName,
|
|
3904
|
+
handlesGDR: !0,
|
|
3881
3905
|
schema: {
|
|
3882
3906
|
types: schemaTypes
|
|
3883
3907
|
},
|