neuro-simulator 0.1.3__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- neuro_simulator/__init__.py +1 -10
- neuro_simulator/agent/__init__.py +1 -8
- neuro_simulator/agent/base.py +43 -0
- neuro_simulator/agent/core.py +105 -398
- neuro_simulator/agent/factory.py +30 -0
- neuro_simulator/agent/llm.py +34 -31
- neuro_simulator/agent/memory/__init__.py +1 -4
- neuro_simulator/agent/memory/manager.py +61 -203
- neuro_simulator/agent/tools/__init__.py +1 -4
- neuro_simulator/agent/tools/core.py +8 -18
- neuro_simulator/api/__init__.py +1 -0
- neuro_simulator/api/agent.py +163 -0
- neuro_simulator/api/stream.py +55 -0
- neuro_simulator/api/system.py +90 -0
- neuro_simulator/cli.py +60 -143
- neuro_simulator/core/__init__.py +1 -0
- neuro_simulator/core/agent_factory.py +52 -0
- neuro_simulator/core/agent_interface.py +91 -0
- neuro_simulator/core/application.py +278 -0
- neuro_simulator/services/__init__.py +1 -0
- neuro_simulator/{chatbot.py → services/audience.py} +24 -24
- neuro_simulator/{audio_synthesis.py → services/audio.py} +18 -15
- neuro_simulator/services/builtin.py +87 -0
- neuro_simulator/services/letta.py +206 -0
- neuro_simulator/{stream_manager.py → services/stream.py} +39 -47
- neuro_simulator/utils/__init__.py +1 -0
- neuro_simulator/utils/logging.py +90 -0
- neuro_simulator/utils/process.py +67 -0
- neuro_simulator/{stream_chat.py → utils/queue.py} +17 -4
- neuro_simulator/utils/state.py +14 -0
- neuro_simulator/{websocket_manager.py → utils/websocket.py} +18 -14
- {neuro_simulator-0.1.3.dist-info → neuro_simulator-0.2.1.dist-info}/METADATA +83 -33
- neuro_simulator-0.2.1.dist-info/RECORD +37 -0
- neuro_simulator/agent/api.py +0 -737
- neuro_simulator/agent/memory.py +0 -137
- neuro_simulator/agent/tools.py +0 -69
- neuro_simulator/builtin_agent.py +0 -83
- neuro_simulator/config.yaml.example +0 -157
- neuro_simulator/letta.py +0 -164
- neuro_simulator/log_handler.py +0 -43
- neuro_simulator/main.py +0 -673
- neuro_simulator/media/neuro_start.mp4 +0 -0
- neuro_simulator/process_manager.py +0 -70
- neuro_simulator/shared_state.py +0 -11
- neuro_simulator-0.1.3.dist-info/RECORD +0 -31
- /neuro_simulator/{config.py → core/config.py} +0 -0
- {neuro_simulator-0.1.3.dist-info → neuro_simulator-0.2.1.dist-info}/WHEEL +0 -0
- {neuro_simulator-0.1.3.dist-info → neuro_simulator-0.2.1.dist-info}/entry_points.txt +0 -0
- {neuro_simulator-0.1.3.dist-info → neuro_simulator-0.2.1.dist-info}/top_level.txt +0 -0
@@ -1,45 +1,49 @@
|
|
1
|
-
#
|
2
|
-
from fastapi import WebSocket
|
3
|
-
from collections import deque
|
1
|
+
# neuro_simulator/utils/websocket.py
|
4
2
|
import asyncio
|
5
3
|
import json
|
6
|
-
|
4
|
+
import logging
|
5
|
+
from collections import deque
|
6
|
+
|
7
|
+
from fastapi import WebSocket
|
8
|
+
from starlette.websockets import WebSocketState
|
9
|
+
|
10
|
+
logger = logging.getLogger(__name__.replace("neuro_simulator", "server", 1))
|
7
11
|
|
8
12
|
class WebSocketManager:
|
9
|
-
"""
|
13
|
+
"""Manages all active WebSocket connections and provides broadcasting capabilities."""
|
10
14
|
def __init__(self):
|
11
15
|
self.active_connections: deque[WebSocket] = deque()
|
12
|
-
|
16
|
+
logger.info("WebSocketManager initialized.")
|
13
17
|
|
14
18
|
async def connect(self, websocket: WebSocket):
|
15
19
|
await websocket.accept()
|
16
20
|
self.active_connections.append(websocket)
|
17
|
-
|
21
|
+
logger.info(f"WebSocket client connected. Total connections: {len(self.active_connections)}")
|
18
22
|
|
19
23
|
def disconnect(self, websocket: WebSocket):
|
20
24
|
try:
|
21
25
|
if websocket in self.active_connections:
|
22
26
|
self.active_connections.remove(websocket)
|
23
|
-
|
27
|
+
logger.info(f"WebSocket client disconnected. Total connections: {len(self.active_connections)}")
|
24
28
|
except Exception as e:
|
25
|
-
|
29
|
+
logger.error(f"Error during WebSocket disconnect: {e}")
|
26
30
|
|
27
31
|
async def send_personal_message(self, message: dict, websocket: WebSocket):
|
28
32
|
if websocket.client_state == WebSocketState.CONNECTED:
|
29
33
|
try:
|
30
34
|
await websocket.send_json(message)
|
31
35
|
except Exception as e:
|
32
|
-
|
36
|
+
logger.warning(f"Could not send personal message, client likely disconnected: {e}")
|
33
37
|
self.disconnect(websocket)
|
34
38
|
|
35
39
|
async def broadcast(self, message: dict):
|
36
40
|
disconnected_sockets = []
|
37
|
-
for connection in list(self.active_connections):
|
41
|
+
for connection in list(self.active_connections):
|
38
42
|
if connection.client_state == WebSocketState.CONNECTED:
|
39
43
|
try:
|
40
44
|
await connection.send_json(message)
|
41
45
|
except Exception as e:
|
42
|
-
|
46
|
+
logger.warning(f"Could not broadcast message to client {connection}, it may have disconnected: {e}")
|
43
47
|
disconnected_sockets.append(connection)
|
44
48
|
else:
|
45
49
|
disconnected_sockets.append(connection)
|
@@ -47,5 +51,5 @@ class WebSocketManager:
|
|
47
51
|
for disconnected_socket in disconnected_sockets:
|
48
52
|
self.disconnect(disconnected_socket)
|
49
53
|
|
50
|
-
#
|
51
|
-
connection_manager = WebSocketManager()
|
54
|
+
# Global singleton instance
|
55
|
+
connection_manager = WebSocketManager()
|
@@ -1,6 +1,6 @@
|
|
1
1
|
Metadata-Version: 2.4
|
2
2
|
Name: neuro_simulator
|
3
|
-
Version: 0.1
|
3
|
+
Version: 0.2.1
|
4
4
|
Summary: Neuro Simulator Server
|
5
5
|
Author-email: Moha-Master <hongkongreporter@outlook.com>
|
6
6
|
License-Expression: MIT
|
@@ -39,7 +39,7 @@ Requires-Dist: flake8; extra == "dev"
|
|
39
39
|
|
40
40
|
*本临时README由AI自动生成*
|
41
41
|
|
42
|
-
这是 Neuro Simulator 的服务端,负责处理直播逻辑、AI 交互、TTS
|
42
|
+
这是 Neuro Simulator 的服务端,负责处理直播逻辑、AI 交互、TTS 合成等核心功能
|
43
43
|
|
44
44
|
## 功能特性
|
45
45
|
|
@@ -51,49 +51,99 @@ Requires-Dist: flake8; extra == "dev"
|
|
51
51
|
|
52
52
|
``` main
|
53
53
|
neuro_simulator/
|
54
|
-
├──
|
55
|
-
├── config.py # 配置管理模块
|
56
|
-
├── letta.py # Letta Agent 集成
|
57
|
-
├── chatbot.py # 观众聊天生成器
|
58
|
-
├── audio_synthesis.py # 音频合成模块
|
59
|
-
├── stream_chat.py # 聊天消息处理
|
60
|
-
├── stream_manager.py # 直播管理器
|
61
|
-
├── websocket_manager.py # WebSocket 连接管理
|
62
|
-
├── process_manager.py # 进程管理器
|
63
|
-
├── shared_state.py # 全局状态管理
|
64
|
-
├── log_handler.py # 日志处理模块
|
65
|
-
├── requirements.txt # Python 依赖列表
|
66
|
-
├── pyproject.toml # Python 包安装配置
|
54
|
+
├── __init__.py
|
67
55
|
├── cli.py # 命令行启动脚本
|
68
|
-
├──
|
69
|
-
|
70
|
-
|
56
|
+
├── core/ # 核心模块
|
57
|
+
│ ├── __init__.py
|
58
|
+
│ ├── application.py # FastAPI应用和主要路由
|
59
|
+
│ ├── config.py # 配置管理模块
|
60
|
+
│ ├── agent_factory.py # Agent工厂模式实现
|
61
|
+
│ ├── agent_interface.py # Agent接口定义
|
62
|
+
│ └── config.yaml.example # 自带的备用配置模板
|
63
|
+
├── agent/ # 内建Agent模块
|
64
|
+
│ ├── __init__.py
|
65
|
+
│ ├── base.py # Agent基类
|
66
|
+
│ ├── core.py # Agent核心实现
|
67
|
+
│ ├── factory.py # Agent工厂
|
68
|
+
│ ├── llm.py # LLM客户端
|
69
|
+
│ ├── memory/ # 记忆管理模块
|
70
|
+
│ │ ├── __init__.py
|
71
|
+
│ │ ├── manager.py # 记忆管理器
|
72
|
+
│ │ ├── context.json # 上下文记忆文件
|
73
|
+
│ │ ├── core_memory.json # 核心记忆文件
|
74
|
+
│ │ ├── init_memory.json # 初始化记忆文件
|
75
|
+
│ │ └── temp_memory.json # 临时记忆文件
|
76
|
+
│ └── tools/ # 工具模块
|
77
|
+
│ ├── __init__.py
|
78
|
+
│ └── core.py # 核心工具实现
|
79
|
+
├── api/ # API路由模块
|
80
|
+
│ ├── __init__.py
|
81
|
+
│ ├── agent.py # Agent管理API
|
82
|
+
│ ├── stream.py # 直播控制API
|
83
|
+
│ └── system.py # 系统管理API
|
84
|
+
├── services/ # 服务模块
|
85
|
+
│ ├── __init__.py
|
86
|
+
│ ├── audience.py # 观众聊天生成器
|
87
|
+
│ ├── audio.py # 音频合成模块
|
88
|
+
│ ├── builtin.py # 内建Agent服务
|
89
|
+
│ ├── letta.py # Letta Agent 集成
|
90
|
+
│ └── stream.py # 直播管理服务
|
91
|
+
├── utils/ # 工具模块
|
92
|
+
│ ├── __init__.py
|
93
|
+
│ ├── logging.py # 日志处理模块
|
94
|
+
│ ├── process.py # 进程管理模块
|
95
|
+
│ ├── queue.py # 队列处理模块
|
96
|
+
│ ├── state.py # 状态管理模块
|
97
|
+
│ └── websocket.py # WebSocket连接管理
|
98
|
+
├── assets/ # 自带的备用媒体文件
|
99
|
+
│ └── neuro_start.mp4 # 用来计算Start Soon长度,仅读取时长
|
100
|
+
├── requirements.txt # Python 依赖列表
|
101
|
+
└── pyproject.toml # Python 包安装配置
|
71
102
|
```
|
72
103
|
|
73
104
|
``` workin'dir
|
74
105
|
working_dir_example/ # 工作目录结构,请将这个目录重命名和复制到你想要的位置(推荐放到~/.config/neuro-simulator)
|
75
|
-
├──
|
106
|
+
├── assets/ # 媒体文件夹,如缺失会使用自带资源覆盖
|
76
107
|
│ └── neuro_start.mp4 # 用来计算Start Soon长度,仅读取时长,请和客户端的视频保持一致
|
77
|
-
├── config.yaml
|
78
|
-
|
108
|
+
├── config.yaml # 由用户手工创建的配置文件
|
109
|
+
├── config.yaml.example # 自动生成的配置文件模板,必须手动重命名和填写
|
110
|
+
└── agent/ # Agent相关文件夹
|
111
|
+
└── memory/ # Agent记忆文件夹
|
112
|
+
├── context.json # 上下文记忆文件
|
113
|
+
├── core_memory.json # 核心记忆文件
|
114
|
+
├── init_memory.json # 初始化记忆文件
|
115
|
+
└── temp_memory.json # 临时记忆文件
|
79
116
|
```
|
80
117
|
|
81
118
|
## 安装与配置
|
82
119
|
|
83
|
-
0. **在运行server前,必须有已经配置完成的Letta Agent。**
|
84
120
|
1. 复制一份 `../docs/working_dir_example` 到你想要的位置,作为配置文件目录.
|
85
121
|
- 程序会在未指定 `--dir` 的情况下自动生成一个工作目录,路径为 `~/.config/neuro-simulator/`
|
86
122
|
2. 然后进入配置文件目录,复制 `config.yaml.example` 到 `config.yaml`
|
87
123
|
3. 编辑 `config.yaml` 文件,填入必要的 API 密钥和配置项:
|
88
|
-
- Letta Token 和 Agent ID
|
89
|
-
- Gemini/OpenAI API Key
|
124
|
+
- 如果使用 Letta Agent,需要配置 Letta Token 和 Agent ID
|
125
|
+
- Gemini/OpenAI API Key(用于观众聊天生成和 Agent)
|
90
126
|
- Azure TTS Key 和 Region
|
91
127
|
|
92
|
-
|
128
|
+
可以自行替换 `$dir/assets/neuro_start.mp4` 为其它视频文件,但记得手动替换 client 中的同名文件
|
129
|
+
|
130
|
+
### Agent配置
|
131
|
+
|
132
|
+
服务端支持两种Agent类型:
|
133
|
+
1. **Letta Agent**:需要配置 Letta Cloud 或自托管的 Letta Server
|
134
|
+
2. **内建 Agent**:使用服务端自带的 Agent,支持 Gemini 和OpenAI API
|
135
|
+
|
136
|
+
在 `config.yaml` 中通过 `agent_type` 字段选择使用的 Agent 类型:
|
137
|
+
- `agent_type: "letta"`:使用 Letta Agent
|
138
|
+
- `agent_type: "builtin"`:使用内建 Agent
|
139
|
+
|
140
|
+
当使用内建Agent时,还需要配置:
|
141
|
+
- `agent.agent_provider`:选择"gemini"或"openai"
|
142
|
+
- `agent.agent_model`:指定具体的模型名称
|
93
143
|
|
94
144
|
### 直接安装方式(无需二次开发)
|
95
145
|
|
96
|
-
若无需二次开发,可以直接使用pip安装:
|
146
|
+
若无需二次开发,可以直接使用 pip 安装:
|
97
147
|
```bash
|
98
148
|
python3 -m venv venv
|
99
149
|
# Windows
|
@@ -104,7 +154,7 @@ venv/bin/pip install neuro-simulator
|
|
104
154
|
|
105
155
|
### 二次开发方式
|
106
156
|
|
107
|
-
|
157
|
+
若需要二次开发,请克隆项目:
|
108
158
|
```bash
|
109
159
|
git clone https://github.com/your-username/Neuro-Simulator.git
|
110
160
|
cd Neuro-Simulator/server
|
@@ -131,7 +181,7 @@ neuro -H 0.0.0.0 -P 8080
|
|
131
181
|
neuro -D /path/to/your/config -H 0.0.0.0 -P 8080
|
132
182
|
```
|
133
183
|
|
134
|
-
服务默认运行在 `http://127.0.0.1:8000
|
184
|
+
服务默认运行在 `http://127.0.0.1:8000`
|
135
185
|
|
136
186
|
## API 接口
|
137
187
|
|
@@ -139,14 +189,14 @@ neuro -D /path/to/your/config -H 0.0.0.0 -P 8080
|
|
139
189
|
|
140
190
|
- `/api/stream/*` - 直播控制接口(启动/停止/重启/状态)
|
141
191
|
- `/api/configs/*` - 配置管理接口(获取/更新/重载配置)
|
142
|
-
- `api_keys` `server`
|
192
|
+
- `api_keys` `server` 等敏感配置项无法从接口获取和修改
|
143
193
|
- `/api/logs` - 日志获取接口
|
144
194
|
- `/api/tts/synthesize` - TTS 合成接口
|
145
195
|
- `/api/system/health` - 健康检查接口
|
146
|
-
- `/ws/stream` -
|
147
|
-
- `/ws/
|
196
|
+
- `/ws/stream` - 客户端使用的直播接口
|
197
|
+
- `/ws/admin` - 日志和内建 Agent的 Context 流接口
|
148
198
|
|
149
|
-
详细接口说明可通过 `http://127.0.0.1:8000/docs` 访问 API
|
199
|
+
详细接口说明可通过 `http://127.0.0.1:8000/docs` 访问 API 文档查
|
150
200
|
|
151
201
|
## 配置说明
|
152
202
|
|
@@ -160,7 +210,7 @@ neuro -D /path/to/your/config -H 0.0.0.0 -P 8080
|
|
160
210
|
- `performance` - 性能相关设置
|
161
211
|
- `server` - 服务器设置(主机、端口、CORS 等)
|
162
212
|
|
163
|
-
有关配置文件的完整示例,请参阅项目根目录下的 `docs/working_dir_example/`
|
213
|
+
有关配置文件的完整示例,请参阅项目根目录下的 `docs/working_dir_example/` 文件夹
|
164
214
|
|
165
215
|
## 安全说明
|
166
216
|
|
@@ -0,0 +1,37 @@
|
|
1
|
+
neuro_simulator/__init__.py,sha256=-tposzyvg6UckPcfSvtc03UjxBa9oCe_zRvlKf8splk,31
|
2
|
+
neuro_simulator/cli.py,sha256=p2jSNbMsQ2HYau-EX3ygmTRhECU9dbSg0v53xgYjOZ4,3894
|
3
|
+
neuro_simulator/agent/__init__.py,sha256=t52CZlyTGWqcGjMs90qvpFpRckY2WSSlO7r_H3K_mSY,32
|
4
|
+
neuro_simulator/agent/base.py,sha256=6v2ZO5UpGCwJEkJ23Oe96Rs510tK4ZOEpZ2DB49IZmM,1262
|
5
|
+
neuro_simulator/agent/core.py,sha256=CJv0We7ZjW_EKQB54Xq-y3wDu2a2HmoaOAdYR057HOg,8941
|
6
|
+
neuro_simulator/agent/factory.py,sha256=e0IBnqJQM7OuKtglrf-pWwqwmg98wh7tOq5LxF2rV-w,1146
|
7
|
+
neuro_simulator/agent/llm.py,sha256=vLz8hp2h2R0JaNfS1RLGYGkri_YoUdlEdNfFVbxeEuI,4261
|
8
|
+
neuro_simulator/agent/memory/__init__.py,sha256=YJ7cynQJI6kD7vjyv3rKc-CZqmoYSuGQtRZl_XdGEps,39
|
9
|
+
neuro_simulator/agent/memory/manager.py,sha256=z5ZI1agkuOev5gtuGA_g6MS3atB7ufdPRVNZZ89yg1w,9259
|
10
|
+
neuro_simulator/agent/tools/__init__.py,sha256=1WZy6PADfi6o1avyy1y-ThWBFAPJ_bBqtkobyYpf5ao,38
|
11
|
+
neuro_simulator/agent/tools/core.py,sha256=o6Oyis-HFD-g6Z_u3T--tkmr9ylKJvybKqMRSMUwi1Q,5555
|
12
|
+
neuro_simulator/api/__init__.py,sha256=5LWyDSayPGdQS8Rv13nmAKLyhPnMVPyTYDdvoMPB4xw,56
|
13
|
+
neuro_simulator/api/agent.py,sha256=ABl_JoIxB4wW_J2J52bWndmTXkfGJBS5LZmbGuh7zv8,6343
|
14
|
+
neuro_simulator/api/stream.py,sha256=Pg793dXLnJ0oydrIwWFgWD0cVdFAx_QOw6L5LjE0uIo,2069
|
15
|
+
neuro_simulator/api/system.py,sha256=hXznMcThuFhwopYWgpzrRxwtBuFnF_b_vinkOaE5XOs,3712
|
16
|
+
neuro_simulator/core/__init__.py,sha256=-ojq25c8XA0CU25b0OxcGjH4IWFEDHR-HXSRSZIuKe8,57
|
17
|
+
neuro_simulator/core/agent_factory.py,sha256=qMFidwT5IrOkyNHwmpO8_fRv20KLbaIBfWF-VTFCLNA,1742
|
18
|
+
neuro_simulator/core/agent_interface.py,sha256=r58Opcgs7SWVovYTjMWuxF8AiTy9QfRz276_YGmSei0,2791
|
19
|
+
neuro_simulator/core/application.py,sha256=zN8KOR_nwsYsR1ZcyZU1GlW3-ijFLbjw5lz1g8DYVIU,11578
|
20
|
+
neuro_simulator/core/config.py,sha256=brA8kiekV_995mpz04JiGj1swIWbZZuWWLNYtbroMyE,14884
|
21
|
+
neuro_simulator/services/__init__.py,sha256=s3ZrAHg5TpJakadAAGY1h0wDw_xqN4Je4aJwJyRBmk4,61
|
22
|
+
neuro_simulator/services/audience.py,sha256=0phlhsujh_GMXm_UMiyOntY-ZMtoseRa_FroIfc5A6w,5028
|
23
|
+
neuro_simulator/services/audio.py,sha256=ZscQA25wVYpm9FUl4Hya7tKH8t0TjR3th9-OEZ0G7xk,2934
|
24
|
+
neuro_simulator/services/builtin.py,sha256=7ePxEom5HIK6wGto_H5M8JOnAjyiHqUuE381DEGgzjE,3821
|
25
|
+
neuro_simulator/services/letta.py,sha256=iXyzFyPVty3SjYCHOJAF1QqhUFmxTQbqk8lYJOwB5pc,9415
|
26
|
+
neuro_simulator/services/stream.py,sha256=dG7RuNI_ICohPkqKZ-zlBppo54BgWm_KYBs-ezzc73E,5907
|
27
|
+
neuro_simulator/utils/__init__.py,sha256=xSEFzjT827W81mNyQ_DLtr00TgFlttqfFgpz9pSxFXQ,58
|
28
|
+
neuro_simulator/utils/logging.py,sha256=BO-q_cCcoeamsc8eJqq2-L3Z8nhApze_v6LnmD-O8Ww,3411
|
29
|
+
neuro_simulator/utils/process.py,sha256=FmwIuOsp19jJBEbAj-WeUpdUsQN7B9_J1rlU0Q1TXuo,2388
|
30
|
+
neuro_simulator/utils/queue.py,sha256=7SSnUnrqxHkqSeYVjp1S5kpr8qlo4IVdU2NsGwFQAVw,1546
|
31
|
+
neuro_simulator/utils/state.py,sha256=E1ilecgMTOAhiw_kNvLG0akkhdZKhzKLrA3oB4NNVTA,538
|
32
|
+
neuro_simulator/utils/websocket.py,sha256=yOdFvJzbNhcUn5EAuyS55G_R8q-snas5OvkOtS8g19E,2292
|
33
|
+
neuro_simulator-0.2.1.dist-info/METADATA,sha256=bbJ6VZxoq8fqq0qVzjm0nYUKIhwfoNPNQd8hDKPSREY,8676
|
34
|
+
neuro_simulator-0.2.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
35
|
+
neuro_simulator-0.2.1.dist-info/entry_points.txt,sha256=qVd5ypnRRgU8Cw7rWfZ7o0OXyS9P9hgY-cRoN_mgz9g,51
|
36
|
+
neuro_simulator-0.2.1.dist-info/top_level.txt,sha256=V8awSKpcrFnjJDiJxSfy7jtOrnuE2BgAR9hLmfMDWK8,16
|
37
|
+
neuro_simulator-0.2.1.dist-info/RECORD,,
|