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

Commit e29535b

Browse files
committed
fix: solve problems with codegate cli and previous context
Cline was not properly detecting the codegate cli commands when there was some previous context
1 parent e50c6aa commit e29535b

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

src/codegate/pipeline/cli/cli.py

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -77,27 +77,47 @@ async def process(
7777

7878
if last_user_message is not None:
7979
last_user_message_str, _ = last_user_message
80-
cleaned_message_str = re.sub(r"<.*?>", "", last_user_message_str).strip()
81-
splitted_message = cleaned_message_str.lower().split(" ")
82-
# We expect codegate as the first word in the message
83-
if splitted_message[0] == "codegate":
84-
context.shortcut_response = True
85-
args = shlex.split(cleaned_message_str)
86-
cmd_out = await codegate_cli(args[1:])
87-
88-
if cleaned_message_str != last_user_message_str:
89-
# it came from Cline, need to wrap into tags
90-
cmd_out = (
91-
f"<attempt_completion><result>{cmd_out}</result></attempt_completion>\n"
80+
last_user_message_str = last_user_message_str.strip()
81+
is_cline_client = any(
82+
"Cline" in str(message.get("content", "")) for message in request.get("messages",
83+
[])
84+
)
85+
if not is_cline_client:
86+
# Check if "codegate" is the first word in the message
87+
match = re.match(r"^codegate(?:\s+(\S+))?", last_user_message_str, re.IGNORECASE)
88+
else:
89+
# Check if "codegate" is the first word after the first XML tag
90+
xml_start = re.search(r"<[^>]+>", last_user_message_str)
91+
if xml_start:
92+
# Start processing only from the first XML tag
93+
relevant_message = last_user_message_str[xml_start.start():]
94+
# Remove all XML tags and trim whitespace
95+
stripped_message = re.sub(r"<[^>]+>", "", relevant_message).strip()
96+
# Check if "codegate" is the first word
97+
match = re.match(r"^codegate(?:\s+(\S+))?", stripped_message, re.IGNORECASE)
98+
else:
99+
match = None
100+
if match:
101+
command = match.group(1) # Extract the second word
102+
command = command.strip()
103+
104+
# Process the command
105+
args = shlex.split(f"codegate {command}")
106+
if args:
107+
cmd_out = await codegate_cli(args[1:])
108+
109+
if is_cline_client:
110+
cmd_out = (
111+
f"<attempt_completion><result>{cmd_out}</result></attempt_completion>\n"
112+
)
113+
return PipelineResult(
114+
response=PipelineResponse(
115+
step_name=self.name,
116+
content=cmd_out,
117+
model=request["model"],
118+
),
119+
context=context,
92120
)
93-
return PipelineResult(
94-
response=PipelineResponse(
95-
step_name=self.name,
96-
content=cmd_out,
97-
model=request["model"],
98-
),
99-
context=context,
100-
)
101121

102122
# Fall through
103123
return PipelineResult(request=request, context=context)

src/codegate/providers/copilot/provider.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -291,12 +291,15 @@ async def _forward_data_through_pipeline(self, data: bytes) -> Union[HttpRequest
291291
# we couldn't parse this into an HTTP request, so we just pass through
292292
return data
293293

294-
body, context = await self._body_through_pipeline(
294+
result = await self._body_through_pipeline(
295295
http_request.method,
296296
http_request.path,
297297
http_request.headers,
298298
http_request.body,
299299
)
300+
if not result:
301+
return data
302+
body, context = result
300303
# TODO: it's weird that we're overwriting the context.
301304
# Should we set the context once? Maybe when
302305
# creating the pipeline instance?

0 commit comments

Comments
 (0)