File size: 13,446 Bytes
b0c3c39
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# requirements: fastapi, uvicorn, beanie, motor, pydantic, passlib[bcrypt], python-dotenv, jose, mistralai
from fastapi import FastAPI, Depends, HTTPException, status, Path, Request
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import JSONResponse, StreamingResponse
from jose import JWTError, jwt
from passlib.context import CryptContext
from datetime import datetime, timedelta
from typing import Optional, List, Dict, Any
from pydantic import BaseModel, EmailStr, ValidationError
import os
from dotenv import load_dotenv
from beanie import Document, init_beanie
from motor.motor_asyncio import AsyncIOMotorClient

# Mistral API
from mistralai import Mistral
import uvicorn

# Load environment variables
load_dotenv()

MONGODB_URL = os.getenv("MONGODB_URL", "mongodb://localhost:27017")
DATABASE_NAME = os.getenv("DATABASE_NAME", "mindmap_db")
SECRET_KEY = os.getenv("SECRET_KEY")
MISTRAL_API_KEY = os.getenv("MISTRAL_API_KEY")
ALLOWED_ORIGINS = os.getenv("CORS_ORIGINS", "http://localhost:3000").split(",")

if not SECRET_KEY:
    raise RuntimeError("SECRET_KEY environment variable must be set for security!")

ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30

# Initialize FastAPI app
app = FastAPI(title="MindMapX API")

# CORS configuration
app.add_middleware(
    CORSMiddleware,
    allow_origins=ALLOWED_ORIGINS,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)

# Add rate limiting
try:
    from slowapi import Limiter, _rate_limit_exceeded_handler
    from slowapi.util import get_remote_address
    limiter = Limiter(key_func=get_remote_address)
    app.state.limiter = limiter
    app.add_exception_handler(429, _rate_limit_exceeded_handler)
except ImportError:
    limiter = None

# Add validation error handler
@app.exception_handler(ValidationError)
async def validation_exception_handler(request: Request, exc: ValidationError):
    return JSONResponse(
        status_code=422,
        content={"detail": [{"msg": str(err), "loc": err["loc"]} for err in exc.errors()]},
    )

@app.exception_handler(HTTPException)
async def http_exception_handler(request: Request, exc: HTTPException):
    return JSONResponse(
        status_code=exc.status_code,
        content={"detail": exc.detail},
    )

# Security configuration
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/login")

# Mistral client
client = Mistral(api_key=MISTRAL_API_KEY) if MISTRAL_API_KEY else None

# Models
class UserBase(BaseModel):
    username: str
    email: Optional[EmailStr] = None

class UserCreate(UserBase):
    password: str

class User(Document, UserBase):
    hashed_password: str
    created_at: datetime
    updated_at: datetime

    class Settings:
        name = "users"
        indexes = ["username", "email"]

class Node(BaseModel):
    id: str
    data: Dict[str, Any]
    position: Dict[str, float]

class Edge(BaseModel):
    id: str
    source: str
    target: str

class MindMapBase(BaseModel):
    name: str = "Untitled"
    nodes: List[Node] = []
    edges: List[Edge] = []

class MindMap(Document, MindMapBase):
    user_id: str
    created_at: datetime
    updated_at: datetime

    class Settings:
        name = "mindmaps"
        indexes = ["user_id", "created_at"]

# Token models
class Token(BaseModel):
    access_token: str
    token_type: str

class TokenData(BaseModel):
    username: Optional[str] = None

# DB init
async def init_db():
    client = AsyncIOMotorClient(MONGODB_URL)
    await init_beanie(
        database=client[DATABASE_NAME],
        document_models=[User, MindMap],
    )

# Helper functions
def verify_password(plain_password: str, hashed_password: str) -> bool:
    return pwd_context.verify(plain_password, hashed_password)

def get_password_hash(password: str) -> str:
    return pwd_context.hash(password)

async def get_user(username: str) -> Optional[User]:
    return await User.find_one({"username": username})

async def authenticate_user(username: str, password: str) -> Optional[User]:
    user = await get_user(username)
    if not user:
        return None
    if not verify_password(password, user.hashed_password):
        return None
    return user

def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
    to_encode = data.copy()
    if expires_delta:
        expire = datetime.utcnow() + expires_delta
    else:
        expire = datetime.utcnow() + timedelta(minutes=15)
    to_encode.update({"exp": expire})
    encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
    return encoded_jwt

async def get_current_user(token: str = Depends(oauth2_scheme)) -> User:
    credentials_exception = HTTPException(
        status_code=status.HTTP_401_UNAUTHORIZED,
        detail="Could not validate credentials",
        headers={"WWW-Authenticate": "Bearer"},
    )
    try:
        payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
        username: str = payload.get("sub")
        if username is None:
            raise credentials_exception
        token_data = TokenData(username=username)
    except JWTError:
        raise credentials_exception
    user = await get_user(username=token_data.username)
    if user is None:
        raise credentials_exception
    return user

# Startup event
@app.on_event("startup")
async def on_startup():
    await init_db()

# Auth endpoints
@app.post("/signup", response_model=Token)
async def signup(user: UserCreate):
    existing_user = await User.find_one({"username": user.username})
    if existing_user:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="Username already registered",
        )
    if user.email:
        existing_email = await User.find_one({"email": user.email})
        if existing_email:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="Email already registered",
            )
    hashed_password = get_password_hash(user.password)
    new_user = User(
        username=user.username,
        email=user.email,
        hashed_password=hashed_password,
        created_at=datetime.utcnow(),
        updated_at=datetime.utcnow(),
    )
    await new_user.insert()
    access_token = create_access_token(
        data={"sub": user.username},
        expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
    )
    return {"access_token": access_token, "token_type": "bearer"}
    
@app.post("/login", response_model=Token)
async def login(form_data: OAuth2PasswordRequestForm = Depends()):
    user = await authenticate_user(form_data.username, form_data.password)
    if not user:
        raise HTTPException(
            status_code=status.HTTP_401_UNAUTHORIZED,
            detail="Incorrect username or password",
            headers={"WWW-Authenticate": "Bearer"},
        )
    access_token = create_access_token(
        data={"sub": user.username},
        expires_delta=timedelta(minutes=ACCESS_TOKEN_EXPIRE_MINUTES),
    )
    return {"access_token": access_token, "token_type": "bearer"}
    
@app.post("/logout")
async def logout(current_user: User = Depends(get_current_user)):
    return {"message": "Successfully logged out"}

# MindMap CRUD endpoints (all under /api/workflows)
@app.get("/api/workflows", response_model=List[Dict[str, Any]])
async def get_workflows(current_user: User = Depends(get_current_user)):
    mindmaps = await MindMap.find({"user_id": current_user.username}).to_list()
    return [
        {
            "id": str(m.id),
            "name": m.name,
            "nodes": m.nodes,
            "edges": m.edges,
            "updatedAt": m.updated_at.isoformat() if m.updated_at else None,
            "createdAt": m.created_at.isoformat() if m.created_at else None,
        }
        for m in mindmaps
    ]

@app.post("/api/workflows", response_model=Dict[str, Any])
async def create_workflow(

    workflow: MindMapBase, current_user: User = Depends(get_current_user)

):
    # Validate unique node IDs
    node_ids = [node.id for node in workflow.nodes]
    if len(node_ids) != len(set(node_ids)):
        raise HTTPException(status_code=400, detail="Duplicate node IDs are not allowed")
    new_map = MindMap(
        user_id=current_user.username,
        name=workflow.name,
        nodes=workflow.nodes,
        edges=workflow.edges,
        created_at=datetime.utcnow(),
        updated_at=datetime.utcnow(),
    )
    await new_map.insert()
    return {
        "id": str(new_map.id),
        "name": new_map.name,
        "nodes": new_map.nodes,
        "edges": new_map.edges,
        "updatedAt": new_map.updated_at.isoformat() if new_map.updated_at else None,
        "createdAt": new_map.created_at.isoformat() if new_map.created_at else None,
    }

@app.get("/api/workflows/{workflow_id}", response_model=Dict[str, Any])
async def get_workflow(

    workflow_id: str, current_user: User = Depends(get_current_user)

):
    mind_map = await MindMap.get(workflow_id)
    if not mind_map or mind_map.user_id != current_user.username:
        raise HTTPException(status_code=404, detail="Workflow not found")
    return {
        "id": str(mind_map.id),
        "name": mind_map.name,
        "nodes": mind_map.nodes,
        "edges": mind_map.edges,
        "updatedAt": mind_map.updated_at.isoformat() if mind_map.updated_at else None,
        "createdAt": mind_map.created_at.isoformat() if mind_map.created_at else None,
    }

@app.put("/api/workflows/{workflow_id}", response_model=Dict[str, Any])
async def update_workflow(

    workflow_id: str,

    workflow: MindMapBase,

    current_user: User = Depends(get_current_user),

):
    mind_map = await MindMap.get(workflow_id)
    if not mind_map or mind_map.user_id != current_user.username:
        raise HTTPException(status_code=404, detail="Workflow not found")
    # Validate unique node IDs
    node_ids = [node.id for node in workflow.nodes]
    if len(node_ids) != len(set(node_ids)):
        raise HTTPException(status_code=400, detail="Duplicate node IDs are not allowed")
    mind_map.name = workflow.name
    mind_map.nodes = workflow.nodes
    mind_map.edges = workflow.edges
    mind_map.updated_at = datetime.utcnow()
    await mind_map.save()
    return {
        "id": str(mind_map.id),
        "name": mind_map.name,
        "nodes": mind_map.nodes,
        "edges": mind_map.edges,
        "updatedAt": mind_map.updated_at.isoformat() if mind_map.updated_at else None,
        "createdAt": mind_map.created_at.isoformat() if mind_map.created_at else None,
    }

@app.delete("/api/workflows/{workflow_id}")
async def delete_workflow(

    workflow_id: str, current_user: User = Depends(get_current_user)

):
    mind_map = await MindMap.get(workflow_id)
    if not mind_map or mind_map.user_id != current_user.username:
        raise HTTPException(status_code=404, detail="Workflow not found")
    await mind_map.delete()
    return {"message": "Workflow deleted successfully"}

# Mistral API endpoints
class ChatMessage(BaseModel):
    role: str  # Options: 'user', 'assistant', 'system'
    content: str

class ChatRequest(BaseModel):
    messages: List[ChatMessage]
    model: str = "mistral-large-latest"
    stream: bool = False
    safe_prompt: Optional[bool] = False
    stop: Optional[List[str]] = None

@app.get("/models")
def list_models():
    if not client:
        raise HTTPException(status_code=500, detail="Mistral API key not configured.")
    models = client.models.list()
    return {"models": [model.id for model in models.data]}

@app.post("/chat")
async def chat(request: ChatRequest):
    if not client:
        raise HTTPException(status_code=500, detail="Mistral API key not configured.")
    # If streaming is requested
    if request.stream:
        def stream_response():
            response = client.chat.stream(
                model=request.model,
                messages=[msg.dict() for msg in request.messages],
                safe_prompt=request.safe_prompt,
                stop=request.stop
            )
            for chunk in response:
                content = chunk.data.choices[0].delta.content
                if content:
                    yield content
        return StreamingResponse(stream_response(), media_type="text/plain")
    # Non-streaming (standard) completion
    else:
        response = client.chat.complete(
            model=request.model,
            messages=[msg.dict() for msg in request.messages],
            safe_prompt=request.safe_prompt,
            stop=request.stop
        )
        return JSONResponse({
            "response": response.choices[0].message.content
        })

# Misc endpoints
@app.get("/")
async def root():
    return {"message": "Mind Map API"}

@app.get("/health")
async def health():
    return {"status": "ok"}

if __name__ == "__main__":
    import uvicorn
    uvicorn.run(app, host="0.0.0.0", port=8000)