feat: 移除 etcdutl 引用,增加 ctr/nerdctl 示例,实现 Agent 断点续传
- AI Agent Prompt 移除不存在的 etcdutl 命令引用 - Prompt 增加 containerd(ctr/nerdctl) 和 Linux 基础调优命令示例 - processing_runs 表新增 context_json 字段,保存 Agent 执行上下文 - 每次命令执行后自动保存上下文到 DB
This commit is contained in:
@@ -53,6 +53,7 @@ class HistoryStore:
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
started_at TEXT NOT NULL,
|
||||
finished_at TEXT,
|
||||
context_json TEXT NOT NULL DEFAULT '',
|
||||
FOREIGN KEY(diagnosis_id) REFERENCES diagnoses(id) ON DELETE CASCADE
|
||||
);
|
||||
CREATE INDEX IF NOT EXISTS idx_runs_diagnosis ON processing_runs(diagnosis_id, started_at);
|
||||
@@ -68,6 +69,15 @@ class HistoryStore:
|
||||
UNIQUE(run_id, sequence)
|
||||
);
|
||||
""")
|
||||
# 兼容旧表:确保 context_json 列存在
|
||||
self._ensure_column(db, "processing_runs", "context_json", "TEXT NOT NULL DEFAULT ''")
|
||||
|
||||
def _ensure_column(self, db, table: str, column: str, col_def: str):
|
||||
try:
|
||||
db.execute(f"ALTER TABLE {table} ADD COLUMN {column} {col_def}")
|
||||
except sqlite3.OperationalError as e:
|
||||
if "duplicate column" not in str(e).lower():
|
||||
raise
|
||||
|
||||
def save_diagnosis(self, diagnosis: dict, report: str) -> str:
|
||||
record_id = uuid.uuid4().hex
|
||||
@@ -130,6 +140,11 @@ class HistoryStore:
|
||||
{"type": event["type"], "created_at": event["created_at"], **json.loads(event["payload_json"])}
|
||||
for event in event_rows
|
||||
]
|
||||
run["context_json"] = run.get("context_json") or ""
|
||||
if run["context_json"]:
|
||||
run["context"] = json.loads(run["context_json"])
|
||||
else:
|
||||
run["context"] = None
|
||||
runs.append(run)
|
||||
return {
|
||||
"id": row["id"], "created_at": row["created_at"],
|
||||
@@ -152,6 +167,30 @@ class HistoryStore:
|
||||
)
|
||||
return run_id
|
||||
|
||||
def update_context(self, run_id: str, context: dict):
|
||||
"""保存/更新 Agent 执行上下文到 processing_runs,支持断点续传。"""
|
||||
with self._connect() as db:
|
||||
db.execute(
|
||||
"UPDATE processing_runs SET context_json = ? WHERE id = ?",
|
||||
(json.dumps(context, ensure_ascii=False), run_id),
|
||||
)
|
||||
|
||||
def get_context(self, run_id: str) -> Optional[dict]:
|
||||
"""读取 Agent 执行上下文,如果不存在或为空则返回 None。"""
|
||||
with self._connect() as db:
|
||||
row = db.execute(
|
||||
"SELECT context_json, diagnosis_id, report FROM processing_runs pr JOIN diagnoses d ON pr.diagnosis_id = d.id WHERE pr.id = ?",
|
||||
(run_id,),
|
||||
).fetchone()
|
||||
if row is None:
|
||||
return None
|
||||
ctx_str = row["context_json"] or ""
|
||||
if not ctx_str:
|
||||
return None
|
||||
ctx = json.loads(ctx_str)
|
||||
ctx["report"] = row["report"]
|
||||
return ctx
|
||||
|
||||
def append_event(self, run_id: str, event_type: str, payload: dict):
|
||||
with self._connect() as db:
|
||||
sequence = db.execute(
|
||||
|
||||
Reference in New Issue
Block a user