Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,43 @@ describe('OpenAICompatibleChatLanguageModel doStream', () => {
expect(finishPartOf(parts).finishReason).toBe('tool-calls')
})

it('accepts functionless continuations of an in-flight tool call', async () => {
// The Chat Completions wire type makes function optional on each delta.
for (const ref of [{ index: 0 }, { id: 'call_1' }, {}]) {
const parts = await streamParts(
sseResponse([
chunk({
tool_calls: [
{
...ref,
id: 'call_1',
function: { name: 'read_files', arguments: '{"paths":' },
},
],
}),
chunk({ tool_calls: [ref] }),
chunk({ tool_calls: [{ ...ref, function: null }] }),
chunk({
tool_calls: [{ ...ref, function: { arguments: '["a.ts"]}' } }],
}),
chunk({}, { finish_reason: 'tool_calls' }),
'[DONE]',
]),
)

expect(parts.some((part) => part.type === 'error')).toBe(false)
expect(parts.filter((part) => part.type === 'tool-call')).toEqual([
{
type: 'tool-call',
toolCallId: 'call_1',
toolName: 'read_files',
input: '{"paths":["a.ts"]}',
},
])
expect(finishPartOf(parts).finishReason).toBe('tool-calls')
}
})

it('keeps honoring an explicit index', async () => {
const parts = await streamParts(
sseResponse([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -677,7 +677,7 @@ export class OpenAICompatibleChatLanguageModel implements LanguageModelV2 {
controller.enqueue({
type: 'tool-input-delta',
id: toolCall.id,
delta: toolCallDelta.function.arguments ?? '',
delta: toolCallDelta.function?.arguments ?? '',
})

// check if tool call is complete
Expand Down Expand Up @@ -841,7 +841,7 @@ export function inferToolCallIndex(
| { id: string; function: { name: string }; hasFinished: boolean }
| undefined
>,
delta: { id?: string | null; function: { name?: string | null } },
delta: { id?: string | null; function?: { name?: string | null } | null },
): number {
if (delta.id != null) {
const existing = toolCalls.findIndex((call) => call?.id === delta.id)
Expand All @@ -852,10 +852,11 @@ export function inferToolCallIndex(
if (
last &&
!last.hasFinished &&
(delta.function.name == null || delta.function.name === last.function.name)
(delta.function?.name == null ||
delta.function?.name === last.function.name)
)
return lastIndex
return delta.function.name == null && last ? lastIndex : toolCalls.length
return delta.function?.name == null && last ? lastIndex : toolCalls.length
}

const openaiCompatibleTokenUsageSchema = z
Expand Down Expand Up @@ -957,10 +958,12 @@ const createOpenAICompatibleChatChunkSchema = <
// the whole chunk, so the tool call was silently lost.
index: z.number().nullish(),
id: z.string().nullish(),
function: z.object({
name: z.string().nullish(),
arguments: z.string().nullish(),
}),
function: z
.object({
name: z.string().nullish(),
arguments: z.string().nullish(),
})
.nullish(),
extra_content: z.unknown().nullish(),
}),
)
Expand Down
Loading