|
| 1 | +import { ChatCompletionCreateParams } from "openai/resources/index"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | + |
| 4 | +import OpenRouter from "./OpenRouter"; |
| 5 | + |
| 6 | +describe("OpenRouter Anthropic Caching", () => { |
| 7 | + it("should detect Anthropic models correctly", () => { |
| 8 | + const openRouter = new OpenRouter({ |
| 9 | + model: "claude-3-5-sonnet-latest", |
| 10 | + apiKey: "test-key", |
| 11 | + }); |
| 12 | + |
| 13 | + // Test private method through modifyChatBody |
| 14 | + const body: ChatCompletionCreateParams = { |
| 15 | + model: "claude-3-5-sonnet-latest", |
| 16 | + messages: [], |
| 17 | + }; |
| 18 | + |
| 19 | + // Should not throw |
| 20 | + openRouter["modifyChatBody"](body); |
| 21 | + }); |
| 22 | + |
| 23 | + it("should add cache_control to user messages when caching is enabled", () => { |
| 24 | + const openRouter = new OpenRouter({ |
| 25 | + model: "anthropic/claude-3.5-sonnet", |
| 26 | + apiKey: "test-key", |
| 27 | + cacheBehavior: { |
| 28 | + cacheConversation: true, |
| 29 | + cacheSystemMessage: false, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + const body: ChatCompletionCreateParams = { |
| 34 | + model: "anthropic/claude-3.5-sonnet", |
| 35 | + messages: [ |
| 36 | + { role: "user", content: "First message" }, |
| 37 | + { role: "assistant", content: "Response" }, |
| 38 | + { role: "user", content: "Second message" }, |
| 39 | + { role: "assistant", content: "Another response" }, |
| 40 | + { role: "user", content: "Third message" }, |
| 41 | + ], |
| 42 | + }; |
| 43 | + |
| 44 | + const modifiedBody = openRouter["modifyChatBody"](body); |
| 45 | + |
| 46 | + // Check that the last two user messages have cache_control |
| 47 | + const userMessages = modifiedBody.messages.filter( |
| 48 | + (msg: any) => msg.role === "user", |
| 49 | + ); |
| 50 | + |
| 51 | + // First user message should not have cache_control |
| 52 | + expect(userMessages[0].content).toBe("First message"); |
| 53 | + |
| 54 | + // Last two user messages should have cache_control |
| 55 | + expect(userMessages[1].content).toEqual([ |
| 56 | + { |
| 57 | + type: "text", |
| 58 | + text: "Second message", |
| 59 | + cache_control: { type: "ephemeral" }, |
| 60 | + }, |
| 61 | + ]); |
| 62 | + |
| 63 | + expect(userMessages[2].content).toEqual([ |
| 64 | + { |
| 65 | + type: "text", |
| 66 | + text: "Third message", |
| 67 | + cache_control: { type: "ephemeral" }, |
| 68 | + }, |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("should correctly handle cache_control with system messages present", () => { |
| 73 | + const openRouter = new OpenRouter({ |
| 74 | + model: "claude-3-5-sonnet-latest", |
| 75 | + apiKey: "test-key", |
| 76 | + cacheBehavior: { |
| 77 | + cacheConversation: true, |
| 78 | + cacheSystemMessage: true, |
| 79 | + }, |
| 80 | + }); |
| 81 | + |
| 82 | + const body: ChatCompletionCreateParams = { |
| 83 | + model: "claude-3-5-sonnet-latest", |
| 84 | + messages: [ |
| 85 | + { role: "system", content: "You are a helpful assistant" }, |
| 86 | + { role: "user", content: "First user message" }, |
| 87 | + { role: "assistant", content: "First assistant response" }, |
| 88 | + { role: "user", content: "Second user message" }, |
| 89 | + { role: "assistant", content: "Second assistant response" }, |
| 90 | + { role: "user", content: "Third user message" }, |
| 91 | + ], |
| 92 | + }; |
| 93 | + |
| 94 | + const modifiedBody = openRouter["modifyChatBody"](body); |
| 95 | + |
| 96 | + // System message should have cache_control |
| 97 | + expect(modifiedBody.messages[0]).toEqual({ |
| 98 | + role: "system", |
| 99 | + content: [ |
| 100 | + { |
| 101 | + type: "text", |
| 102 | + text: "You are a helpful assistant", |
| 103 | + cache_control: { type: "ephemeral" }, |
| 104 | + }, |
| 105 | + ], |
| 106 | + }); |
| 107 | + |
| 108 | + // Check user messages - should follow Anthropic filtering logic |
| 109 | + const userMessages = modifiedBody.messages.filter( |
| 110 | + (msg: any) => msg.role === "user", |
| 111 | + ); |
| 112 | + |
| 113 | + // First user message should NOT have cache_control (only last 2) |
| 114 | + expect(userMessages[0].content).toBe("First user message"); |
| 115 | + |
| 116 | + // Last two user messages should have cache_control |
| 117 | + expect(userMessages[1].content).toEqual([ |
| 118 | + { |
| 119 | + type: "text", |
| 120 | + text: "Second user message", |
| 121 | + cache_control: { type: "ephemeral" }, |
| 122 | + }, |
| 123 | + ]); |
| 124 | + |
| 125 | + expect(userMessages[2].content).toEqual([ |
| 126 | + { |
| 127 | + type: "text", |
| 128 | + text: "Third user message", |
| 129 | + cache_control: { type: "ephemeral" }, |
| 130 | + }, |
| 131 | + ]); |
| 132 | + |
| 133 | + // Assistant messages should remain unchanged |
| 134 | + expect(modifiedBody.messages[2].content).toBe("First assistant response"); |
| 135 | + expect(modifiedBody.messages[4].content).toBe("Second assistant response"); |
| 136 | + }); |
| 137 | + |
| 138 | + it("should add cache_control to system message when caching is enabled", () => { |
| 139 | + const openRouter = new OpenRouter({ |
| 140 | + model: "claude-3-5-sonnet-latest", |
| 141 | + apiKey: "test-key", |
| 142 | + cacheBehavior: { |
| 143 | + cacheConversation: false, |
| 144 | + cacheSystemMessage: true, |
| 145 | + }, |
| 146 | + }); |
| 147 | + |
| 148 | + const body: ChatCompletionCreateParams = { |
| 149 | + model: "claude-3-5-sonnet-latest", |
| 150 | + messages: [ |
| 151 | + { role: "system", content: "You are a helpful assistant" }, |
| 152 | + { role: "user", content: "Hello" }, |
| 153 | + ], |
| 154 | + }; |
| 155 | + |
| 156 | + const modifiedBody = openRouter["modifyChatBody"](body); |
| 157 | + |
| 158 | + // System message should have cache_control |
| 159 | + expect(modifiedBody.messages[0]).toEqual({ |
| 160 | + role: "system", |
| 161 | + content: [ |
| 162 | + { |
| 163 | + type: "text", |
| 164 | + text: "You are a helpful assistant", |
| 165 | + cache_control: { type: "ephemeral" }, |
| 166 | + }, |
| 167 | + ], |
| 168 | + }); |
| 169 | + |
| 170 | + // User message should remain unchanged |
| 171 | + expect(modifiedBody.messages[1]).toEqual({ |
| 172 | + role: "user", |
| 173 | + content: "Hello", |
| 174 | + }); |
| 175 | + }); |
| 176 | + |
| 177 | + it("should handle array content correctly", () => { |
| 178 | + const openRouter = new OpenRouter({ |
| 179 | + model: "claude-3-5-sonnet-latest", |
| 180 | + apiKey: "test-key", |
| 181 | + cacheBehavior: { |
| 182 | + cacheConversation: true, |
| 183 | + cacheSystemMessage: false, |
| 184 | + }, |
| 185 | + }); |
| 186 | + |
| 187 | + const body: ChatCompletionCreateParams = { |
| 188 | + model: "claude-3-5-sonnet-latest", |
| 189 | + messages: [ |
| 190 | + { |
| 191 | + role: "user", |
| 192 | + content: [ |
| 193 | + { type: "text", text: "First part" }, |
| 194 | + { type: "text", text: "Second part" }, |
| 195 | + ], |
| 196 | + }, |
| 197 | + ], |
| 198 | + }; |
| 199 | + |
| 200 | + const modifiedBody = openRouter["modifyChatBody"](body); |
| 201 | + |
| 202 | + // Only the last text part should have cache_control |
| 203 | + expect(modifiedBody.messages[0].content).toEqual([ |
| 204 | + { type: "text", text: "First part" }, |
| 205 | + { |
| 206 | + type: "text", |
| 207 | + text: "Second part", |
| 208 | + cache_control: { type: "ephemeral" }, |
| 209 | + }, |
| 210 | + ]); |
| 211 | + }); |
| 212 | + |
| 213 | + it("should not modify messages for non-Anthropic models", () => { |
| 214 | + const openRouter = new OpenRouter({ |
| 215 | + model: "gpt-4o", |
| 216 | + apiKey: "test-key", |
| 217 | + cacheBehavior: { |
| 218 | + cacheConversation: true, |
| 219 | + cacheSystemMessage: true, |
| 220 | + }, |
| 221 | + }); |
| 222 | + |
| 223 | + const body: ChatCompletionCreateParams = { |
| 224 | + model: "gpt-4o", |
| 225 | + messages: [ |
| 226 | + { role: "system", content: "System message" }, |
| 227 | + { role: "user", content: "User message" }, |
| 228 | + ], |
| 229 | + }; |
| 230 | + |
| 231 | + const modifiedBody = openRouter["modifyChatBody"](body); |
| 232 | + |
| 233 | + // Messages should remain unchanged |
| 234 | + expect(modifiedBody.messages).toEqual(body.messages); |
| 235 | + }); |
| 236 | +}); |
0 commit comments