39 lines
1.6 KiB
JavaScript
39 lines
1.6 KiB
JavaScript
'use strict';
|
|
|
|
const crypto = require('node:crypto');
|
|
|
|
function toolSignature(tool) {
|
|
return crypto.createHash('sha256').update(JSON.stringify(tool)).digest('hex');
|
|
}
|
|
|
|
function inlineTool(tool) {
|
|
const kind = tool?.kind || tool?.meta?.kind;
|
|
const name = String(tool?.name || '');
|
|
const target = String(tool?.input?.tool || tool?.input?.name || tool?.meta?.subtitle || '');
|
|
return ['todo_list', 'collab_agent_tool_call', 'subAgentActivity'].includes(kind)
|
|
|| ['AskUserQuestion', 'subAgentActivity'].includes(name)
|
|
|| /(?:^|[./])ccweb_display_image$/.test(target);
|
|
}
|
|
|
|
// 折叠工具只传显示摘要;原始输入、输出仍由详情接口完整读取。
|
|
function summarizeTools(tools, reference) {
|
|
return (Array.isArray(tools) ? tools : []).map((tool, index) => {
|
|
if (!tool || typeof tool !== 'object' || inlineTool(tool)) return tool;
|
|
const input = {};
|
|
for (const key of ['command', 'path', 'file_path', 'tool', 'name', 'kind']) {
|
|
if (typeof tool.input?.[key] === 'string') input[key] = tool.input[key].slice(0, 240);
|
|
}
|
|
const meta = {};
|
|
for (const key of ['title', 'subtitle', 'kind', 'exitCode', 'status']) {
|
|
const value = tool.meta?.[key];
|
|
if (typeof value === 'string') meta[key] = value.slice(0, 240);
|
|
else if (typeof value === 'number' || typeof value === 'boolean') meta[key] = value;
|
|
}
|
|
return { id: tool.id, name: tool.name, kind: tool.kind, done: tool.done, input, meta,
|
|
detailRef: { ...reference, index, toolId: tool.id || null,
|
|
...(reference.scope ? {} : { signature: toolSignature(tool) }) } };
|
|
});
|
|
}
|
|
|
|
module.exports = { summarizeTools, toolSignature };
|