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

Commit 7d61fdb

Browse files
Fix chat message parsing in Cline (#996)
We receive messages from Cline in the format ``` "messages": [ {"role": "system", "content": "You are Cline, a highly skilled software"}, { "role": "user", "content": [ { "type": "text", "text": "Here is the content" } ] ] ``` Fixed the unit test and added a new one for Continue
1 parent 1582c2a commit 7d61fdb

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/codegate/extract_snippets/body_extractor.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,25 @@ class ClineBodySnippetExtractor(BodyCodeSnippetExtractor):
7070
def __init__(self):
7171
self._snippet_extractor = ClineCodeSnippetExtractor()
7272

73+
def _extract_from_user_messages(self, data: dict) -> set[str]:
74+
"""
75+
The method extracts the code snippets from the user messages in the data got from Cline.
76+
77+
It returns a set of filenames extracted from the code snippets.
78+
"""
79+
80+
filenames: List[str] = []
81+
for msg in data.get("messages", []):
82+
if msg.get("role", "") == "user":
83+
msgs_content = msg.get("content", [])
84+
for msg_content in msgs_content:
85+
if msg_content.get("type", "") == "text":
86+
extracted_snippets = self._snippet_extractor.extract_unique_snippets(
87+
msg_content.get("text")
88+
)
89+
filenames.extend(extracted_snippets.keys())
90+
return set(filenames)
91+
7392
def extract_unique_filenames(self, data: dict) -> set[str]:
7493
return self._extract_from_user_messages(data)
7594

src/codegate/muxing/router.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ async def _get_model_route(
5252
return model_route
5353
except Exception as e:
5454
logger.error(f"Error getting active workspace muxes: {e}")
55-
raise HTTPException(str(e), status_code=404)
55+
raise HTTPException(detail=str(e), status_code=404)
5656

5757
def _setup_routes(self):
5858

@@ -85,7 +85,7 @@ async def route_to_dest_provider(
8585
model_route = await self._get_model_route(thing_to_match)
8686
if not model_route:
8787
raise HTTPException(
88-
"No matching rule found for the active workspace", status_code=404
88+
detail="No matching rule found for the active workspace", status_code=404
8989
)
9090

9191
logger.info(

tests/extract_snippets/test_body_extractor.py

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
from codegate.extract_snippets.body_extractor import (
66
ClineBodySnippetExtractor,
7+
ContinueBodySnippetExtractor,
78
OpenInterpreterBodySnippetExtractor,
89
)
910

@@ -75,9 +76,13 @@ def test_body_extract_openinterpreter_snippets(test_case: BodyCodeSnippetTest):
7576
BodyCodeSnippetTest(
7677
input_body_dict={
7778
"messages": [
79+
{"role": "system", "content": "You are Cline, a highly skilled software"},
7880
{
7981
"role": "user",
80-
"content": '''
82+
"content": [
83+
{
84+
"type": "text",
85+
"text": '''
8186
[<task>
8287
now please analyze the folder 'codegate/src/codegate/api/' (see below for folder content)
8388
</task>
@@ -145,6 +150,8 @@ async def _process_prompt_output_to_partial_qa(
145150
</file_content>
146151
</folder_content>
147152
''',
153+
}
154+
],
148155
},
149156
]
150157
},
@@ -157,3 +164,52 @@ def test_body_extract_cline_snippets(test_case: BodyCodeSnippetTest):
157164
extractor = ClineBodySnippetExtractor()
158165
filenames = extractor.extract_unique_filenames(test_case.input_body_dict)
159166
_evaluate_actual_filenames(filenames, test_case)
167+
168+
169+
@pytest.mark.parametrize(
170+
"test_case",
171+
[
172+
# Analyze processed snippets from OpenInterpreter
173+
BodyCodeSnippetTest(
174+
input_body_dict={
175+
"messages": [
176+
{
177+
"role": "user",
178+
"content": """
179+
```file:///Users/user/StacklokRepos/testing_file.py
180+
import invokehttp
181+
import fastapi
182+
from fastapi import FastAPI, Request, Response, HTTPException
183+
import numpy
184+
185+
GITHUB_TOKEN="ghp_1J9Z3Z2dfg4dfs23dsfsdf232aadfasdfasfasdf32"
186+
187+
def add(a, b):
188+
return a + b
189+
190+
def multiply(a, b):
191+
return a * b
192+
193+
194+
195+
def substract(a, b):
196+
197+
```
198+
199+
please analyze testing_file.py
200+
""",
201+
}
202+
],
203+
"model": "foo-model-replaced-by-mux",
204+
"max_tokens": 4096,
205+
"stream": True,
206+
},
207+
expected_count=1,
208+
expected=["testing_file.py"],
209+
),
210+
],
211+
)
212+
def test_body_extract_continue_snippets(test_case: BodyCodeSnippetTest):
213+
extractor = ContinueBodySnippetExtractor()
214+
filenames = extractor.extract_unique_filenames(test_case.input_body_dict)
215+
_evaluate_actual_filenames(filenames, test_case)

0 commit comments

Comments
 (0)