| | |
| | | ### ---------------------------http status-------------------------------------- |
| | | http_200 = 200 |
| | | http_400 = 400 |
| | | http_500 = 500 |
| | | http_500 = 500 |
| | | |
| | | ###-----------------------app account key-------------------------------------------- |
| | | chat_server = "chat_server" |
| | | workflow_server = "workflow_server" |
| | |
| | | from app.models.base_model import SessionLocal |
| | | from app.service.v2.initialize_data import dialog_menu_sync, default_group_sync, default_role_sync, app_register_sync, \ |
| | | basic_agent_sync |
| | | basic_agent_sync, admin_account_sync |
| | | |
| | | |
| | | async def sync_default_data(): |
| | |
| | | |
| | | @staticmethod |
| | | def decrypted_password(password): |
| | | return cipher_suite.decrypt(password).decode("utf-8") |
| | | |
| | | |
| | | class UserTokenModel(Base): |
| | | __tablename__ = "user_token" |
| | | id = Column(String(16), primary_key=True) |
| | | account = Column(String(255)) |
| | | password = Column(String(255)) |
| | | access_token = Column(String(1000)) |
| | | refresh_token = Column(String(1000)) |
| | | created_at = Column(DateTime, default=datetime.now()) |
| | | updated_at = Column(DateTime, default=datetime.now()) |
| | | |
| | | def to_json(self): |
| | | return { |
| | | 'id': self.id, |
| | | 'account': self.username, |
| | | 'createTime': self.created_at, |
| | | 'updateTime': self.updated_at, |
| | | 'password': self.password, |
| | | 'access_token': self.access_token, |
| | | 'refresh_token': self.refresh_token, |
| | | } |
| | | @staticmethod |
| | | def encrypted_password(password): |
| | | return cipher_suite.encrypt(password.encode("utf-8")).decode("utf-8") |
| | | |
| | | @staticmethod |
| | | def decrypted_password(password): |
| | | return cipher_suite.decrypt(password).decode("utf-8") |
| | |
| | | 'Authorization': f'Bearer {token}' |
| | | } |
| | | |
| | | |
| | |
| | | import json |
| | | |
| | | from app.config.config import settings |
| | | # from Log import logger |
| | | from app.service.v2.app_driver.chat_base import ChatBase |
| | | from app.utils.rsa_crypto import RagflowCrypto |
| | | |
| | | |
| | | class ChatBaseApply(ChatBase): |
| | | |
| | | async def chat_parameters(self, url, params, headers): |
| | | async def chat_get(self, url, params, headers): |
| | | |
| | | res = await self.http_get(url, params, headers) |
| | | if res.status_code == 200: |
| | |
| | | else: |
| | | return {} |
| | | |
| | | async def chat_ping(self, url, params, headers): |
| | | res = await self.http_get(url, params, headers) |
| | | return res.status_code |
| | | |
| | | |
| | | async def chat_post(self, url, data, headers): |
| | | |
| | | res = await self.http_post(url, data, headers) |
| | | if res.status_code == 200: |
| | | return res.json() |
| | | else: |
| | | return {} |
| | | |
| | | async def chat_login(self, url, data, headers): |
| | | |
| | | res = await self.http_post(url, data, headers) |
| | | if res.status_code == 200: |
| | | res_json = res.json() |
| | | authorization = res.headers.get('Authorization') |
| | | if authorization: |
| | | res_json["data"]["access_token"] = authorization |
| | | return res_json |
| | | else: |
| | | return {} |
| | | |
| | | |
| | | @staticmethod |
| | | async def password_encrypt(password): |
| | | password = RagflowCrypto(settings.PUBLIC_KEY, settings.PRIVATE_KEY).encrypt(password) |
| | | return password |
| | | |
| | | |
| | | |
| | | |
| | |
| | | return {} |
| | | url = settings.dify_base_url + DF_CHAT_PARAMETERS |
| | | chat = ChatBaseApply() |
| | | return await chat.chat_parameters(url, {"user": str(user_id)}, await chat.get_headers(token)) |
| | | return await chat.chat_get(url, {"user": str(user_id)}, await chat.get_headers(token)) |
| | | |
| | | |
| | | async def service_chat_sessions(db, chat_id, name): |
| | |
| | | import json |
| | | import time |
| | | import os |
| | | |
| | | import yaml |
| | | |
| | | from Log import logger |
| | | from app.config.const import DIFY, ENV_CONF_PATH |
| | | from app.models import MenuCapacityModel, WebMenuModel, GroupModel, RoleModel, DialogModel, UserModel, UserAppModel, \ |
| | | cipher_suite |
| | | cipher_suite, UserTokenModel |
| | | from app.service.auth import UserAppDao |
| | | from app.service.bisheng import BishengService |
| | | from app.service.difyService import DifyService |
| | |
| | | |
| | | |
| | | async def admin_account_sync(db): |
| | | agent_list = [] |
| | | with open(os.path.join(ENV_CONF_PATH, "default_agent_conf.json"), 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | agent_dict = json.load(file) |
| | | agent_list = agent_dict.get("basic", []) |
| | | user = db.query(UserModel).filter_by(permission="admin").first() |
| | | for agent in agent_list: |
| | | dialog = db.query(DialogModel).filter(DialogModel.id == agent["id"]).first() |
| | | if dialog: |
| | | try: |
| | | dialog.name = agent["name"] |
| | | dialog.description = agent["description"] |
| | | dialog.icon = agent["icon"] |
| | | db.commit() |
| | | except Exception as e: |
| | | logger.error(e) |
| | | else: |
| | | try: |
| | | dialog = DialogModel(id=agent["id"], name=agent["name"], description=agent["description"], |
| | | icon=agent["icon"], tenant_id=user.id if user else "", dialog_type="3", |
| | | agent_id=agent["id"]) |
| | | db.add(dialog) |
| | | db.commit() |
| | | db.refresh(dialog) |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
| | | try: |
| | | config = {} |
| | | now_account =[] |
| | | with open(os.path.join(ENV_CONF_PATH, "account.yaml"), 'r', encoding='utf-8') as file: |
| | | # 加载JSON数据 |
| | | config = yaml.safe_load(file) |
| | | account_list = db.query(UserTokenModel).all() |
| | | for account in account_list: |
| | | if account.id in config: |
| | | |
| | | if account.account != config[account.id]["account"] or account.password != config[account.id]["password"]: |
| | | db.query(UserTokenModel).filter_by(id=account.id).update({"account": config[account.id]["account"], |
| | | "password": config[account.id]["password"], |
| | | "access_token": "" |
| | | }) |
| | | now_account.append(account.id) |
| | | else: |
| | | db.query(UserTokenModel).filter_by(id=account.id).delete() |
| | | for k, v in config.items(): |
| | | if k not in now_account: |
| | | new_account = UserTokenModel(id=k, account=v["account"], password=v["password"]) |
| | | db.add(new_account) |
| | | db.commit() |
| | | except Exception as e: |
| | | print(e) |
| | | db.rollback() |
New file |
| | |
| | | |
| | | |
| | | |
| | | def sync_token(): |
| | | """ |
| | | 1.获取到user_token表中的账号 |
| | | 2.判断是否超过12小时,否则获取新的token |
| | | 3.未超过12小时,则测试token有效性,chat_ping接口,返回401,则重新获取token |
| | | 4.token获取:login |
| | | df:/console/api/workspaces |
| | | rg:/v1/system/version |
| | | :return: |
| | | """ |
| | |
| | | from app.task.fetch_agent import sync_agents, initialize_agents, sync_agents_v2, sync_knowledge, \ |
| | | sync_resources_from_json |
| | | from app.init_config.init_run_data import sync_default_data |
| | | from app.task.sync_account_token import sync_token |
| | | |
| | | init_db() |
| | | |
| | |
| | | # 创建调度器 |
| | | scheduler = BackgroundScheduler() |
| | | scheduler.add_job(sync_agents_v2, 'interval', minutes=60, id="sync_resource_data") |
| | | scheduler.add_job(sync_token, 'interval', minutes=5, id="sync_token_1") |
| | | scheduler.start() |
| | | |
| | | app.include_router(auth_router, prefix='/api/auth', tags=["auth"]) |