File size: 8,580 Bytes
0210631
 
4d860a2
 
 
57d78b2
4d860a2
 
 
d9012b6
 
4d860a2
 
 
57d78b2
33ffec6
 
3d38571
33ffec6
44513b8
972c80f
 
33ffec6
 
 
 
aa34f6b
4d860a2
aa34f6b
57d78b2
 
 
 
 
 
 
4d860a2
aa34f6b
4d860a2
aa34f6b
4d860a2
 
 
 
 
 
 
 
 
33ffec6
4d860a2
 
 
57d78b2
 
 
4d860a2
 
3d38571
 
 
4d860a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
33ffec6
 
 
 
 
 
 
 
 
 
 
 
 
 
4d860a2
33ffec6
4d860a2
 
44c6641
33ffec6
4d860a2
 
 
 
 
3d38571
 
4d860a2
 
 
 
33ffec6
 
4d860a2
 
 
 
 
 
44513b8
 
 
 
 
 
 
4d860a2
 
 
 
 
 
 
 
 
 
57d78b2
4d860a2
 
 
 
 
 
 
 
 
 
 
 
 
d9012b6
 
4d860a2
44513b8
4d860a2
 
 
 
33ffec6
4d860a2
 
 
 
 
 
5352094
477695a
4d860a2
 
d9012b6
477695a
 
 
 
 
 
4d860a2
 
 
 
 
 
 
 
 
 
 
 
57d78b2
4d860a2
 
57d78b2
 
4d860a2
 
 
 
 
57d78b2
4d860a2
 
 
 
 
 
 
 
 
57d78b2
4d860a2
 
57d78b2
 
 
 
4d860a2
 
 
 
44513b8
4d860a2
 
 
070a87d
57d78b2
33ffec6
57d78b2
 
 
 
5352094
57d78b2
 
 
477695a
 
 
 
 
 
 
57d78b2
 
 
 
33ffec6
57d78b2
 
 
 
 
 
 
 
 
 
972c80f
 
 
 
 
 
 
 
e870dd4
070a87d
e870dd4
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
import os
from app.core.config import settings
from fastapi import FastAPI, HTTPException, Depends
from pydantic import BaseModel, EmailStr
from typing import Optional, Dict, Any, TypedDict, Annotated, Sequence
from langchain_core.messages import BaseMessage, HumanMessage
from langgraph.graph import add_messages
from langgraph.types import Command
import logging
from app.graph import graph
from app.state.state import EmailAgentState
from app.database.connection import get_session
from app.database.utils import get_or_create_user
from sqlalchemy.orm import Session
from app.database.connection import SessionLocal
from fastapi import Request
from app.database.models import User
from app.utils.email_encode  import encode_email_for_namespace
from app.core.auth import create_access_token,get_current_user
import traceback
import gradio as gr
from app.gradio_ui import build_demo 

# CREATE GMAIL AUTH FILES FROM HF SECRETS



logger = logging.getLogger(__name__)

def get_session():
    db = SessionLocal()
    try:
        yield db
    finally:
        db.close()

app = FastAPI(title="AI Email Agent API")

# --- Schemas ---

class EmailProcessRequest(BaseModel):
    thread_id: str
    sender_email_id: EmailStr
    sender_subject: str
    sender_email_body: str


class ReviewActionRequest(BaseModel):
    thread_id: str
    
    status: str  # "approved" or "rejected"
    feedback: Optional[str] = None

class SendEmailRequest(BaseModel):
    thread_id: str
    human_message: str
# --- Helper Functions ---




def parse_interrupt(final_state: Dict[str, Any]) -> Optional[Dict[str, Any]]:
    """Parse interrupt from graph state."""
    if "__interrupt__" not in final_state:
        return None

    interrupt_state = final_state.get("__interrupt__")
    if not interrupt_state:
        return None

    interrupt = interrupt_state[0]
    value = getattr(interrupt, "value", {}) or {}

    return {
        "action": value.get("action"),
        "data": value.get("data", {})
    }


# --- Endpoints ---


@app.post("/get-user-data")
def get_user_data(user_email: EmailStr, db: Session = Depends(get_session)):
    """Get user data by email."""
    user = get_or_create_user(db, user_email)

    token = create_access_token({
            "id": user.id,
            "email": user_email
        })
    return {"user_id": str(user.id), "email": user.email, "token": token}



@app.post("/process-email")
def process_email(request: EmailProcessRequest, db: Session = Depends(get_session), current_user: User = Depends(get_current_user)) -> Dict[str, Any]:
    """Process email through the graph pipeline."""
    
    try:
        
        
        thread_id = request.thread_id
        config = {
            "configurable": {
                "thread_id": thread_id,
                "user_id": str(current_user.id),
                "sender_email_id": encode_email_for_namespace(request.sender_email_id ),
            }
        }
        
        input_data = {
            "user_email_id": current_user.email,
            "user_id": current_user.id,
            "user_name": "Atharva",
            "sender_email_id": request.sender_email_id,
            "sender_subject": request.sender_subject,
            "sender_email_body": request.sender_email_body,
        }

        try:

            final_state = graph.invoke(input_data, config=config)
        except Exception as e:
            logger.error(f"Error invoking graph: {str(e)}")
            traceback.print_exc()
            raise HTTPException(status_code=500, detail=f"Graph invocation error: {str(e)}")

        if final_state.get('triage_label') == "FOLLOW_UP_REQUIRED":
            if "__interrupt__" in final_state and not final_state.get("draft_id"):
                parsed_interrupt = parse_interrupt(final_state)
                if parsed_interrupt:
                    data = parsed_interrupt["data"]

                    return {
                        "status": "needs_review",
                        "thread_id": thread_id,
                        "messages": final_state.get("messages", []),
                        "triage_label": final_state.get("triage_label"),
                        "action": parsed_interrupt["action"],
                        "email_draft": {
                            "to": data.get("to"),
                            "subject": data.get("subject"),
                            "body": data.get("body"),
                        }
                    }

        return {
            "thread_id": thread_id,
            "triage_label": final_state.get("triage_label"),
        }

    except Exception as e:
        logger.error(f"Error processing email: {str(e)}")
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))


@app.post("/review-action")
def review_action(request: ReviewActionRequest,db: Session = Depends(get_session), current_user: User = Depends(get_current_user)) -> Dict[str, Any]:
    """Resume graph execution based on user review."""
    
    try:
        config = {
            "configurable": {
                "thread_id": request.thread_id,
                "user_id": str(current_user.id),
                
            }
        }

        state=graph.get_state(config)

        sender_email_id=state.values['sender_email_id']

        config["configurable"]["sender_email_id"] = encode_email_for_namespace(sender_email_id)

        if request.status == "rejected":
            payload = Command(resume={
                "status": "rejected",
                "feedback": request.feedback
            })
        elif request.status == "approved":
            payload = Command(resume={
                "status": "approved"
            })
        else:
            raise HTTPException(status_code=400, detail="Invalid status")

        intermediate_state = graph.invoke(payload, config=config)
        
        # Still in review phase
        if "__interrupt__" in intermediate_state and not intermediate_state.get("draft_id"):
            parsed_interrupt = parse_interrupt(intermediate_state)
            if parsed_interrupt:
                data = parsed_interrupt["data"]
                return {
                    "status": "needs_review",
                    "thread_id": request.thread_id,
                    "triage_label": intermediate_state.get("triage_label"),
                    "action": parsed_interrupt["action"],
                    "email_draft": {
                        "to": data.get("to"),
                        "subject": data.get("subject"),
                        "body": data.get("body"),
                    }
                }
        
        # Draft created, review complete
        if intermediate_state.get("draft_id"):
            return {
                "thread_id": request.thread_id,
                "draft_id": intermediate_state["draft_id"],
                "messages": intermediate_state.get("messages", []),
                "reply_subject": intermediate_state.get("reply_subject"),
                "reply_email_body": intermediate_state.get("reply_email_body"),
            }

    except Exception as e:
        logger.error(f"Error in review action: {str(e)}")
        traceback.print_exc()
        raise HTTPException(status_code=500, detail=str(e))



@app.post("/send_email")
def send_email(request: SendEmailRequest,db: Session = Depends(get_session),current_user: User = Depends(get_current_user)) -> Dict[str, Any]:
   
        config = {
            "configurable": {
                "thread_id": request.thread_id,
                "user_id": str(current_user.id),
            }
        }


        state=graph.get_state(config)

        sender_email_id=state.values['sender_email_id']

        config["configurable"]["sender_email_id"] = encode_email_for_namespace(sender_email_id)

        graph.update_state(
          config,
          {"messages": [HumanMessage(content=request.human_message)]},
       as_node="prepare_context_node" 
        )  
        final_state = graph.invoke(None, config=config)

        return {
            "thread_id": request.thread_id,
            "messages": final_state.get("messages", []),
            "sent_message_id": final_state.get("sent_message_id")
        }



  # adjust import path to wherever you place gradio_ui.py


demo = build_demo()
 
# Mounts the Gradio UI at /ui  →  e.g. https://vinit006-emailagentwithmemory.hf.space/ui
app = gr.mount_gradio_app(app, demo, path="/ui")

# used for local testing

# if __name__ == "__main__":
#     import uvicorn
#     uvicorn.run(app, host="127.0.0.1", port=8080)