@@ -1,6 +1,11 @@
11 import { join, relative } from "node:path";
22 import * as c from "yoctocolors";
33 import { addPromptHistory, getPromptHistory } from "../session/db.ts";
4+import {
5+ type ImageAttachment,
6+ isImageFilename,
7+ loadImageFile,
8+} from "./image-types.ts";
49
510 // ─── ANSI escape sequences ────────────────────────────────────────────────────
611
@@ -32,8 +37,10 @@ const TAB = "\x09";
3237
3338 // ─── Types ────────────────────────────────────────────────────────────────────
3439
40+export type { ImageAttachment };
41+
3542 export type InputResult =
36- | { type: "submit"; text: string }
43+ | { type: "submit"; text: string; images: ImageAttachment[] }
3744 | { type: "interrupt" }
3845 | { type: "eof" }
3946 | { type: "command"; command: string; args: string }
@@ -56,6 +63,54 @@ async function getFileCompletions(
5663 return results;
5764 }
5865
66+// ─── Image detection ─────────────────────────────────────────────────────────
67+
68+/**
69+ * Parse a pasted chunk and extract image attachments if present.
70+ *
71+ * Handles two cases:
72+ * 1. The paste is a `data:image/...;base64,...` data URL.
73+ * 2. The paste is a file path pointing to an image on disk.
74+ *
75+ * Returns `{ attachment, label }` when an image is found so the caller can
76+ * replace the paste buffer with a placeholder label, or `null` when the paste
77+ * is plain text.
78+ */
79+async function tryExtractImageFromPaste(
80+ pasted: string,
81+ cwd: string,
82+): Promise<{ attachment: ImageAttachment; label: string } | null> {
83+ const trimmed = pasted.trim();
84+
85+ // Case 1: base64 data URL — only accept if explicitly base64-encoded
86+ // (non-base64 data URLs like data:image/svg+xml,<svg>... would be sent as
87+ // garbled base64 to the model if we don't guard here)
88+ if (trimmed.startsWith("data:image/")) {
89+ const commaIdx = trimmed.indexOf(",");
90+ if (commaIdx !== -1 && trimmed.slice(0, commaIdx).includes(";base64")) {
91+ const header = trimmed.slice(0, commaIdx); // "data:image/png;base64"
92+ const b64 = trimmed.slice(commaIdx + 1);
93+ const mediaType = header.split(";")[0]?.slice(5) ?? "image/png"; // strip "data:"
94+ return {
95+ attachment: { data: b64, mediaType },
96+ label: `[image: ${mediaType}]`,
97+ };
98+ }
99+ }
100+
101+ // Case 2: file path to an image
102+ if (!trimmed.includes(" ") && isImageFilename(trimmed)) {
103+ const filePath = trimmed.startsWith("/") ? trimmed : join(cwd, trimmed);
104+ const attachment = await loadImageFile(filePath);
105+ if (attachment) {
106+ const name = filePath.split("/").pop() ?? trimmed;
107+ return { attachment, label: `[image: ${name}]` };
108+ }
109+ }
110+
111+ return null;
112+}
113+
59114 // ─── Singleton stdin reader (shared across readline() calls) ─────────────────
60115 // We create the ReadableStream once so that repeated readline() calls don't
61116 // each wrap process.stdin in their own controller, which causes
@@ -141,6 +196,7 @@ export async function readline(opts: {
141196 let searchMode = false;
142197 let searchQuery = "";
143198 let pasteBuffer: string | null = null; // full text of a pending paste
199+ const imageAttachments: ImageAttachment[] = []; // images collected during this prompt
144200
145201 process.stdin.setRawMode(true);
146202 process.stdin.resume();
@@ -149,10 +205,13 @@ export async function readline(opts: {
149205
150206 function renderPrompt(): void {
151207 const cols = process.stdout.columns ?? 80;
152- // Replace the sentinel with a styled placeholder for display
153- const visualBuf = pasteBuffer
154- ? buf.replace(PASTE_SENTINEL, c.dim(pasteLabel(pasteBuffer)))
155- : buf;
208+ // Replace paste sentinel and image labels with styled placeholders for display
209+ const visualBuf = (
210+ pasteBuffer
211+ ? buf.replace(PASTE_SENTINEL, c.dim(pasteLabel(pasteBuffer)))
212+ : buf
213+ ).replace(/\[image: [^\]]+\]/g, (m) => c.dim(c.cyan(m)));
214+
156215 // For cursor positioning we need the visual length without the sentinel substitution
157216 const visualCursor = pasteBuffer
158217 ? (() => {
@@ -397,28 +456,38 @@ export async function readline(opts: {
397456 cursor = 0;
398457 histIdx = history.length + (text ? 1 : 0);
399458
400- if (!text) {
459+ if (!text && !imageAttachments.length) {
401460 renderPrompt();
402461 continue;
403462 }
404463
405- addPromptHistory(text);
464+ // Strip image placeholder labels — actual image data is in imageAttachments.
465+ // Do this before history and before returning so every path sees clean text.
466+ const cleanText = imageAttachments.length
467+ ? text.replace(/\[image: [^\]]+\]/g, "").trim()
468+ : text;
469+ addPromptHistory(cleanText);
406470
407471 // Slash commands
408- if (text.startsWith("/")) {
409- const spaceIdx = text.indexOf(" ");
472+ if (cleanText.startsWith("/")) {
473+ const spaceIdx = cleanText.indexOf(" ");
410474 const command =
411- spaceIdx === -1 ? text.slice(1) : text.slice(1, spaceIdx);
412- const args = spaceIdx === -1 ? "" : text.slice(spaceIdx + 1).trim();
475+ spaceIdx === -1 ? cleanText.slice(1) : cleanText.slice(1, spaceIdx);
476+ const args =
477+ spaceIdx === -1 ? "" : cleanText.slice(spaceIdx + 1).trim();
413478 return { type: "command", command, args };
414479 }
415480
416481 // Shell passthrough
417- if (text.startsWith("!")) {
418- return { type: "shell", command: text.slice(1).trim() };
482+ if (cleanText.startsWith("!")) {
483+ return { type: "shell", command: cleanText.slice(1).trim() };
419484 }
420485
421- return { type: "submit", text };
486+ return {
487+ type: "submit",
488+ text: cleanText,
489+ images: imageAttachments,
490+ };
422491 }
423492
424493 // ── Paste detection ───────────────────────────────────────────────────
@@ -427,6 +496,18 @@ export async function readline(opts: {
427496 if (raw.length > 1) {
428497 // Strip trailing newline that some terminals append on paste
429498 const pasted = raw.replace(/\r?\n$/, "");
499+
500+ // Check if this is an image (data URL or image file path)
501+ const imageResult = await tryExtractImageFromPaste(pasted, cwd);
502+ if (imageResult) {
503+ imageAttachments.push(imageResult.attachment);
504+ // Insert the plain label into buf (no sentinel — nothing to expand at submit)
505+ buf = buf.slice(0, cursor) + imageResult.label + buf.slice(cursor);
506+ cursor += imageResult.label.length;
507+ renderPrompt();
508+ continue;
509+ }
510+
430511 pasteBuffer = pasted;
431512 buf = buf.slice(0, cursor) + PASTE_SENTINEL + buf.slice(cursor);
432513 cursor += PASTE_SENTINEL_LEN;