Skip to content

Commit

Permalink
Began adding endpoint to generate suggested issues for a given node
Browse files Browse the repository at this point in the history
  • Loading branch information
heythisischris committed Nov 12, 2024
1 parent 89962ae commit bbbac79
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 2 deletions.
3 changes: 2 additions & 1 deletion src/app.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Fastify from 'fastify';
import { addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, deleteUser, getApikey, getCharts, getFilters, getProperties, getReports, getResultsAll, getResultsMessages, getResultsSchema, getResultsTags, getResultsUrls, getScans, getUpdates, help, trackUser, updateProperties, updateReports, getPages } from '#src/routes';
import { addProperties, addReports, addResults, addScans, deleteProperties, deleteReports, deleteUser, getApikey, getCharts, getFilters, getProperties, getReports, getResultsAll, getResultsMessages, getResultsSchema, getResultsTags, getResultsUrls, getScans, getUpdates, help, trackUser, updateProperties, updateReports, getPages, suggestIssue } from '#src/routes';
import { CognitoJwtVerifier } from 'aws-jwt-verify';
import { db } from './utils';
import { getScan } from './routes/getScan';
Expand Down Expand Up @@ -66,6 +66,7 @@ fastify.delete('/delete/user', {}, async (request, reply) => deleteUser({ reques
// MISC requests
fastify.post('/help', {}, async (request, reply) => help({ request, reply }));
fastify.post('/track/user', {}, async (request, reply) => trackUser({ request, reply }));
fastify.post('/ai/suggest-issue', {}, async (request, reply) => suggestIssue({ request, reply }));

fastify.listen({ port: 3000 }, (err) => {
console.log(`Server listening on ${fastify.server.address().port}`)
Expand Down
3 changes: 2 additions & 1 deletion src/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,5 @@ export * from './getCharts'
export * from './getApikey'
export * from './getPages'
export * from './deleteUser'
export * from './trackUser'
export * from './trackUser'
export * from './suggestIssue'
56 changes: 56 additions & 0 deletions src/routes/suggestIssue.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { stripHtml } from 'string-strip-html';
import { openai } from '#src/utils';

export const suggestIssue = async ({ request, reply }) => {
const { reportId, messageId, dequeUrl, messageName: accessibilityIssueToFix, codeSnippet: originalCodeSnippet, pageUrl } = request.body;
const accessibilityDocumentation = stripHtml(await (await fetch(dequeUrl)).text()).result;
const originalCode = await (await fetch(pageUrl)).text();

const openaiRawResponse = await openai.chat.completions.create({
model: 'gpt-4-turbo-preview',
tool_choice: { type: 'function', function: { name: 'suggested_fix' } },
tools: [{
type: 'function',
function: {
name: 'suggested_fix',
description: 'Suggest an accessibility fix for the given code snippet and accessibility rule',
parameters: {
type: 'object',
description: 'A suggested fix',
properties: {
suggested_replacement_code: {
type: 'string',
description: 'The suggested replacement code',
},
how_to_implement: {
type: 'string',
description: 'How to implement the suggested code',
},
},
required: ['suggested_replacement_code', 'how_to_implement'],
},
}
}],
messages: [
{ role: 'system', content: `You are an LLM bot running for Equalify, a platform designed to identify accessibility issues for developers to fix.` },
{
role: 'user', content: `Suggest replacement code to fix the accessibility issue identified by Equalify. You may use the accessibility documentation to assist in your resolution:
\`\`\`json
${JSON.stringify({
originalCode,
originalCodeSnippet,
accessibilityIssueToFix,
accessibilityDocumentation,
})}
\`\`\`
`},
]
});

const suggestedFix = JSON.parse(openaiRawResponse.choices?.[0]?.message?.tool_calls?.[0]?.function?.arguments ?? '{}');

return {
...suggestedFix,
originalCode,
};
}

0 comments on commit bbbac79

Please sign in to comment.