repositories / pi-ext

pi-ext

bugabingas pi extensions

owned by bugabinga

web: show local image preview in understand_image

a64748e64cb130075117e6bdd8bcaf9ae092edae

Oliver Jan Krylow <oliver@bugabinga.net> ·

unsigned

Browse filesCompare with HEAD

Changed files (1)

extensions/web/index.ts

Modified
21 21 import { mkdtemp, rm, writeFile } from "node:fs/promises";
22 22 import { tmpdir } from "node:os";
23 23 import { join } from "node:path";
24 -import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
25 -import { getMarkdownTheme, keyHint } from "@earendil-works/pi-coding-agent";
24 +import type {
25 + ExtensionAPI,
26 + ExtensionContext,
27 +} from "@earendil-works/pi-coding-agent";
28 +import {
29 + createReadToolDefinition,
30 + getMarkdownTheme,
31 + keyHint,
32 +} from "@earendil-works/pi-coding-agent";
26 33 import { Box, Markdown, Text } from "@earendil-works/pi-tui";
27 34 import { Type } from "typebox";
28 35 import {
56 63 return n >= 10_000 ? `${(n / 1_000).toFixed(1)}K chars` : `${n} chars`;
66 +async function readLocalImagePreview(
67 + imageSource: string,
68 + signal: AbortSignal | undefined,
69 + ctx: ExtensionContext,
70 +) {
71 + if (
72 + imageSource.startsWith("http://") ||
73 + imageSource.startsWith("https://") ||
74 + imageSource.startsWith("data:")
75 + ) {
76 + return [];
77 + }
79 + const path = imageSource.startsWith("@") ? imageSource.slice(1) : imageSource;
80 + try {
81 + const readTool = createReadToolDefinition(ctx.cwd);
82 + const result = await readTool.execute(
83 + "understand_image_preview",
84 + { path },
85 + signal,
86 + undefined,
87 + ctx,
88 + );
89 + return result.content.filter((block) => block.type === "image");
90 + } catch {
91 + return [];
92 + }
59 95 // ── Temp file lifecycle ───────────────────────────────────────────────────
61 97 const tempDirs = new Set<string>();
1316 1352 signal,
1355 + const imagePreview = await readLocalImagePreview(
1356 + image_source,
1357 + signal,
1358 + ctx,
1359 + );
1318 1361 return {
1319 - content: [{ type: "text", text: content }],
1362 + content: [{ type: "text", text: content }, ...imagePreview],
1320 1363 details: { prompt, image_source },
1322 1365 } catch (error) {