84 lines
2.7 KiB
JavaScript

// ═══════════════════════════════════════
// 铸渊工程Agent · HLDP大脑记录器
// 记录认知推理链,而非流水账
// ═══════════════════════════════════════
const fs = require("fs");
const path = require("path");
const AGENT_ID = process.env.AGENT_ID || "ICE-GL-BD001";
const TERRITORY = path.join(__dirname, "..", "agents", AGENT_ID.replace("ICE-GL-BD", "BD"));
if (!fs.existsSync(TERRITORY)) {
console.warn("[BrainWriter] 领地不存在: " + TERRITORY + " · 将不写入认知记录");
}
/**
* 写入一条认知记录
*/
function record(taskId, entry) {
if (!fs.existsSync(TERRITORY)) return;
const file = path.join(TERRITORY, "brain", "cognitive-record.hldp");
const lines = [
"",
"@task: " + taskId,
"@time: " + new Date().toISOString(),
"@input: " + (entry.input || ""),
"@decision: " + (entry.decision || ""),
"@execution: " + (entry.execution || ""),
"@failure: " + (entry.failure || "无"),
"@reasoning: " + (entry.reasoning || ""),
"@fix: " + (entry.fix || ""),
"@learned: " + (entry.learned || ""),
"@confidence: " + (entry.confidence || "N/A"),
"@new_knowledge: " + (entry.new_knowledge ? "true" : "false")
];
fs.appendFileSync(file, lines.join("\n") + "\n", "utf-8");
}
/**
* 记录错误模式
*/
function recordErrorPattern(pattern) {
if (!fs.existsSync(TERRITORY)) return;
const file = path.join(TERRITORY, "memory", "error-patterns.json");
const data = JSON.parse(fs.readFileSync(file, "utf-8"));
// 检查是否已有同类模式
const existing = data.patterns.find(p => p.type === pattern.type);
if (existing) {
existing.count = (existing.count || 1) + 1;
existing.last_seen = new Date().toISOString();
} else {
data.patterns.push({
type: pattern.type,
description: pattern.description,
count: 1,
first_seen: new Date().toISOString(),
last_seen: new Date().toISOString(),
examples: [pattern.example]
});
}
fs.writeFileSync(file, JSON.stringify(data, null, 2), "utf-8");
}
/**
* 记录成长里程碑
*/
function recordMilestone(event) {
if (!fs.existsSync(TERRITORY)) return;
const file = path.join(TERRITORY, "brain", "growth-timeline.json");
const data = JSON.parse(fs.readFileSync(file, "utf-8"));
data.milestones.push({
time: new Date().toISOString(),
event: event,
project: process.env.CURRENT_PROJECT_ID || "unknown"
});
fs.writeFileSync(file, JSON.stringify(data, null, 2), "utf-8");
}
module.exports = { record, recordErrorPattern, recordMilestone, TERRITORY, AGENT_ID };