monomind 1.10.57 → 1.11.1

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 (232) hide show
  1. package/.claude/agents/core/coordinator.md +71 -0
  2. package/.claude/agents/generated/case-analyst.md +50 -0
  3. package/.claude/agents/generated/copy-editor.md +45 -0
  4. package/.claude/agents/generated/court-reporter.md +50 -0
  5. package/.claude/agents/generated/defender.md +51 -0
  6. package/.claude/agents/generated/editor-in-chief.md +45 -0
  7. package/.claude/agents/generated/fact-checker.md +45 -0
  8. package/.claude/agents/generated/judge.md +51 -0
  9. package/.claude/agents/generated/prosecutor.md +51 -0
  10. package/.claude/agents/generated/reporter.md +45 -0
  11. package/.claude/commands/hooks/README.md +1 -1
  12. package/.claude/commands/hooks/overview.md +1 -1
  13. package/.claude/commands/mastermind/_repeat.md +1 -1
  14. package/.claude/commands/mastermind/do.md +3 -1
  15. package/.claude/commands/mastermind/help.md +2 -2
  16. package/.claude/commands/mastermind/master.md +39 -6
  17. package/.claude/commands/mastermind/memory.md +1 -1
  18. package/.claude/commands/memory/memory-search.md +2 -2
  19. package/.claude/commands/monitoring/status.md +1 -1
  20. package/.claude/commands/{browse.md → monobrowse.md} +2 -2
  21. package/.claude/commands/sparc.md +1 -1
  22. package/.claude/helpers/handlers/graph-status-handler.cjs +1 -1
  23. package/.claude/helpers/loop-tracker.cjs +1 -1
  24. package/.claude/scheduled_tasks.lock +1 -1
  25. package/.claude/skills/agent-browser-testing/SKILL.md +1 -1
  26. package/.claude/skills/hooks-automation/SKILL.md +0 -3
  27. package/.claude/skills/mastermind/build.md +1 -1
  28. package/.claude/skills/mastermind/code-quality-reviewer-prompt.md +60 -0
  29. package/.claude/skills/mastermind/content.md +1 -1
  30. package/.claude/skills/mastermind/createorg.md +79 -2
  31. package/.claude/skills/mastermind/design.md +3 -1
  32. package/.claude/skills/mastermind/finance.md +1 -1
  33. package/.claude/skills/mastermind/implementer-prompt.md +109 -0
  34. package/.claude/skills/mastermind/marketing.md +1 -1
  35. package/.claude/skills/mastermind/ops.md +1 -1
  36. package/.claude/skills/mastermind/plan.md +20 -2
  37. package/.claude/skills/mastermind/release.md +1 -1
  38. package/.claude/skills/mastermind/research.md +1 -1
  39. package/.claude/skills/mastermind/review.md +1 -1
  40. package/.claude/skills/mastermind/sales.md +1 -1
  41. package/.claude/skills/mastermind/spec-reviewer-prompt.md +63 -0
  42. package/.claude/skills/sparc-methodology/SKILL.md +3 -3
  43. package/.claude/skills/swarm-advanced/SKILL.md +1 -4
  44. package/.claude-plugin/README.md +1 -2
  45. package/.claude-plugin/docs/PLUGIN_SUMMARY.md +0 -1
  46. package/README.md +18 -32
  47. package/package.json +2 -2
  48. package/packages/@monomind/cli/README.md +18 -32
  49. package/packages/@monomind/cli/dist/src/agents/registry-builder.d.ts +1 -7
  50. package/packages/@monomind/cli/dist/src/agents/registry-builder.js +10 -6
  51. package/packages/@monomind/cli/dist/src/benchmarks/benchmark-runner.d.ts +59 -12
  52. package/packages/@monomind/cli/dist/src/benchmarks/benchmark-runner.js +67 -13
  53. package/packages/@monomind/cli/dist/src/benchmarks/pretrain/index.d.ts +0 -13
  54. package/packages/@monomind/cli/dist/src/commands/agent-wasm.d.ts +2 -2
  55. package/packages/@monomind/cli/dist/src/commands/agent-wasm.js +5 -5
  56. package/packages/@monomind/cli/dist/src/commands/agent.js +1 -6
  57. package/packages/@monomind/cli/dist/src/commands/analyze.d.ts +1 -1
  58. package/packages/@monomind/cli/dist/src/commands/analyze.js +8 -18
  59. package/packages/@monomind/cli/dist/src/commands/doctor.js +53 -3
  60. package/packages/@monomind/cli/dist/src/commands/embeddings.js +9 -33
  61. package/packages/@monomind/cli/dist/src/commands/hooks.js +31 -74
  62. package/packages/@monomind/cli/dist/src/commands/index.d.ts +4 -6
  63. package/packages/@monomind/cli/dist/src/commands/index.js +8 -15
  64. package/packages/@monomind/cli/dist/src/commands/init.js +2 -2
  65. package/packages/@monomind/cli/dist/src/commands/issues.js +16 -11
  66. package/packages/@monomind/cli/dist/src/commands/memory.js +6 -6
  67. package/packages/@monomind/cli/dist/src/commands/migrate.js +1 -2
  68. package/packages/@monomind/cli/dist/src/commands/monograph.js +18 -11
  69. package/packages/@monomind/cli/dist/src/commands/monovector/backup.d.ts +11 -0
  70. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/backup.js +25 -25
  71. package/packages/@monomind/cli/dist/src/commands/monovector/benchmark.d.ts +11 -0
  72. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/benchmark.js +14 -14
  73. package/packages/@monomind/cli/dist/src/commands/monovector/import.d.ts +18 -0
  74. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/import.js +21 -21
  75. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/index.d.ts +6 -6
  76. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/index.js +22 -22
  77. package/packages/@monomind/cli/dist/src/commands/monovector/init.d.ts +11 -0
  78. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/init.js +36 -36
  79. package/packages/@monomind/cli/dist/src/commands/monovector/migrate.d.ts +11 -0
  80. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/migrate.js +16 -16
  81. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/optimize.d.ts +2 -2
  82. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/optimize.js +10 -10
  83. package/packages/@monomind/cli/dist/src/commands/monovector/setup.d.ts +18 -0
  84. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/setup.js +77 -77
  85. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/status.d.ts +2 -2
  86. package/packages/@monomind/cli/dist/src/commands/{ruvector → monovector}/status.js +34 -34
  87. package/packages/@monomind/cli/dist/src/commands/neural.d.ts +1 -1
  88. package/packages/@monomind/cli/dist/src/commands/neural.js +31 -608
  89. package/packages/@monomind/cli/dist/src/commands/performance.js +7 -10
  90. package/packages/@monomind/cli/dist/src/commands/plugins.js +3 -4
  91. package/packages/@monomind/cli/dist/src/commands/process.js +5 -12
  92. package/packages/@monomind/cli/dist/src/commands/progress.js +16 -16
  93. package/packages/@monomind/cli/dist/src/commands/route.d.ts +2 -2
  94. package/packages/@monomind/cli/dist/src/commands/route.js +23 -23
  95. package/packages/@monomind/cli/dist/src/commands/status.js +0 -3
  96. package/packages/@monomind/cli/dist/src/commands/swarm.js +2 -3
  97. package/packages/@monomind/cli/dist/src/config-adapter.js +27 -0
  98. package/packages/@monomind/cli/dist/src/consensus/audit-writer.d.ts +44 -17
  99. package/packages/@monomind/cli/dist/src/dlq/dlq-replayer.d.ts +1 -1
  100. package/packages/@monomind/cli/dist/src/index.js +5 -5
  101. package/packages/@monomind/cli/dist/src/init/claudemd-generator.js +57 -37
  102. package/packages/@monomind/cli/dist/src/init/executor.js +17 -27
  103. package/packages/@monomind/cli/dist/src/init/helpers-generator.js +2 -2
  104. package/packages/@monomind/cli/dist/src/init/types.d.ts +1 -1
  105. package/packages/@monomind/cli/dist/src/mcp-client.js +1 -7
  106. package/packages/@monomind/cli/dist/src/mcp-server.js +9 -1
  107. package/packages/@monomind/cli/dist/src/mcp-tools/agent-tools.js +1 -52
  108. package/packages/@monomind/cli/dist/src/mcp-tools/analyze-tools.js +5 -5
  109. package/packages/@monomind/cli/dist/src/mcp-tools/browser-tools.d.ts +1 -1
  110. package/packages/@monomind/cli/dist/src/mcp-tools/browser-tools.js +6 -5
  111. package/packages/@monomind/cli/dist/src/mcp-tools/coordination-tools.d.ts +1 -1
  112. package/packages/@monomind/cli/dist/src/mcp-tools/coordination-tools.js +51 -54
  113. package/packages/@monomind/cli/dist/src/mcp-tools/daa-tools.d.ts +1 -1
  114. package/packages/@monomind/cli/dist/src/mcp-tools/embeddings-tools.js +10 -10
  115. package/packages/@monomind/cli/dist/src/mcp-tools/guidance-tools.js +0 -24
  116. package/packages/@monomind/cli/dist/src/mcp-tools/hooks-tools.js +189 -480
  117. package/packages/@monomind/cli/dist/src/mcp-tools/index.d.ts +0 -2
  118. package/packages/@monomind/cli/dist/src/mcp-tools/index.js +0 -2
  119. package/packages/@monomind/cli/dist/src/mcp-tools/monograph-compat.d.ts +334 -0
  120. package/packages/@monomind/cli/dist/src/mcp-tools/monograph-compat.js +1126 -0
  121. package/packages/@monomind/cli/dist/src/mcp-tools/monograph-tools.js +76 -34
  122. package/packages/@monomind/cli/dist/src/mcp-tools/neural-tools.d.ts +1 -1
  123. package/packages/@monomind/cli/dist/src/mcp-tools/neural-tools.js +2 -2
  124. package/packages/@monomind/cli/dist/src/mcp-tools/ruvllm-tools.d.ts +2 -2
  125. package/packages/@monomind/cli/dist/src/mcp-tools/ruvllm-tools.js +3 -3
  126. package/packages/@monomind/cli/dist/src/mcp-tools/terminal-tools.d.ts +1 -1
  127. package/packages/@monomind/cli/dist/src/mcp-tools/terminal-tools.js +29 -19
  128. package/packages/@monomind/cli/dist/src/mcp-tools/wasm-agent-tools.d.ts +1 -1
  129. package/packages/@monomind/cli/dist/src/mcp-tools/wasm-agent-tools.js +2 -2
  130. package/packages/@monomind/cli/dist/src/memory/intelligence.js +14 -8
  131. package/packages/@monomind/cli/dist/src/memory/memory-bridge.d.ts +17 -2
  132. package/packages/@monomind/cli/dist/src/memory/memory-bridge.js +76 -23
  133. package/packages/@monomind/cli/dist/src/memory/memory-initializer.d.ts +2 -2
  134. package/packages/@monomind/cli/dist/src/memory/memory-initializer.js +37 -39
  135. package/packages/@monomind/cli/dist/src/monovector/capabilities.d.ts +34 -0
  136. package/packages/@monomind/cli/dist/src/monovector/capabilities.js +37 -0
  137. package/packages/@monomind/cli/dist/src/monovector/command-outcomes.d.ts +37 -0
  138. package/packages/@monomind/cli/dist/src/monovector/command-outcomes.js +87 -0
  139. package/packages/@monomind/cli/dist/src/monovector/coverage-router.d.ts +103 -0
  140. package/packages/@monomind/cli/dist/src/monovector/coverage-router.js +337 -0
  141. package/packages/@monomind/cli/dist/src/monovector/coverage-tools.d.ts +14 -0
  142. package/packages/@monomind/cli/dist/src/monovector/coverage-tools.js +112 -0
  143. package/packages/@monomind/cli/dist/src/{ruvector → monovector}/diff-classifier.d.ts +2 -2
  144. package/packages/@monomind/cli/dist/src/monovector/index.d.ts +61 -0
  145. package/packages/@monomind/cli/dist/src/monovector/index.js +67 -0
  146. package/packages/@monomind/cli/dist/src/monovector/init-state.d.ts +35 -0
  147. package/packages/@monomind/cli/dist/src/monovector/init-state.js +36 -0
  148. package/packages/@monomind/cli/dist/src/monovector/route-outcomes.d.ts +55 -0
  149. package/packages/@monomind/cli/dist/src/monovector/route-outcomes.js +137 -0
  150. package/packages/@monomind/cli/dist/src/parser.js +3 -3
  151. package/packages/@monomind/cli/dist/src/plugins/store/discovery.js +0 -31
  152. package/packages/@monomind/cli/dist/src/production/circuit-breaker.d.ts +26 -6
  153. package/packages/@monomind/cli/dist/src/production/error-handler.d.ts +55 -30
  154. package/packages/@monomind/cli/dist/src/production/index.d.ts +3 -20
  155. package/packages/@monomind/cli/dist/src/production/index.js +3 -15
  156. package/packages/@monomind/cli/dist/src/production/monitoring.d.ts +54 -34
  157. package/packages/@monomind/cli/dist/src/production/monitoring.js +12 -14
  158. package/packages/@monomind/cli/dist/src/production/rate-limiter.d.ts +64 -19
  159. package/packages/@monomind/cli/dist/src/production/rate-limiter.js +5 -5
  160. package/packages/@monomind/cli/dist/src/production/retry.js +4 -2
  161. package/packages/@monomind/cli/dist/src/services/claim-service.d.ts +50 -59
  162. package/packages/@monomind/cli/dist/src/services/claim-service.js +83 -50
  163. package/packages/@monomind/cli/dist/src/services/config-file-manager.js +8 -1
  164. package/packages/@monomind/cli/dist/src/services/worker-daemon.js +4 -0
  165. package/packages/@monomind/cli/dist/src/types.d.ts +13 -0
  166. package/packages/@monomind/cli/package.json +14 -14
  167. package/packages/@monomind/guidance/dist/index.d.ts +1 -1
  168. package/packages/@monomind/guidance/dist/index.js +1 -1
  169. package/packages/@monomind/guidance/package.json +1 -2
  170. package/scripts/build-monovector.sh +10 -0
  171. package/scripts/publish-monovector.sh +20 -0
  172. package/.claude/commands/monomind/adr.md +0 -11
  173. package/.claude/commands/monomind/budget.md +0 -7
  174. package/.claude/commands/monomind/createtask.md +0 -277
  175. package/.claude/commands/monomind/do.md +0 -498
  176. package/.claude/commands/monomind/graph-status.md +0 -7
  177. package/.claude/commands/monomind/help.md +0 -118
  178. package/.claude/commands/monomind/idea.md +0 -273
  179. package/.claude/commands/monomind/improve.md +0 -352
  180. package/.claude/commands/monomind/loops.md +0 -7
  181. package/.claude/commands/monomind/memory.md +0 -230
  182. package/.claude/commands/monomind/repeat.md +0 -257
  183. package/.claude/commands/monomind/review.md +0 -317
  184. package/.claude/commands/monomind/specialagents.md +0 -125
  185. package/.claude/commands/monomind/swarm.md +0 -161
  186. package/.claude/commands/monomind/understand.md +0 -139
  187. package/.claude/commands/training/README.md +0 -39
  188. package/.claude/commands/training/neural-patterns.md +0 -73
  189. package/.claude/commands/training/neural-train.md +0 -79
  190. package/packages/@monomind/cli/dist/src/commands/appliance-advanced.d.ts +0 -9
  191. package/packages/@monomind/cli/dist/src/commands/appliance-advanced.js +0 -238
  192. package/packages/@monomind/cli/dist/src/commands/appliance.d.ts +0 -8
  193. package/packages/@monomind/cli/dist/src/commands/appliance.js +0 -406
  194. package/packages/@monomind/cli/dist/src/commands/ruvector/backup.d.ts +0 -11
  195. package/packages/@monomind/cli/dist/src/commands/ruvector/benchmark.d.ts +0 -11
  196. package/packages/@monomind/cli/dist/src/commands/ruvector/import.d.ts +0 -18
  197. package/packages/@monomind/cli/dist/src/commands/ruvector/init.d.ts +0 -11
  198. package/packages/@monomind/cli/dist/src/commands/ruvector/migrate.d.ts +0 -11
  199. package/packages/@monomind/cli/dist/src/commands/ruvector/setup.d.ts +0 -18
  200. package/packages/@monomind/cli/dist/src/ruvector/agent-wasm.d.ts +0 -182
  201. package/packages/@monomind/cli/dist/src/ruvector/agent-wasm.js +0 -316
  202. package/packages/@monomind/cli/dist/src/ruvector/ast-analyzer.d.ts +0 -67
  203. package/packages/@monomind/cli/dist/src/ruvector/ast-analyzer.js +0 -277
  204. package/packages/@monomind/cli/dist/src/ruvector/coverage-router.d.ts +0 -160
  205. package/packages/@monomind/cli/dist/src/ruvector/coverage-router.js +0 -539
  206. package/packages/@monomind/cli/dist/src/ruvector/coverage-tools.d.ts +0 -33
  207. package/packages/@monomind/cli/dist/src/ruvector/coverage-tools.js +0 -157
  208. package/packages/@monomind/cli/dist/src/ruvector/enhanced-model-router.d.ts +0 -146
  209. package/packages/@monomind/cli/dist/src/ruvector/enhanced-model-router.js +0 -551
  210. package/packages/@monomind/cli/dist/src/ruvector/flash-attention.d.ts +0 -195
  211. package/packages/@monomind/cli/dist/src/ruvector/flash-attention.js +0 -643
  212. package/packages/@monomind/cli/dist/src/ruvector/graph-analyzer.d.ts +0 -187
  213. package/packages/@monomind/cli/dist/src/ruvector/graph-analyzer.js +0 -929
  214. package/packages/@monomind/cli/dist/src/ruvector/index.d.ts +0 -67
  215. package/packages/@monomind/cli/dist/src/ruvector/index.js +0 -88
  216. package/packages/@monomind/cli/dist/src/ruvector/lora-adapter.d.ts +0 -218
  217. package/packages/@monomind/cli/dist/src/ruvector/lora-adapter.js +0 -455
  218. package/packages/@monomind/cli/dist/src/ruvector/model-router.d.ts +0 -222
  219. package/packages/@monomind/cli/dist/src/ruvector/model-router.js +0 -512
  220. package/packages/@monomind/cli/dist/src/ruvector/moe-router.d.ts +0 -213
  221. package/packages/@monomind/cli/dist/src/ruvector/moe-router.js +0 -649
  222. package/packages/@monomind/cli/dist/src/ruvector/q-learning-router.d.ts +0 -217
  223. package/packages/@monomind/cli/dist/src/ruvector/q-learning-router.js +0 -712
  224. package/packages/@monomind/cli/dist/src/ruvector/ruvllm-wasm.d.ts +0 -179
  225. package/packages/@monomind/cli/dist/src/ruvector/ruvllm-wasm.js +0 -363
  226. package/packages/@monomind/cli/dist/src/ruvector/semantic-router.d.ts +0 -77
  227. package/packages/@monomind/cli/dist/src/ruvector/semantic-router.js +0 -178
  228. package/packages/@monomind/cli/dist/src/ruvector/vector-db.d.ts +0 -69
  229. package/packages/@monomind/cli/dist/src/ruvector/vector-db.js +0 -243
  230. package/packages/@monomind/cli/dist/src/services/ruvector-training.d.ts +0 -222
  231. package/packages/@monomind/cli/dist/src/services/ruvector-training.js +0 -696
  232. /package/packages/@monomind/cli/dist/src/{ruvector → monovector}/diff-classifier.js +0 -0
@@ -1,512 +0,0 @@
1
- /**
2
- * Intelligent Model Router using Tiny Dancer
3
- *
4
- * Dynamically routes requests to optimal Claude model (haiku/sonnet/opus)
5
- * based on task complexity, confidence scores, and historical performance.
6
- *
7
- * Features:
8
- * - FastGRNN-based routing decisions (<100μs)
9
- * - Uncertainty quantification for model escalation
10
- * - Circuit breaker for failover
11
- * - Online learning from routing outcomes
12
- * - Complexity scoring via embeddings
13
- *
14
- * Routing Strategy:
15
- * - Haiku: High confidence, low complexity (fast, cheap)
16
- * - Sonnet: Medium confidence, moderate complexity (balanced)
17
- * - Opus: Low confidence, high complexity (most capable)
18
- *
19
- * @module model-router
20
- */
21
- import { existsSync, mkdirSync, readFileSync, writeFileSync, renameSync } from 'fs';
22
- import { dirname, join } from 'path';
23
- /**
24
- * Model capabilities and characteristics
25
- */
26
- export const MODEL_CAPABILITIES = {
27
- haiku: {
28
- maxComplexity: 0.4,
29
- costMultiplier: 0.04, // ~25x cheaper than Opus
30
- speedMultiplier: 3.0, // ~3x faster than Sonnet
31
- description: 'Fast, cost-effective for simple tasks',
32
- },
33
- sonnet: {
34
- maxComplexity: 0.7,
35
- costMultiplier: 0.2, // ~5x cheaper than Opus
36
- speedMultiplier: 1.5, // ~1.5x faster than Opus
37
- description: 'Balanced capability and cost',
38
- },
39
- opus: {
40
- maxComplexity: 1.0,
41
- costMultiplier: 1.0, // Baseline
42
- speedMultiplier: 1.0, // Baseline
43
- description: 'Most capable for complex reasoning',
44
- },
45
- inherit: {
46
- maxComplexity: 1.0,
47
- costMultiplier: 1.0,
48
- speedMultiplier: 1.0,
49
- description: 'Use parent model selection',
50
- },
51
- };
52
- /**
53
- * Complexity indicators for task classification
54
- */
55
- export const COMPLEXITY_INDICATORS = {
56
- high: [
57
- 'architect', 'design', 'refactor', 'optimize', 'security', 'audit',
58
- 'complex', 'analyze', 'investigate', 'debug', 'performance', 'scale',
59
- 'distributed', 'concurrent', 'algorithm', 'system', 'integration',
60
- ],
61
- medium: [
62
- 'implement', 'feature', 'add', 'update', 'modify', 'fix', 'test',
63
- 'review', 'validate', 'check', 'improve', 'enhance', 'extend',
64
- ],
65
- low: [
66
- 'simple', 'typo', 'comment', 'format', 'rename', 'move', 'copy',
67
- 'delete', 'documentation', 'readme', 'config', 'version', 'bump',
68
- ],
69
- };
70
- // ============================================================================
71
- // Default Configuration
72
- // ============================================================================
73
- const DEFAULT_CONFIG = {
74
- confidenceThreshold: 0.85,
75
- maxUncertainty: 0.15,
76
- enableCircuitBreaker: true,
77
- circuitBreakerThreshold: 5,
78
- statePath: '.swarm/model-router-state.json',
79
- autoSaveInterval: 1, // Save after every decision for CLI persistence
80
- enableCostOptimization: true,
81
- preferSpeed: true,
82
- };
83
- // ============================================================================
84
- // Model Router Implementation
85
- // ============================================================================
86
- /**
87
- * Intelligent Model Router using complexity-based routing
88
- */
89
- export class ModelRouter {
90
- config;
91
- state;
92
- decisionCount = 0;
93
- consecutiveFailures = {
94
- haiku: 0,
95
- sonnet: 0,
96
- opus: 0,
97
- inherit: 0,
98
- };
99
- constructor(config = {}) {
100
- this.config = { ...DEFAULT_CONFIG, ...config };
101
- this.state = this.loadState();
102
- }
103
- /**
104
- * Route a task to the optimal model
105
- */
106
- async route(task, embedding) {
107
- const startTime = performance.now();
108
- // Analyze task complexity
109
- const complexity = this.analyzeComplexity(task, embedding);
110
- // Compute base model scores
111
- const scores = this.computeModelScores(complexity);
112
- // Apply circuit breaker adjustments
113
- const adjustedScores = this.applyCircuitBreaker(scores);
114
- // Select best model
115
- const { model, confidence, uncertainty } = this.selectModel(adjustedScores, complexity.score);
116
- const inferenceTimeUs = (performance.now() - startTime) * 1000;
117
- // Build result
118
- const result = {
119
- model,
120
- confidence,
121
- uncertainty,
122
- complexity: complexity.score,
123
- reasoning: this.buildReasoning(model, complexity, confidence),
124
- alternatives: Object.entries(adjustedScores)
125
- .filter(([m]) => m !== model)
126
- .map(([m, score]) => ({ model: m, score }))
127
- .sort((a, b) => b.score - a.score),
128
- inferenceTimeUs,
129
- costMultiplier: MODEL_CAPABILITIES[model].costMultiplier,
130
- };
131
- // Track decision
132
- this.trackDecision(task, result);
133
- return result;
134
- }
135
- /**
136
- * Analyze task complexity
137
- */
138
- analyzeComplexity(task, embedding) {
139
- const taskLower = task.toLowerCase();
140
- const words = taskLower.split(/\s+/);
141
- // Find complexity indicators
142
- const indicators = {
143
- high: COMPLEXITY_INDICATORS.high.filter(ind => taskLower.includes(ind)),
144
- medium: COMPLEXITY_INDICATORS.medium.filter(ind => taskLower.includes(ind)),
145
- low: COMPLEXITY_INDICATORS.low.filter(ind => taskLower.includes(ind)),
146
- };
147
- // Compute feature scores
148
- const lexicalComplexity = this.computeLexicalComplexity(task);
149
- const semanticDepth = this.computeSemanticDepth(indicators, embedding);
150
- const taskScope = this.computeTaskScope(task, words);
151
- const uncertaintyLevel = this.computeUncertaintyLevel(task);
152
- // Weighted combination
153
- const score = Math.min(1, Math.max(0, lexicalComplexity * 0.2 +
154
- semanticDepth * 0.35 +
155
- taskScope * 0.25 +
156
- uncertaintyLevel * 0.2));
157
- return {
158
- score,
159
- indicators,
160
- features: {
161
- lexicalComplexity,
162
- semanticDepth,
163
- taskScope,
164
- uncertaintyLevel,
165
- },
166
- };
167
- }
168
- /**
169
- * Compute lexical complexity from text features
170
- */
171
- computeLexicalComplexity(task) {
172
- const words = task.split(/\s+/);
173
- const avgWordLength = words.reduce((sum, w) => sum + w.length, 0) / Math.max(1, words.length);
174
- const sentenceLength = words.length;
175
- // Normalize: longer sentences with longer words = more complex
176
- const lengthScore = Math.min(1, sentenceLength / 50);
177
- const wordScore = Math.min(1, (avgWordLength - 3) / 7); // 3-10 char words
178
- return lengthScore * 0.4 + wordScore * 0.6;
179
- }
180
- /**
181
- * Compute semantic depth from indicators and embedding
182
- */
183
- computeSemanticDepth(indicators, embedding) {
184
- // Weight by indicator presence
185
- const highWeight = indicators.high.length * 0.3;
186
- const mediumWeight = indicators.medium.length * 0.15;
187
- const lowWeight = indicators.low.length * -0.1;
188
- let baseScore = Math.min(1, Math.max(0, 0.3 + highWeight + mediumWeight + lowWeight));
189
- // Boost with embedding variance if available
190
- if (embedding && embedding.length > 0) {
191
- const mean = embedding.reduce((a, b) => a + b, 0) / embedding.length;
192
- const variance = embedding.reduce((sum, v) => sum + Math.pow(v - mean, 2), 0) / embedding.length;
193
- // Higher variance suggests more nuanced semantics
194
- baseScore = baseScore * 0.7 + Math.min(1, variance * 10) * 0.3;
195
- }
196
- return baseScore;
197
- }
198
- /**
199
- * Compute task scope from content analysis
200
- */
201
- computeTaskScope(task, words) {
202
- // Multi-file indicators
203
- const multiFilePatterns = [
204
- /multiple files?/i, /across.*modules?/i, /refactor.*codebase/i,
205
- /all.*files/i, /entire.*project/i, /system.*wide/i,
206
- ];
207
- const hasMultiFile = multiFilePatterns.some(p => p.test(task)) ? 0.4 : 0;
208
- // Code generation indicators
209
- const codeGenPatterns = [
210
- /implement/i, /create.*feature/i, /build.*system/i,
211
- /design.*api/i, /write.*tests/i, /add.*functionality/i,
212
- ];
213
- const hasCodeGen = codeGenPatterns.some(p => p.test(task)) ? 0.3 : 0;
214
- // Word count contribution
215
- const wordCountScore = Math.min(0.3, words.length / 100);
216
- return hasMultiFile + hasCodeGen + wordCountScore;
217
- }
218
- /**
219
- * Compute uncertainty level from task phrasing
220
- */
221
- computeUncertaintyLevel(task) {
222
- const uncertainPatterns = [
223
- /not sure/i, /might/i, /maybe/i, /possibly/i, /investigate/i,
224
- /figure out/i, /unclear/i, /unknown/i, /debug/i, /strange/i,
225
- /weird/i, /issue/i, /problem/i, /error/i, /bug/i,
226
- ];
227
- const matchCount = uncertainPatterns.filter(p => p.test(task)).length;
228
- return Math.min(1, matchCount * 0.2);
229
- }
230
- /**
231
- * Compute scores for each model
232
- */
233
- computeModelScores(complexity) {
234
- const { score } = complexity;
235
- // Base scoring: inverse relationship with complexity
236
- // Low complexity → haiku scores high
237
- // High complexity → opus scores high
238
- return {
239
- haiku: Math.max(0, 1 - score * 2), // Drops off quickly as complexity rises
240
- sonnet: 1 - Math.abs(score - 0.5) * 2, // Peaks at medium complexity
241
- opus: Math.min(1, score * 1.5), // Rises with complexity
242
- inherit: 0.1, // Low baseline unless explicitly needed
243
- };
244
- }
245
- /**
246
- * Apply circuit breaker adjustments
247
- */
248
- applyCircuitBreaker(scores) {
249
- if (!this.config.enableCircuitBreaker) {
250
- return scores;
251
- }
252
- const adjusted = { ...scores };
253
- for (const model of Object.keys(adjusted)) {
254
- if (this.consecutiveFailures[model] >= this.config.circuitBreakerThreshold) {
255
- // Circuit is open - heavily penalize this model
256
- adjusted[model] *= 0.1;
257
- }
258
- else if (this.consecutiveFailures[model] > 0) {
259
- // Partial penalty for recent failures
260
- adjusted[model] *= 1 - (this.consecutiveFailures[model] / this.config.circuitBreakerThreshold) * 0.5;
261
- }
262
- }
263
- return adjusted;
264
- }
265
- /**
266
- * Select the best model from scores
267
- */
268
- selectModel(scores, complexityScore) {
269
- // Get sorted models by score
270
- const sorted = Object.entries(scores)
271
- .filter(([m]) => m !== 'inherit')
272
- .sort((a, b) => b[1] - a[1]);
273
- const [bestModel, bestScore] = sorted[0];
274
- const [secondModel, secondScore] = sorted[1] || ['sonnet', 0];
275
- // Confidence is how much better the best is vs second
276
- const confidence = bestScore > 0 ? Math.min(1, bestScore / (bestScore + secondScore + 0.01)) : 0.5;
277
- // Uncertainty based on score spread and complexity
278
- const scoreSpread = bestScore - secondScore;
279
- const uncertainty = Math.max(0, 1 - scoreSpread - confidence * 0.5);
280
- // Escalate if uncertainty is too high
281
- let model = bestModel;
282
- if (uncertainty > this.config.maxUncertainty && bestModel !== 'opus') {
283
- // Escalate to more capable model
284
- model = bestModel === 'haiku' ? 'sonnet' : 'opus';
285
- }
286
- return { model, confidence, uncertainty };
287
- }
288
- /**
289
- * Build human-readable reasoning
290
- */
291
- buildReasoning(model, complexity, confidence) {
292
- const parts = [];
293
- parts.push(`Complexity: ${(complexity.score * 100).toFixed(0)}%`);
294
- if (complexity.indicators.high.length > 0) {
295
- parts.push(`High-complexity indicators: ${complexity.indicators.high.join(', ')}`);
296
- }
297
- parts.push(`Confidence: ${(confidence * 100).toFixed(0)}%`);
298
- parts.push(`Model: ${model} - ${MODEL_CAPABILITIES[model].description}`);
299
- if (this.config.enableCostOptimization) {
300
- parts.push(`Cost: ${MODEL_CAPABILITIES[model].costMultiplier}x baseline`);
301
- }
302
- return parts.join(' | ');
303
- }
304
- /**
305
- * Track routing decision for learning
306
- */
307
- trackDecision(task, result) {
308
- this.decisionCount++;
309
- this.state.totalDecisions++;
310
- this.state.modelDistribution[result.model] =
311
- (this.state.modelDistribution[result.model] || 0) + 1;
312
- // Update running averages
313
- const n = this.state.totalDecisions;
314
- this.state.avgComplexity =
315
- (this.state.avgComplexity * (n - 1) + result.complexity) / n;
316
- this.state.avgConfidence =
317
- (this.state.avgConfidence * (n - 1) + result.confidence) / n;
318
- // Auto-save periodically
319
- if (this.decisionCount % this.config.autoSaveInterval === 0) {
320
- this.saveState();
321
- }
322
- }
323
- /**
324
- * Record outcome for learning
325
- */
326
- recordOutcome(task, model, outcome, complexity) {
327
- // Update circuit breaker state
328
- if (outcome === 'failure') {
329
- this.consecutiveFailures[model]++;
330
- }
331
- else {
332
- this.consecutiveFailures[model] = 0;
333
- }
334
- // Use per-task complexity when provided; fall back to running average so
335
- // callers that don't have the routing result can still record outcomes.
336
- const taskComplexity = complexity ?? this.state.avgComplexity;
337
- // Track in history
338
- this.state.learningHistory.push({
339
- task: task.slice(0, 100),
340
- model,
341
- complexity: taskComplexity,
342
- outcome,
343
- timestamp: new Date().toISOString(),
344
- });
345
- // Keep history bounded
346
- if (this.state.learningHistory.length > 100) {
347
- this.state.learningHistory = this.state.learningHistory.slice(-100);
348
- }
349
- if (outcome === 'failure') {
350
- this.state.circuitBreakerTrips++;
351
- }
352
- this.saveState();
353
- }
354
- /**
355
- * Get router statistics
356
- */
357
- getStats() {
358
- return {
359
- totalDecisions: this.state.totalDecisions,
360
- modelDistribution: { ...this.state.modelDistribution },
361
- avgComplexity: this.state.avgComplexity,
362
- avgConfidence: this.state.avgConfidence,
363
- circuitBreakerTrips: this.state.circuitBreakerTrips,
364
- consecutiveFailures: { ...this.consecutiveFailures },
365
- };
366
- }
367
- /**
368
- * Load state from disk
369
- */
370
- loadState() {
371
- const defaultState = {
372
- totalDecisions: 0,
373
- modelDistribution: { haiku: 0, sonnet: 0, opus: 0, inherit: 0 },
374
- avgComplexity: 0.5,
375
- avgConfidence: 0.8,
376
- circuitBreakerTrips: 0,
377
- lastUpdated: new Date().toISOString(),
378
- learningHistory: [],
379
- };
380
- try {
381
- const fullPath = join(process.cwd(), this.config.statePath);
382
- if (existsSync(fullPath)) {
383
- const data = readFileSync(fullPath, 'utf-8');
384
- const parsed = { ...defaultState, ...JSON.parse(data) };
385
- if (!Number.isFinite(parsed.avgComplexity))
386
- parsed.avgComplexity = defaultState.avgComplexity;
387
- if (!Number.isFinite(parsed.avgConfidence))
388
- parsed.avgConfidence = defaultState.avgConfidence;
389
- if (!Number.isFinite(parsed.totalDecisions) || parsed.totalDecisions < 0)
390
- parsed.totalDecisions = defaultState.totalDecisions;
391
- if (!Number.isFinite(parsed.circuitBreakerTrips) || parsed.circuitBreakerTrips < 0)
392
- parsed.circuitBreakerTrips = defaultState.circuitBreakerTrips;
393
- return parsed;
394
- }
395
- }
396
- catch {
397
- // Ignore load errors
398
- }
399
- return defaultState;
400
- }
401
- /**
402
- * Save state to disk
403
- */
404
- saveState() {
405
- try {
406
- const fullPath = join(process.cwd(), this.config.statePath);
407
- const dir = dirname(fullPath);
408
- if (!existsSync(dir)) {
409
- mkdirSync(dir, { recursive: true });
410
- }
411
- this.state.lastUpdated = new Date().toISOString();
412
- const tmp = fullPath + '.tmp';
413
- writeFileSync(tmp, JSON.stringify(this.state, null, 2));
414
- renameSync(tmp, fullPath);
415
- }
416
- catch {
417
- // Ignore save errors in non-critical scenarios
418
- }
419
- }
420
- /**
421
- * Reset router state
422
- */
423
- reset() {
424
- this.state = {
425
- totalDecisions: 0,
426
- modelDistribution: { haiku: 0, sonnet: 0, opus: 0, inherit: 0 },
427
- avgComplexity: 0.5,
428
- avgConfidence: 0.8,
429
- circuitBreakerTrips: 0,
430
- lastUpdated: new Date().toISOString(),
431
- learningHistory: [],
432
- };
433
- this.consecutiveFailures = { haiku: 0, sonnet: 0, opus: 0, inherit: 0 };
434
- this.decisionCount = 0;
435
- this.saveState();
436
- }
437
- }
438
- // ============================================================================
439
- // Singleton & Factory Functions
440
- // ============================================================================
441
- let modelRouterInstance = null;
442
- let modelRouterInstanceConfig;
443
- /**
444
- * Get or create the singleton ModelRouter instance.
445
- * Throws if called with a config that differs from the one used to create
446
- * the existing instance — silent config mismatch causes hard-to-debug routing bugs.
447
- */
448
- export function getModelRouter(config) {
449
- if (!modelRouterInstance) {
450
- modelRouterInstance = new ModelRouter(config);
451
- modelRouterInstanceConfig = config;
452
- return modelRouterInstance;
453
- }
454
- if (config !== undefined && JSON.stringify(config) !== JSON.stringify(modelRouterInstanceConfig)) {
455
- throw new Error('ModelRouter singleton already initialized with different config. ' +
456
- 'Call resetModelRouter() first, or use createModelRouter() for a separate instance.');
457
- }
458
- return modelRouterInstance;
459
- }
460
- /**
461
- * Reset the singleton instance
462
- */
463
- export function resetModelRouter() {
464
- modelRouterInstance = null;
465
- modelRouterInstanceConfig = undefined;
466
- }
467
- /**
468
- * Create a new ModelRouter instance (non-singleton)
469
- */
470
- export function createModelRouter(config) {
471
- return new ModelRouter(config);
472
- }
473
- // ============================================================================
474
- // Convenience Functions
475
- // ============================================================================
476
- /**
477
- * Quick route function for common use case
478
- */
479
- export async function routeToModel(task) {
480
- const router = getModelRouter();
481
- const result = await router.route(task);
482
- return result.model;
483
- }
484
- /**
485
- * Route with full result
486
- */
487
- export async function routeToModelFull(task, embedding) {
488
- const router = getModelRouter();
489
- return router.route(task, embedding);
490
- }
491
- /**
492
- * Analyze task complexity without routing
493
- */
494
- export function analyzeTaskComplexity(task) {
495
- const router = getModelRouter();
496
- return router.analyzeComplexity(task, undefined);
497
- }
498
- /**
499
- * Get model router statistics
500
- */
501
- export function getModelRouterStats() {
502
- const router = getModelRouter();
503
- return router.getStats();
504
- }
505
- /**
506
- * Record routing outcome for learning
507
- */
508
- export function recordModelOutcome(task, model, outcome) {
509
- const router = getModelRouter();
510
- router.recordOutcome(task, model, outcome);
511
- }
512
- //# sourceMappingURL=model-router.js.map