@causyne/agent 1.20.62 → 1.20.63

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 (59) hide show
  1. package/README.md +2 -2
  2. package/package.json +1 -1
  3. package/src/acp/client.js +48 -0
  4. package/src/acp/client.ts +44 -0
  5. package/src/agentLoop.js +181 -11
  6. package/src/agentLoop.ts +184 -11
  7. package/src/cliMode.js +9 -0
  8. package/src/commands.js +31 -0
  9. package/src/decisionPolicy.js +18 -1
  10. package/src/decisionPolicy.ts +18 -1
  11. package/src/implementationPlan.js +26 -0
  12. package/src/implementationPlan.ts +25 -0
  13. package/src/observer/coldObserver.js +6 -1
  14. package/src/observer/coldObserver.ts +6 -1
  15. package/src/observer.js +24 -2
  16. package/src/observer.ts +24 -2
  17. package/src/providers/acp.js +2 -8
  18. package/src/providers/acp.ts +2 -8
  19. package/src/providers/antigravity.js +7 -10
  20. package/src/providers/antigravity.ts +7 -10
  21. package/src/smartToolsUse.js +1 -1
  22. package/src/smartToolsUse.ts +1 -1
  23. package/src/toolPacks.js +1 -1
  24. package/src/toolPacks.ts +1 -1
  25. package/src/tools/toolSelfRepair.js +51 -8
  26. package/src/tools/toolSelfRepair.ts +42 -9
  27. package/src/tui/CausyneRotor.js +234 -0
  28. package/src/tui/CausyneRotor.ts +280 -0
  29. package/src/tui/activityLayout.js +1 -1
  30. package/src/tui/activityLayout.ts +1 -1
  31. package/src/tui/chatRenderer.js +56 -7
  32. package/src/tui/chatboxClipboard.js +96 -10
  33. package/src/tui/chatboxClipboard.ts +121 -11
  34. package/src/tui/clipboardImage.js +308 -0
  35. package/src/tui/clipboardImage.ts +320 -0
  36. package/src/tui/codeStreamRenderer.js +89 -1
  37. package/src/tui/codeStreamRenderer.ts +109 -1
  38. package/src/tui/dialogs.js +5 -2
  39. package/src/tui/engineSpinner.js +20 -0
  40. package/src/tui/engineSpinner.ts +46 -0
  41. package/src/tui/lineEditor.js +44 -12
  42. package/src/tui/messageInteraction.js +16 -5
  43. package/src/tui/messageInteraction.ts +14 -5
  44. package/src/tui/observerPicker.js +4 -8
  45. package/src/tui/observerPicker.ts +4 -8
  46. package/src/tui/prompt.js +70 -40
  47. package/src/tui/runningLight.js +213 -0
  48. package/src/tui/runningLight.ts +271 -0
  49. package/src/tui/sessionSidebar.js +7 -1
  50. package/src/tui/sessionSidebar.ts +7 -1
  51. package/src/tui/stage.js +4 -4
  52. package/src/tui/stageSurface.js +21 -14
  53. package/src/tui/statusLine.js +27 -4
  54. package/src/tui/statusLine.ts +27 -4
  55. package/src/tui/theme.js +5 -2
  56. package/src/tui/thinkingBox.js +41 -10
  57. package/src/tui/thinkingBox.ts +43 -10
  58. package/src/tui/turnRail.js +155 -24
  59. package/src/tui/turnRail.ts +170 -22
@@ -0,0 +1,280 @@
1
+ /**
2
+ * CausyneRotor.ts
3
+ * Dependency-free, terminal-native animation for a Causyne CLI/TUI.
4
+ * Uses Unicode Braille pixels (2x4 subpixels per character) and ANSI colors.
5
+ * The logo is drawn geometrically, not played from an image/GIF.
6
+ *
7
+ * If an existing TUI (Ink, Blessed, etc.) owns the terminal screen, call
8
+ * renderCausyneRotor(...) in THAT renderer. Do not also start the standalone
9
+ * interval-based renderer.
10
+ */
11
+
12
+ import { stdout } from 'node:process';
13
+
14
+ export type RotorMotion = 'heavy' | 'smooth' | 'pulse';
15
+
16
+ export interface RotorOptions {
17
+ /** Width of the actual rotor in terminal columns. Default: 24. */
18
+ columns?: number | undefined;
19
+ /** Height of the actual rotor in terminal rows. Default: 12. */
20
+ rows?: number | undefined;
21
+ /** 'heavy' is an eased, weighty 60-degree indexed rotation. */
22
+ motion?: RotorMotion | undefined;
23
+ /** Text shown below the rotor. Set to '' to omit. */
24
+ label?: string | undefined;
25
+ /** Set false for non-color terminals. */
26
+ color?: boolean | undefined;
27
+ }
28
+
29
+ type Point = { x: number; y: number };
30
+ type Sample = 0 | 1 | 2 | 3; // empty / charcoal / lime / light outline
31
+
32
+ const BRAILLE_DOTS: ReadonlyArray<readonly [number, number, number]> = [
33
+ [0, 0, 0x01], [0, 1, 0x02], [0, 2, 0x04], [0, 3, 0x40],
34
+ [1, 0, 0x08], [1, 1, 0x10], [1, 2, 0x20], [1, 3, 0x80],
35
+ ];
36
+
37
+ const TAU = Math.PI * 2;
38
+ const STEP = TAU / 6;
39
+ export const COLORS = {
40
+ charcoal: '\x1b[38;2;115;124;137m',
41
+ lime: '\x1b[38;2;181;214;106m',
42
+ outline: '\x1b[38;2;224;230;229m',
43
+ text: '\x1b[38;2;185;192;196m',
44
+ reset: '\x1b[0m',
45
+ } as const;
46
+
47
+ // One triangular blade, defined relative to the hub at (0, 0).
48
+ // The remaining five are exact 60-degree rotations of this triangle.
49
+ // Negative space between blades forms the central hexagonal aperture.
50
+ const BASE_BLADE: readonly Point[] = [
51
+ { x: -0.86, y: -0.64 },
52
+ { x: 0.06, y: -1.06 },
53
+ { x: -0.42, y: -0.105 },
54
+ ];
55
+
56
+ function rotate(p: Point, angle: number): Point {
57
+ const c = Math.cos(angle);
58
+ const s = Math.sin(angle);
59
+ return { x: p.x * c - p.y * s, y: p.x * s + p.y * c };
60
+ }
61
+
62
+ function insideTriangle(p: Point, a: Point, b: Point, c: Point): boolean {
63
+ const cross = (v: Point, w: Point, q: Point): number =>
64
+ (w.x - v.x) * (q.y - v.y) - (w.y - v.y) * (q.x - v.x);
65
+ const u = cross(a, b, p);
66
+ const v = cross(b, c, p);
67
+ const w = cross(c, a, p);
68
+ return (u >= 0 && v >= 0 && w >= 0) || (u <= 0 && v <= 0 && w <= 0);
69
+ }
70
+
71
+ function distanceToSegment(p: Point, a: Point, b: Point): number {
72
+ const dx = b.x - a.x;
73
+ const dy = b.y - a.y;
74
+ const lengthSq = dx * dx + dy * dy;
75
+ const t = lengthSq === 0 ? 0 : Math.max(0, Math.min(1,
76
+ ((p.x - a.x) * dx + (p.y - a.y) * dy) / lengthSq,
77
+ ));
78
+ return Math.hypot(p.x - (a.x + t * dx), p.y - (a.y + t * dy));
79
+ }
80
+
81
+ function smoothstep(t: number): number {
82
+ const x = Math.max(0, Math.min(1, t));
83
+ return x * x * (3 - 2 * x);
84
+ }
85
+
86
+ export function getAngle(elapsedMs: number, motion: RotorMotion = 'heavy'): number {
87
+ const t = Math.max(0, elapsedMs);
88
+ if (motion === 'smooth') return (t / 4200) * TAU;
89
+ if (motion === 'pulse') {
90
+ const cycle = t / 1000;
91
+ return (Math.floor(cycle) + smoothstep(Math.min(1, (cycle % 1) / 0.75))) * STEP;
92
+ }
93
+ // Heavy engine: deliberate acceleration, high-momentum travel, short settle.
94
+ const cycle = t / 860;
95
+ const index = Math.floor(cycle);
96
+ const progress = (cycle % 1) / 0.88; // final 12% holds precisely on an index
97
+ return (index + smoothstep(progress)) * STEP;
98
+ }
99
+
100
+ function sample(p: Point, blades: readonly (readonly Point[])[]): Sample {
101
+ for (let i = 0; i < 6; i++) {
102
+ const blade = blades[i];
103
+ if (!blade) continue;
104
+ const [a, b, c] = blade;
105
+ if (!a || !b || !c || !insideTriangle(p, a, b, c)) continue;
106
+ // 1.5~2 subpixels: subtle white perimeter like your outlined SVG.
107
+ const edge = Math.min(
108
+ distanceToSegment(p, a, b),
109
+ distanceToSegment(p, b, c),
110
+ distanceToSegment(p, c, a),
111
+ );
112
+ if (edge < 0.018) return 3;
113
+ return i === 1 ? 2 : 1;
114
+ }
115
+ return 0;
116
+ }
117
+
118
+ function paint(text: string, kind: Sample, color: boolean): string {
119
+ if (!color) return text;
120
+ const code = kind === 2 ? COLORS.lime : kind === 3 ? COLORS.outline : COLORS.charcoal;
121
+ return code + text + COLORS.reset;
122
+ }
123
+
124
+ /**
125
+ * Returns an ANSI-colored multi-line string. No side effects or timers.
126
+ * Intended for an existing TUI's render() method.
127
+ */
128
+ export function renderCausyneRotor(
129
+ elapsedMs: number,
130
+ options: RotorOptions = {},
131
+ ): string {
132
+ const columns = Math.max(12, Math.min(56, Math.floor(options.columns ?? 24)));
133
+ const rows = Math.max(6, Math.min(28, Math.floor(options.rows ?? 12)));
134
+ const color = options.color ?? true;
135
+ const angle = getAngle(elapsedMs, options.motion ?? 'heavy');
136
+ const blades = Array.from({ length: 6 }, (_, i) => {
137
+ const rotAngle = angle + i * STEP;
138
+ return BASE_BLADE.map((p) => rotate(p, rotAngle));
139
+ });
140
+
141
+ const widthDots = columns * 2;
142
+ const heightDots = rows * 4;
143
+ const scale = Math.min(widthDots, heightDots) / 2.42;
144
+ const lines: string[] = [];
145
+
146
+ for (let row = 0; row < rows; row++) {
147
+ let line = '';
148
+ for (let col = 0; col < columns; col++) {
149
+ let mask = 0;
150
+ let dark = 0;
151
+ let lime = 0;
152
+ let outline = 0;
153
+ for (const [dx, dy, bit] of BRAILLE_DOTS) {
154
+ const px = col * 2 + dx + 0.5;
155
+ const py = row * 4 + dy + 0.5;
156
+ const p = {
157
+ x: (px - widthDots / 2) / scale,
158
+ y: (py - heightDots / 2) / scale,
159
+ };
160
+ const hit = sample(p, blades);
161
+ if (!hit) continue;
162
+ mask |= bit;
163
+ if (hit === 1) dark++;
164
+ else if (hit === 2) lime++;
165
+ else outline++;
166
+ }
167
+ if (!mask) {
168
+ line += ' ';
169
+ } else {
170
+ // A Braille cell has a single foreground color. Choose the dominant fill.
171
+ const kind: Sample = lime > 0 && lime >= dark ? 2 :
172
+ outline >= 3 && dark === 0 && lime === 0 ? 3 : 1;
173
+ line += paint(String.fromCodePoint(0x2800 + mask), kind, color);
174
+ }
175
+ }
176
+ lines.push(line);
177
+ }
178
+
179
+ if (options.label !== '') {
180
+ const label = options.label ?? 'working';
181
+ const status = color
182
+ ? `${COLORS.lime}●${COLORS.reset} ${COLORS.text}${label}${COLORS.reset}`
183
+ : `● ${label}`;
184
+ lines.push(status);
185
+ }
186
+ return lines.join('\n');
187
+ }
188
+
189
+ /**
190
+ * Standalone demo/CLI renderer. It owns its multi-line cursor region.
191
+ * If your app uses Ink/Blessed/another renderer, DO NOT call this function;
192
+ * use renderCausyneRotor() inside the existing renderer instead.
193
+ */
194
+ export function startCausyneRotor(options: RotorOptions & { fps?: number | undefined } = {}): {
195
+ stop: () => void;
196
+ } {
197
+ const color = options.color ?? (Boolean(stdout.isTTY) && !process.env.NO_COLOR && !process.env.CAUSYNE_NO_COLOR);
198
+ if (!stdout.isTTY) {
199
+ stdout.write((options.label ?? 'working') + '\n');
200
+ return { stop() {} };
201
+ }
202
+
203
+ const fps = Math.max(4, Math.min(30, options.fps ?? 16));
204
+ const started = Date.now();
205
+ let stopped = false;
206
+ let lastLines = 0;
207
+
208
+ function draw(): void {
209
+ const frame = renderCausyneRotor(Date.now() - started, { ...options, color });
210
+ const lines = frame.split('\n');
211
+ // Redraw only our reserved lines, never clear the whole terminal.
212
+ if (lastLines > 0) stdout.write(`\x1b[${lastLines}A\r`);
213
+ stdout.write(lines.map((line) => `\x1b[2K${line}\n`).join(''));
214
+ lastLines = lines.length;
215
+ }
216
+
217
+ stdout.write('\x1b[?25l'); // temporarily hide cursor
218
+ draw();
219
+ const timer = setInterval(draw, Math.round(1000 / fps));
220
+
221
+ return {
222
+ stop(): void {
223
+ if (stopped) return;
224
+ stopped = true;
225
+ clearInterval(timer);
226
+ stdout.write('\x1b[?25h'); // restore cursor
227
+ },
228
+ };
229
+ }
230
+
231
+ // ---------------------------------------------------------------------------------------------------------
232
+ // Single-Line Status Row / Inline Spinner Integration
233
+ // ---------------------------------------------------------------------------------------------------------
234
+
235
+ /**
236
+ * 2-character Braille rotor frames using the CausyneRotor palette (lime + charcoal).
237
+ * 6-phase rotation corresponding directly to the 60-degree indexed rotor blades.
238
+ */
239
+ export const ROTOR_SPINNER_FRAMES: readonly string[] = [
240
+ `${COLORS.charcoal}⠇${COLORS.lime}⠙${COLORS.reset}`,
241
+ `${COLORS.charcoal}⠋${COLORS.lime}⠸${COLORS.reset}`,
242
+ `${COLORS.charcoal}⠙${COLORS.lime}⠴${COLORS.reset}`,
243
+ `${COLORS.lime}⠸${COLORS.charcoal}⠦${COLORS.reset}`,
244
+ `${COLORS.lime}⠴${COLORS.charcoal}⠇${COLORS.reset}`,
245
+ `${COLORS.lime}⠦${COLORS.charcoal}⠋${COLORS.reset}`,
246
+ ];
247
+
248
+ export const ROTOR_SPINNER_FRAMES_NO_COLOR: readonly string[] = [
249
+ '⠇⠙', '⠋⠸', '⠙⠴', '⠸⠦', '⠴⠇', '⠦⠋',
250
+ ];
251
+
252
+ export const ROTOR_SPINNER_FRAMES_ASCII: readonly string[] = [
253
+ '[-]', '[\\]', '[|]', '[/]',
254
+ ];
255
+
256
+ export function getRotorSpinnerFrames(color = true, unicode = true): readonly string[] {
257
+ if (!unicode) return ROTOR_SPINNER_FRAMES_ASCII;
258
+ return color ? ROTOR_SPINNER_FRAMES : ROTOR_SPINNER_FRAMES_NO_COLOR;
259
+ }
260
+
261
+ export function getRotorSpinnerFrame(tick: number, color = true, unicode = true): string {
262
+ const frames = getRotorSpinnerFrames(color, unicode);
263
+ const index = Math.abs(Math.floor(tick)) % frames.length;
264
+ return frames[index] ?? frames[0] ?? '⠇⠙';
265
+ }
266
+
267
+ const DOT_FRAMES = [' ', '. ', '.. ', '...'] as const;
268
+
269
+ export function getAnimatedDots(tick: number): string {
270
+ const stage = Math.floor(Math.abs(Math.floor(tick)) / 2) % DOT_FRAMES.length;
271
+ return DOT_FRAMES[stage] ?? ' ';
272
+ }
273
+
274
+ export function formatAnimatedStatus(message: string, tick: number): string {
275
+ let base = (message || 'working').trimEnd();
276
+ while (base.endsWith('.') || base.endsWith('…')) {
277
+ base = base.slice(0, -1).trimEnd();
278
+ }
279
+ return `${base}${getAnimatedDots(tick)}`;
280
+ }
@@ -4,6 +4,6 @@ export function activityRows(message, columns, prefix = '▸ ') {
4
4
  const width = Number.isFinite(columns) ? Math.max(20, Math.floor(columns)) : 80;
5
5
  const inset = visibleWidth(prefix);
6
6
  const bodyWidth = Math.max(1, width - inset - 1);
7
- const lines = wrapBlock(safeDisplay(message).trim(), bodyWidth, { hang: 2 });
7
+ const lines = wrapBlock(safeDisplay(message).trim(), bodyWidth, { hang: 0 });
8
8
  return lines.map((line, index) => `${index === 0 ? prefix : ' '.repeat(inset)}${line}`);
9
9
  }
@@ -5,6 +5,6 @@ export function activityRows(message: string, columns: number, prefix = '▸ '):
5
5
  const width = Number.isFinite(columns) ? Math.max(20, Math.floor(columns)) : 80;
6
6
  const inset = visibleWidth(prefix);
7
7
  const bodyWidth = Math.max(1, width - inset - 1);
8
- const lines = wrapBlock(safeDisplay(message).trim(), bodyWidth, { hang: 2 });
8
+ const lines = wrapBlock(safeDisplay(message).trim(), bodyWidth, { hang: 0 });
9
9
  return lines.map((line, index) => `${index === 0 ? prefix : ' '.repeat(inset)}${line}`);
10
10
  }
@@ -94,6 +94,11 @@ function renderMarkdown(text, width) {
94
94
  continue;
95
95
  }
96
96
 
97
+ if (/^\s*([-*_])\1{2,}\s*$/.test(line)) {
98
+ lines.push(dim(GLYPHS.hLine.repeat(Math.max(4, Math.min(width, 40)))));
99
+ continue;
100
+ }
101
+
97
102
  const bullet = line.match(/^(\s*)[-*+]\s+(.*)$/);
98
103
  if (bullet) {
99
104
  const indent = bullet[1] + ' ';
@@ -104,6 +109,39 @@ function renderMarkdown(text, width) {
104
109
  continue;
105
110
  }
106
111
 
112
+ const numbered = line.match(/^(\s*)(\d+\.)\s+(.*)$/);
113
+ if (numbered) {
114
+ const prefix = `${numbered[1]}${bold(numbered[2])} `;
115
+ const indent = ' '.repeat(visibleWidth(`${numbered[1]}${numbered[2]} `));
116
+ const wrapped = wrap(inline(numbered[3]), width - indent.length);
117
+ wrapped.forEach((part, index) => {
118
+ lines.push(index === 0 ? `${prefix}${part}` : `${indent}${part}`);
119
+ });
120
+ continue;
121
+ }
122
+
123
+ const quote = line.match(/^(\s*)>\s?(.*)$/);
124
+ if (quote) {
125
+ const indent = quote[1] + paint('muted', '│ ');
126
+ const wrapped = wrap(inline(quote[2]), width - visibleWidth(indent));
127
+ wrapped.forEach((part) => {
128
+ lines.push(`${indent}${part}`);
129
+ });
130
+ continue;
131
+ }
132
+
133
+ if (/^\s*\|?(\s*:?-+:?\s*\|)+\s*$/.test(line)) {
134
+ lines.push(dim(' ' + GLYPHS.hLine.repeat(Math.max(4, Math.min(width - 4, 48)))));
135
+ continue;
136
+ }
137
+
138
+ const tableRow = line.match(/^\s*\|(.+)\|\s*$/);
139
+ if (tableRow) {
140
+ const cells = tableRow[1].split('|').map(c => inline(c.trim()));
141
+ lines.push(' ' + cells.join(paint('muted', ' │ ')));
142
+ continue;
143
+ }
144
+
107
145
  const trimmed = line.trim();
108
146
  if (trimmed === '') { lines.push(''); continue; }
109
147
  if (trimmed === '[' || trimmed === ']') continue;
@@ -206,8 +244,18 @@ function inline(text) {
206
244
  out = out.replace(/`([^`]+)`/g, (_m, code) => stash(HAS_COLOR ? paint('accent', code) : code));
207
245
  out = formatMarkdownMath(out);
208
246
  out = HAS_COLOR
209
- ? out.replace(/\*\*([^*]+)\*\*/g, (m, strong) => bold(strong))
247
+ ? out.replace(/\*\*\*([^*]+)\*\*\*/g, (_m, strong) => bold(paint('accent', strong)))
248
+ : out.replace(/\*\*\*([^*]+)\*\*\*/g, '$1');
249
+ out = HAS_COLOR
250
+ ? out.replace(/\*\*([^*]+)\*\*/g, (_m, strong) => bold(strong))
210
251
  : out.replace(/\*\*([^*]+)\*\*/g, '$1');
252
+ out = HAS_COLOR
253
+ ? out.replace(/(?<=^|[\s(])\*([^*\s](?:[^*]*[^*\s])?)\*(?=[\s).,!?]|$)/g, (_m, italic) => `\x1b[3m${italic}\x1b[23m`)
254
+ : out.replace(/(?<=^|[\s(])\*([^*\s](?:[^*]*[^*\s])?)\*(?=[\s).,!?]|$)/g, '$1');
255
+ out = HAS_COLOR
256
+ ? out.replace(/(?<=^|[\s(])_([^\s_](?:[^_]*[^\s_])?)_(?=[\s).,!?]|$)/g, (_m, italic) => `\x1b[3m${italic}\x1b[23m`)
257
+ : out.replace(/(?<=^|[\s(])_([^\s_](?:[^_]*[^\s_])?)_(?=[\s).,!?]|$)/g, '$1');
258
+ out = out.replace(/~~([^~]+)~~/g, HAS_COLOR ? '\x1b[9m$1\x1b[29m' : '$1');
211
259
 
212
260
  if (stashed.length > 0) {
213
261
  out = out.replace(/\u0000(\d+)\u0000/g, (m, index) => stashed[Number(index)] || '');
@@ -472,11 +520,12 @@ export class ChatRenderer {
472
520
  elapsedMs: Date.now() - since,
473
521
  steps: this.stepCount,
474
522
  width: this.width() - 1,
475
- spinnerFrame: frame
523
+ spinnerFrame: frame,
524
+ tick: this.spinnerIndex
476
525
  }));
477
526
  readline.clearLine(this.out, 1);
478
527
  this.spinnerIndex++;
479
- }, 90);
528
+ }, 125);
480
529
  }
481
530
 
482
531
  stopSpinner() {
@@ -952,7 +1001,7 @@ export class ChatRenderer {
952
1001
  this.frame('approval', () => {
953
1002
  this.write();
954
1003
  this.write(`${paint('accent', GLYPHS.arrow)} ${bold(safeDisplay(request.header || 'decision required'))}`);
955
- for (const row of wrapBlock(safeDisplay(request.question || ''), Math.max(20, this.width() - 4))) this.write(` ${row}`);
1004
+ for (const row of wrapBlock(safeDisplay(request.question || ''), Math.max(20, this.width() - 4), { hang: 0 })) this.write(` ${row}`);
956
1005
  this.write();
957
1006
  }, { kind: request.kind || 'other' });
958
1007
  }
@@ -1174,7 +1223,7 @@ export class ChatRenderer {
1174
1223
  const head = label ? `${label} ${detail}` : detail;
1175
1224
 
1176
1225
  this.frame('error', () => {
1177
- const rows = wrapBlock(head, Math.max(20, this.width() - 7));
1226
+ const rows = wrapBlock(head, Math.max(20, this.width() - 7), { hang: 0 });
1178
1227
  this.write(` ${paint('danger', GLYPHS.cross)} ${paint('danger', rows[0] || '')}`);
1179
1228
  for (const row of rows.slice(1)) this.write(` ${dim(row)}`);
1180
1229
  }, { tool: msg.tool });
@@ -1315,7 +1364,7 @@ export class ChatRenderer {
1315
1364
  this.write();
1316
1365
  this.write(truncate(head, width - 1, GLYPHS.ellipsis));
1317
1366
  if (instruction) {
1318
- for (const line of wrapBlock(safeDisplay(instruction), width - 4)) {
1367
+ for (const line of wrapBlock(safeDisplay(instruction), width - 4, { hang: 0 })) {
1319
1368
  this.write(dim(` ${line}`));
1320
1369
  }
1321
1370
  }
@@ -1340,7 +1389,7 @@ export class ChatRenderer {
1340
1389
  this.frame('notice', () => {
1341
1390
  const width = this.measure();
1342
1391
  const marker = paint(tone === 'muted' ? 'rule' : tone, GLYPHS.vLine);
1343
- for (const line of wrapBlock(message, width - 2)) {
1392
+ for (const line of wrapBlock(message, width - 2, { hang: 0 })) {
1344
1393
  this.write(`${marker} ${dim(line)}`);
1345
1394
  }
1346
1395
  }, { tone });
@@ -1,6 +1,8 @@
1
1
  // Chatbox Clipboard Manager: surgical operations for copy, paste, and CRLF normalization.
2
2
  // Supports native terminal mouse selection, readline kill/yank, Alt+C/Alt+W/Alt+V, and /copy /paste commands.
3
3
  import { readClipboardSync, writeClipboardSync, emitOsc52Copy } from './clipboardSync.js';
4
+ import { readClipboardImageSync, formatByteSize, loadLocalImageAttachment, extractPastedFilePath, isLocalImageFile } from './clipboardImage.js';
5
+ export { loadLocalImageAttachment, extractPastedFilePath, isLocalImageFile };
4
6
  /**
5
7
  * Clean up text copied or pasted: normalize Windows CRLF and classic Mac CR to LF,
6
8
  * remove null bytes and non-printable control characters while preserving valid newlines and tabs.
@@ -42,22 +44,103 @@ export function copyChatboxInput(prompt) {
42
44
  }
43
45
  /**
44
46
  * Paste clipboard content into the chatbox prompt editor.
47
+ * Seamlessly supports both text and screenshot images from system clipboard,
48
+ * as well as drag-and-dropped local image file paths and empty bracketed pastes.
45
49
  */
46
50
  export function pasteIntoChatbox(prompt, fallbackText) {
47
- const raw = fallbackText ?? readClipboardSync();
48
- const text = normalizeClipboardText(raw);
49
- if (!text) {
50
- prompt.notice = 'clipboard is empty';
51
+ const clean = normalizeClipboardText(fallbackText);
52
+ // 1. If fallbackText was provided (e.g. drag & drop or file paste), check if it's a local image file
53
+ if (clean) {
54
+ const filePath = extractPastedFilePath(clean);
55
+ const localImg = loadLocalImageAttachment(filePath);
56
+ if (localImg) {
57
+ if (!Array.isArray(prompt.pendingAttachments)) {
58
+ prompt.pendingAttachments = [];
59
+ }
60
+ prompt.pendingAttachments.push(localImg);
61
+ const index = prompt.pendingAttachments.length;
62
+ const tag = `[Image ${index}] `;
63
+ prompt.editor.insert(tag);
64
+ if (typeof prompt.refreshCompletion === 'function') {
65
+ prompt.refreshCompletion();
66
+ }
67
+ prompt.notice = `attached image ${index} (${formatByteSize(localImg.size)})`;
68
+ prompt.scheduleDraw();
69
+ return { success: true, notice: prompt.notice, text: tag, isImage: true };
70
+ }
71
+ }
72
+ // 2. If fallbackText was empty (empty bracketed paste from Cmd+V in terminal when an image is copied)
73
+ // OR no fallbackText was passed (user pressed Ctrl+V, Alt+V, or typed /paste):
74
+ // Probe system clipboard for screenshot / image FIRST!
75
+ if (!clean) {
76
+ const img = readClipboardImageSync();
77
+ if (img) {
78
+ if (!Array.isArray(prompt.pendingAttachments)) {
79
+ prompt.pendingAttachments = [];
80
+ }
81
+ prompt.pendingAttachments.push(img);
82
+ const index = prompt.pendingAttachments.length;
83
+ const tag = `[Image ${index}] `;
84
+ prompt.editor.insert(tag);
85
+ if (typeof prompt.refreshCompletion === 'function') {
86
+ prompt.refreshCompletion();
87
+ }
88
+ prompt.notice = `attached clipboard image ${index} (${formatByteSize(img.size)})`;
89
+ prompt.scheduleDraw();
90
+ return { success: true, notice: prompt.notice, text: tag, isImage: true };
91
+ }
92
+ }
93
+ // 3. If fallbackText had non-image text: insert it into the editor
94
+ if (clean) {
95
+ prompt.editor.insert(clean);
96
+ if (typeof prompt.refreshCompletion === 'function') {
97
+ prompt.refreshCompletion();
98
+ }
99
+ prompt.notice = 'pasted from clipboard';
51
100
  prompt.scheduleDraw();
52
- return { success: false, notice: prompt.notice, text: '' };
101
+ return { success: true, notice: prompt.notice, text: clean, isImage: false };
53
102
  }
54
- prompt.editor.insert(text);
55
- if (typeof prompt.refreshCompletion === 'function') {
56
- prompt.refreshCompletion();
103
+ // 4. No fallbackText provided and no clipboard image: read text from clipboard
104
+ const clipText = normalizeClipboardText(readClipboardSync());
105
+ if (clipText) {
106
+ const filePath = extractPastedFilePath(clipText);
107
+ const localImg = loadLocalImageAttachment(filePath);
108
+ if (localImg) {
109
+ if (!Array.isArray(prompt.pendingAttachments)) {
110
+ prompt.pendingAttachments = [];
111
+ }
112
+ prompt.pendingAttachments.push(localImg);
113
+ const index = prompt.pendingAttachments.length;
114
+ const tag = `[Image ${index}] `;
115
+ prompt.editor.insert(tag);
116
+ if (typeof prompt.refreshCompletion === 'function') {
117
+ prompt.refreshCompletion();
118
+ }
119
+ prompt.notice = `attached image ${index} (${formatByteSize(localImg.size)})`;
120
+ prompt.scheduleDraw();
121
+ return { success: true, notice: prompt.notice, text: tag, isImage: true };
122
+ }
123
+ prompt.editor.insert(clipText);
124
+ if (typeof prompt.refreshCompletion === 'function') {
125
+ prompt.refreshCompletion();
126
+ }
127
+ prompt.notice = 'pasted from clipboard';
128
+ prompt.scheduleDraw();
129
+ return { success: true, notice: prompt.notice, text: clipText, isImage: false };
57
130
  }
58
- prompt.notice = 'pasted from clipboard';
131
+ // 5. Fallback to editor killRing
132
+ if (prompt.editor.killRing) {
133
+ prompt.editor.insert(prompt.editor.killRing);
134
+ if (typeof prompt.refreshCompletion === 'function') {
135
+ prompt.refreshCompletion();
136
+ }
137
+ prompt.notice = 'yanked from kill ring';
138
+ prompt.scheduleDraw();
139
+ return { success: true, notice: prompt.notice, text: prompt.editor.killRing, isImage: false };
140
+ }
141
+ prompt.notice = 'clipboard is empty';
59
142
  prompt.scheduleDraw();
60
- return { success: true, notice: prompt.notice, text };
143
+ return { success: false, notice: prompt.notice, text: '' };
61
144
  }
62
145
  /**
63
146
  * Extract the content of the last assistant turn from the conversation.
@@ -148,5 +231,8 @@ export function runPasteCommand(argv, ctx) {
148
231
  if (!result.success) {
149
232
  return { ok: false, output: 'Clipboard is empty or unavailable.' };
150
233
  }
234
+ if (result.isImage) {
235
+ return { ok: true, output: `Attached screenshot from clipboard (${result.text.trim()}).` };
236
+ }
151
237
  return { ok: true, output: `Pasted ${result.text.length} characters into chatbox input.` };
152
238
  }