liumaolin
commited on
Commit
·
92bb56d
1
Parent(s):
6f036c6
Refactor session ID handling: replace `current_session_id` with `SessionIdManager` for thread-safe management, update related imports and references.
Browse files
src/VoiceDialogue/api/routes/system_routes.py
CHANGED
|
@@ -4,6 +4,7 @@ import time
|
|
| 4 |
|
| 5 |
from fastapi import APIRouter, HTTPException, BackgroundTasks, Request
|
| 6 |
|
|
|
|
| 7 |
from ..core.service_factories import get_audio_capture_service_definition
|
| 8 |
from ..schemas.system_schemas import (
|
| 9 |
SystemStatusResponse, SystemResponse
|
|
@@ -77,6 +78,7 @@ async def start_system(
|
|
| 77 |
|
| 78 |
# 更新状态
|
| 79 |
_system_status["status"] = "starting"
|
|
|
|
| 80 |
|
| 81 |
# 在后台启动系统
|
| 82 |
background_tasks.add_task(
|
|
@@ -162,7 +164,7 @@ async def restart_system(
|
|
| 162 |
await stop_system(fastapi_request)
|
| 163 |
|
| 164 |
# 再启动
|
| 165 |
-
return await start_system(
|
| 166 |
|
| 167 |
except Exception as e:
|
| 168 |
logger.error(f"系统重启失败: {e}", exc_info=True)
|
|
|
|
| 4 |
|
| 5 |
from fastapi import APIRouter, HTTPException, BackgroundTasks, Request
|
| 6 |
|
| 7 |
+
from core.constants import session_manager
|
| 8 |
from ..core.service_factories import get_audio_capture_service_definition
|
| 9 |
from ..schemas.system_schemas import (
|
| 10 |
SystemStatusResponse, SystemResponse
|
|
|
|
| 78 |
|
| 79 |
# 更新状态
|
| 80 |
_system_status["status"] = "starting"
|
| 81 |
+
session_manager.reset_id()
|
| 82 |
|
| 83 |
# 在后台启动系统
|
| 84 |
background_tasks.add_task(
|
|
|
|
| 164 |
await stop_system(fastapi_request)
|
| 165 |
|
| 166 |
# 再启动
|
| 167 |
+
return await start_system(fastapi_request, background_tasks)
|
| 168 |
|
| 169 |
except Exception as e:
|
| 170 |
logger.error(f"系统重启失败: {e}", exc_info=True)
|
src/VoiceDialogue/core/constants.py
CHANGED
|
@@ -1,9 +1,9 @@
|
|
| 1 |
import multiprocessing
|
| 2 |
import threading
|
| 3 |
-
import uuid
|
| 4 |
from collections import OrderedDict
|
| 5 |
|
| 6 |
from utils.cache import LRUCacheDict
|
|
|
|
| 7 |
from .state_manager import VoiceStateManager
|
| 8 |
|
| 9 |
# ======================= 音频配置常量 =======================
|
|
@@ -44,7 +44,7 @@ voice_state_manager = VoiceStateManager()
|
|
| 44 |
|
| 45 |
# 会话缓存
|
| 46 |
chat_history_cache: dict[str, OrderedDict] = {}
|
| 47 |
-
|
| 48 |
dropped_audio_cache = LRUCacheDict(maxsize=50)
|
| 49 |
|
| 50 |
# ======================= 线程事件对象 =======================
|
|
|
|
| 1 |
import multiprocessing
|
| 2 |
import threading
|
|
|
|
| 3 |
from collections import OrderedDict
|
| 4 |
|
| 5 |
from utils.cache import LRUCacheDict
|
| 6 |
+
from .session_id_manager import SessionIdManager
|
| 7 |
from .state_manager import VoiceStateManager
|
| 8 |
|
| 9 |
# ======================= 音频配置常量 =======================
|
|
|
|
| 44 |
|
| 45 |
# 会话缓存
|
| 46 |
chat_history_cache: dict[str, OrderedDict] = {}
|
| 47 |
+
session_manager: SessionIdManager = SessionIdManager()
|
| 48 |
dropped_audio_cache = LRUCacheDict(maxsize=50)
|
| 49 |
|
| 50 |
# ======================= 线程事件对象 =======================
|
src/VoiceDialogue/core/session_id_manager.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import threading
|
| 2 |
+
import uuid
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
class SessionIdManager:
|
| 6 |
+
def __init__(self):
|
| 7 |
+
self._current_id: str = f'{uuid.uuid4()}'
|
| 8 |
+
self._lock = threading.Lock() # 为线程安全添加锁
|
| 9 |
+
|
| 10 |
+
def get_id(self) -> str:
|
| 11 |
+
"""获取当前的会话ID (线程安全)。"""
|
| 12 |
+
with self._lock:
|
| 13 |
+
return self._current_id
|
| 14 |
+
|
| 15 |
+
def set_id(self, new_id: str) -> None:
|
| 16 |
+
"""设置新的会话ID (线程安全)。"""
|
| 17 |
+
with self._lock:
|
| 18 |
+
self._current_id = new_id
|
| 19 |
+
|
| 20 |
+
def reset_id(self) -> str:
|
| 21 |
+
"""生成一个新的UUID作为会话ID,并更新当前ID,然后返回新的ID (线程安全)。"""
|
| 22 |
+
with self._lock:
|
| 23 |
+
self._current_id = f'{uuid.uuid4()}'
|
| 24 |
+
return self._current_id
|
| 25 |
+
|
| 26 |
+
@property
|
| 27 |
+
def current_id(self) -> str:
|
| 28 |
+
"""通过属性方式获取当前的会话ID (线程安全)。"""
|
| 29 |
+
return self.get_id()
|
| 30 |
+
|
| 31 |
+
@current_id.setter
|
| 32 |
+
def current_id(self, value: str) -> None:
|
| 33 |
+
"""通过属性方式设置新的会话ID (线程安全)。"""
|
| 34 |
+
self.set_id(value)
|
src/VoiceDialogue/services/speech/monitor.py
CHANGED
|
@@ -13,12 +13,12 @@ from queue import Empty
|
|
| 13 |
import librosa
|
| 14 |
import numpy as np
|
| 15 |
|
| 16 |
-
from models.voice_task import VoiceTask
|
| 17 |
from core.base import BaseThread
|
| 18 |
from core.constants import (
|
| 19 |
-
voice_state_manager, silence_over_threshold_event, user_still_speaking_event,
|
| 20 |
)
|
| 21 |
from core.enums import AudioState
|
|
|
|
| 22 |
|
| 23 |
|
| 24 |
class SpeechMonitorConfig:
|
|
@@ -185,7 +185,7 @@ class SpeechStateMonitor(BaseThread):
|
|
| 185 |
Returns:
|
| 186 |
VoiceTask: 创建的语音任务
|
| 187 |
"""
|
| 188 |
-
voice_task = VoiceTask(id=self.task_id, session_id=
|
| 189 |
voice_task.answer_id = f'{uuid.uuid4()}'
|
| 190 |
voice_task.user_voice = audio_frames.copy()
|
| 191 |
voice_task.send_time = time.time()
|
|
|
|
| 13 |
import librosa
|
| 14 |
import numpy as np
|
| 15 |
|
|
|
|
| 16 |
from core.base import BaseThread
|
| 17 |
from core.constants import (
|
| 18 |
+
voice_state_manager, silence_over_threshold_event, user_still_speaking_event, session_manager
|
| 19 |
)
|
| 20 |
from core.enums import AudioState
|
| 21 |
+
from models.voice_task import VoiceTask
|
| 22 |
|
| 23 |
|
| 24 |
class SpeechMonitorConfig:
|
|
|
|
| 185 |
Returns:
|
| 186 |
VoiceTask: 创建的语音任务
|
| 187 |
"""
|
| 188 |
+
voice_task = VoiceTask(id=self.task_id, session_id=session_manager.current_id)
|
| 189 |
voice_task.answer_id = f'{uuid.uuid4()}'
|
| 190 |
voice_task.user_voice = audio_frames.copy()
|
| 191 |
voice_task.send_time = time.time()
|