chatgpt-plugin/utils/common.js
2023-02-10 10:46:31 +08:00

29 lines
734 B
JavaScript

import { remark } from 'remark'
import stripMarkdown from 'strip-markdown'
export function markdownToText (markdown) {
return remark()
.use(stripMarkdown)
.processSync(markdown ?? '')
.toString()
}
export async function upsertMessage (message) {
await redis.set(`CHATGPT:MESSAGE:${message.id}`, JSON.stringify(message))
}
export async function getMessageById (id) {
let messageStr = await redis.get(`CHATGPT:MESSAGE:${id}`)
return JSON.parse(messageStr)
}
export async function tryTimes (promiseFn, maxTries = 10) {
try {
return await promiseFn()
} catch (e) {
if (maxTries > 0) {
logger.warn('Failed, retry ' + maxTries)
return tryTimes(promiseFn, maxTries - 1)
}
throw e
}
}