diff --git a/kak/kakrc b/kak/kakrc index 1214511..f314927 100644 --- a/kak/kakrc +++ b/kak/kakrc @@ -59,3 +59,11 @@ lsp-enable lsp-inlay-diagnostics-enable global + +declare-option str fim_tmp "/tmp/kak-fim.buf" + +define-command fim-complete -docstring "FIM-Completion am Cursor (Mistral)" %{ + write -force %opt{fim_tmp} + execute-keys "!python3 %val{config}/mistral-fim.py %opt{fim_tmp} %val{cursor_line} %val{cursor_column}" +} +map global user f ': fim-complete' -docstring 'FIM complete' diff --git a/kak/mistral-fim.py b/kak/mistral-fim.py new file mode 100644 index 0000000..171c019 --- /dev/null +++ b/kak/mistral-fim.py @@ -0,0 +1,29 @@ +#!/usr/bin/env python3 +import json, os, sys, urllib.request + +buf, line, col = sys.argv[1], int(sys.argv[2]), int(sys.argv[3]) +CTX_BEFORE, CTX_AFTER = 120, 60 # Kontext-Budget in Zeilen + +lines = open(buf, encoding="utf-8").read().split("\n") +before, cur, after = lines[:line-1], (lines[line-1] if line-1 < len(lines) else ""), lines[line:] + +prefix = "\n".join(before + [cur[:col-1]]) +suffix = "\n".join([cur[col-1:]] + after) +prefix = "\n".join(prefix.split("\n")[-CTX_BEFORE:]) # nach hinten kappen +suffix = "\n".join(suffix.split("\n")[:CTX_AFTER]) + +body = json.dumps({ + "model": "codestral-latest", + "prompt": prefix, "suffix": suffix, + "max_tokens": 96, "temperature": 0.2, +}).encode() + +req = urllib.request.Request( + "https://api.mistral.ai/v1/fim/completions", data=body, + headers={"Authorization": "Bearer " + os.environ["MISTRAL_API_KEY"], + "Content-Type": "application/json"}) +try: + with urllib.request.urlopen(req, timeout=20) as r: + sys.stdout.write(json.load(r)["choices"][0]["message"]["content"]) +except Exception: + pass # fail silent → leeres Insert