-
-
Notifications
You must be signed in to change notification settings - Fork 120
Expand file tree
/
Copy pathpersistence.ts
More file actions
113 lines (97 loc) · 2.92 KB
/
persistence.ts
File metadata and controls
113 lines (97 loc) · 2.92 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
/**
* State persistence module for DCP plugin.
* Persists pruned tool IDs across sessions so they survive OpenCode restarts.
* Storage location: ~/.local/share/opencode/storage/plugin/dcp/{sessionId}.json
*/
import * as fs from "fs/promises";
import { existsSync } from "fs";
import { homedir } from "os";
import { join } from "path";
import type { SessionState, SessionStats, Prune } from "./types"
import type { Logger } from "../logger";
export interface PersistedSessionState {
sessionName?: string;
prune: Prune
stats: SessionStats;
lastUpdated: string;
}
const STORAGE_DIR = join(
homedir(),
".local",
"share",
"opencode",
"storage",
"plugin",
"dcp"
);
async function ensureStorageDir(): Promise<void> {
if (!existsSync(STORAGE_DIR)) {
await fs.mkdir(STORAGE_DIR, { recursive: true });
}
}
function getSessionFilePath(sessionId: string): string {
return join(STORAGE_DIR, `${sessionId}.json`);
}
export async function saveSessionState(
sessionState: SessionState,
logger: Logger,
sessionName?: string
): Promise<void> {
try {
if (!sessionState.sessionId) {
return;
}
await ensureStorageDir();
const state: PersistedSessionState = {
sessionName: sessionName,
prune: sessionState.prune,
stats: sessionState.stats,
lastUpdated: new Date().toISOString()
};
const filePath = getSessionFilePath(sessionState.sessionId);
const content = JSON.stringify(state, null, 2);
await fs.writeFile(filePath, content, "utf-8");
logger.info("Saved session state to disk", {
sessionId: sessionState.sessionId,
totalTokensSaved: state.stats.totalPruneTokens
});
} catch (error: any) {
logger.error("Failed to save session state", {
sessionId: sessionState.sessionId,
error: error?.message,
});
}
}
export async function loadSessionState(
sessionId: string,
logger: Logger
): Promise<PersistedSessionState | null> {
try {
const filePath = getSessionFilePath(sessionId);
if (!existsSync(filePath)) {
return null;
}
const content = await fs.readFile(filePath, "utf-8");
const state = JSON.parse(content) as PersistedSessionState;
if (!state ||
!state.prune ||
!Array.isArray(state.prune.toolIds) ||
!state.stats
) {
logger.warn("Invalid session state file, ignoring", {
sessionId: sessionId,
});
return null;
}
logger.info("Loaded session state from disk", {
sessionId: sessionId
});
return state;
} catch (error: any) {
logger.warn("Failed to load session state", {
sessionId: sessionId,
error: error?.message,
});
return null;
}
}