Repository navigation
Expand file tree
/
Copy pathfile_tools.py
More file actions
547 lines (456 loc) · 19.2 KB
/
Copy pathfile_tools.py
File metadata and controls
547 lines (456 loc) · 19.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
from typing import Any, Dict, List, Optional
from pathlib import Path
import json
import uuid
import base64
import re
from datetime import datetime
from docx import Document
from pptx import Presentation
from openpyxl import load_workbook
from e2b_code_interpreter import Sandbox
from dotenv import load_dotenv
from backend.shared.logger import get_logger
from backend.shared.constants import CREATED_DOCUMENTS_PATH, REPORTS_CHARTS_FILE, openai_client, IMAGE_EXTENSIONS
from backend.core.prompts import describe_image_prompt
from backend.utils.e2b_file_template import ensure_file_agent_template
from langchain_core.tools import tool
load_dotenv()
GENERATED_FILES = []
FILES_PATH = Path(CREATED_DOCUMENTS_PATH)
FILES_PATH.mkdir(parents=True, exist_ok=True)
FILE_AGENT_SANDBOX_TEMPLATE = "file-agent-template"
logger = get_logger("FILE_TOOLS")
def _resolve_file_path(file_name: str) -> Path:
"""Resolve a filename to an absolute path within CREATED_DOCUMENTS_PATH.
Strips any directory components to prevent path traversal."""
safe_name = Path(file_name).name
return FILES_PATH / safe_name
def _stage_referenced_files_in_sandbox(code: str, sandbox: Sandbox) -> List[str]:
"""Upload locally created files referenced in code into the sandbox."""
uploaded_files = []
pattern = r"""['"]([^'"]+\.(?:pptx|docx|xlsx|xls|png|jpg|jpeg|gif|bmp|pdf|csv|txt))['"]"""
referenced_paths = re.findall(pattern, code, flags=re.IGNORECASE)
for referenced_path in referenced_paths:
local_path = _resolve_file_path(Path(referenced_path).name)
if not local_path.exists() or not local_path.is_file():
continue
sandbox_name = local_path.name
file_bytes = local_path.read_bytes()
sandbox.files.write(f"/home/user/{sandbox_name}", file_bytes)
sandbox.files.write(f"/mnt/data/{sandbox_name}", file_bytes)
uploaded_files.append(sandbox_name)
return sorted(set(uploaded_files))
@tool(parse_docstring=True)
def read_excel_file(
file_name: str, sheet_name: Optional[str] = None, max_rows: int = 100
) -> Dict[str, Any]:
"""Read the contents of an Excel file from the documents directory.
Args:
file_name: Name of the Excel file to read (must be in the created documents folder).
sheet_name: Name of the sheet to read (None reads the active sheet).
max_rows: Maximum number of data rows to return (default 100).
"""
try:
file_path = _resolve_file_path(file_name)
if not file_path.exists():
return {"success": False, "error": f"File not found: {file_name}"}
wb = load_workbook(str(file_path), data_only=True)
if sheet_name:
if sheet_name not in wb.sheetnames:
return {
"success": False,
"error": f"Sheet '{sheet_name}' not found. Available sheets: {', '.join(wb.sheetnames)}",
}
ws = wb[sheet_name]
else:
ws = wb.active
rows = []
for idx, row in enumerate(ws.iter_rows(values_only=True)):
if idx >= max_rows:
break
rows.append([str(cell) if cell is not None else "" for cell in row])
total_rows = ws.max_row or 0
truncated = total_rows > max_rows
return {
"success": True,
"file_name": file_name,
"sheet_name": ws.title,
"all_sheets": wb.sheetnames,
"total_rows": total_rows,
"rows_returned": len(rows),
"truncated": truncated,
"data": rows,
}
except Exception as e:
logger.error(f"Error reading Excel file: {str(e)}")
return {"success": False, "error": f"Failed to read Excel file: {str(e)}"}
@tool(parse_docstring=True)
def read_word_document(file_name: str) -> Dict[str, Any]:
"""Read the contents of a Word document from the documents directory.
Args:
file_name: Name of the Word document to read (must be in the created documents folder).
"""
try:
file_path = _resolve_file_path(file_name)
if not file_path.exists():
return {"success": False, "error": f"File not found: {file_name}"}
doc = Document(str(file_path))
paragraphs = []
for para in doc.paragraphs:
if para.text.strip():
paragraphs.append({
"text": para.text,
"style": para.style.name if para.style else "Normal",
})
tables = []
for table_idx, table in enumerate(doc.tables):
table_rows = []
for row in table.rows:
table_rows.append([cell.text for cell in row.cells])
tables.append({
"table_index": table_idx,
"rows": len(table.rows),
"columns": len(table.columns),
"data": table_rows,
})
return {
"success": True,
"file_name": file_name,
"paragraph_count": len(paragraphs),
"table_count": len(tables),
"paragraphs": paragraphs,
"tables": tables,
}
except Exception as e:
logger.error(f"Error reading Word document: {str(e)}")
return {"success": False, "error": f"Failed to read Word document: {str(e)}"}
@tool(parse_docstring=True)
def read_powerpoint_file(file_name: str) -> Dict[str, Any]:
"""Read the contents of a PowerPoint file from the documents directory.
Args:
file_name: Name of the PowerPoint file to read (must be in the created documents folder).
"""
try:
file_path = _resolve_file_path(file_name)
if not file_path.exists():
return {"success": False, "error": f"File not found: {file_name}"}
prs = Presentation(str(file_path))
slides = []
for slide_idx, slide in enumerate(prs.slides):
slide_data = {
"slide_number": slide_idx + 1,
"layout": slide.slide_layout.name if slide.slide_layout else "Unknown",
"shapes": [],
"tables": [],
"notes": "",
}
for shape in slide.shapes:
if shape.has_text_frame:
text_content = "\n".join(
para.text for para in shape.text_frame.paragraphs if para.text.strip()
)
if text_content.strip():
slide_data["shapes"].append({
"name": shape.name,
"type": "text",
"content": text_content,
})
if shape.has_table:
table = shape.table
table_rows = []
for row in table.rows:
table_rows.append([cell.text for cell in row.cells])
slide_data["tables"].append({
"rows": len(table.rows),
"columns": len(table.columns),
"data": table_rows,
})
if slide.has_notes_slide and slide.notes_slide.notes_text_frame:
slide_data["notes"] = slide.notes_slide.notes_text_frame.text
slides.append(slide_data)
return {
"success": True,
"file_name": file_name,
"slide_count": len(slides),
"slides": slides,
}
except Exception as e:
logger.error(f"Error reading PowerPoint file: {str(e)}")
return {"success": False, "error": f"Failed to read PowerPoint file: {str(e)}"}
@tool(parse_docstring=True)
def describe_file_image(file_name: str, query: str) -> str:
"""Analyze an image file from the documents directory using vision AI.
Use this tool to understand what is shown in an image (PNG, JPG) that exists
in the created documents folder.
Args:
file_name: Name of the image file to analyze (must be in the created documents folder).
query: A question or description of what you want to understand about the image.
"""
try:
file_path = _resolve_file_path(file_name)
if not file_path.exists():
return f"Error: Image file not found: {file_name}"
ext = file_path.suffix.lower()
if ext not in IMAGE_EXTENSIONS:
return f"Error: Unsupported image format '{ext}'. Supported: {', '.join(IMAGE_EXTENSIONS)}"
with open(file_path, "rb") as f:
base64_image = base64.b64encode(f.read()).decode("utf-8")
mime_map = {
".jpg": "image/jpeg",
".jpeg": "image/jpeg",
".png": "image/png",
".gif": "image/gif",
".webp": "image/webp",
".bmp": "image/bmp",
}
mime_type = mime_map.get(ext, "image/png")
messages = [
{
"role": "user",
"content": [
{
"type": "text",
"text": f"{describe_image_prompt}\n\nUser Query: {query}",
},
{
"type": "image_url",
"image_url": {
"url": f"data:{mime_type};base64,{base64_image}",
"detail": "high",
},
},
],
}
]
response = openai_client.chat.completions.create(
model="gpt-5.2",
messages=messages,
max_completion_tokens=1500,
temperature=0.1,
)
if not response.choices[0].message.content:
return "Error: No response received from Vision API"
analysis = response.choices[0].message.content
logger.info(f"Successfully analyzed image: {file_name}")
return f"Image Analysis Results:\n\n{analysis}"
except Exception as e:
error_msg = f"Error analyzing image {file_name}: {str(e)}"
logger.error(error_msg)
return error_msg
@tool(parse_docstring=True)
def execute_file_code(code: str, output_filenames: List[str]) -> Dict[str, Any]:
"""Execute Python code in a sandboxed environment to create or process files.
The sandbox has python-pptx, openpyxl, python-docx, and Pillow pre-installed.
Use this for complex file operations that go beyond what the dedicated tools offer.
Existing files from the created documents folder that are referenced in the code
are automatically uploaded into the sandbox at /home/user/<filename> and
/mnt/data/<filename>. Each output file must be saved under /home/user/ in the sandbox.
Args:
code: Complete Python code to execute. Must save output files under /home/user/.
output_filenames: List of filenames the code will produce under /home/user/ (e.g. ["report.docx", "chart.png"]).
"""
sandbox = None
try:
# Check syntax locally before spending time on a sandbox
try:
compile(code, "<file_code>", "exec")
except SyntaxError as e:
return {
"success": False,
"error": (
f"SyntaxError before execution: {e.msg} at line {e.lineno}\n"
f" {e.text or ''}"
f"Fix the syntax and retry."
),
}
logger.info(f"Creating sandbox for code execution: {code}")
try:
sandbox = Sandbox.create(template=FILE_AGENT_SANDBOX_TEMPLATE, timeout=60)
except Exception as create_err:
logger.warning(
"Sandbox creation failed %s. Building template and retrying once.",
str(create_err),
)
ensure_file_agent_template(alias=FILE_AGENT_SANDBOX_TEMPLATE)
sandbox = Sandbox.create(template=FILE_AGENT_SANDBOX_TEMPLATE, timeout=60)
staged_inputs = _stage_referenced_files_in_sandbox(code, sandbox)
execution = sandbox.run_code(code)
logger.info(f"Execution result: {execution}")
if execution.error:
traceback = execution.error.traceback or ""
logger.error(f"Code execution error: {execution.error.name}: {execution.error.value}\nTraceback:\n{traceback}\nFix the code and retry.")
return {
"success": False,
"error": (
f"Code execution error: {execution.error.name}: {execution.error.value}\n"
f"Traceback:\n{traceback}\n"
f"Fix the code and retry."
),
}
saved_files = []
errors = []
for fname in output_filenames:
safe_name = Path(fname).name
sandbox_path = f"/home/user/{safe_name}"
suffix = Path(safe_name).suffix
is_edit_of_existing_file = safe_name in staged_inputs
if is_edit_of_existing_file:
local_name = safe_name
local_path = FILES_PATH / local_name
else:
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
unique_id = uuid.uuid4().hex[:8]
stem = Path(safe_name).stem
local_name = f"{stem}_{timestamp}_{unique_id}{suffix}"
local_path = FILES_PATH / local_name
try:
content = sandbox.files.read(sandbox_path, format="bytes")
with open(local_path, "wb") as f:
f.write(content)
ext = suffix.lower()
type_map = {
".xlsx": "excel", ".xls": "excel",
".docx": "word", ".doc": "word",
".pptx": "powerpoint",
".png": "image", ".jpg": "image", ".jpeg": "image",
".pdf": "pdf", ".csv": "csv", ".txt": "text",
}
file_type = type_map.get(ext, "unknown")
file_info = {
"filename": local_name,
"file_path": str(local_path),
"file_type": file_type,
"created_at": datetime.now().isoformat(),
"message": (
f"File updated via sandbox: {local_name}"
if is_edit_of_existing_file
else f"File created via sandbox: {local_name}"
),
}
set_generated_files([file_info])
saved_files.append(file_info)
except Exception as e:
errors.append(f"Failed to retrieve '{fname}': {str(e)}")
stdout = ""
if execution.logs and execution.logs.stdout:
stdout = "\n".join(execution.logs.stdout)
result = {
"success": len(saved_files) > 0,
"files_created": saved_files,
"stdout": stdout,
}
if staged_inputs:
result["staged_input_files"] = staged_inputs
if errors:
result["errors"] = errors
return result
except Exception as e:
logger.error(f"Error executing code: {str(e)}")
return {"success": False, "error": f"Sandbox error: {str(e)}"}
finally:
if sandbox:
try:
logger.info(f"Killing sandbox: {sandbox}")
sandbox.kill()
except Exception as e:
logger.warning(f"Failed to kill sandbox: {str(e)}")
@tool(parse_docstring=True)
def list_created_files() -> Dict[str, Any]:
"""List all created files in the documents directory.
Returns a list of all files that have been created, including their names and full paths.
This helps track which files are available and where they are located.
"""
try:
created_files = []
if FILES_PATH.exists() and FILES_PATH.is_dir():
for file_path in FILES_PATH.iterdir():
if file_path.is_file():
extension = file_path.suffix.lower()
type_map = {
".xlsx": "excel", ".xls": "excel",
".docx": "word", ".doc": "word",
".pptx": "powerpoint",
".pdf": "pdf",
".txt": "text",
".csv": "csv",
".png": "image", ".jpg": "image", ".jpeg": "image",
".gif": "image", ".bmp": "image",
}
file_type = type_map.get(extension, "unknown")
created_files.append({
"filename": file_path.name,
"file_path": str(file_path),
"file_type": file_type,
"extension": extension,
})
return {
"success": True,
"count": len(created_files),
"files": created_files,
"message": f"Found {len(created_files)} created file(s) in the documents directory.",
}
except Exception as e:
logger.error(f"Error listing created files: {str(e)}")
return {
"success": False,
"error": f"Failed to list created files: {str(e)}",
"files": [],
}
@tool(parse_docstring=True)
def get_available_charts() -> Dict[str, Any]:
"""Get a list of chart images that have been generated by the plotting agent.
Returns chart PNG filenames and their descriptions so they can be embedded
into Word documents or PowerPoint presentations via the sandbox.
Each chart PNG lives in the created documents folder alongside other files.
"""
try:
if not REPORTS_CHARTS_FILE.exists():
return {
"success": True,
"count": 0,
"charts": [],
"message": "No charts have been generated yet.",
}
with open(REPORTS_CHARTS_FILE, "r", encoding="utf-8") as f:
records = json.load(f)
if not isinstance(records, list):
records = []
charts = []
for record in records:
png_rel = record.get("png_file_path", "")
png_name = Path(png_rel).name if png_rel else ""
png_full = FILES_PATH / png_name if png_name else None
charts.append({
"filename": png_name,
"exists": png_full.exists() if png_full else False,
"description": record.get("description", ""),
"created_at": record.get("creation_time", ""),
})
return {
"success": True,
"count": len(charts),
"charts": charts,
"message": f"Found {len(charts)} chart(s) available for embedding.",
}
except Exception as e:
logger.error(f"Error reading chart reports: {str(e)}")
return {
"success": False,
"error": f"Failed to read chart reports: {str(e)}",
"charts": [],
}
def get_generated_files() -> Dict[str, Any]:
global GENERATED_FILES
return GENERATED_FILES
def clear_generated_files():
global GENERATED_FILES
GENERATED_FILES.clear()
def set_generated_files(files: List[Dict[str, Any]]):
global GENERATED_FILES
for new_file in files:
GENERATED_FILES = [
f for f in GENERATED_FILES
if f.get("filename") != new_file.get("filename")
]
GENERATED_FILES.append(new_file)