Skip to content

Commit d700c19

Browse files
committed
resolved copilot comments
1 parent 14ae8b4 commit d700c19

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

apps/opik-frontend/src/lib/prettifyMessage.ts

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
ConversationData,
88
convertLLMMessagesToMarkdown,
99
LLMMessage,
10+
ToolCall,
1011
} from "./conversationMarkdown";
1112
import { PrettifyMessageResponse, ExtractTextResult } from "./types";
1213

@@ -262,7 +263,7 @@ const extractLLMMessagesArray = (obj: object): LLMMessage[] | null => {
262263
role: msgRecord.role as string,
263264
type: msgRecord.type as string,
264265
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,
266267
tool_call_id: msgRecord.tool_call_id as string,
267268
} as LLMMessage;
268269
});
@@ -293,3 +294,26 @@ const extractLLMMessagesArray = (obj: object): LLMMessage[] | null => {
293294

294295
return null;
295296
};
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

Comments
 (0)