|
259 | 259 | (defn ^:private execute-action! |
260 | 260 | "Execute a single action during state transition" |
261 | 261 | [action db* chat-ctx tool-call-id event-data] |
262 | | - (logger/debug logger-tag "About to run the tool-call action" {:tool-call-id tool-call-id :action action :event-data event-data}) |
263 | 262 | (case action |
264 | 263 | ;; Notification actions |
265 | 264 | :send-progress |
|
633 | 632 | (let [any-rejected-tool-call?* (atom nil)] |
634 | 633 | (run! (fn do-tool-call [{:keys [id name arguments] :as tool-call}] |
635 | 634 | (let [approved?* (promise) ; created here, stored in the state. |
| 635 | + db @db* |
636 | 636 | hook-approved?* (atom true) |
637 | | - details (f.tools/tool-call-details-before-invocation name arguments) |
638 | | - summary (f.tools/tool-call-summary all-tools name arguments config) |
639 | 637 | origin (tool-name->origin name all-tools) |
640 | 638 | server (tool-name->server name all-tools) |
641 | | - approval (f.tools/approval all-tools name arguments @db* config behavior) |
642 | | - ask? (= :ask approval)] |
| 639 | + server-name (:name server) |
| 640 | + approval (f.tools/approval all-tools name arguments db config behavior) |
| 641 | + ask? (= :ask approval) |
| 642 | + details (f.tools/tool-call-details-before-invocation name arguments server db ask?) |
| 643 | + summary (f.tools/tool-call-summary all-tools name arguments config)] |
643 | 644 | ;; assert: In :preparing or :stopping or :cleanup |
644 | 645 | ;; Inform client the tool is about to run and store approval promise |
645 | | - (when-not (#{:stopping :cleanup} (:status (get-tool-call-state @db* chat-id id))) |
| 646 | + (when-not (#{:stopping :cleanup} (:status (get-tool-call-state db chat-id id))) |
646 | 647 | (transition-tool-call! db* chat-ctx id :tool-run |
647 | 648 | {:approved?* approved?* |
648 | 649 | :future-cleanup-complete?* (promise) |
649 | 650 | :name name |
650 | | - :server server |
| 651 | + :server server-name |
651 | 652 | :origin origin |
652 | 653 | :arguments arguments |
653 | 654 | :manual-approval ask? |
654 | 655 | :details details |
655 | 656 | :summary summary})) |
656 | 657 | ;; assert: In: :check-approval or :stopping or :cleanup or :rejected |
657 | | - (when-not (#{:stopping :cleanup :rejected} (:status (get-tool-call-state @db* chat-id id))) |
| 658 | + (when-not (#{:stopping :cleanup :rejected} (:status (get-tool-call-state db chat-id id))) |
658 | 659 | (case approval |
659 | 660 | :ask (transition-tool-call! db* chat-ctx id :approval-ask |
660 | 661 | {:progress-text "Waiting for tool call approval"}) |
|
671 | 672 | (f.hooks/trigger-if-matches! :preToolCall |
672 | 673 | {:chat-id chat-id |
673 | 674 | :tool-name name |
674 | | - :server server |
| 675 | + :server server-name |
675 | 676 | :arguments arguments} |
676 | 677 | {:on-before-action (partial notify-before-hook-action! chat-ctx) |
677 | 678 | :on-after-action (fn [result] |
|
695 | 696 | (delay |
696 | 697 | (future |
697 | 698 | ;; assert: In :executing |
698 | | - (logger/debug logger-tag "Just set up the call promise" |
699 | | - {:tool-call-id id}) |
700 | 699 | (let [result (f.tools/call-tool! name arguments chat-id id behavior db* config messenger metrics |
701 | 700 | (partial get-tool-call-state @db* chat-id id) |
702 | 701 | (partial transition-tool-call! db* chat-ctx id)) |
|
708 | 707 | :details details |
709 | 708 | :summary summary |
710 | 709 | :origin origin |
711 | | - :server server)}) |
| 710 | + :server server-name)}) |
712 | 711 | (add-to-history! {:role "tool_call_output" |
713 | 712 | :content (assoc tool-call |
714 | 713 | :error (:error result) |
|
717 | 716 | :details details |
718 | 717 | :summary summary |
719 | 718 | :origin origin |
720 | | - :server server)}) |
| 719 | + :server server-name)}) |
721 | 720 | ;; assert: In :executing or :stopping |
722 | 721 | (let [state (get-tool-call-state @db* chat-id id) |
723 | 722 | status (:status state)] |
724 | 723 | (case status |
725 | 724 | :executing (transition-tool-call! db* chat-ctx id :execution-end |
726 | 725 | {:origin origin |
727 | 726 | :name name |
728 | | - :server server |
| 727 | + :server server-name |
729 | 728 | :arguments arguments |
730 | 729 | :error (:error result) |
731 | 730 | :outputs (:contents result) |
|
736 | 735 | :stopping (transition-tool-call! db* chat-ctx id :stop-attempted |
737 | 736 | {:origin origin |
738 | 737 | :name name |
739 | | - :server server |
| 738 | + :server server-name |
740 | 739 | :arguments arguments |
741 | 740 | :error (:error result) |
742 | 741 | :outputs (:contents result) |
|
749 | 748 | {:delayed-future delayed-future |
750 | 749 | :origin origin |
751 | 750 | :name name |
752 | | - :server server |
| 751 | + :server server-name |
753 | 752 | :arguments arguments |
754 | 753 | :start-time (System/currentTimeMillis) |
755 | 754 | :details details |
|
767 | 766 | (transition-tool-call! db* chat-ctx id :send-reject |
768 | 767 | {:origin origin |
769 | 768 | :name name |
770 | | - :server server |
| 769 | + :server server-name |
771 | 770 | :arguments arguments |
772 | 771 | :reason code |
773 | 772 | :details details |
|
792 | 791 | {:tool-call-id tool-call-id |
793 | 792 | :promise p}) |
794 | 793 | (deref p) ; TODO: May need a timeout here too, in case the tool does not clean up. |
795 | | - (logger/debug logger-tag "Future has finished cleanup." |
796 | | - {:tool-call-id tool-call-id}))) |
| 794 | + )) |
797 | 795 | (catch Throwable t |
798 | 796 | (logger/debug logger-tag "Ignoring a Throwable while deref'ing a tool call future" |
799 | 797 | {:tool-call-id tool-call-id |
|
0 commit comments