ai
Version:
AI SDK by Vercel - The AI Toolkit for TypeScript and JavaScript
115 lines (94 loc) • 3.46 kB
text/typescript
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: '123',
prompt: 'Hello, world!',
});
});
it('should require options when call options are provided', async () => {
const agent = new ToolLoopAgent<{ callOption: string }>({
model: new MockLanguageModelV3(),
});
expectTypeOf<Parameters<typeof agent.generate>[0]>().toEqualTypeOf<
AgentCallParameters<{ callOption: string }>
>();
});
it('should not require options when call options are not provided', async () => {
const agent = new ToolLoopAgent({
model: new MockLanguageModelV3(),
});
expectTypeOf<Parameters<typeof agent.generate>[0]>().toEqualTypeOf<
AgentCallParameters<never>
>();
});
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<typeof output>().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: '123',
prompt: 'Hello, world!',
});
});
it('should require options when call options are provided', async () => {
const agent = new ToolLoopAgent<{ callOption: string }>({
model: new MockLanguageModelV3(),
});
expectTypeOf<Parameters<typeof agent.stream>[0]>().toEqualTypeOf<
AgentStreamParameters<{ callOption: string }, {}>
>();
});
it('should not require options when call options are not provided', async () => {
const agent = new ToolLoopAgent({
model: new MockLanguageModelV3(),
});
expectTypeOf<Parameters<typeof agent.stream>[0]>().toEqualTypeOf<
AgentStreamParameters<never, {}>
>();
});
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<typeof partialOutputStream>().toEqualTypeOf<
AsyncIterableStream<DeepPartial<{ value: string }>>
>();
});
});
});