@zhin.js/agent 0.0.20 → 0.1.2
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/README.md +5 -2
- package/lib/cron-engine.d.ts +16 -1
- package/lib/cron-engine.d.ts.map +1 -1
- package/lib/cron-engine.js +47 -13
- package/lib/cron-engine.js.map +1 -1
- package/lib/discover-skills.d.ts +3 -1
- package/lib/discover-skills.d.ts.map +1 -1
- package/lib/discover-skills.js +7 -9
- package/lib/discover-skills.js.map +1 -1
- package/lib/discover-tools.d.ts +1 -6
- package/lib/discover-tools.d.ts.map +1 -1
- package/lib/discover-tools.js +2 -6
- package/lib/discover-tools.js.map +1 -1
- package/lib/index.d.ts +2 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -2
- package/lib/index.js.map +1 -1
- package/lib/init/create-zhin-agent.d.ts.map +1 -1
- package/lib/init/create-zhin-agent.js +58 -21
- package/lib/init/create-zhin-agent.js.map +1 -1
- package/lib/init/register-ai-trigger.d.ts.map +1 -1
- package/lib/init/register-ai-trigger.js +10 -3
- package/lib/init/register-ai-trigger.js.map +1 -1
- package/lib/init/register-builtin-tools.d.ts.map +1 -1
- package/lib/init/register-builtin-tools.js +46 -14
- package/lib/init/register-builtin-tools.js.map +1 -1
- package/lib/init/register-db-models.d.ts.map +1 -1
- package/lib/init/register-db-models.js +1 -3
- package/lib/init/register-db-models.js.map +1 -1
- package/lib/init/register-db-upgrade.d.ts.map +1 -1
- package/lib/init/register-db-upgrade.js +1 -8
- package/lib/init/register-db-upgrade.js.map +1 -1
- package/lib/init/register-management-tools.d.ts.map +1 -1
- package/lib/init/register-management-tools.js +33 -20
- package/lib/init/register-management-tools.js.map +1 -1
- package/lib/service.d.ts +4 -0
- package/lib/service.d.ts.map +1 -1
- package/lib/service.js +3 -8
- package/lib/service.js.map +1 -1
- package/lib/zhin-agent/builtin-tools.d.ts +0 -2
- package/lib/zhin-agent/builtin-tools.d.ts.map +1 -1
- package/lib/zhin-agent/builtin-tools.js +0 -55
- package/lib/zhin-agent/builtin-tools.js.map +1 -1
- package/lib/zhin-agent/config.d.ts +4 -1
- package/lib/zhin-agent/config.d.ts.map +1 -1
- package/lib/zhin-agent/config.js +2 -1
- package/lib/zhin-agent/config.js.map +1 -1
- package/lib/zhin-agent/index.d.ts +11 -6
- package/lib/zhin-agent/index.d.ts.map +1 -1
- package/lib/zhin-agent/index.js +147 -81
- package/lib/zhin-agent/index.js.map +1 -1
- package/lib/zhin-agent/prompt.d.ts.map +1 -1
- package/lib/zhin-agent/prompt.js +31 -76
- package/lib/zhin-agent/prompt.js.map +1 -1
- package/lib/zhin-agent/tool-collector.d.ts.map +1 -1
- package/lib/zhin-agent/tool-collector.js +7 -7
- package/lib/zhin-agent/tool-collector.js.map +1 -1
- package/package.json +7 -4
- package/CHANGELOG.md +0 -170
- package/lib/follow-up.d.ts +0 -131
- package/lib/follow-up.d.ts.map +0 -1
- package/lib/follow-up.js +0 -265
- package/lib/follow-up.js.map +0 -1
- package/src/agent.ts +0 -6
- package/src/bootstrap.ts +0 -309
- package/src/builtin-tools.ts +0 -958
- package/src/compaction.ts +0 -28
- package/src/context-manager.ts +0 -15
- package/src/conversation-memory.ts +0 -5
- package/src/cron-engine.ts +0 -338
- package/src/discover-agents.ts +0 -138
- package/src/discover-skills.ts +0 -325
- package/src/discover-tools.ts +0 -302
- package/src/discovery-utils.ts +0 -96
- package/src/file-policy.ts +0 -333
- package/src/follow-up.ts +0 -357
- package/src/hooks.ts +0 -223
- package/src/index.ts +0 -183
- package/src/init/create-zhin-agent.ts +0 -136
- package/src/init/register-ai-service.ts +0 -53
- package/src/init/register-ai-trigger.ts +0 -253
- package/src/init/register-builtin-tools.ts +0 -308
- package/src/init/register-db-models.ts +0 -31
- package/src/init/register-db-upgrade.ts +0 -77
- package/src/init/register-management-tools.ts +0 -71
- package/src/init/register-message-recorder.ts +0 -31
- package/src/init/register-tool-service.ts +0 -9
- package/src/init/shared-refs.ts +0 -20
- package/src/init/types.ts +0 -18
- package/src/init.ts +0 -50
- package/src/output.ts +0 -15
- package/src/rate-limiter.ts +0 -5
- package/src/service.ts +0 -224
- package/src/session.ts +0 -13
- package/src/storage.ts +0 -9
- package/src/subagent.ts +0 -209
- package/src/tone-detector.ts +0 -5
- package/src/tools.ts +0 -214
- package/src/user-profile.ts +0 -182
- package/src/zhin-agent/builtin-tools.ts +0 -247
- package/src/zhin-agent/config.ts +0 -121
- package/src/zhin-agent/exec-policy.ts +0 -285
- package/src/zhin-agent/index.ts +0 -559
- package/src/zhin-agent/prompt.ts +0 -305
- package/src/zhin-agent/tool-collector.ts +0 -249
- package/tests/ai/follow-up.test.ts +0 -175
- package/tests/ai/integration.test.ts +0 -582
- package/tests/ai/multimodal.test.ts +0 -106
- package/tests/ai/setup.ts +0 -186
- package/tests/ai/subagent.test.ts +0 -270
- package/tests/ai/tools-builtin.test.ts +0 -310
- package/tests/ai/user-profile.test.ts +0 -73
- package/tests/ai/zhin-agent.test.ts +0 -306
- package/tests/exec-policy.test.ts +0 -355
- package/tests/file-policy.test.ts +0 -405
- package/tsconfig.json +0 -22
package/src/zhin-agent/index.ts
DELETED
|
@@ -1,559 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* ZhinAgent — 全局持久 AI 大脑
|
|
3
|
-
*
|
|
4
|
-
* 核心能力:
|
|
5
|
-
* 1. 全局单例,应用生命周期内常驻
|
|
6
|
-
* 2. Skill 感知:两级过滤 Skill → Tool
|
|
7
|
-
* 3. 双层记忆:per-scene(对话上下文)+ per-user(长期偏好)
|
|
8
|
-
* 4. 任务规划:复杂请求自动分解为子步骤
|
|
9
|
-
* 5. 多模态输出:结构化 OutputElement[]
|
|
10
|
-
* 6. 智能路径选择:纯闲聊走轻量路径,工具请求走完整路径
|
|
11
|
-
* 7. 用户画像:跨会话个性化记忆
|
|
12
|
-
* 8. 速率限制:防止单用户过度消耗资源
|
|
13
|
-
* 9. 流式输出:onChunk 回调实时推送部分文本
|
|
14
|
-
* 10. 情绪感知:根据用户语气调整回复风格
|
|
15
|
-
* 11. 主动跟进:schedule_followup 定时回查
|
|
16
|
-
* 12. 多模态输入:图片/音频直接传给视觉模型
|
|
17
|
-
*/
|
|
18
|
-
|
|
19
|
-
import { Logger } from '@zhin.js/core';
|
|
20
|
-
import type { Tool, ToolContext, SkillFeature, AIProvider, AgentTool, ChatMessage, ContentPart } from '@zhin.js/core';
|
|
21
|
-
import { createAgent } from '@zhin.js/ai';
|
|
22
|
-
import { SessionManager, createMemorySessionManager } from '@zhin.js/ai';
|
|
23
|
-
import type { ContextManager } from '@zhin.js/ai';
|
|
24
|
-
import { ConversationMemory } from '@zhin.js/ai';
|
|
25
|
-
import type { OutputElement } from '@zhin.js/ai';
|
|
26
|
-
import { parseOutput } from '@zhin.js/ai';
|
|
27
|
-
import { UserProfileStore } from '../user-profile.js';
|
|
28
|
-
import { RateLimiter } from '@zhin.js/ai';
|
|
29
|
-
import { detectTone } from '@zhin.js/ai';
|
|
30
|
-
import { FollowUpManager, type FollowUpSender } from '../follow-up.js';
|
|
31
|
-
import { SubagentManager, type SubagentResultSender } from '../subagent.js';
|
|
32
|
-
import {
|
|
33
|
-
pruneHistoryForContext,
|
|
34
|
-
DEFAULT_CONTEXT_TOKENS,
|
|
35
|
-
} from '@zhin.js/ai';
|
|
36
|
-
import { triggerAIHook, createAIHookEvent } from '../hooks.js';
|
|
37
|
-
|
|
38
|
-
// ── Sub-modules ─────────────────────────────────────────────────────
|
|
39
|
-
import {
|
|
40
|
-
type ZhinAgentConfig,
|
|
41
|
-
type OnChunkCallback,
|
|
42
|
-
DEFAULT_CONFIG,
|
|
43
|
-
KEYWORD_TRIGGERS,
|
|
44
|
-
} from './config.js';
|
|
45
|
-
import { applyExecPolicyToTools } from './exec-policy.js';
|
|
46
|
-
import { collectRelevantTools } from './tool-collector.js';
|
|
47
|
-
import {
|
|
48
|
-
buildEnhancedPersona,
|
|
49
|
-
buildContextHint,
|
|
50
|
-
buildRichSystemPrompt,
|
|
51
|
-
buildUserMessageWithHistory,
|
|
52
|
-
} from './prompt.js';
|
|
53
|
-
import {
|
|
54
|
-
createChatHistoryTool,
|
|
55
|
-
createUserProfileTool,
|
|
56
|
-
createScheduleFollowUpTool,
|
|
57
|
-
createSpawnTaskTool,
|
|
58
|
-
} from './builtin-tools.js';
|
|
59
|
-
|
|
60
|
-
// Re-export public types for backward compat
|
|
61
|
-
export type { ZhinAgentConfig, OnChunkCallback } from './config.js';
|
|
62
|
-
|
|
63
|
-
const logger = new Logger(null, 'ZhinAgent');
|
|
64
|
-
const now = () => performance.now();
|
|
65
|
-
|
|
66
|
-
/** Strip `<think>…</think>` blocks that some reasoning models embed in content. */
|
|
67
|
-
function stripThinkBlocks(text: string): string {
|
|
68
|
-
return text.replace(/<think>[\s\S]*?<\/think>\s*/g, '').trim();
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
// ============================================================================
|
|
72
|
-
// ZhinAgent
|
|
73
|
-
// ============================================================================
|
|
74
|
-
|
|
75
|
-
export class ZhinAgent {
|
|
76
|
-
private provider: AIProvider;
|
|
77
|
-
private config: Required<ZhinAgentConfig>;
|
|
78
|
-
private skillRegistry: SkillFeature | null = null;
|
|
79
|
-
private sessions: SessionManager;
|
|
80
|
-
private contextManager: ContextManager | null = null;
|
|
81
|
-
private memory: ConversationMemory;
|
|
82
|
-
private externalTools: Map<string, AgentTool> = new Map();
|
|
83
|
-
private userProfiles: UserProfileStore;
|
|
84
|
-
private rateLimiter: RateLimiter;
|
|
85
|
-
private followUps: FollowUpManager;
|
|
86
|
-
private subagentManager: SubagentManager | null = null;
|
|
87
|
-
private bootstrapContext: string = '';
|
|
88
|
-
private activeSkillsContext: string = '';
|
|
89
|
-
private skillsSummaryXML: string = '';
|
|
90
|
-
|
|
91
|
-
constructor(provider: AIProvider, config?: ZhinAgentConfig) {
|
|
92
|
-
this.provider = provider;
|
|
93
|
-
this.config = { ...DEFAULT_CONFIG, ...config } as Required<ZhinAgentConfig>;
|
|
94
|
-
this.sessions = createMemorySessionManager();
|
|
95
|
-
this.memory = new ConversationMemory({
|
|
96
|
-
minTopicRounds: this.config.minTopicRounds,
|
|
97
|
-
slidingWindowSize: this.config.slidingWindowSize,
|
|
98
|
-
topicChangeThreshold: this.config.topicChangeThreshold,
|
|
99
|
-
});
|
|
100
|
-
this.memory.setProvider(provider);
|
|
101
|
-
this.userProfiles = new UserProfileStore();
|
|
102
|
-
this.rateLimiter = new RateLimiter(this.config.rateLimit);
|
|
103
|
-
this.followUps = new FollowUpManager();
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
// ── DI setters ──────────────────────────────────────────────────────
|
|
107
|
-
|
|
108
|
-
setSkillRegistry(registry: SkillFeature): void {
|
|
109
|
-
this.skillRegistry = registry;
|
|
110
|
-
logger.debug(`SkillRegistry connected (${registry.size} skills)`);
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
setSessionManager(manager: SessionManager): void {
|
|
114
|
-
this.sessions.dispose();
|
|
115
|
-
this.sessions = manager;
|
|
116
|
-
}
|
|
117
|
-
|
|
118
|
-
setContextManager(manager: ContextManager): void {
|
|
119
|
-
this.contextManager = manager;
|
|
120
|
-
manager.setAIProvider(this.provider);
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
upgradeMemoryToDatabase(msgModel: any, sumModel: any): void {
|
|
124
|
-
this.memory.upgradeToDatabase(msgModel, sumModel);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
upgradeProfilesToDatabase(model: any): void {
|
|
128
|
-
this.userProfiles.upgradeToDatabase(model);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
upgradeFollowUpsToDatabase(model: any): void {
|
|
132
|
-
this.followUps.upgradeToDatabase(model);
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
setFollowUpSender(sender: FollowUpSender): void {
|
|
136
|
-
this.followUps.setSender(sender);
|
|
137
|
-
}
|
|
138
|
-
|
|
139
|
-
async restoreFollowUps(): Promise<number> {
|
|
140
|
-
return this.followUps.restore();
|
|
141
|
-
}
|
|
142
|
-
|
|
143
|
-
initSubagentManager(createTools: () => AgentTool[]): void {
|
|
144
|
-
this.subagentManager = new SubagentManager({
|
|
145
|
-
provider: this.provider,
|
|
146
|
-
workspace: process.cwd(),
|
|
147
|
-
createTools,
|
|
148
|
-
maxIterations: this.config.maxSubagentIterations,
|
|
149
|
-
execPolicyConfig: this.config,
|
|
150
|
-
});
|
|
151
|
-
logger.debug('SubagentManager initialized');
|
|
152
|
-
}
|
|
153
|
-
|
|
154
|
-
setSubagentSender(sender: SubagentResultSender): void {
|
|
155
|
-
if (this.subagentManager) {
|
|
156
|
-
this.subagentManager.setSender(sender);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
getSubagentManager(): SubagentManager | null {
|
|
161
|
-
return this.subagentManager;
|
|
162
|
-
}
|
|
163
|
-
|
|
164
|
-
getUserProfiles(): UserProfileStore {
|
|
165
|
-
return this.userProfiles;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
registerTool(tool: AgentTool): () => void {
|
|
169
|
-
this.externalTools.set(tool.name, tool);
|
|
170
|
-
return () => { this.externalTools.delete(tool.name); };
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
setBootstrapContext(context: string): void {
|
|
174
|
-
this.bootstrapContext = context;
|
|
175
|
-
logger.debug(`Bootstrap context set (${context.length} chars)`);
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
setActiveSkillsContext(content: string): void {
|
|
179
|
-
this.activeSkillsContext = content || '';
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
setSkillsSummaryXML(xml: string): void {
|
|
183
|
-
this.skillsSummaryXML = xml || '';
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
// ── Core processing ─────────────────────────────────────────────────
|
|
187
|
-
|
|
188
|
-
async process(
|
|
189
|
-
content: string,
|
|
190
|
-
context: ToolContext,
|
|
191
|
-
externalTools: Tool[] = [],
|
|
192
|
-
onChunk?: OnChunkCallback,
|
|
193
|
-
): Promise<OutputElement[]> {
|
|
194
|
-
const t0 = now();
|
|
195
|
-
const { senderId, sceneId, platform } = context;
|
|
196
|
-
const sessionId = SessionManager.generateId(platform || '', senderId || '', sceneId);
|
|
197
|
-
const userId = senderId || 'unknown';
|
|
198
|
-
|
|
199
|
-
// 0. Rate limit
|
|
200
|
-
const rateCheck = this.rateLimiter.check(userId);
|
|
201
|
-
if (!rateCheck.allowed) {
|
|
202
|
-
logger.debug(`[速率限制] 用户 ${userId} 被限制: ${rateCheck.message}`);
|
|
203
|
-
return parseOutput(rateCheck.message || '请稍后再试');
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
triggerAIHook(createAIHookEvent('message', 'received', sessionId, {
|
|
207
|
-
userId,
|
|
208
|
-
content,
|
|
209
|
-
platform: platform || '',
|
|
210
|
-
})).catch(() => {});
|
|
211
|
-
|
|
212
|
-
// 1. Collect tools
|
|
213
|
-
const tFilter = now();
|
|
214
|
-
const allTools = collectRelevantTools(content, context, externalTools, {
|
|
215
|
-
config: this.config,
|
|
216
|
-
skillRegistry: this.skillRegistry,
|
|
217
|
-
externalRegistered: this.externalTools,
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
// Inject context-aware built-in tools on keyword match
|
|
221
|
-
if (KEYWORD_TRIGGERS.chatHistory.test(content)) {
|
|
222
|
-
allTools.push(createChatHistoryTool(sessionId, this.memory));
|
|
223
|
-
}
|
|
224
|
-
if (KEYWORD_TRIGGERS.userProfile.test(content)) {
|
|
225
|
-
allTools.push(createUserProfileTool(userId, this.userProfiles));
|
|
226
|
-
}
|
|
227
|
-
if (KEYWORD_TRIGGERS.scheduleFollowUp.test(content)) {
|
|
228
|
-
allTools.push(createScheduleFollowUpTool(sessionId, context, this.followUps));
|
|
229
|
-
}
|
|
230
|
-
if (this.subagentManager && KEYWORD_TRIGGERS.spawnTask.test(content)) {
|
|
231
|
-
allTools.push(createSpawnTaskTool(context, this.subagentManager));
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
const filterMs = (now() - tFilter).toFixed(0);
|
|
235
|
-
|
|
236
|
-
// 2. History + profile
|
|
237
|
-
const tMem = now();
|
|
238
|
-
let historyMessages = await this.buildHistoryMessages(sessionId);
|
|
239
|
-
|
|
240
|
-
const contextTokens = this.config.contextTokens ?? DEFAULT_CONTEXT_TOKENS;
|
|
241
|
-
const maxHistoryShare = this.config.maxHistoryShare ?? 0.5;
|
|
242
|
-
const pruneResult = pruneHistoryForContext({
|
|
243
|
-
messages: historyMessages,
|
|
244
|
-
maxContextTokens: contextTokens,
|
|
245
|
-
maxHistoryShare,
|
|
246
|
-
});
|
|
247
|
-
historyMessages = pruneResult.messages;
|
|
248
|
-
if (pruneResult.droppedCount > 0) {
|
|
249
|
-
logger.debug(`[上下文窗口] 丢弃 ${pruneResult.droppedCount} 条历史消息 (${pruneResult.droppedTokens} tokens)`);
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
const memMs = (now() - tMem).toFixed(0);
|
|
253
|
-
|
|
254
|
-
// 2.5 Profile + tone
|
|
255
|
-
const profileSummary = await this.userProfiles.buildProfileSummary(userId);
|
|
256
|
-
const toneHint = this.config.toneAwareness ? detectTone(content).hint : '';
|
|
257
|
-
const personaEnhanced = buildEnhancedPersona(this.config, profileSummary, toneHint);
|
|
258
|
-
|
|
259
|
-
// 3. No tools → chat path
|
|
260
|
-
if (allTools.length === 0) {
|
|
261
|
-
logger.info(`[System Prompt] chat-path: ${personaEnhanced.length} chars ≈ ${Math.ceil(personaEnhanced.length / 2.5)} tokens`);
|
|
262
|
-
logger.debug(`[闲聊路径] 过滤=${filterMs}ms, 记忆=${memMs}ms (${historyMessages.length}条), 0 工具`);
|
|
263
|
-
const tLLM = now();
|
|
264
|
-
const reply = await this.streamChatWithHistory(content, personaEnhanced, historyMessages, onChunk);
|
|
265
|
-
const llmMs = (now() - tLLM).toFixed(0);
|
|
266
|
-
logger.info(`[闲聊路径] 过滤=${filterMs}ms, 记忆=${memMs}ms, LLM=${llmMs}ms, 总=${(now() - t0).toFixed(0)}ms`);
|
|
267
|
-
await this.saveToSession(sessionId, content, reply, sceneId);
|
|
268
|
-
return parseOutput(reply);
|
|
269
|
-
}
|
|
270
|
-
|
|
271
|
-
logger.debug(`[工具路径] 过滤=${filterMs}ms, 记忆=${memMs}ms, ${allTools.length} 工具 (${allTools.map(t => t.name).join(', ')})`);
|
|
272
|
-
|
|
273
|
-
// 4. Pre-executable tools
|
|
274
|
-
const preExecTools: AgentTool[] = [];
|
|
275
|
-
for (const tool of allTools) {
|
|
276
|
-
if (tool.preExecutable) preExecTools.push(tool);
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
// 5. Pre-execution
|
|
280
|
-
let preData = '';
|
|
281
|
-
if (preExecTools.length > 0) {
|
|
282
|
-
const tPre = now();
|
|
283
|
-
logger.debug(`预执行: ${preExecTools.map(t => t.name).join(', ')}`);
|
|
284
|
-
const results = await Promise.allSettled(
|
|
285
|
-
preExecTools.map(async (tool) => {
|
|
286
|
-
const result = await Promise.race([
|
|
287
|
-
tool.execute({}),
|
|
288
|
-
new Promise<never>((_, rej) =>
|
|
289
|
-
setTimeout(() => rej(new Error('超时')), this.config.preExecTimeout)),
|
|
290
|
-
]);
|
|
291
|
-
return { name: tool.name, result };
|
|
292
|
-
}),
|
|
293
|
-
);
|
|
294
|
-
for (const r of results) {
|
|
295
|
-
if (r.status === 'fulfilled') {
|
|
296
|
-
let s = typeof r.value.result === 'string' ? r.value.result : JSON.stringify(r.value.result);
|
|
297
|
-
if (s.length > 500) {
|
|
298
|
-
s = s.slice(0, 500) + `\n... (truncated, ${s.length} chars total)`;
|
|
299
|
-
}
|
|
300
|
-
preData += `\n【${r.value.name}】${s}`;
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
logger.debug(`预执行耗时: ${(now() - tPre).toFixed(0)}ms`);
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
// 6. Path selection
|
|
307
|
-
let reply: string;
|
|
308
|
-
const hasNonPreExecTools = allTools.some(t => !t.preExecutable);
|
|
309
|
-
|
|
310
|
-
if (!hasNonPreExecTools && preData) {
|
|
311
|
-
// Fast path
|
|
312
|
-
const tLLM = now();
|
|
313
|
-
const prompt = `${personaEnhanced}
|
|
314
|
-
|
|
315
|
-
Pre-fetched data (from user's question):
|
|
316
|
-
${preData}
|
|
317
|
-
|
|
318
|
-
Answer the user's question based on the data above. Be clear and concise; use emoji when appropriate.`;
|
|
319
|
-
logger.info(`[System Prompt] fast-path: ${prompt.length} chars ≈ ${Math.ceil(prompt.length / 2.5)} tokens`);
|
|
320
|
-
reply = await this.streamChatWithHistory(content, prompt, historyMessages, onChunk);
|
|
321
|
-
logger.info(`[快速路径] 过滤=${filterMs}ms, 记忆=${memMs}ms, LLM=${(now() - tLLM).toFixed(0)}ms, 总=${(now() - t0).toFixed(0)}ms`);
|
|
322
|
-
} else {
|
|
323
|
-
// Agent path
|
|
324
|
-
const tAgent = now();
|
|
325
|
-
logger.debug(`Agent 路径: ${allTools.length} 个工具`);
|
|
326
|
-
const contextHint = buildContextHint(context, content);
|
|
327
|
-
|
|
328
|
-
const richPrompt = buildRichSystemPrompt({
|
|
329
|
-
config: this.config,
|
|
330
|
-
skillRegistry: this.skillRegistry,
|
|
331
|
-
skillsSummaryXML: this.skillsSummaryXML,
|
|
332
|
-
activeSkillsContext: this.activeSkillsContext,
|
|
333
|
-
bootstrapContext: this.bootstrapContext,
|
|
334
|
-
});
|
|
335
|
-
const systemPrompt = `${richPrompt}
|
|
336
|
-
${contextHint}
|
|
337
|
-
${preData ? `\nPre-fetched data:\n${preData}\n` : ''}`;
|
|
338
|
-
|
|
339
|
-
const promptChars = systemPrompt.length;
|
|
340
|
-
const estimatedTokens = Math.ceil(promptChars / 2.5);
|
|
341
|
-
logger.info(`[System Prompt] ${promptChars} chars ≈ ${estimatedTokens} tokens`);
|
|
342
|
-
logger.debug(`[System Prompt Preview]\n${systemPrompt.slice(0, 500)}...\n---END PREVIEW---`);
|
|
343
|
-
|
|
344
|
-
const agentTools = applyExecPolicyToTools(this.config, allTools);
|
|
345
|
-
|
|
346
|
-
// Adaptive maxIterations: boost when skills are active (multi-step skill flows)
|
|
347
|
-
const SKILL_ITERATION_BOOST = 3;
|
|
348
|
-
const hasSkillActivation = agentTools.some(t => t.name === 'activate_skill' || t.name === 'install_skill');
|
|
349
|
-
const effectiveMaxIterations = hasSkillActivation
|
|
350
|
-
? this.config.maxIterations + SKILL_ITERATION_BOOST
|
|
351
|
-
: this.config.maxIterations;
|
|
352
|
-
|
|
353
|
-
const agent = createAgent(this.provider, {
|
|
354
|
-
systemPrompt,
|
|
355
|
-
tools: agentTools,
|
|
356
|
-
maxIterations: effectiveMaxIterations,
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
const userMessageWithHistory = buildUserMessageWithHistory(historyMessages, content);
|
|
360
|
-
const result = await agent.run(userMessageWithHistory, []);
|
|
361
|
-
reply = stripThinkBlocks(result.content) || this.fallbackFormat(result.toolCalls);
|
|
362
|
-
logger.info(`[Agent 路径] 过滤=${filterMs}ms, 记忆=${memMs}ms, Agent=${(now() - tAgent).toFixed(0)}ms, 总=${(now() - t0).toFixed(0)}ms`);
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
await this.saveToSession(sessionId, content, reply, sceneId);
|
|
366
|
-
|
|
367
|
-
triggerAIHook(createAIHookEvent('message', 'sent', sessionId, {
|
|
368
|
-
userId,
|
|
369
|
-
content: reply,
|
|
370
|
-
platform: platform || '',
|
|
371
|
-
})).catch(() => {});
|
|
372
|
-
|
|
373
|
-
return parseOutput(reply);
|
|
374
|
-
}
|
|
375
|
-
|
|
376
|
-
async processMultimodal(
|
|
377
|
-
parts: ContentPart[],
|
|
378
|
-
context: ToolContext,
|
|
379
|
-
onChunk?: OnChunkCallback,
|
|
380
|
-
): Promise<OutputElement[]> {
|
|
381
|
-
const { senderId, sceneId, platform } = context;
|
|
382
|
-
const sessionId = SessionManager.generateId(platform || '', senderId || '', sceneId);
|
|
383
|
-
const userId = senderId || 'unknown';
|
|
384
|
-
|
|
385
|
-
const rateCheck = this.rateLimiter.check(userId);
|
|
386
|
-
if (!rateCheck.allowed) {
|
|
387
|
-
return parseOutput(rateCheck.message || '请稍后再试');
|
|
388
|
-
}
|
|
389
|
-
|
|
390
|
-
const historyMessages = await this.buildHistoryMessages(sessionId);
|
|
391
|
-
const profileSummary = await this.userProfiles.buildProfileSummary(userId);
|
|
392
|
-
const personaEnhanced = buildEnhancedPersona(this.config, profileSummary, '');
|
|
393
|
-
|
|
394
|
-
// Build text summary describing the multimodal content
|
|
395
|
-
const textFragments: string[] = [];
|
|
396
|
-
const llmParts: ContentPart[] = [];
|
|
397
|
-
|
|
398
|
-
/** Full multimodal ContentPart union (core/ai may export a narrower type in some builds) */
|
|
399
|
-
type MultimodalPart =
|
|
400
|
-
| ContentPart
|
|
401
|
-
| { type: 'video_url'; video_url: { url: string } }
|
|
402
|
-
| { type: 'face'; face: { id: string; text?: string } };
|
|
403
|
-
|
|
404
|
-
for (const p of parts as MultimodalPart[]) {
|
|
405
|
-
switch (p.type) {
|
|
406
|
-
case 'text':
|
|
407
|
-
textFragments.push(p.text);
|
|
408
|
-
llmParts.push(p);
|
|
409
|
-
break;
|
|
410
|
-
case 'image_url':
|
|
411
|
-
textFragments.push('[图片]');
|
|
412
|
-
llmParts.push(p);
|
|
413
|
-
break;
|
|
414
|
-
case 'video_url':
|
|
415
|
-
textFragments.push('[视频]');
|
|
416
|
-
// Most LLMs don't support video natively; describe it as a URL for context
|
|
417
|
-
llmParts.push({ type: 'text', text: `[用户发送了一个视频: ${p.video_url.url}]` });
|
|
418
|
-
break;
|
|
419
|
-
case 'audio':
|
|
420
|
-
textFragments.push('[音频]');
|
|
421
|
-
llmParts.push(p);
|
|
422
|
-
break;
|
|
423
|
-
case 'face':
|
|
424
|
-
textFragments.push(p.face.text || `[表情:${p.face.id}]`);
|
|
425
|
-
llmParts.push({ type: 'text', text: p.face.text ? `[表情: ${p.face.text}]` : `[表情ID: ${p.face.id}]` });
|
|
426
|
-
break;
|
|
427
|
-
}
|
|
428
|
-
}
|
|
429
|
-
|
|
430
|
-
const textContent = textFragments.join(' ') || '[多模态消息]';
|
|
431
|
-
const visionModel = this.config.visionModel || this.provider.models[0];
|
|
432
|
-
|
|
433
|
-
const messages: ChatMessage[] = [
|
|
434
|
-
{ role: 'system', content: personaEnhanced },
|
|
435
|
-
...historyMessages,
|
|
436
|
-
{ role: 'user', content: llmParts },
|
|
437
|
-
];
|
|
438
|
-
|
|
439
|
-
let reply = '';
|
|
440
|
-
try {
|
|
441
|
-
for await (const chunk of this.provider.chatStream({ model: visionModel, messages })) {
|
|
442
|
-
const delta = chunk.choices?.[0]?.delta;
|
|
443
|
-
if (!delta) continue;
|
|
444
|
-
const text = typeof delta.content === 'string' ? delta.content : '';
|
|
445
|
-
if (text) {
|
|
446
|
-
reply += text;
|
|
447
|
-
if (onChunk) onChunk(text, reply);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
reply = stripThinkBlocks(reply);
|
|
451
|
-
if (!reply) {
|
|
452
|
-
logger.warn('[processMultimodal] 流式响应内容为空,尝试非流式回退');
|
|
453
|
-
const response = await this.provider.chat({ model: visionModel, messages });
|
|
454
|
-
const msg = response.choices[0]?.message?.content;
|
|
455
|
-
reply = stripThinkBlocks(typeof msg === 'string' ? msg : '');
|
|
456
|
-
}
|
|
457
|
-
} catch {
|
|
458
|
-
const response = await this.provider.chat({ model: visionModel, messages });
|
|
459
|
-
const msg = response.choices[0]?.message?.content;
|
|
460
|
-
reply = stripThinkBlocks(typeof msg === 'string' ? msg : '');
|
|
461
|
-
}
|
|
462
|
-
|
|
463
|
-
if (!reply) reply = '抱歉,我无法理解这条消息。';
|
|
464
|
-
await this.saveToSession(sessionId, textContent, reply, sceneId);
|
|
465
|
-
return parseOutput(reply);
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
// ── Internal helpers ────────────────────────────────────────────────
|
|
469
|
-
|
|
470
|
-
private async buildHistoryMessages(sessionId: string): Promise<ChatMessage[]> {
|
|
471
|
-
return this.memory.buildContext(sessionId);
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
private async streamChatWithHistory(
|
|
475
|
-
content: string,
|
|
476
|
-
systemPrompt: string,
|
|
477
|
-
history: ChatMessage[],
|
|
478
|
-
onChunk?: OnChunkCallback,
|
|
479
|
-
): Promise<string> {
|
|
480
|
-
const model = this.provider.models[0];
|
|
481
|
-
const userContent = history.length > 0
|
|
482
|
-
? buildUserMessageWithHistory(history, content)
|
|
483
|
-
: content;
|
|
484
|
-
const messages: ChatMessage[] = [
|
|
485
|
-
{ role: 'system', content: systemPrompt },
|
|
486
|
-
{ role: 'user', content: userContent },
|
|
487
|
-
];
|
|
488
|
-
|
|
489
|
-
try {
|
|
490
|
-
let result = '';
|
|
491
|
-
for await (const chunk of this.provider.chatStream({ model, messages })) {
|
|
492
|
-
const delta = chunk.choices?.[0]?.delta;
|
|
493
|
-
if (!delta) continue;
|
|
494
|
-
const text = typeof delta.content === 'string' ? delta.content : '';
|
|
495
|
-
if (text) {
|
|
496
|
-
result += text;
|
|
497
|
-
if (onChunk) onChunk(text, result);
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
result = stripThinkBlocks(result);
|
|
501
|
-
if (result) return result;
|
|
502
|
-
// Streaming returned empty content — fall back to non-streaming
|
|
503
|
-
logger.warn('[streamChat] 流式响应内容为空,尝试非流式回退');
|
|
504
|
-
} catch {
|
|
505
|
-
// Stream failed — fall back to non-streaming
|
|
506
|
-
}
|
|
507
|
-
const response = await this.provider.chat({ model, messages });
|
|
508
|
-
const msg = response.choices[0]?.message?.content;
|
|
509
|
-
let result = typeof msg === 'string' ? msg : '';
|
|
510
|
-
result = stripThinkBlocks(result);
|
|
511
|
-
if (onChunk && result) onChunk(result, result);
|
|
512
|
-
return result;
|
|
513
|
-
}
|
|
514
|
-
|
|
515
|
-
private async saveToSession(
|
|
516
|
-
sessionId: string,
|
|
517
|
-
userContent: string,
|
|
518
|
-
assistantContent: string,
|
|
519
|
-
sceneId?: string,
|
|
520
|
-
): Promise<void> {
|
|
521
|
-
await this.memory.saveRound(sessionId, userContent, assistantContent);
|
|
522
|
-
await this.sessions.addMessage(sessionId, { role: 'user', content: userContent });
|
|
523
|
-
await this.sessions.addMessage(sessionId, { role: 'assistant', content: assistantContent });
|
|
524
|
-
if (this.contextManager && sceneId) {
|
|
525
|
-
this.contextManager.autoSummarizeIfNeeded(sceneId).catch(() => {});
|
|
526
|
-
}
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
private fallbackFormat(toolCalls: { tool: string; args: any; result: any }[]): string {
|
|
530
|
-
if (toolCalls.length === 0) return 'Done.';
|
|
531
|
-
const userFacing = toolCalls.filter(tc => tc.tool !== 'activate_skill');
|
|
532
|
-
if (userFacing.length === 0) {
|
|
533
|
-
return '技能已激活但未能完成后续操作,请重试或换一种方式描述你的需求。';
|
|
534
|
-
}
|
|
535
|
-
return userFacing.map(tc => {
|
|
536
|
-
const s = typeof tc.result === 'string' ? tc.result : JSON.stringify(tc.result, null, 2);
|
|
537
|
-
return `【${tc.tool}】\n${s}`;
|
|
538
|
-
}).join('\n\n');
|
|
539
|
-
}
|
|
540
|
-
|
|
541
|
-
// ── Lifecycle ───────────────────────────────────────────────────────
|
|
542
|
-
|
|
543
|
-
isReady(): boolean {
|
|
544
|
-
return true;
|
|
545
|
-
}
|
|
546
|
-
|
|
547
|
-
dispose(): void {
|
|
548
|
-
this.memory.dispose();
|
|
549
|
-
this.sessions.dispose();
|
|
550
|
-
this.externalTools.clear();
|
|
551
|
-
this.userProfiles.dispose();
|
|
552
|
-
this.rateLimiter.dispose();
|
|
553
|
-
this.followUps.dispose();
|
|
554
|
-
if (this.subagentManager) {
|
|
555
|
-
this.subagentManager.dispose();
|
|
556
|
-
this.subagentManager = null;
|
|
557
|
-
}
|
|
558
|
-
}
|
|
559
|
-
}
|