Skip to content

Commit 4d7ee39

Browse files
committed
gptel--display: fold for markdown
1 parent 5fad6f1 commit 4d7ee39

File tree

1 file changed

+72
-25
lines changed

1 file changed

+72
-25
lines changed

gptel.el

Lines changed: 72 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@
210210
(require 'map)
211211
(require 'text-property-search)
212212
(require 'cl-generic)
213+
(require 'mule-util)
213214
(require 'gptel-openai)
214215

215216

@@ -2552,6 +2553,9 @@ specified."
25522553
(error
25532554
(user-error "Can not resume request: could not read data from buffer!")))))
25542555

2556+
(defconst gptel-display-max 128
2557+
"Maximum number of chars to be displayed for tool, reason ...")
2558+
25552559
(defun gptel--insert-response (response info &optional raw)
25562560
"Insert the LLM RESPONSE into the gptel buffer.
25572561
@@ -2613,12 +2617,19 @@ Optional RAW disables text properties and transformation."
26132617
(gptel--insert-response (concat separator (car blocks)) info t)
26142618
(gptel--insert-response text info)
26152619
(gptel--insert-response (cdr blocks) info t))
2616-
(when (derived-mode-p 'org-mode) ;fold block
2617-
(save-excursion
2618-
(goto-char (plist-get info :tracking-marker))
2619-
(search-backward "#+end_reasoning" start-marker t)
2620-
(when (looking-at "^#\\+end_reasoning")
2621-
(org-cycle)))))))))
2620+
(save-excursion
2621+
(goto-char (plist-get info :tracking-marker))
2622+
(if (derived-mode-p 'org-mode) ;fold block
2623+
(progn
2624+
(search-backward "#+end_reasoning" start-marker t)
2625+
(when (looking-at "^#\\+end_reasoning")
2626+
(org-cycle)))
2627+
(search-backward "```" start-marker t)
2628+
(when (looking-at "^```$")
2629+
(gptel--hs-fold
2630+
(truncate-string-to-width
2631+
text gptel-display-max nil nil t)))
2632+
)))))))
26222633
(`(tool-call . ,tool-calls)
26232634
(gptel--display-tool-calls tool-calls info))
26242635
(`(tool-result . ,tool-results)
@@ -3015,13 +3026,20 @@ for streaming responses only."
30153026
(propertize "\n```" 'gptel 'ignore))
30163027
gptel-response-separator)
30173028
info t)
3018-
(when (derived-mode-p 'org-mode) ;fold block
3019-
(ignore-errors
3020-
(save-excursion
3021-
(goto-char tracking-marker)
3022-
(search-backward "#+end_reasoning" start-marker t)
3023-
(when (looking-at "^#\\+end_reasoning")
3024-
(org-cycle))))))
3029+
(ignore-errors
3030+
(save-excursion
3031+
(goto-char tracking-marker)
3032+
(if (derived-mode-p 'org-mode) ;fold block
3033+
(progn
3034+
(search-backward "#+end_reasoning" start-marker t)
3035+
(when (looking-at "^#\\+end_reasoning")
3036+
(org-cycle)))
3037+
(search-backward "```" start-marker t)
3038+
(when (looking-at "^```$")
3039+
(gptel--hs-fold
3040+
(truncate-string-to-width
3041+
text gptel-display-max nil nil t)))
3042+
))))
30253043
(unless (and reasoning-marker tracking-marker
30263044
(= reasoning-marker tracking-marker))
30273045
(let ((separator ;Separate from response prefix if required
@@ -3197,22 +3215,25 @@ for tool call results. INFO contains the state of the request."
31973215
separator
31983216
"#+begin_tool "
31993217
truncated-call
3218+
"\n"
32003219
(propertize
3201-
(concat "\n" call "\n\n" (org-escape-code-in-string result))
3220+
(concat call "\n\n" (org-escape-code-in-string result))
32023221
'gptel `(tool . ,id))
3203-
"\n#+end_tool\n")
3222+
"\n#+end_tool"
3223+
gptel-response-separator)
32043224
;; TODO(tool) else branch is handling all front-ends as markdown.
32053225
;; At least escape markdown.
32063226
(concat
32073227
separator
32083228
;; TODO(tool) remove properties and strip instead of ignoring
3209-
(propertize (format "``` tool %s" truncated-call) 'gptel 'ignore)
3229+
(propertize (format "``` tool %s\n" truncated-call) 'gptel 'ignore)
32103230
(propertize
32113231
;; TODO(tool) escape markdown in result
3212-
(concat "\n" call "\n\n" result)
3232+
(concat call "\n\n" result)
32133233
'gptel `(tool . ,id))
32143234
;; TODO(tool) remove properties and strip instead of ignoring
3215-
(propertize "\n```\n" 'gptel 'ignore))))
3235+
(propertize "\n```" 'gptel 'ignore)
3236+
gptel-response-separator)))
32163237
info
32173238
'raw)
32183239
;; tool-result insertion has updated the tracking marker
@@ -3222,13 +3243,39 @@ for tool call results. INFO contains the state of the request."
32223243
(move-marker tool-marker tracking-marker)
32233244
(setq tool-marker (copy-marker tracking-marker nil))
32243245
(plist-put info :tool-marker tool-marker))
3225-
(when (derived-mode-p 'org-mode) ;fold drawer
3226-
(ignore-errors
3227-
(save-excursion
3228-
(goto-char tracking-marker)
3229-
(forward-line -1)
3230-
(when (looking-at "^#\\+end_tool")
3231-
(org-cycle))))))))))
3246+
(ignore-errors
3247+
(save-excursion
3248+
(goto-char tracking-marker)
3249+
(if (derived-mode-p 'org-mode) ;fold drawer
3250+
(progn
3251+
(search-backward "#+end_tool" start-marker t)
3252+
(when (looking-at "^#\\+end_tool")
3253+
(org-cycle)))
3254+
(search-backward "```" start-marker t)
3255+
(when (looking-at "^```$")
3256+
(gptel--hs-fold
3257+
(truncate-string-to-width
3258+
result gptel-display-max nil nil t)))
3259+
))))))))
3260+
3261+
(defun gptel--hs-fold (&optional disp)
3262+
"Fold the current 'gptel text section and display DISP instead."
3263+
(interactive)
3264+
(let (beg end ov)
3265+
;; end of range, should be at \n now
3266+
(setq end (if (eq (get-text-property (point) 'gptel) 'ignore)
3267+
(previous-single-property-change
3268+
(point) 'gptel nil (point-min))
3269+
(next-single-property-change
3270+
(point) 'gptel nil (point-max))))
3271+
(setq beg (previous-single-property-change
3272+
end 'gptel nil (point-min)))
3273+
(setq ov (make-overlay beg end))
3274+
(overlay-put ov 'invisible t)
3275+
(overlay-put ov 'hs 'region)
3276+
(overlay-put ov 'hs-b-offset 0)
3277+
(overlay-put ov 'display (or disp (truncate-string-ellipsis)))
3278+
(overlay-put ov 'evaporate t)))
32323279

32333280
(defun gptel--format-tool-call (name arg-values)
32343281
"Format a tool call for display in the buffer.

0 commit comments

Comments
 (0)