cipher-security 2.0.3 → 2.0.5
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/bin/cipher.js +113 -18
- package/lib/bot/bot.js +1 -1
- package/lib/commands.js +1 -3
- package/lib/gateway/commands.js +125 -50
- package/lib/gateway/index.js +3 -0
- package/lib/mcp/server.js +250 -13
- package/lib/pipeline/index.js +3 -1
- package/lib/pipeline/osint.js +488 -239
- package/lib/pipeline/scanner.js +67 -3
- package/package.json +1 -1
- package/lib/complexity.js +0 -377
package/lib/pipeline/scanner.js
CHANGED
|
@@ -361,7 +361,10 @@ class NucleiRunner {
|
|
|
361
361
|
if (effTags.length) cmd.push('-tags', effTags.join(','));
|
|
362
362
|
if (effSev.length) cmd.push('-severity', effSev.join(','));
|
|
363
363
|
cmd.push('-rl', String(sp.rateLimit), '-bs', String(sp.bulkSize));
|
|
364
|
+
if (sp.concurrency) cmd.push('-c', String(sp.concurrency));
|
|
365
|
+
if (sp.requestTimeout) cmd.push('-timeout', String(sp.requestTimeout));
|
|
364
366
|
if (sp.headless) cmd.push('-headless');
|
|
367
|
+
if (sp.extraArgs) cmd.push(...sp.extraArgs);
|
|
365
368
|
|
|
366
369
|
return this._execute(cmd, target, opts.timeout ?? DEFAULT_TIMEOUT);
|
|
367
370
|
}
|
|
@@ -374,14 +377,75 @@ class NucleiRunner {
|
|
|
374
377
|
* @param {number} [opts.timeout=600]
|
|
375
378
|
* @returns {Promise<ScanResult>}
|
|
376
379
|
*/
|
|
380
|
+
/**
|
|
381
|
+
* Run Nuclei with explicit template paths (synchronous — for quick scans).
|
|
382
|
+
* Uses execFileSync to avoid Go pipe-buffering issues with spawn.
|
|
383
|
+
* @param {string} target
|
|
384
|
+
* @param {object} opts
|
|
385
|
+
* @param {string[]} [opts.templatePaths]
|
|
386
|
+
* @param {number} [opts.timeout=30]
|
|
387
|
+
* @param {number} [opts.rateLimit=150]
|
|
388
|
+
* @param {number} [opts.concurrency=25]
|
|
389
|
+
* @param {number} [opts.requestTimeout=10]
|
|
390
|
+
* @returns {Promise<ScanResult>}
|
|
391
|
+
*/
|
|
377
392
|
scanWithTemplates(target, opts = {}) {
|
|
378
393
|
if (!this.available) return Promise.resolve(this._fail(target));
|
|
379
394
|
const bin = this._resolved || this.nucleiPath;
|
|
380
|
-
const
|
|
395
|
+
const args = ['-jsonl', '-silent', '-u', target];
|
|
381
396
|
for (const tp of opts.templatePaths ?? []) {
|
|
382
|
-
|
|
397
|
+
args.push('-t', tp);
|
|
398
|
+
}
|
|
399
|
+
args.push('-rl', String(opts.rateLimit ?? 150));
|
|
400
|
+
args.push('-c', String(opts.concurrency ?? 25));
|
|
401
|
+
args.push('-timeout', String(opts.requestTimeout ?? 10));
|
|
402
|
+
|
|
403
|
+
const start = Date.now();
|
|
404
|
+
try {
|
|
405
|
+
const out = execFileSync(bin, args, {
|
|
406
|
+
encoding: 'utf-8',
|
|
407
|
+
timeout: (opts.timeout ?? 30) * 1000,
|
|
408
|
+
maxBuffer: 50 * 1024 * 1024,
|
|
409
|
+
stdio: ['pipe', 'pipe', 'pipe'],
|
|
410
|
+
});
|
|
411
|
+
const elapsed = (Date.now() - start) / 1000;
|
|
412
|
+
const findings = [];
|
|
413
|
+
for (const line of out.split('\n')) {
|
|
414
|
+
if (line.startsWith('{')) {
|
|
415
|
+
try { findings.push(Finding.fromNucleiJson(JSON.parse(line))); }
|
|
416
|
+
catch { /* skip parse errors */ }
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
const sevCounts = {};
|
|
420
|
+
for (const f of findings) sevCounts[f.severity] = (sevCounts[f.severity] || 0) + 1;
|
|
421
|
+
return Promise.resolve(new ScanResult({
|
|
422
|
+
target, findings,
|
|
423
|
+
stats: { total: findings.length, ...sevCounts },
|
|
424
|
+
command: `${bin} ${args.join(' ')}`,
|
|
425
|
+
durationSeconds: Math.round(elapsed * 100) / 100,
|
|
426
|
+
success: true,
|
|
427
|
+
}));
|
|
428
|
+
} catch (err) {
|
|
429
|
+
const elapsed = (Date.now() - start) / 1000;
|
|
430
|
+
const timedOut = err.killed || err.signal === 'SIGTERM';
|
|
431
|
+
// Even on timeout, capture any partial output
|
|
432
|
+
const out = err.stdout || '';
|
|
433
|
+
const findings = [];
|
|
434
|
+
for (const line of out.split('\n')) {
|
|
435
|
+
if (line.startsWith('{')) {
|
|
436
|
+
try { findings.push(Finding.fromNucleiJson(JSON.parse(line))); }
|
|
437
|
+
catch { /* skip */ }
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return Promise.resolve(new ScanResult({
|
|
441
|
+
target, findings,
|
|
442
|
+
stats: { total: findings.length },
|
|
443
|
+
errors: timedOut ? [`Scan timed out after ${opts.timeout ?? 30}s`] : [err.message],
|
|
444
|
+
command: `${bin} ${args.join(' ')}`,
|
|
445
|
+
durationSeconds: Math.round(elapsed * 100) / 100,
|
|
446
|
+
success: !timedOut && findings.length > 0,
|
|
447
|
+
}));
|
|
383
448
|
}
|
|
384
|
-
return this._execute(cmd, target, opts.timeout ?? DEFAULT_TIMEOUT);
|
|
385
449
|
}
|
|
386
450
|
|
|
387
451
|
/**
|
package/package.json
CHANGED
package/lib/complexity.js
DELETED
|
@@ -1,377 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2026 defconxt. All rights reserved.
|
|
2
|
-
// Licensed under AGPL-3.0 — see LICENSE file for details.
|
|
3
|
-
// CIPHER is a trademark of defconxt.
|
|
4
|
-
|
|
5
|
-
/**
|
|
6
|
-
* Smart routing complexity classifier for CIPHER CLI.
|
|
7
|
-
*
|
|
8
|
-
* Evaluates multiple heuristic signals to determine query complexity and
|
|
9
|
-
* route to the appropriate backend (Ollama for simple/moderate, Claude for
|
|
10
|
-
* complex/expert).
|
|
11
|
-
*
|
|
12
|
-
* Ported from src/gateway/complexity.py — all keyword dictionaries, regex
|
|
13
|
-
* patterns, score thresholds, and scoring logic are identical.
|
|
14
|
-
*
|
|
15
|
-
* @example
|
|
16
|
-
* import { classify, routeBackend, classifyAndRoute, COMPLEXITY } from './complexity.js';
|
|
17
|
-
*
|
|
18
|
-
* const level = classify("design a zero trust architecture for AWS");
|
|
19
|
-
* const backend = routeBackend(level); // "claude"
|
|
20
|
-
*
|
|
21
|
-
* const [lvl, be] = classifyAndRoute("what port does SSH use");
|
|
22
|
-
* // lvl === 1 (SIMPLE), be === "ollama"
|
|
23
|
-
*/
|
|
24
|
-
|
|
25
|
-
// ---------------------------------------------------------------------------
|
|
26
|
-
// Complexity enum
|
|
27
|
-
// ---------------------------------------------------------------------------
|
|
28
|
-
|
|
29
|
-
/** Query complexity levels, ordered by increasing difficulty. */
|
|
30
|
-
export const COMPLEXITY = Object.freeze({
|
|
31
|
-
SIMPLE: 1, // Factual lookups, single-concept questions → Ollama
|
|
32
|
-
MODERATE: 2, // Multi-part but bounded questions → Ollama
|
|
33
|
-
COMPLEX: 3, // Deep reasoning, threat models, architecture → Claude
|
|
34
|
-
EXPERT: 4, // Cross-domain, multi-framework, advanced analysis → Claude
|
|
35
|
-
});
|
|
36
|
-
|
|
37
|
-
const _NAMES = Object.freeze({
|
|
38
|
-
[COMPLEXITY.SIMPLE]: 'SIMPLE',
|
|
39
|
-
[COMPLEXITY.MODERATE]: 'MODERATE',
|
|
40
|
-
[COMPLEXITY.COMPLEX]: 'COMPLEX',
|
|
41
|
-
[COMPLEXITY.EXPERT]: 'EXPERT',
|
|
42
|
-
});
|
|
43
|
-
|
|
44
|
-
/**
|
|
45
|
-
* Reverse lookup: complexity level integer → human-readable name.
|
|
46
|
-
* @param {number} level
|
|
47
|
-
* @returns {string}
|
|
48
|
-
*/
|
|
49
|
-
export function nameOf(level) {
|
|
50
|
-
return _NAMES[level] ?? 'UNKNOWN';
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// ---------------------------------------------------------------------------
|
|
54
|
-
// Tunable weights — adjust these to change routing sensitivity
|
|
55
|
-
// ---------------------------------------------------------------------------
|
|
56
|
-
|
|
57
|
-
/** Points thresholds for each complexity level. */
|
|
58
|
-
export const THRESHOLDS = Object.freeze({
|
|
59
|
-
moderate: 3, // >= 3 points → MODERATE
|
|
60
|
-
complex: 6, // >= 6 points → COMPLEX
|
|
61
|
-
expert: 10, // >= 10 points → EXPERT
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
/** Message length breakpoints (characters) and their point values. */
|
|
65
|
-
export const LENGTH_SCORES = Object.freeze([
|
|
66
|
-
[500, 4], // Very long queries are likely complex
|
|
67
|
-
[300, 3], // Long queries
|
|
68
|
-
[150, 2], // Medium queries
|
|
69
|
-
[80, 1], // Short but not trivial
|
|
70
|
-
]);
|
|
71
|
-
|
|
72
|
-
// ---------------------------------------------------------------------------
|
|
73
|
-
// Keyword sets — each match adds the specified points
|
|
74
|
-
// ---------------------------------------------------------------------------
|
|
75
|
-
|
|
76
|
-
/** Simple indicators (negative points — pull toward Ollama). */
|
|
77
|
-
export const SIMPLE_KEYWORDS = Object.freeze({
|
|
78
|
-
'what is': -2,
|
|
79
|
-
'what are': -1,
|
|
80
|
-
'what does': -2,
|
|
81
|
-
'what port': -3,
|
|
82
|
-
'which port': -3,
|
|
83
|
-
'default port': -3,
|
|
84
|
-
'how to install': -2,
|
|
85
|
-
'how to start': -2,
|
|
86
|
-
'how to run': -2,
|
|
87
|
-
'syntax for': -2,
|
|
88
|
-
'command for': -2,
|
|
89
|
-
'define ': -2,
|
|
90
|
-
'definition of': -2,
|
|
91
|
-
'meaning of': -2,
|
|
92
|
-
'example of': -1,
|
|
93
|
-
'list of': -1,
|
|
94
|
-
'difference between': -1,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
/** Domain complexity indicators (positive points — push toward Claude). */
|
|
98
|
-
export const COMPLEX_KEYWORDS = Object.freeze({
|
|
99
|
-
// Architecture & design
|
|
100
|
-
'threat model': 4,
|
|
101
|
-
'architecture': 3,
|
|
102
|
-
'design': 2,
|
|
103
|
-
'zero trust': 3,
|
|
104
|
-
'blast radius': 3,
|
|
105
|
-
'compensating control': 3,
|
|
106
|
-
'defense in depth': 2,
|
|
107
|
-
'security architecture': 4,
|
|
108
|
-
'data flow diagram': 3,
|
|
109
|
-
'trust boundary': 3,
|
|
110
|
-
// Risk & compliance
|
|
111
|
-
'dpia': 4,
|
|
112
|
-
'risk assessment': 4,
|
|
113
|
-
'risk analysis': 3,
|
|
114
|
-
'compliance mapping': 4,
|
|
115
|
-
'gap analysis': 3,
|
|
116
|
-
'maturity assessment': 3,
|
|
117
|
-
'audit finding': 3,
|
|
118
|
-
'control mapping': 3,
|
|
119
|
-
// Analysis & reasoning
|
|
120
|
-
'analyze': 2,
|
|
121
|
-
'analyse': 2,
|
|
122
|
-
'evaluate': 2,
|
|
123
|
-
'compare and contrast': 3,
|
|
124
|
-
'trade-off': 2,
|
|
125
|
-
'tradeoff': 2,
|
|
126
|
-
'pros and cons': 2,
|
|
127
|
-
'impact analysis': 3,
|
|
128
|
-
'root cause': 3,
|
|
129
|
-
// Offensive / red team
|
|
130
|
-
'attack chain': 4,
|
|
131
|
-
'attack path': 3,
|
|
132
|
-
'kill chain': 3,
|
|
133
|
-
'exploitation chain': 4,
|
|
134
|
-
'lateral movement': 2,
|
|
135
|
-
'privilege escalation': 2,
|
|
136
|
-
'privesc': 2,
|
|
137
|
-
'post-exploitation': 3,
|
|
138
|
-
'evasion technique': 3,
|
|
139
|
-
'bypass': 1,
|
|
140
|
-
// Incident response
|
|
141
|
-
'incident analysis': 4,
|
|
142
|
-
'incident': 2,
|
|
143
|
-
'forensic analysis': 4,
|
|
144
|
-
'forensic': 2,
|
|
145
|
-
'timeline reconstruction': 4,
|
|
146
|
-
'indicator of compromise': 2,
|
|
147
|
-
'ioc': 1,
|
|
148
|
-
'beacon': 2,
|
|
149
|
-
'cobalt strike': 3,
|
|
150
|
-
'mimikatz': 2,
|
|
151
|
-
'dcsync': 3,
|
|
152
|
-
'triage': 2,
|
|
153
|
-
'containment strategy': 3,
|
|
154
|
-
'eradication plan': 3,
|
|
155
|
-
'breach': 2,
|
|
156
|
-
'malware analysis': 3,
|
|
157
|
-
'reverse engineer': 3,
|
|
158
|
-
// Detection engineering
|
|
159
|
-
'detection logic': 3,
|
|
160
|
-
'sigma rule': 2,
|
|
161
|
-
'detection coverage': 3,
|
|
162
|
-
'false positive': 2,
|
|
163
|
-
'detection gap': 3,
|
|
164
|
-
'correlation rule': 3,
|
|
165
|
-
'hunting hypothesis': 3,
|
|
166
|
-
// Multi-step / planning
|
|
167
|
-
'strategy': 2,
|
|
168
|
-
'roadmap': 3,
|
|
169
|
-
'implementation plan': 3,
|
|
170
|
-
'step by step': 1,
|
|
171
|
-
'runbook': 2,
|
|
172
|
-
'playbook': 2,
|
|
173
|
-
'workflow': 1,
|
|
174
|
-
});
|
|
175
|
-
|
|
176
|
-
/** Framework references — signal expert-level queries. */
|
|
177
|
-
export const FRAMEWORK_KEYWORDS = Object.freeze({
|
|
178
|
-
'nist': 2,
|
|
179
|
-
'nist 800-53': 3,
|
|
180
|
-
'nist 800-171': 3,
|
|
181
|
-
'nist csf': 3,
|
|
182
|
-
'mitre att&ck': 3,
|
|
183
|
-
'mitre attack': 3,
|
|
184
|
-
'stride': 3,
|
|
185
|
-
'pasta': 3,
|
|
186
|
-
'dread': 3,
|
|
187
|
-
'owasp': 2,
|
|
188
|
-
'owasp top 10': 2,
|
|
189
|
-
'cis controls': 2,
|
|
190
|
-
'cis benchmark': 2,
|
|
191
|
-
'iso 27001': 3,
|
|
192
|
-
'iso 27002': 3,
|
|
193
|
-
'soc 2': 2,
|
|
194
|
-
'pci dss': 3,
|
|
195
|
-
'hipaa': 2,
|
|
196
|
-
'gdpr': 2,
|
|
197
|
-
'ccpa': 2,
|
|
198
|
-
'fedramp': 3,
|
|
199
|
-
'cmmc': 3,
|
|
200
|
-
'cve-': 1,
|
|
201
|
-
'cwe-': 1,
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// ---------------------------------------------------------------------------
|
|
205
|
-
// Structural patterns — regex-based signals
|
|
206
|
-
// ---------------------------------------------------------------------------
|
|
207
|
-
|
|
208
|
-
/** Multi-part query indicators (each match adds points). */
|
|
209
|
-
export const STRUCTURE_PATTERNS = Object.freeze([
|
|
210
|
-
// Conjunctions joining distinct requests
|
|
211
|
-
[/\b(?:and also|and then|additionally|furthermore|moreover)\b/, 2],
|
|
212
|
-
// Numbered lists in the query
|
|
213
|
-
[/(?:^|\n)\s*\d+[.)]\s/, 2],
|
|
214
|
-
// Conditional logic
|
|
215
|
-
[/\bif\s+.+\bthen\b/, 2],
|
|
216
|
-
[/\bwhat if\b/, 2],
|
|
217
|
-
[/\bassuming\b/, 1],
|
|
218
|
-
[/\bgiven that\b/, 1],
|
|
219
|
-
// Comparison requests
|
|
220
|
-
[/\bvs\.?\b/, 1],
|
|
221
|
-
[/\bversus\b/, 1],
|
|
222
|
-
[/\bcompare\b/, 2],
|
|
223
|
-
// Scope amplifiers
|
|
224
|
-
[/\b(?:comprehensive|exhaustive|thorough|detailed|in-depth|end-to-end)\b/, 2],
|
|
225
|
-
[/\b(?:enterprise|organization-wide|company-wide|across all)\b/, 2],
|
|
226
|
-
// Multiple question marks (multiple questions)
|
|
227
|
-
[/\?.*\?/, 2],
|
|
228
|
-
]);
|
|
229
|
-
|
|
230
|
-
/** Count of "and" conjunctions — many ands suggest multi-part queries. */
|
|
231
|
-
export const AND_CONJUNCTION_THRESHOLD = 3;
|
|
232
|
-
export const AND_CONJUNCTION_POINTS = 2;
|
|
233
|
-
|
|
234
|
-
// ---------------------------------------------------------------------------
|
|
235
|
-
// Scoring functions
|
|
236
|
-
// ---------------------------------------------------------------------------
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Score based on message length.
|
|
240
|
-
* @param {string} message
|
|
241
|
-
* @returns {number}
|
|
242
|
-
*/
|
|
243
|
-
export function scoreLength(message) {
|
|
244
|
-
const length = message.length;
|
|
245
|
-
for (const [threshold, points] of LENGTH_SCORES) {
|
|
246
|
-
if (length >= threshold) {
|
|
247
|
-
return points;
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
return 0;
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Score based on keyword presence. Returns sum of matched keyword points.
|
|
255
|
-
* @param {string} lower — lowercased message
|
|
256
|
-
* @param {Record<string, number>} keywordSet
|
|
257
|
-
* @returns {number}
|
|
258
|
-
*/
|
|
259
|
-
export function scoreKeywords(lower, keywordSet) {
|
|
260
|
-
let total = 0;
|
|
261
|
-
for (const [keyword, points] of Object.entries(keywordSet)) {
|
|
262
|
-
if (lower.includes(keyword)) {
|
|
263
|
-
total += points;
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
return total;
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
/**
|
|
270
|
-
* Score based on query structure patterns.
|
|
271
|
-
* @param {string} lower — lowercased message
|
|
272
|
-
* @returns {number}
|
|
273
|
-
*/
|
|
274
|
-
export function scoreStructure(lower) {
|
|
275
|
-
let total = 0;
|
|
276
|
-
for (const [pattern, points] of STRUCTURE_PATTERNS) {
|
|
277
|
-
if (pattern.test(lower)) {
|
|
278
|
-
total += points;
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
|
|
282
|
-
// Count "and" conjunctions
|
|
283
|
-
const andMatches = lower.match(/\band\b/g);
|
|
284
|
-
const andCount = andMatches ? andMatches.length : 0;
|
|
285
|
-
if (andCount > AND_CONJUNCTION_THRESHOLD) {
|
|
286
|
-
total += AND_CONJUNCTION_POINTS;
|
|
287
|
-
}
|
|
288
|
-
|
|
289
|
-
return total;
|
|
290
|
-
}
|
|
291
|
-
|
|
292
|
-
/**
|
|
293
|
-
* Bonus points when multiple frameworks are referenced.
|
|
294
|
-
* @param {string} lower — lowercased message
|
|
295
|
-
* @returns {number}
|
|
296
|
-
*/
|
|
297
|
-
export function scoreFrameworkDensity(lower) {
|
|
298
|
-
let matches = 0;
|
|
299
|
-
let totalPoints = 0;
|
|
300
|
-
for (const [keyword, points] of Object.entries(FRAMEWORK_KEYWORDS)) {
|
|
301
|
-
if (lower.includes(keyword)) {
|
|
302
|
-
matches += 1;
|
|
303
|
-
totalPoints += points;
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
|
|
307
|
-
// Bonus for cross-framework queries (referencing 3+ frameworks)
|
|
308
|
-
if (matches >= 3) {
|
|
309
|
-
totalPoints += 3;
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
return totalPoints;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// ---------------------------------------------------------------------------
|
|
316
|
-
// Public API
|
|
317
|
-
// ---------------------------------------------------------------------------
|
|
318
|
-
|
|
319
|
-
/**
|
|
320
|
-
* Classify a query's complexity based on multiple heuristic signals.
|
|
321
|
-
* @param {string} message — The user's query string.
|
|
322
|
-
* @returns {number} Complexity level (1=SIMPLE, 2=MODERATE, 3=COMPLEX, 4=EXPERT).
|
|
323
|
-
*/
|
|
324
|
-
export function classify(message) {
|
|
325
|
-
const lower = message.toLowerCase();
|
|
326
|
-
|
|
327
|
-
let score = 0;
|
|
328
|
-
score += scoreLength(message);
|
|
329
|
-
score += scoreKeywords(lower, SIMPLE_KEYWORDS);
|
|
330
|
-
score += scoreKeywords(lower, COMPLEX_KEYWORDS);
|
|
331
|
-
score += scoreStructure(lower);
|
|
332
|
-
score += scoreFrameworkDensity(lower);
|
|
333
|
-
|
|
334
|
-
// Floor at 0 — negative scores are still SIMPLE
|
|
335
|
-
score = Math.max(score, 0);
|
|
336
|
-
|
|
337
|
-
if (score >= THRESHOLDS.expert) {
|
|
338
|
-
return COMPLEXITY.EXPERT;
|
|
339
|
-
}
|
|
340
|
-
if (score >= THRESHOLDS.complex) {
|
|
341
|
-
return COMPLEXITY.COMPLEX;
|
|
342
|
-
}
|
|
343
|
-
if (score >= THRESHOLDS.moderate) {
|
|
344
|
-
return COMPLEXITY.MODERATE;
|
|
345
|
-
}
|
|
346
|
-
return COMPLEXITY.SIMPLE;
|
|
347
|
-
}
|
|
348
|
-
|
|
349
|
-
/**
|
|
350
|
-
* Map a complexity level to a backend name.
|
|
351
|
-
*
|
|
352
|
-
* COMPLEX and EXPERT → "claude", SIMPLE and MODERATE → "ollama".
|
|
353
|
-
* The "litellm" backend is user-configured, not auto-routed.
|
|
354
|
-
*
|
|
355
|
-
* @param {number} level — The classified complexity level.
|
|
356
|
-
* @returns {string} Backend string: "ollama" or "claude".
|
|
357
|
-
*/
|
|
358
|
-
export function routeBackend(level) {
|
|
359
|
-
if (level >= COMPLEXITY.COMPLEX) {
|
|
360
|
-
return 'claude';
|
|
361
|
-
}
|
|
362
|
-
return 'ollama';
|
|
363
|
-
}
|
|
364
|
-
|
|
365
|
-
/**
|
|
366
|
-
* Classify a message and return both the complexity level and backend.
|
|
367
|
-
*
|
|
368
|
-
* Convenience function combining classify() and routeBackend().
|
|
369
|
-
*
|
|
370
|
-
* @param {string} message — The user's query string.
|
|
371
|
-
* @returns {[number, string]} Tuple of [complexity level, backend name].
|
|
372
|
-
*/
|
|
373
|
-
export function classifyAndRoute(message) {
|
|
374
|
-
const level = classify(message);
|
|
375
|
-
const backend = routeBackend(level);
|
|
376
|
-
return [level, backend];
|
|
377
|
-
}
|