|
7 | 7 | ConversationData, |
8 | 8 | convertLLMMessagesToMarkdown, |
9 | 9 | LLMMessage, |
| 10 | + ToolCall, |
10 | 11 | } from "./conversationMarkdown"; |
11 | 12 | import { PrettifyMessageResponse, ExtractTextResult } from "./types"; |
12 | 13 |
|
@@ -262,7 +263,7 @@ const extractLLMMessagesArray = (obj: object): LLMMessage[] | null => { |
262 | 263 | role: msgRecord.role as string, |
263 | 264 | type: msgRecord.type as string, |
264 | 265 | content: msgRecord.content as string | unknown[], |
265 | | - tool_calls: msgRecord.tool_calls as any, |
| 266 | + tool_calls: isValidToolCallsArray(msgRecord.tool_calls) ? msgRecord.tool_calls : undefined, |
266 | 267 | tool_call_id: msgRecord.tool_call_id as string, |
267 | 268 | } as LLMMessage; |
268 | 269 | }); |
@@ -293,3 +294,26 @@ const extractLLMMessagesArray = (obj: object): LLMMessage[] | null => { |
293 | 294 |
|
294 | 295 | return null; |
295 | 296 | }; |
| 297 | + |
| 298 | +/** |
| 299 | + * Type guard to check if a value is a valid ToolCall array |
| 300 | + * @param value - The value to check |
| 301 | + * @returns True if value is a valid ToolCall array |
| 302 | + */ |
| 303 | +const isValidToolCallsArray = (value: unknown): value is ToolCall[] => { |
| 304 | + if (!Array.isArray(value)) return false; |
| 305 | + |
| 306 | + return value.every((item: unknown) => { |
| 307 | + if (typeof item !== "object" || item === null) return false; |
| 308 | + |
| 309 | + const toolCall = item as Record<string, unknown>; |
| 310 | + return ( |
| 311 | + typeof toolCall.id === "string" && |
| 312 | + toolCall.type === "function" && |
| 313 | + typeof toolCall.function === "object" && |
| 314 | + toolCall.function !== null && |
| 315 | + typeof (toolCall.function as Record<string, unknown>).name === "string" && |
| 316 | + typeof (toolCall.function as Record<string, unknown>).arguments === "string" |
| 317 | + ); |
| 318 | + }); |
| 319 | +}; |
0 commit comments