2026-01-25 10:07:19 +08:00
|
|
|
|
# coding=utf-8
|
|
|
|
|
|
import uiautomator2 as u2
|
|
|
|
|
|
import time
|
|
|
|
|
|
import logging
|
|
|
|
|
|
import sys
|
|
|
|
|
|
import os
|
|
|
|
|
|
import asyncio
|
|
|
|
|
|
|
|
|
|
|
|
# 添加项目根目录到 sys.path 以便导入 Util
|
|
|
|
|
|
project_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
|
|
|
|
|
if project_root not in sys.path:
|
|
|
|
|
|
sys.path.append(project_root)
|
|
|
|
|
|
|
2026-01-25 10:15:31 +08:00
|
|
|
|
from WeiXin.WxUtil import find_input_box_center, perform_input_action, analyze_chat_image
|
2026-01-25 10:07:19 +08:00
|
|
|
|
from Util.LlmUtil import get_llm_response
|
|
|
|
|
|
|
|
|
|
|
|
# 配置日志
|
|
|
|
|
|
log_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Logs")
|
|
|
|
|
|
if not os.path.exists(log_dir):
|
|
|
|
|
|
os.makedirs(log_dir)
|
|
|
|
|
|
|
|
|
|
|
|
logging.basicConfig(
|
|
|
|
|
|
level=logging.INFO,
|
|
|
|
|
|
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
|
|
|
|
handlers=[
|
|
|
|
|
|
logging.FileHandler(os.path.join(log_dir, "T4_InputLlmText.log"), encoding='utf-8'),
|
|
|
|
|
|
logging.StreamHandler()
|
|
|
|
|
|
]
|
|
|
|
|
|
)
|
|
|
|
|
|
logger = logging.getLogger("T4_InputLlmText")
|
|
|
|
|
|
|
|
|
|
|
|
async def generate_and_input():
|
|
|
|
|
|
logger.info("开始执行 T4: 生成 LLM 回复并输入...")
|
|
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
|
# 1. 连接设备
|
|
|
|
|
|
d = u2.connect()
|
|
|
|
|
|
logger.info(f"设备连接成功: {d.info.get('serial')}")
|
|
|
|
|
|
|
2026-01-25 10:15:31 +08:00
|
|
|
|
# 2. 截图并识别对话历史
|
|
|
|
|
|
screenshot_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Screenshots")
|
|
|
|
|
|
if not os.path.exists(screenshot_dir):
|
|
|
|
|
|
os.makedirs(screenshot_dir)
|
|
|
|
|
|
|
|
|
|
|
|
tmp_shot = os.path.join(screenshot_dir, "t4_temp_history_check.jpg")
|
|
|
|
|
|
d.screenshot(tmp_shot)
|
|
|
|
|
|
|
|
|
|
|
|
analyzed_shot = os.path.join(screenshot_dir, "t4_temp_history_analyzed.jpg")
|
|
|
|
|
|
dialogue_log = analyze_chat_image(tmp_shot, analyzed_shot)
|
|
|
|
|
|
|
|
|
|
|
|
history_text = ""
|
|
|
|
|
|
if dialogue_log:
|
|
|
|
|
|
history_text = "\n".join(dialogue_log)
|
|
|
|
|
|
logger.info(f"提取到对话历史:\n{history_text}")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.warning("未提取到对话历史")
|
|
|
|
|
|
history_text = "(无对话历史)"
|
|
|
|
|
|
|
|
|
|
|
|
# 3. 调用 LLM 生成回复
|
|
|
|
|
|
persona = (
|
|
|
|
|
|
"你是一名99年毕业,有27年教学经验的小学高级教师,大张老师。对作文批阅不下20000个,"
|
|
|
|
|
|
"非常熟悉小学阶段、初二以前的初中阶段作文教学。你的回复必须专业、严谨、沉稳且富有权威感。"
|
|
|
|
|
|
"严禁使用任何搞笑、幽默、有趣或轻浮的语气。要以资深教育者的身份给出严肃、务实的指导建议。"
|
|
|
|
|
|
)
|
|
|
|
|
|
prompt = (
|
|
|
|
|
|
f"人设:{persona}\n\n"
|
|
|
|
|
|
f"以下是微信聊天记录('对方'和'我'的对话):\n{history_text}\n\n"
|
|
|
|
|
|
"请作为大张老师,针对最后的消息给出一个简短、专业且严肃的回复,不超过 60 字。"
|
|
|
|
|
|
"禁止出现任何幽默或卖萌的词汇,保持特级教师的庄重感。直接输出回复内容,不要包含任何解释性文字。"
|
|
|
|
|
|
)
|
|
|
|
|
|
logger.info(f"正在以'严谨的大张老师'身份请求 LLM 生成回复...")
|
2026-01-25 10:07:19 +08:00
|
|
|
|
|
|
|
|
|
|
full_response = ""
|
|
|
|
|
|
async for chunk in get_llm_response(prompt, stream=False):
|
|
|
|
|
|
full_response += chunk
|
|
|
|
|
|
|
|
|
|
|
|
llm_text = full_response.strip().strip('"').strip('“').strip('”')
|
|
|
|
|
|
logger.info(f"LLM 生成的回复内容: {llm_text}")
|
|
|
|
|
|
|
|
|
|
|
|
if not llm_text:
|
|
|
|
|
|
logger.error("LLM 生成内容为空,停止执行。")
|
|
|
|
|
|
return
|
|
|
|
|
|
|
2026-01-25 10:15:31 +08:00
|
|
|
|
# 4. 识别输入框位置
|
2026-01-25 10:07:19 +08:00
|
|
|
|
center_point, rect_box = find_input_box_center(tmp_shot)
|
|
|
|
|
|
|
|
|
|
|
|
if center_point:
|
|
|
|
|
|
cx, cy = center_point
|
|
|
|
|
|
logger.info(f"识别到输入框中心: ({cx}, {cy})")
|
|
|
|
|
|
|
2026-01-25 10:15:31 +08:00
|
|
|
|
# 5. 点击输入框并输入文字并发送
|
2026-01-25 10:07:19 +08:00
|
|
|
|
# 使用 WxUtil 中的 perform_input_action
|
|
|
|
|
|
# 该函数内部会先点击坐标,然后输入内容
|
|
|
|
|
|
# 设置 auto_send=True,输入文字后点击发送
|
|
|
|
|
|
logger.info(f"正在点击输入框并输入文字并发送: {llm_text}")
|
|
|
|
|
|
success = perform_input_action(d, center_point, llm_text, auto_send=True)
|
|
|
|
|
|
|
|
|
|
|
|
if success:
|
|
|
|
|
|
logger.info("✅ T4 执行完成:文字已成功输入并点击发送。")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.error("❌ T4 执行失败:输入动作未成功完成。")
|
|
|
|
|
|
else:
|
|
|
|
|
|
logger.error("❌ T4 执行失败:未能识别到输入框位置。")
|
|
|
|
|
|
|
|
|
|
|
|
except Exception as e:
|
|
|
|
|
|
logger.error(f"❌ T4 执行出错: {e}", exc_info=True)
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
asyncio.run(generate_and_input())
|