@runappsai/agent 0.2.1 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3481,29 +3481,88 @@ function domUI() {
3481
3481
  async form(title, fields, opts = {}) {
3482
3482
  const zh = isZh();
3483
3483
  const v = await openDialog((d, done) => {
3484
- d.append(el("div", "font-weight:600;font-size:15px;margin-bottom:6px", title));
3484
+ if (title) d.append(el("div", "font-weight:600;font-size:15px;margin-bottom:6px", title));
3485
3485
  if (opts.message) d.append(el("div", "white-space:pre-wrap;opacity:.8;font-size:13px;margin-bottom:10px", opts.message));
3486
- const inputs = fields.map((f) => {
3487
- const label = el("label", "display:block;font-size:13px;margin:8px 0");
3488
- label.append(el("div", "opacity:.8;margin-bottom:4px", (f.label ?? f.name) + (f.required ? " *" : "")));
3489
- 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");
3490
- input.type = f.type === "number" ? "number" : "text";
3491
- if (f.type === "number") input.step = "any";
3492
- if (f.placeholder) input.placeholder = f.placeholder;
3493
- if (f.initial !== void 0) input.value = String(f.initial);
3494
- label.append(input);
3495
- d.append(label);
3496
- return { f, input };
3486
+ d.style.maxHeight = "calc(100vh - 48px)";
3487
+ d.style.overflowY = "auto";
3488
+ 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";
3489
+ const BAD = /* @__PURE__ */ Symbol("bad");
3490
+ const readers = fields.map((f) => {
3491
+ const choice = !!f.options?.length;
3492
+ const required = choice ? f.required !== false : !!f.required && f.type !== "boolean";
3493
+ const wrap = el(choice ? "div" : "label", "display:block;font-size:13px;margin:10px 0");
3494
+ wrap.append(el("div", "opacity:.8;margin-bottom:4px", (f.label ?? f.name) + (required ? " *" : "")));
3495
+ d.append(wrap);
3496
+ if (f.type === "boolean" && !choice) {
3497
+ const row2 = el("label", "display:flex;gap:8px;align-items:center;cursor:pointer");
3498
+ const input = el("input");
3499
+ input.type = "checkbox";
3500
+ input.checked = f.initial === true || f.initial === "true";
3501
+ row2.append(input, el("span", "", zh ? "\u662F" : "Yes"));
3502
+ wrap.append(row2);
3503
+ return { focus: input, read: () => input.checked };
3504
+ }
3505
+ if (!choice) {
3506
+ const area = f.type === "textarea";
3507
+ const input = area ? el("textarea", fieldStyle + ";min-height:80px;resize:vertical") : el("input", fieldStyle);
3508
+ if (input instanceof HTMLInputElement) {
3509
+ input.type = f.type === "number" ? "number" : f.type === "date" ? "date" : f.type === "datetime" ? "datetime-local" : "text";
3510
+ if (f.type === "number") input.step = "any";
3511
+ }
3512
+ if (f.placeholder) input.placeholder = f.placeholder;
3513
+ if (f.initial !== void 0 && typeof f.initial !== "boolean") input.value = String(f.initial);
3514
+ wrap.append(input);
3515
+ return { focus: input, read: () => {
3516
+ const raw = area ? input.value.replace(/\s+$/, "") : input.value.trim();
3517
+ if (!raw) return required ? BAD : void 0;
3518
+ if (f.type === "number") return Number.isFinite(Number(raw)) ? Number(raw) : BAD;
3519
+ return raw;
3520
+ } };
3521
+ }
3522
+ const name = "ra_frm_" + Math.random().toString(36).slice(2, 8);
3523
+ const inputs = [];
3524
+ const add = (value, label, description) => {
3525
+ const row2 = 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");
3526
+ const input = el("input", "margin-top:3px");
3527
+ input.type = f.multi ? "checkbox" : "radio";
3528
+ input.name = name;
3529
+ input.value = value;
3530
+ inputs.push(input);
3531
+ const text = el("div");
3532
+ text.append(el("div", "", label));
3533
+ if (description) text.append(el("div", "opacity:.7;font-size:12px", description));
3534
+ row2.append(input, text);
3535
+ wrap.append(row2);
3536
+ };
3537
+ for (const o of f.options) add(o.value, o.label ?? o.value, o.description);
3538
+ let other = null;
3539
+ if (f.allowOther) {
3540
+ add("__other__", zh ? "\u5176\u4ED6\u2026" : "Other\u2026");
3541
+ other = el("input", fieldStyle + ";margin-top:4px");
3542
+ other.placeholder = zh ? "\u8BF7\u8F93\u5165" : "Type your answer";
3543
+ other.addEventListener("focus", () => {
3544
+ const o = inputs.find((i) => i.value === "__other__");
3545
+ if (o) o.checked = true;
3546
+ });
3547
+ wrap.append(other);
3548
+ }
3549
+ return { focus: inputs[0], read: () => {
3550
+ const checked = inputs.filter((i) => i.checked);
3551
+ if (checked.some((i) => i.value === "__other__") && !other?.value.trim()) return BAD;
3552
+ const vals = checked.map((i) => i.value === "__other__" ? other.value.trim() : i.value);
3553
+ if (!vals.length) return required ? BAD : void 0;
3554
+ return f.multi ? vals : vals[0];
3555
+ } };
3497
3556
  });
3498
3557
  const submit = () => {
3499
3558
  const out = {};
3500
- for (const { f, input } of inputs) {
3501
- const raw = input.value.trim();
3502
- if (!raw && f.required || raw && f.type === "number" && !Number.isFinite(Number(raw))) {
3503
- input.focus();
3559
+ for (let i = 0; i < fields.length; i++) {
3560
+ const v2 = readers[i].read();
3561
+ if (v2 === BAD) {
3562
+ readers[i].focus.focus();
3504
3563
  return;
3505
3564
  }
3506
- if (raw) out[f.name] = f.type === "number" ? Number(raw) : raw;
3565
+ if (v2 !== void 0) out[fields[i].name] = v2;
3507
3566
  }
3508
3567
  done(out);
3509
3568
  };
@@ -3513,7 +3572,7 @@ function domUI() {
3513
3572
  button(opts.ok ?? (zh ? "\u786E\u5B9A" : "OK"), true, false, submit)
3514
3573
  );
3515
3574
  d.append(row);
3516
- queueMicrotask(() => inputs[0]?.input.focus());
3575
+ queueMicrotask(() => readers[0]?.focus.focus());
3517
3576
  }, opts.signal, null);
3518
3577
  return v && typeof v === "object" && !Array.isArray(v) ? v : null;
3519
3578
  },
@@ -3553,14 +3612,14 @@ function askUserTool() {
3553
3612
  name: "ask_user",
3554
3613
  // The model asks, between its steps; code in the middle of a run does not.
3555
3614
  callableFromCode: false,
3556
- description: "Ask the user one to three questions when the request is genuinely ambiguous and the answer changes what you would do. Give 2\u20135 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.",
3615
+ 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\u20135 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.",
3557
3616
  parameters: {
3558
3617
  type: "object",
3559
3618
  properties: {
3560
3619
  questions: {
3561
3620
  type: "array",
3562
3621
  minItems: 1,
3563
- maxItems: 3,
3622
+ maxItems: 4,
3564
3623
  items: {
3565
3624
  type: "object",
3566
3625
  properties: {
@@ -3589,7 +3648,14 @@ function askUserTool() {
3589
3648
  },
3590
3649
  async execute({ questions }, ctx) {
3591
3650
  const answers = [];
3592
- for (const q of questions) {
3651
+ if (ctx.ui.form && questions.length > 1) {
3652
+ const fields = questions.map((q, i) => q.options && q.options.length ? { 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 } : { name: `q${i + 1}`, label: q.question, required: true });
3653
+ const values = await ctx.ui.form("", fields, { signal: ctx.signal });
3654
+ questions.forEach((q, i) => {
3655
+ const v = values?.[`q${i + 1}`];
3656
+ answers.push({ question: q.question, answer: v === void 0 ? null : Array.isArray(v) ? v : String(v) });
3657
+ });
3658
+ } else for (const q of questions) {
3593
3659
  ctx.signal.throwIfAborted();
3594
3660
  let answer;
3595
3661
  if (q.options && q.options.length) {
package/dist/index.d.ts CHANGED
@@ -36,4 +36,4 @@ export { buildSystemPrompt, estimateTokens, fitBudget, groupTurns } from "./cont
36
36
  export { validateSchema } from "./json-schema.js";
37
37
  export { listZip, readZipEntry, buildZip, type ZipEntry, type Inflate } from "./zip.js";
38
38
  export { skillZipFS, skillFoldersOf, subFS } from "./skill-zips.js";
39
- export type { AgentEventMap, AgentEventName, AgentFS, AgentOptions, AgentUI, FormField, AgentUsage, FileStat, JSONSchema, Permissions, RunEnd, RunEndReason, RunResult, SelectOption, SessionData, SessionSummary, Skill, Tool, ToolCallHookEvent, ToolContext, ToolDefinition, ToolConfirm, ToolResultValue, ToolStatus, ToolDisplay, } from "./types.js";
39
+ export type { AgentEventMap, AgentEventName, AgentFS, AgentOptions, AgentUI, FormField, FormValues, AgentUsage, FileStat, JSONSchema, Permissions, RunEnd, RunEndReason, RunResult, SelectOption, SessionData, SessionSummary, Skill, Tool, ToolCallHookEvent, ToolContext, ToolDefinition, ToolConfirm, ToolResultValue, ToolStatus, ToolDisplay, } from "./types.js";
package/dist/types.d.ts CHANGED
@@ -245,17 +245,39 @@ export interface AgentUI {
245
245
  message?: string;
246
246
  ok?: string;
247
247
  signal?: AbortSignal;
248
- }): Promise<Record<string, string | number> | null>;
248
+ }): Promise<FormValues | null>;
249
249
  notify(message: string, level?: "info" | "warning" | "error"): void;
250
250
  }
251
- /** One field of `AgentUI.form`. */
251
+ /**
252
+ * What `AgentUI.form` resolves: a value per answered field — a number for a `number` field, true or false
253
+ * for a `boolean` one, `YYYY-MM-DD` for a `date`, `YYYY-MM-DDTHH:mm` for a `datetime`, the picked values
254
+ * for a `multi` choice, text for the rest.
255
+ */
256
+ export type FormValues = Record<string, string | number | boolean | string[]>;
257
+ /**
258
+ * One field of `AgentUI.form`. A field with `options` is a choice question
259
+ * (answered by default; `required: false` lets it go unanswered); one without
260
+ * is a text or number box. In the built-in chat a form with a choice in it is
261
+ * asked one question at a time.
262
+ */
252
263
  export interface FormField {
253
264
  name: string;
254
265
  label?: string;
255
- type?: "text" | "number";
266
+ /**
267
+ * The input when there are no `options`: a one-line box (`text`, the default), `number`, `textarea`
268
+ * (several lines), `date`, `datetime` (date and time) or `boolean` (a switch, always answered).
269
+ */
270
+ type?: "text" | "number" | "textarea" | "date" | "datetime" | "boolean";
256
271
  required?: boolean;
257
272
  placeholder?: string;
258
- initial?: string | number;
273
+ /** The starting value: text, a number, a date as `YYYY-MM-DD`, or true / false for a switch. */
274
+ initial?: string | number | boolean;
275
+ /** The choices; the answer is the picked option's `value`. */
276
+ options?: SelectOption[];
277
+ /** Several may be picked (a choice only): the answer is an array. */
278
+ multi?: boolean;
279
+ /** Adds "Other…" with a box for the user's own answer (a choice only). */
280
+ allowOther?: boolean;
259
281
  }
260
282
  export interface FileStat {
261
283
  /** Absolute POSIX path inside the workspace. */
package/dist/ui.cjs CHANGED
@@ -2546,6 +2546,11 @@ var STRINGS = {
2546
2546
  ok: "OK",
2547
2547
  other: "Other\u2026",
2548
2548
  typeAnswer: "Type your answer",
2549
+ prev: "Back",
2550
+ next: "Next",
2551
+ skip: "Skip",
2552
+ swOn: "Yes",
2553
+ swOff: "No",
2549
2554
  args: "Arguments",
2550
2555
  result: "Result",
2551
2556
  error: "Error",
@@ -2862,6 +2867,11 @@ var STRINGS = {
2862
2867
  ok: "\u786E\u5B9A",
2863
2868
  other: "\u5176\u4ED6\u2026",
2864
2869
  typeAnswer: "\u8BF7\u8F93\u5165",
2870
+ prev: "\u4E0A\u4E00\u9898",
2871
+ next: "\u4E0B\u4E00\u9898",
2872
+ skip: "\u8DF3\u8FC7",
2873
+ swOn: "\u662F",
2874
+ swOff: "\u5426",
2865
2875
  args: "\u53C2\u6570",
2866
2876
  result: "\u7ED3\u679C",
2867
2877
  error: "\u9519\u8BEF",
@@ -3472,6 +3482,29 @@ var CSS = tokenCSS() + `
3472
3482
  .ra-q .ra-form{display:flex;flex-direction:column;gap:8px;margin:2px 0 4px}
3473
3483
  .ra-q .ra-ff{display:flex;flex-direction:column;gap:4px;font-size:13px;color:var(--ra-fg-muted)}
3474
3484
  .ra-q textarea.ra-field{min-height:96px;max-height:200px;resize:vertical}
3485
+ .ra-q .ra-qtabs{display:flex;gap:4px;overflow-x:auto;scrollbar-width:none;margin-top:-2px}
3486
+ .ra-q .ra-qtabs::-webkit-scrollbar{display:none}
3487
+ .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)}
3488
+ .ra-q .ra-qtab>span{overflow:hidden;text-overflow:ellipsis;white-space:nowrap}
3489
+ .ra-q .ra-qtab:hover{background:var(--ra-muted)}
3490
+ .ra-q .ra-qtab.done{color:var(--ra-fg)}
3491
+ .ra-q .ra-qtab.done::before{content:"\\2713";color:var(--ra-accent);font-weight:600}
3492
+ .ra-q .ra-qtab.cur{border-color:var(--ra-accent);color:var(--ra-fg)}
3493
+ .ra-q .ra-qpane{display:flex;flex-direction:column;gap:8px}
3494
+ .ra-q .ra-qq{font-size:14px;white-space:pre-wrap}
3495
+ .ra-q .ra-opt.on{border-color:var(--ra-accent)}
3496
+ .ra-q .ra-opts.bad .ra-opt{border-color:var(--ra-red)}
3497
+ .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}
3498
+ .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}
3499
+ .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}
3500
+ .ra-chat input.ra-sw:checked{background:var(--ra-accent)}
3501
+ .ra-chat input.ra-sw:checked::before{transform:translateX(14px)}
3502
+ .ra-chat input.ra-sw:focus{outline:0;box-shadow:none}
3503
+ .ra-chat input.ra-sw:focus-visible{outline:2px solid var(--ra-accent);outline-offset:2px}
3504
+ .ra-q .ra-ff .ra-switch{margin-top:2px}
3505
+ .ra-q input.ra-field:is([type=date],[type=datetime-local]){min-height:32px}
3506
+ .ra-chat[data-scheme=dark] .ra-q input.ra-field:is([type=date],[type=datetime-local]){color-scheme:dark}
3507
+ .ra-chat[data-scheme=light] .ra-q input.ra-field:is([type=date],[type=datetime-local]){color-scheme:light}
3475
3508
  .ra-btns{display:flex;justify-content:flex-end;gap:8px}
3476
3509
  .ra-cbody{margin:2px 0 10px;max-height:min(60vh,560px);overflow:auto;min-width:0}
3477
3510
  .ra-status{font-size:14px;color:var(--ra-fg-muted);padding:4px 0}
@@ -3847,7 +3880,7 @@ function inlineUI(askArea, toasts, t, onChange) {
3847
3880
  queueMicrotask(() => focus?.focus());
3848
3881
  });
3849
3882
  const head = (card, title, message) => {
3850
- card.append(h("div", "ra-qt", title));
3883
+ if (title) card.append(h("div", "ra-qt", title));
3851
3884
  if (message) card.append(h("div", "ra-qm", message));
3852
3885
  };
3853
3886
  const btn = (label, cls, onClick) => {
@@ -3856,6 +3889,245 @@ function inlineUI(askArea, toasts, t, onChange) {
3856
3889
  b.addEventListener("click", onClick);
3857
3890
  return b;
3858
3891
  };
3892
+ const boxFor = (f, initial, onChange2) => {
3893
+ if (f.type === "boolean") {
3894
+ const label = h("label", "ra-switch");
3895
+ const input2 = h("input", "ra-x ra-sw");
3896
+ input2.type = "checkbox";
3897
+ input2.setAttribute("role", "switch");
3898
+ input2.name = f.name;
3899
+ input2.checked = initial === true;
3900
+ const state = h("span", "", input2.checked ? t.swOn : t.swOff);
3901
+ input2.addEventListener("change", () => {
3902
+ state.textContent = input2.checked ? t.swOn : t.swOff;
3903
+ onChange2();
3904
+ });
3905
+ label.append(input2, state);
3906
+ return { el: label, focus: input2, raw: () => input2.checked };
3907
+ }
3908
+ const area = f.type === "textarea";
3909
+ const input = area ? h("textarea", "ra-field") : h("input", "ra-field");
3910
+ if (input instanceof HTMLInputElement) {
3911
+ input.type = f.type === "number" ? "number" : f.type === "date" ? "date" : f.type === "datetime" ? "datetime-local" : "text";
3912
+ if (f.type === "number") input.step = "any";
3913
+ }
3914
+ input.name = f.name;
3915
+ if (f.placeholder) input.placeholder = f.placeholder;
3916
+ input.value = typeof initial === "string" ? initial : "";
3917
+ input.addEventListener("input", () => {
3918
+ input.classList.remove("bad");
3919
+ onChange2();
3920
+ });
3921
+ return { el: input, focus: input, raw: () => input.value };
3922
+ };
3923
+ const boxValue = (f, raw) => {
3924
+ if (typeof raw === "boolean") return raw;
3925
+ const text = f.type === "textarea" ? raw.replace(/\s+$/, "") : raw.trim();
3926
+ if (!text) return void 0;
3927
+ return f.type === "number" ? Number(text) : text;
3928
+ };
3929
+ const startOf = (f) => f.type === "boolean" ? f.initial === true || f.initial === "true" : f.initial !== void 0 && typeof f.initial !== "boolean" ? String(f.initial) : "";
3930
+ const stepForm = (card, fields, okLabel, done) => {
3931
+ const OTHER = "__other__";
3932
+ const n = fields.length;
3933
+ const isChoice = (f) => !!f.options?.length;
3934
+ const required = (f) => isChoice(f) ? f.required !== false : !!f.required;
3935
+ const state = fields.map((f) => ({ picked: [], other: "", raw: startOf(f) }));
3936
+ const valueOf = (i) => {
3937
+ const f = fields[i], s = state[i];
3938
+ if (isChoice(f)) {
3939
+ const vals = s.picked.map((v) => v === OTHER ? s.other.trim() : v).filter(Boolean);
3940
+ return vals.length ? f.multi ? vals : vals[0] : void 0;
3941
+ }
3942
+ return boxValue(f, s.raw);
3943
+ };
3944
+ const valid = (i) => {
3945
+ const v = valueOf(i);
3946
+ if (typeof v === "number" && !Number.isFinite(v)) return false;
3947
+ if (state[i].picked.includes(OTHER) && !state[i].other.trim()) return false;
3948
+ return v !== void 0 || !required(fields[i]);
3949
+ };
3950
+ let cur = 0;
3951
+ const tabs = h("div", "ra-qtabs");
3952
+ const tabEls = fields.map((f, i) => {
3953
+ const b = h("button", "ra-qtab");
3954
+ b.type = "button";
3955
+ const label = f.label ?? f.name;
3956
+ b.append(h("span", "", label));
3957
+ b.title = label;
3958
+ b.addEventListener("click", () => go(i));
3959
+ tabs.append(b);
3960
+ return b;
3961
+ });
3962
+ const pane = h("div", "ra-qpane");
3963
+ const row = h("div", "ra-btns");
3964
+ const prevBtn = btn(t.prev, "ghost", () => go(cur - 1));
3965
+ const nextBtn = btn(t.next, "solid", () => next());
3966
+ row.append(btn(t.skip, "ghost", () => done(null)), prevBtn, nextBtn);
3967
+ if (n > 1) card.append(tabs);
3968
+ card.append(pane, row);
3969
+ const sync = () => {
3970
+ tabEls.forEach((b, i) => {
3971
+ b.classList.toggle("cur", i === cur);
3972
+ b.classList.toggle("done", valueOf(i) !== void 0 && valid(i));
3973
+ });
3974
+ prevBtn.style.display = cur > 0 ? "" : "none";
3975
+ nextBtn.textContent = cur === n - 1 ? okLabel ?? t.submit : t.next;
3976
+ pane.querySelectorAll(".bad").forEach((e) => e.classList.remove("bad"));
3977
+ };
3978
+ const flag = () => {
3979
+ const s = state[cur];
3980
+ const target = s.picked.includes(OTHER) ? pane.querySelector("input.ra-field") : pane.querySelector(".ra-opts,.ra-field");
3981
+ target?.classList.add("bad");
3982
+ if (target instanceof HTMLInputElement) target.focus();
3983
+ };
3984
+ const go = (i) => {
3985
+ if (i < 0 || i >= n || i === cur) return;
3986
+ cur = i;
3987
+ render();
3988
+ tabEls[i]?.scrollIntoView?.({ block: "nearest", inline: "nearest" });
3989
+ };
3990
+ const next = () => {
3991
+ if (!valid(cur)) {
3992
+ flag();
3993
+ return;
3994
+ }
3995
+ if (cur < n - 1) {
3996
+ go(cur + 1);
3997
+ return;
3998
+ }
3999
+ const first = fields.findIndex((_, k) => !valid(k));
4000
+ if (first >= 0) {
4001
+ go(first);
4002
+ flag();
4003
+ return;
4004
+ }
4005
+ const out = {};
4006
+ fields.forEach((f, k) => {
4007
+ const v = valueOf(k);
4008
+ if (v !== void 0) out[f.name] = v;
4009
+ });
4010
+ done(out);
4011
+ };
4012
+ const optionText = (o) => {
4013
+ const text = h("div");
4014
+ text.append(h("span", "", o.label ?? o.value));
4015
+ if (o.description) text.append(h("small", "", o.description));
4016
+ return text;
4017
+ };
4018
+ const render = () => {
4019
+ pane.replaceChildren();
4020
+ const f = fields[cur], s = state[cur];
4021
+ pane.append(h("div", "ra-qq", (f.label ?? f.name) + (required(f) && !isChoice(f) && f.type !== "boolean" ? " *" : "")));
4022
+ let focus = null;
4023
+ if (isChoice(f) && !f.multi && !f.allowOther) {
4024
+ const list = h("div", "ra-opts");
4025
+ for (const o of f.options) {
4026
+ const b = h("button", "ra-opt");
4027
+ b.type = "button";
4028
+ if (s.picked[0] === o.value) {
4029
+ b.classList.add("on");
4030
+ focus = b;
4031
+ }
4032
+ b.append(optionText(o));
4033
+ b.addEventListener("click", () => {
4034
+ s.picked = [o.value];
4035
+ sync();
4036
+ next();
4037
+ });
4038
+ list.append(b);
4039
+ }
4040
+ pane.append(list);
4041
+ focus ?? (focus = list.querySelector("button"));
4042
+ } else if (isChoice(f)) {
4043
+ const list = h("div", "ra-opts");
4044
+ const name = "ra_" + Math.random().toString(36).slice(2, 8);
4045
+ const inputs = [];
4046
+ const read = () => {
4047
+ s.picked = inputs.filter((x) => x.checked).map((x) => x.value);
4048
+ sync();
4049
+ };
4050
+ const all = f.allowOther ? [...f.options, { value: OTHER, label: t.other }] : f.options;
4051
+ for (const o of all) {
4052
+ const label = h("label", "ra-opt");
4053
+ const input = h("input");
4054
+ input.type = f.multi ? "checkbox" : "radio";
4055
+ input.name = name;
4056
+ input.value = o.value;
4057
+ input.checked = s.picked.includes(o.value);
4058
+ input.addEventListener("change", read);
4059
+ inputs.push(input);
4060
+ label.append(input, optionText(o));
4061
+ list.append(label);
4062
+ }
4063
+ pane.append(list);
4064
+ if (f.allowOther) {
4065
+ const other = h("input", "ra-field");
4066
+ other.type = "text";
4067
+ other.placeholder = t.typeAnswer;
4068
+ other.value = s.other;
4069
+ other.addEventListener("focus", () => {
4070
+ const o = inputs.find((x) => x.value === OTHER);
4071
+ if (o && !o.checked) {
4072
+ o.checked = true;
4073
+ read();
4074
+ }
4075
+ });
4076
+ other.addEventListener("input", () => {
4077
+ s.other = other.value;
4078
+ sync();
4079
+ });
4080
+ pane.append(other);
4081
+ }
4082
+ focus = inputs.find((x) => x.checked) ?? inputs[0] ?? null;
4083
+ } else {
4084
+ const box = boxFor(f, s.raw, () => {
4085
+ s.raw = box.raw();
4086
+ sync();
4087
+ });
4088
+ pane.append(box.el);
4089
+ focus = box.focus;
4090
+ }
4091
+ sync();
4092
+ if (card.isConnected) queueMicrotask(() => focus?.focus());
4093
+ };
4094
+ card.addEventListener("keydown", (ev) => {
4095
+ const e = ev;
4096
+ if (e.isComposing) return;
4097
+ const target = e.target;
4098
+ if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) {
4099
+ e.preventDefault();
4100
+ next();
4101
+ return;
4102
+ }
4103
+ if (e.metaKey || e.ctrlKey || e.altKey) return;
4104
+ if (target instanceof HTMLTextAreaElement) return;
4105
+ if (e.key === "Enter") {
4106
+ if (!(target instanceof HTMLButtonElement)) {
4107
+ e.preventDefault();
4108
+ next();
4109
+ }
4110
+ return;
4111
+ }
4112
+ const onInput = target instanceof HTMLInputElement;
4113
+ if (onInput && target.type !== "radio" && target.type !== "checkbox") return;
4114
+ if (e.key === "ArrowLeft" || e.key === "ArrowRight") {
4115
+ if (!onInput) {
4116
+ e.preventDefault();
4117
+ go(cur + (e.key === "ArrowLeft" ? -1 : 1));
4118
+ }
4119
+ return;
4120
+ }
4121
+ if (/^[1-9]$/.test(e.key)) {
4122
+ const opt = pane.querySelectorAll(".ra-opt")[Number(e.key) - 1];
4123
+ if (opt) {
4124
+ e.preventDefault();
4125
+ opt.click();
4126
+ }
4127
+ }
4128
+ });
4129
+ render();
4130
+ };
3859
4131
  return {
3860
4132
  confirm: (title, message, opts = {}) => open((card, done) => {
3861
4133
  head(card, title, message);
@@ -3953,40 +4225,39 @@ function inlineUI(askArea, toasts, t, onChange) {
3953
4225
  }, null, opts.signal),
3954
4226
  form: (title, fields, opts = {}) => open((card, done) => {
3955
4227
  head(card, title, opts.message);
4228
+ if (fields.some((f) => f.options?.length)) {
4229
+ stepForm(card, fields, opts.ok, done);
4230
+ return;
4231
+ }
3956
4232
  const box = h("div", "ra-form");
3957
4233
  const inputs = fields.map((f) => {
3958
- const label = h("label", "ra-ff");
3959
- label.append(h("span", "", (f.label ?? f.name) + (f.required ? " *" : "")));
3960
- const input = h("input", "ra-field");
3961
- input.type = f.type === "number" ? "number" : "text";
3962
- input.name = f.name;
3963
- if (f.type === "number") input.step = "any";
3964
- if (f.placeholder) input.placeholder = f.placeholder;
3965
- if (f.initial !== void 0) input.value = String(f.initial);
3966
- label.append(input);
4234
+ const label = h(f.type === "boolean" ? "div" : "label", "ra-ff");
4235
+ label.append(h("span", "", (f.label ?? f.name) + (f.required && f.type !== "boolean" ? " *" : "")));
4236
+ const field = boxFor(f, startOf(f), () => void 0);
4237
+ label.append(field.el);
3967
4238
  box.append(label);
3968
- return { f, input };
4239
+ return { f, field };
3969
4240
  });
3970
4241
  const submit = () => {
3971
4242
  const out = {};
3972
- for (const { f, input } of inputs) {
3973
- const raw = input.value.trim();
3974
- const bad = !raw && f.required || !!raw && f.type === "number" && !Number.isFinite(Number(raw));
3975
- input.classList.toggle("bad", bad);
4243
+ for (const { f, field } of inputs) {
4244
+ const v = boxValue(f, field.raw());
4245
+ const bad = v === void 0 && !!f.required || typeof v === "number" && !Number.isFinite(v);
4246
+ field.focus.classList.toggle("bad", bad);
3976
4247
  if (bad) {
3977
- input.focus();
4248
+ field.focus.focus();
3978
4249
  return;
3979
4250
  }
3980
- if (raw) out[f.name] = f.type === "number" ? Number(raw) : raw;
4251
+ if (v !== void 0) out[f.name] = v;
3981
4252
  }
3982
4253
  done(out);
3983
4254
  };
3984
4255
  box.addEventListener("keydown", (ev) => {
3985
4256
  const e = ev;
3986
- if (e.key === "Enter" && !e.isComposing) {
3987
- e.preventDefault();
3988
- submit();
3989
- }
4257
+ if (e.key !== "Enter" || e.isComposing) return;
4258
+ if (e.target instanceof HTMLTextAreaElement && !e.metaKey && !e.ctrlKey) return;
4259
+ e.preventDefault();
4260
+ submit();
3990
4261
  });
3991
4262
  card.append(box);
3992
4263
  const row = h("div", "ra-btns");
@@ -4417,6 +4688,7 @@ ${t.webHint}`;
4417
4688
  confirm: async (title2, message, o) => await whenCurrent(id, o?.signal) ? ui.confirm(title2, message, o?.permission && permShown && !o.always ? { ...o, always: { label: t.permAlways, onChoose: () => setPerm("auto") } } : o) : false,
4418
4689
  select: async (title2, options, o) => await whenCurrent(id, o?.signal) ? ui.select(title2, options, o) : null,
4419
4690
  input: async (title2, o) => await whenCurrent(id, o?.signal) ? ui.input(title2, o) : null,
4691
+ form: async (title2, fields, o) => await whenCurrent(id, o?.signal) && ui.form ? ui.form(title2, fields, o) : null,
4420
4692
  notify: (m, l) => ui.notify(m, l)
4421
4693
  });
4422
4694
  const restorable = (a) => !!a.workspace && typeof wsOpt === "function" && !a.workspace.root;
package/dist/ui.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  export { renderChat, enhanceSelect, type ChatOptions, type ChatHandle, type ComposerAction, type SettingsField, type SettingsTab } from "./chat.js";
2
2
  export type { FileDialog } from "./dom.js";
3
- export type { ChatAgent, FormField } from "./index.js";
3
+ export type { ChatAgent, FormField, FormValues } from "./index.js";
4
4
  export { selectBox, type SelectBox, type SelectOption as SelectBoxOption } from "./select.js";
5
5
  export { VirtualList, type VRow, type VListOptions } from "./vlist.js";
6
6
  export { CHAT_PALETTES, CHAT_COLOR_TOKENS, CHAT_SHARED_TOKENS, type ChatPalette, type ChatVars, type ChatToken, type ChatColorToken } from "./chat-theme.js";
package/docs/chat-ui.md CHANGED
@@ -334,7 +334,12 @@ What the chat relies on:
334
334
  `replyActions.delete: false` without `remove`, `clearButton: false` (and no `/new` in `commands`) without `clear`.
335
335
  - **Dialogs**: the host's agent asks through its own `ui`; pass it `chat.ui` to ask in the cards above the composer.
336
336
  Besides `confirm`, `select` (now with `min` / `max` for several choices) and `input`, the built-in UIs have
337
- `form(title, fields)` for several fields at once.
337
+ `form(title, fields)` for several fields at once. A field with `options` is a choice (`multi` for several,
338
+ `allowOther` for the user's own answer; answered unless `required: false`); a field without is a box by its
339
+ `type`: `text` (the default), `number`, `textarea` (several lines; Enter is a new line, Cmd/Ctrl+Enter goes
340
+ on), `date` (`YYYY-MM-DD`), `datetime` (`YYYY-MM-DDTHH:mm`) or `boolean` (a switch, always true or false).
341
+ A form with a choice in it is asked one question at a time — tabs to go back, the answers sent together at
342
+ the end — and a `multi` choice's value is an array.
338
343
 
339
344
  Two things exist for an agent whose messages change outside its own runs:
340
345
 
package/docs/tools.md CHANGED
@@ -243,7 +243,7 @@ In `renderChat` the globe in the composer's toolbar switches them for the user (
243
243
 
244
244
  | Tool | | Option |
245
245
  |---|---|---|
246
- | `ask_user` | One to three questions to the user, with options or free text, when a request is unclear. | `askUser: false` removes it |
246
+ | `ask_user` | One to four questions to the user, with options or free text, when a request is unclear; several are asked together in one form when the UI has `form`. | `askUser: false` removes it |
247
247
  | `run_js` | JavaScript the model writes, run in a Worker: no DOM, no network, a captured console, and `tools` for the agent's other tools ([below](#calling-tools-from-run_js)). The model computes, parses and transforms data with it instead of guessing, and runs skills' scripts. Stopping the run ends the worker, even one stuck in a loop. | `sandbox: false`, or `{ timeoutMs, network, maxToolCalls, maxCodeChars }` |
248
248
  | `list_files` | Files under a folder, with sizes. | with a workspace |
249
249
  | `read_file` | A file, 2000 lines / 50 KB per call, `offset` and `limit` to continue; a PDF as text; a picture shown to a model that sees pictures. | |
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@runappsai/agent",
3
- "version": "0.2.1",
3
+ "version": "0.3.0",
4
4
  "type": "module",
5
5
  "description": "Agent harness: the model ↔ tool loop with a workspace, skills, sessions, compaction, a sandbox and a chat UI. Bring any OpenAI-compatible model provider (RunApps included); no runtime dependencies.",
6
6
  "license": "MIT",