DEV Community

Ramu Narasinga
Ramu Narasinga

Posted on • Originally published at thinkthroo.com

safeJsonParse utility in Suna, an open source generalist AI agent.

In this article, we review a utility function, safeJsonParse in Suna codebase. Suna is an open source generalist AI agent. We will look at:

  1. Where is this function, safeJsonParse, called?

  2. What is its purpose?

  3. safeJsonParse declaration.

Where is this function, safeJsonParse, called?

This safeJsonParse function is called at line 754 in [threadId]/page.tsx.

if (resultMessage) { // Determine tool name from assistant message content let toolName = 'unknown'; try { // Try to extract tool name from content const xmlMatch = assistantMsg.content.match(/<([a-zA-Z\-_]+)(?:\s+[^>]*)?>|<([a-zA-Z\-_]+)(?:\s+[^>]*)?\/>/); if (xmlMatch) { toolName = xmlMatch[1] || xmlMatch[2] || 'unknown'; } else { // Fallback to checking for tool_calls JSON structure const assistantContentParsed = safeJsonParse<{ tool_calls?: { name: string }[] }>(assistantMsg.content, {}); if (assistantContentParsed.tool_calls && assistantContentParsed.tool_calls.length > 0) { toolName = assistantContentParsed.tool_calls[0].name || 'unknown'; } } } catch {} 
Enter fullscreen mode Exit fullscreen mode

What is safeJsonParse util purpose?

The comment just above this function call in [threadId]/page.tsx explains its purpose.

// Fallback to checking for tool_calls JSON structure const assistantContentParsed = safeJsonParse<{ tool_calls?: { name: string }[] }>(assistantMsg.content, {}); if (assistantContentParsed.tool_calls && assistantContentParsed.tool_calls.length > 0) { toolName = assistantContentParsed.tool_calls[0].name || 'unknown'; } 
Enter fullscreen mode Exit fullscreen mode

safeJsonParse declaration

// Helper function to safely parse JSON strings from content/metadata export function safeJsonParse<T>(jsonString: string | undefined | null, fallback: T): T { if (!jsonString) { return fallback; } try { return JSON.parse(jsonString); } catch (e) { // console.warn('Failed to parse JSON string:', jsonString, e); // Optional: log errors return fallback; } } 
Enter fullscreen mode Exit fullscreen mode

This above code snippet is picked from components/thread/utils.ts.

All it does is simply parse the jsonString and in case this parsing fails, return a fallback which is a parameter of safeJsonParse function. Interestingly, console.warn is commented.

About me:

Hey, my name is Ramu Narasinga. I study large open-source projects and create content about their codebase architecture and best practices, sharing it through articles, videos.

Want me to review your codebase architecture? book a meeting —https://thinkthroo.com/consultation/codebase-architecture-review

Business enquiries — ramu@thinkthroo.com

My Github — https://github.com/ramu-narasinga

My website — https://ramunarasinga.com

My YouTube channel — https://www.youtube.com/@ramu-narasinga

Learning platform — https://thinkthroo.com

Codebase Architecture — https://app.thinkthroo.com/architecture

Best practices — https://app.thinkthroo.com/best-practices

Production-grade projects — https://app.thinkthroo.com/production-grade-projects

References:

  1. https://github.com/kortix-ai/suna/blob/main/frontend/src/app/(dashboard)/agents/%5BthreadId%5D/page.tsx#L1427

  2. https://github.com/kortix-ai/suna/blob/main/frontend/src/components/thread/utils.ts#L12

Top comments (0)