xuyonghao
2024-12-27 fdbd37eb2516b67ca41b0b71b738e3368e2825fe
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
import json
import os
from pickle import PROTO
from typing import Dict, List, Tuple
 
from sqlalchemy import create_engine, Column, String, Integer, Text
from sqlalchemy.exc import IntegrityError
from sqlalchemy.orm import sessionmaker, Session
 
from app.config.config import settings
from app.config.const import RAGFLOW, BISHENG, DIFY, ENV_CONF_PATH
from app.models import KnowledgeModel
from app.models.dialog_model import DialogModel
from app.models.user_model import UserAppModel
from app.models.agent_model import AgentModel
from app.models.base_model import SessionLocal, Base
from app.models.resource_model import ResourceModel, ResourceTypeModel
from app.service.v2.app_register import AppRegisterDao
 
# 创建数据库引擎和会话工厂
engine_bisheng = create_engine(settings.sgb_db_url)
engine_ragflow = create_engine(settings.fwr_db_url)
engine_dify = create_engine(settings.dify_database_url)
 
SessionBisheng = sessionmaker(autocommit=False, autoflush=False, bind=engine_bisheng)
SessionRagflow = sessionmaker(autocommit=False, autoflush=False, bind=engine_ragflow)
SessionDify = sessionmaker(autocommit=False, autoflush=False, bind=engine_dify)
 
 
class Flow(Base):
    __tablename__ = 'flow'
    id = Column(String(255), primary_key=True)
    name = Column(String(255), nullable=False)
    status = Column(Integer, nullable=False)
    description = Column(String(255), nullable=False)
    user_id = Column(Integer, nullable=False)
 
 
class Dialog(Base):
    __tablename__ = 'dialog'
    id = Column(String(255), primary_key=True)
    name = Column(String(255), nullable=False)
    status = Column(String(1), nullable=False)
    description = Column(String(255), nullable=False)
    tenant_id = Column(String(36), nullable=False)
 
 
class DfApps(Base):
    __tablename__ = 'apps'
    id = Column(String(36), primary_key=True)
    name = Column(String(255), nullable=False)
    status = Column(String(16), nullable=False)
    description = Column(Text, nullable=False)
    tenant_id = Column(String(36), nullable=False)
    mode = Column(String(36), nullable=False)
 
 
class RgKnowledge(Base):
    __tablename__ = 'knowledgebase'
    id = Column(String(36), primary_key=True)  # id
    name = Column(String(128))  # 名称
    permission = Column(String(32), default="me")
    tenant_id = Column(String(32))  # 创建人id
    description = Column(Text)  # 说明
    status = Column(String(1))  # 状态
    doc_num = Column(Integer)  # 文档
 
 
class RgUserTenant(Base):
    __tablename__ = 'user_tenant'
    id = Column(String(36), primary_key=True)  # id
    tenant_id = Column(String(32))  # 名称
    user_id = Column(String(32))
    role = Column(String(32))  # 创建人id
 
 
# 解析名字
def parse_names(names_str: str) -> List[str]:
    return [name.strip() for name in names_str.split(',')]
 
 
BISHENG_NAMES_TO_SYNC = parse_names(settings.fetch_sgb_agent)
RAGFLOW_NAMES_TO_SYNC = parse_names(settings.fetch_fwr_agent)
 
 
def get_data_from_bisheng(names: List[str]) -> List[Tuple]:
    db = SessionBisheng()
    try:
        if names:
            query = db.query(Flow.id, Flow.name) \
                .filter(Flow.status == 2, Flow.name.in_(names))
        else:
            query = db.query(Flow.id, Flow.name) \
                .filter(Flow.status == 2)
 
        results = query.all()
        print(f"Executing query: {query}")
        # 格式化id为UUID
        formatted_results = [(format_uuid(row[0]), row[1]) for row in results]
        return formatted_results
    finally:
        db.close()
 
 
def format_uuid(uuid_str: str) -> str:
    # 确保输入字符串长度为32
    if len(uuid_str) != 32:
        raise ValueError("Input string must be 32 characters long")
 
    # 插入连字符
    formatted_uuid = f"{uuid_str[:8]}-{uuid_str[8:12]}-{uuid_str[12:16]}-{uuid_str[16:20]}-{uuid_str[20:]}"
    return formatted_uuid
 
 
def get_data_from_ragflow(names: List[str]) -> List[Tuple]:
    db = SessionRagflow()
    try:
        if names:
            query = db.query(Dialog.id, Dialog.name) \
                .filter(Dialog.status == 1, Dialog.name.in_(names))
        else:
            query = db.query(Dialog.id, Dialog.name) \
                .filter(Dialog.status == 1)
 
        results = query.all()
        print(f"Executing query: {query}")
        return results
    finally:
        db.close()
 
 
def update_ids_in_local(data: List[Tuple]):
    db = SessionLocal()
    try:
        for row in data:
            name = row[1]
            new_id = row[0]
            existing_agent = db.query(AgentModel).filter_by(name=name).first()
            if existing_agent:
                existing_agent.id = new_id
                db.add(existing_agent)
        db.commit()
    except IntegrityError:
        db.rollback()
        raise
    finally:
        db.close()
 
 
def initialize_agents():
    db = SessionLocal()
    try:
        count = db.query(AgentModel).count()
        if count > 0:
            result = db.query(AgentModel).delete()
            db.commit()  # 提交事务
        initial_agents = [
            # ('80ee430a-e396-48c4-a12c-7c7cdf5eda51', 1, '报告生成', 'DIFY', 'report'),
            ('basic_excel_merge', 2, '报表合并', 'BASIC', 'excelMerge'),
            ('7638f00638a24c21a68ec6c49b304a35', 4, '文档智能', 'DIFY', 'documentIa'),
            ('da3451da89d911efb9490242ac190006', 3, '知识问答', 'RAGFLOW', 'knowledgeQA'),
            ('e96eb7a589db11ef87d20242ac190006', 5, '智能问答', 'RAGFLOW', 'chat'),
            ('basic_excel_talk', 6, '智能数据', 'BASIC', 'excelTalk'),
            ('basic_question_talk', 7, '出题组卷', 'BASIC', 'questionTalk'),
            ('9d75142a-66eb-4e23-b7d4-03efe4584915', 8, '小数绘图', 'DIFY', 'imageTalk'),
            ('basic_paper_talk', 9, '文档出卷', 'BASIC', 'paperTalk'),
            ('basic_report_clean', 10, '文档报告', 'DIFY', 'reportWorkflow')
        ]
 
        for agent in initial_agents:
            agent_id = format_uuid(agent[0]) if len(agent[0]) == 32 else agent[0]
            db.add(AgentModel(id=agent_id, sort=agent[1], name=agent[2], agent_type=agent[3], type=agent[4]))
 
        db.commit()
        print("Initial agents inserted successfully")
    except IntegrityError:
        db.rollback()
        raise
    finally:
        db.close()
 
 
def sync_agents():
    try:
        # bisheng_data = get_data_from_bisheng(BISHENG_NAMES_TO_SYNC)
        ragflow_data = get_data_from_ragflow(RAGFLOW_NAMES_TO_SYNC)
 
        # update_ids_in_local(bisheng_data)
        update_ids_in_local(ragflow_data)
 
        print("Agents synchronized successfully")
    except Exception as e:
        print(f"Failed to sync agents: {str(e)}")
 
 
def update_ids_in_local(data: List[Tuple]):
    db = SessionLocal()
    try:
        for row in data:
            name = row[1]
            new_id = row[0]
            existing_agent = db.query(AgentModel).filter_by(name=name).first()
            if existing_agent:
                existing_agent.id = new_id
                db.add(existing_agent)
        db.commit()
    except IntegrityError:
        db.rollback()
        raise
    finally:
        db.close()
 
 
def get_rag_user_id(db, tenant_id, app_type):
    user = db.query(UserAppModel).filter(UserAppModel.app_type == app_type, UserAppModel.app_id == tenant_id).first()
    if user:
        return user.user_id
    return tenant_id
 
 
def get_data_from_bisheng_v2(names: List[str]) -> List[Dict]:
    db = SessionBisheng()
    try:
        if names:
            query = db.query(Flow.id, Flow.name, Flow.description, Flow.status, Flow.user_id) \
                .filter(Flow.name.in_(names), Flow.status == "1")
        else:
            query = db.query(Flow.id, Flow.name, Flow.description, Flow.status, Flow.user_id).filter(Flow.status == "1")
 
        results = query.all()
        # print(f"Executing query: {query}")
        # 格式化id为UUID
        formatted_results = [
            {"id": row[0], "name": row[1], "description": row[2], "status": row[3], "user_id": str(row[4]),
             "mode": "agent-dialog"} for row in results]
        return formatted_results
    finally:
        db.close()
 
 
def get_data_from_ragflow_v2(names: List[str]) -> List[Dict]:
    db = SessionRagflow()
    try:
        if names:
            query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id) \
                .filter(Dialog.name.in_(names), Dialog.status == "1")
        else:
            query = db.query(Dialog.id, Dialog.name, Dialog.description, Dialog.status, Dialog.tenant_id).filter(
                Dialog.status == "1")
 
        results = query.all()
        formatted_results = [
            {"id": row[0], "name": row[1], "description": row[2], "status": "1" if row[3] == "1" else "2",
             "user_id": str(row[4]), "mode": "agent-dialog"} for row in results]
        return formatted_results
    finally:
        db.close()
 
 
def get_data_from_dify_v2(names: List[str]) -> List[Dict]:
    db = SessionDify()
    try:
        if names:
            query = db.query(DfApps.id, DfApps.name, DfApps.description, DfApps.status, DfApps.tenant_id, DfApps.mode) \
                .filter(DfApps.name.in_(names))
        else:
            query = db.query(DfApps.id, DfApps.name, DfApps.description, DfApps.status, DfApps.tenant_id, DfApps.mode)
 
        results = query.all()
        formatted_results = [
            {"id": str(row[0]), "name": row[1], "description": row[2], "status": "1",
             "user_id": str(row[4]), "mode": row[5]} for row in results]
        return formatted_results
    finally:
        db.close()
 
 
def update_ids_in_local_v2(data: List[Dict], dialog_type: str):
    db = SessionLocal()
    agent_id_list = []
    type_dict = {"1": RAGFLOW, "2": BISHENG, "4": DIFY}
    try:
        for row in data:
            agent_id_list.append(row["id"])
            existing_agent = db.query(DialogModel).filter_by(id=row["id"]).first()
            if existing_agent:
                existing_agent.name = row["name"]
                existing_agent.description = row["description"]
                # existing_agent.status = row["status"]
                existing_agent.mode = row["mode"]
                # existing_agent.tenant_id = get_rag_user_id(db, row["user_id"], type_dict[dialog_type])
            else:
                existing = DialogModel(id=row["id"], status=row["status"], name=row["name"],
                                       description=row["description"],
                                       tenant_id=get_rag_user_id(db, row["user_id"], type_dict[dialog_type]),
                                       dialog_type=dialog_type, mode=row["mode"])
                db.add(existing)
        db.commit()
        for dialog in db.query(DialogModel).filter_by(dialog_type=dialog_type).all():
            if dialog.id not in agent_id_list:
                # print(dialog.id)
                db.query(DialogModel).filter_by(id=dialog.id).update({"status": "2"})
                db.commit()
    except IntegrityError:
        db.rollback()
        raise
    finally:
        db.close()
 
 
def get_data_from_ragflow_knowledge():
    db = SessionRagflow()
    try:
 
        results = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status,
                           RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).all()
        formatted_results = [
            {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]),
             "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} for row in results]
        return formatted_results
    finally:
        db.close()
 
 
def sync_agents_v2():
    db = SessionLocal()
 
    try:
        app_register = AppRegisterDao(db).get_apps()
        for app in app_register:
            if app["id"] == RAGFLOW:
                ragflow_data = get_data_from_ragflow_v2([])
                if ragflow_data:
                    update_ids_in_local_v2(ragflow_data, "1")
            elif app["id"] == BISHENG:
                bisheng_data = get_data_from_bisheng_v2([])
                if bisheng_data:
                    update_ids_in_local_v2(bisheng_data, "2")
            elif app["id"] == DIFY:
                dify_data = get_data_from_dify_v2([])
                if dify_data:
                    update_ids_in_local_v2(dify_data, "4")
        print("v2 Agents synchronized successfully")
    except Exception as e:
        print(f"v2 Failed to sync agents: {str(e)}")
    finally:
        db.close()
 
 
def update_ids_in_local_knowledge(data, klg_type):
    type_dict = {"1": RAGFLOW, "2": BISHENG, "4": DIFY}
    db = SessionLocal()
    agent_id_list = []
    try:
        for row in data:
            agent_id_list.append(row["id"])
            existing_agent = db.query(KnowledgeModel).filter_by(id=row["id"]).first()
            if existing_agent:
                existing_agent.name = row["name"]
                existing_agent.description = row["description"]
                # existing_agent.tenant_id = get_rag_user_id(db, row["user_id"], type_dict[klg_type])
                existing_agent.permission = row["permission"]
                existing_agent.documents = row["doc_num"]
                existing_agent.status = row["status"]
            else:
                existing = KnowledgeModel(id=row["id"], name=row["name"], description=row["description"],
                                          tenant_id=get_rag_user_id(db, row["user_id"], type_dict[klg_type]),
                                          status=row["status"],
                                          knowledge_type=1, permission=row["permission"], documents=row["doc_num"])
                db.add(existing)
        db.commit()
        for dialog in db.query(KnowledgeModel).filter_by(knowledge_type=klg_type).all():
            if dialog.id not in agent_id_list:
                db.query(KnowledgeModel).filter_by(id=dialog.id).delete()
                db.commit()
    except IntegrityError:
        db.rollback()
        raise
    finally:
        db.close()
 
 
def get_one_from_ragflow_knowledge(klg_id):
    db = SessionRagflow()
    try:
 
        row = db.query(RgKnowledge.id, RgKnowledge.name, RgKnowledge.description, RgKnowledge.status,
                       RgKnowledge.tenant_id, RgKnowledge.doc_num, RgKnowledge.permission).filter(
            RgKnowledge.id == klg_id).first()
        return {"id": row[0], "name": row[1], "description": row[2], "status": str(row[3]),
                "user_id": str(row[4]), "doc_num": row[5], "permission": row[6]} if row else {}
    finally:
        db.close()
 
 
def sync_knowledge():
    db = SessionLocal()
 
    try:
        app_register = AppRegisterDao(db).get_apps()
        for app in app_register:
            if app["id"] == RAGFLOW:
                ragflow_data = get_data_from_ragflow_knowledge()
                if ragflow_data:
                    update_ids_in_local_knowledge(ragflow_data, "1")
            # elif app["id"] == BISHENG:
            #     bisheng_data = get_data_from_bisheng_v2([])
            #     update_ids_in_local_v2(bisheng_data, "2")
            # elif app["id"] == DIFY:
            #     dify_data = get_data_from_dify_v2([])
            #     update_ids_in_local_v2(dify_data, "4")
        print("sync knowledge successfully")
    except Exception as e:
        print(f"Failed to sync knowledge: {str(e)}")
    finally:
        db.close()
 
 
def update_ragflow_user_tenant(user_id: str):
    db = SessionRagflow()
    try:
        if user_id:
            db.query(RgUserTenant).filter(RgUserTenant.user_id == user_id, RgUserTenant.role == "invite").update(
                {"role": "normal"})
            db.query(RgUserTenant).filter(RgUserTenant.tenant_id == user_id, RgUserTenant.role == "invite").update(
                {"role": "normal"})
        else:
            db.query(RgUserTenant).filter(RgUserTenant.role == "invite").update({"role": "normal"})
        db.commit()
    finally:
        db.close()
 
 
def import_type_table(session: Session, node: dict, parent=None):
    resource_type = ResourceTypeModel(
        id=node['id'],
        name=node['name'],
        description=node.get('description')
    )
    if parent:
        resource_type.parent = parent
    session.add(resource_type)
    session.commit()
 
 
def import_tree(session: Session, node: dict, parent=None):
    resource = ResourceModel(
        id=node['id'],
        name=node['name'],
        url=node['url'],
        path=node.get('path'),
        perms=node['perms'],
        description=node.get('description'),
        icon=node.get('icon'),
        seq=node['seq'],
        target=node.get('target'),
        canbdeeleted=node.get('canbdeeleted'),
        resource_type_id=node['resource_type_id'],
        resource_id=node.get('resource_id'),
        status=node['status'],
        hidden=node.get('hidden')
    )
    if parent:
        resource.parent = parent
    session.add(resource)
    if 'children' in node:
        for child in node['children']:
            import_tree(session, child, parent=resource)
    session.commit()
 
 
def sync_resources_from_json():
    db = SessionLocal()
    try:
        if db.query(ResourceTypeModel).count() == 0:
            with open(os.path.join(ENV_CONF_PATH, "resource_type.json"), 'r', encoding='utf-8') as file:
                type_json_data = json.load(file)
 
            db.query(ResourceTypeModel).delete()
            db.commit()
 
            for node in type_json_data:
                import_type_table(db, node)
            print("add resourceType record successfully")
        else:
            print("sync resourcesType successfully")
        if db.query(ResourceModel).count() == 0:
            with open(os.path.join(ENV_CONF_PATH, "resource.json"), 'r', encoding='utf-8') as file:
                json_data = json.load(file)
 
            db.query(ResourceModel).delete()
            db.commit()
 
            for node in json_data:
                import_tree(db, node)
            print("add resources record successfully")
        else:
            print("sync resources successfully")
    except Exception as e:
        print(f"Failed to sync resources or resource type: {str(e)}")
    finally:
        db.close()
 
 
if __name__ == "__main__":
    # a = get_data_from_dify_v2([])
    # print(a)
    update_ragflow_user_tenant("")