@pennyclaw/idle-prune 0.1.1 → 0.1.3
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/dist/index.js +82 -8
- package/openclaw.plugin.json +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,6 +1,23 @@
|
|
|
1
1
|
// index.ts
|
|
2
2
|
import fs from "fs";
|
|
3
3
|
import path from "path";
|
|
4
|
+
const inMemoryLocks = new Map();
|
|
5
|
+
async function withFileLock(key, fn) {
|
|
6
|
+
if (inMemoryLocks.get(key)) return;
|
|
7
|
+
inMemoryLocks.set(key, true);
|
|
8
|
+
try {
|
|
9
|
+
await fn();
|
|
10
|
+
} finally {
|
|
11
|
+
inMemoryLocks.delete(key);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
function isValidSessionFile(sessionFile, sessionsDir) {
|
|
15
|
+
if (!sessionFile || !sessionsDir) return false;
|
|
16
|
+
if (!sessionFile.endsWith(".jsonl")) return false;
|
|
17
|
+
const resolved = path.resolve(sessionFile);
|
|
18
|
+
const base = path.resolve(sessionsDir) + path.sep;
|
|
19
|
+
return resolved.startsWith(base);
|
|
20
|
+
}
|
|
4
21
|
function looksLikeGroupId(from) {
|
|
5
22
|
const lower = from.toLowerCase();
|
|
6
23
|
if (lower.includes(":group:")) return "group";
|
|
@@ -59,11 +76,59 @@ async function pruneTranscript(params) {
|
|
|
59
76
|
}
|
|
60
77
|
}
|
|
61
78
|
if (!changed) return;
|
|
62
|
-
const tmp = `${params.sessionFile}.idle-prune.tmp`;
|
|
79
|
+
const tmp = `${params.sessionFile}.idle-prune.${process.pid}.${Date.now()}.${Math.random().toString(36).slice(2)}.tmp`;
|
|
63
80
|
await fs.promises.writeFile(tmp, out.join("\n"), "utf-8");
|
|
64
81
|
await fs.promises.rename(tmp, params.sessionFile);
|
|
65
82
|
params.logger.info(`idle-prune: pruned tool_result content in ${params.sessionFile}`);
|
|
66
83
|
}
|
|
84
|
+
function parseEntryTimestamp(entry) {
|
|
85
|
+
const ts = entry?.timestamp;
|
|
86
|
+
if (typeof ts === "number" && Number.isFinite(ts)) return ts < 1e12 ? ts * 1e3 : ts;
|
|
87
|
+
if (typeof ts === "string") {
|
|
88
|
+
const parsed = Date.parse(ts);
|
|
89
|
+
if (!Number.isNaN(parsed)) return parsed;
|
|
90
|
+
}
|
|
91
|
+
return void 0;
|
|
92
|
+
}
|
|
93
|
+
async function getLastMessageTimestampBefore(sessionFile, cutoffMs) {
|
|
94
|
+
if (!fs.existsSync(sessionFile)) return void 0;
|
|
95
|
+
const stat = await fs.promises.stat(sessionFile);
|
|
96
|
+
const fileSize = stat.size;
|
|
97
|
+
const maxBytes = 512 * 1024;
|
|
98
|
+
const chunkSize = 64 * 1024;
|
|
99
|
+
let bytesRead = 0;
|
|
100
|
+
let position = fileSize;
|
|
101
|
+
let buffer = "";
|
|
102
|
+
const handle = await fs.promises.open(sessionFile, "r");
|
|
103
|
+
try {
|
|
104
|
+
while (position > 0 && bytesRead < maxBytes) {
|
|
105
|
+
const readSize = Math.min(chunkSize, position);
|
|
106
|
+
position -= readSize;
|
|
107
|
+
const buf = Buffer.alloc(readSize);
|
|
108
|
+
await handle.read(buf, 0, readSize, position);
|
|
109
|
+
bytesRead += readSize;
|
|
110
|
+
buffer = buf.toString("utf-8") + buffer;
|
|
111
|
+
const lines = buffer.split("\n");
|
|
112
|
+
for (let i = lines.length - 1; i >= 0; i--) {
|
|
113
|
+
const line = lines[i];
|
|
114
|
+
if (!line || !line.trim()) continue;
|
|
115
|
+
try {
|
|
116
|
+
const entry = JSON.parse(line);
|
|
117
|
+
if (entry?.type !== "message") continue;
|
|
118
|
+
const ts = parseEntryTimestamp(entry);
|
|
119
|
+
if (typeof ts !== "number") continue;
|
|
120
|
+
if (ts < cutoffMs) return ts;
|
|
121
|
+
} catch {
|
|
122
|
+
continue;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
buffer = lines[0] || "";
|
|
126
|
+
}
|
|
127
|
+
} finally {
|
|
128
|
+
await handle.close();
|
|
129
|
+
}
|
|
130
|
+
return void 0;
|
|
131
|
+
}
|
|
67
132
|
function register(api) {
|
|
68
133
|
const pluginCfg = api.pluginConfig ?? {};
|
|
69
134
|
const idleMinutes = Number.isFinite(pluginCfg.idleMinutes) ? Number(pluginCfg.idleMinutes) : 15;
|
|
@@ -110,14 +175,23 @@ function register(api) {
|
|
|
110
175
|
if (match) sessionKey = match;
|
|
111
176
|
}
|
|
112
177
|
const entry = store[sessionKey];
|
|
113
|
-
if (!entry
|
|
178
|
+
if (!entry) return;
|
|
179
|
+
const sessionsDir = path.dirname(storePath);
|
|
180
|
+
const sessionFile = typeof entry.sessionFile === "string" && entry.sessionFile.trim() || path.join(sessionsDir, `${entry.sessionId}.jsonl`);
|
|
181
|
+
if (!isValidSessionFile(sessionFile, sessionsDir)) return;
|
|
114
182
|
const now = Date.now();
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
183
|
+
let cutoff = typeof event?.timestamp === "number" ? event.timestamp : typeof event?.timestamp === "string" ? Date.parse(event.timestamp) : now;
|
|
184
|
+
if (!Number.isFinite(cutoff)) cutoff = now;
|
|
185
|
+
if (cutoff < 1e12) cutoff *= 1e3;
|
|
186
|
+
const lastTs = await getLastMessageTimestampBefore(sessionFile, cutoff);
|
|
187
|
+
if (!lastTs) return;
|
|
188
|
+
if (now - lastTs < idleMinutes * 60 * 1e3) return;
|
|
189
|
+
await withFileLock(sessionFile, async () => {
|
|
190
|
+
await pruneTranscript({
|
|
191
|
+
sessionFile,
|
|
192
|
+
placeholder,
|
|
193
|
+
logger: api.logger
|
|
194
|
+
});
|
|
121
195
|
});
|
|
122
196
|
});
|
|
123
197
|
}
|
package/openclaw.plugin.json
CHANGED