Skip to content

Commit fdd6f66

Browse files
committed
refactor(functions): rename function helper methods to include tools
1 parent f743730 commit fdd6f66

File tree

7 files changed

+46
-64
lines changed

7 files changed

+46
-64
lines changed

examples/tool-call-helpers-zod.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ async function main() {
7373
],
7474
})
7575
.on('message', (msg) => console.log('msg', msg))
76-
.on('functionCall', (functionCall) => console.log('functionCall', functionCall))
77-
.on('functionCallResult', (functionCallResult) => console.log('functionCallResult', functionCallResult))
76+
.on('finalFunctionToolCall', (functionCall) => console.log('functionCall', functionCall))
77+
.on('finalFunctionToolCallResult', (functionCallResult) =>
78+
console.log('functionCallResult', functionCallResult),
79+
)
7880
.on('content', (diff) => process.stdout.write(diff));
7981

8082
const result = await runner.finalChatCompletion();

examples/tool-call-helpers.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,10 @@ async function main() {
8282
],
8383
})
8484
.on('message', (msg) => console.log('msg', msg))
85-
.on('functionCall', (functionCall) => console.log('functionCall', functionCall))
86-
.on('functionCallResult', (functionCallResult) => console.log('functionCallResult', functionCallResult))
85+
.on('functionToolCall', (functionCall) => console.log('functionCall', functionCall))
86+
.on('functionToolCallResult', (functionCallResult) =>
87+
console.log('functionCallResult', functionCallResult),
88+
)
8789
.on('content', (diff) => process.stdout.write(diff));
8890

8991
const result = await runner.finalChatCompletion();

src/lib/AbstractChatCompletionRunner.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import {
1212
type RunnableFunction,
1313
isRunnableFunctionWithParse,
1414
type BaseFunctionsArgs,
15-
RunnableToolFunction,
15+
type RunnableToolFunction,
1616
} from './RunnableFunction';
1717
import type { ChatCompletionToolRunnerParams } from './ChatCompletionRunner';
1818
import type { ChatCompletionStreamingToolRunnerParams } from './ChatCompletionStreamingRunner';
@@ -60,11 +60,11 @@ export class AbstractChatCompletionRunner<
6060
this._emit('message', message);
6161
if (isToolMessage(message) && message.content) {
6262
// Note, this assumes that {role: 'tool', content: …} is always the result of a call of tool of type=function.
63-
this._emit('functionCallResult', message.content as string);
63+
this._emit('functionToolCallResult', message.content as string);
6464
} else if (isAssistantMessage(message) && message.tool_calls) {
6565
for (const tool_call of message.tool_calls) {
6666
if (tool_call.type === 'function') {
67-
this._emit('functionCall', tool_call.function);
67+
this._emit('functionToolCall', tool_call.function);
6868
}
6969
}
7070
}
@@ -121,7 +121,7 @@ export class AbstractChatCompletionRunner<
121121
return this.#getFinalMessage();
122122
}
123123

124-
#getFinalFunctionCall(): ChatCompletionMessageToolCall.Function | undefined {
124+
#getFinalFunctionToolCall(): ChatCompletionMessageToolCall.Function | undefined {
125125
for (let i = this.messages.length - 1; i >= 0; i--) {
126126
const message = this.messages[i];
127127
if (isAssistantMessage(message) && message?.tool_calls?.length) {
@@ -136,12 +136,12 @@ export class AbstractChatCompletionRunner<
136136
* @returns a promise that resolves with the content of the final FunctionCall, or rejects
137137
* if an error occurred or the stream ended prematurely without producing a ChatCompletionMessage.
138138
*/
139-
async finalFunctionCall(): Promise<ChatCompletionMessageToolCall.Function | undefined> {
139+
async finalFunctionToolCall(): Promise<ChatCompletionMessageToolCall.Function | undefined> {
140140
await this.done();
141-
return this.#getFinalFunctionCall();
141+
return this.#getFinalFunctionToolCall();
142142
}
143143

144-
#getFinalFunctionCallResult(): string | undefined {
144+
#getFinalFunctionToolCallResult(): string | undefined {
145145
for (let i = this.messages.length - 1; i >= 0; i--) {
146146
const message = this.messages[i];
147147
if (
@@ -161,9 +161,9 @@ export class AbstractChatCompletionRunner<
161161
return;
162162
}
163163

164-
async finalFunctionCallResult(): Promise<string | undefined> {
164+
async finalFunctionToolCallResult(): Promise<string | undefined> {
165165
await this.done();
166-
return this.#getFinalFunctionCallResult();
166+
return this.#getFinalFunctionToolCallResult();
167167
}
168168

169169
#calculateTotalUsage(): CompletionUsage {
@@ -201,11 +201,11 @@ export class AbstractChatCompletionRunner<
201201
const finalContent = this.#getFinalContent();
202202
if (finalContent) this._emit('finalContent', finalContent);
203203

204-
const finalFunctionCall = this.#getFinalFunctionCall();
205-
if (finalFunctionCall) this._emit('finalFunctionCall', finalFunctionCall);
204+
const finalFunctionCall = this.#getFinalFunctionToolCall();
205+
if (finalFunctionCall) this._emit('finalFunctionToolCall', finalFunctionCall);
206206

207-
const finalFunctionCallResult = this.#getFinalFunctionCallResult();
208-
if (finalFunctionCallResult != null) this._emit('finalFunctionCallResult', finalFunctionCallResult);
207+
const finalFunctionCallResult = this.#getFinalFunctionToolCallResult();
208+
if (finalFunctionCallResult != null) this._emit('finalFunctionToolCallResult', finalFunctionCallResult);
209209

210210
if (this._chatCompletions.some((c) => c.usage)) {
211211
this._emit('totalUsage', this.#calculateTotalUsage());
@@ -390,14 +390,14 @@ export class AbstractChatCompletionRunner<
390390
}
391391

392392
export interface AbstractChatCompletionRunnerEvents extends BaseEvents {
393-
functionCall: (functionCall: ChatCompletionMessageToolCall.Function) => void;
393+
functionToolCall: (functionCall: ChatCompletionMessageToolCall.Function) => void;
394394
message: (message: ChatCompletionMessageParam) => void;
395395
chatCompletion: (completion: ChatCompletion) => void;
396396
finalContent: (contentSnapshot: string) => void;
397397
finalMessage: (message: ChatCompletionMessageParam) => void;
398398
finalChatCompletion: (completion: ChatCompletion) => void;
399-
finalFunctionCall: (functionCall: ChatCompletionMessageToolCall.Function) => void;
400-
functionCallResult: (content: string) => void;
401-
finalFunctionCallResult: (content: string) => void;
399+
finalFunctionToolCall: (functionCall: ChatCompletionMessageToolCall.Function) => void;
400+
functionToolCallResult: (content: string) => void;
401+
finalFunctionToolCallResult: (content: string) => void;
402402
totalUsage: (usage: CompletionUsage) => void;
403403
}

src/lib/ChatCompletionRunner.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {
22
type ChatCompletionMessageParam,
33
type ChatCompletionCreateParamsNonStreaming,
44
} from '../resources/chat/completions';
5-
import { type RunnableFunctions, type BaseFunctionsArgs, RunnableTools } from './RunnableFunction';
5+
import { type BaseFunctionsArgs, RunnableTools } from './RunnableFunction';
66
import {
77
AbstractChatCompletionRunner,
88
AbstractChatCompletionRunnerEvents,
@@ -16,13 +16,6 @@ export interface ChatCompletionRunnerEvents extends AbstractChatCompletionRunner
1616
content: (content: string) => void;
1717
}
1818

19-
export type ChatCompletionFunctionRunnerParams<FunctionsArgs extends BaseFunctionsArgs> = Omit<
20-
ChatCompletionCreateParamsNonStreaming,
21-
'functions'
22-
> & {
23-
functions: RunnableFunctions<FunctionsArgs>;
24-
};
25-
2619
export type ChatCompletionToolRunnerParams<FunctionsArgs extends BaseFunctionsArgs> = Omit<
2720
ChatCompletionCreateParamsNonStreaming,
2821
'tools'

src/lib/ChatCompletionStreamingRunner.ts

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
} from '../resources/chat/completions';
55
import { RunnerOptions, type AbstractChatCompletionRunnerEvents } from './AbstractChatCompletionRunner';
66
import { type ReadableStream } from '../internal/shim-types';
7-
import { RunnableTools, type BaseFunctionsArgs, type RunnableFunctions } from './RunnableFunction';
7+
import { RunnableTools, type BaseFunctionsArgs } from './RunnableFunction';
88
import { ChatCompletionSnapshot, ChatCompletionStream } from './ChatCompletionStream';
99
import OpenAI from '../index';
1010
import { AutoParseableTool } from '../lib/parser';
@@ -14,13 +14,6 @@ export interface ChatCompletionStreamEvents extends AbstractChatCompletionRunner
1414
chunk: (chunk: ChatCompletionChunk, snapshot: ChatCompletionSnapshot) => void;
1515
}
1616

17-
export type ChatCompletionStreamingFunctionRunnerParams<FunctionsArgs extends BaseFunctionsArgs> = Omit<
18-
ChatCompletionCreateParamsStreaming,
19-
'functions'
20-
> & {
21-
functions: RunnableFunctions<FunctionsArgs>;
22-
};
23-
2417
export type ChatCompletionStreamingToolRunnerParams<FunctionsArgs extends BaseFunctionsArgs> = Omit<
2518
ChatCompletionCreateParamsStreaming,
2619
'tools'

src/resources/beta/chat/completions.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,16 @@ import {
1515
} from '../../chat/completions';
1616
import { ExtractParsedContentFromParams, parseChatCompletion, validateInputTools } from '../../../lib/parser';
1717

18+
export { ChatCompletionStreamingRunner } from '../../../lib/ChatCompletionStreamingRunner';
1819
export {
19-
ChatCompletionStreamingRunner,
20-
type ChatCompletionStreamingFunctionRunnerParams,
21-
} from '../../../lib/ChatCompletionStreamingRunner';
22-
export {
23-
type RunnableFunction,
24-
type RunnableFunctions,
2520
type RunnableFunctionWithParse,
2621
type RunnableFunctionWithoutParse,
2722
ParsingToolFunction,
2823
} from '../../../lib/RunnableFunction';
2924
export { type ChatCompletionToolRunnerParams } from '../../../lib/ChatCompletionRunner';
3025
export { type ChatCompletionStreamingToolRunnerParams } from '../../../lib/ChatCompletionStreamingRunner';
3126
export { ChatCompletionStream, type ChatCompletionStreamParams } from '../../../lib/ChatCompletionStream';
32-
export {
33-
ChatCompletionRunner,
34-
type ChatCompletionFunctionRunnerParams,
35-
} from '../../../lib/ChatCompletionRunner';
27+
export { ChatCompletionRunner } from '../../../lib/ChatCompletionRunner';
3628
import { RequestOptions } from '../../../internal/request-options';
3729
import { type APIPromise } from '../../../index';
3830

tests/lib/ChatCompletionRunFunctions.test.ts

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ class RunnerListener {
151151
.on('content', (content) => this.contents.push(content))
152152
.on('message', (message) => this.messages.push(message))
153153
.on('chatCompletion', (completion) => this.chatCompletions.push(completion))
154-
.on('functionCall', (functionCall) => this.functionCalls.push(functionCall))
155-
.on('functionCallResult', (result) => this.functionCallResults.push(result))
154+
.on('functionToolCall', (functionCall) => this.functionCalls.push(functionCall))
155+
.on('functionToolCallResult', (result) => this.functionCallResults.push(result))
156156
.on('finalContent', (content) => (this.finalContent = content))
157157
.on('finalMessage', (message) => (this.finalMessage = message))
158158
.on('finalChatCompletion', (completion) => (this.finalChatCompletion = completion))
159-
.on('finalFunctionCall', (functionCall) => (this.finalFunctionCall = functionCall))
160-
.on('finalFunctionCallResult', (result) => (this.finalFunctionCallResult = result))
159+
.on('finalFunctionToolCall', (functionCall) => (this.finalFunctionCall = functionCall))
160+
.on('finalFunctionToolCallResult', (result) => (this.finalFunctionCallResult = result))
161161
.on('totalUsage', (usage) => (this.totalUsage = usage))
162162
.on('error', (error) => (this.error = error))
163163
.on('abort', (error) => ((this.error = error), (this.gotAbort = true)))
@@ -175,8 +175,8 @@ class RunnerListener {
175175
await expect(this.runner.finalChatCompletion()).rejects.toThrow(error);
176176
await expect(this.runner.finalMessage()).rejects.toThrow(error);
177177
await expect(this.runner.finalContent()).rejects.toThrow(error);
178-
await expect(this.runner.finalFunctionCall()).rejects.toThrow(error);
179-
await expect(this.runner.finalFunctionCallResult()).rejects.toThrow(error);
178+
await expect(this.runner.finalFunctionToolCall()).rejects.toThrow(error);
179+
await expect(this.runner.finalFunctionToolCallResult()).rejects.toThrow(error);
180180
await expect(this.runner.totalUsage()).rejects.toThrow(error);
181181
await expect(this.runner.done()).rejects.toThrow(error);
182182
} else {
@@ -214,11 +214,11 @@ class RunnerListener {
214214
expect(this.finalChatCompletion).toEqual(this.chatCompletions[this.chatCompletions.length - 1]);
215215
expect(await this.runner.finalChatCompletion()).toEqual(this.finalChatCompletion);
216216
expect(this.finalFunctionCall).toEqual(this.functionCalls[this.functionCalls.length - 1]);
217-
expect(await this.runner.finalFunctionCall()).toEqual(this.finalFunctionCall);
217+
expect(await this.runner.finalFunctionToolCall()).toEqual(this.finalFunctionCall);
218218
expect(this.finalFunctionCallResult).toEqual(
219219
this.functionCallResults[this.functionCallResults.length - 1],
220220
);
221-
expect(await this.runner.finalFunctionCallResult()).toEqual(this.finalFunctionCallResult);
221+
expect(await this.runner.finalFunctionToolCallResult()).toEqual(this.finalFunctionCallResult);
222222
expect(this.chatCompletions).toEqual(this.runner.allChatCompletions());
223223
expect(this.messages).toEqual(this.runner.messages.slice(-this.messages.length));
224224
if (this.chatCompletions.some((c) => c.usage)) {
@@ -266,13 +266,13 @@ class StreamingRunnerListener {
266266
.on('content', (delta, snapshot) => this.eventContents.push([delta, snapshot]))
267267
.on('message', (message) => this.eventMessages.push(message))
268268
.on('chatCompletion', (completion) => this.eventChatCompletions.push(completion))
269-
.on('functionCall', (functionCall) => this.eventFunctionCalls.push(functionCall))
270-
.on('functionCallResult', (result) => this.eventFunctionCallResults.push(result))
269+
.on('functionToolCall', (functionCall) => this.eventFunctionCalls.push(functionCall))
270+
.on('functionToolCallResult', (result) => this.eventFunctionCallResults.push(result))
271271
.on('finalContent', (content) => (this.finalContent = content))
272272
.on('finalMessage', (message) => (this.finalMessage = message))
273273
.on('finalChatCompletion', (completion) => (this.finalChatCompletion = completion))
274-
.on('finalFunctionCall', (functionCall) => (this.finalFunctionCall = functionCall))
275-
.on('finalFunctionCallResult', (result) => (this.finalFunctionCallResult = result))
274+
.on('finalFunctionToolCall', (functionCall) => (this.finalFunctionCall = functionCall))
275+
.on('finalFunctionToolCallResult', (result) => (this.finalFunctionCallResult = result))
276276
.on('error', (error) => (this.error = error))
277277
.on('abort', (abort) => (this.error = abort))
278278
.on('end', () => (this.gotEnd = true));
@@ -285,8 +285,8 @@ class StreamingRunnerListener {
285285
await expect(this.runner.finalChatCompletion()).rejects.toThrow(error);
286286
await expect(this.runner.finalMessage()).rejects.toThrow(error);
287287
await expect(this.runner.finalContent()).rejects.toThrow(error);
288-
await expect(this.runner.finalFunctionCall()).rejects.toThrow(error);
289-
await expect(this.runner.finalFunctionCallResult()).rejects.toThrow(error);
288+
await expect(this.runner.finalFunctionToolCall()).rejects.toThrow(error);
289+
await expect(this.runner.finalFunctionToolCallResult()).rejects.toThrow(error);
290290
await expect(this.runner.done()).rejects.toThrow(error);
291291
} else {
292292
expect(this.error).toBeUndefined();
@@ -318,11 +318,11 @@ class StreamingRunnerListener {
318318
expect(this.finalChatCompletion).toEqual(this.eventChatCompletions[this.eventChatCompletions.length - 1]);
319319
expect(await this.runner.finalChatCompletion()).toEqual(this.finalChatCompletion);
320320
expect(this.finalFunctionCall).toEqual(this.eventFunctionCalls[this.eventFunctionCalls.length - 1]);
321-
expect(await this.runner.finalFunctionCall()).toEqual(this.finalFunctionCall);
321+
expect(await this.runner.finalFunctionToolCall()).toEqual(this.finalFunctionCall);
322322
expect(this.finalFunctionCallResult).toEqual(
323323
this.eventFunctionCallResults[this.eventFunctionCallResults.length - 1],
324324
);
325-
expect(await this.runner.finalFunctionCallResult()).toEqual(this.finalFunctionCallResult);
325+
expect(await this.runner.finalFunctionToolCallResult()).toEqual(this.finalFunctionCallResult);
326326
expect(this.eventChatCompletions).toEqual(this.runner.allChatCompletions());
327327
expect(this.eventMessages).toEqual(this.runner.messages.slice(-this.eventMessages.length));
328328
if (error) {
@@ -1603,7 +1603,7 @@ describe('resource completions', () => {
16031603
},
16041604
{ signal: controller.signal },
16051605
);
1606-
runner.on('functionCallResult', () => controller.abort());
1606+
runner.on('functionToolCallResult', () => controller.abort());
16071607
const listener = new StreamingRunnerListener(runner);
16081608

16091609
await handleRequest(async function* (request): AsyncIterable<OpenAI.Chat.ChatCompletionChunk> {

0 commit comments

Comments
 (0)