Files
Haocode/tests/debug_inject.py
T
2026-09-17 16:30:02 +08:00

49 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
"""代理侧调试注入 CLI(与运行中的 app 通过文件通信)
用法:
python tests/debug_inject.py "备注内容" # 注入 [AGENT] 日志行
python tests/debug_inject.py --show # 打开独立调试窗口
python tests/debug_inject.py --hide # 关闭独立调试窗口
python tests/debug_inject.py --read [N] # 读取会话日志最后 N 行(默认 50)
"""
import sys, os
sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from core import debug_log as dl
def main():
args = sys.argv[1:]
if not args:
print(__doc__)
return 1
if args[0] == "--show":
with open(dl.DEBUG_CMD_PATH, "w", encoding="utf-8") as f:
f.write("show")
print(f"已请求打开调试窗口 -> {dl.DEBUG_CMD_PATH}")
return 0
if args[0] == "--hide":
with open(dl.DEBUG_CMD_PATH, "w", encoding="utf-8") as f:
f.write("hide")
print(f"已请求关闭调试窗口 -> {dl.DEBUG_CMD_PATH}")
return 0
if args[0] == "--read":
n = int(args[1]) if len(args) > 1 else 50
if not os.path.exists(dl.DEBUG_LOG_PATH):
print("(会话日志尚不存在)")
return 0
with open(dl.DEBUG_LOG_PATH, "r", encoding="utf-8") as f:
lines = f.read().splitlines()
print(f"===== {os.path.basename(dl.DEBUG_LOG_PATH)} 最后 {min(n, len(lines))} 行 =====")
for l in lines[-n:]:
print(l)
return 0
# 普通文本 → 注入 [AGENT]
dl.debug_log(args[0], "AGENT")
print(f"已注入 [AGENT]: {args[0]}")
return 0
if __name__ == "__main__":
sys.exit(main())