|
| 1 | +import { LanguageModelV2Prompt } from '@ai-sdk/provider'; |
| 2 | +import { convertReadableStreamToArray } from '@ai-sdk/provider-utils/test'; |
| 3 | +import { createTestServer } from '@ai-sdk/test-server/with-vitest'; |
| 4 | +import fs from 'node:fs'; |
| 5 | +import { beforeEach, describe, expect, it } from 'vitest'; |
| 6 | +import { createMoonshotAI } from './moonshotai-provider'; |
| 7 | + |
| 8 | +const TEST_PROMPT: LanguageModelV2Prompt = [ |
| 9 | + { role: 'user', content: [{ type: 'text', text: 'Hello' }] }, |
| 10 | +]; |
| 11 | + |
| 12 | +const provider = createMoonshotAI({ |
| 13 | + apiKey: 'test-api-key', |
| 14 | +}); |
| 15 | + |
| 16 | +const server = createTestServer({ |
| 17 | + 'https://api.moonshot.ai/v1/chat/completions': {}, |
| 18 | +}); |
| 19 | + |
| 20 | +function prepareChunksFixtureResponse(filename: string) { |
| 21 | + const chunks = fs |
| 22 | + .readFileSync(`src/__fixtures__/${filename}.chunks.txt`, 'utf8') |
| 23 | + .split('\n') |
| 24 | + .filter(line => line.trim().length > 0) |
| 25 | + .map(line => `data: ${line}\n\n`); |
| 26 | + chunks.push('data: [DONE]\n\n'); |
| 27 | + |
| 28 | + server.urls['https://api.moonshot.ai/v1/chat/completions'].response = { |
| 29 | + type: 'stream-chunks', |
| 30 | + chunks, |
| 31 | + }; |
| 32 | +} |
| 33 | + |
| 34 | +describe('MoonshotAIChatLanguageModel', () => { |
| 35 | + describe('doStream', () => { |
| 36 | + describe('cached tokens at top level (MoonshotAI format)', () => { |
| 37 | + beforeEach(() => { |
| 38 | + prepareChunksFixtureResponse('moonshot-cached-tokens'); |
| 39 | + }); |
| 40 | + |
| 41 | + it('should extract cachedInputTokens from top-level cached_tokens', async () => { |
| 42 | + const result = await provider.chatModel('kimi-k2.5').doStream({ |
| 43 | + prompt: TEST_PROMPT, |
| 44 | + }); |
| 45 | + |
| 46 | + const parts = await convertReadableStreamToArray(result.stream); |
| 47 | + const finishPart = parts.find(part => part.type === 'finish'); |
| 48 | + |
| 49 | + expect(finishPart).toBeDefined(); |
| 50 | + expect(finishPart!.type).toBe('finish'); |
| 51 | + if (finishPart!.type === 'finish') { |
| 52 | + expect(finishPart!.usage).toEqual({ |
| 53 | + inputTokens: 100, |
| 54 | + outputTokens: 10, |
| 55 | + totalTokens: 110, |
| 56 | + reasoningTokens: undefined, |
| 57 | + cachedInputTokens: 80, |
| 58 | + }); |
| 59 | + } |
| 60 | + }); |
| 61 | + |
| 62 | + it('should not emit raw chunks when not requested', async () => { |
| 63 | + const result = await provider.chatModel('kimi-k2.5').doStream({ |
| 64 | + prompt: TEST_PROMPT, |
| 65 | + }); |
| 66 | + |
| 67 | + const parts = await convertReadableStreamToArray(result.stream); |
| 68 | + const rawParts = parts.filter(part => part.type === 'raw'); |
| 69 | + |
| 70 | + expect(rawParts).toHaveLength(0); |
| 71 | + }); |
| 72 | + |
| 73 | + it('should emit raw chunks when includeRawChunks is true', async () => { |
| 74 | + const result = await provider.chatModel('kimi-k2.5').doStream({ |
| 75 | + prompt: TEST_PROMPT, |
| 76 | + includeRawChunks: true, |
| 77 | + }); |
| 78 | + |
| 79 | + const parts = await convertReadableStreamToArray(result.stream); |
| 80 | + const rawParts = parts.filter(part => part.type === 'raw'); |
| 81 | + |
| 82 | + expect(rawParts.length).toBeGreaterThan(0); |
| 83 | + }); |
| 84 | + }); |
| 85 | + |
| 86 | + describe('without cached tokens', () => { |
| 87 | + beforeEach(() => { |
| 88 | + prepareChunksFixtureResponse('moonshot-text'); |
| 89 | + }); |
| 90 | + |
| 91 | + it('should handle usage without cached_tokens', async () => { |
| 92 | + const result = await provider.chatModel('kimi-k2.5').doStream({ |
| 93 | + prompt: TEST_PROMPT, |
| 94 | + }); |
| 95 | + |
| 96 | + const parts = await convertReadableStreamToArray(result.stream); |
| 97 | + const finishPart = parts.find(part => part.type === 'finish'); |
| 98 | + |
| 99 | + expect(finishPart).toBeDefined(); |
| 100 | + expect(finishPart!.type).toBe('finish'); |
| 101 | + if (finishPart!.type === 'finish') { |
| 102 | + expect(finishPart!.usage).toEqual({ |
| 103 | + inputTokens: 50, |
| 104 | + outputTokens: 5, |
| 105 | + totalTokens: 55, |
| 106 | + reasoningTokens: undefined, |
| 107 | + cachedInputTokens: undefined, |
| 108 | + }); |
| 109 | + } |
| 110 | + }); |
| 111 | + }); |
| 112 | + }); |
| 113 | +}); |
0 commit comments