perf: move token counting to end of stream to avoid blocking event loop

This commit is contained in:
Sanyela 2025-12-03 15:16:40 +08:00
parent 9bb8be858e
commit 402278240c

View file

@ -286,13 +286,13 @@ export class KiroApiService {
// 配置 HTTP/HTTPS agent 限制连接池大小,避免资源泄漏 // 配置 HTTP/HTTPS agent 限制连接池大小,避免资源泄漏
const httpAgent = new http.Agent({ const httpAgent = new http.Agent({
keepAlive: true, keepAlive: true,
maxSockets: 10, // 每个主机最多 10 个连接 maxSockets: 200, // 每个主机最多 10 个连接
maxFreeSockets: 5, // 最多保留 5 个空闲连接 maxFreeSockets: 5, // 最多保留 5 个空闲连接
timeout: 60000, // 空闲连接 60 秒后关闭 timeout: 60000, // 空闲连接 60 秒后关闭
}); });
const httpsAgent = new https.Agent({ const httpsAgent = new https.Agent({
keepAlive: true, keepAlive: true,
maxSockets: 10, maxSockets: 100,
maxFreeSockets: 5, maxFreeSockets: 5,
timeout: 60000, timeout: 60000,
}); });
@ -1329,7 +1329,7 @@ async initializeAuth(forceRefresh = false) {
for await (const event of this.streamApiReal('', finalModel, requestBody)) { for await (const event of this.streamApiReal('', finalModel, requestBody)) {
if (event.type === 'content' && event.content) { if (event.type === 'content' && event.content) {
totalContent += event.content; totalContent += event.content;
outputTokens += this.countTextTokens(event.content); // 不再每个 chunk 都计算 token改为最后统一计算避免阻塞事件循环
yield { yield {
type: "content_block_delta", type: "content_block_delta",
@ -1441,12 +1441,16 @@ async initializeAuth(forceRefresh = false) {
}; };
yield { type: "content_block_stop", index: blockIndex }; yield { type: "content_block_stop", index: blockIndex };
outputTokens += this.countTextTokens(JSON.stringify(tc.input || {}));
} }
} }
// 6. 发送 message_delta 事件 // 6. 发送 message_delta 事件
// 在流结束后统一计算 output tokens避免在流式循环中阻塞事件循环
outputTokens = this.countTextTokens(totalContent);
for (const tc of toolCalls) {
outputTokens += this.countTextTokens(JSON.stringify(tc.input || {}));
}
yield { yield {
type: "message_delta", type: "message_delta",
delta: { stop_reason: toolCalls.length > 0 ? "tool_use" : "end_turn" }, delta: { stop_reason: toolCalls.length > 0 ? "tool_use" : "end_turn" },