Skip to content

Commit 2b07891

Browse files
committed
【代码评审】AI:写作相关的建议
1 parent 96a499a commit 2b07891

File tree

6 files changed

+204
-192
lines changed

6 files changed

+204
-192
lines changed

src/api/ai/writer/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@ import { fetchEventSource } from '@microsoft/fetch-event-source'
33
import { getAccessToken } from '@/utils/auth'
44
import { config } from '@/config/axios/config'
55

6+
// TODO @hhhero:可以改成 WriteVO 哈,主要是保持一致
67
export interface WriteParams {
8+
// TODO @hhhero:注释。每个属性的后面哈。会更简洁一点
79
/**
810
* 1:撰写 2:回复
911
*/
@@ -33,6 +35,7 @@ export interface WriteParams {
3335
*/
3436
language: number
3537
}
38+
3639
export const writeStream = ({
3740
data,
3841
onClose,
@@ -46,7 +49,6 @@ export const writeStream = ({
4649
onClose?: (...args: any[]) => void
4750
ctrl: AbortController
4851
}) => {
49-
// return request.post({ url: '/ai/write/generate-stream', data })
5052
const token = getAccessToken()
5153
return fetchEventSource(`${config.base_url}/ai/write/generate-stream`, {
5254
method: 'post',

src/views/ai/chat/index/components/conversation/ConversationList.vue

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,7 @@ const conversationTimeGroup = async (list: ChatConversationVO[]) => {
226226
const threeDays = 3 * oneDay
227227
const sevenDays = 7 * oneDay
228228
const thirtyDays = 30 * oneDay
229-
for (const conversation: ChatConversationVO of list) {
229+
for (const conversation of list) {
230230
// 置顶
231231
if (conversation.pinned) {
232232
groupMap['置顶'].push(conversation)
@@ -247,7 +247,6 @@ const conversationTimeGroup = async (list: ChatConversationVO[]) => {
247247
groupMap['三十天前'].push(conversation)
248248
}
249249
}
250-
console.log('----groupMap', groupMap)
251250
return groupMap
252251
}
253252
Lines changed: 98 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<template>
2-
<!-- 定义tab组件 -->
2+
<!-- 定义 tab 组件:撰写/回复等 -->
33
<DefineTab v-slot="{ active, text, itemClick }">
44
<span
55
class="inline-block w-1/2 rounded-full cursor-pointer text-center leading-[30px] relative z-1 text-[5C6370] hover:text-black"
@@ -9,7 +9,7 @@
99
{{ text }}
1010
</span>
1111
</DefineTab>
12-
<!-- 定义label组件 -->
12+
<!-- 定义 label 组件:长度/格式/语气/语言等 -->
1313
<DefineLabel v-slot="{ label, hint, hintClick }">
1414
<h3 class="mt-5 mb-3 flex items-center justify-between text-[14px]">
1515
<span>{{ label }}</span>
@@ -23,6 +23,7 @@
2323
</span>
2424
</h3>
2525
</DefineLabel>
26+
2627
<!-- TODO 小屏幕的时候是定位在左边的,大屏是分开的 -->
2728
<div class="relative" v-bind="$attrs">
2829
<!-- tab -->
@@ -99,97 +100,102 @@
99100
</template>
100101

101102
<script setup lang="ts">
102-
import { createReusableTemplate } from '@vueuse/core'
103-
import { ref } from 'vue'
104-
import Tag from './Tag.vue'
105-
import { WriteParams } from '@/api/ai/writer'
106-
import { omit } from 'lodash-es'
107-
import { getIntDictOptions } from '@/utils/dict'
108-
import dataJson from '../data.json'
109-
110-
type TabType = WriteParams['type']
111-
112-
const message = useMessage()
113-
114-
defineProps<{
115-
isWriting: boolean
116-
}>()
117-
118-
const emits = defineEmits<{
119-
(e: 'submit', params: Partial<WriteParams>)
120-
(e: 'example', param: 'write' | 'reply')
121-
}>()
122-
123-
const example = (type: 'write' | 'reply') => {
124-
writeForm.value = {
125-
...initData,
126-
...omit(dataJson[type], ['data'])
127-
}
128-
emits('example', type)
129-
}
130-
131-
const selectedTab = ref<TabType>(1)
132-
const tabs: {
133-
text: string
134-
value: TabType
135-
}[] = [
136-
{ text: '撰写', value: 1 },
137-
{ text: '回复', value: 2 }
138-
]
139-
const [DefineTab, ReuseTab] = createReusableTemplate<{
140-
active?: boolean
141-
text: string
142-
itemClick: () => void
143-
}>()
144-
145-
const initData: WriteParams = {
146-
type: 1,
147-
prompt: '',
148-
originalContent: '',
149-
tone: 1,
150-
language: 1,
151-
length: 1,
152-
format: 1
153-
}
154-
const writeForm = ref<WriteParams>({ ...initData })
155-
156-
const writeTags = {
157-
// 长度
158-
lenTags: getIntDictOptions('ai_write_length'),
159-
// 格式
160-
161-
formatTags: getIntDictOptions('ai_write_format'),
162-
// 语气
163-
164-
toneTags: getIntDictOptions('ai_write_tone'),
165-
// 语言
166-
langTags: getIntDictOptions('ai_write_language')
167-
//
103+
import { createReusableTemplate } from '@vueuse/core'
104+
import { ref } from 'vue'
105+
import Tag from './Tag.vue'
106+
import { WriteParams } from '@/api/ai/writer'
107+
import { omit } from 'lodash-es'
108+
import { getIntDictOptions } from '@/utils/dict'
109+
import dataJson from '../data.json'
110+
111+
type TabType = WriteParams['type']
112+
113+
const message = useMessage()
114+
115+
defineProps<{
116+
isWriting: boolean
117+
}>()
118+
119+
const emits = defineEmits<{
120+
(e: 'submit', params: Partial<WriteParams>)
121+
(e: 'example', param: 'write' | 'reply')
122+
}>()
123+
124+
const example = (type: 'write' | 'reply') => {
125+
writeForm.value = {
126+
...initData,
127+
...omit(dataJson[type], ['data'])
168128
}
169-
170-
const [DefineLabel, ReuseLabel] = createReusableTemplate<{
171-
label: string
172-
class?: string
173-
hint?: string
174-
hintClick?: () => void
175-
}>()
176-
177-
const switchTab = (value: TabType) => {
178-
selectedTab.value = value
179-
writeForm.value = { ...initData }
129+
emits('example', type)
130+
}
131+
132+
const selectedTab = ref<TabType>(1)
133+
const tabs: {
134+
text: string
135+
value: TabType
136+
}[] = [
137+
{ text: '撰写', value: 1 }, // TODO @hhhero:1、2 这个枚举到 constants 里。方便后续万一要调整
138+
{ text: '回复', value: 2 }
139+
]
140+
const [DefineTab, ReuseTab] = createReusableTemplate<{
141+
active?: boolean
142+
text: string
143+
itemClick: () => void
144+
}>()
145+
146+
const initData: WriteParams = {
147+
type: 1,
148+
prompt: '',
149+
originalContent: '',
150+
tone: 1,
151+
language: 1,
152+
length: 1,
153+
format: 1
154+
}
155+
// TODO @hhhero:这个字段,要不叫 formData,和其他模块保持一致。然后 initData 和它也更好对应上
156+
const writeForm = ref<WriteParams>({ ...initData })
157+
158+
// TODO @hhhero:这种一次性的变量,要不直接 vue template 直接调用。目的是:让 ts 这块,更专注逻辑哈。
159+
const writeTags = {
160+
// 长度 TODO @hhhero:注释放在和面哈;
161+
// TODO @hhhero:一般 length 不用缩写哈。更完整会更容易阅读;
162+
lenTags: getIntDictOptions('ai_write_length'),
163+
// 格式
164+
165+
formatTags: getIntDictOptions('ai_write_format'),
166+
// 语气
167+
168+
toneTags: getIntDictOptions('ai_write_tone'),
169+
// 语言
170+
langTags: getIntDictOptions('ai_write_language')
171+
//
172+
}
173+
174+
// TODO @hhhero:这个写法不错。要不写个简单的注释,我怕很多人不懂哈。
175+
const [DefineLabel, ReuseLabel] = createReusableTemplate<{
176+
label: string
177+
class?: string
178+
hint?: string
179+
hintClick?: () => void
180+
}>()
181+
182+
const switchTab = (value: TabType) => {
183+
selectedTab.value = value
184+
writeForm.value = { ...initData }
185+
}
186+
187+
const submit = () => {
188+
if (selectedTab.value === 2 && !writeForm.value.originalContent) {
189+
message.warning('请输入原文')
190+
return
180191
}
181-
182-
const submit = () => {
183-
if (selectedTab.value === 2 && !writeForm.value.originalContent) {
184-
message.warning('请输入原文')
185-
return
186-
} else if (!writeForm.value.prompt) {
187-
message.warning(`请输入${selectedTab.value === 1 ? '写作' : '回复'}内容`)
188-
return
189-
}
190-
emits('submit', {
191-
...(selectedTab.value === 1 ? omit(writeForm.value, ['originalContent']) : writeForm.value),
192-
type: selectedTab.value
193-
})
192+
if (!writeForm.value.prompt) {
193+
message.warning(`请输入${selectedTab.value === 1 ? '写作' : '回复'}内容`)
194+
return
194195
}
196+
emits('submit', {
197+
...(selectedTab.value === 1 ? omit(writeForm.value, ['originalContent']) : writeForm.value),
198+
type: selectedTab.value
199+
})
200+
}
195201
</script>

src/views/ai/writer/components/Right.vue

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -35,52 +35,52 @@
3535
</template>
3636

3737
<script setup lang="ts">
38-
import { useClipboard } from '@vueuse/core'
39-
const message = useMessage()
40-
const props = defineProps({
41-
msg: {
42-
type: String,
43-
default: ''
44-
},
45-
isWriting: {
46-
type: Boolean,
47-
default: false
48-
}
49-
})
38+
import { useClipboard } from '@vueuse/core'
5039
51-
const emits = defineEmits(['update:msg', 'stopStream'])
40+
const message = useMessage()
41+
const { copied, copy } = useClipboard()
5242
53-
const { copied, copy } = useClipboard()
43+
const props = defineProps({
44+
msg: {
45+
type: String,
46+
default: ''
47+
},
48+
isWriting: {
49+
type: Boolean,
50+
default: false
51+
}
52+
})
5453
55-
const compMsg = computed({
56-
get() {
57-
return props.msg
58-
},
59-
set(val) {
60-
emits('update:msg', val)
61-
}
62-
})
54+
const emits = defineEmits(['update:msg', 'stopStream'])
6355
64-
const showCopy = computed(() => props.msg && !props.isWriting)
56+
// TODO @hhhero:是不是 Msg 改成 Content 这种哈。或者 Message。
57+
const compMsg = computed({
58+
get() {
59+
return props.msg
60+
},
61+
set(val) {
62+
emits('update:msg', val)
63+
}
64+
})
6565
66-
const inputId = computed(() => getCurrentInstance()?.uid)
66+
/** 滚动 */
67+
const contentRef = ref<HTMLDivElement>()
68+
defineExpose({
69+
scrollToBottom() {
70+
contentRef.value?.scrollTo(0, contentRef.value?.scrollHeight)
71+
}
72+
})
6773
68-
const contentRef = ref<HTMLDivElement>()
69-
defineExpose({
70-
scrollToBottom() {
71-
contentRef.value?.scrollTo(0, contentRef.value?.scrollHeight)
72-
}
73-
})
74+
/** 点击复制的时候复制内容 */
75+
const showCopy = computed(() => props.msg && !props.isWriting) // 是否展示拷贝
76+
const inputId = computed(() => getCurrentInstance()?.uid) // TODO @hhhero:这个可以写个注释哈
77+
const copyMsg = () => {
78+
copy(props.msg)
79+
}
7480
75-
// 点击复制的时候复制msg
76-
const copyMsg = () => {
77-
copy(props.msg)
81+
watch(copied, (val) => {
82+
if (val) {
83+
message.success('复制成功')
7884
}
79-
80-
watch(copied, (val) => {
81-
console.log({ copied: val })
82-
if (val) {
83-
message.success('复制成功')
84-
}
85-
})
85+
})
8686
</script>

src/views/ai/writer/components/Tag.vue

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,19 @@
1313
</template>
1414

1515
<script setup lang="ts">
16-
const props = withDefaults(
17-
defineProps<{
18-
tags: { label: string; value: string }[]
19-
modelValue: string
20-
[k: string]: any
21-
}>(),
22-
{
23-
tags: () => []
24-
}
25-
)
16+
const props = withDefaults(
17+
defineProps<{
18+
tags: { label: string; value: string }[]
19+
modelValue: string
20+
[k: string]: any
21+
}>(),
22+
{
23+
tags: () => []
24+
}
25+
)
2626
27-
const emits = defineEmits<{
28-
(e: 'update:modelValue', value: string): void
29-
}>()
27+
const emits = defineEmits<{
28+
(e: 'update:modelValue', value: string): void
29+
}>()
3030
</script>
31-
3231
<style scoped></style>

0 commit comments

Comments
 (0)