Skip to content

Commit c7cde18

Browse files
authored
feat: support built-in prompt type (#628)
* feat: add built-in prompt in prompt-list api Signed-off-by: Bob Du <i@bobdu.cc> * feat: support prompt type tag Signed-off-by: Bob Du <i@bobdu.cc> * refactor: prompt store remove use local storage Signed-off-by: Bob Du <i@bobdu.cc> --------- Signed-off-by: Bob Du <i@bobdu.cc>
1 parent 47076a4 commit c7cde18

File tree

16 files changed

+144
-98
lines changed

16 files changed

+144
-98
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
"lint-staged": "^16.1.0",
5050
"markdown-it": "^14.1.0",
5151
"markdown-it-link-attributes": "^4.0.1",
52-
"naive-ui": "~2.40.4",
52+
"naive-ui": "^2.41.1",
5353
"pinia": "^3.0.2",
5454
"qrcode.vue": "^3.6.0",
5555
"rimraf": "^6.0.1",

pnpm-lock.yaml

Lines changed: 9 additions & 51 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

service/src/routes/prompt.ts

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { auth } from '../middleware/auth'
55
import {
66
clearUserPrompt,
77
deleteUserPrompt,
8+
getBuiltInPromptList,
89
getUserPromptList,
910
importUserPrompt,
1011
upsertUserPrompt,
@@ -15,16 +16,45 @@ export const router = Router()
1516
router.get('/prompt-list', auth, async (req, res) => {
1617
try {
1718
const userId = req.headers.userId as string
18-
const prompts = await getUserPromptList(userId)
19-
const result = []
20-
prompts.data.forEach((p) => {
21-
result.push({
19+
20+
// 获取用户自定义提示词
21+
const userPrompts = await getUserPromptList(userId)
22+
const userResult = []
23+
userPrompts.data.forEach((p) => {
24+
userResult.push({
25+
_id: p._id,
26+
title: p.title,
27+
value: p.value,
28+
type: 'user-defined',
29+
})
30+
})
31+
32+
// 获取平台预置提示词
33+
const builtInPrompts = await getBuiltInPromptList()
34+
const builtInResult = []
35+
builtInPrompts.data.forEach((p) => {
36+
builtInResult.push({
2237
_id: p._id,
2338
title: p.title,
2439
value: p.value,
40+
type: 'built-in',
2541
})
2642
})
27-
res.send({ status: 'Success', message: null, data: { data: result, total: prompts.total } })
43+
44+
// 合并两种类型的提示词
45+
const allPrompts = [...userResult, ...builtInResult]
46+
const totalCount = userPrompts.total + builtInPrompts.total
47+
48+
res.send({
49+
status: 'Success',
50+
message: null,
51+
data: {
52+
data: allPrompts,
53+
total: totalCount,
54+
userTotal: userPrompts.total,
55+
builtInTotal: builtInPrompts.total,
56+
},
57+
})
2858
}
2959
catch (error) {
3060
res.send({ status: 'Fail', message: error.message, data: null })

service/src/storage/model.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,16 @@ export class KeyConfig {
302302
}
303303
}
304304

305+
export class BuiltInPrompt {
306+
_id: ObjectId
307+
title: string
308+
value: string
309+
constructor(title: string, value: string) {
310+
this.title = title
311+
this.value = value
312+
}
313+
}
314+
305315
export class UserPrompt {
306316
_id: ObjectId
307317
userId: string

service/src/storage/mongo.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { WithId } from 'mongodb'
22
import type {
33
AdvancedConfig,
4+
BuiltInPrompt,
45
ChatOptions,
56
Config,
67
GiftCard,
@@ -39,6 +40,7 @@ const userCol = client.db(dbName).collection<UserInfo>('user')
3940
const configCol = client.db(dbName).collection<Config>('config')
4041
const usageCol = client.db(dbName).collection<ChatUsage>('chat_usage')
4142
const keyCol = client.db(dbName).collection<KeyConfig>('key_config')
43+
const builtInPromptCol = client.db(dbName).collection<BuiltInPrompt>('built_in_prompt')
4244
const userPromptCol = client.db(dbName).collection<UserPrompt>('user_prompt')
4345
// 新增兑换券的数据库
4446
// {
@@ -664,6 +666,13 @@ export async function updateApiKeyStatus(id: string, status: Status) {
664666
await keyCol.updateOne({ _id: new ObjectId(id) }, { $set: { status } })
665667
}
666668

669+
export async function getBuiltInPromptList(): Promise<{ data: BuiltInPrompt[], total: number }> {
670+
const total = await builtInPromptCol.countDocuments()
671+
const cursor = builtInPromptCol.find().sort({ _id: -1 })
672+
const data = await cursor.toArray()
673+
return { data, total }
674+
}
675+
667676
export async function upsertUserPrompt(userPrompt: UserPrompt): Promise<UserPrompt> {
668677
if (userPrompt._id === undefined) {
669678
const doc = await userPromptCol.insertOne(userPrompt)

src/components/common/PromptStore/index.vue

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ interface DataProps {
1313
_id?: string
1414
renderKey: string
1515
renderValue: string
16+
renderType: string
1617
title: string
1718
value: string
19+
type: 'built-in' | 'user-defined'
1820
}
1921
2022
interface Props {
@@ -280,9 +282,11 @@ function renderTemplate() {
280282
return {
281283
renderKey: item.title.length <= keyLimit ? item.title : `${item.title.substring(0, keyLimit)}...`,
282284
renderValue: item.value.length <= valueLimit ? item.value : `${item.value.substring(0, valueLimit)}...`,
285+
renderType: item.type === 'built-in' ? t('store.builtIn') : t('store.userDefined'),
283286
title: item.title,
284287
value: item.value,
285288
_id: item._id,
289+
type: item.type,
286290
}
287291
})
288292
}
@@ -298,6 +302,10 @@ const pagination = computed(() => {
298302
// table相关
299303
function createColumns(): DataTableColumns<DataProps> {
300304
return [
305+
{
306+
title: 'type',
307+
key: 'renderType',
308+
},
301309
{
302310
title: t('store.title'),
303311
key: 'renderKey',
@@ -312,6 +320,9 @@ function createColumns(): DataTableColumns<DataProps> {
312320
width: 100,
313321
align: 'center',
314322
render(row) {
323+
if (row.type === 'built-in') {
324+
return ''
325+
}
315326
return h('div', { class: 'flex items-center flex-col gap-2' }, {
316327
default: () => [h(
317328
NButton,
@@ -349,12 +360,10 @@ watch(
349360
)
350361
351362
onMounted(async () => {
352-
if (!!authStore.session?.auth && !authStore.token)
363+
if (!authStore.session?.auth)
353364
return
354-
if (promptStore.getPromptList().promptList.length === 0) {
355-
await handleGetUserPromptList()
356-
promptStore.updatePromptList(promptList.value)
357-
}
365+
await handleGetUserPromptList()
366+
promptStore.updatePromptList(promptList.value)
358367
})
359368
360369
const dataSource = computed(() => {
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<script setup lang="ts">
2+
import { t } from '@/locales'
3+
4+
interface Props {
5+
type: 'built-in' | 'user-defined'
6+
}
7+
8+
defineProps<Props>()
9+
</script>
10+
11+
<template>
12+
<NTag
13+
size="small"
14+
:type="type === 'built-in' ? 'success' : 'info'"
15+
round
16+
style="
17+
width: 85px;
18+
display: inline-flex;
19+
justify-content: center;
20+
align-items: center;
21+
"
22+
>
23+
{{ type === 'built-in' ? t('store.builtIn') : t('store.userDefined') }}
24+
</NTag>
25+
</template>

src/components/common/Setting/model.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ export class UserPrompt {
115115
_id?: string
116116
title: string
117117
value: string
118+
type?: 'built-in' | 'user-defined'
118119
constructor(title: string, value: string) {
119120
this.title = title
120121
this.value = value

src/components/common/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@ import GithubSite from './GitHubSite/index.vue'
22
import HoverButton from './HoverButton/index.vue'
33
import NaiveProvider from './NaiveProvider/index.vue'
44
import PromptStore from './PromptStore/index.vue'
5+
import PromptTypeTag from './PromptTypeTag/index.vue'
56
import Setting from './Setting/index.vue'
67
import SvgIcon from './SvgIcon/index.vue'
78
import UserAvatar from './UserAvatar/index.vue'
89
import Watermark from './Watermark/index.vue'
910

10-
export { GithubSite, HoverButton, NaiveProvider, PromptStore, Setting, SvgIcon, UserAvatar, Watermark }
11+
export { GithubSite, HoverButton, NaiveProvider, PromptStore, PromptTypeTag, Setting, SvgIcon, UserAvatar, Watermark }

src/locales/en-US.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,5 +241,7 @@ export default {
241241
importRepeatContent: 'Content is repeatedly skipped: {msg}',
242242
onlineImportWarning: 'Note: Please check the JSON file source!',
243243
downloadError: 'Please check the network status and JSON file validity',
244+
builtIn: 'Built-in',
245+
userDefined: 'User defined',
244246
},
245247
}

0 commit comments

Comments
 (0)