micro-models-agent 0.16.7 → 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 CHANGED
@@ -162,12 +162,13 @@ export class Repl {
162
162
  let dataUrl;
163
163
  let label;
164
164
  if (source.toLowerCase() === "clipboard") {
165
- const clipPath = await readClipboardImage();
166
- if (!clipPath) {
165
+ const clipBuf = await readClipboardImage();
166
+ if (!clipBuf) {
167
167
  console.log(pc.yellow(t("image.clipboard_empty")));
168
168
  return;
169
169
  }
170
- const result = loadFileAsDataUrl(clipPath);
170
+ const { bufferToDataUrl } = await import("../llm/image-utils");
171
+ const result = await bufferToDataUrl(clipBuf);
171
172
  dataUrl = result.dataUrl;
172
173
  label = "clipboard";
173
174
  }
@@ -182,7 +183,7 @@ export class Repl {
182
183
  console.log(pc.red(t("image.not_found", { path: source })));
183
184
  return;
184
185
  }
185
- const result = loadFileAsDataUrl(absPath);
186
+ const result = await loadFileAsDataUrl(absPath);
186
187
  dataUrl = result.dataUrl;
187
188
  label = source;
188
189
  }
@@ -755,10 +756,10 @@ export class Repl {
755
756
  // Ctrl+V: try to paste image from clipboard
756
757
  if (key.ctrl && key.name === "v" && !this.agentRunning) {
757
758
  try {
758
- const { readClipboardImage, loadFileAsDataUrl } = await import("../llm/image-utils");
759
- const clipPath = await readClipboardImage();
760
- if (clipPath) {
761
- const { dataUrl } = loadFileAsDataUrl(clipPath);
759
+ const { readClipboardImage, bufferToDataUrl } = await import("../llm/image-utils");
760
+ const clipBuf = await readClipboardImage();
761
+ if (clipBuf) {
762
+ const { dataUrl } = await bufferToDataUrl(clipBuf);
762
763
  this.pendingClipboardImage = dataUrl;
763
764
  const sizeKb = Math.round((dataUrl.length * 3) / 4 / 1024);
764
765
  console.log(pc.green(`\n${t("image.attached", { source: "clipboard", size: `${sizeKb} KB` })}`));
@@ -9,105 +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
- * Load an image from a file path and return as base64 data URL.
21
- * Optionally resize if the raw data exceeds maxBytes.
22
- */
23
- export function loadFileAsDataUrl(filePath, maxBytes) {
24
- const mime = detectMime(filePath);
25
- let buf = readFileSync(filePath);
26
- let resized = false;
27
- if (maxBytes && buf.length > maxBytes) {
28
- buf = resizePngBuffer(buf, maxBytes);
29
- resized = true;
30
- }
31
- const b64 = buf.toString("base64");
32
- return { dataUrl: `data:${mime};base64,${b64}`, mime, resized };
33
- }
34
- /**
35
- * Load an image from a URL, return as base64 data URL.
36
- */
37
- export async function loadUrlAsDataUrl(url) {
38
- const resp = await fetch(url);
39
- if (!resp.ok) {
40
- throw new Error(`Failed to fetch image: ${resp.status} ${resp.statusText}`);
41
- }
42
- const contentType = resp.headers.get("content-type") ?? "image/png";
43
- const mime = contentType.split(";")[0].trim();
44
- const buf = Buffer.from(await resp.arrayBuffer());
45
- const b64 = buf.toString("base64");
46
- return { dataUrl: `data:${mime};base64,${b64}`, mime };
47
- }
48
- /**
49
- * Read image from system clipboard (platform-specific).
18
+ * Read image from system clipboard using Bun.Image (macOS/Windows).
19
+ * Falls back to platform-specific commands on Linux.
50
20
  * Returns null if clipboard has no image.
51
21
  */
52
22
  export async function readClipboardImage() {
53
- const { platform } = await import("os");
54
- if (platform() === "win32") {
55
- return readClipboardWindows();
56
- }
57
- if (platform() === "darwin") {
58
- return readClipboardMacos();
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
+ }
59
38
  }
60
- return readClipboardLinux();
39
+ // Linux fallback: xclip/wl-paste → temp file → read
40
+ return readClipboardFallback();
61
41
  }
62
- async function readClipboardWindows() {
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`);
63
48
  try {
64
- const { execSync } = await import("child_process");
65
- const tmpPath = `${process.env.TEMP || process.env.TMP || "/tmp"}\\mma-clip-${Date.now()}.png`;
66
- // Use single quotes in PowerShell to avoid backslash issues
67
- 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' }`;
68
- const out = execSync(`powershell -NoProfile -Command "${psScript}"`, {
69
- timeout: 5000,
70
- encoding: "utf-8",
71
- });
72
- if (out.includes("OK")) {
73
- return tmpPath;
49
+ if (platform() === "linux") {
50
+ execSync(`xclip -selection clipboard -t image/png -o > "${tmpPath}" 2>/dev/null`, { timeout: 5000 });
74
51
  }
75
- return null;
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;
76
58
  }
77
59
  catch {
60
+ try {
61
+ unlinkSync(tmpPath);
62
+ }
63
+ catch { }
78
64
  return null;
79
65
  }
80
66
  }
81
- async function readClipboardMacos() {
82
- try {
83
- const { execSync } = await import("child_process");
84
- const tmpPath = `/tmp/mma-clip-${Date.now()}.png`;
85
- execSync(`pngpaste "${tmpPath}"`, { timeout: 5000 });
86
- return tmpPath;
87
- }
88
- catch {
89
- return null;
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
+ }
90
88
  }
89
+ // Fallback: raw base64 (no resize)
90
+ const b64 = buf.toString("base64");
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}` };
91
94
  }
92
- async function readClipboardLinux() {
93
- try {
94
- const { execSync } = await import("child_process");
95
- const tmpPath = `/tmp/mma-clip-${Date.now()}.png`;
96
- execSync(`xclip -selection clipboard -t image/png -o > "${tmpPath}" 2>/dev/null`, { timeout: 5000 });
97
- return tmpPath;
95
+ /**
96
+ * Load an image from a URL, resize, return as JPEG data URL.
97
+ */
98
+ export async function loadUrlAsDataUrl(url) {
99
+ const resp = await fetch(url);
100
+ if (!resp.ok) {
101
+ throw new Error(`Failed to fetch image: ${resp.status} ${resp.statusText}`);
98
102
  }
99
- catch {
100
- return null;
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
+ }
101
118
  }
119
+ const b64 = buf.toString("base64");
120
+ return { dataUrl: `data:image/png;base64,${b64}` };
102
121
  }
103
122
  /**
104
- * Rough PNG resize by reducing dimensions.
105
- * This is a naive approach for production, use sharp or jimp.
106
- * For now, we just warn if image is too large and return as-is.
123
+ * Process a clipboard buffer (already resized by readClipboardImage)
124
+ * into a data URL. Used by Ctrl+V handler.
107
125
  */
108
- function resizePngBuffer(buf, _maxBytes) {
109
- // Without a native image library, we can't reliably resize.
110
- // Return as-is and let the model handle it (or fail gracefully).
111
- // TODO: integrate sharp for proper resize
112
- return buf;
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
140
+ }
141
+ }
142
+ const b64 = buf.toString("base64");
143
+ return { dataUrl: `data:image/png;base64,${b64}` };
113
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 clipPath = await readClipboardImage();
30
- if (!clipPath) {
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 = loadFileAsDataUrl(clipPath, MAX_IMAGE_BYTES);
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, MAX_IMAGE_BYTES);
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.16.7",
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/node": "^22.0.0",
40
- "typescript": "^5.8.0",
41
- "vitest": "^3.0.0"
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
+ }