( body: ChatCompletionRequestBody, )
| 29 | * names on tool-result messages. |
| 30 | */ |
| 31 | export function addKimiToolCompatibilityFields( |
| 32 | body: ChatCompletionRequestBody, |
| 33 | ): ChatCompletionRequestBody { |
| 34 | const namesByToolCallId = getToolCallNamesById(body.messages) |
| 35 | |
| 36 | return { |
| 37 | ...body, |
| 38 | tools: body.tools?.map((tool, index) => { |
| 39 | if (tool.type !== 'function' || tool.id) { |
| 40 | return tool |
| 41 | } |
| 42 | return { |
| 43 | ...tool, |
| 44 | id: `tool_${index + 1}`, |
| 45 | } |
| 46 | }), |
| 47 | messages: body.messages.map((message) => { |
| 48 | if ( |
| 49 | message.role !== 'tool' || |
| 50 | message.name || |
| 51 | typeof message.tool_call_id !== 'string' |
| 52 | ) { |
| 53 | return message |
| 54 | } |
| 55 | |
| 56 | const name = namesByToolCallId.get(message.tool_call_id) |
| 57 | if (!name) { |
| 58 | return message |
| 59 | } |
| 60 | |
| 61 | return { |
| 62 | ...message, |
| 63 | name, |
| 64 | } |
| 65 | }), |
| 66 | } |
| 67 | } |
no test coverage detected