@causyne/agent 1.20.30 → 1.20.32

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 (41) hide show
  1. package/package.json +1 -1
  2. package/src/agentLoop.js +8 -0
  3. package/src/agentLoop.ts +7 -0
  4. package/src/appChannel.js +52 -74
  5. package/src/appChannel.ts +106 -0
  6. package/src/creative/apps.js +206 -11
  7. package/src/creative/apps.ts +198 -6
  8. package/src/creative/liveBridge.js +1481 -0
  9. package/src/creative/liveBridge.ts +1829 -0
  10. package/src/creative/pipeline.js +31 -3
  11. package/src/creative/pipeline.ts +36 -7
  12. package/src/decideTools.js +3 -1
  13. package/src/decideTools.ts +3 -1
  14. package/src/decisionPolicy.js +5 -2
  15. package/src/decisionPolicy.ts +7 -1
  16. package/src/mcp/mcpRegistry.js +3 -1
  17. package/src/mcp/mcpRegistry.ts +3 -1
  18. package/src/sandbox.js +5 -1
  19. package/src/smartToolsUse.js +2 -1
  20. package/src/smartToolsUse.ts +2 -1
  21. package/src/tool-scripts/resolve_automator.py +167 -34
  22. package/src/toolPacks.js +5 -3
  23. package/src/toolPacks.ts +5 -3
  24. package/src/tools/creativeApps.js +226 -7
  25. package/src/tools/creativeApps.ts +233 -7
  26. package/src/tools/memoryWrite.js +71 -89
  27. package/src/tools/memoryWrite.ts +108 -0
  28. package/src/tools/resolveAutomator.js +356 -89
  29. package/src/tools/resolveAutomator.ts +403 -0
  30. package/src/toolsList.js +1 -1
  31. package/src/toolsList.ts +1 -1
  32. package/src/tui/consoleGuard.js +85 -0
  33. package/src/tui/consoleGuard.ts +102 -0
  34. package/src/tui/messageActionPlacement.js +8 -3
  35. package/src/tui/messageActionPlacement.ts +8 -3
  36. package/src/tui/messageInteraction.js +184 -27
  37. package/src/tui/messageInteraction.ts +221 -45
  38. package/src/tui/mouseHover.js +4 -0
  39. package/src/tui/mouseHover.ts +5 -0
  40. package/src/tui/smartLinkClick.js +353 -0
  41. package/src/tui/smartLinkClick.ts +413 -0
@@ -1,13 +1,17 @@
1
- import { getActiveSessionId, getSession } from '../sessionJournal.js';
1
+ import { getActiveSessionId, getSession, listSessions, saveSessionMessages } from '../sessionJournal.js';
2
2
  import { revokeAllAlways } from '../approvalGrants.js';
3
+ // @ts-expect-error Legacy checkpoints module has no TypeScript declarations yet.
4
+ import { undoLast } from '../checkpoints.js';
3
5
  import { writeClipboardSync } from './clipboardSync.js';
6
+ import { registerStageConsoleGuard, setTransientNotice } from './consoleGuard.js';
7
+ import { detectLinkAtPoint, openDetectedLink } from './smartLinkClick.js';
4
8
  import { sidebarVisible, sessionMainWidth } from './sessionSidebar.js';
5
- import { truncate, padEnd } from './text.js';
9
+ import { stripAnsi, truncate, padEnd } from './text.js';
6
10
  // @ts-expect-error Legacy overlay has no declaration yet.
7
11
  import { Layer } from './overlay.js';
8
12
  // @ts-expect-error Legacy theme has no declaration yet.
9
13
  import { paint, bold, fill } from './theme.js';
10
- /** Two actions need two rows; the generic searchable picker hides the second one. */
14
+ /** Multi-action message menu overlay supporting Copy, Fork, and Revert. */
11
15
  export class MessageActionsLayer extends Layer {
12
16
  messageActions = true;
13
17
  actionLayout = null;
@@ -39,8 +43,11 @@ export class MessageActionsLayer extends Layer {
39
43
  });
40
44
  }
41
45
  key(event) {
42
- if (event.name === 'escape')
46
+ if (event.name === 'escape') {
47
+ this.settled = true;
48
+ this.onPick(null);
43
49
  return 'close';
50
+ }
44
51
  if (event.name === 'up' || event.name === 'down') {
45
52
  const delta = event.name === 'up' ? -1 : 1;
46
53
  this.list.moveTo((this.list.selected + delta + this.items.length) % this.items.length);
@@ -51,8 +58,7 @@ export class MessageActionsLayer extends Layer {
51
58
  this.onPick(this.items[this.list.selected] ?? null);
52
59
  return 'close';
53
60
  }
54
- const e = event;
55
- if (!e.ctrl && !e.alt && (e.name.length === 1 || e.name === 'space' || e.name === 'backspace')) {
61
+ if (!event.ctrl && !event.alt && (event.name.length === 1 || event.name === 'space' || event.name === 'backspace')) {
56
62
  this.settled = true;
57
63
  this.onPick(null);
58
64
  return 'dismiss';
@@ -83,8 +89,25 @@ export function handleMessageMouse(host, loop, event) {
83
89
  // SGR motion carries bit 32; it must update hover without opening the menu.
84
90
  if (isMouseMotion(event.sequence))
85
91
  return true;
86
- if (mouse.button === 0 && mouse.pressed && marker)
87
- openMessageActions(host, loop, marker, mouse.y);
92
+ if (mouse.button === 0 && mouse.pressed) {
93
+ // Smart link detection: if the user clicked (or Cmd/Ctrl+Clicked) on a URL or OSC 8 hyperlink,
94
+ // open the link directly in the system browser instead of popping up the Message Actions modal.
95
+ const hitLink = detectLinkAtPoint(surface, mouse, marker);
96
+ if (hitLink) {
97
+ openDetectedLink(hitLink.url);
98
+ writeClipboardSync(hitLink.url);
99
+ surface.hoveredMessageFrameId = null;
100
+ setTransientNotice(host, `Opened ${hitLink.url}`);
101
+ return true;
102
+ }
103
+ // Never open Message Actions on a modifier click (Ctrl/Alt/Shift+Click) that missed a link by 1 cell.
104
+ if (event.ctrl || event.alt || event.shift) {
105
+ return true;
106
+ }
107
+ if (marker) {
108
+ openMessageActions(host, loop, marker, mouse.y);
109
+ }
110
+ }
88
111
  return true;
89
112
  }
90
113
  const isMessage = (marker) => (marker.kind === 'user' || marker.kind === 'assistant')
@@ -118,8 +141,9 @@ export function messageAt(surface, point) {
118
141
  }
119
142
  return null;
120
143
  }
121
- /** Highlight only the visible rows of the hovered message, without altering transcript data. */
144
+ /** Highlight only the visible non-empty rows of the hovered message, without altering transcript data. */
122
145
  export function markHoveredMessage(rows, surface, contentWidth) {
146
+ registerStageConsoleGuard(surface);
123
147
  const id = surface.hoveredMessageFrameId;
124
148
  if (!id)
125
149
  return;
@@ -130,15 +154,32 @@ export function markHoveredMessage(rows, surface, contentWidth) {
130
154
  const visible = Math.max(0, Math.min(rows.length, flat.rows.length - surface.viewport.scrollTop));
131
155
  const padding = rows.length - visible;
132
156
  const next = flat.markers[flat.markers.indexOf(marker) + 1];
133
- const first = Math.max(marker.row, surface.viewport.scrollTop);
134
- const last = Math.min(next?.row ?? flat.rows.length, surface.viewport.scrollTop + visible);
157
+ const rawStart = marker.row;
158
+ const rawEnd = next?.row ?? flat.rows.length;
159
+ // Skip leading and trailing blank separator lines inside the message frame
160
+ // so we never paint a stray '▸' on an empty spacing row above '▸ Causyne'.
161
+ let firstContentRow = rawStart;
162
+ while (firstContentRow < rawEnd && stripAnsi(flat.rows[firstContentRow] ?? '').trim() === '') {
163
+ firstContentRow++;
164
+ }
165
+ let lastContentRow = rawEnd - 1;
166
+ while (lastContentRow > firstContentRow && stripAnsi(flat.rows[lastContentRow] ?? '').trim() === '') {
167
+ lastContentRow--;
168
+ }
169
+ if (firstContentRow >= rawEnd)
170
+ return;
171
+ const first = Math.max(firstContentRow, surface.viewport.scrollTop);
172
+ const last = Math.min(lastContentRow + 1, surface.viewport.scrollTop + visible);
135
173
  for (let transcriptRow = first; transcriptRow < last; transcriptRow++) {
136
174
  const row = transcriptRow - surface.viewport.scrollTop + padding;
137
175
  if (row < padding || row >= rows.length)
138
176
  continue;
139
- const content = transcriptRow === marker.row
140
- ? `${paint('accent', '▸')} ${truncate(rows[row] ?? '', Math.max(0, contentWidth - 2), '')}`
141
- : rows[row] ?? '';
177
+ const existingRow = rows[row] ?? '';
178
+ const plainTrimmed = stripAnsi(existingRow).trimStart();
179
+ const alreadyHasMarker = plainTrimmed.startsWith('▸') || plainTrimmed.startsWith('│');
180
+ const content = transcriptRow === firstContentRow && !alreadyHasMarker
181
+ ? `${paint('accent', '▸')} ${truncate(existingRow, Math.max(0, contentWidth - 2), '')}`
182
+ : existingRow;
142
183
  rows[row] = fill('surfaceHi', padEnd(content, contentWidth));
143
184
  }
144
185
  }
@@ -150,6 +191,23 @@ function messageText(content) {
150
191
  ? String(part.text) : '').filter(Boolean).join('\n');
151
192
  return '';
152
193
  }
194
+ function messagesMatch(persistedContent, shownText) {
195
+ if (typeof shownText !== 'string')
196
+ return false;
197
+ const persisted = messageText(persistedContent);
198
+ if (persisted === shownText)
199
+ return true;
200
+ const cleanPersisted = persisted.trim();
201
+ const cleanShown = shownText.trim();
202
+ if (!cleanShown)
203
+ return false;
204
+ if (cleanPersisted === cleanShown)
205
+ return true;
206
+ // Handle messages where an attachment system notification was appended on save
207
+ if (cleanPersisted.startsWith(cleanShown))
208
+ return true;
209
+ return false;
210
+ }
153
211
  /** Match in conversation order; never guess a fork point from visual row alone. */
154
212
  export function persistedMessageIndex(marker, markers, messages) {
155
213
  let matched = -1;
@@ -159,7 +217,7 @@ export function persistedMessageIndex(marker, markers, messages) {
159
217
  let found = -1;
160
218
  for (let i = matched + 1; i < messages.length; i++) {
161
219
  const message = messages[i];
162
- if (message?.role === shown.kind && messageText(message.content) === shown.meta?.messageText) {
220
+ if (message?.role === shown.kind && messagesMatch(message.content, shown.meta?.messageText)) {
163
221
  found = i;
164
222
  break;
165
223
  }
@@ -171,28 +229,96 @@ export function persistedMessageIndex(marker, markers, messages) {
171
229
  }
172
230
  return null;
173
231
  }
232
+ function computeNextForkTitle(sourceTitle) {
233
+ const cleanBase = (sourceTitle && sourceTitle !== 'Untitled' ? sourceTitle : 'Conversation')
234
+ .replace(/\s*\(fork #\d+\)$/i, '')
235
+ .trim();
236
+ let maxForkNum = 0;
237
+ try {
238
+ const existing = listSessions();
239
+ const escaped = cleanBase.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
240
+ const forkRe = new RegExp(`^${escaped}\\s*\\(fork #(\\d+)\\)$`, 'i');
241
+ for (const s of existing) {
242
+ const m = forkRe.exec((s.title || '').trim());
243
+ if (m?.[1]) {
244
+ const n = Number(m[1]);
245
+ if (Number.isFinite(n) && n > maxForkNum)
246
+ maxForkNum = n;
247
+ }
248
+ }
249
+ }
250
+ catch {
251
+ // Ignore session listing failures
252
+ }
253
+ return `${cleanBase} (fork #${maxForkNum + 1})`;
254
+ }
255
+ function syncInFlightMessagesToSession(loop, sourceId) {
256
+ if (!sourceId)
257
+ return null;
258
+ let source = getSession(sourceId);
259
+ const inMemory = Array.isArray(loop.messages)
260
+ ? loop.messages.filter(m => m && m.role !== 'system')
261
+ : [];
262
+ const onDiskCount = source?.messages?.length ?? 0;
263
+ if (inMemory.length > onDiskCount) {
264
+ try {
265
+ if (typeof loop.memoryManager?.saveHistory === 'function') {
266
+ loop.memoryManager.saveHistory(loop.messages);
267
+ }
268
+ else {
269
+ saveSessionMessages(sourceId, inMemory);
270
+ }
271
+ source = getSession(sourceId);
272
+ }
273
+ catch {
274
+ // Keep existing source record if disk sync fails
275
+ }
276
+ }
277
+ return source;
278
+ }
174
279
  export function openMessageActions(host, loop, marker, anchorY = null) {
175
280
  const text = marker.meta?.messageText;
176
281
  if (typeof text !== 'string')
177
282
  return;
178
283
  const sourceId = loop.memoryManager?.sessionId || getActiveSessionId();
179
- const source = sourceId ? getSession(sourceId) : null;
284
+ const source = syncInFlightMessagesToSession(loop, sourceId);
180
285
  const markers = host.surface?.transcript.flatten(host.surface.viewport.width).markers || [];
181
286
  const index = source ? persistedMessageIndex(marker, markers, source.messages) : null;
287
+ const canModifySession = Boolean(source && index !== null);
182
288
  const options = [
183
289
  { label: 'Copy message', description: 'to clipboard', value: 'copy' },
184
- ...(!host.busy && index !== null ? [{ label: 'Fork from here', description: 'fresh permissions', value: 'fork' }] : [])
290
+ ...(canModifySession
291
+ ? [
292
+ { label: 'Fork from here', description: 'create a new session · fresh permissions', value: 'fork' },
293
+ { label: 'Revert to here', description: 'undo messages and file changes', value: 'revert' }
294
+ ]
295
+ : [])
185
296
  ];
186
297
  const layer = new MessageActionsLayer(options, (choice) => {
187
- if (choice?.value === 'copy') {
188
- host.notice = writeClipboardSync(text) ? 'Message copied to clipboard' : 'Clipboard unavailable on this terminal';
298
+ if (host.surface) {
299
+ host.surface.hoveredMessageFrameId = null;
189
300
  }
190
- if (choice?.value === 'fork' && source && index !== null && !host.busy) {
301
+ if (!choice) {
302
+ host.scheduleDraw();
303
+ return;
304
+ }
305
+ if (choice.value === 'copy') {
306
+ const ok = writeClipboardSync(text);
307
+ setTransientNotice(host, ok ? 'Message copied to clipboard' : 'Clipboard unavailable on this terminal');
308
+ return;
309
+ }
310
+ if (choice.value === 'fork' && source && index !== null) {
311
+ if (host.busy) {
312
+ host.interrupt?.();
313
+ loop.abort?.();
314
+ syncInFlightMessagesToSession(loop, source.id);
315
+ }
316
+ const forkTitle = computeNextForkTitle(source.title);
191
317
  const forked = loop.memoryManager?.sessionId === source.id && loop.memoryManager.forkSession
192
- ? loop.memoryManager.forkSession(null, index)
318
+ ? loop.memoryManager.forkSession(forkTitle, index)
193
319
  : null;
194
320
  if (!forked) {
195
- host.notice = 'Could not fork this message; session was not changed';
321
+ setTransientNotice(host, 'Could not fork this message; session was not changed');
196
322
  }
197
323
  else {
198
324
  revokeAllAlways();
@@ -201,12 +327,43 @@ export function openMessageActions(host, loop, marker, anchorY = null) {
201
327
  loop.messages = history;
202
328
  loop.taskId = `cli_chat_${Date.now()}`;
203
329
  loop.tokenTracker?.reset?.();
204
- host.renderer?.renderSessionHistory?.(history, {
205
- id: forked.id, title: forked.title, messagesCount: history.length
206
- });
330
+ const info = { id: forked.id, title: forked.title, messagesCount: history.length };
331
+ host.renderer?.setSessionInfo?.(info);
332
+ host.renderer?.renderSessionHistory?.(history, info);
207
333
  host.surface?.viewport?.toBottom?.();
208
- host.notice = 'Forked from selected message. Permissions reset.';
334
+ setTransientNotice(host, `Forked into "${forked.title}". Permissions reset.`);
335
+ }
336
+ return;
337
+ }
338
+ if (choice.value === 'revert' && source && index !== null) {
339
+ if (host.busy) {
340
+ host.interrupt?.();
341
+ loop.abort?.();
342
+ }
343
+ try {
344
+ undoLast({ dryRun: false, scope: 'code' });
345
+ }
346
+ catch {
347
+ // Checkpoints are optional if no files were modified in the reverted turn.
348
+ }
349
+ const latestSource = getSession(source.id) || source;
350
+ const keptMessages = latestSource.messages.slice(0, index);
351
+ saveSessionMessages(source.id, keptMessages);
352
+ if (loop.memoryManager?.session && loop.memoryManager.session.id === source.id) {
353
+ loop.memoryManager.session.messages = keptMessages;
354
+ }
355
+ const history = keptMessages.filter(m => m.role !== 'system');
356
+ loop.messages = history;
357
+ loop.taskId = `cli_chat_${Date.now()}`;
358
+ const info = { id: latestSource.id, title: latestSource.title, messagesCount: history.length };
359
+ host.renderer?.setSessionInfo?.(info);
360
+ host.renderer?.renderSessionHistory?.(history, info);
361
+ host.surface?.viewport?.toBottom?.();
362
+ if (marker.kind === 'user' && text) {
363
+ host.setText?.(text);
209
364
  }
365
+ setTransientNotice(host, 'Reverted messages and file changes to selected point.');
366
+ return;
210
367
  }
211
368
  host.scheduleDraw();
212
369
  }, anchorY);
@@ -218,7 +375,7 @@ export function handleMessageActionMenuMouse(layer, event) {
218
375
  return null;
219
376
  const { x, y, width, count } = layer.actionLayout;
220
377
  const { mouse } = event;
221
- const index = mouse.y - (y + 1); // only the frame precedes the two action rows
378
+ const index = mouse.y - (y + 1); // only the frame precedes the action rows
222
379
  if (mouse.x < x || mouse.x >= x + width || index < 0 || index >= count) {
223
380
  return mouse.button === 0 && mouse.pressed ? 'close' : 'handled';
224
381
  }