feat: le-bot设备assistant_name替换为乐宝 + test页挂到8003端口

This commit is contained in:
SerenNoble 2026-07-15 10:02:57 +08:00
parent 8f9803a5fc
commit 71438fee7b
2 changed files with 108 additions and 94 deletions

View file

@ -235,7 +235,7 @@ public class ConfigServiceImpl implements ConfigService {
// 构建模块配置 // 构建模块配置
buildModuleConfig( buildModuleConfig(
agent.getAgentName(), resolveAssistantName(agent.getAgentName()),
prompt, prompt,
agent.getSummaryMemory(), agent.getSummaryMemory(),
voice, voice,
@ -607,9 +607,17 @@ public class ConfigServiceImpl implements ConfigService {
result.put("selected_module", selectedModule); result.put("selected_module", selectedModule);
if (StringUtils.isNotBlank(prompt)) { if (StringUtils.isNotBlank(prompt)) {
prompt = prompt.replace("{{assistant_name}}", StringUtils.isBlank(assistantName) ? "小智" : assistantName); assistantName = resolveAssistantName(assistantName);
prompt = prompt.replace("{{assistant_name}}", assistantName);
} }
result.put("prompt", prompt); result.put("prompt", prompt);
result.put("summaryMemory", summaryMemory); result.put("summaryMemory", summaryMemory);
} }
private String resolveAssistantName(String name) {
if (StringUtils.isBlank(name) || name.startsWith("le-bot-")) {
return "乐宝";
}
return name;
}
} }

View file

@ -1,92 +1,98 @@
import asyncio import asyncio
from aiohttp import web import os
from config.logger import setup_logging from aiohttp import web
from core.api.ota_handler import OTAHandler from config.logger import setup_logging
from core.api.vision_handler import VisionHandler from core.api.ota_handler import OTAHandler
from core.api.vision_handler import VisionHandler
TAG = __name__
TAG = __name__
class SimpleHttpServer:
def __init__(self, config: dict): class SimpleHttpServer:
self.config = config def __init__(self, config: dict):
self.logger = setup_logging() self.config = config
self.ota_handler = OTAHandler(config) self.logger = setup_logging()
self.vision_handler = VisionHandler(config) self.ota_handler = OTAHandler(config)
self.vision_handler = VisionHandler(config)
def _get_websocket_url(self, local_ip: str, port: int) -> str:
"""获取websocket地址 def _get_websocket_url(self, local_ip: str, port: int) -> str:
"""获取websocket地址
Args:
local_ip: 本地IP地址 Args:
port: 端口号 local_ip: 本地IP地址
port: 端口号
Returns:
str: websocket地址 Returns:
""" str: websocket地址
server_config = self.config["server"] """
websocket_config = server_config.get("websocket") server_config = self.config["server"]
websocket_config = server_config.get("websocket")
if websocket_config and "你" not in websocket_config:
return websocket_config if websocket_config and "你" not in websocket_config:
else: return websocket_config
return f"ws://{local_ip}:{port}/xiaozhi/v1/" else:
return f"ws://{local_ip}:{port}/xiaozhi/v1/"
async def start(self):
try: async def start(self):
server_config = self.config["server"] try:
read_config_from_api = self.config.get("read_config_from_api", False) server_config = self.config["server"]
host = server_config.get("ip", "0.0.0.0") read_config_from_api = self.config.get("read_config_from_api", False)
port = int(server_config.get("http_port", 8003)) host = server_config.get("ip", "0.0.0.0")
port = int(server_config.get("http_port", 8003))
if port:
app = web.Application() if port:
app = web.Application()
if not read_config_from_api:
# 如果没有开启智控台,只是单模块运行,就需要再添加简单OTA接口,用于下发websocket接口 if not read_config_from_api:
app.add_routes( # 如果没有开启智控台,只是单模块运行,就需要再添加简单OTA接口,用于下发websocket接口
[ app.add_routes(
web.get("/xiaozhi/ota/", self.ota_handler.handle_get), [
web.post("/xiaozhi/ota/", self.ota_handler.handle_post), web.get("/xiaozhi/ota/", self.ota_handler.handle_get),
web.options( web.post("/xiaozhi/ota/", self.ota_handler.handle_post),
"/xiaozhi/ota/", self.ota_handler.handle_options web.options(
), "/xiaozhi/ota/", self.ota_handler.handle_options
# 下载接口,仅提供 data/bin/*.bin 下载 ),
web.get( # 下载接口,仅提供 data/bin/*.bin 下载
"/xiaozhi/ota/download/{filename}", web.get(
self.ota_handler.handle_download, "/xiaozhi/ota/download/{filename}",
), self.ota_handler.handle_download,
web.options( ),
"/xiaozhi/ota/download/{filename}", web.options(
self.ota_handler.handle_options, "/xiaozhi/ota/download/{filename}",
), self.ota_handler.handle_options,
] ),
) ]
# 添加路由 )
app.add_routes( # 添加路由
[ # 测试页面
web.get("/mcp/vision/explain", self.vision_handler.handle_get), test_dir = os.path.join(os.path.dirname(os.path.dirname(__file__)), "test")
web.post( if os.path.isdir(test_dir):
"/mcp/vision/explain", self.vision_handler.handle_post app.router.add_static("/test", test_dir, name="test")
),
web.options( app.add_routes(
"/mcp/vision/explain", self.vision_handler.handle_options [
), web.get("/mcp/vision/explain", self.vision_handler.handle_get),
] web.post(
) "/mcp/vision/explain", self.vision_handler.handle_post
),
# 运行服务 web.options(
runner = web.AppRunner(app) "/mcp/vision/explain", self.vision_handler.handle_options
await runner.setup() ),
site = web.TCPSite(runner, host, port) ]
await site.start() )
# 保持服务运行 # 运行服务
while True: runner = web.AppRunner(app)
await asyncio.sleep(3600) # 每隔 1 小时检查一次 await runner.setup()
except Exception as e: site = web.TCPSite(runner, host, port)
self.logger.bind(tag=TAG).error(f"HTTP服务器启动失败: {e}") await site.start()
import traceback
# 保持服务运行
self.logger.bind(tag=TAG).error(f"错误堆栈: {traceback.format_exc()}") while True:
raise await asyncio.sleep(3600) # 每隔 1 小时检查一次
except Exception as e:
self.logger.bind(tag=TAG).error(f"HTTP服务器启动失败: {e}")
import traceback
self.logger.bind(tag=TAG).error(f"错误堆栈: {traceback.format_exc()}")
raise