dsh-image-gen 0.6.8 → 0.6.10

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/index.js CHANGED
@@ -5952,6 +5952,8 @@ const SAVE_WORKSPACE_ROUTE = "/plugins/dsh-image-gen/save-workspace";
5952
5952
  const TEST_CONNECTION_ROUTE = "/plugins/dsh-image-gen/test";
5953
5953
  /** Browser route the workbench infinite canvas pushes live state through. */
5954
5954
  const CANVAS_STATE_ROUTE = "/plugins/dsh-image-gen/canvas-state";
5955
+ /** Upload a selected canvas original to the host attachment store. */
5956
+ const CANVAS_ASSET_ROUTE = "/plugins/dsh-image-gen/canvas-asset";
5955
5957
  const SUBSCRIPTION_LOGIN_ROUTE = "/plugins/dsh-image-gen/subscription-login";
5956
5958
  const SUBSCRIPTION_STATUS_ROUTE = "/plugins/dsh-image-gen/subscription-status";
5957
5959
  /** Every accepted canvas node kind, for validating untrusted pushes. */
@@ -6077,6 +6079,33 @@ const DEFAULT_ZHIPU_BASE_URL = "https://open.bigmodel.cn/api/paas/v4";
6077
6079
  const DEFAULT_COMFYUI_BASE_URL = "http://127.0.0.1:8188";
6078
6080
  const DEFAULT_COMFYUI_TIMEOUT_MS = 3e5;
6079
6081
  const DEFAULT_COMFYUI_WORKFLOW_LABEL = "API workflow";
6082
+ /** Content types Ark's Seedream endpoint can return. */
6083
+ const ARK_OUTPUT_FORMATS = ["png", "jpeg"];
6084
+ /** Whether Ark stamps an "AI generated" watermark on the result. */
6085
+ const ARK_BACKGROUND_MODES = ["opaque", "transparent"];
6086
+ /**
6087
+ * Map the Ark output controls onto request-body fields.
6088
+ *
6089
+ * `background` is opt-in per call site because Ark restricts `transparent` to
6090
+ * image-to-image with a single alpha-bearing reference and rejects the whole
6091
+ * request otherwise:
6092
+ *
6093
+ * text-to-image + transparent → 400 InvalidParameter
6094
+ * "transparent background requires exactly one input image"
6095
+ *
6096
+ * So the generation endpoint must never carry it, and that caller passes
6097
+ * `{ background: false }`. `background: opaque` is never emitted anywhere: it
6098
+ * is already Ark's default, so sending it would change nothing while adding a
6099
+ * field only the edit path can act on.
6100
+ */
6101
+ function arkOutputBody(options, { background = true } = {}) {
6102
+ if (options === void 0) return {};
6103
+ const body = {};
6104
+ if (options.outputFormat !== void 0) body.output_format = options.outputFormat;
6105
+ if (options.watermark !== void 0) body.watermark = options.watermark;
6106
+ if (background && options.background === "transparent") body.background = "transparent";
6107
+ return body;
6108
+ }
6080
6109
  /** Default model names. */
6081
6110
  const DEFAULT_GOOGLE_MODEL = "gemini-3.1-flash-image";
6082
6111
  const DEFAULT_OPENAI_MODEL = "gpt-image-2";
@@ -6170,6 +6199,9 @@ const Config = z.object({
6170
6199
  openaiCompatEditExtra: z.dict(z.any()).default({}),
6171
6200
  seedreamBaseURL: z.string().default(DEFAULT_SEEDREAM_BASE_URL),
6172
6201
  seedreamModel: z.string().default(DEFAULT_SEEDREAM_MODEL),
6202
+ seedreamOutputFormat: z.union(ARK_OUTPUT_FORMATS).default("jpeg"),
6203
+ seedreamWatermark: z.boolean().default(true),
6204
+ seedreamBackground: z.union(ARK_BACKGROUND_MODES).default("opaque"),
6173
6205
  dashscopeEndpoint: z.string().default(DEFAULT_DASHSCOPE_ENDPOINT),
6174
6206
  dashscopeModel: z.string().default(DEFAULT_DASHSCOPE_MODEL),
6175
6207
  xaiBaseURL: z.string().default(DEFAULT_XAI_BASE_URL),
@@ -6214,13 +6246,21 @@ function resolveProvider(config) {
6214
6246
  editExtra: config.openaiCompatEditExtra ?? {}
6215
6247
  };
6216
6248
  }
6217
- case "seedream": return {
6218
- provider: "seedream",
6219
- apiKeyEnv: SEEDREAM_API_KEY_ENV,
6220
- model: config.seedreamModel ?? "doubao-seedream-5-0-260128",
6221
- baseURL: config.seedreamBaseURL ?? "https://ark.cn-beijing.volces.com/api/v3",
6222
- imageSize: "2K"
6223
- };
6249
+ case "seedream": {
6250
+ const seedreamBackground = config.seedreamBackground ?? "opaque";
6251
+ return {
6252
+ provider: "seedream",
6253
+ apiKeyEnv: SEEDREAM_API_KEY_ENV,
6254
+ model: config.seedreamModel ?? "doubao-seedream-5-0-260128",
6255
+ baseURL: config.seedreamBaseURL ?? "https://ark.cn-beijing.volces.com/api/v3",
6256
+ imageSize: "2K",
6257
+ arkOptions: {
6258
+ outputFormat: seedreamBackground === "transparent" ? "png" : config.seedreamOutputFormat ?? "jpeg",
6259
+ watermark: config.seedreamWatermark ?? true,
6260
+ background: seedreamBackground
6261
+ }
6262
+ };
6263
+ }
6224
6264
  case "dashscope": return {
6225
6265
  provider: "dashscope",
6226
6266
  apiKeyEnv: DASHSCOPE_API_KEY_ENV,
@@ -6469,6 +6509,7 @@ function freshestEntry(entries) {
6469
6509
  */
6470
6510
  var CanvasMirror = class {
6471
6511
  entries = /* @__PURE__ */ new Map();
6512
+ sequences = /* @__PURE__ */ new Map();
6472
6513
  nextSeq = 0;
6473
6514
  /**
6474
6515
  * Apply one validated push. A disconnect removes the instance; an unchanged
@@ -6476,6 +6517,14 @@ var CanvasMirror = class {
6476
6517
  * still view them while unrelated canvas edits keep flowing in.
6477
6518
  */
6478
6519
  apply(push, selectionImage) {
6520
+ for (const [id, item] of this.sequences) if (Date.now() - item.receivedAt > 3e5) this.sequences.delete(id);
6521
+ if (push.sequence !== void 0) {
6522
+ if (push.sequence <= (this.sequences.get(push.clientInstance)?.value ?? -1)) return;
6523
+ this.sequences.set(push.clientInstance, {
6524
+ value: push.sequence,
6525
+ receivedAt: Date.now()
6526
+ });
6527
+ }
6479
6528
  if (!push.connected) {
6480
6529
  this.entries.delete(push.clientInstance);
6481
6530
  return;
@@ -6484,8 +6533,8 @@ var CanvasMirror = class {
6484
6533
  const selectionCount = push.selection?.count ?? 0;
6485
6534
  const selectionKinds = [...push.selection?.kinds ?? []];
6486
6535
  const selectionItems = [...push.selection?.items ?? []];
6487
- const selectionSignature = signatureOf(selectionCount, selectionKinds, selectionItems);
6488
- const keepImage = previous !== void 0 && previous.selectionSignature === selectionSignature ? previous.selectionImage : void 0;
6536
+ const selectionSignature = push.selectionRevision === void 0 ? signatureOf(selectionCount, selectionKinds, selectionItems) : JSON.stringify([push.selectionRevision, selectionCount]);
6537
+ const keepImage = push.selectionStatus !== "preparing" && previous !== void 0 && previous.selectionSignature === selectionSignature ? previous.selectionImage : void 0;
6489
6538
  this.entries.set(push.clientInstance, {
6490
6539
  clientInstance: push.clientInstance,
6491
6540
  connected: true,
@@ -6495,6 +6544,8 @@ var CanvasMirror = class {
6495
6544
  selectionKinds,
6496
6545
  selectionItems,
6497
6546
  selectionSignature,
6547
+ ...push.selectionStatus === void 0 ? {} : { selectionStatus: push.selectionStatus },
6548
+ ...push.selectionError === void 0 ? {} : { selectionError: push.selectionError },
6498
6549
  ...selectionImage !== void 0 ? { selectionImage } : keepImage !== void 0 ? { selectionImage: keepImage } : {},
6499
6550
  receivedAt: Date.now(),
6500
6551
  seq: ++this.nextSeq,
@@ -6540,217 +6591,14 @@ var CanvasMirror = class {
6540
6591
  if (entry.selectionItems.length > DIGEST_SELECTION_LIMIT) lines.push(`- ... ${entry.selectionItems.length - DIGEST_SELECTION_LIMIT} more selected shapes omitted`);
6541
6592
  const unlistedSelection = Math.max(0, entry.selectionCount - entry.selectionItems.length);
6542
6593
  if (unlistedSelection > 0) lines.push(`- (${unlistedSelection} further selected shapes not itemized)`);
6543
- const screenshot = entry.selectionImage !== void 0 ? "A screenshot of the selection is available; call view_canvas to see it. edit_image with source=canvas_selection sends the full-resolution originals of selected conversation images, plus the screenshot when the selection also contains other content (hand-drawn strokes, pasted images)." : "No selection screenshot yet — the canvas pushes one about a second after the selection settles. edit_image with source=canvas_selection can still use the full-resolution originals of selected conversation images; for anything else ask the user to re-select the shapes.";
6594
+ const screenshot = entry.selectionImage !== void 0 ? "A screenshot of the selection is available; call view_canvas to see it. edit_image with source=canvas_selection uses separately synchronized originals, including images from earlier conversations and imports, plus the screenshot for mixed selections. Provider reference-count limits still apply." : entry.selectionError !== void 0 ? `Selection synchronization failed: ${entry.selectionError}. Ask the user to retry using the canvas status bar. Do not claim that image-only selections are invisible or suggest adding a stroke.` : "The selection preview and original images are being prepared. Wait for the canvas status bar to report ready, then retry.";
6544
6595
  lines.push(screenshot);
6596
+ if (entry.selectionImage !== void 0 && entry.selectionError !== void 0) lines.push(`The preview is available, but original synchronization failed: ${entry.selectionError}. Editing must not silently omit these references.`);
6545
6597
  }
6546
6598
  return lines.join("\n");
6547
6599
  }
6548
6600
  };
6549
6601
  //#endregion
6550
- //#region lib/types/canvas-state-route.js
6551
- const MAX_CLIENT_INSTANCE_CHARS = 64;
6552
- const MAX_NODE_TEXT_CHARS = 200;
6553
- const MAX_ID_CHARS = 256;
6554
- /** Provider ids come from the shared enum (longest is "openai-compat"). */
6555
- const MAX_PROVIDER_CHARS = 32;
6556
- /** Model ids and ComfyUI workflow labels are user-facing strings. */
6557
- const MAX_MODEL_CHARS = 128;
6558
- const MAX_NODE_COUNT = 1e5;
6559
- const MAX_SELECTION_COUNT = 1e3;
6560
- /** Serve one canvas-state push. */
6561
- async function serveCanvasState(req, res, deps) {
6562
- if (req.method !== "POST") return jsonError$5(res, 405, "method-not-allowed");
6563
- if (!(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) return jsonError$5(res, 415, "json-required");
6564
- const origin = req.headers.origin;
6565
- const host = req.headers.host;
6566
- if (origin !== void 0 && host !== void 0 && origin !== `http://${host}` && origin !== `https://${host}`) return jsonError$5(res, 403, "origin-rejected");
6567
- let body;
6568
- try {
6569
- body = JSON.parse(await readBody$4(req, deps.maxBodyBytes));
6570
- } catch (error) {
6571
- return jsonError$5(res, error instanceof Error && error.message === "request-too-large" ? 413 : 400, "invalid-request");
6572
- }
6573
- const push = parseCanvasStatePush(body);
6574
- if (push === void 0) return jsonError$5(res, 400, "invalid-canvas-state");
6575
- let selectionImage;
6576
- if (push.selectionImage !== void 0) {
6577
- const decoded = decodeSelectionImage(push.selectionImage);
6578
- if (decoded === void 0) return jsonError$5(res, 400, "invalid-selection-image");
6579
- if (decoded.data.byteLength > deps.maxImageBytes) return jsonError$5(res, 413, "selection-image-too-large");
6580
- selectionImage = decoded;
6581
- }
6582
- deps.mirror.apply(push, selectionImage);
6583
- res.writeHead(200, {
6584
- "content-type": "application/json",
6585
- "cache-control": "no-store"
6586
- });
6587
- res.end(JSON.stringify({ ok: true }));
6588
- }
6589
- /**
6590
- * Validate one untrusted canvas-state push at the HTTP boundary.
6591
- * Returns a normalized copy, or undefined when any field is malformed.
6592
- * Present-but-malformed known fields reject the whole push: they signal a
6593
- * client/server version skew rather than a legitimately absent value.
6594
- */
6595
- function parseCanvasStatePush(value) {
6596
- try {
6597
- return parsePush(value);
6598
- } catch (error) {
6599
- if (error instanceof InvalidPush) return void 0;
6600
- throw error;
6601
- }
6602
- }
6603
- /** Sentinel thrown by field validators; caught at the public parse boundary. */
6604
- var InvalidPush = class extends Error {};
6605
- function parsePush(value) {
6606
- const record = plainRecord(value);
6607
- if (record === void 0) throw new InvalidPush();
6608
- const clientInstance = boundedString(record.clientInstance, 1, MAX_CLIENT_INSTANCE_CHARS);
6609
- if (clientInstance === void 0) throw new InvalidPush();
6610
- if (typeof record.connected !== "boolean") throw new InvalidPush();
6611
- if (!nonNegativeInteger$1(record.nodeCount) || record.nodeCount > MAX_NODE_COUNT) throw new InvalidPush();
6612
- if (!nonNegativeInteger$1(record.updatedAt)) throw new InvalidPush();
6613
- if (Math.abs(record.updatedAt - Date.now()) > 3e5) throw new InvalidPush();
6614
- let nodes;
6615
- if (record.nodes !== void 0) {
6616
- if (!Array.isArray(record.nodes) || record.nodes.length > 48) throw new InvalidPush();
6617
- const parsed = [];
6618
- for (const raw of record.nodes) {
6619
- const node = parseNodeSummary(raw);
6620
- if (node === void 0) throw new InvalidPush();
6621
- parsed.push(node);
6622
- }
6623
- nodes = parsed;
6624
- }
6625
- let selection;
6626
- if (record.selection !== void 0) {
6627
- const raw = plainRecord(record.selection);
6628
- if (raw === void 0) throw new InvalidPush();
6629
- if (!nonNegativeInteger$1(raw.count) || raw.count > MAX_SELECTION_COUNT) throw new InvalidPush();
6630
- if (!Array.isArray(raw.kinds) || raw.kinds.length > 8) throw new InvalidPush();
6631
- const kinds = [];
6632
- for (const kind of raw.kinds) {
6633
- if (typeof kind !== "string" || !CANVAS_NODE_KINDS.includes(kind)) throw new InvalidPush();
6634
- kinds.push(kind);
6635
- }
6636
- let items;
6637
- if (raw.items !== void 0) {
6638
- if (!Array.isArray(raw.items) || raw.items.length > 16) throw new InvalidPush();
6639
- if (raw.items.length > raw.count) throw new InvalidPush();
6640
- const parsed = [];
6641
- for (const item of raw.items) {
6642
- const node = parseNodeSummary(item);
6643
- if (node === void 0) throw new InvalidPush();
6644
- parsed.push(node);
6645
- }
6646
- items = parsed;
6647
- }
6648
- selection = items === void 0 ? {
6649
- count: raw.count,
6650
- kinds
6651
- } : {
6652
- count: raw.count,
6653
- kinds,
6654
- items
6655
- };
6656
- }
6657
- if (record.selectionImage !== void 0 && typeof record.selectionImage !== "string") throw new InvalidPush();
6658
- return {
6659
- clientInstance,
6660
- connected: record.connected,
6661
- nodeCount: record.nodeCount,
6662
- ...nodes === void 0 ? {} : { nodes },
6663
- ...selection === void 0 ? {} : { selection },
6664
- ...typeof record.selectionImage === "string" ? { selectionImage: record.selectionImage } : {},
6665
- updatedAt: record.updatedAt
6666
- };
6667
- }
6668
- /** Decode and sanity-check an embedded selection screenshot data URL. */
6669
- function decodeSelectionImage(dataUrl) {
6670
- const match = /^data:(image\/(?:png|jpeg|webp));base64,([A-Za-z0-9+/]*={0,2})$/.exec(dataUrl);
6671
- if (match === null) return void 0;
6672
- const mediaType = match[1];
6673
- const base64 = match[2];
6674
- if (mediaType === void 0 || base64 === void 0) return void 0;
6675
- try {
6676
- const buffer = Buffer.from(base64, "base64");
6677
- if (buffer.byteLength === 0) return void 0;
6678
- return {
6679
- data: new Uint8Array(buffer),
6680
- mediaType
6681
- };
6682
- } catch {
6683
- return;
6684
- }
6685
- }
6686
- function parseNodeSummary(value) {
6687
- const record = plainRecord(value);
6688
- if (record === void 0) return void 0;
6689
- if (typeof record.kind !== "string" || !CANVAS_NODE_KINDS.includes(record.kind)) return void 0;
6690
- const galleryId = optionalBoundedString(record.galleryId, MAX_ID_CHARS);
6691
- const attachmentId = optionalBoundedString(record.attachmentId, MAX_ID_CHARS);
6692
- const name = optionalBoundedString(record.name, MAX_ID_CHARS);
6693
- const text = optionalBoundedString(record.text, MAX_NODE_TEXT_CHARS);
6694
- const prompt = optionalBoundedString(record.prompt, 120);
6695
- const provider = optionalBoundedString(record.provider, MAX_PROVIDER_CHARS);
6696
- const model = optionalBoundedString(record.model, MAX_MODEL_CHARS);
6697
- const width = optionalDimension(record.width);
6698
- const height = optionalDimension(record.height);
6699
- return {
6700
- kind: record.kind,
6701
- ...galleryId === void 0 ? {} : { galleryId },
6702
- ...attachmentId === void 0 ? {} : { attachmentId },
6703
- ...name === void 0 ? {} : { name },
6704
- ...width === void 0 ? {} : { width },
6705
- ...height === void 0 ? {} : { height },
6706
- ...text === void 0 ? {} : { text },
6707
- ...prompt === void 0 ? {} : { prompt },
6708
- ...provider === void 0 ? {} : { provider },
6709
- ...model === void 0 ? {} : { model }
6710
- };
6711
- }
6712
- function plainRecord(value) {
6713
- return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
6714
- }
6715
- function boundedString(value, min, max) {
6716
- if (typeof value !== "string" || value.length < min || value.length > max) return void 0;
6717
- return value;
6718
- }
6719
- /** Absent is fine; present-but-not-a-bounded-string rejects the whole push. */
6720
- function optionalBoundedString(value, max) {
6721
- if (value === void 0) return void 0;
6722
- const parsed = boundedString(value, 0, max);
6723
- if (parsed === void 0) throw new InvalidPush();
6724
- return parsed;
6725
- }
6726
- /** Absent is fine; present-but-not-a-finite-dimension rejects the whole push. */
6727
- function optionalDimension(value) {
6728
- if (value === void 0) return void 0;
6729
- if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1e6) throw new InvalidPush();
6730
- return Math.round(value);
6731
- }
6732
- function nonNegativeInteger$1(value) {
6733
- return typeof value === "number" && Number.isInteger(value) && value >= 0;
6734
- }
6735
- async function readBody$4(req, maxBytes) {
6736
- const chunks = [];
6737
- let bytes = 0;
6738
- for await (const chunk of req) {
6739
- const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
6740
- bytes += buffer.byteLength;
6741
- if (bytes > maxBytes) throw new Error("request-too-large");
6742
- chunks.push(buffer);
6743
- }
6744
- return Buffer.concat(chunks).toString("utf8");
6745
- }
6746
- function jsonError$5(res, status, code) {
6747
- res.writeHead(status, {
6748
- "content-type": "application/json",
6749
- "cache-control": "no-store"
6750
- });
6751
- res.end(JSON.stringify({ error: code }));
6752
- }
6753
- //#endregion
6754
6602
  //#region lib/types/reference-image.js
6755
6603
  /** Latest-DSH compatibility boundary for resolving conversation image references. */
6756
6604
  /**
@@ -6860,7 +6708,7 @@ function parseImageAttachmentRef(value) {
6860
6708
  const ref = record$8(value);
6861
6709
  if (ref === void 0) return void 0;
6862
6710
  if (typeof ref.attachmentId !== "string" || !imageMediaType$4(ref.mediaType)) return void 0;
6863
- if (!nonNegativeInteger(ref.bytes) || !positiveInteger(ref.width) || !positiveInteger(ref.height)) return void 0;
6711
+ if (!nonNegativeInteger$1(ref.bytes) || !positiveInteger(ref.width) || !positiveInteger(ref.height)) return void 0;
6864
6712
  if (ref.name !== void 0 && typeof ref.name !== "string") return void 0;
6865
6713
  const originalDimensions = ref.originalDimensions === void 0 ? void 0 : parseDimensions(ref.originalDimensions);
6866
6714
  if (ref.originalDimensions !== void 0 && originalDimensions === void 0) return void 0;
@@ -6958,13 +6806,263 @@ function containsPath$1(parent, child) {
6958
6806
  function positiveInteger(value) {
6959
6807
  return typeof value === "number" && Number.isInteger(value) && value > 0;
6960
6808
  }
6961
- function nonNegativeInteger(value) {
6809
+ function nonNegativeInteger$1(value) {
6962
6810
  return typeof value === "number" && Number.isInteger(value) && value >= 0;
6963
6811
  }
6964
6812
  function record$8(value) {
6965
6813
  return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
6966
6814
  }
6967
6815
  //#endregion
6816
+ //#region lib/types/canvas-state-route.js
6817
+ const MAX_CLIENT_INSTANCE_CHARS = 64;
6818
+ const MAX_NODE_TEXT_CHARS = 200;
6819
+ const MAX_ID_CHARS = 256;
6820
+ /** Provider ids come from the shared enum (longest is "openai-compat"). */
6821
+ const MAX_PROVIDER_CHARS = 32;
6822
+ /** Model ids and ComfyUI workflow labels are user-facing strings. */
6823
+ const MAX_MODEL_CHARS = 128;
6824
+ const MAX_NODE_COUNT = 1e5;
6825
+ const MAX_SELECTION_COUNT = 1e3;
6826
+ /** Serve one canvas-state push. */
6827
+ async function serveCanvasState(req, res, deps) {
6828
+ if (req.method !== "POST") return jsonError$5(res, 405, "method-not-allowed");
6829
+ if (!(req.headers["content-type"] ?? "").toLowerCase().startsWith("application/json")) return jsonError$5(res, 415, "json-required");
6830
+ const origin = req.headers.origin;
6831
+ const host = req.headers.host;
6832
+ if (origin !== void 0 && host !== void 0 && origin !== `http://${host}` && origin !== `https://${host}`) return jsonError$5(res, 403, "origin-rejected");
6833
+ let body;
6834
+ try {
6835
+ body = JSON.parse(await readBody$4(req, deps.maxBodyBytes));
6836
+ } catch (error) {
6837
+ return jsonError$5(res, error instanceof Error && error.message === "request-too-large" ? 413 : 400, "invalid-request");
6838
+ }
6839
+ const push = parseCanvasStatePush(body);
6840
+ if (push === void 0) return jsonError$5(res, 400, "invalid-canvas-state");
6841
+ let selectionImage;
6842
+ if (push.selectionImage !== void 0) {
6843
+ const decoded = decodeSelectionImage(push.selectionImage);
6844
+ if (decoded === void 0) return jsonError$5(res, 400, "invalid-selection-image");
6845
+ if (decoded.data.byteLength > deps.maxImageBytes) return jsonError$5(res, 413, "selection-image-too-large");
6846
+ selectionImage = decoded;
6847
+ }
6848
+ deps.mirror.apply(push, selectionImage);
6849
+ res.writeHead(200, {
6850
+ "content-type": "application/json",
6851
+ "cache-control": "no-store"
6852
+ });
6853
+ res.end(JSON.stringify({ ok: true }));
6854
+ }
6855
+ /**
6856
+ * Validate one untrusted canvas-state push at the HTTP boundary.
6857
+ * Returns a normalized copy, or undefined when any field is malformed.
6858
+ * Present-but-malformed known fields reject the whole push: they signal a
6859
+ * client/server version skew rather than a legitimately absent value.
6860
+ */
6861
+ function parseCanvasStatePush(value) {
6862
+ try {
6863
+ return parsePush(value);
6864
+ } catch (error) {
6865
+ if (error instanceof InvalidPush) return void 0;
6866
+ throw error;
6867
+ }
6868
+ }
6869
+ /** Sentinel thrown by field validators; caught at the public parse boundary. */
6870
+ var InvalidPush = class extends Error {};
6871
+ function parsePush(value) {
6872
+ const record = plainRecord(value);
6873
+ if (record === void 0) throw new InvalidPush();
6874
+ const clientInstance = boundedString(record.clientInstance, 1, MAX_CLIENT_INSTANCE_CHARS);
6875
+ if (clientInstance === void 0) throw new InvalidPush();
6876
+ if (typeof record.connected !== "boolean") throw new InvalidPush();
6877
+ if (!nonNegativeInteger(record.nodeCount) || record.nodeCount > MAX_NODE_COUNT) throw new InvalidPush();
6878
+ if (!nonNegativeInteger(record.updatedAt)) throw new InvalidPush();
6879
+ if (Math.abs(record.updatedAt - Date.now()) > 3e5) throw new InvalidPush();
6880
+ let nodes;
6881
+ if (record.nodes !== void 0) {
6882
+ if (!Array.isArray(record.nodes) || record.nodes.length > 48) throw new InvalidPush();
6883
+ const parsed = [];
6884
+ for (const raw of record.nodes) {
6885
+ const node = parseNodeSummary(raw);
6886
+ if (node === void 0) throw new InvalidPush();
6887
+ parsed.push(node);
6888
+ }
6889
+ nodes = parsed;
6890
+ }
6891
+ let selection;
6892
+ if (record.selection !== void 0) {
6893
+ const raw = plainRecord(record.selection);
6894
+ if (raw === void 0) throw new InvalidPush();
6895
+ if (!nonNegativeInteger(raw.count) || raw.count > MAX_SELECTION_COUNT) throw new InvalidPush();
6896
+ if (!Array.isArray(raw.kinds) || raw.kinds.length > 8) throw new InvalidPush();
6897
+ const kinds = [];
6898
+ for (const kind of raw.kinds) {
6899
+ if (typeof kind !== "string" || !CANVAS_NODE_KINDS.includes(kind)) throw new InvalidPush();
6900
+ kinds.push(kind);
6901
+ }
6902
+ let items;
6903
+ if (raw.items !== void 0) {
6904
+ if (!Array.isArray(raw.items) || raw.items.length > 16) throw new InvalidPush();
6905
+ if (raw.items.length > raw.count) throw new InvalidPush();
6906
+ const parsed = [];
6907
+ for (const item of raw.items) {
6908
+ const node = parseNodeSummary(item);
6909
+ if (node === void 0) throw new InvalidPush();
6910
+ parsed.push(node);
6911
+ }
6912
+ items = parsed;
6913
+ }
6914
+ selection = items === void 0 ? {
6915
+ count: raw.count,
6916
+ kinds
6917
+ } : {
6918
+ count: raw.count,
6919
+ kinds,
6920
+ items
6921
+ };
6922
+ }
6923
+ if (record.selectionImage !== void 0 && typeof record.selectionImage !== "string") throw new InvalidPush();
6924
+ const selectionRevision = optionalBoundedString(record.selectionRevision, 64);
6925
+ const selectionError = optionalBoundedString(record.selectionError, 300);
6926
+ const selectionStatus = record.selectionStatus;
6927
+ if (selectionStatus !== void 0 && selectionStatus !== "preparing" && selectionStatus !== "ready" && selectionStatus !== "error") throw new InvalidPush();
6928
+ if (record.sequence !== void 0 && (!nonNegativeInteger(record.sequence) || !Number.isSafeInteger(record.sequence))) throw new InvalidPush();
6929
+ return {
6930
+ clientInstance,
6931
+ connected: record.connected,
6932
+ nodeCount: record.nodeCount,
6933
+ ...nodes === void 0 ? {} : { nodes },
6934
+ ...selection === void 0 ? {} : { selection },
6935
+ ...typeof record.selectionImage === "string" ? { selectionImage: record.selectionImage } : {},
6936
+ ...selectionRevision === void 0 ? {} : { selectionRevision },
6937
+ ...selectionStatus === void 0 ? {} : { selectionStatus },
6938
+ ...selectionError === void 0 ? {} : { selectionError },
6939
+ ...record.sequence === void 0 ? {} : { sequence: record.sequence },
6940
+ updatedAt: record.updatedAt
6941
+ };
6942
+ }
6943
+ /** Decode and sanity-check an embedded selection screenshot data URL. */
6944
+ function decodeSelectionImage(dataUrl) {
6945
+ const match = /^data:(image\/(?:png|jpeg|webp));base64,([A-Za-z0-9+/]*={0,2})$/.exec(dataUrl);
6946
+ if (match === null) return void 0;
6947
+ const mediaType = match[1];
6948
+ const base64 = match[2];
6949
+ if (mediaType === void 0 || base64 === void 0) return void 0;
6950
+ try {
6951
+ const buffer = Buffer.from(base64, "base64");
6952
+ if (buffer.byteLength === 0) return void 0;
6953
+ return {
6954
+ data: new Uint8Array(buffer),
6955
+ mediaType
6956
+ };
6957
+ } catch {
6958
+ return;
6959
+ }
6960
+ }
6961
+ function parseNodeSummary(value) {
6962
+ const record = plainRecord(value);
6963
+ if (record === void 0) return void 0;
6964
+ if (typeof record.kind !== "string" || !CANVAS_NODE_KINDS.includes(record.kind)) return void 0;
6965
+ const galleryId = optionalBoundedString(record.galleryId, MAX_ID_CHARS);
6966
+ const attachmentId = optionalBoundedString(record.attachmentId, MAX_ID_CHARS);
6967
+ const attachment = record.attachment === void 0 ? void 0 : parseImageAttachmentRef(record.attachment);
6968
+ if (record.attachment !== void 0 && (attachment === void 0 || attachment.attachmentId.length > MAX_ID_CHARS || (attachment.name?.length ?? 0) > MAX_ID_CHARS)) throw new InvalidPush();
6969
+ const name = optionalBoundedString(record.name, MAX_ID_CHARS);
6970
+ const text = optionalBoundedString(record.text, MAX_NODE_TEXT_CHARS);
6971
+ const prompt = optionalBoundedString(record.prompt, 120);
6972
+ const provider = optionalBoundedString(record.provider, MAX_PROVIDER_CHARS);
6973
+ const model = optionalBoundedString(record.model, MAX_MODEL_CHARS);
6974
+ const width = optionalDimension(record.width);
6975
+ const height = optionalDimension(record.height);
6976
+ return {
6977
+ kind: record.kind,
6978
+ ...galleryId === void 0 ? {} : { galleryId },
6979
+ ...attachmentId === void 0 ? {} : { attachmentId },
6980
+ ...attachment === void 0 ? {} : { attachment },
6981
+ ...name === void 0 ? {} : { name },
6982
+ ...width === void 0 ? {} : { width },
6983
+ ...height === void 0 ? {} : { height },
6984
+ ...text === void 0 ? {} : { text },
6985
+ ...prompt === void 0 ? {} : { prompt },
6986
+ ...provider === void 0 ? {} : { provider },
6987
+ ...model === void 0 ? {} : { model }
6988
+ };
6989
+ }
6990
+ function plainRecord(value) {
6991
+ return typeof value === "object" && value !== null && !Array.isArray(value) ? value : void 0;
6992
+ }
6993
+ function boundedString(value, min, max) {
6994
+ if (typeof value !== "string" || value.length < min || value.length > max) return void 0;
6995
+ return value;
6996
+ }
6997
+ /** Absent is fine; present-but-not-a-bounded-string rejects the whole push. */
6998
+ function optionalBoundedString(value, max) {
6999
+ if (value === void 0) return void 0;
7000
+ const parsed = boundedString(value, 0, max);
7001
+ if (parsed === void 0) throw new InvalidPush();
7002
+ return parsed;
7003
+ }
7004
+ /** Absent is fine; present-but-not-a-finite-dimension rejects the whole push. */
7005
+ function optionalDimension(value) {
7006
+ if (value === void 0) return void 0;
7007
+ if (typeof value !== "number" || !Number.isFinite(value) || value < 0 || value > 1e6) throw new InvalidPush();
7008
+ return Math.round(value);
7009
+ }
7010
+ function nonNegativeInteger(value) {
7011
+ return typeof value === "number" && Number.isInteger(value) && value >= 0;
7012
+ }
7013
+ async function readBody$4(req, maxBytes) {
7014
+ const chunks = [];
7015
+ let bytes = 0;
7016
+ for await (const chunk of req) {
7017
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
7018
+ bytes += buffer.byteLength;
7019
+ if (bytes > maxBytes) throw new Error("request-too-large");
7020
+ chunks.push(buffer);
7021
+ }
7022
+ return Buffer.concat(chunks).toString("utf8");
7023
+ }
7024
+ function jsonError$5(res, status, code) {
7025
+ res.writeHead(status, {
7026
+ "content-type": "application/json",
7027
+ "cache-control": "no-store"
7028
+ });
7029
+ res.end(JSON.stringify({ error: code }));
7030
+ }
7031
+ //#endregion
7032
+ //#region lib/types/canvas-asset-route.js
7033
+ async function serveCanvasAsset(req, res, deps) {
7034
+ const reply = (status, body) => {
7035
+ res.writeHead(status, {
7036
+ "content-type": "application/json",
7037
+ "cache-control": "no-store"
7038
+ });
7039
+ res.end(JSON.stringify(body));
7040
+ };
7041
+ const { origin, host } = req.headers;
7042
+ if (origin !== void 0 && host !== void 0 && origin !== `http://${host}` && origin !== `https://${host}`) return reply(403, { error: "origin-rejected" });
7043
+ if (req.method === "GET") return reply(200, { maxImageBytes: deps.maxImageBytes });
7044
+ if (req.method !== "POST") return reply(405, { error: "method-not-allowed" });
7045
+ const mediaType = req.headers["content-type"]?.split(";")[0]?.trim();
7046
+ if (mediaType !== "image/png" && mediaType !== "image/jpeg" && mediaType !== "image/webp" && mediaType !== "image/gif") return reply(415, { error: "unsupported-image-type" });
7047
+ try {
7048
+ const chunks = [];
7049
+ let bytes = 0;
7050
+ for await (const chunk of req) {
7051
+ const buffer = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
7052
+ bytes += buffer.byteLength;
7053
+ if (bytes > deps.maxImageBytes) return reply(413, { error: "original-image-too-large" });
7054
+ chunks.push(buffer);
7055
+ }
7056
+ if (bytes === 0) return reply(400, { error: "empty-image" });
7057
+ reply(200, { attachment: await deps.saveImage({
7058
+ data: Buffer.concat(chunks),
7059
+ mediaType
7060
+ }) });
7061
+ } catch {
7062
+ reply(400, { error: "canvas-original-unavailable" });
7063
+ }
7064
+ }
7065
+ //#endregion
6968
7066
  //#region lib/types/canvas-tools.js
6969
7067
  /** Register the canvas tools on a context that owns a canvas mirror. */
6970
7068
  function registerCanvasTools(ctx, mirror, deps) {
@@ -7060,7 +7158,7 @@ function registerCanvasTools(ctx, mirror, deps) {
7060
7158
  render: (_args, value) => {
7061
7159
  if (value.attachment === void 0) return [{
7062
7160
  type: "text",
7063
- text: `${value.digest}\nNo selection screenshot is available. Ask the user to open the image-gen workbench infinite canvas and select the shapes, then try again.`
7161
+ text: value.digest
7064
7162
  }];
7065
7163
  return [{
7066
7164
  type: "text",
@@ -7102,18 +7200,19 @@ async function resolveCanvasSelectionReferences(input) {
7102
7200
  const entry = input.mirror.latest();
7103
7201
  if (entry === void 0) throw new Error("The image-gen workbench canvas is not connected. Ask the user to open the image-gen workbench (gallery tab or right-sidebar studio tab), switch to the infinite canvas view, and select the shapes to edit.");
7104
7202
  if (entry.selectionCount === 0) throw new Error("The image-gen workbench canvas has no selection to use as a reference image. Ask the user to select the shapes (for example their sketch) on the infinite canvas, then retry.");
7203
+ if (entry.selectionStatus === "preparing") throw new Error("Canvas selection is still preparing. Wait for the canvas status bar to report ready, then retry.");
7105
7204
  const attachmentIds = [];
7106
- let attachmentBacked = 0;
7107
7205
  for (const item of entry.selectionItems) {
7108
7206
  if (item.kind !== "image" || typeof item.attachmentId !== "string" || item.attachmentId.length === 0) continue;
7109
7207
  attachmentIds.push(item.attachmentId);
7110
- attachmentBacked += 1;
7111
7208
  }
7112
7209
  const fullCoverage = entry.selectionItems.length === entry.selectionCount;
7113
7210
  const originals = [];
7114
- if (attachmentIds.length > 0 && input.agent !== void 0) {
7115
- const refs = findReferenceImages(input.agent.session.deriveMessages(), attachmentIds);
7116
- for (const ref of refs) {
7211
+ const conversationRefs = input.agent === void 0 || attachmentIds.length === 0 ? [] : findReferenceImages(input.agent.session.deriveMessages(), attachmentIds);
7212
+ for (const item of entry.selectionItems) {
7213
+ if (item.kind !== "image") continue;
7214
+ const ref = item.attachment ?? conversationRefs.find((candidate) => candidate.attachmentId === item.attachmentId);
7215
+ if (ref !== void 0) {
7117
7216
  const stored = await input.attachments.readImage(ref, input.signal);
7118
7217
  if (input.maxBytes !== void 0 && stored.data.byteLength > input.maxBytes) throw new Error(`edit_image canvas selection image is too large (${stored.data.byteLength} bytes; maximum ${input.maxBytes})`);
7119
7218
  originals.push({
@@ -7122,10 +7221,10 @@ async function resolveCanvasSelectionReferences(input) {
7122
7221
  });
7123
7222
  }
7124
7223
  }
7125
- if (originals.length > 0 && fullCoverage && attachmentBacked === entry.selectionItems.length && originals.length === attachmentIds.length) return originals;
7224
+ if (originals.length > 0 && fullCoverage && entry.selectionItems.every((item) => item.kind === "image") && originals.length === entry.selectionCount) return originals;
7225
+ if (entry.selectionError !== void 0) throw new Error(`Canvas selection synchronization failed: ${entry.selectionError}. Retry from the canvas status bar; no selected references were omitted.`);
7126
7226
  const screenshot = readSelectionScreenshot(entry, input);
7127
7227
  if (screenshot !== void 0) return [...originals, screenshot];
7128
- if (originals.length > 0) return originals;
7129
7228
  throw new Error("The image-gen workbench canvas selection has no usable reference image yet. Ask the user to keep the shapes selected for a moment longer (the canvas pushes a screenshot about a second after the selection settles), then retry.");
7130
7229
  }
7131
7230
  /** The mirrored selection screenshot bytes, or undefined when none was pushed. */
@@ -7511,6 +7610,7 @@ async function generateDashScopeImage(options) {
7511
7610
  });
7512
7611
  }
7513
7612
  async function editDashScopeImage(options) {
7613
+ if (options.sourceImages.length > 3) throw new Error(`DashScope image editing supports at most 3 reference images; this selection resolved ${options.sourceImages.length}. Select fewer images or choose a provider that supports more references. No images were omitted.`);
7514
7614
  assertQwenImageModel(options.model);
7515
7615
  const formattedSize = formatSize(options.size);
7516
7616
  return requestQwenImage({
@@ -8118,7 +8218,10 @@ async function generateOpenAICompatibleImage(input) {
8118
8218
  model: input.model,
8119
8219
  prompt: input.prompt,
8120
8220
  size: input.size,
8121
- ...input.provider === "seedream" ? { response_format: "url" } : {}
8221
+ ...input.provider === "seedream" ? {
8222
+ response_format: "url",
8223
+ ...arkOutputBody(input.arkOptions, { background: false })
8224
+ } : {}
8122
8225
  })
8123
8226
  }), input.provider, input);
8124
8227
  }
@@ -8306,6 +8409,7 @@ async function editSeedreamImage(input) {
8306
8409
  prompt: input.prompt,
8307
8410
  image: input.sourceImages.map(toDataUrl$1),
8308
8411
  ...input.size === void 0 || input.size.length === 0 ? {} : { size: input.size },
8412
+ ...arkOutputBody(input.arkOptions),
8309
8413
  response_format: "b64_json"
8310
8414
  })
8311
8415
  });
@@ -20624,7 +20728,8 @@ async function generateFromStudio(ctx, config, input, signal, fallbackWorkspaceR
20624
20728
  sourceImages,
20625
20729
  size,
20626
20730
  maxBytes: ctx.attachments.imageLimits.maxImageBytes,
20627
- signal
20731
+ signal,
20732
+ arkOptions: wired.arkOptions
20628
20733
  }) : await generateOpenAICompatibleImage({
20629
20734
  provider: "seedream",
20630
20735
  apiKey: credential,
@@ -20633,7 +20738,8 @@ async function generateFromStudio(ctx, config, input, signal, fallbackWorkspaceR
20633
20738
  prompt: input.prompt,
20634
20739
  size,
20635
20740
  maxBytes: ctx.attachments.imageLimits.maxImageBytes,
20636
- signal
20741
+ signal,
20742
+ arkOptions: wired.arkOptions
20637
20743
  });
20638
20744
  output = size;
20639
20745
  } else {
@@ -21521,6 +21627,14 @@ function apply(ctx, config = {}) {
21521
21627
  maxImageBytes: ctx.attachments.imageLimits.maxImageBytes
21522
21628
  })
21523
21629
  }), "dsh-image-gen: canvas state route");
21630
+ ctx.effect(() => ctx.webServer.register({
21631
+ kind: "exact",
21632
+ path: CANVAS_ASSET_ROUTE,
21633
+ handler: (req, res) => serveCanvasAsset(req, res, {
21634
+ maxImageBytes: ctx.attachments.imageLimits.maxImageBytes,
21635
+ saveImage: (image) => ctx.attachments.saveImage(image)
21636
+ })
21637
+ }), "dsh-image-gen: canvas asset route");
21524
21638
  ctx.inject(["systemPrompt"], (promptCtx) => {
21525
21639
  promptCtx.systemPrompt.context({
21526
21640
  name: "dsh-image-gen:canvas",
@@ -21675,6 +21789,7 @@ function apply(ctx, config = {}) {
21675
21789
  }), active.provider, active.model, size, current(), exec, knownWorkspaceRoots);
21676
21790
  }
21677
21791
  const size = args.size ?? active.imageSize;
21792
+ const arkOptions = active.provider === "seedream" ? active.arkOptions : void 0;
21678
21793
  return saveGenerated(ctx, await generateOpenAICompatibleImage({
21679
21794
  provider: active.provider,
21680
21795
  apiKey: credential,
@@ -21683,7 +21798,8 @@ function apply(ctx, config = {}) {
21683
21798
  prompt: args.prompt,
21684
21799
  size,
21685
21800
  maxBytes: ctx.attachments.imageLimits.maxImageBytes,
21686
- signal: exec.signal
21801
+ signal: exec.signal,
21802
+ ...arkOptions === void 0 ? {} : { arkOptions }
21687
21803
  }), active.provider, active.model, size, current(), exec, knownWorkspaceRoots);
21688
21804
  },
21689
21805
  presentResult: (_args, result) => imagePresentation(result)
@@ -21862,7 +21978,8 @@ function apply(ctx, config = {}) {
21862
21978
  sourceImages,
21863
21979
  size,
21864
21980
  maxBytes: ctx.attachments.imageLimits.maxImageBytes,
21865
- signal: exec.signal
21981
+ signal: exec.signal,
21982
+ arkOptions: active.arkOptions
21866
21983
  }), active.provider, active.model, size, current(), exec, knownWorkspaceRoots);
21867
21984
  return saveGenerated(ctx, await editDashScopeImage({
21868
21985
  apiKey: credential,