-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation.py
More file actions
71 lines (58 loc) · 2.79 KB
/
Copy pathconversation.py
File metadata and controls
71 lines (58 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""Small durable conversation store and deterministic controller routing."""
from __future__ import annotations
import json
import re
import threading
import time
import uuid
from pathlib import Path
class ConversationStore:
def __init__(self, root: Path):
self.root = Path(root) / "conversations"
self.root.mkdir(parents=True, exist_ok=True)
self._lock = threading.RLock()
def _path(self, cid: str) -> Path:
if not re.fullmatch(r"conv-[a-f0-9]{12}", cid):
raise ValueError("invalid conversation id")
return self.root / f"{cid}.json"
def create(self, title: str = "") -> dict:
now = time.time()
item = {"conversation_id": "conv-" + uuid.uuid4().hex[:12], "title": title or "New conversation",
"created_at": now, "updated_at": now, "active_run_id": None, "active_revision": 0,
"status": "new", "messages": [], "revisions": [], "artifacts": [], "preferences": {}}
self.save(item)
return item
def get(self, cid: str) -> dict | None:
try:
return json.loads(self._path(cid).read_text(encoding="utf-8"))
except (OSError, ValueError, json.JSONDecodeError):
return None
def save(self, item: dict) -> dict:
with self._lock:
item["updated_at"] = time.time()
path = self._path(item["conversation_id"])
tmp = path.with_suffix(".tmp")
tmp.write_text(json.dumps(item, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
tmp.replace(path)
return item
def list(self) -> list[dict]:
return sorted((x for p in self.root.glob("conv-*.json") if (x := self.get(p.stem))),
key=lambda x: x.get("updated_at", 0), reverse=True)
def append(self, cid: str, message: dict) -> dict:
item = self.get(cid)
if not item:
raise KeyError(cid)
item.setdefault("messages", []).append(message)
return self.save(item)
def classify_message(text: str, has_run: bool = False) -> str:
value = text.lower().strip()
if any(x in value for x in ("进度", "到哪", "现在怎么样", "status", "progress")):
return "status_query"
if has_run and any(x in value for x in ("修改", "改成", "换成", "不要", "增加", "调整", "不满意", "重新")):
return "revision"
if any(x in value for x in ("代码", "证据", "结果", "pipeline", "operator")) and has_run:
return "artifact_query"
return "new_task"
def message(role: str, content: str, intent: str, run_id: str | None = None, revision: int = 0) -> dict:
return {"message_id": "msg-" + uuid.uuid4().hex[:12], "role": role, "content": content,
"created_at": time.time(), "intent": intent, "run_id": run_id, "revision": revision}