From 25bf0a51088fa0d0acdc39c6096cc45d699f63a1 Mon Sep 17 00:00:00 2001 From: zhaoqingang <zhaoqg0118@163.com> Date: 星期四, 13 二月 2025 10:53:23 +0800 Subject: [PATCH] merge master --- app/service/dialog.py | 76 +++++++++++++++++++++++++++---------- 1 files changed, 55 insertions(+), 21 deletions(-) diff --git a/app/service/dialog.py b/app/service/dialog.py index c302d10..81316ff 100644 --- a/app/service/dialog.py +++ b/app/service/dialog.py @@ -1,12 +1,17 @@ +import json from datetime import datetime from sqlalchemy import or_ -from app.config.const import Dialog_STATSU_DELETE +from app.config.agent_base_url import DF_CHAT_PARAMETERS, DF_CHAT_API_KEY +from app.config.config import settings +from app.config.const import Dialog_STATSU_DELETE, DF_TYPE, Dialog_STATSU_ON, workflow_server from app.models import KnowledgeModel, GroupModel, DialogModel, ConversationModel, group_dialog_table, LabelWorkerModel, \ - LabelModel -from app.models.user_model import UserModel + LabelModel, ApiTokenModel +from app.models.user_model import UserModel, UserTokenModel from Log import logger +from app.service.v2.app_driver.chat_data import ChatBaseApply +from app.service.v2.chat import get_chat_token, add_chat_token, get_app_token async def get_dialog_list(db, user_id, keyword, label, status, page_size, page_index): @@ -32,7 +37,6 @@ if keyword: query = query.filter(DialogModel.name.like('%{}%'.format(keyword))) - query = query.order_by(DialogModel.update_date.desc()) total = query.count() if page_size: @@ -48,14 +52,13 @@ rows.append(kld.to_json()) user_dict = {str(i.id): i.to_dict() for i in db.query(UserModel).filter(UserModel.id.in_(user_id_set)).all()} for i in db.query(LabelModel.id, LabelModel.name, LabelWorkerModel.object_id).outerjoin(LabelWorkerModel, - LabelModel.id == LabelWorkerModel.label_id).filter( - LabelWorkerModel.object_id.in_(dialog_id_set)).all(): - - label_dict[i.object_id] = label_dict.get(i.object_id, []) +[{"labelId": i.id, "labelName": i.name}] + LabelModel.id == LabelWorkerModel.label_id).filter( + LabelWorkerModel.object_id.in_(dialog_id_set)).all(): + label_dict[i.object_id] = label_dict.get(i.object_id, []) + [{"labelId": i.id, "labelName": i.name}] for r in rows: r["user"] = user_dict.get(r["user_id"], {}) r["label"] = label_dict.get(r["id"], []) - return {"total": total, "rows": rows} + return {"total": total, "rows": rows} async def update_session_history(db, data: dict, user_id): @@ -86,17 +89,18 @@ db.rollback() -async def get_session_history(db, user_id, dialog_id, page, limit): +async def get_session_history(db, user_id, dialog_id, page, limit): session_list = db.query(ConversationModel).filter(ConversationModel.tenant_id.__eq__(user_id), ConversationModel.dialog_id.__eq__(dialog_id)).order_by( ConversationModel.update_time.desc()).limit(limit).offset((page - 1) * limit).all() return [i.to_json() for i in session_list] - async def create_dialog_service(db, dialog_id, dialog_name, description, icon, dialog_type, mode, user_id): try: - dialog_model = DialogModel(id=dialog_id,name=dialog_name, description=description,icon=icon, dialog_type=dialog_type, tenant_id=user_id, mode=mode,update_date=datetime.now(),create_date=datetime.now()) + dialog_model = DialogModel(id=dialog_id, name=dialog_name, description=description, icon=icon, + dialog_type=dialog_type, tenant_id=user_id, mode=mode, update_date=datetime.now(), + create_date=datetime.now()) db.add(dialog_model) db.commit() db.refresh(dialog_model) @@ -107,9 +111,38 @@ return True -async def update_dialog_status_service(db, dialog_id, status): +async def update_dialog_status_service(db, dialog_id, status, user_id): try: - db.query(DialogModel).filter_by(id=dialog_id).update({"status":status, "update_date": datetime.now()}) + dialog = db.query(DialogModel).filter_by(id=dialog_id).first() + dialog.status = status + dialog.update_date = datetime.now() + # db.query(DialogModel).filter_by(id=dialog_id).update({"status":status, "update_date": datetime.now()}) + if dialog.dialog_type == DF_TYPE and status == Dialog_STATSU_ON: + chat = ChatBaseApply() + token = await get_chat_token(db, dialog_id) + if not token: + access_token = await get_app_token(db, workflow_server) + # print(workflow) + if access_token: + url = settings.dify_base_url + DF_CHAT_API_KEY.format(dialog_id) + param = await chat.chat_get(url, {}, await chat.get_headers(access_token)) + if param and param.get("data"): + token = param.get("data", [{}])[0].get("token") + token_id = param.get("data", [{}])[0].get("id") + await add_chat_token(db, {"id":token_id, "app_id": dialog_id, "type":"app", "token": token}) + # dialog.parameters = json.dumps(param) + else: + param = await chat.chat_post(url, {}, await chat.get_headers(access_token)) + if param: + token = param.get("token") + token_id = param.get("id") + await add_chat_token(db, {"id": token_id, "app_id": dialog_id, "type": "app", "token": token}) + if token: + url = settings.dify_base_url + DF_CHAT_PARAMETERS + param = await chat.chat_get(url, {"user": str(user_id)}, await chat.get_headers(token)) + if param: + dialog.parameters = json.dumps(param) + db.commit() except Exception as e: logger.error(e) @@ -118,9 +151,11 @@ return True + async def delete_dialog_service(db, dialog_id): try: - db.query(DialogModel).filter_by(id=dialog_id).update({"status":Dialog_STATSU_DELETE, "update_date": datetime.now()}) + db.query(DialogModel).filter_by(id=dialog_id).update( + {"status": Dialog_STATSU_DELETE, "update_date": datetime.now()}) db.commit() except Exception as e: logger.error(e) @@ -131,7 +166,7 @@ async def update_dialog_icon_service(db, dialog_id, icon): try: - db.query(DialogModel).filter_by(id=dialog_id).update({"icon":icon, "update_date": datetime.now()}) + db.query(DialogModel).filter_by(id=dialog_id).update({"icon": icon, "update_date": datetime.now()}) db.commit() except Exception as e: logger.error(e) @@ -177,11 +212,10 @@ rows.append(kld.to_json()) user_dict = {str(i.id): i.to_dict() for i in db.query(UserModel).filter(UserModel.id.in_(user_id_set)).all()} for i in db.query(LabelModel.id, LabelModel.name, LabelWorkerModel.object_id).outerjoin(LabelWorkerModel, - LabelModel.id == LabelWorkerModel.label_id).filter( - LabelWorkerModel.object_id.in_(dialog_id_set)).all(): - - label_dict[i.object_id] = label_dict.get(i.object_id, []) +[{"labelId": i.id, "labelName": i.name}] + LabelModel.id == LabelWorkerModel.label_id).filter( + LabelWorkerModel.object_id.in_(dialog_id_set)).all(): + label_dict[i.object_id] = label_dict.get(i.object_id, []) + [{"labelId": i.id, "labelName": i.name}] for r in rows: r["user"] = user_dict.get(r["user_id"], {}) r["label"] = label_dict.get(r["id"], []) - return {"total": total, "rows": rows} \ No newline at end of file + return {"total": total, "rows": rows} -- Gitblit v1.8.0