responseid.test.ts 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { describe, expect, it } from "vitest";
  2. import { getModel } from "../src/models.ts";
  3. import { complete } from "../src/stream.ts";
  4. import type { Api, Context, Model, StreamOptions } from "../src/types.ts";
  5. import { hasAzureOpenAICredentials, resolveAzureDeploymentName } from "./azure-utils.ts";
  6. import { resolveApiKey } from "./oauth.ts";
  7. type StreamOptionsWithExtras = StreamOptions & Record<string, unknown>;
  8. const oauthTokens = await Promise.all([resolveApiKey("github-copilot"), resolveApiKey("openai-codex")]);
  9. const [githubCopilotToken, openaiCodexToken] = oauthTokens;
  10. async function expectResponseId<TApi extends Api>(model: Model<TApi>, options: StreamOptionsWithExtras = {}) {
  11. const context: Context = {
  12. systemPrompt: "You are a helpful assistant. Be concise.",
  13. messages: [{ role: "user", content: "Reply with exactly: response id test", timestamp: Date.now() }],
  14. };
  15. const response = await complete(model, context, options);
  16. expect(response.stopReason, response.errorMessage).not.toBe("error");
  17. expect(response.responseId).toBeTruthy();
  18. expect(typeof response.responseId).toBe("string");
  19. }
  20. describe("responseId E2E Tests", () => {
  21. describe.skipIf(!process.env.GEMINI_API_KEY)("Google Provider", () => {
  22. const llm = getModel("google", "gemini-2.5-flash");
  23. it("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  24. await expectResponseId(llm);
  25. });
  26. });
  27. describe("Google Vertex Provider", () => {
  28. const vertexProject = process.env.GOOGLE_CLOUD_PROJECT || process.env.GCLOUD_PROJECT;
  29. const vertexLocation = process.env.GOOGLE_CLOUD_LOCATION;
  30. const vertexApiKey = process.env.GOOGLE_CLOUD_API_KEY;
  31. const isVertexConfigured = Boolean(vertexProject && vertexLocation);
  32. const vertexOptions = { project: vertexProject, location: vertexLocation } as const;
  33. const llm = getModel("google-vertex", "gemini-3-flash-preview");
  34. it.skipIf(!isVertexConfigured)("should expose responseId with ADC", { retry: 3, timeout: 30000 }, async () => {
  35. await expectResponseId(llm, vertexOptions);
  36. });
  37. it.skipIf(!vertexApiKey)("should expose responseId with API key", { retry: 3, timeout: 30000 }, async () => {
  38. await expectResponseId(llm, { apiKey: vertexApiKey! });
  39. });
  40. });
  41. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Completions Provider", () => {
  42. const { compat: _compat, ...baseModel } = getModel("openai", "gpt-4o-mini");
  43. void _compat;
  44. const llm: Model<"openai-completions"> = {
  45. ...baseModel,
  46. api: "openai-completions",
  47. };
  48. it("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  49. await expectResponseId(llm);
  50. });
  51. });
  52. describe.skipIf(!process.env.OPENAI_API_KEY)("OpenAI Responses Provider", () => {
  53. const llm = getModel("openai", "gpt-5-mini");
  54. it("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  55. await expectResponseId(llm);
  56. });
  57. });
  58. describe.skipIf(!process.env.ANTHROPIC_API_KEY)("Anthropic Provider", () => {
  59. const llm = getModel("anthropic", "claude-sonnet-4-5");
  60. it("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  61. await expectResponseId(llm);
  62. });
  63. });
  64. describe.skipIf(!hasAzureOpenAICredentials())("Azure OpenAI Responses Provider", () => {
  65. const llm = getModel("azure-openai-responses", "gpt-4o-mini");
  66. const azureDeploymentName = resolveAzureDeploymentName(llm.id);
  67. const azureOptions = azureDeploymentName ? { azureDeploymentName } : {};
  68. it("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  69. await expectResponseId(llm, azureOptions);
  70. });
  71. });
  72. describe.skipIf(!process.env.MISTRAL_API_KEY)("Mistral Provider", () => {
  73. const llm = getModel("mistral", "devstral-medium-latest");
  74. it("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  75. await expectResponseId(llm);
  76. });
  77. });
  78. describe("GitHub Copilot Provider", () => {
  79. it.skipIf(!githubCopilotToken)("OpenAI path should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  80. const llm = getModel("github-copilot", "gpt-5.3-codex");
  81. await expectResponseId(llm, { apiKey: githubCopilotToken });
  82. });
  83. it.skipIf(!githubCopilotToken)(
  84. "Anthropic path should expose responseId",
  85. { retry: 3, timeout: 30000 },
  86. async () => {
  87. const llm = getModel("github-copilot", "claude-sonnet-4.6");
  88. await expectResponseId(llm, { apiKey: githubCopilotToken });
  89. },
  90. );
  91. });
  92. describe("OpenAI Codex Provider", () => {
  93. it.skipIf(!openaiCodexToken)("should expose responseId", { retry: 3, timeout: 30000 }, async () => {
  94. const llm = getModel("openai-codex", "gpt-5.5");
  95. await expectResponseId(llm, { apiKey: openaiCodexToken });
  96. });
  97. });
  98. });