Skip to content

Commit

Permalink
feat: handle no json better
Browse files Browse the repository at this point in the history
  • Loading branch information
irony committed May 1, 2024
1 parent 877613e commit 29e3ba3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 22 deletions.
5 changes: 4 additions & 1 deletion src/discord/interactions/feedback.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import {
ActionRowBuilder,
CommandInteraction,
Interaction,
ModalActionRowComponentBuilder,
ModalBuilder,
TextInputBuilder,
Expand All @@ -8,7 +10,7 @@ import {
import { userFeedback } from '../../queues'

export default {
async execute(interaction, job) {
async execute(interaction: CommandInteraction, job) {
const input = new TextInputBuilder()
.setCustomId('editInput')
.setLabel(`Granska utsläppsdata`)
Expand Down Expand Up @@ -40,6 +42,7 @@ export default {
const userInput = submitted.fields.getTextInputValue('editInput')
//this.emit('edit', documentId, userInput)

interaction.channel.sendTyping()
await submitted.reply({
content: `Tack för din feedback: \n ${userInput}`,
})
Expand Down
42 changes: 21 additions & 21 deletions src/workers/userFeedback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,31 +93,31 @@ const worker = new Worker(

job.log('Response: ' + response)

let json
try {
json =
response
.match(/```json(.|\n)*```/)[0]
?.replace(/```json|```/g, '')
.trim() || '{}'
} catch (error) {
job.log('Error: ' + error)
discord.sendMessage(job.data, response)
discord.sendMessage(job.data, `Fel vid tolkning av JSON: ${error}`)
}
discord.sendMessage(job.data, response.replace(json, '...json...'))
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 json =
response
.match(/```json(.|\n)*```/)?.[0]
?.replace(/```json|```/g, '')
.trim() || '{}'
const parsedJson = json ? JSON.parse(json) : {} // we want to make sure it's valid JSON- otherwise we'll get an error which will trigger a new retry

job.log('Parsed JSON: ' + JSON.stringify(parsedJson, null, 2))
discord.sendMessage(
job.data,
json ? response.replace(json, '...json...') : response
)

discordReview.add(job.name, {
...job.data,
json: JSON.stringify(parsedJson, null, 2),
})
if (Object.keys(parsedJson)) {
job.log('Parsed JSON: ' + JSON.stringify(parsedJson, null, 2))

if (parsedJson.reviewComment)
await discord.sendMessage(job.data, parsedJson.reviewComment)
if (parsedJson.agentResponse)
await discord.sendMessage(job.data, parsedJson.agentResponse)
if (parsedJson.reviewComment)
await discord.sendMessage(job.data, parsedJson.reviewComment)

discordReview.add(job.name, {
...job.data,
json: JSON.stringify(parsedJson, null, 2),
})
}
job.log('Sent to thread' + job.data.threadId)

return json
Expand Down

0 comments on commit 29e3ba3

Please # to comment.