-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproject_editor.py
More file actions
431 lines (374 loc) · 14.1 KB
/
Copy pathproject_editor.py
File metadata and controls
431 lines (374 loc) · 14.1 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#!/usr/bin/env python3
"""
project_editor.py - Meglévő projekt context.json szerkesztése (v5)
- Projekt lista + aktív projekt kijelzése indításkor
- Menü: szerkesztés, új projekt, aktív projekt váltás, kilépés
- Szerkesztés után opcionális aktiválás
- Biztonsági mentés, séma verzió kezelés
"""
import os
import json
import datetime
import shutil
import sys
import os
sys.path.insert(0, os.path.join(os.path.dirname(os.path.abspath(__file__)), "utils"))
from i18n_py import t
from pathlib import Path
# =========================
# CONFIG
# =========================
WORK_DIR = Path(__file__).resolve().parent
PROJECTS_ROOT = WORK_DIR / "projects"
MY_PROJECTS_DIR = PROJECTS_ROOT / "my_projects"
ACTIVE_PROJECT_FILE = PROJECTS_ROOT / "active_project.json"
SCHEMA_VERSION = 3
# =========================
# INPUT HELPERS
# =========================
def get_input(prompt, default=None, required=True):
while True:
full = prompt
if default is not None:
full += f" [{default}]"
full += ": "
v = input(full).strip()
if not v and default is not None:
return default
if not v and required:
print("❌ " + t("editor.required_field"))
continue
return v
def yes_no(prompt, default=False):
import os
lang = os.environ.get('ARCSI_LANG', 'en')
if lang == 'hu':
d = "i" if default else "n"
v = get_input(prompt + " (i/n)", default=d, required=False)
return v.lower() == "i"
else:
d = "y" if default else "n"
v = get_input(prompt + " (y/n)", default=d, required=False)
return v.lower() == "y"
def safe_json_input(prompt, existing=None):
print(prompt)
if existing is not None:
print(json.dumps(existing, indent=2, ensure_ascii=False))
print(t("editor.json_or_enter"))
v = input().strip()
if not v:
return existing
for _ in range(3):
try:
return json.loads(v)
except json.JSONDecodeError:
print("❌ " + t("editor.invalid_json"))
v = input().strip()
print("⚠️ " + t("editor.json_rejected"))
return existing
def get_multiline_input(prompt, existing=None):
print(prompt)
if existing:
print(t("editor.current"))
for l in existing:
print(" -", l)
print(t("editor.enter_to_finish"))
out = []
while True:
l = input()
if l == "":
break
out.append(l)
return out if out else existing
# =========================
# PROJECT LIST & ACTIVE
# =========================
def list_projects():
if not MY_PROJECTS_DIR.exists():
return []
return sorted([
d.name for d in MY_PROJECTS_DIR.iterdir()
if d.is_dir() and (d / "context.json").exists()
])
def get_active_project_name():
if not ACTIVE_PROJECT_FILE.exists():
return None
try:
with open(ACTIVE_PROJECT_FILE, "r", encoding="utf-8") as f:
data = json.load(f)
return data.get("name")
except:
return None
def show_projects_and_active():
projects = list_projects()
active = get_active_project_name()
print("\n" + "=" * 50)
print("📁 " + t("editor.available_projects"))
for p in projects:
marker = t("editor.active_marker") if p == active else ""
print(f" - {p}{marker}")
if active and active not in projects:
print(" ⚠️ " + t("editor.active_not_found", name=active))
print("=" * 50)
def set_active_project(project_name):
"""Beállítja az aktív projektet (csak fájlba ír, nem szerkeszt)."""
if project_name not in list_projects():
print("❌ " + t("editor.project_not_found", name=project_name))
return False
active_data = {
"name": project_name,
"path": f"projects/my_projects/{project_name}",
"set_at": datetime.datetime.now().isoformat()
}
with open(ACTIVE_PROJECT_FILE, "w", encoding="utf-8") as f:
json.dump(active_data, f, indent=2, ensure_ascii=False)
pass # üzenet a hívó helyen jelenik meg
return True
# =========================
# CREATE BLANK PROJECT
# =========================
def create_blank_project(project_name):
"""Létrehoz egy üres projekt sémát."""
project_name = project_name.replace(" ", "_").lower()
path = MY_PROJECTS_DIR / project_name
file = path / "context.json"
if file.exists():
print("⚠️ " + t("editor.project_exists", name=project_name))
return project_name
path.mkdir(parents=True, exist_ok=True)
blank_ctx = {
"name": project_name,
"description": "",
"system_prompt": "Te egy precíz és célorientált AI asszisztens vagy.",
"rules": [],
"schema_version": SCHEMA_VERSION,
"created_at": datetime.datetime.now().isoformat(),
"updated_at": datetime.datetime.now().isoformat()
}
with open(file, "w", encoding="utf-8") as f:
json.dump(blank_ctx, f, indent=2, ensure_ascii=False)
print("✨ " + t("editor.project_initialized", file=file))
return project_name
# =========================
# RENAME SAFE
# =========================
def rename_project(old, new):
if old == new:
return new
old_p = MY_PROJECTS_DIR / old
new_p = MY_PROJECTS_DIR / new
if new_p.exists():
print("❌ Már létezik ilyen projekt")
return old
shutil.move(str(old_p), str(new_p))
print(f"📁 Rename: {old} → {new}")
if ACTIVE_PROJECT_FILE.exists():
with open(ACTIVE_PROJECT_FILE, "r", encoding="utf-8") as f:
active = json.load(f)
if active.get("name") == old:
active["name"] = new
active["path"] = f"projects/my_projects/{new}"
with open(ACTIVE_PROJECT_FILE, "w", encoding="utf-8") as f:
json.dump(active, f, indent=2, ensure_ascii=False)
return new
# =========================
# EDITOR CORE
# =========================
def make_backup(project_path, file_path):
if not file_path.exists():
return
timestamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S")
backup_file = project_path / f"context_{timestamp}.json.bak"
shutil.copy2(str(file_path), str(backup_file))
print("📦 " + t("editor.backup", file=backup_file.name))
backups = sorted(project_path.glob("*.json.bak"))
if len(backups) > 3:
for old in backups[:-3]:
old.unlink()
def edit_project(project_name, ask_activate=True):
"""Szerkeszt egy meglévő projektet, és ha ask_activate=True, megkérdezi a végén hogy aktiválja-e."""
path = MY_PROJECTS_DIR / project_name
file = path / "context.json"
if not file.exists():
print("❌ " + t("editor.project_not_found", name=project_name))
return None
with open(file, "r", encoding="utf-8") as f:
ctx = json.load(f)
make_backup(path, file)
print("\n" + "=" * 60)
print("✏️ " + t("editor.editing", name=project_name))
print("=" * 60)
# Név (átnevezés)
new_name = get_input(t("editor.project_name"), default=ctx.get("name", ""), required=True)
new_name = new_name.replace(" ", "_").lower()
if new_name != project_name:
project_name = rename_project(project_name, new_name)
path = MY_PROJECTS_DIR / project_name
file = path / "context.json"
ctx["name"] = project_name
# Core mezők
ctx["description"] = get_input(t("editor.description"), ctx.get("description", ""), True)
ctx["system_prompt"] = get_input(t("editor.system_prompt"), ctx.get("system_prompt", ""))
ctx["rules"] = get_multiline_input(t("editor.rules"), ctx.get("rules", []))
# Intent
if yes_no(t("editor.edit_intent"), "intent" in ctx):
intent = ctx.get("intent", {})
intent["goal"] = get_input("Goal", intent.get("goal", ""))
intent["success_criteria"] = get_multiline_input(
t("editor.success_criteria"),
intent.get("success_criteria", [])
)
ctx["intent"] = {k: v for k, v in intent.items() if v}
else:
ctx.pop("intent", None)
# Hardware
if yes_no(t("editor.edit_hardware"), "hardware" in ctx):
hw = ctx.get("hardware", {})
hw["board"] = get_input("Board", hw.get("board", ""))
hw["ram"] = get_input("RAM", hw.get("ram", ""))
hw["network"] = get_input("Network", hw.get("network", ""))
hw["storage"] = safe_json_input("Storage JSON", hw.get("storage", []))
ctx["hardware"] = {k: v for k, v in hw.items() if v}
else:
ctx.pop("hardware", None)
# Software
if yes_no(t("editor.edit_software"), "software" in ctx):
ctx["software"] = safe_json_input("Software JSON", ctx.get("software", []))
else:
ctx.pop("software", None)
# Extra
extra = ctx.get("extra", {})
if yes_no(t("editor.edit_extra"), bool(extra)):
while True:
print("\n(a add / b edit / c del / d done)")
a = get_input("action", "d")
if a == "a":
k = get_input("key")
v = safe_json_input("value")
extra[k] = {"value": v, "type": type(v).__name__}
elif a == "b":
print(list(extra.keys()))
k = get_input("key")
if k in extra:
v = safe_json_input("new value", extra[k]["value"])
extra[k] = {"value": v, "type": type(v).__name__}
elif a == "c":
k = get_input("delete key")
extra.pop(k, None)
elif a == "d":
break
if extra:
ctx["extra"] = extra
else:
ctx.pop("extra", None)
# META
if "created_at" not in ctx:
ctx["created_at"] = datetime.datetime.now().isoformat()
ctx["updated_at"] = datetime.datetime.now().isoformat()
ctx["schema_version"] = SCHEMA_VERSION
# Mentés
with open(file, "w", encoding="utf-8") as f:
json.dump(ctx, f, indent=2, ensure_ascii=False)
print("\n✅ " + t("editor.saved", file=file))
# Aktiválási kérdés (ha kell)
if ask_activate and yes_no(t("editor.set_active_after_edit", name=project_name), default=True):
set_active_project(project_name)
return project_name
# =========================
# MAIN MENU
# =========================
def main():
# Biztosítjuk a mappák létezését
MY_PROJECTS_DIR.mkdir(parents=True, exist_ok=True)
PROJECTS_ROOT.mkdir(parents=True, exist_ok=True)
# Indításkor: lista és aktív projekt kijelzése
show_projects_and_active()
# Főmenü
while True:
print("\n🎯 " + t("editor.what_to_do"))
print("1. " + t("editor.edit_project"))
print("2. " + t("editor.new_project"))
print("3. " + t("editor.switch_project"))
print("0. " + t("editor.exit"))
choice = get_input(t("editor.choose_prompt"), required=False).strip()
if choice == "0":
print(t("editor.goodbye"))
sys.exit(0)
elif choice == "1":
# Szerkesztés – válassz projektet
projects = list_projects()
if not projects:
print("❌ " + t("editor.no_projects_create"))
continue
print("\n" + t("editor.choose_project"))
for i, p in enumerate(projects, 1):
print(f"{i}. {p}")
sel = get_input(t("editor.project_name_or_number"), required=True).strip()
if sel.isdigit():
idx = int(sel) - 1
if 0 <= idx < len(projects):
proj = projects[idx]
else:
print("❌ " + t("editor.invalid_number"))
continue
else:
if sel in projects:
proj = sel
else:
print(f"❌ A(z) '{sel}' projekt nem létezik.")
continue
# Szerkesztés (a végén kérdez aktiválásról)
edit_project(proj, ask_activate=True)
elif choice == "2":
# Új projekt létrehozása (csak név bekérés, majd létrehozás, majd szerkesztés)
new_name = get_input(t("editor.new_project_name"), required=True)
new_name = new_name.replace(" ", "_").lower()
if new_name in list_projects():
print(f"❌ A(z) '{new_name}' már létezik. Szerkeszd az 1-es menüpontban.")
continue
created = create_blank_project(new_name)
if created:
# Szerkesztés azonnal, a végén aktiválási kérdéssel
edit_project(created, ask_activate=True)
elif choice == "3":
# Csak aktiválás (nem megy szerkesztőbe)
projects = list_projects()
if not projects:
print("❌ " + t("editor.no_projects_switch"))
continue
print("\n" + t("editor.which_project_active"))
for i, p in enumerate(projects, 1):
print(f"{i}. {p}")
sel = get_input(t("editor.choose_number_or_name"), required=True).strip()
if sel.isdigit():
idx = int(sel) - 1
if 0 <= idx < len(projects):
proj = projects[idx]
else:
print("❌ " + t("editor.invalid_number"))
continue
else:
if sel in projects:
proj = sel
else:
print(f"❌ A(z) '{sel}' projekt nem létezik.")
continue
set_active_project(proj)
print(t("editor.daemon_restarting"))
import subprocess, os
subprocess.run(['pkill', '-f', 'runtime.server'], stderr=subprocess.DEVNULL)
import time
time.sleep(1)
subprocess.Popen(
['python3', '-m', 'runtime.server'],
cwd=os.path.dirname(os.path.abspath(__file__)),
stdout=subprocess.DEVNULL,
stderr=subprocess.DEVNULL
)
time.sleep(2)
print("✅ " + t("editor.done_exit"))
sys.exit(0)
else:
print("❌ " + t("editor.invalid_choice"))
if __name__ == "__main__":
main()