Skip to content

Commit eff87b2

Browse files
committed
gptel--display: fold for markdown
1 parent fcdbe07 commit eff87b2

File tree

1 file changed

+71
-25
lines changed

1 file changed

+71
-25
lines changed

gptel.el

Lines changed: 71 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2542,6 +2542,9 @@ specified."
25422542
(error
25432543
(user-error "Can not resume request: could not read data from buffer!")))))
25442544

2545+
(defconst gptel-display-max 128
2546+
"Maximum number of chars to be displayed for tool, reason ...")
2547+
25452548
(defun gptel--insert-response (response info &optional raw)
25462549
"Insert the LLM RESPONSE into the gptel buffer.
25472550
@@ -2603,12 +2606,19 @@ Optional RAW disables text properties and transformation."
26032606
(gptel--insert-response (concat separator (car blocks)) info t)
26042607
(gptel--insert-response text info)
26052608
(gptel--insert-response (cdr blocks) info t))
2606-
(when (derived-mode-p 'org-mode) ;fold block
2607-
(save-excursion
2608-
(goto-char (plist-get info :tracking-marker))
2609-
(search-backward "#+end_reasoning" start-marker t)
2610-
(when (looking-at "^#\\+end_reasoning")
2611-
(org-cycle)))))))))
2609+
(save-excursion
2610+
(goto-char (plist-get info :tracking-marker))
2611+
(if (derived-mode-p 'org-mode) ;fold block
2612+
(progn
2613+
(search-backward "#+end_reasoning" start-marker t)
2614+
(when (looking-at "^#\\+end_reasoning")
2615+
(org-cycle)))
2616+
(search-backward "```" start-marker t)
2617+
(when (looking-at "^```$")
2618+
(gptel--hs-fold
2619+
(truncate-string-to-width
2620+
text gptel-display-max nil nil t)))
2621+
)))))))
26122622
(`(tool-call . ,tool-calls)
26132623
(gptel--display-tool-calls tool-calls info))
26142624
(`(tool-result . ,tool-results)
@@ -3005,13 +3015,20 @@ for streaming responses only."
30053015
(propertize "\n```" 'gptel 'ignore))
30063016
gptel-response-separator)
30073017
info t)
3008-
(when (derived-mode-p 'org-mode) ;fold block
3009-
(ignore-errors
3010-
(save-excursion
3011-
(goto-char tracking-marker)
3012-
(search-backward "#+end_reasoning" start-marker t)
3013-
(when (looking-at "^#\\+end_reasoning")
3014-
(org-cycle))))))
3018+
(ignore-errors
3019+
(save-excursion
3020+
(goto-char tracking-marker)
3021+
(if (derived-mode-p 'org-mode) ;fold block
3022+
(progn
3023+
(search-backward "#+end_reasoning" start-marker t)
3024+
(when (looking-at "^#\\+end_reasoning")
3025+
(org-cycle)))
3026+
(search-backward "```" start-marker t)
3027+
(when (looking-at "^```$")
3028+
(gptel--hs-fold
3029+
(truncate-string-to-width
3030+
text gptel-display-max nil nil t)))
3031+
))))
30153032
(unless (and reasoning-marker tracking-marker
30163033
(= reasoning-marker tracking-marker))
30173034
(let ((separator ;Separate from response prefix if required
@@ -3187,22 +3204,25 @@ for tool call results. INFO contains the state of the request."
31873204
separator
31883205
"#+begin_tool "
31893206
truncated-call
3207+
"\n"
31903208
(propertize
3191-
(concat "\n" call "\n\n" (org-escape-code-in-string result))
3209+
(concat call "\n\n" (org-escape-code-in-string result))
31923210
'gptel `(tool . ,id))
3193-
"\n#+end_tool\n")
3211+
"\n#+end_tool"
3212+
gptel-response-separator)
31943213
;; TODO(tool) else branch is handling all front-ends as markdown.
31953214
;; At least escape markdown.
31963215
(concat
31973216
separator
31983217
;; TODO(tool) remove properties and strip instead of ignoring
3199-
(propertize (format "``` tool %s" truncated-call) 'gptel 'ignore)
3218+
(propertize (format "``` tool %s\n" truncated-call) 'gptel 'ignore)
32003219
(propertize
32013220
;; TODO(tool) escape markdown in result
3202-
(concat "\n" call "\n\n" result)
3221+
(concat call "\n\n" result)
32033222
'gptel `(tool . ,id))
32043223
;; TODO(tool) remove properties and strip instead of ignoring
3205-
(propertize "\n```\n" 'gptel 'ignore))))
3224+
(propertize "\n```" 'gptel 'ignore)
3225+
gptel-response-separator)))
32063226
info
32073227
'raw)
32083228
;; tool-result insertion has updated the tracking marker
@@ -3212,13 +3232,39 @@ for tool call results. INFO contains the state of the request."
32123232
(move-marker tool-marker tracking-marker)
32133233
(setq tool-marker (copy-marker tracking-marker nil))
32143234
(plist-put info :tool-marker tool-marker))
3215-
(when (derived-mode-p 'org-mode) ;fold drawer
3216-
(ignore-errors
3217-
(save-excursion
3218-
(goto-char tracking-marker)
3219-
(forward-line -1)
3220-
(when (looking-at "^#\\+end_tool")
3221-
(org-cycle))))))))))
3235+
(ignore-errors
3236+
(save-excursion
3237+
(goto-char tracking-marker)
3238+
(if (derived-mode-p 'org-mode) ;fold drawer
3239+
(progn
3240+
(search-backward "#+end_tool" start-marker t)
3241+
(when (looking-at "^#\\+end_tool")
3242+
(org-cycle)))
3243+
(search-backward "```" start-marker t)
3244+
(when (looking-at "^```$")
3245+
(gptel--hs-fold
3246+
(truncate-string-to-width
3247+
result gptel-display-max nil nil t)))
3248+
))))))))
3249+
3250+
(defun gptel--hs-fold (&optional disp)
3251+
"Fold the current 'gptel text section and display DISP instead."
3252+
(interactive)
3253+
(let (beg end ov)
3254+
;; end of range, should be at \n now
3255+
(setq end (if (eq (get-text-property (point) 'gptel) 'ignore)
3256+
(previous-single-property-change
3257+
(point) 'gptel nil (point-min))
3258+
(next-single-property-change
3259+
(point) 'gptel nil (point-max))))
3260+
(setq beg (previous-single-property-change
3261+
end 'gptel nil (point-min)))
3262+
(setq ov (make-overlay beg end))
3263+
(overlay-put ov 'invisible t)
3264+
(overlay-put ov 'hs 'region)
3265+
(overlay-put ov 'hs-b-offset 0)
3266+
(overlay-put ov 'display (or disp (truncate-string-ellipsis)))
3267+
(overlay-put ov 'evaporate t)))
32223268

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

0 commit comments

Comments
 (0)