|
1 | | -import type { LanguageModelV1 } from "@ai-sdk/provider"; |
| 1 | +import type { LanguageModelV1, LanguageModelV1FunctionToolCall } from "@ai-sdk/provider"; |
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * General AI run interface with overloads to handle distinct return types. |
@@ -182,33 +182,79 @@ export function lastMessageWasUser<T extends { role: string }>(messages: T[]) { |
182 | 182 | return messages.length > 0 && messages[messages.length - 1]!.role === "user"; |
183 | 183 | } |
184 | 184 |
|
185 | | -export function processToolCalls(output: any) { |
| 185 | +function mergePartialToolCalls(partialCalls: any[]) { |
| 186 | + const mergedCallsByIndex: any = {}; |
| 187 | + |
| 188 | + for (const partialCall of partialCalls) { |
| 189 | + const index = partialCall.index; |
| 190 | + |
| 191 | + if (!mergedCallsByIndex[index]) { |
| 192 | + mergedCallsByIndex[index] = { |
| 193 | + id: partialCall.id || "", |
| 194 | + type: partialCall.type || "", |
| 195 | + function: { |
| 196 | + name: partialCall.function?.name || "", |
| 197 | + arguments: "", |
| 198 | + }, |
| 199 | + }; |
| 200 | + } else { |
| 201 | + if (partialCall.id) { |
| 202 | + mergedCallsByIndex[index].id = partialCall.id; |
| 203 | + } |
| 204 | + if (partialCall.type) { |
| 205 | + mergedCallsByIndex[index].type = partialCall.type; |
| 206 | + } |
| 207 | + |
| 208 | + if (partialCall.function?.name) { |
| 209 | + mergedCallsByIndex[index].function.name = partialCall.function.name; |
| 210 | + } |
| 211 | + } |
| 212 | + |
| 213 | + // Append arguments if available, this assumes arguments come in the right order |
| 214 | + if (partialCall.function?.arguments) { |
| 215 | + mergedCallsByIndex[index].function.arguments += partialCall.function.arguments; |
| 216 | + } |
| 217 | + } |
| 218 | + |
| 219 | + return Object.values(mergedCallsByIndex); |
| 220 | +} |
| 221 | + |
| 222 | +function processToolCall(toolCall: any): LanguageModelV1FunctionToolCall { |
186 | 223 | // Check for OpenAI format tool calls first |
| 224 | + if (toolCall.function && toolCall.id) { |
| 225 | + return { |
| 226 | + toolCallType: "function", |
| 227 | + toolCallId: toolCall.id, |
| 228 | + toolName: toolCall.function.name, |
| 229 | + args: |
| 230 | + typeof toolCall.function.arguments === "string" |
| 231 | + ? toolCall.function.arguments |
| 232 | + : JSON.stringify(toolCall.function.arguments || {}), |
| 233 | + }; |
| 234 | + } |
| 235 | + return { |
| 236 | + toolCallType: "function", |
| 237 | + toolCallId: toolCall.name, |
| 238 | + toolName: toolCall.name, |
| 239 | + args: |
| 240 | + typeof toolCall.arguments === "string" |
| 241 | + ? toolCall.arguments |
| 242 | + : JSON.stringify(toolCall.arguments || {}), |
| 243 | + }; |
| 244 | +} |
| 245 | + |
| 246 | +export function processToolCalls(output: any): LanguageModelV1FunctionToolCall[] { |
187 | 247 | if (output.tool_calls && Array.isArray(output.tool_calls)) { |
188 | 248 | return output.tool_calls.map((toolCall: any) => { |
189 | | - // Handle new format |
190 | | - if (toolCall.function && toolCall.id) { |
191 | | - return { |
192 | | - toolCallType: "function", |
193 | | - toolCallId: toolCall.id, |
194 | | - toolName: toolCall.function.name, |
195 | | - args: |
196 | | - typeof toolCall.function.arguments === "string" |
197 | | - ? toolCall.function.arguments |
198 | | - : JSON.stringify(toolCall.function.arguments || {}), |
199 | | - }; |
200 | | - } |
201 | | - return { |
202 | | - toolCallType: "function", |
203 | | - toolCallId: toolCall.name, |
204 | | - toolName: toolCall.name, |
205 | | - args: |
206 | | - typeof toolCall.arguments === "string" |
207 | | - ? toolCall.arguments |
208 | | - : JSON.stringify(toolCall.arguments || {}), |
209 | | - }; |
| 249 | + const processedToolCall = processToolCall(toolCall); |
| 250 | + return processedToolCall; |
210 | 251 | }); |
211 | 252 | } |
212 | 253 |
|
213 | 254 | return []; |
214 | 255 | } |
| 256 | + |
| 257 | +export function processPartialToolCalls(partialToolCalls: any[]) { |
| 258 | + const mergedToolCalls = mergePartialToolCalls(partialToolCalls); |
| 259 | + return processToolCalls({ tool_calls: mergedToolCalls }); |
| 260 | +} |
0 commit comments