ctrl in no stream fixed

This commit is contained in:
luijait 2025-05-19 14:44:39 +02:00
parent 770f6b0ac0
commit dad91d79cb
2 changed files with 19 additions and 2 deletions

View File

@ -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

View File

@ -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()