|
|
@ -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,18 +24,17 @@ 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' })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Get the last context from the chat context
|
|
|
|
// Get the last context from the chat context
|
|
|
|
// If there is a last context, add it to the options
|
|
|
|
let options: SendMessageOptions = {}
|
|
|
|
let options = {}
|
|
|
|
|
|
|
|
const lastContext = Array.from(chatContext).pop()
|
|
|
|
const lastContext = Array.from(chatContext).pop()
|
|
|
|
if (lastContext) {
|
|
|
|
|
|
|
|
const { conversationId, parentMessageId } = lastContext
|
|
|
|
|
|
|
|
options = { conversationId, parentMessageId }
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Send the message to the API
|
|
|
|
if (lastContext)
|
|
|
|
|
|
|
|
options = { ...lastContext }
|
|
|
|
|
|
|
|
|
|
|
|
const response = await api.sendMessage(message, { ...options })
|
|
|
|
const response = await api.sendMessage(message, { ...options })
|
|
|
|
|
|
|
|
|
|
|
|
const { conversationId, id } = response
|
|
|
|
const { conversationId, id } = response
|
|
|
@ -42,13 +43,38 @@ async function chatReply(message: string) {
|
|
|
|
if (conversationId && id)
|
|
|
|
if (conversationId && id)
|
|
|
|
chatContext.add({ conversationId, parentMessageId: 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 chatReplayOne(message: string, options?: ChatContext) {
|
|
|
|
|
|
|
|
if (!message)
|
|
|
|
|
|
|
|
return sendResponse({ type: 'fail', message: 'Message is empty' })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
let messageOptions: SendMessageOptions = {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (options) {
|
|
|
|
|
|
|
|
const { conversationId, parentMessageId } = options
|
|
|
|
|
|
|
|
messageOptions = { conversationId, parentMessageId }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const response = await api.sendMessage(message, { ...messageOptions })
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 }
|
|
|
|