cdkkkk commited on
Commit
05df265
·
verified ·
1 Parent(s): c475e22

Upload index.html

Browse files
Files changed (1) hide show
  1. h3/index.html +201 -58
h3/index.html CHANGED
@@ -1250,6 +1250,8 @@ body {
1250
  </label>
1251
  <div class="ai-gen-actions">
1252
  <button id="aiGenRunBtn" class="btn primary" type="button">Generate</button>
 
 
1253
  <button id="aiGenStopBtn" class="btn" type="button" hidden>Stop</button>
1254
  <span id="aiGenStatus" class="llm-status"></span>
1255
  </div>
@@ -1373,6 +1375,8 @@ body {
1373
  <p id="textGenCtxNote" class="scene-hint"></p>
1374
  <div class="ai-gen-actions">
1375
  <button id="textGenRunBtn" class="btn primary" type="button">Generate</button>
 
 
1376
  <button id="textGenStopBtn" class="btn" type="button" hidden>Stop</button>
1377
  <span id="textGenStatus" class="llm-status"></span>
1378
  </div>
@@ -1440,6 +1444,8 @@ body {
1440
  <button id="askClearImgBtn" class="btn" type="button" hidden>Clear</button>
1441
  <span class="spacer"></span>
1442
  <button id="askRunBtn" class="btn primary" type="button">Send</button>
 
 
1443
  <button id="askStopBtn" class="btn" type="button" hidden>Stop</button>
1444
  <span id="askStatus" class="llm-status"></span>
1445
  </div>
@@ -5712,6 +5718,8 @@ let el;
5712
  let cfg = null;
5713
  let inflight = null;
5714
  let baseHint = "(editable)";
 
 
5715
 
5716
  function initTextAi() {
5717
  el = {
@@ -5729,6 +5737,7 @@ function initTextAi() {
5729
  ref: $("textGenRef"),
5730
  ctxNote: $("textGenCtxNote"),
5731
  runBtn: $("textGenRunBtn"),
 
5732
  stopBtn: $("textGenStopBtn"),
5733
  status: $("textGenStatus"),
5734
  resultHint: $("textGenResultHint"),
@@ -5746,6 +5755,7 @@ function initTextAi() {
5746
  });
5747
 
5748
  el.runBtn.addEventListener("click", run);
 
5749
  el.stopBtn.addEventListener("click", abortInflight);
5750
  el.acceptBtn.addEventListener("click", accept);
5751
  el.template.addEventListener("change", onTemplateChange);
@@ -5796,6 +5806,9 @@ function openTextAi(opts) {
5796
  el.template.value = sel;
5797
  applyTemplate(sel);
5798
  el.result.value = "";
 
 
 
5799
  updateCtxNote();
5800
  setBusy(false);
5801
  setStatus("Ready.", "");
@@ -5834,14 +5847,8 @@ function updateCtxNote() {
5834
  : "Generating from your instructions.";
5835
  }
5836
 
5837
- async function run() {
5838
- abortInflight();
5839
- const ctrl = new AbortController();
5840
- inflight = ctrl;
5841
- const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
5842
- setBusy(true);
5843
- setStatus("Generating…", "");
5844
-
5845
  // Reference context first, then the user's brief LAST, so the model treats
5846
  // the brief as the task to perform rather than as more context to echo.
5847
  const parts = [];
@@ -5858,21 +5865,34 @@ async function run() {
5858
  if (el.user.value.trim()) parts.push(`TASK (do this now):\n${el.user.value.trim()}`);
5859
  const userText = parts.join("\n\n") || el.user.value;
5860
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5861
  try {
5862
- const text = await chat(
5863
- [
5864
- { role: "system", content: el.system.value },
5865
- { role: "user", content: userText },
5866
- ],
5867
- { signal: ctrl.signal, model: getTemplateModel(cfg.purpose, el.template.value) }
5868
- );
5869
  el.result.value = text.trim();
5870
  setStatus(
5871
  el.result.value ? "Done." : "The model returned an empty response.",
5872
  el.result.value ? "ok" : "warn"
5873
  );
 
5874
  } catch (err) {
5875
  setStatus(err.message, "error");
 
5876
  } finally {
5877
  clearTimeout(timer);
5878
  inflight = null;
@@ -5880,6 +5900,39 @@ async function run() {
5880
  }
5881
  }
5882
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5883
  function accept() {
5884
  const text = el.result.value;
5885
  if (!text.trim()) return;
@@ -5897,8 +5950,8 @@ function abortInflight() {
5897
 
5898
  function setBusy(busy) {
5899
  el.stopBtn.hidden = !busy;
5900
- el.runBtn.textContent = el.result.value.trim() ? "Regenerate" : "Generate";
5901
- syncButtons(busy);
5902
  }
5903
 
5904
  function syncButtons(busy = false) {
@@ -6172,7 +6225,10 @@ GENERAL
6172
  - If a previous scene is provided, write the NEXT scene that CONTINUES it: keep continuity of subjects, setting, wardrobe and tone, but SHOW NEW, DIFFERENT action driven by the TASK. Do NOT repeat, restate, summarize, or reuse the previous scene's wording, shots, or events. Otherwise write a fresh opening scene.
6173
 
6174
  DETAILED_DESCRIPTION (main body, playback order; be detailed and explicit — aim for a rich ~350-500 words, but do not pad; dialogue-dense scenes may be shorter to fit the spoken timeline)
6175
- - ALWAYS begin detailed_description (BEFORE [Shot 1]) with a 1-2 sentence opening stating the visual STYLE from the brief (e.g. amateur home-video, shaky handheld, VHS, documentary, 2D-animated, cinematic...; do NOT default to "cinematic" unless asked). If any subject_definition is a place, room, or environment, that is the SETTING — you MUST reference it here by its <Subject N> label, not describe it (e.g. "An amateur home-video set in <Subject 1>."). THEN "[Shot 1]" (no timestamp) with the composition.
 
 
 
6176
  - Write ONE shot by default; add "[Shot N] At MM:SS.mmm, the camera cuts to ..." (strictly increasing time, new information) ONLY if the brief asks for multiple shots — otherwise stay in [Shot 1] and move the camera instead of cutting.
6177
  - Per shot, ground every detail in what is visible or audible: composition, subject position and action, environment and lighting (if the setting is a defined subject, name it by its <Subject N> label instead of re-describing it), state changes, camera movement, and synchronized sound/dialogue; note where each <Subject N>/<Picture N> appears or takes effect. Do NOT write a plot summary or a bare list of reference relationships. Cite a frame anchor naturally (e.g. "the shot begins from <Picture 1>").
6178
 
@@ -7378,6 +7434,8 @@ let currentDataUrl = null;
7378
  let imageShown = false;
7379
  /** @type {AbortController|null} */
7380
  let inflight = null;
 
 
7381
 
7382
  function initImageAi(d) {
7383
  deps = d;
@@ -7391,6 +7449,7 @@ function initImageAi(d) {
7391
  system: $("aiGenSystem"),
7392
  user: $("aiGenUser"),
7393
  runBtn: $("aiGenRunBtn"),
 
7394
  stopBtn: $("aiGenStopBtn"),
7395
  status: $("aiGenStatus"),
7396
  result: $("aiGenResult"),
@@ -7407,6 +7466,7 @@ function initImageAi(d) {
7407
  });
7408
 
7409
  el.runBtn.addEventListener("click", run); // "Generate", relabels to "Regenerate"
 
7410
  el.stopBtn.addEventListener("click", abortInflight);
7411
  el.acceptBtn.addEventListener("click", accept);
7412
  el.toggleImgBtn.addEventListener("click", toggleImage);
@@ -7431,6 +7491,9 @@ async function openImageAi(entry) {
7431
  el.template.value = sel;
7432
  applyTemplate(sel);
7433
  el.result.value = "";
 
 
 
7434
  el.imgName.textContent = entry.name;
7435
  syncImageToggle();
7436
  setBusy(false);
@@ -7495,40 +7558,42 @@ function close() {
7495
  el.root.hidden = true;
7496
  }
7497
 
7498
- async function run() {
7499
- if (!currentDataUrl) {
7500
- setStatus("Image not loaded yet.", "warn");
7501
- return;
7502
- }
 
 
 
 
 
 
 
 
7503
 
 
 
7504
  abortInflight();
7505
  const ctrl = new AbortController();
7506
  inflight = ctrl;
7507
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
7508
  setBusy(true);
7509
  setStatus("Generating…", "");
7510
-
7511
  try {
7512
- const text = await chat(
7513
- [
7514
- { role: "system", content: el.system.value },
7515
- {
7516
- role: "user",
7517
- content: [
7518
- { type: "text", text: el.user.value },
7519
- { type: "image_url", image_url: { url: currentDataUrl } },
7520
- ],
7521
- },
7522
- ],
7523
- { signal: ctrl.signal, model: getTemplateModel(PURPOSE, el.template.value) }
7524
- );
7525
  el.result.value = text.trim();
7526
  setStatus(
7527
  el.result.value ? "Done." : "The model returned an empty response.",
7528
  el.result.value ? "ok" : "warn"
7529
  );
 
7530
  } catch (err) {
7531
  setStatus(err.message, "error");
 
7532
  } finally {
7533
  clearTimeout(timer);
7534
  inflight = null;
@@ -7536,6 +7601,41 @@ async function run() {
7536
  }
7537
  }
7538
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7539
  function accept() {
7540
  const text = el.result.value;
7541
  if (!text.trim()) return;
@@ -7554,8 +7654,7 @@ function abortInflight() {
7554
  /** Enable/disable controls for the generating vs idle states. */
7555
  function setBusy(busy) {
7556
  el.stopBtn.hidden = !busy;
7557
- // The single primary button generates, then regenerates once there's output.
7558
- el.runBtn.textContent = el.result.value.trim() ? "Regenerate" : "Generate";
7559
  syncButtons(busy);
7560
  }
7561
 
@@ -7601,6 +7700,8 @@ const GEN_TIMEOUT_MS = 120000;
7601
 
7602
  let el;
7603
  let inflight = null;
 
 
7604
 
7605
  function initAskAi() {
7606
  el = {
@@ -7612,6 +7713,7 @@ function initAskAi() {
7612
  imageInput: $("askImageInput"),
7613
  clearImgBtn: $("askClearImgBtn"),
7614
  runBtn: $("askRunBtn"),
 
7615
  stopBtn: $("askStopBtn"),
7616
  status: $("askStatus"),
7617
  result: $("askResult"),
@@ -7636,6 +7738,7 @@ function initAskAi() {
7636
  el.clearImgBtn.hidden = true;
7637
  });
7638
  el.runBtn.addEventListener("click", run);
 
7639
  el.stopBtn.addEventListener("click", abortInflight);
7640
  el.result.addEventListener("input", syncCopy);
7641
  el.copyBtn.addEventListener("click", copy);
@@ -7643,6 +7746,8 @@ function initAskAi() {
7643
 
7644
  function openAskAi() {
7645
  populateTemplates();
 
 
7646
  setBusy(false);
7647
  setStatus("", "");
7648
  syncCopy();
@@ -7686,39 +7791,44 @@ function onTemplateChange() {
7686
  el.user.value = getTemplateValue(pid, tid, "user");
7687
  }
7688
 
7689
- async function run() {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7690
  abortInflight();
7691
  const ctrl = new AbortController();
7692
  inflight = ctrl;
7693
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
7694
  setBusy(true);
7695
  setStatus("Sending…", "");
7696
-
7697
  try {
7698
- const messages = [];
7699
- if (el.system.value.trim()) messages.push({ role: "system", content: el.system.value });
7700
-
7701
- const file = el.imageInput.files[0];
7702
- if (file) {
7703
- const dataUrl = await fileToDataUrl(file);
7704
- messages.push({
7705
- role: "user",
7706
- content: [
7707
- { type: "text", text: el.user.value },
7708
- { type: "image_url", image_url: { url: dataUrl } },
7709
- ],
7710
- });
7711
- } else {
7712
- messages.push({ role: "user", content: el.user.value });
7713
- }
7714
-
7715
  const [pid, tid] = el.template.value.split("::");
7716
  const model = pid ? getTemplateModel(pid, tid) : "";
7717
  const text = await chat(messages, { signal: ctrl.signal, model });
7718
  el.result.value = text.trim();
7719
  setStatus(el.result.value ? "Done." : "Empty response.", el.result.value ? "ok" : "warn");
 
7720
  } catch (err) {
7721
  setStatus(err.message, "error");
 
7722
  } finally {
7723
  clearTimeout(timer);
7724
  inflight = null;
@@ -7727,6 +7837,38 @@ async function run() {
7727
  }
7728
  }
7729
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7730
  async function copy() {
7731
  try {
7732
  await navigator.clipboard.writeText(el.result.value);
@@ -7745,6 +7887,7 @@ function abortInflight() {
7745
 
7746
  function setBusy(busy) {
7747
  el.runBtn.disabled = busy;
 
7748
  el.stopBtn.hidden = !busy;
7749
  }
7750
 
 
1250
  </label>
1251
  <div class="ai-gen-actions">
1252
  <button id="aiGenRunBtn" class="btn primary" type="button">Generate</button>
1253
+ <button id="aiGenRefineBtn" class="btn" type="button" hidden
1254
+ title="Iterate: sends your user prompt as a follow-up, keeping the conversation and image context">Refine</button>
1255
  <button id="aiGenStopBtn" class="btn" type="button" hidden>Stop</button>
1256
  <span id="aiGenStatus" class="llm-status"></span>
1257
  </div>
 
1375
  <p id="textGenCtxNote" class="scene-hint"></p>
1376
  <div class="ai-gen-actions">
1377
  <button id="textGenRunBtn" class="btn primary" type="button">Generate</button>
1378
+ <button id="textGenRefineBtn" class="btn" type="button" hidden
1379
+ title="Iterate on the result: sends your user prompt as a follow-up, keeping the conversation">Refine</button>
1380
  <button id="textGenStopBtn" class="btn" type="button" hidden>Stop</button>
1381
  <span id="textGenStatus" class="llm-status"></span>
1382
  </div>
 
1444
  <button id="askClearImgBtn" class="btn" type="button" hidden>Clear</button>
1445
  <span class="spacer"></span>
1446
  <button id="askRunBtn" class="btn primary" type="button">Send</button>
1447
+ <button id="askRefineBtn" class="btn" type="button" hidden
1448
+ title="Iterate: sends your prompt as a follow-up, keeping the conversation">Refine</button>
1449
  <button id="askStopBtn" class="btn" type="button" hidden>Stop</button>
1450
  <span id="askStatus" class="llm-status"></span>
1451
  </div>
 
5718
  let cfg = null;
5719
  let inflight = null;
5720
  let baseHint = "(editable)";
5721
+ /** Conversation so far (null before the first generation). Refine appends turns. */
5722
+ let history = null;
5723
 
5724
  function initTextAi() {
5725
  el = {
 
5737
  ref: $("textGenRef"),
5738
  ctxNote: $("textGenCtxNote"),
5739
  runBtn: $("textGenRunBtn"),
5740
+ refineBtn: $("textGenRefineBtn"),
5741
  stopBtn: $("textGenStopBtn"),
5742
  status: $("textGenStatus"),
5743
  resultHint: $("textGenResultHint"),
 
5755
  });
5756
 
5757
  el.runBtn.addEventListener("click", run);
5758
+ el.refineBtn.addEventListener("click", refine);
5759
  el.stopBtn.addEventListener("click", abortInflight);
5760
  el.acceptBtn.addEventListener("click", accept);
5761
  el.template.addEventListener("change", onTemplateChange);
 
5806
  el.template.value = sel;
5807
  applyTemplate(sel);
5808
  el.result.value = "";
5809
+ history = null; // fresh session — no conversation yet
5810
+ el.runBtn.textContent = "Generate";
5811
+ el.refineBtn.hidden = true; // appears after the first generation
5812
  updateCtxNote();
5813
  setBusy(false);
5814
  setStatus("Ready.", "");
 
5847
  : "Generating from your instructions.";
5848
  }
5849
 
5850
+ /** Build the first-turn messages from the fields + context (fresh generation). */
5851
+ function buildFreshMessages() {
 
 
 
 
 
 
5852
  // Reference context first, then the user's brief LAST, so the model treats
5853
  // the brief as the task to perform rather than as more context to echo.
5854
  const parts = [];
 
5865
  if (el.user.value.trim()) parts.push(`TASK (do this now):\n${el.user.value.trim()}`);
5866
  const userText = parts.join("\n\n") || el.user.value;
5867
 
5868
+ const messages = [];
5869
+ if (el.system.value.trim()) messages.push({ role: "system", content: el.system.value });
5870
+ messages.push({ role: "user", content: userText });
5871
+ return messages;
5872
+ }
5873
+
5874
+ /** Send a message list; sets the result and returns the assistant text (null on error). */
5875
+ async function send(messages) {
5876
+ abortInflight();
5877
+ const ctrl = new AbortController();
5878
+ inflight = ctrl;
5879
+ const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
5880
+ setBusy(true);
5881
+ setStatus("Generating…", "");
5882
  try {
5883
+ const text = await chat(messages, {
5884
+ signal: ctrl.signal,
5885
+ model: getTemplateModel(cfg.purpose, el.template.value),
5886
+ });
 
 
 
5887
  el.result.value = text.trim();
5888
  setStatus(
5889
  el.result.value ? "Done." : "The model returned an empty response.",
5890
  el.result.value ? "ok" : "warn"
5891
  );
5892
+ return el.result.value;
5893
  } catch (err) {
5894
  setStatus(err.message, "error");
5895
+ return null;
5896
  } finally {
5897
  clearTimeout(timer);
5898
  inflight = null;
 
5900
  }
5901
  }
5902
 
5903
+ /** Once a result exists, reveal Refine and relabel the primary button. */
5904
+ function afterGenerated() {
5905
+ el.runBtn.textContent = "Regenerate";
5906
+ el.refineBtn.hidden = false;
5907
+ }
5908
+
5909
+ /** Fresh generation: rebuild from the fields, discarding any prior conversation. */
5910
+ async function run() {
5911
+ const messages = buildFreshMessages();
5912
+ const text = await send(messages);
5913
+ if (text != null) {
5914
+ history = [...messages, { role: "assistant", content: el.result.value }];
5915
+ afterGenerated();
5916
+ }
5917
+ }
5918
+
5919
+ /** Refine: keep the conversation and send the current user prompt as a new turn. */
5920
+ async function refine() {
5921
+ if (!history) return run();
5922
+ // Use the (possibly hand-edited) result as the last assistant turn.
5923
+ const base = history.slice();
5924
+ if (base.length && base[base.length - 1].role === "assistant") {
5925
+ base[base.length - 1] = { role: "assistant", content: el.result.value };
5926
+ }
5927
+ const instruction = el.user.value.trim() || "Refine the previous result.";
5928
+ const messages = [...base, { role: "user", content: instruction }];
5929
+ const text = await send(messages);
5930
+ if (text != null) {
5931
+ history = [...messages, { role: "assistant", content: el.result.value }];
5932
+ afterGenerated();
5933
+ }
5934
+ }
5935
+
5936
  function accept() {
5937
  const text = el.result.value;
5938
  if (!text.trim()) return;
 
5950
 
5951
  function setBusy(busy) {
5952
  el.stopBtn.hidden = !busy;
5953
+ el.refineBtn.disabled = busy;
5954
+ syncButtons(busy); // handles runBtn + acceptBtn
5955
  }
5956
 
5957
  function syncButtons(busy = false) {
 
6225
  - If a previous scene is provided, write the NEXT scene that CONTINUES it: keep continuity of subjects, setting, wardrobe and tone, but SHOW NEW, DIFFERENT action driven by the TASK. Do NOT repeat, restate, summarize, or reuse the previous scene's wording, shots, or events. Otherwise write a fresh opening scene.
6226
 
6227
  DETAILED_DESCRIPTION (main body, playback order; be detailed and explicit — aim for a rich ~350-500 words, but do not pad; dialogue-dense scenes may be shorter to fit the spoken timeline)
6228
+ - FORMAT it in TWO parts, in this exact order:
6229
+ (1) ONE opening sentence with NO [Shot] tag, establishing the video type/style (from the brief; do NOT default to "cinematic" unless asked) and the setting. If any subject_definition is a place/room/environment, that is the SETTING — reference it by its <Subject N> label, don't describe it (e.g. "Amateur home-video set in <Subject 1>.").
6230
+ (2) On the NEXT line, start with the literal token "[Shot 1]" (no timestamp) and describe the shot: composition, subjects by their <Subject N> labels, action, camera, and synchronized sound.
6231
+ Never merge the two: the style+setting sentence comes first, THEN "[Shot 1] ...". Never start detailed_description directly with "[Shot 1]".
6232
  - Write ONE shot by default; add "[Shot N] At MM:SS.mmm, the camera cuts to ..." (strictly increasing time, new information) ONLY if the brief asks for multiple shots — otherwise stay in [Shot 1] and move the camera instead of cutting.
6233
  - Per shot, ground every detail in what is visible or audible: composition, subject position and action, environment and lighting (if the setting is a defined subject, name it by its <Subject N> label instead of re-describing it), state changes, camera movement, and synchronized sound/dialogue; note where each <Subject N>/<Picture N> appears or takes effect. Do NOT write a plot summary or a bare list of reference relationships. Cite a frame anchor naturally (e.g. "the shot begins from <Picture 1>").
6234
 
 
7434
  let imageShown = false;
7435
  /** @type {AbortController|null} */
7436
  let inflight = null;
7437
+ /** Conversation so far (null before the first generation). Refine appends turns. */
7438
+ let history = null;
7439
 
7440
  function initImageAi(d) {
7441
  deps = d;
 
7449
  system: $("aiGenSystem"),
7450
  user: $("aiGenUser"),
7451
  runBtn: $("aiGenRunBtn"),
7452
+ refineBtn: $("aiGenRefineBtn"),
7453
  stopBtn: $("aiGenStopBtn"),
7454
  status: $("aiGenStatus"),
7455
  result: $("aiGenResult"),
 
7466
  });
7467
 
7468
  el.runBtn.addEventListener("click", run); // "Generate", relabels to "Regenerate"
7469
+ el.refineBtn.addEventListener("click", refine);
7470
  el.stopBtn.addEventListener("click", abortInflight);
7471
  el.acceptBtn.addEventListener("click", accept);
7472
  el.toggleImgBtn.addEventListener("click", toggleImage);
 
7491
  el.template.value = sel;
7492
  applyTemplate(sel);
7493
  el.result.value = "";
7494
+ history = null; // fresh session
7495
+ el.runBtn.textContent = "Generate";
7496
+ el.refineBtn.hidden = true;
7497
  el.imgName.textContent = entry.name;
7498
  syncImageToggle();
7499
  setBusy(false);
 
7558
  el.root.hidden = true;
7559
  }
7560
 
7561
+ /** First-turn messages: system + the user prompt with the image attached. */
7562
+ function buildFreshMessages() {
7563
+ return [
7564
+ { role: "system", content: el.system.value },
7565
+ {
7566
+ role: "user",
7567
+ content: [
7568
+ { type: "text", text: el.user.value },
7569
+ { type: "image_url", image_url: { url: currentDataUrl } },
7570
+ ],
7571
+ },
7572
+ ];
7573
+ }
7574
 
7575
+ /** Send a message list; sets the result and returns the assistant text (null on error). */
7576
+ async function send(messages) {
7577
  abortInflight();
7578
  const ctrl = new AbortController();
7579
  inflight = ctrl;
7580
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
7581
  setBusy(true);
7582
  setStatus("Generating…", "");
 
7583
  try {
7584
+ const text = await chat(messages, {
7585
+ signal: ctrl.signal,
7586
+ model: getTemplateModel(PURPOSE, el.template.value),
7587
+ });
 
 
 
 
 
 
 
 
 
7588
  el.result.value = text.trim();
7589
  setStatus(
7590
  el.result.value ? "Done." : "The model returned an empty response.",
7591
  el.result.value ? "ok" : "warn"
7592
  );
7593
+ return el.result.value;
7594
  } catch (err) {
7595
  setStatus(err.message, "error");
7596
+ return null;
7597
  } finally {
7598
  clearTimeout(timer);
7599
  inflight = null;
 
7601
  }
7602
  }
7603
 
7604
+ function afterGenerated() {
7605
+ el.runBtn.textContent = "Regenerate";
7606
+ el.refineBtn.hidden = false;
7607
+ }
7608
+
7609
+ /** Fresh generation from the image + fields (discards any prior conversation). */
7610
+ async function run() {
7611
+ if (!currentDataUrl) {
7612
+ setStatus("Image not loaded yet.", "warn");
7613
+ return;
7614
+ }
7615
+ const messages = buildFreshMessages();
7616
+ const text = await send(messages);
7617
+ if (text != null) {
7618
+ history = [...messages, { role: "assistant", content: el.result.value }];
7619
+ afterGenerated();
7620
+ }
7621
+ }
7622
+
7623
+ /** Refine: keep the conversation (incl. the image turn) and send the user prompt anew. */
7624
+ async function refine() {
7625
+ if (!history) return run();
7626
+ const base = history.slice();
7627
+ if (base.length && base[base.length - 1].role === "assistant") {
7628
+ base[base.length - 1] = { role: "assistant", content: el.result.value };
7629
+ }
7630
+ const instruction = el.user.value.trim() || "Refine the previous result.";
7631
+ const messages = [...base, { role: "user", content: instruction }];
7632
+ const text = await send(messages);
7633
+ if (text != null) {
7634
+ history = [...messages, { role: "assistant", content: el.result.value }];
7635
+ afterGenerated();
7636
+ }
7637
+ }
7638
+
7639
  function accept() {
7640
  const text = el.result.value;
7641
  if (!text.trim()) return;
 
7654
  /** Enable/disable controls for the generating vs idle states. */
7655
  function setBusy(busy) {
7656
  el.stopBtn.hidden = !busy;
7657
+ el.refineBtn.disabled = busy;
 
7658
  syncButtons(busy);
7659
  }
7660
 
 
7700
 
7701
  let el;
7702
  let inflight = null;
7703
+ /** Conversation so far (null before the first send). Refine appends turns. */
7704
+ let history = null;
7705
 
7706
  function initAskAi() {
7707
  el = {
 
7713
  imageInput: $("askImageInput"),
7714
  clearImgBtn: $("askClearImgBtn"),
7715
  runBtn: $("askRunBtn"),
7716
+ refineBtn: $("askRefineBtn"),
7717
  stopBtn: $("askStopBtn"),
7718
  status: $("askStatus"),
7719
  result: $("askResult"),
 
7738
  el.clearImgBtn.hidden = true;
7739
  });
7740
  el.runBtn.addEventListener("click", run);
7741
+ el.refineBtn.addEventListener("click", refine);
7742
  el.stopBtn.addEventListener("click", abortInflight);
7743
  el.result.addEventListener("input", syncCopy);
7744
  el.copyBtn.addEventListener("click", copy);
 
7746
 
7747
  function openAskAi() {
7748
  populateTemplates();
7749
+ history = null; // fresh session
7750
+ el.refineBtn.hidden = true; // appears after the first send
7751
  setBusy(false);
7752
  setStatus("", "");
7753
  syncCopy();
 
7791
  el.user.value = getTemplateValue(pid, tid, "user");
7792
  }
7793
 
7794
+ /** First-turn messages: optional system + the user prompt (with image if attached). */
7795
+ async function buildFreshMessages() {
7796
+ const messages = [];
7797
+ if (el.system.value.trim()) messages.push({ role: "system", content: el.system.value });
7798
+ const file = el.imageInput.files[0];
7799
+ if (file) {
7800
+ const dataUrl = await fileToDataUrl(file);
7801
+ messages.push({
7802
+ role: "user",
7803
+ content: [
7804
+ { type: "text", text: el.user.value },
7805
+ { type: "image_url", image_url: { url: dataUrl } },
7806
+ ],
7807
+ });
7808
+ } else {
7809
+ messages.push({ role: "user", content: el.user.value });
7810
+ }
7811
+ return messages;
7812
+ }
7813
+
7814
+ /** Send a message list; sets the result and returns the assistant text (null on error). */
7815
+ async function send(messages) {
7816
  abortInflight();
7817
  const ctrl = new AbortController();
7818
  inflight = ctrl;
7819
  const timer = setTimeout(() => ctrl.abort(), GEN_TIMEOUT_MS);
7820
  setBusy(true);
7821
  setStatus("Sending…", "");
 
7822
  try {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7823
  const [pid, tid] = el.template.value.split("::");
7824
  const model = pid ? getTemplateModel(pid, tid) : "";
7825
  const text = await chat(messages, { signal: ctrl.signal, model });
7826
  el.result.value = text.trim();
7827
  setStatus(el.result.value ? "Done." : "Empty response.", el.result.value ? "ok" : "warn");
7828
+ return el.result.value;
7829
  } catch (err) {
7830
  setStatus(err.message, "error");
7831
+ return null;
7832
  } finally {
7833
  clearTimeout(timer);
7834
  inflight = null;
 
7837
  }
7838
  }
7839
 
7840
+ /** Fresh send: rebuild from the fields, discarding any prior conversation. */
7841
+ async function run() {
7842
+ let messages;
7843
+ try {
7844
+ messages = await buildFreshMessages();
7845
+ } catch (err) {
7846
+ setStatus("Could not read image: " + err.message, "error");
7847
+ return;
7848
+ }
7849
+ const text = await send(messages);
7850
+ if (text != null) {
7851
+ history = [...messages, { role: "assistant", content: el.result.value }];
7852
+ el.refineBtn.hidden = false;
7853
+ }
7854
+ }
7855
+
7856
+ /** Refine: keep the conversation and send the current user prompt as a new turn. */
7857
+ async function refine() {
7858
+ if (!history) return run();
7859
+ const base = history.slice();
7860
+ if (base.length && base[base.length - 1].role === "assistant") {
7861
+ base[base.length - 1] = { role: "assistant", content: el.result.value };
7862
+ }
7863
+ const instruction = el.user.value.trim() || "Refine the previous response.";
7864
+ const messages = [...base, { role: "user", content: instruction }];
7865
+ const text = await send(messages);
7866
+ if (text != null) {
7867
+ history = [...messages, { role: "assistant", content: el.result.value }];
7868
+ el.refineBtn.hidden = false;
7869
+ }
7870
+ }
7871
+
7872
  async function copy() {
7873
  try {
7874
  await navigator.clipboard.writeText(el.result.value);
 
7887
 
7888
  function setBusy(busy) {
7889
  el.runBtn.disabled = busy;
7890
+ el.refineBtn.disabled = busy;
7891
  el.stopBtn.hidden = !busy;
7892
  }
7893