-
Notifications
You must be signed in to change notification settings - Fork 13
/
chat.vue
274 lines (232 loc) · 6.17 KB
/
chat.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<script lang="tsx" setup>
import { LLMTypes, type TransformStreamModelTypes } from '@/components/MarkdownPreview/transform'
import { isMockDevelopment } from '@/config'
import { type InputInst } from 'naive-ui'
import { UAParser } from 'ua-parser-js'
const route = useRoute()
const router = useRouter()
const businessStore = useBusinessStore()
/**
* 默认大模型
*/
const defaultLLMTypeName: TransformStreamModelTypes = isMockDevelopment
? 'standard'
: 'moonshot'
const currentLLMType = computed(() => {
return LLMTypes.find(v => v.modelName === defaultLLMTypeName)
})
const currentChatId = computed(() => {
return route.params.chatId
})
const loading = ref(true)
setTimeout(() => {
loading.value = false
}, 700)
const stylizingLoading = ref(false)
/**
* 输入字符串
*/
const inputTextString = ref('')
const refInputTextString = ref<InputInst | null>()
/**
* 输出字符串 Reader 流(风格化的)
*/
const outputTextReader = ref<ReadableStreamDefaultReader | null>()
const refReaderMarkdownPreview = ref<any>()
const onFailedReader = () => {
outputTextReader.value = null
stylizingLoading.value = false
if (refReaderMarkdownPreview.value) {
refReaderMarkdownPreview.value.initializeEnd()
}
window.$ModalMessage.error('转换失败,请重试')
setTimeout(() => {
if (refInputTextString.value) {
refInputTextString.value.focus()
}
})
}
const onCompletedReader = () => {
stylizingLoading.value = false
setTimeout(() => {
if (refInputTextString.value) {
refInputTextString.value.focus()
}
})
}
const handleCreateStylized = async () => {
// 若正在加载,则点击后恢复初始状态
if (stylizingLoading.value) {
refReaderMarkdownPreview.value.abortReader()
onCompletedReader()
return
}
if (refInputTextString.value && !inputTextString.value.trim()) {
inputTextString.value = ''
refInputTextString.value.focus()
return
}
refReaderMarkdownPreview.value.resetStatus()
refReaderMarkdownPreview.value.initializeStart()
stylizingLoading.value = true
const textContent = inputTextString.value
inputTextString.value = ''
const { error, reader } = await businessStore.createAssistantWriterStylized(currentChatId.value, {
text: textContent,
writer_oid: currentChatId.value
})
if (error) {
onFailedReader()
return
}
if (reader) {
outputTextReader.value = reader
}
}
const keys = useMagicKeys()
const enterCommand = keys['Meta+Enter']
const enterCtrl = keys['Ctrl+Enter']
const activeElement = useActiveElement()
const notUsingInput = computed(() => activeElement.value?.tagName !== 'TEXTAREA')
const parser = new UAParser()
const isMacos = computed(() => {
const os = parser.getOS()
if (!os) return
return os.name?.includes?.('macos')
})
const placeholder = computed(() => {
if (stylizingLoading.value) {
return `输入任意问题...`
}
return `输入任意问题, 按 ${ isMacos ? 'Command' : 'Ctrl' } + Enter 键快捷开始...`
})
watch(
() => enterCommand.value,
() => {
if (!isMacos || notUsingInput.value) return
if (stylizingLoading.value) return
if (!enterCommand.value) {
handleCreateStylized()
}
},
{
deep: true
}
)
watch(
() => enterCtrl.value,
() => {
if (isMacos || notUsingInput.value) return
if (stylizingLoading.value) return
if (!enterCtrl.value) {
handleCreateStylized()
}
},
{
deep: true
}
)
const handleResetState = () => {
if (isMockDevelopment) {
inputTextString.value = ''
} else {
inputTextString.value = '使用中文,回答以下两个问题,分段表示\n1、你是什么模型?\n2、分别使用 Vue3 setup Composition API 语法糖、React 语法编写一个 Button 组件'
}
stylizingLoading.value = false
nextTick(() => {
refInputTextString.value?.focus()
})
refReaderMarkdownPreview.value?.abortReader()
refReaderMarkdownPreview.value?.resetStatus()
}
handleResetState()
</script>
<template>
<LayoutCenterPanel
:loading="loading"
>
<!-- 内容区域 -->
<div
flex="~ col"
h-full
>
<div
flex="~ justify-between items-center"
>
<NavigationNavBar>
<template #bottom>
<div class="pt-10 text-16">
<span>当前模型:</span>
<span class="font-italic font-bold">{{ currentLLMType?.label }}</span>
</div>
</template>
</NavigationNavBar>
</div>
<div
flex="1 ~ col"
min-h-0
pb-20
>
<MarkdownPreview
ref="refReaderMarkdownPreview"
v-model:reader="outputTextReader"
:model="defaultLLMTypeName"
@failed="onFailedReader"
@completed="onCompletedReader"
/>
</div>
<div
flex="~ col items-center"
flex-basis="10%"
p="14px"
py="0"
>
<div
relative
flex="1"
w-full
px-1em
>
<n-input
ref="refInputTextString"
v-model:value="inputTextString"
type="textarea"
autofocus
h-full
class="textarea-resize-none text-15"
:style="{
'--n-border-radius': '20px',
'--n-padding-left': '20px',
'--n-padding-right': '20px',
'--n-padding-vertical': '10px',
}"
:placeholder="placeholder"
/>
<n-float-button
position="absolute"
:right="40"
bottom="50%"
:type="stylizingLoading ? 'primary' : 'default'"
color
:class="[
stylizingLoading && 'opacity-90',
'translate-y-50%'
]"
@click.stop="handleCreateStylized()"
>
<div
v-if="stylizingLoading"
class="i-svg-spinners:pulse-2 c-#fff"
></div>
<div
v-else
class="transform-rotate-z--90 text-22 c-#303133/70 i-hugeicons:start-up-02"
></div>
</n-float-button>
</div>
</div>
</div>
</LayoutCenterPanel>
</template>
<style lang="scss" scoped>
</style>