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 { func handleAPIChatBotGet(w http.ResponseWriter, r *http.Request) error {
cookie, _ := ParseCookie(r) cookie, _ := ParseCookie(r)
sessionD := path.Join(Config.ChatBot.SessionD, cookie.MyName()) 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 { if err != nil {
return err return err
} }
defer f.Close()
io.Copy(w, f) return json.NewEncoder(w).Encode(struct {
return nil Messages string
Prompt string
}{
Messages: string(messages),
Prompt: string(prompt),
})
} }
func handleAPIChatBotPost(w http.ResponseWriter, r *http.Request) error { 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 { if len(prompt) == 0 {
return errors.New("no prompt") 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 { if err := os.WriteFile(path.Join(sessionD, "prompt.txt"), []byte(prompt), os.ModePerm); err != nil {
return err return err
} }

View File

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