mirror of
https://github.com/xinnan-tech/xiaozhi-esp32-server.git
synced 2026-07-21 22:53:56 +08:00
@@ -45,7 +45,7 @@ async def monitor_stdin():
|
|||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
check_ffmpeg_installed()
|
check_ffmpeg_installed()
|
||||||
config = load_config()
|
config = await load_config()
|
||||||
|
|
||||||
# auth_key优先级:配置文件server.auth_key > manager-api.secret > 自动生成
|
# auth_key优先级:配置文件server.auth_key > manager-api.secret > 自动生成
|
||||||
# auth_key用于jwt认证,比如视觉分析接口的jwt认证、ota接口的token生成与websocket认证
|
# auth_key用于jwt认证,比如视觉分析接口的jwt认证、ota接口的token生成与websocket认证
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ from config.manage_api_client import (
|
|||||||
get_server_config,
|
get_server_config,
|
||||||
get_agent_models,
|
get_agent_models,
|
||||||
get_correct_words,
|
get_correct_words,
|
||||||
lookup_address_book,
|
|
||||||
DeviceNotFoundException,
|
DeviceNotFoundException,
|
||||||
DeviceBindException,
|
DeviceBindException,
|
||||||
)
|
)
|
||||||
@@ -24,7 +23,7 @@ def read_config(config_path):
|
|||||||
return config
|
return config
|
||||||
|
|
||||||
|
|
||||||
def load_config():
|
async def load_config():
|
||||||
"""加载配置文件"""
|
"""加载配置文件"""
|
||||||
from core.utils.cache.manager import cache_manager, CacheType
|
from core.utils.cache.manager import cache_manager, CacheType
|
||||||
|
|
||||||
@@ -41,16 +40,7 @@ def load_config():
|
|||||||
custom_config = read_config(custom_config_path)
|
custom_config = read_config(custom_config_path)
|
||||||
|
|
||||||
if custom_config.get("manager-api", {}).get("url"):
|
if custom_config.get("manager-api", {}).get("url"):
|
||||||
import asyncio
|
config = await get_config_from_api_async(custom_config)
|
||||||
try:
|
|
||||||
loop = asyncio.get_running_loop()
|
|
||||||
# 如果已经在事件循环中,使用异步版本
|
|
||||||
config = asyncio.run_coroutine_threadsafe(
|
|
||||||
get_config_from_api_async(custom_config), loop
|
|
||||||
).result()
|
|
||||||
except RuntimeError:
|
|
||||||
# 如果不在事件循环中(启动时),创建新的事件循环
|
|
||||||
config = asyncio.run(get_config_from_api_async(custom_config))
|
|
||||||
else:
|
else:
|
||||||
# 合并配置
|
# 合并配置
|
||||||
config = merge_configs(default_config, custom_config)
|
config = merge_configs(default_config, custom_config)
|
||||||
@@ -139,7 +129,7 @@ def ensure_directories(config):
|
|||||||
selected_provider = selected_modules.get(module_type)
|
selected_provider = selected_modules.get(module_type)
|
||||||
if not selected_provider:
|
if not selected_provider:
|
||||||
continue
|
continue
|
||||||
if config.get(module) is None:
|
if config.get(module_type) is None:
|
||||||
continue
|
continue
|
||||||
if config.get(selected_provider) is None:
|
if config.get(selected_provider) is None:
|
||||||
continue
|
continue
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import os
|
import os
|
||||||
import sys
|
import sys
|
||||||
|
import asyncio
|
||||||
from loguru import logger
|
from loguru import logger
|
||||||
from config.config_loader import load_config
|
from config.config_loader import load_config
|
||||||
from config.settings import check_config_file
|
from config.settings import check_config_file
|
||||||
from datetime import datetime
|
from core.utils.cache.manager import cache_manager, CacheType
|
||||||
|
|
||||||
SERVER_VERSION = "0.9.5"
|
SERVER_VERSION = "0.9.5"
|
||||||
_logger_initialized = False
|
_logger_initialized = False
|
||||||
@@ -45,10 +46,15 @@ def formatter(record):
|
|||||||
return record["message"]
|
return record["message"]
|
||||||
|
|
||||||
|
|
||||||
def setup_logging():
|
def setup_logging(config=None):
|
||||||
check_config_file()
|
|
||||||
"""从配置文件中读取日志配置,并设置日志输出格式和级别"""
|
"""从配置文件中读取日志配置,并设置日志输出格式和级别"""
|
||||||
config = load_config()
|
if config is None:
|
||||||
|
check_config_file()
|
||||||
|
# 先查缓存,避免在 async 上下文中重复 await load_config
|
||||||
|
config = cache_manager.get(CacheType.CONFIG, "main_config")
|
||||||
|
if config is None:
|
||||||
|
# 缓存也没有(理论上不该发生),才走 asyncio.run
|
||||||
|
config = asyncio.run(load_config())
|
||||||
log_config = config["log"]
|
log_config = config["log"]
|
||||||
global _logger_initialized
|
global _logger_initialized
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import os
|
import os
|
||||||
|
import asyncio
|
||||||
from config.config_loader import read_config, get_project_dir, load_config
|
from config.config_loader import read_config, get_project_dir, load_config
|
||||||
|
|
||||||
|
|
||||||
@@ -20,7 +21,7 @@ def check_config_file():
|
|||||||
)
|
)
|
||||||
|
|
||||||
# 检查是否从API读取配置
|
# 检查是否从API读取配置
|
||||||
config = load_config()
|
config = asyncio.run(load_config())
|
||||||
if config.get("read_config_from_api", False):
|
if config.get("read_config_from_api", False):
|
||||||
print("从API读取配置")
|
print("从API读取配置")
|
||||||
old_config_origin = read_config(custom_config_file)
|
old_config_origin = read_config(custom_config_file)
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import json
|
import json
|
||||||
|
import asyncio
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from ..base import MemoryProviderBase, logger
|
from ..base import MemoryProviderBase, logger
|
||||||
@@ -59,7 +60,9 @@ class MemoryProvider(MemoryProviderBase):
|
|||||||
|
|
||||||
messages.append({"role": message.role, "content": content})
|
messages.append({"role": message.role, "content": content})
|
||||||
|
|
||||||
result = self.client.add(messages, user_id=self.role_id)
|
result = await asyncio.to_thread(
|
||||||
|
self.client.add, messages, user_id=self.role_id
|
||||||
|
)
|
||||||
logger.bind(tag=TAG).debug(f"Save memory result: {result}")
|
logger.bind(tag=TAG).debug(f"Save memory result: {result}")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.bind(tag=TAG).error(f"保存记忆失败: {str(e)}")
|
logger.bind(tag=TAG).error(f"保存记忆失败: {str(e)}")
|
||||||
@@ -84,7 +87,9 @@ class MemoryProvider(MemoryProviderBase):
|
|||||||
except (json.JSONDecodeError, KeyError):
|
except (json.JSONDecodeError, KeyError):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
results = self.client.search(search_query, filters=filters)
|
results = await asyncio.to_thread(
|
||||||
|
self.client.search, search_query, filters=filters
|
||||||
|
)
|
||||||
if not results or "results" not in results:
|
if not results or "results" not in results:
|
||||||
return ""
|
return ""
|
||||||
|
|
||||||
|
|||||||
@@ -186,13 +186,19 @@ class MemoryProvider(MemoryProviderBase):
|
|||||||
messages.append({"role": message.role, "content": content})
|
messages.append({"role": message.role, "content": content})
|
||||||
|
|
||||||
# Add memory using PowerMem SDK
|
# Add memory using PowerMem SDK
|
||||||
result = self.memory_client.add(
|
if self.enable_user_profile:
|
||||||
messages=messages,
|
# UserMemory uses sync add
|
||||||
user_id=self.role_id
|
result = await asyncio.to_thread(
|
||||||
)
|
self.memory_client.add,
|
||||||
# Handle both sync and async returns
|
messages=messages,
|
||||||
if asyncio.iscoroutine(result):
|
user_id=self.role_id
|
||||||
result = await result
|
)
|
||||||
|
else:
|
||||||
|
# AsyncMemory uses async add
|
||||||
|
result = await self.memory_client.add(
|
||||||
|
messages=messages,
|
||||||
|
user_id=self.role_id
|
||||||
|
)
|
||||||
|
|
||||||
logger.bind(tag=TAG).debug(f"Save memory result: {result}")
|
logger.bind(tag=TAG).debug(f"Save memory result: {result}")
|
||||||
|
|
||||||
|
|||||||
@@ -42,7 +42,7 @@ TAG = __name__
|
|||||||
class WebSocketServer:
|
class WebSocketServer:
|
||||||
def __init__(self, config: dict):
|
def __init__(self, config: dict):
|
||||||
self.config = config
|
self.config = config
|
||||||
self.logger = setup_logging()
|
self.logger = setup_logging(config)
|
||||||
self.config_lock = asyncio.Lock()
|
self.config_lock = asyncio.Lock()
|
||||||
modules = initialize_modules(
|
modules = initialize_modules(
|
||||||
self.logger,
|
self.logger,
|
||||||
|
|||||||
Reference in New Issue
Block a user