@sanity/assist 2.0.0 → 2.0.1
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/dist/index.d.ts +82 -0
- package/dist/index.esm.js +82 -21
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +82 -21
- package/dist/index.js.map +1 -1
- package/package.json +3 -3
- package/src/assistDocument/components/AssistDocumentForm.tsx +65 -21
- package/src/components/ImageContext.tsx +11 -4
- package/src/fieldActions/generateCaptionActions.tsx +15 -4
- package/src/helpers/misc.ts +8 -4
- package/src/plugin.tsx +6 -1
- package/src/types.ts +34 -3
- package/src/useApiClient.ts +4 -0
package/dist/index.js
CHANGED
|
@@ -898,12 +898,15 @@ function TimeAgo(_ref3) {
|
|
|
898
898
|
}
|
|
899
899
|
function usePathKey(path) {
|
|
900
900
|
return react.useMemo(() => {
|
|
901
|
-
|
|
902
|
-
return Array.isArray(path) ? sanity.pathToString(path) : path;
|
|
903
|
-
}
|
|
904
|
-
return documentRootKey;
|
|
901
|
+
return getPathKey(path);
|
|
905
902
|
}, [path]);
|
|
906
903
|
}
|
|
904
|
+
function getPathKey(path) {
|
|
905
|
+
if (path.length) {
|
|
906
|
+
return Array.isArray(path) ? sanity.pathToString(path) : path;
|
|
907
|
+
}
|
|
908
|
+
return documentRootKey;
|
|
909
|
+
}
|
|
907
910
|
function getInstructionTitle(instruction) {
|
|
908
911
|
var _a;
|
|
909
912
|
return (_a = instruction == null ? void 0 : instruction.title) != null ? _a : "Untitled";
|
|
@@ -1112,6 +1115,9 @@ function TaskItem(props) {
|
|
|
1112
1115
|
});
|
|
1113
1116
|
}
|
|
1114
1117
|
const basePath = "/assist/tasks/instruction";
|
|
1118
|
+
function canUseAssist(status) {
|
|
1119
|
+
return (status == null ? void 0 : status.enabled) && status.initialized && status.validToken;
|
|
1120
|
+
}
|
|
1115
1121
|
function useApiClient(customApiClient) {
|
|
1116
1122
|
const client = sanity.useClient({
|
|
1117
1123
|
apiVersion: "2023-06-05"
|
|
@@ -1770,7 +1776,6 @@ function AssistDocumentFormEditable(props) {
|
|
|
1770
1776
|
onChange(sanity.set((_a = documentSchema.title) != null ? _a : documentSchema.name, ["title"]));
|
|
1771
1777
|
}
|
|
1772
1778
|
}, [title, documentSchema, onChange, id]);
|
|
1773
|
-
const fieldExists = !!(fields == null ? void 0 : fields.some(f => f._key === typePath));
|
|
1774
1779
|
const {
|
|
1775
1780
|
onPathOpen,
|
|
1776
1781
|
...formCallbacks
|
|
@@ -1802,7 +1807,8 @@ function AssistDocumentFormEditable(props) {
|
|
|
1802
1807
|
children: [/* @__PURE__ */jsxRuntime.jsx(FieldsInitializer, {
|
|
1803
1808
|
pathKey: typePath,
|
|
1804
1809
|
activePath,
|
|
1805
|
-
|
|
1810
|
+
fields,
|
|
1811
|
+
documentSchema,
|
|
1806
1812
|
onChange
|
|
1807
1813
|
}, typePath), instruction && /* @__PURE__ */jsxRuntime.jsx(BackToInstructionListLink, {}), activePath && /* @__PURE__ */jsxRuntime.jsx(sanity.FormCallbacksProvider, {
|
|
1808
1814
|
...newCallbacks,
|
|
@@ -1850,21 +1856,60 @@ function FieldsInitializer(_ref9) {
|
|
|
1850
1856
|
let {
|
|
1851
1857
|
pathKey,
|
|
1852
1858
|
activePath,
|
|
1853
|
-
|
|
1859
|
+
fields,
|
|
1860
|
+
documentSchema,
|
|
1854
1861
|
onChange
|
|
1855
1862
|
} = _ref9;
|
|
1863
|
+
const {
|
|
1864
|
+
config: {
|
|
1865
|
+
__presets: presets
|
|
1866
|
+
}
|
|
1867
|
+
} = useAiAssistanceConfig();
|
|
1868
|
+
const existingField = fields == null ? void 0 : fields.find(f => f._key === pathKey);
|
|
1869
|
+
const documentPresets = !!(documentSchema == null ? void 0 : documentSchema.name) && (presets == null ? void 0 : presets[documentSchema == null ? void 0 : documentSchema.name]);
|
|
1870
|
+
const missingPresetInstructions = react.useMemo(() => {
|
|
1871
|
+
var _a, _b;
|
|
1872
|
+
if (!documentPresets || !pathKey) {
|
|
1873
|
+
return void 0;
|
|
1874
|
+
}
|
|
1875
|
+
const existingInstructions = existingField == null ? void 0 : existingField.instructions;
|
|
1876
|
+
const presetField = (_a = documentPresets.fields) == null ? void 0 : _a.find(f => f.path === pathKey);
|
|
1877
|
+
return (_b = presetField == null ? void 0 : presetField.instructions) == null ? void 0 : _b.filter(i => !(existingInstructions == null ? void 0 : existingInstructions.some(ei => ei._key === i._key)));
|
|
1878
|
+
}, [documentPresets, pathKey, existingField]);
|
|
1856
1879
|
const initialized = react.useRef(false);
|
|
1857
1880
|
react.useEffect(() => {
|
|
1858
|
-
if (initialized.current ||
|
|
1881
|
+
if (initialized.current || !pathKey) {
|
|
1859
1882
|
return;
|
|
1860
1883
|
}
|
|
1861
|
-
|
|
1862
|
-
|
|
1863
|
-
|
|
1864
|
-
|
|
1865
|
-
|
|
1884
|
+
if (existingField && !(missingPresetInstructions == null ? void 0 : missingPresetInstructions.length)) {
|
|
1885
|
+
return;
|
|
1886
|
+
}
|
|
1887
|
+
let event = sanity.PatchEvent.from([sanity.setIfMissing([], ["fields"])]);
|
|
1888
|
+
if (!existingField) {
|
|
1889
|
+
event = event.append(sanity.insert([sanity.typed({
|
|
1890
|
+
_key: pathKey,
|
|
1891
|
+
_type: assistFieldTypeName,
|
|
1892
|
+
path: pathKey
|
|
1893
|
+
})], "after", ["fields", -1]));
|
|
1894
|
+
}
|
|
1895
|
+
if (missingPresetInstructions == null ? void 0 : missingPresetInstructions.length) {
|
|
1896
|
+
event = event.append(sanity.insert(missingPresetInstructions.map(preset => {
|
|
1897
|
+
var _a;
|
|
1898
|
+
return {
|
|
1899
|
+
...preset,
|
|
1900
|
+
_type: "sanity.assist.instruction",
|
|
1901
|
+
prompt: (_a = preset.prompt) == null ? void 0 : _a.map(p => ({
|
|
1902
|
+
markDefs: [],
|
|
1903
|
+
...p
|
|
1904
|
+
}))
|
|
1905
|
+
};
|
|
1906
|
+
}), "after", ["fields", {
|
|
1907
|
+
_key: pathKey
|
|
1908
|
+
}, "instructions", -1]));
|
|
1909
|
+
}
|
|
1910
|
+
onChange(event);
|
|
1866
1911
|
initialized.current = true;
|
|
1867
|
-
}, [activePath, onChange, pathKey,
|
|
1912
|
+
}, [activePath, onChange, pathKey, existingField, missingPresetInstructions]);
|
|
1868
1913
|
return null;
|
|
1869
1914
|
}
|
|
1870
1915
|
function FieldAutocomplete(props) {
|
|
@@ -5323,7 +5368,11 @@ const contextDocumentSchema = sanity.defineType({
|
|
|
5323
5368
|
// Strict ESM env, designed to run outside Node.js in envs that provide WebCrypto (deno, browsers, etc)
|
|
5324
5369
|
|
|
5325
5370
|
function getRandomValues(typedArray) {
|
|
5326
|
-
|
|
5371
|
+
const crypto = typeof window !== 'undefined' && 'crypto' in window ? window.crypto : globalThis.crypto;
|
|
5372
|
+
if (!crypto || !crypto.getRandomValues) {
|
|
5373
|
+
throw new Error('WebCrypto not available in this environment');
|
|
5374
|
+
}
|
|
5375
|
+
return crypto.getRandomValues(typedArray);
|
|
5327
5376
|
}
|
|
5328
5377
|
function whatwgRNG() {
|
|
5329
5378
|
let length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 16;
|
|
@@ -5331,7 +5380,7 @@ function whatwgRNG() {
|
|
|
5331
5380
|
getRandomValues(rnds8);
|
|
5332
5381
|
return rnds8;
|
|
5333
5382
|
}
|
|
5334
|
-
const getByteHexTable = (() => {
|
|
5383
|
+
const getByteHexTable = /* @__PURE__ */(() => {
|
|
5335
5384
|
let table;
|
|
5336
5385
|
return () => {
|
|
5337
5386
|
if (table) {
|
|
@@ -6143,7 +6192,8 @@ function ImageContextProvider(props) {
|
|
|
6143
6192
|
documentSchemaType
|
|
6144
6193
|
} = useAssistDocumentContext();
|
|
6145
6194
|
const {
|
|
6146
|
-
config
|
|
6195
|
+
config,
|
|
6196
|
+
status
|
|
6147
6197
|
} = useAiAssistanceConfig();
|
|
6148
6198
|
const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
|
|
6149
6199
|
const {
|
|
@@ -6154,14 +6204,14 @@ function ImageContextProvider(props) {
|
|
|
6154
6204
|
} = sanity.useSyncState(publicId(documentId), documentSchemaType.name);
|
|
6155
6205
|
react.useEffect(() => {
|
|
6156
6206
|
const descriptionField = getDescriptionFieldOption(schemaType);
|
|
6157
|
-
if (assetRef && documentId && descriptionField && assetRef !== assetRefState && !isSyncing) {
|
|
6207
|
+
if (assetRef && documentId && descriptionField && assetRef !== assetRefState && !isSyncing && canUseAssist(status)) {
|
|
6158
6208
|
setAssetRefState(assetRef);
|
|
6159
6209
|
generateCaption({
|
|
6160
6210
|
path: sanity.pathToString([...path, descriptionField]),
|
|
6161
6211
|
documentId
|
|
6162
6212
|
});
|
|
6163
6213
|
}
|
|
6164
|
-
}, [schemaType, path, assetRef, assetRefState, documentId, generateCaption, isSyncing]);
|
|
6214
|
+
}, [schemaType, path, assetRef, assetRefState, documentId, generateCaption, isSyncing, status]);
|
|
6165
6215
|
const context = react.useMemo(() => {
|
|
6166
6216
|
const descriptionField = getDescriptionFieldOption(schemaType);
|
|
6167
6217
|
const imageInstructionField = getImageInstructionFieldOption(schemaType);
|
|
@@ -6184,7 +6234,11 @@ const generateCaptionsActions = {
|
|
|
6184
6234
|
useAction(props) {
|
|
6185
6235
|
const pathKey = usePathKey(props.path);
|
|
6186
6236
|
const {
|
|
6187
|
-
|
|
6237
|
+
openInspector
|
|
6238
|
+
} = desk.useDocumentPane();
|
|
6239
|
+
const {
|
|
6240
|
+
config,
|
|
6241
|
+
status
|
|
6188
6242
|
} = useAiAssistanceConfig();
|
|
6189
6243
|
const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
|
|
6190
6244
|
const {
|
|
@@ -6214,6 +6268,13 @@ const generateCaptionsActions = {
|
|
|
6214
6268
|
if (loading) {
|
|
6215
6269
|
return;
|
|
6216
6270
|
}
|
|
6271
|
+
if (!canUseAssist(status)) {
|
|
6272
|
+
openInspector(aiInspectorId, {
|
|
6273
|
+
[fieldPathParam]: pathKey,
|
|
6274
|
+
[instructionParam]: void 0
|
|
6275
|
+
});
|
|
6276
|
+
return;
|
|
6277
|
+
}
|
|
6217
6278
|
generateCaption({
|
|
6218
6279
|
path: pathKey,
|
|
6219
6280
|
documentId: documentId != null ? documentId : ""
|
|
@@ -6223,7 +6284,7 @@ const generateCaptionsActions = {
|
|
|
6223
6284
|
disabled: loading,
|
|
6224
6285
|
hidden: !imageContext.assetRef
|
|
6225
6286
|
});
|
|
6226
|
-
}, [generateCaption, pathKey, documentId, loading, imageContext]);
|
|
6287
|
+
}, [generateCaption, pathKey, documentId, loading, imageContext, status, openInspector]);
|
|
6227
6288
|
}
|
|
6228
6289
|
return void 0;
|
|
6229
6290
|
}
|