@dougbots/opencode-image-loader 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/dist/index.d.ts +7 -0
- package/dist/index.js +52 -0
- package/dist/index.js.map +1 -0
- package/package.json +35 -0
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { readFile, stat } from "fs/promises";
|
|
2
|
+
import { extname, resolve } from "path";
|
|
3
|
+
import { tool } from "@opencode-ai/plugin/tool";
|
|
4
|
+
//#region src/index.ts
|
|
5
|
+
const SUPPORTED = {
|
|
6
|
+
".png": "image/png",
|
|
7
|
+
".jpg": "image/jpeg",
|
|
8
|
+
".jpeg": "image/jpeg",
|
|
9
|
+
".gif": "image/gif",
|
|
10
|
+
".webp": "image/webp",
|
|
11
|
+
".svg": "image/svg+xml",
|
|
12
|
+
".bmp": "image/bmp",
|
|
13
|
+
".tiff": "image/tiff",
|
|
14
|
+
".tif": "image/tiff"
|
|
15
|
+
};
|
|
16
|
+
const imageTool = tool({
|
|
17
|
+
description: "Loads an image file from disk and converts it to a base64 data URL. Use this when you need to 'see' an image file provided by the user.",
|
|
18
|
+
args: { filePath: tool.schema.string().describe("The absolute or relative path to the image file.") },
|
|
19
|
+
async execute({ filePath }, ctx) {
|
|
20
|
+
try {
|
|
21
|
+
const absolutePath = resolve(ctx.directory, filePath);
|
|
22
|
+
const stats = await stat(absolutePath);
|
|
23
|
+
if (!stats.isFile()) return { output: `Error: '${filePath}' is not a file.` };
|
|
24
|
+
const buffer = await readFile(absolutePath);
|
|
25
|
+
const ext = extname(absolutePath).toLowerCase();
|
|
26
|
+
const mimeType = SUPPORTED[ext] || "application/octet-stream";
|
|
27
|
+
if (!SUPPORTED[ext]) return { output: `Error: Unsupported image extension '${ext}'. Supported: ${Object.keys(SUPPORTED).join(", ")}` };
|
|
28
|
+
const dataUrl = `data:${mimeType};base64,${buffer.toString("base64")}`;
|
|
29
|
+
ctx.metadata({
|
|
30
|
+
mimeType,
|
|
31
|
+
fileSize: stats.size,
|
|
32
|
+
path: absolutePath
|
|
33
|
+
});
|
|
34
|
+
return {
|
|
35
|
+
output: `Successfully converted '${filePath}' to base64.`,
|
|
36
|
+
attachments: [{
|
|
37
|
+
type: "file",
|
|
38
|
+
mime: mimeType,
|
|
39
|
+
url: dataUrl,
|
|
40
|
+
filename: absolutePath.split("/").pop()
|
|
41
|
+
}]
|
|
42
|
+
};
|
|
43
|
+
} catch (error) {
|
|
44
|
+
return { output: `Error loading image: ${error instanceof Error ? error.message : String(error)}` };
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
const ImageLoaderPlugin = async () => ({ tool: { image_to_base64: imageTool } });
|
|
49
|
+
//#endregion
|
|
50
|
+
export { ImageLoaderPlugin };
|
|
51
|
+
|
|
52
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","names":[],"sources":["../src/index.ts"],"sourcesContent":["import type { Plugin } from \"@opencode-ai/plugin\";\nimport { readFile, stat } from \"fs/promises\";\nimport { extname, resolve } from \"path\";\nimport { tool } from \"@opencode-ai/plugin/tool\";\n\nconst SUPPORTED: Record<string, string> = {\n \".png\": \"image/png\",\n \".jpg\": \"image/jpeg\",\n \".jpeg\": \"image/jpeg\",\n \".gif\": \"image/gif\",\n \".webp\": \"image/webp\",\n \".svg\": \"image/svg+xml\",\n \".bmp\": \"image/bmp\",\n \".tiff\": \"image/tiff\",\n \".tif\": \"image/tiff\",\n};\n\nconst imageTool = tool({\n description:\n \"Loads an image file from disk and converts it to a base64 data URL. Use this when you need to 'see' an image file provided by the user.\",\n args: {\n filePath: tool.schema\n .string()\n .describe(\"The absolute or relative path to the image file.\"),\n },\n async execute({ filePath }, ctx) {\n try {\n const absolutePath = resolve(ctx.directory, filePath);\n const stats = await stat(absolutePath);\n\n if (!stats.isFile()) {\n return {\n output: `Error: '${filePath}' is not a file.`,\n };\n }\n\n const buffer = await readFile(absolutePath);\n const ext = extname(absolutePath).toLowerCase();\n const mimeType = SUPPORTED[ext] || \"application/octet-stream\";\n\n if (!SUPPORTED[ext]) {\n return {\n output: `Error: Unsupported image extension '${ext}'. Supported: ${Object.keys(SUPPORTED).join(\", \")}`,\n };\n }\n\n const base64 = buffer.toString(\"base64\");\n const dataUrl = `data:${mimeType};base64,${base64}`;\n\n ctx.metadata({\n mimeType,\n fileSize: stats.size,\n path: absolutePath,\n });\n\n return {\n output: `Successfully converted '${filePath}' to base64.`,\n attachments: [\n {\n type: \"file\",\n mime: mimeType,\n url: dataUrl,\n filename: absolutePath.split(\"/\").pop(),\n },\n ],\n };\n } catch (error: unknown) {\n const message = error instanceof Error ? error.message : String(error);\n return { output: `Error loading image: ${message}` };\n }\n },\n});\n\nexport const ImageLoaderPlugin: Plugin = async () => ({\n tool: {\n image_to_base64: imageTool,\n },\n});\n"],"mappings":";;;;AAKA,MAAM,YAAoC;CACxC,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,SAAS;CACT,QAAQ;CACR,QAAQ;CACR,SAAS;CACT,QAAQ;AACV;AAEA,MAAM,YAAY,KAAK;CACrB,aACE;CACF,MAAM,EACJ,UAAU,KAAK,OACZ,OAAO,CAAC,CACR,SAAS,kDAAkD,EAChE;CACA,MAAM,QAAQ,EAAE,YAAY,KAAK;EAC/B,IAAI;GACF,MAAM,eAAe,QAAQ,IAAI,WAAW,QAAQ;GACpD,MAAM,QAAQ,MAAM,KAAK,YAAY;GAErC,IAAI,CAAC,MAAM,OAAO,GAChB,OAAO,EACL,QAAQ,WAAW,SAAS,kBAC9B;GAGF,MAAM,SAAS,MAAM,SAAS,YAAY;GAC1C,MAAM,MAAM,QAAQ,YAAY,CAAC,CAAC,YAAY;GAC9C,MAAM,WAAW,UAAU,QAAQ;GAEnC,IAAI,CAAC,UAAU,MACb,OAAO,EACL,QAAQ,uCAAuC,IAAI,gBAAgB,OAAO,KAAK,SAAS,CAAC,CAAC,KAAK,IAAI,IACrG;GAIF,MAAM,UAAU,QAAQ,SAAS,UADlB,OAAO,SAAS,QACiB;GAEhD,IAAI,SAAS;IACX;IACA,UAAU,MAAM;IAChB,MAAM;GACR,CAAC;GAED,OAAO;IACL,QAAQ,2BAA2B,SAAS;IAC5C,aAAa,CACX;KACE,MAAM;KACN,MAAM;KACN,KAAK;KACL,UAAU,aAAa,MAAM,GAAG,CAAC,CAAC,IAAI;IACxC,CACF;GACF;EACF,SAAS,OAAgB;GAEvB,OAAO,EAAE,QAAQ,wBADD,iBAAiB,QAAQ,MAAM,UAAU,OAAO,KAAK,IAClB;EACrD;CACF;AACF,CAAC;AAED,MAAa,oBAA4B,aAAa,EACpD,MAAM,EACJ,iBAAiB,UACnB,EACF"}
|
package/package.json
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@dougbots/opencode-image-loader",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Image-to-base64 tool for opencode — lets image-capable models see local image files",
|
|
5
|
+
"keywords": ["opencode-plugin", "image", "base64"],
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "git+https://github.com/sdougbrown/opencode-image-loader.git"
|
|
10
|
+
},
|
|
11
|
+
"type": "module",
|
|
12
|
+
"files": ["dist"],
|
|
13
|
+
"main": "./dist/index.js",
|
|
14
|
+
"types": "./dist/index.d.ts",
|
|
15
|
+
"exports": {
|
|
16
|
+
".": {
|
|
17
|
+
"types": "./dist/index.d.ts",
|
|
18
|
+
"default": "./dist/index.js"
|
|
19
|
+
}
|
|
20
|
+
},
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsdown"
|
|
26
|
+
},
|
|
27
|
+
"dependencies": {
|
|
28
|
+
"@opencode-ai/plugin": "^1.15.4"
|
|
29
|
+
},
|
|
30
|
+
"devDependencies": {
|
|
31
|
+
"@types/bun": "^1.3.12",
|
|
32
|
+
"tsdown": "^0.22.0",
|
|
33
|
+
"typescript": "^5.7.0"
|
|
34
|
+
}
|
|
35
|
+
}
|