Files
deepseek-web/detect_generating.py
T

86 lines
3.5 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
探测元宝页面的生成状态特征
运行后,请在元宝页面发送一条消息,观察输出
"""
import asyncio
from playwright.async_api import async_playwright
from pathlib import Path
YUANBAO_URL = "https://yuanbao.tencent.com/chat"
async def detect():
async with async_playwright() as p:
user_data = str(Path.home() / ".yuanbao_chat_profile")
context = await p.chromium.launch_persistent_context(
user_data, channel="chrome", headless=False,
args=["--start-maximized"], no_viewport=True
)
page = context.pages[0] if context.pages else await context.new_page()
await page.goto(YUANBAO_URL)
print("=" * 60)
print("元宝生成状态探测器")
print("=" * 60)
print("\n1. 请在元宝页面登录并创建新对话")
print("2. 在元宝页面发送一条消息(比如'你好')")
print("3. 观察生成过程中和生成完成后的输出差异")
print("4. 按 Ctrl+C 退出")
print("=" * 60)
input("\n准备好后按回车开始探测...")
# 持续探测
prev_state = None
counter = 0
while True:
try:
# 获取最后一条AI消息的完整信息
result = await page.evaluate("""
() => {
const aiMessages = document.querySelectorAll(
'.agent-chat__list__item--ai, [class*="agent-chat"][class*="ai"], [class*="message"][class*="ai"]'
);
if (aiMessages.length === 0) return null;
const lastMsg = aiMessages[aiMessages.length - 1];
const textEl = lastMsg.querySelector('.agent-chat__speech-text, [class*="text"], [class*="content"]');
return {
className: lastMsg.className,
innerHTML: lastMsg.innerHTML.substring(0, 500),
textContent: textEl ? textEl.innerText.substring(0, 200) : '',
hasCursor: lastMsg.innerHTML.includes('cursor') || lastMsg.innerHTML.includes('blink'),
childElements: Array.from(lastMsg.children).map(c => c.tagName + '.' + c.className).slice(0, 5)
};
}
""")
if result:
state_hash = f"{result['className']}_{result['hasCursor']}"
if state_hash != prev_state:
prev_state = state_hash
counter += 1
print(f"\n--- 探测 #{counter} ---")
print(f"Class: {result['className']}")
print(f"Has Cursor: {result['hasCursor']}")
print(f"Child Elements: {result['childElements']}")
print(f"Text Preview: {result['textContent'][:100]}...")
print(f"HTML Snippet: {result['innerHTML'][:200]}...")
await asyncio.sleep(0.5)
except KeyboardInterrupt:
break
except Exception as e:
print(f"Error: {e}")
await asyncio.sleep(1)
await context.close()
print("\n探测结束")
if __name__ == "__main__":
asyncio.run(detect())