Gaykar commited on
Commit
4a43592
·
1 Parent(s): 7f4fd1e

added context manager to remove errors

Browse files
app/nodes/archive_node.py CHANGED
@@ -2,8 +2,13 @@ from app.state.state import EmailAgentState
2
  from langchain_core.runnables.config import RunnableConfig
3
  from app.database.connection import get_session
4
  from app.database.utils import save_received_email
 
5
 
6
 
 
 
 
 
7
  def archive_node(state: EmailAgentState,config: RunnableConfig) -> dict:
8
 
9
  print(f"[ARCHIVE] {state['triage_label']} — {state['sender_subject']}")
@@ -15,7 +20,7 @@ def archive_node(state: EmailAgentState,config: RunnableConfig) -> dict:
15
 
16
  thread_id = config.get("configurable", {}).get("thread_id")
17
 
18
- with get_session() as session:
19
  try:
20
  # 2. Persist the received email even if it's unsafe (for records/logging)
21
  save_received_email(session, user_id, thread_id, state)
@@ -31,4 +36,10 @@ def archive_node(state: EmailAgentState,config: RunnableConfig) -> dict:
31
  raise e
32
 
33
 
34
- return {}
 
 
 
 
 
 
 
2
  from langchain_core.runnables.config import RunnableConfig
3
  from app.database.connection import get_session
4
  from app.database.utils import save_received_email
5
+ from contextlib import contextmanager
6
 
7
 
8
+ # A Context Manager is your code's automatic cleanup crew. Its only job is to open a resource when you need it, and make sure it gets closed the exact moment you are done
9
+ session_context=contextmanager(get_session)
10
+
11
+ # We are converting your simple get_session function into a fully functioning Python Context Manager.
12
  def archive_node(state: EmailAgentState,config: RunnableConfig) -> dict:
13
 
14
  print(f"[ARCHIVE] {state['triage_label']} — {state['sender_subject']}")
 
20
 
21
  thread_id = config.get("configurable", {}).get("thread_id")
22
 
23
+ with session_context() as session:
24
  try:
25
  # 2. Persist the received email even if it's unsafe (for records/logging)
26
  save_received_email(session, user_id, thread_id, state)
 
36
  raise e
37
 
38
 
39
+ return {}
40
+
41
+
42
+
43
+
44
+ # A Context Manager is a brilliant Python tool that automatically handles the setup and cleanup of a resource for you. It guarantees that no matter what happens inside your code—even if an unexpected error crashes the system—your resources (like database connections or files) are safely closed and cleaned up.
45
+ # A context manager is a Python utility used to allocate and release resources precisely when we want them to. By wrapping our database generator with @contextmanager, it allows us to use the standard with statement. This guarantees resource safety, ensuring that our database session is automatically and cleanly closed in the finally block, even if our core logic throws a runtime exception.
app/nodes/store_memory_data_node.py CHANGED
@@ -8,6 +8,10 @@ from app.database.connection import get_session
8
  from app.database.utils import save_sent_email, save_received_email
9
  from langchain_core.messages import AIMessage
10
  from app.utils.token_utils import *
 
 
 
 
11
 
12
  def store_memory_and_data_node(state: EmailAgentState, config: RunnableConfig):
13
  """
@@ -52,7 +56,7 @@ def store_memory_and_data_node(state: EmailAgentState, config: RunnableConfig):
52
  thread_id = config.get("configurable", {}).get("thread_id")
53
 
54
  # Using 'with' handles opening/closing even if an error occurs
55
- with get_session() as session:
56
  try:
57
  save_sent_email(session, sender_id, thread_id, state)
58
  save_received_email(session, sender_id, thread_id, state)
 
8
  from app.database.utils import save_sent_email, save_received_email
9
  from langchain_core.messages import AIMessage
10
  from app.utils.token_utils import *
11
+ from contextlib import contextmanager
12
+
13
+
14
+ session_context=contextmanager(get_session)
15
 
16
  def store_memory_and_data_node(state: EmailAgentState, config: RunnableConfig):
17
  """
 
56
  thread_id = config.get("configurable", {}).get("thread_id")
57
 
58
  # Using 'with' handles opening/closing even if an error occurs
59
+ with session_context() as session:
60
  try:
61
  save_sent_email(session, sender_id, thread_id, state)
62
  save_received_email(session, sender_id, thread_id, state)
app/nodes/summarise_email_body_node.py CHANGED
@@ -11,12 +11,8 @@ from app.utils.token_utils import summarise_email_body
11
 
12
  def summarise_email_body_node(state:EmailAgentState)->dict:
13
 
14
-
15
  subject=state['sender_subject']
16
  body=state['sender_email_body']
17
-
18
-
19
-
20
  summary=summarise_email_body(body)
21
 
22
  return {"sender_email_body":summary}
 
11
 
12
  def summarise_email_body_node(state:EmailAgentState)->dict:
13
 
 
14
  subject=state['sender_subject']
15
  body=state['sender_email_body']
 
 
 
16
  summary=summarise_email_body(body)
17
 
18
  return {"sender_email_body":summary}
app/nodes/unsafe_email_node.py CHANGED
@@ -2,6 +2,9 @@ from app.state.state import EmailAgentState
2
  from langchain_core.runnables.config import RunnableConfig
3
  from app.database.connection import get_session
4
  from app.database.utils import save_received_email
 
 
 
5
 
6
  def unsafe_emails_node(state: EmailAgentState, config: RunnableConfig) -> dict:
7
  """
@@ -14,7 +17,7 @@ def unsafe_emails_node(state: EmailAgentState, config: RunnableConfig) -> dict:
14
  user_id = state['user_id']
15
  thread_id = config.get("configurable", {}).get("thread_id")
16
 
17
- with get_session() as session:
18
  try:
19
  # 2. Persist the received email even if it's unsafe (for records/logging)
20
  save_received_email(session, user_id, thread_id, state)
 
2
  from langchain_core.runnables.config import RunnableConfig
3
  from app.database.connection import get_session
4
  from app.database.utils import save_received_email
5
+ from contextlib import contextmanager
6
+
7
+ session_context=contextmanager(get_session)
8
 
9
  def unsafe_emails_node(state: EmailAgentState, config: RunnableConfig) -> dict:
10
  """
 
17
  user_id = state['user_id']
18
  thread_id = config.get("configurable", {}).get("thread_id")
19
 
20
+ with session_context() as session:
21
  try:
22
  # 2. Persist the received email even if it's unsafe (for records/logging)
23
  save_received_email(session, user_id, thread_id, state)