File size: 5,554 Bytes
6b0fb19
 
 
 
 
977a36a
 
 
6b0fb19
 
 
 
 
 
 
 
977a36a
a21addb
977a36a
 
6b0fb19
 
 
c254073
9e51ade
1820e22
 
a21addb
932c41e
1820e22
977a36a
932c41e
6b0fb19
977a36a
6b0fb19
 
 
977a36a
 
 
 
6b0fb19
 
 
977a36a
6b0fb19
977a36a
 
6b0fb19
977a36a
 
6b0fb19
977a36a
 
6b0fb19
 
977a36a
 
 
 
 
6b0fb19
977a36a
 
 
 
 
 
 
 
6b0fb19
 
977a36a
 
 
 
6b0fb19
 
 
977a36a
 
 
 
6b0fb19
977a36a
 
6b0fb19
 
977a36a
 
 
 
 
6b0fb19
 
977a36a
 
 
 
6b0fb19
 
977a36a
 
 
 
 
6b0fb19
 
977a36a
 
 
 
6b0fb19
 
977a36a
 
 
 
6b0fb19
 
977a36a
6b0fb19
977a36a
 
6b0fb19
 
977a36a
6b0fb19
 
977a36a
 
 
 
6b0fb19
 
977a36a
 
 
6b0fb19
977a36a
6b0fb19
 
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
from datetime import datetime
from sqlalchemy import (
    Column,
    Integer,
    String,
    Boolean,
    DateTime,
    ForeignKey,
    Text,
    JSON,
)
from sqlalchemy.orm import relationship

from app.db import Base


# ============================================================
# USERS
# ============================================================
class User(Base):
    __tablename__ = "users"

    id = Column(Integer, primary_key=True, index=True)
    full_name = Column(String, nullable=True)          # İsim (opsiyonel)
    email = Column(String, unique=True, index=True, nullable=False)
    password_hash = Column(String, nullable=False)

    # Sadece basit durumlar için yeterli alanlar
    is_active = Column(Boolean, default=True)

    created_at = Column(DateTime, default=datetime.utcnow)
    last_login = Column(DateTime, nullable=True)

    workflows = relationship("Workflow", back_populates="owner")
    chat_sessions = relationship("ChatSession", back_populates="user")


# ============================================================
# WORKFLOWS – Kullanıcıların oluşturduğu n8n benzeri akışlar
# ============================================================
class Workflow(Base):
    __tablename__ = "workflows"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False)
    description = Column(Text, nullable=True)
    graph_json = Column(JSON, nullable=False)     # tüm node/edge yapısı
    is_active = Column(Boolean, default=True)

    owner_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    owner = relationship("User", back_populates="workflows")

    created_at = Column(DateTime, default=datetime.utcnow)
    updated_at = Column(DateTime, default=datetime.utcnow)


# ============================================================
# WORKFLOW RUNS – Workflow çalıştırma geçmişi
# ============================================================
class WorkflowRun(Base):
    __tablename__ = "workflow_runs"

    id = Column(Integer, primary_key=True, index=True)
    workflow_id = Column(Integer, ForeignKey("workflows.id"))
    status = Column(String, default="running")  # running | success | failed
    input_data = Column(JSON, nullable=True)
    output_data = Column(JSON, nullable=True)
    error_message = Column(Text, nullable=True)
    started_at = Column(DateTime, default=datetime.utcnow)
    finished_at = Column(DateTime, nullable=True)


# ============================================================
# NODES – Her workflow içindeki tek tek node’lar
# ============================================================
class WorkflowNode(Base):
    __tablename__ = "workflow_nodes"

    id = Column(Integer, primary_key=True, index=True)
    workflow_id = Column(Integer, ForeignKey("workflows.id"))
    node_id = Column(String, nullable=False)       # frontend'deki unique node key
    type = Column(String, nullable=False)          # ai, http, timer, condition...
    config = Column(JSON, nullable=True)           # node'un ayarları (prompt, url, headers)

    position_x = Column(Integer, default=0)
    position_y = Column(Integer, default=0)


# ============================================================
# INTEGRATION PROVIDERS — OpenAI, HF, Slack, Gmail vs.
# ============================================================
class IntegrationProvider(Base):
    __tablename__ = "integration_providers"

    id = Column(Integer, primary_key=True, index=True)
    name = Column(String, nullable=False)            # "openai", "huggingface", "slack"
    category = Column(String, nullable=True)         # "llm", "storage", "messaging"
    base_url = Column(String, nullable=True)
    description = Column(Text, nullable=True)


# ============================================================
# USER CREDENTIALS – Kullanıcı kendi API keylerini girer
# ============================================================
class UserCredential(Base):
    __tablename__ = "user_credentials"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    provider_id = Column(Integer, ForeignKey("integration_providers.id"))
    credential_json = Column(JSON, nullable=False)   # {"api_key": "...", "org_id": "..."}
    created_at = Column(DateTime, default=datetime.utcnow)


# ============================================================
# CHAT SESSION — Chat ara yüzü konuşmaları tutar
# ============================================================
class ChatSession(Base):
    __tablename__ = "chat_sessions"

    id = Column(Integer, primary_key=True, index=True)
    user_id = Column(Integer, ForeignKey("users.id"), nullable=True)
    title = Column(String, nullable=True)
    created_at = Column(DateTime, default=datetime.utcnow)

    user = relationship("User", back_populates="chat_sessions")
    messages = relationship("ChatMessage", back_populates="session")


# ============================================================
# CHAT MESSAGE – Chat içerisindeki mesajlar
# ============================================================
class ChatMessage(Base):
    __tablename__ = "chat_messages"

    id = Column(Integer, primary_key=True, index=True)
    session_id = Column(Integer, ForeignKey("chat_sessions.id"))
    role = Column(String, nullable=False)  # user | assistant | system
    content = Column(Text, nullable=False)
    timestamp = Column(DateTime, default=datetime.utcnow)

    session = relationship("ChatSession", back_populates="messages")