-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.ts
More file actions
56 lines (45 loc) · 1.68 KB
/
buffer.ts
File metadata and controls
56 lines (45 loc) · 1.68 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
import { MAX_BUFFER_SIZE } from "./types";
import type { BufferedMessage } from "./types";
// ── Conversation buffer (module-level, survives plugin re-registration) ──
const conversationBuffers = new Map<string, BufferedMessage[]>();
export function appendToBuffer(key: string, msg: BufferedMessage): void {
let buffer = conversationBuffers.get(key);
if (!buffer) {
buffer = [];
conversationBuffers.set(key, buffer);
}
buffer.push(msg);
if (buffer.length > MAX_BUFFER_SIZE) {
buffer.shift();
}
}
export function getBuffer(key: string): BufferedMessage[] {
return conversationBuffers.get(key) ?? [];
}
export function clearBuffer(key: string): void {
conversationBuffers.delete(key);
}
/**
* Build a consistent buffer key from sender identity.
* `from` already contains channel prefix (e.g. "telegram:5236510026").
*/
export function makeBufferKey(from?: string): string {
return from || "unknown";
}
// ── Active key tracking (module-level, survives plugin re-registration) ──
// Maps channelId → last active buffer key, so agent_end can find the right buffer.
// lastActiveKey is a fallback for when agent_end's messageProvider doesn't match any channelId.
const activeKeyMap = new Map<string, string>();
let lastActiveKey: string | null = null;
export function setActiveKey(channelId: string, bufferKey: string): void {
activeKeyMap.set(channelId, bufferKey);
lastActiveKey = bufferKey;
}
export function getActiveKey(channelId: string): string | null {
return activeKeyMap.get(channelId) ?? lastActiveKey;
}
/** Reset internal state for testing only. */
export function _resetForTesting(): void {
activeKeyMap.clear();
lastActiveKey = null;
}