ai-discovery-manager-cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +252 -0
- package/dist/chat.js +827 -0
- package/dist/checkpoints.js +134 -0
- package/dist/cli.js +796 -0
- package/dist/doctor.js +148 -0
- package/dist/hypothesisSchema.js +106 -0
- package/dist/jsonOutput.js +65 -0
- package/dist/mcpManager.js +196 -0
- package/dist/models.js +117 -0
- package/dist/safety.js +152 -0
- package/dist/specialistContracts.js +355 -0
- package/dist/workspaceTools.js +166 -0
- package/package.json +32 -0
|
@@ -0,0 +1,134 @@
|
|
|
1
|
+
import { mkdir, readFile, writeFile } from "node:fs/promises";
|
|
2
|
+
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
3
|
+
import path from "node:path";
|
|
4
|
+
const RUNS_DIR = ".ai-discovery/runs";
|
|
5
|
+
function makeRunId(command) {
|
|
6
|
+
const stamp = new Date()
|
|
7
|
+
.toISOString()
|
|
8
|
+
.replace(/[:.]/g, "-")
|
|
9
|
+
.replace("T", "_")
|
|
10
|
+
.replace("Z", "");
|
|
11
|
+
const suffix = Math.random().toString(36).slice(2, 8);
|
|
12
|
+
return `${command}-${stamp}-${suffix}`;
|
|
13
|
+
}
|
|
14
|
+
function assertRunId(runId) {
|
|
15
|
+
if (!/^[A-Za-z0-9_.-]+$/.test(runId)) {
|
|
16
|
+
throw new Error(`Invalid run id "${runId}".`);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
export function checkpointDir(workspace, runId) {
|
|
20
|
+
assertRunId(runId);
|
|
21
|
+
return path.resolve(workspace, RUNS_DIR, runId);
|
|
22
|
+
}
|
|
23
|
+
export async function createRunCheckpoint(workspace, command, options, prompt, resumedFrom) {
|
|
24
|
+
const runId = makeRunId(command);
|
|
25
|
+
const dir = checkpointDir(workspace, runId);
|
|
26
|
+
const checkpoint = {
|
|
27
|
+
runId,
|
|
28
|
+
dir,
|
|
29
|
+
promptPath: path.join(dir, "prompt.md"),
|
|
30
|
+
optionsPath: path.join(dir, "options.json"),
|
|
31
|
+
partialPath: path.join(dir, "partial.md"),
|
|
32
|
+
finalPath: path.join(dir, "final.md"),
|
|
33
|
+
metadataPath: path.join(dir, "metadata.json"),
|
|
34
|
+
resultPath: path.join(dir, "result.json"),
|
|
35
|
+
};
|
|
36
|
+
await mkdir(dir, { recursive: true });
|
|
37
|
+
await writeFile(checkpoint.promptPath, prompt, "utf8");
|
|
38
|
+
await writeFile(checkpoint.optionsPath, `${JSON.stringify(options, null, 2)}\n`, "utf8");
|
|
39
|
+
await writeFile(checkpoint.partialPath, "", "utf8");
|
|
40
|
+
await writeMetadata(checkpoint, {
|
|
41
|
+
runId,
|
|
42
|
+
resumedFrom,
|
|
43
|
+
createdAt: new Date().toISOString(),
|
|
44
|
+
updatedAt: new Date().toISOString(),
|
|
45
|
+
status: "running",
|
|
46
|
+
command,
|
|
47
|
+
});
|
|
48
|
+
return checkpoint;
|
|
49
|
+
}
|
|
50
|
+
export async function loadRunCheckpoint(workspace, runId) {
|
|
51
|
+
const dir = checkpointDir(workspace, runId);
|
|
52
|
+
const [optionsRaw, prompt, partial, metadataRaw] = await Promise.all([
|
|
53
|
+
readFile(path.join(dir, "options.json"), "utf8"),
|
|
54
|
+
readFile(path.join(dir, "prompt.md"), "utf8"),
|
|
55
|
+
readFile(path.join(dir, "partial.md"), "utf8").catch(() => ""),
|
|
56
|
+
readFile(path.join(dir, "metadata.json"), "utf8").catch(() => undefined),
|
|
57
|
+
]);
|
|
58
|
+
return {
|
|
59
|
+
runId,
|
|
60
|
+
dir,
|
|
61
|
+
options: JSON.parse(optionsRaw),
|
|
62
|
+
prompt,
|
|
63
|
+
partial,
|
|
64
|
+
metadata: metadataRaw
|
|
65
|
+
? JSON.parse(metadataRaw)
|
|
66
|
+
: undefined,
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
export async function writePartialCheckpoint(checkpoint, partial) {
|
|
70
|
+
await writeFile(checkpoint.partialPath, partial, "utf8");
|
|
71
|
+
}
|
|
72
|
+
export function writePartialCheckpointSync(checkpoint, partial, status = "interrupted", error) {
|
|
73
|
+
mkdirSync(checkpoint.dir, { recursive: true });
|
|
74
|
+
writeFileSync(checkpoint.partialPath, partial, "utf8");
|
|
75
|
+
const prior = (() => {
|
|
76
|
+
try {
|
|
77
|
+
return JSON.parse(readFileSync(checkpoint.metadataPath, "utf8"));
|
|
78
|
+
}
|
|
79
|
+
catch {
|
|
80
|
+
return {};
|
|
81
|
+
}
|
|
82
|
+
})();
|
|
83
|
+
writeFileSync(checkpoint.metadataPath, `${JSON.stringify({
|
|
84
|
+
...prior,
|
|
85
|
+
runId: checkpoint.runId,
|
|
86
|
+
updatedAt: new Date().toISOString(),
|
|
87
|
+
status,
|
|
88
|
+
error,
|
|
89
|
+
}, null, 2)}\n`, "utf8");
|
|
90
|
+
}
|
|
91
|
+
export async function writeMetadata(checkpoint, metadata) {
|
|
92
|
+
await writeFile(checkpoint.metadataPath, `${JSON.stringify({ ...metadata, updatedAt: new Date().toISOString() }, null, 2)}\n`, "utf8");
|
|
93
|
+
}
|
|
94
|
+
export async function completeRunCheckpoint(checkpoint, finalOutput, result) {
|
|
95
|
+
const usage = result.usage;
|
|
96
|
+
await writeFile(checkpoint.partialPath, finalOutput, "utf8");
|
|
97
|
+
await writeFile(checkpoint.finalPath, finalOutput, "utf8");
|
|
98
|
+
await writeFile(checkpoint.resultPath, `${JSON.stringify({
|
|
99
|
+
runId: checkpoint.runId,
|
|
100
|
+
artifactPath: result.artifactPath,
|
|
101
|
+
traceId: result.traceId,
|
|
102
|
+
usage,
|
|
103
|
+
citations: result.citations,
|
|
104
|
+
}, null, 2)}\n`, "utf8");
|
|
105
|
+
await writeMetadata(checkpoint, {
|
|
106
|
+
runId: checkpoint.runId,
|
|
107
|
+
createdAt: new Date().toISOString(),
|
|
108
|
+
updatedAt: new Date().toISOString(),
|
|
109
|
+
status: "completed",
|
|
110
|
+
command: result.command,
|
|
111
|
+
artifactPath: result.artifactPath,
|
|
112
|
+
traceId: result.traceId,
|
|
113
|
+
usage,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
export function buildResumePrompt(checkpoint) {
|
|
117
|
+
return [
|
|
118
|
+
"Resume an interrupted AI Discovery run.",
|
|
119
|
+
"",
|
|
120
|
+
"Original prompt:",
|
|
121
|
+
checkpoint.prompt.trimEnd(),
|
|
122
|
+
"",
|
|
123
|
+
"Partial Markdown already produced:",
|
|
124
|
+
checkpoint.partial.trim()
|
|
125
|
+
? checkpoint.partial.trimEnd()
|
|
126
|
+
: "(No partial Markdown was saved.)",
|
|
127
|
+
"",
|
|
128
|
+
"Resume instructions:",
|
|
129
|
+
"- Continue toward one coherent final artifact.",
|
|
130
|
+
"- Reuse any usable partial Markdown instead of repeating work unnecessarily.",
|
|
131
|
+
"- Repair obvious truncation at the resume boundary.",
|
|
132
|
+
"- Preserve the original citation, safety, validation, and reproducibility requirements.",
|
|
133
|
+
].join("\n");
|
|
134
|
+
}
|