Skip to content

Commit ab65cf1

Browse files
authored
Feature: Allow add multiple files with /file command (#25)
* feat(command_processor.py): enhance file command to support multiple file inputs and provide detailed processing feedback This change allows users to process multiple files in a single command, improving usability. It also adds error handling and notifications for successful and failed file processing, ensuring users receive clear feedback on the command's execution. * refactor(command_processor.py): add type hints for better code clarity and maintainability fix(command_processor.py): ensure consistent handling of file content types and improve variable declarations * refactor(command_processor.py): update comments for clarity and remove unused notification code to streamline the file command handling process * refactor(command_processor.py): remove redundant notification for file processing and simplify success notification format * refactor(command_processor.py): simplify file command handling by removing unnecessary checks and consolidating logic for better readability and maintainability * refactor(command_processor.py): update type hint for all_file_contents to specify it contains dictionaries and enhance file content formatting feat(command_processor.py): wrap file content with XML tags and structure it as a dictionary for better message handling * refactor(command_processor.py): simplify file content processing by removing unnecessary variable assignments and XML formatting to enhance code clarity and maintainability
1 parent d72da87 commit ab65cf1

File tree

1 file changed

+64
-34
lines changed

1 file changed

+64
-34
lines changed

AgentCrew/modules/chat/message/command_processor.py

Lines changed: 64 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from dataclasses import dataclass
2-
from typing import Optional, Tuple
2+
from typing import Optional, Tuple, List, Dict, Any, Union
33
import os
44

55
from AgentCrew.modules.agents.local_agent import LocalAgent
@@ -438,39 +438,69 @@ def _handle_agent_command(self, command: str) -> Tuple[bool, str]:
438438
)
439439

440440
def _handle_file_command(self, user_input: str) -> CommandResult:
441-
"""Handle file command."""
442-
file_path = user_input[6:].strip()
443-
file_path = os.path.expanduser(file_path)
444-
445-
self.message_handler._notify("file_processing", {"file_path": file_path})
446-
447-
# Process file with the file handling service
448-
if self.message_handler.file_handler is None:
449-
self.message_handler.file_handler = FileHandler()
450-
file_content = self.message_handler.file_handler.process_file(file_path)
451-
# Fallback to llm handle
452-
if not file_content:
453-
from AgentCrew.modules.agents.base import MessageType
454-
455-
file_content = self.message_handler.agent.format_message(
456-
MessageType.FileContent, {"file_uri": file_path}
457-
)
441+
"""Handle file command with support for multiple files."""
442+
# Extract file paths from user input (space-separated)
443+
file_paths_str: str = user_input[6:].strip()
444+
file_paths: List[str] = [os.path.expanduser(path.strip()) for path in file_paths_str.split() if path.strip()]
445+
446+
if not file_paths:
447+
self.message_handler._notify("error", "No file paths provided")
448+
return CommandResult(handled=True, clear_flag=True)
449+
450+
processed_files: List[str] = []
451+
failed_files: List[str] = []
452+
all_file_contents: List[Dict[str, str]] = []
453+
454+
# Process each file
455+
for file_path in file_paths:
456+
self.message_handler._notify("file_processing", {"file_path": file_path})
457+
458+
# Process file with the file handling service
459+
if self.message_handler.file_handler is None:
460+
self.message_handler.file_handler = FileHandler()
461+
file_content = self.message_handler.file_handler.process_file(file_path)
462+
463+
# Fallback to llm handle
464+
if not file_content:
465+
from AgentCrew.modules.agents.base import MessageType
466+
467+
file_content = self.message_handler.agent.format_message(
468+
MessageType.FileContent, {"file_uri": file_path}
469+
)
458470

459-
if file_content:
471+
if file_content:
472+
all_file_contents.append(file_content)
473+
processed_files.append(file_path)
474+
self.message_handler._notify(
475+
"file_processed",
476+
{
477+
"file_path": file_path,
478+
"message": file_content,
479+
},
480+
)
481+
else:
482+
failed_files.append(file_path)
483+
self.message_handler._notify(
484+
"error",
485+
f"Failed to process file {file_path} Or Model is not supported",
486+
)
487+
488+
# Add all successfully processed file contents to messages
489+
if all_file_contents:
460490
self.message_handler._messages_append(
461-
{"role": "user", "content": [file_content]}
462-
)
463-
self.message_handler._notify(
464-
"file_processed",
465-
{
466-
"file_path": file_path,
467-
"message": self.message_handler.agent.history[-1],
468-
},
491+
{"role": "user", "content": all_file_contents}
469492
)
470-
return CommandResult(handled=True, clear_flag=True)
471-
else:
472-
self.message_handler._notify(
473-
"error",
474-
f"Failed to process file {file_path} Or Model is not supported",
475-
)
476-
return CommandResult(handled=True, clear_flag=True)
493+
494+
# Notify about overall processing results
495+
if failed_files:
496+
self.message_handler._notify(
497+
"system_message",
498+
f"Processed {len(processed_files)} files successfully. Failed to process: {', '.join(failed_files)}"
499+
)
500+
else:
501+
self.message_handler._notify(
502+
"system_message",
503+
f"Successfully processed {len(processed_files)} files: {', '.join(processed_files)}"
504+
)
505+
506+
return CommandResult(handled=True, clear_flag=True)

0 commit comments

Comments
 (0)