Decode code text output files from b64 to str to ease client processing

This commit is contained in:
Debanjum 2024-11-08 17:19:42 -08:00
parent 7b39f2014a
commit af0215765c
3 changed files with 15 additions and 2 deletions

View file

@ -203,6 +203,10 @@ export function renderCodeGenImageInline(message: string, codeContext: CodeConte
if (file.filename.match(/\.(png|jpg|jpeg|gif|webp)$/i)) { if (file.filename.match(/\.(png|jpg|jpeg|gif|webp)$/i)) {
const replacement = `![${file.filename}](data:image/${file.filename.split(".").pop()};base64,${file.b64_data})`; const replacement = `![${file.filename}](data:image/${file.filename.split(".").pop()};base64,${file.b64_data})`;
message = message.replace(regex, replacement); message = message.replace(regex, replacement);
} else if (file.filename.match(/\.(txt|org|md)$/i)) {
// render output files generated by codegen as downloadable links
const replacement = `![${file.filename}](data:text/plain;base64,${file.b64_data})`;
message = message.replace(regex, replacement);
} }
}); });
}); });

View file

@ -420,6 +420,7 @@ const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>((props, ref) =>
message += `\n\n${inferredQueries[0]}`; message += `\n\n${inferredQueries[0]}`;
} }
} }
// Handle user attached images rendering // Handle user attached images rendering
let messageForClipboard = message; let messageForClipboard = message;
let messageToRender = message; let messageToRender = message;
@ -480,8 +481,7 @@ const ChatMessage = forwardRef<HTMLDivElement, ChatMessageProps>((props, ref) =>
file.filename.endsWith(".org") || file.filename.endsWith(".org") ||
file.filename.endsWith(".md") file.filename.endsWith(".md")
) { ) {
const decodedText = atob(file.b64_data); message += `\n\n## ${file.filename}\n\`\`\`\n${file.b64_data}\n\`\`\`\n`;
message += `\n\n\`\`\`\n${decodedText}\n\`\`\``;
} }
}); });
}); });

View file

@ -2,7 +2,9 @@ import base64
import datetime import datetime
import json import json
import logging import logging
import mimetypes
import os import os
from pathlib import Path
from typing import Any, Callable, List, NamedTuple, Optional from typing import Any, Callable, List, NamedTuple, Optional
import aiohttp import aiohttp
@ -160,6 +162,13 @@ async def execute_sandboxed_python(code: str, input_data: list[dict], sandbox_ur
if response.status == 200: if response.status == 200:
result: dict[str, Any] = await response.json() result: dict[str, Any] = await response.json()
result["code"] = cleaned_code result["code"] = cleaned_code
# Store decoded output files
for output_file in result.get("output_files", []):
# Decode text files as UTF-8
if mimetypes.guess_type(output_file["filename"])[0].startswith("text/") or Path(
output_file["filename"]
).suffix in [".org", ".md", ".json"]:
output_file["b64_data"] = base64.b64decode(output_file["b64_data"]).decode("utf-8")
return result return result
else: else:
return { return {