Compare commits
1 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5201349363 |
@@ -8,6 +8,7 @@
|
|||||||
:disabled="disabled"
|
:disabled="disabled"
|
||||||
:loading="loading"
|
:loading="loading"
|
||||||
:hint="hint"
|
:hint="hint"
|
||||||
|
:hide-details="loading"
|
||||||
append-inner-icon="send"
|
append-inner-icon="send"
|
||||||
@keyup.enter.exact="enterOnly"
|
@keyup.enter.exact="enterOnly"
|
||||||
@click:appendInner="clickSendBtn"
|
@click:appendInner="clickSendBtn"
|
||||||
|
|||||||
@@ -18,6 +18,7 @@
|
|||||||
"highlight.js": "^11.7.0",
|
"highlight.js": "^11.7.0",
|
||||||
"is-mobile": "^3.1.1",
|
"is-mobile": "^3.1.1",
|
||||||
"marked": "^4.2.12",
|
"marked": "^4.2.12",
|
||||||
|
"nanoid": "^4.0.1",
|
||||||
"vuetify": "^3.0.6"
|
"vuetify": "^3.0.6"
|
||||||
},
|
},
|
||||||
"license": "MIT"
|
"license": "MIT"
|
||||||
|
|||||||
@@ -1,12 +1,20 @@
|
|||||||
<script setup>
|
<script setup>
|
||||||
import { fetchEventSource } from '@microsoft/fetch-event-source'
|
import {EventStreamContentType, fetchEventSource} from '@microsoft/fetch-event-source'
|
||||||
|
|
||||||
const runtimeConfig = useRuntimeConfig()
|
const runtimeConfig = useRuntimeConfig()
|
||||||
const currentModel = useCurrentModel()
|
const currentModel = useCurrentModel()
|
||||||
const openaiApiKey = useApiKey()
|
const openaiApiKey = useApiKey()
|
||||||
const fetchingResponse = ref(false)
|
const fetchingResponse = ref(false)
|
||||||
|
|
||||||
|
let ctrl
|
||||||
|
const abortFetch = () => {
|
||||||
|
if (ctrl) {
|
||||||
|
ctrl.abort()
|
||||||
|
}
|
||||||
|
fetchingResponse.value = false
|
||||||
|
}
|
||||||
const fetchReply = async (message, parentMessageId) => {
|
const fetchReply = async (message, parentMessageId) => {
|
||||||
const ctrl = new AbortController()
|
ctrl = new AbortController()
|
||||||
try {
|
try {
|
||||||
await fetchEventSource('/api/conversation', {
|
await fetchEventSource('/api/conversation', {
|
||||||
signal: ctrl.signal,
|
signal: ctrl.signal,
|
||||||
@@ -22,43 +30,50 @@ const fetchReply = async (message, parentMessageId) => {
|
|||||||
conversationId: currentConversation.value.id
|
conversationId: currentConversation.value.id
|
||||||
}),
|
}),
|
||||||
onopen(response) {
|
onopen(response) {
|
||||||
if (response.status === 200) {
|
if (response.ok && response.headers.get('content-type') === EventStreamContentType) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
throw new Error(`Failed to send message. HTTP ${response.status} - ${response.statusText}`);
|
throw new Error(`Failed to send message. HTTP ${response.status} - ${response.statusText}`);
|
||||||
},
|
},
|
||||||
onclose() {
|
onclose() {
|
||||||
|
if (ctrl.signal.aborted === true) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
throw new Error(`Failed to send message. Server closed the connection unexpectedly.`);
|
throw new Error(`Failed to send message. Server closed the connection unexpectedly.`);
|
||||||
},
|
},
|
||||||
onerror(err) {
|
onerror(err) {
|
||||||
throw err;
|
throw err;
|
||||||
},
|
},
|
||||||
onmessage(message) {
|
onmessage(message) {
|
||||||
if (message.event === 'error') {
|
const event = message.event
|
||||||
throw new Error(JSON.parse(message.data).error);
|
const data = JSON.parse(message.data)
|
||||||
|
|
||||||
|
if (event === 'error') {
|
||||||
|
throw new Error(data.error);
|
||||||
}
|
}
|
||||||
const { type, data } = JSON.parse(message.data);
|
|
||||||
if (type === 'done') {
|
if (event === 'done') {
|
||||||
if (currentConversation.value.id === null) {
|
if (currentConversation.value.id === null) {
|
||||||
currentConversation.value.id = data.conversationId
|
currentConversation.value.id = data.conversationId
|
||||||
}
|
}
|
||||||
currentConversation.value.messages[currentConversation.value.messages.length - 1].id = data.messageId
|
currentConversation.value.messages[currentConversation.value.messages.length - 1].id = data.messageId
|
||||||
ctrl.abort();
|
abortFetch()
|
||||||
fetchingResponse.value = false
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (currentConversation.value.messages[currentConversation.value.messages.length - 1].from === 'ai') {
|
if (currentConversation.value.messages[currentConversation.value.messages.length - 1].from === 'ai') {
|
||||||
currentConversation.value.messages[currentConversation.value.messages.length - 1].message += data
|
currentConversation.value.messages[currentConversation.value.messages.length - 1].message += data.content
|
||||||
} else {
|
} else {
|
||||||
currentConversation.value.messages.push({id: null, from: 'ai', message: data})
|
currentConversation.value.messages.push({id: null, from: 'ai', message: data.content})
|
||||||
}
|
}
|
||||||
|
|
||||||
scrollChatWindow()
|
scrollChatWindow()
|
||||||
},
|
},
|
||||||
})
|
})
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
ctrl.abort()
|
console.log(err)
|
||||||
|
abortFetch()
|
||||||
showSnackbar(err.message)
|
showSnackbar(err.message)
|
||||||
fetchingResponse.value = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -70,6 +85,9 @@ const currentConversation = ref({})
|
|||||||
|
|
||||||
const grab = ref(null)
|
const grab = ref(null)
|
||||||
const scrollChatWindow = () => {
|
const scrollChatWindow = () => {
|
||||||
|
if (grab.value === null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
grab.value.scrollIntoView({behavior: 'smooth'})
|
grab.value.scrollIntoView({behavior: 'smooth'})
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -91,8 +109,7 @@ const send = (message) => {
|
|||||||
scrollChatWindow()
|
scrollChatWindow()
|
||||||
}
|
}
|
||||||
const stop = () => {
|
const stop = () => {
|
||||||
ctrl.abort();
|
abortFetch()
|
||||||
fetchingResponse.value = false
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const snackbar = ref(false)
|
const snackbar = ref(false)
|
||||||
@@ -125,7 +142,7 @@ createNewConversation()
|
|||||||
</v-container>
|
</v-container>
|
||||||
<v-divider></v-divider>
|
<v-divider></v-divider>
|
||||||
</v-card>
|
</v-card>
|
||||||
<div ref="grab" class="w-100" style="height: 150px;"></div>
|
<div ref="grab" class="w-100" style="height: 200px;"></div>
|
||||||
</div>
|
</div>
|
||||||
<Welcome v-else />
|
<Welcome v-else />
|
||||||
<v-footer app class="d-flex flex-column">
|
<v-footer app class="d-flex flex-column">
|
||||||
|
|||||||
@@ -1,25 +1,13 @@
|
|||||||
import ChatGPTClient from '@waylaidwanderer/chatgpt-api'
|
import ChatGPTClient from '@waylaidwanderer/chatgpt-api'
|
||||||
import { PassThrough } from 'node:stream'
|
import { PassThrough } from 'node:stream'
|
||||||
|
import { nanoid } from 'nanoid'
|
||||||
|
|
||||||
const serializeSSEEvent = (chunk) => {
|
const serializeSSEEvent = (event, data) => {
|
||||||
let payload = "";
|
const id = nanoid();
|
||||||
if (chunk.id) {
|
const eventStr = event ? `event: ${event}\n` : '';
|
||||||
payload += `id: ${chunk.id}\n`;
|
const dataStr = data ? `data: ${JSON.stringify(data)}\n` : '';
|
||||||
}
|
|
||||||
if (chunk.event) {
|
return `id: ${id}\n${eventStr}${dataStr}\n`;
|
||||||
payload += `event: ${chunk.event}\n`;
|
|
||||||
}
|
|
||||||
if (chunk.data) {
|
|
||||||
payload += `data: ${chunk.data}\n`;
|
|
||||||
}
|
|
||||||
if (chunk.retry) {
|
|
||||||
payload += `retry: ${chunk.retry}\n`;
|
|
||||||
}
|
|
||||||
if (!payload) {
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
payload += "\n";
|
|
||||||
return payload;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defineEventHandler(async (event) => {
|
export default defineEventHandler(async (event) => {
|
||||||
@@ -27,9 +15,13 @@ export default defineEventHandler(async (event) => {
|
|||||||
const conversationId = body.conversationId ? body.conversationId.toString() : undefined
|
const conversationId = body.conversationId ? body.conversationId.toString() : undefined
|
||||||
const parentMessageId = body.parentMessageId ? body.parentMessageId.toString() : undefined
|
const parentMessageId = body.parentMessageId ? body.parentMessageId.toString() : undefined
|
||||||
const tunnel = new PassThrough()
|
const tunnel = new PassThrough()
|
||||||
const writeToTunnel = (data) => {
|
const writeToTunnel = (event, data) => {
|
||||||
tunnel.write(serializeSSEEvent(data))
|
tunnel.write(serializeSSEEvent(event, data))
|
||||||
}
|
}
|
||||||
|
const endTunnel = () => {
|
||||||
|
tunnel.end()
|
||||||
|
}
|
||||||
|
|
||||||
setResponseHeaders(event, {
|
setResponseHeaders(event, {
|
||||||
'Content-Type': 'text/event-stream',
|
'Content-Type': 'text/event-stream',
|
||||||
'Cache-Control': 'no-cache',
|
'Cache-Control': 'no-cache',
|
||||||
@@ -37,13 +29,11 @@ export default defineEventHandler(async (event) => {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if (!body.openaiApiKey) {
|
if (!body.openaiApiKey) {
|
||||||
writeToTunnel({
|
writeToTunnel('error', {
|
||||||
event: 'error',
|
code: 503,
|
||||||
data: JSON.stringify({
|
error: 'You haven\'t set the api key of openai',
|
||||||
code: 503,
|
|
||||||
error: 'You haven\'t set the api key of openai',
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
|
endTunnel()
|
||||||
return sendStream(event, tunnel)
|
return sendStream(event, tunnel)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -79,29 +69,19 @@ export default defineEventHandler(async (event) => {
|
|||||||
parentMessageId,
|
parentMessageId,
|
||||||
onProgress: (token) => {
|
onProgress: (token) => {
|
||||||
// console.log(token)
|
// console.log(token)
|
||||||
writeToTunnel({ data: JSON.stringify({
|
writeToTunnel('message',{content: token})
|
||||||
type: 'token',
|
|
||||||
data: token
|
|
||||||
})
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
writeToTunnel({ data: JSON.stringify({
|
writeToTunnel('done',response)
|
||||||
type: 'done',
|
console.info(response)
|
||||||
data: response
|
|
||||||
}) })
|
|
||||||
console.log(response)
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
const code = e?.json?.data?.code || 503;
|
const code = e?.json?.data?.code || 503;
|
||||||
const message = e?.json?.error?.message || 'There was an error communicating with ChatGPT.';
|
const message = e?.json?.error?.message || 'There was an error communicating with ChatGPT.';
|
||||||
writeToTunnel({
|
writeToTunnel('error', {
|
||||||
event: 'error',
|
code,
|
||||||
data: JSON.stringify({
|
error: message
|
||||||
code,
|
|
||||||
error: message,
|
|
||||||
}),
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
tunnel.end()
|
||||||
return sendStream(event, tunnel)
|
return sendStream(event, tunnel)
|
||||||
})
|
})
|
||||||
@@ -3266,7 +3266,7 @@ nanoid@^3.3.4:
|
|||||||
resolved "https://registry.npmmirror.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
|
resolved "https://registry.npmmirror.com/nanoid/-/nanoid-3.3.4.tgz#730b67e3cd09e2deacf03c027c81c9d9dbc5e8ab"
|
||||||
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
|
integrity sha512-MqBkQh/OHTS2egovRtLk45wEyNXwF+cokD+1YPf9u5VfJiRdAiRwB2froX5Co9Rh20xs4siNPm8naNotSD6RBw==
|
||||||
|
|
||||||
nanoid@^4.0.0:
|
nanoid@^4.0.0, nanoid@^4.0.1:
|
||||||
version "4.0.1"
|
version "4.0.1"
|
||||||
resolved "https://registry.npmmirror.com/nanoid/-/nanoid-4.0.1.tgz#398d7ccfdbf9faf2231b2ca7e8fff5dbca6a509b"
|
resolved "https://registry.npmmirror.com/nanoid/-/nanoid-4.0.1.tgz#398d7ccfdbf9faf2231b2ca7e8fff5dbca6a509b"
|
||||||
integrity sha512-udKGtCCUafD3nQtJg9wBhRP3KMbPglUsgV5JVsXhvyBs/oefqb4sqMEhKBBgqZncYowu58p1prsZQBYvAj/Gww==
|
integrity sha512-udKGtCCUafD3nQtJg9wBhRP3KMbPglUsgV5JVsXhvyBs/oefqb4sqMEhKBBgqZncYowu58p1prsZQBYvAj/Gww==
|
||||||
|
|||||||
Reference in New Issue
Block a user