Repository navigation
Expand file tree
/
Copy pathcreate.ts
More file actions
140 lines (127 loc) · 5.01 KB
/
Copy pathcreate.ts
File metadata and controls
140 lines (127 loc) · 5.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import { createHash } from "node:crypto";
import { mkdirSync, readdirSync, unlinkSync, writeFileSync } from "node:fs";
import { join, resolve } from "node:path";
import { parseAgentMarkdown } from "@agentgg/core";
import { renderAgentSpecMd } from "./agent-spec.js";
import { loadCreateInstructions } from "./create-agent.js";
import type { Detector } from "./detect.js";
import { logError } from "./log.js";
import type { LoadedReport } from "./report-loader.js";
/**
* Orchestration for `agentgg create`. One distillation session per report
* file: the detector reads the report + explores the code root with its
* tools, then emits an `AgentSpec` constrained by the schema. We render
* the spec to an agent `.md`, validate the round-trip through the loader,
* and write to `<outputDir>/<slug>-<hash>.md`.
*
* The filename hash is derived from the report path + content + code
* root, so a re-run on the same inputs overwrites the same file
* (idempotent), and runs against different reports never collide. Same
* naming shape as findings (`{slug}-{title}-{id}.md`).
*/
export interface CreateOptions {
rootDir: string;
outputDir: string;
reports: LoadedReport[];
detector: Detector;
excludePatterns: string[];
includePatterns: string[];
maxFileSizeKb: number;
maxTurns: number;
signal?: AbortSignal;
verbose?: boolean;
}
export interface CreateOutcome {
reportPath: string;
status: "created" | "failed";
agentPath?: string;
slug?: string;
error?: string;
}
export async function runCreate(opts: CreateOptions): Promise<CreateOutcome[]> {
if (!opts.detector.createAgent) {
throw new Error(
`The active provider (${opts.detector.name}) does not support \`agentgg create\` yet.`,
);
}
mkdirSync(opts.outputDir, { recursive: true });
const instructions = loadCreateInstructions();
const outcomes: CreateOutcome[] = [];
// Sequential rather than parallel: a future user is most likely passing
// a handful of reports interactively, and a tool-enabled session against
// the same code root benefits from cache warmth on consecutive calls
// (and avoids fighting for the LLM rate limit).
for (let i = 0; i < opts.reports.length; i++) {
const report = opts.reports[i] as LoadedReport;
console.log(`[${i + 1}/${opts.reports.length}] Distilling ${report.name} into an agent...`);
try {
const spec = await opts.detector.createAgent({
rootDir: opts.rootDir,
instructions,
reportName: report.name,
reportContent: report.content,
excludePatterns: opts.excludePatterns,
includePatterns: opts.includePatterns,
maxFileSizeKb: opts.maxFileSizeKb,
maxTurns: opts.maxTurns,
signal: opts.signal,
});
const md = renderAgentSpecMd(spec);
// Round-trip the rendered markdown through the same loader scan.ts
// uses. Catches regex compile failures, slug shape violations, and
// any other schema mismatch before the file lands on disk.
try {
parseAgentMarkdown(md);
} catch (err) {
throw new Error(
`Generated agent failed lint: ${(err as Error).message}. The model produced an invalid spec; raise --max-turns or retry.`,
);
}
const hash = shortHash(opts.rootDir, report.path, report.content);
const filename = `${spec.slug}-${hash}.md`;
const outPath = join(opts.outputDir, filename);
// The hash (not the model-chosen slug) is the stable identity of a
// distillation. Drop any prior agent sharing this hash suffix so a
// re-run on identical inputs replaces it instead of forking a second
// file when the slug drifts.
for (const name of readdirSync(opts.outputDir)) {
if (name !== filename && name.endsWith(`-${hash}.md`)) {
unlinkSync(join(opts.outputDir, name));
}
}
writeFileSync(outPath, md, "utf8");
console.log(` -> ${outPath}`);
outcomes.push({
reportPath: report.path,
status: "created",
agentPath: outPath,
slug: spec.slug,
});
} catch (err) {
const msg = err instanceof Error ? err.message : String(err);
logError(msg);
outcomes.push({
reportPath: report.path,
status: "failed",
error: msg,
});
}
}
return outcomes;
}
/**
* Stable 8-char hash for the agent filename suffix. Derived from
* (code root, report path, report content) so:
* - Same inputs → same hash → idempotent re-runs replace the prior agent
* (any file sharing this suffix is removed first), even if the model
* picks a different slug.
* - Different reports → different hash → no collisions in the output dir.
* - Editing the report content → new hash → fresh file (the model's
* read of the report changed, so the previous agent is stale).
*/
function shortHash(rootDir: string, reportPath: string, reportContent: string): string {
return createHash("sha256")
.update(`${resolve(rootDir)}|${resolve(reportPath)}|${reportContent}`)
.digest("hex")
.slice(0, 8);
}