Refactor: reformat all code for lefthook using ruff and gofmt (#16585)

This commit is contained in:
Wang Qi
2026-07-03 12:53:39 +08:00
committed by GitHub
parent 19fcb4a981
commit 6a4b9be426
588 changed files with 11123 additions and 15412 deletions

View File

@@ -12,4 +12,4 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
#

View File

@@ -20,7 +20,7 @@ from .session import Session
class Agent(Base):
def __init__(self, rag, res_dict):
self.id = None
self.id = None
self.avatar = None
self.canvas_type = None
self.description = None
@@ -30,42 +30,17 @@ class Agent(Base):
class Dsl(Base):
def __init__(self, rag, res_dict):
self.answer = []
self.components = {
"begin": {
"downstream": ["Answer:China"],
"obj": {
"component_name": "Begin",
"params": {}
},
"upstream": []
}
}
self.components = {"begin": {"downstream": ["Answer:China"], "obj": {"component_name": "Begin", "params": {}}, "upstream": []}}
self.graph = {
"edges": [],
"nodes": [
{
"data": {
"label": "Begin",
"name": "begin"
},
"id": "begin",
"position": {
"x": 50,
"y": 200
},
"sourcePosition": "left",
"targetPosition": "right",
"type": "beginNode"
}
]
"nodes": [{"data": {"label": "Begin", "name": "begin"}, "id": "begin", "position": {"x": 50, "y": 200}, "sourcePosition": "left", "targetPosition": "right", "type": "beginNode"}],
}
self.history = []
self.messages = []
self.path = []
self.history = []
self.messages = []
self.path = []
self.reference = []
super().__init__(rag, res_dict)
def create_session(self, **kwargs) -> Session:
res = self.post(f"/agents/{self.id}/sessions", json=kwargs)
res = res.json()
@@ -73,11 +48,8 @@ class Agent(Base):
return Session(self.rag, res.get("data"))
raise Exception(res.get("message"))
def list_sessions(self, page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True,
id: str = None) -> list[Session]:
res = self.get(f"/agents/{self.id}/sessions",
{"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id})
def list_sessions(self, page: int = 1, page_size: int = 30, orderby: str = "create_time", desc: bool = True, id: str = None) -> list[Session]:
res = self.get(f"/agents/{self.id}/sessions", {"page": page, "page_size": page_size, "orderby": orderby, "desc": desc, "id": id})
res = res.json()
if res.get("code") == 0:
result_list = []
@@ -86,7 +58,7 @@ class Agent(Base):
result_list.append(temp_agent)
return result_list
raise Exception(res.get("message"))
def delete_sessions(self, ids: list[str] | None = None, delete_all: bool = False):
payload = {"ids": ids}
if delete_all:

View File

@@ -16,6 +16,7 @@
from .base import Base
class ChunkUpdateError(Exception):
def __init__(self, code=None, message=None, details=None):
self.code = code
@@ -23,6 +24,7 @@ class ChunkUpdateError(Exception):
self.details = details
super().__init__(message)
class Chunk(Base):
def __init__(self, rag, res_dict):
self.id = ""
@@ -48,17 +50,12 @@ class Chunk(Base):
res_dict.pop(k)
super().__init__(rag, res_dict)
#for backward compatibility
# for backward compatibility
if not self.document_name:
self.document_name = self.document_keyword
def update(self, update_message: dict):
res = self.patch(f"/datasets/{self.dataset_id}/documents/{self.document_id}/chunks/{self.id}", update_message)
res = res.json()
if res.get("code") != 0:
raise ChunkUpdateError(
code=res.get("code"),
message=res.get("message"),
details=res.get("details")
)
raise ChunkUpdateError(code=res.get("code"), message=res.get("message"), details=res.get("details"))

View File

@@ -113,18 +113,21 @@ class DataSet(Base):
def _get_documents_status(self, document_ids):
import time
terminal_states = {"DONE", "FAIL", "CANCEL"}
interval_sec = 1
pending = set(document_ids)
finished = []
while pending:
for doc_id in list(pending):
def fetch_doc(doc_id: str) -> Document | None:
try:
docs = self.list_documents(id=doc_id)
return docs[0] if docs else None
except Exception:
return None
doc = fetch_doc(doc_id)
if doc is None:
continue
@@ -137,13 +140,12 @@ class DataSet(Base):
if pending:
time.sleep(interval_sec)
return finished
def async_parse_documents(self, document_ids):
res = self.post(f"/datasets/{self.id}/chunks", {"document_ids": document_ids})
res = res.json()
if res.get("code") != 0:
raise Exception(res.get("message"))
def parse_documents(self, document_ids):
try:
@@ -151,9 +153,8 @@ class DataSet(Base):
self._get_documents_status(document_ids)
except KeyboardInterrupt:
self.async_cancel_parse_documents(document_ids)
return self._get_documents_status(document_ids)
return self._get_documents_status(document_ids)
def async_cancel_parse_documents(self, document_ids):
res = self.rm(f"/datasets/{self.id}/chunks", {"document_ids": document_ids})

View File

@@ -18,7 +18,6 @@ from .base import Base
class Memory(Base):
def __init__(self, rag, res_dict):
self.id = ""
self.name = ""
@@ -33,7 +32,7 @@ class Memory(Base):
self.description = ""
self.memory_size = 5 * 1024 * 1024
self.forgetting_policy = "FIFO"
self.temperature = 0.5,
self.temperature = (0.5,)
self.system_prompt = ""
self.user_prompt = ""
for k in list(res_dict.keys()):
@@ -57,13 +56,8 @@ class Memory(Base):
self._update_from_dict(self.rag, res.get("data", {}))
return self
def list_memory_messages(self, agent_id: str | list[str]=None, keywords: str=None, page: int=1, page_size: int=50):
params = {
"agent_id": agent_id,
"keywords": keywords,
"page": page,
"page_size": page_size
}
def list_memory_messages(self, agent_id: str | list[str] = None, keywords: str = None, page: int = 1, page_size: int = 50):
params = {"agent_id": agent_id, "keywords": keywords, "page": page, "page_size": page_size}
res = self.get(f"/memories/{self.id}", params)
res = res.json()
if res.get("code") != 0:
@@ -78,9 +72,7 @@ class Memory(Base):
return True
def update_message_status(self, message_id: int, status: bool):
update_message = {
"status": status
}
update_message = {"status": status}
res = self.put(f"/messages/{self.id}:{message_id}", update_message)
res = res.json()
if res.get("code") != 0:

View File

@@ -36,7 +36,6 @@ class Session(Base):
self.__session_type = "agent"
super().__init__(rag, res_dict)
def ask(
self,
question="",
@@ -89,8 +88,7 @@ class Session(Base):
if inputs is not None or release is not None or return_trace is not None:
logger.debug(
"Session.ask explicit-params session_type=%s session_id=%s "
"input_keys=%s release=%s return_trace=%s",
"Session.ask explicit-params session_type=%s session_id=%s input_keys=%s release=%s return_trace=%s",
self.__session_type,
getattr(self, "id", None),
list(inputs.keys()) if isinstance(inputs, dict) else None,
@@ -111,7 +109,7 @@ class Session(Base):
continue # Skip empty lines
line = line.strip()
if line.startswith("data:"):
content = line[len("data:"):].strip()
content = line[len("data:") :].strip()
if content == "[DONE]":
break # End of stream
else:
@@ -122,14 +120,11 @@ class Session(Base):
except json.JSONDecodeError:
continue # Skip lines that are not valid JSON
event = json_data.get("event",None)
event = json_data.get("event", None)
if event and event != "message":
continue
if (
(self.__session_type == "agent" and event == "message_end")
or (self.__session_type == "chat" and json_data.get("data") is True)
):
if (self.__session_type == "agent" and event == "message_end") or (self.__session_type == "chat" and json_data.get("data") is True):
return
if self.__session_type == "agent":
yield self._structure_answer(json_data)
@@ -141,7 +136,6 @@ class Session(Base):
except ValueError:
raise Exception(f"Invalid response {res}")
yield self._structure_answer(json_data["data"])
def _structure_answer(self, json_data):
answer = ""
@@ -150,10 +144,7 @@ class Session(Base):
elif self.__session_type == "chat":
answer = json_data["answer"]
reference = json_data.get("reference", {})
temp_dict = {
"content": answer,
"role": "assistant"
}
temp_dict = {"content": answer, "role": "assistant"}
if reference and "chunks" in reference:
chunks = reference["chunks"]
temp_dict["reference"] = chunks
@@ -163,8 +154,7 @@ class Session(Base):
def _ask_chat(self, question: str, stream: bool, **kwargs):
json_data = {"question": question, "stream": stream, "session_id": self.id}
json_data.update(kwargs)
res = self.post(f"/chats/{self.chat_id}/completions",
json_data, stream=stream)
res = self.post(f"/chats/{self.chat_id}/completions", json_data, stream=stream)
return res
def _ask_agent(self, question: str, stream: bool, **kwargs):
@@ -180,8 +170,7 @@ class Session(Base):
return res
def update(self, update_message):
res = self.patch(f"/chats/{self.chat_id}/sessions/{self.id}",
update_message)
res = self.patch(f"/chats/{self.chat_id}/sessions/{self.id}", update_message)
res = res.json()
if res.get("code") != 0:
raise Exception(res.get("message"))