diff --git a/backend/agent/tools/sb_files_tool.py b/backend/agent/tools/sb_files_tool.py index d8813d7e..b131b46b 100644 --- a/backend/agent/tools/sb_files_tool.py +++ b/backend/agent/tools/sb_files_tool.py @@ -4,6 +4,7 @@ from utils.files_utils import should_exclude_file, clean_path from agentpress.thread_manager import ThreadManager from utils.logger import logger import os +import json class SandboxFilesTool(SandboxToolsBase): """Tool for executing file system operations in a Daytona sandbox. All operations are performed relative to the /workspace directory.""" @@ -134,6 +135,10 @@ class SandboxFilesTool(SandboxToolsBase): if parent_dir: await self.sandbox.fs.create_folder(parent_dir, "755") + # convert to json string if file_contents is a dict + if isinstance(file_contents, dict): + file_contents = json.dumps(file_contents, indent=4) + # Write the file content await self.sandbox.fs.upload_file(file_contents.encode(), full_path) await self.sandbox.fs.set_file_permissions(full_path, permissions) diff --git a/frontend/src/components/thread/file-viewer-modal.tsx b/frontend/src/components/thread/file-viewer-modal.tsx index 5e81d001..b19fa8a1 100644 --- a/frontend/src/components/thread/file-viewer-modal.tsx +++ b/frontend/src/components/thread/file-viewer-modal.tsx @@ -783,7 +783,14 @@ export function FileViewerModal({ console.log(`[FILE VIEWER] Created blob URL: ${url} for ${selectedFilePath}`); setBlobUrlForRenderer(url); setTextContentForRenderer(null); - } else { + } else if (typeof cachedFileContent === 'object') { + // convert to json string if file_contents is a object + const jsonString = JSON.stringify(cachedFileContent, null, 2); + console.log(`[FILE VIEWER] Setting text content for object file: ${selectedFilePath}`); + setTextContentForRenderer(jsonString); + setBlobUrlForRenderer(null); + } + else { // Unknown content type console.warn(`[FILE VIEWER] Unknown content type for: ${selectedFilePath}`, typeof cachedFileContent); setTextContentForRenderer(null); diff --git a/frontend/src/components/thread/tool-views/utils.ts b/frontend/src/components/thread/tool-views/utils.ts index 72647419..2c489b31 100644 --- a/frontend/src/components/thread/tool-views/utils.ts +++ b/frontend/src/components/thread/tool-views/utils.ts @@ -507,8 +507,12 @@ export function extractFileContent( return null; } -function processFileContent(content: string): string { - if (!content) return content; +function processFileContent(content: string | object): string { + if (!content) return ''; + if (typeof content === 'object') { + return JSON.stringify(content, null, 2); + } + const trimmedContent = content.trim(); const isLikelyJson = (trimmedContent.startsWith('{') && trimmedContent.endsWith('}')) || (trimmedContent.startsWith('[') && trimmedContent.endsWith(']'));