0a7ad20f54
- gateway API返回结果打印到Dad可见的输出 - 每个TODO在Dad面前出现1次(直接修复)或2次(创建+结果) - LLM也搞不定的会在结果里说清楚
105 lines
3.9 KiB
Python
105 lines
3.9 KiB
Python
#!/usr/bin/env python3
|
|
"""self_todo_executor.py — TODO自动执行器 (no_agent模式)
|
|
|
|
每10分钟轮询pending TODOs,执行fix_action。
|
|
成功→completed。失败→调gateway API,带完整上下文让知微处理。
|
|
"""
|
|
|
|
import json, sqlite3, subprocess, time, urllib.request
|
|
from pathlib import Path
|
|
from datetime import datetime
|
|
|
|
BASE = Path("/home/hmo/MoFin")
|
|
DB_PATH = BASE / "data" / "mofin.db"
|
|
GATEWAY_URL = "http://localhost:8643/v1/chat/completions"
|
|
GATEWAY_KEY = "hermes123"
|
|
|
|
|
|
def main():
|
|
start = time.time()
|
|
conn = sqlite3.connect(str(DB_PATH))
|
|
conn.row_factory = sqlite3.Row
|
|
|
|
rows = conn.execute(
|
|
"SELECT id, title, description, fix_action FROM todos WHERE status='pending' "
|
|
"ORDER BY CASE priority WHEN 'high' THEN 0 WHEN 'medium' THEN 1 ELSE 2 END, "
|
|
"created_at ASC LIMIT 3"
|
|
).fetchall()
|
|
|
|
if not rows:
|
|
conn.close()
|
|
print("[SILENT] 无待处理TODO")
|
|
return
|
|
|
|
for row in rows:
|
|
tid = row["id"]
|
|
title = row["title"]
|
|
desc = row["description"] or ""
|
|
fix = row["fix_action"]
|
|
|
|
conn.execute("UPDATE todos SET status='in_progress' WHERE id=?", (tid,))
|
|
conn.commit()
|
|
|
|
if not fix:
|
|
# 无修复方案 → 带完整描述调gateway
|
|
context = f"[自愈执行器] 系统体检发现以下问题,无自动修复方案,需分析处理。\n\n问题: {title}\n\n详情:\n{desc}".strip()
|
|
else:
|
|
# 执行修复命令
|
|
try:
|
|
r = subprocess.run(fix, shell=True, capture_output=True, text=True, timeout=30)
|
|
if r.returncode == 0:
|
|
conn.execute("UPDATE todos SET status='completed', note=? WHERE id=?",
|
|
(f"已修复: {r.stdout.strip()[:200]}", tid))
|
|
conn.commit()
|
|
print(f" ✅ {title}: 已修复")
|
|
continue
|
|
output = r.stderr.strip()[:500] or r.stdout.strip()[:500]
|
|
except subprocess.TimeoutExpired:
|
|
output = "执行超时(30s)"
|
|
except Exception as e:
|
|
output = str(e)[:200]
|
|
|
|
context = (
|
|
f"[自愈执行器] 尝试自动修复失败,需知微分析处理。\n\n"
|
|
f"问题: {title}\n\n"
|
|
f"详情:\n{desc}\n\n"
|
|
f"尝试的修复命令: {fix}\n"
|
|
f"失败输出: {output}\n\n"
|
|
f"请分析失败原因并完成修复,完成后标记TODO #{tid} 为 completed。"
|
|
)
|
|
|
|
# 调gateway让知微处理(带完整上下文)
|
|
payload = json.dumps({
|
|
"model": "default",
|
|
"messages": [{"role": "user", "content": context}],
|
|
"max_tokens": 1000,
|
|
}).encode()
|
|
req = urllib.request.Request(GATEWAY_URL, data=payload,
|
|
headers={"Content-Type": "application/json",
|
|
"Authorization": f"Bearer {GATEWAY_KEY}"})
|
|
try:
|
|
resp = urllib.request.urlopen(req, timeout=120)
|
|
reply = json.loads(resp.read())
|
|
result = reply["choices"][0]["message"]["content"][:500]
|
|
conn.execute("UPDATE todos SET status='completed', note=? WHERE id=?",
|
|
(f"知微已处理: {result[:200]}", tid))
|
|
print(f" 🔶 {title}")
|
|
print(f" {result[:300]}")
|
|
except Exception as e:
|
|
conn.execute("UPDATE todos SET status='pending', note=? WHERE id=?",
|
|
(f"调用知微失败: {str(e)[:100]},下次再试", tid))
|
|
print(f" ⚠️ {title}: gateway API调用失败,下次再试")
|
|
|
|
conn.commit()
|
|
|
|
conn.close()
|
|
|
|
if rows:
|
|
print(f"自愈执行器 | {datetime.now().strftime('%H:%M')} | {len(rows)}条 ({time.time()-start:.0f}s)")
|
|
else:
|
|
print("[SILENT] 无待处理TODO")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|