u-foo 3.0.2 → 3.0.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "u-foo",
3
- "version": "3.0.2",
3
+ "version": "3.0.3",
4
4
  "description": "Multi-Agent Workspace Protocol. Just add u. claude → uclaude, codex → ucodex.",
5
5
  "license": "SEE LICENSE IN LICENSE",
6
6
  "homepage": "https://ufoo.dev",
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Tool description for read_image (workspace vision).
5
+ */
6
+
7
+ const READ_IMAGE_TOOL_NAME = "read_image";
8
+
9
+ function getReadImageToolDescription() {
10
+ return `Read an image file from the workspace so the model can see it.
11
+
12
+ Usage notes:
13
+ - The path parameter is relative to the workspace root.
14
+ - Supports png, jpeg, gif, and webp. Max size ~5MB.
15
+ - Use this instead of read for screenshots, UI mocks, diagrams, and other binary images.
16
+ - Text files still use read. Do not call read_image on non-image files.
17
+ - Vision is attached for the current model call only; call read_image again if you need the image later.`;
18
+ }
19
+
20
+ module.exports = {
21
+ READ_IMAGE_TOOL_NAME,
22
+ getReadImageToolDescription,
23
+ };
@@ -34,6 +34,7 @@ const {
34
34
  const { renderExecutionSegmentContext } = require("./executionSegment");
35
35
  const { renderPlanModeContext } = require("./planMode");
36
36
  const { drainAgentMailboxForTurn } = require("../runtime/agentWakeup");
37
+ const { stripVisionBase64, degradeVisionContent } = require("../providers/visionBlocks");
37
38
 
38
39
  const DEFAULT_TRANSCRIPT_WINDOW = 12;
39
40
  const DEFAULT_RECENT_TOOL_EVENTS = 4;
@@ -291,6 +292,19 @@ function sanitizeModelMessages(messages = []) {
291
292
  continue;
292
293
  }
293
294
 
295
+ // Drop ephemeral OpenAI vision companion user messages (image_url data URIs)
296
+ // when rebuilding history so base64 does not re-enter later turns.
297
+ if (role === "user" && Array.isArray(message.content)) {
298
+ const hasImageUrl = message.content.some((block) => (
299
+ block && String(block.type || "").trim().toLowerCase() === "image_url"
300
+ ));
301
+ if (hasImageUrl) {
302
+ const degraded = degradeVisionContent(message.content);
303
+ out.push({ role: "user", content: degraded });
304
+ continue;
305
+ }
306
+ }
307
+
294
308
  out.push(message);
295
309
  }
296
310
  return out;
@@ -435,16 +449,18 @@ function persistToolResultToContext({
435
449
  rawResult = {},
436
450
  segmentId = "",
437
451
  } = {}) {
452
+ const rawForStorage = stripVisionBase64(rawResult);
438
453
  const saved = saveArtifact(workspaceRoot, sessionId, {
439
454
  type: "tool_result",
440
455
  tool,
441
456
  args,
442
- raw: rawResult,
457
+ raw: rawForStorage,
443
458
  createdBy: tool,
444
459
  });
445
460
  const artifactId = saved.artifact && saved.artifact.artifactId
446
461
  ? saved.artifact.artifactId
447
462
  : "";
463
+ // Reduce from the live result so vision base64 stays available for this turn.
448
464
  const reduced = reduceToolResult(tool, rawResult, artifactId, args);
449
465
  return {
450
466
  artifactId,
@@ -181,7 +181,7 @@ function formatPlanModeStatus(executionState = null) {
181
181
  lines.push("Rules while ON:");
182
182
  lines.push(" - Use plan_graph to create/expand/complete the plan");
183
183
  lines.push(" - write / edit / bash are blocked as direct tools");
184
- lines.push(" - read / artifact_read allowed for exploration");
184
+ lines.push(" - read / read_image / artifact_read allowed for exploration");
185
185
  lines.push(" - Runtime auto-advances ready tool nodes after plan_graph");
186
186
  lines.push(" - User leaves with /plan off (agents cannot toggle Plan Mode)");
187
187
  } else {
@@ -223,7 +223,7 @@ function renderPlanModeContext(executionState = null) {
223
223
  }
224
224
 
225
225
  lines.push(
226
- "Allowed direct tools: read, artifact_read, plan_graph, ask_user.",
226
+ "Allowed direct tools: read, read_image, artifact_read, plan_graph, ask_user.",
227
227
  "Blocked direct tools: write, edit, bash (route them as plan_graph tool nodes or task_loop).",
228
228
  "Plan Mode constrains the Agent Loop only; running TaskLoops are not paused or reconfigured by /plan off.",
229
229
  "Only the user can leave Plan Mode (/plan off). That does not cancel the graph or TaskRuns — use cancel_graph / control.cancel_task.",
@@ -31,7 +31,7 @@ const {
31
31
  } = require("../skills");
32
32
  const { hashContent } = require("./artifacts");
33
33
 
34
- const PROMPT_VERSION = "native-v6";
34
+ const PROMPT_VERSION = "native-v7";
35
35
 
36
36
  function buildImmutablePrefix() {
37
37
  return [
@@ -44,23 +44,24 @@ function buildImmutablePrefix() {
44
44
  getOutputEfficiencySection(),
45
45
  [
46
46
  "Tool calling grammar:",
47
- "- Use read, write, edit, bash, and artifact_read for direct, single-goal work, even when it takes several tool calls.",
47
+ "- Use read, write, edit, bash, and artifact_read for direct, single-goal text work, even when it takes several tool calls.",
48
+ "- Use read_image to load workspace png/jpeg/gif/webp images for vision. Do not use read on binary images. Vision is attached for the active model call only; call read_image again if you need the image later.",
48
49
  "- TaskRuns are orthogonal to Plan Mode. A TaskRun does not require Plan Mode or a plan_graph. Use task_run operation=start with an objective for a standalone single-point TaskRun; it starts asynchronously and returns immediately.",
49
50
  "- On complex or multi-goal requests, automatically decompose into concrete sub-objectives and start TaskRun(s) via task_run. Prefer task_run for independent or loosely coupled tracks; use plan_graph only when you need durable dependencies, checkpoints, or a shared executable plan.",
50
51
  "- Use plan_graph for durable graph structure: create, patch, inspect, cancel_graph, and control. Graph-bound TaskRuns use plan_graph control.start_task on execution.kind=task_loop nodes.",
51
- "- Plan Mode is a runtime posture for the Agent Loop, not an agent tool. While Plan Mode is ON, direct write, edit, and bash calls from the Agent Loop are blocked; read and artifact_read remain available. TaskRuns still run independently of Plan Mode.",
52
+ "- Plan Mode is a runtime posture for the Agent Loop, not an agent tool. While Plan Mode is ON, direct write, edit, and bash calls from the Agent Loop are blocked; read, read_image, and artifact_read remain available. TaskRuns still run independently of Plan Mode.",
52
53
  "- In the Agent Loop, plan_graph operation=create automatically enables Plan Mode. The user may also use /plan on or /plan off.",
53
54
  "- Turning Plan Mode off does not cancel an existing graph or running TaskRuns. Cancel with task_run (standalone) or plan_graph operation=cancel_graph / control.cancel_task (graph-bound).",
54
55
  "- When the user enables Plan Mode and no active graph exists, create a plan_graph before performing side effects.",
55
56
  "- After an accepted plan_graph create or patch, Runtime automatically advances ready tool nodes. Never invent or request an execute_graph tool.",
56
- "- Do not call plan_graph or task_run together with read, write, edit, bash, or artifact_read in the same assistant turn.",
57
+ "- Do not call plan_graph or task_run together with read, read_image, write, edit, bash, or artifact_read in the same assistant turn.",
57
58
  "- When an active graph is waiting on a task, advance that node through plan_graph instead of bypassing it with direct workspace tools: use patch.expand_node for execution.kind=expand, control.complete_task (nodeId) for execution.kind=inline_llm, or control.start_task for execution.kind=task_loop.",
58
59
  "- control.complete_task with nodeId completes a waiting_llm inline_llm task for the current Graph owner. control.complete_task with taskRunId (or task_run complete) is reserved for the owning TaskLoop. Do not directly complete expand or aggregate tasks.",
59
60
  "- While Plan Mode is ON, workspace mutations must be represented as plan_graph tool nodes or performed inside a running TaskRun/task_loop.",
60
61
  "- Treat a User reminder as the latest user instruction. Reconcile it before continuing from tool results. If it is compatible with the active plan, resume the waiting plan node; otherwise patch, cancel, or replan first.",
61
62
  "- TaskLoops do not consume User reminders. The Agent Loop is woken by runtime task_started, task_succeeded, task_failed, and task_cancelled events.",
62
63
  "- Runtime enforces TaskRun concurrency limits and workspace write leases. Direct Agent write, edit, or bash calls may be rejected while writing TaskRuns are active.",
63
- "- Tool results may contain an artifactId. Use artifact_read to hydrate raw stored output or a slice of it; use read for workspace file paths.",
64
+ "- Tool results may contain an artifactId. Use artifact_read to hydrate raw stored output or a slice of it; use read for workspace text paths; use read_image for workspace images.",
64
65
  "- Use ask_user only when user input is required to proceed. It must be the only tool call in the turn. Use kind=approval for yes/no confirmation, kind=choice for numbered options, and kind=chat for free text.",
65
66
  "- The answer to ask_user is returned only as that tool's result, not as a separate user message or pending User reminder. Continue from the returned answer and do not repeat the question.",
66
67
  "- ask_user is available only to the Agent Loop. It pauses the Agent Loop, but running TaskRuns continue unless explicitly cancelled.",
@@ -287,9 +287,43 @@ function reduceArtifactReadResult(raw = {}, artifactId = "") {
287
287
  };
288
288
  }
289
289
 
290
+ function reduceReadImageResult(raw = {}, artifactId = "") {
291
+ const source = raw && typeof raw === "object" ? raw : {};
292
+ const pathText = String(source.path || "").trim();
293
+ const mediaType = String(source.mediaType || "").trim();
294
+ const bytes = Number.isFinite(source.bytes) ? source.bytes : null;
295
+ const preview = source.ok === false
296
+ ? clipText(String(source.error || "read_image failed"), PREVIEW_MAX_CHARS)
297
+ : clipText(
298
+ `image ${pathText || "file"} (${mediaType || "unknown"}, ${bytes != null ? `${bytes} bytes` : "size?"})`,
299
+ PREVIEW_MAX_CHARS,
300
+ );
301
+ const modelPayload = {
302
+ ok: source.ok !== false,
303
+ kind: "image",
304
+ artifactId,
305
+ path: pathText,
306
+ mediaType,
307
+ bytes,
308
+ preview,
309
+ };
310
+ if (source.error) modelPayload.error = String(source.error);
311
+ // Keep base64 in the in-memory model payload for the current turn only.
312
+ // Artifacts / transcript strip it via stripVisionBase64 before persistence.
313
+ if (modelPayload.ok && source.base64) {
314
+ modelPayload.base64 = String(source.base64);
315
+ }
316
+ return {
317
+ preview,
318
+ summary: `read_image ${pathText || "file"} (${mediaType || "image"})`,
319
+ modelPayload,
320
+ };
321
+ }
322
+
290
323
  function reduceToolResult(tool = "", raw = {}, artifactId = "", args = {}) {
291
324
  const name = String(tool || "").trim().toLowerCase();
292
325
  if (name === "read") return reduceReadResult(raw, artifactId);
326
+ if (name === "read_image") return reduceReadImageResult(raw, artifactId);
293
327
  if (name === "bash") return reduceBashResult(raw, artifactId, args);
294
328
  if (name === "write") return reduceWriteResult(raw, artifactId);
295
329
  if (name === "edit") return reduceEditResult(raw, artifactId);
@@ -318,6 +352,7 @@ module.exports = {
318
352
  parseSearchMatches,
319
353
  reduceToolResult,
320
354
  reduceReadResult,
355
+ reduceReadImageResult,
321
356
  reduceBashResult,
322
357
  reduceTestResult,
323
358
  reduceGitDiffResult,
@@ -5,6 +5,7 @@ const {
5
5
  createTranscriptEventId,
6
6
  appendTranscriptEvent,
7
7
  } = require("./transcript");
8
+ const { stripVisionBase64, degradeVisionContent } = require("../providers/visionBlocks");
8
9
 
9
10
  function messageRole(message = {}) {
10
11
  return String(message && message.role || "").trim().toLowerCase();
@@ -35,6 +36,24 @@ function parseToolArtifactContent(content = "") {
35
36
  }
36
37
  }
37
38
 
39
+ function contentForStorage(content) {
40
+ if (typeof content === "string") return content;
41
+ if (Array.isArray(content)) {
42
+ const hasVision = content.some((block) => {
43
+ if (!block || typeof block !== "object") return false;
44
+ const type = String(block.type || "").trim().toLowerCase();
45
+ return type === "image" || type === "image_url"
46
+ || (type === "tool_result" && Array.isArray(block.content));
47
+ });
48
+ if (hasVision) return degradeVisionContent(stripVisionBase64(content));
49
+ return stripVisionBase64(content);
50
+ }
51
+ if (content && typeof content === "object") {
52
+ return stripVisionBase64(content);
53
+ }
54
+ return content;
55
+ }
56
+
38
57
  function messageToTranscriptEventForStorage(message = {}, extra = {}) {
39
58
  if (!message || typeof message !== "object") return null;
40
59
  const role = messageRole(message);
@@ -60,9 +79,10 @@ function messageToTranscriptEventForStorage(message = {}, extra = {}) {
60
79
  toolCallId: message.tool_call_id ? String(message.tool_call_id) : undefined,
61
80
  });
62
81
  }
63
- const preview = typeof message.content === "string"
64
- ? message.content.slice(0, 600)
65
- : JSON.stringify(message.content).slice(0, 600);
82
+ const stored = contentForStorage(message.content);
83
+ const preview = typeof stored === "string"
84
+ ? stored.slice(0, 600)
85
+ : JSON.stringify(stored).slice(0, 600);
66
86
  return normalizeTranscriptEvent({
67
87
  ...base,
68
88
  role: "tool",
@@ -75,14 +95,14 @@ function messageToTranscriptEventForStorage(message = {}, extra = {}) {
75
95
  if (role === "assistant" && Array.isArray(message.tool_calls) && message.tool_calls.length > 0) {
76
96
  return normalizeTranscriptEvent({
77
97
  ...base,
78
- content: message.content,
98
+ content: contentForStorage(message.content),
79
99
  toolCalls: message.tool_calls,
80
100
  });
81
101
  }
82
102
 
83
103
  return normalizeTranscriptEvent({
84
104
  ...base,
85
- content: message.content,
105
+ content: contentForStorage(message.content),
86
106
  });
87
107
  }
88
108
 
@@ -1,6 +1,7 @@
1
1
  "use strict";
2
2
 
3
3
  const { runReadTool } = require("./tools/read");
4
+ const { runReadImageTool } = require("./tools/readImage");
4
5
  const { runWriteTool } = require("./tools/write");
5
6
  const { runEditTool } = require("./tools/edit");
6
7
  const { runBashTool } = require("./tools/bash");
@@ -11,6 +12,7 @@ const { runAskUserTool } = require("./tools/askUser");
11
12
 
12
13
  const TOOL_NAMES = [
13
14
  "read",
15
+ "read_image",
14
16
  "write",
15
17
  "edit",
16
18
  "bash",
@@ -23,6 +25,7 @@ const TOOL_NAMES = [
23
25
  function normalizeToolName(value = "") {
24
26
  const text = String(value || "").trim().toLowerCase();
25
27
  if (text === "read") return "read";
28
+ if (text === "read_image" || text === "read-image" || text === "readimage") return "read_image";
26
29
  if (text === "write") return "write";
27
30
  if (text === "edit") return "edit";
28
31
  if (text === "bash") return "bash";
@@ -44,6 +47,7 @@ function runToolCall(input = {}, options = {}) {
44
47
  };
45
48
  }
46
49
  if (tool === "read") return runReadTool(args, options);
50
+ if (tool === "read_image") return runReadImageTool(args, options);
47
51
  if (tool === "write") return runWriteTool(args, options);
48
52
  if (tool === "edit") return runEditTool(args, options);
49
53
  if (tool === "artifact_read") return runArtifactReadTool(args, options);
@@ -0,0 +1,367 @@
1
+ "use strict";
2
+
3
+ const fs = require("fs");
4
+ const os = require("os");
5
+ const path = require("path");
6
+ const { execFileSync } = require("child_process");
7
+ const { mediaTypeFromPath, sniffMediaType, MAX_IMAGE_BYTES } = require("./tools/readImage");
8
+
9
+ const IMAGE_EXT_RE = /\.(png|jpe?g|gif|webp)$/i;
10
+ const FILE_URL_RE = /^file:\/\//i;
11
+
12
+ function uploadsDir(workspaceRoot = "", sessionId = "") {
13
+ const root = path.resolve(String(workspaceRoot || process.cwd()));
14
+ const sid = String(sessionId || "session").trim().replace(/[^a-zA-Z0-9._-]+/g, "_") || "session";
15
+ return path.join(root, ".ufoo", "agent", "ucode", "uploads", sid);
16
+ }
17
+
18
+ function safeBaseName(filePath = "") {
19
+ const base = path.basename(String(filePath || "image.png"));
20
+ const cleaned = base.replace(/[^\w.\-()+ ]+/g, "_").replace(/\s+/g, " ").trim();
21
+ if (!cleaned) return "image.png";
22
+ if (!IMAGE_EXT_RE.test(cleaned)) return `${cleaned}.png`;
23
+ return cleaned.slice(0, 120);
24
+ }
25
+
26
+ function decodeFileUrl(value = "") {
27
+ const text = String(value || "").trim();
28
+ if (!FILE_URL_RE.test(text)) return text;
29
+ try {
30
+ const parsed = new URL(text);
31
+ if (parsed.protocol !== "file:") return text;
32
+ return decodeURIComponent(parsed.pathname || "");
33
+ } catch {
34
+ return text.replace(FILE_URL_RE, "");
35
+ }
36
+ }
37
+
38
+ function looksLikeImagePath(candidate = "") {
39
+ const text = decodeFileUrl(String(candidate || "").trim().replace(/^['"]|['"]$/g, ""));
40
+ if (!text || !IMAGE_EXT_RE.test(text)) return false;
41
+ if (text.startsWith("/") || /^[A-Za-z]:[\\/]/.test(text) || text.startsWith("~")) return true;
42
+ // Relative paths ending in image ext (drag from cwd listings)
43
+ if (!/\s/.test(text) && IMAGE_EXT_RE.test(text)) return true;
44
+ return false;
45
+ }
46
+
47
+ function expandHome(filePath = "") {
48
+ const text = String(filePath || "");
49
+ if (text.startsWith("~/")) return path.join(os.homedir(), text.slice(2));
50
+ return text;
51
+ }
52
+
53
+ /**
54
+ * Extract image file paths from terminal paste / drag-drop text.
55
+ * Supports file://, quoted paths with spaces, and bare absolute paths.
56
+ */
57
+ function extractImagePathsFromPaste(text = "") {
58
+ const raw = String(text || "");
59
+ if (!raw.trim()) return [];
60
+ const found = [];
61
+ const seen = new Set();
62
+
63
+ function pushPath(candidate) {
64
+ let next = decodeFileUrl(String(candidate || "").trim());
65
+ next = next.replace(/^['"]|['"]$/g, "");
66
+ if (!looksLikeImagePath(next)) return;
67
+ next = expandHome(next);
68
+ const key = path.resolve(next);
69
+ if (seen.has(key)) return;
70
+ seen.add(key);
71
+ found.push(next);
72
+ }
73
+
74
+ // Quoted paths (possibly with spaces)
75
+ const quoted = /["']([^"']+\.(?:png|jpe?g|gif|webp))["']/gi;
76
+ let match;
77
+ while ((match = quoted.exec(raw))) {
78
+ pushPath(match[1]);
79
+ }
80
+
81
+ // file:// URLs
82
+ const fileUrls = /file:\/\/[^\s"'<>]+/gi;
83
+ while ((match = fileUrls.exec(raw))) {
84
+ pushPath(match[0]);
85
+ }
86
+
87
+ // Bare tokens / lines
88
+ for (const line of raw.split(/\r?\n/)) {
89
+ const trimmed = line.trim();
90
+ if (!trimmed) continue;
91
+ if (looksLikeImagePath(trimmed)) {
92
+ pushPath(trimmed);
93
+ continue;
94
+ }
95
+ for (const token of trimmed.split(/\s+/)) {
96
+ if (looksLikeImagePath(token)) pushPath(token);
97
+ }
98
+ }
99
+
100
+ return found;
101
+ }
102
+
103
+ function stripExtractedPathsFromText(text = "", paths = []) {
104
+ let out = String(text || "");
105
+ for (const p of paths) {
106
+ const variants = [
107
+ `"${p}"`,
108
+ `'${p}'`,
109
+ p,
110
+ p.startsWith("/") ? `file://${p}` : "",
111
+ p.startsWith("/") ? `file://${encodeURI(p)}` : "",
112
+ ].filter(Boolean);
113
+ for (const v of variants) {
114
+ out = out.split(v).join(" ");
115
+ }
116
+ }
117
+ return out
118
+ .replace(/[ \t]+\n/g, "\n")
119
+ .replace(/\n{3,}/g, "\n\n")
120
+ .replace(/[ \t]{2,}/g, " ")
121
+ .replace(/\s+"/g, " ")
122
+ .replace(/"\s+/g, " ")
123
+ .replace(/\s+'/g, " ")
124
+ .replace(/'\s+/g, " ")
125
+ .trim();
126
+ }
127
+
128
+ function formatImageLogLabel({ relPath = "", fileName = "", path: pathText = "" } = {}) {
129
+ const name = String(fileName || "").trim()
130
+ || path.basename(String(relPath || pathText || "").trim())
131
+ || "image";
132
+ return `[image: ${name}]`;
133
+ }
134
+
135
+ function formatUserLogWithAttachments(userText = "", attachments = []) {
136
+ const labels = (Array.isArray(attachments) ? attachments : [])
137
+ .map((item) => formatImageLogLabel(item))
138
+ .filter(Boolean);
139
+ const body = String(userText || "").trim();
140
+ if (labels.length === 0) return body;
141
+ if (!body) return labels.join(" ");
142
+ return `${labels.join(" ")} ${body}`;
143
+ }
144
+
145
+ function buildAttachedImagesPromptPrefix(attachments = []) {
146
+ const list = Array.isArray(attachments) ? attachments : [];
147
+ if (list.length === 0) return "";
148
+ const lines = [
149
+ "[Attached images — call read_image on each path]",
150
+ ...list.map((item) => `- ${item.relPath || item.path || ""}`).filter((line) => line !== "- "),
151
+ "",
152
+ ];
153
+ return lines.join("\n");
154
+ }
155
+
156
+ function ingestImageFile({
157
+ sourcePath = "",
158
+ workspaceRoot = process.cwd(),
159
+ sessionId = "",
160
+ buffer = null,
161
+ preferredName = "",
162
+ } = {}) {
163
+ const root = path.resolve(String(workspaceRoot || process.cwd()));
164
+ let data = buffer;
165
+ let fromPath = String(sourcePath || "").trim();
166
+
167
+ if (!data) {
168
+ if (!fromPath) {
169
+ return { ok: false, error: "sourcePath or buffer required" };
170
+ }
171
+ fromPath = expandHome(decodeFileUrl(fromPath));
172
+ try {
173
+ const stat = fs.statSync(fromPath);
174
+ if (!stat.isFile()) return { ok: false, error: `not a file: ${fromPath}` };
175
+ if (stat.size > MAX_IMAGE_BYTES) {
176
+ return {
177
+ ok: false,
178
+ error: `image too large (${stat.size} bytes); max ${MAX_IMAGE_BYTES}`,
179
+ };
180
+ }
181
+ data = fs.readFileSync(fromPath);
182
+ } catch (err) {
183
+ return { ok: false, error: err && err.message ? err.message : "read failed" };
184
+ }
185
+ }
186
+
187
+ if (!Buffer.isBuffer(data)) {
188
+ return { ok: false, error: "image buffer required" };
189
+ }
190
+ if (data.length > MAX_IMAGE_BYTES) {
191
+ return {
192
+ ok: false,
193
+ error: `image too large (${data.length} bytes); max ${MAX_IMAGE_BYTES}`,
194
+ };
195
+ }
196
+
197
+ const sniffed = sniffMediaType(data);
198
+ const fromName = mediaTypeFromPath(preferredName || fromPath);
199
+ const mediaType = sniffed || fromName;
200
+ if (!mediaType) {
201
+ return { ok: false, error: "unsupported image type (use png, jpeg, gif, or webp)" };
202
+ }
203
+
204
+ const ext = mediaType === "image/jpeg"
205
+ ? ".jpg"
206
+ : mediaType === "image/gif"
207
+ ? ".gif"
208
+ : mediaType === "image/webp"
209
+ ? ".webp"
210
+ : ".png";
211
+
212
+ let base = safeBaseName(preferredName || fromPath || `clipboard${ext}`);
213
+ if (!IMAGE_EXT_RE.test(base)) base = `${base}${ext}`;
214
+ // Normalize extension to sniffed type
215
+ base = `${path.basename(base, path.extname(base))}${ext}`;
216
+
217
+ const dir = uploadsDir(root, sessionId);
218
+ fs.mkdirSync(dir, { recursive: true });
219
+ const stamp = Date.now().toString(36);
220
+ const destName = `${stamp}-${base}`;
221
+ const absPath = path.join(dir, destName);
222
+ fs.writeFileSync(absPath, data);
223
+
224
+ const relPath = path.relative(root, absPath).split(path.sep).join("/");
225
+ return {
226
+ ok: true,
227
+ relPath,
228
+ absPath,
229
+ fileName: base,
230
+ mediaType,
231
+ bytes: data.length,
232
+ };
233
+ }
234
+
235
+ function tryIngestClipboardImage({
236
+ workspaceRoot = process.cwd(),
237
+ sessionId = "",
238
+ platform = process.platform,
239
+ execFile = execFileSync,
240
+ } = {}) {
241
+ if (platform !== "darwin") {
242
+ return { ok: false, error: "clipboard image ingest is only supported on macOS" };
243
+ }
244
+
245
+ const tmpPath = path.join(
246
+ os.tmpdir(),
247
+ `ufoo-clipboard-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 8)}.png`,
248
+ );
249
+ // AppleScript: write clipboard PNGf to a temp file.
250
+ const script = [
251
+ `set outPath to POSIX file ${JSON.stringify(tmpPath)}`,
252
+ "try",
253
+ " set pngData to the clipboard as «class PNGf»",
254
+ " set fileRef to open for access outPath with write permission",
255
+ " set eof of fileRef to 0",
256
+ " write pngData to fileRef",
257
+ " close access fileRef",
258
+ ' return "ok"',
259
+ "on error errMsg number errNum",
260
+ " try",
261
+ " close access outPath",
262
+ " end try",
263
+ ' return "err:" & errMsg',
264
+ "end try",
265
+ ].join("\n");
266
+
267
+ let resultText = "";
268
+ try {
269
+ resultText = String(execFile("osascript", ["-e", script], {
270
+ encoding: "utf8",
271
+ timeout: 5000,
272
+ maxBuffer: 1024 * 1024,
273
+ }) || "").trim();
274
+ } catch (err) {
275
+ try { fs.unlinkSync(tmpPath); } catch { /* ignore */ }
276
+ return {
277
+ ok: false,
278
+ error: err && err.message ? err.message : "clipboard read failed",
279
+ };
280
+ }
281
+
282
+ if (!resultText.startsWith("ok")) {
283
+ try { fs.unlinkSync(tmpPath); } catch { /* ignore */ }
284
+ return {
285
+ ok: false,
286
+ error: resultText.replace(/^err:/, "").trim() || "no PNG image on clipboard",
287
+ };
288
+ }
289
+
290
+ try {
291
+ const ingested = ingestImageFile({
292
+ sourcePath: tmpPath,
293
+ workspaceRoot,
294
+ sessionId,
295
+ preferredName: `clipboard-${Date.now().toString(36)}.png`,
296
+ });
297
+ return ingested;
298
+ } finally {
299
+ try { fs.unlinkSync(tmpPath); } catch { /* ignore */ }
300
+ }
301
+ }
302
+
303
+ /**
304
+ * Handle a paste chunk: ingest image paths and/or macOS clipboard bitmap.
305
+ * Returns text to insert into the editor (paths removed) plus attachments.
306
+ */
307
+ function handleImagePaste(text = "", {
308
+ workspaceRoot = process.cwd(),
309
+ sessionId = "",
310
+ tryClipboard = true,
311
+ platform = process.platform,
312
+ execFile = execFileSync,
313
+ } = {}) {
314
+ const raw = String(text || "");
315
+ const paths = extractImagePathsFromPaste(raw);
316
+ const attachments = [];
317
+ const errors = [];
318
+
319
+ for (const sourcePath of paths) {
320
+ const ingested = ingestImageFile({ sourcePath, workspaceRoot, sessionId });
321
+ if (ingested.ok) attachments.push(ingested);
322
+ else errors.push(ingested.error || "ingest failed");
323
+ }
324
+
325
+ let remaining = stripExtractedPathsFromText(raw, paths);
326
+
327
+ // If paste had no usable text/paths, try clipboard PNG (Cmd+V of a screenshot).
328
+ const trimmedRemaining = remaining.trim();
329
+ const looksEmptyOrBinary = !trimmedRemaining
330
+ || /[\x00-\x08\x0e-\x1f]/.test(raw)
331
+ || (Buffer.byteLength(raw, "utf8") > 200 && paths.length === 0 && !/\s/.test(raw.slice(0, 40)));
332
+
333
+ if (tryClipboard && attachments.length === 0 && looksEmptyOrBinary) {
334
+ const clip = tryIngestClipboardImage({
335
+ workspaceRoot,
336
+ sessionId,
337
+ platform,
338
+ execFile,
339
+ });
340
+ if (clip.ok) {
341
+ attachments.push(clip);
342
+ remaining = "";
343
+ } else if (paths.length === 0 && !trimmedRemaining) {
344
+ errors.push(clip.error || "clipboard ingest failed");
345
+ }
346
+ }
347
+
348
+ return {
349
+ text: remaining,
350
+ attachments,
351
+ errors,
352
+ };
353
+ }
354
+
355
+ module.exports = {
356
+ IMAGE_EXT_RE,
357
+ uploadsDir,
358
+ safeBaseName,
359
+ extractImagePathsFromPaste,
360
+ stripExtractedPathsFromText,
361
+ formatImageLogLabel,
362
+ formatUserLogWithAttachments,
363
+ buildAttachedImagesPromptPrefix,
364
+ ingestImageFile,
365
+ tryIngestClipboardImage,
366
+ handleImagePaste,
367
+ };