From 83d9a5dfc73e4dabe6786d5faf03697de60ed21f Mon Sep 17 00:00:00 2001 From: Hao Wu Date: Sun, 8 Sep 2024 13:10:03 +0800 Subject: [PATCH 1/2] Refactor bot view and add API helper generation --- web/src/locales/zh-CN.json | 4 +- web/src/service/bot.ts | 43 +++++++++++++++ web/src/typings/chat.d.ts | 20 +++++++ web/src/views/bot/all.vue | 106 ++++++++++++++++++------------------- 4 files changed, 118 insertions(+), 55 deletions(-) create mode 100644 web/src/service/bot.ts diff --git a/web/src/locales/zh-CN.json b/web/src/locales/zh-CN.json index e54f3df5..66b8ecc9 100644 --- a/web/src/locales/zh-CN.json +++ b/web/src/locales/zh-CN.json @@ -2,6 +2,7 @@ "common": { "ask_user_register": "请注册, 只有注册账号才能继续对话", "help": "第一条是主题(prompt, 角色定义), 上下文默认包括10条信息, 参数可以点击按钮进行调节, 请务必注意隐私, 不输入涉密, 敏感信息.", + "copy": "复制", "edit": "编辑", "delete": "删除", "save": "保存", @@ -40,7 +41,8 @@ "list": "机器人列表", "all": { "title": "机器人列表" - } + }, + "showCode": "生成API调用代码" }, "chat": { "new": "新对话", diff --git a/web/src/service/bot.ts b/web/src/service/bot.ts new file mode 100644 index 00000000..23227ea2 --- /dev/null +++ b/web/src/service/bot.ts @@ -0,0 +1,43 @@ +import { displayLocaleDate, formatYearMonth } from '@/utils/date' + + +export function post_url(uuid: string): string { + return `#/bot/${uuid}` +} + + +export function generateAPIHelper(uuid: string, apiToken: string, origin: string) { + const data = { + "message": "Your message here", + "snapshot_uuid": uuid, + "stream": false, + } + return `curl -X POST ${origin}/api/chatbot -H "Content-Type: application/json" -H "Authorization: Bearer ${apiToken}" -d '${JSON.stringify(data)}'` +} + +export function getChatbotPosts(posts: Snapshot.Snapshot[]) { + return posts + .filter((post: Snapshot.Snapshot) => post.typ === 'chatbot') + .map((post: Snapshot.Snapshot): Snapshot.PostLink => ({ + uuid: post.uuid, + date: displayLocaleDate(post.createdAt), + title: post.title, + })) +} + +export function postsByYearMonthTransform(posts: Snapshot.PostLink[]) { + const init: Record = {} + return posts.reduce((acc, post) => { + const yearMonth = formatYearMonth(new Date(post.date)) + if (!acc[yearMonth]) + acc[yearMonth] = [] + + acc[yearMonth].push(post) + return acc + }, init) +} + +export function getPostLinks(snapshots: Snapshot.Snapshot[]): Record { + const chatbotPosts = getChatbotPosts(snapshots) + return postsByYearMonthTransform(chatbotPosts) +} \ No newline at end of file diff --git a/web/src/typings/chat.d.ts b/web/src/typings/chat.d.ts index 9ee89e75..aa14164a 100644 --- a/web/src/typings/chat.d.ts +++ b/web/src/typings/chat.d.ts @@ -78,4 +78,24 @@ declare namespace Chat { userEmail: string rateLimit: string } + + } + +declare namespace Snapshot { + + interface Snapshot { + uuid: string; + title: string; + summary: string; + tags: Record; + createdAt: string; + typ: 'chatbot' | 'snapshot'; + } + + interface PostLink { + uuid: string; + date: string; + title: string; + } +} \ No newline at end of file diff --git a/web/src/views/bot/all.vue b/web/src/views/bot/all.vue index 68b6f63c..26a69b71 100644 --- a/web/src/views/bot/all.vue +++ b/web/src/views/bot/all.vue @@ -1,56 +1,36 @@