Skip to content

Commit 94408cd

Browse files
committed
enhance: ChatGPT stream reception
1 parent 11d1d59 commit 94408cd

File tree

8 files changed

+70
-32
lines changed

8 files changed

+70
-32
lines changed

frontend/components.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ declare module '@vue/runtime-core' {
5656
ATabs: typeof import('ant-design-vue/es')['Tabs']
5757
ATag: typeof import('ant-design-vue/es')['Tag']
5858
ATextarea: typeof import('ant-design-vue/es')['Textarea']
59-
ATooltip: typeof import('ant-design-vue/es')['Tooltip']
6059
BreadcrumbBreadcrumb: typeof import('./src/components/Breadcrumb/Breadcrumb.vue')['default']
6160
ChartAreaChart: typeof import('./src/components/Chart/AreaChart.vue')['default']
6261
ChartRadialBarChart: typeof import('./src/components/Chart/RadialBarChart.vue')['default']

frontend/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "nginx-ui-frontend-next",
33
"private": true,
4-
"version": "1.7.7",
4+
"version": "1.7.8",
55
"type": "commonjs",
66
"scripts": {
77
"dev": "vite",

frontend/src/components/ChatGPT/ChatGPT.vue

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,15 @@ async function request() {
4646
4747
console.log('fetching...')
4848
49+
messages.value.push(t.value)
50+
51+
emit('update:history_messages', messages.value)
52+
4953
let res = await fetch(urlJoin(window.location.pathname, '/api/chat_gpt'), {
5054
method: 'POST',
5155
headers: {'Accept': 'text/event-stream', Authorization: token.value},
52-
body: JSON.stringify({messages: messages.value})
56+
body: JSON.stringify({messages: messages.value.slice(0, messages.value?.length - 1)})
5357
})
54-
55-
messages.value.push(t.value)
5658
// read body as stream
5759
console.log('reading...')
5860
let reader = res.body!.getReader()
@@ -62,6 +64,8 @@ async function request() {
6264
6365
let buffer = ''
6466
67+
let hasCodeBlockIndicator = false
68+
6569
while (true) {
6670
let {done, value} = await reader.read()
6771
if (done) {
@@ -78,17 +82,32 @@ async function request() {
7882
const decoder = new TextDecoder('utf-8')
7983
const raw = decoder.decode(input)
8084
81-
const regex = /{"content":"(.+?)"}/g
82-
const matches = raw.match(regex)
85+
// console.log(input, raw)
86+
87+
const line = raw.split('\n\n')
88+
89+
line?.forEach(v => {
90+
const data = v.slice('event:message\ndata:'.length)
91+
if (!data) {
92+
return
93+
}
94+
const content = JSON.parse(data).content
95+
96+
if (!hasCodeBlockIndicator) {
97+
hasCodeBlockIndicator = content.indexOf('`') > -1
98+
}
8399
84-
matches?.forEach(v => {
85-
const content = JSON.parse(v).content
86100
for (let c of content) {
87101
buffer += c
88-
if (isCodeBlockComplete(buffer)) {
89-
t.value.content = buffer
102+
if (hasCodeBlockIndicator) {
103+
if (isCodeBlockComplete(buffer)) {
104+
t.value.content = buffer
105+
hasCodeBlockIndicator = false
106+
} else {
107+
t.value.content = buffer + '\n```'
108+
}
90109
} else {
91-
t.value.content = buffer + '\n```'
110+
t.value.content = buffer
92111
}
93112
}
94113
})
@@ -167,11 +186,12 @@ async function regenerate(index: number) {
167186
168187
const editing_idx = ref(-1)
169188
170-
const button_shape = computed(() => loading.value ? 'square' : 'circle')
189+
const show = computed(() => messages?.value?.length > 1)
190+
171191
</script>
172192

173193
<template>
174-
<a-card class="chatgpt" title="ChatGPT" v-if="messages?.length>1">
194+
<a-card class="chatgpt" title="ChatGPT" v-if="show">
175195
<div class="chatgpt-container">
176196
<a-list
177197
class="chatgpt-log"

frontend/src/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"1.7.7","build_id":85,"total_build":155}
1+
{"version":"1.7.8","build_id":86,"total_build":156}

frontend/src/views/config/ConfigEdit.vue

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import {message} from 'ant-design-vue'
88
import CodeEditor from '@/components/CodeEditor/CodeEditor.vue'
99
import ngx from '@/api/ngx'
1010
import InspectConfig from '@/views/config/InspectConfig.vue'
11+
import ChatGPT from '@/components/ChatGPT/ChatGPT.vue'
1112
1213
const {$gettext, interpolate} = gettext
1314
const route = useRoute()
@@ -23,16 +24,22 @@ const name = computed(() => {
2324
})
2425
2526
const configText = ref('')
27+
const history_chatgpt_record = ref([])
28+
const file_path = ref('')
2629
2730
function init() {
2831
if (name.value) {
2932
config.get(name.value).then(r => {
3033
configText.value = r.config
34+
history_chatgpt_record.value = r.chatgpt_messages
35+
file_path.value = r.file_path
3136
}).catch(r => {
3237
message.error(r.message ?? $gettext('Server error'))
3338
})
3439
} else {
3540
configText.value = ''
41+
history_chatgpt_record.value = []
42+
file_path.value = ''
3643
}
3744
}
3845
@@ -58,28 +65,39 @@ function format_code() {
5865
})
5966
}
6067
68+
const editor_md = computed(() => history_chatgpt_record?.value?.length > 1 ? 16 : 24)
69+
const chat_md = computed(() => history_chatgpt_record?.value?.length > 1 ? 8 : 24)
70+
6171
</script>
6272

6373

6474
<template>
6575
<inspect-config ref="inspect_config"/>
76+
<a-row :gutter="16">
77+
<a-col :xs="24" :sm="24" :md="editor_md">
78+
<a-card :title="$gettext('Edit Configuration')">
79+
<code-editor v-model:content="configText"/>
80+
<footer-tool-bar>
81+
<a-space>
82+
<a-button @click="$router.go(-1)">
83+
<translate>Back</translate>
84+
</a-button>
85+
<a-button @click="format_code">
86+
<translate>Format Code</translate>
87+
</a-button>
88+
<a-button type="primary" @click="save">
89+
<translate>Save</translate>
90+
</a-button>
91+
</a-space>
92+
</footer-tool-bar>
93+
</a-card>
94+
</a-col>
6695

67-
<a-card :title="$gettext('Edit Configuration')">
68-
<code-editor v-model:content="configText"/>
69-
<footer-tool-bar>
70-
<a-space>
71-
<a-button @click="$router.go(-1)">
72-
<translate>Back</translate>
73-
</a-button>
74-
<a-button @click="format_code">
75-
<translate>Format Code</translate>
76-
</a-button>
77-
<a-button type="primary" @click="save">
78-
<translate>Save</translate>
79-
</a-button>
80-
</a-space>
81-
</footer-tool-bar>
82-
</a-card>
96+
<a-col class="col-right" :xs="24" :sm="24" :md="chat_md">
97+
<chat-g-p-t :content="configText" :path="file_path"
98+
v-model:history_messages="history_chatgpt_record"/>
99+
</a-col>
100+
</a-row>
83101
</template>
84102

85103
<style lang="less" scoped>

frontend/version.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"version":"1.7.7","build_id":85,"total_build":155}
1+
{"version":"1.7.8","build_id":86,"total_build":156}

resources/demo/demo.db

0 Bytes
Binary file not shown.

server/api/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ func GetConfig(c *gin.Context) {
9696
c.JSON(http.StatusOK, gin.H{
9797
"config": string(content),
9898
"chatgpt_messages": chatgpt.Content,
99+
"file_path": path,
99100
})
100101

101102
}

0 commit comments

Comments
 (0)