Skip to content

Commit

Permalink
fix to allow subcommands
Browse files Browse the repository at this point in the history
  • Loading branch information
yrobla committed Jan 24, 2025
1 parent ccca29b commit 22fdab0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 21 deletions.
42 changes: 21 additions & 21 deletions src/codegate/pipeline/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
PipelineStep,
)
from codegate.pipeline.cli.commands import CustomInstructions, Version, Workspace
from codegate.utils.utils import get_tool_name_from_messages

HELP_TEXT = """
## CodeGate CLI\n
Expand Down Expand Up @@ -78,35 +79,34 @@ async def process(
if last_user_message is not None:
last_user_message_str, _ = last_user_message
last_user_message_str = last_user_message_str.strip()
is_cline_client = any(
"Cline" in str(message.get("content", ""))
for message in request.get("messages", [])
)
if not is_cline_client:
# Check if "codegate" is the first word in the message
match = re.match(r"^codegate(?:\s+(\S+))?", last_user_message_str, re.IGNORECASE)
else:
# Check if "codegate" is the first word after the first XML tag
xml_start = re.search(r"<[^>]+>", last_user_message_str)
if xml_start:
# Start processing only from the first XML tag
relevant_message = last_user_message_str[xml_start.start() :]
# Remove all XML tags and trim whitespace
stripped_message = re.sub(r"<[^>]+>", "", relevant_message).strip()
# Check if "codegate" is the first word
match = re.match(r"^codegate(?:\s+(\S+))?", stripped_message, re.IGNORECASE)
base_tool = get_tool_name_from_messages(request)
if base_tool and base_tool == "cline":
# Check if there are <task> or <feedback> tags
tag_match = re.search(r"<(task|feedback)>(.*?)</\1>", last_user_message_str, re.DOTALL)
if tag_match:
# Extract the content between the tags
stripped_message = tag_match.group(2).strip()
else:
match = None
# If no <task> or <feedback> tags, use the entire message
stripped_message = last_user_message_str.strip()

# Remove all other XML tags and trim whitespace
stripped_message = re.sub(r"<[^>]+>", "", stripped_message).strip()

# Check if "codegate" is the first word
match = re.match(r"^codegate(?:\s+(.*))?", stripped_message, re.IGNORECASE)
else:
# Check if "codegate" is the first word in the message
match = re.match(r"^codegate(?:\s+(.*))?", last_user_message_str, re.IGNORECASE)
if match:
command = match.group(1) # Extract the second word
command = match.group(1) or ""
command = command.strip()

# Process the command
args = shlex.split(f"codegate {command}")
if args:
cmd_out = await codegate_cli(args[1:])

if is_cline_client:
if base_tool and base_tool == "cline":
cmd_out = (
f"<attempt_completion><result>{cmd_out}</result></attempt_completion>\n"
)
Expand Down
20 changes: 20 additions & 0 deletions src/codegate/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,23 @@ def generate_vector_string(package) -> str:
# add description
vector_str += f" - Package offers this functionality: {package['description']}"
return vector_str


def get_tool_name_from_messages(data):
"""
Identifies the tool name based on the content of the messages.
Args:
request (dict): The request object containing messages.
tools (list): A list of tool names to search for.
Returns:
str: The name of the tool found in the messages, or None if no match is found.
"""
tools = ["Cline",]
for message in data.get("messages", []):
message_content = str(message.get("content", ""))
for tool in tools:
if tool in message_content:
return tool.lower()
return None

0 comments on commit 22fdab0

Please sign in to comment.