@qihoo/tuitui-openclaw-channel 1.0.15 → 1.0.16

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/histories.ts +68 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@qihoo/tuitui-openclaw-channel",
3
- "version": "1.0.15",
3
+ "version": "1.0.16",
4
4
  "maintainers": [
5
5
  {
6
6
  "name": "huzunjie",
package/src/histories.ts CHANGED
@@ -1,8 +1,70 @@
1
- import {
2
- DEFAULT_GROUP_HISTORY_LIMIT,
3
- recordPendingHistoryEntryIfEnabled,
4
- type HistoryEntry,
5
- } from "openclaw/plugin-sdk/reply-history";
1
+ const DEFAULT_GROUP_HISTORY_LIMIT = 50;
2
+ const MAX_HISTORY_KEYS = 1000;
3
+ type HistoryEntry = {
4
+ sender: string;
5
+ body: string;
6
+ timestamp?: number;
7
+ messageId?: string;
8
+ };
9
+
10
+ function evictOldHistoryKeys<T>(
11
+ historyMap: Map<string, T[]>,
12
+ maxKeys: number = MAX_HISTORY_KEYS,
13
+ ): void {
14
+ if (historyMap.size <= maxKeys) {
15
+ return;
16
+ }
17
+ const keysToDelete = historyMap.size - maxKeys;
18
+ const iterator = historyMap.keys();
19
+ for (let i = 0; i < keysToDelete; i++) {
20
+ const key = iterator.next().value;
21
+ if (key !== undefined) {
22
+ historyMap.delete(key);
23
+ }
24
+ }
25
+ }
26
+
27
+ function appendHistoryEntry<T extends HistoryEntry>(params: {
28
+ historyMap: Map<string, T[]>;
29
+ historyKey: string;
30
+ entry: T;
31
+ limit: number;
32
+ }): T[] {
33
+ const { historyMap, historyKey, entry } = params;
34
+ if (params.limit <= 0) {
35
+ return [];
36
+ }
37
+ const history = historyMap.get(historyKey) ?? [];
38
+ history.push(entry);
39
+ while (history.length > params.limit) {
40
+ history.shift();
41
+ }
42
+ if (historyMap.has(historyKey)) {
43
+ // Refresh insertion order so eviction keeps recently used histories.
44
+ historyMap.delete(historyKey);
45
+ }
46
+ historyMap.set(historyKey, history);
47
+ // Evict oldest keys if map exceeds max size to prevent unbounded memory growth
48
+ evictOldHistoryKeys(historyMap);
49
+ return history;
50
+ }
51
+
52
+ function recordPendingHistoryEntryIfEnabled<T extends HistoryEntry>(params: {
53
+ historyMap: Map<string, T[]>;
54
+ historyKey: string;
55
+ entry?: T | null;
56
+ limit: number;
57
+ }): T[] {
58
+ if (!params.entry || params.limit <= 0) {
59
+ return [];
60
+ }
61
+ return appendHistoryEntry({
62
+ historyMap: params.historyMap,
63
+ historyKey: params.historyKey,
64
+ entry: params.entry,
65
+ limit: params.limit,
66
+ });
67
+ }
6
68
 
7
69
  // 每个 accountId 独立的 group history map,防止多账户冲突
8
70
  const accountGroupHistories = new Map<string, Map<string, HistoryEntry[]>>();
@@ -10,7 +72,7 @@ const accountGroupHistories = new Map<string, Map<string, HistoryEntry[]>>();
10
72
  /**
11
73
  * 记录没有被@,被忽略的群消息到对应 account 的 pending history 中
12
74
  */
13
- export async function addUnmentionedHistory(apiRuntime, accountId: string, chatId: string, tuituiUserName: any, tuituiAccount: string, text: any, timestamp :any) {
75
+ export async function addUnmentionedHistory(apiRuntime: any, accountId: string, chatId: string, tuituiUserName: any, tuituiAccount: string, text: any, timestamp :any) {
14
76
  const cfg = await apiRuntime.config.loadConfig();
15
77
  const senderDesc = tuituiUserName ? `${tuituiUserName} (${tuituiAccount})` : tuituiAccount;
16
78