Skip to content

Fix code block errors for code completion when code-gecko is used

Alexander Chueshev requested to merge ac/fix-end-block-errors into main

What does this merge request do and why?

This MR provides a slightly improved version of the already implemented algorithm to trim code blocks when code completion mode is activated.

Motivation: The suggested code often repeats closing delimiters such as ], }, or ". This leads to syntax errors and slows down the developer, making them less likely to trust or accept the suggestion.

Improvement: the algorithm tries (max 4 attempts, by default) to reduce errors by removing right-hand characters step-by-step from the completion. The user prefix may already contain an error, so truncate completion only if this process will reduce errors to the original state. Use cases - !467 (diffs). The original algorithm always looks to reduce errors to zero.

Supported models: code-gecko.

Ref: gitlab-org/editor-extensions/gitlab-lsp#62
Closes #344 (closed)

How to set up and validate locally

curl -X 'POST' \
  'http://localhost:5052/v2/completions' \
  -H 'accept: application/json' \
  -H 'Content-Type: application/json' \
  -d '{
  "project_path": "string",
  "project_id": 0,
  "current_file": {
    "file_name": "app.go",
    "language_identifier": "golang",
    "content_above_cursor": "func (h* HotelHandler) HandleGetHotels(c *fiber.Ctx) error {\n\thotels, err := h.Store.Hotels.GetHotels(c.Context(), nil)\n\tif err != nil {\n\t\treturn err\n\t}\n\treturn",
    "content_below_cursor": "}"
  },
  "model_provider": "vertex-ai",
  "telemetry": [],
  "prompt_version": 1
}'
{
  "id": "id",
  "model": {
    "engine": "vertex-ai",
    "name": "code-gecko@latest",
    "lang": "go"
  },
  "experiments": [
    {
      "name": "exp_truncate_suffix",
      "variant": 0
    }
  ],
  "object": "text_completion",
  "created": 1700492139,
  "choices": [
    {
      "text": " c.JSON(hotels)\n",
      "index": 0,
      "finish_reason": "length"
    }
  ]
}

Merge request checklist

  • Tests added for new functionality. If not, please raise an issue to follow up.
  • Documentation added/updated, if needed.
Edited by Alexander Chueshev

Merge request reports