Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 114 additions & 0 deletions packages/ai/src/agent/tool-loop-agent.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { LanguageModelV3CallOptions } from '@ai-sdk/provider';
import { convertArrayToReadableStream } from '@ai-sdk/provider-utils/test';
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { z } from 'zod/v4';
import { object } from '../generate-text/output';
import { MockLanguageModelV3 } from '../test/mock-language-model-v3';
import { ToolLoopAgent } from './tool-loop-agent';

Expand Down Expand Up @@ -89,6 +91,52 @@ describe('ToolLoopAgent', () => {
expect(doGenerateOptions?.abortSignal).toBeDefined();
});

it('should forward Output.object responseFormat to generateText', async () => {
let jsonDoGenerateOptions: LanguageModelV3CallOptions | undefined;
const jsonModel = new MockLanguageModelV3({
doGenerate: async options => {
jsonDoGenerateOptions = options;
return {
content: [{ type: 'text', text: '{"value":"ok"}' }],
finishReason: { unified: 'stop', raw: 'stop' },
usage: {
cachedInputTokens: undefined,
inputTokens: {
total: 3,
noCache: 3,
cacheRead: undefined,
cacheWrite: undefined,
},
outputTokens: {
total: 10,
text: 10,
reasoning: undefined,
},
},
warnings: [],
};
},
});

const agent = new ToolLoopAgent({
model: jsonModel,
output: object({
schema: z.object({ value: z.string() }),
}),
});

await agent.generate({
prompt: 'Hello, world!',
});

expect(jsonDoGenerateOptions?.responseFormat).toMatchObject({
type: 'json',
schema: expect.objectContaining({
type: 'object',
}),
});
});

it('should pass experimental_download to generateText', async () => {
const downloadFunction = vi
.fn()
Expand Down Expand Up @@ -366,6 +414,72 @@ describe('ToolLoopAgent', () => {
expect(doStreamOptions?.abortSignal).toBeDefined();
});

it('should forward Output.object responseFormat to streamText', async () => {
let jsonDoStreamOptions: LanguageModelV3CallOptions | undefined;
const jsonModel = new MockLanguageModelV3({
doStream: async options => {
jsonDoStreamOptions = options;
return {
stream: convertArrayToReadableStream([
{
type: 'stream-start',
warnings: [],
},
{
type: 'response-metadata',
id: 'id-0',
modelId: 'mock-model-id',
timestamp: new Date(0),
},
{ type: 'text-start', id: '1' },
{ type: 'text-delta', id: '1', delta: '{"value":"ok"}' },
{ type: 'text-end', id: '1' },
{
type: 'finish',
finishReason: { unified: 'stop', raw: 'stop' },
usage: {
inputTokens: {
total: 3,
noCache: 3,
cacheRead: undefined,
cacheWrite: undefined,
},
outputTokens: {
total: 10,
text: 10,
reasoning: undefined,
},
},
providerMetadata: {
testProvider: { testKey: 'testValue' },
},
},
]),
};
},
});

const agent = new ToolLoopAgent({
model: jsonModel,
output: object({
schema: z.object({ value: z.string() }),
}),
});

const result = await agent.stream({
prompt: 'Hello, world!',
});

await result.consumeStream();

expect(jsonDoStreamOptions?.responseFormat).toMatchObject({
type: 'json',
schema: expect.objectContaining({
type: 'object',
}),
});
});

it('should pass string instructions', async () => {
const agent = new ToolLoopAgent({
model: mockModel,
Expand Down
Loading