@causyne/agent 1.20.60 → 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.
- package/README.md +2 -2
- package/package.json +1 -1
- package/src/acp/agentCatalog.js +1 -1
- package/src/acp/agentCatalog.ts +1 -1
- package/src/acp/client.js +48 -0
- package/src/acp/client.ts +44 -0
- package/src/acp/peerProfiles.js +3 -3
- package/src/acp/peerProfiles.ts +3 -3
- package/src/agentLoop.js +239 -33
- package/src/agentLoop.ts +248 -36
- package/src/checkpoints.js +23 -5
- package/src/cliMode.js +13 -2
- package/src/commands.js +534 -78
- package/src/contextManager.d.ts +3 -0
- package/src/contextManager.js +78 -5
- package/src/cortex/autoCondenser.js +105 -145
- package/src/cortex/autoCondenser.ts +185 -0
- package/src/cortex/cortexEngine.js +179 -0
- package/src/cortex/cortexEngine.ts +209 -0
- package/src/cortex/provisioner.js +12 -14
- package/src/cortex/provisioner.ts +29 -0
- package/src/cortex/vectorDb.js +307 -386
- package/src/cortex/vectorDb.ts +473 -0
- package/src/decisionPolicy.js +18 -1
- package/src/decisionPolicy.ts +18 -1
- package/src/implementationPlan.js +26 -0
- package/src/implementationPlan.ts +25 -0
- package/src/observer/coldObserver.js +6 -1
- package/src/observer/coldObserver.ts +6 -1
- package/src/observer.js +24 -2
- package/src/observer.ts +24 -2
- package/src/providers/acp.js +2 -8
- package/src/providers/acp.ts +2 -8
- package/src/providers/antigravity.js +7 -10
- package/src/providers/antigravity.ts +7 -10
- package/src/reporting.js +30 -7
- package/src/sessionJournal.js +13 -0
- package/src/sessionJournal.ts +13 -0
- package/src/smartToolsUse.js +1 -1
- package/src/smartToolsUse.ts +1 -1
- package/src/tokenTracker.js +22 -0
- package/src/tokenTracker.ts +28 -0
- package/src/toolPacks.js +1 -1
- package/src/toolPacks.ts +1 -1
- package/src/tools/toolSelfRepair.js +51 -8
- package/src/tools/toolSelfRepair.ts +42 -9
- package/src/tui/CausyneRotor.js +234 -0
- package/src/tui/CausyneRotor.ts +280 -0
- package/src/tui/activityLayout.js +1 -1
- package/src/tui/activityLayout.ts +1 -1
- package/src/tui/chatRenderer.js +62 -9
- package/src/tui/chatboxClipboard.js +96 -10
- package/src/tui/chatboxClipboard.ts +121 -11
- package/src/tui/clipboardImage.js +308 -0
- package/src/tui/clipboardImage.ts +320 -0
- package/src/tui/codeStreamRenderer.js +89 -1
- package/src/tui/codeStreamRenderer.ts +109 -1
- package/src/tui/completion.js +34 -15
- package/src/tui/composerControls.js +5 -0
- package/src/tui/composerControls.ts +5 -0
- package/src/tui/dialogs.js +5 -2
- package/src/tui/engineSpinner.js +20 -0
- package/src/tui/engineSpinner.ts +46 -0
- package/src/tui/layout.js +1 -1
- package/src/tui/layout.ts +1 -1
- package/src/tui/lineEditor.js +44 -12
- package/src/tui/messageInteraction.js +16 -5
- package/src/tui/messageInteraction.ts +14 -5
- package/src/tui/observerPicker.js +319 -0
- package/src/tui/observerPicker.ts +382 -0
- package/src/tui/prompt.js +247 -55
- package/src/tui/runningLight.js +213 -0
- package/src/tui/runningLight.ts +271 -0
- package/src/tui/sessionSidebar.js +7 -1
- package/src/tui/sessionSidebar.ts +7 -1
- package/src/tui/stage.js +4 -4
- package/src/tui/stageSurface.js +27 -10
- package/src/tui/statusLine.js +27 -4
- package/src/tui/statusLine.ts +27 -4
- package/src/tui/theme.js +5 -2
- package/src/tui/thinkingBox.js +41 -10
- package/src/tui/thinkingBox.ts +43 -10
- package/src/tui/turnRail.js +155 -24
- package/src/tui/turnRail.ts +170 -22
- package/src/tui/usageCard.js +178 -0
- package/src/tui/usageCard.ts +240 -0
- package/src/utils/paths.js +30 -1
- package/src/utils/paths.ts +32 -1
- package/src/cortex/autoCondenser.d.ts +0 -2
- package/src/cortex/vectorDb.d.ts +0 -1
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
// Sovereign Cross-Platform Clipboard Image Reader & Attachment Manager.
|
|
2
|
+
// Captures screenshots and images pasted from system clipboard across macOS (osascript),
|
|
3
|
+
// Windows/WSL (PowerShell System.Windows.Forms), and Linux (wl-paste, xclip).
|
|
4
|
+
// Provides seamless multimodal image attachment for TUI prompt editors and slash commands.
|
|
5
|
+
import { spawnSync } from 'node:child_process';
|
|
6
|
+
import fs from 'node:fs';
|
|
7
|
+
import path from 'node:path';
|
|
8
|
+
import os from 'node:os';
|
|
9
|
+
import { fileURLToPath } from 'node:url';
|
|
10
|
+
import { causyneHome, resolveUserPath } from '../utils/paths.js';
|
|
11
|
+
/**
|
|
12
|
+
* Format bytes into readable string: '128 KB', '1.5 MB'.
|
|
13
|
+
*/
|
|
14
|
+
export function formatByteSize(bytes) {
|
|
15
|
+
if (bytes < 1024)
|
|
16
|
+
return `${bytes} B`;
|
|
17
|
+
if (bytes < 1024 * 1024)
|
|
18
|
+
return `${Math.round(bytes / 1024)} KB`;
|
|
19
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Detect image mime type from binary magic bytes.
|
|
23
|
+
*/
|
|
24
|
+
export function detectImageMimeType(buffer) {
|
|
25
|
+
if (!buffer || buffer.length < 8)
|
|
26
|
+
return null;
|
|
27
|
+
// PNG: 89 50 4E 47 0D 0A 1A 0A
|
|
28
|
+
if (buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e && buffer[3] === 0x47) {
|
|
29
|
+
return 'image/png';
|
|
30
|
+
}
|
|
31
|
+
// JPEG: FF D8 FF
|
|
32
|
+
if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
|
33
|
+
return 'image/jpeg';
|
|
34
|
+
}
|
|
35
|
+
// GIF: 47 49 46 38
|
|
36
|
+
if (buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x38) {
|
|
37
|
+
return 'image/gif';
|
|
38
|
+
}
|
|
39
|
+
// WebP: 52 49 46 46 ... 57 45 42 50
|
|
40
|
+
if (buffer.length >= 12 &&
|
|
41
|
+
buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46 &&
|
|
42
|
+
buffer[8] === 0x57 && buffer[9] === 0x45 && buffer[10] === 0x42 && buffer[11] === 0x50) {
|
|
43
|
+
return 'image/webp';
|
|
44
|
+
}
|
|
45
|
+
return null;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Ensure the causyne attachments directory exists and return its path.
|
|
49
|
+
*/
|
|
50
|
+
export function getAttachmentsDirectory() {
|
|
51
|
+
const dir = path.join(causyneHome(), 'attachments');
|
|
52
|
+
if (!fs.existsSync(dir)) {
|
|
53
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
54
|
+
}
|
|
55
|
+
return dir;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Save raw image buffer to persistent attachments directory and return attachment object.
|
|
59
|
+
*/
|
|
60
|
+
export function persistImageBuffer(buffer, preferredName) {
|
|
61
|
+
const mimeType = detectImageMimeType(buffer);
|
|
62
|
+
if (!mimeType)
|
|
63
|
+
return null;
|
|
64
|
+
const ext = mimeType === 'image/jpeg' ? 'jpg'
|
|
65
|
+
: mimeType === 'image/webp' ? 'webp'
|
|
66
|
+
: mimeType === 'image/gif' ? 'gif'
|
|
67
|
+
: 'png';
|
|
68
|
+
const timestamp = Date.now();
|
|
69
|
+
const filename = preferredName
|
|
70
|
+
? path.basename(preferredName)
|
|
71
|
+
: `screenshot_${timestamp}.${ext}`;
|
|
72
|
+
const attachmentsDir = getAttachmentsDirectory();
|
|
73
|
+
const filePath = path.join(attachmentsDir, filename);
|
|
74
|
+
fs.writeFileSync(filePath, buffer, { mode: 0o600 });
|
|
75
|
+
return {
|
|
76
|
+
type: 'image',
|
|
77
|
+
mimeType,
|
|
78
|
+
base64: buffer.toString('base64'),
|
|
79
|
+
name: filename,
|
|
80
|
+
path: filePath,
|
|
81
|
+
size: buffer.length
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Read screenshot or image from macOS clipboard using built-in osascript.
|
|
86
|
+
* Supports both PNG and JPEG image data placed on clipboard by macOS, browsers, and terminals.
|
|
87
|
+
*/
|
|
88
|
+
function readDarwinClipboardImageSync() {
|
|
89
|
+
const tempFile = path.join('/tmp', `causyne_clip_${Date.now()}_${Math.random().toString(36).slice(2, 8)}.png`);
|
|
90
|
+
// Script uses standard AppleScript POSIX file API with safe error handling and file descriptor close guard
|
|
91
|
+
const script = [
|
|
92
|
+
'try',
|
|
93
|
+
' set imageData to (the clipboard as "PNGf")',
|
|
94
|
+
` set fileRef to open for access POSIX file "${tempFile}" with write permission`,
|
|
95
|
+
' set eof fileRef to 0',
|
|
96
|
+
' write imageData to fileRef',
|
|
97
|
+
' close access fileRef',
|
|
98
|
+
' return "ok"',
|
|
99
|
+
'on error',
|
|
100
|
+
' try',
|
|
101
|
+
` close access (POSIX file "${tempFile}")`,
|
|
102
|
+
' end try',
|
|
103
|
+
' try',
|
|
104
|
+
' set imageData to (the clipboard as JPEG picture)',
|
|
105
|
+
` set fileRef to open for access POSIX file "${tempFile}" with write permission`,
|
|
106
|
+
' set eof fileRef to 0',
|
|
107
|
+
' write imageData to fileRef',
|
|
108
|
+
' close access fileRef',
|
|
109
|
+
' return "ok"',
|
|
110
|
+
' on error',
|
|
111
|
+
' try',
|
|
112
|
+
` close access (POSIX file "${tempFile}")`,
|
|
113
|
+
' end try',
|
|
114
|
+
' return ""',
|
|
115
|
+
' end try',
|
|
116
|
+
'end try'
|
|
117
|
+
].join('\n');
|
|
118
|
+
try {
|
|
119
|
+
const res = spawnSync('osascript', ['-e', script], {
|
|
120
|
+
encoding: 'utf8',
|
|
121
|
+
timeout: 3000,
|
|
122
|
+
windowsHide: true
|
|
123
|
+
});
|
|
124
|
+
if (res.status === 0 && res.stdout.trim() === 'ok' && fs.existsSync(tempFile)) {
|
|
125
|
+
const buf = fs.readFileSync(tempFile);
|
|
126
|
+
return buf.length > 0 ? buf : null;
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
catch {
|
|
130
|
+
// osascript clipboard read is best-effort
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
try {
|
|
134
|
+
if (fs.existsSync(tempFile))
|
|
135
|
+
fs.unlinkSync(tempFile);
|
|
136
|
+
}
|
|
137
|
+
catch {
|
|
138
|
+
// Ignore cleanup error
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
return null;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Read screenshot or image from Windows or WSL clipboard via PowerShell.
|
|
145
|
+
*/
|
|
146
|
+
function readWindowsClipboardImageSync() {
|
|
147
|
+
const command = process.platform === 'win32' ? 'powershell.exe' : 'powershell.exe';
|
|
148
|
+
const psScript = 'Add-Type -AssemblyName System.Windows.Forms; $img = [System.Windows.Forms.Clipboard]::GetImage(); if ($img) { $ms = New-Object System.IO.MemoryStream; $img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png); [System.Convert]::ToBase64String($ms.ToArray()) }';
|
|
149
|
+
try {
|
|
150
|
+
const res = spawnSync(command, ['-NonInteractive', '-NoProfile', '-Command', psScript], {
|
|
151
|
+
encoding: 'utf8',
|
|
152
|
+
timeout: 4000,
|
|
153
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
154
|
+
windowsHide: true
|
|
155
|
+
});
|
|
156
|
+
if (res.status === 0 && res.stdout) {
|
|
157
|
+
const b64 = res.stdout.trim();
|
|
158
|
+
if (b64.length > 0) {
|
|
159
|
+
const buf = Buffer.from(b64, 'base64');
|
|
160
|
+
return buf.length > 0 ? buf : null;
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
catch {
|
|
165
|
+
// PowerShell clipboard image is best-effort
|
|
166
|
+
}
|
|
167
|
+
return null;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Read screenshot or image from Linux clipboard via Wayland (wl-paste) or X11 (xclip).
|
|
171
|
+
*/
|
|
172
|
+
function readLinuxClipboardImageSync() {
|
|
173
|
+
// Try Wayland first
|
|
174
|
+
try {
|
|
175
|
+
const wl = spawnSync('wl-paste', ['-t', 'image/png'], {
|
|
176
|
+
timeout: 2500,
|
|
177
|
+
maxBuffer: 32 * 1024 * 1024
|
|
178
|
+
});
|
|
179
|
+
if (wl.status === 0 && Buffer.isBuffer(wl.stdout) && wl.stdout.length > 0) {
|
|
180
|
+
return wl.stdout;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
catch {
|
|
184
|
+
// Fall through to X11
|
|
185
|
+
}
|
|
186
|
+
// Try X11 xclip
|
|
187
|
+
try {
|
|
188
|
+
const xc = spawnSync('xclip', ['-selection', 'clipboard', '-t', 'image/png', '-o'], {
|
|
189
|
+
timeout: 2500,
|
|
190
|
+
maxBuffer: 32 * 1024 * 1024
|
|
191
|
+
});
|
|
192
|
+
if (xc.status === 0 && Buffer.isBuffer(xc.stdout) && xc.stdout.length > 0) {
|
|
193
|
+
return xc.stdout;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
catch {
|
|
197
|
+
// Fall through
|
|
198
|
+
}
|
|
199
|
+
return null;
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Synchronously read screenshot or image from the system clipboard.
|
|
203
|
+
* Returns ClipboardImageAttachment if an image is present, or null if none.
|
|
204
|
+
*/
|
|
205
|
+
export function readClipboardImageSync() {
|
|
206
|
+
try {
|
|
207
|
+
let buf = null;
|
|
208
|
+
if (process.platform === 'darwin') {
|
|
209
|
+
buf = readDarwinClipboardImageSync();
|
|
210
|
+
}
|
|
211
|
+
else if (process.platform === 'win32' || (os.release().toLowerCase().includes('wsl'))) {
|
|
212
|
+
buf = readWindowsClipboardImageSync();
|
|
213
|
+
}
|
|
214
|
+
else if (process.platform === 'linux') {
|
|
215
|
+
buf = readLinuxClipboardImageSync();
|
|
216
|
+
}
|
|
217
|
+
if (buf && buf.length > 0) {
|
|
218
|
+
return persistImageBuffer(buf);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
catch {
|
|
222
|
+
// Non-throwing guard
|
|
223
|
+
}
|
|
224
|
+
return null;
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* Clean up and extract a valid local file path from pasted text or drag-and-drop strings.
|
|
228
|
+
* Strips quotes, file:// URIs, and backslash escape sequences.
|
|
229
|
+
*/
|
|
230
|
+
export function extractPastedFilePath(input) {
|
|
231
|
+
let raw = String(input ?? '').trim();
|
|
232
|
+
// Strip surrounding quotes
|
|
233
|
+
while (raw.length >= 2 && ((raw.startsWith('"') && raw.endsWith('"')) || (raw.startsWith("'") && raw.endsWith("'")))) {
|
|
234
|
+
raw = raw.slice(1, -1).trim();
|
|
235
|
+
}
|
|
236
|
+
// Handle file:// URI
|
|
237
|
+
if (raw.startsWith('file://')) {
|
|
238
|
+
try {
|
|
239
|
+
raw = fileURLToPath(raw);
|
|
240
|
+
}
|
|
241
|
+
catch {
|
|
242
|
+
// ignore
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
// Unescape backslash-escaped spaces/characters on Unix, and backslash-escaped spaces on Windows
|
|
246
|
+
if (process.platform !== 'win32') {
|
|
247
|
+
raw = raw.replace(/\\(.)/g, '$1');
|
|
248
|
+
}
|
|
249
|
+
else {
|
|
250
|
+
raw = raw.replace(/\\ /g, ' ');
|
|
251
|
+
}
|
|
252
|
+
return raw;
|
|
253
|
+
}
|
|
254
|
+
const KNOWN_IMAGE_EXTENSIONS = new Set([
|
|
255
|
+
'.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.tiff', '.tif'
|
|
256
|
+
]);
|
|
257
|
+
/**
|
|
258
|
+
* Check if a file path corresponds to an existing local image file.
|
|
259
|
+
*/
|
|
260
|
+
export function isLocalImageFile(rawPath) {
|
|
261
|
+
try {
|
|
262
|
+
const cleaned = extractPastedFilePath(rawPath);
|
|
263
|
+
if (!cleaned)
|
|
264
|
+
return false;
|
|
265
|
+
const resolved = resolveUserPath(cleaned);
|
|
266
|
+
const ext = path.extname(resolved).toLowerCase();
|
|
267
|
+
if (!KNOWN_IMAGE_EXTENSIONS.has(ext))
|
|
268
|
+
return false;
|
|
269
|
+
if (!fs.existsSync(resolved))
|
|
270
|
+
return false;
|
|
271
|
+
const stat = fs.statSync(resolved);
|
|
272
|
+
return stat.isFile() && stat.size > 0 && stat.size <= 25 * 1024 * 1024;
|
|
273
|
+
}
|
|
274
|
+
catch {
|
|
275
|
+
return false;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
/**
|
|
279
|
+
* Load a local image file as an attachment (e.g. from /image or /attach command).
|
|
280
|
+
*/
|
|
281
|
+
export function loadLocalImageAttachment(rawPath) {
|
|
282
|
+
try {
|
|
283
|
+
const cleaned = extractPastedFilePath(rawPath);
|
|
284
|
+
if (!cleaned)
|
|
285
|
+
return null;
|
|
286
|
+
const resolved = resolveUserPath(cleaned);
|
|
287
|
+
if (!fs.existsSync(resolved))
|
|
288
|
+
return null;
|
|
289
|
+
const stat = fs.statSync(resolved);
|
|
290
|
+
if (!stat.isFile() || stat.size > 25 * 1024 * 1024)
|
|
291
|
+
return null; // 25 MB ceiling
|
|
292
|
+
const buffer = fs.readFileSync(resolved);
|
|
293
|
+
const mimeType = detectImageMimeType(buffer);
|
|
294
|
+
if (!mimeType)
|
|
295
|
+
return null;
|
|
296
|
+
return {
|
|
297
|
+
type: 'image',
|
|
298
|
+
mimeType,
|
|
299
|
+
base64: buffer.toString('base64'),
|
|
300
|
+
name: path.basename(resolved),
|
|
301
|
+
path: resolved,
|
|
302
|
+
size: buffer.length
|
|
303
|
+
};
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
@@ -0,0 +1,320 @@
|
|
|
1
|
+
// Sovereign Cross-Platform Clipboard Image Reader & Attachment Manager.
|
|
2
|
+
// Captures screenshots and images pasted from system clipboard across macOS (osascript),
|
|
3
|
+
// Windows/WSL (PowerShell System.Windows.Forms), and Linux (wl-paste, xclip).
|
|
4
|
+
// Provides seamless multimodal image attachment for TUI prompt editors and slash commands.
|
|
5
|
+
|
|
6
|
+
import { spawnSync } from 'node:child_process';
|
|
7
|
+
import fs from 'node:fs';
|
|
8
|
+
import path from 'node:path';
|
|
9
|
+
import os from 'node:os';
|
|
10
|
+
import { fileURLToPath } from 'node:url';
|
|
11
|
+
import { causyneHome, resolveUserPath } from '../utils/paths.js';
|
|
12
|
+
|
|
13
|
+
export interface ClipboardImageAttachment {
|
|
14
|
+
type: 'image';
|
|
15
|
+
mimeType: 'image/png' | 'image/jpeg' | 'image/webp' | 'image/gif';
|
|
16
|
+
base64: string;
|
|
17
|
+
name: string;
|
|
18
|
+
path: string;
|
|
19
|
+
size: number;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Format bytes into readable string: '128 KB', '1.5 MB'.
|
|
24
|
+
*/
|
|
25
|
+
export function formatByteSize(bytes: number): string {
|
|
26
|
+
if (bytes < 1024) return `${bytes} B`;
|
|
27
|
+
if (bytes < 1024 * 1024) return `${Math.round(bytes / 1024)} KB`;
|
|
28
|
+
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* Detect image mime type from binary magic bytes.
|
|
33
|
+
*/
|
|
34
|
+
export function detectImageMimeType(buffer: Buffer): 'image/png' | 'image/jpeg' | 'image/webp' | 'image/gif' | null {
|
|
35
|
+
if (!buffer || buffer.length < 8) return null;
|
|
36
|
+
// PNG: 89 50 4E 47 0D 0A 1A 0A
|
|
37
|
+
if (buffer[0] === 0x89 && buffer[1] === 0x50 && buffer[2] === 0x4e && buffer[3] === 0x47) {
|
|
38
|
+
return 'image/png';
|
|
39
|
+
}
|
|
40
|
+
// JPEG: FF D8 FF
|
|
41
|
+
if (buffer[0] === 0xff && buffer[1] === 0xd8 && buffer[2] === 0xff) {
|
|
42
|
+
return 'image/jpeg';
|
|
43
|
+
}
|
|
44
|
+
// GIF: 47 49 46 38
|
|
45
|
+
if (buffer[0] === 0x47 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x38) {
|
|
46
|
+
return 'image/gif';
|
|
47
|
+
}
|
|
48
|
+
// WebP: 52 49 46 46 ... 57 45 42 50
|
|
49
|
+
if (buffer.length >= 12 &&
|
|
50
|
+
buffer[0] === 0x52 && buffer[1] === 0x49 && buffer[2] === 0x46 && buffer[3] === 0x46 &&
|
|
51
|
+
buffer[8] === 0x57 && buffer[9] === 0x45 && buffer[10] === 0x42 && buffer[11] === 0x50) {
|
|
52
|
+
return 'image/webp';
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Ensure the causyne attachments directory exists and return its path.
|
|
59
|
+
*/
|
|
60
|
+
export function getAttachmentsDirectory(): string {
|
|
61
|
+
const dir = path.join(causyneHome(), 'attachments');
|
|
62
|
+
if (!fs.existsSync(dir)) {
|
|
63
|
+
fs.mkdirSync(dir, { recursive: true, mode: 0o700 });
|
|
64
|
+
}
|
|
65
|
+
return dir;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Save raw image buffer to persistent attachments directory and return attachment object.
|
|
70
|
+
*/
|
|
71
|
+
export function persistImageBuffer(
|
|
72
|
+
buffer: Buffer,
|
|
73
|
+
preferredName?: string
|
|
74
|
+
): ClipboardImageAttachment | null {
|
|
75
|
+
const mimeType = detectImageMimeType(buffer);
|
|
76
|
+
if (!mimeType) return null;
|
|
77
|
+
|
|
78
|
+
const ext = mimeType === 'image/jpeg' ? 'jpg'
|
|
79
|
+
: mimeType === 'image/webp' ? 'webp'
|
|
80
|
+
: mimeType === 'image/gif' ? 'gif'
|
|
81
|
+
: 'png';
|
|
82
|
+
|
|
83
|
+
const timestamp = Date.now();
|
|
84
|
+
const filename = preferredName
|
|
85
|
+
? path.basename(preferredName)
|
|
86
|
+
: `screenshot_${timestamp}.${ext}`;
|
|
87
|
+
|
|
88
|
+
const attachmentsDir = getAttachmentsDirectory();
|
|
89
|
+
const filePath = path.join(attachmentsDir, filename);
|
|
90
|
+
|
|
91
|
+
fs.writeFileSync(filePath, buffer, { mode: 0o600 });
|
|
92
|
+
|
|
93
|
+
return {
|
|
94
|
+
type: 'image',
|
|
95
|
+
mimeType,
|
|
96
|
+
base64: buffer.toString('base64'),
|
|
97
|
+
name: filename,
|
|
98
|
+
path: filePath,
|
|
99
|
+
size: buffer.length
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Read screenshot or image from macOS clipboard using built-in osascript.
|
|
105
|
+
* Supports both PNG and JPEG image data placed on clipboard by macOS, browsers, and terminals.
|
|
106
|
+
*/
|
|
107
|
+
function readDarwinClipboardImageSync(): Buffer | null {
|
|
108
|
+
const tempFile = path.join('/tmp', `causyne_clip_${Date.now()}_${Math.random().toString(36).slice(2, 8)}.png`);
|
|
109
|
+
// Script uses standard AppleScript POSIX file API with safe error handling and file descriptor close guard
|
|
110
|
+
const script = [
|
|
111
|
+
'try',
|
|
112
|
+
' set imageData to (the clipboard as "PNGf")',
|
|
113
|
+
` set fileRef to open for access POSIX file "${tempFile}" with write permission`,
|
|
114
|
+
' set eof fileRef to 0',
|
|
115
|
+
' write imageData to fileRef',
|
|
116
|
+
' close access fileRef',
|
|
117
|
+
' return "ok"',
|
|
118
|
+
'on error',
|
|
119
|
+
' try',
|
|
120
|
+
` close access (POSIX file "${tempFile}")`,
|
|
121
|
+
' end try',
|
|
122
|
+
' try',
|
|
123
|
+
' set imageData to (the clipboard as JPEG picture)',
|
|
124
|
+
` set fileRef to open for access POSIX file "${tempFile}" with write permission`,
|
|
125
|
+
' set eof fileRef to 0',
|
|
126
|
+
' write imageData to fileRef',
|
|
127
|
+
' close access fileRef',
|
|
128
|
+
' return "ok"',
|
|
129
|
+
' on error',
|
|
130
|
+
' try',
|
|
131
|
+
` close access (POSIX file "${tempFile}")`,
|
|
132
|
+
' end try',
|
|
133
|
+
' return ""',
|
|
134
|
+
' end try',
|
|
135
|
+
'end try'
|
|
136
|
+
].join('\n');
|
|
137
|
+
|
|
138
|
+
try {
|
|
139
|
+
const res = spawnSync('osascript', ['-e', script], {
|
|
140
|
+
encoding: 'utf8',
|
|
141
|
+
timeout: 3000,
|
|
142
|
+
windowsHide: true
|
|
143
|
+
});
|
|
144
|
+
if (res.status === 0 && res.stdout.trim() === 'ok' && fs.existsSync(tempFile)) {
|
|
145
|
+
const buf = fs.readFileSync(tempFile);
|
|
146
|
+
return buf.length > 0 ? buf : null;
|
|
147
|
+
}
|
|
148
|
+
} catch {
|
|
149
|
+
// osascript clipboard read is best-effort
|
|
150
|
+
} finally {
|
|
151
|
+
try {
|
|
152
|
+
if (fs.existsSync(tempFile)) fs.unlinkSync(tempFile);
|
|
153
|
+
} catch {
|
|
154
|
+
// Ignore cleanup error
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/**
|
|
161
|
+
* Read screenshot or image from Windows or WSL clipboard via PowerShell.
|
|
162
|
+
*/
|
|
163
|
+
function readWindowsClipboardImageSync(): Buffer | null {
|
|
164
|
+
const command = process.platform === 'win32' ? 'powershell.exe' : 'powershell.exe';
|
|
165
|
+
const psScript = 'Add-Type -AssemblyName System.Windows.Forms; $img = [System.Windows.Forms.Clipboard]::GetImage(); if ($img) { $ms = New-Object System.IO.MemoryStream; $img.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png); [System.Convert]::ToBase64String($ms.ToArray()) }';
|
|
166
|
+
|
|
167
|
+
try {
|
|
168
|
+
const res = spawnSync(command, ['-NonInteractive', '-NoProfile', '-Command', psScript], {
|
|
169
|
+
encoding: 'utf8',
|
|
170
|
+
timeout: 4000,
|
|
171
|
+
maxBuffer: 32 * 1024 * 1024,
|
|
172
|
+
windowsHide: true
|
|
173
|
+
});
|
|
174
|
+
if (res.status === 0 && res.stdout) {
|
|
175
|
+
const b64 = res.stdout.trim();
|
|
176
|
+
if (b64.length > 0) {
|
|
177
|
+
const buf = Buffer.from(b64, 'base64');
|
|
178
|
+
return buf.length > 0 ? buf : null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
} catch {
|
|
182
|
+
// PowerShell clipboard image is best-effort
|
|
183
|
+
}
|
|
184
|
+
return null;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
/**
|
|
188
|
+
* Read screenshot or image from Linux clipboard via Wayland (wl-paste) or X11 (xclip).
|
|
189
|
+
*/
|
|
190
|
+
function readLinuxClipboardImageSync(): Buffer | null {
|
|
191
|
+
// Try Wayland first
|
|
192
|
+
try {
|
|
193
|
+
const wl = spawnSync('wl-paste', ['-t', 'image/png'], {
|
|
194
|
+
timeout: 2500,
|
|
195
|
+
maxBuffer: 32 * 1024 * 1024
|
|
196
|
+
});
|
|
197
|
+
if (wl.status === 0 && Buffer.isBuffer(wl.stdout) && wl.stdout.length > 0) {
|
|
198
|
+
return wl.stdout;
|
|
199
|
+
}
|
|
200
|
+
} catch {
|
|
201
|
+
// Fall through to X11
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
// Try X11 xclip
|
|
205
|
+
try {
|
|
206
|
+
const xc = spawnSync('xclip', ['-selection', 'clipboard', '-t', 'image/png', '-o'], {
|
|
207
|
+
timeout: 2500,
|
|
208
|
+
maxBuffer: 32 * 1024 * 1024
|
|
209
|
+
});
|
|
210
|
+
if (xc.status === 0 && Buffer.isBuffer(xc.stdout) && xc.stdout.length > 0) {
|
|
211
|
+
return xc.stdout;
|
|
212
|
+
}
|
|
213
|
+
} catch {
|
|
214
|
+
// Fall through
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
return null;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/**
|
|
221
|
+
* Synchronously read screenshot or image from the system clipboard.
|
|
222
|
+
* Returns ClipboardImageAttachment if an image is present, or null if none.
|
|
223
|
+
*/
|
|
224
|
+
export function readClipboardImageSync(): ClipboardImageAttachment | null {
|
|
225
|
+
try {
|
|
226
|
+
let buf: Buffer | null = null;
|
|
227
|
+
if (process.platform === 'darwin') {
|
|
228
|
+
buf = readDarwinClipboardImageSync();
|
|
229
|
+
} else if (process.platform === 'win32' || (os.release().toLowerCase().includes('wsl'))) {
|
|
230
|
+
buf = readWindowsClipboardImageSync();
|
|
231
|
+
} else if (process.platform === 'linux') {
|
|
232
|
+
buf = readLinuxClipboardImageSync();
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (buf && buf.length > 0) {
|
|
236
|
+
return persistImageBuffer(buf);
|
|
237
|
+
}
|
|
238
|
+
} catch {
|
|
239
|
+
// Non-throwing guard
|
|
240
|
+
}
|
|
241
|
+
return null;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
/**
|
|
245
|
+
* Clean up and extract a valid local file path from pasted text or drag-and-drop strings.
|
|
246
|
+
* Strips quotes, file:// URIs, and backslash escape sequences.
|
|
247
|
+
*/
|
|
248
|
+
export function extractPastedFilePath(input: string): string {
|
|
249
|
+
let raw = String(input ?? '').trim();
|
|
250
|
+
// Strip surrounding quotes
|
|
251
|
+
while (raw.length >= 2 && ((raw.startsWith('"') && raw.endsWith('"')) || (raw.startsWith("'") && raw.endsWith("'")))) {
|
|
252
|
+
raw = raw.slice(1, -1).trim();
|
|
253
|
+
}
|
|
254
|
+
// Handle file:// URI
|
|
255
|
+
if (raw.startsWith('file://')) {
|
|
256
|
+
try {
|
|
257
|
+
raw = fileURLToPath(raw);
|
|
258
|
+
} catch {
|
|
259
|
+
// ignore
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
// Unescape backslash-escaped spaces/characters on Unix, and backslash-escaped spaces on Windows
|
|
263
|
+
if (process.platform !== 'win32') {
|
|
264
|
+
raw = raw.replace(/\\(.)/g, '$1');
|
|
265
|
+
} else {
|
|
266
|
+
raw = raw.replace(/\\ /g, ' ');
|
|
267
|
+
}
|
|
268
|
+
return raw;
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const KNOWN_IMAGE_EXTENSIONS = new Set([
|
|
272
|
+
'.png', '.jpg', '.jpeg', '.webp', '.gif', '.bmp', '.tiff', '.tif'
|
|
273
|
+
]);
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Check if a file path corresponds to an existing local image file.
|
|
277
|
+
*/
|
|
278
|
+
export function isLocalImageFile(rawPath: string): boolean {
|
|
279
|
+
try {
|
|
280
|
+
const cleaned = extractPastedFilePath(rawPath);
|
|
281
|
+
if (!cleaned) return false;
|
|
282
|
+
const resolved = resolveUserPath(cleaned);
|
|
283
|
+
const ext = path.extname(resolved).toLowerCase();
|
|
284
|
+
if (!KNOWN_IMAGE_EXTENSIONS.has(ext)) return false;
|
|
285
|
+
if (!fs.existsSync(resolved)) return false;
|
|
286
|
+
const stat = fs.statSync(resolved);
|
|
287
|
+
return stat.isFile() && stat.size > 0 && stat.size <= 25 * 1024 * 1024;
|
|
288
|
+
} catch {
|
|
289
|
+
return false;
|
|
290
|
+
}
|
|
291
|
+
}
|
|
292
|
+
|
|
293
|
+
/**
|
|
294
|
+
* Load a local image file as an attachment (e.g. from /image or /attach command).
|
|
295
|
+
*/
|
|
296
|
+
export function loadLocalImageAttachment(rawPath: string): ClipboardImageAttachment | null {
|
|
297
|
+
try {
|
|
298
|
+
const cleaned = extractPastedFilePath(rawPath);
|
|
299
|
+
if (!cleaned) return null;
|
|
300
|
+
const resolved = resolveUserPath(cleaned);
|
|
301
|
+
if (!fs.existsSync(resolved)) return null;
|
|
302
|
+
const stat = fs.statSync(resolved);
|
|
303
|
+
if (!stat.isFile() || stat.size > 25 * 1024 * 1024) return null; // 25 MB ceiling
|
|
304
|
+
|
|
305
|
+
const buffer = fs.readFileSync(resolved);
|
|
306
|
+
const mimeType = detectImageMimeType(buffer);
|
|
307
|
+
if (!mimeType) return null;
|
|
308
|
+
|
|
309
|
+
return {
|
|
310
|
+
type: 'image',
|
|
311
|
+
mimeType,
|
|
312
|
+
base64: buffer.toString('base64'),
|
|
313
|
+
name: path.basename(resolved),
|
|
314
|
+
path: resolved,
|
|
315
|
+
size: buffer.length
|
|
316
|
+
};
|
|
317
|
+
} catch {
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
320
|
+
}
|