210
210
(require 'map )
211
211
(require 'text-property-search )
212
212
(require 'cl-generic )
213
+ (require 'mule-util )
213
214
(require 'gptel-openai )
214
215
215
216
@@ -2552,6 +2553,9 @@ specified."
2552
2553
(error
2553
2554
(user-error " Can not resume request: could not read data from buffer!" )))))
2554
2555
2556
+ (defconst gptel-display-max 128
2557
+ " Maximum number of chars to be displayed for tool, reason ..." )
2558
+
2555
2559
(defun gptel--insert-response (response info &optional raw )
2556
2560
" Insert the LLM RESPONSE into the gptel buffer.
2557
2561
@@ -2613,12 +2617,19 @@ Optional RAW disables text properties and transformation."
2613
2617
(gptel--insert-response (concat separator (car blocks)) info t )
2614
2618
(gptel--insert-response text info)
2615
2619
(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
+ )))))))
2622
2633
(`(tool-call . , tool-calls )
2623
2634
(gptel--display-tool-calls tool-calls info))
2624
2635
(`(tool-result . , tool-results )
@@ -3015,13 +3026,20 @@ for streaming responses only."
3015
3026
(propertize " \n ```" 'gptel 'ignore ))
3016
3027
gptel-response-separator)
3017
3028
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
+ ))))
3025
3043
(unless (and reasoning-marker tracking-marker
3026
3044
(= reasoning-marker tracking-marker))
3027
3045
(let ((separator ; Separate from response prefix if required
@@ -3197,22 +3215,25 @@ for tool call results. INFO contains the state of the request."
3197
3215
separator
3198
3216
" #+begin_tool "
3199
3217
truncated-call
3218
+ " \n "
3200
3219
(propertize
3201
- (concat " \n " call " \n\n " (org-escape-code-in-string result))
3220
+ (concat call " \n\n " (org-escape-code-in-string result))
3202
3221
'gptel `(tool . , id ))
3203
- " \n #+end_tool\n " )
3222
+ " \n #+end_tool"
3223
+ gptel-response-separator)
3204
3224
; ; TODO(tool) else branch is handling all front-ends as markdown.
3205
3225
; ; At least escape markdown.
3206
3226
(concat
3207
3227
separator
3208
3228
; ; 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 )
3210
3230
(propertize
3211
3231
; ; TODO(tool) escape markdown in result
3212
- (concat " \n " call " \n\n " result)
3232
+ (concat call " \n\n " result)
3213
3233
'gptel `(tool . , id ))
3214
3234
; ; TODO(tool) remove properties and strip instead of ignoring
3215
- (propertize " \n ```\n " 'gptel 'ignore ))))
3235
+ (propertize " \n ```" 'gptel 'ignore )
3236
+ gptel-response-separator)))
3216
3237
info
3217
3238
'raw )
3218
3239
; ; tool-result insertion has updated the tracking marker
@@ -3222,13 +3243,39 @@ for tool call results. INFO contains the state of the request."
3222
3243
(move-marker tool-marker tracking-marker)
3223
3244
(setq tool-marker (copy-marker tracking-marker nil ))
3224
3245
(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 )))
3232
3279
3233
3280
(defun gptel--format-tool-call (name arg-values )
3234
3281
" Format a tool call for display in the buffer.
0 commit comments