lynkr 8.0.0 → 8.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 (102) hide show
  1. package/README.md +1 -1
  2. package/package.json +1 -1
  3. package/src/api/openai-router.js +34 -2
  4. package/src/clients/standard-tools.js +23 -0
  5. package/src/config/index.js +20 -0
  6. package/src/orchestrator/index.js +2 -2
  7. package/src/server.js +2 -12
  8. package/src/tools/index.js +4 -0
  9. package/src/tools/lazy-loader.js +7 -0
  10. package/src/tools/tinyfish.js +358 -0
  11. package/src/tools/truncate.js +1 -0
  12. package/.github/FUNDING.yml +0 -15
  13. package/.github/workflows/README.md +0 -215
  14. package/.github/workflows/ci.yml +0 -69
  15. package/.github/workflows/index.yml +0 -62
  16. package/.github/workflows/web-tools-tests.yml +0 -56
  17. package/CITATIONS.bib +0 -6
  18. package/DEPLOYMENT.md +0 -1001
  19. package/LYNKR-TUI-PLAN.md +0 -984
  20. package/PERFORMANCE-REPORT.md +0 -866
  21. package/PLAN-per-client-model-routing.md +0 -252
  22. package/docs/42642f749da6234f41b6b425c3bb07c9.txt +0 -1
  23. package/docs/BingSiteAuth.xml +0 -4
  24. package/docs/docs-style.css +0 -478
  25. package/docs/docs.html +0 -198
  26. package/docs/google5be250e608e6da39.html +0 -1
  27. package/docs/index.html +0 -577
  28. package/docs/index.md +0 -584
  29. package/docs/robots.txt +0 -4
  30. package/docs/sitemap.xml +0 -44
  31. package/docs/style.css +0 -1223
  32. package/docs/toon-integration-spec.md +0 -130
  33. package/documentation/README.md +0 -101
  34. package/documentation/api.md +0 -806
  35. package/documentation/claude-code-cli.md +0 -679
  36. package/documentation/codex-cli.md +0 -397
  37. package/documentation/contributing.md +0 -571
  38. package/documentation/cursor-integration.md +0 -734
  39. package/documentation/docker.md +0 -874
  40. package/documentation/embeddings.md +0 -762
  41. package/documentation/faq.md +0 -713
  42. package/documentation/features.md +0 -403
  43. package/documentation/headroom.md +0 -519
  44. package/documentation/installation.md +0 -758
  45. package/documentation/memory-system.md +0 -476
  46. package/documentation/production.md +0 -636
  47. package/documentation/providers.md +0 -1009
  48. package/documentation/routing.md +0 -476
  49. package/documentation/testing.md +0 -629
  50. package/documentation/token-optimization.md +0 -325
  51. package/documentation/tools.md +0 -697
  52. package/documentation/troubleshooting.md +0 -969
  53. package/final-test.js +0 -33
  54. package/headroom-sidecar/config.py +0 -93
  55. package/headroom-sidecar/requirements.txt +0 -14
  56. package/headroom-sidecar/server.py +0 -451
  57. package/monitor-agents.sh +0 -31
  58. package/scripts/audit-log-reader.js +0 -399
  59. package/scripts/compact-dictionary.js +0 -204
  60. package/scripts/test-deduplication.js +0 -448
  61. package/src/db/database.sqlite +0 -0
  62. package/te +0 -11622
  63. package/test/README.md +0 -212
  64. package/test/azure-openai-config.test.js +0 -213
  65. package/test/azure-openai-error-resilience.test.js +0 -238
  66. package/test/azure-openai-format-conversion.test.js +0 -354
  67. package/test/azure-openai-integration.test.js +0 -287
  68. package/test/azure-openai-routing.test.js +0 -175
  69. package/test/azure-openai-streaming.test.js +0 -171
  70. package/test/bedrock-integration.test.js +0 -457
  71. package/test/comprehensive-test-suite.js +0 -928
  72. package/test/config-validation.test.js +0 -207
  73. package/test/cursor-integration.test.js +0 -484
  74. package/test/format-conversion.test.js +0 -578
  75. package/test/hybrid-routing-integration.test.js +0 -269
  76. package/test/hybrid-routing-performance.test.js +0 -428
  77. package/test/llamacpp-integration.test.js +0 -882
  78. package/test/lmstudio-integration.test.js +0 -347
  79. package/test/memory/extractor.test.js +0 -398
  80. package/test/memory/retriever.test.js +0 -613
  81. package/test/memory/retriever.test.js.bak +0 -585
  82. package/test/memory/search.test.js +0 -537
  83. package/test/memory/search.test.js.bak +0 -389
  84. package/test/memory/store.test.js +0 -344
  85. package/test/memory/store.test.js.bak +0 -312
  86. package/test/memory/surprise.test.js +0 -300
  87. package/test/memory-performance.test.js +0 -472
  88. package/test/openai-integration.test.js +0 -683
  89. package/test/openrouter-error-resilience.test.js +0 -418
  90. package/test/passthrough-mode.test.js +0 -385
  91. package/test/performance-benchmark.js +0 -351
  92. package/test/performance-tests.js +0 -528
  93. package/test/routing.test.js +0 -225
  94. package/test/toon-compression.test.js +0 -131
  95. package/test/web-tools.test.js +0 -329
  96. package/test-agents-simple.js +0 -43
  97. package/test-cli-connection.sh +0 -33
  98. package/test-learning-unit.js +0 -126
  99. package/test-learning.js +0 -112
  100. package/test-parallel-agents.sh +0 -124
  101. package/test-parallel-direct.js +0 -155
  102. 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
- });