@causyne/agent 1.19.0 → 1.20.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 (128) hide show
  1. package/README.md +0 -18
  2. package/package.json +1 -1
  3. package/src/acp/client.js +2 -1
  4. package/src/acp/client.ts +2 -1
  5. package/src/acp/server.js +5 -6
  6. package/src/acp/server.ts +4 -6
  7. package/src/agentLoop.d.ts +1 -0
  8. package/src/agentLoop.js +505 -241
  9. package/src/agentLoop.ts +527 -258
  10. package/src/agentPolicy.js +9 -0
  11. package/src/agentPolicy.ts +9 -0
  12. package/src/approvalPolicy.js +55 -0
  13. package/src/approvalPolicy.ts +66 -0
  14. package/src/browser/browserRuntime.js +217 -14
  15. package/src/browser/browserRuntime.ts +255 -14
  16. package/src/browser/causyneBrowser.js +1215 -68
  17. package/src/browser/causyneBrowser.ts +1340 -72
  18. package/src/cliMode.js +66 -37
  19. package/src/commands.js +343 -72
  20. package/src/config.js +22 -12
  21. package/src/continuum/ingest.js +1 -1
  22. package/src/continuum/ingest.ts +1 -1
  23. package/src/continuum/ui/continuumPicker.js +18 -1
  24. package/src/continuum/ui/continuumPicker.ts +19 -1
  25. package/src/dynamicCatalogue.js +87 -20
  26. package/src/dynamicCatalogue.ts +85 -15
  27. package/src/launchIntent.js +40 -0
  28. package/src/launchIntent.ts +49 -0
  29. package/src/memoryManager.js +36 -8
  30. package/src/modelCatalogue.js +2 -0
  31. package/src/planCache.js +320 -11
  32. package/src/planCache.ts +402 -13
  33. package/src/providers/acp.js +67 -1
  34. package/src/providers/acp.ts +65 -2
  35. package/src/providers/antigravity.d.ts +5 -1
  36. package/src/providers/antigravity.js +643 -32
  37. package/src/providers/antigravity.ts +671 -33
  38. package/src/providers/base.d.ts +9 -0
  39. package/src/providers/factory.js +2 -0
  40. package/src/providers/pi.js +157 -0
  41. package/src/providers/pi.ts +144 -0
  42. package/src/provisioning/capabilityProvisioner.js +17 -0
  43. package/src/provisioning/capabilityTools.js +1 -1
  44. package/src/sandbox.js +30 -39
  45. package/src/sessionJournal.js +66 -10
  46. package/src/sessionJournal.ts +84 -10
  47. package/src/taskShape.js +9 -9
  48. package/src/taskShape.ts +9 -9
  49. package/src/timeMachine.js +124 -0
  50. package/src/timeMachine.ts +141 -0
  51. package/src/toolPacks.js +4 -10
  52. package/src/tools/archive.js +196 -59
  53. package/src/tools/archive.ts +220 -0
  54. package/src/tools/browserAutomator.js +78 -9
  55. package/src/tools/browserAutomator.ts +90 -9
  56. package/src/tools/desktopOpen.js +114 -31
  57. package/src/tools/desktopOpen.ts +144 -0
  58. package/src/tools/gitExec.js +5 -3
  59. package/src/tools/nativeEngine.js +4 -3
  60. package/src/tools/nativeEngine.ts +5 -3
  61. package/src/tools/openSourceToolProvisioner.js +5 -4
  62. package/src/tools/openSourceToolProvisioner.ts +5 -4
  63. package/src/tools/registry.js +3 -0
  64. package/src/tools/screenshot.js +55 -71
  65. package/src/tools/screenshot.ts +70 -0
  66. package/src/tools/sysRun.js +284 -117
  67. package/src/tools/sysRun.ts +347 -0
  68. package/src/tools/toolSelfRepair.js +109 -2
  69. package/src/tools/toolSelfRepair.ts +100 -2
  70. package/src/tools/updatePlan.js +49 -1
  71. package/src/tools/updatePlan.ts +43 -1
  72. package/src/tools/viewImage.js +93 -78
  73. package/src/tools/viewImage.ts +116 -0
  74. package/src/tools/webFetch.js +20 -6
  75. package/src/tools/webFetch.ts +26 -6
  76. package/src/tui/block.d.ts +1 -0
  77. package/src/tui/chatRenderer.d.ts +21 -0
  78. package/src/tui/chatRenderer.js +118 -45
  79. package/src/tui/clipboardSync.js +28 -1
  80. package/src/tui/clipboardSync.ts +26 -1
  81. package/src/tui/codeProgress.js +299 -0
  82. package/src/tui/codeProgress.ts +304 -0
  83. package/src/tui/codeStreamRenderer.js +116 -0
  84. package/src/tui/codeStreamRenderer.ts +124 -0
  85. package/src/tui/commandPresentation.js +11 -0
  86. package/src/tui/commandPresentation.ts +18 -0
  87. package/src/tui/completion.js +45 -13
  88. package/src/tui/composerControls.js +66 -0
  89. package/src/tui/composerControls.ts +78 -0
  90. package/src/tui/diff.d.ts +2 -0
  91. package/src/tui/home.js +4 -2
  92. package/src/tui/homeTransition.js +21 -0
  93. package/src/tui/homeTransition.ts +27 -0
  94. package/src/tui/homeView.js +62 -0
  95. package/src/tui/homeView.ts +91 -0
  96. package/src/tui/layout.js +46 -0
  97. package/src/tui/layout.ts +52 -0
  98. package/src/tui/messageActionPlacement.js +16 -0
  99. package/src/tui/messageActionPlacement.ts +25 -0
  100. package/src/tui/messageInteraction.js +221 -0
  101. package/src/tui/messageInteraction.ts +275 -0
  102. package/src/tui/modelPicker.js +13 -0
  103. package/src/tui/modelPicker.ts +12 -0
  104. package/src/tui/mouseHover.js +6 -0
  105. package/src/tui/mouseHover.ts +5 -0
  106. package/src/tui/prompt.js +199 -52
  107. package/src/tui/resizeRecovery.js +15 -0
  108. package/src/tui/resizeRecovery.ts +27 -0
  109. package/src/tui/scrollInteraction.js +97 -0
  110. package/src/tui/scrollInteraction.ts +189 -0
  111. package/src/tui/sessionLayout.js +12 -0
  112. package/src/tui/sessionLayout.ts +12 -0
  113. package/src/tui/sessionPicker.js +280 -0
  114. package/src/tui/sessionPicker.ts +364 -0
  115. package/src/tui/sessionSidebar.js +78 -0
  116. package/src/tui/sessionSidebar.ts +95 -0
  117. package/src/tui/setup.js +1 -1
  118. package/src/tui/setup.ts +1 -1
  119. package/src/tui/stageSurface.js +128 -57
  120. package/src/tui/statusLine.js +5 -5
  121. package/src/tui/text.d.ts +5 -0
  122. package/src/tui/textLayout.js +23 -0
  123. package/src/tui/textLayout.ts +28 -0
  124. package/src/tui/viewport.js +204 -279
  125. package/src/tui/viewport.ts +326 -0
  126. package/src/types/contracts.ts +54 -0
  127. package/src/utils/paths.js +5 -4
  128. package/src/utils/paths.ts +5 -5
package/src/tui/prompt.js CHANGED
@@ -29,6 +29,12 @@ import { panelRows } from './overlay.js';
29
29
  import { commandPalette, skillPicker, ApprovalLayer, DecisionLayer, ConfirmLayer, HomeLayer, TextLayer } from './dialogs.js';
30
30
  import { describeTool } from './toolDisplay.js';
31
31
  import { readClipboardSync } from './clipboardSync.js';
32
+ import { handleViewportWheel, handleScrollbarMouse } from './scrollInteraction.js';
33
+ import { centerRows, sessionRows, dialogWidth, dialogHeight } from './layout.js';
34
+ import { settleStageResize } from './resizeRecovery.js';
35
+ import { sidebarVisible, sessionMainWidth } from './sessionSidebar.js';
36
+ import { isStartupDialog, startupDialogView } from './homeView.js';
37
+ import { handleMessageMouse, handleMessageActionMenuMouse, isMouseMotion } from './messageInteraction.js';
32
38
 
33
39
  const CTRL_C_WINDOW_MS = 2000;
34
40
 
@@ -93,6 +99,11 @@ export class ChatPrompt {
93
99
  this.surface = options.surface || null;
94
100
  this.onExit = options.onExit || (() => process.exit(0));
95
101
  this.onExpand = options.onExpand || (() => {});
102
+ this.onOpenMode = options.onOpenMode || null;
103
+ this.onOpenModel = options.onOpenModel || null;
104
+ this.onOpenVariant = options.onOpenVariant || null;
105
+ this.messageActionLoop = options.messageActionLoop || null;
106
+ this.getMode = options.getMode || (() => 'B');
96
107
 
97
108
  this.editor = new LineEditor();
98
109
  this.history = options.history || new PromptHistory();
@@ -126,9 +137,8 @@ export class ChatPrompt {
126
137
 
127
138
  // Commands for the palette, and the dialog that currently owns the keyboard.
128
139
  //
129
- // A dialog is not a floating window here — it is rows of the live region, the
130
- // way openclaw draws its pickers. There is no cell grid to overlay onto and
131
- // no way to dim what is already in the user's scrollback.
140
+ // Dialogs use the live stage as a centered temporary view and restore the
141
+ // transcript when dismissed. Terminal scrollback is not repainted.
132
142
  this.commands = options.commands || [];
133
143
 
134
144
  // Is this line a COMMAND rather than a message?
@@ -147,6 +157,7 @@ export class ChatPrompt {
147
157
  this.notice = '';
148
158
  this.redrawScheduled = false;
149
159
  this.resizeTimer = null;
160
+ this.suspendedSurface = null;
150
161
  }
151
162
 
152
163
  // -------------------------------------------------------------------------
@@ -202,7 +213,7 @@ export class ChatPrompt {
202
213
  * instead of once per pixel.
203
214
  */
204
215
  handleResize = () => {
205
- this.erase();
216
+ if (!this.resizeTimer) this.erase();
206
217
  // The Stage owns its own geometry and cannot repaint a band whose height it
207
218
  // no longer knows. Resizing it here, before the settle timer, would do it
208
219
  // once per pixel of a drag; it is done in the settled callback below.
@@ -219,14 +230,19 @@ export class ChatPrompt {
219
230
  // A stage that can no longer fit gives the band back and hands the
220
231
  // session to the scrollback renderer rather than painting into a region
221
232
  // too short to hold a viewport.
222
- if (this.surface && !this.surface.resize()) this.surface = null;
233
+ const settled = settleStageResize(this.surface, this.suspendedSurface);
234
+ this.surface = settled.current;
235
+ this.suspendedSurface = settled.suspended;
236
+ if (settled.restored) this.attachSurface(settled.current);
223
237
  this.draw();
224
238
  }, RESIZE_SETTLE_MS);
225
239
  if (this.resizeTimer.unref) this.resizeTimer.unref();
226
240
  };
227
241
 
228
242
  handleData = (chunk) => {
243
+ let motionOnly = true;
229
244
  for (const key of this.decoder.decode(chunk)) {
245
+ if (key.name !== 'mouse' || !isMouseMotion(key.sequence)) motionOnly = false;
230
246
  try {
231
247
  this.handleKey(key);
232
248
  } catch (e) {
@@ -235,7 +251,8 @@ export class ChatPrompt {
235
251
  process.stderr.write(`\n[tui] key "${key.name}" failed: ${e.message}\n`);
236
252
  }
237
253
  }
238
- this.draw();
254
+ // All-motion chunks repaint only when the hovered message actually changes.
255
+ if (!motionOnly) this.draw();
239
256
  };
240
257
 
241
258
  /**
@@ -261,12 +278,17 @@ export class ChatPrompt {
261
278
  // uses in full-screen mode, which reports status instead of animating it.
262
279
  // Reuse that and make the status a row of the live region.
263
280
  this.renderer = renderer;
281
+ if (this.surface) this.surface.sidebarStatus = renderer.status;
264
282
  renderer.quietStatus = true;
265
283
  renderer.onStatusChange = (message) => {
266
284
  this.statusMessage = message;
267
285
  this.scheduleDraw();
268
286
  };
269
- renderer.onPlanChange = () => this.scheduleDraw();
287
+ renderer.onPlanChange = (plan) => {
288
+ if (this.surface) this.surface.sidebarPlan = plan;
289
+ this.scheduleDraw();
290
+ return this.surface && sidebarVisible(this.surface.width) ? 'sidebar' : undefined;
291
+ };
270
292
  }
271
293
 
272
294
  /**
@@ -315,17 +337,20 @@ export class ChatPrompt {
315
337
  const status = this.renderer && this.renderer.status;
316
338
  if (!status) return null;
317
339
 
318
- const mode = this.busy ? 'Working' : 'Build';
340
+ const mode = `${this.busy ? 'Working' : 'Build'} ${this.getMode()}`;
319
341
  const parts = [bold(paint('accent', mode))];
320
342
  if (status.model) parts.push(bold(paint('ink', status.model)));
321
343
  const line = parts.join(dim(` ${GLYPHS.dot} `));
322
344
  const trailing = [status.provider, status.tier].filter(Boolean).join(' ');
323
- const full = trailing ? `${line} ${dim(trailing)}` : line;
345
+ const base = trailing ? `${line} ${dim(trailing)}` : line;
346
+ const hints = dim(' Ctrl+G mode · Alt+M model · Ctrl+T variant');
347
+ const full = visibleWidth(base) + visibleWidth(hints) <= width ? base + hints : base;
324
348
  return visibleWidth(full) > width ? truncate(full, width, GLYPHS.ellipsis) : full;
325
349
  }
326
350
 
327
351
  /** Persistent checklist shown outside the chat transcript. */
328
352
  planRows(width) {
353
+ if (this.surface && sidebarVisible(this.surface.width)) return [];
329
354
  const plan = this.renderer && Array.isArray(this.renderer.latestPlan) ? this.renderer.latestPlan : [];
330
355
  if (plan.length === 0) return [];
331
356
  const completed = plan.filter(item => item.status === 'completed').length;
@@ -422,6 +447,17 @@ export class ChatPrompt {
422
447
  this.scheduleDraw();
423
448
  }
424
449
 
450
+ /** Update active model and provider displayed in agentRow and status footer. */
451
+ updateModel(provider, model) {
452
+ if (this.renderer && this.renderer.status) {
453
+ this.renderer.status.setSession({
454
+ provider: provider ? String(provider) : '',
455
+ model: model ? String(model) : ''
456
+ });
457
+ }
458
+ this.scheduleDraw();
459
+ }
460
+
425
461
  /**
426
462
  * Browse the compiled skill library.
427
463
  *
@@ -444,7 +480,7 @@ export class ChatPrompt {
444
480
 
445
481
  /** Show local command output inside the live region, never in chat scrollback. */
446
482
  showText(title, text, options = {}) {
447
- const panelWidth = Math.max(28, Math.min(this.width() - 2, options.width || 76));
483
+ const panelWidth = dialogWidth(this.width(), options.width || 76);
448
484
  const contentWidth = Math.max(12, panelWidth - 4);
449
485
  const lines = wrapBlock(safeDisplay(String(text == null ? '' : text)), contentWidth);
450
486
  return this.openLayer(new TextLayer({
@@ -510,10 +546,7 @@ export class ChatPrompt {
510
546
  * count and starts eating the conversation above.
511
547
  */
512
548
  width() {
513
- // The Stage may ask for a narrower measure — it does so only for the opening
514
- // composition, while there is no transcript yet, so the input box lines up
515
- // with the banner above it instead of stretching past it. Once the session
516
- // starts it hands back the full width and nothing below here changes.
549
+ // The Stage keeps the composer at one readable measure through the session.
517
550
  if (this.surface && typeof this.surface.composeWidth === 'function') {
518
551
  const asked = this.surface.composeWidth();
519
552
  if (Number.isFinite(asked) && asked > 0) return Math.max(20, asked);
@@ -585,6 +618,23 @@ export class ChatPrompt {
585
618
  this.editor.clear();
586
619
  this.completion = null;
587
620
 
621
+ // Control commands take priority over busy turns so the user is never locked out.
622
+ // Commands like /stop, /new, /model, /session, /sessions, /continuum, /clear can interrupt
623
+ // a stalled/long-running turn and execute immediately.
624
+ const isControlCommand = this.isCommand(text) && (
625
+ text === '/stop' || text === '/interrupt' ||
626
+ text.startsWith('/new') ||
627
+ text.startsWith('/model') ||
628
+ text.startsWith('/session') ||
629
+ text.startsWith('/sessions') ||
630
+ text.startsWith('/continuum') ||
631
+ text === '/clear'
632
+ );
633
+
634
+ if (isControlCommand && this.busy) {
635
+ this.interrupt();
636
+ }
637
+
588
638
  if (this.busy) {
589
639
  // The turn is still running: hold the line rather than dropping it.
590
640
  this.queue.push(text);
@@ -603,22 +653,13 @@ export class ChatPrompt {
603
653
  return;
604
654
  }
605
655
 
606
- // Echo what was sent as a BLOCK, so the transcript reads as a conversation.
607
- //
608
- // A `▸ ` prefix made the user's own words indistinguishable from a tool row
609
- // at a glance. The reference shows the user's message as its own left-barred,
610
- // filled block (screenshot 15.02.21), which is the seam that lets the eye
611
- // find where each exchange begins when scrolling back through a long turn.
612
- // width - 1, not width. A block built to the FULL terminal width fills the
613
- // final column, the terminal wraps it, and one drawn row becomes two real
614
- // ones — so erase() walks back half as far as it should and leaves the
615
- // remainder on screen. That is why a six-line message came back as six
616
- // stray fragments of its own tail. barBlock honours whatever width it is
617
- // given; reserving the column is the caller's job.
618
- this.commit('user', (width) => {
656
+ // Render live submissions through the same path used when history reflows.
657
+ if (this.surface?.homeInfo) this.surface.leaveHome();
658
+ if (this.renderer?.renderUserMessage) this.renderer.renderUserMessage(text);
659
+ else this.commit('user', (width) => {
619
660
  const blockWidth = Math.max(8, width - 1);
620
661
  return barBlock(
621
- wrapBlock(text, Math.max(8, blockWidth - visibleWidth(GLYPHS.bar) - GUTTER)),
662
+ wrapBlock(safeDisplay(text), Math.max(8, blockWidth - visibleWidth(GLYPHS.bar) - GUTTER)),
622
663
  { width: blockWidth, role: 'user', filled: true, surface: 'surfaceHi' }
623
664
  );
624
665
  });
@@ -660,7 +701,12 @@ export class ChatPrompt {
660
701
  // This MUST route before any default key handling (like paste) so modal
661
702
  // dialogs (e.g. InputLayer for API keys) receive bracketed paste events.
662
703
  if (this.layer) {
663
- const action = this.layer.key(key);
704
+ const previousSelection = this.layer.messageActions ? this.layer.list?.selected : null;
705
+ const action = key.name === 'mouse' && this.layer.messageActions
706
+ ? handleMessageActionMenuMouse(this.layer, key)
707
+ : this.layer.key(key);
708
+ if (this.layer?.messageActions && isMouseMotion(key.sequence)
709
+ && previousSelection !== this.layer.list?.selected) this.scheduleDraw();
664
710
  if (action === 'close') { this.closeLayer(); return; }
665
711
  // A layer may hand a key BACK rather than swallow it. The home menu does
666
712
  // this for every printable character, so `↑↓` picks an action while typing
@@ -682,6 +728,22 @@ export class ChatPrompt {
682
728
  if (this.completion && this.handleCompletionKey(key)) return;
683
729
  if (this.handleScrollKey(key)) return;
684
730
 
731
+ const control = key.ctrl && key.name === 'g' ? this.onOpenMode
732
+ : key.alt && key.name === 'm' ? this.onOpenModel
733
+ : key.ctrl && key.name === 't' ? this.onOpenVariant : null;
734
+ if (control) {
735
+ if (this.busy) {
736
+ this.notice = 'Finish or interrupt the current turn before switching controls';
737
+ this.scheduleDraw();
738
+ } else {
739
+ Promise.resolve(control()).catch(error => {
740
+ this.notice = error instanceof Error ? error.message : String(error);
741
+ this.scheduleDraw();
742
+ });
743
+ }
744
+ return;
745
+ }
746
+
685
747
  switch (key.name) {
686
748
  case 'escape': return this.handleEscape();
687
749
  case 'enter':
@@ -758,24 +820,19 @@ export class ChatPrompt {
758
820
  // fall through would put `mouse` or `wheel` into the switch below, where a
759
821
  // future case could act on it by accident — and, worse, a terminal that
760
822
  // kept reporting after a failed teardown would type into the user's line.
761
- if (key.name === 'mouse') return true;
823
+ if (key.name === 'mouse') {
824
+ if (this.messageActionLoop && isMouseMotion(key.sequence)) {
825
+ return handleMessageMouse(this, this.messageActionLoop, key);
826
+ }
827
+ if (handleScrollbarMouse(key, viewport, this.surface, this)) return true;
828
+ if (!this.layer && this.messageActionLoop) return handleMessageMouse(this, this.messageActionLoop, key);
829
+ return true;
830
+ }
762
831
  if (key.name === 'wheel') {
763
832
  if (!viewport || !key.wheel) return true;
833
+ if (this.renderer?.scrollCodePanel?.(key, this.surface)) return true;
764
834
 
765
- viewport.wheel(key.wheel.direction);
766
-
767
- // At the top of the band, an upward wheel belongs to the TERMINAL — the
768
- // conversation continues above us in its own scrollback. Releasing mouse
769
- // reporting is the only way to give it back; the bytes for this event are
770
- // already consumed, so the release takes effect from the next notch.
771
- if (!viewport.lastWheelMoved && key.wheel.direction === 'up'
772
- && this.surface && typeof this.surface.releaseMouse === 'function') {
773
- if (this.surface.releaseMouse()) {
774
- this.notice = 'top of this view — scrolling with the terminal now; press any key to come back';
775
- this.draw();
776
- }
777
- }
778
- return true;
835
+ return handleViewportWheel(key, viewport, this.surface, this);
779
836
  }
780
837
 
781
838
  if (!viewport) return false;
@@ -880,7 +937,14 @@ export class ChatPrompt {
880
937
  }
881
938
 
882
939
  handleEscape() {
883
- if (this.completion) { this.completion = null; return; }
940
+ if (this.completion) {
941
+ this.completion = null;
942
+ if (this.editor.getText().startsWith('/')) {
943
+ this.editor.clear();
944
+ }
945
+ this.scheduleDraw();
946
+ return;
947
+ }
884
948
  if (this.busy) return this.interrupt();
885
949
  this.editor.clear();
886
950
  }
@@ -1004,7 +1068,57 @@ export class ChatPrompt {
1004
1068
  handleCompletionKey(key) {
1005
1069
  if (key.name === 'up') { this.moveSelection(-1); return true; }
1006
1070
  if (key.name === 'down') { this.moveSelection(1); return true; }
1071
+ if (key.name === 'mouse') {
1072
+ if (key.mouse && key.mouse.pressed && key.mouse.button === 0) {
1073
+ let item = null;
1074
+ if (this.pickerLayout && Number.isFinite(key.mouse.row)) {
1075
+ const stageHeight = this.surface?.stage?.height || process.stdout.rows || 24;
1076
+ const promptTop = Math.max(1, stageHeight - this.pickerLayout.totalPromptRows + 1);
1077
+ const pickerTop = promptTop + this.pickerLayout.pickerStart;
1078
+ const pickerBottom = pickerTop + this.pickerLayout.pickerCount - 1;
1079
+
1080
+ if (key.mouse.row >= pickerTop && key.mouse.row <= pickerBottom) {
1081
+ const index = key.mouse.row - pickerTop;
1082
+ item = this.completion.items && this.completion.items[index];
1083
+ } else {
1084
+ // Click outside picker closes it
1085
+ this.completion = null;
1086
+ this.scheduleDraw();
1087
+ return true;
1088
+ }
1089
+ } else {
1090
+ item = this.completion.items && this.completion.items[this.selected];
1091
+ }
1092
+
1093
+ if (item) {
1094
+ if (this.completion.kind === 'command') {
1095
+ this.editor.clear();
1096
+ this.completion = null;
1097
+ this.deliver(item.value);
1098
+ return true;
1099
+ }
1100
+ if (this.completion.kind === 'command-argument') {
1101
+ const cmd = this.completion.command;
1102
+ this.editor.clear();
1103
+ this.completion = null;
1104
+ this.deliver(`/${cmd} ${item.value}`);
1105
+ return true;
1106
+ }
1107
+ this.acceptCompletion();
1108
+ return true;
1109
+ }
1110
+ }
1111
+ return true;
1112
+ }
1007
1113
  if (key.name === 'enter' && !key.alt) {
1114
+ const item = this.completion.items && this.completion.items[this.selected];
1115
+ if (this.completion.kind === 'command' && item) {
1116
+ // Instant execution on single Enter for any slash command
1117
+ this.editor.clear();
1118
+ this.completion = null;
1119
+ this.deliver(item.value);
1120
+ return true;
1121
+ }
1008
1122
  if (this.completionIsExact()) {
1009
1123
  // Nothing left to complete. Close the picker and let the key through to
1010
1124
  // submit, rather than re-offering what is already on the line.
@@ -1015,7 +1129,14 @@ export class ChatPrompt {
1015
1129
  return true;
1016
1130
  }
1017
1131
  if (key.name === 'tab') { this.acceptCompletion(); return true; }
1018
- if (key.name === 'escape') { this.completion = null; return true; }
1132
+ if (key.name === 'escape') {
1133
+ this.completion = null;
1134
+ if (this.editor.getText().startsWith('/')) {
1135
+ this.editor.clear();
1136
+ }
1137
+ this.scheduleDraw();
1138
+ return true;
1139
+ }
1019
1140
  return false;
1020
1141
  }
1021
1142
 
@@ -1102,6 +1223,7 @@ export class ChatPrompt {
1102
1223
  }
1103
1224
 
1104
1225
  draw() {
1226
+ if (this.resizeTimer) return;
1105
1227
  if (!this.active) return;
1106
1228
  this.erase();
1107
1229
 
@@ -1165,7 +1287,13 @@ export class ChatPrompt {
1165
1287
  punctuate: true
1166
1288
  });
1167
1289
 
1290
+ const planRows = this.planRows(width);
1168
1291
  const pickerRows = this.completion ? this.pickerRows(width) : [];
1292
+ this.pickerLayout = this.completion ? {
1293
+ pickerStart: planRows.length + boxRows.length,
1294
+ pickerCount: pickerRows.length,
1295
+ totalPromptRows: 0
1296
+ } : null;
1169
1297
  const noticeRows = this.notice ? [dim(` ${this.notice}`)] : [];
1170
1298
 
1171
1299
  // And a row of air between the box and the footer. The picker and a notice
@@ -1175,13 +1303,16 @@ export class ChatPrompt {
1175
1303
  const status = this.statusRow(width);
1176
1304
  const gap = status.length > 0 ? new Array(FOOTER_GAP).fill('') : [];
1177
1305
 
1178
- const planRows = this.planRows(width);
1179
1306
  const all = this.clampRows(
1180
1307
  planRows.concat(boxRows, pickerRows, noticeRows, gap, status), width
1181
1308
  );
1309
+ if (this.pickerLayout) this.pickerLayout.totalPromptRows = all.length;
1182
1310
 
1183
1311
  // Park the cursor where the user is typing, not at the end of the picker.
1184
- this.present(all, planRows.length + caretRow + padTop, layout.cursorCol + inset);
1312
+ const positioned = this.surface
1313
+ ? (this.surface.homeInfo ? centerRows(all, this.surface.width) : sessionRows(all, sessionMainWidth(this.surface.width)))
1314
+ : { rows: all, inset: 0 };
1315
+ this.present(positioned.rows, planRows.length + caretRow + padTop, layout.cursorCol + inset + positioned.inset);
1185
1316
  }
1186
1317
 
1187
1318
  /**
@@ -1198,7 +1329,8 @@ export class ChatPrompt {
1198
1329
  if (this.surface) {
1199
1330
  this.drawnRows = 0;
1200
1331
  this.cursorRow = 0;
1201
- this.surface.present(all, { row: caretRow, col: caretCol });
1332
+ this.surface.present(all, { row: caretRow, col: caretCol,
1333
+ modal: Boolean(this.layer), inline: Boolean(this.layer && !isStartupDialog(this.surface)) });
1202
1334
  return;
1203
1335
  }
1204
1336
 
@@ -1241,14 +1373,29 @@ export class ChatPrompt {
1241
1373
 
1242
1374
  /** The live region while a dialog is up: the panel, then the status row. */
1243
1375
  drawLayer(width) {
1244
- const panelWidth = Math.max(24, Math.min(width - 1, this.layer.width || 64));
1245
- const maxHeight = Math.max(6, Math.min(this.layer.preferredHeight(), 20));
1376
+ if (this.layer.messageActions && this.surface?.presentMessageActions) {
1377
+ const availableWidth = sessionMainWidth(this.surface.width);
1378
+ const panelWidth = dialogWidth(Math.max(8, availableWidth - 3), this.layer.width || 68);
1379
+ const panel = panelRows(this.layer, panelWidth, this.layer.preferredHeight());
1380
+ this.surface.presentMessageActions(panel, this.layer);
1381
+ return;
1382
+ }
1383
+ if (isStartupDialog(this.surface)) {
1384
+ const view = startupDialogView(this.layer, this.surface.width, this.surface.stage.maxHeight);
1385
+ return this.present(view.rows, view.caretRow, view.caretCol);
1386
+ }
1387
+ const inline = this.surface && !this.surface.homeInfo;
1388
+ const availableWidth = inline ? sessionMainWidth(this.surface.width) : width;
1389
+ const panelWidth = dialogWidth(availableWidth, this.layer.width || 64);
1390
+ const maxHeight = dialogHeight(this.surface?.stage?.maxHeight || this.out?.rows, this.layer.preferredHeight());
1246
1391
  const panel = panelRows(this.layer, panelWidth, maxHeight);
1247
1392
  const all = this.clampRows(panel.concat(this.statusRow(width)), width);
1248
1393
 
1249
1394
  // panelRows() called body(), which is where a layer publishes its cursor.
1250
1395
  const cursor = this.layer.cursor;
1251
- this.present(all, cursor ? 1 + cursor.y : 0, cursor ? 2 + cursor.x : 0);
1396
+ const centered = this.surface ? centerRows(all, availableWidth) : { rows: all, inset: 0 };
1397
+ this.present(centered.rows, cursor ? 1 + cursor.y : 0,
1398
+ (cursor ? 2 + cursor.x : 0) + centered.inset);
1252
1399
  }
1253
1400
 
1254
1401
  pickerRows(width) {
@@ -0,0 +1,15 @@
1
+ /** A temporary small terminal must not permanently discard the live surface. */
2
+ export function settleStageResize(current, suspended) {
3
+ if (current) {
4
+ return current.resize()
5
+ ? { current, suspended: null, restored: false }
6
+ : { current: null, suspended: current, restored: false };
7
+ }
8
+ if (suspended) {
9
+ suspended.resize();
10
+ if (suspended.stage.maxHeight > 0 && suspended.open()) {
11
+ return { current: suspended, suspended: null, restored: true };
12
+ }
13
+ }
14
+ return { current: null, suspended, restored: false };
15
+ }
@@ -0,0 +1,27 @@
1
+ export interface ResizableStage {
2
+ stage: { maxHeight: number };
3
+ resize(): boolean;
4
+ open(): boolean;
5
+ }
6
+
7
+ export interface ResizeState<T extends ResizableStage> {
8
+ current: T | null;
9
+ suspended: T | null;
10
+ restored: boolean;
11
+ }
12
+
13
+ /** A temporary small terminal must not permanently discard the live surface. */
14
+ export function settleStageResize<T extends ResizableStage>(current: T | null, suspended: T | null): ResizeState<T> {
15
+ if (current) {
16
+ return current.resize()
17
+ ? { current, suspended: null, restored: false }
18
+ : { current: null, suspended: current, restored: false };
19
+ }
20
+ if (suspended) {
21
+ suspended.resize();
22
+ if (suspended.stage.maxHeight > 0 && suspended.open()) {
23
+ return { current: suspended, suspended: null, restored: true };
24
+ }
25
+ }
26
+ return { current: null, suspended, restored: false };
27
+ }
@@ -0,0 +1,97 @@
1
+ // Sovereign TUI Scroll & Mouse Isolation Controller
2
+ //
3
+ // Implements OpenCode-compliant viewport containment:
4
+ // 1. Mouse wheel events are strictly confined to the chat box viewport.
5
+ // 2. Overscrolling past the top (0) or bottom (maxScrollTop) saturates cleanly.
6
+ // 3. Mouse reporting is NEVER released to the terminal during an active session,
7
+ // preventing terminal scrollback desync and UI freezes.
8
+ // 4. Scrollbar (rail) mouse interactions clamp cleanly even if clicked above or
9
+ // below the chat box bounds.
10
+ /**
11
+ * Handle mouse wheel scrolling with full viewport containment.
12
+ * Replaces the buggy overscroll release with clean boundary saturation.
13
+ */
14
+ export function handleViewportWheel(key, viewport, surface, host) {
15
+ if (!viewport || !key.wheel)
16
+ return true;
17
+ // Auto-heal: If the surface was ever detached, immediately re-capture mouse
18
+ // so the session stays responsive and never freezes.
19
+ if (surface && typeof surface.captureMouse === 'function' && surface.detached) {
20
+ surface.captureMouse();
21
+ if (host.notice.startsWith('top of this view')) {
22
+ host.notice = '';
23
+ }
24
+ }
25
+ // Pure isolated scrolling: clamp strictly inside [0, maxScrollTop]
26
+ viewport.wheel(key.wheel.direction === 'down' ? 'down' : 'up');
27
+ // Immediately draw updated viewport
28
+ host.draw();
29
+ return true;
30
+ }
31
+ /**
32
+ * Handle mouse clicks on the scrollbar rail.
33
+ * Safely clamps coordinates even when clicking above or below the chat box.
34
+ */
35
+ export function handleScrollbarMouse(key, viewport, surface, host) {
36
+ if (!viewport || !surface || !key.mouse || !key.mouse.pressed)
37
+ return false;
38
+ if (key.mouse.button !== 0)
39
+ return false; // Left click only
40
+ // If completion picker is active, check if click was on the picker
41
+ if (host.pickerLayout && Number.isFinite(key.mouse.y)) {
42
+ const termRows = host.out?.rows || 24;
43
+ const stageH = surface.stage?.height || termRows;
44
+ const promptTop = Math.max(1, stageH - host.pickerLayout.totalPromptRows + 1);
45
+ const pickerTop = promptTop + host.pickerLayout.pickerStart - 1;
46
+ const pickerBottom = pickerTop + host.pickerLayout.pickerCount;
47
+ if (key.mouse.y >= pickerTop && key.mouse.y <= pickerBottom) {
48
+ return false; // Leave to completion handler
49
+ }
50
+ }
51
+ const termRows = host.out?.rows || 24;
52
+ const stageH = surface.stage?.height || termRows;
53
+ const stageTop = Math.max(0, termRows - stageH);
54
+ const viewportH = viewport.height;
55
+ const stageW = typeof surface.stage?.width === 'function' ? surface.stage.width() : (surface.width || 80);
56
+ const hasRail = typeof surface.railEnabled === 'function' ? surface.railEnabled(stageW) : true;
57
+ const contentW = hasRail ? Math.max(20, stageW - 3) : stageW;
58
+ const mouseX = key.mouse.x;
59
+ const mouseY = key.mouse.y;
60
+ // Rail occupies column contentW - 1 through stageW
61
+ const isRailColumn = mouseX >= contentW - 1 && mouseX <= stageW;
62
+ if (isRailColumn) {
63
+ // Auto-heal if detached
64
+ if (typeof surface.captureMouse === 'function' && surface.detached) {
65
+ surface.captureMouse();
66
+ if (host.notice.startsWith('top of this view'))
67
+ host.notice = '';
68
+ }
69
+ // Boundary clamping:
70
+ // Clicks preceding/above chat box clamp to top (0)
71
+ if (mouseY < stageTop) {
72
+ viewport.scrollTo(0);
73
+ host.draw();
74
+ return true;
75
+ }
76
+ // Clicks below viewport clamp to bottom (maxScrollTop) and resume following
77
+ if (mouseY >= stageTop + viewportH) {
78
+ viewport.toBottom();
79
+ host.draw();
80
+ return true;
81
+ }
82
+ // Clicks on rail inside viewport: compute proportional scroll
83
+ const relY = mouseY - stageTop;
84
+ const fraction = viewportH > 1 ? relY / (viewportH - 1) : 0;
85
+ const clampedFraction = Math.max(0, Math.min(1, fraction));
86
+ const target = Math.round(clampedFraction * viewport.maxScrollTop());
87
+ if (target >= viewport.maxScrollTop()) {
88
+ viewport.toBottom();
89
+ }
90
+ else {
91
+ viewport.scrollTo(target);
92
+ }
93
+ host.draw();
94
+ return true;
95
+ }
96
+ return false;
97
+ }