Skip to content

Commit

Permalink
chore (core): rename CoreTool to Tool (#4548)
Browse files Browse the repository at this point in the history
  • Loading branch information
lgrammel authored Jan 27, 2025
1 parent 9881bd8 commit 3a602ca
Show file tree
Hide file tree
Showing 42 changed files with 206 additions and 168 deletions.
7 changes: 7 additions & 0 deletions .changeset/fluffy-snails-hide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@ai-sdk/provider-utils': patch
'@ai-sdk/ui-utils': patch
'ai': patch
---

chore (core): rename CoreTool to Tool
4 changes: 2 additions & 2 deletions content/docs/03-ai-sdk-core/05-generating-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ all text to uppercase:

```ts
const upperCaseTransform =
<TOOLS extends Record<string, CoreTool>>() =>
<TOOLS extends ToolSet>() =>
(options: { tools: TOOLS; stopStream: () => void }) =>
new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({
transform(chunk, controller) {
Expand All @@ -263,7 +263,7 @@ and all callbacks are invoked.

```ts
const stopWordTransform =
<TOOLS extends Record<string, CoreTool>>() =>
<TOOLS extends ToolSet>() =>
({ stopStream }: { stopStream: () => void }) =>
new TransformStream<TextStreamPart<TOOLS>, TextStreamPart<TOOLS>>({
// note: this is a simplified transformation for testing;
Expand Down
16 changes: 8 additions & 8 deletions content/docs/03-ai-sdk-core/15-tools-and-tool-calling.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -304,18 +304,18 @@ To enable this, the AI SDK provides several helper types for tools, tool calls,
You can use them to strongly type your variables, function parameters, and return types
in parts of the code that are not directly related to `streamText` or `generateText`.

Each tool call is typed with `CoreToolCall<NAME extends string, ARGS>`, depending
Each tool call is typed with `ToolCall<NAME extends string, ARGS>`, depending
on the tool that has been invoked.
Similarly, the tool results are typed with `CoreToolResult<NAME extends string, ARGS, RESULT>`.
Similarly, the tool results are typed with `ToolResult<NAME extends string, ARGS, RESULT>`.

The tools in `streamText` and `generateText` are defined as a `Record<string, CoreTool>`.
The type inference helpers `CoreToolCallUnion<TOOLS extends Record<string, CoreTool>>`
and `CoreToolResultUnion<TOOLS extends Record<string, CoreTool>>` can be used to
The tools in `streamText` and `generateText` are defined as a `ToolSet`.
The type inference helpers `ToolCallUnion<TOOLS extends ToolSet>`
and `ToolResultUnion<TOOLS extends ToolSet>` can be used to
extract the tool call and tool result types from the tools.

```ts highlight="18-19,23-24"
import { openai } from '@ai-sdk/openai';
import { CoreToolCallUnion, CoreToolResultUnion, generateText, tool } from 'ai';
import { ToolCallUnion, ToolResultUnion, generateText, tool } from 'ai';
import { z } from 'zod';

const myToolSet = {
Expand All @@ -331,8 +331,8 @@ const myToolSet = {
}),
};

type MyToolCall = CoreToolCallUnion<typeof myToolSet>;
type MyToolResult = CoreToolResultUnion<typeof myToolSet>;
type MyToolCall = ToolCallUnion<typeof myToolSet>;
type MyToolResult = ToolResultUnion<typeof myToolSet>;

async function generateSomething(prompt: string): Promise<{
text: string;
Expand Down
4 changes: 2 additions & 2 deletions content/docs/07-reference/01-ai-sdk-core/01-generate-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -260,12 +260,12 @@ To see `generateText` in action, check out [these examples](#examples).
},
{
name: 'tools',
type: 'Record<string, CoreTool>',
type: 'ToolSet',
description:
'Tools that are accessible to and can be called by the model. The model needs to support calling tools.',
properties: [
{
type: 'CoreTool',
type: 'Tool',
parameters: [
{
name: 'description',
Expand Down
4 changes: 2 additions & 2 deletions content/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,12 @@ To see `streamText` in action, check out [these examples](#examples).
},
{
name: 'tools',
type: 'Record<string, CoreTool>',
type: 'ToolSet',
description:
'Tools that are accessible to and can be called by the model. The model needs to support calling tools.',
properties: [
{
type: 'CoreTool',
type: 'Tool',
parameters: [
{
name: 'description',
Expand Down
4 changes: 2 additions & 2 deletions content/docs/07-reference/01-ai-sdk-core/20-tool.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ export const weatherTool = tool({
content={[
{
name: 'tool',
type: 'CoreTool',
type: 'Tool',
description: 'The tool definition.',
properties: [
{
type: 'CoreTool',
type: 'Tool',
parameters: [
{
name: 'description',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export async function POST(req: Request) {
},
{
name: 'options',
type: '{ tools?: Record<string, Tool> }',
type: '{ tools?: ToolSet }',
description:
'Optional configuration object. Provide tools to enable multi-modal tool responses.',
},
Expand Down
2 changes: 1 addition & 1 deletion content/docs/07-reference/03-ai-sdk-rsc/01-stream-ui.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ To see `streamUI` in action, check out [these examples](#examples).
},
{
name: 'tools',
type: 'Record<string, Tool>',
type: 'ToolSet',
description:
'Tools that are accessible to and can be called by the model.',
properties: [
Expand Down
4 changes: 2 additions & 2 deletions content/docs/07-reference/03-ai-sdk-rsc/20-render.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ A helper function to create a streamable UI from LLM providers. This function is
},
{
name: 'functions',
type: 'Record<string, Tool>',
type: 'ToolSet',
isOptional: true,
description:
'Tools that are accessible to and can be called by the model.',
Expand Down Expand Up @@ -196,7 +196,7 @@ A helper function to create a streamable UI from LLM providers. This function is
},
{
name: 'tools',
type: 'Record<string, Tool>',
type: 'ToolSet',
isOptional: true,
description:
'Tools that are accessible to and can be called by the model.',
Expand Down
6 changes: 3 additions & 3 deletions examples/ai-core/src/types/tool-set.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { openai } from '@ai-sdk/openai';
import { CoreToolCallUnion, CoreToolResultUnion, generateText, tool } from 'ai';
import { ToolCallUnion, ToolResultUnion, generateText, tool } from 'ai';
import { z } from 'zod';

const myToolSet = {
Expand All @@ -15,8 +15,8 @@ const myToolSet = {
}),
};

type MyToolCall = CoreToolCallUnion<typeof myToolSet>;
type MyToolResult = CoreToolResultUnion<typeof myToolSet>;
type MyToolCall = ToolCallUnion<typeof myToolSet>;
type MyToolResult = ToolResultUnion<typeof myToolSet>;

async function generateSomething(prompt: string): Promise<{
text: string;
Expand Down
7 changes: 2 additions & 5 deletions packages/ai/core/generate-text/generate-text-result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CoreAssistantMessage, CoreToolMessage } from '../prompt';
import { CoreTool } from '../tool/tool';
import {
CallWarning,
FinishReason,
Expand All @@ -12,15 +11,13 @@ import { LanguageModelUsage } from '../types/usage';
import { StepResult } from './step-result';
import { ToolCallArray } from './tool-call';
import { ToolResultArray } from './tool-result';
import { ToolSet } from './tool-set';

/**
The result of a `generateText` call.
It contains the generated text, the tool calls that were made during the generation, and the results of the tool calls.
*/
export interface GenerateTextResult<
TOOLS extends Record<string, CoreTool>,
OUTPUT,
> {
export interface GenerateTextResult<TOOLS extends ToolSet, OUTPUT> {
/**
The generated text.
*/
Expand Down
12 changes: 6 additions & 6 deletions packages/ai/core/generate-text/generate-text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ import { getTracer } from '../telemetry/get-tracer';
import { recordSpan } from '../telemetry/record-span';
import { selectTelemetryAttributes } from '../telemetry/select-telemetry-attributes';
import { TelemetrySettings } from '../telemetry/telemetry-settings';
import { CoreTool } from '../tool/tool';
import { CoreToolChoice, LanguageModel, ProviderMetadata } from '../types';
import { LanguageModel, ProviderMetadata, ToolChoice } from '../types';
import {
addLanguageModelUsage,
calculateLanguageModelUsage,
Expand All @@ -33,6 +32,7 @@ import { toResponseMessages } from './to-response-messages';
import { ToolCallArray } from './tool-call';
import { ToolCallRepairFunction } from './tool-call-repair';
import { ToolResultArray } from './tool-result';
import { ToolSet } from './tool-set';

const originalGenerateId = createIdGenerator({
prefix: 'aitxt',
Expand Down Expand Up @@ -92,7 +92,7 @@ If set and supported by the model, calls will generate deterministic results.
A result object that contains the generated text, the results of the tool calls, and additional information.
*/
export async function generateText<
TOOLS extends Record<string, CoreTool>,
TOOLS extends ToolSet,
OUTPUT = never,
OUTPUT_PARTIAL = never,
>({
Expand Down Expand Up @@ -134,7 +134,7 @@ The tools that the model can call. The model needs to support calling tools.
/**
The tool choice strategy. Default: 'auto'.
*/
toolChoice?: CoreToolChoice<TOOLS>;
toolChoice?: ToolChoice<TOOLS>;

/**
Maximum number of sequential LLM calls (steps), e.g. when you use tool calls. Must be at least 1.
Expand Down Expand Up @@ -560,7 +560,7 @@ A function that attempts to repair a tool call that failed to parse.
});
}

async function executeTools<TOOLS extends Record<string, CoreTool>>({
async function executeTools<TOOLS extends ToolSet>({
toolCalls,
tools,
tracer,
Expand Down Expand Up @@ -653,7 +653,7 @@ async function executeTools<TOOLS extends Record<string, CoreTool>>({
);
}

class DefaultGenerateTextResult<TOOLS extends Record<string, CoreTool>, OUTPUT>
class DefaultGenerateTextResult<TOOLS extends ToolSet, OUTPUT>
implements GenerateTextResult<TOOLS, OUTPUT>
{
readonly text: GenerateTextResult<TOOLS, OUTPUT>['text'];
Expand Down
14 changes: 9 additions & 5 deletions packages/ai/core/generate-text/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ export type { StreamTextTransform } from './stream-text';
export type { StreamTextResult, TextStreamPart } from './stream-text-result';
export type { ToolCallRepairFunction } from './tool-call-repair';

// TODO 4.1: rename to ToolCall and ToolResult, deprecate old names
export type {
ToolCall as CoreToolCall,
ToolCallUnion as CoreToolCallUnion,
CoreToolCall,
CoreToolCallUnion,
ToolCall,
ToolCallUnion,
} from './tool-call';
export type {
ToolResult as CoreToolResult,
ToolResultUnion as CoreToolResultUnion,
CoreToolResult,
CoreToolResultUnion,
ToolResult,
ToolResultUnion,
} from './tool-result';
export type { ToolSet } from './tool-set';
8 changes: 4 additions & 4 deletions packages/ai/core/generate-text/parse-tool-call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import { safeParseJSON, safeValidateTypes } from '@ai-sdk/provider-utils';
import { Schema, asSchema } from '@ai-sdk/ui-utils';
import { InvalidToolArgumentsError } from '../../errors/invalid-tool-arguments-error';
import { NoSuchToolError } from '../../errors/no-such-tool-error';
import { ToolCallRepairError } from '../../errors/tool-call-repair-error';
import { CoreMessage } from '../prompt';
import { CoreTool } from '../tool';
import { inferParameters } from '../tool/tool';
import { ToolCallUnion } from './tool-call';
import { ToolCallRepairFunction } from './tool-call-repair';
import { ToolCallRepairError } from '../../errors/tool-call-repair-error';
import { ToolSet } from './tool-set';

export async function parseToolCall<TOOLS extends Record<string, CoreTool>>({
export async function parseToolCall<TOOLS extends ToolSet>({
toolCall,
tools,
repairToolCall,
Expand Down Expand Up @@ -68,7 +68,7 @@ export async function parseToolCall<TOOLS extends Record<string, CoreTool>>({
}
}

async function doParseToolCall<TOOLS extends Record<string, CoreTool>>({
async function doParseToolCall<TOOLS extends ToolSet>({
toolCall,
tools,
}: {
Expand Down
8 changes: 3 additions & 5 deletions packages/ai/core/generate-text/run-tools-transformation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import { assembleOperationName } from '../telemetry/assemble-operation-name';
import { recordSpan } from '../telemetry/record-span';
import { selectTelemetryAttributes } from '../telemetry/select-telemetry-attributes';
import { TelemetrySettings } from '../telemetry/telemetry-settings';
import { CoreTool } from '../tool';
import {
FinishReason,
LanguageModelUsage,
Expand All @@ -19,10 +18,9 @@ import { parseToolCall } from './parse-tool-call';
import { ToolCallUnion } from './tool-call';
import { ToolCallRepairFunction } from './tool-call-repair';
import { ToolResultUnion } from './tool-result';
import { ToolSet } from './tool-set';

export type SingleRequestTextStreamPart<
TOOLS extends Record<string, CoreTool>,
> =
export type SingleRequestTextStreamPart<TOOLS extends ToolSet> =
| {
type: 'text-delta';
textDelta: string;
Expand Down Expand Up @@ -66,7 +64,7 @@ export type SingleRequestTextStreamPart<
error: unknown;
};

export function runToolsTransformation<TOOLS extends Record<string, CoreTool>>({
export function runToolsTransformation<TOOLS extends ToolSet>({
tools,
generatorStream,
toolCallStreaming,
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/core/generate-text/smooth-stream.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { InvalidArgumentError } from '@ai-sdk/provider';
import { delay as originalDelay } from '@ai-sdk/provider-utils';
import { CoreTool } from '../tool/tool';
import { TextStreamPart } from './stream-text-result';
import { ToolSet } from './tool-set';

const CHUNKING_REGEXPS = {
word: /\s*\S+\s+/m,
Expand All @@ -16,7 +16,7 @@ const CHUNKING_REGEXPS = {
*
* @returns A transform stream that smooths text streaming output.
*/
export function smoothStream<TOOLS extends Record<string, CoreTool>>({
export function smoothStream<TOOLS extends ToolSet>({
delayInMs = 10,
chunking = 'word',
_internal: { delay = originalDelay } = {},
Expand Down
4 changes: 2 additions & 2 deletions packages/ai/core/generate-text/step-result.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { CoreAssistantMessage, CoreToolMessage } from '../prompt/message';
import { CoreTool } from '../tool';
import {
CallWarning,
FinishReason,
Expand All @@ -11,6 +10,7 @@ import {
import { LanguageModelUsage } from '../types/usage';
import { ToolCallArray } from './tool-call';
import { ToolResultArray } from './tool-result';
import { ToolSet } from './tool-set';

/**
A message that was generated during the generation process.
Expand All @@ -27,7 +27,7 @@ Message ID generated by the AI SDK.
/**
* The result of a single step in the generation process.
*/
export type StepResult<TOOLS extends Record<string, CoreTool>> = {
export type StepResult<TOOLS extends ToolSet> = {
/**
The generated text.
*/
Expand Down
9 changes: 3 additions & 6 deletions packages/ai/core/generate-text/stream-text-result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { ServerResponse } from 'node:http';
import { StreamData } from '../../streams/stream-data';
import { DataStreamWriter } from '../data-stream/data-stream-writer';
import { CoreAssistantMessage, CoreToolMessage } from '../prompt/message';
import { CoreTool } from '../tool';
import {
CallWarning,
FinishReason,
Expand All @@ -16,14 +15,12 @@ import { AsyncIterableStream } from '../util/async-iterable-stream';
import { StepResult } from './step-result';
import { ToolCallUnion } from './tool-call';
import { ToolResultUnion } from './tool-result';
import { ToolSet } from './tool-set';

/**
A result object for accessing different stream types and additional information.
*/
export interface StreamTextResult<
TOOLS extends Record<string, CoreTool>,
PARTIAL_OUTPUT,
> {
export interface StreamTextResult<TOOLS extends ToolSet, PARTIAL_OUTPUT> {
/**
Warnings from the model provider (e.g. unsupported settings) for the first step.
*/
Expand Down Expand Up @@ -224,7 +221,7 @@ A stream of partial outputs. It uses the `experimental_output` specification.
toTextStreamResponse(init?: ResponseInit): Response;
}

export type TextStreamPart<TOOLS extends Record<string, CoreTool>> =
export type TextStreamPart<TOOLS extends ToolSet> =
| {
type: 'text-delta';
textDelta: string;
Expand Down
Loading

0 comments on commit 3a602ca

Please sign in to comment.