From a1d7e81859f554f3a53680cc35f0f49bf1f77098 Mon Sep 17 00:00:00 2001
From: wwf <1971391498@qq.com>
Date: 星期四, 14 五月 2026 14:37:02 +0800
Subject: [PATCH] 导入项目
---
src/views/ai/write/index/index.vue | 78 +++++++++++++++++++++++++++++++++++++++
1 files changed, 78 insertions(+), 0 deletions(-)
diff --git a/src/views/ai/write/index/index.vue b/src/views/ai/write/index/index.vue
new file mode 100644
index 0000000..0079eed
--- /dev/null
+++ b/src/views/ai/write/index/index.vue
@@ -0,0 +1,78 @@
+<template>
+ <div class="absolute top-0 left-0 right-0 bottom-0 flex">
+ <Left
+ :is-writing="isWriting"
+ class="h-full"
+ @submit="submit"
+ @reset="reset"
+ @example="handleExampleClick"
+ />
+ <Right
+ :is-writing="isWriting"
+ @stop-stream="stopStream"
+ ref="rightRef"
+ class="flex-grow"
+ v-model:content="writeResult"
+ />
+ </div>
+</template>
+
+<script setup lang="ts">
+import Left from './components/Left.vue'
+import Right from './components/Right.vue'
+import { WriteApi, WriteVO } from '@/api/ai/write'
+import { WriteExample } from '@/views/ai/utils/constants'
+
+const message = useMessage()
+
+const writeResult = ref('') // 鍐欎綔缁撴灉
+const isWriting = ref(false) // 鏄惁姝e湪鍐欎綔涓�
+const abortController = ref<AbortController>() // // 鍐欎綔杩涜涓� abort 鎺у埗鍣�(鎺у埗 stream 鍐欎綔)
+
+/** 鍋滄 stream 鐢熸垚 */
+const stopStream = () => {
+ abortController.value?.abort()
+ isWriting.value = false
+}
+
+/** 鎵ц鍐欎綔 */
+const rightRef = ref<InstanceType<typeof Right>>()
+const submit = (data: WriteVO) => {
+ abortController.value = new AbortController()
+ writeResult.value = ''
+ isWriting.value = true
+ WriteApi.writeStream({
+ data,
+ onMessage: async (res) => {
+ const { code, data, msg } = JSON.parse(res.data)
+ if (code !== 0) {
+ message.alert(`鍐欎綔寮傚父! ${msg}`)
+ stopStream()
+ return
+ }
+ writeResult.value = writeResult.value + data
+ // 婊氬姩鍒板簳閮�
+ await nextTick()
+ rightRef.value?.scrollToBottom()
+ },
+ ctrl: abortController.value,
+ onClose: stopStream,
+ onError: (error) => {
+ console.error('鍐欎綔寮傚父', error)
+ stopStream()
+ // 闇�瑕佹姏鍑哄紓甯革紝绂佹閲嶈瘯
+ throw error
+ }
+ })
+}
+
+/** 鐐瑰嚮绀轰緥瑙﹀彂 */
+const handleExampleClick = (type: keyof typeof WriteExample) => {
+ writeResult.value = WriteExample[type].data
+}
+
+/** 鐐瑰嚮閲嶇疆鐨勬椂鍊欐竻绌哄啓浣滅殑缁撴灉**/
+const reset = () => {
+ writeResult.value = ''
+}
+</script>
--
Gitblit v1.8.0