@@ -2542,6 +2542,9 @@ specified."
2542
2542
(error
2543
2543
(user-error " Can not resume request: could not read data from buffer!" )))))
2544
2544
2545
+ (defconst gptel-display-max 128
2546
+ " Maximum number of chars to be displayed for tool, reason ..." )
2547
+
2545
2548
(defun gptel--insert-response (response info &optional raw )
2546
2549
" Insert the LLM RESPONSE into the gptel buffer.
2547
2550
@@ -2603,12 +2606,19 @@ Optional RAW disables text properties and transformation."
2603
2606
(gptel--insert-response (concat separator (car blocks)) info t )
2604
2607
(gptel--insert-response text info)
2605
2608
(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
+ )))))))
2612
2622
(`(tool-call . , tool-calls )
2613
2623
(gptel--display-tool-calls tool-calls info))
2614
2624
(`(tool-result . , tool-results )
@@ -3005,13 +3015,20 @@ for streaming responses only."
3005
3015
(propertize " \n ```" 'gptel 'ignore ))
3006
3016
gptel-response-separator)
3007
3017
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
+ ))))
3015
3032
(unless (and reasoning-marker tracking-marker
3016
3033
(= reasoning-marker tracking-marker))
3017
3034
(let ((separator ; Separate from response prefix if required
@@ -3187,22 +3204,25 @@ for tool call results. INFO contains the state of the request."
3187
3204
separator
3188
3205
" #+begin_tool "
3189
3206
truncated-call
3207
+ " \n "
3190
3208
(propertize
3191
- (concat " \n " call " \n\n " (org-escape-code-in-string result))
3209
+ (concat call " \n\n " (org-escape-code-in-string result))
3192
3210
'gptel `(tool . , id ))
3193
- " \n #+end_tool\n " )
3211
+ " \n #+end_tool"
3212
+ gptel-response-separator)
3194
3213
; ; TODO(tool) else branch is handling all front-ends as markdown.
3195
3214
; ; At least escape markdown.
3196
3215
(concat
3197
3216
separator
3198
3217
; ; 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 )
3200
3219
(propertize
3201
3220
; ; TODO(tool) escape markdown in result
3202
- (concat " \n " call " \n\n " result)
3221
+ (concat call " \n\n " result)
3203
3222
'gptel `(tool . , id ))
3204
3223
; ; TODO(tool) remove properties and strip instead of ignoring
3205
- (propertize " \n ```\n " 'gptel 'ignore ))))
3224
+ (propertize " \n ```" 'gptel 'ignore )
3225
+ gptel-response-separator)))
3206
3226
info
3207
3227
'raw )
3208
3228
; ; tool-result insertion has updated the tracking marker
@@ -3212,13 +3232,39 @@ for tool call results. INFO contains the state of the request."
3212
3232
(move-marker tool-marker tracking-marker)
3213
3233
(setq tool-marker (copy-marker tracking-marker nil ))
3214
3234
(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 )))
3222
3268
3223
3269
(defun gptel--format-tool-call (name arg-values )
3224
3270
" Format a tool call for display in the buffer.
0 commit comments