Skip to content

Commit

Permalink
bug fix
Browse files Browse the repository at this point in the history
  • Loading branch information
shuxiao9058 committed Jul 21, 2023
1 parent d5087ee commit b00b323
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 66 deletions.
65 changes: 3 additions & 62 deletions tabnine-chat-curl.el
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ PROCESS and _STATUS are process parameters."
(error-stack (plist-get response :stack)))
(message "TabNine Chat error: (%s) %s" http-msg error-msg)
(setq http-msg (concat "(" http-msg ") " (string-trim error-stack)))))
((eq response 'json-read-error)
((eq response 'json-error)
(message "TabNine Chat error (%s): Malformed JSON in response." http-msg))
(t (message "TabNine Chat error (%s): Could not parse HTTP response." http-msg)))))
(with-current-buffer tabnine-chat-buffer
Expand Down Expand Up @@ -266,11 +266,10 @@ PROCESS is the process under watch, OUTPUT is the output received."
#'tabnine-chat-curl--stream-insert-response)
(let* ((content-strs))
(condition-case nil
(while (not (eobp))
(while (re-search-forward "^\{" nil t)
(when-let* ((line-content (decode-coding-string
(buffer-substring-no-properties
(save-excursion
(beginning-of-line) (point))
(line-beginning-position)
(save-excursion
(end-of-line) (point)))
'utf-8) )
Expand Down Expand Up @@ -303,64 +302,6 @@ PROCESS and _STATUS are process parameters."
(setf (alist-get process tabnine-chat-curl--process-alist nil 'remove) nil)
(kill-buffer proc-buf)))

(defun tabnine-chat-curl--parse-response (buf token)
"Parse the buffer BUF with curl's response.
TOKEN is used to disambiguate multiple requests in a single
buffer."
(with-current-buffer buf
(progn
;; (if (search-backward token nil t)
;; (search-forward ")" nil t)
;; (goto-char (point-min)))
(goto-char (point-min))
(if-let* ((http-msg (progn (goto-char (point-min))
(while (looking-at "^HTTP/[.0-9]+ +[0-9]+ Connection established")
(forward-line 2))
(string-trim
(buffer-substring
(line-beginning-position)
(line-end-position)))))
(body (let ((start)
(end))
(setq end (save-excursion
(goto-char (point-max))
(search-backward token nil t)
(backward-char)
(point)))
(goto-char (point-min))
(if (re-search-forward "^\{" nil t)
(setq start (save-excursion (beginning-of-line) (point)))
(setq start (progn (forward-paragraph) (point))))
(decode-coding-string
(buffer-substring-no-properties start end) 'utf-8)))
(http-status
(save-match-data
(and (string-match "HTTP/[.0-9]+ +\\([0-9]+\\)" http-msg)
(match-string 1 http-msg)))))
(cond
((equal http-status "404");; token expired
(message "TabNine token is expired, set tabnine--access-token to nil.")
(setq tabnine--access-token nil))
((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)))
(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))
;; (error-type (plist-get error-plist :type)))
;; (list nil (concat "(" http-msg ") " (string-trim error-type)) error-msg)))
;; ((eq response 'json-read-error)
;; (list nil (concat "(" http-msg ") Malformed JSON in response.")
;; "Malformed JSON in response"))
(t (list nil (concat "(" http-msg ") Could not parse HTTP response.")
"Could not parse HTTP response.")))
;; (list nil (concat "(" http-msg ") Could not parse HTTP response.")
;; "Could not parse HTTP response.")
))))

(provide 'tabnine-chat-curl)

;;; tabnine-chat-curl.el ends here
11 changes: 8 additions & 3 deletions tabnine-chat.el
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ Default 1MB."
(fix-code . "Find errors in the selected code and fix them"))
"A list of cons cells that map method to TabNine Chat prompt string.")

(defun tabnine-chat--get-conversion-id()
(defun tabnine-chat--conversion-id()
"Get conversion ID."
(unless tabnine-chat--conversation-id
(setq tabnine-chat--conversation-id (tabnine-util--random-uuid)))
Expand Down Expand Up @@ -248,7 +248,7 @@ Method can be explain-code, document-code, generate-test-for-code or fix-code."
:by "user"))
(contexts (tabnine-chat--cached-contexts context editor-context)))
(list
:conversationId (tabnine-chat--get-conversion-id)
:conversationId (tabnine-chat--conversion-id)
:messageId (tabnine-util--random-uuid)
:input contexts
:isTelemetryEnabled :json-false))))
Expand Down Expand Up @@ -486,7 +486,12 @@ Return body, http-status, http-msg and error in list."
((equal http-status "404");; token expired
(message "TabNine token is expired, set tabnine--access-token to nil.")
(setq tabnine--access-token nil))
(t (unless (looking-at "^HTTP/[.0-9]+ +[0-9]+ Connection established")
(t (unless (progn (goto-char (point-min))
(when (looking-at "^HTTP/[.0-9]+ +[0-9]+ Connection established")
(string-trim
(buffer-substring
(line-beginning-position)
(line-end-position)))))
(message "Unknow error: %s, buffer text: %s" http-msg (buffer-string)))
(list body http-status http-msg "unknow error")))))))

Expand Down
3 changes: 2 additions & 1 deletion tabnine-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ Example of a UUID: 1df63142-a513-c850-31a3-535fc3520c3d."
(json-object-type 'plist)
(json-false nil))
(condition-case nil (json-read-from-string ,str)
(json-readtable-error 'json-read-error)))))
(json-readtable-error 'json-error)
(json-error 'json-error)))))

(defun tabnine-util--get-list-errors()
"Get current buffer error list."
Expand Down

0 comments on commit b00b323

Please sign in to comment.