File size: 1,693 Bytes
7286745
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

async function main() {
  await prisma.$transaction(
    async tx => {
      const messages = await tx.message.findMany();
      for (const message of messages) {
        const { responseBody, response, result } = message;
        let newResponseBody = responseBody;
        let newResult = result;
        if (responseBody && result) {
          continue;
        }
        if (!responseBody && response) {
          console.log('update response body', message.id);
          newResponseBody = response
            .split('\n')
            .filter(chunk => !!chunk.trim())
            .map(chunk => JSON.parse(chunk))
            .filter(
              body => body.type !== 'final_error' && body.type !== 'final_code',
            );
        }
        if (!result && response) {
          console.log('update response result', message.id);
          newResult = response
            .split('\n')
            .filter(chunk => !!chunk.trim())
            .map(chunk => JSON.parse(chunk))
            ?.find(
              body => body.type === 'final_error' || body.type === 'final_code',
            );
        }

        await tx.message.update({
          where: { id: message.id },
          data: {
            ...(newResult ? { result: newResult } : {}),
            ...(newResponseBody ? { responseBody: newResponseBody } : {}),
          },
        });
      }
    },
    { timeout: 30000 },
  );
}

main()
  .catch(async e => {
    console.error(e);
    await prisma.$disconnect();
    process.exit(1);
  })
  .finally(async () => {
    console.log('finished');
    await prisma.$disconnect();
  });