Files
pmp-tool/dist-server/lib/experience-manager.js
xiaohei 61ed9e9dc3
Some checks failed
CI / lint-and-typecheck (push) Failing after 30s
CI / test (push) Has been skipped
CI / build (push) Has been skipped
feat: MVP v0.5 complete - All P0 features implemented and frontend verified. Backend API structure ready, pending final ES module configuration for deployment.
2026-04-12 01:46:38 +08:00

137 lines
5.2 KiB
JavaScript

"use strict";
/**
* 经验管理员 - 知识管理 + 执行记录 + 跨Agent协调
*/
var __assign = (this && this.__assign) || function () {
__assign = Object.assign || function(t) {
for (var s, i = 1, n = arguments.length; i < n; i++) {
s = arguments[i];
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
t[p] = s[p];
}
return t;
};
return __assign.apply(this, arguments);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ExperienceManager = void 0;
/**
* 经验管理员
*/
var ExperienceManager = /** @class */ (function () {
function ExperienceManager() {
this.executionLog = [];
this.decisionLog = [];
this.knowledgeBase = new Map();
this.records = [];
}
/**
* 记录Agent执行结果
*/
ExperienceManager.prototype.recordExecution = function (record) {
this.records.push(record);
// Update knowledge base
this._updateKnowledge(record);
};
/**
* 获取相关上下文供Agent使用
*/
ExperienceManager.prototype.getContext = function (taskType, keywords) {
// Find recent similar executions
var recentExecutions = this.records
.filter(function (r) { return r.atomicType === taskType; })
.slice(-10);
// Find relevant knowledge entries
var relevantKnowledge = Array.from(this.knowledgeBase.values())
.filter(function (k) { return k.taskType === taskType && k.score >= 3; });
// Generate suggestion
var bestPractice = relevantKnowledge
.sort(function (a, b) { return b.score - a.score; })[0];
var suggestion = bestPractice
? "Based on ".concat(bestPractice.successCount, " successful executions, avg score ").concat(bestPractice.score.toFixed(1), ": use input pattern ").concat(JSON.stringify(Object.keys(bestPractice.inputPattern)))
: 'No prior experience for this task type. Using default approach.';
return { recentExecutions: recentExecutions, relevantKnowledge: relevantKnowledge, suggestion: suggestion };
};
/**
* 记录决策
*/
ExperienceManager.prototype.recordDecision = function (decision) {
this.decisionLog.push(__assign(__assign({}, decision), { id: "decision-".concat(Date.now()), createdAt: new Date().toISOString() }));
};
/**
* 检查是否需要人工介入
*/
ExperienceManager.prototype.checkInterventionNeeded = function (taskId, score, consecutiveFailures) {
if (score <= 2) {
return {
needed: true,
reason: "Agent execution score too low (".concat(score, "/5). Needs human review."),
};
}
if (consecutiveFailures >= 3) {
return {
needed: true,
reason: "".concat(consecutiveFailures, " consecutive failures. Human intervention required."),
};
}
return { needed: false, reason: '' };
};
/**
* 获取项目知识库摘要
*/
ExperienceManager.prototype.getKnowledgeSummary = function () {
var entries = Array.from(this.knowledgeBase.values());
var byType = {};
for (var _i = 0, entries_1 = entries; _i < entries_1.length; _i++) {
var e = entries_1[_i];
byType[e.taskType] = (byType[e.taskType] || 0) + 1;
}
var avgScore = entries.length > 0
? entries.reduce(function (sum, e) { return sum + e.score; }, 0) / entries.length
: 0;
return { totalEntries: entries.length, byType: byType, avgScore: avgScore };
};
/**
* 获取执行历史
*/
ExperienceManager.prototype.getExecutionHistory = function (taskId) {
if (taskId) {
return this.records.filter(function (r) { return r.taskId === taskId; });
}
return this.records;
};
/**
* 获取决策历史
*/
ExperienceManager.prototype.getDecisionHistory = function () {
return this.decisionLog;
};
// --- Private ---
ExperienceManager.prototype._updateKnowledge = function (record) {
var key = "".concat(record.atomicType, "_").concat(record.model);
var existing = this.knowledgeBase.get(key);
if (existing) {
existing.successCount += record.success ? 1 : 0;
existing.failCount += record.success ? 0 : 1;
// Running average score
var totalRuns = existing.successCount + existing.failCount;
existing.score = (existing.score * (totalRuns - 1) + record.score) / totalRuns;
}
else {
this.knowledgeBase.set(key, {
id: "kb-".concat(Date.now()),
taskType: record.atomicType,
description: "Pattern for ".concat(record.atomicType, " with ").concat(record.model),
inputPattern: record.input,
outputPattern: record.output,
score: record.score,
successCount: record.success ? 1 : 0,
failCount: record.success ? 0 : 1,
learnedAt: new Date().toISOString(),
});
}
};
return ExperienceManager;
}());
exports.ExperienceManager = ExperienceManager;