Skip to content

Commit

Permalink
not using @microsoft/fetch-event-source, we write our own
Browse files Browse the repository at this point in the history
Signed-off-by: cbh778899 <[email protected]>
  • Loading branch information
cbh778899 committed Jul 8, 2024
1 parent 6b3f6eb commit 26277ae
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
50 changes: 29 additions & 21 deletions components/chat-page/chatMain.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import useConversation from "../../global/useConversation.js";
import request, { streamRequest } from "../../tools/request.js";
import request from "../../tools/request.js";

let conversation = {}, main_elem;

Expand All @@ -8,7 +8,7 @@ const {
sendMessage:appendConversationMessage
} = useConversation(c=>{
if(c.id === conversation.id) return;
conversation = {...c};
conversation = c;
if(!conversation.id) return;

updateConversation();
Expand Down Expand Up @@ -65,8 +65,14 @@ async function sendMessage(message, send) {
main_elem.appendChild(createBlock('out', message)[0]);
const [bot_answer, bot_answer_message] = createBlock('in');
main_elem.appendChild(bot_answer);
bot_answer.focus();

const content = await send(bot_answer_message);
const response = await request('chat', {
method: 'POST',
body: { sessionUuid: conversation.id || "uuid", message }
})

const content = await send(response, bot_answer_message);

appendConversationMessage([
{ type: 'out', message },
Expand All @@ -75,33 +81,35 @@ async function sendMessage(message, send) {
}

function sendMessageWaiting(msg) {
return sendMessage(msg, async pending_elem => {
const response = await request('chat', {
method: 'POST',
body: { sessionUuid: conversation.id || "uuid", message }
})

return sendMessage(msg, async (response, pending_elem) => {
const { message } = await response.json();
pending_elem.textContent = message;
return message;
})
}

async function sendMessageStream(msg) {
return sendMessage(msg, async pending_elem => {
return sendMessage(msg, async (response, pending_elem) => {
let resp_content = ''
await streamRequest('chat', {
method: 'POST',
body: {
sessionUuid: conversation.id,
message: msg
const reader = response.body.pipeThrough(new TextDecoderStream()).getReader();
let pending_content = ''
while(true) {
const {value, done} = await reader.read();
if(done) break;
pending_content += value;
if(pending_content.includes('\n\n')) {
const splitted_content = pending_content.split('\n\n')
try {
const json = JSON.parse(splitted_content.shift().replace('data: ', ''))
resp_content += json.content;
pending_elem.textContent = resp_content;
pending_content = splitted_content.join('')
if(json.stop) break;
} catch(error) {
console.error(error);
}
}
}, res=>{
const { content } = JSON.parse(res.data);
resp_content += content;
pending_elem.textContent = resp_content;
})
return resp_content;
}
})
}

Expand Down
5 changes: 3 additions & 2 deletions global/useConversation.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ const defaultConversationSetting = {
}

let currentConversation = {
...defaultConversationSetting
...defaultConversationSetting,
history: []
};

const { onmount, remount, dismount, updateAll } = createHook();
Expand All @@ -26,7 +27,7 @@ export default function useConversation(updated) {
const { sessionUuid } = await (await request('chat/seesionuuid')).json();
currentConversation = {
...defaultConversationSetting,
id: sessionUuid
id: sessionUuid, history: []
};
addHistory({
id: currentConversation.id,
Expand Down
9 changes: 0 additions & 9 deletions tools/request.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { API_ADDRESS } from "../settings.js";
import useSessionId from "../global/useSessionId.js";
import { fetchEventSource } from 'https://cdn.jsdelivr.net/npm/@microsoft/[email protected]/+esm'

let session_id = '';
useSessionId(id=>{session_id = id});
Expand All @@ -9,14 +8,6 @@ export default function request(url, options={}) {
return fetch(reqAddress(url), generateRequest(url, options))
}

export function streamRequest(url, options={}, onmessage) {
return fetchEventSource(reqAddress(url), {
...generateRequest(url, options),
onmessage,
signal: AbortSignal.timeout(5000)
})
}

export function reqAddress(url) {
return `${API_ADDRESS}/${url}`
}
Expand Down

0 comments on commit 26277ae

Please sign in to comment.