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