Merge pull request #223 from tickernelz/fix/kiro-tool-filtering
Fix Kiro provider 500 error by enforcing per-tool description length limit
This commit is contained in:
commit
a7347cf64e
1 changed files with 22 additions and 64 deletions
|
|
@ -747,74 +747,32 @@ async initializeAuth(forceRefresh = false) {
|
|||
// 所有工具都被过滤掉了,不添加 tools 上下文
|
||||
console.log('[Kiro] All tools were filtered out');
|
||||
} else {
|
||||
const TARGET_SIZE = 20000;
|
||||
const MAX_DESCRIPTION_LENGTH = 9216;
|
||||
|
||||
const simplifySchema = (schema) => {
|
||||
if (!schema || typeof schema !== 'object') return { type: 'object' };
|
||||
const result = { type: schema.type || 'object' };
|
||||
if (schema.properties && typeof schema.properties === 'object') {
|
||||
result.properties = {};
|
||||
for (const [key, value] of Object.entries(schema.properties)) {
|
||||
result.properties[key] = { type: value.type || 'string' };
|
||||
if (value.enum) result.properties[key].enum = value.enum;
|
||||
}
|
||||
let truncatedCount = 0;
|
||||
const kiroTools = filteredTools.map(tool => {
|
||||
let desc = tool.description || "";
|
||||
const originalLength = desc.length;
|
||||
|
||||
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`);
|
||||
}
|
||||
if (schema.required && Array.isArray(schema.required) && schema.required.length > 0) {
|
||||
result.required = schema.required;
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
const buildTools = (maxDescLen, useSimplifiedSchema) => {
|
||||
return filteredTools.map(tool => {
|
||||
let desc = tool.description || "";
|
||||
if (maxDescLen !== null && desc.length > maxDescLen) {
|
||||
desc = desc.substring(0, maxDescLen) + "...";
|
||||
}
|
||||
return {
|
||||
toolSpecification: {
|
||||
name: tool.name,
|
||||
description: desc,
|
||||
inputSchema: {
|
||||
json: useSimplifiedSchema
|
||||
? simplifySchema(tool.input_schema)
|
||||
: (tool.input_schema || {})
|
||||
}
|
||||
|
||||
return {
|
||||
toolSpecification: {
|
||||
name: tool.name,
|
||||
description: desc,
|
||||
inputSchema: {
|
||||
json: tool.input_schema || {}
|
||||
}
|
||||
};
|
||||
});
|
||||
};
|
||||
|
||||
// 先尝试原始大小
|
||||
let kiroTools = buildTools(null, false);
|
||||
let size = JSON.stringify(kiroTools).length;
|
||||
const originalSize = size;
|
||||
|
||||
// 超过限制则压缩
|
||||
if (size > TARGET_SIZE) {
|
||||
// 简化 schema
|
||||
kiroTools = buildTools(null, true);
|
||||
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)`);
|
||||
};
|
||||
});
|
||||
|
||||
if (truncatedCount > 0) {
|
||||
console.log(`[Kiro] Truncated ${truncatedCount} tool description(s) to max ${MAX_DESCRIPTION_LENGTH} chars`);
|
||||
}
|
||||
|
||||
toolsContext = { tools: kiroTools };
|
||||
|
|
|
|||
Loading…
Reference in a new issue