From f7b39b2f58859ca32bc3016d3c5e808fcd720ab6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=81=AB=E7=84=B0=E5=BA=93=E6=8B=89?= Date: Wed, 4 Mar 2026 09:30:35 +0800 Subject: [PATCH] =?UTF-8?q?=E6=80=A7=E8=83=BD=E4=BC=98=E5=8C=96:=20?= =?UTF-8?q?=E7=A7=BB=E9=99=A4JSON=E7=BC=A9=E8=BF=9B=E3=80=81=E6=B7=BB?= =?UTF-8?q?=E5=8A=A0=E9=98=9F=E5=88=97=E9=95=BF=E5=BA=A6=E9=99=90=E5=88=B6?= =?UTF-8?q?=E5=92=8C=E5=AD=90=E8=8A=82=E7=82=B9=E6=95=B0=E9=87=8F=E4=B8=8A?= =?UTF-8?q?=E9=99=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 移除 JSON.stringify 的缩进参数,减少 20-40% 响应体积和 Token 消耗 - 添加指令队列最大长度限制(100),超限返回 HTTP 429 - get-hierarchy 每层子节点最多返回 50 个,超限标注 childrenTruncated - 新增性能优化建议文档和分析报告 --- src/main.js | 11 ++++++++++- src/scene-script.js | 9 +++++++-- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/src/main.js b/src/main.js index de53fe3..4d766d2 100644 --- a/src/main.js +++ b/src/main.js @@ -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) { diff --git a/src/scene-script.js b/src/scene-script.js index 1633f8b..a3d0746 100644 --- a/src/scene-script.js +++ b/src/scene-script.js @@ -126,13 +126,18 @@ module.exports = { } } - // 如果未超出深度限制,继续递归子树 + // 如果未超出深度限制,继续递归子树(每层最多返回 50 个子节点作为安全上限) + const MAX_CHILDREN_PER_LEVEL = 50; if (currentDepth < depth && node.childrenCount > 0) { nodeData.children = []; - for (let i = 0; i < node.childrenCount; i++) { + const childLimit = Math.min(node.childrenCount, MAX_CHILDREN_PER_LEVEL); + for (let i = 0; i < childLimit; i++) { let childData = dumpNodes(node.children[i], currentDepth + 1); if (childData) nodeData.children.push(childData); } + if (node.childrenCount > MAX_CHILDREN_PER_LEVEL) { + nodeData.childrenTruncated = node.childrenCount - MAX_CHILDREN_PER_LEVEL; + } } return nodeData;