TabbyML / tabby

Self-hosted AI coding assistant
https://tabbyml.com
Other
22.03k stars 1.02k forks source link

tabby-agent doesn't respect the client's insertTextMode for regular textDocument/completion #3330

Open jwortmann opened 1 month ago

jwortmann commented 1 month ago

I want to report what I believe is a bug in tabby-agent.

tabby was started via tabby serve --model StarCoder-1B --no-webserver

For testing a LSP client integration, I enabled the regular textDocument/completion request for now. The client declares in initialze that it only supports InsertTextMode.adjustIndentation, which means that

/**
* The editor adjusts leading whitespace of new lines so that
* they match the indentation up to the cursor of the line for
* which the item is accepted.
*/

Using the following Python file

# find max element in array
def findMaxElement(arr):

and requesting textDocument/completion at the cursor position shown in the following screenshot, the textEdit of the completion item in the response contains extra (incorrect) indentation; see the JSON payload below.

tabby1

{
    "isIncomplete": true,
    "items": [
        {
            "label": "maxElement = arr[0]\n",
            "labelDetails": {
                "detail": "    for i in range(1, len(arr)):\n",
                "description": "Tabby"
            },
            "kind": 1,
            "documentation": {
                "kind": "markdown",
                "value": "```\n    maxElement = arr[0]\n    for i in range(1, len(arr)):\n        if arr[i] > maxElement:\n            maxElement = arr[i]\n    return maxElement\n```\n ---\nSuggested by Tabby."
            },
            "textEdit": {
                "newText": "maxElement = arr[0]\n    for i in range(1, len(arr)):\n        if arr[i] > maxElement:\n            maxElement = arr[i]\n    return maxElement",
                "range": {
                    "start": {
                        "line": 2,
                        "character": 4
                    },
                    "end": {
                        "line": 2,
                        "character": 4
                    }
                }
            },
            "data": {
                "eventId": {
                    "completionId": "cmpl-e6481dd3-5fc3-4b60-8c43-aefe5d2b8fd1",
                    "choiceIndex": 0
                }
            }
        }
    ]
}

This results in wrong indentation when the completion item gets accepted:

tabby2

The right thing to do for tabby-agent would be to remove leading indentation from the newText as if the code block (cursor position) had no initial indentation.

Note that this is only relevant for textDocument/completion but not for textDocument/inlineCompletion, because afaik indentation in inline completions should always be handled as InsertTextMode.asIs.

icycodes commented 1 month ago

Thank you for reporting this bug and proposing the detail solution.
Indeed, the tabby-agent does not fully respect all client capabilities regarding the method textDocument/completion, including InsertMode. I will fix it in the next tabby-agent minor release.