forked from openclaw/openclaw
-
Notifications
You must be signed in to change notification settings - Fork 73
Expand file tree
/
Copy pathskill-backup.ts
More file actions
54 lines (43 loc) · 1.24 KB
/
skill-backup.ts
File metadata and controls
54 lines (43 loc) · 1.24 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
const TOKEN_BUDGET = 25_000;
const PER_SKILL_LIMIT = 5_000;
interface BackupEntry {
content: string;
lastUsed: number;
seq: number;
}
export class SkillBackup {
private store = new Map<string, BackupEntry>();
private seqCounter = 0;
record(skillName: string, content: string) {
this.store.set(skillName, {
content,
lastUsed: Date.now(),
seq: this.seqCounter++,
});
}
getRestorationPayload(): Array<{ name: string; content: string }> {
if (this.store.size === 0) return [];
const sorted = [...this.store.entries()].sort(
(a, b) => b[1].lastUsed - a[1].lastUsed || b[1].seq - a[1].seq,
);
let totalTokens = 0;
const result: Array<{ name: string; content: string }> = [];
for (const [name, { content }] of sorted) {
const estimatedTokens = Math.ceil(content.length / 4);
const capped = Math.min(estimatedTokens, PER_SKILL_LIMIT);
if (totalTokens + capped > TOKEN_BUDGET) break;
result.push({
name,
content: content.slice(0, PER_SKILL_LIMIT * 4),
});
totalTokens += capped;
}
return result;
}
has(skillName: string): boolean {
return this.store.has(skillName);
}
get size(): number {
return this.store.size;
}
}