forked from lioensky/VCPToolBox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsemanticModelRouter.js
More file actions
508 lines (432 loc) · 16.5 KB
/
Copy pathsemanticModelRouter.js
File metadata and controls
508 lines (432 loc) · 16.5 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
// modules/semanticModelRouter.js
const fs = require('fs').promises;
const fsSync = require('fs');
const path = require('path');
const {
extractTextFromMessageContent,
findLastRealUserMessage
} = require('./messageProcessor.js');
const DEFAULT_CONFIG = {
enabled: true,
autoModelName: 'VCPModelAuto',
defaultPreset: 'default',
matchThreshold: 0.18,
contextWeights: [0.7, 0.3],
presets: {
default: {
displayName: 'VCPModelAuto',
defaultModel: '',
fallbackModels: [],
routes: []
}
}
};
function isPlainObject(value) {
return value && typeof value === 'object' && !Array.isArray(value);
}
function asNonEmptyString(value, fallback = '') {
return typeof value === 'string' && value.trim() ? value.trim() : fallback;
}
function uniqueStrings(values) {
const seen = new Set();
const result = [];
for (const value of Array.isArray(values) ? values : []) {
const item = asNonEmptyString(value);
if (!item || seen.has(item)) continue;
seen.add(item);
result.push(item);
}
return result;
}
function cosineSimilarity(vectorA, vectorB) {
if (!Array.isArray(vectorA) && !(vectorA instanceof Float32Array)) return 0;
if (!Array.isArray(vectorB) && !(vectorB instanceof Float32Array)) return 0;
const len = Math.min(vectorA.length, vectorB.length);
if (len <= 0) return 0;
let dotProduct = 0;
let normA = 0;
let normB = 0;
for (let i = 0; i < len; i++) {
const a = Number(vectorA[i]) || 0;
const b = Number(vectorB[i]) || 0;
dotProduct += a * b;
normA += a * a;
normB += b * b;
}
return normA === 0 || normB === 0
? 0
: dotProduct / (Math.sqrt(normA) * Math.sqrt(normB));
}
function findLastMessageText(messages, role, ragPlugin = null) {
if (!Array.isArray(messages)) return '';
if (role === 'user') {
const lastUserMessage = findLastRealUserMessage(messages, {
sanitize: ragPlugin && typeof ragPlugin.sanitizeForEmbedding === 'function'
? ragPlugin.sanitizeForEmbedding.bind(ragPlugin)
: null
});
return lastUserMessage.sanitizedContent || '';
}
for (let i = messages.length - 1; i >= 0; i--) {
const message = messages[i];
if (!message || message.role !== role) continue;
const text = extractTextFromMessageContent(message.content).trim();
if (!text) continue;
return text;
}
return '';
}
class SemanticModelRouter {
constructor() {
this.configPath = path.join(process.cwd(), 'SemanticModelRouter.json');
this.config = JSON.parse(JSON.stringify(DEFAULT_CONFIG));
this.debugMode = false;
this.watchHandle = null;
this.reloadTimer = null;
this.descriptionVectorCache = new Map();
}
setDebugMode(debugMode) {
this.debugMode = !!debugMode;
}
async initialize(configPath = null, debugMode = false) {
this.setDebugMode(debugMode);
this.configPath = configPath || this.configPath;
await this.loadConfig();
this.startWatcher();
}
async ensureConfigFile() {
try {
await fs.access(this.configPath);
} catch (error) {
if (error.code !== 'ENOENT') throw error;
const exampleConfig = {
enabled: true,
autoModelName: 'VCPModelAuto',
defaultPreset: 'default',
matchThreshold: 0.18,
contextWeights: [0.7, 0.3],
presets: {
default: {
displayName: 'VCPModelAuto',
defaultModel: '请填写默认模型ID',
fallbackModels: [
'请填写容灾备用模型ID-1',
'请填写容灾备用模型ID-2'
],
routes: [
{
name: 'coding',
model: '请填写代码模型ID',
description: '编程、代码修改、调试、架构设计、软件工程任务'
},
{
name: 'creative',
model: '请填写创作模型ID',
description: '文学创作、角色扮演、剧情续写、情感表达、长文本润色'
}
]
}
}
};
await fs.writeFile(this.configPath, JSON.stringify(exampleConfig, null, 2), 'utf-8');
console.log(`[SemanticModelRouter] 未找到配置文件,已创建示例配置: ${this.configPath}`);
}
}
normalizeConfig(rawConfig) {
const normalized = {
...DEFAULT_CONFIG,
...(isPlainObject(rawConfig) ? rawConfig : {})
};
normalized.enabled = normalized.enabled !== false;
normalized.autoModelName = asNonEmptyString(normalized.autoModelName, DEFAULT_CONFIG.autoModelName);
normalized.defaultPreset = asNonEmptyString(normalized.defaultPreset, DEFAULT_CONFIG.defaultPreset);
normalized.matchThreshold = Number.isFinite(Number(normalized.matchThreshold))
? Number(normalized.matchThreshold)
: DEFAULT_CONFIG.matchThreshold;
normalized.contextWeights = Array.isArray(normalized.contextWeights) && normalized.contextWeights.length > 0
? normalized.contextWeights.map(value => Number(value)).filter(value => Number.isFinite(value) && value >= 0)
: DEFAULT_CONFIG.contextWeights;
if (normalized.contextWeights.length === 0) {
normalized.contextWeights = DEFAULT_CONFIG.contextWeights;
}
const rawPresets = isPlainObject(normalized.presets) ? normalized.presets : DEFAULT_CONFIG.presets;
normalized.presets = {};
for (const [presetName, preset] of Object.entries(rawPresets)) {
if (!isPlainObject(preset)) continue;
const safeName = asNonEmptyString(presetName);
if (!safeName) continue;
const routes = Array.isArray(preset.routes)
? preset.routes
.filter(route => isPlainObject(route))
.map(route => ({
name: asNonEmptyString(route.name, route.model || 'unnamed'),
model: asNonEmptyString(route.model),
description: asNonEmptyString(route.description),
// failoverPool: 当此模型被语义命中后失败时,是否允许把其他 routes 也作为容灾尝试,
// 以及它自身是否会被列为其他模型失败时的容灾候选。默认 true。
// 设为 false 表示该模型只在语义命中时使用,命中后失败直接走 defaultModel + fallbackModels。
failoverPool: route.failoverPool !== false,
enabled: route.enabled !== false
}))
.filter(route => route.model && route.description && route.enabled)
: [];
normalized.presets[safeName] = {
displayName: asNonEmptyString(preset.displayName, safeName === normalized.defaultPreset ? normalized.autoModelName : safeName),
defaultModel: asNonEmptyString(preset.defaultModel),
fallbackModels: uniqueStrings(preset.fallbackModels),
matchThreshold: Number.isFinite(Number(preset.matchThreshold))
? Number(preset.matchThreshold)
: normalized.matchThreshold,
contextWeights: Array.isArray(preset.contextWeights) && preset.contextWeights.length > 0
? preset.contextWeights.map(value => Number(value)).filter(value => Number.isFinite(value) && value >= 0)
: normalized.contextWeights,
routes
};
}
if (!normalized.presets[normalized.defaultPreset]) {
const firstPresetName = Object.keys(normalized.presets)[0];
if (firstPresetName) {
normalized.defaultPreset = firstPresetName;
} else {
normalized.presets.default = JSON.parse(JSON.stringify(DEFAULT_CONFIG.presets.default));
normalized.defaultPreset = DEFAULT_CONFIG.defaultPreset;
}
}
return normalized;
}
async loadConfig() {
try {
await this.ensureConfigFile();
const content = await fs.readFile(this.configPath, 'utf-8');
const rawConfig = JSON.parse(content);
this.config = this.normalizeConfig(rawConfig);
this.descriptionVectorCache.clear();
console.log(
`[SemanticModelRouter] 配置已加载: enabled=${this.config.enabled}, presets=${Object.keys(this.config.presets).length}`
);
} catch (error) {
console.error(`[SemanticModelRouter] 加载配置失败,使用内置默认配置: ${error.message}`);
this.config = this.normalizeConfig(DEFAULT_CONFIG);
this.descriptionVectorCache.clear();
}
}
startWatcher() {
if (this.watchHandle) return;
try {
this.watchHandle = fsSync.watch(this.configPath, { persistent: false }, () => {
if (this.reloadTimer) clearTimeout(this.reloadTimer);
this.reloadTimer = setTimeout(() => {
this.loadConfig().catch(error => {
console.error('[SemanticModelRouter] 热加载配置失败:', error.message);
});
}, 250);
});
console.log('[SemanticModelRouter] 已启用配置热加载。');
} catch (error) {
console.warn(`[SemanticModelRouter] 启用配置热加载失败: ${error.message}`);
}
}
getVirtualModels() {
if (!this.config.enabled) return [];
const models = new Map();
models.set(this.config.autoModelName, {
id: this.config.autoModelName,
object: 'model',
owned_by: 'vcp-semantic-router'
});
for (const [presetName, preset] of Object.entries(this.config.presets || {})) {
const publicName = presetName === this.config.defaultPreset
? this.config.autoModelName
: presetName;
models.set(publicName, {
id: publicName,
object: 'model',
owned_by: 'vcp-semantic-router',
display_name: preset.displayName || publicName
});
}
return Array.from(models.values());
}
resolvePresetName(requestedModel) {
const modelName = asNonEmptyString(requestedModel);
if (!modelName) return null;
if (modelName === this.config.autoModelName) {
return this.config.defaultPreset;
}
if (this.config.presets && this.config.presets[modelName]) {
return modelName;
}
return null;
}
isRoutingModel(requestedModel) {
return !!this.resolvePresetName(requestedModel);
}
getRagPlugin(pluginManager) {
const ragPlugin = pluginManager?.messagePreprocessors?.get('RAGDiaryPlugin');
if (!ragPlugin || typeof ragPlugin.getSingleEmbeddingCached !== 'function') {
return null;
}
return ragPlugin;
}
async getDescriptionVector(ragPlugin, description) {
const key = String(description || '').trim();
if (!key) return null;
if (this.descriptionVectorCache.has(key)) {
return this.descriptionVectorCache.get(key);
}
let vector = null;
// 复用 KnowledgeBaseManager 的持久化描述向量缓存:
// getPluginDescriptionVector() 会以 plugin_desc_hash:<sha256(description)> 写入 SQLite kv_store,
// 与工具动态折叠描述向量共享同一套持久化缓存,避免重启后重复向量化模型路由描述字段。
if (ragPlugin.vectorDBManager && typeof ragPlugin.vectorDBManager.getPluginDescriptionVector === 'function') {
vector = await ragPlugin.vectorDBManager.getPluginDescriptionVector(
key,
ragPlugin.getSingleEmbeddingCached.bind(ragPlugin)
);
} else {
vector = await ragPlugin.getSingleEmbeddingCached(key);
}
this.descriptionVectorCache.set(key, vector);
return vector;
}
async buildContextVector(messages, ragPlugin, preset) {
let userContent = findLastMessageText(messages, 'user', ragPlugin);
let aiContent = findLastMessageText(messages, 'assistant');
if (!userContent && !aiContent) return null;
if (typeof ragPlugin.sanitizeForEmbedding === 'function') {
aiContent = aiContent ? ragPlugin.sanitizeForEmbedding(aiContent, 'assistant') : '';
}
const [userVector, aiVector] = await Promise.all([
userContent ? ragPlugin.getSingleEmbeddingCached(userContent) : Promise.resolve(null),
aiContent ? ragPlugin.getSingleEmbeddingCached(aiContent) : Promise.resolve(null)
]);
const weights = Array.isArray(preset.contextWeights) && preset.contextWeights.length > 0
? preset.contextWeights
: this.config.contextWeights;
if (typeof ragPlugin._getWeightedAverageVector === 'function') {
return ragPlugin._getWeightedAverageVector([userVector, aiVector], weights);
}
return userVector || aiVector || null;
}
buildFallbackPlan(preset, rankedRoutes = []) {
const models = [];
if (rankedRoutes.length > 0) {
const primary = rankedRoutes[0];
if (primary && primary.model) models.push(primary.model);
// 只有当首选模型允许进入容灾池时,才把其他语义命中的 routes 按相似度顺序追加进容灾链
if (primary && primary.failoverPool !== false) {
for (let i = 1; i < rankedRoutes.length; i++) {
const route = rankedRoutes[i];
if (!route || !route.model) continue;
if (route.failoverPool === false) continue; // 显式声明不参与容灾的 route 跳过
models.push(route.model);
}
}
}
if (preset.defaultModel) models.push(preset.defaultModel);
models.push(...preset.fallbackModels);
return uniqueStrings(models);
}
buildDefaultPlan({ requestedModel, presetName, preset, reason }) {
const candidates = this.buildFallbackPlan(preset, []);
const selectedModel = candidates[0] || requestedModel;
return {
active: true,
requestedModel,
presetName,
selectedModel,
candidates: candidates.length > 0 ? candidates : [selectedModel],
match: null,
reason
};
}
async resolveRoute({ requestedModel, messages, pluginManager }) {
const presetName = this.resolvePresetName(requestedModel);
if (!this.config.enabled || !presetName) {
return {
active: false,
requestedModel,
presetName: null,
selectedModel: requestedModel,
candidates: [requestedModel],
match: null,
reason: 'not_routing_model'
};
}
const preset = this.config.presets[presetName];
if (!preset) {
return {
active: false,
requestedModel,
presetName: null,
selectedModel: requestedModel,
candidates: [requestedModel],
match: null,
reason: 'preset_not_found'
};
}
try {
const ragPlugin = this.getRagPlugin(pluginManager);
if (!ragPlugin) {
return this.buildDefaultPlan({
requestedModel,
presetName,
preset,
reason: 'rag_plugin_unavailable'
});
}
const contextVector = await this.buildContextVector(messages, ragPlugin, preset);
if (!contextVector) {
return this.buildDefaultPlan({
requestedModel,
presetName,
preset,
reason: 'context_embedding_unavailable'
});
}
const scoredRoutes = [];
for (const route of preset.routes || []) {
const descriptionVector = await this.getDescriptionVector(ragPlugin, route.description);
const similarity = cosineSimilarity(contextVector, descriptionVector);
scoredRoutes.push({
name: route.name,
model: route.model,
description: route.description,
failoverPool: route.failoverPool !== false,
similarity
});
}
scoredRoutes.sort((a, b) => b.similarity - a.similarity);
const threshold = Number.isFinite(Number(preset.matchThreshold))
? Number(preset.matchThreshold)
: this.config.matchThreshold;
const matchedRoutes = scoredRoutes.filter(route => route.similarity >= threshold);
const selectedRoutes = matchedRoutes.length > 0 ? matchedRoutes : [];
const candidates = this.buildFallbackPlan(preset, selectedRoutes);
const selectedModel = candidates[0] || preset.defaultModel || requestedModel;
if (this.debugMode) {
const top = scoredRoutes.slice(0, 5).map(route => `${route.name}:${route.model}:${route.similarity.toFixed(3)}`).join(', ');
console.log(`[SemanticModelRouter] preset=${presetName}, selected=${selectedModel}, threshold=${threshold}, top=[${top}]`);
}
return {
active: true,
requestedModel,
presetName,
selectedModel,
candidates: candidates.length > 0 ? candidates : [selectedModel],
match: selectedRoutes[0] || null,
rankedRoutes: scoredRoutes,
reason: selectedRoutes.length > 0 ? 'semantic_match' : 'below_threshold_default'
};
} catch (error) {
console.error('[SemanticModelRouter] 语义模型路由失败,回退默认模型:', error.message);
return this.buildDefaultPlan({
requestedModel,
presetName,
preset,
reason: `routing_error:${error.message}`
});
}
}
}
module.exports = SemanticModelRouter;