|
| 1 | +import { createOpenRouter } from '@/src'; |
| 2 | +import { streamText } from 'ai'; |
| 3 | +import { it, vi } from 'vitest'; |
| 4 | + |
| 5 | +vi.setConfig({ |
| 6 | + testTimeout: 42_000, |
| 7 | +}); |
| 8 | + |
| 9 | +it('should trigger cache read', async () => { |
| 10 | + // First call to warm the cache |
| 11 | + await callLLM(); |
| 12 | + // Second call to test cache read |
| 13 | + const response = await callLLM(); |
| 14 | + const providerMetadata = await response.providerMetadata; |
| 15 | + expect(providerMetadata?.openrouter).toMatchObject({ |
| 16 | + usage: expect.objectContaining({ |
| 17 | + promptTokens: expect.any(Number), |
| 18 | + completionTokens: expect.any(Number), |
| 19 | + promptTokensDetails: expect.objectContaining({ |
| 20 | + cachedTokens: expect.any(Number), |
| 21 | + }), |
| 22 | + completionTokensDetails: expect.any(Object), |
| 23 | + totalTokens: expect.any(Number), |
| 24 | + cost: expect.any(Number), |
| 25 | + }), |
| 26 | + }); |
| 27 | + |
| 28 | + const cachedTokens = Number( |
| 29 | + // @ts-ignore |
| 30 | + providerMetadata?.openrouter?.usage?.promptTokensDetails?.cachedTokens, |
| 31 | + ); |
| 32 | + |
| 33 | + expect(cachedTokens).toBeGreaterThan(0); |
| 34 | +}); |
| 35 | + |
| 36 | +async function callLLM() { |
| 37 | + const openrouter = createOpenRouter({ |
| 38 | + apiKey: process.env.OPENROUTER_API_KEY, |
| 39 | + baseUrl: `${process.env.OPENROUTER_API_BASE}/api/v1`, |
| 40 | + }); |
| 41 | + const model = openrouter('anthropic/claude-3.7-sonnet', { |
| 42 | + usage: { |
| 43 | + include: true, |
| 44 | + }, |
| 45 | + }); |
| 46 | + const response = streamText({ |
| 47 | + model, |
| 48 | + messages: [ |
| 49 | + { |
| 50 | + role: 'user', |
| 51 | + content: [ |
| 52 | + { |
| 53 | + type: 'text', |
| 54 | + text: 'a'.repeat(4200), |
| 55 | + providerOptions: { |
| 56 | + openrouter: { |
| 57 | + cache_control: { |
| 58 | + type: 'ephemeral', |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + }, |
| 63 | + { |
| 64 | + type: 'text', |
| 65 | + text: 'How many "a" did I use in the previous message?', |
| 66 | + }, |
| 67 | + ], |
| 68 | + }, |
| 69 | + ], |
| 70 | + }); |
| 71 | + |
| 72 | + await response.consumeStream(); |
| 73 | + return response; |
| 74 | +} |
0 commit comments