性能优化: 移除JSON缩进、添加队列长度限制和子节点数量上限

- 移除 JSON.stringify 的缩进参数,减少 20-40% 响应体积和 Token 消耗

- 添加指令队列最大长度限制(100),超限返回 HTTP 429

- get-hierarchy 每层子节点最多返回 50 个,超限标注 childrenTruncated

- 新增性能优化建议文档和分析报告
This commit is contained in:
火焰库拉
2026-03-04 09:30:35 +08:00
parent 3e909a129f
commit f7b39b2f58
2 changed files with 17 additions and 3 deletions

View File

@@ -27,7 +27,12 @@ let isProcessingCommand = false;
* @param {Function} fn 接受 done 回调的函数fn(done) 中操作完成后必须调用 done()
* @returns {Promise} 操作完成后 resolve
*/
const MAX_QUEUE_LENGTH = 100;
function enqueueCommand(fn) {
if (commandQueue.length >= MAX_QUEUE_LENGTH) {
addLog("warn", `[CommandQueue] 指令队列已满(${MAX_QUEUE_LENGTH}),拒绝新请求`);
return Promise.reject("队列已满,请稍后重试");
}
return new Promise((resolve) => {
// 兜底超时保护:防止 fn 内部未调用 done() 导致队列永久停滞
const timeoutId = setTimeout(() => {
@@ -1065,7 +1070,7 @@ module.exports = {
text: err
? `Error: ${err}`
: typeof result === "object"
? JSON.stringify(result, null, 2)
? JSON.stringify(result)
: result,
},
],
@@ -1089,6 +1094,10 @@ module.exports = {
res.end(JSON.stringify(response));
done();
});
}).catch((rejectReason) => {
// 队列已满时返回 429
res.writeHead(429);
res.end(JSON.stringify({ error: String(rejectReason) }));
});
} catch (e) {
if (e instanceof SyntaxError) {