feat: 统一服务端返回参数

main
ChenZhaoYu 2 years ago
parent 73c9ac4d8f
commit dd522eb404

@ -1,7 +1,9 @@
import * as dotenv from 'dotenv' import * as dotenv from 'dotenv'
import type { SendMessageOptions } from 'chatgpt'
import { ChatGPTAPI } from 'chatgpt' import { ChatGPTAPI } from 'chatgpt'
import { sendResponse } from './utils'
interface ChatContext { export interface ChatContext {
conversationId?: string conversationId?: string
parentMessageId?: string parentMessageId?: string
} }
@ -22,33 +24,57 @@ const api = new ChatGPTAPI({ apiKey })
async function chatReply(message: string) { async function chatReply(message: string) {
if (!message) if (!message)
return return sendResponse({ type: 'fail', message: 'Message is empty' })
// Get the last context from the chat context try {
// If there is a last context, add it to the options // Get the last context from the chat context
let options = {} let options: SendMessageOptions = {}
const lastContext = Array.from(chatContext).pop()
if (lastContext) { const lastContext = Array.from(chatContext).pop()
const { conversationId, parentMessageId } = lastContext
options = { conversationId, parentMessageId } if (lastContext)
options = { ...lastContext }
const response = await api.sendMessage(message, { ...options })
const { conversationId, id } = response
// Add the new context to the chat context
if (conversationId && id)
chatContext.add({ conversationId, parentMessageId: id })
return sendResponse({ type: 'success', data: response })
} }
catch (error: any) {
return sendResponse({ type: 'fail', message: error.message })
}
}
async function chatReplayOne(message: string, options?: ChatContext) {
if (!message)
return sendResponse({ type: 'fail', message: 'Message is empty' })
// Send the message to the API try {
const response = await api.sendMessage(message, { ...options }) let messageOptions: SendMessageOptions = {}
const { conversationId, id } = response if (options) {
const { conversationId, parentMessageId } = options
messageOptions = { conversationId, parentMessageId }
// Add the new context to the chat context const response = await api.sendMessage(message, { ...messageOptions })
if (conversationId && id)
chatContext.add({ conversationId, parentMessageId: id })
return response return sendResponse({ type: 'success', data: response })
}
}
catch (error: any) {
return sendResponse({ type: 'fail', message: error.message })
}
} }
async function clearChatContext() { async function clearChatContext() {
// Clear the chat context // Clear the chat context
chatContext.clear() chatContext.clear()
return Promise.resolve({ message: 'Chat context cleared' }) return sendResponse({ type: 'success', message: 'Chat context cleared' })
} }
export { chatReply, clearChatContext } export { chatReply, chatReplayOne, clearChatContext }

@ -1,5 +1,6 @@
import express from 'express' import express from 'express'
import { chatReply, clearChatContext } from './chatgpt' import type { ChatContext } from './chatgpt'
import { chatReplayOne, chatReply, clearChatContext } from './chatgpt'
const app = express() const app = express()
@ -15,9 +16,25 @@ app.all('*', (req, res, next) => {
app.listen(3002, () => globalThis.console.log('Server is running on port 3002')) app.listen(3002, () => globalThis.console.log('Server is running on port 3002'))
app.post('/chat', async (req, res) => { app.post('/chat', async (req, res) => {
const { message } = req.body try {
const response = await chatReply(message) const { prompt } = req.body as { prompt: string }
res.send(response) const response = await chatReply(prompt)
res.send(response)
}
catch (error) {
res.send(error)
}
})
app.post('./chatOne', async (req, res) => {
try {
const { prompt, options = {} } = req.body as { prompt: string; options?: ChatContext }
const response = await chatReplayOne(prompt, options)
res.send(response)
}
catch (error) {
res.send(error)
}
}) })
app.post('/clear', async (req, res) => { app.post('/clear', async (req, res) => {

@ -0,0 +1,22 @@
interface SendResponseOptions {
type: 'success' | 'fail'
message?: string
data?: any
}
export function sendResponse(options: SendResponseOptions) {
if (options.type === 'success') {
return Promise.resolve({
message: options.message ?? 'Success',
data: options.data ?? null,
status: options.type,
})
}
// eslint-disable-next-line prefer-promise-reject-errors
return Promise.reject({
message: options.message ?? 'Failed',
data: options.data ?? null,
status: options.type,
})
}
Loading…
Cancel
Save