|
1 | 1 | import { it, expect, describe } from "vitest"; |
2 | 2 | import { callProxyV1, createCapturingFetch } from "../../utils/tests"; |
| 3 | +import { FetchFn } from "../proxy"; |
3 | 4 | import { |
4 | 5 | OpenAIChatCompletion, |
5 | 6 | OpenAIChatCompletionChunk, |
@@ -51,6 +52,100 @@ it("should convert OpenAI streaming request to Anthropic and back", async () => |
51 | 52 | expect(hasContent).toBe(true); |
52 | 53 | }); |
53 | 54 |
|
| 55 | +it("should request identity encoding for streaming Anthropic chat completions", async () => { |
| 56 | + const { fetch, requests } = createCapturingFetch({ captureOnly: true }); |
| 57 | + |
| 58 | + await callProxyV1<OpenAIChatCompletionCreateParams, OpenAIChatCompletion>({ |
| 59 | + body: { |
| 60 | + model: "claude-3-haiku-20240307", |
| 61 | + messages: [{ role: "user", content: "Stream a short response." }], |
| 62 | + stream: true, |
| 63 | + max_tokens: 32, |
| 64 | + }, |
| 65 | + fetch, |
| 66 | + }); |
| 67 | + |
| 68 | + expect(requests).toHaveLength(1); |
| 69 | + expect(requests[0].headers["accept-encoding"]).toBe("identity"); |
| 70 | +}); |
| 71 | + |
| 72 | +it("should mark streaming responses as no-transform", async () => { |
| 73 | + const encoder = new TextEncoder(); |
| 74 | + const anthropicEvents = [ |
| 75 | + { |
| 76 | + type: "message_start", |
| 77 | + message: { |
| 78 | + id: "msg_test", |
| 79 | + type: "message", |
| 80 | + role: "assistant", |
| 81 | + content: [], |
| 82 | + model: "claude-3-haiku-20240307", |
| 83 | + stop_reason: null, |
| 84 | + stop_sequence: null, |
| 85 | + usage: { input_tokens: 4, output_tokens: 0 }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + { |
| 89 | + type: "content_block_start", |
| 90 | + index: 0, |
| 91 | + content_block: { type: "text", text: "" }, |
| 92 | + }, |
| 93 | + { |
| 94 | + type: "content_block_delta", |
| 95 | + index: 0, |
| 96 | + delta: { type: "text_delta", text: "hello" }, |
| 97 | + }, |
| 98 | + { |
| 99 | + type: "message_delta", |
| 100 | + delta: { stop_reason: "end_turn", stop_sequence: null }, |
| 101 | + usage: { output_tokens: 1 }, |
| 102 | + }, |
| 103 | + ]; |
| 104 | + const fetch: FetchFn = async () => |
| 105 | + new Response( |
| 106 | + new ReadableStream({ |
| 107 | + start(controller) { |
| 108 | + for (const event of anthropicEvents) { |
| 109 | + controller.enqueue( |
| 110 | + encoder.encode(`data: ${JSON.stringify(event)}\n\n`), |
| 111 | + ); |
| 112 | + } |
| 113 | + controller.close(); |
| 114 | + }, |
| 115 | + }), |
| 116 | + { |
| 117 | + headers: { |
| 118 | + "content-type": "text/event-stream; charset=utf-8", |
| 119 | + }, |
| 120 | + }, |
| 121 | + ); |
| 122 | + |
| 123 | + const { headers, events } = await callProxyV1< |
| 124 | + OpenAIChatCompletionCreateParams, |
| 125 | + OpenAIChatCompletionChunk |
| 126 | + >({ |
| 127 | + body: { |
| 128 | + model: "claude-3-haiku-20240307", |
| 129 | + messages: [{ role: "user", content: "Say hello." }], |
| 130 | + stream: true, |
| 131 | + max_tokens: 32, |
| 132 | + }, |
| 133 | + fetch, |
| 134 | + getApiSecrets: async () => [ |
| 135 | + { |
| 136 | + type: "anthropic", |
| 137 | + secret: "test-secret", |
| 138 | + name: "anthropic", |
| 139 | + }, |
| 140 | + ], |
| 141 | + }); |
| 142 | + |
| 143 | + expect(headers["cache-control"]).toContain("no-transform"); |
| 144 | + expect( |
| 145 | + events().some((event) => event.data.choices[0]?.delta?.content === "hello"), |
| 146 | + ).toBe(true); |
| 147 | +}); |
| 148 | + |
54 | 149 | it("should convert OpenAI non-streaming request to Anthropic and back", async () => { |
55 | 150 | const { json } = await callProxyV1< |
56 | 151 | OpenAIChatCompletionCreateParams, |
|
0 commit comments