Skip to content

Commit d75a202

Browse files
committed
rename BetaRunnableTool
1 parent 9b0b3df commit d75a202

File tree

5 files changed

+21
-19
lines changed

5 files changed

+21
-19
lines changed

src/helpers/beta/json-schema.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { FromSchema, JSONSchema } from 'json-schema-to-ts';
2-
import type { BetaRunnableTool, Promisable } from '../../lib/beta/BetaRunnableTool';
2+
import type { BetaRunnableChatCompletionFunctionTool, Promisable } from '../../lib/beta/BetaRunnableTool';
33
import type { FunctionTool } from '../../resources/beta';
44
import { OpenAIError } from '../../error';
55

@@ -15,7 +15,7 @@ export function betaTool<const Schema extends Exclude<JSONSchema, boolean> & { t
1515
inputSchema: Schema;
1616
description: string;
1717
run: (args: NoInfer<FromSchema<Schema>>) => Promisable<string | Array<FunctionTool>>;
18-
}): BetaRunnableTool<NoInfer<FromSchema<Schema>>> {
18+
}): BetaRunnableChatCompletionFunctionTool<NoInfer<FromSchema<Schema>>> {
1919
if (options.inputSchema.type !== 'object') {
2020
throw new OpenAIError(
2121
`JSON schema for tool "${options.name}" must be an object, but got ${options.inputSchema.type}`,

src/helpers/beta/zod.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { infer as zodInfer, ZodType } from 'zod/v4';
22
import * as z from 'zod/v4';
3-
import type { BetaRunnableTool, Promisable } from '../../lib/beta/BetaRunnableTool';
3+
import type { BetaRunnableChatCompletionFunctionTool, Promisable } from '../../lib/beta/BetaRunnableTool';
44
import type { ChatCompletionContentPart } from '../../resources';
55

66
/**
@@ -14,7 +14,7 @@ export function betaZodFunctionTool<InputSchema extends ZodType>(options: {
1414
parameters: InputSchema;
1515
description: string;
1616
run: (args: zodInfer<InputSchema>) => Promisable<string | ChatCompletionContentPart[]>;
17-
}): BetaRunnableTool<zodInfer<InputSchema>> {
17+
}): BetaRunnableChatCompletionFunctionTool<zodInfer<InputSchema>> {
1818
const jsonSchema = z.toJSONSchema(options.parameters, { reused: 'ref' });
1919

2020
return {

src/lib/beta/BetaRunnableTool.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import type { ChatCompletionContentPart, ChatCompletionTool } from '../../resources';
1+
import type { ChatCompletionContentPart, ChatCompletionFunctionTool } from '../../resources';
22

33
export type Promisable<T> = T | Promise<T>;
44

5-
// this type is just an extension of BetaTool with a run and parse method
5+
// this type is just an extension of ChatCompletionFunctionTool with a run and parse method
66
// that will be called by `toolRunner()` helpers
7-
export type BetaRunnableTool<Input = any> = ChatCompletionTool & {
7+
export type BetaRunnableChatCompletionFunctionTool<Input = any> = ChatCompletionFunctionTool & {
88
run: (args: Input) => Promisable<string | ChatCompletionContentPart[]>;
99
parse: (content: unknown) => Input;
1010
};

src/lib/beta/BetaToolRunner.ts

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import type {
1111
ChatCompletionTool,
1212
ChatCompletionToolMessageParam,
1313
} from '../../resources/chat/completions';
14-
import type { BetaRunnableTool } from './BetaRunnableTool';
14+
import type { BetaRunnableChatCompletionFunctionTool } from './BetaRunnableTool';
1515

1616
/**
1717
* Just Promise.withResolvers(), which is not available in all environments.
@@ -255,7 +255,10 @@ export class BetaToolRunner<Stream extends boolean>
255255
}
256256
const toolsResponse = generateToolResponse(
257257
lastMessage,
258-
this.#state.params.tools.filter((tool): tool is BetaRunnableTool<any> => 'run' in tool),
258+
this.#state.params.tools.filter(
259+
(tool): tool is BetaRunnableChatCompletionFunctionTool<any> =>
260+
'run' in tool && tool.type === 'function',
261+
),
259262
);
260263
this.#toolResponse = toolsResponse;
261264
return toolsResponse;
@@ -358,7 +361,7 @@ export class BetaToolRunner<Stream extends boolean>
358361

359362
async function generateToolResponse(
360363
lastMessage: ChatCompletionMessage,
361-
tools: BetaRunnableTool<any>[],
364+
tools: BetaRunnableChatCompletionFunctionTool<any>[],
362365
): Promise<null | ChatCompletionToolMessageParam[]> {
363366
// Only process if the last message is from the assistant and has tool use blocks
364367
if (
@@ -383,7 +386,7 @@ async function generateToolResponse(
383386

384387
const tool = tools.find(
385388
(t) => t.type === 'function' && toolUse.function.name === t.function.name,
386-
) as BetaRunnableTool;
389+
) as BetaRunnableChatCompletionFunctionTool;
387390

388391
if (!tool || !('run' in tool)) {
389392
return {
@@ -435,7 +438,7 @@ type Simplify<T> = { [KeyType in keyof T]: T[KeyType] } & {};
435438
*/
436439
export type BetaToolRunnerParams = Simplify<
437440
Omit<ChatCompletionCreateParams, 'tools'> & {
438-
tools: (ChatCompletionTool | BetaRunnableTool<any>)[];
441+
tools: (ChatCompletionTool | BetaRunnableChatCompletionFunctionTool<any>)[];
439442
/**
440443
* Maximum number of iterations (API requests) to make in the tool execution loop.
441444
* Each iteration consists of: assistant response → tool execution → tool results.

tests/lib/tools/BetaToolRunner.test.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import OpenAI from 'openai';
22
import { mockFetch } from '../../utils/mock-fetch';
3-
import type { BetaRunnableTool } from 'openai/lib/beta/BetaRunnableTool';
3+
import type { BetaRunnableChatCompletionFunctionTool } from 'openai/lib/beta/BetaRunnableTool';
44
import type {
55
ChatCompletion,
66
ChatCompletionChunk,
@@ -11,9 +11,8 @@ import type {
1111
} from 'openai/resources';
1212
import type { Fetch } from 'openai/internal/builtin-types';
1313
import type { BetaToolRunnerParams } from 'openai/lib/beta/BetaToolRunner';
14-
import { betaZodFunctionTool } from 'openai/helpers/beta/zod';
1514

16-
const weatherTool: BetaRunnableTool<{ location: string }> = {
15+
const weatherTool: BetaRunnableChatCompletionFunctionTool<{ location: string }> = {
1716
type: 'function',
1817
function: {
1918
name: 'getWeather',
@@ -29,7 +28,7 @@ const weatherTool: BetaRunnableTool<{ location: string }> = {
2928
parse: (input: unknown) => input as { location: string },
3029
};
3130

32-
const calculatorTool: BetaRunnableTool<{ a: number; b: number; operation: string }> = {
31+
const calculatorTool: BetaRunnableChatCompletionFunctionTool<{ a: number; b: number; operation: string }> = {
3332
type: 'function',
3433
function: {
3534
name: 'calculate',
@@ -766,7 +765,7 @@ describe('ToolRunner', () => {
766765
});
767766

768767
it('handles tool execution errors', async () => {
769-
const errorTool: BetaRunnableTool<{ shouldFail: boolean }> = {
768+
const errorTool: BetaRunnableChatCompletionFunctionTool<{ shouldFail: boolean }> = {
770769
type: 'function',
771770
function: {
772771
name: 'errorTool',
@@ -1010,7 +1009,7 @@ describe('ToolRunner', () => {
10101009
});
10111010

10121011
it('allows you to use non-string returning custom tools', async () => {
1013-
const customTool: BetaRunnableTool<{ location: string }> = {
1012+
const customTool: BetaRunnableChatCompletionFunctionTool<{ location: string }> = {
10141013
type: 'function',
10151014
function: {
10161015
name: 'getWeather',
@@ -1140,7 +1139,7 @@ describe('ToolRunner', () => {
11401139

11411140
it('calls tools at most once', async () => {
11421141
let weatherToolCallCount = 0;
1143-
const trackingWeatherTool: BetaRunnableTool<{ location: string }> = {
1142+
const trackingWeatherTool: BetaRunnableChatCompletionFunctionTool<{ location: string }> = {
11441143
type: 'function',
11451144
function: {
11461145
name: 'getWeather',

0 commit comments

Comments
 (0)