Skip to content

Commit

Permalink
Improved application interruption and termination
Browse files Browse the repository at this point in the history
  • Loading branch information
yorevs committed Oct 31, 2024
1 parent 96ea494 commit 5570856
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
9 changes: 5 additions & 4 deletions src/main/askai/core/askai.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
from askai.core.processors.ai_processor import AIProcessor
from askai.core.support.chat_context import ChatContext
from askai.core.support.shared_instances import shared
from askai.core.support.utilities import read_stdin
from askai.core.support.utilities import read_stdin, display_text
from askai.exception.exceptions import *
from askai.tui.app_icons import AppIcons

Expand Down Expand Up @@ -131,9 +131,10 @@ def abort(self, signals: Any, frame: Any) -> None:
log.warning(f"User interrupted: signals: {signals} frame: {frame}")
self._abort_count += 1
if self._abort_count > 1:
log.warning(f"User aborted. Exiting!")
sys.exit(ExitStatus.ABORTED)
events.abort.emit(message="User interrupted")
display_text(f"%RED%\n{msg.terminate_requested('User aborted [ctrl+c]')}%NC%", markdown=False)
log.warning(f"User aborted. Exiting…")
self._abort()
events.abort.emit(message="User interrupted [ctrl+c]")
threading.Timer(1, lambda: setattr(self, '_abort_count', 0)).start()
terminal.restore()

Expand Down
4 changes: 2 additions & 2 deletions src/main/askai/core/askai_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,10 @@ def quote_exceeded(self) -> str:
)

def interruption_requested(self, reason: str) -> str:
return f"AI has interrupted the execution => {reason}"
return f" Interrupting execution => {reason}"

def terminate_requested(self, reason: str) -> str:
return f"AI has terminated the execution => {reason}"
return f" Terminating execution => {reason}"


assert (msg := AskAiMessages().INSTANCE) is not None
19 changes: 11 additions & 8 deletions src/main/askai/core/processors/splitter/splitter_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

from askai.core.askai_configs import configs
from askai.core.askai_events import AskAiEvents, ASKAI_BUS_NAME, ABORT_EVENT
from askai.core.askai_messages import msg
from askai.core.enums.acc_color import AccColor
from askai.core.processors.splitter.splitter_pipeline import SplitterPipeline
from askai.core.processors.splitter.splitter_states import States
Expand All @@ -44,19 +45,21 @@ def __init__(self, query: str):
def pipeline(self) -> SplitterPipeline:
return self._pipeline

def display(self, text: str) -> None:
def display(self, text: str, force: bool = False) -> None:
"""Display a debug message if debugging mode is active.
:param text: The debug message to display
:param force: Force displaying the message regardless of the debug flag.
"""
if is_debugging():
if force or is_debugging():
text_formatter.console.print(Text.from_markup(text))

def interrupt(self, ev: Event) -> None:
"""Interrupt the active execution pipeline.
:param ev: The interruption event,
"""
self._interrupted = True
self.display(f"[red] Execution interrupted => {ev.args.message} ![/red]")
if self.is_alive():
self.display(f"[red]{msg.interruption_requested(ev.args.message)} ![/red]", True)
self._interrupted = True

def run(self) -> None:
"""Execute the splitter pipeline."""
Expand All @@ -66,11 +69,11 @@ def run(self) -> None:
self.pipeline.track_previous()
if 1 < configs.max_router_retries < 1 + self.pipeline.failures[self.pipeline.state.value]:
self.display(
f"\n[red] Max retries exceeded: {configs.max_agent_retries}[/red]\n")
f"\n[red] Max retries exceeded: {configs.max_agent_retries}[/red]\n", True)
break
if 1 < configs.max_iteractions < 1 + self.pipeline.iteractions:
self.display(
f"\n[red] Max iteractions exceeded: {configs.max_iteractions}[/red]\n")
f"\n[red] Max iteractions exceeded: {configs.max_iteractions}[/red]\n", True)
break
match self.pipeline.state:
case States.STARTUP:
Expand Down Expand Up @@ -109,7 +112,7 @@ def run(self) -> None:
self.pipeline.ev_final_answer()
case _:
self.display(
f"[red] Error: Machine halted before complete!({self.pipeline.state})[/red]")
f"[red] Error: Machine halted before complete!({self.pipeline.state})[/red]", True)
break

execution_status: bool = self.pipeline.previous != self.pipeline.state
Expand Down Expand Up @@ -137,4 +140,4 @@ def run(self) -> None:

if final_state != States.COMPLETE:
retries: int = self.pipeline.failures[self.pipeline.state.value]
self.display(f" Failed to generate a response after {retries} retries")
self.display(f" Failed to generate a response after {retries} retries", True)

0 comments on commit 5570856

Please sign in to comment.