fix: 修复DEFAULT_phrases拼写错误导致JS不执行

This commit is contained in:
2026-04-12 17:45:43 +08:00
parent a62fe929c1
commit 51cc8161f1
3 changed files with 57 additions and 31 deletions
+1
View File
@@ -532,6 +532,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
while True: while True:
data = await websocket.receive_json() data = await websocket.receive_json()
action = data.get("action") action = data.get("action")
logger.info(f"WebSocket收到消息: action={action}")
# 每次消息处理时创建新的数据库会话,处理完后关闭 # 每次消息处理时创建新的数据库会话,处理完后关闭
try: try:
+34 -24
View File
@@ -268,8 +268,7 @@ class LLMService:
thinking_prefix = agent_config.get('thinking_prefix', '') thinking_prefix = agent_config.get('thinking_prefix', '')
thinking_suffix = agent_config.get('thinking_suffix', '') thinking_suffix = agent_config.get('thinking_suffix', '')
in_thinking = False buffer = "" # 用于累积和检测思考部分
thinking_buffer = ""
async with httpx.AsyncClient(timeout=60.0) as client: async with httpx.AsyncClient(timeout=60.0) as client:
async with client.stream("POST", url, headers=headers, json=payload) as response: async with client.stream("POST", url, headers=headers, json=payload) as response:
@@ -284,36 +283,47 @@ class LLMService:
delta = data['choices'][0].get('delta', {}) delta = data['choices'][0].get('delta', {})
if 'content' in delta: if 'content' in delta:
text = delta['content'] text = delta['content']
buffer += text
# 检测思考部分 # 检测思考部分(简化逻辑)
if thinking_prefix and thinking_suffix: if thinking_prefix and thinking_suffix and thinking_prefix in buffer:
for char in text: # 尝试解析思考部分
if in_thinking: try:
thinking_buffer += char start_idx = buffer.find(thinking_prefix)
# 检查是否结束思考 if start_idx >= 0:
if thinking_buffer.endswith(thinking_suffix): # 找到思考开始,继续找结束
thinking_content = thinking_buffer[:-len(thinking_suffix)] end_idx = buffer.find(thinking_suffix, start_idx)
yield {"type": "thinking", "text": thinking_content} if end_idx > start_idx:
in_thinking = False # 思考部分完整,发送思考然后发送内容
thinking_buffer = "" thinking = buffer[start_idx + len(thinking_prefix):end_idx]
yield {"type": "thinking", "text": thinking}
# 发送思考后的内容
remaining = buffer[end_idx + len(thinking_suffix):]
if remaining:
yield {"type": "content", "text": remaining}
buffer = ""
else: else:
# 检查是否接近结束 # 思考部分还没结束,先发送之前的内容
suffix_len = len(thinking_suffix) if start_idx > 0:
if len(thinking_buffer) >= suffix_len: yield {"type": "content", "text": buffer[:start_idx]}
yield {"type": "thinking", "text": thinking_buffer[-suffix_len:]} # 等待更多数据来完成思考部分
buffer = buffer[start_idx:]
else: else:
if char == thinking_prefix[0]: # 没有思考标记,直接发送内容
# 可能开始思考 yield {"type": "content", "text": text}
thinking_buffer = char buffer = ""
if len(thinking_prefix) == 1: except:
in_thinking = True yield {"type": "content", "text": text}
else:
yield {"type": "content", "text": char}
else: else:
# 没有思考标记配置,直接发送内容
yield {"type": "content", "text": text} yield {"type": "content", "text": text}
except json.JSONDecodeError: except json.JSONDecodeError:
continue continue
# 处理剩余buffer
if buffer:
yield {"type": "content", "text": buffer}
# 全局实例 # 全局实例
llm_service = LLMService() llm_service = LLMService()
+22 -7
View File
@@ -113,7 +113,7 @@
</select> </select>
</div> </div>
<div style="font-size:12px;color:#666;background:#f0f0f0;padding:4px 8px;border-radius:4px;"> <div style="font-size:12px;color:#666;background:#f0f0f0;padding:4px 8px;border-radius:4px;">
<i class="ri-user-line"></i> 主用户 <i class="ri-user-line"></i> 主用户 <span id="wsStatus" style="color:#999;">连接中...</span>
</div> </div>
</div> </div>
</div> </div>
@@ -262,7 +262,7 @@
// 加载快捷语句 // 加载快捷语句
function loadQuickPhrases() { function loadQuickPhrases() {
const saved = localStorage.getItem('quickPhrases'); const saved = localStorage.getItem('quickPhrases');
quickPhrases = saved ? JSON.parse(saved) : DEFAULT_phrrases; quickPhrases = saved ? JSON.parse(saved) : DEFAULT_phrases;
renderQuickPhrases(); renderQuickPhrases();
} }
@@ -336,12 +336,27 @@
// WebSocket连接 // WebSocket连接
function connectWebSocket() { function connectWebSocket() {
const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:'; const protocol = window.location.protocol === 'https:' ? 'wss:' : 'ws:';
ws = new WebSocket(`${protocol}//${window.location.host}/ws/${userId}`); const wsUrl = `${protocol}//${window.location.host}/ws/${userId}`;
console.log('WebSocket连接:', wsUrl);
ws = new WebSocket(wsUrl);
ws.onopen = () => console.log('WebSocket已连接'); ws.onopen = () => {
ws.onmessage = (event) => handleWebSocketMessage(JSON.parse(event.data)); console.log('WebSocket已连接');
ws.onclose = () => setTimeout(connectWebSocket, 3000); document.getElementById('wsStatus')?.textContent = '已连接';
ws.onerror = (e) => console.error('WebSocket错误:', e); };
ws.onmessage = (event) => {
console.log('WebSocket收到消息:', event.data);
handleWebSocketMessage(JSON.parse(event.data));
};
ws.onclose = (e) => {
console.log('WebSocket断开:', e.code, e.reason);
document.getElementById('wsStatus')?.textContent = '断开';
setTimeout(connectWebSocket, 3000);
};
ws.onerror = (e) => {
console.error('WebSocket错误:', e);
document.getElementById('wsStatus')?.textContent = '错误';
};
} }
// 处理WebSocket消息 // 处理WebSocket消息