mirror of https://github.com/kortix-ai/suna.git
edge case: handle json file
This commit is contained in:
parent
03298b9616
commit
30e22f04ad
|
@ -4,6 +4,7 @@ from utils.files_utils import should_exclude_file, clean_path
|
||||||
from agentpress.thread_manager import ThreadManager
|
from agentpress.thread_manager import ThreadManager
|
||||||
from utils.logger import logger
|
from utils.logger import logger
|
||||||
import os
|
import os
|
||||||
|
import json
|
||||||
|
|
||||||
class SandboxFilesTool(SandboxToolsBase):
|
class SandboxFilesTool(SandboxToolsBase):
|
||||||
"""Tool for executing file system operations in a Daytona sandbox. All operations are performed relative to the /workspace directory."""
|
"""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:
|
if parent_dir:
|
||||||
await self.sandbox.fs.create_folder(parent_dir, "755")
|
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
|
# Write the file content
|
||||||
await self.sandbox.fs.upload_file(file_contents.encode(), full_path)
|
await self.sandbox.fs.upload_file(file_contents.encode(), full_path)
|
||||||
await self.sandbox.fs.set_file_permissions(full_path, permissions)
|
await self.sandbox.fs.set_file_permissions(full_path, permissions)
|
||||||
|
|
|
@ -783,7 +783,14 @@ export function FileViewerModal({
|
||||||
console.log(`[FILE VIEWER] Created blob URL: ${url} for ${selectedFilePath}`);
|
console.log(`[FILE VIEWER] Created blob URL: ${url} for ${selectedFilePath}`);
|
||||||
setBlobUrlForRenderer(url);
|
setBlobUrlForRenderer(url);
|
||||||
setTextContentForRenderer(null);
|
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
|
// Unknown content type
|
||||||
console.warn(`[FILE VIEWER] Unknown content type for: ${selectedFilePath}`, typeof cachedFileContent);
|
console.warn(`[FILE VIEWER] Unknown content type for: ${selectedFilePath}`, typeof cachedFileContent);
|
||||||
setTextContentForRenderer(null);
|
setTextContentForRenderer(null);
|
||||||
|
|
|
@ -507,8 +507,12 @@ export function extractFileContent(
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function processFileContent(content: string): string {
|
function processFileContent(content: string | object): string {
|
||||||
if (!content) return content;
|
if (!content) return '';
|
||||||
|
if (typeof content === 'object') {
|
||||||
|
return JSON.stringify(content, null, 2);
|
||||||
|
}
|
||||||
|
|
||||||
const trimmedContent = content.trim();
|
const trimmedContent = content.trim();
|
||||||
const isLikelyJson = (trimmedContent.startsWith('{') && trimmedContent.endsWith('}')) ||
|
const isLikelyJson = (trimmedContent.startsWith('{') && trimmedContent.endsWith('}')) ||
|
||||||
(trimmedContent.startsWith('[') && trimmedContent.endsWith(']'));
|
(trimmedContent.startsWith('[') && trimmedContent.endsWith(']'));
|
||||||
|
|
Loading…
Reference in New Issue