Skip to content

Commit

Permalink
Optimize filter and sort candidates
Browse files Browse the repository at this point in the history
  • Loading branch information
shuxiao9058 committed Nov 12, 2024
1 parent c725eed commit 4ca9d35
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 41 deletions.
4 changes: 2 additions & 2 deletions tabnine-chat-curl.el
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ INFO is a plist with the following keys:
`(("Content-Type" . "application/json; charset=utf-8")
("Authorization" . ,(concat "Bearer " (tabnine--access-token)))))
(request (tabnine-chat--make-request info))
(url-request-data (tabnine-util--json-serialize request))
(data-json (encode-coding-string (tabnine-util--json-serialize request) 'utf-8))
(url-str (format "%s/chat/v1/generate_chat_response_async" tabnine-api-server))
(curl-args (append
(list "--location" "--silent" "--compressed" "--disable"
(format "-X%s" "POST")
(format "-m%s" 60)
(format "-d%s" url-request-data))
(format "-d%s" data-json))
(when (and tabnine-network-proxy (stringp tabnine-network-proxy)
(not (string-empty-p tabnine-network-proxy)))
(list "--insecure" "--proxy" tabnine-network-proxy))
Expand Down
12 changes: 5 additions & 7 deletions tabnine-chat.el
Original file line number Diff line number Diff line change
Expand Up @@ -366,19 +366,17 @@ Should be messageContext's item or userContext' value."
(defun tabnine-chat--code-around-by-line(line-count before)
"TabNine Chat get code around BEFORE or after with LINE-COUNT."
(save-excursion
(let* ((curr-line (line-number-at-pos))
(lines (count-lines (point-min) (point-max)))
(point-min)
(let* ((point-min)
(point-max))
(save-excursion
(if before
(forward-line (max 0 (- curr-line line-count)))
(forward-line (min lines (+ curr-line 1))))
(forward-line (- line-count))
(forward-line 1))
(setq point-min (line-beginning-position)))
(save-excursion
(if before
(forward-line (max 0 (- curr-line 1)))
(forward-line (min lines (+ curr-line line-count))))
(forward-line -1)
(forward-line line-count))
(setq point-max (line-end-position)))
(decode-coding-string
(buffer-substring-no-properties point-min point-max)
Expand Down
56 changes: 29 additions & 27 deletions tabnine-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -690,15 +690,15 @@ PROCESS is the process under watch, EVENT is the event occurred."
"Filter duplicates and bad COMPLETIONS result."
(when completions
(when-let* ((completions (cl-remove-duplicates completions
:key (lambda (x) (plist-get x :new_prefix))
:test #'s-equals-p))
(completions (cl-remove-duplicates completions
:key (lambda (x) (let ((new_prefix (plist-get x :new_prefix))
(new_posfix (plist-get x :new_posfix)))
(s-trim (concat new_prefix (or new_posfix "")))))
:test #'s-equals-p))
(completions (cl-remove-if #'tabnine--invalid-completion-p
completions)))
:key (lambda (x) (plist-get x :new_prefix))
:test #'s-equals-p))
(completions (cl-remove-duplicates completions
:key (lambda (x) (let ((new_prefix (plist-get x :new_prefix))
(new_posfix (plist-get x :new_posfix)))
(s-trim (concat new_prefix (or new_posfix "")))))
:test #'s-equals-p))
(completions (cl-remove-if #'tabnine--invalid-completion-p
completions)))
completions)))

(defun tabnine--process-filter (_ output)
Expand All @@ -721,11 +721,23 @@ PROCESS is the process under watch, OUTPUT is the output received."
(when (s-present? str)
(setq result (tabnine-util--read-json str))
(unless (equal result 'json-error)
(setq tabnine--response result)
(when (and result (tabnine--valid-response-p result))
(setq tabnine--completion-cache-result result)
(when-let* ((completions (tabnine--filter-completions (plist-get result :results))))
(setq completions (cl-sort completions
(lambda(a b)
(let* ((get-detail-number-fn
(lambda(x)
(when-let* ((md (plist-get x :completion_metadata))
(detail (plist-get md :detail)))
(string-to-number (s-trim detail)))))
(detail-a (funcall get-detail-number-fn a))
(detail-b (funcall get-detail-number-fn b)))
(> detail-a detail-b)))))
(plist-put result :results completions))
(setq tabnine--completion-cache-result result)
(when (and tabnine-mode (equal (point) tabnine--trigger-point))
(tabnine--show-completion-1 result)))))
(tabnine--show-completion-1 result)))
(setq tabnine--response result)))
(setq ss (cdr ss)))))

;;
Expand Down Expand Up @@ -800,8 +812,7 @@ PROCESS is the process under watch, OUTPUT is the output received."
(let* ((cache tabnine--completion-cache)
(correlation_id (plist-get result :correlation_id))
(old_prefix (plist-get cache :old_prefix))
(completions (plist-get cache :results))
(completions (tabnine--filter-completions completions)))
(completions (plist-get cache :results)))
(cond ((seq-empty-p completions)
(message "No completion is available."))
((= (length completions) 1)
Expand Down Expand Up @@ -961,7 +972,7 @@ Use TRANSFORM-FN to transform completion if provided."
(defun tabnine--show-completion-1 (response)
"Show completion result after first get RESPONSE."
(when-let* ((result response)
(completions (tabnine--filter-completions (plist-get result :results)))
(completions (plist-get result :results))
(completion (if (seq-empty-p completions) nil (seq-elt completions 0)))
(old_prefix (plist-get result :old_prefix))
(new_prefix (plist-get completion :new_prefix))
Expand Down Expand Up @@ -1244,19 +1255,10 @@ command that triggered `post-command-hook'."
"Get candidates for RESPONSE."
(when (tabnine--response-display-with-capf-p response)
(let* ((result response)
(completions (tabnine--filter-completions (plist-get result :results)))
(candidates (tabnine--construct-candidates
(completions (plist-get result :results)))
(tabnine--construct-candidates
completions
#'tabnine--construct-candidate-generic)))
(cl-sort candidates
(lambda(a b)
(let* ((get-candidate-detail-number-fn
(lambda(x)
(let ((detail (get-text-property 0 'detail x)))
(string-to-number (s-trim detail)))))
(detail-a (funcall get-candidate-detail-number-fn a))
(detail-b (funcall get-candidate-detail-number-fn b)))
(> detail-a detail-b)))))))
#'tabnine--construct-candidate-generic))))

(defun tabnine--post-completion (candidate)
"Replace old suffix with new suffix for CANDIDATE."
Expand Down
9 changes: 4 additions & 5 deletions tabnine-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,11 @@ Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d."
(if (progn
(require 'json)
(fboundp 'json-serialize))
`(encode-coding-string
(json-serialize ,params
:null-object nil
:false-object :json-false) 'utf-8)
`(json-serialize ,params
:null-object nil
:false-object :json-false)
`(let ((json-false :json-false))
(encode-coding-string (json-encode ,params) 'utf-8))))
(json-encode ,params))))

(defmacro tabnine-util--read-json (str)
"Read JSON string STR. and return the decoded object."
Expand Down

0 comments on commit 4ca9d35

Please sign in to comment.