openai-completions-response-model.test.ts 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { beforeEach, describe, expect, it, vi } from "vitest";
  2. import { complete } from "../src/stream.ts";
  3. import type { Model } from "../src/types.ts";
  4. // Router/virtual ids (e.g. OpenRouter `auto`) keep `model` pinned to the
  5. // requested id and surface the routed concrete id on `responseModel`.
  6. const mockState = vi.hoisted(() => ({
  7. chunks: [] as unknown[],
  8. }));
  9. vi.mock("openai", () => {
  10. class FakeOpenAI {
  11. chat = {
  12. completions: {
  13. create: () => {
  14. const chunks = mockState.chunks;
  15. const stream = {
  16. async *[Symbol.asyncIterator]() {
  17. for (const chunk of chunks) yield chunk;
  18. },
  19. };
  20. const promise = Promise.resolve(stream) as Promise<typeof stream> & {
  21. withResponse: () => Promise<{
  22. data: typeof stream;
  23. response: { status: number; headers: Headers };
  24. }>;
  25. };
  26. promise.withResponse = async () => ({
  27. data: stream,
  28. response: { status: 200, headers: new Headers() },
  29. });
  30. return promise;
  31. },
  32. },
  33. };
  34. }
  35. return { default: FakeOpenAI };
  36. });
  37. function openRouterAuto(): Model<"openai-completions"> {
  38. return {
  39. id: "openrouter/auto",
  40. name: "OpenRouter Auto",
  41. api: "openai-completions",
  42. provider: "openrouter",
  43. baseUrl: "https://openrouter.ai/api/v1",
  44. reasoning: false,
  45. input: ["text"],
  46. cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
  47. contextWindow: 200_000,
  48. maxTokens: 8192,
  49. };
  50. }
  51. describe("openai-completions responseModel", () => {
  52. beforeEach(() => {
  53. mockState.chunks = [];
  54. });
  55. it("surfaces routed chunk.model on responseModel without changing model", async () => {
  56. mockState.chunks = [
  57. { id: "chatcmpl-1", model: "anthropic/claude-opus-4.8", choices: [{ index: 0, delta: { content: "hi" } }] },
  58. {
  59. id: "chatcmpl-1",
  60. model: "anthropic/claude-opus-4.8",
  61. choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
  62. usage: {
  63. prompt_tokens: 10,
  64. completion_tokens: 5,
  65. prompt_tokens_details: { cached_tokens: 0 },
  66. completion_tokens_details: { reasoning_tokens: 0 },
  67. },
  68. },
  69. ];
  70. const message = await complete(
  71. openRouterAuto(),
  72. { messages: [{ role: "user", content: "hi", timestamp: Date.now() }] },
  73. { apiKey: "test" },
  74. );
  75. expect(message.model).toBe("openrouter/auto");
  76. expect(message.responseModel).toBe("anthropic/claude-opus-4.8");
  77. expect(message.provider).toBe("openrouter");
  78. expect(message.stopReason).toBe("stop");
  79. });
  80. it("leaves responseModel undefined when chunks echo the requested id", async () => {
  81. mockState.chunks = [
  82. { id: "chatcmpl-2", model: "openrouter/auto", choices: [{ index: 0, delta: { content: "hi" } }] },
  83. {
  84. id: "chatcmpl-2",
  85. model: "openrouter/auto",
  86. choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
  87. usage: {
  88. prompt_tokens: 1,
  89. completion_tokens: 1,
  90. prompt_tokens_details: { cached_tokens: 0 },
  91. completion_tokens_details: { reasoning_tokens: 0 },
  92. },
  93. },
  94. ];
  95. const message = await complete(
  96. openRouterAuto(),
  97. { messages: [{ role: "user", content: "hi", timestamp: Date.now() }] },
  98. { apiKey: "test" },
  99. );
  100. expect(message.model).toBe("openrouter/auto");
  101. expect(message.responseModel).toBeUndefined();
  102. });
  103. it("ignores empty or missing chunk.model", async () => {
  104. mockState.chunks = [
  105. { id: "chatcmpl-3", choices: [{ index: 0, delta: { content: "hi" } }] },
  106. { id: "chatcmpl-3", model: "", choices: [{ index: 0, delta: { content: "!" } }] },
  107. {
  108. id: "chatcmpl-3",
  109. choices: [{ index: 0, delta: {}, finish_reason: "stop" }],
  110. usage: {
  111. prompt_tokens: 1,
  112. completion_tokens: 2,
  113. prompt_tokens_details: { cached_tokens: 0 },
  114. completion_tokens_details: { reasoning_tokens: 0 },
  115. },
  116. },
  117. ];
  118. const message = await complete(
  119. openRouterAuto(),
  120. { messages: [{ role: "user", content: "hi", timestamp: Date.now() }] },
  121. { apiKey: "test" },
  122. );
  123. expect(message.model).toBe("openrouter/auto");
  124. expect(message.responseModel).toBeUndefined();
  125. });
  126. });