Skip to content

Commit

Permalink
add reactivity for settings
Browse files Browse the repository at this point in the history
  • Loading branch information
sid597 committed Mar 2, 2024
1 parent a5d1f7b commit 395718c
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 20 deletions.
3 changes: 2 additions & 1 deletion src/main/ui/actions/chat.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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 [<p!]]
[ui.extract-data.chat :as ed :refer [data-for-pages]]
[cljs.core.async :as async :refer [<! >! go chan put! take! timeout]]))
Expand Down Expand Up @@ -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))))})))

Expand Down
78 changes: 61 additions & 17 deletions src/main/ui/render_comp/chat.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))))

Expand Down Expand Up @@ -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]]]))))



Expand Down
12 changes: 10 additions & 2 deletions src/main/ui/utils.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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)))


Expand Down Expand Up @@ -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?]
Expand Down

0 comments on commit 395718c

Please sign in to comment.