feat: v2.7.2 消息样式美化和优化代码 (#111)

* perf: 优化代码

* feat: 美化消息,支持 markdown 全语法

* chore: version 2.7.2
This commit is contained in:
Redon
2023-02-24 15:03:49 +08:00
committed by GitHub
parent 1e2f893ef6
commit b6fd9ae766
14 changed files with 65 additions and 130 deletions

View File

@@ -65,35 +65,6 @@ async function chatReply(
}
}
/** 实验性质的函数,用于处理聊天过程中的中间结果 */
async function chatReplyProcess(
message: string,
lastContext?: { conversationId?: string; parentMessageId?: string },
process?: (chat: ChatMessage) => void,
) {
if (!message)
return sendResponse({ type: 'Fail', message: 'Message is empty' })
try {
let options: SendMessageOptions = { timeoutMs }
if (lastContext)
options = { ...lastContext }
const response = await api.sendMessage(message, {
...options,
onProgress: (partialResponse) => {
process?.(partialResponse)
},
})
return sendResponse({ type: 'Success', data: response })
}
catch (error: any) {
return sendResponse({ type: 'Fail', message: error.message })
}
}
async function chatConfig() {
return sendResponse({
type: 'Success',
@@ -107,4 +78,4 @@ async function chatConfig() {
export type { ChatContext, ChatMessage }
export { chatReply, chatReplyProcess, chatConfig }
export { chatReply, chatConfig }

View File

@@ -1,6 +1,6 @@
import express from 'express'
import type { ChatContext, ChatMessage } from './chatgpt'
import { chatConfig, chatReply, chatReplyProcess } from './chatgpt'
import type { ChatContext } from './chatgpt'
import { chatConfig, chatReply } from './chatgpt'
const app = express()
const router = express.Router()
@@ -26,24 +26,6 @@ router.post('/chat', async (req, res) => {
}
})
/** 实验性质的函数,用于处理聊天过程中的中间结果 */
router.post('/chat-process', async (req, res) => {
res.setHeader('Content-type', 'application/octet-stream')
try {
const { prompt, options = {} } = req.body as { prompt: string; options?: ChatContext }
await chatReplyProcess(prompt, options, (chat: ChatMessage) => {
res.write(JSON.stringify(chat))
})
}
catch (error) {
res.write(JSON.stringify(error))
}
finally {
res.end()
}
})
router.post('/config', async (req, res) => {
try {
const response = await chatConfig()