|
| 1 | +import { afterEach, describe, expect, it } from "vitest"; |
| 2 | +import { GenkitPlugin } from "./genkit-plugin"; |
| 3 | +import { genkitChannels } from "./genkit-channels"; |
| 4 | + |
| 5 | +function singleQueueStream<T>( |
| 6 | + chunks: T[], |
| 7 | +): AsyncIterable<T> & AsyncIterator<T> { |
| 8 | + let index = 0; |
| 9 | + return { |
| 10 | + async next() { |
| 11 | + await Promise.resolve(); |
| 12 | + if (index >= chunks.length) { |
| 13 | + return { done: true, value: undefined }; |
| 14 | + } |
| 15 | + return { done: false, value: chunks[index++] }; |
| 16 | + }, |
| 17 | + [Symbol.asyncIterator]() { |
| 18 | + return this; |
| 19 | + }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +async function drainMicrotasks(): Promise<void> { |
| 24 | + for (let index = 0; index < 5; index++) { |
| 25 | + await Promise.resolve(); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +async function collectAsync<T>(stream: AsyncIterable<T>): Promise<T[]> { |
| 30 | + const chunks: T[] = []; |
| 31 | + for await (const chunk of stream) { |
| 32 | + chunks.push(chunk); |
| 33 | + } |
| 34 | + return chunks; |
| 35 | +} |
| 36 | + |
| 37 | +describe("GenkitPlugin stream patching", () => { |
| 38 | + const plugin = new GenkitPlugin(); |
| 39 | + |
| 40 | + afterEach(() => { |
| 41 | + plugin.disable(); |
| 42 | + }); |
| 43 | + |
| 44 | + it("does not consume generateStream chunks before user code reads them", async () => { |
| 45 | + plugin.enable(); |
| 46 | + const stream = singleQueueStream([{ text: "hello" }, { text: " world" }]); |
| 47 | + |
| 48 | + const result = genkitChannels.generateStream.traceSync( |
| 49 | + () => ({ |
| 50 | + response: Promise.resolve({ |
| 51 | + text: "hello world", |
| 52 | + usage: { |
| 53 | + inputTokens: 1, |
| 54 | + outputTokens: 2, |
| 55 | + totalTokens: 3, |
| 56 | + }, |
| 57 | + }), |
| 58 | + stream, |
| 59 | + }), |
| 60 | + { arguments: [{ prompt: "Say hello world." }] } as Parameters< |
| 61 | + typeof genkitChannels.generateStream.traceSync |
| 62 | + >[1], |
| 63 | + ); |
| 64 | + |
| 65 | + await drainMicrotasks(); |
| 66 | + |
| 67 | + await expect(collectAsync(result.stream)).resolves.toEqual([ |
| 68 | + { text: "hello" }, |
| 69 | + { text: " world" }, |
| 70 | + ]); |
| 71 | + }); |
| 72 | + |
| 73 | + it("does not consume action.stream chunks before user code reads them", async () => { |
| 74 | + plugin.enable(); |
| 75 | + const stream = singleQueueStream(["first", "second"]); |
| 76 | + const action = Object.assign(() => Promise.resolve(), { |
| 77 | + __action: { |
| 78 | + actionType: "tool", |
| 79 | + name: "streamTool", |
| 80 | + }, |
| 81 | + }); |
| 82 | + |
| 83 | + const result = genkitChannels.actionStream.traceSync( |
| 84 | + () => ({ |
| 85 | + output: Promise.resolve({ done: true }), |
| 86 | + stream, |
| 87 | + }), |
| 88 | + { |
| 89 | + arguments: [{ input: true }], |
| 90 | + self: action, |
| 91 | + } as Parameters<typeof genkitChannels.actionStream.traceSync>[1], |
| 92 | + ); |
| 93 | + |
| 94 | + await drainMicrotasks(); |
| 95 | + |
| 96 | + await expect(collectAsync(result.stream)).resolves.toEqual([ |
| 97 | + "first", |
| 98 | + "second", |
| 99 | + ]); |
| 100 | + }); |
| 101 | +}); |
0 commit comments