mirror of
https://github.com/langbot-app/LangBot.git
synced 2026-06-09 23:36:02 +00:00
style: introduce ruff as linter and formatter (#1356)
* style: remove necessary imports * style: fix F841 * style: fix F401 * style: fix F811 * style: fix E402 * style: fix E721 * style: fix E722 * style: fix E722 * style: fix F541 * style: ruff format * style: all passed * style: add ruff in deps * style: more ignores in ruff.toml * style: add pre-commit
This commit is contained in:
committed by
GitHub
parent
09e70d70e9
commit
209f16af76
@@ -1,3 +1,3 @@
|
||||
"""
|
||||
审计相关操作
|
||||
"""
|
||||
"""
|
||||
|
||||
@@ -3,11 +3,9 @@ from __future__ import annotations
|
||||
import abc
|
||||
import uuid
|
||||
import json
|
||||
import logging
|
||||
import asyncio
|
||||
|
||||
import aiohttp
|
||||
import requests
|
||||
|
||||
from ...core import app, entities as core_entities
|
||||
|
||||
@@ -38,22 +36,22 @@ class APIGroup(metaclass=abc.ABCMeta):
|
||||
"""
|
||||
执行请求
|
||||
"""
|
||||
self._runtime_info["account_id"] = "-1"
|
||||
self._runtime_info['account_id'] = '-1'
|
||||
|
||||
url = self.prefix + path
|
||||
data = json.dumps(data)
|
||||
headers["Content-Type"] = "application/json"
|
||||
headers['Content-Type'] = 'application/json'
|
||||
|
||||
try:
|
||||
async with aiohttp.ClientSession() as session:
|
||||
async with session.request(
|
||||
method, url, data=data, params=params, headers=headers, **kwargs
|
||||
) as resp:
|
||||
self.ap.logger.debug("data: %s", data)
|
||||
self.ap.logger.debug("ret: %s", await resp.text())
|
||||
self.ap.logger.debug('data: %s', data)
|
||||
self.ap.logger.debug('ret: %s', await resp.text())
|
||||
|
||||
except Exception as e:
|
||||
self.ap.logger.debug(f"上报失败: {e}")
|
||||
self.ap.logger.debug(f'上报失败: {e}')
|
||||
|
||||
async def do(
|
||||
self,
|
||||
@@ -68,8 +66,8 @@ class APIGroup(metaclass=abc.ABCMeta):
|
||||
|
||||
return self.ap.task_mgr.create_task(
|
||||
self._do(method, path, data, params, headers, **kwargs),
|
||||
kind="telemetry-operation",
|
||||
name=f"{method} {path}",
|
||||
kind='telemetry-operation',
|
||||
name=f'{method} {path}',
|
||||
scopes=[core_entities.LifecycleControlScope.APPLICATION],
|
||||
).task
|
||||
|
||||
@@ -80,7 +78,7 @@ class APIGroup(metaclass=abc.ABCMeta):
|
||||
def basic_info(self):
|
||||
"""获取基本信息"""
|
||||
basic_info = APIGroup._basic_info.copy()
|
||||
basic_info["rid"] = self.gen_rid()
|
||||
basic_info['rid'] = self.gen_rid()
|
||||
return basic_info
|
||||
|
||||
def runtime_info(self):
|
||||
|
||||
@@ -9,7 +9,7 @@ class V2MainDataAPI(apigroup.APIGroup):
|
||||
|
||||
def __init__(self, prefix: str, ap: app.Application):
|
||||
self.ap = ap
|
||||
super().__init__(prefix+"/main", ap)
|
||||
super().__init__(prefix + '/main', ap)
|
||||
|
||||
async def do(self, *args, **kwargs):
|
||||
if not self.ap.instance_config.data['telemetry']['report']:
|
||||
@@ -25,31 +25,31 @@ class V2MainDataAPI(apigroup.APIGroup):
|
||||
):
|
||||
"""提交更新记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/update",
|
||||
'POST',
|
||||
'/update',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"update_info": {
|
||||
"spent_seconds": spent_seconds,
|
||||
"infer_reason": infer_reason,
|
||||
"old_version": old_version,
|
||||
"new_version": new_version,
|
||||
}
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'update_info': {
|
||||
'spent_seconds': spent_seconds,
|
||||
'infer_reason': infer_reason,
|
||||
'old_version': old_version,
|
||||
'new_version': new_version,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def post_announcement_showed(
|
||||
self,
|
||||
ids: list[int],
|
||||
):
|
||||
"""提交公告已阅"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/announcement",
|
||||
'POST',
|
||||
'/announcement',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"announcement_info": {
|
||||
"ids": ids,
|
||||
}
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'announcement_info': {
|
||||
'ids': ids,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -9,39 +9,33 @@ class V2PluginDataAPI(apigroup.APIGroup):
|
||||
|
||||
def __init__(self, prefix: str, ap: app.Application):
|
||||
self.ap = ap
|
||||
super().__init__(prefix+"/plugin", ap)
|
||||
super().__init__(prefix + '/plugin', ap)
|
||||
|
||||
async def do(self, *args, **kwargs):
|
||||
if not self.ap.instance_config.data['telemetry']['report']:
|
||||
return None
|
||||
return await super().do(*args, **kwargs)
|
||||
|
||||
async def post_install_record(
|
||||
self,
|
||||
plugin: dict
|
||||
):
|
||||
async def post_install_record(self, plugin: dict):
|
||||
"""提交插件安装记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/install",
|
||||
'POST',
|
||||
'/install',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"plugin": plugin,
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'plugin': plugin,
|
||||
},
|
||||
)
|
||||
|
||||
async def post_remove_record(
|
||||
self,
|
||||
plugin: dict
|
||||
):
|
||||
async def post_remove_record(self, plugin: dict):
|
||||
"""提交插件卸载记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/remove",
|
||||
'POST',
|
||||
'/remove',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"plugin": plugin,
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'plugin': plugin,
|
||||
},
|
||||
)
|
||||
|
||||
async def post_update_record(
|
||||
@@ -52,14 +46,14 @@ class V2PluginDataAPI(apigroup.APIGroup):
|
||||
):
|
||||
"""提交插件更新记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/update",
|
||||
'POST',
|
||||
'/update',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"plugin": plugin,
|
||||
"update_info": {
|
||||
"old_version": old_version,
|
||||
"new_version": new_version,
|
||||
}
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'plugin': plugin,
|
||||
'update_info': {
|
||||
'old_version': old_version,
|
||||
'new_version': new_version,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
@@ -9,7 +9,7 @@ class V2UsageDataAPI(apigroup.APIGroup):
|
||||
|
||||
def __init__(self, prefix: str, ap: app.Application):
|
||||
self.ap = ap
|
||||
super().__init__(prefix+"/usage", ap)
|
||||
super().__init__(prefix + '/usage', ap)
|
||||
|
||||
async def do(self, *args, **kwargs):
|
||||
if not self.ap.instance_config.data['telemetry']['report']:
|
||||
@@ -28,25 +28,25 @@ class V2UsageDataAPI(apigroup.APIGroup):
|
||||
):
|
||||
"""提交请求记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/query",
|
||||
'POST',
|
||||
'/query',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"runtime": self.runtime_info(),
|
||||
"session_info": {
|
||||
"type": session_type,
|
||||
"id": session_id,
|
||||
'basic': self.basic_info(),
|
||||
'runtime': self.runtime_info(),
|
||||
'session_info': {
|
||||
'type': session_type,
|
||||
'id': session_id,
|
||||
},
|
||||
"query_info": {
|
||||
"ability_provider": query_ability_provider,
|
||||
"usage": usage,
|
||||
"model_name": model_name,
|
||||
"response_seconds": response_seconds,
|
||||
"retry_times": retry_times,
|
||||
}
|
||||
}
|
||||
'query_info': {
|
||||
'ability_provider': query_ability_provider,
|
||||
'usage': usage,
|
||||
'model_name': model_name,
|
||||
'response_seconds': response_seconds,
|
||||
'retry_times': retry_times,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def post_event_record(
|
||||
self,
|
||||
plugins: list[dict],
|
||||
@@ -54,18 +54,18 @@ class V2UsageDataAPI(apigroup.APIGroup):
|
||||
):
|
||||
"""提交事件触发记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/event",
|
||||
'POST',
|
||||
'/event',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"runtime": self.runtime_info(),
|
||||
"plugins": plugins,
|
||||
"event_info": {
|
||||
"name": event_name,
|
||||
}
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'runtime': self.runtime_info(),
|
||||
'plugins': plugins,
|
||||
'event_info': {
|
||||
'name': event_name,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
async def post_function_record(
|
||||
self,
|
||||
plugin: dict,
|
||||
@@ -74,15 +74,14 @@ class V2UsageDataAPI(apigroup.APIGroup):
|
||||
):
|
||||
"""提交内容函数使用记录"""
|
||||
return await self.do(
|
||||
"POST",
|
||||
"/function",
|
||||
'POST',
|
||||
'/function',
|
||||
data={
|
||||
"basic": self.basic_info(),
|
||||
"plugin": plugin,
|
||||
"function_info": {
|
||||
"name": function_name,
|
||||
"description": function_description,
|
||||
}
|
||||
}
|
||||
'basic': self.basic_info(),
|
||||
'plugin': plugin,
|
||||
'function_info': {
|
||||
'name': function_name,
|
||||
'description': function_description,
|
||||
},
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ from ...core import app
|
||||
|
||||
class V2CenterAPI:
|
||||
"""中央服务器 v2 API 交互类"""
|
||||
|
||||
|
||||
main: main.V2MainDataAPI = None
|
||||
"""主 API 组"""
|
||||
|
||||
@@ -21,15 +21,20 @@ class V2CenterAPI:
|
||||
plugin: plugin.V2PluginDataAPI = None
|
||||
"""插件 API 组"""
|
||||
|
||||
def __init__(self, ap: app.Application, backend_url: str, basic_info: dict = None, runtime_info: dict = None):
|
||||
def __init__(
|
||||
self,
|
||||
ap: app.Application,
|
||||
backend_url: str,
|
||||
basic_info: dict = None,
|
||||
runtime_info: dict = None,
|
||||
):
|
||||
"""初始化"""
|
||||
|
||||
logging.debug("basic_info: %s, runtime_info: %s", basic_info, runtime_info)
|
||||
|
||||
logging.debug('basic_info: %s, runtime_info: %s', basic_info, runtime_info)
|
||||
|
||||
apigroup.APIGroup._basic_info = basic_info
|
||||
apigroup.APIGroup._runtime_info = runtime_info
|
||||
|
||||
self.main = main.V2MainDataAPI(backend_url, ap)
|
||||
self.usage = usage.V2UsageDataAPI(backend_url, ap)
|
||||
self.plugin = plugin.V2PluginDataAPI(backend_url, ap)
|
||||
|
||||
|
||||
@@ -16,6 +16,7 @@ identifier = {
|
||||
HOST_ID_FILE = os.path.expanduser('~/.langbot/host_id.json')
|
||||
INSTANCE_ID_FILE = 'data/labels/instance_id.json'
|
||||
|
||||
|
||||
def init():
|
||||
global identifier
|
||||
|
||||
@@ -23,14 +24,11 @@ def init():
|
||||
os.mkdir(os.path.expanduser('~/.langbot'))
|
||||
|
||||
if not os.path.exists(HOST_ID_FILE):
|
||||
new_host_id = 'host_'+str(uuid.uuid4())
|
||||
new_host_id = 'host_' + str(uuid.uuid4())
|
||||
new_host_create_ts = int(time.time())
|
||||
|
||||
with open(HOST_ID_FILE, 'w') as f:
|
||||
json.dump({
|
||||
'host_id': new_host_id,
|
||||
'host_create_ts': new_host_create_ts
|
||||
}, f)
|
||||
json.dump({'host_id': new_host_id, 'host_create_ts': new_host_create_ts}, f)
|
||||
|
||||
identifier['host_id'] = new_host_id
|
||||
identifier['host_create_ts'] = new_host_create_ts
|
||||
@@ -51,20 +49,25 @@ def init():
|
||||
instance_id = {}
|
||||
with open(INSTANCE_ID_FILE, 'r') as f:
|
||||
instance_id = json.load(f)
|
||||
|
||||
if instance_id['host_id'] != identifier['host_id']: # 如果实例 id 不是当前主机的,删除
|
||||
|
||||
if (
|
||||
instance_id['host_id'] != identifier['host_id']
|
||||
): # 如果实例 id 不是当前主机的,删除
|
||||
os.remove(INSTANCE_ID_FILE)
|
||||
|
||||
if not os.path.exists(INSTANCE_ID_FILE):
|
||||
new_instance_id = 'instance_'+str(uuid.uuid4())
|
||||
new_instance_id = 'instance_' + str(uuid.uuid4())
|
||||
new_instance_create_ts = int(time.time())
|
||||
|
||||
with open(INSTANCE_ID_FILE, 'w') as f:
|
||||
json.dump({
|
||||
'host_id': identifier['host_id'],
|
||||
'instance_id': new_instance_id,
|
||||
'instance_create_ts': new_instance_create_ts
|
||||
}, f)
|
||||
json.dump(
|
||||
{
|
||||
'host_id': identifier['host_id'],
|
||||
'instance_id': new_instance_id,
|
||||
'instance_create_ts': new_instance_create_ts,
|
||||
},
|
||||
f,
|
||||
)
|
||||
|
||||
identifier['instance_id'] = new_instance_id
|
||||
identifier['instance_create_ts'] = new_instance_create_ts
|
||||
@@ -80,6 +83,7 @@ def init():
|
||||
identifier['instance_id'] = loaded_instance_id
|
||||
identifier['instance_create_ts'] = loaded_instance_create_ts
|
||||
|
||||
|
||||
def print_out():
|
||||
global identifier
|
||||
print(identifier)
|
||||
|
||||
Reference in New Issue
Block a user