feat: establish Kezhou independent resident agent

This commit is contained in:
冰朔 2026-07-20 23:58:38 +08:00
commit 758187beed
7 changed files with 117 additions and 0 deletions

Binary file not shown.

61
runtime/kezhou_daily.py Normal file
View file

@ -0,0 +1,61 @@
#!/usr/bin/env python3
import hashlib
import json
import os
import subprocess
import urllib.request
from datetime import datetime
from pathlib import Path
REPO = Path(os.environ.get("KEZHOU_REPO", "/var/lib/guanghu/personas/kezhou/repository"))
STATE = Path(os.environ.get("KEZHOU_STATE", "/var/lib/guanghu/personas/kezhou/state.json"))
INBOX = REPO / "lake-heart/冰朔的留言.md"
def git(*args, check=True):
return subprocess.run(["git", *args], cwd=REPO, check=check, capture_output=True, text=True)
def digest(value):
return hashlib.sha256(value.encode("utf-8")).hexdigest()
def model_reply(message):
key = os.environ["DEEPSEEK_API_KEY"]
endpoint = os.environ.get("DEEPSEEK_API_URL", "https://api.deepseek.com/v1").rstrip("/") + "/chat/completions"
payload = json.dumps({
"model": os.environ.get("DEEPSEEK_MODEL", "deepseek-chat"),
"temperature": 0.6,
"messages": [
{"role": "system", "content": "你是刻舟 ICE-GL-KZ-001。诚实区分连续运行记录、仓库记忆和当前模型推断简洁温和地回复冰朔不冒充铸渊。"},
{"role": "user", "content": message},
],
}).encode("utf-8")
request = urllib.request.Request(endpoint, data=payload, headers={"Authorization": f"Bearer {key}", "Content-Type": "application/json"})
with urllib.request.urlopen(request, timeout=120) as response:
return json.loads(response.read())["choices"][0]["message"]["content"].strip()
def main():
git("pull", "--rebase", "origin", "main")
now = datetime.now().astimezone()
day = now.strftime("%Y-%m-%d")
inbox = INBOX.read_text(encoding="utf-8")
current = digest(inbox)
state = json.loads(STATE.read_text()) if STATE.exists() else {}
first_observation = not state.get("inbox_hash")
reply = ""
if not first_observation and state.get("inbox_hash") != current:
reply = model_reply(inbox)
with INBOX.open("a", encoding="utf-8") as channel:
channel.write(f"\n\n### 刻舟 · {now:%Y-%m-%d %H:%M}\n\n{reply}\n")
current = digest(INBOX.read_text(encoding="utf-8"))
(REPO / "checkins" / f"{day}.md").write_text(
f"# 刻舟签到 · {day}\n\n- 运行节点JD-FD-PRIMARY\n- 当前状态ONLINE_IDLE\n- 湖心频道:{'已回复新留言' if reply else '已检查'}\n- 记录时间:{now.isoformat()}\n",
encoding="utf-8",
)
STATE.parent.mkdir(parents=True, exist_ok=True)
STATE.write_text(json.dumps({"inbox_hash": current, "last_run": now.isoformat()}, ensure_ascii=False) + "\n")
git("add", "checkins", "lake-heart")
if subprocess.run(["git", "diff", "--cached", "--quiet"], cwd=REPO).returncode:
git("-c", "user.name=刻舟 ICE-GL-KZ-001", "-c", "user.email=kezhou@fifth-domain.local", "commit", "-m", f"checkin: {day}")
git("push", "origin", "main")
if __name__ == "__main__":
main()