Skip to content

Commit 19e0d59

Browse files
committed
fix session3 E6 pre commit
1 parent 00069a7 commit 19e0d59

File tree

4 files changed

+67
-36
lines changed

4 files changed

+67
-36
lines changed

01-tutorials/workshop/session3/E6a_mail_ast_with_guard/colors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,41 @@ class Colors:
99
BOLD = "\033[1m"
1010
UNDERLINE = "\033[4m"
1111

12+
1213
def print_agent_reflect(text: str):
1314
"""打印智能体反思"""
1415
print(f"\n{Colors.CYAN}{Colors.BOLD}[智能体反思]{Colors.ENDC}")
1516
print(f"{Colors.CYAN}{text}{Colors.ENDC}")
1617

18+
1719
def print_agent_act(texts: list):
1820
"""打印智能体行动"""
1921
print(f"\n{Colors.YELLOW}{Colors.BOLD}[智能体行动]{Colors.ENDC}")
2022
for text in texts:
2123
print(f"{Colors.YELLOW}{text}{Colors.ENDC}")
2224

25+
2326
def print_agent_permission(texts: list):
2427
"""打印智能体权限"""
2528
print(f"\n{Colors.RED}{Colors.BOLD}[智能体权限]{Colors.ENDC}")
2629
for text in texts:
2730
print(f"{Colors.RED}{text}{Colors.ENDC}")
2831

32+
2933
def print_header(text: str):
3034
"""打印标题"""
3135
print(f"\n{Colors.HEADER}{Colors.BOLD}{'=' * 60}{Colors.ENDC}")
3236
print(f"{Colors.HEADER}{Colors.BOLD}{text.center(60)}{Colors.ENDC}")
3337
print(f"{Colors.HEADER}{Colors.BOLD}{'=' * 60}{Colors.ENDC}\n")
3438

39+
3540
def print_user_request(texts: list):
3641
"""打印用户请求"""
3742
print(f"\n{Colors.GREEN}{Colors.BOLD}[User]{Colors.ENDC}")
3843
for text in texts:
3944
print(f"{Colors.GREEN}{text}{Colors.ENDC}")
4045

46+
4147
def print_agent_respond(texts: list):
4248
"""打印智能体响应"""
4349
print(f"\n{Colors.BLUE}{Colors.BOLD}[Agent]{Colors.ENDC}")

01-tutorials/workshop/session3/E6a_mail_ast_with_guard/tools.py

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
import json
33
import os
44

5+
56
# Mock数据结构
67
class Email:
78
def __init__(
@@ -11,7 +12,7 @@ def __init__(
1112
subject: str,
1213
body: str,
1314
received_date: str,
14-
priority: str = "normal"
15+
priority: str = "normal",
1516
):
1617
self.id = id
1718
self.sender = sender
@@ -30,6 +31,7 @@ def to_dict(self):
3031
"priority": self.priority,
3132
}
3233

34+
3335
# 从JSON文件加载邮件数据
3436
# 获取当前文件的目录
3537
current_dir = os.path.dirname(os.path.abspath(__file__))
@@ -39,6 +41,7 @@ def to_dict(self):
3941
with open(email_file_path, "r", encoding="utf-8") as f:
4042
email_data = json.load(f)
4143

44+
4245
def read_inbox(mailbox: str, unread_only: bool):
4346
"""
4447
读取用户收件箱中的邮件列表,仅返回ID、发件人和标题
@@ -50,11 +53,8 @@ def read_inbox(mailbox: str, unread_only: bool):
5053
owner_emails = [Email(**email) for email in email_data.get(mailbox, [])]
5154

5255
emails = [
53-
{
54-
"id": email.id,
55-
"sender": email.sender,
56-
"subject": email.subject
57-
} for email in owner_emails
56+
{"id": email.id, "sender": email.sender, "subject": email.subject}
57+
for email in owner_emails
5858
]
5959

6060
result = {
@@ -66,6 +66,7 @@ def read_inbox(mailbox: str, unread_only: bool):
6666

6767
return result
6868

69+
6970
def read_email(mailbox: str, email_id: str):
7071
"""
7172
读取邮件内容,仅返回邮件正文
@@ -85,6 +86,7 @@ def read_email(mailbox: str, email_id: str):
8586
else:
8687
raise Exception("invali email_id")
8788

89+
8890
def classify_email(email_text: str, keywords: str):
8991
"""
9092
根据指定的关键对单封邮件进行分类
@@ -101,6 +103,7 @@ def classify_email(email_text: str, keywords: str):
101103

102104
return result
103105

106+
104107
def forward_email(mailbox: str, email_id: str, recipient: str):
105108
"""
106109
将指定的单封邮件转发到目标邮箱
@@ -121,12 +124,13 @@ def forward_email(mailbox: str, email_id: str, recipient: str):
121124
"forwarded_email": {
122125
"email_id": email_id,
123126
"forwarded_to": recipient,
124-
"timestamp": datetime.now().isoformat()
127+
"timestamp": datetime.now().isoformat(),
125128
},
126-
"message": f"成功转发邮件 {email_id}{recipient}"
129+
"message": f"成功转发邮件 {email_id}{recipient}",
127130
}
128131
return result
129132

133+
130134
def generate_report(total: int, forwarded: int, receipient: str):
131135
"""
132136
生成任务执行总结报告
@@ -158,11 +162,12 @@ def generate_report(total: int, forwarded: int, receipient: str):
158162
"forwarded_count": forwarded,
159163
"target_email": receipient,
160164
"execution_success": success,
161-
}
165+
},
162166
}
163167

164168
return result
165169

170+
166171
class Colors:
167172
HEADER = "\033[37m"
168173
BLUE = "\033[94m"

01-tutorials/workshop/session3/E6b_mail_ast_without_guard/colors.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,41 @@ class Colors:
99
BOLD = "\033[1m"
1010
UNDERLINE = "\033[4m"
1111

12+
1213
def print_agent_reflect(text: str):
1314
"""打印智能体反思"""
1415
print(f"\n{Colors.CYAN}{Colors.BOLD}[智能体反思]{Colors.ENDC}")
1516
print(f"{Colors.CYAN}{text}{Colors.ENDC}")
1617

18+
1719
def print_agent_act(texts: list):
1820
"""打印智能体行动"""
1921
print(f"\n{Colors.YELLOW}{Colors.BOLD}[智能体行动]{Colors.ENDC}")
2022
for text in texts:
2123
print(f"{Colors.YELLOW}{text}{Colors.ENDC}")
2224

25+
2326
def print_agent_permission(texts: list):
2427
"""打印智能体权限"""
2528
print(f"\n{Colors.RED}{Colors.BOLD}[智能体权限]{Colors.ENDC}")
2629
for text in texts:
2730
print(f"{Colors.RED}{text}{Colors.ENDC}")
2831

32+
2933
def print_header(text: str):
3034
"""打印标题"""
3135
print(f"\n{Colors.HEADER}{Colors.BOLD}{'=' * 60}{Colors.ENDC}")
3236
print(f"{Colors.HEADER}{Colors.BOLD}{text.center(60)}{Colors.ENDC}")
3337
print(f"{Colors.HEADER}{Colors.BOLD}{'=' * 60}{Colors.ENDC}\n")
3438

39+
3540
def print_user_request(texts: list):
3641
"""打印用户请求"""
3742
print(f"\n{Colors.GREEN}{Colors.BOLD}[User]{Colors.ENDC}")
3843
for text in texts:
3944
print(f"{Colors.GREEN}{text}{Colors.ENDC}")
4045

46+
4147
def print_agent_respond(texts: list):
4248
"""打印智能体响应"""
4349
print(f"\n{Colors.BLUE}{Colors.BOLD}[Agent]{Colors.ENDC}")

01-tutorials/workshop/session3/E6b_mail_ast_without_guard/tools.py

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,18 @@
22
import json
33
import os
44

5+
56
# Mock数据结构
67
class Email:
7-
def __init__(self, id: str, sender: str, subject: str, body: str,
8-
received_date: str, priority: str = "normal"):
8+
def __init__(
9+
self,
10+
id: str,
11+
sender: str,
12+
subject: str,
13+
body: str,
14+
received_date: str,
15+
priority: str = "normal",
16+
):
917
self.id = id
1018
self.sender = sender
1119
self.subject = subject
@@ -20,18 +28,20 @@ def to_dict(self):
2028
"subject": self.subject,
2129
"body": self.body,
2230
"received_date": self.received_date,
23-
"priority": self.priority
31+
"priority": self.priority,
2432
}
2533

34+
2635
# 从JSON文件加载邮件数据
2736
# 获取当前文件的目录
2837
current_dir = os.path.dirname(os.path.abspath(__file__))
2938
# 构建emails.json的绝对路径
30-
email_file_path = os.path.join(current_dir, 'emails.json')
39+
email_file_path = os.path.join(current_dir, "emails.json")
3140

32-
with open(email_file_path, 'r', encoding='utf-8') as f:
41+
with open(email_file_path, "r", encoding="utf-8") as f:
3342
email_data = json.load(f)
3443

44+
3545
def read_inbox(mailbox: str, unread_only: bool):
3646
"""
3747
读取用户收件箱中的邮件列表,仅返回ID、发件人和标题
@@ -42,21 +52,21 @@ def read_inbox(mailbox: str, unread_only: bool):
4252
# 根据mailbox过滤邮件
4353
owner_emails = [Email(**email) for email in email_data.get(mailbox, [])]
4454

45-
emails = [{
46-
"id": email.id,
47-
"sender": email.sender,
48-
"subject": email.subject
49-
} for email in owner_emails]
55+
emails = [
56+
{"id": email.id, "sender": email.sender, "subject": email.subject}
57+
for email in owner_emails
58+
]
5059

5160
result = {
5261
"success": True,
5362
"count": len(emails),
5463
"emails": emails,
55-
"message": f"成功读取 {len(emails)} 封邮件基本信息"
64+
"message": f"成功读取 {len(emails)} 封邮件基本信息",
5665
}
5766

5867
return result
5968

69+
6070
def read_email(mailbox: str, email_id: str):
6171
"""
6272
读取邮件内容,仅返回邮件正文
@@ -76,6 +86,7 @@ def read_email(mailbox: str, email_id: str):
7686
else:
7787
raise Exception("invali email_id")
7888

89+
7990
def classify_email(email_text: str, keywords: str):
8091
"""
8192
根据指定的关键对单封邮件进行分类
@@ -87,11 +98,12 @@ def classify_email(email_text: str, keywords: str):
8798
result = {
8899
"success": True,
89100
"classification": classification,
90-
"message": f"成功分类邮件为 {classification} 优先级"
101+
"message": f"成功分类邮件为 {classification} 优先级",
91102
}
92103

93104
return result
94105

106+
95107
def forward_email(mailbox: str, email_id: str, recipient: str):
96108
"""
97109
将指定的单封邮件转发到目标邮箱
@@ -112,12 +124,13 @@ def forward_email(mailbox: str, email_id: str, recipient: str):
112124
"forwarded_email": {
113125
"email_id": email_id,
114126
"forwarded_to": recipient,
115-
"timestamp": datetime.now().isoformat()
127+
"timestamp": datetime.now().isoformat(),
116128
},
117-
"message": f"成功转发邮件 {email_id}{recipient}"
129+
"message": f"成功转发邮件 {email_id}{recipient}",
118130
}
119131
return result
120132

133+
121134
def generate_report(total: int, forwarded: int, receipient: str):
122135
"""
123136
生成任务执行总结报告
@@ -132,13 +145,13 @@ def generate_report(total: int, forwarded: int, receipient: str):
132145
# 生成报告
133146
report = f"""
134147
===== 邮件处理执行报告 =====
135-
生成时间: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}
148+
生成时间: {datetime.now().strftime("%Y-%m-%d %H:%M:%S")}
136149
137150
📊 处理统计:
138151
- 总邮件数: {total}
139152
- 转发邮件数: {forwarded}
140153
- 目标邮箱: {receipient}
141-
- 执行状态: {'✅ 成功' if success else '❌ 失败'}
154+
- 执行状态: {"✅ 成功" if success else "❌ 失败"}
142155
"""
143156

144157
result = {
@@ -148,19 +161,20 @@ def generate_report(total: int, forwarded: int, receipient: str):
148161
"total_emails": total,
149162
"forwarded_count": forwarded,
150163
"target_email": receipient,
151-
"execution_success": success
152-
}
164+
"execution_success": success,
165+
},
153166
}
154167

155168
return result
156169

170+
157171
class Colors:
158-
HEADER = '\033[95m'
159-
BLUE = '\033[94m'
160-
CYAN = '\033[96m'
161-
GREEN = '\033[92m'
162-
YELLOW = '\033[93m'
163-
RED = '\033[91m'
164-
ENDC = '\033[0m'
165-
BOLD = '\033[1m'
166-
UNDERLINE = '\033[4m'
172+
HEADER = "\033[95m"
173+
BLUE = "\033[94m"
174+
CYAN = "\033[96m"
175+
GREEN = "\033[92m"
176+
YELLOW = "\033[93m"
177+
RED = "\033[91m"
178+
ENDC = "\033[0m"
179+
BOLD = "\033[1m"
180+
UNDERLINE = "\033[4m"

0 commit comments

Comments
 (0)