chivgent 1.1.3 → 1.1.5

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.
Files changed (173) hide show
  1. package/README.md +8 -7
  2. package/README.zh-CN.md +7 -7
  3. package/dist/agent.d.ts +10 -1
  4. package/dist/agent.d.ts.map +1 -1
  5. package/dist/agent.js +57 -7
  6. package/dist/agent.js.map +1 -1
  7. package/dist/cli-options.d.ts +2 -0
  8. package/dist/cli-options.d.ts.map +1 -1
  9. package/dist/cli-options.js +4 -0
  10. package/dist/cli-options.js.map +1 -1
  11. package/dist/cli-runtime.d.ts +3 -0
  12. package/dist/cli-runtime.d.ts.map +1 -1
  13. package/dist/cli-runtime.js +31 -6
  14. package/dist/cli-runtime.js.map +1 -1
  15. package/dist/cli.d.ts.map +1 -1
  16. package/dist/cli.js +47 -15
  17. package/dist/cli.js.map +1 -1
  18. package/dist/evals/cli.js +5 -5
  19. package/dist/evals/cli.js.map +1 -1
  20. package/dist/events.d.ts +5 -0
  21. package/dist/events.d.ts.map +1 -1
  22. package/dist/events.js.map +1 -1
  23. package/dist/messages.d.ts +5 -0
  24. package/dist/messages.d.ts.map +1 -1
  25. package/dist/permissions.d.ts +3 -2
  26. package/dist/permissions.d.ts.map +1 -1
  27. package/dist/permissions.js +10 -5
  28. package/dist/permissions.js.map +1 -1
  29. package/dist/prompts.d.ts +8 -3
  30. package/dist/prompts.d.ts.map +1 -1
  31. package/dist/prompts.js +12 -3
  32. package/dist/prompts.js.map +1 -1
  33. package/dist/providers/deepseek.d.ts +1 -0
  34. package/dist/providers/deepseek.d.ts.map +1 -1
  35. package/dist/providers/deepseek.js +2 -2
  36. package/dist/providers/deepseek.js.map +1 -1
  37. package/dist/providers/interactive.d.ts +9 -3
  38. package/dist/providers/interactive.d.ts.map +1 -1
  39. package/dist/providers/interactive.js +116 -19
  40. package/dist/providers/interactive.js.map +1 -1
  41. package/dist/questions.d.ts +58 -0
  42. package/dist/questions.d.ts.map +1 -0
  43. package/dist/questions.js +155 -0
  44. package/dist/questions.js.map +1 -0
  45. package/dist/remote/server.d.ts +6 -1
  46. package/dist/remote/server.d.ts.map +1 -1
  47. package/dist/remote/server.js +100 -16
  48. package/dist/remote/server.js.map +1 -1
  49. package/dist/repl.d.ts +8 -7
  50. package/dist/repl.d.ts.map +1 -1
  51. package/dist/repl.js +317 -310
  52. package/dist/repl.js.map +1 -1
  53. package/dist/runtime/desktop-runtime.d.ts +18 -0
  54. package/dist/runtime/desktop-runtime.d.ts.map +1 -1
  55. package/dist/runtime/desktop-runtime.js +19 -5
  56. package/dist/runtime/desktop-runtime.js.map +1 -1
  57. package/dist/session.d.ts +3 -0
  58. package/dist/session.d.ts.map +1 -1
  59. package/dist/session.js +5 -0
  60. package/dist/session.js.map +1 -1
  61. package/dist/shell/approval.d.ts +12 -2
  62. package/dist/shell/approval.d.ts.map +1 -1
  63. package/dist/shell/approval.js +16 -5
  64. package/dist/shell/approval.js.map +1 -1
  65. package/dist/shell/output-store.d.ts +49 -0
  66. package/dist/shell/output-store.d.ts.map +1 -0
  67. package/dist/shell/output-store.js +250 -0
  68. package/dist/shell/output-store.js.map +1 -0
  69. package/dist/shell/output.d.ts +23 -2
  70. package/dist/shell/output.d.ts.map +1 -1
  71. package/dist/shell/output.js +106 -24
  72. package/dist/shell/output.js.map +1 -1
  73. package/dist/tools/bash.d.ts +6 -4
  74. package/dist/tools/bash.d.ts.map +1 -1
  75. package/dist/tools/bash.js +39 -19
  76. package/dist/tools/bash.js.map +1 -1
  77. package/dist/tools/list-files.d.ts.map +1 -1
  78. package/dist/tools/list-files.js +3 -0
  79. package/dist/tools/list-files.js.map +1 -1
  80. package/dist/tools/parallel.d.ts.map +1 -1
  81. package/dist/tools/parallel.js +5 -1
  82. package/dist/tools/parallel.js.map +1 -1
  83. package/dist/tools/question.d.ts +72 -0
  84. package/dist/tools/question.d.ts.map +1 -0
  85. package/dist/tools/question.js +52 -0
  86. package/dist/tools/question.js.map +1 -0
  87. package/dist/tools/read-shell-output.d.ts +32 -0
  88. package/dist/tools/read-shell-output.d.ts.map +1 -0
  89. package/dist/tools/read-shell-output.js +39 -0
  90. package/dist/tools/read-shell-output.js.map +1 -0
  91. package/dist/tools/search-text.d.ts +6 -2
  92. package/dist/tools/search-text.d.ts.map +1 -1
  93. package/dist/tools/search-text.js +24 -8
  94. package/dist/tools/search-text.js.map +1 -1
  95. package/dist/tools/tool.d.ts +9 -0
  96. package/dist/tools/tool.d.ts.map +1 -1
  97. package/dist/tools/tool.js.map +1 -1
  98. package/dist/tui/activity.d.ts.map +1 -1
  99. package/dist/tui/activity.js +52 -13
  100. package/dist/tui/activity.js.map +1 -1
  101. package/dist/tui/animation.d.ts +21 -0
  102. package/dist/tui/animation.d.ts.map +1 -0
  103. package/dist/tui/animation.js +97 -0
  104. package/dist/tui/animation.js.map +1 -0
  105. package/dist/tui/editor-input.d.ts +20 -38
  106. package/dist/tui/editor-input.d.ts.map +1 -1
  107. package/dist/tui/editor-input.js +136 -347
  108. package/dist/tui/editor-input.js.map +1 -1
  109. package/dist/tui/editor.d.ts +11 -24
  110. package/dist/tui/editor.d.ts.map +1 -1
  111. package/dist/tui/editor.js +79 -147
  112. package/dist/tui/editor.js.map +1 -1
  113. package/dist/tui/git-diff.d.ts.map +1 -1
  114. package/dist/tui/git-diff.js +2 -23
  115. package/dist/tui/git-diff.js.map +1 -1
  116. package/dist/tui/input.d.ts +12 -16
  117. package/dist/tui/input.d.ts.map +1 -1
  118. package/dist/tui/input.js +89 -127
  119. package/dist/tui/input.js.map +1 -1
  120. package/dist/tui/live.d.ts +4 -0
  121. package/dist/tui/live.d.ts.map +1 -1
  122. package/dist/tui/live.js +18 -8
  123. package/dist/tui/live.js.map +1 -1
  124. package/dist/tui/paint.d.ts +3 -0
  125. package/dist/tui/paint.d.ts.map +1 -1
  126. package/dist/tui/paint.js +9 -2
  127. package/dist/tui/paint.js.map +1 -1
  128. package/dist/tui/provider-picker.d.ts +9 -6
  129. package/dist/tui/provider-picker.d.ts.map +1 -1
  130. package/dist/tui/provider-picker.js +11 -9
  131. package/dist/tui/provider-picker.js.map +1 -1
  132. package/dist/tui/state.d.ts +1 -1
  133. package/dist/tui/state.d.ts.map +1 -1
  134. package/dist/tui/state.js +9 -1
  135. package/dist/tui/state.js.map +1 -1
  136. package/dist/tui/view.d.ts +34 -0
  137. package/dist/tui/view.d.ts.map +1 -1
  138. package/dist/tui/view.js +142 -57
  139. package/dist/tui/view.js.map +1 -1
  140. package/dist/tui/welcome.d.ts +4 -4
  141. package/dist/tui/welcome.d.ts.map +1 -1
  142. package/dist/tui/welcome.js +58 -25
  143. package/dist/tui/welcome.js.map +1 -1
  144. package/dist/websearch/backend.d.ts.map +1 -1
  145. package/dist/websearch/backend.js +10 -6
  146. package/dist/websearch/backend.js.map +1 -1
  147. package/dist/workspace/fs.d.ts +4 -0
  148. package/dist/workspace/fs.d.ts.map +1 -0
  149. package/dist/workspace/fs.js +6 -0
  150. package/dist/workspace/fs.js.map +1 -0
  151. package/dist/workspace/git-read.d.ts +3 -0
  152. package/dist/workspace/git-read.d.ts.map +1 -0
  153. package/dist/workspace/git-read.js +23 -0
  154. package/dist/workspace/git-read.js.map +1 -0
  155. package/dist/workspace/list.d.ts.map +1 -1
  156. package/dist/workspace/list.js +9 -5
  157. package/dist/workspace/list.js.map +1 -1
  158. package/dist/workspace/search.d.ts +1 -0
  159. package/dist/workspace/search.d.ts.map +1 -1
  160. package/dist/workspace/search.js +38 -6
  161. package/dist/workspace/search.js.map +1 -1
  162. package/dist/workspace/text.d.ts +1 -1
  163. package/dist/workspace/text.d.ts.map +1 -1
  164. package/dist/workspace/text.js +2 -3
  165. package/dist/workspace/text.js.map +1 -1
  166. package/dist/workspace/types.d.ts +7 -1
  167. package/dist/workspace/types.d.ts.map +1 -1
  168. package/dist/workspace/types.js.map +1 -1
  169. package/package.json +1 -1
  170. package/dist/tui/menu.d.ts +0 -26
  171. package/dist/tui/menu.d.ts.map +0 -1
  172. package/dist/tui/menu.js +0 -75
  173. package/dist/tui/menu.js.map +0 -1
package/dist/repl.js CHANGED
@@ -1,19 +1,78 @@
1
1
  import { TuiInput } from "./tui/input.js";
2
2
  import { EditorController } from "./tui/editor-input.js";
3
3
  import { readLiveGitDiff } from "./tui/git-diff.js";
4
- import { terminalText } from "./tui/text.js";
4
+ import { displayWidth, terminalText } from "./tui/text.js";
5
5
  import { terminalColorEnabled } from "./tui/selection.js";
6
6
  import { PatchReviewController } from "./tui/review.js";
7
- import { ProviderPickerController } from "./tui/provider-picker.js";
8
- import { InputMenu } from "./tui/menu.js";
9
- import { createInterface, emitKeypressEvents } from "node:readline";
7
+ import { ProviderPickerController, providerPickerLabel } from "./tui/provider-picker.js";
8
+ import { createInterface } from "node:readline";
10
9
  import { formatTokens } from "./providers/usage.js";
11
10
  import { approvalPreview, commandReviewable } from "./tui/activity.js";
11
+ import { fitLine } from "./tui/text.js";
12
12
  import { terminalWidth } from "./tui/live.js";
13
13
  import { formatDiffPage, mostRecentSuccessfulPatch } from "./tui/diff.js";
14
14
  import { describePermissionMode, parsePermissionMode, WEB_SEARCH_APPROVAL_PREFIX } from "./permissions.js";
15
15
  import { HeuristicTokenEstimator } from "./context/token-estimator.js";
16
16
  export const REPL_PROMPT = "› ";
17
+ function parseQuestionAnswer(question, input) {
18
+ const value = input.trim();
19
+ if (value.length === 0 || value.length > 2_000)
20
+ return undefined;
21
+ if (question.options === undefined)
22
+ return value;
23
+ if (question.multiple === true) {
24
+ const selected = [];
25
+ for (const part of value.split(",").map(item => item.trim())) {
26
+ if (/^text:/i.test(part) && question.allowFreeText === true) {
27
+ const custom = part.slice(5).trim();
28
+ if (custom.length === 0 || custom.length > 2_000)
29
+ return undefined;
30
+ selected.push(custom);
31
+ }
32
+ else {
33
+ if (!/^\d+$/.test(part))
34
+ return undefined;
35
+ const option = question.options[Number(part) - 1];
36
+ if (option === undefined || selected.includes(option.label))
37
+ return undefined;
38
+ selected.push(option.label);
39
+ }
40
+ }
41
+ return selected.length === 0 ? undefined : selected;
42
+ }
43
+ if (/^text:/i.test(value) && question.allowFreeText === true) {
44
+ const custom = value.slice(5).trim();
45
+ return custom.length > 0 && custom.length <= 2_000 ? custom : undefined;
46
+ }
47
+ if (!/^\d+$/.test(value))
48
+ return undefined;
49
+ const option = question.options[Number(value) - 1];
50
+ return option?.label;
51
+ }
52
+ function wrapQuestionText(value, width) {
53
+ const lines = [];
54
+ let line = "";
55
+ let cells = 0;
56
+ for (const part of new Intl.Segmenter(undefined, { granularity: "grapheme" }).segment(value)) {
57
+ if (part.segment === "\n") {
58
+ lines.push(line);
59
+ line = "";
60
+ cells = 0;
61
+ continue;
62
+ }
63
+ const cellsInPart = displayWidth(part.segment);
64
+ if (line.length > 0 && cells + cellsInPart > width) {
65
+ lines.push(line);
66
+ line = "";
67
+ cells = 0;
68
+ }
69
+ line += part.segment;
70
+ cells += cellsInPart;
71
+ }
72
+ if (line.length > 0 || lines.length === 0)
73
+ lines.push(line);
74
+ return lines;
75
+ }
17
76
  export const BUILT_IN_COMMANDS = [
18
77
  "help",
19
78
  "status",
@@ -34,10 +93,6 @@ export const LEGACY_COMMAND_ALIASES = [
34
93
  "session",
35
94
  "plan",
36
95
  "tools",
37
- "compose",
38
- "editor",
39
- "review",
40
- "gitdiff",
41
96
  "quit",
42
97
  "logout",
43
98
  ];
@@ -61,7 +116,6 @@ const HELP = `Commands:
61
116
  /help Show this help
62
117
  /status Show live session, model, permissions, plan state, and usage (alias: /session)
63
118
  /compact Summarise older history for the model; keeps the full transcript
64
- /plan [on|off] Toggle restricted read-only Plan Mode
65
119
  /permissions [ask-first|workspace|unrestricted|plan] Inspect or change this process's policy
66
120
  /new [title] Save this session and start a fresh session
67
121
  /resume [id] Switch to a saved session in this workspace
@@ -77,10 +131,10 @@ const HELP = `Commands:
77
131
  Anything else is sent to the model. Ctrl+C stops a running answer.
78
132
  `;
79
133
  function helpText(context) {
80
- const controls = context.inlineComposer
81
- ? `Rich TUI input: Enter sends; Ctrl+O inserts a newline; Ctrl+R searches prompt history; arrows move the cursor. Ctrl+C clears the current draft; Ctrl+D exits when it is empty.${context.permissionPolicy === undefined ? "" : " Shift+Tab cycles Plan → Auto mode → Ask for me at the idle prompt."}\n\n`
134
+ const controls = context.tui
135
+ ? `Rich TUI input: Enter sends; Ctrl+O inserts a newline; ↑/↓ cycles prompt history; arrows move the cursor. Ctrl+C clears the current draft; Ctrl+D exits when it is empty.${context.permissionPolicy === undefined ? "" : " Shift+Tab cycles Plan → Auto mode → Ask for me at the idle prompt."}\n\n`
82
136
  : "";
83
- const exit = context.inlineComposer ? "" : "At the simple prompt Ctrl+C or Ctrl+D exits.\n";
137
+ const exit = context.tui ? "" : "At the simple prompt Ctrl+C or Ctrl+D exits.\n";
84
138
  const extras = context.extensionCommands ?? [];
85
139
  if (extras.length === 0) {
86
140
  return `${controls}${HELP}${exit}`;
@@ -205,22 +259,6 @@ export function handleSlashCommand(line, context) {
205
259
  context.write(formatDiffPage(patch, page, context.width ?? 80, context.color === true).text);
206
260
  return "handled";
207
261
  }
208
- case "/compose":
209
- context.write(context.inlineComposer
210
- ? "The multiline editor is already active in this prompt. Enter sends; Ctrl+O inserts a newline.\n"
211
- : "Use /compose at the interactive prompt to enter multi-line mode.\n");
212
- return "handled";
213
- case "/editor":
214
- context.write(context.inlineComposer
215
- ? "The cursor-addressable multiline editor is already active in this prompt. Enter sends; Ctrl+O inserts a newline.\n"
216
- : "Use /editor in the rich interactive TUI to open the multiline editor.\n");
217
- return "handled";
218
- case "/review":
219
- context.write("Use /review in an interactive TUI to browse the most recent successful patch.\n");
220
- return "handled";
221
- case "/gitdiff":
222
- context.write("Use /gitdiff [--staged] in an interactive TUI to approve read-only Git diff.\n");
223
- return "handled";
224
262
  case "/clear":
225
263
  context.session.clear();
226
264
  context.write("Transcript cleared.\n");
@@ -350,13 +388,14 @@ function describeStatus(context) {
350
388
  * keeps an API key off the screen and out of the scrollback. The original
351
389
  * writer is always restored, even if reading throws.
352
390
  */
353
- async function withoutEcho(readline, read) {
391
+ async function withoutEcho(readline, read, echoWhile = () => false) {
354
392
  const internals = readline;
355
393
  const original = internals._writeToOutput?.bind(readline);
356
394
  if (original === undefined) {
357
395
  return read();
358
396
  }
359
- internals._writeToOutput = () => undefined;
397
+ internals._writeToOutput = (text) => { if (echoWhile())
398
+ original(text); };
360
399
  try {
361
400
  return await read();
362
401
  }
@@ -364,13 +403,12 @@ async function withoutEcho(readline, read) {
364
403
  internals._writeToOutput = original;
365
404
  }
366
405
  }
367
- /** Plain-REPL Provider chooser; only the API key itself is hidden. */
368
- async function readProviderChoice(readNextLine, write, providerIds, active) {
369
- write("Select a Provider for /login:\n");
370
- providerIds.forEach((id, index) => {
371
- write(` ${index + 1}. ${id}${id === active ? " (current)" : ""}\n`);
406
+ async function readSetupChoice(readNextLine, write, ids, active, title, label) {
407
+ write(`${title}\n`);
408
+ ids.forEach((id, index) => {
409
+ write(` ${index + 1}. ${label(id)}${id === active ? " (current)" : ""}\n`);
372
410
  });
373
- write("Provider (number or name, Enter cancels): ");
411
+ write("Choice (number or name, Enter cancels): ");
374
412
  const answer = await readNextLine();
375
413
  if (answer === undefined)
376
414
  return undefined;
@@ -378,17 +416,28 @@ async function readProviderChoice(readNextLine, write, providerIds, active) {
378
416
  if (choice.length === 0)
379
417
  return undefined;
380
418
  const number = Number(choice);
381
- if (Number.isInteger(number) && number >= 1 && number <= providerIds.length) {
382
- return providerIds[number - 1];
419
+ if (Number.isInteger(number) && number >= 1 && number <= ids.length) {
420
+ return ids[number - 1];
383
421
  }
384
- return providerIds.find((id) => id.toLowerCase() === choice.toLowerCase());
422
+ return ids.find((id) => id.toLowerCase() === choice.toLowerCase());
385
423
  }
386
424
  async function runSignIn(readline, readNextLine, write, signIn) {
387
- write(`API key for ${signIn.provider} (hidden, saved locally; Enter skips): `);
388
425
  // The key is read from the same line source the loop uses. readline's
389
426
  // question() competes with the loop's own iterator for input, so the answer
390
427
  // would be swallowed or never arrive.
391
- const answer = await withoutEcho(readline, readNextLine);
428
+ // The hidden readline answer must also be unavailable through the Up key.
429
+ const discardKey = (history) => { history.shift(); };
430
+ readline.once("history", discardKey);
431
+ let answer;
432
+ try {
433
+ answer = await withoutEcho(readline, () => {
434
+ write(`API key for ${signIn.provider} (hidden, saved locally; Enter skips): `);
435
+ return readNextLine();
436
+ });
437
+ }
438
+ finally {
439
+ readline.off("history", discardKey);
440
+ }
392
441
  write("\n");
393
442
  if (answer === undefined) {
394
443
  return;
@@ -417,20 +466,12 @@ export async function runRepl(options) {
417
466
  options.stderr.write(text);
418
467
  };
419
468
  const tuiInput = options.tui ? new TuiInput(options.input) : undefined;
420
- // Decode independently so readline uses normal editing for every key. Its
421
- // chunked-paste fast path can lose wrapped cursor rows or overwrite text
422
- // when inserting a paste in the middle of an existing draft.
423
- if (tuiInput !== undefined)
424
- emitKeypressEvents(tuiInput);
425
469
  const readline = createInterface({
426
470
  input: tuiInput ?? options.input,
427
471
  output: options.output,
428
472
  terminal: true,
429
473
  prompt: REPL_PROMPT,
430
474
  });
431
- const menu = tuiInput === undefined ? undefined : new InputMenu(readline, options.output);
432
- let dismissedLine;
433
- let refreshQueued = false;
434
475
  const menuPriority = ["status", "diff", "provider", "model", "new", "resume", "help"];
435
476
  const commands = [...BUILT_IN_COMMANDS, ...(options.extensionCommands ?? []).map((command) => command.name)]
436
477
  .filter((name, index, all) => all.indexOf(name) === index)
@@ -447,107 +488,20 @@ export async function runRepl(options) {
447
488
  const suffix = description === undefined ? "" : ` · ${description}`;
448
489
  return { value: name, label: `/${name}${suffix}` };
449
490
  });
450
- const menuItems = (line) => {
451
- if (line === "/provider" || line.startsWith("/provider ")) {
452
- const query = line.slice("/provider".length).trim().toLowerCase();
453
- return (options.providers?.providerIds() ?? []).filter((id) => id.includes(query))
454
- .map((id) => ({ value: `provider ${id}`, label: `${id === options.providers?.provider ? "●" : "○"} ${id}` }));
455
- }
456
- if (line.startsWith("/") && !/\s/.test(line)) {
457
- return commands.filter((item) => item.label.startsWith(line));
458
- }
459
- return [];
460
- };
461
- const refreshMenu = () => {
462
- if (menu === undefined || inputClosed || signingIn || controller !== undefined)
463
- return;
464
- const line = readline.line;
465
- menu.show(line === dismissedLine ? [] : menuItems(line), true);
466
- };
467
- const queueRefresh = () => {
468
- if (refreshQueued)
469
- return;
470
- refreshQueued = true;
471
- setImmediate(() => { refreshQueued = false; refreshMenu(); });
472
- };
473
- const replaceDraft = (value) => {
474
- menu?.reset();
475
- readline.write(null, { ctrl: true, name: "a" });
476
- readline.write(null, { ctrl: true, name: "k" });
477
- readline.write(value);
478
- };
479
491
  let controller;
492
+ let questionMode = false;
480
493
  let promptVisible = false;
481
494
  let inputClosed = false;
482
495
  let signingIn = false;
483
- let composing = false;
484
- let composeCancelled = false;
485
496
  let interrupted = false;
486
- if (tuiInput !== undefined) {
487
- tuiInput.setMenuControls(() => {
488
- menu?.clear();
489
- dismissedLine = undefined;
490
- }, (key) => {
491
- if (signingIn || controller !== undefined)
492
- return false;
493
- if (key === "enter" && !menu?.active && readline.line === "/provider" && options.providers !== undefined) {
494
- menu?.show(menuItems(readline.line));
495
- return true;
496
- }
497
- if (key === "tab" && !menu?.active) {
498
- const first = menuItems(readline.line)[0]?.value;
499
- if (first !== undefined) {
500
- replaceDraft(`/${first}${first === "model" || first === "endpoint" ? " " : ""}`);
501
- queueRefresh();
502
- return true;
503
- }
504
- }
505
- if (!menu?.active)
506
- return false;
507
- if (key === "up" || key === "down") {
508
- menu.move(key === "up" ? -1 : 1);
509
- return true;
510
- }
511
- if (key === "escape") {
512
- dismissedLine = readline.line;
513
- menu.clear();
514
- return true;
515
- }
516
- const chosen = menu.selection;
517
- if (chosen === undefined)
518
- return false;
519
- const replacement = `/${chosen}`;
520
- if (key === "tab") {
521
- replaceDraft(replacement + (chosen === "model" || chosen === "endpoint" ? " " : ""));
522
- queueRefresh();
523
- return true;
524
- }
525
- if (key === "enter") {
526
- replaceDraft(replacement);
527
- if (chosen === "provider" || chosen === "model" || chosen === "endpoint") {
528
- if (chosen !== "provider")
529
- readline.write(" ");
530
- queueRefresh();
531
- }
532
- else {
533
- tuiInput.markSubmitted();
534
- readline.write("\r");
535
- }
536
- return true;
537
- }
538
- return false;
539
- });
540
- tuiInput.on("keypress", queueRefresh);
541
- }
542
497
  const showPrompt = () => {
543
- if (inputClosed || options.inlineComposer)
498
+ if (inputClosed || tuiInput !== undefined)
544
499
  return;
545
500
  promptVisible = true;
546
501
  readline.prompt();
547
502
  };
548
- readline.on("line", () => { menu?.reset(); promptVisible = false; });
503
+ readline.on("line", () => { promptVisible = false; });
549
504
  readline.on("close", () => {
550
- menu?.reset();
551
505
  inputClosed = true;
552
506
  controller?.abort();
553
507
  if (options.tui && promptVisible) {
@@ -557,20 +511,9 @@ export async function runRepl(options) {
557
511
  }
558
512
  });
559
513
  readline.on("SIGINT", () => {
560
- if (composing) {
561
- composeCancelled = true;
562
- readline.write("\r");
563
- return;
564
- }
565
514
  if (controller === undefined) {
566
- if (options.tui) {
567
- // Erase wrapped drafts before removing the prompt on close.
568
- readline.write(null, { ctrl: true, name: "e" });
569
- readline.write(null, { ctrl: true, name: "u" });
570
- }
571
- else {
515
+ if (!options.tui)
572
516
  options.output.write("\n");
573
- }
574
517
  interrupted = true;
575
518
  readline.close();
576
519
  return;
@@ -580,49 +523,147 @@ export async function runRepl(options) {
580
523
  if (options.banner !== undefined) {
581
524
  write(options.banner);
582
525
  }
583
- showPrompt();
584
526
  const lines = readline[Symbol.asyncIterator]();
585
527
  const readNextLine = async () => {
586
528
  tuiInput?.acceptLine();
587
529
  const next = await lines.next();
588
530
  return next.done === true ? undefined : next.value;
589
531
  };
532
+ const questionPresenter = async (request, signal) => {
533
+ if (inputClosed || controller?.signal.aborted === true)
534
+ return { status: "unavailable", reason: "the local input channel is closed" };
535
+ options.liveRegion?.pause();
536
+ tuiInput?.setBusy(false);
537
+ questionMode = true;
538
+ const answers = [];
539
+ try {
540
+ for (let index = 0; index < request.questions.length; index += 1) {
541
+ const question = request.questions[index];
542
+ if (question === undefined)
543
+ continue;
544
+ if (signal?.aborted)
545
+ return { status: "cancelled" };
546
+ let answer;
547
+ for (;;) {
548
+ const width = Math.max(1, terminalWidth({ columns: options.output.columns }) - 1);
549
+ const line = (value) => { for (const part of wrapQuestionText(terminalText(value), width))
550
+ write(`${part}\n`); };
551
+ line(`Question ${index + 1}/${request.questions.length} · ${question.header}`);
552
+ line(question.question);
553
+ question.options?.forEach((option, optionIndex) => {
554
+ line(` ${optionIndex + 1}. ${option.label}${option.description === undefined ? "" : ` — ${option.description}`}`);
555
+ });
556
+ if (question.options === undefined)
557
+ line("Enter your answer.");
558
+ else if (question.multiple === true)
559
+ line(`Enter choice numbers separated by commas${question.allowFreeText === true ? "; use text:… to add a custom answer" : ""}.`);
560
+ else
561
+ line(`Enter a choice number${question.allowFreeText === true ? " or text:… for another answer" : ""}.`);
562
+ line(options.tui === true
563
+ ? "Type /cancel or press Esc to cancel this question. Ctrl+C cancels the run."
564
+ : "Type /cancel to cancel this question. Ctrl+C cancels the run.");
565
+ readline.setPrompt("answer> ");
566
+ readline.prompt();
567
+ let escaped = false;
568
+ const cancelOnEscape = () => { escaped = true; if (!inputClosed)
569
+ readline.write("\n"); };
570
+ tuiInput?.setEscapeHandler(cancelOnEscape);
571
+ const unblockOnAbort = () => { if (!inputClosed)
572
+ readline.write("\n"); };
573
+ signal?.addEventListener("abort", unblockOnAbort, { once: true });
574
+ try {
575
+ if (signal?.aborted)
576
+ return { status: "cancelled" };
577
+ const input = await readNextLine();
578
+ if (signal?.aborted)
579
+ return { status: "cancelled" };
580
+ if (input === undefined)
581
+ return { status: "unavailable", reason: "the local input channel closed before an answer arrived" };
582
+ if (escaped || input.trim().toLowerCase() === "/cancel")
583
+ return { status: "cancelled" };
584
+ answer = parseQuestionAnswer(question, input);
585
+ if (answer !== undefined)
586
+ break;
587
+ line("That answer does not match this question. Choose a listed number or enter a non-empty answer.");
588
+ }
589
+ finally {
590
+ tuiInput?.setEscapeHandler(undefined);
591
+ signal?.removeEventListener("abort", unblockOnAbort);
592
+ }
593
+ }
594
+ answers.push({ questionId: question.id, value: answer });
595
+ }
596
+ if (inputClosed)
597
+ return { status: "unavailable", reason: "the local input channel closed before the answer could be submitted" };
598
+ return { ...request, answers };
599
+ }
600
+ finally {
601
+ questionMode = false;
602
+ readline.setPrompt(REPL_PROMPT);
603
+ tuiInput?.setBusy(true);
604
+ options.liveRegion?.resume();
605
+ }
606
+ };
590
607
  // Approval and normal input share the same readline iterator.
591
608
  if (options.shellApproval !== undefined) {
592
609
  options.shellApproval.setHandler(async (command, signal, context) => {
593
610
  if (signal?.aborted)
594
- return false;
595
- menu?.clear();
611
+ return { approved: false, reason: "cancelled" };
612
+ let resizeListener;
596
613
  options.liveRegion?.pause();
597
614
  tuiInput?.setApprovalMode(true);
598
615
  tuiInput?.setBusy(false);
599
616
  try {
600
617
  const width = terminalWidth({ columns: options.output.columns });
601
618
  const wraps = context?.startsWith(WEB_SEARCH_APPROVAL_PREFIX) === true;
619
+ const reviewable = wraps || commandReviewable(command, width);
620
+ const approvalOutput = options.output;
621
+ let previewInvalidated = false;
622
+ const invalidateOnResize = () => {
623
+ if (approvalOutput.columns !== width)
624
+ previewInvalidated = true;
625
+ };
626
+ if (!wraps) {
627
+ resizeListener = invalidateOnResize;
628
+ options.output.on("resize", invalidateOnResize);
629
+ }
602
630
  write(approvalPreview(command, width, context ?? "Shell command", wraps ? { wrap: true } : {}));
603
- if (!wraps && !commandReviewable(command, width)) {
604
- write(" Denied: command cannot be fully reviewed at this width. Ask for a shorter command.\n");
605
- return false;
631
+ if (!reviewable) {
632
+ return { approved: false, reason: "unreviewable" };
606
633
  }
607
634
  readline.setPrompt("approve> ");
608
635
  readline.prompt();
609
636
  // Ctrl+C must unblock the pending line read, not leave the agent
610
637
  // stuck waiting for a reply to an approval that was cancelled.
611
638
  const unblockOnAbort = () => {
612
- readline.write("\n");
639
+ if (!inputClosed)
640
+ readline.write("\n");
613
641
  };
614
642
  signal?.addEventListener("abort", unblockOnAbort, { once: true });
615
643
  try {
616
644
  if (signal?.aborted)
617
- return false;
645
+ return { approved: false, reason: "cancelled" };
618
646
  const answer = await readNextLine();
619
- return answer?.trim().toLowerCase() === "y" && signal?.aborted !== true;
647
+ if (signal?.aborted)
648
+ return { approved: false, reason: "cancelled" };
649
+ if (answer === undefined)
650
+ return { approved: false, reason: "unavailable" };
651
+ if (answer.trim().toLowerCase() !== "y")
652
+ return { approved: false, reason: "user" };
653
+ const currentWidth = terminalWidth({ columns: options.output.columns });
654
+ if (!wraps && (previewInvalidated || currentWidth !== width || !commandReviewable(command, currentWidth))) {
655
+ write(`${fitLine(" Denied: resized; review again.", currentWidth - 1)}\n`);
656
+ return { approved: false, reason: "unreviewable" };
657
+ }
658
+ return { approved: true };
620
659
  }
621
660
  finally {
622
661
  signal?.removeEventListener("abort", unblockOnAbort);
623
662
  }
624
663
  }
625
664
  finally {
665
+ if (resizeListener !== undefined)
666
+ options.output.off("resize", resizeListener);
626
667
  readline.setPrompt(REPL_PROMPT);
627
668
  tuiInput?.setApprovalMode(false);
628
669
  tuiInput?.setBusy(true);
@@ -630,63 +671,10 @@ export async function runRepl(options) {
630
671
  }
631
672
  });
632
673
  }
633
- /** Modal composer retains readline editing, Unicode and native history. */
634
- const collectMultiline = async () => {
635
- const parts = [];
636
- let bytes = 0;
637
- composing = true;
638
- composeCancelled = false;
639
- tuiInput?.setComposeMode(true);
640
- menu?.clear();
641
- dismissedLine = undefined;
642
- readline.setPrompt(" │ ");
643
- write("\nCompose prompt · Enter adds a line · /send submits · /cancel discards\n");
644
- try {
645
- for (;;) {
646
- readline.prompt();
647
- const part = await readNextLine();
648
- if (tuiInput?.composeLimitExceeded) {
649
- write("Draft too large (64 KiB paste buffer). Nothing was sent.\n");
650
- return undefined;
651
- }
652
- if (composeCancelled || part === undefined || part.trim() === "/cancel") {
653
- write("Draft discarded.\n");
654
- return undefined;
655
- }
656
- if (part.trim() === "/send") {
657
- const message = parts.join("\n");
658
- if (message.trim().length === 0) {
659
- write("Empty draft discarded.\n");
660
- return undefined;
661
- }
662
- write(`Submitted ${parts.length} lines.\n`);
663
- return message;
664
- }
665
- bytes += Buffer.byteLength(part, "utf8") + 1;
666
- if (parts.length >= 200 || bytes > 64 * 1024) {
667
- write("Draft too large (200 lines / 64 KiB). Nothing was sent.\n");
668
- return undefined;
669
- }
670
- // //send and //cancel are literal lines instead of commands.
671
- parts.push(part.startsWith("//send") || part.startsWith("//cancel") ? part.slice(1) : part);
672
- }
673
- }
674
- finally {
675
- composing = false;
676
- composeCancelled = false;
677
- tuiInput?.setComposeMode(false);
678
- readline.setPrompt(REPL_PROMPT);
679
- }
680
- };
681
- /** Cursor-editable modal, with exclusive keyboard and paint ownership. */
682
- const collectEditor = async (isDefault = false) => {
683
- if (tuiInput === undefined) {
684
- write("The cursor editor requires the rich TUI in an interactive terminal. Use /compose here instead.\n");
674
+ /** Rounded inline composer with exclusive keyboard and paint ownership. */
675
+ const collectEditor = async () => {
676
+ if (tuiInput === undefined)
685
677
  return undefined;
686
- }
687
- menu?.clear();
688
- dismissedLine = undefined;
689
- composing = true;
690
678
  let resolveDraft;
691
679
  const pending = new Promise(resolve => { resolveDraft = resolve; });
692
680
  const output = options.output;
@@ -705,13 +693,11 @@ export async function runRepl(options) {
705
693
  return `${provider} · ${model}`;
706
694
  };
707
695
  const editor = new EditorController(output, resolveDraft, {
708
- submitOnEnter: isDefault,
709
696
  history: recent,
710
- slashCommands: commands.map(item => ({ value: item.value, label: item.label })),
711
- mouse: isDefault && options.tui === true && output.isTTY === true,
712
- color: isDefault && terminalColorEnabled(output.isTTY === true, process.env),
713
- ...(isDefault && options.tui ? { footerIdentity } : {}),
714
- ...(isDefault && options.tui && options.permissionPolicy !== undefined ? {
697
+ slashCommands: commands,
698
+ color: terminalColorEnabled(output.isTTY === true, process.env),
699
+ footerIdentity,
700
+ ...(options.permissionPolicy !== undefined ? {
715
701
  footerStatus: () => {
716
702
  if (activeSession.planMode)
717
703
  return "Plan · Shift+Tab Auto mode · /help";
@@ -749,8 +735,6 @@ export async function runRepl(options) {
749
735
  const result = await pending;
750
736
  if (editor.exitRequested.value)
751
737
  readline.close();
752
- if (!isDefault)
753
- write(result === undefined ? "Draft discarded.\n" : "Draft submitted.\n");
754
738
  return result;
755
739
  }
756
740
  finally {
@@ -758,7 +742,6 @@ export async function runRepl(options) {
758
742
  output.off("resize", resized);
759
743
  readline.off("close", closed);
760
744
  options.input.off("end", closed);
761
- composing = false;
762
745
  }
763
746
  };
764
747
  /** Read-only patch viewer, safe and separate from the model's tool calls. */
@@ -772,7 +755,6 @@ export async function runRepl(options) {
772
755
  write("Interactive patch review requires the rich TUI in an interactive terminal; use /diff instead.\n");
773
756
  return;
774
757
  }
775
- menu?.clear();
776
758
  let resolveReview;
777
759
  const pending = new Promise(resolve => { resolveReview = resolve; });
778
760
  const output = options.output;
@@ -792,16 +774,11 @@ export async function runRepl(options) {
792
774
  readline.off("close", closed);
793
775
  }
794
776
  };
795
- const inspectLiveGitDiff = async (line) => {
777
+ const inspectLiveGitDiff = async (mode) => {
796
778
  if (tuiInput === undefined) {
797
779
  write("Live Git review requires the rich TUI in an interactive terminal.\n");
798
780
  return;
799
781
  }
800
- if (line !== "/gitdiff" && line !== "/gitdiff --staged") {
801
- write("Usage: /gitdiff [--staged]\n");
802
- return;
803
- }
804
- const mode = line.endsWith("--staged") ? "staged" : "working";
805
782
  // Explicit user confirmation before starting any local Git process.
806
783
  const root = terminalText(activeSession.cwd);
807
784
  write(`\nRead-only local Git diff (${mode}) in:\n${root}\nNo network, hooks, external diff or file writes. Approve once? [y/N]\n`);
@@ -834,23 +811,16 @@ export async function runRepl(options) {
834
811
  write(`${error instanceof Error ? error.message : "Git diff unavailable"}\n`);
835
812
  }
836
813
  };
837
- /** `/login` must name its target before any hidden key prompt. */
838
- const chooseLoginProvider = async () => {
839
- const providers = options.providers;
840
- if (providers === undefined)
841
- return undefined;
842
- const ids = [...providers.providerIds()];
814
+ const chooseSetupOption = async (ids, active, title, label) => {
843
815
  if (ids.length === 0)
844
816
  return undefined;
845
817
  if (tuiInput === undefined) {
846
- return readProviderChoice(readNextLine, write, ids, providers.provider);
818
+ return readSetupChoice(readNextLine, write, ids, active, title, label);
847
819
  }
848
- menu?.clear();
849
- dismissedLine = undefined;
850
820
  let resolveChoice;
851
821
  const pending = new Promise(resolve => { resolveChoice = resolve; });
852
822
  const output = options.output;
853
- const picker = new ProviderPickerController(output, ids, providers.provider, resolveChoice);
823
+ const picker = new ProviderPickerController(output, ids, active, resolveChoice, { title, item: label });
854
824
  const resized = () => { picker.resized(); };
855
825
  const closed = () => { picker.cancel(); };
856
826
  tuiInput.setRawHandler(chunk => { picker.receive(chunk); });
@@ -868,6 +838,81 @@ export async function runRepl(options) {
868
838
  options.input.off("end", closed);
869
839
  }
870
840
  };
841
+ const chooseLoginProvider = (title = "Select a Provider for /login") => {
842
+ const providers = options.providers;
843
+ if (providers === undefined)
844
+ return Promise.resolve(undefined);
845
+ return chooseSetupOption(providers.providerIds(), providers.provider, title, providerPickerLabel);
846
+ };
847
+ const signInWithSubscription = async (target) => {
848
+ const signIn = options.signIn;
849
+ if (signIn?.oauth === undefined) {
850
+ write("OAuth sign-in is unavailable in this session.\n");
851
+ return;
852
+ }
853
+ controller = new AbortController();
854
+ tuiInput?.setBusy(true);
855
+ try {
856
+ write("Waiting for subscription sign-in. Ctrl+C cancels.\n");
857
+ const failure = await signIn.oauth(target, controller.signal);
858
+ write(failure ?? `Signed in to ${target} subscription for this session. Ready for your next prompt.\n`);
859
+ }
860
+ finally {
861
+ tuiInput?.setBusy(false);
862
+ controller = undefined;
863
+ }
864
+ };
865
+ const connectProvider = async (startup) => {
866
+ const { providers, signIn } = options;
867
+ if (providers === undefined || signIn === undefined)
868
+ return;
869
+ const wasReady = signIn.ready();
870
+ signingIn = true;
871
+ try {
872
+ const chosen = await chooseLoginProvider(startup ? "Connect a model · Select a Provider" : undefined);
873
+ if (chosen === undefined) {
874
+ write("No Provider selected; no key was stored.\n");
875
+ return;
876
+ }
877
+ const selected = await providers.select(chosen, "setup");
878
+ write(selected.message);
879
+ if (!selected.ok || (startup && signIn.ready()))
880
+ return;
881
+ for (const field of providers.missingSetup ?? []) {
882
+ write(field === "model" ? "Model ID (Enter cancels): " : "API endpoint URL (Enter cancels): ");
883
+ const value = (await readNextLine())?.trim();
884
+ if (!value)
885
+ return;
886
+ const configured = field === "model" ? await providers.changeModel(value, "setup") : await providers.changeEndpoint(value, "setup");
887
+ write(configured.message);
888
+ if (!configured.ok)
889
+ return;
890
+ }
891
+ if (startup && signIn.ready())
892
+ return;
893
+ if (signIn.oauth !== undefined && (chosen === "openai" || chosen === "xai")) {
894
+ const method = await chooseSetupOption(["key", "oauth"], "", `Connect ${chosen} · Choose a sign-in method`, (id) => id === "key" ? "Enter an API key" : chosen === "openai" ? "Sign in with ChatGPT" : "Sign in with xAI");
895
+ if (method === undefined)
896
+ return;
897
+ if (method === "oauth") {
898
+ await signInWithSubscription(chosen);
899
+ return;
900
+ }
901
+ }
902
+ await runSignIn(readline, readNextLine, write, signIn);
903
+ }
904
+ finally {
905
+ signingIn = false;
906
+ if (!wasReady && signIn.ready()) {
907
+ try {
908
+ await options.rememberProviderSetup?.();
909
+ }
910
+ catch (error) {
911
+ write(`Could not save model setup: ${error instanceof Error ? error.message : String(error)}\n`);
912
+ }
913
+ }
914
+ }
915
+ };
871
916
  const signInAfterProviderSelection = async () => {
872
917
  if (!options.tui || options.signIn === undefined)
873
918
  return;
@@ -892,9 +937,15 @@ export async function runRepl(options) {
892
937
  }
893
938
  };
894
939
  try {
940
+ if (options.offerProviderSetup && options.signIn?.ready() === false) {
941
+ await connectProvider(true);
942
+ if (!inputClosed && !options.signIn.ready())
943
+ write("Run /login to connect a model before sending a prompt.\n");
944
+ }
945
+ showPrompt();
895
946
  for (;;) {
896
- let line = options.inlineComposer && tuiInput !== undefined
897
- ? await collectEditor(true)
947
+ const line = tuiInput !== undefined
948
+ ? await collectEditor()
898
949
  : await readNextLine();
899
950
  if (line === undefined) {
900
951
  if (inputClosed)
@@ -905,51 +956,29 @@ export async function runRepl(options) {
905
956
  showPrompt();
906
957
  continue;
907
958
  }
908
- const fromComposer = line.trim() === "/compose";
909
- const fromEditor = line.trim() === "/editor";
910
- if (line.trim() === "/review" || line.trim() === "/diff patch") {
959
+ const trimmed = line.trim();
960
+ if (tuiInput !== undefined && trimmed === "/diff patch") {
911
961
  await inspectPatch();
912
962
  if (inputClosed)
913
963
  break;
914
964
  showPrompt();
915
965
  continue;
916
966
  }
917
- if (line.trim() === "/diff" || line.trim() === "/diff --staged" || line.trim().startsWith("/gitdiff")) {
918
- const gitDiffLine = line.trim().startsWith("/gitdiff")
919
- ? line.trim()
920
- : `/gitdiff${line.trim().slice("/diff".length)}`;
921
- await inspectLiveGitDiff(gitDiffLine);
967
+ if (tuiInput !== undefined && (trimmed === "/diff" || trimmed === "/diff --staged")) {
968
+ await inspectLiveGitDiff(trimmed.endsWith("--staged") ? "staged" : "working");
922
969
  if (inputClosed)
923
970
  break;
924
971
  showPrompt();
925
972
  continue;
926
973
  }
927
- if (fromComposer || fromEditor) {
928
- if (options.inlineComposer && tuiInput !== undefined) {
929
- write("The cursor-addressable multiline editor is already active in this prompt. Enter sends; Ctrl+O inserts a newline.\n");
930
- if (inputClosed)
931
- break;
932
- showPrompt();
933
- continue;
934
- }
935
- const draft = fromEditor ? await collectEditor() : await collectMultiline();
936
- if (draft === undefined) {
937
- if (inputClosed)
938
- break;
939
- showPrompt();
940
- continue;
941
- }
942
- line = draft;
943
- }
944
- if (options.inlineComposer && tuiInput !== undefined) {
974
+ if (tuiInput !== undefined) {
945
975
  const isOutputTTY = typeof options.output === "object" && options.output !== null && "isTTY" in options.output && options.output.isTTY === true;
946
- const useColor = options.tui === true && terminalColorEnabled(isOutputTTY, process.env);
976
+ const useColor = terminalColorEnabled(isOutputTTY, process.env);
947
977
  const prefix = useColor ? "\u001b[1m\u001b[2m›\u001b[0m " : "› ";
948
- const lines = line.split("\n");
949
- const formattedPrompt = lines.map((l, i) => `${i === 0 ? prefix : " "}${terminalText(l)}`).join("\n");
978
+ const formattedPrompt = line.split("\n").map((l, i) => `${i === 0 ? prefix : " "}${terminalText(l)}`).join("\n");
950
979
  write(`\n${formattedPrompt}\n\n`);
951
980
  }
952
- const outcome = fromComposer || fromEditor ? "not-a-command" : handleSlashCommand(line, {
981
+ const outcome = handleSlashCommand(line, {
953
982
  session: activeSession,
954
983
  write,
955
984
  width: terminalWidth({ columns: options.output.columns }),
@@ -969,7 +998,7 @@ export async function runRepl(options) {
969
998
  ...(options.webSearch === undefined ? {} : { webSearch: options.webSearch }),
970
999
  ...(options.permissions === undefined ? {} : { permissions: options.permissions }),
971
1000
  ...(options.permissionPolicy === undefined ? {} : { permissionPolicy: options.permissionPolicy }),
972
- ...(options.inlineComposer === true ? { inlineComposer: true } : {}),
1001
+ ...(options.tui === true ? { tui: true } : {}),
973
1002
  ...(tuiInput !== undefined ? { interactivePicker: true } : {}),
974
1003
  ...(options.sessionOptions === undefined ? {} : { sessionOptions: options.sessionOptions }),
975
1004
  });
@@ -1004,14 +1033,7 @@ export async function runRepl(options) {
1004
1033
  continue;
1005
1034
  }
1006
1035
  }
1007
- controller = new AbortController();
1008
- try {
1009
- const failure = await options.signIn.oauth(target, controller.signal);
1010
- write(failure ?? `Signed in to ${target} subscription for this session. API keys remain preferred on the next launch.\n`);
1011
- }
1012
- finally {
1013
- controller = undefined;
1014
- }
1036
+ await signInWithSubscription(target);
1015
1037
  showPrompt();
1016
1038
  continue;
1017
1039
  }
@@ -1028,30 +1050,12 @@ export async function runRepl(options) {
1028
1050
  continue;
1029
1051
  }
1030
1052
  }
1031
- else if (options.providers !== undefined) {
1032
- signingIn = true;
1033
- let chosen;
1034
- try {
1035
- chosen = await chooseLoginProvider();
1036
- }
1037
- finally {
1038
- signingIn = false;
1039
- }
1040
- if (chosen === undefined) {
1041
- write("No Provider selected; no key was stored.\n");
1042
- if (inputClosed)
1043
- break;
1044
- showPrompt();
1045
- continue;
1046
- }
1047
- const selected = await options.providers.select(chosen);
1048
- write(selected.message);
1049
- if (!selected.ok) {
1050
- if (inputClosed)
1051
- break;
1052
- showPrompt();
1053
- continue;
1054
- }
1053
+ else if (options.providers !== undefined && options.signIn !== undefined) {
1054
+ await connectProvider(false);
1055
+ if (inputClosed)
1056
+ break;
1057
+ showPrompt();
1058
+ continue;
1055
1059
  }
1056
1060
  if (options.signIn !== undefined) {
1057
1061
  signingIn = true;
@@ -1141,7 +1145,7 @@ export async function runRepl(options) {
1141
1145
  if (typeof outcome === "object" && outcome.kind === "plan") {
1142
1146
  try {
1143
1147
  await activeSession.setPlanMode(outcome.enabled);
1144
- write(`Plan Mode ${activeSession.planMode ? "on" : "off"}. ${activeSession.planMode ? "Only built-in read tools and update_plan may execute." : "Session capabilities restored."}\n`);
1148
+ write(`Plan Mode ${activeSession.planMode ? "on" : "off"}. ${activeSession.planMode ? "Built-in read tools, configured web_search, update_plan, and question may execute." : "Session capabilities restored."}\n`);
1145
1149
  }
1146
1150
  catch (error) {
1147
1151
  write(`Could not change Plan Mode: ${error instanceof Error ? error.message : String(error)}\n`);
@@ -1160,6 +1164,7 @@ export async function runRepl(options) {
1160
1164
  const next = await options.sessionOptions?.create?.(outcome.title);
1161
1165
  if (next === undefined)
1162
1166
  throw new Error("Session creation is unavailable.");
1167
+ await activeSession.close();
1163
1168
  activeSession = next;
1164
1169
  write(`Started a new session${next.title ? ` · ${next.title}` : ""}. Previous session saved.\n`);
1165
1170
  }
@@ -1182,6 +1187,7 @@ export async function runRepl(options) {
1182
1187
  const next = await options.sessionOptions?.resume?.(id);
1183
1188
  if (next === undefined)
1184
1189
  throw new Error(`Unknown session in this workspace: ${id}`);
1190
+ await activeSession.close();
1185
1191
  activeSession = next;
1186
1192
  write(`Resumed saved session${next.title ? ` · ${next.title}` : ""}.\n`);
1187
1193
  }
@@ -1224,9 +1230,9 @@ export async function runRepl(options) {
1224
1230
  tuiInput?.setBusy(true);
1225
1231
  try {
1226
1232
  const signal = controller.signal;
1227
- const prompt = () => activeSession.prompt(line, { signal });
1233
+ const prompt = () => activeSession.prompt(line, { signal, questionPresenter });
1228
1234
  if (options.tui)
1229
- await withoutEcho(readline, prompt);
1235
+ await withoutEcho(readline, prompt, () => questionMode);
1230
1236
  else
1231
1237
  await prompt();
1232
1238
  }
@@ -1245,6 +1251,7 @@ export async function runRepl(options) {
1245
1251
  options.shellApproval?.setHandler(undefined);
1246
1252
  readline.close();
1247
1253
  tuiInput?.dispose();
1254
+ await activeSession.close();
1248
1255
  }
1249
1256
  return interrupted ? 130 : 0;
1250
1257
  }