(question, ragMode = false)
| 39 | } |
| 40 | |
| 41 | async function send(question, ragMode = false) { |
| 42 | if (!question.trim().length) { |
| 43 | return; |
| 44 | } |
| 45 | update((state) => ({ ...state, state: chatStates.RECEIVING })); |
| 46 | addMessage("me", question, ragMode); |
| 47 | const messageId = addMessage("bot", "", ragMode); |
| 48 | try { |
| 49 | const evt = new EventSource(`${API_ENDPOINT}?text=${encodeURI(question)}&rag=${ragMode}`); |
| 50 | question = ""; |
| 51 | evt.onmessage = (e) => { |
| 52 | if (e.data) { |
| 53 | const data = JSON.parse(e.data); |
| 54 | if (data.init) { |
| 55 | updateMessage(messageId, "", data.model); |
| 56 | return; |
| 57 | } |
| 58 | updateMessage(messageId, data.token); |
| 59 | } |
| 60 | }; |
| 61 | evt.onerror = (e) => { |
| 62 | // Stream will end with an error |
| 63 | // and we want to close the connection on end (otherwise it will keep reconnecting) |
| 64 | evt.close(); |
| 65 | update((state) => ({ ...state, state: chatStates.IDLE })); |
| 66 | }; |
| 67 | } catch (e) { |
| 68 | updateMessage(messageId, "Error: " + e.message); |
| 69 | update((state) => ({ ...state, state: chatStates.IDLE })); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | return { |
| 74 | subscribe, |
nothing calls this directly
no test coverage detected