97 lines
2.7 KiB
JavaScript
97 lines
2.7 KiB
JavaScript
|
|
/**
|
|||
|
|
* VibeEngineering V2 - 上下文提取脚本
|
|||
|
|
* 从 transcript.jsonl 提取最后一次 TaskUpdate completed 之后的内容
|
|||
|
|
*/
|
|||
|
|
|
|||
|
|
const fs = require('fs');
|
|||
|
|
|
|||
|
|
function extractSinceLastTaskComplete(transcriptPath) {
|
|||
|
|
const content = fs.readFileSync(transcriptPath, 'utf-8');
|
|||
|
|
const lines = content.split('\n').filter(Boolean);
|
|||
|
|
|
|||
|
|
// 倒序找最后一次 TaskUpdate status=completed
|
|||
|
|
let cutoffIndex = 0;
|
|||
|
|
for (let i = lines.length - 1; i >= 0; i--) {
|
|||
|
|
try {
|
|||
|
|
const entry = JSON.parse(lines[i]);
|
|||
|
|
if (entry.tool_name === 'TaskUpdate' &&
|
|||
|
|
entry.tool_input?.status === 'completed') {
|
|||
|
|
cutoffIndex = i + 1;
|
|||
|
|
break;
|
|||
|
|
}
|
|||
|
|
} catch (e) {
|
|||
|
|
continue;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const relevantLines = lines.slice(cutoffIndex);
|
|||
|
|
const simplified = simplifyEntries(relevantLines);
|
|||
|
|
return simplified.map(e => JSON.stringify(e)).join('\n');
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
function simplifyEntries(lines) {
|
|||
|
|
return lines.map(line => {
|
|||
|
|
try {
|
|||
|
|
const entry = JSON.parse(line);
|
|||
|
|
|
|||
|
|
// Claude 输出:全部保留
|
|||
|
|
if (entry.role === 'assistant') {
|
|||
|
|
return { type: 'assistant', content: entry.content };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 用户输入:全部保留
|
|||
|
|
if (entry.role === 'user') {
|
|||
|
|
return { type: 'user', content: entry.content };
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// Task tool 返回(子代理结果):全部保留
|
|||
|
|
if (entry.type === 'tool_result' && entry.agentId) {
|
|||
|
|
return {
|
|||
|
|
type: 'task_result',
|
|||
|
|
agentId: entry.agentId,
|
|||
|
|
content: entry.content
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 工具调用
|
|||
|
|
if (entry.tool_name) {
|
|||
|
|
const input = entry.tool_input || {};
|
|||
|
|
|
|||
|
|
// Task tool(派发子代理):prompt 全部保留
|
|||
|
|
if (entry.tool_name === 'Task') {
|
|||
|
|
return {
|
|||
|
|
type: 'tool',
|
|||
|
|
name: 'Task',
|
|||
|
|
subagent_type: input.subagent_type,
|
|||
|
|
description: input.description,
|
|||
|
|
prompt: input.prompt
|
|||
|
|
};
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 其他工具:只保留关键参数
|
|||
|
|
const simplified = { type: 'tool', name: entry.tool_name };
|
|||
|
|
if (input.file_path) simplified.file = input.file_path;
|
|||
|
|
if (input.command) simplified.command = input.command;
|
|||
|
|
if (input.pattern) simplified.pattern = input.pattern;
|
|||
|
|
if (input.path) simplified.path = input.path;
|
|||
|
|
if (input.taskId) simplified.taskId = input.taskId;
|
|||
|
|
if (input.status) simplified.status = input.status;
|
|||
|
|
|
|||
|
|
if (entry.tool_result?.exit_code !== undefined) {
|
|||
|
|
simplified.exit_code = entry.tool_result.exit_code;
|
|||
|
|
}
|
|||
|
|
return simplified;
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
return null;
|
|||
|
|
} catch (e) {
|
|||
|
|
return null;
|
|||
|
|
}
|
|||
|
|
}).filter(Boolean);
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
const transcriptPath = process.argv[2];
|
|||
|
|
if (transcriptPath) {
|
|||
|
|
console.log(extractSinceLastTaskComplete(transcriptPath));
|
|||
|
|
}
|