edge case: handle json file

This commit is contained in:
Krishav Raj Singh 2025-07-08 22:30:47 +05:30
parent 03298b9616
commit 30e22f04ad
3 changed files with 19 additions and 3 deletions

View File

@ -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)

View File

@ -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);

View File

@ -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(']'));