|
1 |
| -import { CoreMessage, FinishReason, simulateReadableStream } from 'ai'; |
| 1 | +import { simulateReadableStream } from 'ai'; |
2 | 2 | import { MockLanguageModelV1 } from 'ai/test';
|
3 |
| - |
4 |
| -interface ReasoningChunk { |
5 |
| - type: 'reasoning'; |
6 |
| - textDelta: string; |
7 |
| -} |
8 |
| - |
9 |
| -interface TextDeltaChunk { |
10 |
| - type: 'text-delta'; |
11 |
| - textDelta: string; |
12 |
| -} |
13 |
| - |
14 |
| -interface FinishChunk { |
15 |
| - type: 'finish'; |
16 |
| - finishReason: FinishReason; |
17 |
| - logprobs: undefined; |
18 |
| - usage: { completionTokens: number; promptTokens: number }; |
19 |
| -} |
20 |
| - |
21 |
| -type Chunk = TextDeltaChunk | ReasoningChunk | FinishChunk; |
22 |
| - |
23 |
| -const getResponseChunksByPrompt = ( |
24 |
| - prompt: CoreMessage[], |
25 |
| - isReasoningEnabled: boolean = false, |
26 |
| -): Array<Chunk> => { |
27 |
| - const userMessage = prompt.at(-1); |
28 |
| - |
29 |
| - if (!userMessage) { |
30 |
| - throw new Error('No user message found'); |
31 |
| - } |
32 |
| - |
33 |
| - if (isReasoningEnabled) { |
34 |
| - if ( |
35 |
| - compareMessages(userMessage, { |
36 |
| - role: 'user', |
37 |
| - content: [{ type: 'text', text: 'why is the sky blue?' }], |
38 |
| - }) |
39 |
| - ) { |
40 |
| - return [ |
41 |
| - { type: 'reasoning', textDelta: 'the ' }, |
42 |
| - { type: 'reasoning', textDelta: 'sky ' }, |
43 |
| - { type: 'reasoning', textDelta: 'is ' }, |
44 |
| - { type: 'reasoning', textDelta: 'blue ' }, |
45 |
| - { type: 'reasoning', textDelta: 'because ' }, |
46 |
| - { type: 'reasoning', textDelta: 'of ' }, |
47 |
| - { type: 'reasoning', textDelta: 'rayleigh ' }, |
48 |
| - { type: 'reasoning', textDelta: 'scattering! ' }, |
49 |
| - { type: 'text-delta', textDelta: "it's " }, |
50 |
| - { type: 'text-delta', textDelta: 'just ' }, |
51 |
| - { type: 'text-delta', textDelta: 'blue ' }, |
52 |
| - { type: 'text-delta', textDelta: 'duh!' }, |
53 |
| - { |
54 |
| - type: 'finish', |
55 |
| - finishReason: 'stop', |
56 |
| - logprobs: undefined, |
57 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
58 |
| - }, |
59 |
| - ]; |
60 |
| - } else if ( |
61 |
| - compareMessages(userMessage, { |
62 |
| - role: 'user', |
63 |
| - content: [{ type: 'text', text: 'why is grass green?' }], |
64 |
| - }) |
65 |
| - ) { |
66 |
| - return [ |
67 |
| - { type: 'reasoning', textDelta: 'grass ' }, |
68 |
| - { type: 'reasoning', textDelta: 'is ' }, |
69 |
| - { type: 'reasoning', textDelta: 'green ' }, |
70 |
| - { type: 'reasoning', textDelta: 'because ' }, |
71 |
| - { type: 'reasoning', textDelta: 'of ' }, |
72 |
| - { type: 'reasoning', textDelta: 'chlorophyll ' }, |
73 |
| - { type: 'reasoning', textDelta: 'absorption! ' }, |
74 |
| - { type: 'text-delta', textDelta: "it's " }, |
75 |
| - { type: 'text-delta', textDelta: 'just ' }, |
76 |
| - { type: 'text-delta', textDelta: 'green ' }, |
77 |
| - { type: 'text-delta', textDelta: 'duh!' }, |
78 |
| - { |
79 |
| - type: 'finish', |
80 |
| - finishReason: 'stop', |
81 |
| - logprobs: undefined, |
82 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
83 |
| - }, |
84 |
| - ]; |
85 |
| - } |
86 |
| - } |
87 |
| - |
88 |
| - if ( |
89 |
| - compareMessages(userMessage, { |
90 |
| - role: 'user', |
91 |
| - content: [{ type: 'text', text: 'why is grass green?' }], |
92 |
| - }) |
93 |
| - ) { |
94 |
| - return [ |
95 |
| - { type: 'text-delta', textDelta: "it's " }, |
96 |
| - { type: 'text-delta', textDelta: 'just ' }, |
97 |
| - { type: 'text-delta', textDelta: 'green ' }, |
98 |
| - { type: 'text-delta', textDelta: 'duh!' }, |
99 |
| - { |
100 |
| - type: 'finish', |
101 |
| - finishReason: 'stop', |
102 |
| - logprobs: undefined, |
103 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
104 |
| - }, |
105 |
| - ]; |
106 |
| - } else if ( |
107 |
| - compareMessages(userMessage, { |
108 |
| - role: 'user', |
109 |
| - content: [{ type: 'text', text: 'why is the sky blue?' }], |
110 |
| - }) |
111 |
| - ) { |
112 |
| - return [ |
113 |
| - { type: 'text-delta', textDelta: "it's " }, |
114 |
| - { type: 'text-delta', textDelta: 'just ' }, |
115 |
| - { type: 'text-delta', textDelta: 'blue ' }, |
116 |
| - { type: 'text-delta', textDelta: 'duh!' }, |
117 |
| - { |
118 |
| - type: 'finish', |
119 |
| - finishReason: 'stop', |
120 |
| - logprobs: undefined, |
121 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
122 |
| - }, |
123 |
| - ]; |
124 |
| - } else if ( |
125 |
| - compareMessages(userMessage, { |
126 |
| - role: 'user', |
127 |
| - content: [ |
128 |
| - { type: 'text', text: 'What are the advantages of using Next.js?' }, |
129 |
| - ], |
130 |
| - }) |
131 |
| - ) { |
132 |
| - return [ |
133 |
| - { type: 'text-delta', textDelta: 'with ' }, |
134 |
| - { type: 'text-delta', textDelta: 'next.js ' }, |
135 |
| - { type: 'text-delta', textDelta: 'you ' }, |
136 |
| - { type: 'text-delta', textDelta: 'can ' }, |
137 |
| - { type: 'text-delta', textDelta: 'ship ' }, |
138 |
| - { type: 'text-delta', textDelta: 'fast! ' }, |
139 |
| - { |
140 |
| - type: 'finish', |
141 |
| - finishReason: 'stop', |
142 |
| - logprobs: undefined, |
143 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
144 |
| - }, |
145 |
| - ]; |
146 |
| - } else if ( |
147 |
| - compareMessages(userMessage, { |
148 |
| - role: 'user', |
149 |
| - content: [ |
150 |
| - { |
151 |
| - type: 'text', |
152 |
| - text: 'who painted this?', |
153 |
| - }, |
154 |
| - { |
155 |
| - type: 'image', |
156 |
| - image: '...', |
157 |
| - }, |
158 |
| - ], |
159 |
| - }) |
160 |
| - ) { |
161 |
| - return [ |
162 |
| - { type: 'text-delta', textDelta: 'this ' }, |
163 |
| - { type: 'text-delta', textDelta: 'painting ' }, |
164 |
| - { type: 'text-delta', textDelta: 'is ' }, |
165 |
| - { type: 'text-delta', textDelta: 'by ' }, |
166 |
| - { type: 'text-delta', textDelta: 'monet!' }, |
167 |
| - { |
168 |
| - type: 'finish', |
169 |
| - finishReason: 'stop', |
170 |
| - logprobs: undefined, |
171 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
172 |
| - }, |
173 |
| - ]; |
174 |
| - } |
175 |
| - |
176 |
| - return []; |
177 |
| -}; |
| 3 | +import { getResponseChunksByPrompt } from '@/tests/prompts/utils'; |
178 | 4 |
|
179 | 5 | export const chatModel = new MockLanguageModelV1({
|
180 | 6 | doGenerate: async () => ({
|
@@ -236,46 +62,10 @@ export const artifactModel = new MockLanguageModelV1({
|
236 | 62 | usage: { promptTokens: 10, completionTokens: 20 },
|
237 | 63 | text: `Hello, world!`,
|
238 | 64 | }),
|
239 |
| - doStream: async () => ({ |
| 65 | + doStream: async ({ prompt }) => ({ |
240 | 66 | stream: simulateReadableStream({
|
241 |
| - chunks: [ |
242 |
| - { type: 'text-delta', textDelta: 'test' }, |
243 |
| - { |
244 |
| - type: 'finish', |
245 |
| - finishReason: 'stop', |
246 |
| - logprobs: undefined, |
247 |
| - usage: { completionTokens: 10, promptTokens: 3 }, |
248 |
| - }, |
249 |
| - ], |
| 67 | + chunks: getResponseChunksByPrompt(prompt), |
250 | 68 | }),
|
251 | 69 | rawCall: { rawPrompt: null, rawSettings: {} },
|
252 | 70 | }),
|
253 | 71 | });
|
254 |
| - |
255 |
| -function compareMessages(msg1: CoreMessage, msg2: CoreMessage): boolean { |
256 |
| - if (msg1.role !== msg2.role) return false; |
257 |
| - |
258 |
| - if (!Array.isArray(msg1.content) || !Array.isArray(msg2.content)) { |
259 |
| - return false; |
260 |
| - } |
261 |
| - |
262 |
| - if (msg1.content.length !== msg2.content.length) return false; |
263 |
| - |
264 |
| - for (let i = 0; i < msg1.content.length; i++) { |
265 |
| - const item1 = msg1.content[i]; |
266 |
| - const item2 = msg2.content[i]; |
267 |
| - |
268 |
| - if (item1.type !== item2.type) return false; |
269 |
| - |
270 |
| - if (item1.type === 'image' && item2.type === 'image') { |
271 |
| - // if (item1.image.toString() !== item2.image.toString()) return false; |
272 |
| - // if (item1.mimeType !== item2.mimeType) return false; |
273 |
| - } else if (item1.type === 'text' && item2.type === 'text') { |
274 |
| - if (item1.text !== item2.text) return false; |
275 |
| - } else { |
276 |
| - return false; |
277 |
| - } |
278 |
| - } |
279 |
| - |
280 |
| - return true; |
281 |
| -} |
0 commit comments