📦 EqualifyEverything / equalify-v2-api

📄 analyzeAuditResults.ts · 94 lines
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94import { db, event, graphqlQuery, openai } from '#src/utils';

export const analyzeAuditResults = async () => {
    const audit_id = event.queryStringParameters.id;

    await db.connect();
    const audit = (await db.query({
        text: `SELECT * FROM "audits" WHERE "id" = $1`,
        values: [audit_id],
    })).rows[0];
    const urls = (await db.query({
        text: `SELECT "id", "url" FROM "urls" WHERE "audit_id" = $1`,
        values: [audit_id],
    })).rows;

    const response = await graphqlQuery({
        query: `query ($audit_id: uuid) {
            nodes(where: {audit_id: {_eq: $audit_id}}) {
                id
                created_at
                html
                targets
                url_id
                equalified
                node_updates { created_at equalified }
                message_nodes {
                    id
                    node { equalified }
                    message {
                        id
                        message
                        type
                        message_tags { tag { id tag } }
                    }
                }
            }
        }`,
        variables: { audit_id: audit_id },
    });

    const urlMap = urls.reduce((acc, url) => {
        acc[url.id] = url.url;
        return acc;
    }, {});

    const rows = response.nodes.slice(0, 10000).map(node => {
        return {
            node_id: node.id,
            url_id: urlMap[node.url_id] || '',
            html: node.html,
            targrets: node.targets,
            equalified: node.equalified,
            created_at: node.created_at,
            messages: node.message_nodes.map(mn =>
                `${mn.message.type}: ${mn.message.message}`
            )
        }
    });
    const aiResponse = await openai.chat.completions.create({
        model: 'gpt-4.1-mini',
        response_format: { type: 'json_object' },
        max_completion_tokens: 32768,
        messages: [{
            role: "system",
            content: `
                Your job is to analyze these audit results and return HTML with a detailed summary, breakdown, and infographics. Use CSS for styling.
                Use Equalify branding (you can use this image: https://sales.equalify.app/equalifyv2.png).

                You must respond with this format:
                \`\`\`json
                {"result":"HTML CONTENT"}
                \`\`\`

                Here is the audit metadata:
                \`\`\`json
                ${JSON.stringify(audit)}
                \`\`\`

                Here are the audit results:
                \`\`\`json
                ${JSON.stringify(rows)}
                \`\`\`
            `,
        }
        ],
    });
    const parsedResponse = JSON.parse(aiResponse?.choices?.[0]?.message?.content)?.result;
    await db.clean();
    return {
        statusCode: 200,
        headers: { 'content-type': 'text/html' },
        body: parsedResponse,
    }
}