(self, input: dict, request: Request)
| 20 | } |
| 21 | |
| 22 | async def communicate(self, input: dict, request: Request): |
| 23 | # Handle both JSON and multipart/form-data |
| 24 | if request.content_type.startswith("multipart/form-data"): |
| 25 | text = request.form.get("text", "") |
| 26 | ctxid = request.form.get("context", "") |
| 27 | message_id = request.form.get("message_id", None) |
| 28 | attachments = request.files.getlist("attachments") |
| 29 | attachment_paths = [] |
| 30 | |
| 31 | upload_folder_int = "/a0/usr/uploads" |
| 32 | upload_folder_ext = files.get_abs_path("usr/uploads") # for development environment |
| 33 | |
| 34 | if attachments: |
| 35 | os.makedirs(upload_folder_ext, exist_ok=True) |
| 36 | for attachment in attachments: |
| 37 | if attachment.filename is None: |
| 38 | continue |
| 39 | filename = safe_filename(attachment.filename) |
| 40 | if not filename: |
| 41 | continue |
| 42 | save_path = files.get_abs_path(upload_folder_ext, filename) |
| 43 | attachment.save(save_path) |
| 44 | attachment_paths.append(os.path.join(upload_folder_int, filename)) |
| 45 | else: |
| 46 | # Handle JSON request as before |
| 47 | input_data = request.get_json() |
| 48 | text = input_data.get("text", "") |
| 49 | ctxid = input_data.get("context", "") |
| 50 | message_id = input_data.get("message_id", None) |
| 51 | attachment_paths = [] |
| 52 | |
| 53 | # Now process the message |
| 54 | message = text |
| 55 | |
| 56 | # Obtain agent context |
| 57 | context = self.use_context(ctxid) |
| 58 | |
| 59 | # call extension point, alow it to modify data |
| 60 | data = { "message": message, "attachment_paths": attachment_paths } |
| 61 | await extension.call_extensions_async("user_message_ui", agent=context.get_agent(), data=data) |
| 62 | message = data.get("message", "") |
| 63 | attachment_paths = data.get("attachment_paths", []) |
| 64 | |
| 65 | # Store attachments in agent data |
| 66 | # context.agent0.set_data("attachments", attachment_paths) |
| 67 | |
| 68 | # Log to console and UI using helper function |
| 69 | mq.log_user_message(context, message, attachment_paths, message_id) |
| 70 | |
| 71 | return context.communicate(UserMessage(message=message, attachments=attachment_paths, id=message_id or "")), context |
no test coverage detected