Project Files
dist / orchestrator.js
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.AutoBuilder = void 0;
const coder_1 = require("./agents/coder");
const optimizer_1 = require("./agents/optimizer");
const planner_1 = require("./agents/planner");
const reviewer_1 = require("./agents/reviewer");
const tester_1 = require("./agents/tester");
const store_1 = require("./memory/store");
const llm_1 = require("./llm");
const workspace_1 = require("./workspace");
const pathUtils_1 = require("./pathUtils");
class AutoBuilder {
client;
workspaceRoot;
memory;
constructor(client, workspaceRoot, memory = new store_1.MemoryStore(workspaceRoot)) {
this.client = client;
this.workspaceRoot = workspaceRoot;
this.memory = memory;
}
async build(spec, options = {}) {
const model = await this.client.llm.model();
const memorySummary = await this.memory.summary(12);
const targetLabel = options.targetDir ?? spec;
const targetRoot = (0, pathUtils_1.resolveForgeTarget)(this.workspaceRoot, targetLabel || (0, llm_1.slugify)(spec));
const maxRounds = Math.max(1, Math.min(8, options.maxRounds ?? 4));
const planner = new planner_1.Planner(model, this.memory);
const coder = new coder_1.Coder(model);
const reviewer = new reviewer_1.Reviewer(model);
const tester = new tester_1.Tester();
const optimizer = new optimizer_1.Optimizer(model);
await this.memory.add("request", spec, { targetRoot });
const plan = await planner.run(spec, targetRoot, memorySummary);
let rounds = 0;
let snapshot = await (0, workspace_1.snapshotWorkspace)(targetRoot);
if (snapshot.length === 0) {
const initialPatches = await coder.run({
plan,
snapshot: "[]",
issues: [],
memorySummary,
});
await this.applyPatchesWithRollback(targetRoot, initialPatches);
snapshot = await (0, workspace_1.snapshotWorkspace)(targetRoot);
}
while (rounds < maxRounds) {
snapshot = await (0, workspace_1.snapshotWorkspace)(targetRoot);
const review = await reviewer.run({
plan,
snapshot: JSON.stringify(snapshot, null, 2),
});
const test = await tester.run(snapshot);
if (review.passed && test.passed)
break;
const issues = [...review.issues, ...test.issues];
await this.memory.add("issues", JSON.stringify(issues), { rounds, targetRoot });
const fixes = await optimizer.run({
plan,
snapshot: JSON.stringify(snapshot, null, 2),
issues,
memorySummary,
});
try {
await this.applyPatchesWithRollback(targetRoot, fixes);
}
catch (error) {
console.error("Failed to apply patches, stopping build:", error);
break;
}
rounds += 1;
}
const finalSnapshot = await (0, workspace_1.snapshotWorkspace)(targetRoot);
const finalReview = await reviewer.run({ plan, snapshot: JSON.stringify(finalSnapshot, null, 2) });
const finalTest = await tester.run(finalSnapshot);
const finalStatus = {
pluginName: plan.pluginName,
targetRoot,
passed: finalReview.passed && finalTest.passed,
rounds,
issues: [...finalReview.issues, ...finalTest.issues],
};
await this.memory.add("result", JSON.stringify(finalStatus), { targetRoot });
return {
targetRoot,
rounds,
review: finalReview,
test: finalTest,
files: finalSnapshot,
plan,
};
}
async applyPatchesWithRollback(root, patches) {
for (const patch of patches) {
try {
await (0, workspace_1.applyPatchToFile)(root, patch);
await this.memory.add("patch", `${patch.operation} ${patch.file}`, {
file: patch.file,
operation: patch.operation,
});
}
catch (error) {
await this.memory.add("patch-error", `${patch.file}: ${error.message}`, { file: patch.file, operation: patch.operation });
}
}
}
}
exports.AutoBuilder = AutoBuilder;