dint-cli 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- dint/__init__.py +7 -0
- dint/__main__.py +4 -0
- dint/cli.py +313 -0
- dint/engines/__init__.py +30 -0
- dint/engines/base.py +63 -0
- dint/engines/claude.py +83 -0
- dint/engines/codex.py +95 -0
- dint/engines/copilot.py +79 -0
- dint/engines/grok.py +76 -0
- dint/envfile.py +60 -0
- dint/jobs.py +316 -0
- dint/logbook/__init__.py +200 -0
- dint/logbook/mcp.py +149 -0
- dint/logbook/rest.py +85 -0
- dint/proc.py +113 -0
- dint/router.py +100 -0
- dint/runtime.py +197 -0
- dint/status.py +58 -0
- dint/types.py +59 -0
- dint_cli-0.1.0.dist-info/METADATA +155 -0
- dint_cli-0.1.0.dist-info/RECORD +25 -0
- dint_cli-0.1.0.dist-info/WHEEL +5 -0
- dint_cli-0.1.0.dist-info/entry_points.txt +2 -0
- dint_cli-0.1.0.dist-info/licenses/LICENSE +21 -0
- dint_cli-0.1.0.dist-info/top_level.txt +1 -0
dint/engines/grok.py
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
"""Grok CLI: `grok -p --output-format streaming-json`.
|
|
2
|
+
|
|
3
|
+
Native NDJSON (not Claude stream-json):
|
|
4
|
+
{"type":"text","data":"..."}
|
|
5
|
+
{"type":"tool_call","toolName":"read_file",...}
|
|
6
|
+
{"type":"end","sessionId":"..."}
|
|
7
|
+
{"type":"error","message":"..."}
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
import shutil
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from dint.engines.base import CliEngine
|
|
18
|
+
from dint.types import Event
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class GrokEngine(CliEngine):
|
|
22
|
+
name = "grok"
|
|
23
|
+
|
|
24
|
+
def __init__(self, binary: str | None = None) -> None:
|
|
25
|
+
super().__init__(binary or os.environ.get("DINT_GROK_BIN") or shutil.which("grok") or "grok")
|
|
26
|
+
|
|
27
|
+
def argv(self, prompt: str, cwd: str, session_id: str | None) -> list[str]:
|
|
28
|
+
cmd = [
|
|
29
|
+
self.binary,
|
|
30
|
+
"--no-auto-update",
|
|
31
|
+
"--always-approve",
|
|
32
|
+
"--cwd",
|
|
33
|
+
cwd,
|
|
34
|
+
"--output-format",
|
|
35
|
+
"streaming-json",
|
|
36
|
+
"-p",
|
|
37
|
+
prompt,
|
|
38
|
+
]
|
|
39
|
+
if session_id:
|
|
40
|
+
cmd.extend(["--resume", session_id])
|
|
41
|
+
return cmd
|
|
42
|
+
|
|
43
|
+
def parse_line(self, line: str, session_id: str | None) -> list[Event]:
|
|
44
|
+
return parse_grok_line(line, session_id=session_id)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def parse_grok_line(line: str, *, session_id: str | None = None) -> list[Event]:
|
|
48
|
+
obj = _json(line)
|
|
49
|
+
if not obj:
|
|
50
|
+
return []
|
|
51
|
+
sid = obj.get("sessionId") or obj.get("session_id") or session_id
|
|
52
|
+
kind = str(obj.get("type") or "")
|
|
53
|
+
if kind == "text":
|
|
54
|
+
text = obj.get("data")
|
|
55
|
+
if isinstance(text, str) and text:
|
|
56
|
+
return [Event(type="text", text=text, session_id=sid)]
|
|
57
|
+
return []
|
|
58
|
+
if kind == "tool_call":
|
|
59
|
+
name = str(obj.get("toolName") or obj.get("title") or "tool")
|
|
60
|
+
return [Event(type="tool", text=name, tool=name, session_id=sid)]
|
|
61
|
+
if kind == "end":
|
|
62
|
+
return [Event(type="done", session_id=sid)]
|
|
63
|
+
if kind == "error":
|
|
64
|
+
return [Event(type="error", text=str(obj.get("message") or "grok error"), session_id=sid)]
|
|
65
|
+
return []
|
|
66
|
+
|
|
67
|
+
|
|
68
|
+
def _json(line: str) -> dict[str, Any] | None:
|
|
69
|
+
line = line.strip()
|
|
70
|
+
if not line:
|
|
71
|
+
return None
|
|
72
|
+
try:
|
|
73
|
+
obj = json.loads(line)
|
|
74
|
+
except json.JSONDecodeError:
|
|
75
|
+
return None
|
|
76
|
+
return obj if isinstance(obj, dict) else None
|
dint/envfile.py
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
"""Load KEY=VALUE from .env without overriding the real environment."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def home_dir() -> Path:
|
|
10
|
+
override = os.environ.get("DINT_HOME")
|
|
11
|
+
if override:
|
|
12
|
+
return Path(override).expanduser()
|
|
13
|
+
return Path.home() / ".dint"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def home_env_path() -> Path:
|
|
17
|
+
return home_dir() / ".env"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def load_dotenv(start: str | None = None) -> Path | None:
|
|
21
|
+
cwd_env = _find_env(start)
|
|
22
|
+
home_env = home_env_path()
|
|
23
|
+
applied: Path | None = None
|
|
24
|
+
if cwd_env is not None:
|
|
25
|
+
_apply(cwd_env)
|
|
26
|
+
applied = cwd_env
|
|
27
|
+
if home_env.is_file() and home_env != cwd_env:
|
|
28
|
+
_apply(home_env)
|
|
29
|
+
applied = applied or home_env
|
|
30
|
+
return applied
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def write_env(path: Path, values: dict[str, str]) -> None:
|
|
34
|
+
path.parent.mkdir(parents=True, exist_ok=True)
|
|
35
|
+
lines = [f"{key}={value}" for key, value in values.items()]
|
|
36
|
+
path.write_text("\n".join(lines) + "\n", encoding="utf-8")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
def _apply(path: Path) -> None:
|
|
40
|
+
for line in path.read_text(encoding="utf-8").splitlines():
|
|
41
|
+
line = line.strip()
|
|
42
|
+
if not line or line.startswith("#") or "=" not in line:
|
|
43
|
+
continue
|
|
44
|
+
key, _, value = line.partition("=")
|
|
45
|
+
key = key.strip()
|
|
46
|
+
value = value.strip().strip('"').strip("'")
|
|
47
|
+
if key and key not in os.environ:
|
|
48
|
+
os.environ[key] = value
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _find_env(start: str | None) -> Path | None:
|
|
52
|
+
here = Path(start or os.getcwd()).resolve()
|
|
53
|
+
for _ in range(6):
|
|
54
|
+
candidate = here / ".env"
|
|
55
|
+
if candidate.is_file():
|
|
56
|
+
return candidate
|
|
57
|
+
if here.parent == here:
|
|
58
|
+
break
|
|
59
|
+
here = here.parent
|
|
60
|
+
return None
|
dint/jobs.py
ADDED
|
@@ -0,0 +1,316 @@
|
|
|
1
|
+
"""Webhook-driven jobs: Logbook #job -> start_chat + send."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import os
|
|
7
|
+
import sys
|
|
8
|
+
import threading
|
|
9
|
+
import time
|
|
10
|
+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
|
|
11
|
+
from typing import Any
|
|
12
|
+
from urllib.parse import urlparse
|
|
13
|
+
|
|
14
|
+
from dint.logbook import extract_json, format_content
|
|
15
|
+
from dint.logbook.mcp import McpLogbook
|
|
16
|
+
from dint.router import ENGINES, Router
|
|
17
|
+
|
|
18
|
+
JOB_TAG = "job"
|
|
19
|
+
ACK_TAG = "job-run"
|
|
20
|
+
_job_lock = threading.Lock()
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def post_job(router: Router, engine: str, prompt: str, cwd: str = ".") -> str:
|
|
24
|
+
engine = engine.strip().lower()
|
|
25
|
+
if engine not in ENGINES:
|
|
26
|
+
raise ValueError(f"unknown engine: {engine}")
|
|
27
|
+
prompt = prompt.strip()
|
|
28
|
+
if not prompt:
|
|
29
|
+
raise ValueError("prompt is required")
|
|
30
|
+
job = {
|
|
31
|
+
"engine": engine,
|
|
32
|
+
"cwd": os.path.abspath(cwd),
|
|
33
|
+
"prompt": prompt,
|
|
34
|
+
}
|
|
35
|
+
content = format_content(router.store.project, [JOB_TAG, engine], job)
|
|
36
|
+
result = router.store.logbook.post_message(content)
|
|
37
|
+
return str(result.get("id") or result.get("Id") or "")
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
def wait_for_job_chat(router: Router, job_id: str, *, timeout: float = 20) -> str | None:
|
|
41
|
+
deadline = time.time() + timeout
|
|
42
|
+
while time.time() < deadline:
|
|
43
|
+
chat_id = _chat_id_for_job(router, job_id)
|
|
44
|
+
if chat_id:
|
|
45
|
+
return chat_id
|
|
46
|
+
time.sleep(0.4)
|
|
47
|
+
return None
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _chat_id_for_job(router: Router, job_id: str) -> str | None:
|
|
51
|
+
for item in router.store.logbook.get_messages(f"#{ACK_TAG}", length=50):
|
|
52
|
+
parent = str(item.get("parentId") or item.get("ParentId") or "")
|
|
53
|
+
rec = extract_json(str(item.get("content") or item.get("Content") or "")) or {}
|
|
54
|
+
if rec.get("jobId") == job_id or parent == job_id:
|
|
55
|
+
chat_id = rec.get("chatId")
|
|
56
|
+
if chat_id:
|
|
57
|
+
return str(chat_id)
|
|
58
|
+
return None
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def parse_job(content: str, tags: list[str] | None = None) -> dict[str, str] | None:
|
|
62
|
+
names = {_norm_tag(t) for t in (tags or [])}
|
|
63
|
+
if not names:
|
|
64
|
+
names = {part[1:].lower() for part in content.split() if part.startswith("#")}
|
|
65
|
+
if JOB_TAG not in names or ACK_TAG in names:
|
|
66
|
+
return None
|
|
67
|
+
payload = extract_json(content) or {}
|
|
68
|
+
engine = str(payload.get("engine") or "").strip().lower()
|
|
69
|
+
if not engine:
|
|
70
|
+
engine = next((e for e in ENGINES if e in names), "")
|
|
71
|
+
prompt = str(payload.get("prompt") or payload.get("text") or "").strip()
|
|
72
|
+
if not prompt:
|
|
73
|
+
prompt = _strip_meta(content).strip()
|
|
74
|
+
cwd = str(payload.get("cwd") or os.getcwd())
|
|
75
|
+
if engine not in ENGINES or not prompt:
|
|
76
|
+
return None
|
|
77
|
+
return {"engine": engine, "cwd": os.path.abspath(cwd), "prompt": prompt}
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
def handle_event(payload: dict[str, Any], router: Router) -> str | None:
|
|
81
|
+
event_type = _pick(payload, "eventType")
|
|
82
|
+
if event_type and str(event_type) not in {"message.created"}:
|
|
83
|
+
return None
|
|
84
|
+
data = _pick(payload, "data")
|
|
85
|
+
if not isinstance(data, dict):
|
|
86
|
+
data = payload
|
|
87
|
+
message_id = str(_pick(data, "messageId", "id") or "")
|
|
88
|
+
tags = _as_tags(_pick(data, "tags"))
|
|
89
|
+
if tags and JOB_TAG not in {_norm_tag(t) for t in tags}:
|
|
90
|
+
return None
|
|
91
|
+
content = str(_pick(data, "content") or "")
|
|
92
|
+
if not content and message_id:
|
|
93
|
+
row = _fetch_message(router, message_id)
|
|
94
|
+
if row:
|
|
95
|
+
content = str(row.get("content") or row.get("Content") or "")
|
|
96
|
+
tags = _as_tags(row.get("tags") or row.get("Tags") or tags)
|
|
97
|
+
job = parse_job(content, tags)
|
|
98
|
+
if job is None:
|
|
99
|
+
return None
|
|
100
|
+
if message_id:
|
|
101
|
+
with _job_lock:
|
|
102
|
+
existing = _chat_id_for_job(router, message_id)
|
|
103
|
+
if existing:
|
|
104
|
+
print(f"job {message_id} already chat {existing}", flush=True)
|
|
105
|
+
return existing
|
|
106
|
+
started = getattr(router, "_started_jobs", None)
|
|
107
|
+
if started is None:
|
|
108
|
+
started = set()
|
|
109
|
+
router._started_jobs = started
|
|
110
|
+
if message_id in started:
|
|
111
|
+
return None
|
|
112
|
+
started.add(message_id)
|
|
113
|
+
chat_id = router.start_chat(job["engine"], job["cwd"])
|
|
114
|
+
_ack(router, message_id, chat_id, job)
|
|
115
|
+
print(f"job {message_id} -> chat {chat_id}", flush=True)
|
|
116
|
+
threading.Thread(
|
|
117
|
+
target=_run_send,
|
|
118
|
+
args=(router, chat_id, job["prompt"]),
|
|
119
|
+
daemon=True,
|
|
120
|
+
name=f"dint-job-{chat_id}",
|
|
121
|
+
).start()
|
|
122
|
+
return chat_id
|
|
123
|
+
|
|
124
|
+
|
|
125
|
+
def serve(host: str = "127.0.0.1", port: int = 8787, *, register: bool = True) -> None:
|
|
126
|
+
router = Router()
|
|
127
|
+
httpd = ThreadingHTTPServer((host, port), _handler(router))
|
|
128
|
+
hook = f"http://{host}:{port}/webhook"
|
|
129
|
+
print(f"dint jobs listening on {hook}", flush=True)
|
|
130
|
+
thread = threading.Thread(target=httpd.serve_forever, name="dint-serve", daemon=True)
|
|
131
|
+
thread.start()
|
|
132
|
+
if register:
|
|
133
|
+
_try_register(hook)
|
|
134
|
+
else:
|
|
135
|
+
print(f"set LOGBOOK_WEBHOOK_URL={hook} on logbook-server", flush=True)
|
|
136
|
+
try:
|
|
137
|
+
while thread.is_alive():
|
|
138
|
+
thread.join(timeout=0.5)
|
|
139
|
+
except KeyboardInterrupt:
|
|
140
|
+
httpd.shutdown()
|
|
141
|
+
print("stopped", flush=True)
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _handler(router: Router) -> type[BaseHTTPRequestHandler]:
|
|
145
|
+
seen: set[str] = set()
|
|
146
|
+
|
|
147
|
+
class Handler(BaseHTTPRequestHandler):
|
|
148
|
+
def log_message(self, fmt: str, *args: object) -> None:
|
|
149
|
+
line = fmt % args
|
|
150
|
+
if "/health" in line:
|
|
151
|
+
return
|
|
152
|
+
sys.stderr.write("%s\n" % line)
|
|
153
|
+
|
|
154
|
+
def do_GET(self) -> None: # noqa: N802
|
|
155
|
+
path = urlparse(self.path).path
|
|
156
|
+
if path in {"/", "/health"}:
|
|
157
|
+
self._ok({"ok": True, "service": "dint-serve"})
|
|
158
|
+
return
|
|
159
|
+
self.send_error(404)
|
|
160
|
+
|
|
161
|
+
def do_POST(self) -> None: # noqa: N802
|
|
162
|
+
path = urlparse(self.path).path
|
|
163
|
+
if path not in {"/", "/webhook", "/jobs"}:
|
|
164
|
+
self.send_error(404)
|
|
165
|
+
return
|
|
166
|
+
length = int(self.headers.get("Content-Length") or 0)
|
|
167
|
+
raw = self.rfile.read(length).decode("utf-8") if length else "{}"
|
|
168
|
+
if not _signature_ok(raw, dict(self.headers)):
|
|
169
|
+
self.send_error(401, "bad signature")
|
|
170
|
+
return
|
|
171
|
+
try:
|
|
172
|
+
payload = json.loads(raw) if raw else {}
|
|
173
|
+
except json.JSONDecodeError:
|
|
174
|
+
self.send_error(400, "invalid json")
|
|
175
|
+
return
|
|
176
|
+
data = payload.get("data") or payload.get("Data") or {}
|
|
177
|
+
event_id = str(
|
|
178
|
+
payload.get("eventId")
|
|
179
|
+
or payload.get("EventId")
|
|
180
|
+
or (data.get("messageId") if isinstance(data, dict) else "")
|
|
181
|
+
or ""
|
|
182
|
+
)
|
|
183
|
+
if event_id and event_id in seen:
|
|
184
|
+
self._ok({"ok": True, "duplicate": True})
|
|
185
|
+
return
|
|
186
|
+
if event_id:
|
|
187
|
+
seen.add(event_id)
|
|
188
|
+
if len(seen) > 500:
|
|
189
|
+
seen.clear()
|
|
190
|
+
try:
|
|
191
|
+
chat_id = handle_event(payload, router)
|
|
192
|
+
except Exception as e:
|
|
193
|
+
print(f"job error: {e}", file=sys.stderr, flush=True)
|
|
194
|
+
self.send_error(500, str(e)[:200])
|
|
195
|
+
return
|
|
196
|
+
self._ok({"ok": True, "chatId": chat_id})
|
|
197
|
+
|
|
198
|
+
def _ok(self, body: dict[str, Any]) -> None:
|
|
199
|
+
data = json.dumps(body).encode("utf-8")
|
|
200
|
+
self.send_response(200)
|
|
201
|
+
self.send_header("Content-Type", "application/json")
|
|
202
|
+
self.send_header("Content-Length", str(len(data)))
|
|
203
|
+
self.end_headers()
|
|
204
|
+
self.wfile.write(data)
|
|
205
|
+
|
|
206
|
+
return Handler
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def _run_send(router: Router, chat_id: str, prompt: str) -> None:
|
|
210
|
+
try:
|
|
211
|
+
print(f"job chat {chat_id} starting", flush=True)
|
|
212
|
+
n_text = 0
|
|
213
|
+
for event in router.send(chat_id, prompt):
|
|
214
|
+
if event.type == "text" and event.text:
|
|
215
|
+
n_text += 1
|
|
216
|
+
bot = next((t for t in reversed(router.list_turns(chat_id)) if t.get("role") == "bot"), None)
|
|
217
|
+
if bot and bot.get("text"):
|
|
218
|
+
preview = str(bot["text"]).replace("\n", " ")[:80]
|
|
219
|
+
print(f"job chat {chat_id} done {preview}", flush=True)
|
|
220
|
+
else:
|
|
221
|
+
print(f"job chat {chat_id} done (no bot text, chunks={n_text})", flush=True)
|
|
222
|
+
except Exception as e:
|
|
223
|
+
print(f"job chat {chat_id} failed: {e}", file=sys.stderr, flush=True)
|
|
224
|
+
|
|
225
|
+
|
|
226
|
+
def _ack(router: Router, job_id: str, chat_id: str, job: dict[str, str]) -> None:
|
|
227
|
+
content = format_content(
|
|
228
|
+
router.store.project,
|
|
229
|
+
[ACK_TAG, job["engine"], f"chat-{chat_id}"],
|
|
230
|
+
{"chatId": chat_id, "engine": job["engine"], "cwd": job["cwd"], "jobId": job_id},
|
|
231
|
+
)
|
|
232
|
+
try:
|
|
233
|
+
router.store.logbook.post_message(content, parent_id=job_id or None)
|
|
234
|
+
except Exception as e:
|
|
235
|
+
print(f"job ack failed: {e}", file=sys.stderr, flush=True)
|
|
236
|
+
|
|
237
|
+
|
|
238
|
+
def _fetch_message(router: Router, message_id: str) -> dict[str, Any] | None:
|
|
239
|
+
logbook = router.store.logbook
|
|
240
|
+
getter = getattr(logbook, "get_message", None)
|
|
241
|
+
if callable(getter):
|
|
242
|
+
row = getter(message_id)
|
|
243
|
+
if isinstance(row, dict):
|
|
244
|
+
return row
|
|
245
|
+
rows = logbook.get_messages(message_id, length=20)
|
|
246
|
+
for row in rows:
|
|
247
|
+
if str(row.get("id") or row.get("Id") or "") == message_id:
|
|
248
|
+
return row
|
|
249
|
+
return rows[0] if rows else None
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
def _try_register(hook: str) -> None:
|
|
253
|
+
url = os.environ.get("LOGBOOK_URL", "http://127.0.0.1:5100")
|
|
254
|
+
key = os.environ.get("LOGBOOK_API_KEY", "")
|
|
255
|
+
secret = os.environ.get("LOGBOOK_WEBHOOK_SECRET") or os.environ.get("DINT_WEBHOOK_SECRET") or ""
|
|
256
|
+
try:
|
|
257
|
+
mcp = McpLogbook(url, key)
|
|
258
|
+
mcp.register_webhook(hook, secret=secret, event_types="message.created")
|
|
259
|
+
print(f"registered webhook at logbook: {hook}", flush=True)
|
|
260
|
+
except Exception as e:
|
|
261
|
+
print(
|
|
262
|
+
f"could not register webhook ({e}). "
|
|
263
|
+
f"Start logbook-server with LOGBOOK_WEBHOOK_URL={hook}",
|
|
264
|
+
file=sys.stderr,
|
|
265
|
+
flush=True,
|
|
266
|
+
)
|
|
267
|
+
|
|
268
|
+
|
|
269
|
+
def _signature_ok(body: str, headers: dict[str, str]) -> bool:
|
|
270
|
+
import hmac
|
|
271
|
+
import hashlib
|
|
272
|
+
|
|
273
|
+
secret = os.environ.get("LOGBOOK_WEBHOOK_SECRET") or os.environ.get("DINT_WEBHOOK_SECRET") or ""
|
|
274
|
+
if not secret:
|
|
275
|
+
return True
|
|
276
|
+
got = ""
|
|
277
|
+
for key, value in headers.items():
|
|
278
|
+
if key.lower() == "x-logbook-signature":
|
|
279
|
+
got = value
|
|
280
|
+
break
|
|
281
|
+
if not got:
|
|
282
|
+
return False
|
|
283
|
+
expected = "sha256=" + hmac.new(secret.encode("utf-8"), body.encode("utf-8"), hashlib.sha256).hexdigest()
|
|
284
|
+
return hmac.compare_digest(expected, got)
|
|
285
|
+
|
|
286
|
+
|
|
287
|
+
def _pick(obj: dict[str, Any], *names: str) -> Any:
|
|
288
|
+
lower = {str(k).lower(): v for k, v in obj.items()}
|
|
289
|
+
for name in names:
|
|
290
|
+
value = lower.get(name.lower())
|
|
291
|
+
if value is not None:
|
|
292
|
+
return value
|
|
293
|
+
return None
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def _as_tags(value: Any) -> list[str]:
|
|
297
|
+
if value is None:
|
|
298
|
+
return []
|
|
299
|
+
if isinstance(value, str):
|
|
300
|
+
return [value] if value.strip() else []
|
|
301
|
+
return [str(t) for t in value]
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
def _norm_tag(tag: str) -> str:
|
|
305
|
+
return str(tag).strip().lstrip("#").lower()
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def _strip_meta(content: str) -> str:
|
|
309
|
+
parts: list[str] = []
|
|
310
|
+
for token in content.split():
|
|
311
|
+
if token.startswith("#") or token.startswith("@"):
|
|
312
|
+
continue
|
|
313
|
+
if token.startswith("{"):
|
|
314
|
+
break
|
|
315
|
+
parts.append(token)
|
|
316
|
+
return " ".join(parts)
|
dint/logbook/__init__.py
ADDED
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"""Chat ledger on Codicent Logbook.
|
|
2
|
+
|
|
3
|
+
Matches Codicent-core conversations:
|
|
4
|
+
- header is the root (no parentId)
|
|
5
|
+
- each turn posts with parentId = previous message
|
|
6
|
+
- #user vs #bot tags
|
|
7
|
+
- get_history (GetMessageHistory) walks the ParentId / OriginalMessageId chain
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import json
|
|
13
|
+
import os
|
|
14
|
+
from datetime import datetime, timezone
|
|
15
|
+
from typing import Any
|
|
16
|
+
|
|
17
|
+
from dint.logbook.mcp import McpLogbook
|
|
18
|
+
from dint.logbook.rest import LogbookError, RestLogbook
|
|
19
|
+
from dint.types import Chat, Logbook, Role
|
|
20
|
+
|
|
21
|
+
CHAT_ROLES = {"header", "session", "user", "bot", "tool", "error"}
|
|
22
|
+
TURN_ROLES = {"user", "bot", "tool", "error"}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _env(name: str, default: str) -> str:
|
|
26
|
+
return os.environ.get(name, default)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def default_logbook() -> Logbook:
|
|
30
|
+
url = _env("LOGBOOK_URL", "http://127.0.0.1:5100")
|
|
31
|
+
key = _env("LOGBOOK_API_KEY", "test-local-key")
|
|
32
|
+
if _env("LOGBOOK_TRANSPORT", "rest") == "mcp":
|
|
33
|
+
return McpLogbook(url, key)
|
|
34
|
+
return RestLogbook(url, key)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
class ChatLog:
|
|
38
|
+
"""One header per chat; turns are children via parentId."""
|
|
39
|
+
|
|
40
|
+
def __init__(self, logbook: Logbook | None = None, *, project: str | None = None) -> None:
|
|
41
|
+
self.logbook = logbook or default_logbook()
|
|
42
|
+
self.project = project or _env("LOGBOOK_PROJECT", "dint")
|
|
43
|
+
|
|
44
|
+
def post(self, chat: Chat, role: Role, text: str = "") -> str:
|
|
45
|
+
tags = ["chat", chat.engine, f"chat-{chat.chat_id}"]
|
|
46
|
+
if role == "session":
|
|
47
|
+
text = text or (chat.external_session_id or "")
|
|
48
|
+
elif role != "header":
|
|
49
|
+
tags.extend(["turn", role])
|
|
50
|
+
payload = {
|
|
51
|
+
"chatId": chat.chat_id,
|
|
52
|
+
"engine": chat.engine,
|
|
53
|
+
"role": role,
|
|
54
|
+
"cwd": chat.cwd,
|
|
55
|
+
"externalSessionId": chat.external_session_id,
|
|
56
|
+
"text": text,
|
|
57
|
+
"ts": _now(),
|
|
58
|
+
}
|
|
59
|
+
msg_id = _id(
|
|
60
|
+
self.logbook.post_message(
|
|
61
|
+
format_content(self.project, tags, payload),
|
|
62
|
+
parent_id=chat.tail_id,
|
|
63
|
+
)
|
|
64
|
+
)
|
|
65
|
+
chat.tail_id = msg_id or chat.tail_id
|
|
66
|
+
if role == "header":
|
|
67
|
+
chat.header_id = msg_id
|
|
68
|
+
return msg_id
|
|
69
|
+
|
|
70
|
+
def get_chat(self, chat_id: str) -> Chat | None:
|
|
71
|
+
rows = self.list_messages(chat_id)
|
|
72
|
+
if not rows:
|
|
73
|
+
return None
|
|
74
|
+
header = next((r for r in rows if r.get("role") == "header"), rows[0])
|
|
75
|
+
session = None
|
|
76
|
+
for r in rows:
|
|
77
|
+
if r.get("externalSessionId"):
|
|
78
|
+
session = r["externalSessionId"]
|
|
79
|
+
return Chat(
|
|
80
|
+
chat_id=header["chatId"],
|
|
81
|
+
engine=header["engine"],
|
|
82
|
+
cwd=header["cwd"],
|
|
83
|
+
external_session_id=session,
|
|
84
|
+
header_id=header.get("id"),
|
|
85
|
+
tail_id=rows[-1].get("id"),
|
|
86
|
+
)
|
|
87
|
+
|
|
88
|
+
def list_messages(self, chat_id: str) -> list[dict[str, Any]]:
|
|
89
|
+
# GetMessageHistory can truncate content; #chat-<id> search keeps the full body.
|
|
90
|
+
tagged = [
|
|
91
|
+
r
|
|
92
|
+
for r in (_record(item) for item in self.logbook.get_messages(f"#chat-{chat_id}"))
|
|
93
|
+
if r and r.get("chatId") == chat_id and r.get("role") in CHAT_ROLES
|
|
94
|
+
]
|
|
95
|
+
by_id: dict[str, dict[str, Any]] = {}
|
|
96
|
+
header_id = next((r.get("id") for r in tagged if r.get("role") == "header" and r.get("id")), None)
|
|
97
|
+
if header_id:
|
|
98
|
+
for rec in (_record(item) for item in self.logbook.get_history(header_id)):
|
|
99
|
+
if rec and rec.get("id") and rec.get("role") in CHAT_ROLES:
|
|
100
|
+
by_id[str(rec["id"])] = rec
|
|
101
|
+
for rec in tagged:
|
|
102
|
+
rid = rec.get("id")
|
|
103
|
+
by_id[str(rid) if rid else f"anon-{len(by_id)}"] = rec
|
|
104
|
+
rows = list(by_id.values())
|
|
105
|
+
rows.sort(key=lambda r: (str(r.get("ts") or ""), str(r.get("id") or "")))
|
|
106
|
+
return rows
|
|
107
|
+
|
|
108
|
+
def list_turns(self, chat_id: str) -> list[dict[str, Any]]:
|
|
109
|
+
return [r for r in self.list_messages(chat_id) if r.get("role") in TURN_ROLES]
|
|
110
|
+
|
|
111
|
+
def list_chats(self, *, length: int = 100) -> list[dict[str, Any]]:
|
|
112
|
+
rows = [_record(item) for item in self.logbook.get_messages("#chat", length=length)]
|
|
113
|
+
chats: dict[str, dict[str, Any]] = {}
|
|
114
|
+
for rec in rows:
|
|
115
|
+
if not rec or not rec.get("chatId"):
|
|
116
|
+
continue
|
|
117
|
+
cid = str(rec["chatId"])
|
|
118
|
+
cur = chats.setdefault(
|
|
119
|
+
cid,
|
|
120
|
+
{
|
|
121
|
+
"chatId": cid,
|
|
122
|
+
"engine": rec.get("engine") or "",
|
|
123
|
+
"cwd": rec.get("cwd") or "",
|
|
124
|
+
"externalSessionId": None,
|
|
125
|
+
"ts": "",
|
|
126
|
+
"preview": "",
|
|
127
|
+
"headerId": None,
|
|
128
|
+
},
|
|
129
|
+
)
|
|
130
|
+
if rec.get("engine"):
|
|
131
|
+
cur["engine"] = rec["engine"]
|
|
132
|
+
if rec.get("cwd"):
|
|
133
|
+
cur["cwd"] = rec["cwd"]
|
|
134
|
+
if rec.get("externalSessionId"):
|
|
135
|
+
cur["externalSessionId"] = rec["externalSessionId"]
|
|
136
|
+
if rec.get("role") == "header":
|
|
137
|
+
cur["headerId"] = rec.get("id")
|
|
138
|
+
ts = rec.get("ts") or ""
|
|
139
|
+
if ts >= (cur.get("ts") or ""):
|
|
140
|
+
cur["ts"] = ts
|
|
141
|
+
preview = rec.get("text") or rec.get("role") or ""
|
|
142
|
+
if preview:
|
|
143
|
+
cur["preview"] = preview
|
|
144
|
+
return sorted(chats.values(), key=lambda c: c.get("ts") or "", reverse=True)
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def _record(item: dict[str, Any]) -> dict[str, Any] | None:
|
|
148
|
+
content = item.get("content") or item.get("Content") or ""
|
|
149
|
+
payload = extract_json(str(content))
|
|
150
|
+
if not payload:
|
|
151
|
+
return None
|
|
152
|
+
payload["id"] = str(item.get("id") or item.get("Id") or payload.get("id") or "")
|
|
153
|
+
parent = item.get("parentId") or item.get("ParentId")
|
|
154
|
+
if parent:
|
|
155
|
+
payload["parentId"] = str(parent)
|
|
156
|
+
return payload
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def _id(result: dict[str, Any]) -> str:
|
|
160
|
+
return str(result.get("id") or result.get("Id") or "")
|
|
161
|
+
|
|
162
|
+
|
|
163
|
+
def format_content(project: str, tags: list[str], payload: dict[str, Any]) -> str:
|
|
164
|
+
parts: list[str] = []
|
|
165
|
+
if project:
|
|
166
|
+
parts.append("@" + project.lstrip("@"))
|
|
167
|
+
seen: set[str] = set()
|
|
168
|
+
for tag in tags:
|
|
169
|
+
t = tag.strip().lstrip("#")
|
|
170
|
+
if t and t not in seen:
|
|
171
|
+
seen.add(t)
|
|
172
|
+
parts.append("#" + t)
|
|
173
|
+
parts.append(json.dumps(payload, ensure_ascii=False, separators=(",", ":")))
|
|
174
|
+
return " ".join(parts)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def extract_json(content: str) -> dict[str, Any] | None:
|
|
178
|
+
i = content.find("{")
|
|
179
|
+
if i < 0:
|
|
180
|
+
return None
|
|
181
|
+
try:
|
|
182
|
+
data = json.loads(content[i:])
|
|
183
|
+
except json.JSONDecodeError:
|
|
184
|
+
return None
|
|
185
|
+
return data if isinstance(data, dict) else None
|
|
186
|
+
|
|
187
|
+
|
|
188
|
+
def _now() -> str:
|
|
189
|
+
return datetime.now(timezone.utc).replace(microsecond=0).isoformat().replace("+00:00", "Z")
|
|
190
|
+
|
|
191
|
+
|
|
192
|
+
__all__ = [
|
|
193
|
+
"ChatLog",
|
|
194
|
+
"LogbookError",
|
|
195
|
+
"McpLogbook",
|
|
196
|
+
"RestLogbook",
|
|
197
|
+
"default_logbook",
|
|
198
|
+
"extract_json",
|
|
199
|
+
"format_content",
|
|
200
|
+
]
|