blackopsrepl commited on
Commit
600ed2b
·
1 Parent(s): 08465c5

hotfix: fix file attachment in mcp backend

Browse files
Files changed (1) hide show
  1. src/handlers/mcp_backend.py +28 -12
src/handlers/mcp_backend.py CHANGED
@@ -42,15 +42,18 @@ setup_logging()
42
  logger = get_logger(__name__)
43
 
44
 
45
- async def process_message_and_attached_file(file_path: str, message_body: str) -> dict:
 
 
46
  """
47
  MCP API endpoint for processing calendar files and task descriptions.
48
 
49
  This is a separate workflow from the main Gradio UI and handles external API requests.
50
 
51
  Args:
52
- file_path (str): Path to the attached file (typically .ics calendar file)
53
  message_body (str): The body of the last chat message, which contains the task description
 
54
  Returns:
55
  dict: Contains confirmation, file info, calendar entries, error, and solved schedule info
56
  """
@@ -59,7 +62,10 @@ async def process_message_and_attached_file(file_path: str, message_body: str) -
59
  debug_mode = is_debug_enabled()
60
 
61
  logger.info("MCP Handler: Processing message with attached file")
62
- logger.debug("File path: %s", file_path)
 
 
 
63
  logger.debug("Message: %s", message_body)
64
  logger.debug("Debug mode: %s", debug_mode)
65
 
@@ -67,25 +73,33 @@ async def process_message_and_attached_file(file_path: str, message_body: str) -
67
  start_time = time.time()
68
 
69
  try:
70
- # Step 1: Extract calendar entries from the attached file
71
  logger.info("Step 1: Extracting calendar entries...")
72
 
73
- with open(file_path, "rb") as f:
74
- file_bytes = f.read()
 
 
 
 
 
 
75
 
76
- calendar_entries, error = extract_ical_entries(file_bytes)
77
 
78
  if error:
79
  logger.error("Failed to extract calendar entries: %s", error)
80
  return {
81
  "error": f"Failed to extract calendar entries: {error}",
82
- "status": "failed",
83
  "timestamp": time.time(),
84
  "processing_time_seconds": time.time() - start_time,
85
  }
86
 
87
  logger.info("Extracted %d calendar entries", len(calendar_entries))
88
- if debug_mode:
 
 
89
  logger.debug(
90
  "Calendar entries details: %s",
91
  [e.get("summary", "No summary") for e in calendar_entries[:5]],
@@ -167,7 +181,8 @@ async def process_message_and_attached_file(file_path: str, message_body: str) -
167
  "status": "success",
168
  "message": "Schedule solved successfully",
169
  "file_info": {
170
- "path": file_path,
 
171
  "calendar_entries_count": len(calendar_entries),
172
  },
173
  "calendar_entries": calendar_entries,
@@ -219,7 +234,8 @@ async def process_message_and_attached_file(file_path: str, message_body: str) -
219
  "status": "timeout",
220
  "message": "Schedule solving timed out after maximum polls",
221
  "file_info": {
222
- "path": file_path,
 
223
  "calendar_entries_count": len(calendar_entries),
224
  },
225
  "calendar_entries": calendar_entries,
@@ -239,7 +255,7 @@ async def process_message_and_attached_file(file_path: str, message_body: str) -
239
  return {
240
  "error": str(e),
241
  "status": "failed",
242
- "file_path": file_path,
243
  "message_body": message_body,
244
  "processing_time_seconds": processing_time,
245
  "timestamp": time.time(),
 
42
  logger = get_logger(__name__)
43
 
44
 
45
+ async def process_message_and_attached_file(
46
+ file_content: bytes, message_body: str, file_name: str = "calendar.ics"
47
+ ) -> dict:
48
  """
49
  MCP API endpoint for processing calendar files and task descriptions.
50
 
51
  This is a separate workflow from the main Gradio UI and handles external API requests.
52
 
53
  Args:
54
+ file_content (bytes): The actual file content bytes (typically .ics calendar file)
55
  message_body (str): The body of the last chat message, which contains the task description
56
+ file_name (str): Optional filename for logging purposes
57
  Returns:
58
  dict: Contains confirmation, file info, calendar entries, error, and solved schedule info
59
  """
 
62
  debug_mode = is_debug_enabled()
63
 
64
  logger.info("MCP Handler: Processing message with attached file")
65
+ logger.debug("File name: %s", file_name)
66
+ logger.debug(
67
+ "File content size: %d bytes", len(file_content) if file_content else 0
68
+ )
69
  logger.debug("Message: %s", message_body)
70
  logger.debug("Debug mode: %s", debug_mode)
71
 
 
73
  start_time = time.time()
74
 
75
  try:
76
+ # Step 1: Extract calendar entries from the file content
77
  logger.info("Step 1: Extracting calendar entries...")
78
 
79
+ if not file_content:
80
+ logger.error("No file content provided")
81
+ return {
82
+ "error": "No file content provided",
83
+ "status": "no_file_content",
84
+ "timestamp": time.time(),
85
+ "processing_time_seconds": time.time() - start_time,
86
+ }
87
 
88
+ calendar_entries, error = extract_ical_entries(file_content)
89
 
90
  if error:
91
  logger.error("Failed to extract calendar entries: %s", error)
92
  return {
93
  "error": f"Failed to extract calendar entries: {error}",
94
+ "status": "calendar_parse_failed",
95
  "timestamp": time.time(),
96
  "processing_time_seconds": time.time() - start_time,
97
  }
98
 
99
  logger.info("Extracted %d calendar entries", len(calendar_entries))
100
+
101
+ # Log the calendar entries for debugging
102
+ if debug_mode and calendar_entries:
103
  logger.debug(
104
  "Calendar entries details: %s",
105
  [e.get("summary", "No summary") for e in calendar_entries[:5]],
 
181
  "status": "success",
182
  "message": "Schedule solved successfully",
183
  "file_info": {
184
+ "name": file_name,
185
+ "size_bytes": len(file_content),
186
  "calendar_entries_count": len(calendar_entries),
187
  },
188
  "calendar_entries": calendar_entries,
 
234
  "status": "timeout",
235
  "message": "Schedule solving timed out after maximum polls",
236
  "file_info": {
237
+ "name": file_name,
238
+ "size_bytes": len(file_content),
239
  "calendar_entries_count": len(calendar_entries),
240
  },
241
  "calendar_entries": calendar_entries,
 
255
  return {
256
  "error": str(e),
257
  "status": "failed",
258
+ "file_name": file_name,
259
  "message_body": message_body,
260
  "processing_time_seconds": processing_time,
261
  "timestamp": time.time(),