added repl injection
This commit is contained in:
95
CLI.py
95
CLI.py
@@ -78,6 +78,7 @@ def _install_rich_traceback(*, show_locals: bool = False) -> None:
|
||||
_install_rich_traceback(show_locals=False)
|
||||
|
||||
from SYS.logger import debug, set_debug
|
||||
from SYS.repl_queue import pop_repl_commands
|
||||
from SYS.worker_manager import WorkerManager
|
||||
|
||||
from SYS.cmdlet_catalog import (
|
||||
@@ -2155,11 +2156,101 @@ Come to love it when others take what you share, as there is no greater joy
|
||||
refresh_interval=0.5,
|
||||
)
|
||||
|
||||
queued_inputs: List[Dict[str, Any]] = []
|
||||
queued_inputs_lock = threading.Lock()
|
||||
repl_queue_stop = threading.Event()
|
||||
|
||||
def _drain_repl_queue() -> None:
|
||||
try:
|
||||
pending = pop_repl_commands(self.ROOT, limit=8)
|
||||
except Exception:
|
||||
pending = []
|
||||
if not pending:
|
||||
return
|
||||
with queued_inputs_lock:
|
||||
queued_inputs.extend(pending)
|
||||
|
||||
def _inject_repl_command(payload: Dict[str, Any]) -> bool:
|
||||
nonlocal session
|
||||
command_text = str(payload.get("command") or "").strip()
|
||||
source_text = str(payload.get("source") or "external").strip() or "external"
|
||||
if not command_text or session is None:
|
||||
return False
|
||||
|
||||
update_toolbar(f"queued from {source_text}: {command_text[:96]}")
|
||||
app = getattr(session, "app", None)
|
||||
if app is None or not getattr(app, "is_running", False):
|
||||
return False
|
||||
|
||||
injected = False
|
||||
|
||||
def _apply() -> None:
|
||||
nonlocal injected
|
||||
try:
|
||||
buffer = getattr(session, "default_buffer", None)
|
||||
if buffer is not None:
|
||||
buffer.document = Document(text=command_text, cursor_position=len(command_text))
|
||||
try:
|
||||
buffer.validate_and_handle()
|
||||
injected = True
|
||||
return
|
||||
except Exception:
|
||||
pass
|
||||
app.exit(result=command_text)
|
||||
injected = True
|
||||
except Exception:
|
||||
injected = False
|
||||
|
||||
try:
|
||||
loop = getattr(app, "loop", None)
|
||||
if loop is not None and hasattr(loop, "call_soon_threadsafe"):
|
||||
loop.call_soon_threadsafe(_apply)
|
||||
return True
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
try:
|
||||
_apply()
|
||||
except Exception:
|
||||
injected = False
|
||||
return injected
|
||||
|
||||
def _queue_poll_loop() -> None:
|
||||
while not repl_queue_stop.is_set():
|
||||
_drain_repl_queue()
|
||||
with queued_inputs_lock:
|
||||
next_payload = queued_inputs[0] if queued_inputs else None
|
||||
if next_payload and _inject_repl_command(next_payload):
|
||||
with queued_inputs_lock:
|
||||
if queued_inputs and queued_inputs[0] is next_payload:
|
||||
queued_inputs.pop(0)
|
||||
repl_queue_stop.wait(0.25)
|
||||
|
||||
_drain_repl_queue()
|
||||
repl_queue_thread = threading.Thread(
|
||||
target=_queue_poll_loop,
|
||||
name="medeia-repl-queue",
|
||||
daemon=True,
|
||||
)
|
||||
repl_queue_thread.start()
|
||||
|
||||
while True:
|
||||
try:
|
||||
user_input = session.prompt(prompt_text).strip()
|
||||
with queued_inputs_lock:
|
||||
queued_payload = queued_inputs.pop(0) if queued_inputs else None
|
||||
|
||||
if queued_payload is not None:
|
||||
source_text = str(queued_payload.get("source") or "external").strip() or "external"
|
||||
user_input = str(queued_payload.get("command") or "").strip()
|
||||
if user_input:
|
||||
print(f"{prompt_text}{user_input} [queued:{source_text}]")
|
||||
else:
|
||||
user_input = ""
|
||||
else:
|
||||
user_input = session.prompt(prompt_text).strip()
|
||||
except (EOFError, KeyboardInterrupt):
|
||||
print("He who is victorious through deceit is defeated by the truth.")
|
||||
repl_queue_stop.set()
|
||||
break
|
||||
|
||||
if not user_input:
|
||||
@@ -2346,6 +2437,8 @@ Come to love it when others take what you share, as there is no greater joy
|
||||
if pipeline_ctx_ref:
|
||||
pipeline_ctx_ref.clear_current_command_text()
|
||||
|
||||
repl_queue_stop.set()
|
||||
|
||||
|
||||
|
||||
_PTK_Lexer = object # type: ignore
|
||||
|
||||
Reference in New Issue
Block a user