Skip to content

Commit

Permalink
Add HTTP server and prototype interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
whilo committed Apr 12, 2024
1 parent 5c7b38c commit 5ddf9d8
Show file tree
Hide file tree
Showing 10 changed files with 181 additions and 31 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@

databases/
notes/
downloads/
.clj-kondo/
resources/config.edn
*.log
9 changes: 0 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,6 @@ Make sure to define your credentials in `resources/config.edn`. Prompts are stor

Just start a REPL and explore the `towers` namespace.

## Agenda

- add scheduling
- add accounting
- abstract message format
- renamte 'tag' to entity or something more serious
- update schema in prototype on next reset
- add payments

## License

Copyright © 2024 Christian Weilbach
Expand Down
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
morse/morse {:mvn/version "0.4.3"}
io.replikativ/kabel {:mvn/version "0.2.2"}
http-kit/http-kit {:mvn/version "2.7.0"}
metosin/reitit {:mvn/version "0.7.0-alpha7"}
compojure/compojure {:mvn/version "1.7.1"}
ring/ring-jetty-adapter {:mvn/version "1.12.0"}
etaoin/etaoin {:mvn/version "1.0.40"}
Expand Down
91 changes: 91 additions & 0 deletions resources/public/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Simmie</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.9.3/css/bulma.min.css">
<style>
body, html {
height: 100%;
margin: 0;
}
.full-height {
height: 100%;
}
.content {
color: black;
background-color: white;
padding: 20px;
margin: 20px;
}
</style>
</head>
<body>
<section class="hero is-fullheight">
<div class="hero-head">
<header class="navbar">
<div class="container">
<div id="navbarMenu" class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item" href="#">Home</a>
<a class="navbar-item" href="#">Features</a>
<a class="navbar-item" href="#">About</a>
</div>
</div>
<div class="navbar-brand">
<a class="navbar-item" href="/">
<img src="simmie.png" alt="Simmie logo">
</a>
<span class="navbar-burger" data-target="navbarMenu">
<span></span>
<span></span>
<span></span>
</span>
</div>
</div>
</header>
</div>
<div class="hero-body">
<div class="container has-text-centered">
<div class="content">
<h1>Hey!</h1>
<p>Simmie is a conversational AI system that combines different forms of intelligence to help you pursue your goals or just have fun.</p>
</div>
</div>
</div>
<div class="hero-foot">
<footer class="footer">
<div class="content has-text-centered">
<p>Copyright © 2024 Christian Weilbach. All rights reserved.</p>
</div>
</footer>
</div>
</section>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Get all "navbar-burger" elements
const $navbarBurgers = Array.prototype.slice.call(document.querySelectorAll('.navbar-burger'), 0);

// Check if there are any navbar burgers
if ($navbarBurgers.length > 0) {

// Add a click event on each of them
$navbarBurgers.forEach(el => {
el.addEventListener('click', () => {

// Get the target from the "data-target" attribute
const target = el.dataset.target;
const $target = document.getElementById(target);

// Toggle the "is-active" class on both the "navbar-burger" and the targeted "navbar-menu"
el.classList.toggle('is-active');
$target.classList.toggle('is-active');

});
});
}
});
</script>
</body>
</html>
Binary file added resources/public/simmie.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
28 changes: 28 additions & 0 deletions src/ie/simm/http.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
(ns ie.simm.http
(:require [muuntaja.core :as m]
[reitit.ring :as ring]
[reitit.coercion.spec]
[reitit.ring.coercion :as rrc]
[reitit.ring.middleware.muuntaja :as muuntaja]
[reitit.ring.middleware.parameters :as parameters]))

(defn website-routes []
[["/hello" {:get (fn [request]
{:status 200
:body "Hello, world!"})}]])

(defn ring-handler [routes]
(let [routes (concat (vec (apply concat (vals routes))) (website-routes))]
(prn "ROUTES" routes)
(ring/ring-handler
(ring/router
routes
{:data {:coercion reitit.coercion.spec/coercion
:muuntaja m/instance
:middleware [parameters/parameters-middleware
rrc/coerce-request-middleware
muuntaja/format-response-middleware
rrc/coerce-response-middleware]}})
(ring/routes
(ring/create-resource-handler {:path "/"})
(ring/create-default-handler)))))
37 changes: 29 additions & 8 deletions src/ie/simm/runtimes/assistance.clj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@
(.close zip-out)
zip-file))


(def base-url "https://ec2-34-218-223-7.us-west-2.compute.amazonaws.com")

(defn assistance
"This interpreter can derive facts and effects through a relational database."
[[S peer [in out]]]
Expand All @@ -113,7 +116,26 @@
_ (tap mo out)
pub-out (chan)
_ (tap mo pub-out)
po (pub pub-out :type)]
po (pub pub-out :type)

;; TODO figure out prefix, here conflict if notes/
routes [["/download/notes/:chat-id/notes.zip"
{:get (fn [{{:keys [chat-id]} :path-params}]
{:status 200 :body (zip-notes chat-id)})}]
["/notes/:chat-id"
{:get (fn [{{:keys [chat-id]} :path-params}]
;; list the notes in basic HTML
{:status 200
:body (str "<html><body><h1>Notes</h1><a href=\"/download/notes/" chat-id "/notes.zip\">Download</a><ul>"
(str/join "" (map (fn [f] (str "<li><a href=\"/" f "\">" f "</a></li>"))
(rest (file-seq (io/file (str "notes/" chat-id))))))
"</ul></body></html>")})}]
;; access each individual node link as referenced above
["/notes/:chat-id/:note"
{:get (fn [{{:keys [chat-id note]} :path-params}]
{:status 200
:body (slurp (io/file (str "notes/" chat-id "/" note)))})}]]]
(swap! peer assoc-in [:http :routes :assistance] routes)
;; we will continuously interpret the messages
(go-loop-try S [m (<? S msg-ch)]
(when m
Expand Down Expand Up @@ -166,9 +188,9 @@
_ (debug "active tags" #_summaries active-tags)

;; 4. derive reply
assist-prompt (format pr/assistance
assist-prompt (format pr/assistance
(str/join "\n\n" (map (fn [[t s]] (format "Title: %s\nBody: %s" t s))
summaries))
summaries))
conv
(str (java.util.Date.)))
_ (debug "prompt" assist-prompt)
Expand Down Expand Up @@ -213,11 +235,11 @@

_ (when (.contains reply "LIST_NOTES")
(debug "listing notes")
(let [issues (d/q '[:find [?t ...] :where [_ :note/title ?t]] @conn)]
(d/transact conn (msg->txs (:result (<? S (send-text! (:id chat) (str "Notes:\n" (str/join "\n" (map #(format "* %s" %) issues))))))))))
(let [#_#_issues (d/q '[:find [?t ...] :where [_ :note/title ?t]] @conn)]
(d/transact conn (msg->txs (:result (<? S (send-text! (:id chat) (str "Notes:\n" (format "%s/notes/%s" base-url (:id chat)) #_(str/join "\n" (map #(format "* %s" %) issues))))))))))


_ (if (or (.contains reply "QUIET") (.contains reply "IMAGEGEN") (.contains reply "LIST_ISSUES") (.contains reply "ADD_ISSUE")
_ (if (or (.contains reply "QUIET") (.contains reply "IMAGEGEN") (.contains reply "LIST_ISSUES") (.contains reply "ADD_ISSUE")
(.contains reply "REMOVE_ISSUE") (.contains reply "SEND_NOTES") (.contains reply "RETRIEVE_NOTE") (.contains reply "LIST_NOTES"))
(debug "No reply necessary")
(let [reply (if-let [terms (second (re-find #"WEBSEARCH\(['\"](.*)['\"]\)" reply))]
Expand All @@ -235,8 +257,7 @@
_ (<? S (timeout too-fast-by))
reply-msg (<? S (send-text! (:id chat) reply))
_ (debug "reply-msg" reply-msg)
_ (d/transact conn (msg->txs (:result reply-msg)))]))]
)
_ (d/transact conn (msg->txs (:result reply-msg)))]))])
(catch Exception e
(let [error-id (uuid)]
(error "Could not process message(" error-id "): " m e)
Expand Down
26 changes: 18 additions & 8 deletions src/ie/simm/runtimes/telegram.clj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
[compojure.core :refer [routes POST]]
[compojure.route :as route]
[jsonista.core :as json]
[ring.adapter.jetty :refer [run-jetty]]
[superv.async :refer [go-try S <? <?? go-loop-try put?]]
[taoensso.timbre :refer [debug]]
[hasch.core :refer [uuid]]
Expand All @@ -37,7 +36,7 @@
(io/file local-path))]
local-path))))

(defn server [in]
(defn server [peer in]
(let [telegram-routes (routes
(POST "/telegram-callback" {body :body}
(let [msg (-> body slurp (json/read-value json/keyword-keys-object-mapper) :message)
Expand All @@ -49,11 +48,22 @@
(put? S in m)
{:body "Thanks!"}))
(route/not-found "Not Found"))
_ (debug "starting jetty telegram server")
server (run-jetty telegram-routes {:port 8080 :join? false})]
#(.stop server)))

(defn long-polling [in]
telegram-routes [["/telegram-callback" {:post
(fn [{:keys [body]}]
(let [msg (-> body slurp (json/read-value json/keyword-keys-object-mapper) :message)]
(debug "received telegram message:" msg)
(put? S in {:type ::message :request-id (uuid) :msg (fetch-voice! msg)})
{:status 200 :body "Success."}))
#_{:parameters {:body [:map]}
:responses {200 {:body [:map]}}
:handler _}}]]
_ (debug "created telegram routes")
;;server (run-jetty telegram-routes {:port 8080 :join? false})
]
(swap! peer assoc-in [:http :routes :telegram] telegram-routes)
#(fn [])))

(defn long-polling [peer in]
(let [_ (h/defhandler bot-api
(h/message-fn (fn [message]
(debug "received telegram message:" message)
Expand All @@ -68,7 +78,7 @@
([[S peer [in out]]]
(telegram server [S peer [in out]]))
([mechanism [S peer [in out]]]
(let [stop-fn (mechanism in)
(let [stop-fn (mechanism peer in)
p (pub in (fn [_] :always))
next-in (chan)
_ (sub p :always next-in)
Expand Down
1 change: 0 additions & 1 deletion src/ie/simm/runtimes/text_extractor.clj
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,6 @@
text (<? S (extract-url S text chat))
msg (assoc msg :text text)
m (assoc m :msg msg)]
(prn "NEW M" m)
(>? S next-in m))
(catch Exception e
(let [error-id (uuid)]
Expand Down
18 changes: 13 additions & 5 deletions src/ie/simm/simmie.clj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@
(:require [taoensso.timbre :as log]
[taoensso.timbre.appenders.core :as appenders]
[superv.async :refer [S go-try <? <??] :as sasync]
[ie.simm.http :refer [ring-handler]]
[ring.adapter.jetty :refer [run-jetty]]
[clojure.core.async :refer [chan close!]]
[ie.simm.towers :refer [default debug test-tower]]
[nrepl.server :refer [start-server stop-server]]
[ie.simm.prompts :as pr]
[datahike.experimental.gc :as gc])
[nrepl.server :refer [start-server stop-server]])
(:gen-class))

(defonce server (start-server :port 37888))
(defonce nrepl-server (start-server :port 37888))

(log/merge-config!
{:appenders {:spit (appenders/spit-appender {:fname "server.log"})}})
Expand All @@ -23,7 +23,15 @@
out (chan)]
(def chans [in out])
(def peer (atom {}))
(sasync/restarting-supervisor (fn [S] (go-try S ((debug) [S peer chans])))
(sasync/restarting-supervisor (fn [S] (go-try S
((debug) [S peer chans])
(let [ring (ring-handler (get-in @peer [:http :routes]))
server (run-jetty ring {:port 8080 :join? false})]
(swap! peer assoc-in [:http :server] server)
(log/info "Server started.")
;; this only unblocks on crash
(<? S (chan))
(.stop server))))
:delay (* 10 1000)
:log-fn (fn [level msg] (log/log level msg)))
(log/info "Server started.")
Expand Down

0 comments on commit 5ddf9d8

Please sign in to comment.