mirror of
https://github.com/All-Hands-AI/OpenHands.git
synced 2026-04-29 03:00:45 -04:00
CLI: Create conversation last minute (#11576)
Co-authored-by: openhands <openhands@all-hands.dev> Co-authored-by: Engel Nyst <enyst@users.noreply.github.com>
This commit is contained in:
@@ -2,7 +2,7 @@ import uuid
|
||||
|
||||
from prompt_toolkit import HTML, print_formatted_text
|
||||
|
||||
from openhands.sdk import BaseConversation, Conversation, Workspace, register_tool
|
||||
from openhands.sdk import Agent, BaseConversation, Conversation, Workspace, register_tool
|
||||
from openhands.tools.execute_bash import BashTool
|
||||
from openhands.tools.file_editor import FileEditorTool
|
||||
from openhands.tools.task_tracker import TaskTrackerTool
|
||||
@@ -26,8 +26,38 @@ class MissingAgentSpec(Exception):
|
||||
pass
|
||||
|
||||
|
||||
def setup_conversation(
|
||||
|
||||
def load_agent_specs(
|
||||
conversation_id: str | None = None,
|
||||
) -> Agent:
|
||||
agent_store = AgentStore()
|
||||
agent = agent_store.load(session_id=conversation_id)
|
||||
if not agent:
|
||||
raise MissingAgentSpec(
|
||||
'Agent specification not found. Please configure your agent settings.'
|
||||
)
|
||||
return agent
|
||||
|
||||
|
||||
def verify_agent_exists_or_setup_agent() -> Agent:
|
||||
"""Verify agent specs exists by attempting to load it.
|
||||
|
||||
"""
|
||||
settings_screen = SettingsScreen()
|
||||
try:
|
||||
agent = load_agent_specs()
|
||||
return agent
|
||||
except MissingAgentSpec:
|
||||
# For first-time users, show the full settings flow with choice between basic/advanced
|
||||
settings_screen.configure_settings(first_time=True)
|
||||
|
||||
|
||||
# Try once again after settings setup attempt
|
||||
return load_agent_specs()
|
||||
|
||||
|
||||
def setup_conversation(
|
||||
conversation_id: uuid,
|
||||
include_security_analyzer: bool = True
|
||||
) -> BaseConversation:
|
||||
"""
|
||||
@@ -40,28 +70,8 @@ def setup_conversation(
|
||||
MissingAgentSpec: If agent specification is not found or invalid.
|
||||
"""
|
||||
|
||||
# Use provided conversation_id or generate a random one
|
||||
if conversation_id is None:
|
||||
conversation_id = uuid.uuid4()
|
||||
elif isinstance(conversation_id, str):
|
||||
try:
|
||||
conversation_id = uuid.UUID(conversation_id)
|
||||
except ValueError as e:
|
||||
print_formatted_text(
|
||||
HTML(
|
||||
f"<yellow>Warning: '{conversation_id}' is not a valid UUID.</yellow>"
|
||||
)
|
||||
)
|
||||
raise e
|
||||
|
||||
with LoadingContext('Initializing OpenHands agent...'):
|
||||
agent_store = AgentStore()
|
||||
agent = agent_store.load(session_id=str(conversation_id))
|
||||
if not agent:
|
||||
raise MissingAgentSpec(
|
||||
'Agent specification not found. Please configure your agent settings.'
|
||||
)
|
||||
|
||||
agent = load_agent_specs(str(conversation_id))
|
||||
|
||||
if not include_security_analyzer:
|
||||
# Remove security analyzer from agent spec
|
||||
@@ -86,31 +96,3 @@ def setup_conversation(
|
||||
)
|
||||
return conversation
|
||||
|
||||
|
||||
|
||||
def start_fresh_conversation(
|
||||
resume_conversation_id: str | None = None
|
||||
) -> BaseConversation:
|
||||
"""Start a fresh conversation by creating a new conversation instance.
|
||||
|
||||
Handles the complete conversation setup process including settings screen
|
||||
if agent configuration is missing.
|
||||
|
||||
Args:
|
||||
resume_conversation_id: Optional conversation ID to resume
|
||||
|
||||
Returns:
|
||||
BaseConversation: A new conversation instance
|
||||
"""
|
||||
conversation = None
|
||||
settings_screen = SettingsScreen()
|
||||
try:
|
||||
conversation = setup_conversation(resume_conversation_id)
|
||||
return conversation
|
||||
except MissingAgentSpec:
|
||||
# For first-time users, show the full settings flow with choice between basic/advanced
|
||||
settings_screen.configure_settings(first_time=True)
|
||||
|
||||
|
||||
# Try once again after settings setup attempt
|
||||
return setup_conversation(resume_conversation_id)
|
||||
|
||||
Reference in New Issue
Block a user