Skip to content

graff — a fast agentic coding harness in Zig: multi-provider, MCP, workflows, DGM evolution loop, TS/Python SDKs

Python

fix(native): render pasted images in user bubbles via /api/attach

Rach Pradhan5d15d57a7832
diff --git a/apps/native/app/api/attach/route.ts b/apps/native/app/api/attach/route.ts
index 175c54ff..3e17d1d0 100644
--- a/apps/native/app/api/attach/route.ts
+++ b/apps/native/app/api/attach/route.ts
@@ -1,4 +1,4 @@
-import { mkdirSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
+import { mkdirSync, readFileSync, realpathSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
import os from "node:os";
import path from "node:path";
import { NextRequest } from "next/server";
@@ -52,6 +52,31 @@ function safeName(raw: string, type: string): string {
return sub ? `pasted.${sub === "jpeg" ? "jpg" : sub}` : "pasted";
}
+const IMAGE_TYPES: Record<string, string> = {
+ ".png": "image/png", ".jpg": "image/jpeg", ".jpeg": "image/jpeg",
+ ".gif": "image/gif", ".webp": "image/webp", ".avif": "image/avif", ".bmp": "image/bmp",
+};
+
+/** Serve only staged raster images, never arbitrary paths or active SVG content. */
+export async function GET(req: NextRequest) {
+ const name = req.nextUrl.searchParams.get("name") ?? "";
+ const type = IMAGE_TYPES[path.extname(name).toLowerCase()];
+ if (!name || path.basename(name) !== name || !type) {
+ return new Response("Invalid image", { status: 400 });
+ }
+ try {
+ const target = realpathSync(path.join(DIR, name));
+ if (path.dirname(target) !== realpathSync(DIR)) return new Response("Not found", { status: 404 });
+ if (statSync(target).size > MAX_BYTES) return new Response("Image too large", { status: 413 });
+ return new Response(readFileSync(target), { headers: {
+ "Content-Type": type, "Cache-Control": "private, max-age=3600",
+ "X-Content-Type-Options": "nosniff",
+ } });
+ } catch {
+ return new Response("Image no longer available", { status: 404 });
+ }
+}
+
export async function POST(req: NextRequest) {
let form: FormData;
try {
diff --git a/apps/native/components/site/ChatBubbles.tsx b/apps/native/components/site/ChatBubbles.tsx
index bfb9f32a..db015645 100644
--- a/apps/native/components/site/ChatBubbles.tsx
+++ b/apps/native/components/site/ChatBubbles.tsx
@@ -56,14 +56,31 @@ const ToolGroup = memo(function ToolGroup({ tools, diffs, onOpenPath }: {
}, (previous, next) => previous.diffs === next.diffs && previous.onOpenPath === next.onOpenPath &&
previous.tools.length === next.tools.length && previous.tools.every((tool, index) => tool === next.tools[index]));
+function PastedImage({ name }: { name: string }) {
+ const [failed, setFailed] = useState(false);
+ const src = `/api/attach?name=${encodeURIComponent(name)}`;
+ if (failed) return <span className="block text-xs text-ink-3">Image no longer available</span>;
+ return (
+ <a href={src} target="_blank" rel="noreferrer" aria-label="Open pasted image" className="block my-2">
+ {/* Local staged pixels: no remote image optimizer or expiring object URL. */}
+ {/* eslint-disable-next-line @next/next/no-img-element */}
+ <img src={src} alt="Pasted image" loading="lazy" onError={() => setFailed(true)}
+ className="block max-h-80 max-w-full rounded-lg object-contain" />
+ </a>
+ );
+}
+
export const UserBubble = memo(function UserBubble({ text }: { text: string }) {
+ const parts = text.split(/(@\[[^\]\n]*\/graff-native-attachments\/[^/\]\n]+\.(?:png|jpe?g|gif|webp|avif|bmp)\])/gi);
return (
<div className="flex justify-end pl-10 sm:pl-24" style={{ animation: "fade-up 300ms cubic-bezier(0.23,1,0.32,1) both" }}>
<div
className="rounded-xl px-3.5 py-2 min-w-0 whitespace-pre-wrap break-words [overflow-wrap:anywhere] text-[13px] leading-relaxed text-ink shadow-hairline"
style={{ background: "color-mix(in oklab, var(--accent) 12%, var(--surface))" }}
>
- {text}
+ {parts.map((part, index) => index % 2 === 1
+ ? <PastedImage key={`${index}-${part}`} name={part.slice(part.lastIndexOf("/") + 1, -1)} />
+ : part)}
</div>
</div>
);