|
|
@@ -1,6 +1,7 @@
|
|
|
<script>
|
|
|
import {page} from "$app/state";
|
|
|
import {PUBLIC_API_URL} from "$env/static/public";
|
|
|
+ import {onMount} from "svelte";
|
|
|
|
|
|
import DashboardNav from "../../dashboard/DashboardNav.svelte";
|
|
|
import Message from "./Message.svelte";
|
|
|
@@ -37,14 +38,16 @@
|
|
|
|
|
|
const response = await fetch(`${PUBLIC_API_URL}/game/${game.id}/session/${sessions[0].id}/turn`, {
|
|
|
method: "POST",
|
|
|
- headers: {"Content-Type": "application/json"},
|
|
|
+ headers: {
|
|
|
+ "Content-Type": "application/json",
|
|
|
+ "Accept-Encoding": "identity"
|
|
|
+ },
|
|
|
credentials: "include",
|
|
|
body: JSON.stringify({content: text})
|
|
|
});
|
|
|
|
|
|
const reader = response.body.getReader();
|
|
|
const decoder = new TextDecoder();
|
|
|
- let streamingIndex = turns.length - 1;
|
|
|
let buffer = "";
|
|
|
|
|
|
while(true){
|
|
|
@@ -52,27 +55,29 @@
|
|
|
if(done) break;
|
|
|
|
|
|
buffer += decoder.decode(value, {stream: true});
|
|
|
-
|
|
|
- const lines = buffer.split("\n\n");
|
|
|
+ const lines = buffer.split("\n");
|
|
|
buffer = lines.pop();
|
|
|
|
|
|
for(let i = 0; i < lines.length; i++){
|
|
|
- if(!lines[i].startsWith("data: ")) continue;
|
|
|
- const payload = JSON.parse(lines[i].slice(6));
|
|
|
-
|
|
|
- if(payload.type === "chunk"){
|
|
|
- turns[streamingIndex].llm_text += payload.text;
|
|
|
- }else if(payload.type === "done"){
|
|
|
- console.log(payload);
|
|
|
- streaming = false;
|
|
|
- }
|
|
|
+ if(!lines[i].startsWith("data:")) continue;
|
|
|
+ const data = lines[i].slice(5).trim();
|
|
|
+ if(data === "[DONE]") continue;
|
|
|
+ try{
|
|
|
+ const json = JSON.parse(data);
|
|
|
+ const chunk = json.choices?.[0]?.delta?.content;
|
|
|
+ if(chunk) turns[turns.length-1].llm_text += chunk;
|
|
|
+ }catch{}
|
|
|
}
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
- if(turns.length === 0){
|
|
|
- sendMessage("");
|
|
|
+ streaming = false;
|
|
|
}
|
|
|
+
|
|
|
+ onMount(()=>{
|
|
|
+ if(turns.length === 0){
|
|
|
+ sendMessage("");
|
|
|
+ }
|
|
|
+ });
|
|
|
</script>
|
|
|
|
|
|
<div class="page">
|