dsh-codex-subscription 3.2.2 → 3.2.3

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/lib/client.js CHANGED
@@ -153,6 +153,63 @@ window.__ModuleLoader__.load({
153
153
  };
154
154
  }
155
155
  //#endregion
156
+ //#region src/use-image-source.js
157
+ function useImageSource(load, identity, preview) {
158
+ const [attempt, setAttempt] = (0, react.useState)(0);
159
+ const [state, setState] = (0, react.useState)({
160
+ identity,
161
+ src: preview,
162
+ failed: false,
163
+ usable: false
164
+ });
165
+ (0, react.useEffect)(() => {
166
+ let live = true;
167
+ setState((previous) => ({
168
+ identity,
169
+ src: previous.identity === identity ? previous.src : preview,
170
+ failed: false,
171
+ usable: previous.identity === identity && previous.usable
172
+ }));
173
+ Promise.resolve().then(() => attempt === 0 && preview ? preview : load()).then((src) => {
174
+ if (live) setState((previous) => ({
175
+ identity,
176
+ src,
177
+ failed: !src,
178
+ usable: previous.identity === identity && previous.src === src && previous.usable
179
+ }));
180
+ }, () => {
181
+ if (live) setState((previous) => ({
182
+ ...previous,
183
+ failed: true
184
+ }));
185
+ });
186
+ return () => {
187
+ live = false;
188
+ };
189
+ }, [
190
+ identity,
191
+ load,
192
+ preview,
193
+ attempt
194
+ ]);
195
+ return {
196
+ src: state.identity === identity ? state.src : preview,
197
+ failed: state.identity === identity && state.failed,
198
+ usable: state.identity === identity && state.usable,
199
+ loaded: () => setState((previous) => previous.identity === identity ? {
200
+ ...previous,
201
+ usable: true
202
+ } : previous),
203
+ fail: () => setState((previous) => previous.identity === identity ? {
204
+ ...previous,
205
+ failed: true,
206
+ usable: false
207
+ } : previous),
208
+ retry: () => setAttempt((value) => value + 1),
209
+ attempt
210
+ };
211
+ }
212
+ //#endregion
156
213
  //#region src/image-edit.js
157
214
  const isRecord = (value) => value !== null && typeof value === "object" && !Array.isArray(value);
158
215
  const validCoordinate = (value) => typeof value === "number" && Number.isFinite(value) && value >= 0 && value <= 1;
@@ -236,7 +293,7 @@ window.__ModuleLoader__.load({
236
293
  * sourceInDraft?: boolean }} options
237
294
  */
238
295
  function officialImageTools({ src, name, t, attachForEdit, sourceInDraft = false }) {
239
- const send = (draft, extraFiles = []) => attachForEdit(src, name, draft, [], void 0, sourceInDraft, extraFiles);
296
+ const send = (draft, extraFiles = [], signal) => attachForEdit(src, name, draft, [], void 0, sourceInDraft, extraFiles, { signal });
240
297
  const base = {
241
298
  pendingLabel: t("imageEditPreparing"),
242
299
  errorLabel: t("imageEditFailed"),
@@ -248,17 +305,17 @@ window.__ModuleLoader__.load({
248
305
  id: "remove-background",
249
306
  kind: "tool",
250
307
  label: t("imageRemoveBackground"),
251
- onInvoke: () => send(t("imageRemoveBackgroundPrompt"))
308
+ onInvoke: ({ signal } = {}) => send(t("imageRemoveBackgroundPrompt"), [], signal)
252
309
  },
253
310
  {
254
311
  ...base,
255
312
  id: "erase",
256
313
  kind: "mask",
257
314
  label: t("imageErase"),
258
- onInvoke: async ({ mask }) => {
315
+ onInvoke: async ({ mask, signal }) => {
259
316
  if (!(mask instanceof Blob)) throw new Error("Mark the area to erase first");
260
317
  const maskName = `${String(name).replace(/\.[a-z0-9]+$/iu, "")}-erase-mask.png`;
261
- return send(t("imageErasePrompt"), [new File([mask], maskName, { type: "image/png" })]);
318
+ return send(t("imageErasePrompt"), [new File([mask], maskName, { type: "image/png" })], signal);
262
319
  }
263
320
  },
264
321
  {
@@ -270,18 +327,30 @@ window.__ModuleLoader__.load({
270
327
  value: ratio,
271
328
  label: `${t(`imageRatio_${id}`)} ${ratio}`
272
329
  })),
273
- onInvoke: ({ option }) => send(t("imageResizePrompt").replaceAll("{aspectRatio}", option))
330
+ onInvoke: ({ option, signal }) => send(t("imageResizePrompt").replaceAll("{aspectRatio}", option), [], signal)
274
331
  }
275
332
  ];
276
333
  }
277
334
  //#endregion
335
+ //#region src/composer-attachment-native.js
336
+ function nativeComposerAttachments(entries, ownComponent) {
337
+ return entries.find((entry) => entry.component !== ownComponent && entry.locale === "conversation" && !entry.store && !entry.children && (entry.inject === void 0 || typeof entry.inject === "function"));
338
+ }
339
+ function nativeComposerProps(entry, props, translate) {
340
+ return {
341
+ ...props,
342
+ ...entry.inject?.(props.sessionId),
343
+ t: translate
344
+ };
345
+ }
346
+ //#endregion
278
347
  //#region src/client-image-previews.jsx
279
- function openPreview(props, item, opener, sourceInDraft = false) {
348
+ function openPreview(props, item, opener, sourceInDraft = false, group = [item]) {
280
349
  const { service, preference, t, attachForEdit } = props;
281
350
  const settings = preference.getSnapshot();
282
- const referenceName = `annotated-${item.name}.png`;
283
- service.open({
284
- items: [{
351
+ const items = group.map((item) => {
352
+ const referenceName = `annotated-${String(item.name).replace(/\.[a-z0-9]+$/iu, "")}.png`;
353
+ return {
285
354
  ...item,
286
355
  actions: settings.imageEditing ? [
287
356
  {
@@ -290,12 +359,15 @@ window.__ModuleLoader__.load({
290
359
  pendingLabel: t("imageEditPreparing"),
291
360
  errorLabel: t("imageEditFailed"),
292
361
  closeOnSuccess: true,
293
- onInvoke: ({ annotations }) => attachForEdit(item.src, item.name, buildImageEditDraft({
362
+ onInvoke: ({ annotations, signal }) => attachForEdit(item.src, item.name, buildImageEditDraft({
294
363
  annotations,
295
364
  translate: t,
296
365
  sourceName: item.name,
297
366
  referenceName
298
- }), annotations, referenceName, sourceInDraft)
367
+ }), annotations, referenceName, sourceInDraft, void 0, {
368
+ signal,
369
+ sourceId: sourceInDraft ? item.id : void 0
370
+ })
299
371
  },
300
372
  ...officialImageTools({
301
373
  src: item.src,
@@ -312,8 +384,13 @@ window.__ModuleLoader__.load({
312
384
  onInvoke: () => props.openSketchImage(item.src, item.name)
313
385
  }] : []
314
386
  ] : []
315
- }],
387
+ };
388
+ });
389
+ service.open({
390
+ items,
391
+ index: Math.max(0, items.findIndex((candidate) => candidate.id === item.id)),
316
392
  opener,
393
+ scope: props.sessionId,
317
394
  source: sourceInDraft ? "codex-draft" : "codex-message",
318
395
  annotations: settings.imageAnnotations
319
396
  });
@@ -323,31 +400,34 @@ window.__ModuleLoader__.load({
323
400
  const entry = (0, react.useSyncExternalStore)(watchNativeAttachments, nativeAttachments);
324
401
  (0, react.useEffect)(() => {
325
402
  const current = service.getSnapshot();
326
- if (current?.source === "codex-draft" && !attachments.some((item) => item.id === current.items[0]?.id)) service.close();
403
+ if (current?.source === "codex-draft" && current.items.some((image) => !attachments.some((item) => item.id === image.id))) service.close();
327
404
  }, [attachments, service]);
328
405
  (0, react.useEffect)(() => () => {
329
406
  if (service.getSnapshot()?.source === "codex-draft") service.close();
330
407
  }, [service]);
331
408
  const latest = (0, react.useRef)(props);
409
+ const root = (0, react.useRef)(null);
332
410
  latest.current = props;
333
411
  (0, react.useEffect)(() => {
334
412
  const onWindowClick = (event) => {
335
413
  const target = event.target;
336
414
  if (!(target instanceof Element)) return;
337
415
  const button = target.closest("button"), image = button?.querySelector(":scope > img");
416
+ if (!button || !root.current?.contains(button)) return;
338
417
  const item = image && latest.current.attachments.find((candidate) => candidate.previewUrl === image.src);
339
418
  if (!item || event.button !== 0 || event.ctrlKey || event.metaKey || event.altKey) return;
340
419
  event.preventDefault();
341
420
  event.stopPropagation();
342
421
  event.stopImmediatePropagation();
343
- openPreview(latest.current, {
422
+ const group = latest.current.attachments.filter((item) => item.previewUrl).map((item) => ({
344
423
  id: item.id,
345
424
  src: item.previewUrl,
346
425
  name: item.file.name,
347
426
  width: item.width,
348
427
  height: item.height,
349
428
  bytes: item.file.size
350
- }, button, true);
429
+ }));
430
+ openPreview(latest.current, group.find((image) => image.id === item.id), button, true, group);
351
431
  };
352
432
  window.addEventListener("click", onWindowClick, true);
353
433
  return () => window.removeEventListener("click", onWindowClick, true);
@@ -355,39 +435,15 @@ window.__ModuleLoader__.load({
355
435
  if (!entry) return null;
356
436
  const NativeAttachments = entry.component;
357
437
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
438
+ ref: root,
358
439
  style: { display: "contents" },
359
- children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NativeAttachments, {
360
- ...props,
361
- t: nativeTranslate
362
- })
440
+ children: /* @__PURE__ */ (0, react_jsx_runtime.jsx)(NativeAttachments, { ...nativeComposerProps(entry, props, nativeTranslate) })
363
441
  });
364
442
  }
365
- function MessageImagePreview({ image, ...props }) {
443
+ function MessageImagePreview({ image, onOpen, ...props }) {
366
444
  const { loadImage, t } = props;
367
- const [src, setSrc] = (0, react.useState)(image.preview?.url);
368
- const [failed, setFailed] = (0, react.useState)(false);
369
- const [attempt, setAttempt] = (0, react.useState)(0);
370
- (0, react.useEffect)(() => {
371
- if (image.preview) {
372
- setSrc(image.preview.url);
373
- return;
374
- }
375
- let live = true;
376
- setSrc(void 0);
377
- setFailed(false);
378
- Promise.resolve().then(() => loadImage(image.attachment)).then((value) => {
379
- if (live) setSrc(value);
380
- }, () => {
381
- if (live) setFailed(true);
382
- });
383
- return () => {
384
- live = false;
385
- };
386
- }, [
387
- image,
388
- loadImage,
389
- attempt
390
- ]);
445
+ const reload = (0, react.useCallback)(() => image.attachment ? loadImage(image.attachment) : image.preview?.url, [image, loadImage]);
446
+ const { src, failed, retry, fail, attempt, usable, loaded } = useImageSource(reload, image.attachment?.attachmentId ?? image.preview?.url ?? image, image.preview?.url);
391
447
  const item = {
392
448
  ...image.attachment ?? image.preview,
393
449
  ...image.label ? { name: image.label } : {}
@@ -399,24 +455,49 @@ window.__ModuleLoader__.load({
399
455
  disabled: !src && !failed,
400
456
  onClick: (event) => {
401
457
  if (failed) {
402
- setAttempt((value) => value + 1);
458
+ retry();
403
459
  return;
404
460
  }
405
- openPreview(props, {
461
+ onOpen(image, {
406
462
  id: item.attachmentId ?? src,
407
463
  src,
464
+ reload,
408
465
  name: item.name ?? "image.png",
409
466
  width: item.width,
410
467
  height: item.height
411
468
  }, event.currentTarget);
412
469
  },
413
- children: src ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
470
+ children: failed && !usable ? t("accountRetry") : src ? /* @__PURE__ */ (0, react_jsx_runtime.jsxs)(react_jsx_runtime.Fragment, { children: [/* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
414
471
  src,
472
+ onError: fail,
473
+ onLoad: loaded,
415
474
  alt: item.name ?? "image"
416
- }) : failed ? t("accountRetry") : "…"
475
+ }, attempt), failed ? t("accountRetry") : null] }) : "…"
417
476
  });
418
477
  }
419
478
  function MessageImagePreviews({ images, align, compact, thumbnail, ...props }) {
479
+ const opening = (0, react.useRef)(0);
480
+ (0, react.useEffect)(() => () => {
481
+ opening.current++;
482
+ }, [images]);
483
+ const onOpen = async (selected, item, opener) => {
484
+ const revision = ++opening.current;
485
+ const group = await Promise.all(images.map(async (image) => {
486
+ if (image === selected) return item;
487
+ const ref = image.attachment ?? image.preview;
488
+ const reload = () => image.attachment ? props.loadImage(image.attachment) : image.preview?.url;
489
+ const src = image.preview?.url ?? await reload().catch(() => "about:blank");
490
+ return {
491
+ id: ref.attachmentId ?? src,
492
+ src,
493
+ reload,
494
+ name: ref.name ?? "image.png",
495
+ width: ref.width,
496
+ height: ref.height
497
+ };
498
+ }));
499
+ if (revision === opening.current) openPreview(props, item, opener, false, group);
500
+ };
420
501
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
421
502
  className: "codexMessageImages",
422
503
  "data-align": align,
@@ -424,6 +505,7 @@ window.__ModuleLoader__.load({
424
505
  "data-thumbnail": thumbnail || void 0,
425
506
  children: images.map((image, index) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(MessageImagePreview, {
426
507
  image,
508
+ onOpen,
427
509
  ...props
428
510
  }, image.attachment?.attachmentId ?? image.preview?.url ?? index))
429
511
  });
@@ -645,6 +727,10 @@ window.__ModuleLoader__.load({
645
727
  }
646
728
  //#endregion
647
729
  //#region src/original-image-download.js
730
+ function imageEditSource(rpc, sessionId, original, preview) {
731
+ if (original === void 0) return preview;
732
+ return async ({ signal } = {}) => new Blob([await readOriginalImage(rpc, sessionId, original, { signal })], { type: original.mediaType });
733
+ }
648
734
  function decodeBase64Chunk(value) {
649
735
  if (typeof value !== "string" || value.length === 0 || value.length > Math.ceil(4194304 / 3) * 4 + 8) throw new Error("Invalid original image chunk");
650
736
  let decoded;
@@ -715,90 +801,84 @@ window.__ModuleLoader__.load({
715
801
  URL.revokeObjectURL(url);
716
802
  }
717
803
  }
718
- function CodexGeneratedImage({ attachment, original, rpc, sessionId, loadImage, openSketchImage, attachForEdit, getImageViewer, getInternalImageViewer, t, features }) {
719
- const [attempt, setAttempt] = (0, react.useState)(0);
720
- const [error, setError] = (0, react.useState)(false);
721
- const [src, setSrc] = (0, react.useState)();
804
+ function CodexGeneratedImage({ attachment, original, gallery, rpc, sessionId, loadImage, openSketchImage, attachForEdit, getImageViewer, getInternalImageViewer, t, features }) {
805
+ const { src, failed: error, fail, retry, attempt, usable, loaded } = useImageSource((0, react.useCallback)(() => loadImage(attachment), [attachment, loadImage]), attachment.attachmentId ?? attachment);
722
806
  const triggerRef = (0, react.useRef)(null);
723
- (0, react.useEffect)(() => {
724
- let live = true;
725
- setError(false);
726
- setSrc(void 0);
727
- Promise.resolve().then(() => loadImage(attachment)).then((value) => {
728
- if (live) setSrc(value);
729
- }).catch(() => {
730
- if (live) setError(true);
731
- });
732
- return () => {
733
- live = false;
734
- };
735
- }, [
736
- attachment,
737
- loadImage,
738
- attempt
739
- ]);
807
+ const opening = (0, react.useRef)(0);
808
+ (0, react.useEffect)(() => () => {
809
+ opening.current++;
810
+ }, [attachment]);
740
811
  const label = attachment.name ?? t("imageLabel");
741
- const downloadName = imageDownloadName(attachment);
742
- const downloadOriginal = async ({ signal, onProgress } = {}) => {
743
- if (original === void 0) return;
744
- triggerBlobDownload(await readOriginalImage(rpc, sessionId, original, {
745
- signal,
746
- onProgress
747
- }), original.mediaType, original.name);
748
- };
749
- const openImage = () => {
812
+ const makeItem = (attachment, original, src) => {
813
+ const downloadName = imageDownloadName(attachment);
814
+ const editSource = imageEditSource(rpc, sessionId, original, src);
815
+ return {
816
+ id: attachment.attachmentId ?? downloadName,
817
+ src,
818
+ reload: () => loadImage(attachment),
819
+ name: attachment.name ?? t("imageLabel"),
820
+ width: original?.width ?? attachment.width,
821
+ height: original?.height ?? attachment.height,
822
+ bytes: original?.bytes ?? attachment.bytes,
823
+ download: original === void 0 ? void 0 : {
824
+ pendingLabel: t("imageDownloadPreparing"),
825
+ errorLabel: t("imageDownloadFailed"),
826
+ onInvoke: async ({ signal, onProgress } = {}) => triggerBlobDownload(await readOriginalImage(rpc, sessionId, original, {
827
+ signal,
828
+ onProgress
829
+ }), original.mediaType, original.name)
830
+ },
831
+ actions: !features.imageEditing ? [] : [
832
+ {
833
+ id: "continue-editing",
834
+ label: t("imageEdit"),
835
+ pendingLabel: t("imageEditPreparing"),
836
+ errorLabel: t("imageEditFailed"),
837
+ closeOnSuccess: true,
838
+ onInvoke: ({ annotations = [], signal }) => {
839
+ const imageKey = String(attachment.attachmentId ?? "image").replace(/[^a-zA-Z0-9_-]/g, "_");
840
+ const sourceName = annotations.length === 0 ? downloadName : `codex-edit-${imageKey}-source.png`;
841
+ const referenceName = `codex-edit-${imageKey}-annotations.png`;
842
+ return attachForEdit(editSource, sourceName, buildImageEditDraft({
843
+ annotations,
844
+ translate: t,
845
+ width: original?.width ?? attachment.width,
846
+ height: original?.height ?? attachment.height,
847
+ sourceName,
848
+ referenceName
849
+ }), annotations, referenceName, false, void 0, { signal });
850
+ }
851
+ },
852
+ ...officialImageTools({
853
+ src: editSource,
854
+ name: downloadName,
855
+ t,
856
+ attachForEdit
857
+ }),
858
+ ...features.imageSketch && openSketchImage ? [{
859
+ id: "sketch",
860
+ label: t("imageToSketch"),
861
+ pendingLabel: t("imageEditPreparing"),
862
+ errorLabel: t("imageEditFailed"),
863
+ onInvoke: () => openSketchImage(src, downloadName)
864
+ }] : []
865
+ ]
866
+ };
867
+ };
868
+ const openImage = async () => {
750
869
  if (src === void 0) return;
870
+ const revision = ++opening.current;
871
+ const items = await Promise.all((gallery ?? [{
872
+ attachment,
873
+ original
874
+ }]).map(async (image) => makeItem(image.attachment, image.original, image.attachment === attachment ? src : await loadImage(image.attachment).catch(() => "about:blank"))));
875
+ if (revision !== opening.current) return;
751
876
  const request = {
752
- items: [{
753
- id: attachment.attachmentId ?? downloadName,
754
- src,
755
- name: label,
756
- width: attachment.width,
757
- height: attachment.height,
758
- bytes: attachment.bytes,
759
- download: original === void 0 ? void 0 : {
760
- pendingLabel: t("imageDownloadPreparing"),
761
- errorLabel: t("imageDownloadFailed"),
762
- onInvoke: downloadOriginal
763
- },
764
- actions: !features.imageEditing ? [] : [
765
- {
766
- id: "continue-editing",
767
- label: t("imageEdit"),
768
- pendingLabel: t("imageEditPreparing"),
769
- errorLabel: t("imageEditFailed"),
770
- closeOnSuccess: true,
771
- onInvoke: ({ annotations = [] }) => {
772
- const imageKey = String(attachment.attachmentId ?? "image").replace(/[^a-zA-Z0-9_-]/g, "_");
773
- const sourceName = annotations.length === 0 ? downloadName : `codex-edit-${imageKey}-source.png`;
774
- const referenceName = `codex-edit-${imageKey}-annotations.png`;
775
- return attachForEdit(src, sourceName, buildImageEditDraft({
776
- annotations,
777
- translate: t,
778
- width: attachment.width,
779
- height: attachment.height,
780
- sourceName,
781
- referenceName
782
- }), annotations, referenceName);
783
- }
784
- },
785
- ...officialImageTools({
786
- src,
787
- name: downloadName,
788
- t,
789
- attachForEdit
790
- }),
791
- ...features.imageSketch && openSketchImage ? [{
792
- id: "sketch",
793
- label: t("imageToSketch"),
794
- pendingLabel: t("imageEditPreparing"),
795
- errorLabel: t("imageEditFailed"),
796
- onInvoke: () => openSketchImage(src, downloadName)
797
- }] : []
798
- ]
799
- }],
877
+ items,
878
+ index: Math.max(0, items.findIndex((item) => item.id === attachment.attachmentId)),
800
879
  opener: triggerRef.current,
801
880
  source: "codex-generated",
881
+ scope: sessionId,
802
882
  annotations: features.imageViewer && features.imageAnnotations
803
883
  };
804
884
  if (features.imageViewer && getInternalImageViewer?.()?.open?.(request) === true) return;
@@ -809,26 +889,28 @@ window.__ModuleLoader__.load({
809
889
  });
810
890
  else window.open(src, "_blank", "noopener,noreferrer");
811
891
  };
812
- if (error) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
892
+ if (error && !usable) return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
813
893
  type: "button",
814
894
  className: "codexGeneratedImageRetry",
815
- onClick: () => setAttempt((value) => value + 1),
895
+ onClick: retry,
816
896
  children: t("imageLoadFailed")
817
897
  });
818
- return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("button", {
898
+ return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("button", {
819
899
  ref: triggerRef,
820
900
  type: "button",
821
901
  className: "codexGeneratedImageFrame",
822
- title: t("imageOpen"),
902
+ title: error ? t("imageLoadFailed") : t("imageOpen"),
823
903
  "aria-label": t("imageOpenNamed").replace("{value}", String(label)),
824
- onClick: openImage,
825
- children: src === void 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: t("imageLoading") }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
904
+ onClick: error ? retry : openImage,
905
+ children: [src === void 0 ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: t("imageLoading") }) : /* @__PURE__ */ (0, react_jsx_runtime.jsx)("img", {
826
906
  src,
907
+ onError: fail,
908
+ onLoad: loaded,
827
909
  alt: label
828
- })
910
+ }, attempt), error ? /* @__PURE__ */ (0, react_jsx_runtime.jsx)("span", { children: t("imageLoadFailed") }) : null]
829
911
  });
830
912
  }
831
- function CodexImageToolRow({ presentation = "tool", block, sessionId, rpc, loadImage, openSketchImage, attachForEdit, getImageViewer, getInternalImageViewer, t, preference }) {
913
+ function CodexImageToolRow({ presentation = "tool", block, gallery, sessionId, rpc, loadImage, openSketchImage, attachForEdit, getImageViewer, getInternalImageViewer, t, preference }) {
832
914
  const features = (0, react.useSyncExternalStore)(preference.subscribe, preference.getSnapshot);
833
915
  const settled = block?.kind === "tool-result";
834
916
  const image = settled ? block.content.find((item) => item?.type === "image" && item.attachment !== void 0) : void 0;
@@ -865,6 +947,7 @@ window.__ModuleLoader__.load({
865
947
  features,
866
948
  attachment: image.attachment,
867
949
  original,
950
+ gallery,
868
951
  rpc,
869
952
  sessionId,
870
953
  loadImage,
@@ -905,10 +988,15 @@ window.__ModuleLoader__.load({
905
988
  });
906
989
  }
907
990
  function CodexImageOutput({ node, ...props }) {
991
+ const gallery = node.data.blocks.flatMap((block) => block.content.filter((item) => item?.type === "image" && item.attachment).map((item) => ({
992
+ attachment: item.attachment,
993
+ original: decodeImagePresentation(block?.meta)?.original
994
+ })));
908
995
  return /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
909
996
  className: "codexImageOutput",
910
997
  children: node.data.blocks.map((block) => /* @__PURE__ */ (0, react_jsx_runtime.jsx)(CodexImageToolRow, {
911
998
  block,
999
+ gallery,
912
1000
  ...props,
913
1001
  presentation: "output"
914
1002
  }, block.toolCallId))
@@ -1742,6 +1830,7 @@ window.__ModuleLoader__.load({
1742
1830
  forecastCompactPending: "待估",
1743
1831
  forecastCompactChanging: "重估",
1744
1832
  forecastCompactStale: "待更新",
1833
+ imageMore: "更多操作",
1745
1834
  imageEditShort: "继续编辑",
1746
1835
  forecastResolution: "暂时估不准:读数变化不足以确定速度",
1747
1836
  forecastChanging: "使用强度变化,正在重新估计",
@@ -2660,6 +2749,7 @@ window.__ModuleLoader__.load({
2660
2749
  forecastCompactPending: "Estimating",
2661
2750
  forecastCompactChanging: "Re-estimating",
2662
2751
  forecastCompactStale: "Stale",
2752
+ imageMore: "More actions",
2663
2753
  imageEditShort: "Edit",
2664
2754
  forecastResolution: "Not enough change to estimate the rate yet",
2665
2755
  forecastChanging: "Usage intensity changed; estimating again",
@@ -3193,7 +3283,7 @@ window.__ModuleLoader__.load({
3193
3283
  imagePrevious: "Previous image",
3194
3284
  imageNext: "Next image",
3195
3285
  imageZoomHint: "Wheel to zoom · drag to pan · double-click for 100%",
3196
- imageActual: "100%",
3286
+ imageActual: "Actual size",
3197
3287
  imageEditDefault: "Edit this image.",
3198
3288
  imageRegionNotes: "Region changes:",
3199
3289
  imageEdit: "Continue editing in composer",
@@ -3869,6 +3959,7 @@ a.codexVoiceCall{display:flex;align-items:center;justify-content:center;box-sizi
3869
3959
  height: boundedNumber(item.height, void 0),
3870
3960
  bytes: boundedNumber(item.bytes, void 0),
3871
3961
  download: downloadOf(item.download),
3962
+ reload: typeof item.reload === "function" ? item.reload : void 0,
3872
3963
  actions: actionsOf(item.actions)
3873
3964
  }];
3874
3965
  });
@@ -3879,6 +3970,7 @@ a.codexVoiceCall{display:flex;align-items:center;justify-content:center;box-sizi
3879
3970
  index: Math.max(0, Math.min(items.length - 1, requestedIndex)),
3880
3971
  opener: typeof HTMLElement !== "undefined" && request?.opener instanceof HTMLElement ? request.opener : void 0,
3881
3972
  source: typeof request?.source === "string" ? request.source : "dsh-codex-subscription",
3973
+ scope: typeof request?.scope === "string" ? request.scope : "",
3882
3974
  annotations: request?.annotations !== false
3883
3975
  };
3884
3976
  }
@@ -3894,6 +3986,30 @@ a.codexVoiceCall{display:flex;align-items:center;justify-content:center;box-sizi
3894
3986
  #revision = 0;
3895
3987
  #snapshot;
3896
3988
  #annotationsByImage = /* @__PURE__ */ new Map();
3989
+ #operations = /* @__PURE__ */ new Set();
3990
+ async invoke(action, context = {}, revision = this.#snapshot?.revision) {
3991
+ if (!this.#snapshot || this.#snapshot.revision !== revision) throw new Error("Image preview changed");
3992
+ this.#cancelOperations();
3993
+ const controller = new AbortController();
3994
+ this.#operations.add(controller);
3995
+ try {
3996
+ const value = await action.onInvoke({
3997
+ ...context,
3998
+ signal: controller.signal
3999
+ });
4000
+ controller.signal.throwIfAborted();
4001
+ return value;
4002
+ } finally {
4003
+ this.#operations.delete(controller);
4004
+ }
4005
+ }
4006
+ #cancelOperations() {
4007
+ for (const controller of this.#operations) controller.abort();
4008
+ this.#operations.clear();
4009
+ }
4010
+ cancelPending() {
4011
+ this.#cancelOperations();
4012
+ }
3897
4013
  constructor() {
3898
4014
  this.subscribe = (listener) => {
3899
4015
  this.#listeners.add(listener);
@@ -3902,16 +4018,25 @@ a.codexVoiceCall{display:flex;align-items:center;justify-content:center;box-sizi
3902
4018
  };
3903
4019
  };
3904
4020
  this.getSnapshot = () => this.#snapshot;
3905
- this.getAnnotationsSnapshot = () => Object.fromEntries([...this.#annotationsByImage].map(([id, annotations]) => [id, copyAnnotations(annotations)]));
4021
+ this.getAnnotationsSnapshot = () => {
4022
+ const prefix = `${this.#snapshot?.scope ?? ""}\0`;
4023
+ return Object.fromEntries([...this.#annotationsByImage].filter(([id]) => id.startsWith(prefix)).map(([id, annotations]) => [id.slice(prefix.length), copyAnnotations(annotations)]));
4024
+ };
3906
4025
  }
3907
4026
  setAnnotations(imageId, annotations) {
3908
4027
  if (typeof imageId !== "string" || imageId === "" || !Array.isArray(annotations)) return;
4028
+ imageId = `${this.#snapshot?.scope ?? ""}\0${imageId}`;
3909
4029
  if (annotations.length === 0) this.#annotationsByImage.delete(imageId);
3910
- else this.#annotationsByImage.set(imageId, copyAnnotations(annotations));
4030
+ else {
4031
+ this.#annotationsByImage.delete(imageId);
4032
+ this.#annotationsByImage.set(imageId, copyAnnotations(annotations));
4033
+ while (this.#annotationsByImage.size > 128) this.#annotationsByImage.delete(this.#annotationsByImage.keys().next().value);
4034
+ }
3911
4035
  }
3912
4036
  open(request) {
3913
4037
  const normalized = normalizeSubscriptionViewerRequest(request);
3914
4038
  if (normalized === void 0) return false;
4039
+ this.#cancelOperations();
3915
4040
  this.#revision += 1;
3916
4041
  this.#snapshot = {
3917
4042
  ...normalized,
@@ -3922,6 +4047,7 @@ a.codexVoiceCall{display:flex;align-items:center;justify-content:center;box-sizi
3922
4047
  }
3923
4048
  close() {
3924
4049
  if (this.#snapshot === void 0) return;
4050
+ this.#cancelOperations();
3925
4051
  const opener = this.#snapshot.opener;
3926
4052
  this.#snapshot = void 0;
3927
4053
  this.#emit();
@@ -5130,7 +5256,7 @@ a.codexVoiceCall{display:flex;align-items:center;justify-content:center;box-sizi
5130
5256
  }
5131
5257
  //#endregion
5132
5258
  //#region src/version.js
5133
- const PACKAGE_VERSION = "3.2.2";
5259
+ const PACKAGE_VERSION = "3.2.3";
5134
5260
  //#endregion
5135
5261
  //#region src/client-recovery.js
5136
5262
  async function recoveryCall(rpc, endpoint, payload = {}, timeoutMs = 1e4) {
@@ -10466,7 +10592,7 @@ void main(){
10466
10592
  capability: areas[e.area],
10467
10593
  action: e.area,
10468
10594
  source: "network",
10469
- status: e.status === "ok" ? "completed" : "failed",
10595
+ status: e.status === "ok" ? "completed" : e.status,
10470
10596
  observedAt: e.observedAt
10471
10597
  }))].filter((e) => Number.isSafeInteger(e.observedAt) && now >= e.observedAt && now - e.observedAt <= 9e5).sort((a, b) => a.observedAt - b.observedAt);
10472
10598
  }
@@ -14580,22 +14706,28 @@ void main(){
14580
14706
  imageViewer.close();
14581
14707
  liveOpen("sketch", document.activeElement, new File([blob], name || "image.png", { type: blob.type || "image/png" }));
14582
14708
  };
14583
- const attachForEdit = (sessionId) => async (src, filename, draft, annotations = [], referenceName, sourceInDraft = false, extraFiles) => {
14709
+ const attachForEdit = (sessionId) => async (src, filename, draft, annotations = [], referenceName, sourceInDraft = false, extraFiles, operation = {}) => {
14584
14710
  const tool = Array.isArray(extraFiles);
14585
14711
  if (!preference.getSnapshot().imageEditing) throw new Error("Image editing is disabled");
14586
14712
  return withComposerSession(sessions, sessionId, async (actx) => {
14587
- const response = await fetch(src, { signal: lifetime.signal });
14588
- if (!response.ok) throw new Error("Could not read generated image");
14589
- const blob = await response.blob();
14713
+ const signal = operation.signal ? AbortSignal.any([lifetime.signal, operation.signal]) : lifetime.signal;
14714
+ signal.throwIfAborted();
14715
+ const blob = typeof src === "function" ? await src({ signal }) : await fetch(src, { signal }).then((response) => {
14716
+ if (!response.ok) throw new Error("Could not read generated image");
14717
+ return response.blob();
14718
+ });
14719
+ signal.throwIfAborted();
14590
14720
  const files = sourceInDraft ? [] : [new File([blob], filename, { type: blob.type || "image/png" })];
14591
14721
  if (annotations.length > 0) {
14592
14722
  const reference = await createAnnotatedImageReference(blob, annotations);
14593
14723
  files.push(new File([reference], referenceName, { type: "image/png" }));
14594
14724
  }
14595
14725
  if (tool) files.push(...extraFiles);
14596
- lifetime.signal.throwIfAborted();
14726
+ signal.throwIfAborted();
14597
14727
  const input = conversation.input.for(actx);
14598
14728
  if (!preference.getSnapshot().imageEditing) throw new Error("Image editing is disabled");
14729
+ const ids = input.state.getSnapshot().attachmentIds;
14730
+ if (operation.sourceId && Array.isArray(ids) && !ids.includes(operation.sourceId)) throw new Error("The source attachment was removed");
14599
14731
  if (files.length) attachImageFiles(conversation, input, files, sessionId);
14600
14732
  openComposerSession(sessions, ctx.get("uiWorkspace"), sessionId);
14601
14733
  if (sourceInDraft && !annotations.length && !tool) return;
@@ -14604,7 +14736,7 @@ void main(){
14604
14736
  else input.notify("info", draft);
14605
14737
  });
14606
14738
  };
14607
- const nativeAttachments = () => ctx.slots.entries("conversation.input.attachments").find((entry) => entry.component !== ComposerImagePreviews && entry.locale === "conversation" && !entry.inject && !entry.store && !entry.children);
14739
+ const nativeAttachments = () => nativeComposerAttachments(ctx.slots.entries("conversation.input.attachments"), ComposerImagePreviews);
14608
14740
  const watchNativeAttachments = (callback) => ctx.slots.subscribe("conversation.input.attachments", callback);
14609
14741
  const nativeTranslate = ctx.locale.bind("conversation");
14610
14742
  for (const [name, component] of [
@@ -14616,12 +14748,13 @@ void main(){
14616
14748
  ]) ctx.slots.inject(name, () => {
14617
14749
  let dispose;
14618
14750
  const sync = () => {
14619
- if (preference.getSnapshot().imageViewer) dispose ??= ctx.slots.register({
14751
+ if (preference.getSnapshot().imageViewer && (component !== ComposerImagePreviews || nativeAttachments())) dispose ??= ctx.slots.register({
14620
14752
  name,
14621
14753
  priority: -10,
14622
14754
  inject: (sessionId) => ({
14623
14755
  preference,
14624
14756
  t,
14757
+ sessionId,
14625
14758
  nativeAttachments,
14626
14759
  watchNativeAttachments,
14627
14760
  nativeTranslate,
@@ -14637,8 +14770,10 @@ void main(){
14637
14770
  };
14638
14771
  sync();
14639
14772
  const unwatch = preference.subscribe(sync);
14773
+ const unwatchNative = component === ComposerImagePreviews ? watchNativeAttachments(sync) : () => {};
14640
14774
  return () => {
14641
14775
  unwatch();
14776
+ unwatchNative();
14642
14777
  dispose?.();
14643
14778
  };
14644
14779
  });