完善角色信息的添加

This commit is contained in:
997146918 2025-07-07 18:21:29 +08:00
parent 34bc37e864
commit 3c6679032d
2 changed files with 11 additions and 2 deletions

Binary file not shown.

View File

@ -95,6 +95,14 @@ async def handle_characterlist(client: WebSocket):
protocol["data"] = json.dumps(characterforUE)
await senddata(client, protocol)
async def handle_addcharacter(client: WebSocket, chracterJson: str):
### 添加角色到数据库 ###
character_info = json.loads(chracterJson)
id = database.add_character(character_info)
logger.log(logging.INFO, f"添加角色到数据库 id = {id}")
# id = database.add_character({"name":"张三","age":35,"personality":"成熟稳重/惜字如金","profession":"阿里巴巴算法工程师"
# ,"characterBackground":"浙大计算机系毕业专注AI优化项目","chat_style":"请在对话中表现出专业、冷静、惜字如金。用口语化的方式简短回答"})
async def process_protocol_json(json_str: str, client: WebSocket):
### 处理协议JSON ###
try:
@ -103,6 +111,8 @@ async def process_protocol_json(json_str: str, client: WebSocket):
data = protocol.get("data")
if cmd == "CharacterList":
await handle_characterlist(client)
elif cmd == "AddCharacter":
await handle_addcharacter(client, data)
except json.JSONDecodeError as e:
print(f"JSON解析错误: {e}")
@ -263,8 +273,7 @@ if __name__ == "__main__":
#Test database
database = DatabaseHandle()
id = database.add_character({"name":"张三","age":35,"personality":"成熟稳重/惜字如金","profession":"阿里巴巴算法工程师"
,"characterBackground":"浙大计算机系毕业专注AI优化项目","chat_style":"请在对话中表现出专业、冷静、惜字如金。用口语化的方式简短回答"})
id = database.add_character({"name":"李明","age":30,"personality":"活泼健谈","profession":"产品经理"
,"characterBackground":"公司资深产品经理","chat_style":"热情"})