refactor(kiro): simplify tool size compression logic
The code refactors the tool building process in initializeAuth to handle description truncation and size limits more efficiently. It introduces per-tool description truncation using a fixed max length, and simplifies the compression logic by removing the iterative description adjustment loop. This improves code clarity and performance.
This commit is contained in:
parent
2e9c24d1f0
commit
2e9e319933
1 changed files with 22 additions and 27 deletions
|
|
@ -747,7 +747,8 @@ async initializeAuth(forceRefresh = false) {
|
||||||
// 所有工具都被过滤掉了,不添加 tools 上下文
|
// 所有工具都被过滤掉了,不添加 tools 上下文
|
||||||
console.log('[Kiro] All tools were filtered out');
|
console.log('[Kiro] All tools were filtered out');
|
||||||
} else {
|
} else {
|
||||||
const TARGET_SIZE = 20000;
|
const MAX_DESCRIPTION_LENGTH = 9216;
|
||||||
|
const TARGET_TOTAL_SIZE = 200000;
|
||||||
|
|
||||||
const simplifySchema = (schema) => {
|
const simplifySchema = (schema) => {
|
||||||
if (!schema || typeof schema !== 'object') return { type: 'object' };
|
if (!schema || typeof schema !== 'object') return { type: 'object' };
|
||||||
|
|
@ -765,12 +766,18 @@ async initializeAuth(forceRefresh = false) {
|
||||||
return result;
|
return result;
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildTools = (maxDescLen, useSimplifiedSchema) => {
|
const buildToolsWithPerToolTruncation = (useSimplifiedSchema) => {
|
||||||
return filteredTools.map(tool => {
|
let truncatedCount = 0;
|
||||||
|
const tools = filteredTools.map(tool => {
|
||||||
let desc = tool.description || "";
|
let desc = tool.description || "";
|
||||||
if (maxDescLen !== null && desc.length > maxDescLen) {
|
const originalLength = desc.length;
|
||||||
desc = desc.substring(0, maxDescLen) + "...";
|
|
||||||
|
if (desc.length > MAX_DESCRIPTION_LENGTH) {
|
||||||
|
desc = desc.substring(0, MAX_DESCRIPTION_LENGTH) + "...";
|
||||||
|
truncatedCount++;
|
||||||
|
console.log(`[Kiro] Truncated tool '${tool.name}' description: ${originalLength} -> ${desc.length} chars`);
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
toolSpecification: {
|
toolSpecification: {
|
||||||
name: tool.name,
|
name: tool.name,
|
||||||
|
|
@ -783,37 +790,25 @@ async initializeAuth(forceRefresh = false) {
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (truncatedCount > 0) {
|
||||||
|
console.log(`[Kiro] Truncated ${truncatedCount} tool description(s) to max ${MAX_DESCRIPTION_LENGTH} chars`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return tools;
|
||||||
};
|
};
|
||||||
|
|
||||||
// 先尝试原始大小
|
// 先尝试原始大小
|
||||||
let kiroTools = buildTools(null, false);
|
let kiroTools = buildToolsWithPerToolTruncation(false);
|
||||||
let size = JSON.stringify(kiroTools).length;
|
let size = JSON.stringify(kiroTools).length;
|
||||||
const originalSize = size;
|
const originalSize = size;
|
||||||
|
|
||||||
// 超过限制则压缩
|
// 超过限制则压缩
|
||||||
if (size > TARGET_SIZE) {
|
if (size > TARGET_TOTAL_SIZE) {
|
||||||
|
console.log(`[Kiro] Total tools size ${size} exceeds target ${TARGET_TOTAL_SIZE}, simplifying schemas...`);
|
||||||
// 简化 schema
|
// 简化 schema
|
||||||
kiroTools = buildTools(null, true);
|
kiroTools = buildToolsWithPerToolTruncation(true);
|
||||||
size = JSON.stringify(kiroTools).length;
|
size = JSON.stringify(kiroTools).length;
|
||||||
|
|
||||||
// 缩短描述
|
|
||||||
if (size > TARGET_SIZE) {
|
|
||||||
const ratio = TARGET_SIZE / size;
|
|
||||||
const totalDescLen = tools.reduce((sum, t) => sum + (t.description || "").length, 0);
|
|
||||||
const avgDescLen = totalDescLen / tools.length;
|
|
||||||
let targetDescLen = Math.floor(avgDescLen * ratio * 0.8);
|
|
||||||
targetDescLen = Math.max(50, Math.min(500, targetDescLen));
|
|
||||||
|
|
||||||
kiroTools = buildTools(targetDescLen, true);
|
|
||||||
size = JSON.stringify(kiroTools).length;
|
|
||||||
|
|
||||||
while (size > TARGET_SIZE && targetDescLen > 50) {
|
|
||||||
targetDescLen = Math.floor(targetDescLen * 0.7);
|
|
||||||
targetDescLen = Math.max(50, targetDescLen);
|
|
||||||
kiroTools = buildTools(targetDescLen, true);
|
|
||||||
size = JSON.stringify(kiroTools).length;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log(`[Kiro] Tools compressed: ${originalSize} -> ${size} bytes (${filteredTools.length} tools)`);
|
console.log(`[Kiro] Tools compressed: ${originalSize} -> ${size} bytes (${filteredTools.length} tools)`);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue