|  |  |  | 
|---|
|  |  |  | from datetime import datetime | 
|---|
|  |  |  |  | 
|---|
|  |  |  | import httpx | 
|---|
|  |  |  |  | 
|---|
|  |  |  | from app.config.config import settings | 
|---|
|  |  |  | 
|---|
|  |  |  | def __init__(self, base_url: str): | 
|---|
|  |  |  | self.base_url = base_url | 
|---|
|  |  |  |  | 
|---|
|  |  |  | def _check_response(self, response: httpx.Response): | 
|---|
|  |  |  | if response.status_code not in [200, 201]: | 
|---|
|  |  |  | raise Exception(f"Failed to fetch data from Bisheng API: {response.text}") | 
|---|
|  |  |  | response_data = response.json() | 
|---|
|  |  |  | status_code = response_data.get("status_code", 0) | 
|---|
|  |  |  | if status_code != 200: | 
|---|
|  |  |  | raise Exception(f"Failed to fetch data from Bisheng API: {response.text}") | 
|---|
|  |  |  | # 检查返回的数据类型 | 
|---|
|  |  |  | if isinstance(response_data.get("data"), dict): | 
|---|
|  |  |  | return response_data.get("data", {}) | 
|---|
|  |  |  | elif isinstance(response_data.get("data"), list): | 
|---|
|  |  |  | return response_data.get("data", []) | 
|---|
|  |  |  | else: | 
|---|
|  |  |  | return {} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def register(self, username: str, password: str): | 
|---|
|  |  |  | public_key = await self.get_public_key_api() | 
|---|
|  |  |  | password = BishengCrypto(public_key, settings.PRIVATE_KEY).encrypt(password) | 
|---|
|  |  |  | 
|---|
|  |  |  | json={"user_name": username, "password": password}, | 
|---|
|  |  |  | headers={'Content-Type': 'application/json'} | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | if response.status_code != 200 and response.status_code != 201: | 
|---|
|  |  |  | raise Exception(f"Bisheng registration failed: {response.text}") | 
|---|
|  |  |  | return self._check_response(response) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def login(self, username: str, password: str) -> str: | 
|---|
|  |  |  | public_key = await self.get_public_key_api() | 
|---|
|  |  |  | 
|---|
|  |  |  | json={"user_name": username, "password": password}, | 
|---|
|  |  |  | headers={'Content-Type': 'application/json'} | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | if response.status_code != 200 and response.status_code != 201: | 
|---|
|  |  |  | raise Exception(f"Bisheng login failed: {response.text}") | 
|---|
|  |  |  | return response.json().get('data', {}).get('access_token') | 
|---|
|  |  |  | data = self._check_response(response) | 
|---|
|  |  |  | return data.get('access_token') | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def get_public_key_api(self) -> dict: | 
|---|
|  |  |  | async with httpx.AsyncClient() as client: | 
|---|
|  |  |  | 
|---|
|  |  |  | f"{self.base_url}/api/v1/user/public_key", | 
|---|
|  |  |  | headers={'Content-Type': 'application/json'} | 
|---|
|  |  |  | ) | 
|---|
|  |  |  | if response.status_code != 200: | 
|---|
|  |  |  | raise Exception(f"Failed to get public key: {response.text}") | 
|---|
|  |  |  | return response.json().get('data', {}).get('public_key') | 
|---|
|  |  |  | data = self._check_response(response) | 
|---|
|  |  |  | return data.get('public_key') | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def get_chat_sessions(self, token: str) -> list: | 
|---|
|  |  |  | url = f"{self.base_url}/api/v1/chat/list?page=1&limit=40" | 
|---|
|  |  |  | headers = {'cookie': f"access_token_cookie={token};"} | 
|---|
|  |  |  | async with httpx.AsyncClient() as client: | 
|---|
|  |  |  | response = await client.get(url, headers=headers) | 
|---|
|  |  |  | if response.status_code != 200: | 
|---|
|  |  |  | raise Exception(f"Failed to fetch data from Bisheng API: {response.text}") | 
|---|
|  |  |  | data = self._check_response(response) | 
|---|
|  |  |  |  | 
|---|
|  |  |  | data = response.json().get("data", []) | 
|---|
|  |  |  | result = [ | 
|---|
|  |  |  | { | 
|---|
|  |  |  | "id": item["chat_id"], | 
|---|
|  |  |  | 
|---|
|  |  |  | for item in data | 
|---|
|  |  |  | ] | 
|---|
|  |  |  | return result | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def variable_list(self, token: str, agent_id: str) -> list: | 
|---|
|  |  |  | url = f"{self.base_url}/api/v1/variable/list?flow_id={agent_id}" | 
|---|
|  |  |  | headers = {'cookie': f"access_token_cookie={token};"} | 
|---|
|  |  |  | async with httpx.AsyncClient() as client: | 
|---|
|  |  |  | response = await client.get(url, headers=headers) | 
|---|
|  |  |  | data = self._check_response(response) | 
|---|
|  |  |  | return data | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def upload(self, token: str, filename: str, file: bytes) -> dict: | 
|---|
|  |  |  | url = f"{self.base_url}/api/v1/knowledge/upload" | 
|---|
|  |  |  | headers = {'cookie': f"access_token_cookie={token};"} | 
|---|
|  |  |  |  | 
|---|
|  |  |  | # 创建表单数据,包含文件 | 
|---|
|  |  |  | files = {"file": (filename, file)} | 
|---|
|  |  |  | async with httpx.AsyncClient() as client: | 
|---|
|  |  |  | response = await client.post(url, headers=headers, files=files) | 
|---|
|  |  |  | data = self._check_response(response) | 
|---|
|  |  |  | file_path = data.get("file_path", "") | 
|---|
|  |  |  | result = { | 
|---|
|  |  |  | "file_path": file_path | 
|---|
|  |  |  | } | 
|---|
|  |  |  |  | 
|---|
|  |  |  | return result | 
|---|
|  |  |  |  | 
|---|
|  |  |  | async def user_list(self, token: str) -> list: | 
|---|
|  |  |  | url = f"{self.base_url}/api/v1/user/list" | 
|---|
|  |  |  | headers = {'cookie': f"access_token_cookie={token};"} | 
|---|
|  |  |  | async with httpx.AsyncClient() as client: | 
|---|
|  |  |  | response = await client.get(url, headers=headers) | 
|---|
|  |  |  | data = self._check_response(response) | 
|---|
|  |  |  | return data | 
|---|