ok submitting a prompt

master
bel 2023-06-17 14:31:47 -06:00
parent 287b72639d
commit 1a7f783fe0
3 changed files with 94 additions and 2 deletions

View File

@ -171,7 +171,12 @@ func handleUI(w http.ResponseWriter, r *http.Request) error {
w.Write(htmlIndex)
return nil
case "/chatbot":
w.Write(htmlChatBot)
if !Config.Debug {
w.Write(htmlChatBot)
} else {
b, _ := os.ReadFile("./template.d/chatbot.html")
w.Write(b)
}
return nil
default:
return handleNotFound(w, r)
@ -258,6 +263,7 @@ func handleNotFound(w http.ResponseWriter, r *http.Request) error {
}
func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error {
r.Header.Set("Content-Type", "application/x-www-form-urlencoded")
err := r.ParseForm()
if err != nil {
return err
@ -268,11 +274,26 @@ func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error {
return handleAPIChatBotPost(w, r)
case http.MethodPut:
return handleAPIChatBotPut(w, r)
case http.MethodGet:
return handleAPIChatBotGet(w, r)
default:
return handleNotFound(w, r)
}
}
func handleAPIChatBotGet(w http.ResponseWriter, r *http.Request) error {
cookie, _ := ParseCookie(r)
sessionD := path.Join(Config.ChatBot.SessionD, cookie.MyName())
f, err := os.Open(path.Join(sessionD, "prompt.txt"))
if err != nil {
return err
}
defer f.Close()
io.Copy(w, f)
return nil
}
func handleAPIChatBotPost(w http.ResponseWriter, r *http.Request) error {
cookie, _ := ParseCookie(r)
sessionD := path.Join(Config.ChatBot.SessionD, cookie.MyName())

View File

@ -52,6 +52,19 @@ func TestAPIV0ChatBot(t *testing.T) {
t.Errorf("dupe generation: %s", stderrStash.Bytes())
stderrStash.Reset()
}
resp3 := httpDo(t, http.MethodGet, "/api/v0/chatbot", "")
got3, err := io.ReadAll(resp3.Body)
if err != nil {
t.Fatal(err)
}
if !bytes.Contains(got3, got) {
t.Error("forgot got")
}
if !bytes.Contains(got3, got2) {
t.Error("forgot got2")
}
})
t.Run("post over post", func(t *testing.T) {

View File

@ -1,8 +1,66 @@
<html>
<header>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/dark.css">
<script>
function loadStream() {
http("GET", "/api/v0/chatbot", (body, status) => {
document.getElementById("stream-log").content = body
}, null)
}
function startStream(newPrompt) {
body = new URLSearchParams(new FormData(newPrompt)).toString()
console.log(body)
http("POST", "/api/v0/chatbot", (body, status) => {
if (status != 200) {
log(body)
return
}
}, body)
}
function log() {
console.log(arguments)
document.getElementById("debug-log").innerHTML += "\n" + new String(arguments)
}
function http(method, remote, callback, body) {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE) {
callback(xmlhttp.responseText, xmlhttp.status)
}
};
xmlhttp.open(method, remote, true);
if (typeof body == "undefined") {
body = null
}
xmlhttp.send(body);
}
function callback(responseBody, responseStatus) {
}
</script>
</header>
<body>
<body onload="loadStream()">
<details>
<summary>Set up a new session</summary>
<form id="prompt" onsubmit="startStream(this); return false;">
<textarea name="Prompt"></textarea>
<button type="submit">Start with prompt</button>
</form>
</details>
<details open=true>
<summary>Use your session</summary>
<form id="stream" onsubmit="return false;">
<textarea id="stream-log" readonly=true></textarea>
<div style="display: flex; flex-direction: row;">
<input style="flex-grow: 1;" type="text" name="Message"/>
<button type="submit">Send</button>
</div>
</form>
</details>
<pre id="debug-log">
</pre>
</body>
<footer>
</footer>