Skip to content

Commit

Permalink
add tabnine-chat docs
Browse files Browse the repository at this point in the history
  • Loading branch information
shuxiao9058 committed Jul 18, 2023
1 parent 50b166d commit ff1b6aa
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 46 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ An unofficial TabNine(with TabNine Chat supported) package for Emacs.

# Screen Recording

- TabNine complete

![screenshot-3.gif](./assets/screenshot-3.gif)

- TabNine Chat

![screenshot-4.gif](./assets/screenshot-4.gif)

## Screenshot

- Snippets displayed with overlay, screenshot:
Expand Down Expand Up @@ -117,6 +123,8 @@ If candidate icons of tabnine displayed unnormally [capf icon error](https://git

## TabNine Chat

TabNine Chat is still in BETA - to join the BETA - send `Tabnine Pro` email to `support@tabnine.com` to join BETA test.

| Command | Note |
| ----------------------------------- | --------------------------------------------- |
| tabnine-chat-explain-code | Explain the selected code |
Expand Down
Binary file added assets/screenshot-4.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
59 changes: 32 additions & 27 deletions tabnine-chat-curl.el
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,22 @@

(require 'tabnine-core)
(require 'tabnine-chat)
(require 'tabnine-util)

(eval-when-compile
(require 'cl-lib)
(require 'subr-x))
(require 'map)
(require 's)

(defvar tabnine-chat-curl--process-alist nil
"Alist of active TabNine Chat curl requests.")

(defun tabnine-chat-curl--get-args (info)
(defun tabnine-chat-curl--get-args (prompt token)
"Produce list of arguments for calling Curl.
INFO is the operation info."
(let* ((request (tabnine-chat--make-request (plist-get info :prompt)))
PROMPT is the chat prompt to send, TOKEN is a unique identifier.."
(let* ((request (tabnine-chat--make-request prompt))
(encoded (tabnine-util--json-serialize request))
(data (url-http--encode-string encoded))
(url (format "%s/chat/generate_chat_response" tabnine-api-server))
Expand All @@ -58,15 +60,13 @@ INFO is the operation info."
(append
(list "--location" "--silent" "--compressed" "--disable"
(format "-X%s" "POST")
;; (format "-w(%s . %%{size_header})" token)
(format "-w(%s . %%{size_header})" token)
(format "-m%s" 60)
"-D-"
(format "-d%s" data))
(when (and tabnine-network-proxy (stringp tabnine-network-proxy)
(not (string-empty-p tabnine-network-proxy)))
(list "--proxy" tabnine-network-proxy
"--proxy-negotiate"
"--proxy-user" ":"))
(list "--proxy" tabnine-network-proxy))
(cl-loop for (key . val) in headers
collect (format "-H%s: %s" key val))
(list url))))
Expand All @@ -87,7 +87,7 @@ the response is inserted into the current buffer after point."
(let* ((token (md5 (format "%s%s%s%s"
(random) (emacs-pid) (user-full-name)
(recent-keys))))
(args (tabnine-chat-curl--get-args info))
(args (tabnine-chat-curl--get-args (plist-get info :prompt) token))
(process (apply #'start-process "tabnine-chat-curl"
(generate-new-buffer tabnine-chat--curl-buffer-name) "curl" args)))
(with-current-buffer (process-buffer process)
Expand Down Expand Up @@ -227,12 +227,14 @@ PROCESS is the process under watch, OUTPUT is the output received."
(save-excursion
(goto-char (point-min))
(when-let* (((not (= (line-end-position) (point-max))))
(http-msg (buffer-substring (line-beginning-position)
(line-end-position)))
(http-msg (progn
(goto-char (point-min))
(buffer-substring (line-beginning-position)
(line-end-position))))
(http-status
(save-match-data
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
(match-string 1 http-msg)))))
(save-match-data
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
(match-string 1 http-msg)))))
(plist-put proc-info :http-status http-status)
(plist-put proc-info :status (string-trim http-msg))))
;; Handle read-only TabNine Chat buffer
Expand Down Expand Up @@ -309,23 +311,26 @@ buffer."
;; (goto-char (point-min)))
(goto-char (point-min))

(if-let* ((http-msg (string-trim
(buffer-substring (line-beginning-position)
(line-end-position))))
(if-let* ((http-msg (progn (goto-char (point-min))
(string-trim
(buffer-substring
(line-beginning-position)
(line-end-position)))))
(body (progn (goto-char (point-min))
(forward-paragraph)
(decode-coding-string
(buffer-substring-no-properties (point) (point-max))
'utf-8)))
(http-status
(save-match-data
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
(match-string 1 http-msg))))
(json-object-type 'plist)
(response (progn (goto-char header-size)
(condition-case nil
(json-read)
(json-readtable-error 'json-read-error)))))
(save-match-data
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
(match-string 1 http-msg)))))
(cond
((equal http-status "200")
(list (string-trim
(map-nested-elt response '(:choices 0 :message :content)))
http-msg))
(let* ((ss (s-split "\n" (s-trim body)))
(ss (cl-remove-if (lambda(x) (not (s-present? x))) ss))
(json-ss (mapcar (lambda(x) (tabnine-util--read-json x)) ss)))
(list (tabnine-chat--results-to-text json-ss) http-msg)))
((plist-get response :error)
(let* ((error-plist (plist-get response :error))
(error-msg (plist-get error-plist :message))
Expand Down
28 changes: 19 additions & 9 deletions tabnine-chat.el
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,16 @@
;; Dependencies
;;

(require 'url)
(require 'url-http)
(require 'tabnine-core)
(require 'tabnine-util)

(eval-when-compile
(require 'cl-lib))

(require 'url)
(require 'url-http)
(require 's)

;; (declare-function tabnine-chat-menu "tabnine-chat-transient")
(declare-function tabnine-util--path-to-uri "tabnine-util")
(declare-function tabnine-util--language-id-buffer "tabnine-util")
Expand Down Expand Up @@ -267,7 +272,9 @@ the response is inserted into the current buffer after point."
:position (with-current-buffer buffer
(tabnine-chat--update-header-line " Waiting..." 'warning)
(goto-char (point-max))
(insert (format "%s%s\t\r\n" (tabnine-chat-prompt-prefix-string) (alist-get method tabnine-chat-prompts-alist)))
(skip-chars-backward "\t\r\n")
(insert (alist-get method tabnine-chat-prompts-alist))
(goto-char (point-max))
(point-marker)))))
(funcall
(if tabnine-chat-use-curl
Expand All @@ -294,12 +301,15 @@ the response is inserted into the current buffer after point."
(forward-paragraph)
(decode-coding-string
(buffer-substring-no-properties (point) (point-max))
'utf-8))))
'utf-8)))
(http-status (save-match-data
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
(match-string 1 http-msg)))))
(cond
((string-match-p "404 Not Found" http-msg);; token expired
((equal http-status "404");; token expired
(message "TabNine token is expired, set tabnine--access-token to nil.")
(setq tabnine--access-token nil))
((string-match-p "200 OK" http-msg)
((equal http-status "200")
(let* ((ss (s-split "\n" (s-trim body)))
(ss (cl-remove-if (lambda(x) (not (s-present? x))) ss))
(json-ss (mapcar (lambda(x) (tabnine-util--read-json x)) ss)))
Expand Down Expand Up @@ -496,17 +506,17 @@ text stream."
;;

(defun tabnine-chat-explain-code()
"TabNine chat explain code."
"Explain the selected code."
(interactive)
(tabnine-chat--request 'explain-code))

(defun tabnine-chat-generate-test-for-code()
"TabNine chat generate test for code."
"Write test for the selected code."
(interactive)
(tabnine-chat--request 'generate-test-for-code))

(defun tabnine-chat-document-code()
"TabNine chat write document code."
"Add documentation for the selected code."
(interactive)
(tabnine-chat--request 'document-code))

Expand Down
10 changes: 0 additions & 10 deletions tabnine-core.el
Original file line number Diff line number Diff line change
Expand Up @@ -163,10 +163,6 @@ e.g.: http://user:password@127.0.0.1:7890"
:group 'tabnine
:type 'string)

;;
;; Faces
;;

;;
;; Variables
;;
Expand Down Expand Up @@ -537,10 +533,6 @@ REQUEST should be JSON-serializable object."
(interactive)
(tabnine--request 'configuration))

;;
;; Major mode definition
;;

;;
;; Auto completion
;;
Expand Down Expand Up @@ -752,7 +744,6 @@ PROCESS is the process under watch, OUTPUT is the output received."
(when (tabnine--overlay-visible-p)
(tabnine--get-completions-cycling (tabnine--cycle-completion -1))))


;;
;; UI
;;
Expand Down Expand Up @@ -969,7 +960,6 @@ TabNine will not be triggered if any predicate returns t."
:type '(repeat function)
:group 'tabnine)


(defcustom tabnine-enable-predicates '(evil-insert-state-p tabnine--buffer-changed
tabnine--completion-triggers-p)
"A list of predicate functions with no argument to enable TabNine.
Expand Down

0 comments on commit ff1b6aa

Please sign in to comment.