zalo-agent-cli 1.4.2 → 1.5.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/package.json +1 -1
- package/src/mcp/image-downloader.js +125 -0
- package/src/mcp/mcp-config.js +5 -0
- package/src/mcp/mcp-tools.js +46 -1
package/package.json
CHANGED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Download Zalo images to local filesystem, organized by thread name.
|
|
3
|
+
* Folder structure: {downloadDir}/{threadName}/{date}_{time}_{sender}_{msgId}.{ext}
|
|
4
|
+
* Cross-platform: opens images with system viewer (open/xdg-open/start).
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { mkdirSync, writeFileSync } from "fs";
|
|
8
|
+
import { join } from "path";
|
|
9
|
+
import { platform } from "os";
|
|
10
|
+
import { exec } from "child_process";
|
|
11
|
+
import { CONFIG_DIR } from "../core/credentials.js";
|
|
12
|
+
|
|
13
|
+
/** Default download directory when not configured */
|
|
14
|
+
const DEFAULT_DIR = join(CONFIG_DIR, "images");
|
|
15
|
+
|
|
16
|
+
/** Characters unsafe for filesystem paths — stripped from folder/file names */
|
|
17
|
+
const UNSAFE_CHARS = /[/\\:*?"<>|]/g;
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Sanitize a string for use as a filesystem name.
|
|
21
|
+
* @param {string} name
|
|
22
|
+
* @returns {string}
|
|
23
|
+
*/
|
|
24
|
+
function sanitize(name) {
|
|
25
|
+
return (name || "unknown").replace(UNSAFE_CHARS, "_").trim() || "unknown";
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Guess file extension from URL or content-type header.
|
|
30
|
+
* @param {string} url
|
|
31
|
+
* @param {string} [contentType]
|
|
32
|
+
* @returns {string}
|
|
33
|
+
*/
|
|
34
|
+
function guessExtension(url, contentType) {
|
|
35
|
+
// Try content-type first
|
|
36
|
+
if (contentType) {
|
|
37
|
+
const match = contentType.match(/image\/(jpeg|jpg|png|gif|webp|bmp)/i);
|
|
38
|
+
if (match) return match[1] === "jpeg" ? "jpg" : match[1].toLowerCase();
|
|
39
|
+
}
|
|
40
|
+
// Try URL path
|
|
41
|
+
const urlMatch = url.match(/\.(jpeg|jpg|png|gif|webp|bmp)(\?|$)/i);
|
|
42
|
+
if (urlMatch) return urlMatch[1] === "jpeg" ? "jpg" : urlMatch[1].toLowerCase();
|
|
43
|
+
return "jpg"; // safe default for Zalo CDN
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Build the folder name for a thread.
|
|
48
|
+
* Groups: thread display name. DMs: "DM_{senderName}".
|
|
49
|
+
* @param {object} message - Normalized message
|
|
50
|
+
* @param {string} [threadName] - Resolved thread name from cache
|
|
51
|
+
* @returns {string}
|
|
52
|
+
*/
|
|
53
|
+
function buildFolderName(message, threadName) {
|
|
54
|
+
if (message.threadType === "group") {
|
|
55
|
+
return sanitize(threadName || message.threadId);
|
|
56
|
+
}
|
|
57
|
+
return sanitize(`DM_${threadName || message.senderName || message.senderId}`);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Build a descriptive filename from message metadata.
|
|
62
|
+
* Format: {YYYY-MM-DD}_{HH-mm}_{sender}_{msgId}.{ext}
|
|
63
|
+
* @param {object} message
|
|
64
|
+
* @param {string} ext - File extension
|
|
65
|
+
* @returns {string}
|
|
66
|
+
*/
|
|
67
|
+
function buildFileName(message, ext) {
|
|
68
|
+
const d = new Date(message.timestamp);
|
|
69
|
+
const date = d.toISOString().slice(0, 10); // YYYY-MM-DD
|
|
70
|
+
const time = `${String(d.getHours()).padStart(2, "0")}-${String(d.getMinutes()).padStart(2, "0")}`;
|
|
71
|
+
const sender = sanitize(message.senderName || message.senderId);
|
|
72
|
+
const msgId = (message.id || "noId").slice(-8); // last 8 chars for brevity
|
|
73
|
+
return `${date}_${time}_${sender}_${msgId}.${ext}`;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Open a file with the system's default viewer (cross-platform).
|
|
78
|
+
* @param {string} filePath
|
|
79
|
+
*/
|
|
80
|
+
function openWithSystemViewer(filePath) {
|
|
81
|
+
const cmds = { darwin: "open", win32: "start", linux: "xdg-open" };
|
|
82
|
+
const cmd = cmds[platform()] || "xdg-open";
|
|
83
|
+
// Use double quotes for paths with spaces; detach so CLI doesn't block
|
|
84
|
+
exec(`${cmd} "${filePath}"`, (err) => {
|
|
85
|
+
if (err) console.error(`[image-dl] Failed to open viewer: ${err.message}`);
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Download an image from URL and save to organized local folder.
|
|
91
|
+
* @param {object} message - Normalized message with attachment.url
|
|
92
|
+
* @param {object} [options]
|
|
93
|
+
* @param {string} [options.downloadDir] - Base download directory
|
|
94
|
+
* @param {boolean} [options.autoOpen] - Open image after download
|
|
95
|
+
* @param {string} [options.threadName] - Thread display name from cache
|
|
96
|
+
* @returns {Promise<{ success: boolean, path: string, folder: string, fileName: string }>}
|
|
97
|
+
*/
|
|
98
|
+
export async function downloadImage(message, options = {}) {
|
|
99
|
+
const url = message.attachment?.url;
|
|
100
|
+
if (!url) throw new Error("Message has no attachment URL");
|
|
101
|
+
|
|
102
|
+
const baseDir = options.downloadDir || DEFAULT_DIR;
|
|
103
|
+
const folder = buildFolderName(message, options.threadName);
|
|
104
|
+
const folderPath = join(baseDir, folder);
|
|
105
|
+
mkdirSync(folderPath, { recursive: true });
|
|
106
|
+
|
|
107
|
+
// Fetch image from Zalo CDN
|
|
108
|
+
const response = await fetch(url);
|
|
109
|
+
if (!response.ok) throw new Error(`Download failed: HTTP ${response.status}`);
|
|
110
|
+
|
|
111
|
+
const contentType = response.headers.get("content-type") || "";
|
|
112
|
+
const ext = guessExtension(url, contentType);
|
|
113
|
+
const fileName = buildFileName(message, ext);
|
|
114
|
+
const filePath = join(folderPath, fileName);
|
|
115
|
+
|
|
116
|
+
const buffer = Buffer.from(await response.arrayBuffer());
|
|
117
|
+
writeFileSync(filePath, buffer);
|
|
118
|
+
|
|
119
|
+
// Auto-open with system viewer if configured
|
|
120
|
+
if (options.autoOpen) {
|
|
121
|
+
openWithSystemViewer(filePath);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
return { success: true, path: filePath, folder, fileName };
|
|
125
|
+
}
|
package/src/mcp/mcp-config.js
CHANGED
|
@@ -26,6 +26,10 @@ export function getDefaultConfig() {
|
|
|
26
26
|
bufferMaxAge: "2h",
|
|
27
27
|
bufferMaxSize: 500,
|
|
28
28
|
},
|
|
29
|
+
images: {
|
|
30
|
+
downloadDir: null, // default: ~/.zalo-agent-cli/images/
|
|
31
|
+
autoOpen: true,
|
|
32
|
+
},
|
|
29
33
|
};
|
|
30
34
|
}
|
|
31
35
|
|
|
@@ -44,6 +48,7 @@ export function loadMCPConfig() {
|
|
|
44
48
|
...saved,
|
|
45
49
|
notify: { ...defaults.notify, ...saved.notify },
|
|
46
50
|
limits: { ...defaults.limits, ...saved.limits },
|
|
51
|
+
images: { ...defaults.images, ...saved.images },
|
|
47
52
|
};
|
|
48
53
|
} catch {
|
|
49
54
|
// File doesn't exist or invalid JSON — use defaults
|
package/src/mcp/mcp-tools.js
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* MCP tool registrations for Zalo message access and sending.
|
|
3
|
-
* Registers
|
|
3
|
+
* Registers 6 tools: zalo_get_messages, zalo_send_message, zalo_list_threads, zalo_search_threads, zalo_mark_read, zalo_view_image.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { z } from "zod";
|
|
7
|
+
import { downloadImage } from "./image-downloader.js";
|
|
7
8
|
|
|
8
9
|
/** Thread type constants matching zca-js ThreadType enum */
|
|
9
10
|
const THREAD_USER = 0;
|
|
@@ -193,4 +194,48 @@ export function registerTools(server, api, buffer, filter, config, nameCache) {
|
|
|
193
194
|
}
|
|
194
195
|
},
|
|
195
196
|
);
|
|
197
|
+
|
|
198
|
+
// --- zalo_view_image ---
|
|
199
|
+
const imgConfig = config.images || {};
|
|
200
|
+
server.registerTool(
|
|
201
|
+
"zalo_view_image",
|
|
202
|
+
{
|
|
203
|
+
title: "View Zalo Image",
|
|
204
|
+
description:
|
|
205
|
+
"Download a Zalo image to local filesystem and optionally open it with system viewer. " +
|
|
206
|
+
"Images are organized by thread folder and named with date/sender metadata. " +
|
|
207
|
+
"Pass the message ID from zalo_get_messages to download its attached image.",
|
|
208
|
+
inputSchema: z.object({
|
|
209
|
+
messageId: z.string().describe("Message ID from zalo_get_messages that has an image attachment"),
|
|
210
|
+
threadId: z.string().optional().describe("Thread ID to search in. Omit to search all threads."),
|
|
211
|
+
autoOpen: z
|
|
212
|
+
.boolean()
|
|
213
|
+
.default(imgConfig.autoOpen ?? true)
|
|
214
|
+
.describe("Open image with system viewer after download"),
|
|
215
|
+
}),
|
|
216
|
+
},
|
|
217
|
+
async ({ messageId, threadId, autoOpen }) => {
|
|
218
|
+
try {
|
|
219
|
+
// Find the message in the buffer by ID
|
|
220
|
+
const allMessages = buffer.read(threadId, 0, 9999).messages;
|
|
221
|
+
const message = allMessages.find((m) => m.id === messageId);
|
|
222
|
+
if (!message) return err(`Message ${messageId} not found in buffer`);
|
|
223
|
+
if (!message.attachment?.url) return err(`Message ${messageId} has no image attachment`);
|
|
224
|
+
|
|
225
|
+
// Resolve thread name for folder organization
|
|
226
|
+
const threadName = nameCache?.get(message.threadId)?.name || null;
|
|
227
|
+
|
|
228
|
+
const result = await downloadImage(message, {
|
|
229
|
+
downloadDir: imgConfig.downloadDir || undefined,
|
|
230
|
+
autoOpen,
|
|
231
|
+
threadName,
|
|
232
|
+
});
|
|
233
|
+
|
|
234
|
+
return ok(result);
|
|
235
|
+
} catch (e) {
|
|
236
|
+
console.error("[mcp-tools] zalo_view_image error:", e.message);
|
|
237
|
+
return err(e.message);
|
|
238
|
+
}
|
|
239
|
+
},
|
|
240
|
+
);
|
|
196
241
|
}
|