Skip to content

Commit

Permalink
feat: activate userFeedback again
Browse files Browse the repository at this point in the history
  • Loading branch information
irony committed Apr 13, 2024
1 parent 2276ea2 commit b64b861
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 12 deletions.
16 changes: 11 additions & 5 deletions src/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,17 @@ import {
import commands from './commands'
import config from './config/discord'
import elastic from './elastic'
import { EventEmitter } from 'events'

export class Discord {
export class Discord extends EventEmitter {
client: Client<boolean>
rest: REST
commands: Array<any>
token: string
channelId: string

constructor({ token, guildId, clientId, channelId }) {
super()
this.token = token
this.channelId = channelId
this.client = new Client({ intents: [GatewayIntentBits.Guilds] })
Expand Down Expand Up @@ -53,10 +55,11 @@ export class Discord {
} else if (interaction.isButton()) {
let reportState = ''

const [action, documentId] = interaction.customId.split('-');
const [action, documentId] = interaction.customId.split('-')
switch (action) {
case 'approve':
reportState = 'approved';
this.emit('approve', documentId)
reportState = 'approved'
interaction.update({
embeds: [
new EmbedBuilder()
Expand All @@ -69,7 +72,7 @@ export class Discord {
})
break
case 'edit':
reportState = 'edited';
reportState = 'edited'
const input = new TextInputBuilder()
.setCustomId('editInput')
.setLabel(`Granska utsläppsdata`)
Expand Down Expand Up @@ -100,13 +103,16 @@ export class Discord {

if (submitted) {
const userInput = submitted.fields.getTextInputValue('editInput')
this.emit('edit', documentId, userInput)

await submitted.reply({
content: `Tack för din granskning: \n ${userInput}`,
})
}
break
case 'reject':
reportState = 'rejected';
reportState = 'rejected'
this.emit('reject', documentId)
interaction.update({
content: 'Rejected!',
embeds: [],
Expand Down
15 changes: 14 additions & 1 deletion src/workers/discordReview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ class JobData extends Job {
async function saveToDb(id: string, report: any) {
return await elastic.indexReport(id, report)
}
const createButtonRow = (documentId) => { // TODO: move to discord.ts
const createButtonRow = (documentId) => {
// TODO: move to discord.ts
return new ActionRowBuilder().addComponents(
new ButtonBuilder()
.setCustomId(`approve-${documentId}`)
Expand Down Expand Up @@ -86,6 +87,18 @@ ${job.data.url}
throw error
}

discord.once('edit', async (documentId, feedback) => {
if (documentId === documentId) {
job.log(`Got feedback: ${feedback}`)
await userFeedback.add('userFeedback', {
...job.data,
documentId,
json: parsedJson,
feedback,
})
}
})

job.updateProgress(40)
job.updateProgress(100)
},
Expand Down
9 changes: 3 additions & 6 deletions src/workers/userFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,16 @@ const embedder = new OpenAIEmbeddingFunction({
class JobData extends Job {
data: {
url: string
json: string
channelId: string
threadId: string
messageId: string
pdfHash: string
json: string
feedback: string
}
}

const worker = new Worker(
'userFeedback',
async (job: JobData) => {
const { feedback, url, json: previousJson } = job.data
const { feedback, url, json: previousJson, threadId } = job.data
const client = new ChromaClient(chromadb)
const collection = await client.getCollection({
name: cleanCollectionName(url),
Expand Down Expand Up @@ -80,7 +77,7 @@ const worker = new Worker(

const parsedJson = JSON.parse(json) // we want to make sure it's valid JSON- otherwise we'll get an error which will trigger a new retry

const thread = await discord.client.channels.fetch(job.data.threadId)
const thread = await discord.client.channels.fetch(threadId)

if (thread.isThread()) {
const summary = await summaryTable(parsedJson)
Expand Down

0 comments on commit b64b861

Please # to comment.