|
| 1 | +import { ActionContextOf, actor } from "actor-core"; |
| 2 | +import { LinearClient } from "@linear/sdk"; |
| 3 | +import { actorClient } from "./app"; |
| 4 | +import { WebhookIssue, WebhookComment } from "../linear-types"; |
| 5 | +import { CoreMessage, generateText } from "ai"; |
| 6 | +import { anthropic } from "@ai-sdk/anthropic"; |
| 7 | + |
| 8 | +interface IssueAgentState { |
| 9 | + messages: CoreMessage[]; |
| 10 | +} |
| 11 | + |
| 12 | +export const issueAgent = actor({ |
| 13 | + state: { |
| 14 | + messages: [], |
| 15 | + } as IssueAgentState, |
| 16 | + actions: { |
| 17 | + issueMention: async (c, appUserId: string, issue: WebhookIssue) => { |
| 18 | + // Do nothing |
| 19 | + }, |
| 20 | + issueEmojiReaction: async ( |
| 21 | + c, |
| 22 | + appUserId: string, |
| 23 | + issue: WebhookIssue, |
| 24 | + emoji: string, |
| 25 | + ) => { |
| 26 | + // Do nothing |
| 27 | + }, |
| 28 | + issueCommentMention: async ( |
| 29 | + c, |
| 30 | + appUserId: string, |
| 31 | + issue: WebhookIssue, |
| 32 | + comment: WebhookComment, |
| 33 | + ) => { |
| 34 | + c.log.info("mentioned in comment", { |
| 35 | + issue: issue.id, |
| 36 | + comment: comment.id, |
| 37 | + }); |
| 38 | + |
| 39 | + const linearClient = await buildLinearClient(appUserId); |
| 40 | + |
| 41 | + c.log.info("acknowledging comment", { |
| 42 | + issue: issue.id, |
| 43 | + comment: comment.id, |
| 44 | + }); |
| 45 | + await linearClient.createReaction({ |
| 46 | + commentId: comment.id, |
| 47 | + emoji: "👀", |
| 48 | + }); |
| 49 | + |
| 50 | + c.log.info("generating response to comment", { |
| 51 | + issue: issue.id, |
| 52 | + comment: comment.id, |
| 53 | + }); |
| 54 | + const fetchedComment = await linearClient.comment({ |
| 55 | + id: comment.id, |
| 56 | + }); |
| 57 | + const response = await prompt( |
| 58 | + c, |
| 59 | + `The user mentioned me in a comment:\n\n\`\`\`\n${comment.body}\n\`\`\``, |
| 60 | + ); |
| 61 | + await linearClient.createComment({ |
| 62 | + issueId: issue.id, |
| 63 | + // Must use the top-most comment ID |
| 64 | + parentId: fetchedComment.parentId ?? comment.id, |
| 65 | + body: response, |
| 66 | + }); |
| 67 | + }, |
| 68 | + issueAssignedToYou: async ( |
| 69 | + c, |
| 70 | + appUserId: string, |
| 71 | + issue: WebhookIssue, |
| 72 | + ) => { |
| 73 | + c.log.info(`Issue assigned to app: ${issue.title} (${issue.id})`); |
| 74 | + const linearClient = await buildLinearClient(appUserId); |
| 75 | + |
| 76 | + // Set issue as started |
| 77 | + c.log.info("finding issue state", { issue: issue.id }); |
| 78 | + const fetchedIssue = await linearClient.issue(issue.id); |
| 79 | + const state = await fetchedIssue.state; |
| 80 | + if ( |
| 81 | + state && |
| 82 | + state.type !== "started" && |
| 83 | + state.type !== "completed" && |
| 84 | + state.type !== "canceled" |
| 85 | + ) { |
| 86 | + const states = await linearClient.workflowStates(); |
| 87 | + const startedState = states.nodes.find( |
| 88 | + (s) => s.type === "started", |
| 89 | + ); |
| 90 | + if (startedState) { |
| 91 | + c.log.info("updating issue state", { |
| 92 | + issue: issue.id, |
| 93 | + state: startedState.id, |
| 94 | + }); |
| 95 | + await fetchedIssue.update({ stateId: startedState.id }); |
| 96 | + } else { |
| 97 | + c.log.warn("could not find started state"); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // Generate response |
| 102 | + c.log.info("generating response to issue", { issue: issue.id }); |
| 103 | + const response = await prompt( |
| 104 | + c, |
| 105 | + `I've been assigned to issue: "${issue.title}". The description is:\n\n\`\`\`\n${fetchedIssue.description}\n\`\`\``, |
| 106 | + ); |
| 107 | + await linearClient.createComment({ |
| 108 | + issueId: issue.id, |
| 109 | + body: response, |
| 110 | + }); |
| 111 | + }, |
| 112 | + issueCommentReaction: async ( |
| 113 | + c, |
| 114 | + appUserId: string, |
| 115 | + issue: WebhookIssue, |
| 116 | + comment: WebhookComment, |
| 117 | + emoji: string, |
| 118 | + ) => { |
| 119 | + // Do nothing |
| 120 | + }, |
| 121 | + issueUnassignedFromYou: async ( |
| 122 | + c, |
| 123 | + appUserId: string, |
| 124 | + issue: WebhookIssue, |
| 125 | + ) => { |
| 126 | + const linearClient = await buildLinearClient(appUserId); |
| 127 | + |
| 128 | + c.log.info("responding to issue unassigned", { issue: issue.id }); |
| 129 | + await linearClient.createComment({ |
| 130 | + issueId: issue.id, |
| 131 | + body: "I've been unassigned from this issue.", |
| 132 | + }); |
| 133 | + }, |
| 134 | + issueNewComment: async ( |
| 135 | + c, |
| 136 | + appUserId: string, |
| 137 | + issue: WebhookIssue, |
| 138 | + comment: WebhookComment, |
| 139 | + ) => { |
| 140 | + // Do nothing |
| 141 | + }, |
| 142 | + issueStatusChanged: async ( |
| 143 | + c, |
| 144 | + appUserId: string, |
| 145 | + issue: WebhookIssue, |
| 146 | + ) => { |
| 147 | + // Do nothing |
| 148 | + }, |
| 149 | + }, |
| 150 | +}); |
| 151 | + |
| 152 | +async function buildLinearClient(appUserId: string): Promise<LinearClient> { |
| 153 | + const accessToken = await actorClient.linearAppUser |
| 154 | + .get(appUserId) |
| 155 | + .getAccessToken(); |
| 156 | + return new LinearClient({ accessToken }); |
| 157 | +} |
| 158 | + |
| 159 | +const SYSTEM_PROMPT = ` |
| 160 | +You are a code generation assistant for Linear. Your job is to: |
| 161 | +
|
| 162 | +1. Read issue descriptions and generate appropriate code solutions |
| 163 | +2. Iterate on your code based on comments and feedback |
| 164 | +3. Provide brief explanations of your implementation |
| 165 | +
|
| 166 | +When responding: |
| 167 | +- Always provide the full requested code |
| 168 | +- Do not exclude parts of the code, always include the full code |
| 169 | +- Focus on delivering working code that meets requirements |
| 170 | +- Keep explanations concise and relevant |
| 171 | +- If no language is specified, use TypeScript |
| 172 | +
|
| 173 | +Your goal is to save developers time by providing ready-to-implement solutions. |
| 174 | +`; |
| 175 | + |
| 176 | +async function prompt(c: ActionContextOf<typeof issueAgent>, content: string) { |
| 177 | + c.log.debug("generating text", { messages: c.state.messages }); |
| 178 | + |
| 179 | + c.state.messages.push({ role: "user", content }); |
| 180 | + |
| 181 | + const { text, response } = await generateText({ |
| 182 | + model: anthropic("claude-4-opus-20250514"), |
| 183 | + system: SYSTEM_PROMPT, |
| 184 | + messages: c.state.messages, |
| 185 | + }); |
| 186 | + |
| 187 | + c.state.messages.push(...response.messages); |
| 188 | + |
| 189 | + return text; |
| 190 | +} |
0 commit comments