Files
KiloStar/kilostar/api/chat.py
T
zhaoxi 99520c69d7 feat(system):优化后端
1.新增后端测试
2.增加了后端的加密
3.增加了i18n(国际化)
2026-05-31 15:39:34 +00:00

139 lines
4.3 KiB
Python

# Copyright 2026 zhaoxi826
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# 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.
from fastapi import APIRouter, Depends
from pydantic import BaseModel
from kilostar.utils.ray_hook import ray_actor_hook
from kilostar.utils.access import Accessor, TokenData
from kilostar.core.individual.regulatory_node.template import (
MessageRequest,
MessageResponse,
)
chat_router = APIRouter(prefix="/api/v1/chat", tags=["chat"])
def _extract_reply(resp: MessageResponse | None) -> str | None:
"""从 RegulatoryNode.working 的输出里取出对用户的回复文本。
RegulatoryNode 现在的 output_type 只剩 ``MessageResponse``(聊天/简单任务/汇报),
没有则视为节点降级为静默——上层不写回 chat history。
"""
if resp is None:
return None
return resp.reply_message
async def _ask_regulatory(
*, user_id: str, chat_id: str, message: str
) -> str | None:
"""统一封装 chat 入口对 RegulatoryNode 的调用。"""
regulatory_node = ray_actor_hook("regulatory_node").regulatory_node
payload = MessageRequest(
platform="client",
user_name=user_id,
platform_id=chat_id,
message=message,
)
resp: MessageResponse | None = await regulatory_node.working.remote(payload)
return _extract_reply(resp)
class CreateChatRequest(BaseModel):
title: str = "新对话"
initial_message: str
class SendMessageRequest(BaseModel):
message: str
@chat_router.post("")
async def create_chat_session(
request: CreateChatRequest,
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
chat = await postgres_database.create_chat_session.remote(
user_id=token_data.user_id, title=request.title
)
# 存入用户消息
await postgres_database.add_chat_message.remote(
chat_id=chat.chat_id, message=request.initial_message, message_owner="user"
)
# 调用监管节点处理简单任务/交流
response_msg = await _ask_regulatory(
user_id=token_data.user_id,
chat_id=chat.chat_id,
message=request.initial_message,
)
# 存入回复消息
if response_msg:
await postgres_database.add_chat_message.remote(
chat_id=chat.chat_id, message=response_msg, message_owner="regulatory_node"
)
return {"chat_id": chat.chat_id, "reply": response_msg}
@chat_router.get("")
async def list_chat_sessions(
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
sessions = await postgres_database.list_chat_sessions.remote(
user_id=token_data.user_id
)
return {"sessions": sessions}
@chat_router.get("/{chat_id}")
async def get_chat_history(
chat_id: str, token_data: TokenData = Depends(Accessor.get_current_user)
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
messages = await postgres_database.list_chat_messages.remote(chat_id=chat_id)
return {"messages": messages}
@chat_router.post("/{chat_id}/reply")
async def send_chat_message(
chat_id: str,
request: SendMessageRequest,
token_data: TokenData = Depends(Accessor.get_current_user),
):
postgres_database = ray_actor_hook("postgres_database").postgres_database
# 存用户消息
await postgres_database.add_chat_message.remote(
chat_id=chat_id, message=request.message, message_owner="user"
)
# 调用监管节点
response_msg = await _ask_regulatory(
user_id=token_data.user_id,
chat_id=chat_id,
message=request.message,
)
# 存回复
if response_msg:
await postgres_database.add_chat_message.remote(
chat_id=chat_id, message=response_msg, message_owner="regulatory_node"
)
return {"reply": response_msg}