@causyne/agent 1.20.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 (125) 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 +52 -29
  19. package/src/commands.js +141 -17
  20. package/src/config.js +22 -12
  21. package/src/continuum/ui/continuumPicker.js +18 -1
  22. package/src/continuum/ui/continuumPicker.ts +19 -1
  23. package/src/dynamicCatalogue.js +87 -20
  24. package/src/dynamicCatalogue.ts +85 -15
  25. package/src/launchIntent.js +40 -0
  26. package/src/launchIntent.ts +49 -0
  27. package/src/memoryManager.js +26 -2
  28. package/src/modelCatalogue.js +2 -0
  29. package/src/planCache.js +320 -11
  30. package/src/planCache.ts +402 -13
  31. package/src/providers/acp.js +67 -1
  32. package/src/providers/acp.ts +65 -2
  33. package/src/providers/antigravity.d.ts +5 -1
  34. package/src/providers/antigravity.js +643 -32
  35. package/src/providers/antigravity.ts +671 -33
  36. package/src/providers/base.d.ts +9 -0
  37. package/src/providers/factory.js +2 -0
  38. package/src/providers/pi.js +157 -0
  39. package/src/providers/pi.ts +144 -0
  40. package/src/provisioning/capabilityProvisioner.js +17 -0
  41. package/src/provisioning/capabilityTools.js +1 -1
  42. package/src/sandbox.js +30 -39
  43. package/src/sessionJournal.js +53 -10
  44. package/src/sessionJournal.ts +64 -10
  45. package/src/taskShape.js +9 -9
  46. package/src/taskShape.ts +9 -9
  47. package/src/timeMachine.js +124 -0
  48. package/src/timeMachine.ts +141 -0
  49. package/src/toolPacks.js +4 -10
  50. package/src/tools/archive.js +196 -59
  51. package/src/tools/archive.ts +220 -0
  52. package/src/tools/browserAutomator.js +78 -9
  53. package/src/tools/browserAutomator.ts +90 -9
  54. package/src/tools/desktopOpen.js +114 -31
  55. package/src/tools/desktopOpen.ts +144 -0
  56. package/src/tools/gitExec.js +5 -3
  57. package/src/tools/nativeEngine.js +4 -3
  58. package/src/tools/nativeEngine.ts +5 -3
  59. package/src/tools/openSourceToolProvisioner.js +5 -4
  60. package/src/tools/openSourceToolProvisioner.ts +5 -4
  61. package/src/tools/registry.js +3 -0
  62. package/src/tools/screenshot.js +55 -71
  63. package/src/tools/screenshot.ts +70 -0
  64. package/src/tools/sysRun.js +284 -117
  65. package/src/tools/sysRun.ts +347 -0
  66. package/src/tools/toolSelfRepair.js +109 -2
  67. package/src/tools/toolSelfRepair.ts +100 -2
  68. package/src/tools/updatePlan.js +49 -1
  69. package/src/tools/updatePlan.ts +43 -1
  70. package/src/tools/viewImage.js +93 -78
  71. package/src/tools/viewImage.ts +116 -0
  72. package/src/tools/webFetch.js +20 -6
  73. package/src/tools/webFetch.ts +26 -6
  74. package/src/tui/block.d.ts +1 -0
  75. package/src/tui/chatRenderer.d.ts +21 -0
  76. package/src/tui/chatRenderer.js +34 -47
  77. package/src/tui/clipboardSync.js +28 -1
  78. package/src/tui/clipboardSync.ts +26 -1
  79. package/src/tui/codeProgress.js +299 -0
  80. package/src/tui/codeProgress.ts +304 -0
  81. package/src/tui/codeStreamRenderer.js +116 -0
  82. package/src/tui/codeStreamRenderer.ts +124 -0
  83. package/src/tui/commandPresentation.js +11 -0
  84. package/src/tui/commandPresentation.ts +18 -0
  85. package/src/tui/completion.js +45 -13
  86. package/src/tui/composerControls.js +66 -0
  87. package/src/tui/composerControls.ts +78 -0
  88. package/src/tui/diff.d.ts +2 -0
  89. package/src/tui/home.js +4 -2
  90. package/src/tui/homeTransition.js +21 -0
  91. package/src/tui/homeTransition.ts +27 -0
  92. package/src/tui/homeView.js +62 -0
  93. package/src/tui/homeView.ts +91 -0
  94. package/src/tui/layout.js +46 -0
  95. package/src/tui/layout.ts +52 -0
  96. package/src/tui/messageActionPlacement.js +16 -0
  97. package/src/tui/messageActionPlacement.ts +25 -0
  98. package/src/tui/messageInteraction.js +221 -0
  99. package/src/tui/messageInteraction.ts +275 -0
  100. package/src/tui/modelPicker.js +4 -24
  101. package/src/tui/modelPicker.ts +4 -20
  102. package/src/tui/mouseHover.js +6 -0
  103. package/src/tui/mouseHover.ts +5 -0
  104. package/src/tui/prompt.js +152 -76
  105. package/src/tui/resizeRecovery.js +15 -0
  106. package/src/tui/resizeRecovery.ts +27 -0
  107. package/src/tui/scrollInteraction.js +97 -0
  108. package/src/tui/scrollInteraction.ts +189 -0
  109. package/src/tui/sessionLayout.js +12 -0
  110. package/src/tui/sessionLayout.ts +12 -0
  111. package/src/tui/sessionPicker.js +111 -16
  112. package/src/tui/sessionPicker.ts +121 -16
  113. package/src/tui/sessionSidebar.js +78 -0
  114. package/src/tui/sessionSidebar.ts +95 -0
  115. package/src/tui/setup.js +1 -1
  116. package/src/tui/setup.ts +1 -1
  117. package/src/tui/stageSurface.js +120 -58
  118. package/src/tui/text.d.ts +5 -0
  119. package/src/tui/textLayout.js +23 -0
  120. package/src/tui/textLayout.ts +28 -0
  121. package/src/tui/viewport.js +204 -279
  122. package/src/tui/viewport.ts +326 -0
  123. package/src/types/contracts.ts +54 -0
  124. package/src/utils/paths.js +5 -4
  125. 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;
@@ -455,7 +480,7 @@ export class ChatPrompt {
455
480
 
456
481
  /** Show local command output inside the live region, never in chat scrollback. */
457
482
  showText(title, text, options = {}) {
458
- const panelWidth = Math.max(28, Math.min(this.width() - 2, options.width || 76));
483
+ const panelWidth = dialogWidth(this.width(), options.width || 76);
459
484
  const contentWidth = Math.max(12, panelWidth - 4);
460
485
  const lines = wrapBlock(safeDisplay(String(text == null ? '' : text)), contentWidth);
461
486
  return this.openLayer(new TextLayer({
@@ -521,10 +546,7 @@ export class ChatPrompt {
521
546
  * count and starts eating the conversation above.
522
547
  */
523
548
  width() {
524
- // The Stage may ask for a narrower measure — it does so only for the opening
525
- // composition, while there is no transcript yet, so the input box lines up
526
- // with the banner above it instead of stretching past it. Once the session
527
- // 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.
528
550
  if (this.surface && typeof this.surface.composeWidth === 'function') {
529
551
  const asked = this.surface.composeWidth();
530
552
  if (Number.isFinite(asked) && asked > 0) return Math.max(20, asked);
@@ -631,22 +653,13 @@ export class ChatPrompt {
631
653
  return;
632
654
  }
633
655
 
634
- // Echo what was sent as a BLOCK, so the transcript reads as a conversation.
635
- //
636
- // A `▸ ` prefix made the user's own words indistinguishable from a tool row
637
- // at a glance. The reference shows the user's message as its own left-barred,
638
- // filled block (screenshot 15.02.21), which is the seam that lets the eye
639
- // find where each exchange begins when scrolling back through a long turn.
640
- // width - 1, not width. A block built to the FULL terminal width fills the
641
- // final column, the terminal wraps it, and one drawn row becomes two real
642
- // ones — so erase() walks back half as far as it should and leaves the
643
- // remainder on screen. That is why a six-line message came back as six
644
- // stray fragments of its own tail. barBlock honours whatever width it is
645
- // given; reserving the column is the caller's job.
646
- 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) => {
647
660
  const blockWidth = Math.max(8, width - 1);
648
661
  return barBlock(
649
- wrapBlock(text, Math.max(8, blockWidth - visibleWidth(GLYPHS.bar) - GUTTER)),
662
+ wrapBlock(safeDisplay(text), Math.max(8, blockWidth - visibleWidth(GLYPHS.bar) - GUTTER)),
650
663
  { width: blockWidth, role: 'user', filled: true, surface: 'surfaceHi' }
651
664
  );
652
665
  });
@@ -688,7 +701,12 @@ export class ChatPrompt {
688
701
  // This MUST route before any default key handling (like paste) so modal
689
702
  // dialogs (e.g. InputLayer for API keys) receive bracketed paste events.
690
703
  if (this.layer) {
691
- 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();
692
710
  if (action === 'close') { this.closeLayer(); return; }
693
711
  // A layer may hand a key BACK rather than swallow it. The home menu does
694
712
  // this for every printable character, so `↑↓` picks an action while typing
@@ -710,6 +728,22 @@ export class ChatPrompt {
710
728
  if (this.completion && this.handleCompletionKey(key)) return;
711
729
  if (this.handleScrollKey(key)) return;
712
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
+
713
747
  switch (key.name) {
714
748
  case 'escape': return this.handleEscape();
715
749
  case 'enter':
@@ -786,24 +820,19 @@ export class ChatPrompt {
786
820
  // fall through would put `mouse` or `wheel` into the switch below, where a
787
821
  // future case could act on it by accident — and, worse, a terminal that
788
822
  // kept reporting after a failed teardown would type into the user's line.
789
- 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
+ }
790
831
  if (key.name === 'wheel') {
791
832
  if (!viewport || !key.wheel) return true;
833
+ if (this.renderer?.scrollCodePanel?.(key, this.surface)) return true;
792
834
 
793
- viewport.wheel(key.wheel.direction);
794
-
795
- // At the top of the band, an upward wheel belongs to the TERMINAL — the
796
- // conversation continues above us in its own scrollback. Releasing mouse
797
- // reporting is the only way to give it back; the bytes for this event are
798
- // already consumed, so the release takes effect from the next notch.
799
- if (!viewport.lastWheelMoved && key.wheel.direction === 'up'
800
- && this.surface && typeof this.surface.releaseMouse === 'function') {
801
- if (this.surface.releaseMouse()) {
802
- this.notice = 'top of this view — scrolling with the terminal now; press any key to come back';
803
- this.draw();
804
- }
805
- }
806
- return true;
835
+ return handleViewportWheel(key, viewport, this.surface, this);
807
836
  }
808
837
 
809
838
  if (!viewport) return false;
@@ -1039,36 +1068,57 @@ export class ChatPrompt {
1039
1068
  handleCompletionKey(key) {
1040
1069
  if (key.name === 'up') { this.moveSelection(-1); return true; }
1041
1070
  if (key.name === 'down') { this.moveSelection(1); return true; }
1042
- if (key.name === 'enter' && !key.alt) {
1043
- const item = this.completion.items && this.completion.items[this.selected];
1044
- if (this.completion.kind === 'command' && item) {
1045
- if (item.value === '/model' || item.value === '/sessions' || item.value === '/session' || item.value === '/new' || item.value === '/continuum') {
1046
- this.editor.clear();
1047
- this.completion = null;
1048
- this.deliver(item.value);
1049
- return true;
1050
- }
1051
- }
1052
- if (this.completion.kind === 'command-argument' && item) {
1053
- if (this.completion.command === 'model') {
1054
- this.editor.clear();
1055
- this.completion = null;
1056
- this.deliver(`/model ${item.value}`);
1057
- return true;
1058
- }
1059
- if (this.completion.command === 'sessions' || this.completion.command === 'session') {
1060
- this.editor.clear();
1061
- this.completion = null;
1062
- this.deliver(`/sessions ${item.value}`);
1063
- 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];
1064
1091
  }
1065
- if (this.completion.command === 'continuum') {
1066
- this.editor.clear();
1067
- this.completion = null;
1068
- this.deliver(`/continuum ${item.value}`);
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();
1069
1108
  return true;
1070
1109
  }
1071
1110
  }
1111
+ return true;
1112
+ }
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
+ }
1072
1122
  if (this.completionIsExact()) {
1073
1123
  // Nothing left to complete. Close the picker and let the key through to
1074
1124
  // submit, rather than re-offering what is already on the line.
@@ -1173,6 +1223,7 @@ export class ChatPrompt {
1173
1223
  }
1174
1224
 
1175
1225
  draw() {
1226
+ if (this.resizeTimer) return;
1176
1227
  if (!this.active) return;
1177
1228
  this.erase();
1178
1229
 
@@ -1236,7 +1287,13 @@ export class ChatPrompt {
1236
1287
  punctuate: true
1237
1288
  });
1238
1289
 
1290
+ const planRows = this.planRows(width);
1239
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;
1240
1297
  const noticeRows = this.notice ? [dim(` ${this.notice}`)] : [];
1241
1298
 
1242
1299
  // And a row of air between the box and the footer. The picker and a notice
@@ -1246,13 +1303,16 @@ export class ChatPrompt {
1246
1303
  const status = this.statusRow(width);
1247
1304
  const gap = status.length > 0 ? new Array(FOOTER_GAP).fill('') : [];
1248
1305
 
1249
- const planRows = this.planRows(width);
1250
1306
  const all = this.clampRows(
1251
1307
  planRows.concat(boxRows, pickerRows, noticeRows, gap, status), width
1252
1308
  );
1309
+ if (this.pickerLayout) this.pickerLayout.totalPromptRows = all.length;
1253
1310
 
1254
1311
  // Park the cursor where the user is typing, not at the end of the picker.
1255
- 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);
1256
1316
  }
1257
1317
 
1258
1318
  /**
@@ -1269,7 +1329,8 @@ export class ChatPrompt {
1269
1329
  if (this.surface) {
1270
1330
  this.drawnRows = 0;
1271
1331
  this.cursorRow = 0;
1272
- 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)) });
1273
1334
  return;
1274
1335
  }
1275
1336
 
@@ -1312,14 +1373,29 @@ export class ChatPrompt {
1312
1373
 
1313
1374
  /** The live region while a dialog is up: the panel, then the status row. */
1314
1375
  drawLayer(width) {
1315
- const panelWidth = Math.max(24, Math.min(width - 1, this.layer.width || 64));
1316
- 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());
1317
1391
  const panel = panelRows(this.layer, panelWidth, maxHeight);
1318
1392
  const all = this.clampRows(panel.concat(this.statusRow(width)), width);
1319
1393
 
1320
1394
  // panelRows() called body(), which is where a layer publishes its cursor.
1321
1395
  const cursor = this.layer.cursor;
1322
- 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);
1323
1399
  }
1324
1400
 
1325
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
+ }