Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore insertion of role token #61

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 22 additions & 25 deletions org-ai-openai.el
Original file line number Diff line number Diff line change
Expand Up @@ -231,32 +231,29 @@ the response into."
(backward-char))

;; insert text
(if-let* ((choices (or (alist-get 'choices response)
(plist-get response 'choices)))
(choice (aref choices 0))
(delta (plist-get choice 'delta)))
(when-let* ((choices (or (alist-get 'choices response)
(plist-get response 'choices)))
(choice (aref choices 0))
(delta (plist-get choice 'delta)))
(when-let ((role (plist-get delta 'role)))
(setq org-ai--current-chat-role role)
(cond
((plist-get delta 'role)
(let ((role (plist-get delta 'role)))
(progn
(setq org-ai--current-chat-role role)
(cond
((string= role "assistant")
(insert "\n[AI]: "))
((string= role "user")
(insert "\n[ME]: "))
((string= role "system")
(insert "\n[SYS]: ")))
(run-hook-with-args 'org-ai-after-chat-insertion-hook 'role role))))
((plist-get delta 'content)
(let ((text (plist-get delta 'content)))
(when (or org-ai--chat-got-first-response (not (string= (string-trim text) "")))
(when (and (not org-ai--chat-got-first-response) (string-prefix-p "```" text))
;; start markdown codeblock responses on their own line
(insert "\n"))
(insert (decode-coding-string text 'utf-8))
(run-hook-with-args 'org-ai-after-chat-insertion-hook 'text text))
(setq org-ai--chat-got-first-response t)))))
((string= role "assistant")
(insert "\n[AI]: "))
((string= role "user")
(insert "\n[ME]: "))
((string= role "system")
(insert "\n[SYS]: ")))
(run-hook-with-args 'org-ai-after-chat-insertion-hook 'role role))

(let ((text (or (plist-get delta 'content) "")))
(when (or org-ai--chat-got-first-response (not (string= (string-trim text) "")))
(when (and (not org-ai--chat-got-first-response) (string-prefix-p "```" text))
;; start markdown codeblock responses on their own line
(insert "\n"))
(insert (decode-coding-string text 'utf-8))
(run-hook-with-args 'org-ai-after-chat-insertion-hook 'text text))
(setq org-ai--chat-got-first-response t)))

(setq org-ai--current-insert-position-marker (point-marker))))))

Expand Down