@zhin.js/agent 0.0.1 → 0.0.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/CHANGELOG.md +17 -0
- package/lib/agent.d.ts +4 -129
- package/lib/agent.d.ts.map +1 -1
- package/lib/agent.js +3 -733
- package/lib/agent.js.map +1 -1
- package/lib/compaction.d.ts +3 -129
- package/lib/compaction.d.ts.map +1 -1
- package/lib/compaction.js +2 -367
- package/lib/compaction.js.map +1 -1
- package/lib/context-manager.d.ts +3 -210
- package/lib/context-manager.d.ts.map +1 -1
- package/lib/context-manager.js +2 -310
- package/lib/context-manager.js.map +1 -1
- package/lib/conversation-memory.d.ts +3 -189
- package/lib/conversation-memory.d.ts.map +1 -1
- package/lib/conversation-memory.js +2 -616
- package/lib/conversation-memory.js.map +1 -1
- package/lib/init/create-zhin-agent.d.ts.map +1 -1
- package/lib/init/create-zhin-agent.js +1 -3
- package/lib/init/create-zhin-agent.js.map +1 -1
- package/lib/init/register-management-tools.js +3 -3
- package/lib/output.d.ts +3 -90
- package/lib/output.d.ts.map +1 -1
- package/lib/output.js +2 -173
- package/lib/output.js.map +1 -1
- package/lib/rate-limiter.d.ts +3 -35
- package/lib/rate-limiter.d.ts.map +1 -1
- package/lib/rate-limiter.js +2 -83
- package/lib/rate-limiter.js.map +1 -1
- package/lib/session.d.ts +3 -190
- package/lib/session.d.ts.map +1 -1
- package/lib/session.js +2 -462
- package/lib/session.js.map +1 -1
- package/lib/storage.d.ts +3 -65
- package/lib/storage.d.ts.map +1 -1
- package/lib/storage.js +2 -102
- package/lib/storage.js.map +1 -1
- package/lib/tone-detector.d.ts +3 -16
- package/lib/tone-detector.d.ts.map +1 -1
- package/lib/tone-detector.js +2 -69
- package/lib/tone-detector.js.map +1 -1
- package/package.json +3 -2
- package/src/agent.ts +4 -852
- package/src/compaction.ts +27 -528
- package/src/context-manager.ts +14 -439
- package/src/conversation-memory.ts +3 -814
- package/src/init/create-zhin-agent.ts +1 -3
- package/src/init/register-management-tools.ts +3 -3
- package/src/output.ts +14 -260
- package/src/rate-limiter.ts +3 -127
- package/src/session.ts +12 -565
- package/src/storage.ts +8 -134
- package/src/tone-detector.ts +3 -87
- package/tests/ai/setup.ts +20 -84
- package/tests/ai/agent.test.ts +0 -565
- package/tests/ai/context-manager.test.ts +0 -413
- package/tests/ai/conversation-memory.test.ts +0 -128
- package/tests/ai/output.test.ts +0 -128
- package/tests/ai/rate-limiter.test.ts +0 -108
- package/tests/ai/session.test.ts +0 -334
- package/tests/ai/tone-detector.test.ts +0 -80
|
@@ -1,192 +1,6 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* 两张表分离存储:
|
|
5
|
-
*
|
|
6
|
-
* ai_messages 表(逐条记录):
|
|
7
|
-
* ┌──────────────────────────────────────────────────────────┐
|
|
8
|
-
* │ session_id | role | content | round | time │
|
|
9
|
-
* │ s1 | user | 你好 | 1 | ... │
|
|
10
|
-
* │ s1 | assistant | 你好呀! | 1 | ... │
|
|
11
|
-
* │ ... | ... | ... | ... | ... │
|
|
12
|
-
* └──────────────────────────────────────────────────────────┘
|
|
13
|
-
*
|
|
14
|
-
* ai_summaries 表(链式摘要):
|
|
15
|
-
* ┌────────────────────────────────────────────────────────────────────┐
|
|
16
|
-
* │ id | session_id | parent_id | from_round | to_round | summary │
|
|
17
|
-
* │ 1 | s1 | null | 1 | 7 | 用户讨论了… │
|
|
18
|
-
* │ 2 | s1 | 1 | 8 | 15 | 之前...又… │
|
|
19
|
-
* └────────────────────────────────────────────────────────────────────┘
|
|
20
|
-
*
|
|
21
|
-
* 摘要触发规则:
|
|
22
|
-
* 1. 检测到话题切换(当前消息与最近消息的关键词重合度低)
|
|
23
|
-
* 2. 且上一个话题持续 ≥ minTopicRounds 轮
|
|
24
|
-
* → 异步生成摘要,覆盖上一话题的全部消息
|
|
25
|
-
*
|
|
26
|
-
* 上下文构建规则:
|
|
27
|
-
* 1. 取最新 summary + 最近 slidingWindowSize 轮消息(滑动窗口)
|
|
28
|
-
* 2. 检查连续性:summary.to_round === window第一条round - 1
|
|
29
|
-
* → 连续:[summary] + [window]
|
|
30
|
-
* → 不连续:丢弃 summary,仅用 [window]
|
|
2
|
+
* Re-export from @zhin.js/ai for backward compatibility.
|
|
31
3
|
*/
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
export declare const AI_MESSAGE_MODEL: {
|
|
35
|
-
session_id: {
|
|
36
|
-
type: "text";
|
|
37
|
-
nullable: boolean;
|
|
38
|
-
};
|
|
39
|
-
role: {
|
|
40
|
-
type: "text";
|
|
41
|
-
nullable: boolean;
|
|
42
|
-
};
|
|
43
|
-
content: {
|
|
44
|
-
type: "text";
|
|
45
|
-
nullable: boolean;
|
|
46
|
-
};
|
|
47
|
-
round: {
|
|
48
|
-
type: "integer";
|
|
49
|
-
nullable: boolean;
|
|
50
|
-
};
|
|
51
|
-
created_at: {
|
|
52
|
-
type: "integer";
|
|
53
|
-
default: number;
|
|
54
|
-
};
|
|
55
|
-
};
|
|
56
|
-
/** ai_summaries 表结构(链式) */
|
|
57
|
-
export declare const AI_SUMMARY_MODEL: {
|
|
58
|
-
session_id: {
|
|
59
|
-
type: "text";
|
|
60
|
-
nullable: boolean;
|
|
61
|
-
};
|
|
62
|
-
parent_id: {
|
|
63
|
-
type: "integer";
|
|
64
|
-
nullable: boolean;
|
|
65
|
-
};
|
|
66
|
-
from_round: {
|
|
67
|
-
type: "integer";
|
|
68
|
-
nullable: boolean;
|
|
69
|
-
};
|
|
70
|
-
to_round: {
|
|
71
|
-
type: "integer";
|
|
72
|
-
nullable: boolean;
|
|
73
|
-
};
|
|
74
|
-
summary: {
|
|
75
|
-
type: "text";
|
|
76
|
-
nullable: boolean;
|
|
77
|
-
};
|
|
78
|
-
created_at: {
|
|
79
|
-
type: "integer";
|
|
80
|
-
default: number;
|
|
81
|
-
};
|
|
82
|
-
};
|
|
83
|
-
/**
|
|
84
|
-
* 数据库模型接口(与 RelatedModel 的链式查询 API 对齐)
|
|
85
|
-
*
|
|
86
|
-
* select(...fields) → Selection (thenable, 支持 .where().orderBy().limit())
|
|
87
|
-
* create(data) → Promise<any>
|
|
88
|
-
*/
|
|
89
|
-
interface DbModel {
|
|
90
|
-
select(...fields: string[]): any;
|
|
91
|
-
create(data: Record<string, any>): Promise<any>;
|
|
92
|
-
}
|
|
93
|
-
export interface ConversationMemoryConfig {
|
|
94
|
-
/** 一个话题至少持续多少轮才触发摘要(默认 5) */
|
|
95
|
-
minTopicRounds?: number;
|
|
96
|
-
/** 滑动窗口大小:最近 N 轮消息(默认 5) */
|
|
97
|
-
slidingWindowSize?: number;
|
|
98
|
-
/** 话题切换检测阈值(0-1,值越低越敏感,默认 0.15) */
|
|
99
|
-
topicChangeThreshold?: number;
|
|
100
|
-
}
|
|
101
|
-
export declare class ConversationMemory {
|
|
102
|
-
private store;
|
|
103
|
-
private provider;
|
|
104
|
-
private config;
|
|
105
|
-
private summarizing;
|
|
106
|
-
/** per-session 话题跟踪 */
|
|
107
|
-
private topicStates;
|
|
108
|
-
/** per-session 轮次缓存(避免每次查数据库) */
|
|
109
|
-
private roundCache;
|
|
110
|
-
/** 各 session 最后活跃时间(用于淘汰) */
|
|
111
|
-
private lastAccess;
|
|
112
|
-
/** 内存缓存上限:超过此数量时淘汰过期条目 */
|
|
113
|
-
private static readonly MAX_TRACKED_SESSIONS;
|
|
114
|
-
/** 缓存过期时间:24 小时未访问即淘汰 */
|
|
115
|
-
private static readonly SESSION_CACHE_TTL;
|
|
116
|
-
constructor(config?: ConversationMemoryConfig);
|
|
117
|
-
setProvider(provider: AIProvider): void;
|
|
118
|
-
upgradeToDatabase(msgModel: DbModel, sumModel: DbModel): void;
|
|
119
|
-
/**
|
|
120
|
-
* 保存一轮对话,并检测话题切换来触发摘要
|
|
121
|
-
*/
|
|
122
|
-
saveRound(sessionId: string, userContent: string, assistantContent: string): Promise<void>;
|
|
123
|
-
/**
|
|
124
|
-
* 话题检测流程(全程异步,不阻塞对话):
|
|
125
|
-
*
|
|
126
|
-
* 1. 短消息(token ≤ 3)→ 跳过检测,视为延续话题
|
|
127
|
-
* 2. Jaccard 快检 → 高相似(≥ 0.5) → 肯定同话题,跳过 LLM
|
|
128
|
-
* 3. Jaccard 不确定(< 0.5) → 调 LLM 裁决
|
|
129
|
-
* 4. LLM 判定切换 + 旧话题 ≥ minTopicRounds → 触发摘要
|
|
130
|
-
*/
|
|
131
|
-
private handleTopicAndSummary;
|
|
132
|
-
/** 更新话题状态(同话题情况) */
|
|
133
|
-
private updateTopicState;
|
|
134
|
-
/**
|
|
135
|
-
* 调用 LLM 判断话题是否切换
|
|
136
|
-
*
|
|
137
|
-
* 输入: 最近几条用户消息 + 当前用户消息
|
|
138
|
-
* 输出: true = 话题切换, false = 同话题
|
|
139
|
-
*/
|
|
140
|
-
private detectTopicChangeByLLM;
|
|
141
|
-
/**
|
|
142
|
-
* 异步生成链式摘要(不阻塞对话)
|
|
143
|
-
*/
|
|
144
|
-
private generateSummaryAsync;
|
|
145
|
-
/**
|
|
146
|
-
* 构建 LLM 上下文消息列表
|
|
147
|
-
*
|
|
148
|
-
* 规则:
|
|
149
|
-
* 1. 取滑动窗口(最近 slidingWindowSize 轮)
|
|
150
|
-
* 2. 取最新 summary
|
|
151
|
-
* 3. 检查连续性:summary.to_round === window第一轮 - 1
|
|
152
|
-
* → 连续:[summary] + [window 消息]
|
|
153
|
-
* → 不连续:仅 [window 消息]
|
|
154
|
-
*/
|
|
155
|
-
buildContext(sessionId: string): Promise<ChatMessage[]>;
|
|
156
|
-
private callLLMSummarize;
|
|
157
|
-
searchMessages(sessionId: string, keyword: string, limit?: number): Promise<{
|
|
158
|
-
round: number;
|
|
159
|
-
role: string;
|
|
160
|
-
content: string;
|
|
161
|
-
time: number;
|
|
162
|
-
}[]>;
|
|
163
|
-
getMessagesByRound(sessionId: string, fromRound: number, toRound: number): Promise<{
|
|
164
|
-
round: number;
|
|
165
|
-
role: string;
|
|
166
|
-
content: string;
|
|
167
|
-
time: number;
|
|
168
|
-
}[]>;
|
|
169
|
-
getCurrentRound(sessionId: string): Promise<number>;
|
|
170
|
-
traceByKeyword(sessionId: string, keyword: string, limit?: number): Promise<{
|
|
171
|
-
summary: {
|
|
172
|
-
id: number;
|
|
173
|
-
fromRound: number;
|
|
174
|
-
toRound: number;
|
|
175
|
-
summary: string;
|
|
176
|
-
} | null;
|
|
177
|
-
messages: {
|
|
178
|
-
round: number;
|
|
179
|
-
role: string;
|
|
180
|
-
content: string;
|
|
181
|
-
time: number;
|
|
182
|
-
}[];
|
|
183
|
-
}>;
|
|
184
|
-
/**
|
|
185
|
-
* 淘汰长时间未访问的 session 缓存,防止 topicStates/roundCache 无限增长。
|
|
186
|
-
* 触发条件:Map size 超过 MAX_TRACKED_SESSIONS。
|
|
187
|
-
*/
|
|
188
|
-
private pruneStaleSessionCaches;
|
|
189
|
-
dispose(): void;
|
|
190
|
-
}
|
|
191
|
-
export {};
|
|
4
|
+
export { ConversationMemory, AI_MESSAGE_MODEL, AI_SUMMARY_MODEL } from '@zhin.js/ai';
|
|
5
|
+
export type { ConversationMemoryConfig } from '@zhin.js/ai';
|
|
192
6
|
//# sourceMappingURL=conversation-memory.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"conversation-memory.d.ts","sourceRoot":"","sources":["../src/conversation-memory.ts"],"names":[],"mappings":"AAAA
|
|
1
|
+
{"version":3,"file":"conversation-memory.d.ts","sourceRoot":"","sources":["../src/conversation-memory.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,OAAO,EAAE,kBAAkB,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAC;AACrF,YAAY,EAAE,wBAAwB,EAAE,MAAM,aAAa,CAAC"}
|