lynkr 7.2.5 → 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 (124) hide show
  1. package/README.md +3 -3
  2. package/config/model-tiers.json +89 -0
  3. package/install.sh +6 -1
  4. package/package.json +4 -2
  5. package/scripts/setup.js +0 -1
  6. package/src/agents/executor.js +14 -6
  7. package/src/api/middleware/session.js +15 -2
  8. package/src/api/openai-router.js +162 -37
  9. package/src/api/providers-handler.js +15 -1
  10. package/src/api/router.js +107 -2
  11. package/src/budget/index.js +4 -3
  12. package/src/clients/databricks.js +431 -234
  13. package/src/clients/gpt-utils.js +181 -0
  14. package/src/clients/ollama-utils.js +66 -140
  15. package/src/clients/routing.js +0 -1
  16. package/src/clients/standard-tools.js +99 -3
  17. package/src/config/index.js +133 -35
  18. package/src/context/toon.js +173 -0
  19. package/src/logger/index.js +23 -0
  20. package/src/orchestrator/index.js +688 -213
  21. package/src/routing/agentic-detector.js +320 -0
  22. package/src/routing/complexity-analyzer.js +202 -2
  23. package/src/routing/cost-optimizer.js +305 -0
  24. package/src/routing/index.js +168 -159
  25. package/src/routing/model-tiers.js +365 -0
  26. package/src/server.js +4 -14
  27. package/src/sessions/cleanup.js +3 -3
  28. package/src/sessions/record.js +10 -1
  29. package/src/sessions/store.js +7 -2
  30. package/src/tools/agent-task.js +48 -1
  31. package/src/tools/index.js +19 -2
  32. package/src/tools/lazy-loader.js +7 -0
  33. package/src/tools/tinyfish.js +358 -0
  34. package/src/tools/truncate.js +1 -0
  35. package/.github/FUNDING.yml +0 -15
  36. package/.github/workflows/README.md +0 -215
  37. package/.github/workflows/ci.yml +0 -69
  38. package/.github/workflows/index.yml +0 -62
  39. package/.github/workflows/web-tools-tests.yml +0 -56
  40. package/CITATIONS.bib +0 -6
  41. package/CLAWROUTER_ROUTING_PLAN.md +0 -910
  42. package/DEPLOYMENT.md +0 -1001
  43. package/LYNKR-TUI-PLAN.md +0 -984
  44. package/PERFORMANCE-REPORT.md +0 -866
  45. package/PLAN-per-client-model-routing.md +0 -252
  46. package/ROUTER_COMPARISON.md +0 -173
  47. package/TIER_ROUTING_PLAN.md +0 -771
  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 -197
  52. package/docs/google5be250e608e6da39.html +0 -1
  53. package/docs/index.html +0 -577
  54. package/docs/index.md +0 -577
  55. package/docs/robots.txt +0 -4
  56. package/docs/sitemap.xml +0 -44
  57. package/docs/style.css +0 -1223
  58. package/documentation/README.md +0 -100
  59. package/documentation/api.md +0 -806
  60. package/documentation/claude-code-cli.md +0 -672
  61. package/documentation/codex-cli.md +0 -397
  62. package/documentation/contributing.md +0 -571
  63. package/documentation/cursor-integration.md +0 -731
  64. package/documentation/docker.md +0 -867
  65. package/documentation/embeddings.md +0 -760
  66. package/documentation/faq.md +0 -659
  67. package/documentation/features.md +0 -396
  68. package/documentation/headroom.md +0 -519
  69. package/documentation/installation.md +0 -706
  70. package/documentation/memory-system.md +0 -476
  71. package/documentation/production.md +0 -601
  72. package/documentation/providers.md +0 -906
  73. package/documentation/testing.md +0 -629
  74. package/documentation/token-optimization.md +0 -323
  75. package/documentation/tools.md +0 -697
  76. package/documentation/troubleshooting.md +0 -893
  77. package/final-test.js +0 -33
  78. package/headroom-sidecar/config.py +0 -93
  79. package/headroom-sidecar/requirements.txt +0 -14
  80. package/headroom-sidecar/server.py +0 -451
  81. package/monitor-agents.sh +0 -31
  82. package/scripts/audit-log-reader.js +0 -399
  83. package/scripts/compact-dictionary.js +0 -204
  84. package/scripts/test-deduplication.js +0 -448
  85. package/src/db/database.sqlite +0 -0
  86. package/test/README.md +0 -212
  87. package/test/azure-openai-config.test.js +0 -204
  88. package/test/azure-openai-error-resilience.test.js +0 -238
  89. package/test/azure-openai-format-conversion.test.js +0 -354
  90. package/test/azure-openai-integration.test.js +0 -281
  91. package/test/azure-openai-routing.test.js +0 -177
  92. package/test/azure-openai-streaming.test.js +0 -171
  93. package/test/bedrock-integration.test.js +0 -471
  94. package/test/comprehensive-test-suite.js +0 -928
  95. package/test/config-validation.test.js +0 -207
  96. package/test/cursor-integration.test.js +0 -484
  97. package/test/format-conversion.test.js +0 -578
  98. package/test/hybrid-routing-integration.test.js +0 -254
  99. package/test/hybrid-routing-performance.test.js +0 -418
  100. package/test/llamacpp-integration.test.js +0 -863
  101. package/test/lmstudio-integration.test.js +0 -335
  102. package/test/memory/extractor.test.js +0 -398
  103. package/test/memory/retriever.test.js +0 -613
  104. package/test/memory/retriever.test.js.bak +0 -585
  105. package/test/memory/search.test.js +0 -537
  106. package/test/memory/search.test.js.bak +0 -389
  107. package/test/memory/store.test.js +0 -344
  108. package/test/memory/store.test.js.bak +0 -312
  109. package/test/memory/surprise.test.js +0 -300
  110. package/test/memory-performance.test.js +0 -472
  111. package/test/openai-integration.test.js +0 -686
  112. package/test/openrouter-error-resilience.test.js +0 -418
  113. package/test/passthrough-mode.test.js +0 -385
  114. package/test/performance-benchmark.js +0 -351
  115. package/test/performance-tests.js +0 -528
  116. package/test/routing.test.js +0 -219
  117. package/test/web-tools.test.js +0 -329
  118. package/test-agents-simple.js +0 -43
  119. package/test-cli-connection.sh +0 -33
  120. package/test-learning-unit.js +0 -126
  121. package/test-learning.js +0 -112
  122. package/test-parallel-agents.sh +0 -124
  123. package/test-parallel-direct.js +0 -155
  124. package/test-subagents.sh +0 -117
@@ -1,686 +0,0 @@
1
- const assert = require("assert");
2
- const { describe, it, beforeEach, afterEach } = require("node:test");
3
-
4
- describe("OpenAI Integration", () => {
5
- let originalEnv;
6
-
7
- beforeEach(() => {
8
- originalEnv = { ...process.env };
9
-
10
- // Clear module cache
11
- delete require.cache[require.resolve("../src/config")];
12
- delete require.cache[require.resolve("../src/clients/routing")];
13
- delete require.cache[require.resolve("../src/clients/openrouter-utils")];
14
- });
15
-
16
- afterEach(() => {
17
- process.env = originalEnv;
18
- });
19
-
20
- describe("Configuration", () => {
21
- it("should accept openai as a valid MODEL_PROVIDER", () => {
22
- process.env.MODEL_PROVIDER = "openai";
23
- process.env.OPENAI_API_KEY = "sk-test-key";
24
-
25
- const config = require("../src/config");
26
- assert.strictEqual(config.modelProvider.type, "openai");
27
- });
28
-
29
- it("should throw error when OPENAI_API_KEY is missing", () => {
30
- process.env.MODEL_PROVIDER = "openai";
31
- delete process.env.OPENAI_API_KEY;
32
-
33
- assert.throws(
34
- () => require("../src/config"),
35
- /Set OPENAI_API_KEY before starting the proxy/
36
- );
37
- });
38
-
39
- it("should use default model when OPENAI_MODEL is not set", () => {
40
- process.env.MODEL_PROVIDER = "openai";
41
- process.env.OPENAI_API_KEY = "sk-test-key";
42
- delete process.env.OPENAI_MODEL;
43
-
44
- const config = require("../src/config");
45
- assert.strictEqual(config.openai.model, "gpt-4o");
46
- });
47
-
48
- it("should use custom model when OPENAI_MODEL is set", () => {
49
- process.env.MODEL_PROVIDER = "openai";
50
- process.env.OPENAI_API_KEY = "sk-test-key";
51
- process.env.OPENAI_MODEL = "gpt-4o-mini";
52
-
53
- const config = require("../src/config");
54
- assert.strictEqual(config.openai.model, "gpt-4o-mini");
55
- });
56
-
57
- it("should use default endpoint when OPENAI_ENDPOINT is not set", () => {
58
- process.env.MODEL_PROVIDER = "openai";
59
- process.env.OPENAI_API_KEY = "sk-test-key";
60
- delete process.env.OPENAI_ENDPOINT;
61
-
62
- const config = require("../src/config");
63
- assert.strictEqual(config.openai.endpoint, "https://api.openai.com/v1/chat/completions");
64
- });
65
-
66
- it("should use custom endpoint when OPENAI_ENDPOINT is set", () => {
67
- process.env.MODEL_PROVIDER = "openai";
68
- process.env.OPENAI_API_KEY = "sk-test-key";
69
- process.env.OPENAI_ENDPOINT = "https://custom.openai.com/v1/chat/completions";
70
-
71
- const config = require("../src/config");
72
- assert.strictEqual(config.openai.endpoint, "https://custom.openai.com/v1/chat/completions");
73
- });
74
-
75
- it("should store organization when OPENAI_ORGANIZATION is set", () => {
76
- process.env.MODEL_PROVIDER = "openai";
77
- process.env.OPENAI_API_KEY = "sk-test-key";
78
- process.env.OPENAI_ORGANIZATION = "org-test123";
79
-
80
- const config = require("../src/config");
81
- assert.strictEqual(config.openai.organization, "org-test123");
82
- });
83
-
84
- it("should have null organization when OPENAI_ORGANIZATION is not set", () => {
85
- process.env.MODEL_PROVIDER = "openai";
86
- process.env.OPENAI_API_KEY = "sk-test-key";
87
- delete process.env.OPENAI_ORGANIZATION;
88
-
89
- const config = require("../src/config");
90
- assert.strictEqual(config.openai.organization, null);
91
- });
92
- });
93
-
94
- describe("Routing", () => {
95
- it("should route to openai when MODEL_PROVIDER is openai", () => {
96
- process.env.MODEL_PROVIDER = "openai";
97
- process.env.OPENAI_API_KEY = "sk-test-key";
98
- process.env.PREFER_OLLAMA = "false";
99
-
100
- const config = require("../src/config");
101
- const routing = require("../src/clients/routing");
102
-
103
- const payload = { messages: [{ role: "user", content: "test" }] };
104
- const provider = routing.determineProvider(payload);
105
-
106
- assert.strictEqual(provider, "openai");
107
- });
108
-
109
- it("should route to openai as fallback when heavy tool count", () => {
110
- // Clear any existing OpenRouter key to ensure fallback to OpenAI
111
- delete process.env.OPENROUTER_API_KEY;
112
-
113
- process.env.MODEL_PROVIDER = "ollama";
114
- process.env.PREFER_OLLAMA = "true";
115
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
116
- process.env.OLLAMA_MAX_TOOLS_FOR_ROUTING = "2";
117
- process.env.OPENROUTER_MAX_TOOLS_FOR_ROUTING = "5";
118
- process.env.OPENAI_API_KEY = "sk-test-key";
119
- process.env.FALLBACK_ENABLED = "true";
120
- process.env.FALLBACK_PROVIDER = "openai";
121
-
122
- const config = require("../src/config");
123
- const routing = require("../src/clients/routing");
124
-
125
- // 10 tools - above both Ollama and OpenRouter thresholds, should go to fallback
126
- const payload = {
127
- messages: [{ role: "user", content: "test" }],
128
- tools: Array.from({ length: 10 }, (_, i) => ({ name: `tool${i}`, description: "test" })),
129
- };
130
-
131
- const provider = routing.determineProvider(payload);
132
- // Should route to openai as the configured fallback provider
133
- assert.strictEqual(provider, "openai");
134
- });
135
-
136
- it("should use openai as fallback provider when configured", () => {
137
- process.env.MODEL_PROVIDER = "ollama";
138
- process.env.PREFER_OLLAMA = "true";
139
- process.env.OLLAMA_MODEL = "qwen2.5-coder:latest";
140
- process.env.FALLBACK_PROVIDER = "openai";
141
- process.env.OPENAI_API_KEY = "sk-test-key";
142
- process.env.FALLBACK_ENABLED = "true";
143
-
144
- // Clear cache after env setup
145
- delete require.cache[require.resolve("../src/config/index.js")];
146
- delete require.cache[require.resolve("../src/clients/routing")];
147
- delete require.cache[require.resolve("../src/routing/index.js")];
148
-
149
- require("../src/config");
150
- const routing = require("../src/clients/routing");
151
-
152
- assert.strictEqual(routing.getFallbackProvider(), "openai");
153
- });
154
- });
155
-
156
- describe("Response Conversion", () => {
157
- // OpenAI uses the same response format as OpenRouter, so we can reuse the converter
158
-
159
- it("should convert OpenAI text response to Anthropic format", () => {
160
- process.env.MODEL_PROVIDER = "databricks";
161
- process.env.DATABRICKS_API_KEY = "test-key";
162
- process.env.DATABRICKS_API_BASE = "http://test.com";
163
-
164
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
165
-
166
- const openAIResponse = {
167
- id: "chatcmpl-123",
168
- object: "chat.completion",
169
- created: 1677652288,
170
- model: "gpt-4o",
171
- choices: [
172
- {
173
- index: 0,
174
- message: {
175
- role: "assistant",
176
- content: "Hello! How can I help you today?"
177
- },
178
- finish_reason: "stop"
179
- }
180
- ],
181
- usage: {
182
- prompt_tokens: 9,
183
- completion_tokens: 12,
184
- total_tokens: 21
185
- }
186
- };
187
-
188
- const result = convertOpenRouterResponseToAnthropic(openAIResponse, "claude-sonnet-4-5");
189
-
190
- assert.strictEqual(result.role, "assistant");
191
- assert.strictEqual(result.model, "claude-sonnet-4-5");
192
- assert.strictEqual(Array.isArray(result.content), true);
193
- assert.strictEqual(result.content.length, 1);
194
- assert.strictEqual(result.content[0].type, "text");
195
- assert.strictEqual(result.content[0].text, "Hello! How can I help you today?");
196
- assert.strictEqual(result.stop_reason, "end_turn");
197
- assert.strictEqual(result.usage.input_tokens, 9);
198
- assert.strictEqual(result.usage.output_tokens, 12);
199
- });
200
-
201
- it("should convert OpenAI tool call response to Anthropic format", () => {
202
- process.env.MODEL_PROVIDER = "databricks";
203
- process.env.DATABRICKS_API_KEY = "test-key";
204
- process.env.DATABRICKS_API_BASE = "http://test.com";
205
-
206
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
207
-
208
- const openAIResponse = {
209
- id: "chatcmpl-123",
210
- object: "chat.completion",
211
- model: "gpt-4o",
212
- choices: [
213
- {
214
- index: 0,
215
- message: {
216
- role: "assistant",
217
- content: "I'll create that file for you.",
218
- tool_calls: [
219
- {
220
- id: "call_abc123",
221
- type: "function",
222
- function: {
223
- name: "Write",
224
- arguments: JSON.stringify({
225
- file_path: "/tmp/test.txt",
226
- content: "Hello World"
227
- })
228
- }
229
- }
230
- ]
231
- },
232
- finish_reason: "tool_calls"
233
- }
234
- ],
235
- usage: {
236
- prompt_tokens: 50,
237
- completion_tokens: 30,
238
- total_tokens: 80
239
- }
240
- };
241
-
242
- const result = convertOpenRouterResponseToAnthropic(openAIResponse, "claude-sonnet-4-5");
243
-
244
- assert.strictEqual(result.role, "assistant");
245
- assert.strictEqual(result.content.length, 2); // text + tool_use
246
- assert.strictEqual(result.content[0].type, "text");
247
- assert.strictEqual(result.content[0].text, "I'll create that file for you.");
248
- assert.strictEqual(result.content[1].type, "tool_use");
249
- assert.strictEqual(result.content[1].name, "Write");
250
- assert.strictEqual(result.content[1].id, "call_abc123");
251
- assert.deepStrictEqual(result.content[1].input, {
252
- file_path: "/tmp/test.txt",
253
- content: "Hello World"
254
- });
255
- assert.strictEqual(result.stop_reason, "tool_use");
256
- });
257
-
258
- it("should convert OpenAI parallel tool calls to Anthropic format", () => {
259
- process.env.MODEL_PROVIDER = "databricks";
260
- process.env.DATABRICKS_API_KEY = "test-key";
261
- process.env.DATABRICKS_API_BASE = "http://test.com";
262
-
263
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
264
-
265
- const openAIResponse = {
266
- id: "chatcmpl-123",
267
- model: "gpt-4o",
268
- choices: [
269
- {
270
- message: {
271
- role: "assistant",
272
- content: "I'll read both files.",
273
- tool_calls: [
274
- {
275
- id: "call_1",
276
- type: "function",
277
- function: {
278
- name: "Read",
279
- arguments: JSON.stringify({ file_path: "/tmp/file1.txt" })
280
- }
281
- },
282
- {
283
- id: "call_2",
284
- type: "function",
285
- function: {
286
- name: "Read",
287
- arguments: JSON.stringify({ file_path: "/tmp/file2.txt" })
288
- }
289
- }
290
- ]
291
- },
292
- finish_reason: "tool_calls"
293
- }
294
- ],
295
- usage: { prompt_tokens: 30, completion_tokens: 40, total_tokens: 70 }
296
- };
297
-
298
- const result = convertOpenRouterResponseToAnthropic(openAIResponse, "claude-sonnet-4-5");
299
-
300
- assert.strictEqual(result.content.length, 3); // text + 2 tool_uses
301
- assert.strictEqual(result.content[0].type, "text");
302
- assert.strictEqual(result.content[1].type, "tool_use");
303
- assert.strictEqual(result.content[1].name, "Read");
304
- assert.strictEqual(result.content[1].id, "call_1");
305
- assert.strictEqual(result.content[2].type, "tool_use");
306
- assert.strictEqual(result.content[2].name, "Read");
307
- assert.strictEqual(result.content[2].id, "call_2");
308
- });
309
-
310
- it("should handle OpenAI response with only tool calls (no text content)", () => {
311
- process.env.MODEL_PROVIDER = "databricks";
312
- process.env.DATABRICKS_API_KEY = "test-key";
313
- process.env.DATABRICKS_API_BASE = "http://test.com";
314
-
315
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
316
-
317
- const openAIResponse = {
318
- id: "chatcmpl-123",
319
- model: "gpt-4o",
320
- choices: [
321
- {
322
- message: {
323
- role: "assistant",
324
- content: null, // OpenAI often returns null content with tool calls
325
- tool_calls: [
326
- {
327
- id: "call_xyz",
328
- type: "function",
329
- function: {
330
- name: "Bash",
331
- arguments: JSON.stringify({ command: "ls -la" })
332
- }
333
- }
334
- ]
335
- },
336
- finish_reason: "tool_calls"
337
- }
338
- ],
339
- usage: { prompt_tokens: 20, completion_tokens: 15, total_tokens: 35 }
340
- };
341
-
342
- const result = convertOpenRouterResponseToAnthropic(openAIResponse, "claude-sonnet-4-5");
343
-
344
- // Should have tool_use block (at least one)
345
- assert.strictEqual(result.role, "assistant");
346
- assert.strictEqual(Array.isArray(result.content), true);
347
- assert.strictEqual(result.content.length >= 1, true);
348
- // Find the tool_use block
349
- const toolUseBlock = result.content.find(c => c.type === "tool_use");
350
- assert.strictEqual(toolUseBlock !== undefined, true);
351
- assert.strictEqual(toolUseBlock.name, "Bash");
352
- });
353
- });
354
-
355
- describe("Message Conversion", () => {
356
- it("should convert Anthropic messages to OpenAI format", () => {
357
- process.env.MODEL_PROVIDER = "databricks";
358
- process.env.DATABRICKS_API_KEY = "test-key";
359
- process.env.DATABRICKS_API_BASE = "http://test.com";
360
-
361
- const { convertAnthropicMessagesToOpenRouter } = require("../src/clients/openrouter-utils");
362
-
363
- const anthropicMessages = [
364
- {
365
- role: "user",
366
- content: [
367
- { type: "text", text: "Hello, how are you?" }
368
- ]
369
- },
370
- {
371
- role: "assistant",
372
- content: [
373
- { type: "text", text: "I'm doing well, thank you!" }
374
- ]
375
- }
376
- ];
377
-
378
- const result = convertAnthropicMessagesToOpenRouter(anthropicMessages);
379
-
380
- assert.strictEqual(result.length, 2);
381
- assert.strictEqual(result[0].role, "user");
382
- assert.strictEqual(result[0].content, "Hello, how are you?");
383
- assert.strictEqual(result[1].role, "assistant");
384
- assert.strictEqual(result[1].content, "I'm doing well, thank you!");
385
- });
386
-
387
- it("should convert Anthropic tool_result messages to OpenAI format", () => {
388
- process.env.MODEL_PROVIDER = "databricks";
389
- process.env.DATABRICKS_API_KEY = "test-key";
390
- process.env.DATABRICKS_API_BASE = "http://test.com";
391
-
392
- const { convertAnthropicMessagesToOpenRouter } = require("../src/clients/openrouter-utils");
393
-
394
- // Must have a preceding assistant message with tool_use for tool_result to be valid
395
- const anthropicMessages = [
396
- {
397
- role: "user",
398
- content: [{ type: "text", text: "Create a file" }]
399
- },
400
- {
401
- role: "assistant",
402
- content: [
403
- { type: "text", text: "I'll create that file." },
404
- {
405
- type: "tool_use",
406
- id: "call_123",
407
- name: "Write",
408
- input: { file_path: "/tmp/test.txt", content: "Hello" }
409
- }
410
- ]
411
- },
412
- {
413
- role: "user",
414
- content: [
415
- {
416
- type: "tool_result",
417
- tool_use_id: "call_123",
418
- content: "File created successfully"
419
- }
420
- ]
421
- }
422
- ];
423
-
424
- const result = convertAnthropicMessagesToOpenRouter(anthropicMessages);
425
-
426
- // Should have user message, assistant message with tool call, and tool result
427
- assert.strictEqual(result.length >= 3, true);
428
- // Find the tool result message
429
- const toolResultMsg = result.find(m => m.role === "tool");
430
- assert.strictEqual(toolResultMsg !== undefined, true);
431
- assert.strictEqual(toolResultMsg.tool_call_id, "call_123");
432
- assert.strictEqual(toolResultMsg.content, "File created successfully");
433
- });
434
- });
435
-
436
- describe("Tool Conversion", () => {
437
- it("should convert Anthropic tools to OpenAI format", () => {
438
- process.env.MODEL_PROVIDER = "databricks";
439
- process.env.DATABRICKS_API_KEY = "test-key";
440
- process.env.DATABRICKS_API_BASE = "http://test.com";
441
-
442
- const { convertAnthropicToolsToOpenRouter } = require("../src/clients/openrouter-utils");
443
-
444
- const anthropicTools = [
445
- {
446
- name: "Write",
447
- description: "Write content to a file",
448
- input_schema: {
449
- type: "object",
450
- properties: {
451
- file_path: { type: "string", description: "Path to the file" },
452
- content: { type: "string", description: "Content to write" }
453
- },
454
- required: ["file_path", "content"]
455
- }
456
- }
457
- ];
458
-
459
- const result = convertAnthropicToolsToOpenRouter(anthropicTools);
460
-
461
- assert.strictEqual(result.length, 1);
462
- assert.strictEqual(result[0].type, "function");
463
- assert.strictEqual(result[0].function.name, "Write");
464
- assert.strictEqual(result[0].function.description, "Write content to a file");
465
- assert.deepStrictEqual(result[0].function.parameters, {
466
- type: "object",
467
- properties: {
468
- file_path: { type: "string", description: "Path to the file" },
469
- content: { type: "string", description: "Content to write" }
470
- },
471
- required: ["file_path", "content"]
472
- });
473
- });
474
- });
475
-
476
- describe("Error Handling", () => {
477
- it("should throw error when OpenAI response has no choices", () => {
478
- process.env.MODEL_PROVIDER = "databricks";
479
- process.env.DATABRICKS_API_KEY = "test-key";
480
- process.env.DATABRICKS_API_BASE = "http://test.com";
481
-
482
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
483
-
484
- const errorResponse = {
485
- error: {
486
- message: "Rate limit exceeded",
487
- type: "rate_limit_error",
488
- code: "rate_limit_exceeded"
489
- }
490
- };
491
-
492
- assert.throws(
493
- () => convertOpenRouterResponseToAnthropic(errorResponse, "test-model"),
494
- /No choices in OpenRouter response/
495
- );
496
- });
497
-
498
- it("should throw error when OpenAI response has empty choices array", () => {
499
- process.env.MODEL_PROVIDER = "databricks";
500
- process.env.DATABRICKS_API_KEY = "test-key";
501
- process.env.DATABRICKS_API_BASE = "http://test.com";
502
-
503
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
504
-
505
- const emptyChoicesResponse = {
506
- id: "chatcmpl-123",
507
- model: "gpt-4o",
508
- choices: [],
509
- usage: { prompt_tokens: 0, completion_tokens: 0, total_tokens: 0 }
510
- };
511
-
512
- assert.throws(
513
- () => convertOpenRouterResponseToAnthropic(emptyChoicesResponse, "test-model"),
514
- /No choices in OpenRouter response/
515
- );
516
- });
517
-
518
- it("should handle malformed tool call arguments gracefully", () => {
519
- process.env.MODEL_PROVIDER = "databricks";
520
- process.env.DATABRICKS_API_KEY = "test-key";
521
- process.env.DATABRICKS_API_BASE = "http://test.com";
522
-
523
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
524
-
525
- const responseWithBadArgs = {
526
- id: "chatcmpl-123",
527
- model: "gpt-4o",
528
- choices: [
529
- {
530
- message: {
531
- role: "assistant",
532
- content: "Using tool",
533
- tool_calls: [
534
- {
535
- id: "call_bad",
536
- type: "function",
537
- function: {
538
- name: "Write",
539
- arguments: "this is not valid json {"
540
- }
541
- }
542
- ]
543
- },
544
- finish_reason: "tool_calls"
545
- }
546
- ],
547
- usage: { prompt_tokens: 10, completion_tokens: 20, total_tokens: 30 }
548
- };
549
-
550
- const result = convertOpenRouterResponseToAnthropic(responseWithBadArgs, "test-model");
551
-
552
- // Should still convert, but with empty input object
553
- assert.strictEqual(result.content[1].type, "tool_use");
554
- assert.deepStrictEqual(result.content[1].input, {});
555
- });
556
- });
557
-
558
- describe("Finish Reason Mapping", () => {
559
- it("should map stop finish_reason to end_turn", () => {
560
- process.env.MODEL_PROVIDER = "databricks";
561
- process.env.DATABRICKS_API_KEY = "test-key";
562
- process.env.DATABRICKS_API_BASE = "http://test.com";
563
-
564
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
565
-
566
- const response = {
567
- choices: [
568
- {
569
- message: { role: "assistant", content: "Done" },
570
- finish_reason: "stop"
571
- }
572
- ],
573
- usage: { prompt_tokens: 5, completion_tokens: 1, total_tokens: 6 }
574
- };
575
-
576
- const result = convertOpenRouterResponseToAnthropic(response, "test-model");
577
- assert.strictEqual(result.stop_reason, "end_turn");
578
- });
579
-
580
- it("should map tool_calls finish_reason to tool_use", () => {
581
- process.env.MODEL_PROVIDER = "databricks";
582
- process.env.DATABRICKS_API_KEY = "test-key";
583
- process.env.DATABRICKS_API_BASE = "http://test.com";
584
-
585
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
586
-
587
- const response = {
588
- choices: [
589
- {
590
- message: {
591
- role: "assistant",
592
- content: "Calling tool",
593
- tool_calls: [
594
- {
595
- id: "call_1",
596
- type: "function",
597
- function: { name: "test", arguments: "{}" }
598
- }
599
- ]
600
- },
601
- finish_reason: "tool_calls"
602
- }
603
- ],
604
- usage: { prompt_tokens: 5, completion_tokens: 10, total_tokens: 15 }
605
- };
606
-
607
- const result = convertOpenRouterResponseToAnthropic(response, "test-model");
608
- assert.strictEqual(result.stop_reason, "tool_use");
609
- });
610
-
611
- it("should map length finish_reason to max_tokens", () => {
612
- process.env.MODEL_PROVIDER = "databricks";
613
- process.env.DATABRICKS_API_KEY = "test-key";
614
- process.env.DATABRICKS_API_BASE = "http://test.com";
615
-
616
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
617
-
618
- const response = {
619
- choices: [
620
- {
621
- message: { role: "assistant", content: "This is a truncated response that..." },
622
- finish_reason: "length"
623
- }
624
- ],
625
- usage: { prompt_tokens: 5, completion_tokens: 100, total_tokens: 105 }
626
- };
627
-
628
- const result = convertOpenRouterResponseToAnthropic(response, "test-model");
629
- assert.strictEqual(result.stop_reason, "max_tokens");
630
- });
631
- });
632
-
633
- describe("Usage Metrics", () => {
634
- it("should correctly map OpenAI usage to Anthropic format", () => {
635
- process.env.MODEL_PROVIDER = "databricks";
636
- process.env.DATABRICKS_API_KEY = "test-key";
637
- process.env.DATABRICKS_API_BASE = "http://test.com";
638
-
639
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
640
-
641
- const response = {
642
- choices: [
643
- {
644
- message: { role: "assistant", content: "Response" },
645
- finish_reason: "stop"
646
- }
647
- ],
648
- usage: {
649
- prompt_tokens: 150,
650
- completion_tokens: 75,
651
- total_tokens: 225
652
- }
653
- };
654
-
655
- const result = convertOpenRouterResponseToAnthropic(response, "test-model");
656
-
657
- // OpenAI prompt_tokens -> Anthropic input_tokens
658
- // OpenAI completion_tokens -> Anthropic output_tokens
659
- assert.strictEqual(result.usage.input_tokens, 150);
660
- assert.strictEqual(result.usage.output_tokens, 75);
661
- });
662
-
663
- it("should handle missing usage gracefully", () => {
664
- process.env.MODEL_PROVIDER = "databricks";
665
- process.env.DATABRICKS_API_KEY = "test-key";
666
- process.env.DATABRICKS_API_BASE = "http://test.com";
667
-
668
- const { convertOpenRouterResponseToAnthropic } = require("../src/clients/openrouter-utils");
669
-
670
- const response = {
671
- choices: [
672
- {
673
- message: { role: "assistant", content: "Response" },
674
- finish_reason: "stop"
675
- }
676
- ]
677
- // No usage field
678
- };
679
-
680
- const result = convertOpenRouterResponseToAnthropic(response, "test-model");
681
-
682
- assert.strictEqual(result.usage.input_tokens, 0);
683
- assert.strictEqual(result.usage.output_tokens, 0);
684
- });
685
- });
686
- });