@ryantest/openclaw-qqbot 0.0.1
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 +22 -0
- package/README.md +483 -0
- package/README.zh.md +478 -0
- package/bin/qqbot-cli.js +243 -0
- package/clawdbot.plugin.json +16 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.js +26 -0
- package/dist/src/admin-resolver.d.ts +27 -0
- package/dist/src/admin-resolver.js +122 -0
- package/dist/src/api.d.ts +156 -0
- package/dist/src/api.js +599 -0
- package/dist/src/channel.d.ts +11 -0
- package/dist/src/channel.js +354 -0
- package/dist/src/config.d.ts +25 -0
- package/dist/src/config.js +161 -0
- package/dist/src/credential-backup.d.ts +31 -0
- package/dist/src/credential-backup.js +66 -0
- package/dist/src/gateway.d.ts +18 -0
- package/dist/src/gateway.js +1265 -0
- package/dist/src/image-server.d.ts +68 -0
- package/dist/src/image-server.js +462 -0
- package/dist/src/inbound-attachments.d.ts +58 -0
- package/dist/src/inbound-attachments.js +234 -0
- package/dist/src/known-users.d.ts +100 -0
- package/dist/src/known-users.js +263 -0
- package/dist/src/message-queue.d.ts +50 -0
- package/dist/src/message-queue.js +115 -0
- package/dist/src/onboarding.d.ts +10 -0
- package/dist/src/onboarding.js +203 -0
- package/dist/src/outbound-deliver.d.ts +48 -0
- package/dist/src/outbound-deliver.js +462 -0
- package/dist/src/outbound.d.ts +203 -0
- package/dist/src/outbound.js +1102 -0
- package/dist/src/proactive.d.ts +170 -0
- package/dist/src/proactive.js +399 -0
- package/dist/src/ref-index-store.d.ts +70 -0
- package/dist/src/ref-index-store.js +273 -0
- package/dist/src/reply-dispatcher.d.ts +35 -0
- package/dist/src/reply-dispatcher.js +311 -0
- package/dist/src/runtime.d.ts +3 -0
- package/dist/src/runtime.js +10 -0
- package/dist/src/session-store.d.ts +52 -0
- package/dist/src/session-store.js +254 -0
- package/dist/src/slash-commands.d.ts +71 -0
- package/dist/src/slash-commands.js +1179 -0
- package/dist/src/startup-greeting.d.ts +30 -0
- package/dist/src/startup-greeting.js +78 -0
- package/dist/src/stt.d.ts +21 -0
- package/dist/src/stt.js +70 -0
- package/dist/src/tools/channel.d.ts +16 -0
- package/dist/src/tools/channel.js +234 -0
- package/dist/src/tools/remind.d.ts +2 -0
- package/dist/src/tools/remind.js +247 -0
- package/dist/src/types.d.ts +175 -0
- package/dist/src/types.js +1 -0
- package/dist/src/typing-keepalive.d.ts +27 -0
- package/dist/src/typing-keepalive.js +64 -0
- package/dist/src/update-checker.d.ts +34 -0
- package/dist/src/update-checker.js +166 -0
- package/dist/src/user-messages.d.ts +8 -0
- package/dist/src/user-messages.js +8 -0
- package/dist/src/utils/audio-convert.d.ts +89 -0
- package/dist/src/utils/audio-convert.js +704 -0
- package/dist/src/utils/file-utils.d.ts +55 -0
- package/dist/src/utils/file-utils.js +150 -0
- package/dist/src/utils/image-size.d.ts +51 -0
- package/dist/src/utils/image-size.js +234 -0
- package/dist/src/utils/media-tags.d.ts +14 -0
- package/dist/src/utils/media-tags.js +164 -0
- package/dist/src/utils/payload.d.ts +112 -0
- package/dist/src/utils/payload.js +186 -0
- package/dist/src/utils/platform.d.ts +137 -0
- package/dist/src/utils/platform.js +390 -0
- package/dist/src/utils/text-parsing.d.ts +32 -0
- package/dist/src/utils/text-parsing.js +80 -0
- package/dist/src/utils/upload-cache.d.ts +34 -0
- package/dist/src/utils/upload-cache.js +93 -0
- package/index.ts +31 -0
- package/moltbot.plugin.json +16 -0
- package/node_modules/@eshaz/web-worker/LICENSE +201 -0
- package/node_modules/@eshaz/web-worker/README.md +134 -0
- package/node_modules/@eshaz/web-worker/browser.js +17 -0
- package/node_modules/@eshaz/web-worker/cjs/browser.js +16 -0
- package/node_modules/@eshaz/web-worker/cjs/node.js +219 -0
- package/node_modules/@eshaz/web-worker/index.d.ts +4 -0
- package/node_modules/@eshaz/web-worker/node.js +223 -0
- package/node_modules/@eshaz/web-worker/package.json +54 -0
- package/node_modules/@wasm-audio-decoders/common/index.js +5 -0
- package/node_modules/@wasm-audio-decoders/common/package.json +36 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderCommon.js +231 -0
- package/node_modules/@wasm-audio-decoders/common/src/WASMAudioDecoderWorker.js +129 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/README +67 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/build_puff.js +31 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.c +863 -0
- package/node_modules/@wasm-audio-decoders/common/src/puff/puff.h +35 -0
- package/node_modules/@wasm-audio-decoders/common/src/utilities.js +3 -0
- package/node_modules/@wasm-audio-decoders/common/types.d.ts +7 -0
- package/node_modules/mpg123-decoder/README.md +265 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js +185 -0
- package/node_modules/mpg123-decoder/dist/mpg123-decoder.min.js.map +1 -0
- package/node_modules/mpg123-decoder/index.js +8 -0
- package/node_modules/mpg123-decoder/package.json +58 -0
- package/node_modules/mpg123-decoder/src/EmscriptenWasm.js +464 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoder.js +200 -0
- package/node_modules/mpg123-decoder/src/MPEGDecoderWebWorker.js +21 -0
- package/node_modules/mpg123-decoder/types.d.ts +30 -0
- package/node_modules/silk-wasm/LICENSE +21 -0
- package/node_modules/silk-wasm/README.md +85 -0
- package/node_modules/silk-wasm/lib/index.cjs +16 -0
- package/node_modules/silk-wasm/lib/index.d.ts +70 -0
- package/node_modules/silk-wasm/lib/index.mjs +16 -0
- package/node_modules/silk-wasm/lib/silk.wasm +0 -0
- package/node_modules/silk-wasm/lib/utils.d.ts +4 -0
- package/node_modules/silk-wasm/package.json +39 -0
- package/node_modules/simple-yenc/.github/FUNDING.yml +1 -0
- package/node_modules/simple-yenc/.prettierignore +1 -0
- package/node_modules/simple-yenc/LICENSE +7 -0
- package/node_modules/simple-yenc/README.md +163 -0
- package/node_modules/simple-yenc/dist/esm.js +1 -0
- package/node_modules/simple-yenc/dist/index.js +1 -0
- package/node_modules/simple-yenc/package.json +50 -0
- package/node_modules/simple-yenc/rollup.config.js +27 -0
- package/node_modules/simple-yenc/src/simple-yenc.js +302 -0
- package/node_modules/ws/LICENSE +20 -0
- package/node_modules/ws/README.md +548 -0
- package/node_modules/ws/browser.js +8 -0
- package/node_modules/ws/index.js +13 -0
- package/node_modules/ws/lib/buffer-util.js +131 -0
- package/node_modules/ws/lib/constants.js +19 -0
- package/node_modules/ws/lib/event-target.js +292 -0
- package/node_modules/ws/lib/extension.js +203 -0
- package/node_modules/ws/lib/limiter.js +55 -0
- package/node_modules/ws/lib/permessage-deflate.js +528 -0
- package/node_modules/ws/lib/receiver.js +706 -0
- package/node_modules/ws/lib/sender.js +602 -0
- package/node_modules/ws/lib/stream.js +161 -0
- package/node_modules/ws/lib/subprotocol.js +62 -0
- package/node_modules/ws/lib/validation.js +152 -0
- package/node_modules/ws/lib/websocket-server.js +554 -0
- package/node_modules/ws/lib/websocket.js +1393 -0
- package/node_modules/ws/package.json +69 -0
- package/node_modules/ws/wrapper.mjs +8 -0
- package/openclaw.plugin.json +16 -0
- package/package.json +76 -0
- package/scripts/cleanup-legacy-plugins.sh +124 -0
- package/scripts/proactive-api-server.ts +369 -0
- package/scripts/send-proactive.ts +293 -0
- package/scripts/set-markdown.sh +156 -0
- package/scripts/test-sendmedia.ts +116 -0
- package/scripts/upgrade-via-alt-pkg.sh +307 -0
- package/scripts/upgrade-via-npm.ps1 +296 -0
- package/scripts/upgrade-via-npm.sh +301 -0
- package/scripts/upgrade-via-source.sh +774 -0
- package/skills/qqbot-channel/SKILL.md +263 -0
- package/skills/qqbot-channel/references/api_references.md +521 -0
- package/skills/qqbot-media/SKILL.md +56 -0
- package/skills/qqbot-remind/SKILL.md +149 -0
- package/src/admin-resolver.ts +140 -0
- package/src/api.ts +819 -0
- package/src/bot-logs-2026-03-21T11-21-47(2).txt +46 -0
- package/src/channel.ts +381 -0
- package/src/config.ts +187 -0
- package/src/credential-backup.ts +72 -0
- package/src/gateway.log +43 -0
- package/src/gateway.ts +1404 -0
- package/src/image-server.ts +539 -0
- package/src/inbound-attachments.ts +304 -0
- package/src/known-users.ts +353 -0
- package/src/message-queue.ts +169 -0
- package/src/onboarding.ts +274 -0
- package/src/openclaw-2026-03-21.log +3729 -0
- package/src/openclaw-plugin-sdk.d.ts +522 -0
- package/src/outbound-deliver.ts +552 -0
- package/src/outbound.ts +1266 -0
- package/src/proactive.ts +530 -0
- package/src/ref-index-store.ts +357 -0
- package/src/reply-dispatcher.ts +334 -0
- package/src/runtime.ts +14 -0
- package/src/session-store.ts +303 -0
- package/src/slash-commands.ts +1305 -0
- package/src/startup-greeting.ts +98 -0
- package/src/stt.ts +86 -0
- package/src/tools/channel.ts +281 -0
- package/src/tools/remind.ts +296 -0
- package/src/types.ts +183 -0
- package/src/typing-keepalive.ts +59 -0
- package/src/update-checker.ts +179 -0
- package/src/user-messages.ts +7 -0
- package/src/utils/audio-convert.ts +803 -0
- package/src/utils/file-utils.ts +167 -0
- package/src/utils/image-size.ts +266 -0
- package/src/utils/media-tags.ts +182 -0
- package/src/utils/payload.ts +265 -0
- package/src/utils/platform.ts +435 -0
- package/src/utils/text-parsing.ts +82 -0
- package/src/utils/upload-cache.ts +128 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地图床服务器
|
|
3
|
+
* 提供安全的图片存储和访问服务
|
|
4
|
+
*/
|
|
5
|
+
export interface ImageServerConfig {
|
|
6
|
+
/** 监听端口 */
|
|
7
|
+
port: number;
|
|
8
|
+
/** 图片存储目录 */
|
|
9
|
+
storageDir: string;
|
|
10
|
+
/** 外部访问的基础 URL(如 http://your-server:port),留空则自动生成 */
|
|
11
|
+
baseUrl?: string;
|
|
12
|
+
/** 图片过期时间(秒),0 表示不过期 */
|
|
13
|
+
ttlSeconds?: number;
|
|
14
|
+
/** 允许的图片格式 */
|
|
15
|
+
allowedFormats?: string[];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* 启动图床服务器
|
|
19
|
+
*/
|
|
20
|
+
export declare function startImageServer(config?: Partial<ImageServerConfig>): Promise<string>;
|
|
21
|
+
/**
|
|
22
|
+
* 停止图床服务器
|
|
23
|
+
*/
|
|
24
|
+
export declare function stopImageServer(): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* 保存图片并返回访问 URL
|
|
27
|
+
* @param imageData 图片数据(Buffer 或 base64 字符串)
|
|
28
|
+
* @param mimeType 图片 MIME 类型
|
|
29
|
+
* @param ttlSeconds 过期时间(秒),默认使用配置值
|
|
30
|
+
* @returns 图片访问 URL
|
|
31
|
+
*/
|
|
32
|
+
export declare function saveImage(imageData: Buffer | string, mimeType?: string, ttlSeconds?: number): string;
|
|
33
|
+
/**
|
|
34
|
+
* 从本地文件路径保存图片到图床
|
|
35
|
+
* @param filePath 本地文件路径
|
|
36
|
+
* @param ttlSeconds 过期时间(秒),默认使用配置值
|
|
37
|
+
* @returns 图片访问 URL,如果文件不存在或不是图片则返回 null
|
|
38
|
+
*/
|
|
39
|
+
export declare function saveImageFromPath(filePath: string, ttlSeconds?: number): string | null;
|
|
40
|
+
/**
|
|
41
|
+
* 检查图床服务器是否运行中
|
|
42
|
+
*/
|
|
43
|
+
export declare function isImageServerRunning(): boolean;
|
|
44
|
+
/**
|
|
45
|
+
* 确保图床服务器正在运行
|
|
46
|
+
* 如果未运行,则自动启动
|
|
47
|
+
* @param publicBaseUrl 公网访问的基础 URL(如 http://your-server:18765)
|
|
48
|
+
* @returns 基础 URL,启动失败返回 null
|
|
49
|
+
*/
|
|
50
|
+
export declare function ensureImageServer(publicBaseUrl?: string): Promise<string | null>;
|
|
51
|
+
/**
|
|
52
|
+
* 下载远程文件并保存到本地
|
|
53
|
+
* @param url 远程文件 URL
|
|
54
|
+
* @param destDir 目标目录
|
|
55
|
+
* @param originalFilename 原始文件名(可选,完整文件名包含扩展名)
|
|
56
|
+
* @param options 下载选项
|
|
57
|
+
* @returns 本地文件路径,失败返回 null
|
|
58
|
+
*/
|
|
59
|
+
export declare function downloadFile(url: string, destDir: string, originalFilename?: string, options?: {
|
|
60
|
+
/** 超时时间(毫秒),默认 120000(2分钟) */
|
|
61
|
+
timeoutMs?: number;
|
|
62
|
+
/** 最大文件大小(字节),默认 50MB */
|
|
63
|
+
maxSizeBytes?: number;
|
|
64
|
+
}): Promise<string | null>;
|
|
65
|
+
/**
|
|
66
|
+
* 获取图床服务器配置
|
|
67
|
+
*/
|
|
68
|
+
export declare function getImageServerConfig(): Required<ImageServerConfig>;
|
|
@@ -0,0 +1,462 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 本地图床服务器
|
|
3
|
+
* 提供安全的图片存储和访问服务
|
|
4
|
+
*/
|
|
5
|
+
import http from "node:http";
|
|
6
|
+
import fs from "node:fs";
|
|
7
|
+
import path from "node:path";
|
|
8
|
+
import crypto from "node:crypto";
|
|
9
|
+
const DEFAULT_CONFIG = {
|
|
10
|
+
port: 18765,
|
|
11
|
+
storageDir: "./qqbot-images",
|
|
12
|
+
baseUrl: "",
|
|
13
|
+
ttlSeconds: 3600, // 默认 1 小时过期
|
|
14
|
+
allowedFormats: ["png", "jpg", "jpeg", "gif", "webp"],
|
|
15
|
+
};
|
|
16
|
+
let serverInstance = null;
|
|
17
|
+
let currentConfig = { ...DEFAULT_CONFIG };
|
|
18
|
+
let imageIndex = new Map();
|
|
19
|
+
/**
|
|
20
|
+
* 生成安全的随机 ID
|
|
21
|
+
*/
|
|
22
|
+
function generateImageId() {
|
|
23
|
+
return crypto.randomBytes(16).toString("hex");
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* 验证请求路径是否安全(防止目录遍历攻击)
|
|
27
|
+
*/
|
|
28
|
+
function isPathSafe(requestPath, baseDir) {
|
|
29
|
+
const normalizedBase = path.resolve(baseDir);
|
|
30
|
+
const normalizedPath = path.resolve(baseDir, requestPath);
|
|
31
|
+
return normalizedPath.startsWith(normalizedBase + path.sep) || normalizedPath === normalizedBase;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* 获取 MIME 类型
|
|
35
|
+
*/
|
|
36
|
+
function getMimeType(ext) {
|
|
37
|
+
const mimeTypes = {
|
|
38
|
+
png: "image/png",
|
|
39
|
+
jpg: "image/jpeg",
|
|
40
|
+
jpeg: "image/jpeg",
|
|
41
|
+
gif: "image/gif",
|
|
42
|
+
webp: "image/webp",
|
|
43
|
+
};
|
|
44
|
+
return mimeTypes[ext.toLowerCase()] || "application/octet-stream";
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* 从 MIME 类型获取扩展名
|
|
48
|
+
*/
|
|
49
|
+
function getExtFromMime(mimeType) {
|
|
50
|
+
const extMap = {
|
|
51
|
+
"image/png": "png",
|
|
52
|
+
"image/jpeg": "jpg",
|
|
53
|
+
"image/gif": "gif",
|
|
54
|
+
"image/webp": "webp",
|
|
55
|
+
"application/pdf": "pdf",
|
|
56
|
+
"application/json": "json",
|
|
57
|
+
"text/plain": "txt",
|
|
58
|
+
"text/csv": "csv",
|
|
59
|
+
"application/msword": "doc",
|
|
60
|
+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document": "docx",
|
|
61
|
+
"application/vnd.ms-excel": "xls",
|
|
62
|
+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet": "xlsx",
|
|
63
|
+
};
|
|
64
|
+
return extMap[mimeType] || null;
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* 清理过期图片
|
|
68
|
+
*/
|
|
69
|
+
function cleanupExpiredImages() {
|
|
70
|
+
const now = Date.now();
|
|
71
|
+
const expiredIds = [];
|
|
72
|
+
for (const [id, image] of imageIndex) {
|
|
73
|
+
if (image.ttl > 0 && now - image.createdAt > image.ttl * 1000) {
|
|
74
|
+
expiredIds.push(id);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
for (const id of expiredIds) {
|
|
78
|
+
const image = imageIndex.get(id);
|
|
79
|
+
if (image) {
|
|
80
|
+
const filePath = path.join(currentConfig.storageDir, image.filename);
|
|
81
|
+
try {
|
|
82
|
+
if (fs.existsSync(filePath)) {
|
|
83
|
+
fs.unlinkSync(filePath);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
// 忽略删除错误
|
|
88
|
+
}
|
|
89
|
+
imageIndex.delete(id);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* 加载已有的图片索引
|
|
95
|
+
*/
|
|
96
|
+
function loadImageIndex() {
|
|
97
|
+
const indexPath = path.join(currentConfig.storageDir, ".index.json");
|
|
98
|
+
try {
|
|
99
|
+
if (fs.existsSync(indexPath)) {
|
|
100
|
+
const data = JSON.parse(fs.readFileSync(indexPath, "utf-8"));
|
|
101
|
+
imageIndex = new Map(Object.entries(data));
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch {
|
|
105
|
+
imageIndex = new Map();
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* 保存图片索引
|
|
110
|
+
*/
|
|
111
|
+
function saveImageIndex() {
|
|
112
|
+
const indexPath = path.join(currentConfig.storageDir, ".index.json");
|
|
113
|
+
try {
|
|
114
|
+
const data = Object.fromEntries(imageIndex);
|
|
115
|
+
fs.writeFileSync(indexPath, JSON.stringify(data, null, 2));
|
|
116
|
+
}
|
|
117
|
+
catch {
|
|
118
|
+
// 忽略保存错误
|
|
119
|
+
}
|
|
120
|
+
}
|
|
121
|
+
/**
|
|
122
|
+
* 处理 HTTP 请求
|
|
123
|
+
*/
|
|
124
|
+
function handleRequest(req, res) {
|
|
125
|
+
const url = new URL(req.url || "/", `http://localhost:${currentConfig.port}`);
|
|
126
|
+
const pathname = url.pathname;
|
|
127
|
+
// 设置 CORS 头(允许 QQ 服务器访问)
|
|
128
|
+
res.setHeader("Access-Control-Allow-Origin", "*");
|
|
129
|
+
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
|
|
130
|
+
if (req.method === "OPTIONS") {
|
|
131
|
+
res.writeHead(204);
|
|
132
|
+
res.end();
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
// 只允许 GET 请求访问图片
|
|
136
|
+
if (req.method !== "GET") {
|
|
137
|
+
res.writeHead(405, { "Content-Type": "text/plain" });
|
|
138
|
+
res.end("Method Not Allowed");
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
// 解析图片 ID(路径格式: /images/{id}.{ext})
|
|
142
|
+
const match = pathname.match(/^\/images\/([a-f0-9]{32})\.(\w+)$/);
|
|
143
|
+
if (!match) {
|
|
144
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
145
|
+
res.end("Not Found");
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const [, imageId, requestedExt] = match;
|
|
149
|
+
const image = imageIndex.get(imageId);
|
|
150
|
+
if (!image) {
|
|
151
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
152
|
+
res.end("Image Not Found");
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
// 检查是否过期
|
|
156
|
+
if (image.ttl > 0 && Date.now() - image.createdAt > image.ttl * 1000) {
|
|
157
|
+
res.writeHead(410, { "Content-Type": "text/plain" });
|
|
158
|
+
res.end("Image Expired");
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
// 安全检查:确保文件路径在存储目录内
|
|
162
|
+
const filePath = path.join(currentConfig.storageDir, image.filename);
|
|
163
|
+
if (!isPathSafe(image.filename, currentConfig.storageDir)) {
|
|
164
|
+
res.writeHead(403, { "Content-Type": "text/plain" });
|
|
165
|
+
res.end("Forbidden");
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
// 读取并返回图片
|
|
169
|
+
try {
|
|
170
|
+
if (!fs.existsSync(filePath)) {
|
|
171
|
+
res.writeHead(404, { "Content-Type": "text/plain" });
|
|
172
|
+
res.end("File Not Found");
|
|
173
|
+
return;
|
|
174
|
+
}
|
|
175
|
+
const imageData = fs.readFileSync(filePath);
|
|
176
|
+
res.writeHead(200, {
|
|
177
|
+
"Content-Type": image.mimeType,
|
|
178
|
+
"Content-Length": imageData.length,
|
|
179
|
+
"Cache-Control": image.ttl > 0 ? `max-age=${image.ttl}` : "max-age=31536000",
|
|
180
|
+
});
|
|
181
|
+
res.end(imageData);
|
|
182
|
+
}
|
|
183
|
+
catch (err) {
|
|
184
|
+
res.writeHead(500, { "Content-Type": "text/plain" });
|
|
185
|
+
res.end("Internal Server Error");
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
/**
|
|
189
|
+
* 启动图床服务器
|
|
190
|
+
*/
|
|
191
|
+
export function startImageServer(config) {
|
|
192
|
+
return new Promise((resolve, reject) => {
|
|
193
|
+
if (serverInstance) {
|
|
194
|
+
const baseUrl = currentConfig.baseUrl || `http://localhost:${currentConfig.port}`;
|
|
195
|
+
resolve(baseUrl);
|
|
196
|
+
return;
|
|
197
|
+
}
|
|
198
|
+
currentConfig = { ...DEFAULT_CONFIG, ...config };
|
|
199
|
+
// 确保存储目录存在
|
|
200
|
+
if (!fs.existsSync(currentConfig.storageDir)) {
|
|
201
|
+
fs.mkdirSync(currentConfig.storageDir, { recursive: true });
|
|
202
|
+
}
|
|
203
|
+
// 加载图片索引
|
|
204
|
+
loadImageIndex();
|
|
205
|
+
// 启动定期清理
|
|
206
|
+
const cleanupInterval = setInterval(cleanupExpiredImages, 60000); // 每分钟清理一次
|
|
207
|
+
serverInstance = http.createServer(handleRequest);
|
|
208
|
+
serverInstance.on("error", (err) => {
|
|
209
|
+
clearInterval(cleanupInterval);
|
|
210
|
+
reject(err);
|
|
211
|
+
});
|
|
212
|
+
serverInstance.listen(currentConfig.port, () => {
|
|
213
|
+
const baseUrl = currentConfig.baseUrl || `http://localhost:${currentConfig.port}`;
|
|
214
|
+
resolve(baseUrl);
|
|
215
|
+
});
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* 停止图床服务器
|
|
220
|
+
*/
|
|
221
|
+
export function stopImageServer() {
|
|
222
|
+
return new Promise((resolve) => {
|
|
223
|
+
if (serverInstance) {
|
|
224
|
+
serverInstance.close(() => {
|
|
225
|
+
serverInstance = null;
|
|
226
|
+
saveImageIndex();
|
|
227
|
+
resolve();
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
resolve();
|
|
232
|
+
}
|
|
233
|
+
});
|
|
234
|
+
}
|
|
235
|
+
/**
|
|
236
|
+
* 保存图片并返回访问 URL
|
|
237
|
+
* @param imageData 图片数据(Buffer 或 base64 字符串)
|
|
238
|
+
* @param mimeType 图片 MIME 类型
|
|
239
|
+
* @param ttlSeconds 过期时间(秒),默认使用配置值
|
|
240
|
+
* @returns 图片访问 URL
|
|
241
|
+
*/
|
|
242
|
+
export function saveImage(imageData, mimeType = "image/png", ttlSeconds) {
|
|
243
|
+
// 转换 base64 为 Buffer
|
|
244
|
+
let buffer;
|
|
245
|
+
if (typeof imageData === "string") {
|
|
246
|
+
// 处理 data URL 格式
|
|
247
|
+
const base64Match = imageData.match(/^data:([^;]+);base64,(.+)$/);
|
|
248
|
+
if (base64Match) {
|
|
249
|
+
mimeType = base64Match[1];
|
|
250
|
+
buffer = Buffer.from(base64Match[2], "base64");
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
buffer = Buffer.from(imageData, "base64");
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
else {
|
|
257
|
+
buffer = imageData;
|
|
258
|
+
}
|
|
259
|
+
// 生成唯一 ID 和文件名
|
|
260
|
+
const imageId = generateImageId();
|
|
261
|
+
const ext = getExtFromMime(mimeType) || "png";
|
|
262
|
+
const filename = `${imageId}.${ext}`;
|
|
263
|
+
// 确保存储目录存在
|
|
264
|
+
if (!fs.existsSync(currentConfig.storageDir)) {
|
|
265
|
+
fs.mkdirSync(currentConfig.storageDir, { recursive: true });
|
|
266
|
+
}
|
|
267
|
+
// 保存文件
|
|
268
|
+
const filePath = path.join(currentConfig.storageDir, filename);
|
|
269
|
+
fs.writeFileSync(filePath, buffer);
|
|
270
|
+
// 记录到索引
|
|
271
|
+
const image = {
|
|
272
|
+
id: imageId,
|
|
273
|
+
filename,
|
|
274
|
+
mimeType,
|
|
275
|
+
createdAt: Date.now(),
|
|
276
|
+
ttl: ttlSeconds ?? currentConfig.ttlSeconds,
|
|
277
|
+
};
|
|
278
|
+
imageIndex.set(imageId, image);
|
|
279
|
+
saveImageIndex();
|
|
280
|
+
// 返回访问 URL
|
|
281
|
+
const baseUrl = currentConfig.baseUrl || `http://localhost:${currentConfig.port}`;
|
|
282
|
+
return `${baseUrl}/images/${imageId}.${ext}`;
|
|
283
|
+
}
|
|
284
|
+
/**
|
|
285
|
+
* 从本地文件路径保存图片到图床
|
|
286
|
+
* @param filePath 本地文件路径
|
|
287
|
+
* @param ttlSeconds 过期时间(秒),默认使用配置值
|
|
288
|
+
* @returns 图片访问 URL,如果文件不存在或不是图片则返回 null
|
|
289
|
+
*/
|
|
290
|
+
export function saveImageFromPath(filePath, ttlSeconds) {
|
|
291
|
+
try {
|
|
292
|
+
console.log(`[image-server] saveImageFromPath: ${filePath}`);
|
|
293
|
+
// 检查文件是否存在
|
|
294
|
+
if (!fs.existsSync(filePath)) {
|
|
295
|
+
console.log(`[image-server] File not found: ${filePath}`);
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
// 读取文件
|
|
299
|
+
const buffer = fs.readFileSync(filePath);
|
|
300
|
+
console.log(`[image-server] File size: ${buffer.length}`);
|
|
301
|
+
// 根据扩展名获取 MIME 类型
|
|
302
|
+
const ext = path.extname(filePath).toLowerCase().replace(".", "");
|
|
303
|
+
console.log(`[image-server] Extension: "${ext}"`);
|
|
304
|
+
const mimeType = getMimeType(ext);
|
|
305
|
+
console.log(`[image-server] MIME type: ${mimeType}`);
|
|
306
|
+
// 只处理图片文件
|
|
307
|
+
if (!mimeType.startsWith("image/")) {
|
|
308
|
+
console.log(`[image-server] Not an image file`);
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
// 使用 saveImage 保存
|
|
312
|
+
return saveImage(buffer, mimeType, ttlSeconds);
|
|
313
|
+
}
|
|
314
|
+
catch (err) {
|
|
315
|
+
console.error(`[image-server] saveImageFromPath error:`, err);
|
|
316
|
+
return null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* 检查图床服务器是否运行中
|
|
321
|
+
*/
|
|
322
|
+
export function isImageServerRunning() {
|
|
323
|
+
return serverInstance !== null;
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* 确保图床服务器正在运行
|
|
327
|
+
* 如果未运行,则自动启动
|
|
328
|
+
* @param publicBaseUrl 公网访问的基础 URL(如 http://your-server:18765)
|
|
329
|
+
* @returns 基础 URL,启动失败返回 null
|
|
330
|
+
*/
|
|
331
|
+
export async function ensureImageServer(publicBaseUrl) {
|
|
332
|
+
if (isImageServerRunning()) {
|
|
333
|
+
return publicBaseUrl || currentConfig.baseUrl || `http://0.0.0.0:${currentConfig.port}`;
|
|
334
|
+
}
|
|
335
|
+
try {
|
|
336
|
+
const config = {
|
|
337
|
+
port: DEFAULT_CONFIG.port,
|
|
338
|
+
storageDir: DEFAULT_CONFIG.storageDir,
|
|
339
|
+
// 使用用户配置的公网地址
|
|
340
|
+
baseUrl: publicBaseUrl || `http://0.0.0.0:${DEFAULT_CONFIG.port}`,
|
|
341
|
+
ttlSeconds: 3600, // 1 小时过期
|
|
342
|
+
};
|
|
343
|
+
await startImageServer(config);
|
|
344
|
+
console.log(`[image-server] Auto-started on port ${config.port}, baseUrl: ${config.baseUrl}`);
|
|
345
|
+
return config.baseUrl;
|
|
346
|
+
}
|
|
347
|
+
catch (err) {
|
|
348
|
+
console.error(`[image-server] Failed to auto-start: ${err}`);
|
|
349
|
+
return null;
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* 下载远程文件并保存到本地
|
|
354
|
+
* @param url 远程文件 URL
|
|
355
|
+
* @param destDir 目标目录
|
|
356
|
+
* @param originalFilename 原始文件名(可选,完整文件名包含扩展名)
|
|
357
|
+
* @param options 下载选项
|
|
358
|
+
* @returns 本地文件路径,失败返回 null
|
|
359
|
+
*/
|
|
360
|
+
export async function downloadFile(url, destDir, originalFilename, options) {
|
|
361
|
+
const timeoutMs = options?.timeoutMs ?? 120000;
|
|
362
|
+
const maxSizeBytes = options?.maxSizeBytes ?? 50 * 1024 * 1024;
|
|
363
|
+
const controller = new AbortController();
|
|
364
|
+
const timeoutId = setTimeout(() => controller.abort(), timeoutMs);
|
|
365
|
+
try {
|
|
366
|
+
// 确保目录存在
|
|
367
|
+
if (!fs.existsSync(destDir)) {
|
|
368
|
+
fs.mkdirSync(destDir, { recursive: true });
|
|
369
|
+
}
|
|
370
|
+
// 下载文件(带超时控制)
|
|
371
|
+
const response = await fetch(url, { signal: controller.signal });
|
|
372
|
+
if (!response.ok) {
|
|
373
|
+
console.error(`[image-server] Download failed: ${response.status} ${response.statusText}`);
|
|
374
|
+
return null;
|
|
375
|
+
}
|
|
376
|
+
// Content-Length 预检
|
|
377
|
+
const contentLength = response.headers.get("content-length");
|
|
378
|
+
if (contentLength) {
|
|
379
|
+
const declaredSize = parseInt(contentLength, 10);
|
|
380
|
+
if (declaredSize > maxSizeBytes) {
|
|
381
|
+
console.error(`[image-server] Download rejected: Content-Length ${declaredSize} exceeds limit ${maxSizeBytes}`);
|
|
382
|
+
return null;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
// 流式下载,实时监控大小
|
|
386
|
+
const reader = response.body?.getReader();
|
|
387
|
+
if (!reader) {
|
|
388
|
+
console.error(`[image-server] Download failed: no response body`);
|
|
389
|
+
return null;
|
|
390
|
+
}
|
|
391
|
+
const chunks = [];
|
|
392
|
+
let totalSize = 0;
|
|
393
|
+
while (true) {
|
|
394
|
+
const { done, value } = await reader.read();
|
|
395
|
+
if (done)
|
|
396
|
+
break;
|
|
397
|
+
totalSize += value.byteLength;
|
|
398
|
+
if (totalSize > maxSizeBytes) {
|
|
399
|
+
reader.cancel();
|
|
400
|
+
console.error(`[image-server] Download aborted: size ${totalSize} exceeds limit ${maxSizeBytes}`);
|
|
401
|
+
return null;
|
|
402
|
+
}
|
|
403
|
+
chunks.push(value);
|
|
404
|
+
}
|
|
405
|
+
const buffer = Buffer.concat(chunks);
|
|
406
|
+
// 从 Content-Disposition 解析文件名(如果没有提供 originalFilename)
|
|
407
|
+
if (!originalFilename) {
|
|
408
|
+
const disposition = response.headers.get("content-disposition");
|
|
409
|
+
if (disposition) {
|
|
410
|
+
const filenameMatch = disposition.match(/filename\*?=(?:UTF-8''|")?([^";]+)"?/i);
|
|
411
|
+
if (filenameMatch?.[1]) {
|
|
412
|
+
try {
|
|
413
|
+
originalFilename = decodeURIComponent(filenameMatch[1]);
|
|
414
|
+
}
|
|
415
|
+
catch { /* keep undefined */ }
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
// 确定文件名
|
|
420
|
+
let finalFilename;
|
|
421
|
+
if (originalFilename) {
|
|
422
|
+
let decodedFilename = originalFilename;
|
|
423
|
+
try {
|
|
424
|
+
decodedFilename = decodeURIComponent(originalFilename);
|
|
425
|
+
}
|
|
426
|
+
catch { /* keep original */ }
|
|
427
|
+
const ext = path.extname(decodedFilename);
|
|
428
|
+
const baseName = path.basename(decodedFilename, ext);
|
|
429
|
+
const timestamp = Date.now();
|
|
430
|
+
finalFilename = `${baseName}_${timestamp}${ext}`;
|
|
431
|
+
}
|
|
432
|
+
else {
|
|
433
|
+
// 没有原始文件名,尝试从 Content-Type 推导扩展名
|
|
434
|
+
const contentType = response.headers.get("content-type");
|
|
435
|
+
const ext = contentType ? (getExtFromMime(contentType.split(";")[0]?.trim() ?? "") ?? "bin") : "bin";
|
|
436
|
+
finalFilename = `${generateImageId()}.${ext}`;
|
|
437
|
+
}
|
|
438
|
+
const filePath = path.join(destDir, finalFilename);
|
|
439
|
+
// 保存文件
|
|
440
|
+
fs.writeFileSync(filePath, buffer);
|
|
441
|
+
console.log(`[image-server] Downloaded file: ${filePath} (${buffer.length} bytes, ${Date.now()}ms)`);
|
|
442
|
+
return filePath;
|
|
443
|
+
}
|
|
444
|
+
catch (err) {
|
|
445
|
+
if (err instanceof Error && err.name === "AbortError") {
|
|
446
|
+
console.error(`[image-server] Download timeout after ${timeoutMs}ms: ${url}`);
|
|
447
|
+
}
|
|
448
|
+
else {
|
|
449
|
+
console.error(`[image-server] Download error:`, err);
|
|
450
|
+
}
|
|
451
|
+
return null;
|
|
452
|
+
}
|
|
453
|
+
finally {
|
|
454
|
+
clearTimeout(timeoutId);
|
|
455
|
+
}
|
|
456
|
+
}
|
|
457
|
+
/**
|
|
458
|
+
* 获取图床服务器配置
|
|
459
|
+
*/
|
|
460
|
+
export function getImageServerConfig() {
|
|
461
|
+
return { ...currentConfig };
|
|
462
|
+
}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* 入站附件处理模块
|
|
3
|
+
*
|
|
4
|
+
* 负责下载、转换、转录用户发送的附件(图片/语音/文件),
|
|
5
|
+
* 并归类为统一的 ProcessedAttachments 结构供 gateway 消费。
|
|
6
|
+
*/
|
|
7
|
+
export interface RawAttachment {
|
|
8
|
+
content_type: string;
|
|
9
|
+
url: string;
|
|
10
|
+
filename?: string;
|
|
11
|
+
voice_wav_url?: string;
|
|
12
|
+
asr_refer_text?: string;
|
|
13
|
+
}
|
|
14
|
+
export type TranscriptSource = "stt" | "asr" | "fallback";
|
|
15
|
+
/** processAttachments 的返回值 */
|
|
16
|
+
export interface ProcessedAttachments {
|
|
17
|
+
/** 附件描述文本(其它类型附件) */
|
|
18
|
+
attachmentInfo: string;
|
|
19
|
+
/** 图片本地路径或远程 URL */
|
|
20
|
+
imageUrls: string[];
|
|
21
|
+
/** 图片 MIME 类型(与 imageUrls 一一对应) */
|
|
22
|
+
imageMediaTypes: string[];
|
|
23
|
+
/** 语音本地路径 */
|
|
24
|
+
voiceAttachmentPaths: string[];
|
|
25
|
+
/** 语音远程 URL */
|
|
26
|
+
voiceAttachmentUrls: string[];
|
|
27
|
+
/** QQ ASR 原始识别文本 */
|
|
28
|
+
voiceAsrReferTexts: string[];
|
|
29
|
+
/** 语音转录文本 */
|
|
30
|
+
voiceTranscripts: string[];
|
|
31
|
+
/** 转录来源 */
|
|
32
|
+
voiceTranscriptSources: TranscriptSource[];
|
|
33
|
+
/** 每个附件的本地路径(与原始 attachments 数组一一对应,未下载的为 null) */
|
|
34
|
+
attachmentLocalPaths: Array<string | null>;
|
|
35
|
+
}
|
|
36
|
+
interface ProcessContext {
|
|
37
|
+
accountId: string;
|
|
38
|
+
cfg: unknown;
|
|
39
|
+
log?: {
|
|
40
|
+
info: (msg: string) => void;
|
|
41
|
+
error: (msg: string) => void;
|
|
42
|
+
debug?: (msg: string) => void;
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* 处理入站消息的附件列表。
|
|
47
|
+
*
|
|
48
|
+
* 三阶段流水线:
|
|
49
|
+
* 1. 并行下载所有附件到本地
|
|
50
|
+
* 2. 并行处理语音转换 + STT 转录
|
|
51
|
+
* 3. 按原始顺序归类结果
|
|
52
|
+
*/
|
|
53
|
+
export declare function processAttachments(attachments: RawAttachment[] | undefined, ctx: ProcessContext): Promise<ProcessedAttachments>;
|
|
54
|
+
/**
|
|
55
|
+
* 将语音转录结果组装为用户消息中的文本片段。
|
|
56
|
+
*/
|
|
57
|
+
export declare function formatVoiceText(transcripts: string[]): string;
|
|
58
|
+
export {};
|