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

Fix chat message parsing in Cline #996

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions src/codegate/extract_snippets/body_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ class ClineBodySnippetExtractor(BodyCodeSnippetExtractor):
def __init__(self):
self._snippet_extractor = ClineCodeSnippetExtractor()

def _extract_from_user_messages(self, data: dict) -> set[str]:
"""
The method extracts the code snippets from the user messages in the data got from Cline.

It returns a set of filenames extracted from the code snippets.
"""

filenames: List[str] = []
for msg in data.get("messages", []):
if msg.get("role", "") == "user":
msgs_content = msg.get("content", [])
for msg_content in msgs_content:
if msg_content.get("type", "") == "text":
extracted_snippets = self._snippet_extractor.extract_unique_snippets(
msg_content.get("text")
)
filenames.extend(extracted_snippets.keys())
return set(filenames)

def extract_unique_filenames(self, data: dict) -> set[str]:
return self._extract_from_user_messages(data)

Expand Down
4 changes: 2 additions & 2 deletions src/codegate/muxing/router.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def _get_model_route(
return model_route
except Exception as e:
logger.error(f"Error getting active workspace muxes: {e}")
raise HTTPException(str(e), status_code=404)
raise HTTPException(detail=str(e), status_code=404)

def _setup_routes(self):

Expand Down Expand Up @@ -85,7 +85,7 @@ async def route_to_dest_provider(
model_route = await self._get_model_route(thing_to_match)
if not model_route:
raise HTTPException(
"No matching rule found for the active workspace", status_code=404
detail="No matching rule found for the active workspace", status_code=404
)

logger.info(
Expand Down
58 changes: 57 additions & 1 deletion tests/extract_snippets/test_body_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from codegate.extract_snippets.body_extractor import (
ClineBodySnippetExtractor,
ContinueBodySnippetExtractor,
OpenInterpreterBodySnippetExtractor,
)

Expand Down Expand Up @@ -75,9 +76,13 @@ def test_body_extract_openinterpreter_snippets(test_case: BodyCodeSnippetTest):
BodyCodeSnippetTest(
input_body_dict={
"messages": [
{"role": "system", "content": "You are Cline, a highly skilled software"},
{
"role": "user",
"content": '''
"content": [
{
"type": "text",
"text": '''
[<task>
now please analyze the folder 'codegate/src/codegate/api/' (see below for folder content)
</task>
Expand Down Expand Up @@ -145,6 +150,8 @@ async def _process_prompt_output_to_partial_qa(
</file_content>
</folder_content>
''',
}
],
},
]
},
Expand All @@ -157,3 +164,52 @@ def test_body_extract_cline_snippets(test_case: BodyCodeSnippetTest):
extractor = ClineBodySnippetExtractor()
filenames = extractor.extract_unique_filenames(test_case.input_body_dict)
_evaluate_actual_filenames(filenames, test_case)


@pytest.mark.parametrize(
"test_case",
[
# Analyze processed snippets from OpenInterpreter
BodyCodeSnippetTest(
input_body_dict={
"messages": [
{
"role": "user",
"content": """
```file:///Users/user/StacklokRepos/testing_file.py
import invokehttp
import fastapi
from fastapi import FastAPI, Request, Response, HTTPException
import numpy

GITHUB_TOKEN="ghp_1J9Z3Z2dfg4dfs23dsfsdf232aadfasdfasfasdf32"

def add(a, b):
return a + b

def multiply(a, b):
return a * b



def substract(a, b):

```

please analyze testing_file.py
""",
}
],
"model": "foo-model-replaced-by-mux",
"max_tokens": 4096,
"stream": True,
},
expected_count=1,
expected=["testing_file.py"],
),
],
)
def test_body_extract_continue_snippets(test_case: BodyCodeSnippetTest):
extractor = ContinueBodySnippetExtractor()
filenames = extractor.extract_unique_filenames(test_case.input_body_dict)
_evaluate_actual_filenames(filenames, test_case)