anthropic-eager-tool-input-e2e.test.ts 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. import { Type } from "typebox";
  2. import { describe, expect, it } from "vitest";
  3. import { getEnvApiKey } from "../src/env-api-keys.ts";
  4. import { getModels, getProviders } from "../src/models.ts";
  5. import { complete } from "../src/stream.ts";
  6. import type { Api, KnownProvider, Model, ProviderStreamOptions, Tool } from "../src/types.ts";
  7. import { resolveApiKey } from "./oauth.ts";
  8. const githubCopilotToken = await resolveApiKey("github-copilot");
  9. const echoToolSchema = Type.Object({
  10. value: Type.String({ description: "The value to echo" }),
  11. });
  12. const echoTool: Tool<typeof echoToolSchema> = {
  13. name: "echo_value",
  14. description: "Echo a string value",
  15. parameters: echoToolSchema,
  16. };
  17. interface AnthropicEagerE2ECase {
  18. name: string;
  19. provider: KnownProvider;
  20. model: Model<"anthropic-messages">;
  21. apiKey: string | undefined;
  22. }
  23. function getE2EApiKey(provider: KnownProvider): string | undefined {
  24. if (provider === "github-copilot") {
  25. return githubCopilotToken;
  26. }
  27. return getEnvApiKey(provider);
  28. }
  29. function getAnthropicMessagesModels(provider: KnownProvider): Model<"anthropic-messages">[] {
  30. const models = getModels(provider) as Model<Api>[];
  31. return models.filter((model) => model.api === "anthropic-messages") as Model<"anthropic-messages">[];
  32. }
  33. const anthropicMessagesCases: AnthropicEagerE2ECase[] = getProviders().flatMap((provider) =>
  34. getAnthropicMessagesModels(provider).map((model) => ({
  35. name: `${provider}/${model.id}`,
  36. provider,
  37. model,
  38. apiKey: getE2EApiKey(provider),
  39. })),
  40. );
  41. function getProbePriority(model: Model<"anthropic-messages">): number {
  42. const modelId = model.id.toLowerCase();
  43. const cost = model.cost.input + model.cost.output;
  44. let priority = cost;
  45. // Prefer current Claude 4 Haiku routes when present: they are cheap and avoid
  46. // stale Claude 3.x aliases that can remain in catalogs after upstream removal.
  47. if (modelId.includes("haiku") && (modelId.includes("4-5") || modelId.includes("4.5"))) {
  48. priority -= 1000;
  49. } else if (modelId.includes("sonnet") && (modelId.includes("4-") || modelId.includes("4."))) {
  50. priority -= 750;
  51. } else if (modelId.includes("claude") && (modelId.includes("4-") || modelId.includes("4."))) {
  52. priority -= 500;
  53. }
  54. return priority;
  55. }
  56. function selectOneCasePerProvider(cases: AnthropicEagerE2ECase[]): AnthropicEagerE2ECase[] {
  57. const byProvider = new Map<KnownProvider, AnthropicEagerE2ECase[]>();
  58. for (const testCase of cases) {
  59. const providerCases = byProvider.get(testCase.provider) ?? [];
  60. providerCases.push(testCase);
  61. byProvider.set(testCase.provider, providerCases);
  62. }
  63. return Array.from(byProvider.values()).map(
  64. (providerCases) =>
  65. providerCases.sort(
  66. (a, b) => getProbePriority(a.model) - getProbePriority(b.model) || a.model.id.localeCompare(b.model.id),
  67. )[0],
  68. );
  69. }
  70. const generatedCompatCases = selectOneCasePerProvider(anthropicMessagesCases);
  71. const forcedEagerProbeCases = selectOneCasePerProvider(
  72. anthropicMessagesCases.filter((testCase) => testCase.model.compat?.supportsEagerToolInputStreaming !== false),
  73. );
  74. function withEagerToolInputStreaming(model: Model<"anthropic-messages">): Model<"anthropic-messages"> {
  75. return {
  76. ...model,
  77. compat: {
  78. ...model.compat,
  79. supportsEagerToolInputStreaming: true,
  80. },
  81. };
  82. }
  83. async function expectToolEnabledRequestAccepted(
  84. model: Model<"anthropic-messages">,
  85. apiKey: string | undefined,
  86. ): Promise<void> {
  87. const options: ProviderStreamOptions = {
  88. apiKey,
  89. maxTokens: 128,
  90. thinkingEnabled: false,
  91. };
  92. const response = await complete(
  93. model,
  94. {
  95. systemPrompt: "You are a concise assistant. Use tools when useful.",
  96. messages: [
  97. {
  98. role: "user",
  99. content: "Call echo_value with value set to eager-input-streaming-compat.",
  100. timestamp: Date.now(),
  101. },
  102. ],
  103. tools: [echoTool],
  104. },
  105. options,
  106. );
  107. expect(response.errorMessage, response.errorMessage).toBeFalsy();
  108. expect(response.stopReason, response.errorMessage).not.toBe("error");
  109. }
  110. describe("Anthropic Messages eager tool input streaming E2E", () => {
  111. it("covers every generated anthropic-messages model", () => {
  112. const expectedModels = getProviders().flatMap((provider) =>
  113. getAnthropicMessagesModels(provider).map((model) => `${provider}/${model.id}`),
  114. );
  115. expect(anthropicMessagesCases.map((testCase) => testCase.name).sort()).toEqual(expectedModels.sort());
  116. });
  117. describe("generated compatibility settings", () => {
  118. for (const testCase of generatedCompatCases) {
  119. it.skipIf(!testCase.apiKey)(`${testCase.name} accepts configured tool streaming`, { retry: 2 }, async () => {
  120. await expectToolEnabledRequestAccepted(testCase.model, testCase.apiKey);
  121. });
  122. }
  123. });
  124. describe("forced eager_input_streaming probe", () => {
  125. for (const testCase of forcedEagerProbeCases) {
  126. const model = withEagerToolInputStreaming(testCase.model);
  127. it.skipIf(!testCase.apiKey)(
  128. `${testCase.name} accepts forced eager_input_streaming`,
  129. { retry: 2 },
  130. async () => {
  131. await expectToolEnabledRequestAccepted(model, testCase.apiKey);
  132. },
  133. );
  134. }
  135. });
  136. });