@sanity/assist 1.2.15-lang.2 → 1.2.15-lang.4

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.esm.js CHANGED
@@ -137,6 +137,17 @@ function getCaptionFieldOption(schemaType) {
137
137
  }
138
138
  return getCaptionFieldOption(schemaType.type);
139
139
  }
140
+ function getImagePromptFieldOption(schemaType) {
141
+ var _a;
142
+ if (!schemaType) {
143
+ return void 0;
144
+ }
145
+ const imagePromptField = (_a = schemaType.options) == null ? void 0 : _a.imagePromptField;
146
+ if (imagePromptField) {
147
+ return imagePromptField;
148
+ }
149
+ return getImagePromptFieldOption(schemaType.type);
150
+ }
140
151
  function isSchemaAssistEnabled(type) {
141
152
  var _a, _b;
142
153
  return !((_b = (_a = type.options) == null ? void 0 : _a.aiWritingAssistance) == null ? void 0 : _b.exclude);
@@ -971,6 +982,7 @@ function useTranslate(apiClient) {
971
982
  let {
972
983
  documentId,
973
984
  languagePath,
985
+ translatePath,
974
986
  fieldLanguageMap
975
987
  } = _ref4;
976
988
  setLoading(true);
@@ -982,6 +994,7 @@ function useTranslate(apiClient) {
982
994
  types,
983
995
  languagePath,
984
996
  fieldLanguageMap,
997
+ translatePath: translatePath.length === 0 ? documentRootKey : pathToString(translatePath),
985
998
  userId: user == null ? void 0 : user.id
986
999
  }
987
1000
  }).catch(e => {
@@ -1045,6 +1058,48 @@ function useGenerateCaption(apiClient) {
1045
1058
  loading
1046
1059
  }), [generateCaption, loading]);
1047
1060
  }
1061
+ function useGenerateImage(apiClient) {
1062
+ const [loading, setLoading] = useState(false);
1063
+ const user = useCurrentUser();
1064
+ const schema = useSchema();
1065
+ const types = useMemo(() => serializeSchema(schema, {
1066
+ leanFormat: true
1067
+ }), [schema]);
1068
+ const toast = useToast();
1069
+ const generateImage = useCallback(_ref6 => {
1070
+ let {
1071
+ path,
1072
+ documentId
1073
+ } = _ref6;
1074
+ setLoading(true);
1075
+ return apiClient.request({
1076
+ method: "POST",
1077
+ url: "/assist/tasks/generate-image/".concat(apiClient.config().dataset, "?projectId=").concat(apiClient.config().projectId),
1078
+ body: {
1079
+ path,
1080
+ documentId,
1081
+ types,
1082
+ userId: user == null ? void 0 : user.id
1083
+ }
1084
+ }).catch(e => {
1085
+ toast.push({
1086
+ status: "error",
1087
+ title: "Generate image from prompt failed",
1088
+ description: e.message
1089
+ });
1090
+ setLoading(false);
1091
+ throw e;
1092
+ }).finally(() => {
1093
+ setTimeout(() => {
1094
+ setLoading(false);
1095
+ }, 2e3);
1096
+ });
1097
+ }, [setLoading, apiClient, toast, user, types]);
1098
+ return useMemo(() => ({
1099
+ generateImage,
1100
+ loading
1101
+ }), [generateImage, loading]);
1102
+ }
1048
1103
  function useGetInstructStatus(apiClient) {
1049
1104
  const [loading, setLoading] = useState(true);
1050
1105
  const getInstructStatus = useCallback(async () => {
@@ -1234,8 +1289,8 @@ function RunInstructionProvider(props) {
1234
1289
  runInstructionRequest({
1235
1290
  ...request,
1236
1291
  instructionKey: instruction._key,
1237
- userTexts: Object.entries(inputs).map(_ref6 => {
1238
- let [key, value] = _ref6;
1292
+ userTexts: Object.entries(inputs).map(_ref7 => {
1293
+ let [key, value] = _ref7;
1239
1294
  return {
1240
1295
  blockKey: key,
1241
1296
  userInput: value
@@ -1248,8 +1303,8 @@ function RunInstructionProvider(props) {
1248
1303
  const open = !!runRequest;
1249
1304
  const runDisabled = useMemo(() => {
1250
1305
  var _a2, _b;
1251
- return ((_b = (_a2 = runRequest == null ? void 0 : runRequest.userInputBlocks) == null ? void 0 : _a2.length) != null ? _b : 0) > Object.entries(inputs).filter(_ref7 => {
1252
- let [, value] = _ref7;
1306
+ return ((_b = (_a2 = runRequest == null ? void 0 : runRequest.userInputBlocks) == null ? void 0 : _a2.length) != null ? _b : 0) > Object.entries(inputs).filter(_ref8 => {
1307
+ let [, value] = _ref8;
1253
1308
  return !!value;
1254
1309
  }).length;
1255
1310
  }, [runRequest == null ? void 0 : runRequest.userInputBlocks, inputs]);
@@ -1641,13 +1696,13 @@ function useSelectedSchema(fieldPath, documentSchema) {
1641
1696
  return currentSchema;
1642
1697
  }, [documentSchema, fieldPath]);
1643
1698
  }
1644
- function FieldsInitializer(_ref8) {
1699
+ function FieldsInitializer(_ref9) {
1645
1700
  let {
1646
1701
  pathKey,
1647
1702
  activePath,
1648
1703
  fieldExists,
1649
1704
  onChange
1650
- } = _ref8;
1705
+ } = _ref9;
1651
1706
  const initialized = useRef(false);
1652
1707
  useEffect(() => {
1653
1708
  if (initialized.current || fieldExists || activePath || !pathKey) {
@@ -2175,10 +2230,10 @@ const assistInspector = {
2175
2230
  showAsAction: false
2176
2231
  }),
2177
2232
  component: AssistInspectorWrapper,
2178
- onClose(_ref9) {
2233
+ onClose(_ref10) {
2179
2234
  let {
2180
2235
  params
2181
- } = _ref9;
2236
+ } = _ref10;
2182
2237
  return {
2183
2238
  params: typed({
2184
2239
  ...params,
@@ -2251,11 +2306,11 @@ var __template$3 = (cooked, raw) => __freeze$3(__defProp$3(cooked, "raw", {
2251
2306
  var _a$3, _b$1;
2252
2307
  const fadeIn = keyframes(_a$3 || (_a$3 = __template$3(["\n 0% {\n opacity: 0;\n transform: scale(0.75);\n }\n 40% {\n opacity: 0;\n transform: scale(0.75);\n }\n 100% {\n opacity: 1;\n transform: scale(1);\n }\n"])));
2253
2308
  const FadeInDiv = styled.div(_b$1 || (_b$1 = __template$3(["\n animation-name: ", ";\n animation-timing-function: ease-in-out;\n"])), fadeIn);
2254
- const FadeInContent = forwardRef(function FadeInContent2(_ref10, ref) {
2309
+ const FadeInContent = forwardRef(function FadeInContent2(_ref11, ref) {
2255
2310
  let {
2256
2311
  children,
2257
2312
  durationMs = 250
2258
- } = _ref10;
2313
+ } = _ref11;
2259
2314
  return /* @__PURE__ */jsx(FadeInDiv, {
2260
2315
  ref,
2261
2316
  style: {
@@ -3316,7 +3371,7 @@ const defaultLanguageOutputs = function (member, enclosingType, translateFromLan
3316
3371
  }
3317
3372
  return void 0;
3318
3373
  };
3319
- function getTranslationMap(documentSchema, documentMembers, translateFromLanguageId, outputLanguageIds, langFn) {
3374
+ function getFieldLanguageMap(documentSchema, documentMembers, translateFromLanguageId, outputLanguageIds, langFn) {
3320
3375
  var _a, _b, _c;
3321
3376
  const translationMaps = [];
3322
3377
  for (const member of documentMembers) {
@@ -4545,16 +4600,29 @@ const getLanguageParams = (select, document) => {
4545
4600
  }
4546
4601
  return selectedValue;
4547
4602
  };
4603
+ const toFieldLanguagesKeyPrefix = "sanityStudio:assist:field-languages:from:";
4604
+ function getPreferredToFieldLanguages(fromLanguageId) {
4605
+ if (typeof localStorage === "undefined") {
4606
+ return [];
4607
+ }
4608
+ const value = localStorage.getItem("".concat(toFieldLanguagesKeyPrefix).concat(fromLanguageId));
4609
+ return value ? JSON.parse(value) : [];
4610
+ }
4611
+ function setPreferredToFieldLanguages(fromLanguageId, languageIds) {
4612
+ if (typeof localStorage === "undefined") {
4613
+ return;
4614
+ }
4615
+ localStorage.setItem("".concat(toFieldLanguagesKeyPrefix).concat(fromLanguageId), JSON.stringify(languageIds));
4616
+ }
4548
4617
  const FieldTranslationContext = createContext({
4549
4618
  openFieldTranslation: () => {},
4550
4619
  translationLoading: false
4551
- //loadLanguages: () => {},
4552
4620
  });
4553
4621
  function useFieldTranslation() {
4554
4622
  return useContext(FieldTranslationContext);
4555
4623
  }
4556
4624
  function FieldTranslationProvider(props) {
4557
- var _a, _b;
4625
+ var _a, _b, _c;
4558
4626
  const {
4559
4627
  config: assistConfig
4560
4628
  } = useAiAssistanceConfig();
@@ -4564,64 +4632,63 @@ function FieldTranslationProvider(props) {
4564
4632
  translate: runTranslate
4565
4633
  } = useTranslate(apiClient);
4566
4634
  const [dialogOpen, setDialogOpen] = useState(false);
4567
- const [document, setDocument] = useState();
4568
- const [documentSchema, setDocumentSchema] = useState();
4635
+ const [fieldTranslationParams, setFieldTranslationParams] = useState();
4569
4636
  const [languages, setLanguages] = useState();
4570
4637
  const [fromLanguage, setFromLanguage] = useState(void 0);
4571
4638
  const [toLanguages, setToLanguages] = useState(void 0);
4572
- const [translationMap, setTranslationMap] = useState();
4639
+ const [fieldLanguageMaps, setFieldLanguageMaps] = useState();
4573
4640
  const close = useCallback(() => {
4574
4641
  setDialogOpen(false);
4575
4642
  setLanguages(void 0);
4576
- setDocument(void 0);
4577
- setDocument(void 0);
4643
+ setFieldTranslationParams(void 0);
4578
4644
  }, []);
4579
4645
  const languageClient = useClient({
4580
4646
  apiVersion: (_b = config == null ? void 0 : config.apiVersion) != null ? _b : "2022-11-27"
4581
4647
  });
4582
- const documentId = document == null ? void 0 : document._id;
4648
+ const documentId = (_c = fieldTranslationParams == null ? void 0 : fieldTranslationParams.document) == null ? void 0 : _c._id;
4583
4649
  const id = useId();
4584
- const selectFromLanguage = useCallback((from, languages2, document2, documentSchema2) => {
4650
+ const selectFromLanguage = useCallback((from, languages2, params) => {
4585
4651
  var _a2, _b2;
4652
+ const {
4653
+ document,
4654
+ documentSchema
4655
+ } = params != null ? params : {};
4586
4656
  setFromLanguage(from);
4587
- if (!document2 || !documentSchema2 || !languages2) {
4588
- setTranslationMap(void 0);
4657
+ if (!document || !documentSchema || !params || !languages2) {
4658
+ setFieldLanguageMaps(void 0);
4589
4659
  return;
4590
4660
  }
4591
- const to = languages2.filter(l => l.id !== (from == null ? void 0 : from.id));
4661
+ const preferred = getPreferredToFieldLanguages(from.id);
4662
+ const to = languages2.filter(l => l.id !== (from == null ? void 0 : from.id)).filter(l => !preferred.length || preferred.includes(l.id));
4592
4663
  setToLanguages(to);
4593
4664
  const fromId = from == null ? void 0 : from.id;
4594
4665
  const toIds = (_a2 = to == null ? void 0 : to.map(l => l.id)) != null ? _a2 : [];
4595
- const docMembers = getDocumentMembersFlat(document2, documentSchema2);
4666
+ const docMembers = getDocumentMembersFlat(document, documentSchema);
4596
4667
  if (fromId && (toIds == null ? void 0 : toIds.length)) {
4597
- const transMap = getTranslationMap(documentSchema2, docMembers, fromId, toIds, (_b2 = config == null ? void 0 : config.translationOutputs) != null ? _b2 : defaultLanguageOutputs);
4598
- setTranslationMap(transMap);
4668
+ const transMap = getFieldLanguageMap(documentSchema, docMembers, fromId, toIds, (_b2 = config == null ? void 0 : config.translationOutputs) != null ? _b2 : defaultLanguageOutputs);
4669
+ setFieldLanguageMaps(transMap);
4599
4670
  } else {
4600
- setTranslationMap(void 0);
4671
+ setFieldLanguageMaps(void 0);
4601
4672
  }
4602
4673
  }, [config]);
4603
4674
  const toggleToLanguage = useCallback((toggledLang, toLanguages2, languages2) => {
4604
- if (!languages2) {
4675
+ if (!languages2 || !fromLanguage) {
4605
4676
  return;
4606
4677
  }
4607
4678
  const wasSelected = !!(toLanguages2 == null ? void 0 : toLanguages2.find(l => l.id === toggledLang.id));
4608
4679
  const newToLangs = languages2.filter(anyLang => !!(toLanguages2 == null ? void 0 : toLanguages2.find(selectedLang => toggledLang.id !== selectedLang.id && selectedLang.id === anyLang.id)) || toggledLang.id === anyLang.id && !wasSelected);
4609
4680
  setToLanguages(newToLangs);
4610
- }, []);
4611
- const openFieldTranslation = useCallback(async _ref11 => {
4612
- let {
4613
- document: document2,
4614
- documentSchema: documentSchema2
4615
- } = _ref11;
4681
+ setPreferredToFieldLanguages(fromLanguage.id, newToLangs.map(l => l.id));
4682
+ }, [fromLanguage]);
4683
+ const openFieldTranslation = useCallback(async params => {
4616
4684
  setDialogOpen(true);
4617
- const languageParams = getLanguageParams(config == null ? void 0 : config.selectLanguageParams, document2);
4685
+ const languageParams = getLanguageParams(config == null ? void 0 : config.selectLanguageParams, params.document);
4618
4686
  const languages2 = await (typeof (config == null ? void 0 : config.languages) === "function" ? config == null ? void 0 : config.languages(languageClient, languageParams) : Promise.resolve(config == null ? void 0 : config.languages));
4619
4687
  setLanguages(languages2);
4620
- setDocument(document2);
4621
- setDocumentSchema(documentSchema2);
4688
+ setFieldTranslationParams(params);
4622
4689
  const fromLanguage2 = languages2 == null ? void 0 : languages2[0];
4623
4690
  if (fromLanguage2) {
4624
- selectFromLanguage(fromLanguage2, languages2, document2, documentSchema2);
4691
+ selectFromLanguage(fromLanguage2, languages2, params);
4625
4692
  } else {
4626
4693
  console.error("No languages available for selected language params", languageParams);
4627
4694
  }
@@ -4632,12 +4699,14 @@ function FieldTranslationProvider(props) {
4632
4699
  translationLoading: false
4633
4700
  };
4634
4701
  }, [openFieldTranslation]);
4635
- const runDisabled = !fromLanguage || !(toLanguages == null ? void 0 : toLanguages.length) || !(translationMap == null ? void 0 : translationMap.length) || !documentId;
4702
+ const runDisabled = !fromLanguage || !(toLanguages == null ? void 0 : toLanguages.length) || !(fieldLanguageMaps == null ? void 0 : fieldLanguageMaps.length) || !documentId;
4636
4703
  const onRunTranslation = useCallback(() => {
4637
- if (translationMap && documentId) {
4704
+ const translatePath = fieldTranslationParams == null ? void 0 : fieldTranslationParams.translatePath;
4705
+ if (fieldLanguageMaps && documentId && translatePath) {
4638
4706
  runTranslate({
4639
4707
  documentId,
4640
- fieldLanguageMap: translationMap.map(map => ({
4708
+ translatePath: fieldTranslationParams == null ? void 0 : fieldTranslationParams.translatePath,
4709
+ fieldLanguageMap: fieldLanguageMaps.map(map => ({
4641
4710
  ...map,
4642
4711
  // eslint-disable-next-line max-nested-callbacks
4643
4712
  outputs: map.outputs.filter(out => !!(toLanguages == null ? void 0 : toLanguages.find(l => l.id === out.id)))
@@ -4645,7 +4714,7 @@ function FieldTranslationProvider(props) {
4645
4714
  });
4646
4715
  }
4647
4716
  close();
4648
- }, [translationMap, documentId, runTranslate, close, toLanguages]);
4717
+ }, [fieldLanguageMaps, documentId, runTranslate, close, toLanguages, fieldTranslationParams == null ? void 0 : fieldTranslationParams.translatePath]);
4649
4718
  const runButton = /* @__PURE__ */jsx(Button, {
4650
4719
  text: "Translate",
4651
4720
  tone: "primary",
@@ -4704,7 +4773,7 @@ function FieldTranslationProvider(props) {
4704
4773
  name: "fromLang",
4705
4774
  value: l.id,
4706
4775
  checked: l.id === (fromLanguage == null ? void 0 : fromLanguage.id),
4707
- onClick: () => selectFromLanguage(l, languages, document, documentSchema)
4776
+ onClick: () => selectFromLanguage(l, languages, fieldTranslationParams)
4708
4777
  }), /* @__PURE__ */jsx(Text, {
4709
4778
  children: (_a2 = l.title) != null ? _a2 : l.id
4710
4779
  })]
@@ -4727,8 +4796,7 @@ function FieldTranslationProvider(props) {
4727
4796
  name: "toLang",
4728
4797
  value: l.id,
4729
4798
  checked: !!(toLanguages == null ? void 0 : toLanguages.find(tl => tl.id === l.id)),
4730
- onClick: () => toggleToLanguage(l, toLanguages, languages),
4731
- disabled: !(translationMap == null ? void 0 : translationMap.find(tm => tm.outputs.find(o => o.id === l.id)))
4799
+ onClick: () => toggleToLanguage(l, toLanguages, languages)
4732
4800
  }), /* @__PURE__ */jsx(Text, {
4733
4801
  children: (_a2 = l.title) != null ? _a2 : l.id
4734
4802
  })]
@@ -4793,7 +4861,10 @@ function ErrorWrapper(props) {
4793
4861
  const catchError = useCallback(params => {
4794
4862
  setError(params.error);
4795
4863
  }, [setError]);
4796
- const unsetValue = useCallback(() => onChange(PatchEvent.from(unset())), [onChange]);
4864
+ const unsetValue = useCallback(() => {
4865
+ onChange(PatchEvent.from(unset()));
4866
+ setError(void 0);
4867
+ }, [onChange]);
4797
4868
  const dismiss = useCallback(() => setError(void 0), []);
4798
4869
  const catcher = /* @__PURE__ */jsx(ErrorBoundary, {
4799
4870
  onCatch: catchError,
@@ -6101,7 +6172,7 @@ function PrivateIcon() {
6101
6172
  children: /* @__PURE__ */jsx(LockIcon, {})
6102
6173
  });
6103
6174
  }
6104
- const ImageContext = createContext(void 0);
6175
+ const ImageContext = createContext({});
6105
6176
  function ImageContextProvider(props) {
6106
6177
  var _a;
6107
6178
  const {
@@ -6137,17 +6208,19 @@ function ImageContextProvider(props) {
6137
6208
  }, [schemaType, path, assetRef, assetRefState, documentId, generateCaption, isSyncing]);
6138
6209
  const context = useMemo(() => {
6139
6210
  const captionField = getCaptionFieldOption(schemaType);
6140
- return captionField ? {
6141
- captionPath: pathToString([...path, captionField]),
6211
+ const imagePromptField = getImagePromptFieldOption(schemaType);
6212
+ return {
6213
+ captionPath: captionField ? pathToString([...path, captionField]) : void 0,
6214
+ imagePromptPath: imagePromptField ? pathToString([...path, imagePromptField]) : void 0,
6142
6215
  assetRef
6143
- } : void 0;
6216
+ };
6144
6217
  }, [schemaType, path, assetRef]);
6145
6218
  return /* @__PURE__ */jsx(ImageContext.Provider, {
6146
6219
  value: context,
6147
6220
  children: props.renderDefault(props)
6148
6221
  });
6149
6222
  }
6150
- function node$2(node2) {
6223
+ function node$3(node2) {
6151
6224
  return node2;
6152
6225
  }
6153
6226
  const generateCaptionsActions = {
@@ -6168,7 +6241,7 @@ const generateCaptionsActions = {
6168
6241
  documentId
6169
6242
  } = useAssistDocumentContext();
6170
6243
  return useMemo(() => {
6171
- return node$2({
6244
+ return node$3({
6172
6245
  type: "action",
6173
6246
  icon: loading ? () => /* @__PURE__ */jsx(Box, {
6174
6247
  style: {
@@ -6199,27 +6272,31 @@ const generateCaptionsActions = {
6199
6272
  return void 0;
6200
6273
  }
6201
6274
  };
6202
- function node$1(node2) {
6275
+ function node$2(node2) {
6203
6276
  return node2;
6204
6277
  }
6205
6278
  const translateActions = {
6206
6279
  name: "sanity-assist-translate",
6207
6280
  useAction(props) {
6208
- var _a, _b, _c, _d, _e, _f, _g;
6281
+ var _a, _b, _c, _d, _e, _f, _g, _h;
6209
6282
  const {
6210
6283
  config
6211
6284
  } = useAiAssistanceConfig();
6212
6285
  const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
6213
- const isDocumentLevel = props.path.length === 0;
6214
6286
  const {
6215
- schemaType,
6287
+ schemaType: fieldSchemaType,
6288
+ path,
6216
6289
  documentId,
6290
+ documentSchemaType,
6217
6291
  documentIsAssistable
6218
6292
  } = props;
6219
- const fieldTransEnabled = (_c = (_b = (_a = config.translate) == null ? void 0 : _a.field) == null ? void 0 : _b.documentTypes) == null ? void 0 : _c.includes(schemaType.name);
6220
- const docTransTypes = (_e = (_d = config.translate) == null ? void 0 : _d.document) == null ? void 0 : _e.documentTypes;
6221
- const documentTranslationEnabled = isDocumentLevel && (!docTransTypes && isAssistSupported(schemaType) || (docTransTypes == null ? void 0 : docTransTypes.includes(schemaType.name)));
6222
- if (documentTranslationEnabled || fieldTransEnabled) {
6293
+ const isDocumentLevel = path.length === 0;
6294
+ const docTransTypes = (_b = (_a = config.translate) == null ? void 0 : _a.document) == null ? void 0 : _b.documentTypes;
6295
+ const options = fieldSchemaType == null ? void 0 : fieldSchemaType.options;
6296
+ const addFieldAction = isDocumentLevel || ((_c = options == null ? void 0 : options.aiWritingAssistance) == null ? void 0 : _c.translateAction);
6297
+ const fieldTransEnabled = addFieldAction && documentSchemaType && ((_f = (_e = (_d = config.translate) == null ? void 0 : _d.field) == null ? void 0 : _e.documentTypes) == null ? void 0 : _f.includes(documentSchemaType.name));
6298
+ const documentTranslationEnabled = addFieldAction && documentSchemaType && (!docTransTypes && isAssistSupported(fieldSchemaType) || (docTransTypes == null ? void 0 : docTransTypes.includes(documentSchemaType.name)));
6299
+ if (documentSchemaType && (documentTranslationEnabled || fieldTransEnabled)) {
6223
6300
  const {
6224
6301
  value: documentValue,
6225
6302
  onChange: documentOnChange
@@ -6232,39 +6309,52 @@ const translateActions = {
6232
6309
  task: translationApi.translate
6233
6310
  });
6234
6311
  docRef.current = documentValue;
6235
- const languagePath = (_g = (_f = config.translate) == null ? void 0 : _f.document) == null ? void 0 : _g.languageField;
6236
- const translateDocumentAction = useMemo(() => languagePath && documentTranslationEnabled ? node$1({
6237
- type: "action",
6238
- icon: translationApi.loading ? () => /* @__PURE__ */jsx(Box, {
6239
- style: {
6240
- height: 17
6241
- },
6242
- children: /* @__PURE__ */jsx(Spinner, {
6312
+ const languagePath = (_h = (_g = config.translate) == null ? void 0 : _g.document) == null ? void 0 : _h.languageField;
6313
+ const translateDocumentAction = useMemo(() => {
6314
+ var _a2;
6315
+ if (!languagePath || !documentTranslationEnabled) {
6316
+ return void 0;
6317
+ }
6318
+ const {
6319
+ value: languageId
6320
+ } = (_a2 = extractWithPath(languagePath, documentValue)[0]) != null ? _a2 : {};
6321
+ const title = path.length ? "Translate" : "Translate document";
6322
+ return node$2({
6323
+ type: "action",
6324
+ icon: translationApi.loading ? () => /* @__PURE__ */jsx(Box, {
6243
6325
  style: {
6244
- transform: "translateY(6px)"
6326
+ height: 17
6327
+ },
6328
+ children: /* @__PURE__ */jsx(Spinner, {
6329
+ style: {
6330
+ transform: "translateY(6px)"
6331
+ }
6332
+ })
6333
+ }) : TranslateIcon,
6334
+ title,
6335
+ onAction: () => {
6336
+ if (translationApi.loading || !languagePath || !documentId) {
6337
+ return;
6245
6338
  }
6246
- })
6247
- }) : TranslateIcon,
6248
- title: "Translate document",
6249
- onAction: () => {
6250
- if (translationApi.loading || !languagePath || !documentId) {
6251
- return;
6252
- }
6253
- translate({
6254
- languagePath,
6255
- documentId: documentId != null ? documentId : ""
6256
- });
6257
- },
6258
- renderAsButton: true,
6259
- disabled: translationApi.loading
6260
- }) : void 0, [languagePath, translate, documentId, translationApi.loading, documentTranslationEnabled]);
6339
+ translate({
6340
+ languagePath,
6341
+ translatePath: path,
6342
+ documentId: documentId != null ? documentId : ""
6343
+ });
6344
+ },
6345
+ renderAsButton: true,
6346
+ disabled: translationApi.loading || !languageId ? {
6347
+ reason: "Language is not set for this document"
6348
+ } : void 0
6349
+ });
6350
+ }, [languagePath, translate, documentId, translationApi.loading, documentTranslationEnabled, path]);
6261
6351
  const fieldTranslate = useFieldTranslation();
6262
6352
  const openFieldTranslation = useDraftDelayedTask({
6263
6353
  documentOnChange,
6264
6354
  isDocAssistable: documentIsAssistable != null ? documentIsAssistable : false,
6265
6355
  task: fieldTranslate.openFieldTranslation
6266
6356
  });
6267
- const translateFieldsAction = useMemo(() => fieldTransEnabled ? node$1({
6357
+ const translateFieldsAction = useMemo(() => fieldTransEnabled ? node$2({
6268
6358
  type: "action",
6269
6359
  icon: fieldTranslate.translationLoading ? () => /* @__PURE__ */jsx(Box, {
6270
6360
  style: {
@@ -6276,24 +6366,25 @@ const translateActions = {
6276
6366
  }
6277
6367
  })
6278
6368
  }) : TranslateIcon,
6279
- title: "Translate fields",
6369
+ title: "Translate fields...",
6280
6370
  onAction: () => {
6281
6371
  if (fieldTranslate.translationLoading || !documentId) {
6282
6372
  return;
6283
6373
  }
6284
6374
  openFieldTranslation({
6285
6375
  document: docRef.current,
6286
- documentSchema: schemaType
6376
+ documentSchema: documentSchemaType,
6377
+ translatePath: path
6287
6378
  });
6288
6379
  },
6289
6380
  renderAsButton: true,
6290
6381
  disabled: fieldTranslate.translationLoading
6291
- }) : void 0, [openFieldTranslation, schemaType, documentId, fieldTranslate.translationLoading, fieldTransEnabled]);
6382
+ }) : void 0, [openFieldTranslation, documentSchemaType, documentId, fieldTranslate.translationLoading, fieldTransEnabled, path]);
6292
6383
  return useMemo(() => {
6293
- return node$1({
6384
+ return node$2({
6294
6385
  type: "group",
6295
6386
  icon: () => null,
6296
- title: "Translate",
6387
+ title: "Translation",
6297
6388
  children: [translateDocumentAction, translateFieldsAction].filter(c => !!c),
6298
6389
  expanded: true
6299
6390
  });
@@ -6302,6 +6393,58 @@ const translateActions = {
6302
6393
  return void 0;
6303
6394
  }
6304
6395
  };
6396
+ function node$1(node2) {
6397
+ return node2;
6398
+ }
6399
+ const generateImagActions = {
6400
+ name: "sanity-assist-generate-image",
6401
+ useAction(props) {
6402
+ const pathKey = usePathKey(props.path);
6403
+ const {
6404
+ config
6405
+ } = useAiAssistanceConfig();
6406
+ const apiClient = useApiClient(config == null ? void 0 : config.__customApiClient);
6407
+ const {
6408
+ generateImage,
6409
+ loading
6410
+ } = useGenerateImage(apiClient);
6411
+ const imageContext = useContext(ImageContext);
6412
+ if (imageContext && pathKey === (imageContext == null ? void 0 : imageContext.imagePromptPath)) {
6413
+ const {
6414
+ documentId
6415
+ } = useAssistDocumentContext();
6416
+ return useMemo(() => {
6417
+ return node$1({
6418
+ type: "action",
6419
+ icon: loading ? () => /* @__PURE__ */jsx(Box, {
6420
+ style: {
6421
+ height: 17
6422
+ },
6423
+ children: /* @__PURE__ */jsx(Spinner, {
6424
+ style: {
6425
+ transform: "translateY(6px)"
6426
+ }
6427
+ })
6428
+ }) : ImageIcon,
6429
+ title: "Generate image from prompt",
6430
+ onAction: () => {
6431
+ if (loading) {
6432
+ return;
6433
+ }
6434
+ generateImage({
6435
+ path: pathKey,
6436
+ documentId: documentId != null ? documentId : ""
6437
+ });
6438
+ },
6439
+ renderAsButton: true,
6440
+ disabled: loading,
6441
+ hidden: !imageContext.assetRef
6442
+ });
6443
+ }, [generateImage, pathKey, documentId, loading, imageContext]);
6444
+ }
6445
+ return void 0;
6446
+ }
6447
+ };
6305
6448
  function node(node2) {
6306
6449
  return node2;
6307
6450
  }
@@ -6358,10 +6501,12 @@ const assistFieldActions = {
6358
6501
  const isPathSelected = pathKey === selectedPath;
6359
6502
  const isSelected = isInspectorOpen && isPathSelected;
6360
6503
  const imageCaptionAction = generateCaptionsActions.useAction(props);
6504
+ const imageGenAction = generateImagActions.useAction(props);
6361
6505
  const translateAction = translateActions.useAction(typed({
6362
6506
  ...props,
6363
6507
  documentId: assistableDocumentId,
6364
- documentIsAssistable
6508
+ documentIsAssistable,
6509
+ documentSchemaType
6365
6510
  }));
6366
6511
  const manageInstructions = useCallback(() => isSelected ? closeInspector(aiInspectorId) : openInspector(aiInspectorId, {
6367
6512
  [fieldPathParam]: pathKey,
@@ -6389,7 +6534,7 @@ const assistFieldActions = {
6389
6534
  }, [fieldAssist == null ? void 0 : fieldAssist.instructions]);
6390
6535
  const instructions = useMemo(() => [...privateInstructions, ...sharedInstructions], [privateInstructions, sharedInstructions]);
6391
6536
  const runInstructionsGroup = useMemo(() => {
6392
- return (instructions == null ? void 0 : instructions.length) || imageCaptionAction || translateAction ? node({
6537
+ return (instructions == null ? void 0 : instructions.length) || imageCaptionAction || translateAction || imageGenAction ? node({
6393
6538
  type: "group",
6394
6539
  icon: () => null,
6395
6540
  title: "Run instructions",
@@ -6400,10 +6545,10 @@ const assistFieldActions = {
6400
6545
  hidden: isHidden,
6401
6546
  documentIsNew: !!documentIsNew,
6402
6547
  assistSupported
6403
- }))), imageCaptionAction].filter(a => !!a),
6548
+ }))), imageCaptionAction, imageGenAction].filter(a => !!a),
6404
6549
  expanded: true
6405
6550
  }) : void 0;
6406
- }, [instructions, currentUser == null ? void 0 : currentUser.id, onInstructionAction, isHidden, documentIsNew, assistSupported, imageCaptionAction, translateAction]);
6551
+ }, [instructions, currentUser == null ? void 0 : currentUser.id, onInstructionAction, isHidden, documentIsNew, assistSupported, imageCaptionAction, translateAction, imageGenAction]);
6407
6552
  const instructionsLength = (instructions == null ? void 0 : instructions.length) || 0;
6408
6553
  const manageInstructionsItem = useMemo(() => node({
6409
6554
  type: "action",
@@ -6416,13 +6561,13 @@ const assistFieldActions = {
6416
6561
  type: "group",
6417
6562
  icon: SparklesIcon,
6418
6563
  title: pluginTitleShort,
6419
- children: [runInstructionsGroup, translateAction, assistSupported && manageInstructionsItem].filter(c => !!c),
6564
+ children: [runInstructionsGroup, translateAction, assistSupported && manageInstructionsItem].filter(c => !!c).filter(c => c.type === "group" ? c.children.length : true),
6420
6565
  expanded: false,
6421
6566
  renderAsButton: true,
6422
- hidden: !assistSupported && !imageCaptionAction && !translateAction
6567
+ hidden: !assistSupported && !imageCaptionAction && !translateAction && !imageGenAction
6423
6568
  }), [
6424
6569
  //documentIsNew,
6425
- runInstructionsGroup, manageInstructionsItem, assistSupported, imageCaptionAction, translateAction]);
6570
+ runInstructionsGroup, manageInstructionsItem, assistSupported, imageCaptionAction, translateAction, imageGenAction]);
6426
6571
  const emptyAction = useMemo(() => node({
6427
6572
  type: "action",
6428
6573
  hidden: !assistSupported,
@@ -6432,7 +6577,7 @@ const assistFieldActions = {
6432
6577
  title: pluginTitleShort,
6433
6578
  selected: isSelected
6434
6579
  }), [assistSupported, manageInstructions, isSelected]);
6435
- if (instructionsLength === 0 && !imageCaptionAction && !translateAction) {
6580
+ if (instructionsLength === 0 && !imageCaptionAction && !translateAction && !imageGenAction) {
6436
6581
  return emptyAction;
6437
6582
  }
6438
6583
  return group;
@@ -6551,12 +6696,28 @@ function AssistDocumentInput(_ref23) {
6551
6696
  ...props
6552
6697
  } = _ref23;
6553
6698
  useInstructionToaster(documentId, props.schemaType);
6699
+ const schemaType = useMemo(() => {
6700
+ if (props.schemaType.name !== assistDocumentTypeName) {
6701
+ return props.schemaType;
6702
+ }
6703
+ return {
6704
+ ...props.schemaType,
6705
+ type: {
6706
+ ...props.schemaType.type,
6707
+ // compatability with i18nArrays plugin that requires this to be document
6708
+ name: "document"
6709
+ }
6710
+ };
6711
+ }, [props.schemaType]);
6554
6712
  return /* @__PURE__ */jsx(FirstAssistedPathProvider, {
6555
6713
  members: props.members,
6556
6714
  children: /* @__PURE__ */jsx(AssistDocumentContextProvider, {
6557
- schemaType: props.schemaType,
6715
+ schemaType,
6558
6716
  documentId,
6559
- children: props.renderDefault(props)
6717
+ children: props.renderDefault({
6718
+ ...props,
6719
+ schemaType
6720
+ })
6560
6721
  })
6561
6722
  });
6562
6723
  }
@@ -6632,7 +6793,8 @@ const assist = definePlugin(config => {
6632
6793
  },
6633
6794
  document: {
6634
6795
  inspectors: (prev, context) => {
6635
- const docSchema = context.schema.get(context.documentType);
6796
+ const documentType = context.documentType;
6797
+ const docSchema = context.schema.get(documentType);
6636
6798
  if (docSchema && isSchemaAssistEnabled(docSchema)) {
6637
6799
  return [...prev, assistInspector];
6638
6800
  }
@@ -6643,6 +6805,9 @@ const assist = definePlugin(config => {
6643
6805
  documentType,
6644
6806
  schema
6645
6807
  } = _ref24;
6808
+ if (documentType === assistDocumentTypeName) {
6809
+ return [];
6810
+ }
6646
6811
  const docSchema = schema.get(documentType);
6647
6812
  if (docSchema && isSchemaAssistEnabled(docSchema)) {
6648
6813
  return [...prev, assistFieldActions];
@@ -6655,6 +6820,9 @@ const assist = definePlugin(config => {
6655
6820
  schema,
6656
6821
  schemaType
6657
6822
  } = _ref25;
6823
+ if (schemaType === assistDocumentTypeName) {
6824
+ return [];
6825
+ }
6658
6826
  const docSchema = schema.get(schemaType);
6659
6827
  if (docSchema && isObjectSchemaType(docSchema) && isSchemaAssistEnabled(docSchema)) {
6660
6828
  return [...prev, createAssistDocumentPresence(documentId, docSchema)];