From dad91d79cbccbed3bec6d4ac7156bbfda4f2f8f8 Mon Sep 17 00:00:00 2001 From: luijait Date: Mon, 19 May 2025 14:44:39 +0200 Subject: [PATCH] ctrl in no stream fixed --- src/cai/cli.py | 15 ++++++++++++++- src/cai/tools/common.py | 6 +++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/cai/cli.py b/src/cai/cli.py index a7283c83..b17c3aea 100644 --- a/src/cai/cli.py +++ b/src/cai/cli.py @@ -702,7 +702,20 @@ def run_cai_cli(starting_agent, context_variables=None, max_turns=float('inf')): start_idle_timer() except KeyboardInterrupt: - # No need to clean up streaming context as model handles it + print("\n\033[91mKeyboard interrupt detected\033[0m") + + if hasattr(agent, 'model'): + from cai.sdk.agents.models.openai_chatcompletions import _Converter + if hasattr(_Converter, 'recent_tool_calls'): + for call_id, call_info in _Converter.recent_tool_calls.items(): + # Add a tool response indicating the interruption + tool_msg = { + "role": "tool", + "tool_call_id": call_id, + "content": "Operation interrupted by user", + } + add_to_message_history(tool_msg) + pass except Exception as e: import traceback diff --git a/src/cai/tools/common.py b/src/cai/tools/common.py index 1846d56b..7d6d1cff 100644 --- a/src/cai/tools/common.py +++ b/src/cai/tools/common.py @@ -1260,7 +1260,11 @@ def run_command(command, ctf=None, stdout=False, # pylint: disable=too-many-arg stop_active_timer() start_idle_timer() return result - + except Exception as KeyboardInterrupt: + # Ensure we switch back to idle mode if any unexpected error occurs + stop_active_timer() + start_idle_timer() + return "User interrupted execution pressing ctrl+c" except Exception as e: # Ensure we switch back to idle mode if any unexpected error occurs stop_active_timer()