master
bel 2023-06-17 15:02:54 -06:00
parent 8abea5c8c1
commit 30bd4311a5
2 changed files with 28 additions and 7 deletions

View File

@ -288,14 +288,23 @@ func handleAPIChatBot(w http.ResponseWriter, r *http.Request) error {
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"))
messages, err := os.ReadFile(path.Join(sessionD, "prompt.txt"))
if err != nil {
return err
}
prompt, err := os.ReadFile(path.Join(sessionD, "initial-prompt.txt"))
if err != nil {
return err
}
defer f.Close()
io.Copy(w, f)
return nil
return json.NewEncoder(w).Encode(struct {
Messages string
Prompt string
}{
Messages: string(messages),
Prompt: string(prompt),
})
}
func handleAPIChatBotPost(w http.ResponseWriter, r *http.Request) error {
@ -314,6 +323,9 @@ func handleAPIChatBotPost(w http.ResponseWriter, r *http.Request) error {
if len(prompt) == 0 {
return errors.New("no prompt")
}
if err := os.WriteFile(path.Join(sessionD, "initial-prompt.txt"), []byte(prompt), os.ModePerm); err != nil {
return err
}
if err := os.WriteFile(path.Join(sessionD, "prompt.txt"), []byte(prompt), os.ModePerm); err != nil {
return err
}

View File

@ -4,10 +4,17 @@
<script>
function loadStream() {
http("GET", "/api/v0/chatbot", (body, status) => {
document.getElementById("stream-log").content = body
var data = JSON.parse(body)
document.getElementById("stream-log").innerHTML = data["Messages"]
document.getElementById("stream-prompt").innerHTML = data["Prompt"]
}, null)
}
function appendStreamLog(message) {
if (message)
document.getElementById("stream-log").innerHTML += "\n" + message
}
function startStream(newPrompt) {
body = new URLSearchParams(new FormData(newPrompt)).toString()
http("POST", "/api/v0/chatbot", (body, status) => {
@ -23,6 +30,7 @@
for(var e of newMessage.elements)
if(!e.attributes.readonly)
e.disabled = true
appendStreamLog(document.getElementsByName("Message")[0].value)
http("PUT", "/api/v0/chatbot", (body, status) => {
for(var e of newMessage.elements)
e.disabled = false
@ -30,7 +38,8 @@
log(body)
return
}
log(body)
appendStreamLog(body)
document.getElementsByName("Message")[0].value = ""
}, body)
}
@ -58,7 +67,7 @@
<details>
<summary>Set up a new session</summary>
<form id="prompt" onsubmit="startStream(this); return false;">
<textarea name="Prompt"></textarea>
<textarea id="stream-prompt" name="Prompt"></textarea>
<button type="submit">Start with prompt</button>
</form>
</details>