Skip to content
Closed
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions packages/ai/src/agent/create-agent-ui-stream-response.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ describe('createAgentUIStreamResponse', () => {
"tools": [
{
"description": "Example tool",
"hasExecuteFunction": false,
"inputSchema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
Expand All @@ -209,6 +210,7 @@ describe('createAgentUIStreamResponse', () => {
},
"name": "example",
"providerOptions": undefined,
"title": undefined,
"type": "function",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ exports[`streamText > options.stopWhen > 2 steps: initial, tool-result > should
"ai.prompt.messages": "[{"role":"user","content":[{"type":"text","text":"test-input"}]}]",
"ai.prompt.toolChoice": "{"type":"auto"}",
"ai.prompt.tools": [
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false}}",
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false},"hasExecuteFunction":true}",
],
"ai.response.avgOutputTokensPerSecond": 20,
"ai.response.finishReason": "tool-calls",
Expand Down Expand Up @@ -172,7 +172,7 @@ exports[`streamText > options.stopWhen > 2 steps: initial, tool-result > should
"ai.prompt.messages": "[{"role":"user","content":[{"type":"text","text":"test-input"}]},{"role":"assistant","content":[{"type":"reasoning","text":"thinking"},{"type":"tool-call","toolCallId":"call-1","toolName":"tool1","input":{"value":"value"}}]},{"role":"tool","content":[{"type":"tool-result","toolCallId":"call-1","toolName":"tool1","output":{"type":"text","value":"result1"}}]}]",
"ai.prompt.toolChoice": "{"type":"auto"}",
"ai.prompt.tools": [
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false}}",
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false},"hasExecuteFunction":true}",
],
"ai.response.avgOutputTokensPerSecond": 25,
"ai.response.finishReason": "stop",
Expand Down Expand Up @@ -255,7 +255,7 @@ exports[`streamText > options.transform > with base transformation > telemetry s
"ai.prompt.messages": "[{"role":"user","content":[{"type":"text","text":"test-input"}]}]",
"ai.prompt.toolChoice": "{"type":"auto"}",
"ai.prompt.tools": [
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false}}",
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false},"hasExecuteFunction":true}",
],
"ai.response.avgOutputTokensPerSecond": 20,
"ai.response.finishReason": "stop",
Expand Down Expand Up @@ -338,6 +338,7 @@ exports[`streamText > programmatic tool calling > 5 steps: code_execution trigge
{
"input": {
"code": "game_loop()",
"type": "programmatic-tool-call",
},
"providerExecuted": true,
"providerOptions": undefined,
Expand Down Expand Up @@ -1127,7 +1128,7 @@ exports[`streamText > telemetry > should record successful tool call 1`] = `
"ai.prompt.messages": "[{"role":"user","content":[{"type":"text","text":"test-input"}]}]",
"ai.prompt.toolChoice": "{"type":"auto"}",
"ai.prompt.tools": [
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false}}",
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false},"hasExecuteFunction":true}",
],
"ai.response.avgOutputTokensPerSecond": 20,
"ai.response.finishReason": "stop",
Expand Down
58 changes: 38 additions & 20 deletions packages/ai/src/generate-text/generate-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -490,6 +490,7 @@ describe('generateText', () => {
type: 'function',
name: 'tool1',
description: undefined,
title: undefined,
inputSchema: {
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: false,
Expand All @@ -498,11 +499,13 @@ describe('generateText', () => {
type: 'object',
},
providerOptions: undefined,
hasExecuteFunction: false,
},
{
type: 'function',
name: 'tool2',
description: undefined,
title: undefined,
inputSchema: {
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: false,
Expand All @@ -511,6 +514,7 @@ describe('generateText', () => {
type: 'object',
},
providerOptions: undefined,
hasExecuteFunction: false,
},
]);

Expand Down Expand Up @@ -587,6 +591,7 @@ describe('generateText', () => {
type: 'function',
name: 'tool1',
description: undefined,
title: undefined,
inputSchema: {
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: false,
Expand All @@ -595,6 +600,7 @@ describe('generateText', () => {
type: 'object',
},
providerOptions: undefined,
hasExecuteFunction: true,
},
]);

Expand Down Expand Up @@ -2539,6 +2545,7 @@ describe('generateText', () => {
type: 'function',
name: 'tool1',
description: undefined,
title: 'Tool One',
inputSchema: {
$schema: 'http://json-schema.org/draft-07/schema#',
additionalProperties: false,
Expand All @@ -2547,6 +2554,7 @@ describe('generateText', () => {
type: 'object',
},
providerOptions: undefined,
hasExecuteFunction: true,
},
]);

Expand Down Expand Up @@ -3327,6 +3335,7 @@ describe('generateText', () => {
"tools": [
{
"description": undefined,
"hasExecuteFunction": true,
"inputSchema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
Expand All @@ -3342,6 +3351,7 @@ describe('generateText', () => {
},
"name": "tool1",
"providerOptions": undefined,
"title": undefined,
"type": "function",
},
],
Expand Down Expand Up @@ -4229,28 +4239,30 @@ describe('generateText', () => {
});

expect(tools).toMatchInlineSnapshot(`
[
{
"description": undefined,
"inputSchema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"value": {
"type": "string",
[
{
"description": undefined,
"hasExecuteFunction": true,
"inputSchema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
"properties": {
"value": {
"type": "string",
},
},
"required": [
"value",
],
"type": "object",
},
"required": [
"value",
],
"type": "object",
"name": "tool1",
"providerOptions": undefined,
"title": undefined,
"type": "function",
},
"name": "tool1",
"providerOptions": undefined,
"type": "function",
},
]
`);
]
`);
});
});

Expand Down Expand Up @@ -4385,7 +4397,7 @@ describe('generateText', () => {
"ai.prompt.messages": "[{"role":"user","content":[{"type":"text","text":"test-input"}]}]",
"ai.prompt.toolChoice": "{"type":"auto"}",
"ai.prompt.tools": [
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false}}",
"{"type":"function","name":"tool1","inputSchema":{"$schema":"http://json-schema.org/draft-07/schema#","type":"object","properties":{"value":{"type":"string"}},"required":["value"],"additionalProperties":false},"hasExecuteFunction":true}",
],
"ai.request.headers.user-agent": "ai/0.0.0-test",
"ai.response.finishReason": "stop",
Expand Down Expand Up @@ -4813,25 +4825,29 @@ describe('generateText', () => {
type: 'function',
name: 'tool1',
description: undefined,
title: undefined,
inputSchema: {
additionalProperties: false,
properties: { value: { type: 'string' } },
required: ['value'],
type: 'object',
},
providerOptions: undefined,
hasExecuteFunction: false,
},
{
type: 'function',
name: 'tool2',
description: undefined,
title: undefined,
inputSchema: {
additionalProperties: false,
properties: { somethingElse: { type: 'string' } },
required: ['somethingElse'],
type: 'object',
},
providerOptions: undefined,
hasExecuteFunction: false,
},
]);

Expand Down Expand Up @@ -6061,6 +6077,7 @@ describe('generateText', () => {
},
{
"description": "Roll a die and return the result.",
"hasExecuteFunction": true,
"inputSchema": {
"$schema": "http://json-schema.org/draft-07/schema#",
"additionalProperties": false,
Expand All @@ -6086,6 +6103,7 @@ describe('generateText', () => {
],
},
},
"title": undefined,
"type": "function",
},
]
Expand Down
9 changes: 3 additions & 6 deletions packages/ai/src/generate-text/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ import type {
OnToolCallStartEvent,
} from './core-events';
import { executeToolCall } from './execute-tool-call';
import { filterActiveTools } from './filter-active-tool';
import { GenerateTextResult } from './generate-text-result';
import { DefaultGeneratedFile } from './generated-file';
import { isApprovalNeeded } from './is-approval-needed';
Expand Down Expand Up @@ -700,15 +699,13 @@ export async function generateText<
experimental_context =
prepareStepResult?.experimental_context ?? experimental_context;

const stepActiveTools = filterActiveTools({
tools,
activeTools: prepareStepResult?.activeTools ?? activeTools,
});
const stepActiveTools = prepareStepResult?.activeTools ?? activeTools;

const { toolChoice: stepToolChoice, tools: stepTools } =
await prepareToolsAndToolChoice({
tools: stepActiveTools,
tools,
toolChoice: prepareStepResult?.toolChoice ?? toolChoice,
activeTools: stepActiveTools,
});

const stepMessages = prepareStepResult?.messages ?? stepInputMessages;
Expand Down
7 changes: 7 additions & 0 deletions packages/ai/src/generate-text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ export {
streamModelCall as experimental_streamModelCall,
type ModelCallStreamPart as Experimental_ModelCallStreamPart,
} from './stream-model-call';
export type {
FunctionToolDescriptor,
ProviderToolDescriptor,
ToolChoiceDescriptor,
ToolDescriptor,
} from './tool-descriptor';
export { prepareToolsAndToolChoice as experimental_prepareToolsAndToolChoice } from '../prompt/prepare-tools-and-tool-choice';
export {
streamText,
type StreamTextOnChunkCallback,
Expand Down
11 changes: 10 additions & 1 deletion packages/ai/src/generate-text/stream-model-call.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { describe, expect, it } from 'vitest';
import z from 'zod';
import { NoSuchToolError } from '../error/no-such-tool-error';
import { MockLanguageModelV4 } from '../test/mock-language-model-v4';
import { prepareToolsAndToolChoice } from '../prompt/prepare-tools-and-tool-choice';
import { streamModelCall } from './stream-model-call';
import { ToolCallRepairFunction } from './tool-call-repair-function';
import { ToolSet } from './tool-set';
Expand Down Expand Up @@ -44,9 +45,17 @@ async function streamModelCallResult<TOOLS extends ToolSet>({
}),
});

const { tools: preparedTools, toolChoice: preparedToolChoice } =
await prepareToolsAndToolChoice({
tools,
toolChoice: undefined,
activeTools: undefined,
});

const { stream } = await streamModelCall({
model,
tools,
tools: preparedTools,
toolChoice: preparedToolChoice,
prompt: 'test prompt',
system: undefined,
repairToolCall,
Expand Down
Loading
Loading