fix: 修复DEFAULT_phrases拼写错误导致JS不执行
This commit is contained in:
@@ -532,6 +532,7 @@ async def websocket_endpoint(websocket: WebSocket, user_id: str):
|
||||
while True:
|
||||
data = await websocket.receive_json()
|
||||
action = data.get("action")
|
||||
logger.info(f"WebSocket收到消息: action={action}")
|
||||
|
||||
# 每次消息处理时创建新的数据库会话,处理完后关闭
|
||||
try:
|
||||
|
||||
@@ -268,8 +268,7 @@ class LLMService:
|
||||
|
||||
thinking_prefix = agent_config.get('thinking_prefix', '')
|
||||
thinking_suffix = agent_config.get('thinking_suffix', '')
|
||||
in_thinking = False
|
||||
thinking_buffer = ""
|
||||
buffer = "" # 用于累积和检测思考部分
|
||||
|
||||
async with httpx.AsyncClient(timeout=60.0) as client:
|
||||
async with client.stream("POST", url, headers=headers, json=payload) as response:
|
||||
@@ -284,36 +283,47 @@ class LLMService:
|
||||
delta = data['choices'][0].get('delta', {})
|
||||
if 'content' in delta:
|
||||
text = delta['content']
|
||||
buffer += text
|
||||
|
||||
# 检测思考部分
|
||||
if thinking_prefix and thinking_suffix:
|
||||
for char in text:
|
||||
if in_thinking:
|
||||
thinking_buffer += char
|
||||
# 检查是否结束思考
|
||||
if thinking_buffer.endswith(thinking_suffix):
|
||||
thinking_content = thinking_buffer[:-len(thinking_suffix)]
|
||||
yield {"type": "thinking", "text": thinking_content}
|
||||
in_thinking = False
|
||||
thinking_buffer = ""
|
||||
# 检测思考部分(简化逻辑)
|
||||
if thinking_prefix and thinking_suffix and thinking_prefix in buffer:
|
||||
# 尝试解析思考部分
|
||||
try:
|
||||
start_idx = buffer.find(thinking_prefix)
|
||||
if start_idx >= 0:
|
||||
# 找到思考开始,继续找结束
|
||||
end_idx = buffer.find(thinking_suffix, start_idx)
|
||||
if end_idx > start_idx:
|
||||
# 思考部分完整,发送思考然后发送内容
|
||||
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:
|
||||
# 检查是否接近结束
|
||||
suffix_len = len(thinking_suffix)
|
||||
if len(thinking_buffer) >= suffix_len:
|
||||
yield {"type": "thinking", "text": thinking_buffer[-suffix_len:]}
|
||||
# 思考部分还没结束,先发送之前的内容
|
||||
if start_idx > 0:
|
||||
yield {"type": "content", "text": buffer[:start_idx]}
|
||||
# 等待更多数据来完成思考部分
|
||||
buffer = buffer[start_idx:]
|
||||
else:
|
||||
if char == thinking_prefix[0]:
|
||||
# 可能开始思考
|
||||
thinking_buffer = char
|
||||
if len(thinking_prefix) == 1:
|
||||
in_thinking = True
|
||||
else:
|
||||
yield {"type": "content", "text": char}
|
||||
# 没有思考标记,直接发送内容
|
||||
yield {"type": "content", "text": text}
|
||||
buffer = ""
|
||||
except:
|
||||
yield {"type": "content", "text": text}
|
||||
else:
|
||||
# 没有思考标记配置,直接发送内容
|
||||
yield {"type": "content", "text": text}
|
||||
except json.JSONDecodeError:
|
||||
continue
|
||||
|
||||
# 处理剩余buffer
|
||||
if buffer:
|
||||
yield {"type": "content", "text": buffer}
|
||||
|
||||
|
||||
# 全局实例
|
||||
llm_service = LLMService()
|
||||
@@ -113,7 +113,7 @@
|
||||
</select>
|
||||
</div>
|
||||
<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>
|
||||
@@ -262,7 +262,7 @@
|
||||
// 加载快捷语句
|
||||
function loadQuickPhrases() {
|
||||
const saved = localStorage.getItem('quickPhrases');
|
||||
quickPhrases = saved ? JSON.parse(saved) : DEFAULT_phrrases;
|
||||
quickPhrases = saved ? JSON.parse(saved) : DEFAULT_phrases;
|
||||
renderQuickPhrases();
|
||||
}
|
||||
|
||||
@@ -336,12 +336,27 @@
|
||||
// WebSocket连接
|
||||
function connectWebSocket() {
|
||||
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.onmessage = (event) => handleWebSocketMessage(JSON.parse(event.data));
|
||||
ws.onclose = () => setTimeout(connectWebSocket, 3000);
|
||||
ws.onerror = (e) => console.error('WebSocket错误:', e);
|
||||
ws.onopen = () => {
|
||||
console.log('WebSocket已连接');
|
||||
document.getElementById('wsStatus')?.textContent = '已连接';
|
||||
};
|
||||
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消息
|
||||
|
||||
Reference in New Issue
Block a user