summaryrefslogtreecommitdiff
path: root/pi/agent/extensions/prompt-history/index.ts
blob: 2ef7e9c35b6c5e1a6076db505a58c1ad791b71e3 (plain)
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
import { readFileSync, writeFileSync, mkdirSync } from "node:fs";
import { homedir } from "node:os";
import { join } from "node:path";
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";

const HISTORY_FILE = join(homedir(), ".pi", "prompt-history.json");
const MAX_ENTRIES = 500;

// Load persisted history from disk. Returns entries newest-first (same order as editor.history).
function loadHistory(): string[] {
	try {
		const raw = readFileSync(HISTORY_FILE, "utf8");
		const parsed = JSON.parse(raw);
		return Array.isArray(parsed) ? parsed : [];
	} catch {
		return [];
	}
}

// Persist history to disk, capping at MAX_ENTRIES.
function saveHistory(entries: string[]): void {
	try {
		mkdirSync(join(homedir(), ".pi"), { recursive: true });
		const capped = entries.slice(0, MAX_ENTRIES);
		writeFileSync(HISTORY_FILE, JSON.stringify(capped, null, 2), "utf8");
	} catch {
		// Best-effort: don't crash the agent on a write failure.
	}
}

// Merge two newest-first lists, deduplicating consecutive identical entries,
// keeping at most MAX_ENTRIES total.
function mergeHistory(fresh: string[], persisted: string[]): string[] {
	const merged: string[] = [];
	const seen = new Set<string>();
	for (const entry of [...fresh, ...persisted]) {
		if (!seen.has(entry)) {
			seen.add(entry);
			merged.push(entry);
		}
		if (merged.length >= MAX_ENTRIES) break;
	}
	return merged;
}

export default function promptHistoryExtension(pi: ExtensionAPI): void {
	// Restore persisted history into the editor on every session start (including
	// after /reload-runtime). Entries are merged with whatever the editor already
	// has so in-session history is never lost.
	pi.on("session_start", async (_event, ctx) => {
		if (!ctx.hasUI) return;
		const persisted = loadHistory();
		if (persisted.length === 0) return;

		// getHistory returns the editor's current in-memory history (newest-first).
		const current = ctx.ui.getHistory();
		const merged = mergeHistory(current, persisted);

		// Re-seed the editor: add entries oldest-first so the final array order
		// (newest-first inside the editor) matches the merged list.
		for (const entry of [...merged].reverse()) {
			ctx.ui.addToHistory(entry);
		}

		// Persist the merged result so nothing accumulated in previous runs is lost.
		saveHistory(merged);
	});

	// Capture every submitted prompt and append it to the persistent history file.
	pi.on("before_agent_start", async (event, _ctx) => {
		const text = event.prompt.trim();
		if (!text) return;

		const current = loadHistory();
		// Avoid duplicating the most recent entry.
		if (current[0] === text) return;

		saveHistory([text, ...current]);
	});
}