|
1 | 1 | # SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
2 | 2 | # SPDX-License-Identifier: AGPL-3.0-or-later |
3 | 3 | from asyncio import sleep |
4 | | -from typing import Optional |
5 | 4 |
|
6 | 5 | from niquests import ConnectionError, Timeout |
7 | 6 | from langchain_core.tools import tool |
@@ -50,81 +49,82 @@ async def send_email(subject: str, body: str, account_id: int, from_email: str, |
50 | 49 | async def get_mail_account_list(): |
51 | 50 | """ |
52 | 51 | Lists all available email accounts of the current user including their account id |
53 | | - :param subject: The subject of the email |
54 | | - :param body: The body of the email |
55 | | - :param account_id: The id of the account to send from |
56 | | - :param to_emails: The emails to send |
| 52 | + :return: list of email accounts with their ids and configuration |
57 | 53 | """ |
58 | 54 |
|
59 | 55 | return await nc.ocs('GET', '/ocs/v2.php/apps/mail/account/list') |
60 | | - |
| 56 | + |
61 | 57 |
|
62 | 58 | @tool |
63 | 59 | @safe_tool |
64 | 60 | async def list_mail_folders(account_id: int): |
65 | 61 | """ |
66 | 62 | List all mail folders/mailboxes for an account |
67 | 63 | :param account_id: The id of the account (obtainable via get_mail_account_list) |
68 | | - :return: list of folders with their names and message counts |
| 64 | + :return: list of folders with their ids, names, and message counts |
69 | 65 | """ |
70 | | - return await nc.ocs('GET', f'/ocs/v2.php/apps/mail/account/{account_id}/mailboxes') |
| 66 | + response = await nc._session._create_adapter(True).request('GET', f"{nc.app_cfg.endpoint}/index.php/apps/mail/api/mailboxes", headers={ |
| 67 | + "Content-Type": "application/json", |
| 68 | + }, params={'accountId': account_id}) |
| 69 | + return response.json() |
71 | 70 |
|
72 | 71 | @tool |
73 | 72 | @safe_tool |
74 | | - async def search_emails(account_id: int, search_term: str, mailbox_name: Optional[str] = None, limit: int = 20): |
| 73 | + async def get_email_messages(mailbox_id: int, limit: int = 20): |
75 | 74 | """ |
76 | | - Search for emails in an account |
77 | | - :param account_id: The id of the account (obtainable via get_mail_account_list) |
78 | | - :param search_term: The text to search for in emails |
79 | | - :param mailbox_name: Optional mailbox/folder to search in (e.g., "INBOX", "Sent") |
80 | | - :param limit: Maximum number of results to return (default 20) |
81 | | - :return: list of matching emails |
| 75 | + Get messages from a specific mailbox |
| 76 | + :param mailbox_id: The id of the mailbox (obtainable via list_mail_folders) |
| 77 | + :param limit: Maximum number of messages to return (default 20) |
| 78 | + :return: list of email messages |
82 | 79 | """ |
83 | | - params = { |
84 | | - 'searchQuery': search_term, |
85 | | - 'limit': limit |
86 | | - } |
87 | | - if mailbox_name: |
88 | | - params['mailboxName'] = mailbox_name |
89 | | - |
90 | | - return await nc.ocs('GET', f'/ocs/v2.php/apps/mail/account/{account_id}/messages', params=params) |
| 80 | + response = await nc._session._create_adapter(True).request('GET', f"{nc.app_cfg.endpoint}/index.php/apps/mail/api/messages", headers={ |
| 81 | + "Content-Type": "application/json", |
| 82 | + }, params={'mailboxId': mailbox_id, 'limit': limit}) |
| 83 | + return response.json() |
91 | 84 |
|
92 | 85 | @tool |
93 | 86 | @safe_tool |
94 | | - async def get_email_messages(account_id: int, mailbox_name: str = 'INBOX', limit: int = 20): |
| 87 | + async def search_emails(mailbox_id: int, search_term: str, limit: int = 20): |
95 | 88 | """ |
96 | | - Get messages from a specific mailbox |
97 | | - :param account_id: The id of the account (obtainable via get_mail_account_list) |
98 | | - :param mailbox_name: The mailbox/folder name (default "INBOX") |
99 | | - :param limit: Maximum number of messages to return (default 20) |
100 | | - :return: list of email messages |
| 89 | + Search for emails in a mailbox |
| 90 | + :param mailbox_id: The id of the mailbox to search in (obtainable via list_mail_folders) |
| 91 | + :param search_term: The text to search for in emails |
| 92 | + :param limit: Maximum number of results to return (default 20) |
| 93 | + :return: list of matching emails |
101 | 94 | """ |
102 | | - return await nc.ocs('GET', f'/ocs/v2.php/apps/mail/account/{account_id}/mailboxes/{mailbox_name}/messages', params={'limit': limit}) |
| 95 | + response = await nc._session._create_adapter(True).request('GET', f"{nc.app_cfg.endpoint}/index.php/apps/mail/api/messages", headers={ |
| 96 | + "Content-Type": "application/json", |
| 97 | + }, params={'mailboxId': mailbox_id, 'filter': search_term, 'limit': limit}) |
| 98 | + return response.json() |
103 | 99 |
|
104 | 100 | @tool |
105 | 101 | @dangerous_tool |
106 | | - async def move_email_to_folder(account_id: int, message_id: int, target_mailbox: str): |
| 102 | + async def move_email_to_folder(message_id: int, dest_mailbox_id: int): |
107 | 103 | """ |
108 | 104 | Move an email to a different folder |
109 | | - :param account_id: The id of the account (obtainable via get_mail_account_list) |
110 | | - :param message_id: The id of the message to move |
111 | | - :param target_mailbox: The name of the destination folder (obtainable via list_mail_folders) |
| 105 | + :param message_id: The id of the message to move (obtainable via get_email_messages or search_emails) |
| 106 | + :param dest_mailbox_id: The id of the destination mailbox (obtainable via list_mail_folders) |
112 | 107 | :return: confirmation |
113 | 108 | """ |
114 | | - return await nc.ocs('POST', f'/ocs/v2.php/apps/mail/account/{account_id}/message/{message_id}/move', json={ |
115 | | - 'mailboxName': target_mailbox |
| 109 | + response = await nc._session._create_adapter(True).request('POST', f"{nc.app_cfg.endpoint}/index.php/apps/mail/api/messages/{message_id}/move", headers={ |
| 110 | + "Content-Type": "application/json", |
| 111 | + }, json={ |
| 112 | + 'destFolderId': dest_mailbox_id |
116 | 113 | }) |
| 114 | + return response.json() |
117 | 115 |
|
118 | 116 | @tool |
119 | 117 | @dangerous_tool |
120 | | - async def delete_email(account_id: int, message_id: int): |
| 118 | + async def delete_email(message_id: int): |
121 | 119 | """ |
122 | 120 | Delete an email message |
123 | | - :param account_id: The id of the account (obtainable via get_mail_account_list) |
124 | | - :param message_id: The id of the message to delete |
| 121 | + :param message_id: The id of the message to delete (obtainable via get_email_messages or search_emails) |
125 | 122 | :return: confirmation |
126 | 123 | """ |
127 | | - return await nc.ocs('DELETE', f'/ocs/v2.php/apps/mail/account/{account_id}/message/{message_id}') |
| 124 | + response = await nc._session._create_adapter(True).request('DELETE', f"{nc.app_cfg.endpoint}/index.php/apps/mail/api/messages/{message_id}", headers={ |
| 125 | + "Content-Type": "application/json", |
| 126 | + }) |
| 127 | + return response.json() |
128 | 128 |
|
129 | 129 | return [ |
130 | 130 | send_email, |
|
0 commit comments