Skip to content

Commit 2ce47c1

Browse files
committed
handle csv
1 parent 1db6069 commit 2ce47c1

File tree

3 files changed

+67
-10
lines changed

3 files changed

+67
-10
lines changed

agixt/XT.py

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,13 +1131,27 @@ def _get_file_access_instructions(
11311131
actual_path = converted_file_name if converted_file_name else file_name
11321132

11331133
# Base info about the file
1134-
info = f"## Uploaded File: `{file_name}`\n"
1135-
info += f"- **Size:** {file_size_kb} KB ({file_tokens} tokens)\n"
1136-
info += f"- **Path for commands:** `{actual_path}`\n"
1137-
info += f"- **URL:** [{file_name}]({file_url})\n"
1134+
# For xlsx/xls files that were converted, emphasize the CSV as the primary file to use
1135+
if (
1136+
converted_file_name
1137+
and converted_file_name != file_name
1138+
and file_type
1139+
and file_type.lower() in ["xlsx", "xls"]
1140+
):
1141+
info = f"## File Available: `{converted_file_name}` (converted from `{file_name}`)\n"
1142+
info += f"- **Original file:** `{file_name}` (Excel format - do NOT read this directly)\n"
1143+
info += f"- **Use this file:** `{actual_path}` (CSV format - use this for all commands)\n"
1144+
info += f"- **Size:** {file_size_kb} KB ({file_tokens} tokens)\n"
1145+
info += f"- **URL:** [{file_name}]({file_url})\n"
1146+
info += "\n**IMPORTANT:** The Excel file has been converted to CSV. Always use the CSV file (`{actual_path}`) for Read File and pandas operations.\n"
1147+
else:
1148+
info = f"## Uploaded File: `{file_name}`\n"
1149+
info += f"- **Size:** {file_size_kb} KB ({file_tokens} tokens)\n"
1150+
info += f"- **Path for commands:** `{actual_path}`\n"
1151+
info += f"- **URL:** [{file_name}]({file_url})\n"
11381152

1139-
if converted_file_name and converted_file_name != file_name:
1140-
info += f"- **Converted to:** `{converted_file_name}` (CSV format)\n"
1153+
if converted_file_name and converted_file_name != file_name:
1154+
info += f"- **Converted to:** `{converted_file_name}` (CSV format)\n"
11411155

11421156
info += "\n"
11431157

@@ -1228,9 +1242,9 @@ async def learn_spreadsheet(
12281242
csv_file_path = file_path.replace(f".{file_type}", ".csv")
12291243
csv_file_name = os.path.basename(csv_file_path)
12301244
df.to_csv(csv_file_path, index=False)
1231-
string_file_content += f"Content from file uploaded named `{file_name}` (also saved as `{csv_file_name}`):\n```csv\n{csv}```\n"
1245+
string_file_content += f"Content from uploaded Excel file `{file_name}` (converted and saved as `{csv_file_name}` - use this CSV file for all Read File and pandas operations):\n```csv\n{csv}```\n"
12321246
return (
1233-
f"Converted [{file_name}]({file_path}) and converted to CSV format at [{csv_file_name}]({csv_file_path}).",
1247+
f"Converted [{file_name}]({file_path}) to CSV format at [{csv_file_name}]({csv_file_path}). Use `{csv_file_name}` for file operations.",
12341248
string_file_content,
12351249
)
12361250
except Exception as e:

agixt/extensions/essential_abilities.py

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,7 @@ async def read_file(
439439
- The user can browse the agents workspace by clicking the folder icon in their chat input bar
440440
- For large files or data analysis, consider using Execute Python Code to extract specific information
441441
- For CSV/data files, use Execute Python Code with pandas to analyze data efficiently
442+
- XLSX/XLS files are automatically converted to CSV format for reading
442443
"""
443444
MAX_LINES = 100 # Maximum lines to return per read
444445
try:
@@ -452,6 +453,47 @@ async def read_file(
452453
try:
453454
filepath = self.safe_join(filename)
454455

456+
# Check if this is an Excel file - convert to CSV if needed
457+
file_ext = os.path.splitext(filename)[1].lower()
458+
csv_notice = ""
459+
if file_ext in [".xlsx", ".xls"]:
460+
import pandas as pd
461+
462+
# Check if CSV version already exists
463+
base_name = os.path.splitext(filename)[0]
464+
csv_filename = f"{base_name}.csv"
465+
csv_filepath = self.safe_join(csv_filename)
466+
467+
if not os.path.exists(csv_filepath):
468+
# Convert Excel to CSV
469+
try:
470+
xl = pd.ExcelFile(filepath)
471+
if len(xl.sheet_names) > 1:
472+
# Multiple sheets - convert each to separate CSV
473+
csv_files = []
474+
for i, sheet_name in enumerate(xl.sheet_names, 1):
475+
df = xl.parse(sheet_name)
476+
sheet_csv_filename = f"{base_name}_{i}.csv"
477+
sheet_csv_filepath = self.safe_join(sheet_csv_filename)
478+
df.to_csv(sheet_csv_filepath, index=False)
479+
csv_files.append(sheet_csv_filename)
480+
csv_notice = f"**Note**: Excel file `{filename}` has {len(xl.sheet_names)} sheets. Converted to: {', '.join(csv_files)}. Reading first sheet (`{csv_files[0]}`).\n\n"
481+
csv_filepath = self.safe_join(csv_files[0])
482+
csv_filename = csv_files[0]
483+
else:
484+
# Single sheet
485+
df = pd.read_excel(filepath)
486+
df.to_csv(csv_filepath, index=False)
487+
csv_notice = f"**Note**: Excel file `{filename}` converted to `{csv_filename}` for reading.\n\n"
488+
except Exception as e:
489+
return f"Error: Failed to convert Excel file to CSV: {str(e)}"
490+
else:
491+
csv_notice = f"**Note**: Reading CSV version `{csv_filename}` of Excel file `{filename}`.\n\n"
492+
493+
# Update filepath to read the CSV version
494+
filepath = csv_filepath
495+
filename = csv_filename
496+
455497
# Read the file lines
456498
with open(filepath, "r", encoding="utf-8") as f:
457499
lines = f.readlines()
@@ -479,7 +521,8 @@ async def read_file(
479521
lines_returned = len(selected_lines)
480522

481523
# Build header with line information
482-
header = (
524+
header = csv_notice # Include Excel->CSV conversion notice if applicable
525+
header += (
483526
f"Lines {actual_start}-{actual_end} of {total_lines} total lines:\n"
484527
)
485528
header += "=" * 40 + "\n"

agixt/version

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v1.8.1
1+
v1.8.2

0 commit comments

Comments
 (0)