| 1 | import { ChatMessage, SlashCommand } from "../../../index.js"; |
| 2 | import { removeQuotesAndEscapes } from "../../../util/index.js"; |
| 3 | import { renderChatMessage } from "../../../util/messageContent.js"; |
| 4 | |
| 5 | const PROMPT = ( |
| 6 | input: string, |
| 7 | title: string, |
| 8 | ) => `You will be asked to generate the body of a GitHub issue given a user request. You should follow these rules: |
| 9 | - Be descriptive but do not make up details |
| 10 | - If the the user request includes any code snippets that are relevant, reference them in code blocks |
| 11 | - Describe step by step how to reproduce the problem |
| 12 | - Describe the ideal solution to the problem |
| 13 | - Describe the expected behavior after the issue has been resolved |
| 14 | - This issue will be read by a team member |
| 15 | - Use markdown formatting, but you do not need to surround the entire body with triple backticks |
| 16 | {additional_instructions} |
| 17 | |
| 18 | Here is the user request: '${input}' |
| 19 | |
| 20 | Title: ${title} |
| 21 | |
| 22 | Body:\n\n`; |
| 23 | |
| 24 | const DraftIssueCommand: SlashCommand = { |
| 25 | name: "issue", |
| 26 | description: "Draft a GitHub issue", |
| 27 | run: async function* ({ input, llm, history, params, abortController }) { |
| 28 | if (params?.repositoryUrl === undefined) { |
| 29 | yield "This command requires a repository URL to be set in the config file."; |
| 30 | return; |
| 31 | } |
| 32 | let title = await llm.complete( |
| 33 | `Generate a title for the GitHub issue requested in this user input: '${input}'. Use no more than 20 words and output nothing other than the title. Do not surround it with quotes. The title is: `, |
| 34 | new AbortController().signal, |
| 35 | { maxTokens: 30 }, |
| 36 | ); |
| 37 | |
| 38 | title = `${removeQuotesAndEscapes(title.trim())}\n\n`; |
| 39 | yield title; |
| 40 | |
| 41 | let body = ""; |
| 42 | const messages: ChatMessage[] = [ |
| 43 | ...history.filter((msg) => msg.role !== "system"), |
| 44 | { role: "user", content: PROMPT(input, title) }, |
| 45 | ]; |
| 46 | |
| 47 | for await (const chunk of llm.streamChat( |
| 48 | messages, |
| 49 | abortController.signal, |
| 50 | )) { |
| 51 | body += chunk.content; |
| 52 | yield renderChatMessage(chunk); |
| 53 | } |
| 54 | |
| 55 | const url = `${params.repositoryUrl}/issues/new?title=${encodeURIComponent( |
| 56 | title, |
| 57 | )}&body=${encodeURIComponent(body)}`; |
| 58 | yield `\n\n[Link to draft of issue](${url})`; |
| 59 | }, |
| 60 | }; |
| 61 | |
| 62 | export default DraftIssueCommand; |