Unverified Commit 1b89d7dc authored by Sam Ko's avatar Sam Ko Committed by GitHub

refactor(github): refactor triage-issues-with-ai to generateObject (#69696)

## Why

It turns out we should have been using `generateObject()` instead all
along for this use case.

![CleanShot 2024-09-04 at 14 18
31@2x](https://github.com/user-attachments/assets/033d51f3-12a1-47af-8967-67e365ba2ead)
parent afcad43b
import { z } from 'zod'
const userSchema = z
.object({
avatar_url: z.string().optional(),
deleted: z.boolean().optional(),
email: z.string().nullable().optional(),
events_url: z.string().optional(),
followers_url: z.string().optional(),
following_url: z.string().optional(),
gists_url: z.string().optional(),
gravatar_id: z.string().optional(),
html_url: z.string().optional(),
id: z.number(),
login: z.string(),
name: z.string().optional(),
node_id: z.string().optional(),
organizations_url: z.string().optional(),
received_events_url: z.string().optional(),
repos_url: z.string().optional(),
site_admin: z.boolean().optional(),
starred_url: z.string().optional(),
subscriptions_url: z.string().optional(),
type: z.enum(['Bot', 'User', 'Organization']).optional(),
url: z.string().optional(),
})
.strict()
const labelSchema = z
.object({
color: z.string(),
default: z.boolean(),
description: z.string().nullable(),
id: z.number(),
name: z.string(),
node_id: z.string(),
url: z.string(),
})
.strict()
const milestoneSchema = z
.object({
closed_at: z.string().nullable(),
closed_issues: z.number(),
created_at: z.string(),
creator: userSchema.nullable(),
description: z.string().nullable(),
due_on: z.string().nullable(),
html_url: z.string(),
id: z.number(),
labels_url: z.string(),
node_id: z.string(),
number: z.number(),
open_issues: z.number(),
state: z.enum(['closed', 'open']),
title: z.string(),
updated_at: z.string(),
url: z.string(),
})
.strict()
.describe('A collection of related issues.')
const permissionsSchema = z
.object({
actions: z.enum(['read', 'write']),
administration: z.enum(['read', 'write']),
content_references: z.enum(['read', 'write']),
contents: z.enum(['read', 'write']),
deployments: z.enum(['read', 'write']),
discussions: z.enum(['read', 'write']),
emails: z.enum(['read', 'write']),
environments: z.enum(['read', 'write']),
issues: z.enum(['read', 'write']),
keys: z.enum(['read', 'write']),
members: z.enum(['read', 'write']),
metadata: z.enum(['read', 'write']),
organization_administration: z.enum(['read', 'write']),
organization_hooks: z.enum(['read', 'write']),
organization_packages: z.enum(['read', 'write']),
organization_plan: z.enum(['read', 'write']),
organization_projects: z.enum(['read', 'write']),
organization_secrets: z.enum(['read', 'write']),
organization_self_hosted_runners: z.enum(['read', 'write']),
organization_user_blocking: z.enum(['read', 'write']),
packages: z.enum(['read', 'write']),
pages: z.enum(['read', 'write']),
pull_requests: z.enum(['read', 'write']),
repository_hooks: z.enum(['read', 'write']),
repository_projects: z.enum(['read', 'write']),
secret_scanning_alerts: z.enum(['read', 'write']),
secrets: z.enum(['read', 'write']),
security_events: z.enum(['read', 'write']),
security_scanning_alert: z.enum(['read', 'write']),
single_file: z.enum(['read', 'write']),
statuses: z.enum(['read', 'write']),
team_discussions: z.enum(['read', 'write']),
vulnerability_alerts: z.enum(['read', 'write']),
workflows: z.enum(['read', 'write']),
})
.strict()
const performedViaGitHubAppSchema = z
.object({
created_at: z.string().nullable(),
description: z.string().nullable(),
events: z.enum([
'branch_protection_rule',
'check_run',
'check_suite',
'code_scanning_alert',
'commit_comment',
'content_reference',
'create',
'delete',
'deployment',
'deployment_review',
'deployment_status',
'deploy_key',
'discussion',
'discussion_comment',
'fork',
'gollum',
'issues',
'issue_comment',
'label',
'member',
'membership',
'milestone',
'organization',
'org_block',
'page_build',
'project',
'project_card',
'project_column',
'public',
'pull_request',
'pull_request_review',
'pull_request_review_comment',
'push',
'registry_package',
'release',
'repository',
'repository_dispatch',
'secret_scanning_alert',
'star',
'status',
'team',
'team_add',
'watch',
'workflow_dispatch',
'workflow_run',
'reminder',
'pull_request_review_thread',
]),
external_url: z.string().nullable(),
html_url: z.string(),
id: z.number().nullable(),
name: z.string(),
node_id: z.string(),
owner: userSchema.nullable(),
permissions: permissionsSchema,
slug: z.string(),
updated_at: z.string().nullable(),
})
.strict()
const reactionSchema = z
.object({
'+1': z.number().optional(),
'-1': z.number().optional(),
confused: z.number().optional(),
eyes: z.number().optional(),
heart: z.number().optional(),
hooray: z.number().optional(),
laugh: z.number().optional(),
rocket: z.number().optional(),
total_count: z.number(),
url: z.string(),
})
.strict()
export const issueSchema = z
.object({
issue: z.object({
active_lock_reason: z
.enum(['resolved', 'off-topic', 'too heated', 'spam'])
.nullable(),
assignee: userSchema.nullable().optional(),
assignees: z.array(userSchema).optional(),
author_association: z.enum([
'COLLABORATOR',
'CONTRIBUTOR',
'FIRST_TIMER',
'FIRST_TIME_CONTRIBUTOR',
'MANNEQUIN',
'MEMBER',
'NONE',
'OWNER',
]),
body: z.string().nullable(),
closed_at: z.string().nullable(),
comments: z.number(),
comments_url: z.string(),
created_at: z.string(),
events_url: z.string(),
html_url: z.string(),
id: z.number(),
labels: z.array(labelSchema).default([]),
labels_url: z.string(),
locked: z.boolean(),
milestone: milestoneSchema.nullable(),
node_id: z.string(),
number: z.number(),
performed_via_github_app: performedViaGitHubAppSchema.nullable(),
reactions: reactionSchema,
repository_url: z.string(),
state: z.enum(['closed', 'open']),
state_reason: z.string().nullable(),
timeline_url: z.string(),
title: z.string(),
updated_at: z.string(),
url: z.string(),
user: userSchema.nullable(),
}),
})
.strict()
.describe('A GitHub issue.')
export type Issue = z.infer<typeof issueSchema>
...@@ -13,9 +13,9 @@ ...@@ -13,9 +13,9 @@
"@actions/core": "^1.10.1", "@actions/core": "^1.10.1",
"@actions/exec": "^1.1.1", "@actions/exec": "^1.1.1",
"@actions/github": "6.0.0", "@actions/github": "6.0.0",
"@ai-sdk/openai": "^0.0.54", "@ai-sdk/openai": "^0.0.55",
"@slack/web-api": "^7.3.4", "@slack/web-api": "^7.3.4",
"ai": "^3.3.19", "ai": "^3.3.26",
"slack-block-builder": "^2.8.0", "slack-block-builder": "^2.8.0",
"zod": "^3.23.8" "zod": "^3.23.8"
}, },
......
import { WebClient } from '@slack/web-api' import { WebClient } from '@slack/web-api'
import { info, setFailed } from '@actions/core' import { info, setFailed } from '@actions/core'
import { context } from '@actions/github' import { context } from '@actions/github'
import { generateText, tool } from 'ai' import { generateObject } from 'ai'
import { openai } from '@ai-sdk/openai' import { openai } from '@ai-sdk/openai'
import { z } from 'zod'
import { BlockCollection, Divider, Section } from 'slack-block-builder' import { BlockCollection, Divider, Section } from 'slack-block-builder'
import { getLatestCanaryVersion, getLatestVersion } from '../lib/util.mjs' import { getLatestCanaryVersion, getLatestVersion } from '../lib/util.mjs'
import { issueSchema } from '../lib/types'
async function main() { async function main() {
if (!process.env.OPENAI_API_KEY) throw new TypeError('OPENAI_API_KEY not set') if (!process.env.OPENAI_API_KEY) throw new TypeError('OPENAI_API_KEY not set')
...@@ -19,9 +19,6 @@ async function main() { ...@@ -19,9 +19,6 @@ async function main() {
const channel = '#next-info' const channel = '#next-info'
const issue = context.payload.issue const issue = context.payload.issue
const html_url = issue.html_url
const number = issue.number
const title = issue.title
let latestVersion: string let latestVersion: string
let latestCanaryVersion: string let latestCanaryVersion: string
...@@ -42,20 +39,17 @@ async function main() { ...@@ -42,20 +39,17 @@ async function main() {
const guidelines = await res.text() const guidelines = await res.text()
const result = await generateText({ const {
object: { explanation, isSevere, number, title, url },
} = await generateObject({
model: openai(model), model: openai(model),
maxToolRoundtrips: 1, schema: z.object({
tools: { explanation: z.string().describe('The explanation of the severity.'),
report_to_slack: tool({ isSevere: z.boolean().describe('Whether the issue is severe.'),
description: 'Report to Slack.', number: z.number().describe('The issue number.'),
parameters: issueSchema, title: z.string().describe('The issue title.'),
execute: async () => { url: z.string().describe('The issue URL.'),
// necessary to have an execute the tool automatically, }),
// so just putting an info message here as a placeholder
info('Reporting to Slack...')
},
}),
},
system: system:
'Your job is to determine the severity of a GitHub issue using the triage guidelines and the latest versions of Next.js. Succinctly explain why you chose the severity, without paraphrasing the triage guidelines. Report to Slack the explanation only if the severity is considered severe.', 'Your job is to determine the severity of a GitHub issue using the triage guidelines and the latest versions of Next.js. Succinctly explain why you chose the severity, without paraphrasing the triage guidelines. Report to Slack the explanation only if the severity is considered severe.',
prompt: prompt:
...@@ -66,14 +60,14 @@ async function main() { ...@@ -66,14 +60,14 @@ async function main() {
}) })
// the ai determined that the issue was severe enough to report on slack // the ai determined that the issue was severe enough to report on slack
if (result.roundtrips.length > 1) { if (isSevere) {
const blocks = BlockCollection([ const blocks = BlockCollection([
Section({ Section({
text: `:github2: <${html_url}|#${number}>: ${title}\n_Note: This issue was evaluated and reported on Slack with *${model}*._`, text: `:github2: <${url}|#${number}>: ${title}\n_Note: This issue was evaluated and reported on Slack with *${model}*._`,
}), }),
Divider(), Divider(),
Section({ Section({
text: `_${result.text}_`, text: `_${explanation}_`,
}), }),
]) ])
...@@ -89,7 +83,7 @@ async function main() { ...@@ -89,7 +83,7 @@ async function main() {
// the ai will also provide a reason why the issue was not severe enough to report on slack // the ai will also provide a reason why the issue was not severe enough to report on slack
info( info(
`result.text: ${result.text}\nhtml_url: ${html_url}\nnumber: ${number}\ntitle: ${title}` `Explanation: ${explanation}\nhtml_url: ${url}\nnumber: ${number}\ntitle: ${title}`
) )
} catch (error) { } catch (error) {
setFailed(error) setFailed(error)
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment