Skip to content
This repository was archived by the owner on Jun 5, 2025. It is now read-only.

fix: solve problems with codegate cli and previous context #757

Merged
merged 6 commits into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 40 additions & 20 deletions src/codegate/pipeline/cli/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,27 +77,47 @@ async def process(

if last_user_message is not None:
last_user_message_str, _ = last_user_message
cleaned_message_str = re.sub(r"<.*?>", "", last_user_message_str).strip()
splitted_message = cleaned_message_str.lower().split(" ")
# We expect codegate as the first word in the message
if splitted_message[0] == "codegate":
context.shortcut_response = True
args = shlex.split(cleaned_message_str)
cmd_out = await codegate_cli(args[1:])

if cleaned_message_str != last_user_message_str:
# it came from Cline, need to wrap into tags
cmd_out = (
f"<attempt_completion><result>{cmd_out}</result></attempt_completion>\n"
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)
else:
match = None
if match:
command = match.group(1) # Extract the second word
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:
cmd_out = (
f"<attempt_completion><result>{cmd_out}</result></attempt_completion>\n"
)
return PipelineResult(
response=PipelineResponse(
step_name=self.name,
content=cmd_out,
model=request["model"],
),
context=context,
)
return PipelineResult(
response=PipelineResponse(
step_name=self.name,
content=cmd_out,
model=request["model"],
),
context=context,
)

# Fall through
return PipelineResult(request=request, context=context)
5 changes: 4 additions & 1 deletion src/codegate/providers/copilot/provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,12 +291,15 @@ async def _forward_data_through_pipeline(self, data: bytes) -> Union[HttpRequest
# we couldn't parse this into an HTTP request, so we just pass through
return data

body, context = await self._body_through_pipeline(
result = await self._body_through_pipeline(
http_request.method,
http_request.path,
http_request.headers,
http_request.body,
)
if not result:
return data
body, context = result
# TODO: it's weird that we're overwriting the context.
# Should we set the context once? Maybe when
# creating the pipeline instance?
Expand Down
Loading