-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (67 loc) · 2.49 KB
/
index.js
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
import bodyParser from "body-parser";
import express from "express";
import pa11y from "pa11y";
import cors from "cors";
import OpenAI from "openai";
import dotenv from "dotenv";
dotenv.config();
const app = express();
const port = 5009;
// Middleware
app.use(cors());
app.use(bodyParser.json());
// create a route
app.get("/pa11y/test", async (req, res) => {
const { url } = req.query;
if (!url) {
res.status(400).json({ error: "Invalid URL" });
} else {
try {
const results = await pa11y(url);
res.status(200).json(results);
} catch (error) {
console.error(error);
res
.status(500)
.json({ error: "An error occurred while testing the URL" });
}
}
});
const openai = new OpenAI({ apiKey: process.env.OPENAI_API_KEY });
app.post("/scanner", async (req, res) => {
const { url } = req.body;
if (!url) {
res.status(400).json({ error: "Invalid URL" });
} else {
const results = await pa11y(url);
const scan = JSON.stringify(results);
const completion = await openai.chat.completions.create({
messages: [
{
role: "user",
content:
"These are the accessibility issues after a scan, could you please give me 2 lists. So one is the violations and the other is solution. The violations should only tell the user about the violations, nothing else. and the solution should be the answer for the violations. i want you to use bullet points everytime for the solutions that are possible and for every violation. so each bullet point has a solution and a violation. be clear and concise and dont repeat anything twice. so each violation should always have a solution in the solution list that you are providing. so if user has bad accessibility on the header, you tell the user about that in the solution. and keep it the same for everything else." +
scan,
},
],
model: "gpt-3.5-turbo",
});
const answer = completion.choices[0].message.content;
const splitContent = answer.split('**Solutions:**');
const violationsContent = splitContent[0].replace('**Violations:**', '').trim();
const solutionsContent = splitContent[1].trim();
res.json({
role: "assistant",
title: "Violations and Solutions",
content: {
violations: violationsContent,
solutions: solutionsContent,
},
});
console.log(answer);
}
});
// Server start
app.listen(port, () => {
console.log(`Server is running at http://localhost:${port}`);
});