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/logbook/mcp.py
ADDED
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
"""Logbook over MCP `/mcp` tools `post_message` and `get_messages`."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import http.client
|
|
6
|
+
import json
|
|
7
|
+
import urllib.parse
|
|
8
|
+
from typing import Any
|
|
9
|
+
|
|
10
|
+
from dint.logbook.rest import LogbookError
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class McpLogbook:
|
|
14
|
+
def __init__(self, url: str, api_key: str = "") -> None:
|
|
15
|
+
parsed = urllib.parse.urlparse(url)
|
|
16
|
+
self.host = parsed.hostname or "127.0.0.1"
|
|
17
|
+
self.port = parsed.port or (443 if parsed.scheme == "https" else 80)
|
|
18
|
+
self.path = "/mcp"
|
|
19
|
+
self.api_key = api_key
|
|
20
|
+
self.session_id: str | None = None
|
|
21
|
+
self._rid = 0
|
|
22
|
+
self._ready = False
|
|
23
|
+
|
|
24
|
+
def post_message(self, message: str, parent_id: str | None = None) -> dict:
|
|
25
|
+
args: dict[str, Any] = {"message": message, "userId": "dint"}
|
|
26
|
+
if parent_id:
|
|
27
|
+
args["parentId"] = parent_id
|
|
28
|
+
result = self._tool("post_message", args)
|
|
29
|
+
return result if isinstance(result, dict) else {"id": str(result)}
|
|
30
|
+
|
|
31
|
+
def get_messages(
|
|
32
|
+
self,
|
|
33
|
+
search: str | None = None,
|
|
34
|
+
*,
|
|
35
|
+
start: int = 0,
|
|
36
|
+
length: int = 100,
|
|
37
|
+
) -> list[dict]:
|
|
38
|
+
args: dict[str, Any] = {"start": start, "length": length}
|
|
39
|
+
if search:
|
|
40
|
+
args["search"] = search
|
|
41
|
+
result = self._tool("get_messages", args)
|
|
42
|
+
if isinstance(result, list):
|
|
43
|
+
return [m for m in result if isinstance(m, dict)]
|
|
44
|
+
if isinstance(result, dict):
|
|
45
|
+
return list(result.get("messages") or result.get("items") or [])
|
|
46
|
+
return []
|
|
47
|
+
|
|
48
|
+
def get_message(self, message_id: str) -> dict | None:
|
|
49
|
+
result = self._tool("get_message_by_id", {"messageId": message_id})
|
|
50
|
+
if isinstance(result, dict) and (result.get("id") or result.get("Id") or result.get("content")):
|
|
51
|
+
return result
|
|
52
|
+
rows = self.get_messages(message_id, length=20)
|
|
53
|
+
for row in rows:
|
|
54
|
+
if str(row.get("id") or row.get("Id") or "") == message_id:
|
|
55
|
+
return row
|
|
56
|
+
return rows[0] if rows else None
|
|
57
|
+
|
|
58
|
+
def register_webhook(self, url: str, secret: str = "", event_types: str = "message.created") -> Any:
|
|
59
|
+
args: dict[str, Any] = {"url": url, "eventTypes": event_types}
|
|
60
|
+
if secret:
|
|
61
|
+
args["secret"] = secret
|
|
62
|
+
return self._tool("register_webhook", args)
|
|
63
|
+
|
|
64
|
+
def get_history(self, message_id: str) -> list[dict]:
|
|
65
|
+
result = self._tool("get_message_history", {"messageId": message_id})
|
|
66
|
+
if isinstance(result, list):
|
|
67
|
+
return [m for m in result if isinstance(m, dict)]
|
|
68
|
+
if isinstance(result, dict):
|
|
69
|
+
return list(result.get("messages") or result.get("items") or [])
|
|
70
|
+
return []
|
|
71
|
+
|
|
72
|
+
def _tool(self, name: str, arguments: dict[str, Any]) -> Any:
|
|
73
|
+
self._ensure()
|
|
74
|
+
data = self._call("tools/call", {"name": name, "arguments": arguments})
|
|
75
|
+
result = (data or {}).get("result") or {}
|
|
76
|
+
content = result.get("content")
|
|
77
|
+
if isinstance(content, list):
|
|
78
|
+
texts = [c.get("text", "") for c in content if c.get("type") == "text"]
|
|
79
|
+
joined = "\n".join(texts)
|
|
80
|
+
try:
|
|
81
|
+
return json.loads(joined)
|
|
82
|
+
except json.JSONDecodeError:
|
|
83
|
+
return joined if len(texts) == 1 else texts
|
|
84
|
+
return result
|
|
85
|
+
|
|
86
|
+
def _ensure(self) -> None:
|
|
87
|
+
if self._ready:
|
|
88
|
+
return
|
|
89
|
+
self._call(
|
|
90
|
+
"initialize",
|
|
91
|
+
{
|
|
92
|
+
"protocolVersion": "2024-11-05",
|
|
93
|
+
"capabilities": {},
|
|
94
|
+
"clientInfo": {"name": "dint", "version": "0.1.0"},
|
|
95
|
+
},
|
|
96
|
+
)
|
|
97
|
+
try:
|
|
98
|
+
self._call("notifications/initialized", {}, notify=True)
|
|
99
|
+
except LogbookError:
|
|
100
|
+
pass
|
|
101
|
+
self._ready = True
|
|
102
|
+
|
|
103
|
+
def _headers(self) -> dict[str, str]:
|
|
104
|
+
headers = {
|
|
105
|
+
"Content-Type": "application/json",
|
|
106
|
+
"Accept": "application/json, text/event-stream",
|
|
107
|
+
}
|
|
108
|
+
if self.api_key:
|
|
109
|
+
headers["Authorization"] = f"Bearer {self.api_key}"
|
|
110
|
+
if self.session_id:
|
|
111
|
+
headers["Mcp-Session-Id"] = self.session_id
|
|
112
|
+
return headers
|
|
113
|
+
|
|
114
|
+
def _call(self, method: str, params: dict | None = None, *, notify: bool = False) -> Any:
|
|
115
|
+
self._rid += 1
|
|
116
|
+
body: dict[str, Any] = {"jsonrpc": "2.0", "method": method}
|
|
117
|
+
if not notify:
|
|
118
|
+
body["id"] = self._rid
|
|
119
|
+
if params is not None:
|
|
120
|
+
body["params"] = params
|
|
121
|
+
try:
|
|
122
|
+
conn = http.client.HTTPConnection(self.host, self.port, timeout=30)
|
|
123
|
+
conn.request("POST", self.path, json.dumps(body).encode("utf-8"), self._headers())
|
|
124
|
+
resp = conn.getresponse()
|
|
125
|
+
raw = resp.read().decode("utf-8")
|
|
126
|
+
sid = resp.getheader("Mcp-Session-Id")
|
|
127
|
+
if sid:
|
|
128
|
+
self.session_id = sid
|
|
129
|
+
except OSError as e:
|
|
130
|
+
raise LogbookError(
|
|
131
|
+
f"Cannot reach logbook MCP at {self.host}:{self.port}{self.path} ({e}). "
|
|
132
|
+
"Start logbook-server — https://logbook.codicent.ai"
|
|
133
|
+
) from e
|
|
134
|
+
if notify:
|
|
135
|
+
return {"status": resp.status}
|
|
136
|
+
if resp.status >= 400:
|
|
137
|
+
raise LogbookError(f"MCP HTTP {resp.status}: {raw[:400]}")
|
|
138
|
+
data = _parse(raw)
|
|
139
|
+
if isinstance(data, dict) and data.get("error"):
|
|
140
|
+
raise LogbookError(f"MCP error: {data['error']}")
|
|
141
|
+
return data
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def _parse(raw: str) -> Any:
|
|
145
|
+
if "data:" in raw:
|
|
146
|
+
for line in raw.splitlines():
|
|
147
|
+
if line.startswith("data:"):
|
|
148
|
+
return json.loads(line[5:].strip())
|
|
149
|
+
return json.loads(raw) if raw.strip() else None
|
dint/logbook/rest.py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"""Logbook over REST — PostMessage, GetMessages, GetMessageHistory."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import json
|
|
6
|
+
import urllib.error
|
|
7
|
+
import urllib.parse
|
|
8
|
+
import urllib.request
|
|
9
|
+
from typing import Any
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
class LogbookError(RuntimeError):
|
|
13
|
+
pass
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
class RestLogbook:
|
|
17
|
+
def __init__(self, url: str, api_key: str = "") -> None:
|
|
18
|
+
self.url = url.rstrip("/")
|
|
19
|
+
self.api_key = api_key
|
|
20
|
+
|
|
21
|
+
def post_message(self, message: str, parent_id: str | None = None) -> dict:
|
|
22
|
+
body: dict[str, Any] = {"content": message, "userId": "dint"}
|
|
23
|
+
if parent_id:
|
|
24
|
+
body["parentId"] = parent_id
|
|
25
|
+
data = self._request("POST", "/api/messages", body)
|
|
26
|
+
if not isinstance(data, dict):
|
|
27
|
+
raise LogbookError(f"unexpected post response: {data!r}")
|
|
28
|
+
return data
|
|
29
|
+
|
|
30
|
+
def get_messages(
|
|
31
|
+
self,
|
|
32
|
+
search: str | None = None,
|
|
33
|
+
*,
|
|
34
|
+
start: int = 0,
|
|
35
|
+
length: int = 100,
|
|
36
|
+
) -> list[dict]:
|
|
37
|
+
q = {"start": str(start), "length": str(length)}
|
|
38
|
+
if search:
|
|
39
|
+
q["search"] = search
|
|
40
|
+
data = self._request("GET", "/api/messages?" + urllib.parse.urlencode(q))
|
|
41
|
+
if isinstance(data, list):
|
|
42
|
+
return [m for m in data if isinstance(m, dict)]
|
|
43
|
+
if isinstance(data, dict):
|
|
44
|
+
return list(data.get("messages") or data.get("items") or [])
|
|
45
|
+
return []
|
|
46
|
+
|
|
47
|
+
def health(self) -> dict:
|
|
48
|
+
data = self._request("GET", "/health", auth=False)
|
|
49
|
+
return data if isinstance(data, dict) else {"raw": data}
|
|
50
|
+
|
|
51
|
+
def get_message(self, message_id: str) -> dict | None:
|
|
52
|
+
rows = self.get_messages(message_id, length=20)
|
|
53
|
+
for row in rows:
|
|
54
|
+
rid = str(row.get("id") or row.get("Id") or "")
|
|
55
|
+
if rid == message_id:
|
|
56
|
+
return row
|
|
57
|
+
return rows[0] if rows else None
|
|
58
|
+
|
|
59
|
+
def get_history(self, message_id: str) -> list[dict]:
|
|
60
|
+
data = self._request("GET", f"/api/messages/{urllib.parse.quote(message_id)}/history")
|
|
61
|
+
if isinstance(data, list):
|
|
62
|
+
return [m for m in data if isinstance(m, dict)]
|
|
63
|
+
if isinstance(data, dict):
|
|
64
|
+
return list(data.get("messages") or data.get("items") or [])
|
|
65
|
+
return []
|
|
66
|
+
|
|
67
|
+
def _request(self, method: str, path: str, body: dict | None = None, *, auth: bool = True) -> Any:
|
|
68
|
+
raw_body = None if body is None else json.dumps(body).encode("utf-8")
|
|
69
|
+
headers = {"Accept": "application/json"}
|
|
70
|
+
if body is not None:
|
|
71
|
+
headers["Content-Type"] = "application/json"
|
|
72
|
+
if auth and self.api_key:
|
|
73
|
+
headers["Authorization"] = f"Bearer {self.api_key}"
|
|
74
|
+
req = urllib.request.Request(self.url + path, data=raw_body, headers=headers, method=method)
|
|
75
|
+
try:
|
|
76
|
+
with urllib.request.urlopen(req, timeout=30) as resp:
|
|
77
|
+
text = resp.read().decode("utf-8")
|
|
78
|
+
return json.loads(text) if text else None
|
|
79
|
+
except urllib.error.HTTPError as e:
|
|
80
|
+
raise LogbookError(f"HTTP {e.code} {method} {path}: {e.read().decode('utf-8', 'replace')}") from e
|
|
81
|
+
except urllib.error.URLError as e:
|
|
82
|
+
raise LogbookError(
|
|
83
|
+
f"Cannot reach logbook at {self.url} ({e.reason}). "
|
|
84
|
+
"Start logbook-server — https://logbook.codicent.ai"
|
|
85
|
+
) from e
|
dint/proc.py
ADDED
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import subprocess
|
|
5
|
+
import sys
|
|
6
|
+
import threading
|
|
7
|
+
from collections.abc import Iterator
|
|
8
|
+
from typing import IO
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class CancelledError(RuntimeError):
|
|
12
|
+
"""Raised when a running engine turn is cancelled."""
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class RunningProcess:
|
|
16
|
+
def __init__(self, proc: subprocess.Popen[str]) -> None:
|
|
17
|
+
self.proc = proc
|
|
18
|
+
self._lock = threading.Lock()
|
|
19
|
+
self._cancelled = False
|
|
20
|
+
|
|
21
|
+
@property
|
|
22
|
+
def cancelled(self) -> bool:
|
|
23
|
+
with self._lock:
|
|
24
|
+
return self._cancelled
|
|
25
|
+
|
|
26
|
+
def cancel(self) -> None:
|
|
27
|
+
with self._lock:
|
|
28
|
+
self._cancelled = True
|
|
29
|
+
_kill(self.proc)
|
|
30
|
+
|
|
31
|
+
def lines(self) -> Iterator[str]:
|
|
32
|
+
stdout = self.proc.stdout
|
|
33
|
+
if stdout is None:
|
|
34
|
+
return
|
|
35
|
+
try:
|
|
36
|
+
for line in stdout:
|
|
37
|
+
if self.cancelled:
|
|
38
|
+
raise CancelledError("turn cancelled")
|
|
39
|
+
yield line.rstrip("\r\n")
|
|
40
|
+
finally:
|
|
41
|
+
try:
|
|
42
|
+
stdout.close()
|
|
43
|
+
except OSError:
|
|
44
|
+
pass
|
|
45
|
+
code = self.proc.poll()
|
|
46
|
+
if code is None:
|
|
47
|
+
if self.cancelled:
|
|
48
|
+
_kill(self.proc)
|
|
49
|
+
else:
|
|
50
|
+
try:
|
|
51
|
+
self.proc.wait(timeout=5)
|
|
52
|
+
except subprocess.TimeoutExpired:
|
|
53
|
+
_kill(self.proc)
|
|
54
|
+
if self.cancelled:
|
|
55
|
+
raise CancelledError("turn cancelled")
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def spawn(
|
|
59
|
+
argv: list[str],
|
|
60
|
+
*,
|
|
61
|
+
cwd: str,
|
|
62
|
+
extra_env: dict[str, str] | None = None,
|
|
63
|
+
) -> RunningProcess:
|
|
64
|
+
env = os.environ.copy()
|
|
65
|
+
env["PYTHONUNBUFFERED"] = "1"
|
|
66
|
+
env["NO_COLOR"] = "1"
|
|
67
|
+
if extra_env:
|
|
68
|
+
env.update(extra_env)
|
|
69
|
+
proc = subprocess.Popen(
|
|
70
|
+
argv,
|
|
71
|
+
cwd=cwd,
|
|
72
|
+
stdout=subprocess.PIPE,
|
|
73
|
+
stderr=subprocess.PIPE,
|
|
74
|
+
stdin=subprocess.DEVNULL,
|
|
75
|
+
text=True,
|
|
76
|
+
encoding="utf-8",
|
|
77
|
+
errors="replace",
|
|
78
|
+
env=env,
|
|
79
|
+
bufsize=1,
|
|
80
|
+
)
|
|
81
|
+
_drain_stderr(proc.stderr)
|
|
82
|
+
return RunningProcess(proc)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
def _drain_stderr(stream: IO[str] | None) -> None:
|
|
86
|
+
if stream is None:
|
|
87
|
+
return
|
|
88
|
+
|
|
89
|
+
def _read() -> None:
|
|
90
|
+
try:
|
|
91
|
+
stream.read()
|
|
92
|
+
except OSError:
|
|
93
|
+
pass
|
|
94
|
+
|
|
95
|
+
threading.Thread(target=_read, daemon=True).start()
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def _kill(proc: subprocess.Popen[str]) -> None:
|
|
99
|
+
if proc.poll() is not None:
|
|
100
|
+
return
|
|
101
|
+
if sys.platform == "win32":
|
|
102
|
+
subprocess.run(
|
|
103
|
+
["taskkill", "/PID", str(proc.pid), "/T", "/F"],
|
|
104
|
+
stdout=subprocess.DEVNULL,
|
|
105
|
+
stderr=subprocess.DEVNULL,
|
|
106
|
+
check=False,
|
|
107
|
+
)
|
|
108
|
+
return
|
|
109
|
+
proc.terminate()
|
|
110
|
+
try:
|
|
111
|
+
proc.wait(timeout=3)
|
|
112
|
+
except subprocess.TimeoutExpired:
|
|
113
|
+
proc.kill()
|
dint/router.py
ADDED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import uuid
|
|
4
|
+
from collections.abc import Iterator
|
|
5
|
+
|
|
6
|
+
from dint.engines import default_engines
|
|
7
|
+
from dint.logbook import ChatLog
|
|
8
|
+
from dint.types import Chat, Engine, Event
|
|
9
|
+
|
|
10
|
+
ENGINES = {"claude", "codex", "grok", "copilot"}
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Router:
|
|
14
|
+
"""Fan-out: Engine CLIs do the work, ChatLog is the ledger."""
|
|
15
|
+
|
|
16
|
+
def __init__(
|
|
17
|
+
self,
|
|
18
|
+
store: ChatLog | None = None,
|
|
19
|
+
engines: dict[str, Engine] | None = None,
|
|
20
|
+
) -> None:
|
|
21
|
+
self.store = store or ChatLog()
|
|
22
|
+
self.engines = engines or default_engines()
|
|
23
|
+
self._chats: dict[str, Chat] = {}
|
|
24
|
+
self._active: dict[str, Engine] = {}
|
|
25
|
+
|
|
26
|
+
def start_chat(self, engine: str, cwd: str) -> str:
|
|
27
|
+
engine = engine.strip().lower()
|
|
28
|
+
if engine not in ENGINES:
|
|
29
|
+
raise ValueError(f"unknown engine: {engine}")
|
|
30
|
+
chat = Chat(uuid.uuid4().hex[:12], engine, cwd)
|
|
31
|
+
self.store.post(chat, "header")
|
|
32
|
+
self._chats[chat.chat_id] = chat
|
|
33
|
+
return chat.chat_id
|
|
34
|
+
|
|
35
|
+
def send(self, chat_id: str, prompt: str) -> Iterator[Event]:
|
|
36
|
+
if chat_id in self._active:
|
|
37
|
+
raise RuntimeError(f"chat {chat_id} already has a running turn")
|
|
38
|
+
chat = self._load(chat_id)
|
|
39
|
+
engine = self._engine_for(chat.engine)
|
|
40
|
+
self.store.post(chat, "user", prompt)
|
|
41
|
+
self._active[chat_id] = engine
|
|
42
|
+
assistant: list[str] = []
|
|
43
|
+
error: str | None = None
|
|
44
|
+
saw_session = bool(chat.external_session_id)
|
|
45
|
+
try:
|
|
46
|
+
for event in engine.send(prompt, cwd=chat.cwd, session_id=chat.external_session_id):
|
|
47
|
+
if event.session_id and event.session_id != chat.external_session_id:
|
|
48
|
+
chat.external_session_id = event.session_id
|
|
49
|
+
self._chats[chat_id] = chat
|
|
50
|
+
if not saw_session:
|
|
51
|
+
self.store.post(chat, "session")
|
|
52
|
+
saw_session = True
|
|
53
|
+
if event.type == "text" and event.text:
|
|
54
|
+
assistant.append(event.text)
|
|
55
|
+
elif event.type == "tool":
|
|
56
|
+
self.store.post(chat, "tool", event.text or event.tool or "tool")
|
|
57
|
+
elif event.type == "error":
|
|
58
|
+
error = event.text or "error"
|
|
59
|
+
yield event
|
|
60
|
+
finally:
|
|
61
|
+
self._active.pop(chat_id, None)
|
|
62
|
+
text = "".join(assistant).strip()
|
|
63
|
+
if text:
|
|
64
|
+
self.store.post(chat, "bot", text)
|
|
65
|
+
if error:
|
|
66
|
+
self.store.post(chat, "error", error)
|
|
67
|
+
|
|
68
|
+
def cancel(self, chat_id: str) -> None:
|
|
69
|
+
engine = self._active.get(chat_id)
|
|
70
|
+
if engine is not None:
|
|
71
|
+
engine.cancel()
|
|
72
|
+
else:
|
|
73
|
+
self._load(chat_id)
|
|
74
|
+
|
|
75
|
+
def list_turns(self, chat_id: str) -> list[dict]:
|
|
76
|
+
return self.store.list_turns(chat_id)
|
|
77
|
+
|
|
78
|
+
def get_chat(self, chat_id: str) -> Chat:
|
|
79
|
+
return self._load(chat_id)
|
|
80
|
+
|
|
81
|
+
def list_chats(self) -> list[dict]:
|
|
82
|
+
return self.store.list_chats()
|
|
83
|
+
|
|
84
|
+
def _engine_for(self, name: str) -> Engine:
|
|
85
|
+
proto = self.engines[name]
|
|
86
|
+
cls = type(proto)
|
|
87
|
+
binary = getattr(proto, "binary", None)
|
|
88
|
+
if binary is not None:
|
|
89
|
+
try:
|
|
90
|
+
return cls(binary=binary)
|
|
91
|
+
except TypeError:
|
|
92
|
+
pass
|
|
93
|
+
return proto
|
|
94
|
+
|
|
95
|
+
def _load(self, chat_id: str) -> Chat:
|
|
96
|
+
chat = self.store.get_chat(chat_id) or self._chats.get(chat_id)
|
|
97
|
+
if chat is None:
|
|
98
|
+
raise KeyError(f"unknown chat: {chat_id}")
|
|
99
|
+
self._chats[chat_id] = chat
|
|
100
|
+
return chat
|
dint/runtime.py
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"""Start logbook-server and dint serve if they are not already up."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import secrets
|
|
7
|
+
import shutil
|
|
8
|
+
import subprocess
|
|
9
|
+
import sys
|
|
10
|
+
import time
|
|
11
|
+
import urllib.error
|
|
12
|
+
import urllib.request
|
|
13
|
+
from pathlib import Path
|
|
14
|
+
from urllib.parse import urlparse
|
|
15
|
+
|
|
16
|
+
from dint.envfile import home_dir, home_env_path, write_env
|
|
17
|
+
|
|
18
|
+
LOGBOOK_INSTALL = "https://logbook.codicent.ai"
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def ensure_runtime(*, serve: bool = True) -> None:
|
|
22
|
+
ensure_defaults()
|
|
23
|
+
ensure_logbook()
|
|
24
|
+
if serve:
|
|
25
|
+
ensure_serve()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def ensure_defaults() -> None:
|
|
29
|
+
home_dir().mkdir(parents=True, exist_ok=True)
|
|
30
|
+
os.environ.setdefault("LOGBOOK_URL", "http://127.0.0.1:5100")
|
|
31
|
+
os.environ.setdefault("LOGBOOK_PROJECT", "dint")
|
|
32
|
+
if not os.environ.get("LOGBOOK_API_KEY", "").strip():
|
|
33
|
+
os.environ["LOGBOOK_API_KEY"] = secrets.token_urlsafe(16)
|
|
34
|
+
path = home_env_path()
|
|
35
|
+
if not path.is_file():
|
|
36
|
+
write_env(
|
|
37
|
+
path,
|
|
38
|
+
{
|
|
39
|
+
"LOGBOOK_URL": os.environ["LOGBOOK_URL"],
|
|
40
|
+
"LOGBOOK_API_KEY": os.environ["LOGBOOK_API_KEY"],
|
|
41
|
+
"LOGBOOK_PROJECT": os.environ["LOGBOOK_PROJECT"],
|
|
42
|
+
},
|
|
43
|
+
)
|
|
44
|
+
os.environ.setdefault("DINT_ENV_FILE", str(path))
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def ensure_logbook() -> None:
|
|
48
|
+
if probe(logbook_url() + "/health"):
|
|
49
|
+
return
|
|
50
|
+
binary = shutil.which("logbook-server")
|
|
51
|
+
if not binary:
|
|
52
|
+
raise RuntimeError(
|
|
53
|
+
"logbook-server is not on PATH and nothing is listening on "
|
|
54
|
+
f"{logbook_url()}. Install it:\n {logbook_install_cmd()}\n"
|
|
55
|
+
f"then re-run dint — {LOGBOOK_INSTALL}"
|
|
56
|
+
)
|
|
57
|
+
env = os.environ.copy()
|
|
58
|
+
parsed = urlparse(logbook_url())
|
|
59
|
+
env["LOGBOOK_PROJECT"] = os.environ.get("LOGBOOK_PROJECT", "dint")
|
|
60
|
+
env["LOGBOOK_API_KEY"] = os.environ.get("LOGBOOK_API_KEY", "")
|
|
61
|
+
env["LOGBOOK_PORT"] = str(parsed.port or 5100)
|
|
62
|
+
env["LOGBOOK_WEBHOOK_URL"] = jobs_url() + "/webhook"
|
|
63
|
+
pid = spawn_detached(
|
|
64
|
+
[binary],
|
|
65
|
+
cwd=str(home_dir()),
|
|
66
|
+
env=env,
|
|
67
|
+
log_path=home_dir() / "logbook.log",
|
|
68
|
+
)
|
|
69
|
+
_note(f"started logbook-server pid={pid}")
|
|
70
|
+
if not wait_for(logbook_url() + "/health"):
|
|
71
|
+
raise RuntimeError(
|
|
72
|
+
f"logbook-server did not become ready at {logbook_url()}. "
|
|
73
|
+
f"See {home_dir() / 'logbook.log'}"
|
|
74
|
+
)
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def ensure_serve() -> None:
|
|
78
|
+
if os.environ.get("DINT_ROLE") == "serve":
|
|
79
|
+
return
|
|
80
|
+
if probe(jobs_url() + "/health"):
|
|
81
|
+
return
|
|
82
|
+
env = os.environ.copy()
|
|
83
|
+
env["DINT_ROLE"] = "serve"
|
|
84
|
+
pid = spawn_detached(
|
|
85
|
+
[
|
|
86
|
+
sys.executable,
|
|
87
|
+
"-m",
|
|
88
|
+
"dint",
|
|
89
|
+
"serve",
|
|
90
|
+
"--host",
|
|
91
|
+
jobs_host(),
|
|
92
|
+
"--port",
|
|
93
|
+
str(jobs_port()),
|
|
94
|
+
],
|
|
95
|
+
cwd=str(home_dir()),
|
|
96
|
+
env=env,
|
|
97
|
+
log_path=home_dir() / "serve.log",
|
|
98
|
+
)
|
|
99
|
+
_note(f"started dint serve pid={pid}")
|
|
100
|
+
if not wait_for(jobs_url() + "/health"):
|
|
101
|
+
raise RuntimeError(
|
|
102
|
+
f"dint serve did not become ready at {jobs_url()}. "
|
|
103
|
+
f"See {home_dir() / 'serve.log'}"
|
|
104
|
+
)
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def serve_running() -> bool:
|
|
108
|
+
return probe(jobs_url() + "/health")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def pick_engine() -> str | None:
|
|
112
|
+
preferred = os.environ.get("DINT_ENGINE", "").strip().lower()
|
|
113
|
+
order = ["grok", "claude", "copilot", "codex"]
|
|
114
|
+
if preferred:
|
|
115
|
+
order = [preferred] + [e for e in order if e != preferred]
|
|
116
|
+
for name in order:
|
|
117
|
+
if shutil.which(name):
|
|
118
|
+
return name
|
|
119
|
+
return None
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def logbook_url() -> str:
|
|
123
|
+
return os.environ.get("LOGBOOK_URL", "http://127.0.0.1:5100").rstrip("/")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def jobs_host() -> str:
|
|
127
|
+
return os.environ.get("DINT_JOBS_HOST", "127.0.0.1")
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def jobs_port() -> int:
|
|
131
|
+
return int(os.environ.get("DINT_JOBS_PORT", "8787"))
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def jobs_url() -> str:
|
|
135
|
+
return f"http://{jobs_host()}:{jobs_port()}"
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def logbook_install_cmd() -> str:
|
|
139
|
+
if sys.platform == "win32":
|
|
140
|
+
return "iwr https://logbook.codicent.ai/install.ps1 | iex"
|
|
141
|
+
return "curl -fsSL https://logbook.codicent.ai/install.sh | bash"
|
|
142
|
+
|
|
143
|
+
|
|
144
|
+
def probe(url: str, timeout: float = 1.5) -> bool:
|
|
145
|
+
try:
|
|
146
|
+
req = urllib.request.Request(url, method="GET", headers={"Accept": "application/json"})
|
|
147
|
+
with urllib.request.urlopen(req, timeout=timeout):
|
|
148
|
+
return True
|
|
149
|
+
except (urllib.error.URLError, urllib.error.HTTPError, TimeoutError, OSError):
|
|
150
|
+
return False
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def wait_for(url: str, *, timeout: float = 12) -> bool:
|
|
154
|
+
deadline = time.time() + timeout
|
|
155
|
+
while time.time() < deadline:
|
|
156
|
+
if probe(url):
|
|
157
|
+
return True
|
|
158
|
+
time.sleep(0.2)
|
|
159
|
+
return False
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def spawn_detached(
|
|
163
|
+
argv: list[str],
|
|
164
|
+
*,
|
|
165
|
+
cwd: str,
|
|
166
|
+
env: dict[str, str],
|
|
167
|
+
log_path: Path,
|
|
168
|
+
) -> int:
|
|
169
|
+
log_path.parent.mkdir(parents=True, exist_ok=True)
|
|
170
|
+
log = open(log_path, "a", encoding="utf-8")
|
|
171
|
+
kwargs: dict = {
|
|
172
|
+
"cwd": cwd,
|
|
173
|
+
"env": env,
|
|
174
|
+
"stdin": subprocess.DEVNULL,
|
|
175
|
+
"stdout": log,
|
|
176
|
+
"stderr": log,
|
|
177
|
+
"close_fds": True,
|
|
178
|
+
}
|
|
179
|
+
if sys.platform == "win32":
|
|
180
|
+
no_window = getattr(subprocess, "CREATE_NO_WINDOW", 0)
|
|
181
|
+
base = subprocess.CREATE_NEW_PROCESS_GROUP | no_window
|
|
182
|
+
# 0x01000000 = CREATE_BREAKAWAY_FROM_JOB so serve outlives the parent shell
|
|
183
|
+
kwargs["creationflags"] = base | 0x01000000
|
|
184
|
+
try:
|
|
185
|
+
proc = subprocess.Popen(argv, **kwargs)
|
|
186
|
+
except OSError:
|
|
187
|
+
kwargs["creationflags"] = base
|
|
188
|
+
proc = subprocess.Popen(argv, **kwargs)
|
|
189
|
+
return int(proc.pid)
|
|
190
|
+
kwargs["start_new_session"] = True
|
|
191
|
+
proc = subprocess.Popen(argv, **kwargs)
|
|
192
|
+
log.close()
|
|
193
|
+
return int(proc.pid)
|
|
194
|
+
|
|
195
|
+
|
|
196
|
+
def _note(msg: str) -> None:
|
|
197
|
+
print(msg, file=sys.stderr, flush=True)
|