@maheidem/pi-auto-image-attach 0.1.0
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/LICENSE +32 -0
- package/README.md +29 -0
- package/index.ts +109 -0
- package/package.json +46 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 maheidem
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
---
|
|
24
|
+
|
|
25
|
+
This project builds against and vendors patterns from works under the MIT
|
|
26
|
+
License, copyright their respective authors:
|
|
27
|
+
|
|
28
|
+
- @earendil-works/pi-coding-agent (pi coding agent), including the official
|
|
29
|
+
`subagent` and `plan-mode` extension examples and RPC documentation.
|
|
30
|
+
- @earendil-works/pi-tui.
|
|
31
|
+
- The local `/loop` extension (same workspace), from which
|
|
32
|
+
`ui/settings-panel.ts` is vendored.
|
package/README.md
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
# @maheidem/pi-auto-image-attach
|
|
2
|
+
|
|
3
|
+
A Pi extension that auto-attaches pasted image file paths as real image content.
|
|
4
|
+
|
|
5
|
+
## Why
|
|
6
|
+
|
|
7
|
+
Dragging an image into the Pi TUI inserts an escaped file-path string (upstream
|
|
8
|
+
gap: earendil-works/pi#6572). The model then only sees a path, not the image.
|
|
9
|
+
This extension intercepts `input`, detects image paths (png/jpg/jpeg/webp/gif/bmp,
|
|
10
|
+
absolute or relative, with backslash-escaped spaces), sniffs the magic bytes for
|
|
11
|
+
the MIME type, base64-encodes the file, attaches it as `ImageContent`, and strips
|
|
12
|
+
the raw path from the message text.
|
|
13
|
+
|
|
14
|
+
Only fires for `interactive` input with no already-attached images; silently
|
|
15
|
+
passes through otherwise.
|
|
16
|
+
|
|
17
|
+
## Install
|
|
18
|
+
|
|
19
|
+
```
|
|
20
|
+
pi install npm:@maheidem/pi-auto-image-attach
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Or add the path to `~/.pi/agent/settings.json → packages`, then `/reload`.
|
|
24
|
+
|
|
25
|
+
## Caveats
|
|
26
|
+
|
|
27
|
+
- Relative paths resolve against pi's CWD.
|
|
28
|
+
- Vision still depends on the active model's `input` supporting images (see
|
|
29
|
+
model-discovery's vision override for local oMLX models).
|
package/index.ts
ADDED
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Auto-Image-Attach Extension
|
|
3
|
+
*
|
|
4
|
+
* When you drop an image into pi's chat, it's inserted as a file path
|
|
5
|
+
* (with escaped spaces). This extension intercepts the input, detects
|
|
6
|
+
* image file paths, reads them, and attaches them as ImageContent blocks
|
|
7
|
+
* so the model actually sees the image.
|
|
8
|
+
*
|
|
9
|
+
* Handles:
|
|
10
|
+
* - Absolute paths: /var/folders/.../Screenshot 2026-07-30 at 15.00.00.png
|
|
11
|
+
* - Relative paths: ./screenshot.png
|
|
12
|
+
* - Escaped spaces: /path/to/Screenshot\ 2026-07-30\ at\ 15.00.00.png
|
|
13
|
+
*
|
|
14
|
+
* Location: ~/.pi/agent/extensions/auto-image-attach/index.ts
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
18
|
+
|
|
19
|
+
/** Minimal local ImageContent (pi-ai's type is not re-exported by pi-coding-agent). */
|
|
20
|
+
interface ImageContent {
|
|
21
|
+
type: "image";
|
|
22
|
+
data: string;
|
|
23
|
+
mimeType: string;
|
|
24
|
+
}
|
|
25
|
+
import * as fs from "node:fs";
|
|
26
|
+
import * as path from "node:path";
|
|
27
|
+
|
|
28
|
+
const IMAGE_EXTENSIONS = new Set([
|
|
29
|
+
".png", ".jpg", ".jpeg", ".webp", ".gif", ".bmp",
|
|
30
|
+
]);
|
|
31
|
+
|
|
32
|
+
function isImagePath(filePath: string): boolean {
|
|
33
|
+
return IMAGE_EXTENSIONS.has(path.extname(filePath).toLowerCase());
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
async function detectImageMimeType(filePath: string): Promise<string | null> {
|
|
37
|
+
try {
|
|
38
|
+
const fd = await fs.promises.open(filePath, "r");
|
|
39
|
+
const buffer = Buffer.alloc(4100);
|
|
40
|
+
const { bytesRead } = await fd.read(buffer, 0, 4100, 0);
|
|
41
|
+
await fd.close();
|
|
42
|
+
const s = buffer.subarray(0, bytesRead);
|
|
43
|
+
if (s[0] === 0x89 && s[1] === 0x50 && s[2] === 0x4e && s[3] === 0x47 &&
|
|
44
|
+
s[4] === 0x0d && s[5] === 0x0a && s[6] === 0x1a && s[7] === 0x0a) return "image/png";
|
|
45
|
+
if (s[0] === 0xff && s[1] === 0xd8 && s[2] === 0xff) return "image/jpeg";
|
|
46
|
+
if (s[0] === 0x47 && s[1] === 0x49 && s[2] === 0x46 && s[3] === 0x38) return "image/gif";
|
|
47
|
+
if (s[0] === 0x52 && s[1] === 0x49 && s[2] === 0x46 && s[3] === 0x46 &&
|
|
48
|
+
s[8] === 0x57 && s[9] === 0x45 && s[10] === 0x42 && s[11] === 0x50) return "image/webp";
|
|
49
|
+
if (s[0] === 0x42 && s[1] === 0x4d) return "image/bmp";
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
catch { return null; }
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
interface ImagePathEntry {
|
|
56
|
+
rawPath: string; // Original from text (with backslash escapes)
|
|
57
|
+
cleanPath: string; // Resolved path (for file operations)
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export default function (pi: ExtensionAPI) {
|
|
61
|
+
pi.on("input", async (event, _ctx) => {
|
|
62
|
+
if (event.source !== "interactive") return { action: "continue" };
|
|
63
|
+
if (event.images?.length) return { action: "continue" };
|
|
64
|
+
|
|
65
|
+
const text = event.text;
|
|
66
|
+
const entries: ImagePathEntry[] = [];
|
|
67
|
+
|
|
68
|
+
// Find image extensions, scan back for path start (handles escaped spaces)
|
|
69
|
+
const extRegex = /\.(png|jpe?g|webp|gif|bmp)($|["'\s,;:])/gi;
|
|
70
|
+
let m;
|
|
71
|
+
while ((m = extRegex.exec(text)) !== null) {
|
|
72
|
+
const end = m.index + m[0].length;
|
|
73
|
+
// Scan back to unescaped space or beginning
|
|
74
|
+
let start = end - 1;
|
|
75
|
+
while (start > 0 && !(text[start] === " " && text[start - 1] !== "\\")) start--;
|
|
76
|
+
const raw = text.substring(start, end);
|
|
77
|
+
const clean = raw.replace(/\\/g, "");
|
|
78
|
+
if ((clean.startsWith("/") || clean.startsWith("./") || clean.startsWith("../"))
|
|
79
|
+
&& isImagePath(clean) && fs.existsSync(clean)) {
|
|
80
|
+
entries.push({ rawPath: raw, cleanPath: clean });
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
if (entries.length === 0) return { action: "continue" };
|
|
85
|
+
|
|
86
|
+
// Read and encode images
|
|
87
|
+
const images: ImageContent[] = [];
|
|
88
|
+
for (const { cleanPath } of entries) {
|
|
89
|
+
try {
|
|
90
|
+
const mimeType = await detectImageMimeType(cleanPath);
|
|
91
|
+
if (!mimeType) continue;
|
|
92
|
+
const data = fs.readFileSync(cleanPath).toString("base64");
|
|
93
|
+
images.push({ type: "image", data, mimeType });
|
|
94
|
+
} catch { /* skip unreadable */ }
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (images.length === 0) return { action: "continue" };
|
|
98
|
+
|
|
99
|
+
// Remove image paths from text
|
|
100
|
+
let cleaned = text;
|
|
101
|
+
for (const { rawPath } of entries) {
|
|
102
|
+
const escaped = rawPath.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
|
|
103
|
+
cleaned = cleaned.replace(new RegExp(escaped, "g"), "");
|
|
104
|
+
}
|
|
105
|
+
cleaned = cleaned.replace(/\s+/g, " ").trim();
|
|
106
|
+
|
|
107
|
+
return { action: "transform", text: cleaned || text, images };
|
|
108
|
+
});
|
|
109
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@maheidem/pi-auto-image-attach",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"description": "Pi extension: auto-attaches pasted/dragged image file paths as ImageContent blocks so the model actually sees the image.",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"pi-package",
|
|
8
|
+
"extension",
|
|
9
|
+
"images"
|
|
10
|
+
],
|
|
11
|
+
"files": [
|
|
12
|
+
"index.ts",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"typecheck": "tsc -p tsconfig.json"
|
|
18
|
+
},
|
|
19
|
+
"peerDependencies": {
|
|
20
|
+
"@earendil-works/pi-coding-agent": ">=0.84.0"
|
|
21
|
+
},
|
|
22
|
+
"devDependencies": {
|
|
23
|
+
"@earendil-works/pi-coding-agent": "^0.84.4",
|
|
24
|
+
"@types/node": "^22.0.0",
|
|
25
|
+
"typescript": "^5.9.3"
|
|
26
|
+
},
|
|
27
|
+
"pi": {
|
|
28
|
+
"extensions": [
|
|
29
|
+
"./index.ts"
|
|
30
|
+
]
|
|
31
|
+
},
|
|
32
|
+
"license": "MIT",
|
|
33
|
+
"author": "maheidem",
|
|
34
|
+
"publishConfig": {
|
|
35
|
+
"access": "public"
|
|
36
|
+
},
|
|
37
|
+
"repository": {
|
|
38
|
+
"type": "git",
|
|
39
|
+
"url": "git+https://github.com/Maheidem/pi-coder-management.git",
|
|
40
|
+
"directory": "custom-extensions/auto-image-attach"
|
|
41
|
+
},
|
|
42
|
+
"homepage": "https://github.com/Maheidem/pi-coder-management/tree/main/custom-extensions/auto-image-attach#readme",
|
|
43
|
+
"bugs": {
|
|
44
|
+
"url": "https://github.com/Maheidem/pi-coder-management/issues"
|
|
45
|
+
}
|
|
46
|
+
}
|