@smoose/pi-beautify 0.1.3 → 0.1.4
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/README.md +1 -1
- package/package.json +1 -1
- package/src/index.ts +4 -31
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ A small pi extension for visual polish.
|
|
|
4
4
|
|
|
5
5
|
## Clipboard image chips
|
|
6
6
|
|
|
7
|
-
Pi currently pastes clipboard images as long temporary file paths. This extension replaces newly pasted `pi-clipboard-*` paths in the editor with compact chips like `[
|
|
7
|
+
Pi currently pastes clipboard images as long temporary file paths. This extension replaces newly pasted `pi-clipboard-*` paths in the editor with compact chips like `[image1]` for display, then restores those chips to the original file paths before the prompt is sent so the request stays identical to native pi behavior.
|
|
8
8
|
|
|
9
9
|
## Installation
|
|
10
10
|
|
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -1,27 +1,15 @@
|
|
|
1
1
|
import { CustomEditor, type AppKeybinding, type ExtensionAPI, type KeybindingsManager, type Theme } from "@earendil-works/pi-coding-agent";
|
|
2
|
-
import type { ImageContent } from "@earendil-works/pi-ai";
|
|
3
2
|
import { getKeybindings, matchesKey, truncateToWidth, type AutocompleteProvider, type EditorComponent, type EditorTheme, type TUI } from "@earendil-works/pi-tui";
|
|
4
|
-
import { existsSync, readFileSync } from "node:fs";
|
|
5
|
-
import { extname } from "node:path";
|
|
6
3
|
|
|
7
4
|
interface Attachment {
|
|
8
5
|
token: string;
|
|
9
6
|
path: string;
|
|
10
|
-
mimeType: string;
|
|
11
7
|
}
|
|
12
8
|
|
|
13
9
|
const CLIPBOARD_PATH_RE = /(?:[^\s"'`<>]+[\\/])?pi-clipboard-[0-9a-f-]+\.(?:png|jpe?g|webp|gif)/gi;
|
|
14
10
|
const TOKEN_RE = /\[image(\d+)\]/g;
|
|
15
11
|
const TOKEN_LINE_RE = /\[image\d+\]/g;
|
|
16
12
|
|
|
17
|
-
function mimeTypeForPath(path: string): string {
|
|
18
|
-
const ext = extname(path).toLowerCase();
|
|
19
|
-
if (ext === ".jpg" || ext === ".jpeg") return "image/jpeg";
|
|
20
|
-
if (ext === ".webp") return "image/webp";
|
|
21
|
-
if (ext === ".gif") return "image/gif";
|
|
22
|
-
return "image/png";
|
|
23
|
-
}
|
|
24
|
-
|
|
25
13
|
function imageChip(id: number): string {
|
|
26
14
|
return `[image${id}]`;
|
|
27
15
|
}
|
|
@@ -118,7 +106,7 @@ class ImageTokenController {
|
|
|
118
106
|
while (usedIds.has(id)) id++;
|
|
119
107
|
usedIds.add(id);
|
|
120
108
|
const token = imageChip(id);
|
|
121
|
-
this.attachments.set(token, { token, path
|
|
109
|
+
this.attachments.set(token, { token, path });
|
|
122
110
|
return `${token} `;
|
|
123
111
|
}
|
|
124
112
|
}
|
|
@@ -337,15 +325,6 @@ function collectImageAttachments(text: string, attachments: Map<string, Attachme
|
|
|
337
325
|
return selected;
|
|
338
326
|
}
|
|
339
327
|
|
|
340
|
-
function toImageContent(attachment: Attachment): ImageContent | undefined {
|
|
341
|
-
if (!existsSync(attachment.path)) return undefined;
|
|
342
|
-
return {
|
|
343
|
-
type: "image",
|
|
344
|
-
data: readFileSync(attachment.path).toString("base64"),
|
|
345
|
-
mimeType: attachment.mimeType,
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
|
|
349
328
|
export default function piBeautify(pi: ExtensionAPI) {
|
|
350
329
|
const attachments = new Map<string, Attachment>();
|
|
351
330
|
|
|
@@ -368,23 +347,17 @@ export default function piBeautify(pi: ExtensionAPI) {
|
|
|
368
347
|
if (ctx.hasUI) ctx.ui.setStatus("pi-beautify", undefined);
|
|
369
348
|
});
|
|
370
349
|
|
|
371
|
-
pi.on("input", async (event
|
|
350
|
+
pi.on("input", async (event) => {
|
|
372
351
|
const selected = collectImageAttachments(event.text, attachments);
|
|
373
352
|
if (selected.length === 0) return { action: "continue" };
|
|
374
353
|
|
|
375
|
-
const
|
|
376
|
-
if (converted.length === 0) {
|
|
377
|
-
if (ctx.hasUI) ctx.ui.notify("pi-beautify: image file disappeared before submit", "warning");
|
|
378
|
-
return { action: "continue" };
|
|
379
|
-
}
|
|
380
|
-
|
|
354
|
+
const text = event.text.replace(TOKEN_RE, (full, id) => attachments.get(imageChip(Number(id)))?.path ?? full);
|
|
381
355
|
for (const attachment of selected) attachments.delete(attachment.token);
|
|
382
356
|
|
|
383
|
-
const text = event.text.replace(TOKEN_RE, (_full, id) => `[attached image ${id}]`);
|
|
384
357
|
return {
|
|
385
358
|
action: "transform",
|
|
386
359
|
text,
|
|
387
|
-
images:
|
|
360
|
+
images: event.images,
|
|
388
361
|
};
|
|
389
362
|
});
|
|
390
363
|
}
|