Skip to content
Merged
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
44 changes: 43 additions & 1 deletion src/lib/AbstractChatCompletionRunner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { uuid4 } from '../internal/utils/uuid';
import { isAutoParsableTool, parseChatCompletion } from '../lib/parser';
import type {
ChatCompletion,
ChatCompletionAssistantMessageParam,
ChatCompletionCreateParams,
ChatCompletionMessage,
ChatCompletionMessageFunctionToolCall,
Expand Down Expand Up @@ -49,6 +50,47 @@ function normalizeToolCallIds(chatCompletion: ChatCompletion): void {
}
}

/**
* Parsed completions contain response-only and helper-only fields. Keep those
* on runner.messages for callers, but only replay valid request fields.
*/
function toRequestMessage(message: ChatCompletionMessageParam): ChatCompletionMessageParam {
if (!isAssistantMessage(message)) return message;

const requestMessage: ChatCompletionAssistantMessageParam = { role: 'assistant' };

if (message.audio != null) requestMessage.audio = { id: message.audio.id };
if (message.content !== undefined) requestMessage.content = message.content;
if (message.function_call != null) requestMessage.function_call = message.function_call;
if (message.name !== undefined) requestMessage.name = message.name;
if (message.refusal != null) requestMessage.refusal = message.refusal;
if (message.tool_calls !== undefined) {
requestMessage.tool_calls = message.tool_calls.map((toolCall) => {
if (toolCall.type === 'custom') {
return {
id: toolCall.id,
type: toolCall.type,
custom: {
input: toolCall.custom.input,
name: toolCall.custom.name,
},
};
}

return {
id: toolCall.id,
type: toolCall.type,
function: {
arguments: toolCall.function.arguments,
name: toolCall.function.name,
},
};
});
}

return requestMessage;
}

export interface ChatCompletionRunnerContext {
messages: ChatCompletionMessageParam[];
abort(): void;
Expand Down Expand Up @@ -413,7 +455,7 @@ export class AbstractChatCompletionRunner<
...restParams,
tool_choice,
tools,
messages: [...this.messages],
messages: this.messages.map(toRequestMessage),
},
options,
);
Expand Down
43 changes: 1 addition & 42 deletions tests/lib/ChatCompletionRunFunctions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ describe('resource completions', () => {
});

await handleRequest(async (request) => {
// Parsed response metadata stays on the runner but must not be sent back to the API.
expect(request.messages).toEqual([
{
role: 'user',
Expand All @@ -652,16 +653,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
refusal: null,
parsed: null,
tool_calls: [
{
type: 'function',
id: '123',
function: {
arguments: '',
name: 'getWeather',
parsed_arguments: null,
},
},
],
Expand Down Expand Up @@ -1281,16 +1279,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '123',
function: {
arguments: '{"a": 1, "b": 2, "c": 3}',
name: 'numProperties',
parsed_arguments: null,
},
},
],
Expand Down Expand Up @@ -1436,16 +1431,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '123',
function: {
arguments: '[{"a": 1, "b": 2, "c": 3}]',
name: 'numProperties',
parsed_arguments: null,
},
},
],
Expand Down Expand Up @@ -1495,16 +1487,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '123',
function: {
arguments: '[{"a": 1, "b": 2, "c": 3}]',
name: 'numProperties',
parsed_arguments: null,
},
},
],
Expand All @@ -1517,16 +1506,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '1234',
function: {
arguments: '{"a": 1, "b": 2, "c": 3}',
name: 'numProperties',
parsed_arguments: null,
},
},
],
Expand Down Expand Up @@ -1757,16 +1743,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '123',
function: {
arguments: '',
name: 'get_weather',
parsed_arguments: null,
},
},
],
Expand Down Expand Up @@ -1813,16 +1796,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '123',
function: {
arguments: '',
name: 'get_weather',
parsed_arguments: null,
},
},
],
Expand All @@ -1835,16 +1815,13 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
id: '1234',
function: {
arguments: '',
name: 'getWeather',
parsed_arguments: null,
},
},
],
Expand Down Expand Up @@ -2006,8 +1983,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down Expand Up @@ -2133,8 +2108,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down Expand Up @@ -2697,8 +2670,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down Expand Up @@ -2820,8 +2791,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down Expand Up @@ -2861,8 +2830,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand All @@ -2882,8 +2849,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down Expand Up @@ -3107,8 +3072,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down Expand Up @@ -3161,8 +3124,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand All @@ -3182,8 +3143,6 @@ describe('resource completions', () => {
{
role: 'assistant',
content: null,
parsed: null,
refusal: null,
tool_calls: [
{
type: 'function',
Expand Down
Loading