anthropic-request-metadata-wire.test.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. import { createAnthropic } from "@ai-sdk/anthropic";
  2. import { generateText, streamText } from "ai";
  3. import { describe, expect, it } from "vitest";
  4. import { createGenerateTextOptions, createStreamTextOptions } from "../src/model/runner-options.js";
  5. import type { ResolvedAiSdkModel } from "../src/model/runner-runtime.js";
  6. import { createTestModelProperties } from "./test-model-format.js";
  7. const metadataUserId = JSON.stringify({
  8. device_id: "7f1431f0-53e8-41c1-9cf9-22d11aa51d6a",
  9. account_uuid: "",
  10. session_id: "2d480878-38af-4bae-a54f-096e2fb0f4d7",
  11. });
  12. describe("Anthropic request metadata wire shape", () => {
  13. it("sends metadata.user_id in generate requests", async () => {
  14. const capture = createRequestCapture();
  15. const options = createOptions(capture.fetch, "generate");
  16. await expect(generateText(options)).rejects.toBeDefined();
  17. expect(capture.body()).toMatchObject({
  18. metadata: {
  19. user_id: metadataUserId,
  20. },
  21. });
  22. });
  23. it("sends metadata.user_id in stream requests", async () => {
  24. const capture = createRequestCapture();
  25. const result = streamText(createOptions(capture.fetch, "stream"));
  26. await drain(result.fullStream);
  27. expect(capture.body()).toMatchObject({
  28. metadata: {
  29. user_id: metadataUserId,
  30. },
  31. });
  32. });
  33. });
  34. function createOptions(
  35. fetch: typeof globalThis.fetch,
  36. kind: "generate" | "stream",
  37. ): ReturnType<typeof createGenerateTextOptions> | ReturnType<typeof createStreamTextOptions> {
  38. const provider = createAnthropic({
  39. apiKey: "fake-key",
  40. baseURL: "https://api.example.test",
  41. fetch,
  42. });
  43. const input = {
  44. anthropicMetadataUserId: metadataUserId,
  45. includeModelIO: false,
  46. request: {
  47. messages: [{ content: "hi", role: "user" as const }],
  48. providerOptions: {
  49. anthropic: {
  50. effort: "high",
  51. metadata: { userId: "stale-static-user-id" },
  52. },
  53. },
  54. },
  55. resolved: {
  56. properties: createTestModelProperties(),
  57. model: provider("claude-test"),
  58. providerKind: "anthropic" as const,
  59. providerId: "anthropic",
  60. modelId: "claude-test",
  61. } as ResolvedAiSdkModel,
  62. statusContext: {
  63. providerId: "anthropic",
  64. modelId: "claude-test",
  65. providerKind: "anthropic",
  66. requestId: `req_anthropic_metadata_${kind}`,
  67. sessionId: "sess_2d480878-38af-4bae-a54f-096e2fb0f4d7",
  68. traceId: `trace_anthropic_metadata_${kind}`,
  69. } as never,
  70. };
  71. return kind === "generate" ? createGenerateTextOptions(input) : createStreamTextOptions(input);
  72. }
  73. function createRequestCapture(): {
  74. body: () => Record<string, unknown>;
  75. fetch: typeof globalThis.fetch;
  76. } {
  77. let capturedBody: Record<string, unknown> | undefined;
  78. const fetch: typeof globalThis.fetch = async (_input, init) => {
  79. if (typeof init?.body !== "string") {
  80. throw new Error("Expected a JSON request body");
  81. }
  82. capturedBody = JSON.parse(init.body) as Record<string, unknown>;
  83. return new Response(JSON.stringify({ error: { message: "wire capture", type: "test" } }), {
  84. headers: { "content-type": "application/json" },
  85. status: 400,
  86. });
  87. };
  88. return {
  89. body() {
  90. if (!capturedBody) {
  91. throw new Error("Expected the provider request body to be captured");
  92. }
  93. return capturedBody;
  94. },
  95. fetch,
  96. };
  97. }
  98. async function drain(stream: AsyncIterable<unknown>): Promise<void> {
  99. for await (const _chunk of stream) {
  100. // Drain the provider stream until its expected wire-capture error.
  101. }
  102. }