|
@@ -1,6 +1,7 @@
|
|
|
import { Score } from '../../db/models'
|
|
|
import { ScoreData } from '../../db/models/Score'
|
|
|
import { sleep } from '../../utils'
|
|
|
+import chainService from '../chainService'
|
|
|
import difyService, {
|
|
|
CompletionMessagesPayload,
|
|
|
DifyRateLimitExceedError,
|
|
@@ -14,7 +15,43 @@ import {
|
|
|
async function getCompletionMessages(
|
|
|
payload: CompletionMessagesPayload
|
|
|
): Promise<Score> {
|
|
|
- const { query, onUpdate, signature } = payload
|
|
|
+ const { query, onUpdate, signature, address } = payload
|
|
|
+
|
|
|
+ const signatureRowExist = await Score.findOne({ where: { signature } })
|
|
|
+
|
|
|
+ if (signatureRowExist) {
|
|
|
+ const { messageId } = signatureRowExist
|
|
|
+
|
|
|
+ const words = signatureRowExist.answer.split(' ').map((word) => ` ${word}`)
|
|
|
+ if (words[0]) {
|
|
|
+ words[0] = words[0].trim()
|
|
|
+ }
|
|
|
+
|
|
|
+ for (const word of words) {
|
|
|
+ const data = { event: 'message', messageId, signature, answer: word }
|
|
|
+
|
|
|
+ const message = JSON.stringify(data)
|
|
|
+
|
|
|
+ onUpdate(`data: ${message}\n\n`)
|
|
|
+ await sleep(20)
|
|
|
+ }
|
|
|
+
|
|
|
+ const data = { event: 'message_end', messageId, signature }
|
|
|
+
|
|
|
+ const message = JSON.stringify(data)
|
|
|
+
|
|
|
+ onUpdate(`data: ${message}\n\n`)
|
|
|
+
|
|
|
+ return signatureRowExist
|
|
|
+ }
|
|
|
+
|
|
|
+ const checkResult = await chainService.checkTxIsSendBananaToBlackhole(
|
|
|
+ address,
|
|
|
+ signature
|
|
|
+ )
|
|
|
+ if (!checkResult.success) {
|
|
|
+ throw new Error('Invalid transaction signature')
|
|
|
+ }
|
|
|
const scoreId = Score.getScoreId(query)
|
|
|
const exist = await Score.findOne({ where: { scoreId } })
|
|
|
|
|
@@ -41,7 +78,17 @@ async function getCompletionMessages(
|
|
|
|
|
|
onUpdate(`data: ${message}\n\n`)
|
|
|
|
|
|
- return exist
|
|
|
+ const row = await Score.create({
|
|
|
+ address,
|
|
|
+ signature,
|
|
|
+ scoreId: exist.scoreId,
|
|
|
+ query: exist.query,
|
|
|
+ answer: exist.answer,
|
|
|
+ messageId,
|
|
|
+ score: exist.score,
|
|
|
+ scoreText: exist.scoreText,
|
|
|
+ })
|
|
|
+ return row
|
|
|
}
|
|
|
|
|
|
try {
|
|
@@ -49,17 +96,15 @@ async function getCompletionMessages(
|
|
|
await difyService.getCompletionMessagesByFetchAPI(payload)
|
|
|
|
|
|
const [scoreText, score] = Score.getScore(answer)
|
|
|
- const [row] = await Score.findOrCreate({
|
|
|
- where: { scoreId },
|
|
|
- defaults: {
|
|
|
- signature,
|
|
|
- scoreId,
|
|
|
- query,
|
|
|
- answer,
|
|
|
- messageId,
|
|
|
- score,
|
|
|
- scoreText,
|
|
|
- },
|
|
|
+ const row = await Score.create({
|
|
|
+ address,
|
|
|
+ signature,
|
|
|
+ scoreId,
|
|
|
+ query,
|
|
|
+ answer,
|
|
|
+ messageId,
|
|
|
+ score,
|
|
|
+ scoreText,
|
|
|
})
|
|
|
return row
|
|
|
} catch (e) {
|