micro-models-agent 0.16.8 → 0.17.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/cli/repl.js +9 -15
- package/dist/llm/image-utils.js +109 -70
- package/dist/tools/attach-image.js +5 -6
- package/package.json +44 -43
package/dist/cli/repl.js
CHANGED
|
@@ -161,17 +161,16 @@ export class Repl {
|
|
|
161
161
|
const { resolve } = await import("path");
|
|
162
162
|
let dataUrl;
|
|
163
163
|
let label;
|
|
164
|
-
let warning;
|
|
165
164
|
if (source.toLowerCase() === "clipboard") {
|
|
166
|
-
const
|
|
167
|
-
if (!
|
|
165
|
+
const clipBuf = await readClipboardImage();
|
|
166
|
+
if (!clipBuf) {
|
|
168
167
|
console.log(pc.yellow(t("image.clipboard_empty")));
|
|
169
168
|
return;
|
|
170
169
|
}
|
|
171
|
-
const
|
|
170
|
+
const { bufferToDataUrl } = await import("../llm/image-utils");
|
|
171
|
+
const result = await bufferToDataUrl(clipBuf);
|
|
172
172
|
dataUrl = result.dataUrl;
|
|
173
173
|
label = "clipboard";
|
|
174
|
-
warning = result.warning;
|
|
175
174
|
}
|
|
176
175
|
else if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
177
176
|
const result = await loadUrlAsDataUrl(source);
|
|
@@ -184,10 +183,9 @@ export class Repl {
|
|
|
184
183
|
console.log(pc.red(t("image.not_found", { path: source })));
|
|
185
184
|
return;
|
|
186
185
|
}
|
|
187
|
-
const result = loadFileAsDataUrl(absPath);
|
|
186
|
+
const result = await loadFileAsDataUrl(absPath);
|
|
188
187
|
dataUrl = result.dataUrl;
|
|
189
188
|
label = source;
|
|
190
|
-
warning = result.warning;
|
|
191
189
|
}
|
|
192
190
|
// Store the image data on the agent's context manager for the next message
|
|
193
191
|
const contextManager = this.agent.contextManager;
|
|
@@ -201,8 +199,6 @@ export class Repl {
|
|
|
201
199
|
});
|
|
202
200
|
const sizeKb = Math.round((dataUrl.length * 3) / 4 / 1024);
|
|
203
201
|
console.log(pc.green(t("image.attached", { source: label, size: `${sizeKb} KB` })));
|
|
204
|
-
if (warning)
|
|
205
|
-
console.log(pc.yellow(` ${warning}`));
|
|
206
202
|
}
|
|
207
203
|
catch (err) {
|
|
208
204
|
console.log(pc.red(t("image.error", { message: err.message })));
|
|
@@ -760,15 +756,13 @@ export class Repl {
|
|
|
760
756
|
// Ctrl+V: try to paste image from clipboard
|
|
761
757
|
if (key.ctrl && key.name === "v" && !this.agentRunning) {
|
|
762
758
|
try {
|
|
763
|
-
const { readClipboardImage,
|
|
764
|
-
const
|
|
765
|
-
if (
|
|
766
|
-
const { dataUrl
|
|
759
|
+
const { readClipboardImage, bufferToDataUrl } = await import("../llm/image-utils");
|
|
760
|
+
const clipBuf = await readClipboardImage();
|
|
761
|
+
if (clipBuf) {
|
|
762
|
+
const { dataUrl } = await bufferToDataUrl(clipBuf);
|
|
767
763
|
this.pendingClipboardImage = dataUrl;
|
|
768
764
|
const sizeKb = Math.round((dataUrl.length * 3) / 4 / 1024);
|
|
769
765
|
console.log(pc.green(`\n${t("image.attached", { source: "clipboard", size: `${sizeKb} KB` })}`));
|
|
770
|
-
if (warning)
|
|
771
|
-
console.log(pc.yellow(` ${warning}`));
|
|
772
766
|
this.rl.prompt();
|
|
773
767
|
}
|
|
774
768
|
}
|
package/dist/llm/image-utils.js
CHANGED
|
@@ -9,97 +9,136 @@ const MIME_MAP = {
|
|
|
9
9
|
".bmp": "image/bmp",
|
|
10
10
|
".svg": "image/svg+xml",
|
|
11
11
|
};
|
|
12
|
-
/**
|
|
13
|
-
* Detect MIME type from file extension.
|
|
14
|
-
*/
|
|
12
|
+
/** Detect MIME type from file extension. */
|
|
15
13
|
export function detectMime(filePath) {
|
|
16
14
|
const ext = extname(filePath).toLowerCase();
|
|
17
15
|
return MIME_MAP[ext] ?? "image/png";
|
|
18
16
|
}
|
|
19
17
|
/**
|
|
20
|
-
*
|
|
21
|
-
*
|
|
18
|
+
* Read image from system clipboard using Bun.Image (macOS/Windows).
|
|
19
|
+
* Falls back to platform-specific commands on Linux.
|
|
20
|
+
* Returns null if clipboard has no image.
|
|
22
21
|
*/
|
|
23
|
-
export function
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
export async function readClipboardImage() {
|
|
23
|
+
// Bun.Image.fromClipboard() works on macOS and Windows
|
|
24
|
+
if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
|
|
25
|
+
try {
|
|
26
|
+
const img = Bun.Image.fromClipboard();
|
|
27
|
+
if (img) {
|
|
28
|
+
const buf = await img
|
|
29
|
+
.resize(800, 800, { fit: "inside" })
|
|
30
|
+
.jpeg({ quality: 60 })
|
|
31
|
+
.buffer();
|
|
32
|
+
return Buffer.from(buf);
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
catch {
|
|
36
|
+
// Bun.Image.fromClipboard() failed — fall through to platform fallback
|
|
37
|
+
}
|
|
33
38
|
}
|
|
39
|
+
// Linux fallback: xclip/wl-paste → temp file → read
|
|
40
|
+
return readClipboardFallback();
|
|
41
|
+
}
|
|
42
|
+
async function readClipboardFallback() {
|
|
43
|
+
const { platform } = await import("os");
|
|
44
|
+
const { execSync } = await import("child_process");
|
|
45
|
+
const { readFileSync, unlinkSync } = await import("fs");
|
|
46
|
+
const { join } = await import("path");
|
|
47
|
+
const tmpPath = join(process.env.TEMP || process.env.TMP || "/tmp", `mma-clip-${Date.now()}.png`);
|
|
48
|
+
try {
|
|
49
|
+
if (platform() === "linux") {
|
|
50
|
+
execSync(`xclip -selection clipboard -t image/png -o > "${tmpPath}" 2>/dev/null`, { timeout: 5000 });
|
|
51
|
+
}
|
|
52
|
+
else {
|
|
53
|
+
return null; // macOS/Windows should use Bun.Image
|
|
54
|
+
}
|
|
55
|
+
const buf = readFileSync(tmpPath);
|
|
56
|
+
unlinkSync(tmpPath);
|
|
57
|
+
return buf.length > 0 ? buf : null;
|
|
58
|
+
}
|
|
59
|
+
catch {
|
|
60
|
+
try {
|
|
61
|
+
unlinkSync(tmpPath);
|
|
62
|
+
}
|
|
63
|
+
catch { }
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Load an image from a file path, resize via Bun.Image, return as JPEG data URL.
|
|
69
|
+
* Target: ~800px wide, JPEG quality 60 — typically 5-15KB (~2-4K tokens).
|
|
70
|
+
*/
|
|
71
|
+
export async function loadFileAsDataUrl(filePath) {
|
|
72
|
+
const buf = readFileSync(filePath);
|
|
73
|
+
// Use Bun.Image if available — resize + convert to JPEG
|
|
74
|
+
if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
|
|
75
|
+
try {
|
|
76
|
+
const img = new Bun.Image(buf);
|
|
77
|
+
const { width } = await img.metadata();
|
|
78
|
+
const targetWidth = Math.min(800, width || 800);
|
|
79
|
+
const dataUrl = await img
|
|
80
|
+
.resize(targetWidth, 800, { fit: "inside" })
|
|
81
|
+
.jpeg({ quality: 60 })
|
|
82
|
+
.dataurl();
|
|
83
|
+
return { dataUrl };
|
|
84
|
+
}
|
|
85
|
+
catch {
|
|
86
|
+
// Bun.Image failed — fall through to raw base64
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
// Fallback: raw base64 (no resize)
|
|
34
90
|
const b64 = buf.toString("base64");
|
|
35
|
-
|
|
91
|
+
const ext = filePath.split(".").pop()?.toLowerCase();
|
|
92
|
+
const mime = ext === "jpg" || ext === "jpeg" ? "image/jpeg" : ext === "webp" ? "image/webp" : "image/png";
|
|
93
|
+
return { dataUrl: `data:${mime};base64,${b64}` };
|
|
36
94
|
}
|
|
37
95
|
/**
|
|
38
|
-
* Load an image from a URL, return as
|
|
96
|
+
* Load an image from a URL, resize, return as JPEG data URL.
|
|
39
97
|
*/
|
|
40
98
|
export async function loadUrlAsDataUrl(url) {
|
|
41
99
|
const resp = await fetch(url);
|
|
42
100
|
if (!resp.ok) {
|
|
43
101
|
throw new Error(`Failed to fetch image: ${resp.status} ${resp.statusText}`);
|
|
44
102
|
}
|
|
45
|
-
const contentType = resp.headers.get("content-type") ?? "image/png";
|
|
46
|
-
const mime = contentType.split(";")[0].trim();
|
|
47
103
|
const buf = Buffer.from(await resp.arrayBuffer());
|
|
104
|
+
if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
|
|
105
|
+
try {
|
|
106
|
+
const img = new Bun.Image(buf);
|
|
107
|
+
const { width } = await img.metadata();
|
|
108
|
+
const targetWidth = Math.min(800, width || 800);
|
|
109
|
+
const dataUrl = await img
|
|
110
|
+
.resize(targetWidth, 800, { fit: "inside" })
|
|
111
|
+
.jpeg({ quality: 60 })
|
|
112
|
+
.dataurl();
|
|
113
|
+
return { dataUrl };
|
|
114
|
+
}
|
|
115
|
+
catch {
|
|
116
|
+
// fall through
|
|
117
|
+
}
|
|
118
|
+
}
|
|
48
119
|
const b64 = buf.toString("base64");
|
|
49
|
-
return { dataUrl: `data
|
|
120
|
+
return { dataUrl: `data:image/png;base64,${b64}` };
|
|
50
121
|
}
|
|
51
122
|
/**
|
|
52
|
-
*
|
|
53
|
-
*
|
|
123
|
+
* Process a clipboard buffer (already resized by readClipboardImage)
|
|
124
|
+
* into a data URL. Used by Ctrl+V handler.
|
|
54
125
|
*/
|
|
55
|
-
export async function
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
// Use single quotes in PowerShell to avoid backslash issues
|
|
70
|
-
const psScript = `Add-Type -AssemblyName System.Windows.Forms; $img = [System.Windows.Forms.Clipboard]::GetImage(); if ($img -ne $null) { $img.Save('${tmpPath}'); Write-Output 'OK' } else { Write-Output 'EMPTY' }`;
|
|
71
|
-
const out = execSync(`powershell -NoProfile -Command "${psScript}"`, {
|
|
72
|
-
timeout: 5000,
|
|
73
|
-
encoding: "utf-8",
|
|
74
|
-
});
|
|
75
|
-
if (out.includes("OK")) {
|
|
76
|
-
return tmpPath;
|
|
126
|
+
export async function bufferToDataUrl(buf) {
|
|
127
|
+
if (typeof Bun !== "undefined" && typeof Bun.Image !== "undefined") {
|
|
128
|
+
try {
|
|
129
|
+
const img = new Bun.Image(buf);
|
|
130
|
+
const { width } = await img.metadata();
|
|
131
|
+
const targetWidth = Math.min(800, width || 800);
|
|
132
|
+
const dataUrl = await img
|
|
133
|
+
.resize(targetWidth, 800, { fit: "inside" })
|
|
134
|
+
.jpeg({ quality: 60 })
|
|
135
|
+
.dataurl();
|
|
136
|
+
return { dataUrl };
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
// fall through
|
|
77
140
|
}
|
|
78
|
-
return null;
|
|
79
|
-
}
|
|
80
|
-
catch {
|
|
81
|
-
return null;
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
async function readClipboardMacos() {
|
|
85
|
-
try {
|
|
86
|
-
const { execSync } = await import("child_process");
|
|
87
|
-
const tmpPath = `/tmp/mma-clip-${Date.now()}.png`;
|
|
88
|
-
execSync(`pngpaste "${tmpPath}"`, { timeout: 5000 });
|
|
89
|
-
return tmpPath;
|
|
90
|
-
}
|
|
91
|
-
catch {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
async function readClipboardLinux() {
|
|
96
|
-
try {
|
|
97
|
-
const { execSync } = await import("child_process");
|
|
98
|
-
const tmpPath = `/tmp/mma-clip-${Date.now()}.png`;
|
|
99
|
-
execSync(`xclip -selection clipboard -t image/png -o > "${tmpPath}" 2>/dev/null`, { timeout: 5000 });
|
|
100
|
-
return tmpPath;
|
|
101
|
-
}
|
|
102
|
-
catch {
|
|
103
|
-
return null;
|
|
104
141
|
}
|
|
142
|
+
const b64 = buf.toString("base64");
|
|
143
|
+
return { dataUrl: `data:image/png;base64,${b64}` };
|
|
105
144
|
}
|
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
import { existsSync } from "fs";
|
|
2
2
|
import { resolve } from "path";
|
|
3
3
|
import { t } from "../i18n/index";
|
|
4
|
-
import { loadFileAsDataUrl, loadUrlAsDataUrl, readClipboardImage } from "../llm/image-utils";
|
|
4
|
+
import { loadFileAsDataUrl, loadUrlAsDataUrl, readClipboardImage, bufferToDataUrl } from "../llm/image-utils";
|
|
5
5
|
import { logSecurityBlock } from "../modules/security/audit-log";
|
|
6
|
-
const MAX_IMAGE_BYTES = 10 * 1024 * 1024; // 10 MB
|
|
7
6
|
export const attachImageTool = {
|
|
8
7
|
name: "attach_image",
|
|
9
8
|
description: "Attach an image to the conversation from a file path, URL, or clipboard. The image will be included in the next message sent to the model. Supports PNG, JPEG, GIF, WebP.",
|
|
@@ -26,14 +25,14 @@ export const attachImageTool = {
|
|
|
26
25
|
try {
|
|
27
26
|
let dataUrl;
|
|
28
27
|
if (source.toLowerCase() === "clipboard") {
|
|
29
|
-
const
|
|
30
|
-
if (!
|
|
28
|
+
const clipBuf = await readClipboardImage();
|
|
29
|
+
if (!clipBuf) {
|
|
31
30
|
return {
|
|
32
31
|
success: false,
|
|
33
32
|
output: t("image.clipboard_empty"),
|
|
34
33
|
};
|
|
35
34
|
}
|
|
36
|
-
const result =
|
|
35
|
+
const result = await bufferToDataUrl(clipBuf);
|
|
37
36
|
dataUrl = result.dataUrl;
|
|
38
37
|
}
|
|
39
38
|
else if (source.startsWith("http://") || source.startsWith("https://")) {
|
|
@@ -60,7 +59,7 @@ export const attachImageTool = {
|
|
|
60
59
|
};
|
|
61
60
|
}
|
|
62
61
|
}
|
|
63
|
-
const result = loadFileAsDataUrl(absPath
|
|
62
|
+
const result = await loadFileAsDataUrl(absPath);
|
|
64
63
|
dataUrl = result.dataUrl;
|
|
65
64
|
}
|
|
66
65
|
// Add the image as a pending content part on the context manager
|
package/package.json
CHANGED
|
@@ -1,43 +1,44 @@
|
|
|
1
|
-
{
|
|
2
|
-
"name": "micro-models-agent",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"description": "Micro Models Agent (MMA) — LLM agent harness for small models (Qwen3.5-9B, 32K-64K context)",
|
|
5
|
-
"type": "module",
|
|
6
|
-
"bin": {
|
|
7
|
-
"mma": "bin/mma.mjs"
|
|
8
|
-
},
|
|
9
|
-
"files": [
|
|
10
|
-
"dist/",
|
|
11
|
-
"bin/"
|
|
12
|
-
],
|
|
13
|
-
"engines": {
|
|
14
|
-
"node": ">=20"
|
|
15
|
-
},
|
|
16
|
-
"scripts": {
|
|
17
|
-
"mma": "bun run src/cli/main.ts",
|
|
18
|
-
"build": "bun run build:tsc && bun run build:copy-assets",
|
|
19
|
-
"build:tsc": "tsc -p tsconfig.build.json",
|
|
20
|
-
"build:copy-assets": "powershell -Command \"Copy-Item -Recurse -Force 'src/modules/skills/builtin' 'dist/skills/builtin'\"",
|
|
21
|
-
"build:prod": "bun run 'build:bundle' && bun run 'build:copy-assets'",
|
|
22
|
-
"build:clean": "cmd /c \"if exist dist rmdir /s /q dist\"",
|
|
23
|
-
"build:bundle": "bun build ./src/cli/main.ts --outfile ./dist/main.js --target node --format esm --external playwright",
|
|
24
|
-
"dev": "bun --watch src/cli/main.ts",
|
|
25
|
-
"typecheck": "tsc --noEmit",
|
|
26
|
-
"test": "vitest run",
|
|
27
|
-
"test:watch": "vitest",
|
|
28
|
-
"test:integration": "vitest run --config vitest.integration.config.ts"
|
|
29
|
-
},
|
|
30
|
-
"dependencies": {
|
|
31
|
-
"commander": "^12.0.0",
|
|
32
|
-
"js-tiktoken": "^1.0.0",
|
|
33
|
-
"jsonrepair": "^3.15.0",
|
|
34
|
-
"picocolors": "^1.1.1",
|
|
35
|
-
"playwright": "^1.62.0",
|
|
36
|
-
"string-width": "^8.2.2"
|
|
37
|
-
},
|
|
38
|
-
"devDependencies": {
|
|
39
|
-
"@types/
|
|
40
|
-
"
|
|
41
|
-
"
|
|
42
|
-
|
|
43
|
-
}
|
|
1
|
+
{
|
|
2
|
+
"name": "micro-models-agent",
|
|
3
|
+
"version": "0.17.0",
|
|
4
|
+
"description": "Micro Models Agent (MMA) — LLM agent harness for small models (Qwen3.5-9B, 32K-64K context)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"mma": "bin/mma.mjs"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"dist/",
|
|
11
|
+
"bin/"
|
|
12
|
+
],
|
|
13
|
+
"engines": {
|
|
14
|
+
"node": ">=20"
|
|
15
|
+
},
|
|
16
|
+
"scripts": {
|
|
17
|
+
"mma": "bun run src/cli/main.ts",
|
|
18
|
+
"build": "bun run build:tsc && bun run build:copy-assets",
|
|
19
|
+
"build:tsc": "tsc -p tsconfig.build.json",
|
|
20
|
+
"build:copy-assets": "powershell -Command \"Copy-Item -Recurse -Force 'src/modules/skills/builtin' 'dist/skills/builtin'\"",
|
|
21
|
+
"build:prod": "bun run 'build:bundle' && bun run 'build:copy-assets'",
|
|
22
|
+
"build:clean": "cmd /c \"if exist dist rmdir /s /q dist\"",
|
|
23
|
+
"build:bundle": "bun build ./src/cli/main.ts --outfile ./dist/main.js --target node --format esm --external playwright",
|
|
24
|
+
"dev": "bun --watch src/cli/main.ts",
|
|
25
|
+
"typecheck": "tsc --noEmit",
|
|
26
|
+
"test": "vitest run",
|
|
27
|
+
"test:watch": "vitest",
|
|
28
|
+
"test:integration": "vitest run --config vitest.integration.config.ts"
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"commander": "^12.0.0",
|
|
32
|
+
"js-tiktoken": "^1.0.0",
|
|
33
|
+
"jsonrepair": "^3.15.0",
|
|
34
|
+
"picocolors": "^1.1.1",
|
|
35
|
+
"playwright": "^1.62.0",
|
|
36
|
+
"string-width": "^8.2.2"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@types/bun": "^1.3.14",
|
|
40
|
+
"@types/node": "^22.0.0",
|
|
41
|
+
"typescript": "^5.8.0",
|
|
42
|
+
"vitest": "^3.0.0"
|
|
43
|
+
}
|
|
44
|
+
}
|