@runappsai/agent 0.2.1 → 0.3.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/chat.js CHANGED
@@ -33,7 +33,7 @@ const STRINGS = {
33
33
  queued: "Queued messages", queuedN: "{n} message queued", queuedNs: "{n} messages queued", queueNext: "Next:", sendNow: "Send now (insert into the current task)", editQueued: "Edit", expand: "Show all", collapse: "Collapse", clear: "Clear queue", thinking: "Thinking…", thought: "Thought process",
34
34
  enter: "to send", newline: "new line", hintIdle: "↵ {enter} · ⇧↵ {newline}", hintNext: "Tab to use the suggestion · ↵ {enter}", hintBusy: "↵ steer · {mod}↵ follow-up · ⇧↵ {newline}", empty: "Start a conversation", history: "Chats", newChat: "New chat", clearChat: "Clear", confirmClear: "Clear this conversation? Its messages are removed; the files stay.", noSessions: "No other conversations yet", notSaved: "not saved yet",
35
35
  compacted: "Context compacted", trimmed: "Earlier conversation trimmed to fit the context", allow: "Allow", decline: "Decline", cancel: "Cancel", submit: "Submit", ok: "OK",
36
- other: "Other…", typeAnswer: "Type your answer", args: "Arguments", result: "Result", error: "Error", declined: "declined", blocked: "blocked", skipped: "skipped",
36
+ other: "Other…", typeAnswer: "Type your answer", prev: "Back", next: "Next", skip: "Skip", swOn: "Yes", swOff: "No", args: "Arguments", result: "Result", error: "Error", declined: "declined", blocked: "blocked", skipped: "skipped",
37
37
  open: "Open in editor", stopped: "Stopped.", maxTurns: "Paused at the turn limit ({n})", retry: "Retry", messages: "messages",
38
38
  ovAttachments: "The attachments on this message are too large for the model.", ovMessage: "This message is too long for the model.", ovHistory: "This conversation has grown too long for the model.", ovTools: "The connected tools take up too much of the model's context.", ovModel: "This request does not fit the current model.",
39
39
  ovDropFiles: "Resend without attachments", ovEdit: "Edit the message", ovCompact: "Compact and continue", ovNew: "Continue in a new chat", ovToolSettings: "Tool settings", ovUseModel: "Retry with {name}", ovNoText: "The message had only attachments: type what you want to ask.", ovNoSummary: "Could not summarise the conversation; the new chat starts without it.", skill: "Skill", template: "Template",
@@ -74,7 +74,7 @@ const STRINGS = {
74
74
  queued: "排队中的消息", queuedN: "{n} 条消息排队中", queuedNs: "{n} 条消息排队中", queueNext: "下一条:", sendNow: "立即发送(插入当前任务)", editQueued: "编辑", expand: "展开", collapse: "收起", clear: "清空队列", thinking: "思考中…", thought: "思考过程",
75
75
  enter: "发送", newline: "换行", hintIdle: "↵ {enter} · ⇧↵ {newline}", hintNext: "Tab 使用建议 · ↵ {enter}", hintBusy: "↵ 插入 · {mod}↵ 排队 · ⇧↵ {newline}", empty: "开始对话", history: "会话", newChat: "新对话", clearChat: "清除", confirmClear: "清除这次对话?消息会被删除,文件保留。", noSessions: "还没有其它对话", notSaved: "尚未保存",
76
76
  compacted: "上下文已压缩", trimmed: "较早的对话已裁剪以适应上下文", allow: "允许", decline: "拒绝", cancel: "取消", submit: "提交", ok: "确定",
77
- other: "其他…", typeAnswer: "请输入", args: "参数", result: "结果", error: "错误", declined: "已拒绝", blocked: "已拦截", skipped: "已跳过",
77
+ other: "其他…", typeAnswer: "请输入", prev: "上一题", next: "下一题", skip: "跳过", swOn: "是", swOff: "否", args: "参数", result: "结果", error: "错误", declined: "已拒绝", blocked: "已拦截", skipped: "已跳过",
78
78
  open: "在编辑器中打开", stopped: "已停止。", maxTurns: "已达最大轮数 {n},暂停", retry: "重试", messages: "条消息",
79
79
  ovAttachments: "这条消息的附件太大,超出了模型能处理的长度。", ovMessage: "这条消息太长,超出了模型能处理的长度。", ovHistory: "这段对话太长,模型放不下了。", ovTools: "已连接的工具太多,占满了模型的上下文。", ovModel: "当前模型放不下这次请求。",
80
80
  ovDropFiles: "去掉附件后重发", ovEdit: "修改这条消息", ovCompact: "压缩后继续", ovNew: "开新对话继续", ovToolSettings: "打开工具设置", ovUseModel: "换用 {name} 重试", ovNoText: "这条消息只有附件,请输入想问的内容。", ovNoSummary: "这段对话没能生成摘要,新对话不会带上之前的内容。", skill: "Skill", template: "模板",
@@ -456,6 +456,29 @@ const CSS = tokenCSS() + `
456
456
  .ra-q .ra-form{display:flex;flex-direction:column;gap:8px;margin:2px 0 4px}
457
457
  .ra-q .ra-ff{display:flex;flex-direction:column;gap:4px;font-size:13px;color:var(--ra-fg-muted)}
458
458
  .ra-q textarea.ra-field{min-height:96px;max-height:200px;resize:vertical}
459
+ .ra-q .ra-qtabs{display:flex;gap:4px;overflow-x:auto;scrollbar-width:none;margin-top:-2px}
460
+ .ra-q .ra-qtabs::-webkit-scrollbar{display:none}
461
+ .ra-q .ra-qtab{flex:none;display:inline-flex;align-items:center;gap:4px;max-width:11em;height:24px;padding:0 9px;border-radius:12px;border:1px solid var(--ra-border);background:var(--ra-bg);font-size:12px;color:var(--ra-fg-muted)}
462
+ .ra-q .ra-qtab>span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
463
+ .ra-q .ra-qtab:hover{background:var(--ra-muted)}
464
+ .ra-q .ra-qtab.done{color:var(--ra-fg)}
465
+ .ra-q .ra-qtab.done::before{content:"\\2713";color:var(--ra-accent);font-weight:600}
466
+ .ra-q .ra-qtab.cur{border-color:var(--ra-accent);color:var(--ra-fg)}
467
+ .ra-q .ra-qpane{display:flex;flex-direction:column;gap:8px}
468
+ .ra-q .ra-qq{font-size:14px;white-space:pre-wrap}
469
+ .ra-q .ra-opt.on{border-color:var(--ra-accent)}
470
+ .ra-q .ra-opts.bad .ra-opt{border-color:var(--ra-red)}
471
+ .ra-q .ra-switch{display:inline-flex;align-items:center;gap:8px;font-size:14px;color:var(--ra-fg);cursor:pointer;align-self:flex-start}
472
+ .ra-chat input.ra-sw{appearance:none;-webkit-appearance:none;box-sizing:border-box;position:relative;flex:none;width:34px;height:20px;margin:0;padding:0;border:0;border-radius:10px;background:color-mix(in srgb,var(--ra-fg-muted) 45%,transparent);cursor:pointer;transition:background-color .15s}
473
+ .ra-chat input.ra-sw::before{content:"";position:absolute;top:2px;left:2px;width:16px;height:16px;border-radius:50%;background:#fff;box-shadow:0 1px 2px rgba(0,0,0,.25);transition:transform .15s}
474
+ .ra-chat input.ra-sw:checked{background:var(--ra-accent)}
475
+ .ra-chat input.ra-sw:checked::before{transform:translateX(14px)}
476
+ .ra-chat input.ra-sw:focus{outline:0;box-shadow:none}
477
+ .ra-chat input.ra-sw:focus-visible{outline:2px solid var(--ra-accent);outline-offset:2px}
478
+ .ra-q .ra-ff .ra-switch{margin-top:2px}
479
+ .ra-q input.ra-field:is([type=date],[type=datetime-local]){min-height:32px}
480
+ .ra-chat[data-scheme=dark] .ra-q input.ra-field:is([type=date],[type=datetime-local]){color-scheme:dark}
481
+ .ra-chat[data-scheme=light] .ra-q input.ra-field:is([type=date],[type=datetime-local]){color-scheme:light}
459
482
  .ra-btns{display:flex;justify-content:flex-end;gap:8px}
460
483
  .ra-cbody{margin:2px 0 10px;max-height:min(60vh,560px);overflow:auto;min-width:0}
461
484
  .ra-status{font-size:14px;color:var(--ra-fg-muted);padding:4px 0}
@@ -875,11 +898,255 @@ function inlineUI(askArea, toasts, t, onChange) {
875
898
  queueMicrotask(() => focus?.focus());
876
899
  });
877
900
  const head = (card, title, message) => {
878
- card.append(h("div", "ra-qt", title));
901
+ if (title)
902
+ card.append(h("div", "ra-qt", title));
879
903
  if (message)
880
904
  card.append(h("div", "ra-qm", message));
881
905
  };
882
906
  const btn = (label, cls, onClick) => { const b = h("button", "ra-btn xs " + cls, label); b.type = "button"; b.addEventListener("click", onClick); return b; };
907
+ /**
908
+ * The input of a field that is not a choice: a text, number, date or date-and-time box, a text area
909
+ * (`textarea`, Enter makes a new line) or a switch (`boolean`). `raw` is what it holds now (a switch's
910
+ * state, a box's text); `boxValue` turns that into the answer.
911
+ */
912
+ const boxFor = (f, initial, onChange) => {
913
+ if (f.type === "boolean") {
914
+ const label = h("label", "ra-switch");
915
+ // ra-x: not the chat's checkbox look; this one is a switch.
916
+ const input = h("input", "ra-x ra-sw");
917
+ input.type = "checkbox";
918
+ input.setAttribute("role", "switch");
919
+ input.name = f.name;
920
+ input.checked = initial === true;
921
+ const state = h("span", "", input.checked ? t.swOn : t.swOff);
922
+ input.addEventListener("change", () => { state.textContent = input.checked ? t.swOn : t.swOff; onChange(); });
923
+ label.append(input, state);
924
+ return { el: label, focus: input, raw: () => input.checked };
925
+ }
926
+ const area = f.type === "textarea";
927
+ const input = area ? h("textarea", "ra-field") : h("input", "ra-field");
928
+ if (input instanceof HTMLInputElement) {
929
+ input.type = f.type === "number" ? "number" : f.type === "date" ? "date" : f.type === "datetime" ? "datetime-local" : "text";
930
+ if (f.type === "number")
931
+ input.step = "any";
932
+ }
933
+ input.name = f.name;
934
+ if (f.placeholder)
935
+ input.placeholder = f.placeholder;
936
+ input.value = typeof initial === "string" ? initial : "";
937
+ input.addEventListener("input", () => { input.classList.remove("bad"); onChange(); });
938
+ return { el: input, focus: input, raw: () => input.value };
939
+ };
940
+ /** A box's answer: undefined when empty, NaN for a number that does not parse, a switch's true or false. */
941
+ const boxValue = (f, raw) => {
942
+ if (typeof raw === "boolean")
943
+ return raw;
944
+ const text = f.type === "textarea" ? raw.replace(/\s+$/, "") : raw.trim();
945
+ if (!text)
946
+ return undefined;
947
+ return f.type === "number" ? Number(text) : text;
948
+ };
949
+ /** A field's starting value for its box: a switch's on or off, a box's text. */
950
+ const startOf = (f) => (f.type === "boolean" ? f.initial === true || f.initial === "true" : f.initial !== undefined && typeof f.initial !== "boolean" ? String(f.initial) : "");
951
+ /**
952
+ * A form with a choice in it, asked one question at a time: tabs on top (done ones ticked, any can be
953
+ * gone back to), a single choice answered by a click moves on, and the last question's button sends
954
+ * every answer together. Keys: 1–9 pick, Enter goes on, ←/→ change question, Escape skips the lot.
955
+ */
956
+ const stepForm = (card, fields, okLabel, done) => {
957
+ const OTHER = "__other__";
958
+ const n = fields.length;
959
+ const isChoice = (f) => !!f.options?.length;
960
+ // A choice must be answered unless it says otherwise; a box only when it says so (as in the plain form).
961
+ const required = (f) => (isChoice(f) ? f.required !== false : !!f.required);
962
+ const state = fields.map((f) => ({ picked: [], other: "", raw: startOf(f) }));
963
+ /** A question's answer now; undefined when it has none. */
964
+ const valueOf = (i) => {
965
+ const f = fields[i], s = state[i];
966
+ if (isChoice(f)) {
967
+ const vals = s.picked.map((v) => (v === OTHER ? s.other.trim() : v)).filter(Boolean);
968
+ return vals.length ? (f.multi ? vals : vals[0]) : undefined;
969
+ }
970
+ return boxValue(f, s.raw);
971
+ };
972
+ const valid = (i) => {
973
+ const v = valueOf(i);
974
+ if (typeof v === "number" && !Number.isFinite(v))
975
+ return false;
976
+ // "Other" picked with nothing typed is not an answer.
977
+ if (state[i].picked.includes(OTHER) && !state[i].other.trim())
978
+ return false;
979
+ return v !== undefined || !required(fields[i]);
980
+ };
981
+ let cur = 0;
982
+ const tabs = h("div", "ra-qtabs");
983
+ const tabEls = fields.map((f, i) => {
984
+ const b = h("button", "ra-qtab");
985
+ b.type = "button";
986
+ const label = f.label ?? f.name;
987
+ b.append(h("span", "", label));
988
+ b.title = label;
989
+ b.addEventListener("click", () => go(i));
990
+ tabs.append(b);
991
+ return b;
992
+ });
993
+ const pane = h("div", "ra-qpane");
994
+ const row = h("div", "ra-btns");
995
+ const prevBtn = btn(t.prev, "ghost", () => go(cur - 1));
996
+ const nextBtn = btn(t.next, "solid", () => next());
997
+ row.append(btn(t.skip, "ghost", () => done(null)), prevBtn, nextBtn);
998
+ if (n > 1)
999
+ card.append(tabs);
1000
+ card.append(pane, row);
1001
+ const sync = () => {
1002
+ tabEls.forEach((b, i) => { b.classList.toggle("cur", i === cur); b.classList.toggle("done", valueOf(i) !== undefined && valid(i)); });
1003
+ prevBtn.style.display = cur > 0 ? "" : "none";
1004
+ nextBtn.textContent = cur === n - 1 ? okLabel ?? t.submit : t.next;
1005
+ pane.querySelectorAll(".bad").forEach((e) => e.classList.remove("bad"));
1006
+ };
1007
+ const flag = () => {
1008
+ const s = state[cur];
1009
+ const target = s.picked.includes(OTHER) ? pane.querySelector("input.ra-field") : pane.querySelector(".ra-opts,.ra-field");
1010
+ target?.classList.add("bad");
1011
+ if (target instanceof HTMLInputElement)
1012
+ target.focus();
1013
+ };
1014
+ const go = (i) => {
1015
+ if (i < 0 || i >= n || i === cur)
1016
+ return;
1017
+ cur = i;
1018
+ render();
1019
+ tabEls[i]?.scrollIntoView?.({ block: "nearest", inline: "nearest" });
1020
+ };
1021
+ const next = () => {
1022
+ if (!valid(cur)) {
1023
+ flag();
1024
+ return;
1025
+ }
1026
+ if (cur < n - 1) {
1027
+ go(cur + 1);
1028
+ return;
1029
+ }
1030
+ const first = fields.findIndex((_, k) => !valid(k));
1031
+ if (first >= 0) {
1032
+ go(first);
1033
+ flag();
1034
+ return;
1035
+ }
1036
+ const out = {};
1037
+ fields.forEach((f, k) => { const v = valueOf(k); if (v !== undefined)
1038
+ out[f.name] = v; });
1039
+ done(out);
1040
+ };
1041
+ const optionText = (o) => { const text = h("div"); text.append(h("span", "", o.label ?? o.value)); if (o.description)
1042
+ text.append(h("small", "", o.description)); return text; };
1043
+ const render = () => {
1044
+ pane.replaceChildren();
1045
+ const f = fields[cur], s = state[cur];
1046
+ pane.append(h("div", "ra-qq", (f.label ?? f.name) + (required(f) && !isChoice(f) && f.type !== "boolean" ? " *" : "")));
1047
+ let focus = null;
1048
+ if (isChoice(f) && !f.multi && !f.allowOther) {
1049
+ // A single choice: a click answers and moves on.
1050
+ const list = h("div", "ra-opts");
1051
+ for (const o of f.options) {
1052
+ const b = h("button", "ra-opt");
1053
+ b.type = "button";
1054
+ if (s.picked[0] === o.value) {
1055
+ b.classList.add("on");
1056
+ focus = b;
1057
+ }
1058
+ b.append(optionText(o));
1059
+ b.addEventListener("click", () => { s.picked = [o.value]; sync(); next(); });
1060
+ list.append(b);
1061
+ }
1062
+ pane.append(list);
1063
+ focus ??= list.querySelector("button");
1064
+ }
1065
+ else if (isChoice(f)) {
1066
+ const list = h("div", "ra-opts");
1067
+ const name = "ra_" + Math.random().toString(36).slice(2, 8);
1068
+ const inputs = [];
1069
+ const read = () => { s.picked = inputs.filter((x) => x.checked).map((x) => x.value); sync(); };
1070
+ const all = f.allowOther ? [...f.options, { value: OTHER, label: t.other }] : f.options;
1071
+ for (const o of all) {
1072
+ const label = h("label", "ra-opt");
1073
+ const input = h("input");
1074
+ input.type = f.multi ? "checkbox" : "radio";
1075
+ input.name = name;
1076
+ input.value = o.value;
1077
+ input.checked = s.picked.includes(o.value);
1078
+ input.addEventListener("change", read);
1079
+ inputs.push(input);
1080
+ label.append(input, optionText(o));
1081
+ list.append(label);
1082
+ }
1083
+ pane.append(list);
1084
+ if (f.allowOther) {
1085
+ const other = h("input", "ra-field");
1086
+ other.type = "text";
1087
+ other.placeholder = t.typeAnswer;
1088
+ other.value = s.other;
1089
+ other.addEventListener("focus", () => { const o = inputs.find((x) => x.value === OTHER); if (o && !o.checked) {
1090
+ o.checked = true;
1091
+ read();
1092
+ } });
1093
+ other.addEventListener("input", () => { s.other = other.value; sync(); });
1094
+ pane.append(other);
1095
+ }
1096
+ focus = inputs.find((x) => x.checked) ?? inputs[0] ?? null;
1097
+ }
1098
+ else {
1099
+ const box = boxFor(f, s.raw, () => { s.raw = box.raw(); sync(); });
1100
+ pane.append(box.el);
1101
+ focus = box.focus;
1102
+ }
1103
+ sync();
1104
+ if (card.isConnected)
1105
+ queueMicrotask(() => focus?.focus());
1106
+ };
1107
+ card.addEventListener("keydown", (ev) => {
1108
+ const e = ev;
1109
+ if (e.isComposing)
1110
+ return;
1111
+ const target = e.target;
1112
+ // In a text area Enter is a new line and Cmd/Ctrl+Enter goes on; elsewhere Enter goes on (on a button it is that button's click).
1113
+ if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
1114
+ e.preventDefault();
1115
+ next();
1116
+ return;
1117
+ }
1118
+ if (e.metaKey || e.ctrlKey || e.altKey)
1119
+ return;
1120
+ if (target instanceof HTMLTextAreaElement)
1121
+ return;
1122
+ if (e.key === "Enter") {
1123
+ if (!(target instanceof HTMLButtonElement)) {
1124
+ e.preventDefault();
1125
+ next();
1126
+ }
1127
+ return;
1128
+ }
1129
+ // In a text box every key is the box's; on a radio or checkbox the arrows are its own (they move the pick).
1130
+ const onInput = target instanceof HTMLInputElement;
1131
+ if (onInput && target.type !== "radio" && target.type !== "checkbox")
1132
+ return;
1133
+ if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
1134
+ if (!onInput) {
1135
+ e.preventDefault();
1136
+ go(cur + (e.key === "ArrowLeft" ? -1 : 1));
1137
+ }
1138
+ return;
1139
+ }
1140
+ if (/^[1-9]$/.test(e.key)) {
1141
+ const opt = pane.querySelectorAll(".ra-opt")[Number(e.key) - 1];
1142
+ if (opt) {
1143
+ e.preventDefault();
1144
+ opt.click();
1145
+ }
1146
+ }
1147
+ });
1148
+ render();
1149
+ };
883
1150
  return {
884
1151
  confirm: (title, message, opts = {}) => open((card, done) => {
885
1152
  head(card, title, message);
@@ -972,42 +1239,46 @@ function inlineUI(askArea, toasts, t, onChange) {
972
1239
  }, null, opts.signal),
973
1240
  form: (title, fields, opts = {}) => open((card, done) => {
974
1241
  head(card, title, opts.message);
1242
+ // With a choice in it: one question at a time.
1243
+ if (fields.some((f) => f.options?.length)) {
1244
+ stepForm(card, fields, opts.ok, done);
1245
+ return;
1246
+ }
975
1247
  const box = h("div", "ra-form");
976
1248
  const inputs = fields.map((f) => {
977
- const label = h("label", "ra-ff");
978
- label.append(h("span", "", (f.label ?? f.name) + (f.required ? " *" : "")));
979
- const input = h("input", "ra-field");
980
- input.type = f.type === "number" ? "number" : "text";
981
- input.name = f.name;
982
- if (f.type === "number")
983
- input.step = "any";
984
- if (f.placeholder)
985
- input.placeholder = f.placeholder;
986
- if (f.initial !== undefined)
987
- input.value = String(f.initial);
988
- label.append(input);
1249
+ // A switch's label is beside it, a box's above it.
1250
+ const label = h(f.type === "boolean" ? "div" : "label", "ra-ff");
1251
+ label.append(h("span", "", (f.label ?? f.name) + (f.required && f.type !== "boolean" ? " *" : "")));
1252
+ const field = boxFor(f, startOf(f), () => undefined);
1253
+ label.append(field.el);
989
1254
  box.append(label);
990
- return { f, input };
1255
+ return { f, field };
991
1256
  });
992
1257
  const submit = () => {
993
1258
  const out = {};
994
- for (const { f, input } of inputs) {
995
- const raw = input.value.trim();
996
- const bad = (!raw && f.required) || (!!raw && f.type === "number" && !Number.isFinite(Number(raw)));
997
- input.classList.toggle("bad", bad);
1259
+ for (const { f, field } of inputs) {
1260
+ const v = boxValue(f, field.raw());
1261
+ const bad = (v === undefined && !!f.required) || (typeof v === "number" && !Number.isFinite(v));
1262
+ field.focus.classList.toggle("bad", bad);
998
1263
  if (bad) {
999
- input.focus();
1264
+ field.focus.focus();
1000
1265
  return;
1001
1266
  }
1002
- if (raw)
1003
- out[f.name] = f.type === "number" ? Number(raw) : raw;
1267
+ if (v !== undefined)
1268
+ out[f.name] = v;
1004
1269
  }
1005
1270
  done(out);
1006
1271
  };
1007
- box.addEventListener("keydown", (ev) => { const e = ev; if (e.key === "Enter" && !e.isComposing) {
1272
+ box.addEventListener("keydown", (ev) => {
1273
+ const e = ev;
1274
+ if (e.key !== "Enter" || e.isComposing)
1275
+ return;
1276
+ // In a text area Enter is a new line; Cmd/Ctrl+Enter sends.
1277
+ if (e.target instanceof HTMLTextAreaElement && !e.metaKey && !e.ctrlKey)
1278
+ return;
1008
1279
  e.preventDefault();
1009
1280
  submit();
1010
- } });
1281
+ });
1011
1282
  card.append(box);
1012
1283
  const row = h("div", "ra-btns");
1013
1284
  row.append(btn(t.cancel, "ghost", () => done(null)), btn(opts.ok ?? t.submit, "solid", submit));
@@ -1475,6 +1746,7 @@ export function renderChat(target, opts) {
1475
1746
  confirm: async (title, message, o) => ((await whenCurrent(id, o?.signal)) ? ui.confirm(title, message, o?.permission && permShown && !o.always ? { ...o, always: { label: t.permAlways, onChoose: () => setPerm("auto") } } : o) : false),
1476
1747
  select: async (title, options, o) => ((await whenCurrent(id, o?.signal)) ? ui.select(title, options, o) : null),
1477
1748
  input: async (title, o) => ((await whenCurrent(id, o?.signal)) ? ui.input(title, o) : null),
1749
+ form: async (title, fields, o) => ((await whenCurrent(id, o?.signal)) && ui.form ? ui.form(title, fields, o) : null),
1478
1750
  notify: (m, l) => ui.notify(m, l),
1479
1751
  });
1480
1752
  /** Whether rewinding may restore files: only a directory of the session's own, kept in IndexedDB (not a folder on disk, not shared). */
package/dist/dialogs.d.ts CHANGED
@@ -3,5 +3,5 @@ import type { AgentUI, Tool } from "./types.js";
3
3
  export declare function domUI(): AgentUI;
4
4
  /** An `AgentUI` with no user: every question is declined. */
5
5
  export declare function nullUI(): AgentUI;
6
- /** The `ask_user` tool: up to three questions, each with options or free text. */
6
+ /** The `ask_user` tool: up to four questions, each with options or free text, asked together in one form when the UI has forms. */
7
7
  export declare function askUserTool(): Tool;
package/dist/dialogs.js CHANGED
@@ -145,41 +145,108 @@ export function domUI() {
145
145
  async form(title, fields, opts = {}) {
146
146
  const zh = isZh();
147
147
  const v = await openDialog((d, done) => {
148
- d.append(el("div", "font-weight:600;font-size:15px;margin-bottom:6px", title));
148
+ if (title)
149
+ d.append(el("div", "font-weight:600;font-size:15px;margin-bottom:6px", title));
149
150
  if (opts.message)
150
151
  d.append(el("div", "white-space:pre-wrap;opacity:.8;font-size:13px;margin-bottom:10px", opts.message));
151
- const inputs = fields.map((f) => {
152
- const label = el("label", "display:block;font-size:13px;margin:8px 0");
153
- label.append(el("div", "opacity:.8;margin-bottom:4px", (f.label ?? f.name) + (f.required ? " *" : "")));
154
- const input = el("input", "font:inherit;padding:8px 10px;border:1px solid rgba(127,127,127,.35);border-radius:8px;background:transparent;color:inherit;width:100%;box-sizing:border-box");
155
- input.type = f.type === "number" ? "number" : "text";
156
- if (f.type === "number")
157
- input.step = "any";
158
- if (f.placeholder)
159
- input.placeholder = f.placeholder;
160
- if (f.initial !== undefined)
161
- input.value = String(f.initial);
162
- label.append(input);
163
- d.append(label);
164
- return { f, input };
152
+ // Every field one under the other (a choice as its list of options); the dialog scrolls when they do not fit.
153
+ d.style.maxHeight = "calc(100vh - 48px)";
154
+ d.style.overflowY = "auto";
155
+ const fieldStyle = "font:inherit;padding:8px 10px;border:1px solid rgba(127,127,127,.35);border-radius:8px;background:transparent;color:inherit;width:100%;box-sizing:border-box";
156
+ const BAD = Symbol("bad");
157
+ const readers = fields.map((f) => {
158
+ const choice = !!f.options?.length;
159
+ const required = choice ? f.required !== false : !!f.required && f.type !== "boolean";
160
+ const wrap = el(choice ? "div" : "label", "display:block;font-size:13px;margin:10px 0");
161
+ wrap.append(el("div", "opacity:.8;margin-bottom:4px", (f.label ?? f.name) + (required ? " *" : "")));
162
+ d.append(wrap);
163
+ if (f.type === "boolean" && !choice) {
164
+ // A switch (a checkbox here): always answered, true or false.
165
+ const row = el("label", "display:flex;gap:8px;align-items:center;cursor:pointer");
166
+ const input = el("input");
167
+ input.type = "checkbox";
168
+ input.checked = f.initial === true || f.initial === "true";
169
+ row.append(input, el("span", "", zh ? "是" : "Yes"));
170
+ wrap.append(row);
171
+ return { focus: input, read: () => input.checked };
172
+ }
173
+ if (!choice) {
174
+ const area = f.type === "textarea";
175
+ const input = area ? el("textarea", fieldStyle + ";min-height:80px;resize:vertical") : el("input", fieldStyle);
176
+ if (input instanceof HTMLInputElement) {
177
+ input.type = f.type === "number" ? "number" : f.type === "date" ? "date" : f.type === "datetime" ? "datetime-local" : "text";
178
+ if (f.type === "number")
179
+ input.step = "any";
180
+ }
181
+ if (f.placeholder)
182
+ input.placeholder = f.placeholder;
183
+ if (f.initial !== undefined && typeof f.initial !== "boolean")
184
+ input.value = String(f.initial);
185
+ wrap.append(input);
186
+ /** The value, undefined when empty, or BAD when it does not do. */
187
+ return { focus: input, read: () => {
188
+ const raw = area ? input.value.replace(/\s+$/, "") : input.value.trim();
189
+ if (!raw)
190
+ return required ? BAD : undefined;
191
+ if (f.type === "number")
192
+ return Number.isFinite(Number(raw)) ? Number(raw) : BAD;
193
+ return raw;
194
+ } };
195
+ }
196
+ const name = "ra_frm_" + Math.random().toString(36).slice(2, 8);
197
+ const inputs = [];
198
+ const add = (value, label, description) => {
199
+ const row = el("label", "display:flex;gap:8px;align-items:flex-start;padding:6px 10px;border:1px solid rgba(127,127,127,.3);border-radius:8px;cursor:pointer;margin-top:4px");
200
+ const input = el("input", "margin-top:3px");
201
+ input.type = f.multi ? "checkbox" : "radio";
202
+ input.name = name;
203
+ input.value = value;
204
+ inputs.push(input);
205
+ const text = el("div");
206
+ text.append(el("div", "", label));
207
+ if (description)
208
+ text.append(el("div", "opacity:.7;font-size:12px", description));
209
+ row.append(input, text);
210
+ wrap.append(row);
211
+ };
212
+ for (const o of f.options)
213
+ add(o.value, o.label ?? o.value, o.description);
214
+ let other = null;
215
+ if (f.allowOther) {
216
+ add("__other__", zh ? "其他…" : "Other…");
217
+ other = el("input", fieldStyle + ";margin-top:4px");
218
+ other.placeholder = zh ? "请输入" : "Type your answer";
219
+ other.addEventListener("focus", () => { const o = inputs.find((i) => i.value === "__other__"); if (o)
220
+ o.checked = true; });
221
+ wrap.append(other);
222
+ }
223
+ return { focus: inputs[0], read: () => {
224
+ const checked = inputs.filter((i) => i.checked);
225
+ if (checked.some((i) => i.value === "__other__") && !other?.value.trim())
226
+ return BAD;
227
+ const vals = checked.map((i) => (i.value === "__other__" ? other.value.trim() : i.value));
228
+ if (!vals.length)
229
+ return required ? BAD : undefined;
230
+ return f.multi ? vals : vals[0];
231
+ } };
165
232
  });
166
233
  const submit = () => {
167
234
  const out = {};
168
- for (const { f, input } of inputs) {
169
- const raw = input.value.trim();
170
- if ((!raw && f.required) || (raw && f.type === "number" && !Number.isFinite(Number(raw)))) {
171
- input.focus();
235
+ for (let i = 0; i < fields.length; i++) {
236
+ const v = readers[i].read();
237
+ if (v === BAD) {
238
+ readers[i].focus.focus();
172
239
  return;
173
240
  }
174
- if (raw)
175
- out[f.name] = f.type === "number" ? Number(raw) : raw;
241
+ if (v !== undefined)
242
+ out[fields[i].name] = v;
176
243
  }
177
244
  done(out);
178
245
  };
179
246
  const row = el("div", "display:flex;justify-content:flex-end;gap:8px;margin-top:12px");
180
247
  row.append(button(zh ? "取消" : "Cancel", false, false, () => done(null)), button(opts.ok ?? (zh ? "确定" : "OK"), true, false, submit));
181
248
  d.append(row);
182
- queueMicrotask(() => inputs[0]?.input.focus());
249
+ queueMicrotask(() => readers[0]?.focus.focus());
183
250
  }, opts.signal, null);
184
251
  return v && typeof v === "object" && !Array.isArray(v) ? v : null;
185
252
  },
@@ -207,20 +274,20 @@ export function nullUI() {
207
274
  },
208
275
  };
209
276
  }
210
- /** The `ask_user` tool: up to three questions, each with options or free text. */
277
+ /** The `ask_user` tool: up to four questions, each with options or free text, asked together in one form when the UI has forms. */
211
278
  export function askUserTool() {
212
279
  return defineTool({
213
280
  name: "ask_user",
214
281
  // The model asks, between its steps; code in the middle of a run does not.
215
282
  callableFromCode: false,
216
- description: "Ask the user one to three questions when the request is genuinely ambiguous and the answer changes what you would do. Give 2–5 short options when there are natural choices (allow_other adds a free-text choice); leave options out for a free-text answer. Do not ask what you can reasonably decide yourself.",
283
+ description: "Ask the user one to four questions when the request is genuinely ambiguous and the answer changes what you would do. Put everything you need to know in one call (they are asked together), not one question per call. Give 2–5 short options when there are natural choices (allow_other adds a free-text choice); leave options out for a free-text answer. Do not ask what you can reasonably decide yourself.",
217
284
  parameters: {
218
285
  type: "object",
219
286
  properties: {
220
287
  questions: {
221
288
  type: "array",
222
289
  minItems: 1,
223
- maxItems: 3,
290
+ maxItems: 4,
224
291
  items: {
225
292
  type: "object",
226
293
  properties: {
@@ -249,20 +316,32 @@ export function askUserTool() {
249
316
  },
250
317
  async execute({ questions }, ctx) {
251
318
  const answers = [];
252
- for (const q of questions) {
253
- ctx.signal.throwIfAborted();
254
- let answer;
255
- if (q.options && q.options.length) {
256
- const picked = await ctx.ui.select(q.question, q.options.map((o) => ({ value: o.label, label: o.label, description: o.description })), { multi: !!q.multi, allowOther: q.allow_other !== false, signal: ctx.signal });
257
- answer = picked === null ? null : q.multi ? picked : (picked[0] ?? null);
258
- }
259
- else {
260
- answer = await ctx.ui.input(q.question, { multiline: true, signal: ctx.signal });
261
- }
262
- answers.push({ question: q.question, answer });
263
- if (answer === null)
264
- break;
319
+ if (ctx.ui.form && questions.length > 1) {
320
+ // Several: one form, every question on one card, the answers sent together (one question is asked as before).
321
+ const fields = questions.map((q, i) => q.options && q.options.length
322
+ ? { name: `q${i + 1}`, label: q.question, options: q.options.map((o) => ({ value: o.label, label: o.label, description: o.description })), multi: !!q.multi, allowOther: q.allow_other !== false }
323
+ : { name: `q${i + 1}`, label: q.question, required: true });
324
+ const values = await ctx.ui.form("", fields, { signal: ctx.signal });
325
+ questions.forEach((q, i) => {
326
+ const v = values?.[`q${i + 1}`];
327
+ answers.push({ question: q.question, answer: v === undefined ? null : Array.isArray(v) ? v : String(v) });
328
+ });
265
329
  }
330
+ else
331
+ for (const q of questions) {
332
+ ctx.signal.throwIfAborted();
333
+ let answer;
334
+ if (q.options && q.options.length) {
335
+ const picked = await ctx.ui.select(q.question, q.options.map((o) => ({ value: o.label, label: o.label, description: o.description })), { multi: !!q.multi, allowOther: q.allow_other !== false, signal: ctx.signal });
336
+ answer = picked === null ? null : q.multi ? picked : (picked[0] ?? null);
337
+ }
338
+ else {
339
+ answer = await ctx.ui.input(q.question, { multiline: true, signal: ctx.signal });
340
+ }
341
+ answers.push({ question: q.question, answer });
342
+ if (answer === null)
343
+ break;
344
+ }
266
345
  const unanswered = answers.some((a) => a.answer === null) || answers.length < questions.length;
267
346
  return {
268
347
  content: unanswered