summaryrefslogtreecommitdiff
path: root/pi/agent/extensions
diff options
context:
space:
mode:
Diffstat (limited to 'pi/agent/extensions')
-rw-r--r--pi/agent/extensions/prompt-history/index.ts43
1 files changed, 3 insertions, 40 deletions
diff --git a/pi/agent/extensions/prompt-history/index.ts b/pi/agent/extensions/prompt-history/index.ts
index 2ef7e9c..2ed39d9 100644
--- a/pi/agent/extensions/prompt-history/index.ts
+++ b/pi/agent/extensions/prompt-history/index.ts
@@ -1,12 +1,12 @@
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";
+import type { ExtensionAPI } from "@earendil-works/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).
+// Load persisted history from disk. Returns entries newest-first.
function loadHistory(): string[] {
try {
const raw = readFileSync(HISTORY_FILE, "utf8");
@@ -28,44 +28,7 @@ function saveHistory(entries: string[]): void {
}
}
-// 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();
@@ -77,4 +40,4 @@ export default function promptHistoryExtension(pi: ExtensionAPI): void {
saveHistory([text, ...current]);
});
-}
+} \ No newline at end of file