-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
55 lines (50 loc) · 1.66 KB
/
Copy pathrun.py
File metadata and controls
55 lines (50 loc) · 1.66 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
"""Start or reopen the local Codex Workflow Loop application."""
import argparse
import json
from pathlib import Path
import urllib.request
import webbrowser
from app.service import Service, chat_id
from app.server import make_server
ROOT = Path(__file__).resolve().parent
URL = "http://127.0.0.1:43821"
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--no-browser", action="store_true")
args = parser.parse_args()
try:
with urllib.request.urlopen(URL + "/api/health", timeout=1) as response:
running = json.load(response).get("app") == "workflow-loop"
if running:
if not args.no_browser:
webbrowser.open(URL)
return
except (OSError, ValueError):
pass
storage = ROOT / "data" / "chats.json"
old = ROOT / "chats.json"
if old.exists() and not storage.exists():
migrated = []
for value in json.loads(old.read_text(encoding="utf-8")):
try:
identity = chat_id(value)
if not any(row["id"] == identity for row in migrated):
migrated.append(dict(id=identity, label=""))
except ValueError:
continue
storage.parent.mkdir(exist_ok=True)
storage.write_text(json.dumps(migrated, indent=2), encoding="utf-8")
old.unlink()
service = Service(storage)
server = make_server(service, ROOT / "web")
if not args.no_browser:
webbrowser.open(URL)
try:
server.serve_forever()
except KeyboardInterrupt:
pass
finally:
service.close()
server.server_close()
if __name__ == "__main__":
main()