lynkr 9.14.1 → 9.14.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/README.md +5 -1
  2. package/bin/cli.js +14 -6
  3. package/bin/lynkr-desktop-token.js +108 -0
  4. package/bin/lynkr-init.js +13 -1
  5. package/bin/lynkr-restart.js +125 -0
  6. package/bin/opencode-setup.js +175 -0
  7. package/bin/run.js +92 -0
  8. package/config/model-capabilities.json +20 -0
  9. package/config/model-capability-seeds.json +75 -0
  10. package/config/model-tiers.json +13 -1
  11. package/package.json +16 -22
  12. package/scripts/claude-desktop.js +212 -0
  13. package/scripts/ope-report.js +78 -0
  14. package/scripts/seed-capabilities.js +230 -0
  15. package/src/api/claude-desktop-gateway.js +123 -0
  16. package/src/api/health.js +0 -1
  17. package/src/api/mcp-broker.js +149 -0
  18. package/src/api/middleware/budget-enforcer.js +77 -7
  19. package/src/api/middleware/budget.js +36 -2
  20. package/src/api/middleware/session.js +0 -0
  21. package/src/api/openai-router.js +210 -15
  22. package/src/api/providers-handler.js +16 -1
  23. package/src/api/router.js +73 -106
  24. package/src/auth-mode.js +40 -4
  25. package/src/budget/index.js +86 -0
  26. package/src/cache/embeddings.js +125 -35
  27. package/src/cache/onnx-embedder.js +92 -0
  28. package/src/cache/prompt.js +0 -7
  29. package/src/clients/bedrock-utils.js +2 -1
  30. package/src/clients/codex-process.js +1 -1
  31. package/src/clients/codex-utils.js +0 -2
  32. package/src/clients/databricks.js +337 -46
  33. package/src/clients/gpt-utils.js +84 -8
  34. package/src/clients/health-probe.js +157 -0
  35. package/src/clients/openai-format.js +73 -23
  36. package/src/clients/provider-capabilities.js +27 -2
  37. package/src/clients/resilience.js +0 -1
  38. package/src/clients/responses-format.js +0 -56
  39. package/src/clients/routing.js +1 -1
  40. package/src/config/index.js +13 -2
  41. package/src/context/budget.js +1 -1
  42. package/src/context/compression.js +127 -19
  43. package/src/context/tool-result-compressor.js +3 -5
  44. package/src/dashboard/api.js +1 -1
  45. package/src/logger/oversized-error-stream.js +1 -1
  46. package/src/memory/extractor.js +3 -3
  47. package/src/memory/search.js +2 -2
  48. package/src/memory/store.js +9 -24
  49. package/src/memory/tools.js +14 -0
  50. package/src/observability/metrics.js +52 -3
  51. package/src/observability/otel.js +180 -0
  52. package/src/orchestrator/azure-responses-sse.js +223 -0
  53. package/src/orchestrator/index.js +216 -212
  54. package/src/orchestrator/passthrough-stream.js +7 -1
  55. package/src/orchestrator/sse-transformer.js +130 -13
  56. package/src/policy/safe-commands.js +0 -1
  57. package/src/routing/bandit.js +39 -0
  58. package/src/routing/calibration.js +21 -1
  59. package/src/routing/capabilities.js +88 -0
  60. package/src/routing/capability-seeds/benchmarks-dir.js +58 -0
  61. package/src/routing/capability-seeds/family-heuristics.js +77 -0
  62. package/src/routing/capability-seeds/family.js +105 -0
  63. package/src/routing/capability-seeds/models-dev.js +64 -0
  64. package/src/routing/capability-seeds/normalize.js +160 -0
  65. package/src/routing/capability-seeds/registry.js +125 -0
  66. package/src/routing/capability-seeds/sources.js +69 -0
  67. package/src/routing/capability-seeds/swebench.js +66 -0
  68. package/src/routing/client-profiles.js +26 -0
  69. package/src/routing/complexity-analyzer.js +5 -8
  70. package/src/routing/confidence-scorer.js +2 -2
  71. package/src/routing/cost-optimizer.js +1 -2
  72. package/src/routing/decide.js +149 -0
  73. package/src/routing/deescalator.js +38 -1
  74. package/src/routing/embedding-cache.js +0 -1
  75. package/src/routing/index.js +202 -86
  76. package/src/routing/intent-score.js +0 -1
  77. package/src/routing/knn-router.js +27 -5
  78. package/src/routing/latency-tracker.js +1 -1
  79. package/src/routing/model-registry.js +25 -0
  80. package/src/routing/model-slots.js +65 -0
  81. package/src/routing/model-tiers.js +2 -0
  82. package/src/routing/ope.js +255 -0
  83. package/src/routing/openai-model-slots.js +82 -0
  84. package/src/routing/regret-estimator.js +1 -1
  85. package/src/routing/shortfall.js +290 -0
  86. package/src/routing/stuck-detector.js +127 -0
  87. package/src/routing/telemetry.js +11 -2
  88. package/src/server.js +41 -3
  89. package/src/stores/file-store.js +5 -1
  90. package/src/tools/web-search-exec.js +182 -0
  91. package/src/training/trajectory-compressor.js +1 -1
  92. package/src/workers/pool.js +1 -1
  93. package/src/workers/worker.js +1 -1
  94. package/scripts/convert-routellm.py +0 -105
  95. package/src/diff/comments.js +0 -153
  96. package/src/policy/web-fallback.js +0 -33
@@ -0,0 +1,92 @@
1
+ /**
2
+ * In-process ONNX embedder (ROUTING-NOTES §4.10.2).
3
+ *
4
+ * Runs the embedding model inside the Lynkr process via transformers.js
5
+ * (@huggingface/transformers, an optionalDependency), removing the external
6
+ * Ollama/llama.cpp server from the semantic-cache and kNN hot path — no
7
+ * network hop, no "is the server up" failure mode, no contention with
8
+ * completion traffic on the same local runtime.
9
+ *
10
+ * Model choice is deliberate: the ONNX build of the SAME model the Ollama
11
+ * path defaults to (nomic-embed-text, 768-dim). Same model = same embedding
12
+ * space = existing kNN index entries and semantic-cache vectors stay valid.
13
+ * (INT8 quantization introduces small numeric differences vs Ollama's
14
+ * serving precision, but cosine similarity is preserved far above the 0.92
15
+ * cache threshold.)
16
+ *
17
+ * Weights are downloaded once from the HuggingFace hub into
18
+ * ~/.lynkr/models (override: LYNKR_ONNX_CACHE_DIR) and verified by
19
+ * transformers.js's own integrity checks. Nothing is bundled in the npm
20
+ * package.
21
+ *
22
+ * Opt-in via LYNKR_EMBEDDINGS_PROVIDER=onnx. If the optional dependency is
23
+ * missing or the model fails to load, the caller's degradation machinery
24
+ * (cache/embeddings.js) takes over — loud, observable, recoverable.
25
+ *
26
+ * @module cache/onnx-embedder
27
+ */
28
+
29
+ const os = require('os');
30
+ const path = require('path');
31
+ const logger = require('../logger');
32
+
33
+ const MODEL_ID = process.env.LYNKR_ONNX_EMBEDDING_MODEL || 'Xenova/nomic-embed-text-v1';
34
+ const CACHE_DIR = process.env.LYNKR_ONNX_CACHE_DIR
35
+ || path.join(os.homedir(), '.lynkr', 'models');
36
+ // q8 keeps the download ~25–35MB and inference CPU-friendly.
37
+ const DTYPE = process.env.LYNKR_ONNX_DTYPE || 'q8';
38
+
39
+ /** @type {Promise<Function>|null} memoized pipeline load (single flight) */
40
+ let pipelinePromise = null;
41
+
42
+ async function _loadPipeline() {
43
+ // Lazy, inside the function: @huggingface/transformers is an
44
+ // optionalDependency and an ESM-only package — import() from CJS.
45
+ const { pipeline, env } = await import('@huggingface/transformers');
46
+ env.cacheDir = CACHE_DIR;
47
+ const started = Date.now();
48
+ logger.info({ model: MODEL_ID, cacheDir: CACHE_DIR, dtype: DTYPE },
49
+ '[OnnxEmbedder] Loading embedding model (first run downloads weights)');
50
+ const extractor = await pipeline('feature-extraction', MODEL_ID, { dtype: DTYPE });
51
+ logger.info({ model: MODEL_ID, loadMs: Date.now() - started },
52
+ '[OnnxEmbedder] Embedding model ready');
53
+ return extractor;
54
+ }
55
+
56
+ /**
57
+ * Generate an embedding fully in-process.
58
+ * @param {string} text
59
+ * @returns {Promise<number[]>} mean-pooled, L2-normalized vector (768-dim
60
+ * for the default model)
61
+ */
62
+ async function generateOnnxEmbedding(text) {
63
+ if (!pipelinePromise) {
64
+ pipelinePromise = _loadPipeline().catch((err) => {
65
+ // Reset so a later call can retry (e.g. transient download failure) —
66
+ // a failed load must not latch permanently, same principle as the
67
+ // provider degradation fix in cache/embeddings.js.
68
+ pipelinePromise = null;
69
+ throw err;
70
+ });
71
+ }
72
+ const extractor = await pipelinePromise;
73
+ const output = await extractor(text, { pooling: 'mean', normalize: true });
74
+ return Array.from(output.data);
75
+ }
76
+
77
+ /** True when the optional dependency is installed (cheap resolve check). */
78
+ function isOnnxAvailable() {
79
+ try {
80
+ require.resolve('@huggingface/transformers');
81
+ return true;
82
+ } catch {
83
+ return false;
84
+ }
85
+ }
86
+
87
+ /** Test helper — drop the memoized pipeline. */
88
+ function _resetPipeline() {
89
+ pipelinePromise = null;
90
+ }
91
+
92
+ module.exports = { generateOnnxEmbedding, isOnnxAvailable, _resetPipeline, MODEL_ID };
@@ -19,13 +19,6 @@ const fs = require("fs");
19
19
  const config = require("../config");
20
20
  const logger = require("../logger");
21
21
 
22
- function cloneValue(value) {
23
- if (typeof structuredClone === "function") {
24
- return structuredClone(value);
25
- }
26
- return JSON.parse(JSON.stringify(value));
27
- }
28
-
29
22
  function normaliseObject(value) {
30
23
  if (value === null || typeof value !== "object") return value;
31
24
  if (Array.isArray(value)) {
@@ -116,7 +116,7 @@ function convertAnthropicToBedrockFormat(body, modelFamily) {
116
116
  },
117
117
  };
118
118
 
119
- case "llama":
119
+ case "llama": {
120
120
  // Meta Llama format
121
121
  const llamaPrompt = messagesToPrompt(body.messages);
122
122
  return {
@@ -125,6 +125,7 @@ function convertAnthropicToBedrockFormat(body, modelFamily) {
125
125
  temperature: body.temperature || 0.7,
126
126
  top_p: body.top_p || 0.9,
127
127
  };
128
+ }
128
129
 
129
130
  case "jurassic":
130
131
  // AI21 Jurassic format
@@ -279,7 +279,7 @@ class CodexProcess {
279
279
  * Reject all pending requests
280
280
  */
281
281
  _rejectAllPending(error) {
282
- for (const [id, pending] of this.pendingRequests) {
282
+ for (const [_id, pending] of this.pendingRequests) {
283
283
  clearTimeout(pending.timeout);
284
284
  pending.reject(error);
285
285
  }
@@ -7,8 +7,6 @@
7
7
  * @module clients/codex-utils
8
8
  */
9
9
 
10
- const logger = require("../logger");
11
-
12
10
  /**
13
11
  * Extract text content from Anthropic message format
14
12
  * Handles both string content and content block arrays