forked from nicobailon/pi-subagents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathartifacts.ts
More file actions
92 lines (75 loc) · 2.67 KB
/
artifacts.ts
File metadata and controls
92 lines (75 loc) · 2.67 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
import * as fs from "node:fs";
import * as os from "node:os";
import * as path from "node:path";
import type { ArtifactPaths } from "./types.js";
const TEMP_ARTIFACTS_DIR = path.join(os.tmpdir(), "pi-subagent-artifacts");
const CLEANUP_MARKER_FILE = ".last-cleanup";
export function getArtifactsDir(sessionFile: string | null): string {
if (sessionFile) {
const sessionDir = path.dirname(sessionFile);
return path.join(sessionDir, "subagent-artifacts");
}
return TEMP_ARTIFACTS_DIR;
}
export function getArtifactPaths(artifactsDir: string, runId: string, agent: string, index?: number): ArtifactPaths {
const suffix = index !== undefined ? `_${index}` : "";
const safeAgent = agent.replace(/[^\w.-]/g, "_");
const base = `${runId}_${safeAgent}${suffix}`;
return {
inputPath: path.join(artifactsDir, `${base}_input.md`),
outputPath: path.join(artifactsDir, `${base}_output.md`),
jsonlPath: path.join(artifactsDir, `${base}.jsonl`),
metadataPath: path.join(artifactsDir, `${base}_meta.json`),
};
}
export function ensureArtifactsDir(dir: string): void {
fs.mkdirSync(dir, { recursive: true });
}
export function writeArtifact(filePath: string, content: string): void {
fs.writeFileSync(filePath, content, "utf-8");
}
export function writeMetadata(filePath: string, metadata: object): void {
fs.writeFileSync(filePath, JSON.stringify(metadata, null, 2), "utf-8");
}
export function appendJsonl(filePath: string, line: string): void {
fs.appendFileSync(filePath, `${line}\n`);
}
export function cleanupOldArtifacts(dir: string, maxAgeDays: number): void {
if (!fs.existsSync(dir)) return;
const markerPath = path.join(dir, CLEANUP_MARKER_FILE);
const now = Date.now();
if (fs.existsSync(markerPath)) {
const stat = fs.statSync(markerPath);
if (now - stat.mtimeMs < 24 * 60 * 60 * 1000) return;
}
const maxAgeMs = maxAgeDays * 24 * 60 * 60 * 1000;
const cutoff = now - maxAgeMs;
for (const file of fs.readdirSync(dir)) {
if (file === CLEANUP_MARKER_FILE) continue;
const filePath = path.join(dir, file);
try {
const stat = fs.statSync(filePath);
if (stat.mtimeMs < cutoff) {
fs.unlinkSync(filePath);
}
} catch {}
}
fs.writeFileSync(markerPath, String(now));
}
export function cleanupAllArtifactDirs(maxAgeDays: number): void {
cleanupOldArtifacts(TEMP_ARTIFACTS_DIR, maxAgeDays);
const sessionsBase = path.join(os.homedir(), ".pi", "agent", "sessions");
if (!fs.existsSync(sessionsBase)) return;
let dirs: string[];
try {
dirs = fs.readdirSync(sessionsBase);
} catch {
return;
}
for (const dir of dirs) {
const artifactsDir = path.join(sessionsBase, dir, "subagent-artifacts");
try {
cleanupOldArtifacts(artifactsDir, maxAgeDays);
} catch {}
}
}