Skip to content

Commit 8c63e6a

Browse files
committed
include tool call ids
1 parent e8b9af6 commit 8c63e6a

11 files changed

Lines changed: 73 additions & 2 deletions

packages/internal/src/openai-compatible/chat/convert-to-openai-compatible-chat-messages.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ describe('tool calls', () => {
147147
{
148148
role: 'tool',
149149
content: JSON.stringify({ oof: '321rab' }),
150+
name: 'thwomp',
150151
tool_call_id: 'quux',
151152
},
152153
])
@@ -196,6 +197,7 @@ describe('tool calls', () => {
196197
{
197198
role: 'tool',
198199
content: 'It is sunny today',
200+
name: 'getWeather',
199201
tool_call_id: 'call-1',
200202
},
201203
])
@@ -541,11 +543,13 @@ describe('provider-specific metadata merging', () => {
541543
{
542544
role: 'tool',
543545
tool_call_id: 'call123',
546+
name: 'calculator',
544547
content: JSON.stringify({ stepOne: 'data chunk 1' }),
545548
},
546549
{
547550
role: 'tool',
548551
tool_call_id: 'call123',
552+
name: 'calculator',
549553
content: JSON.stringify({ stepTwo: 'data chunk 2' }),
550554
partial: true,
551555
},

packages/internal/src/openai-compatible/chat/convert-to-openai-compatible-chat-messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,7 @@ export function convertToOpenAICompatibleChatMessages(
124124
messages.push({
125125
role: 'tool',
126126
tool_call_id: toolResponse.toolCallId,
127+
name: toolResponse.toolName,
127128
content: contentValue,
128129
...toolResponseMetadata,
129130
})

packages/internal/src/openai-compatible/chat/openai-compatible-api-types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,5 +59,6 @@ export interface OpenAICompatibleMessageToolCall extends JsonRecord {
5959
export interface OpenAICompatibleToolMessage extends JsonRecord {
6060
role: 'tool';
6161
content: string;
62+
name?: string;
6263
tool_call_id: string;
6364
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { describe, expect, it } from 'bun:test'
2+
3+
import { prepareTools } from './openai-compatible-prepare-tools'
4+
5+
describe('prepareTools', () => {
6+
it('adds stable ids to function tool definitions', () => {
7+
const result = prepareTools({
8+
tools: [
9+
{
10+
type: 'function',
11+
name: 'read_files',
12+
description: 'Read files',
13+
inputSchema: { type: 'object' },
14+
},
15+
{
16+
type: 'function',
17+
name: 'write_todos',
18+
description: 'Write todos',
19+
inputSchema: { type: 'object' },
20+
},
21+
],
22+
})
23+
24+
expect(result.tools).toEqual([
25+
{
26+
id: 'tool_1',
27+
type: 'function',
28+
function: {
29+
name: 'read_files',
30+
description: 'Read files',
31+
parameters: { type: 'object' },
32+
},
33+
},
34+
{
35+
id: 'tool_2',
36+
type: 'function',
37+
function: {
38+
name: 'write_todos',
39+
description: 'Write todos',
40+
parameters: { type: 'object' },
41+
},
42+
},
43+
])
44+
})
45+
})

packages/internal/src/openai-compatible/chat/openai-compatible-prepare-tools.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export function prepareTools({
1616
tools:
1717
| undefined
1818
| Array<{
19+
id: string;
1920
type: 'function';
2021
function: {
2122
name: string;
@@ -41,6 +42,7 @@ export function prepareTools({
4142
}
4243

4344
const openaiCompatTools: Array<{
45+
id: string;
4446
type: 'function';
4547
function: {
4648
name: string;
@@ -49,11 +51,12 @@ export function prepareTools({
4951
};
5052
}> = [];
5153

52-
for (const tool of tools) {
54+
for (const [index, tool] of tools.entries()) {
5355
if (tool.type === 'provider-defined') {
5456
toolWarnings.push({ type: 'unsupported-tool', tool });
5557
} else {
5658
openaiCompatTools.push({
59+
id: `tool_${index + 1}`,
5760
type: 'function',
5861
function: {
5962
name: tool.name,

packages/internal/src/openrouter-ai-sdk/chat/convert-to-openrouter-chat-messages.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -481,6 +481,7 @@ describe('cache control', () => {
481481
{
482482
role: 'tool',
483483
tool_call_id: 'call-123',
484+
name: 'calculator',
484485
content: JSON.stringify({ answer: 42 }),
485486
cache_control: { type: 'ephemeral' },
486487
},

packages/internal/src/openrouter-ai-sdk/chat/convert-to-openrouter-chat-messages.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ export function convertToOpenRouterChatMessages(
198198
messages.push({
199199
role: 'tool',
200200
tool_call_id: toolResponse.toolCallId,
201+
name: toolResponse.toolName,
201202
content,
202203
cache_control:
203204
getCacheControl(providerOptions) ??

packages/internal/src/openrouter-ai-sdk/chat/index.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,7 @@ describe('doGenerate', () => {
629629
messages: [{ role: 'user', content: [{ type: 'text', text: 'Hello' }] }],
630630
tools: [
631631
{
632+
id: 'tool_1',
632633
type: 'function',
633634
function: {
634635
name: 'test-tool',

packages/internal/src/openrouter-ai-sdk/chat/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,8 @@ export class OpenRouterChatLanguageModel implements LanguageModelV2 {
166166
// TODO: support built-in tools
167167
const mappedTools = tools
168168
.filter((tool) => tool.type === 'function')
169-
.map((tool) => ({
169+
.map((tool, index) => ({
170+
id: `tool_${index + 1}`,
170171
type: 'function' as const,
171172
function: {
172173
name: tool.name,

packages/internal/src/openrouter-ai-sdk/types/openrouter-chat-completions-input.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ export interface ChatCompletionMessageToolCall {
7373
export interface ChatCompletionToolMessageParam {
7474
role: 'tool'
7575
content: string
76+
name?: string
7677
tool_call_id: string
7778
cache_control?: OpenRouterCacheControl
7879
}

0 commit comments

Comments
 (0)