Add image paste drag drop dialog
This commit is contained in:
1 parent
44f3b3cb7c
commit
3cf58f80d1
11 files changed
+725
-42
No files matched your search
@@ -1,11 +1,42 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { describe, expect, it, vi } from "vitest";
|
||||
|
||||
import {
|
||||
CodexAppServerManager,
|
||||
classifyCodexStderrLine,
|
||||
isRecoverableThreadResumeError,
|
||||
normalizeCodexModelSlug,
|
||||
} from "./codexAppServerManager";
|
||||
|
||||
function createSendTurnHarness() {
|
||||
const manager = new CodexAppServerManager();
|
||||
const context = {
|
||||
session: {
|
||||
sessionId: "sess_1",
|
||||
provider: "codex",
|
||||
status: "ready",
|
||||
threadId: "thread_1",
|
||||
createdAt: "2026-02-10T00:00:00.000Z",
|
||||
updatedAt: "2026-02-10T00:00:00.000Z",
|
||||
},
|
||||
};
|
||||
|
||||
const requireSession = vi
|
||||
.spyOn(manager as unknown as { requireSession: (sessionId: string) => unknown }, "requireSession")
|
||||
.mockReturnValue(context);
|
||||
const sendRequest = vi
|
||||
.spyOn(manager as unknown as { sendRequest: (...args: unknown[]) => Promise<unknown> }, "sendRequest")
|
||||
.mockResolvedValue({
|
||||
turn: {
|
||||
id: "turn_1",
|
||||
},
|
||||
});
|
||||
const updateSession = vi
|
||||
.spyOn(manager as unknown as { updateSession: (...args: unknown[]) => void }, "updateSession")
|
||||
.mockImplementation(() => {});
|
||||
|
||||
return { manager, context, requireSession, sendRequest, updateSession };
|
||||
}
|
||||
|
||||
describe("classifyCodexStderrLine", () => {
|
||||
it("ignores empty lines", () => {
|
||||
expect(classifyCodexStderrLine(" ")).toBeNull();
|
||||
@@ -75,3 +106,86 @@ describe("isRecoverableThreadResumeError", () => {
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sendTurn", () => {
|
||||
it("sends text and image user input items to turn/start", async () => {
|
||||
const { manager, context, requireSession, sendRequest, updateSession } = createSendTurnHarness();
|
||||
|
||||
const result = await manager.sendTurn({
|
||||
sessionId: "sess_1",
|
||||
input: "Inspect this image",
|
||||
images: [
|
||||
{
|
||||
name: "error.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 1_024,
|
||||
dataUrl: "data:image/png;base64,AAAA",
|
||||
},
|
||||
],
|
||||
model: "gpt-5.3",
|
||||
effort: "high",
|
||||
});
|
||||
|
||||
expect(result).toEqual({
|
||||
threadId: "thread_1",
|
||||
turnId: "turn_1",
|
||||
});
|
||||
expect(requireSession).toHaveBeenCalledWith("sess_1");
|
||||
expect(sendRequest).toHaveBeenCalledWith(context, "turn/start", {
|
||||
threadId: "thread_1",
|
||||
input: [
|
||||
{
|
||||
type: "text",
|
||||
text: "Inspect this image",
|
||||
text_elements: [],
|
||||
},
|
||||
{
|
||||
type: "image",
|
||||
url: "data:image/png;base64,AAAA",
|
||||
},
|
||||
],
|
||||
model: "gpt-5.3-codex",
|
||||
effort: "high",
|
||||
});
|
||||
expect(updateSession).toHaveBeenCalledWith(context, {
|
||||
status: "running",
|
||||
activeTurnId: "turn_1",
|
||||
});
|
||||
});
|
||||
|
||||
it("supports image-only turns", async () => {
|
||||
const { manager, context, sendRequest } = createSendTurnHarness();
|
||||
|
||||
await manager.sendTurn({
|
||||
sessionId: "sess_1",
|
||||
images: [
|
||||
{
|
||||
name: "diagram.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 256,
|
||||
dataUrl: "data:image/png;base64,BBBB",
|
||||
},
|
||||
],
|
||||
});
|
||||
|
||||
expect(sendRequest).toHaveBeenCalledWith(context, "turn/start", {
|
||||
threadId: "thread_1",
|
||||
input: [
|
||||
{
|
||||
type: "image",
|
||||
url: "data:image/png;base64,BBBB",
|
||||
},
|
||||
],
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects empty turn input", async () => {
|
||||
const { manager } = createSendTurnHarness();
|
||||
|
||||
await expect(
|
||||
manager.sendTurn({
|
||||
sessionId: "sess_1",
|
||||
}),
|
||||
).rejects.toThrow("Turn input must include text or image attachments.");
|
||||
});
|
||||
});
|
||||
@@ -257,20 +257,38 @@ export class CodexAppServerManager extends EventEmitter<CodexAppServerManagerEve
|
||||
throw new Error("Session is missing a thread id.");
|
||||
}
|
||||
|
||||
const turnInput: Array<
|
||||
| { type: "text"; text: string; text_elements: [] }
|
||||
| { type: "image"; url: string }
|
||||
> = [];
|
||||
if (input.input) {
|
||||
turnInput.push({
|
||||
type: "text",
|
||||
text: input.input,
|
||||
text_elements: [],
|
||||
});
|
||||
}
|
||||
for (const image of input.images ?? []) {
|
||||
turnInput.push({
|
||||
type: "image",
|
||||
url: image.dataUrl,
|
||||
});
|
||||
}
|
||||
if (turnInput.length === 0) {
|
||||
throw new Error("Turn input must include text or image attachments.");
|
||||
}
|
||||
|
||||
const turnStartParams: {
|
||||
threadId: string;
|
||||
input: Array<{ type: "text"; text: string; text_elements: [] }>;
|
||||
input: Array<
|
||||
| { type: "text"; text: string; text_elements: [] }
|
||||
| { type: "image"; url: string }
|
||||
>;
|
||||
model?: string;
|
||||
effort?: string;
|
||||
} = {
|
||||
threadId: context.session.threadId,
|
||||
input: [
|
||||
{
|
||||
type: "text",
|
||||
text: input.input,
|
||||
text_elements: [],
|
||||
},
|
||||
],
|
||||
input: turnInput,
|
||||
};
|
||||
const normalizedModel = normalizeCodexModelSlug(input.model);
|
||||
if (normalizedModel) {
|
||||
|
||||
@@ -1,9 +1,14 @@
|
||||
import {
|
||||
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
|
||||
PROVIDER_SEND_TURN_MAX_IMAGE_BYTES,
|
||||
PROVIDER_SEND_TURN_MAX_IMAGES,
|
||||
type ProviderSendTurnImageInput,
|
||||
type ProviderApprovalDecision,
|
||||
type ProviderEvent,
|
||||
} from "@t3tools/contracts";
|
||||
import {
|
||||
type ClipboardEvent,
|
||||
type DragEvent,
|
||||
type FormEvent,
|
||||
Fragment,
|
||||
type KeyboardEvent,
|
||||
@@ -33,6 +38,7 @@ import {
|
||||
formatTimestamp,
|
||||
} from "../session-logic";
|
||||
import { useStore } from "../store";
|
||||
import type { ChatImageAttachment } from "../types";
|
||||
import BranchToolbar from "./BranchToolbar";
|
||||
import GitActionsControl from "./GitActionsControl";
|
||||
import { isTerminalToggleShortcut } from "../terminal-shortcuts";
|
||||
@@ -57,6 +63,9 @@ function editorLabel(editor: (typeof EDITORS)[number]): string {
|
||||
|
||||
const LAST_EDITOR_KEY = "t3code:last-editor";
|
||||
const MAX_VISIBLE_WORK_LOG_ENTRIES = 6;
|
||||
const IMAGE_SIZE_LIMIT_LABEL = `${Math.round(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES / (1024 * 1024))}MB`;
|
||||
const IMAGE_ONLY_BOOTSTRAP_PROMPT =
|
||||
"[User attached one or more images without additional text. Respond using the conversation context and the attached image(s).]";
|
||||
|
||||
function workToneClass(tone: "thinking" | "tool" | "info" | "error"): string {
|
||||
if (tone === "error") return "text-rose-300/50 dark:text-rose-300/50";
|
||||
@@ -80,6 +89,16 @@ interface EnsuredSessionInfo {
|
||||
continuityState: SessionContinuityState;
|
||||
}
|
||||
|
||||
interface ComposerImageAttachment extends Omit<ChatImageAttachment, "previewUrl"> {
|
||||
previewUrl: string;
|
||||
file: File;
|
||||
}
|
||||
|
||||
interface ExpandedImagePreview {
|
||||
src: string;
|
||||
name: string;
|
||||
}
|
||||
|
||||
function asRecord(value: unknown): Record<string, unknown> | undefined {
|
||||
if (!value || typeof value !== "object") return undefined;
|
||||
return value as Record<string, unknown>;
|
||||
@@ -131,10 +150,30 @@ function derivePendingApprovals(events: ProviderEvent[]): PendingApprovalCard[]
|
||||
return Array.from(pending.values());
|
||||
}
|
||||
|
||||
function readFileAsDataUrl(file: File): Promise<string> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const reader = new FileReader();
|
||||
reader.addEventListener("load", () => {
|
||||
if (typeof reader.result === "string") {
|
||||
resolve(reader.result);
|
||||
return;
|
||||
}
|
||||
reject(new Error("Could not read image data."));
|
||||
});
|
||||
reader.addEventListener("error", () => {
|
||||
reject(reader.error ?? new Error("Failed to read image."));
|
||||
});
|
||||
reader.readAsDataURL(file);
|
||||
});
|
||||
}
|
||||
|
||||
export default function ChatView() {
|
||||
const { state, dispatch } = useStore();
|
||||
const api = useNativeApi();
|
||||
const [prompt, setPrompt] = useState("");
|
||||
const [composerImages, setComposerImages] = useState<ComposerImageAttachment[]>([]);
|
||||
const [isDragOverComposer, setIsDragOverComposer] = useState(false);
|
||||
const [expandedImage, setExpandedImage] = useState<ExpandedImagePreview | null>(null);
|
||||
const [isSending, setIsSending] = useState(false);
|
||||
const [isConnecting, setIsConnecting] = useState(false);
|
||||
const [isModelMenuOpen, setIsModelMenuOpen] = useState(false);
|
||||
@@ -152,6 +191,8 @@ export default function ChatView() {
|
||||
const [terminalFocusRequestId, setTerminalFocusRequestId] = useState(0);
|
||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||
const textareaRef = useRef<HTMLTextAreaElement>(null);
|
||||
const composerImagesRef = useRef<ComposerImageAttachment[]>([]);
|
||||
const dragDepthRef = useRef(0);
|
||||
const modelMenuRef = useRef<HTMLDivElement>(null);
|
||||
const editorMenuRef = useRef<HTMLDivElement>(null);
|
||||
const terminalOpenByThreadRef = useRef<Record<string, boolean>>({});
|
||||
@@ -264,7 +305,15 @@ export default function ChatView() {
|
||||
(activeThread.messages.length > 0 ||
|
||||
(activeThread.session !== null && activeThread.session.status !== "closed")),
|
||||
);
|
||||
const terminalShortcutHint = navigator.platform.includes("Mac") ? "\u2318J" : "Ctrl+J";
|
||||
const terminalShortcutHint = navigator.platform.includes("Mac")
|
||||
? "\u2318J"
|
||||
: "Ctrl+J";
|
||||
const revokePreviewUrls = useCallback((images: Array<{ previewUrl?: string }>) => {
|
||||
for (const image of images) {
|
||||
if (!image.previewUrl) continue;
|
||||
URL.revokeObjectURL(image.previewUrl);
|
||||
}
|
||||
}, []);
|
||||
const focusComposer = useCallback(() => {
|
||||
const textarea = textareaRef.current;
|
||||
if (!textarea) return;
|
||||
@@ -319,6 +368,44 @@ export default function ChatView() {
|
||||
setExpandedWorkGroups({});
|
||||
}, [activeThread?.id]);
|
||||
|
||||
useEffect(() => {
|
||||
composerImagesRef.current = composerImages;
|
||||
}, [composerImages]);
|
||||
|
||||
useEffect(() => {
|
||||
setComposerImages((existing) => {
|
||||
revokePreviewUrls(existing);
|
||||
return [];
|
||||
});
|
||||
dragDepthRef.current = 0;
|
||||
setIsDragOverComposer(false);
|
||||
setExpandedImage(null);
|
||||
}, [activeThread?.id, revokePreviewUrls]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
revokePreviewUrls(composerImagesRef.current);
|
||||
};
|
||||
}, [revokePreviewUrls]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!expandedImage) {
|
||||
return;
|
||||
}
|
||||
|
||||
const onKeyDown = (event: globalThis.KeyboardEvent) => {
|
||||
if (event.key !== "Escape") {
|
||||
return;
|
||||
}
|
||||
setExpandedImage(null);
|
||||
};
|
||||
|
||||
window.addEventListener("keydown", onKeyDown);
|
||||
return () => {
|
||||
window.removeEventListener("keydown", onKeyDown);
|
||||
};
|
||||
}, [expandedImage]);
|
||||
|
||||
const activeWorktreePath = activeThread?.worktreePath;
|
||||
|
||||
useEffect(() => {
|
||||
@@ -431,6 +518,124 @@ export default function ChatView() {
|
||||
setIsEditorMenuOpen(false);
|
||||
};
|
||||
|
||||
const setThreadError = (threadId: string | null, error: string | null) => {
|
||||
if (!threadId) return;
|
||||
dispatch({
|
||||
type: "SET_ERROR",
|
||||
threadId,
|
||||
error,
|
||||
});
|
||||
};
|
||||
|
||||
const addComposerImages = (files: File[]) => {
|
||||
if (!activeThreadId || files.length === 0) return;
|
||||
|
||||
const nextImages = [...composerImagesRef.current];
|
||||
let error: string | null = null;
|
||||
for (const file of files) {
|
||||
if (!file.type.startsWith("image/")) {
|
||||
error = `Unsupported file type for '${file.name}'. Please attach image files only.`;
|
||||
continue;
|
||||
}
|
||||
if (file.size > PROVIDER_SEND_TURN_MAX_IMAGE_BYTES) {
|
||||
error = `'${file.name}' exceeds the ${IMAGE_SIZE_LIMIT_LABEL} attachment limit.`;
|
||||
continue;
|
||||
}
|
||||
if (nextImages.length >= PROVIDER_SEND_TURN_MAX_IMAGES) {
|
||||
error = `You can attach up to ${PROVIDER_SEND_TURN_MAX_IMAGES} images per message.`;
|
||||
break;
|
||||
}
|
||||
|
||||
const previewUrl = URL.createObjectURL(file);
|
||||
nextImages.push({
|
||||
id: crypto.randomUUID(),
|
||||
name: file.name || "image",
|
||||
mimeType: file.type,
|
||||
sizeBytes: file.size,
|
||||
previewUrl,
|
||||
file,
|
||||
});
|
||||
}
|
||||
|
||||
setComposerImages(nextImages);
|
||||
setThreadError(activeThreadId, error);
|
||||
};
|
||||
|
||||
const removeComposerImage = (imageId: string) => {
|
||||
setComposerImages((existing) => {
|
||||
const match = existing.find((image) => image.id === imageId);
|
||||
if (match?.previewUrl) {
|
||||
URL.revokeObjectURL(match.previewUrl);
|
||||
}
|
||||
return existing.filter((image) => image.id !== imageId);
|
||||
});
|
||||
};
|
||||
|
||||
const openImageDialog = (src: string | undefined, name: string) => {
|
||||
if (!src) {
|
||||
return;
|
||||
}
|
||||
setExpandedImage({ src, name });
|
||||
};
|
||||
|
||||
const onComposerPaste = (event: ClipboardEvent<HTMLTextAreaElement>) => {
|
||||
const files = Array.from(event.clipboardData.files);
|
||||
if (files.length === 0) {
|
||||
return;
|
||||
}
|
||||
const imageFiles = files.filter((file) => file.type.startsWith("image/"));
|
||||
if (imageFiles.length === 0) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
addComposerImages(imageFiles);
|
||||
};
|
||||
|
||||
const onComposerDragEnter = (event: DragEvent<HTMLDivElement>) => {
|
||||
if (!event.dataTransfer.types.includes("Files")) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
dragDepthRef.current += 1;
|
||||
setIsDragOverComposer(true);
|
||||
};
|
||||
|
||||
const onComposerDragOver = (event: DragEvent<HTMLDivElement>) => {
|
||||
if (!event.dataTransfer.types.includes("Files")) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
event.dataTransfer.dropEffect = "copy";
|
||||
setIsDragOverComposer(true);
|
||||
};
|
||||
|
||||
const onComposerDragLeave = (event: DragEvent<HTMLDivElement>) => {
|
||||
if (!event.dataTransfer.types.includes("Files")) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
const nextTarget = event.relatedTarget;
|
||||
if (nextTarget instanceof Node && event.currentTarget.contains(nextTarget)) {
|
||||
return;
|
||||
}
|
||||
dragDepthRef.current = Math.max(0, dragDepthRef.current - 1);
|
||||
if (dragDepthRef.current === 0) {
|
||||
setIsDragOverComposer(false);
|
||||
}
|
||||
};
|
||||
|
||||
const onComposerDrop = (event: DragEvent<HTMLDivElement>) => {
|
||||
if (!event.dataTransfer.types.includes("Files")) {
|
||||
return;
|
||||
}
|
||||
event.preventDefault();
|
||||
dragDepthRef.current = 0;
|
||||
setIsDragOverComposer(false);
|
||||
const files = Array.from(event.dataTransfer.files);
|
||||
addComposerImages(files);
|
||||
focusComposer();
|
||||
};
|
||||
|
||||
const ensureSession = async (cwdOverride?: string): Promise<EnsuredSessionInfo | null> => {
|
||||
if (!api || !activeThread || !activeProject) return null;
|
||||
if (activeThread.session && activeThread.session.status !== "closed") {
|
||||
@@ -492,8 +697,9 @@ export default function ChatView() {
|
||||
e.preventDefault();
|
||||
if (!api || !activeThread || isSending || isConnecting) return;
|
||||
const trimmed = prompt.trim();
|
||||
if (!trimmed) return;
|
||||
if (!trimmed && composerImages.length === 0) return;
|
||||
if (!activeProject) return;
|
||||
const composerImagesSnapshot = [...composerImages];
|
||||
|
||||
// On first message: lock in branch + create worktree if needed.
|
||||
let sessionCwd: string | undefined;
|
||||
@@ -529,7 +735,12 @@ export default function ChatView() {
|
||||
|
||||
// Auto-title from first message
|
||||
if (activeThread.messages.length === 0) {
|
||||
const title = trimmed.length > 50 ? `${trimmed.slice(0, 50)}...` : trimmed;
|
||||
const titleSeed =
|
||||
trimmed ||
|
||||
(composerImagesSnapshot.length > 0
|
||||
? `Image: ${composerImagesSnapshot[0]?.name ?? "attachment"}`
|
||||
: "New thread");
|
||||
const title = titleSeed.length > 50 ? `${titleSeed.slice(0, 50)}...` : titleSeed;
|
||||
dispatch({
|
||||
type: "SET_THREAD_TITLE",
|
||||
threadId: activeThread.id,
|
||||
@@ -537,43 +748,61 @@ export default function ChatView() {
|
||||
});
|
||||
}
|
||||
|
||||
dispatch({
|
||||
type: "SET_ERROR",
|
||||
threadId: activeThread.id,
|
||||
error: null,
|
||||
});
|
||||
setThreadError(activeThread.id, null);
|
||||
const messageImages: ChatImageAttachment[] = composerImagesSnapshot.map((image) => ({
|
||||
id: image.id,
|
||||
name: image.name,
|
||||
mimeType: image.mimeType,
|
||||
sizeBytes: image.sizeBytes,
|
||||
previewUrl: image.previewUrl,
|
||||
}));
|
||||
dispatch({
|
||||
type: "PUSH_USER_MESSAGE",
|
||||
threadId: activeThread.id,
|
||||
id: crypto.randomUUID(),
|
||||
text: trimmed,
|
||||
...(messageImages.length > 0 ? { images: messageImages } : {}),
|
||||
});
|
||||
const previousMessages = activeThread.messages;
|
||||
setPrompt("");
|
||||
setComposerImages([]);
|
||||
|
||||
const sessionInfo = await ensureSession(sessionCwd);
|
||||
if (!sessionInfo) return;
|
||||
|
||||
setIsSending(true);
|
||||
try {
|
||||
const turnImages = await Promise.all(
|
||||
composerImagesSnapshot.map(async (image): Promise<ProviderSendTurnImageInput> => ({
|
||||
name: image.name,
|
||||
mimeType: image.mimeType,
|
||||
sizeBytes: image.sizeBytes,
|
||||
dataUrl: await readFileAsDataUrl(image.file),
|
||||
})),
|
||||
);
|
||||
const shouldBootstrap =
|
||||
previousMessages.length > 0 &&
|
||||
(sessionInfo.continuityState === "new" || sessionInfo.continuityState === "fallback_new");
|
||||
const latestPromptForBootstrap = trimmed || IMAGE_ONLY_BOOTSTRAP_PROMPT;
|
||||
const input = shouldBootstrap
|
||||
? buildBootstrapInput(previousMessages, trimmed, PROVIDER_SEND_TURN_MAX_INPUT_CHARS).text
|
||||
: trimmed;
|
||||
? buildBootstrapInput(
|
||||
previousMessages,
|
||||
latestPromptForBootstrap,
|
||||
PROVIDER_SEND_TURN_MAX_INPUT_CHARS,
|
||||
).text
|
||||
: (trimmed || undefined);
|
||||
await api.providers.sendTurn({
|
||||
sessionId: sessionInfo.sessionId,
|
||||
input,
|
||||
...(input ? { input } : {}),
|
||||
...(turnImages.length > 0 ? { images: turnImages } : {}),
|
||||
model: selectedModel || undefined,
|
||||
effort: selectedEffort || undefined,
|
||||
});
|
||||
} catch (err) {
|
||||
dispatch({
|
||||
type: "SET_ERROR",
|
||||
threadId: activeThread.id,
|
||||
error: err instanceof Error ? err.message : "Failed to send message.",
|
||||
});
|
||||
setThreadError(
|
||||
activeThread.id,
|
||||
err instanceof Error ? err.message : "Failed to send message.",
|
||||
);
|
||||
} finally {
|
||||
setIsSending(false);
|
||||
}
|
||||
@@ -876,13 +1105,39 @@ export default function ChatView() {
|
||||
}
|
||||
|
||||
if (timelineEntry.message.role === "user") {
|
||||
const userImages = timelineEntry.message.images ?? [];
|
||||
return (
|
||||
<Fragment key={timelineEntry.id}>
|
||||
<div className="flex justify-end">
|
||||
<div className="max-w-[80%] rounded-2xl rounded-br-sm border border-border bg-secondary px-4 py-3">
|
||||
<pre className="whitespace-pre-wrap wrap-break-word font-mono text-sm leading-relaxed text-foreground">
|
||||
{timelineEntry.message.text}
|
||||
</pre>
|
||||
{userImages.length > 0 && (
|
||||
<div className="mb-2 grid max-w-[420px] grid-cols-2 gap-2">
|
||||
{userImages.map((image) => (
|
||||
<div
|
||||
key={image.id}
|
||||
className="overflow-hidden rounded-lg border border-border/80 bg-background/70"
|
||||
>
|
||||
{image.previewUrl ? (
|
||||
<img
|
||||
src={image.previewUrl}
|
||||
alt={image.name}
|
||||
className="h-full max-h-[220px] w-full cursor-zoom-in object-cover"
|
||||
onClick={() => openImageDialog(image.previewUrl, image.name)}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex min-h-[72px] items-center justify-center px-2 py-3 text-center text-[11px] text-muted-foreground/70">
|
||||
{image.name}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
{timelineEntry.message.text && (
|
||||
<pre className="whitespace-pre-wrap break-words font-mono text-sm leading-relaxed text-foreground">
|
||||
{timelineEntry.message.text}
|
||||
</pre>
|
||||
)}
|
||||
<p className="mt-1.5 text-right text-[10px] text-muted-foreground/30">
|
||||
{formatTimestamp(timelineEntry.message.createdAt)}
|
||||
</p>
|
||||
@@ -945,9 +1200,48 @@ export default function ChatView() {
|
||||
{/* Input bar */}
|
||||
<div className="px-5 pb-1 pt-2">
|
||||
<form onSubmit={onSend} className="mx-auto max-w-3xl">
|
||||
<div className="group rounded-[20px] border border-border bg-card transition-colors duration-200 focus-within:border-ring">
|
||||
<div
|
||||
className={`group rounded-[20px] border bg-card transition-colors duration-200 focus-within:border-ring ${
|
||||
isDragOverComposer ? "border-primary/70 bg-accent/30" : "border-border"
|
||||
}`}
|
||||
onDragEnter={onComposerDragEnter}
|
||||
onDragOver={onComposerDragOver}
|
||||
onDragLeave={onComposerDragLeave}
|
||||
onDrop={onComposerDrop}
|
||||
>
|
||||
{/* Textarea area */}
|
||||
<div className="px-4 pt-4 pb-2">
|
||||
{composerImages.length > 0 && (
|
||||
<div className="mb-3 flex flex-wrap gap-2">
|
||||
{composerImages.map((image) => (
|
||||
<div
|
||||
key={image.id}
|
||||
className="relative h-16 w-16 overflow-hidden rounded-lg border border-border/80 bg-background"
|
||||
>
|
||||
{image.previewUrl ? (
|
||||
<img
|
||||
src={image.previewUrl}
|
||||
alt={image.name}
|
||||
className="h-full w-full cursor-zoom-in object-cover"
|
||||
onClick={() => openImageDialog(image.previewUrl, image.name)}
|
||||
/>
|
||||
) : (
|
||||
<div className="flex h-full w-full items-center justify-center px-1 text-center text-[10px] text-muted-foreground/70">
|
||||
{image.name}
|
||||
</div>
|
||||
)}
|
||||
<button
|
||||
type="button"
|
||||
className="absolute right-1 top-1 flex h-4 w-4 items-center justify-center rounded-full bg-background/80 text-[11px] leading-none text-foreground transition-colors duration-150 hover:bg-background"
|
||||
onClick={() => removeComposerImage(image.id)}
|
||||
aria-label={`Remove ${image.name}`}
|
||||
>
|
||||
×
|
||||
</button>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
)}
|
||||
<textarea
|
||||
ref={textareaRef}
|
||||
className="w-full resize-none bg-transparent text-[14px] leading-relaxed text-foreground placeholder:text-muted-foreground/35 focus:outline-none"
|
||||
@@ -955,8 +1249,11 @@ export default function ChatView() {
|
||||
value={prompt}
|
||||
onChange={(e) => setPrompt(e.target.value)}
|
||||
onKeyDown={onKeyDown}
|
||||
onPaste={onComposerPaste}
|
||||
placeholder={
|
||||
phase === "disconnected" ? "Ask for follow-up changes" : "Ask anything..."
|
||||
phase === "disconnected"
|
||||
? "Ask for follow-up changes or attach images"
|
||||
: "Ask anything, or attach images..."
|
||||
}
|
||||
disabled={isSending || isConnecting}
|
||||
/>
|
||||
@@ -1146,7 +1443,7 @@ export default function ChatView() {
|
||||
<button
|
||||
type="submit"
|
||||
className="flex h-8 w-8 items-center justify-center rounded-full bg-primary/90 text-primary-foreground transition-all duration-150 hover:bg-primary hover:scale-105 disabled:opacity-30 disabled:hover:scale-100"
|
||||
disabled={isSending || isConnecting || !prompt.trim()}
|
||||
disabled={isSending || isConnecting || (!prompt.trim() && composerImages.length === 0)}
|
||||
aria-label={
|
||||
isConnecting ? "Connecting" : isSending ? "Sending" : "Send message"
|
||||
}
|
||||
@@ -1221,6 +1518,39 @@ export default function ChatView() {
|
||||
}
|
||||
/>
|
||||
)}
|
||||
|
||||
{expandedImage && (
|
||||
<div
|
||||
className="fixed inset-0 z-50 flex items-center justify-center bg-black/75 px-4 py-6 [-webkit-app-region:no-drag]"
|
||||
role="dialog"
|
||||
aria-modal="true"
|
||||
aria-label="Expanded image preview"
|
||||
onClick={() => setExpandedImage(null)}
|
||||
>
|
||||
<div
|
||||
className="relative isolate max-h-[92vh] max-w-[92vw]"
|
||||
onClick={(event) => event.stopPropagation()}
|
||||
>
|
||||
<button
|
||||
type="button"
|
||||
className="absolute right-2 top-2 z-30 flex h-8 w-8 cursor-pointer items-center justify-center rounded-full bg-black/60 text-xl leading-none text-white transition-colors duration-150 hover:bg-black/80 focus:outline-none focus-visible:ring-2 focus-visible:ring-white/80 [-webkit-app-region:no-drag]"
|
||||
onClick={() => setExpandedImage(null)}
|
||||
aria-label="Close image preview"
|
||||
>
|
||||
<span className="pointer-events-none">×</span>
|
||||
</button>
|
||||
<img
|
||||
src={expandedImage.src}
|
||||
alt={expandedImage.name}
|
||||
className="max-h-[86vh] max-w-[92vw] select-none rounded-lg border border-border/70 bg-background object-contain shadow-2xl"
|
||||
draggable={false}
|
||||
/>
|
||||
<p className="mt-2 max-w-[92vw] truncate text-center text-xs text-muted-foreground/80">
|
||||
{expandedImage.name}
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
@@ -91,4 +91,31 @@ describe("buildBootstrapInput", () => {
|
||||
expect(result.omittedCount).toBe(1);
|
||||
expect(result.truncated).toBe(true);
|
||||
});
|
||||
|
||||
it("captures user image attachment context in transcript blocks", () => {
|
||||
const result = buildBootstrapInput(
|
||||
[
|
||||
{
|
||||
id: "u-image",
|
||||
role: "user",
|
||||
text: "",
|
||||
images: [
|
||||
{
|
||||
id: "img-1",
|
||||
name: "screenshot.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 2_048,
|
||||
},
|
||||
],
|
||||
createdAt: "2026-02-09T00:00:00.000Z",
|
||||
streaming: false,
|
||||
},
|
||||
],
|
||||
"What does this error mean?",
|
||||
1_500,
|
||||
);
|
||||
|
||||
expect(result.text).toContain("Attached image");
|
||||
expect(result.text).toContain("screenshot.png");
|
||||
});
|
||||
});
|
||||
@@ -18,8 +18,33 @@ function messageRoleLabel(message: ChatMessage): "USER" | "ASSISTANT" {
|
||||
return message.role === "assistant" ? "ASSISTANT" : "USER";
|
||||
}
|
||||
|
||||
function imageSummary(message: ChatMessage): string | null {
|
||||
const count = message.images?.length ?? 0;
|
||||
if (count === 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const names = message.images?.slice(0, 3).map((image) => image.name) ?? [];
|
||||
const namesSummary = names.join(", ");
|
||||
const extraCount = count - names.length;
|
||||
const extraSummary = extraCount > 0 ? ` (+${extraCount} more)` : "";
|
||||
return `[Attached image${count === 1 ? "" : "s"}: ${namesSummary}${extraSummary}]`;
|
||||
}
|
||||
|
||||
function buildMessageBlock(message: ChatMessage): string {
|
||||
return `${messageRoleLabel(message)}:\n${message.text}`;
|
||||
const text = message.text;
|
||||
const images = imageSummary(message);
|
||||
|
||||
if (text && images) {
|
||||
return `${messageRoleLabel(message)}:\n${text}\n${images}`;
|
||||
}
|
||||
if (text) {
|
||||
return `${messageRoleLabel(message)}:\n${text}`;
|
||||
}
|
||||
if (images) {
|
||||
return `${messageRoleLabel(message)}:\n${images}`;
|
||||
}
|
||||
return `${messageRoleLabel(message)}:\n(empty message)`;
|
||||
}
|
||||
|
||||
function finalizeWithPrompt(
|
||||
@@ -68,7 +93,7 @@ export function buildBootstrapInput(
|
||||
let includedNewestFirst: string[] = [];
|
||||
for (const block of newestFirstBlocks) {
|
||||
const nextNewestFirst = [...includedNewestFirst, block];
|
||||
const nextChronological = [...nextNewestFirst].reverse();
|
||||
const nextChronological = nextNewestFirst.toReversed();
|
||||
const omittedCount = newestFirstBlocks.length - nextChronological.length;
|
||||
const transcriptBody =
|
||||
omittedCount > 0
|
||||
@@ -80,7 +105,7 @@ export function buildBootstrapInput(
|
||||
includedNewestFirst = nextNewestFirst;
|
||||
}
|
||||
|
||||
let includedChronological = [...includedNewestFirst].reverse();
|
||||
let includedChronological = includedNewestFirst.toReversed();
|
||||
while (true) {
|
||||
const omittedCount = newestFirstBlocks.length - includedChronological.length;
|
||||
const transcriptBody =
|
||||
|
||||
@@ -200,6 +200,15 @@ describe("toPersistedState", () => {
|
||||
id: "m-1",
|
||||
role: "user",
|
||||
text: "Hi",
|
||||
images: [
|
||||
{
|
||||
id: "img-1",
|
||||
name: "diagram.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 4_096,
|
||||
previewUrl: "blob:preview-1",
|
||||
},
|
||||
],
|
||||
createdAt: "2026-02-08T10:00:00.000Z",
|
||||
streaming: false,
|
||||
},
|
||||
@@ -236,7 +245,23 @@ describe("toPersistedState", () => {
|
||||
model: "gpt-5.3-codex",
|
||||
terminalOpen: true,
|
||||
terminalHeight: 320,
|
||||
messages: thread.messages,
|
||||
messages: [
|
||||
{
|
||||
id: "m-1",
|
||||
role: "user",
|
||||
text: "Hi",
|
||||
images: [
|
||||
{
|
||||
id: "img-1",
|
||||
name: "diagram.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 4_096,
|
||||
},
|
||||
],
|
||||
createdAt: "2026-02-08T10:00:00.000Z",
|
||||
streaming: false,
|
||||
},
|
||||
],
|
||||
createdAt: thread.createdAt,
|
||||
branch: null,
|
||||
worktreePath: null,
|
||||
|
||||
@@ -23,6 +23,16 @@ const persistedMessageSchema = z.object({
|
||||
id: z.string().min(1),
|
||||
role: z.enum(["user", "assistant"]),
|
||||
text: z.string(),
|
||||
images: z
|
||||
.array(
|
||||
z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
mimeType: z.string().min(1),
|
||||
sizeBytes: z.number().int().min(1),
|
||||
}),
|
||||
)
|
||||
.optional(),
|
||||
createdAt: z.string().min(1),
|
||||
streaming: z.boolean(),
|
||||
});
|
||||
@@ -126,10 +136,19 @@ function hydrateThread(
|
||||
terminalOpen: thread.terminalOpen ?? false,
|
||||
terminalHeight: thread.terminalHeight ?? DEFAULT_THREAD_TERMINAL_HEIGHT,
|
||||
session: null,
|
||||
messages: thread.messages.map((message) => ({
|
||||
...message,
|
||||
streaming: false,
|
||||
})),
|
||||
messages: thread.messages.map((message) => {
|
||||
const hydratedImages = message.images?.map((image) => ({
|
||||
...image,
|
||||
}));
|
||||
return {
|
||||
id: message.id,
|
||||
role: message.role,
|
||||
text: message.text,
|
||||
...(hydratedImages && hydratedImages.length > 0 ? { images: hydratedImages } : {}),
|
||||
createdAt: message.createdAt,
|
||||
streaming: false,
|
||||
};
|
||||
}),
|
||||
events: [],
|
||||
error: null,
|
||||
createdAt: thread.createdAt,
|
||||
@@ -189,7 +208,23 @@ export function toPersistedState(
|
||||
model: thread.model,
|
||||
terminalOpen: thread.terminalOpen,
|
||||
terminalHeight: thread.terminalHeight,
|
||||
messages: thread.messages,
|
||||
messages: thread.messages.map((message) => ({
|
||||
id: message.id,
|
||||
role: message.role,
|
||||
text: message.text,
|
||||
...(message.images && message.images.length > 0
|
||||
? {
|
||||
images: message.images.map((image) => ({
|
||||
id: image.id,
|
||||
name: image.name,
|
||||
mimeType: image.mimeType,
|
||||
sizeBytes: image.sizeBytes,
|
||||
})),
|
||||
}
|
||||
: {}),
|
||||
createdAt: message.createdAt,
|
||||
streaming: message.streaming,
|
||||
})),
|
||||
createdAt: thread.createdAt,
|
||||
branch: thread.branch,
|
||||
worktreePath: thread.worktreePath,
|
||||
|
||||
@@ -13,6 +13,7 @@ import { resolveModelSlug } from "./model-logic";
|
||||
import { hydratePersistedState, toPersistedState } from "./persistenceSchema";
|
||||
import { applyEventToMessages, asObject, asString, evolveSession } from "./session-logic";
|
||||
import {
|
||||
type ChatImageAttachment,
|
||||
DEFAULT_RUNTIME_MODE,
|
||||
type Project,
|
||||
type RuntimeMode,
|
||||
@@ -37,7 +38,13 @@ type Action =
|
||||
activeAssistantItemRef: { current: string | null };
|
||||
}
|
||||
| { type: "UPDATE_SESSION"; threadId: string; session: ProviderSession }
|
||||
| { type: "PUSH_USER_MESSAGE"; threadId: string; id: string; text: string }
|
||||
| {
|
||||
type: "PUSH_USER_MESSAGE";
|
||||
threadId: string;
|
||||
id: string;
|
||||
text: string;
|
||||
images?: ChatImageAttachment[];
|
||||
}
|
||||
| { type: "SET_ERROR"; threadId: string; error: string | null }
|
||||
| { type: "SET_THREAD_TITLE"; threadId: string; title: string }
|
||||
| { type: "SET_THREAD_MODEL"; threadId: string; model: string }
|
||||
@@ -380,6 +387,7 @@ export function reducer(state: AppState, action: Action): AppState {
|
||||
id: action.id,
|
||||
role: "user" as const,
|
||||
text: action.text,
|
||||
...(action.images && action.images.length > 0 ? { images: action.images } : {}),
|
||||
createdAt: new Date().toISOString(),
|
||||
streaming: false,
|
||||
},
|
||||
|
||||
@@ -5,10 +5,19 @@ export type RuntimeMode = "approval-required" | "full-access";
|
||||
export const DEFAULT_RUNTIME_MODE: RuntimeMode = "full-access";
|
||||
export const DEFAULT_THREAD_TERMINAL_HEIGHT = 280;
|
||||
|
||||
export interface ChatImageAttachment {
|
||||
id: string;
|
||||
name: string;
|
||||
mimeType: string;
|
||||
sizeBytes: number;
|
||||
previewUrl?: string;
|
||||
}
|
||||
|
||||
export interface ChatMessage {
|
||||
id: string;
|
||||
role: "user" | "assistant";
|
||||
text: string;
|
||||
images?: ChatImageAttachment[];
|
||||
createdAt: string;
|
||||
streaming: boolean;
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
PROVIDER_SEND_TURN_MAX_IMAGES,
|
||||
providerEventSchema,
|
||||
providerRespondToRequestInputSchema,
|
||||
providerSendTurnInputSchema,
|
||||
@@ -40,9 +41,64 @@ describe("providerSendTurnInputSchema", () => {
|
||||
effort: " high ",
|
||||
});
|
||||
expect(parsed.input).toBe("summarize this repo");
|
||||
expect(parsed.images).toEqual([]);
|
||||
expect(parsed.model).toBe("gpt-5.2-codex");
|
||||
expect(parsed.effort).toBe("high");
|
||||
});
|
||||
|
||||
it("accepts image-only turns", () => {
|
||||
const parsed = providerSendTurnInputSchema.parse({
|
||||
sessionId: "sess_1",
|
||||
images: [
|
||||
{
|
||||
name: "diagram.png",
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 1_024,
|
||||
dataUrl: "data:image/png;base64,AAAA",
|
||||
},
|
||||
],
|
||||
});
|
||||
expect(parsed.input).toBeUndefined();
|
||||
expect(parsed.images).toHaveLength(1);
|
||||
});
|
||||
|
||||
it("rejects turns with neither text nor images", () => {
|
||||
expect(() =>
|
||||
providerSendTurnInputSchema.parse({
|
||||
sessionId: "sess_1",
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects non-image data urls", () => {
|
||||
expect(() =>
|
||||
providerSendTurnInputSchema.parse({
|
||||
sessionId: "sess_1",
|
||||
images: [
|
||||
{
|
||||
name: "not-image.txt",
|
||||
mimeType: "text/plain",
|
||||
sizeBytes: 25,
|
||||
dataUrl: "data:text/plain;base64,QQ==",
|
||||
},
|
||||
],
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
|
||||
it("rejects more than the max image count", () => {
|
||||
expect(() =>
|
||||
providerSendTurnInputSchema.parse({
|
||||
sessionId: "sess_1",
|
||||
images: Array.from({ length: PROVIDER_SEND_TURN_MAX_IMAGES + 1 }, (_, index) => ({
|
||||
name: `image-${index}.png`,
|
||||
mimeType: "image/png",
|
||||
sizeBytes: 1_024,
|
||||
dataUrl: "data:image/png;base64,AAAA",
|
||||
})),
|
||||
}),
|
||||
).toThrow();
|
||||
});
|
||||
});
|
||||
|
||||
describe("providerEventSchema", () => {
|
||||
|
||||
@@ -55,12 +55,46 @@ export const providerSessionStartInputSchema = z.object({
|
||||
});
|
||||
|
||||
export const PROVIDER_SEND_TURN_MAX_INPUT_CHARS = 120_000;
|
||||
export const PROVIDER_SEND_TURN_MAX_IMAGES = 8;
|
||||
export const PROVIDER_SEND_TURN_MAX_IMAGE_BYTES = 10 * 1024 * 1024;
|
||||
export const PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS = 14_000_000;
|
||||
|
||||
export const providerSendTurnImageInputSchema = z
|
||||
.object({
|
||||
name: z.string().trim().min(1).max(255),
|
||||
mimeType: z
|
||||
.string()
|
||||
.trim()
|
||||
.min(1)
|
||||
.max(100)
|
||||
.regex(/^image\//i, "mimeType must be an image/* MIME type"),
|
||||
sizeBytes: z.number().int().min(1).max(PROVIDER_SEND_TURN_MAX_IMAGE_BYTES),
|
||||
dataUrl: z.string().trim().min(1).max(PROVIDER_SEND_TURN_MAX_IMAGE_DATA_URL_CHARS),
|
||||
})
|
||||
.superRefine((value, ctx) => {
|
||||
if (!value.dataUrl.startsWith("data:image/")) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Image dataUrl must start with data:image/",
|
||||
path: ["dataUrl"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const providerSendTurnInputSchema = z.object({
|
||||
sessionId: z.string().min(1),
|
||||
input: z.string().trim().min(1).max(PROVIDER_SEND_TURN_MAX_INPUT_CHARS),
|
||||
input: z.string().trim().min(1).max(PROVIDER_SEND_TURN_MAX_INPUT_CHARS).optional(),
|
||||
images: z.array(providerSendTurnImageInputSchema).max(PROVIDER_SEND_TURN_MAX_IMAGES).default([]),
|
||||
model: z.string().trim().min(1).optional(),
|
||||
effort: z.string().trim().min(1).optional(),
|
||||
}).superRefine((value, ctx) => {
|
||||
if (!value.input && value.images.length === 0) {
|
||||
ctx.addIssue({
|
||||
code: z.ZodIssueCode.custom,
|
||||
message: "Either input text or at least one image is required",
|
||||
path: ["input"],
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
export const providerTurnStartResultSchema = z.object({
|
||||
@@ -110,6 +144,8 @@ export type ProviderApprovalDecision = z.infer<typeof providerApprovalDecisionSc
|
||||
export type ProviderSessionStatus = z.infer<typeof providerSessionStatusSchema>;
|
||||
export type ProviderSession = z.infer<typeof providerSessionSchema>;
|
||||
export type ProviderSessionStartInput = z.input<typeof providerSessionStartInputSchema>;
|
||||
export type ProviderSendTurnImage = z.infer<typeof providerSendTurnImageInputSchema>;
|
||||
export type ProviderSendTurnImageInput = z.input<typeof providerSendTurnImageInputSchema>;
|
||||
export type ProviderSendTurnInput = z.input<typeof providerSendTurnInputSchema>;
|
||||
export type ProviderTurnStartResult = z.infer<typeof providerTurnStartResultSchema>;
|
||||
export type ProviderInterruptTurnInput = z.input<typeof providerInterruptTurnInputSchema>;
|
||||
|
||||
Reference in new issue
Block a user