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

Commit 4fe3b44

Browse files
committed
fix problems with codegate version and cline detection
fix lint
1 parent 2b19368 commit 4fe3b44

File tree

8 files changed

+35
-20
lines changed

8 files changed

+35
-20
lines changed

src/codegate/pipeline/cli/cli.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import re
12
import shlex
23

34
from litellm import ChatCompletionRequest
@@ -76,12 +77,19 @@ async def process(
7677

7778
if last_user_message is not None:
7879
last_user_message_str, _ = last_user_message
79-
splitted_message = last_user_message_str.lower().split(" ")
80+
cleaned_message_str = re.sub(r"<.*?>", "", last_user_message_str).strip()
81+
splitted_message = cleaned_message_str.lower().split(" ")
8082
# We expect codegate as the first word in the message
8183
if splitted_message[0] == "codegate":
8284
context.shortcut_response = True
83-
args = shlex.split(last_user_message_str)
85+
args = shlex.split(cleaned_message_str)
8486
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"
92+
)
8593
return PipelineResult(
8694
response=PipelineResponse(
8795
step_name=self.name,

src/codegate/providers/anthropic/completion_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ async def execute_completion(
1616
api_key: Optional[str],
1717
stream: bool = False,
1818
is_fim_request: bool = False,
19+
base_tool: Optional[str] = "",
1920
) -> Union[ModelResponse, AsyncIterator[ModelResponse]]:
2021
"""
2122
Ensures the model name is prefixed with 'anthropic/' to explicitly route to Anthropic's API.

src/codegate/providers/base.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,19 @@ async def complete(
233233
# Execute the completion and translate the response
234234
# This gives us either a single response or a stream of responses
235235
# based on the streaming flag
236+
is_cline_client = any(
237+
"Cline" in str(message.get("content", "")) for message in data.get("messages", [])
238+
)
239+
base_tool = ""
240+
if is_cline_client:
241+
base_tool = "cline"
242+
236243
model_response = await self._completion_handler.execute_completion(
237-
provider_request, api_key=api_key, stream=streaming, is_fim_request=is_fim_request # type: ignore
244+
provider_request,
245+
api_key=api_key,
246+
stream=streaming,
247+
is_fim_request=is_fim_request,
248+
base_tool=base_tool,
238249
)
239250
if not streaming:
240251
normalized_response = self._output_normalizer.normalize(model_response)

src/codegate/providers/completion/base.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ async def execute_completion(
2020
api_key: Optional[str],
2121
stream: bool = False, # TODO: remove this param?
2222
is_fim_request: bool = False,
23+
is_cline_client: bool = False,
2324
) -> Union[ModelResponse, AsyncIterator[ModelResponse]]:
2425
"""Execute the completion request"""
2526
pass

src/codegate/providers/litellmshim/litellmshim.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ async def execute_completion(
4343
api_key: Optional[str],
4444
stream: bool = False,
4545
is_fim_request: bool = False,
46+
base_tool: Optional[str] = "",
4647
) -> Union[ModelResponse, AsyncIterator[ModelResponse]]:
4748
"""
4849
Execute the completion request with LiteLLM's API

src/codegate/providers/llamacpp/completion_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ async def execute_completion(
5252
api_key: Optional[str],
5353
stream: bool = False,
5454
is_fim_request: bool = False,
55+
base_tool: Optional[str] = "",
5556
) -> Union[ModelResponse, AsyncIterator[ModelResponse]]:
5657
"""
5758
Execute the completion request with inference engine API

src/codegate/providers/ollama/completion_handler.py

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,16 @@
1212

1313

1414
async def ollama_stream_generator(
15-
stream: AsyncIterator[ChatResponse], is_cline_client: bool
15+
stream: AsyncIterator[ChatResponse], base_tool: str
1616
) -> AsyncIterator[str]:
1717
"""OpenAI-style SSE format"""
1818
try:
1919
async for chunk in stream:
2020
try:
21-
yield f"{chunk.model_dump_json()}\n\n"
2221
# TODO We should wire in the client info so we can respond with
2322
# the correct format and start to handle multiple clients
2423
# in a more robust way.
25-
if not is_cline_client:
24+
if base_tool != "cline":
2625
yield f"{chunk.model_dump_json()}\n"
2726
else:
2827
# First get the raw dict from the chunk
@@ -63,6 +62,9 @@ async def ollama_stream_generator(
6362
for field in optional_fields:
6463
if field in chunk_dict:
6564
response[field] = chunk_dict[field]
65+
66+
print("in cline")
67+
print(json.dumps(response))
6668
yield f"data: {json.dumps(response)}\n"
6769
except Exception as e:
6870
logger.error(f"Error in stream generator: {str(e)}")
@@ -76,26 +78,18 @@ class OllamaShim(BaseCompletionHandler):
7678

7779
def __init__(self, base_url):
7880
self.client = AsyncClient(host=base_url, timeout=300)
79-
self.is_cline_client = False
81+
self.base_tool = ""
8082

8183
async def execute_completion(
8284
self,
8385
request: ChatCompletionRequest,
8486
api_key: Optional[str],
8587
stream: bool = False,
8688
is_fim_request: bool = False,
87-
is_cline_client: bool = False,
89+
base_tool: Optional[str] = "",
8890
) -> Union[ChatResponse, GenerateResponse]:
8991
"""Stream response directly from Ollama API."""
90-
91-
# TODO: I don't like this, but it's a quick fix for now until we start
92-
# passing through the client info so we can respond with the correct
93-
# format.
94-
# Determine if the client is a Cline client
95-
self.is_cline_client = any(
96-
"Cline" in str(message.get("content", "")) for message in request.get("messages", [])
97-
)
98-
92+
self.base_tool = base_tool
9993
if is_fim_request:
10094
prompt = request["messages"][0].get("content", "")
10195
response = await self.client.generate(
@@ -116,7 +110,7 @@ def _create_streaming_response(self, stream: AsyncIterator[ChatResponse]) -> Str
116110
is the format that FastAPI expects for streaming responses.
117111
"""
118112
return StreamingResponse(
119-
ollama_stream_generator(stream, self.is_cline_client),
113+
ollama_stream_generator(stream, self.base_tool or ""),
120114
media_type="application/x-ndjson; charset=utf-8",
121115
headers={
122116
"Cache-Control": "no-cache",

src/codegate/providers/ollama/provider.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,6 @@ async def show_model(request: Request):
8080
@self.router.post(f"/{self.provider_route_name}/v1/generate")
8181
async def create_completion(request: Request):
8282
body = await request.body()
83-
print("i request")
84-
print(body)
8583
data = json.loads(body)
8684
# `base_url` is used in the providers pipeline to do the packages lookup.
8785
# Force it to be the one that comes in the configuration.

0 commit comments

Comments
 (0)