Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/danielroe/carpenter. LLM-powered automations for triaging Nuxt issues github.com/nuxt/nuxt
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482import type { z } from 'zod'import { isError } from 'h3'import type { H3Event } from 'h3'import { generateText } from 'ai'import { createWorkersAI } from 'workers-ai-provider'
import type { IssuesEvent, IssueCommentEvent } from '@octokit/webhooks-types'import { toXML } from '../utils/xml'import { analyzedIssueSchema, commentAnalysisResponseSchema, commentAnalysisSchema, enhancedAnalysisResponseSchema, enhancedAnalysisSchema, IssueLabel, IssueType, responseSchema, translationResponseSchema } from '../utils/schema'import { isCollaboratorOrHigher } from '../utils/author-role'import type { EnhancedContext } from '../utils/context'import { gatherEnhancedContext, wasClosedAsNotPlanned, wasClosedAsDuplicate, wasClosedAsCompleted, buildEnhancedPromptContent } from '../utils/context'import { transferIssueToSpam } from '../utils/issue-transfer'
export default defineEventHandler(async (event) => { if (!import.meta.dev && !(await isValidGitHubWebhook(event))) { throw createError({ statusCode: 401, message: 'Unauthorized: webhook is not valid' }) }
const webhookPayload = await readBody(event) as IssuesEvent | IssueCommentEvent const { action } = webhookPayload
if (!('issue' in webhookPayload) || 'pull_request' in webhookPayload.issue) { return }
if ('comment' in webhookPayload) { return handleIssueComment(event, webhookPayload) }
if (action === 'edited') { return handleIssueEdit(event, webhookPayload) }
if (action === 'opened') { return handleNewIssue(event, webhookPayload) }
if (action === 'closed') { return }
if (action === 'labeled') { return handleIssueLabeled(event, webhookPayload) }
return null})
type CommentAnalysisResult = { reproductionProvided?: boolean possibleRegression?: boolean}
type EnhancedAnalysisResult = { reproductionProvided?: boolean possibleRegression?: boolean shouldReopen?: boolean isDifferentFromDuplicate?: boolean confidence?: 'low' | 'medium' | 'high'}
async function analyzeClosedIssueComment( event: H3Event, issue: IssueCommentEvent['issue'], repository: IssueCommentEvent['repository'], issueLabels: string[],): Promise<{ result: EnhancedAnalysisResult, context: EnhancedContext }> { const enhancedContext = await gatherEnhancedContext(event, issue, repository, { includeComments: true, maxComments: 5, includeTimeline: true, })
const promptContent = buildEnhancedPromptContent(enhancedContext, true)
// Determine analysis strategy based on how the issue was closed let systemPrompt = `You are analyzing a closed issue in an open source project to determine if it should be reopened based on new evidence. `
if (wasClosedAsNotPlanned(enhancedContext)) { systemPrompt += `This issue was closed as 'not planned'. Consider if new evidence in comments suggests it should be reconsidered. Pay attention to issue history - if it has been closed and reopened multiple times, be more conservative about reopening. ` } else if (wasClosedAsDuplicate(enhancedContext, issueLabels)) { systemPrompt += `This issue was closed as 'duplicate'. Only suggest reopening if there's clear evidence this is a different issue than the original. ` } else if (wasClosedAsCompleted(enhancedContext)) { systemPrompt += `This issue was closed as completed. Only suggest reopening if there's evidence the issue has reappeared (possible regression). ` }
systemPrompt += `Do not answer with anything else other than valid JSON. Here's the JSON schema you must adhere to:\n${toXML(enhancedAnalysisSchema)}\n`
const workersai = createWorkersAI({ binding: event.context.cloudflare.env.AI }) const { text: aiText } = await generateText({ model: workersai('@hf/nousresearch/hermes-2-pro-mistral-7b'), messages: [ { role: 'system', content: systemPrompt }, { role: 'user', content: promptContent }, ], })
setHeader(event, 'x-enhanced-comment-analysis', aiText) const result = enhancedAnalysisResponseSchema.parse(JSON.parse(aiText || '{}')) return { result, context: enhancedContext }}
async function handleIssueComment(event: H3Event, { comment, issue, repository }: IssueCommentEvent) { if (comment.user?.type === 'Bot') { return }
// Early return if collaborator - they can manually reopen issues if needed if (isCollaboratorOrHigher(comment.author_association)) { return }
if ('pull_request' in issue) { return }
const issueLabels = issue.labels?.map(label => label.name) || [] const hasNeedsReproductionLabel = issueLabels.includes(IssueLabel.NeedsReproduction)
if (!hasNeedsReproductionLabel && issue.state !== 'closed') { return }
const github = useGitHubAPI(event) const promises: Array<Promise<unknown>> = []
try { let analysisResult: CommentAnalysisResult | EnhancedAnalysisResult let enhancedContext: EnhancedContext | null = null
// For closed issues, use enhanced context analysis if (issue.state === 'closed') { const analysis = await analyzeClosedIssueComment(event, issue, repository, issueLabels) analysisResult = analysis.result enhancedContext = analysis.context } else { // Use original analysis for open issues const workersai = createWorkersAI({ binding: event.context.cloudflare.env.AI }) const { text: aiText } = await generateText({ model: workersai('@hf/nousresearch/hermes-2-pro-mistral-7b'), messages: [ { role: 'system', content: `You categorise issues in an open source project. Reported bugs must have enough information to reproduce them, either a full code example or a link to GitHub, StackBlitz or CodeSandbox. Do not answer with anything else other than valid JSON. Here's the json schema you must adhere to:\n${toXML(commentAnalysisSchema)}\n`, }, { role: 'user', content: JSON.stringify({ body: getNormalizedIssueContent(comment.body) }) }, ], })
setHeader(event, 'x-comment-analysis', aiText) analysisResult = commentAnalysisResponseSchema.parse(JSON.parse(aiText || '{}')) }
// Handle analysis results // 1. if a comment adds a reproduction if (hasNeedsReproductionLabel && analysisResult.reproductionProvided) { // we can go ahead and remove the 'needs reproduction' label promises.push( github.issues.removeLabel({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, name: IssueLabel.NeedsReproduction, }), ) // ... plus, if issue is closed, we'll reopen it if (issue.state === 'closed') { promises.push( github.issues.update({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, state: 'open', }), ) } } // 2. if a resolved issue reappears (with enhanced logic for closed issues) else if (issue.state === 'closed' && analysisResult.possibleRegression) { // For closed issues, we have enhanced analysis results available const enhancedResult = analysisResult as EnhancedAnalysisResult
// Check if we should reopen based on enhanced analysis const shouldReopen = enhancedResult.shouldReopen || analysisResult.possibleRegression
if (!shouldReopen) { return Promise.resolve([]) }
// For duplicate issues, only reopen if clearly different if (wasClosedAsDuplicate(enhancedContext!, issueLabels) && !enhancedResult.isDifferentFromDuplicate) { return Promise.resolve([]) }
// Collaborators and above can explicitly reopen if needed if (isCollaboratorOrHigher(comment.author_association)) { return Promise.resolve([]) }
// Check confidence level for automated reopening if (enhancedResult.confidence !== 'high' && !analysisResult.possibleRegression) { return Promise.resolve([]) }
// then reopen the issue promises.push( github.issues.update({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, state: 'open', }), )
// ... and add appropriate labels const labelsToAdd = [IssueLabel.PendingTriage] if (analysisResult.possibleRegression) { labelsToAdd.push(IssueLabel.PossibleRegression) }
promises.push( github.issues.addLabels({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, labels: labelsToAdd, }), ) }
event.waitUntil(Promise.all(promises)) return Promise.allSettled(promises) } catch (e) { console.error('Error processing issue comment', e) return null }}
async function handleIssueEdit(event: H3Event, { issue, repository }: IssuesEvent) { if (issue.user?.type === 'Bot') { return }
const issueLabels = issue.labels?.map(label => label.name) || [] const hasNeedsReproductionLabel = issueLabels.includes(IssueLabel.NeedsReproduction)
if (!hasNeedsReproductionLabel) { return null }
const github = useGitHubAPI(event) const promises: Array<Promise<unknown>> = []
try { const workersai = createWorkersAI({ binding: event.context.cloudflare.env.AI }) const { text: aiText } = await generateText({ model: workersai('@hf/nousresearch/hermes-2-pro-mistral-7b'), messages: [ { role: 'system', content: `You categorise issues in an open source project. Reported bugs must have enough information to reproduce them, either a full code example or a link to GitHub, StackBlitz or CodeSandbox. Do not answer with anything else other than valid JSON. Here's the json schema you must adhere to:\n${toXML(commentAnalysisSchema)}\n`, }, { role: 'user', content: JSON.stringify({ title: issue.title, body: getNormalizedIssueContent(issue.body || '') }) }, ], })
setHeader(event, 'x-issue-edit-analysis', aiText)
const analysisResult: CommentAnalysisResult = commentAnalysisResponseSchema.parse(JSON.parse(aiText || '{}'))
if (analysisResult.reproductionProvided) { // we can go ahead and remove the 'needs reproduction' label promises.push( github.issues.removeLabel({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, name: IssueLabel.NeedsReproduction, }), )
event.waitUntil(Promise.all(promises)) return Promise.allSettled(promises) } } catch (e) { console.error('Error processing issue edit', e) return null }
return null}
async function handleIssueLabeled(event: H3Event, payload: IssuesEvent) { // Type guard to ensure this is a labeled event if (payload.action !== 'labeled') { return null }
// TypeScript now knows this is IssuesLabeledEvent const { issue, label } = payload
// Only handle when the 'spam' label is added if (!label || label.name !== IssueLabel.Spam) { return null }
const runtimeConfig = useRuntimeConfig(event) const github = useGitHubAPI(event)
try { // Transfer the issue to the spam repository const result = await transferIssueToSpam( github, issue.node_id, runtimeConfig.github.targetRepositoryNodeId, )
return { transferred: true, issueNumber: result.transferredIssueNumber } } catch (e) { console.error('Error transferring spam-labeled issue', e) throw createError({ statusCode: 500, message: 'Error transferring spam-labeled issue', }) }}
async function handleNewIssue(event: H3Event, { action, issue, repository }: IssuesEvent) { if (action !== 'opened') return null
const workersai = createWorkersAI({ binding: event.context.cloudflare.env.AI }) const runtimeConfig = useRuntimeConfig(event)
let analyzedIssue: z.infer<typeof analyzedIssueSchema> | null = null
// Run the AI model and parse the response try { const { text: aiText } = await generateText({ model: workersai('@hf/nousresearch/hermes-2-pro-mistral-7b'), messages: [ { role: 'system', content: `You categorise issues in an open source project. Reported bugs must have enough information to reproduce them, either a full code example or a link to GitHub, StackBlitz or CodeSandbox. If the issue looks like spam (contains gibberish, nonsense, etc.), it is marked as spam. Do not mark issues as spam purely based on non-English content or bad grammar. Do not answer with anything else other than valid JSON. Here\`s the json schema you must adhere to:\n${toXML(responseSchema)}\n`, }, { role: 'user', content: JSON.stringify({ title: issue.title, body: getNormalizedIssueContent(issue.body || '') }) }, ], })
setHeader(event, 'x-ai-response', aiText)
if (!aiText) { console.error('Missing AI response') throw createError({ statusCode: 500, message: 'Missing AI response', }) }
try { analyzedIssue = analyzedIssueSchema.parse(JSON.parse(aiText.trim())) } catch (e) { console.error('Invalid AI response', aiText, e) throw createError({ statusCode: 500, message: 'Invalid AI response', }) } } catch (e) { if (isError(e)) { throw e }
console.error('Unknown AI error', e) throw createError({ statusCode: 500, message: 'Unknown AI error', }) }
const github = useGitHubAPI(event) const promises: Array<Promise<unknown>> = []
// Update the GitHub issue try { const labels: IssueLabel[] = []
if (analyzedIssue.issueType === IssueType.Spam) { promises.push( transferIssueToSpam( github, issue.node_id, runtimeConfig.github.targetRepositoryNodeId, ), ) } else { if (analyzedIssue.issueType === IssueType.Bug && !analyzedIssue.reproductionProvided) { labels.push(IssueLabel.NeedsReproduction) } if (analyzedIssue.issueType === IssueType.Bug && analyzedIssue.possibleRegression) { labels.push(IssueLabel.PossibleRegression) } if (analyzedIssue.nitro) { labels.push(IssueLabel.Nitro) } }
if (labels.length > 0) { promises.push( github.issues.addLabels({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, labels, }), ) }
if (['documentation', 'bug', 'enhancement'].includes(analyzedIssue.issueType)) { promises.push(github.issues.update({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, type: analyzedIssue.issueType, })) }
// Translate non-English issue titles to English if (analyzedIssue.spokenLanguage !== 'en' && analyzedIssue.issueType !== IssueType.Spam) { try { const ai = event.context.cloudflare.env.AI const res = await ai.run('@cf/meta/m2m100-1.2b', { text: issue.title, source_lang: analyzedIssue.spokenLanguage, target_lang: 'english', })
setHeader(event, 'x-translation-response', JSON.stringify(res))
const { translated_text } = translationResponseSchema.parse(res)
if (!translated_text || !translated_text.trim().length) return promises.push( github.issues.update({ owner: repository.owner.login, repo: repository.name, issue_number: issue.number, title: `[${analyzedIssue?.spokenLanguage}:translated] ${translated_text}`, }), ) } catch (e) { console.error('Error translating issue title', e) } }
event.waitUntil(Promise.all(promises)) setHeaders(event, { 'x-assigned-labels': JSON.stringify(labels), 'x-analysis': JSON.stringify(analyzedIssue), })
return Promise.allSettled(promises) } catch (e) { console.error('Error updating issue', e) throw createError({ statusCode: 500, message: 'Error updating issue', }) }}