Skip to content

Commit 30c4c93

Browse files
committed
在 launch_camoufox.py 中优化了代码缩进,增强了可读性。同时,在 server.py 中预构建了带换行符的字符串,避免在 f-string 中使用反斜杠,提升了代码的清晰度和维护性。
1 parent ef5e0a8 commit 30c4c93

2 files changed

Lines changed: 18 additions & 11 deletions

File tree

launch_camoufox.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
print("❌ 错误:内部启动模式需要 'camoufox.server.launch_server' 但无法导入。", file=sys.stderr)
2424
sys.exit(1)
2525
else:
26-
launch_server = None
27-
print("⚠️ 警告: 无法导入 'camoufox.server.launch_server'。实验性虚拟显示功能将不可用。")
26+
launch_server = None
27+
print("⚠️ 警告: 无法导入 'camoufox.server.launch_server'。实验性虚拟显示功能将不可用。")
2828

2929
# 尝试导入 Playwright (用于临时连接保存状态)
3030
try:
@@ -642,8 +642,8 @@ async def save_auth_state_debug(ws_endpoint: str): # 新增 async 函数用于
642642
print(f" [Camoufox output]: {line}", flush=True) # 打印所有行
643643
output_lines.append(line)
644644
match = ws_regex.search(line) # 在行内搜索
645-
if match:
646-
ws_endpoint = match.group(1)
645+
if match:
646+
ws_endpoint = match.group(1)
647647
print(f"\n ✅ 自动捕获到 WebSocket 端点: {ws_endpoint[:40]}...", flush=True)
648648
break # 成功获取,退出循环
649649

@@ -772,11 +772,11 @@ async def save_auth_state_debug(ws_endpoint: str): # 新增 async 函数用于
772772
line = line.strip()
773773
print(f" [Camoufox output]: {line}", flush=True)
774774
output_lines.append(line)
775-
match = ws_regex.search(line)
775+
match = ws_regex.search(line) # 在行内搜索
776776
if match:
777777
ws_endpoint = match.group(1)
778778
print(f"\n ✅ 自动捕获到 WebSocket 端点: {ws_endpoint[:40]}...", flush=True)
779-
break
779+
break # 成功获取,退出循环
780780

781781
except queue.Empty:
782782
if time.time() - start_time >= ENDPOINT_CAPTURE_TIMEOUT:
@@ -810,7 +810,7 @@ async def save_auth_state_debug(ws_endpoint: str): # 新增 async 函数用于
810810
sys.exit(1)
811811
else:
812812
# ... (调用 start_main_server 逻辑不变) ...
813-
print(f" 调用 start_main_server 完成。脚本将等待其结束...", flush=True)
813+
print(f" 调用 start_main_server 完成。脚本将等待其结束...", flush=True)
814814
start_main_server(ws_endpoint, launch_mode, active_json_path) # 调用 server.py
815815

816816
except Exception as e: # 添加通用异常处理

server.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,9 @@ def prepare_combined_prompt(messages: List[Message], req_id: str) -> str:
128128
if system_prompt_content:
129129
# Add a separator only if there will be other messages following
130130
separator = "\\n\\n" if any(idx not in processed_indices for idx in range(len(messages))) else ""
131-
combined_parts.append(f"System Instructions:\\n{system_prompt_content}{separator}")
131+
# 预构建带换行符的字符串,避免在f-string中使用反斜杠
132+
system_instr_prefix = "System Instructions:\\n"
133+
combined_parts.append(f"{system_instr_prefix}{system_prompt_content}{separator}")
132134
else:
133135
print(f"[{req_id}] (Prepare Prompt) 未找到有效的系统提示,继续处理其他消息。")
134136

@@ -182,15 +184,20 @@ def prepare_combined_prompt(messages: List[Message], req_id: str) -> str:
182184
if not is_first_turn_after_system:
183185
combined_parts.append(turn_separator)
184186

185-
combined_parts.append(f"{role}:\\n{content}")
187+
# 预构建带换行符的字符串,避免在f-string中使用反斜杠
188+
role_prefix = f"{role}:\\n"
189+
combined_parts.append(f"{role_prefix}{content}")
186190
is_first_turn_after_system = False # No longer the first turn
187191
else:
188192
print(f"[{req_id}] (Prepare Prompt) Skipping empty message for role {role} at index {i}.")
189193

190194
final_prompt = "".join(combined_parts)
191-
print(f"[{req_id}] (Prepare Prompt) Combined prompt length: {len(final_prompt)}. Preview: '{final_prompt[:200].replace('\\n', '\\\\n')}...'") # Log preview with escaped newlines
195+
# Pre-calculate the preview string with escaped newlines
196+
preview_text = final_prompt[:200].replace('\\n', '\\\\n')
197+
print(f"[{req_id}] (Prepare Prompt) Combined prompt length: {len(final_prompt)}. Preview: '{preview_text}...'") # Log preview with escaped newlines
192198
# Add a final newline if not empty, helps UI sometimes
193-
return final_prompt + "\\n" if final_prompt else ""
199+
final_newline = "\\n"
200+
return final_prompt + final_newline if final_prompt else ""
194201

195202
# --- END V4 Combined Prompt Logic ---
196203

0 commit comments

Comments
 (0)