@sanity/assist 2.0.0 → 2.0.2

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.js CHANGED
@@ -898,12 +898,15 @@ function TimeAgo(_ref3) {
898
898
  }
899
899
  function usePathKey(path) {
900
900
  return react.useMemo(() => {
901
- if (path.length) {
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
- fieldExists,
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,67 @@ function FieldsInitializer(_ref9) {
1850
1856
  let {
1851
1857
  pathKey,
1852
1858
  activePath,
1853
- fieldExists,
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 || fieldExists || activePath || !pathKey) {
1881
+ var _a;
1882
+ if (initialized.current || !pathKey) {
1883
+ return;
1884
+ }
1885
+ if (existingField && !(missingPresetInstructions == null ? void 0 : missingPresetInstructions.length)) {
1859
1886
  return;
1860
1887
  }
1861
- onChange([sanity.setIfMissing([], ["fields"]), sanity.insert([sanity.typed({
1862
- _key: pathKey,
1863
- _type: assistFieldTypeName,
1864
- path: pathKey
1865
- })], "after", ["fields", -1])]);
1888
+ let event = sanity.PatchEvent.from([sanity.setIfMissing([], ["fields"])]);
1889
+ if (!existingField) {
1890
+ event = event.append(sanity.insert([sanity.typed({
1891
+ _key: pathKey,
1892
+ _type: assistFieldTypeName,
1893
+ path: pathKey,
1894
+ instructions: []
1895
+ })], "after", ["fields", -1]));
1896
+ }
1897
+ if (!((_a = existingField == null ? void 0 : existingField.instructions) == null ? void 0 : _a.length)) {
1898
+ event = event.append([sanity.setIfMissing([], ["fields", {
1899
+ _key: pathKey
1900
+ }, "instructions"])]);
1901
+ }
1902
+ if (missingPresetInstructions == null ? void 0 : missingPresetInstructions.length) {
1903
+ event = event.append(sanity.insert(missingPresetInstructions.map(preset => {
1904
+ var _a2;
1905
+ return {
1906
+ ...preset,
1907
+ _type: "sanity.assist.instruction",
1908
+ prompt: (_a2 = preset.prompt) == null ? void 0 : _a2.map(p => ({
1909
+ markDefs: [],
1910
+ ...p
1911
+ }))
1912
+ };
1913
+ }), "after", ["fields", {
1914
+ _key: pathKey
1915
+ }, "instructions", -1]));
1916
+ }
1917
+ onChange(event);
1866
1918
  initialized.current = true;
1867
- }, [activePath, onChange, pathKey, fieldExists]);
1919
+ }, [activePath, onChange, pathKey, existingField, missingPresetInstructions]);
1868
1920
  return null;
1869
1921
  }
1870
1922
  function FieldAutocomplete(props) {
@@ -5323,7 +5375,11 @@ const contextDocumentSchema = sanity.defineType({
5323
5375
  // Strict ESM env, designed to run outside Node.js in envs that provide WebCrypto (deno, browsers, etc)
5324
5376
 
5325
5377
  function getRandomValues(typedArray) {
5326
- return window.crypto.getRandomValues(typedArray);
5378
+ const crypto = typeof window !== 'undefined' && 'crypto' in window ? window.crypto : globalThis.crypto;
5379
+ if (!crypto || !crypto.getRandomValues) {
5380
+ throw new Error('WebCrypto not available in this environment');
5381
+ }
5382
+ return crypto.getRandomValues(typedArray);
5327
5383
  }
5328
5384
  function whatwgRNG() {
5329
5385
  let length = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : 16;
@@ -5331,7 +5387,7 @@ function whatwgRNG() {
5331
5387
  getRandomValues(rnds8);
5332
5388
  return rnds8;
5333
5389
  }
5334
- const getByteHexTable = (() => {
5390
+ const getByteHexTable = /* @__PURE__ */(() => {
5335
5391
  let table;
5336
5392
  return () => {
5337
5393
  if (table) {
@@ -6143,7 +6199,8 @@ function ImageContextProvider(props) {
6143
6199
  documentSchemaType
6144
6200
  } = useAssistDocumentContext();
6145
6201
  const {
6146
- config
6202
+ config,
6203
+ status
6147
6204
  } = useAiAssistanceConfig();
6148
6205
  const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
6149
6206
  const {
@@ -6156,12 +6213,14 @@ function ImageContextProvider(props) {
6156
6213
  const descriptionField = getDescriptionFieldOption(schemaType);
6157
6214
  if (assetRef && documentId && descriptionField && assetRef !== assetRefState && !isSyncing) {
6158
6215
  setAssetRefState(assetRef);
6159
- generateCaption({
6160
- path: sanity.pathToString([...path, descriptionField]),
6161
- documentId
6162
- });
6216
+ if (canUseAssist(status)) {
6217
+ generateCaption({
6218
+ path: sanity.pathToString([...path, descriptionField]),
6219
+ documentId
6220
+ });
6221
+ }
6163
6222
  }
6164
- }, [schemaType, path, assetRef, assetRefState, documentId, generateCaption, isSyncing]);
6223
+ }, [schemaType, path, assetRef, assetRefState, documentId, generateCaption, isSyncing, status]);
6165
6224
  const context = react.useMemo(() => {
6166
6225
  const descriptionField = getDescriptionFieldOption(schemaType);
6167
6226
  const imageInstructionField = getImageInstructionFieldOption(schemaType);
@@ -6184,7 +6243,11 @@ const generateCaptionsActions = {
6184
6243
  useAction(props) {
6185
6244
  const pathKey = usePathKey(props.path);
6186
6245
  const {
6187
- config
6246
+ openInspector
6247
+ } = desk.useDocumentPane();
6248
+ const {
6249
+ config,
6250
+ status
6188
6251
  } = useAiAssistanceConfig();
6189
6252
  const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
6190
6253
  const {
@@ -6214,6 +6277,13 @@ const generateCaptionsActions = {
6214
6277
  if (loading) {
6215
6278
  return;
6216
6279
  }
6280
+ if (!canUseAssist(status)) {
6281
+ openInspector(aiInspectorId, {
6282
+ [fieldPathParam]: pathKey,
6283
+ [instructionParam]: void 0
6284
+ });
6285
+ return;
6286
+ }
6217
6287
  generateCaption({
6218
6288
  path: pathKey,
6219
6289
  documentId: documentId != null ? documentId : ""
@@ -6223,7 +6293,7 @@ const generateCaptionsActions = {
6223
6293
  disabled: loading,
6224
6294
  hidden: !imageContext.assetRef
6225
6295
  });
6226
- }, [generateCaption, pathKey, documentId, loading, imageContext]);
6296
+ }, [generateCaption, pathKey, documentId, loading, imageContext, status, openInspector]);
6227
6297
  }
6228
6298
  return void 0;
6229
6299
  }