lynkr 8.0.0 → 9.0.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 (128) hide show
  1. package/.lynkr/telemetry.db +0 -0
  2. package/.lynkr/telemetry.db-shm +0 -0
  3. package/.lynkr/telemetry.db-wal +0 -0
  4. package/README.md +196 -322
  5. package/lynkr-skill.tar.gz +0 -0
  6. package/package.json +4 -3
  7. package/src/api/openai-router.js +64 -13
  8. package/src/api/providers-handler.js +171 -3
  9. package/src/api/router.js +9 -2
  10. package/src/clients/circuit-breaker.js +10 -247
  11. package/src/clients/codex-process.js +342 -0
  12. package/src/clients/codex-utils.js +143 -0
  13. package/src/clients/databricks.js +210 -63
  14. package/src/clients/resilience.js +540 -0
  15. package/src/clients/retry.js +22 -167
  16. package/src/clients/standard-tools.js +23 -0
  17. package/src/config/index.js +77 -0
  18. package/src/context/compression.js +42 -9
  19. package/src/context/distill.js +492 -0
  20. package/src/orchestrator/index.js +48 -8
  21. package/src/routing/complexity-analyzer.js +258 -5
  22. package/src/routing/index.js +12 -2
  23. package/src/routing/latency-tracker.js +148 -0
  24. package/src/routing/model-tiers.js +2 -0
  25. package/src/routing/quality-scorer.js +113 -0
  26. package/src/routing/telemetry.js +464 -0
  27. package/src/server.js +13 -12
  28. package/src/tools/code-graph.js +538 -0
  29. package/src/tools/code-mode.js +304 -0
  30. package/src/tools/index.js +4 -0
  31. package/src/tools/lazy-loader.js +18 -0
  32. package/src/tools/mcp-remote.js +7 -0
  33. package/src/tools/smart-selection.js +11 -0
  34. package/src/tools/tinyfish.js +358 -0
  35. package/src/tools/truncate.js +1 -0
  36. package/src/utils/payload.js +206 -0
  37. package/src/utils/perf-timer.js +80 -0
  38. package/.github/FUNDING.yml +0 -15
  39. package/.github/workflows/README.md +0 -215
  40. package/.github/workflows/ci.yml +0 -69
  41. package/.github/workflows/index.yml +0 -62
  42. package/.github/workflows/web-tools-tests.yml +0 -56
  43. package/CITATIONS.bib +0 -6
  44. package/DEPLOYMENT.md +0 -1001
  45. package/LYNKR-TUI-PLAN.md +0 -984
  46. package/PERFORMANCE-REPORT.md +0 -866
  47. package/PLAN-per-client-model-routing.md +0 -252
  48. package/docs/42642f749da6234f41b6b425c3bb07c9.txt +0 -1
  49. package/docs/BingSiteAuth.xml +0 -4
  50. package/docs/docs-style.css +0 -478
  51. package/docs/docs.html +0 -198
  52. package/docs/google5be250e608e6da39.html +0 -1
  53. package/docs/index.html +0 -577
  54. package/docs/index.md +0 -584
  55. package/docs/robots.txt +0 -4
  56. package/docs/sitemap.xml +0 -44
  57. package/docs/style.css +0 -1223
  58. package/docs/toon-integration-spec.md +0 -130
  59. package/documentation/README.md +0 -101
  60. package/documentation/api.md +0 -806
  61. package/documentation/claude-code-cli.md +0 -679
  62. package/documentation/codex-cli.md +0 -397
  63. package/documentation/contributing.md +0 -571
  64. package/documentation/cursor-integration.md +0 -734
  65. package/documentation/docker.md +0 -874
  66. package/documentation/embeddings.md +0 -762
  67. package/documentation/faq.md +0 -713
  68. package/documentation/features.md +0 -403
  69. package/documentation/headroom.md +0 -519
  70. package/documentation/installation.md +0 -758
  71. package/documentation/memory-system.md +0 -476
  72. package/documentation/production.md +0 -636
  73. package/documentation/providers.md +0 -1009
  74. package/documentation/routing.md +0 -476
  75. package/documentation/testing.md +0 -629
  76. package/documentation/token-optimization.md +0 -325
  77. package/documentation/tools.md +0 -697
  78. package/documentation/troubleshooting.md +0 -969
  79. package/final-test.js +0 -33
  80. package/headroom-sidecar/config.py +0 -93
  81. package/headroom-sidecar/requirements.txt +0 -14
  82. package/headroom-sidecar/server.py +0 -451
  83. package/monitor-agents.sh +0 -31
  84. package/scripts/audit-log-reader.js +0 -399
  85. package/scripts/compact-dictionary.js +0 -204
  86. package/scripts/test-deduplication.js +0 -448
  87. package/src/db/database.sqlite +0 -0
  88. package/te +0 -11622
  89. package/test/README.md +0 -212
  90. package/test/azure-openai-config.test.js +0 -213
  91. package/test/azure-openai-error-resilience.test.js +0 -238
  92. package/test/azure-openai-format-conversion.test.js +0 -354
  93. package/test/azure-openai-integration.test.js +0 -287
  94. package/test/azure-openai-routing.test.js +0 -175
  95. package/test/azure-openai-streaming.test.js +0 -171
  96. package/test/bedrock-integration.test.js +0 -457
  97. package/test/comprehensive-test-suite.js +0 -928
  98. package/test/config-validation.test.js +0 -207
  99. package/test/cursor-integration.test.js +0 -484
  100. package/test/format-conversion.test.js +0 -578
  101. package/test/hybrid-routing-integration.test.js +0 -269
  102. package/test/hybrid-routing-performance.test.js +0 -428
  103. package/test/llamacpp-integration.test.js +0 -882
  104. package/test/lmstudio-integration.test.js +0 -347
  105. package/test/memory/extractor.test.js +0 -398
  106. package/test/memory/retriever.test.js +0 -613
  107. package/test/memory/retriever.test.js.bak +0 -585
  108. package/test/memory/search.test.js +0 -537
  109. package/test/memory/search.test.js.bak +0 -389
  110. package/test/memory/store.test.js +0 -344
  111. package/test/memory/store.test.js.bak +0 -312
  112. package/test/memory/surprise.test.js +0 -300
  113. package/test/memory-performance.test.js +0 -472
  114. package/test/openai-integration.test.js +0 -683
  115. package/test/openrouter-error-resilience.test.js +0 -418
  116. package/test/passthrough-mode.test.js +0 -385
  117. package/test/performance-benchmark.js +0 -351
  118. package/test/performance-tests.js +0 -528
  119. package/test/routing.test.js +0 -225
  120. package/test/toon-compression.test.js +0 -131
  121. package/test/web-tools.test.js +0 -329
  122. package/test-agents-simple.js +0 -43
  123. package/test-cli-connection.sh +0 -33
  124. package/test-learning-unit.js +0 -126
  125. package/test-learning.js +0 -112
  126. package/test-parallel-agents.sh +0 -124
  127. package/test-parallel-direct.js +0 -155
  128. package/test-subagents.sh +0 -117
@@ -1,225 +0,0 @@
1
- const assert = require("assert");
2
- const { describe, it, beforeEach, afterEach } = require("node:test");
3
-
4
- describe("Routing Logic", () => {
5
- let config;
6
- let routing;
7
- let originalConfig;
8
-
9
- beforeEach(() => {
10
- // Clear module cache to get fresh instances
11
- delete require.cache[require.resolve("../src/config/index.js")];
12
- delete require.cache[require.resolve("../src/clients/routing")];
13
- delete require.cache[require.resolve("../src/routing/index.js")];
14
- delete require.cache[require.resolve("../src/routing/model-tiers")];
15
- delete require.cache[require.resolve("../src/routing/complexity-analyzer")];
16
- delete require.cache[require.resolve("../src/routing/cost-optimizer")];
17
- delete require.cache[require.resolve("../src/routing/agentic-detector")];
18
-
19
- // Store original config
20
- originalConfig = { ...process.env };
21
-
22
- // Explicitly set valid fallback to override any local .env pollution (e.g. lmstudio)
23
- process.env.FALLBACK_PROVIDER = "databricks";
24
-
25
- // Ensure no TIER_* vars leak between tests
26
- process.env.TIER_SIMPLE = "";
27
- process.env.TIER_MEDIUM = "";
28
- process.env.TIER_COMPLEX = "";
29
- process.env.TIER_REASONING = "";
30
- });
31
-
32
- afterEach(() => {
33
- // Restore original environment
34
- process.env = originalConfig;
35
- });
36
-
37
- describe("static routing (tier routing disabled)", () => {
38
- it("should return configured provider when tier routing is disabled", async () => {
39
- process.env.MODEL_PROVIDER = "databricks";
40
- process.env.DATABRICKS_API_KEY = "test-key";
41
- process.env.DATABRICKS_API_BASE = "http://test.com";
42
-
43
- config = require("../src/config");
44
- routing = require("../src/clients/routing");
45
-
46
- const payload = { messages: [{ role: "user", content: "test" }] };
47
- const result = await routing.determineProviderSmart(payload);
48
-
49
- assert.strictEqual(result.provider, "databricks");
50
- assert.strictEqual(result.method, "static");
51
- });
52
-
53
- it("should return ollama when MODEL_PROVIDER is ollama", async () => {
54
- process.env.MODEL_PROVIDER = "ollama";
55
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
56
-
57
- config = require("../src/config");
58
- routing = require("../src/clients/routing");
59
-
60
- const payload = {
61
- messages: [{ role: "user", content: "test" }],
62
- tools: [],
63
- };
64
-
65
- const result = await routing.determineProviderSmart(payload);
66
- assert.strictEqual(result.provider, "ollama");
67
- assert.strictEqual(result.method, "static");
68
- });
69
-
70
- it("should return primary provider regardless of tool count", async () => {
71
- process.env.MODEL_PROVIDER = "ollama";
72
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
73
-
74
- config = require("../src/config");
75
- routing = require("../src/clients/routing");
76
-
77
- const payload = {
78
- messages: [{ role: "user", content: "test" }],
79
- tools: [
80
- { name: "tool1", description: "test" },
81
- { name: "tool2", description: "test" },
82
- ],
83
- };
84
-
85
- const result = await routing.determineProviderSmart(payload);
86
- assert.strictEqual(result.provider, "ollama");
87
- assert.strictEqual(result.method, "static");
88
- });
89
-
90
- it("should return primary provider even with many tools", async () => {
91
- process.env.MODEL_PROVIDER = "databricks";
92
- process.env.DATABRICKS_API_KEY = "test-key";
93
- process.env.DATABRICKS_API_BASE = "http://test.com";
94
-
95
- config = require("../src/config");
96
- routing = require("../src/clients/routing");
97
-
98
- const payload = {
99
- messages: [{ role: "user", content: "test" }],
100
- tools: [
101
- { name: "tool1", description: "test" },
102
- { name: "tool2", description: "test" },
103
- { name: "tool3", description: "test" },
104
- { name: "tool4", description: "test" },
105
- { name: "tool5", description: "test" },
106
- ],
107
- };
108
-
109
- const result = await routing.determineProviderSmart(payload);
110
- assert.strictEqual(result.provider, "databricks");
111
- assert.strictEqual(result.method, "static");
112
- });
113
-
114
- it("should return configured MODEL_PROVIDER", async () => {
115
- process.env.MODEL_PROVIDER = "databricks";
116
- process.env.DATABRICKS_API_KEY = "test-key";
117
- process.env.DATABRICKS_API_BASE = "http://test.com";
118
-
119
- config = require("../src/config");
120
- routing = require("../src/clients/routing");
121
-
122
- const payload = {
123
- messages: [{ role: "user", content: "test" }],
124
- tools: [{ name: "tool1", description: "test" }],
125
- };
126
-
127
- const result = await routing.determineProviderSmart(payload);
128
- assert.strictEqual(result.provider, "databricks");
129
- assert.strictEqual(result.method, "static");
130
- });
131
- });
132
-
133
- describe("determineProviderSmart()", () => {
134
- it("should return static routing when tier routing is disabled (no TIER_* vars)", async () => {
135
- process.env.MODEL_PROVIDER = "databricks";
136
- process.env.DATABRICKS_API_KEY = "test-key";
137
- process.env.DATABRICKS_API_BASE = "http://test.com";
138
-
139
- config = require("../src/config");
140
- routing = require("../src/clients/routing");
141
-
142
- const payload = { messages: [{ role: "user", content: "test" }] };
143
- const result = await routing.determineProviderSmart(payload);
144
-
145
- assert.strictEqual(result.provider, "databricks");
146
- assert.strictEqual(result.method, "static");
147
- assert.strictEqual(result.reason, "tier_routing_disabled");
148
- assert.strictEqual(result.model, null);
149
- });
150
-
151
- it("should use tier routing when TIER_* vars are set", async () => {
152
- process.env.MODEL_PROVIDER = "ollama";
153
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
154
- process.env.TIER_SIMPLE = "ollama:llama3.2";
155
- process.env.TIER_MEDIUM = "ollama:llama3.2";
156
- process.env.TIER_COMPLEX = "databricks:claude-sonnet";
157
- process.env.TIER_REASONING = "databricks:claude-sonnet";
158
- process.env.DATABRICKS_API_KEY = "test-key";
159
- process.env.DATABRICKS_API_BASE = "http://test.com";
160
-
161
- config = require("../src/config");
162
- routing = require("../src/clients/routing");
163
-
164
- const payload = { messages: [{ role: "user", content: "test" }] };
165
- const result = await routing.determineProviderSmart(payload);
166
-
167
- // When tier routing is enabled, method should not be 'static'
168
- assert.notStrictEqual(result.method, "static");
169
- assert.ok(result.provider, "provider should be set");
170
- });
171
- });
172
-
173
- describe("isFallbackEnabled()", () => {
174
- it("should return true by default", () => {
175
- process.env.MODEL_PROVIDER = "ollama";
176
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
177
- // Override .env file which sets FALLBACK_ENABLED=false
178
- // Test default behavior when not set to "false"
179
- process.env.FALLBACK_ENABLED = "true";
180
-
181
- config = require("../src/config");
182
- routing = require("../src/clients/routing");
183
-
184
- assert.strictEqual(routing.isFallbackEnabled(), true);
185
- });
186
-
187
- it("should return false when explicitly disabled", () => {
188
- process.env.MODEL_PROVIDER = "ollama";
189
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
190
- process.env.FALLBACK_ENABLED = "false";
191
-
192
- config = require("../src/config");
193
- routing = require("../src/clients/routing");
194
-
195
- assert.strictEqual(routing.isFallbackEnabled(), false);
196
- });
197
- });
198
-
199
- describe("getFallbackProvider()", () => {
200
- it("should return databricks by default", () => {
201
- process.env.MODEL_PROVIDER = "ollama";
202
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
203
- process.env.DATABRICKS_API_KEY = "test-key";
204
- process.env.DATABRICKS_API_BASE = "http://test.com";
205
-
206
- config = require("../src/config");
207
- routing = require("../src/clients/routing");
208
-
209
- assert.strictEqual(routing.getFallbackProvider(), "databricks");
210
- });
211
-
212
- it("should return configured fallback provider", () => {
213
- process.env.MODEL_PROVIDER = "ollama";
214
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
215
- process.env.FALLBACK_PROVIDER = "azure-anthropic";
216
- process.env.AZURE_ANTHROPIC_ENDPOINT = "http://test.com";
217
- process.env.AZURE_ANTHROPIC_API_KEY = "test-key";
218
-
219
- config = require("../src/config");
220
- routing = require("../src/clients/routing");
221
-
222
- assert.strictEqual(routing.getFallbackProvider(), "azure-anthropic");
223
- });
224
- });
225
- });
@@ -1,131 +0,0 @@
1
- const assert = require("assert");
2
- const { describe, it } = require("node:test");
3
-
4
- const { applyToonCompression } = require("../src/context/toon");
5
-
6
- function createLargeJsonString() {
7
- return JSON.stringify({
8
- rows: Array.from({ length: 8 }, (_, idx) => ({
9
- id: idx + 1,
10
- label: `item-${idx + 1}`,
11
- value: `value-${idx + 1}`.repeat(20),
12
- })),
13
- });
14
- }
15
-
16
- describe("TOON compression", () => {
17
- it("is a no-op when TOON is disabled", () => {
18
- const payload = {
19
- stream: false,
20
- tool_choice: { type: "auto" },
21
- tools: [{ name: "Read", input_schema: { type: "object", properties: {} } }],
22
- messages: [{ role: "user", content: createLargeJsonString() }],
23
- };
24
- const before = JSON.parse(JSON.stringify(payload));
25
-
26
- const { payload: after, stats } = applyToonCompression(
27
- payload,
28
- { enabled: false, minBytes: 1, failOpen: true },
29
- { encode: () => "should-not-run" },
30
- );
31
-
32
- assert.deepStrictEqual(after, before);
33
- assert.strictEqual(stats.enabled, false);
34
- assert.strictEqual(stats.convertedCount, 0);
35
- });
36
-
37
- it("falls back safely when encoder throws (fail-open)", () => {
38
- const payload = {
39
- messages: [{ role: "user", content: createLargeJsonString() }],
40
- };
41
- const original = payload.messages[0].content;
42
-
43
- const { payload: after, stats } = applyToonCompression(
44
- payload,
45
- { enabled: true, minBytes: 1, failOpen: true, logStats: false },
46
- {
47
- encode: () => {
48
- throw new Error("simulated toon encode failure");
49
- },
50
- },
51
- );
52
-
53
- assert.strictEqual(after.messages[0].content, original);
54
- assert.strictEqual(stats.failureCount, 1);
55
- assert.strictEqual(stats.convertedCount, 0);
56
- });
57
-
58
- it("does not mutate protocol fields while compressing eligible message content", () => {
59
- const payload = {
60
- model: "kimi-k2.5",
61
- stream: true,
62
- tool_choice: { type: "tool", name: "Read" },
63
- tools: [
64
- {
65
- name: "Read",
66
- description: "Read files",
67
- input_schema: {
68
- type: "object",
69
- properties: { file_path: { type: "string" } },
70
- required: ["file_path"],
71
- },
72
- },
73
- ],
74
- messages: [
75
- { role: "user", content: createLargeJsonString() },
76
- { role: "tool", content: createLargeJsonString() }, // tool role should never be touched
77
- ],
78
- };
79
- const beforeTools = JSON.parse(JSON.stringify(payload.tools));
80
- const beforeToolChoice = JSON.parse(JSON.stringify(payload.tool_choice));
81
- const beforeToolRoleContent = payload.messages[1].content;
82
-
83
- const { payload: after, stats } = applyToonCompression(
84
- payload,
85
- { enabled: true, minBytes: 1, failOpen: false, logStats: false },
86
- { encode: () => "rows[1]{id,label,value}:\n 1,item-1,value-1" },
87
- );
88
-
89
- assert.strictEqual(after.messages[0].content, "rows[1]{id,label,value}:\n 1,item-1,value-1");
90
- assert.strictEqual(after.messages[1].content, beforeToolRoleContent);
91
- assert.deepStrictEqual(after.tools, beforeTools);
92
- assert.deepStrictEqual(after.tool_choice, beforeToolChoice);
93
- assert.strictEqual(after.stream, true);
94
- assert.strictEqual(after.model, "kimi-k2.5");
95
- assert.strictEqual(stats.convertedCount, 1);
96
- });
97
-
98
- it("compresses Anthropic text blocks while preserving tool protocol blocks", () => {
99
- const largeJson = createLargeJsonString();
100
- const payload = {
101
- messages: [
102
- {
103
- role: "user",
104
- content: [
105
- { type: "text", text: largeJson },
106
- { type: "input_text", input_text: largeJson },
107
- {
108
- type: "tool_result",
109
- tool_use_id: "toolu_123",
110
- content: largeJson,
111
- is_error: false,
112
- },
113
- ],
114
- },
115
- ],
116
- };
117
-
118
- const originalToolResultContent = payload.messages[0].content[2].content;
119
-
120
- const { payload: after, stats } = applyToonCompression(
121
- payload,
122
- { enabled: true, minBytes: 1, failOpen: false, logStats: false },
123
- { encode: () => "rows[1]{id,label,value}:\n 1,item-1,value-1" },
124
- );
125
-
126
- assert.strictEqual(after.messages[0].content[0].text, "rows[1]{id,label,value}:\n 1,item-1,value-1");
127
- assert.strictEqual(after.messages[0].content[1].input_text, "rows[1]{id,label,value}:\n 1,item-1,value-1");
128
- assert.strictEqual(after.messages[0].content[2].content, originalToolResultContent);
129
- assert.strictEqual(stats.convertedCount, 2);
130
- });
131
- });
@@ -1,329 +0,0 @@
1
- const { describe, it, before, after } = require("node:test");
2
- const assert = require("node:assert");
3
- const http = require("http");
4
-
5
- // Mock configuration
6
- process.env.MODEL_PROVIDER = "databricks"; // Override any .env settings
7
- process.env.DATABRICKS_API_KEY = "test-key";
8
- process.env.DATABRICKS_API_BASE = "http://test.com";
9
- process.env.WEB_SEARCH_ENDPOINT = "http://localhost:9999/search";
10
- process.env.WEB_SEARCH_TIMEOUT_MS = "5000";
11
- process.env.WEB_FETCH_BODY_PREVIEW_MAX = "1000";
12
- process.env.WEB_SEARCH_RETRY_ENABLED = "true";
13
- process.env.WEB_SEARCH_MAX_RETRIES = "2";
14
-
15
- const config = require("../src/config");
16
- const { webAgent, getAgentStats, fetchWithAgent } = require("../src/tools/web-client");
17
-
18
- describe("Web Tools Tests", () => {
19
- describe("HTML Extraction", () => {
20
- // Import the extraction function by loading the module
21
- let extractTextFromHtml;
22
-
23
- before(() => {
24
- // Recreate the HTML extraction function for testing
25
- extractTextFromHtml = function(html) {
26
- if (typeof html !== "string") return "";
27
- let text = html;
28
- text = text.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, " ");
29
- text = text.replace(/<style\b[^<]*(?:(?!<\/style>)<[^<]*)*<\/style>/gi, " ");
30
- text = text.replace(/<!--[\s\S]*?-->/g, " ");
31
- text = text.replace(/<\/(div|p|br|h[1-6]|li|tr|section|article|header|footer|nav)>/gi, "\n");
32
- text = text.replace(/<[^>]+>/g, " ");
33
- text = text
34
- .replace(/&nbsp;/g, " ")
35
- .replace(/&amp;/g, "&")
36
- .replace(/&lt;/g, "<")
37
- .replace(/&gt;/g, ">")
38
- .replace(/&quot;/g, '"')
39
- .replace(/&#39;/g, "'")
40
- .replace(/&apos;/g, "'");
41
- text = text.replace(/\r\n/g, "\n");
42
- text = text.replace(/\r/g, "\n");
43
- text = text.replace(/[ \t]+/g, " ");
44
- text = text.replace(/\n\s+/g, "\n");
45
- text = text.replace(/\n{3,}/g, "\n\n");
46
- return text.trim();
47
- };
48
- });
49
-
50
- it("should extract text from simple HTML", () => {
51
- const html = "<p>Hello World</p>";
52
- const result = extractTextFromHtml(html);
53
- assert.strictEqual(result, "Hello World");
54
- });
55
-
56
- it("should remove script tags and content", () => {
57
- const html = "<div>Content<script>alert('test')</script>More</div>";
58
- const result = extractTextFromHtml(html);
59
- assert.ok(!result.includes("alert"));
60
- assert.ok(result.includes("Content"));
61
- assert.ok(result.includes("More"));
62
- });
63
-
64
- it("should remove style tags and content", () => {
65
- const html = "<div>Text<style>body { color: red; }</style>More</div>";
66
- const result = extractTextFromHtml(html);
67
- assert.ok(!result.includes("color"));
68
- assert.ok(result.includes("Text"));
69
- assert.ok(result.includes("More"));
70
- });
71
-
72
- it("should decode HTML entities", () => {
73
- const html = "<p>&nbsp;&amp;&lt;&gt;&quot;&#39;</p>";
74
- const result = extractTextFromHtml(html);
75
- assert.ok(result.includes("&"));
76
- assert.ok(result.includes("<"));
77
- assert.ok(result.includes(">"));
78
- assert.ok(result.includes('"'));
79
- assert.ok(result.includes("'"));
80
- });
81
-
82
- it("should convert block elements to newlines", () => {
83
- const html = "<div>Line 1</div><p>Line 2</p><h1>Line 3</h1>";
84
- const result = extractTextFromHtml(html);
85
- const lines = result.split("\n").filter(l => l.trim());
86
- assert.strictEqual(lines.length, 3);
87
- });
88
-
89
- it("should normalize whitespace", () => {
90
- const html = "<p>Text with spaces</p>";
91
- const result = extractTextFromHtml(html);
92
- assert.strictEqual(result, "Text with spaces");
93
- });
94
-
95
- it("should handle empty or non-string input", () => {
96
- assert.strictEqual(extractTextFromHtml(""), "");
97
- assert.strictEqual(extractTextFromHtml(null), "");
98
- assert.strictEqual(extractTextFromHtml(undefined), "");
99
- assert.strictEqual(extractTextFromHtml(123), "");
100
- });
101
-
102
- it("should remove HTML comments", () => {
103
- const html = "<div>Text<!-- comment here -->More</div>";
104
- const result = extractTextFromHtml(html);
105
- assert.ok(!result.includes("comment"));
106
- assert.ok(result.includes("Text"));
107
- assert.ok(result.includes("More"));
108
- });
109
-
110
- it("should handle complex nested HTML", () => {
111
- const html = `
112
- <html>
113
- <head><title>Test</title></head>
114
- <body>
115
- <header><h1>Title</h1></header>
116
- <div class="content">
117
- <p>Paragraph 1</p>
118
- <ul>
119
- <li>Item 1</li>
120
- <li>Item 2</li>
121
- </ul>
122
- </div>
123
- <footer>Footer</footer>
124
- </body>
125
- </html>
126
- `;
127
- const result = extractTextFromHtml(html);
128
- assert.ok(result.includes("Test"));
129
- assert.ok(result.includes("Title"));
130
- assert.ok(result.includes("Paragraph 1"));
131
- assert.ok(result.includes("Item 1"));
132
- assert.ok(result.includes("Footer"));
133
- });
134
- });
135
-
136
- describe("Web Client Agent", () => {
137
- it("should create agent with correct configuration", () => {
138
- assert.ok(webAgent, "Agent should be created");
139
- const stats = getAgentStats();
140
- assert.strictEqual(stats.agent, "undici");
141
- assert.strictEqual(stats.keepAlive, true);
142
- assert.strictEqual(stats.maxConnections, 50);
143
- assert.strictEqual(stats.pipelining, 10);
144
- });
145
-
146
- it("should have fetchWithAgent function", () => {
147
- assert.strictEqual(typeof fetchWithAgent, "function");
148
- });
149
- });
150
-
151
- describe("Configuration", () => {
152
- it("should load web search configuration correctly", () => {
153
- assert.ok(config.webSearch, "webSearch config should exist");
154
- assert.strictEqual(config.webSearch.enabled, true);
155
- assert.strictEqual(config.webSearch.endpoint, "http://localhost:9999/search");
156
- assert.strictEqual(config.webSearch.timeoutMs, 5000);
157
- assert.strictEqual(config.webSearch.bodyPreviewMax, 1000);
158
- assert.strictEqual(config.webSearch.retryEnabled, true);
159
- assert.strictEqual(config.webSearch.maxRetries, 2);
160
- });
161
-
162
- it("should have retry configuration", () => {
163
- assert.strictEqual(config.webSearch.retryEnabled, true);
164
- assert.strictEqual(config.webSearch.maxRetries, 2);
165
- });
166
-
167
- it("should have configurable body preview max", () => {
168
- assert.strictEqual(config.webSearch.bodyPreviewMax, 1000);
169
- });
170
- });
171
-
172
- describe("Retry Logic Integration", () => {
173
- let mockServer;
174
- let requestCounts;
175
-
176
- before((done) => {
177
- requestCounts = {};
178
- mockServer = http.createServer((req, res) => {
179
- const url = req.url;
180
- requestCounts[url] = (requestCounts[url] || 0) + 1;
181
-
182
- if (url === "/fail-twice") {
183
- if (requestCounts[url] <= 2) {
184
- res.writeHead(500, { "Content-Type": "application/json" });
185
- res.end(JSON.stringify({ error: "Server error" }));
186
- } else {
187
- res.writeHead(200, { "Content-Type": "application/json" });
188
- res.end(JSON.stringify({ success: true }));
189
- }
190
- } else if (url === "/timeout") {
191
- // Don't respond to simulate timeout
192
- setTimeout(() => {
193
- res.writeHead(200);
194
- res.end("OK");
195
- }, 10000);
196
- } else if (url === "/rate-limit") {
197
- if (requestCounts[url] <= 2) {
198
- res.writeHead(429, {
199
- "Content-Type": "application/json",
200
- "Retry-After": "1"
201
- });
202
- res.end(JSON.stringify({ error: "Rate limited" }));
203
- } else {
204
- res.writeHead(200, { "Content-Type": "application/json" });
205
- res.end(JSON.stringify({ success: true }));
206
- }
207
- } else {
208
- res.writeHead(200, { "Content-Type": "application/json" });
209
- res.end(JSON.stringify({ data: "ok" }));
210
- }
211
- });
212
-
213
- mockServer.listen(9999, done);
214
- });
215
-
216
- after((done) => {
217
- if (mockServer) {
218
- mockServer.close(done);
219
- } else {
220
- done();
221
- }
222
- });
223
-
224
- it("should retry on server errors and eventually succeed", async () => {
225
- const { withRetry } = require("../src/clients/retry");
226
-
227
- const response = await withRetry(async () => {
228
- return await fetchWithAgent("http://localhost:9999/fail-twice");
229
- }, {
230
- maxRetries: 3,
231
- initialDelay: 50,
232
- maxDelay: 200,
233
- });
234
-
235
- // After retries, should get successful response
236
- assert.ok(response.ok, "Response should be ok after retries");
237
- const result = await response.json();
238
- assert.ok(result.success);
239
- assert.ok(requestCounts["/fail-twice"] >= 3, `Expected at least 3 requests, got ${requestCounts["/fail-twice"]}`);
240
- });
241
-
242
- it("should handle 429 rate limiting with retry", async () => {
243
- const { withRetry } = require("../src/clients/retry");
244
-
245
- const response = await withRetry(async () => {
246
- return await fetchWithAgent("http://localhost:9999/rate-limit");
247
- }, {
248
- maxRetries: 3,
249
- initialDelay: 50,
250
- maxDelay: 200,
251
- });
252
-
253
- assert.ok(response.ok, "Should eventually succeed after retries");
254
- const result = await response.json();
255
- assert.ok(result.success, "Result should indicate success");
256
- assert.ok(requestCounts["/rate-limit"] >= 2, "Should have retried at least once");
257
- });
258
- });
259
-
260
- describe("Error Handling", () => {
261
- it("should categorize error codes correctly", () => {
262
- // Test that error codes are properly set
263
- const testError = (status, expectedCode) => {
264
- const error = new Error("Test error");
265
- error.status = status;
266
- error.code = status === 429 ? "RATE_LIMITED" :
267
- status >= 500 ? "SERVER_ERROR" : "REQUEST_ERROR";
268
-
269
- assert.strictEqual(error.code, expectedCode);
270
- };
271
-
272
- testError(429, "RATE_LIMITED");
273
- testError(500, "SERVER_ERROR");
274
- testError(502, "SERVER_ERROR");
275
- testError(503, "SERVER_ERROR");
276
- testError(400, "REQUEST_ERROR");
277
- testError(404, "REQUEST_ERROR");
278
- });
279
- });
280
-
281
- describe("Performance", () => {
282
- let testServer;
283
-
284
- before((done) => {
285
- testServer = http.createServer((req, res) => {
286
- res.writeHead(200, { "Content-Type": "text/plain" });
287
- res.end("OK");
288
- });
289
- testServer.listen(9998, done);
290
- });
291
-
292
- after((done) => {
293
- if (testServer) {
294
- testServer.close(done);
295
- } else {
296
- done();
297
- }
298
- });
299
-
300
- it("should reuse connections with keep-alive", async () => {
301
- const times = [];
302
-
303
- // Make 5 sequential requests
304
- for (let i = 0; i < 5; i++) {
305
- const start = Date.now();
306
- await fetchWithAgent("http://localhost:9998/");
307
- times.push(Date.now() - start);
308
- }
309
-
310
- // First request typically slower (connection setup)
311
- // Subsequent requests should be faster (reused connection)
312
- const avgSubsequent = times.slice(1).reduce((a, b) => a + b, 0) / (times.length - 1);
313
-
314
- // Subsequent requests should be reasonably fast
315
- assert.ok(avgSubsequent < 100, `Subsequent requests too slow: ${avgSubsequent}ms`);
316
- });
317
- });
318
-
319
- describe("Body Preview Configuration", () => {
320
- it("should limit body preview to configured max", () => {
321
- const maxLength = config.webSearch.bodyPreviewMax;
322
- const longContent = "x".repeat(maxLength * 2);
323
- const preview = longContent.slice(0, maxLength);
324
-
325
- assert.strictEqual(preview.length, maxLength);
326
- assert.ok(preview.length < longContent.length);
327
- });
328
- });
329
- });