diff --git a/src/main/ui/actions/chat.cljs b/src/main/ui/actions/chat.cljs index cd69236..f678cee 100644 --- a/src/main/ui/actions/chat.cljs +++ b/src/main/ui/actions/chat.cljs @@ -2,7 +2,7 @@ (:require [applied-science.js-interop :as j] [reagent.core :as r] - [ui.utils :refer [count-tokens-api q p pp get-parent-parent extract-from-code-block call-openai-api log update-block-string-and-move is-a-page? get-child-with-str move-block create-new-block]] + [ui.utils :refer [count-tokens-api update-block-string-for-block-with-child q p pp get-parent-parent extract-from-code-block call-openai-api log update-block-string-and-move is-a-page? get-child-with-str move-block create-new-block]] [cljs.core.async.interop :as asy :refer [! go chan put! take! timeout]])) @@ -55,6 +55,7 @@ :token-count-atom token-count-atom}) (p (str pre "Add assistant response block in messages: " m-uid)) (reset! message-atom (get-child-with-str block-uid "Messages")) + (update-block-string-for-block-with-child block-uid "Settings" "Active?" (str (not @active?))) (reset! active? false)) 500))))}))) diff --git a/src/main/ui/render_comp/chat.cljs b/src/main/ui/render_comp/chat.cljs index 045b2dd..4f5688a 100644 --- a/src/main/ui/render_comp/chat.cljs +++ b/src/main/ui/render_comp/chat.cljs @@ -3,40 +3,84 @@ [applied-science.js-interop :as j] ["@blueprintjs/core" :as bp :refer [Checkbox Tooltip HTMLSelect Button ButtonGroup Card Slider Divider Menu MenuItem Popover MenuDivider]] [ui.components.chat :as comp :refer [send-message-component chin chat-context chat-history]] - [ui.utils :refer [watch-children create-struct settings-struct get-child-of-child-with-str q p get-parent-parent extract-from-code-block call-openai-api log update-block-string-and-move is-a-page? get-child-with-str move-block create-new-block]] + [ui.utils :refer [watch-children update-block-string-for-block-with-child watch-string create-struct settings-struct get-child-of-child-with-str q p get-parent-parent extract-from-code-block call-openai-api log update-block-string-and-move is-a-page? get-child-with-str move-block create-new-block]] [ui.actions.chat :refer [send-context-and-message load-context]] [reagent.dom :as rd])) + (defn chat-ui [block-uid] #_(println "block uid for chat" block-uid) - (let [settings (get-child-with-str block-uid "Settings") - context (r/atom (get-child-with-str block-uid "Context")) - messages (r/atom (get-child-with-str block-uid "Messages")) - chat (r/atom (get-child-with-str block-uid "Chat")) - messages-atom (r/atom []) - active? (r/atom false) - token-count (r/atom (js/parseInt (get-child-of-child-with-str block-uid "Settings" "Token count"))) - default-msg-value (r/atom (js/parseInt (get-child-of-child-with-str block-uid "Settings" "Max tokens"))) - default-temp (r/atom (js/parseFloat (get-child-of-child-with-str block-uid "Settings" "Temperature"))) - default-model (r/atom (get-child-of-child-with-str block-uid "Settings" "Model")) - get-linked-refs (r/atom (if (= "true" (get-child-of-child-with-str block-uid "Settings" "Get linked refs")) - true - false))] + (let [settings (get-child-with-str block-uid "Settings") + context (r/atom (get-child-with-str block-uid "Context")) + messages (r/atom (get-child-with-str block-uid "Messages")) + chat (r/atom (get-child-with-str block-uid "Chat")) + messages-atom (r/atom []) + active? (r/atom (if (= "true" (get-child-of-child-with-str block-uid "Settings" "Active?")) + true + false)) + token-count (r/atom (js/parseInt (get-child-of-child-with-str block-uid "Settings" "Token count"))) + default-max-tokens (r/atom (js/parseInt (get-child-of-child-with-str block-uid "Settings" "Max tokens"))) + default-temp (r/atom (js/parseFloat (get-child-of-child-with-str block-uid "Settings" "Temperature"))) + default-model (r/atom (get-child-of-child-with-str block-uid "Settings" "Model")) + get-linked-refs (r/atom (if (= "true" (get-child-of-child-with-str block-uid "Settings" "Get linked refs")) + true + false))] (watch-children (:uid @messages) (fn [_ aft] (p "context children changed" aft) (reset! messages-atom aft))) + + (watch-string + (get-child-of-child-with-str block-uid "Settings" "Token count" false) + (fn [b aft] + (p "token count changed" aft) + (reset! token-count (js/parseInt (:string aft))))) + + (watch-string + (get-child-of-child-with-str block-uid "Settings" "Max tokens" false) + (fn [_ aft] + (p "max tokens changed" aft) + (reset! default-max-tokens (js/parseInt (:string aft))))) + + (watch-string + (get-child-of-child-with-str block-uid "Settings" "Temperature" false) + (fn [_ aft] + (p "temperature changed" aft) + (reset! default-temp (js/parseFloat (:string aft))))) + + (watch-string + (get-child-of-child-with-str block-uid "Settings" "Model" false) + (fn [_ aft] + (p "model name changed" aft) + (reset! default-model (:string aft)))) + + (watch-string + (get-child-of-child-with-str block-uid "Settings" "Get linked refs" false) + (fn [_ aft] + (p "get linked refs changed" aft) + (reset! get-linked-refs (if (= "true" (:string aft)) + true + false)))) + (watch-string + (get-child-of-child-with-str block-uid "Settings" "Active?" false) + (fn [_ aft] + (p "Active button changed" aft) + (reset! active? (if (= "true" (:string aft)) + true + false)))) + (fn [_] - (let [msg-children @messages-atom + (let [msg-children @messages-atom c-msg (:children @context) callback (fn [{:keys [b-uid] :or {b-uid block-uid}}] ;(println "called callback to load context") (when (not @active?) (p "*Send* Button clicked") (do + (update-block-string-for-block-with-child block-uid "Settings" "Active?" (str (not @active?))) (reset! active? true) (load-context chat @@ -47,7 +91,7 @@ {:model (if (= "gpt-4" @default-model) "gpt-4-0125-preview" "gpt-3.5-turbo-0125") - :max-tokens @default-msg-value + :max-tokens @default-max-tokens :temperature @default-temp} token-count)))) @@ -89,7 +133,7 @@ :background-color "whitesmoke" :border "1px"}} [chat-context chat handle-key-event]] - [chin default-model default-msg-value default-temp get-linked-refs active? block-uid callback]]])))) + [chin default-model default-max-tokens default-temp get-linked-refs active? block-uid callback]]])))) diff --git a/src/main/ui/utils.cljs b/src/main/ui/utils.cljs index 0afe34b..4a90248 100644 --- a/src/main/ui/utils.cljs +++ b/src/main/ui/utils.cljs @@ -104,7 +104,6 @@ (defn add-pull-watch [pull-pattern entity-id callback] - (println "add pull watch :" entity-id) (let [roam-api (.-data (.-roamAlphaAPI js/window)) add-pull-watch-fn (.-addPullWatch roam-api) js-callback (fn [before after] @@ -118,6 +117,13 @@ (defn watch-children [block-uid cb] (let [pull-pattern "[:block/uid :block/order {:block/children ...}]" entity-id (str [:block/uid block-uid])] + (println "add pull watch :" entity-id) + (add-pull-watch pull-pattern entity-id cb))) + +(defn watch-string [block-uid cb] + (let [pull-pattern "[:block/uid :block/string]" + entity-id (str [:block/uid block-uid])] + (p "add string watch :" entity-id) (add-pull-watch pull-pattern entity-id cb))) @@ -425,7 +431,9 @@ {:s "Temperature" :c [{:s "0.9"}]} {:s "Get linked refs" - :c [{:s "true"}]}]}) + :c [{:s "true"}]} + {:s "Active?" + :c [{:s "false"}]}]}) (defn common-chat-struct [context-structure context-block-uid context-open?]