@@ -424,11 +424,13 @@ parameters.
424424ARGS are the original function call arguments."
425425 (if (derived-mode-p 'org-mode )
426426 (pcase-let ((`(, gptel--system-message , gptel-backend , gptel-model
427- , gptel-temperature , gptel-max-tokens )
427+ , gptel-temperature , gptel-max-tokens
428+ , gptel--num-messages-to-send , gptel-tools )
428429 (seq-mapn (lambda (a b ) (or a b))
429430 (gptel-org--entry-properties)
430431 (list gptel--system-message gptel-backend gptel-model
431- gptel-temperature gptel-max-tokens))))
432+ gptel-temperature gptel-max-tokens
433+ gptel--num-messages-to-send gptel-tools))))
432434 (apply send-fun args))
433435 (apply send-fun args)))
434436
@@ -445,12 +447,12 @@ ARGS are the original function call arguments."
445447(defun gptel-org--entry-properties (&optional pt )
446448 " Find gptel configuration properties stored at PT."
447449 (pcase-let
448- ((`(, system , backend , model , temperature , tokens , num )
450+ ((`(, system , backend , model , temperature , tokens , num , tools )
449451 (mapcar
450452 (lambda (prop ) (org-entry-get (or pt (point )) prop 'selective ))
451453 '(" GPTEL_SYSTEM" " GPTEL_BACKEND" " GPTEL_MODEL"
452454 " GPTEL_TEMPERATURE" " GPTEL_MAX_TOKENS"
453- " GPTEL_NUM_MESSAGES_TO_SEND" ))))
455+ " GPTEL_NUM_MESSAGES_TO_SEND" " GPTEL_TOOLS " ))))
454456 (when system
455457 (setq system (string-replace " \\ n" " \n " system)))
456458 (when backend
@@ -461,7 +463,16 @@ ARGS are the original function call arguments."
461463 (setq temperature (gptel--to-number temperature)))
462464 (when tokens (setq tokens (gptel--to-number tokens)))
463465 (when num (setq num (gptel--to-number num)))
464- (list system backend model temperature tokens num)))
466+ (when tools
467+ (setq tools (cl-loop
468+ for tname in (split-string tools)
469+ for tool = (with-demoted-errors " gptel: %S"
470+ (gptel-get-tool tname))
471+ if tool collect tool else do
472+ (display-warning
473+ '(gptel org tools)
474+ (format " Tool %s not found, ignoring " tname)))))
475+ (list system backend model temperature tokens num tools)))
465476
466477(defun gptel-org--restore-state ()
467478 " Restore gptel state for Org buffers when turning on `gptel-mode' ."
@@ -472,7 +483,7 @@ ARGS are the original function call arguments."
472483 (progn
473484 (when-let* ((bounds (org-entry-get (point-min ) " GPTEL_BOUNDS" )))
474485 (gptel--restore-props (read bounds)))
475- (pcase-let ((`(, system , backend , model , temperature , tokens , num )
486+ (pcase-let ((`(, system , backend , model , temperature , tokens , num , tools )
476487 (gptel-org--entry-properties (point-min ))))
477488 (when system (setq-local gptel--system-message system))
478489 (if backend (setq-local gptel-backend backend)
@@ -486,7 +497,8 @@ ARGS are the original function call arguments."
486497 (when model (setq-local gptel-model model))
487498 (when temperature (setq-local gptel-temperature temperature))
488499 (when tokens (setq-local gptel-max-tokens tokens))
489- (when num (setq-local gptel--num-messages-to-send num))))
500+ (when num (setq-local gptel--num-messages-to-send num))
501+ (when tools (setq-local gptel-tools tools))))
490502 (:success (message " gptel chat restored. " ))
491503 (error (message " Could not restore gptel state, sorry! Error: %s " status)))
492504 (set-buffer-modified-p modified))))
@@ -503,20 +515,27 @@ non-nil (default), display a message afterwards."
503515 (interactive (list (point ) t ))
504516 (org-entry-put pt " GPTEL_MODEL" (gptel--model-name gptel-model))
505517 (org-entry-put pt " GPTEL_BACKEND" (gptel-backend-name gptel-backend))
506- (unless (equal (default-value 'gptel-temperature ) gptel-temperature)
507- (org-entry-put pt " GPTEL_TEMPERATURE"
508- (number-to-string gptel-temperature)))
509- (when (natnump gptel--num-messages-to-send)
510- (org-entry-put pt " GPTEL_NUM_MESSAGES_TO_SEND"
511- (number-to-string gptel--num-messages-to-send)))
512- (org-entry-put pt " GPTEL_SYSTEM"
518+ (org-entry-put pt " GPTEL_SYSTEM" ; TODO: Handle nil case correctly
513519 (and-let* ((msg (car-safe
514520 (gptel--parse-directive
515521 gptel--system-message))))
516522 (string-replace " \n " " \\ n" msg)))
517- (when gptel-max-tokens
518- (org-entry-put
519- pt " GPTEL_MAX_TOKENS" (number-to-string gptel-max-tokens)))
523+ (if gptel-tools
524+ (org-entry-put
525+ pt " GPTEL_TOOLS" (mapconcat #'gptel-tool-name gptel-tools " " ))
526+ (org-entry-delete pt " GPTEL_TOOLS" ))
527+ (if (equal (default-value 'gptel-temperature ) gptel-temperature)
528+ (org-entry-delete pt " GPTEL_TEMPERATURE" )
529+ (org-entry-put pt " GPTEL_TEMPERATURE"
530+ (number-to-string gptel-temperature)))
531+ (if (natnump gptel--num-messages-to-send)
532+ (org-entry-put pt " GPTEL_NUM_MESSAGES_TO_SEND"
533+ (number-to-string gptel--num-messages-to-send))
534+ (org-entry-delete pt " GPTEL_NUM_MESSAGES_TO_SEND" ))
535+ (if gptel-max-tokens
536+ (org-entry-put
537+ pt " GPTEL_MAX_TOKENS" (number-to-string gptel-max-tokens))
538+ (org-entry-delete pt " GPTEL_MAX_TOKENS" ))
520539 (when msg
521540 (message " Added gptel configuration to current headline. " )))
522541
0 commit comments