|
| 1 | +import z from "zod" |
| 2 | +// Use Bun.$ (namespace access) instead of destructured $ to support test mocking |
| 3 | +import Bun from "bun" |
| 4 | +import os from "os" |
| 5 | +import path from "path" |
| 6 | +import { Tool } from "../../tool/tool" |
| 7 | +import { Installation } from "@/installation" |
| 8 | + |
| 9 | +const CATEGORY_LABELS = { |
| 10 | + bug: "bug", |
| 11 | + feature: "enhancement", |
| 12 | + improvement: "improvement", |
| 13 | + ux: "ux", |
| 14 | +} satisfies Record<"bug" | "feature" | "improvement" | "ux", string> |
| 15 | + |
| 16 | +export const FeedbackSubmitTool = Tool.define("feedback_submit", { |
| 17 | + description: |
| 18 | + "Submit user feedback as a GitHub issue to the altimate-code repository. " + |
| 19 | + "Creates an issue with appropriate labels and metadata. " + |
| 20 | + "Requires the `gh` CLI to be installed and authenticated.", |
| 21 | + parameters: z.object({ |
| 22 | + title: z.string().trim().min(1).describe("A concise title for the feedback issue"), |
| 23 | + category: z |
| 24 | + .enum(["bug", "feature", "improvement", "ux"]) |
| 25 | + .describe("The category of feedback: bug, feature, improvement, or ux"), |
| 26 | + description: z.string().trim().min(1).describe("Detailed description of the feedback"), |
| 27 | + include_context: z |
| 28 | + .boolean() |
| 29 | + .optional() |
| 30 | + .default(false) |
| 31 | + .describe("Whether to include session context (working directory basename, platform info) in the issue body"), |
| 32 | + }), |
| 33 | + async execute(args, ctx) { |
| 34 | + const ghNotInstalled = { |
| 35 | + title: "Feedback submission failed", |
| 36 | + metadata: { error: "gh_not_installed", issueUrl: "" }, |
| 37 | + output: |
| 38 | + "The `gh` CLI is not installed. Please install it to submit feedback:\n" + |
| 39 | + " - macOS: `brew install gh`\n" + |
| 40 | + " - Linux: https://github.com/cli/cli/blob/trunk/docs/install_linux.md\n" + |
| 41 | + " - Windows: `winget install GitHub.cli`\n\n" + |
| 42 | + "Then authenticate with: `gh auth login`", |
| 43 | + } |
| 44 | + |
| 45 | + // Check if gh CLI is available |
| 46 | + let ghVersion: string |
| 47 | + try { |
| 48 | + ghVersion = await Bun.$`gh --version`.quiet().nothrow().text() |
| 49 | + } catch { |
| 50 | + // ENOENT — gh binary not found on PATH |
| 51 | + return ghNotInstalled |
| 52 | + } |
| 53 | + if (!ghVersion.trim().startsWith("gh version")) { |
| 54 | + return ghNotInstalled |
| 55 | + } |
| 56 | + |
| 57 | + // Check if authenticated |
| 58 | + let authStatus: { exitCode: number } |
| 59 | + try { |
| 60 | + authStatus = await Bun.$`gh auth status`.quiet().nothrow() |
| 61 | + } catch { |
| 62 | + return { |
| 63 | + title: "Feedback submission failed", |
| 64 | + metadata: { error: "gh_auth_check_failed", issueUrl: "" }, |
| 65 | + output: |
| 66 | + "Failed to verify `gh` authentication status. Please check your installation with:\n" + |
| 67 | + " `gh auth status`", |
| 68 | + } |
| 69 | + } |
| 70 | + if (authStatus.exitCode !== 0) { |
| 71 | + return { |
| 72 | + title: "Feedback submission failed", |
| 73 | + metadata: { error: "gh_not_authenticated", issueUrl: "" }, |
| 74 | + output: |
| 75 | + "The `gh` CLI is not authenticated. Please run:\n" + |
| 76 | + " `gh auth login`\n\n" + |
| 77 | + "Then try submitting feedback again.", |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + // Collect metadata |
| 82 | + const version = Installation.VERSION |
| 83 | + const platform = process.platform |
| 84 | + const arch = process.arch |
| 85 | + const osRelease = os.release() |
| 86 | + |
| 87 | + // Build issue body |
| 88 | + let body = `${args.description}\n\n` |
| 89 | + body += `---\n\n` |
| 90 | + body += `### Metadata\n\n` |
| 91 | + body += `| Field | Value |\n` |
| 92 | + body += `|-------|-------|\n` |
| 93 | + body += `| CLI Version | ${version} |\n` |
| 94 | + body += `| Platform | ${platform} |\n` |
| 95 | + body += `| Architecture | ${arch} |\n` |
| 96 | + body += `| OS Release | ${osRelease} |\n` |
| 97 | + body += `| Category | ${args.category} |\n` |
| 98 | + |
| 99 | + if (args.include_context) { |
| 100 | + const cwdBasename = path.basename(process.cwd()) || "unknown" |
| 101 | + body += `| Working Directory | ${cwdBasename} |\n` |
| 102 | + body += `| Session ID | ${ctx.sessionID} |\n` |
| 103 | + } |
| 104 | + |
| 105 | + // Build labels |
| 106 | + const labels = ["user-feedback", "from-cli", CATEGORY_LABELS[args.category]] |
| 107 | + |
| 108 | + // Create the issue |
| 109 | + let issueResult: { stdout: Buffer; stderr: Buffer; exitCode: number } |
| 110 | + try { |
| 111 | + issueResult = await Bun.$`gh issue create --repo AltimateAI/altimate-code --title ${args.title} --body ${body} --label ${labels.join(",")}`.quiet().nothrow() |
| 112 | + } catch { |
| 113 | + return { |
| 114 | + title: "Feedback submission failed", |
| 115 | + metadata: { error: "issue_creation_failed", issueUrl: "" }, |
| 116 | + output: "Failed to create GitHub issue. The `gh` CLI encountered an unexpected error.\n\nPlease check your gh CLI installation and try again.", |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + const stdout = issueResult.stdout.toString().trim() |
| 121 | + const stderr = issueResult.stderr.toString().trim() |
| 122 | + |
| 123 | + if (issueResult.exitCode !== 0 || !stdout || !stdout.includes("github.com")) { |
| 124 | + const errorDetail = stderr || stdout || "No output from gh CLI" |
| 125 | + return { |
| 126 | + title: "Feedback submission failed", |
| 127 | + metadata: { error: "issue_creation_failed", issueUrl: "" }, |
| 128 | + output: `Failed to create GitHub issue.\n\n${errorDetail}\n\nPlease check your gh CLI authentication and try again.`, |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return { |
| 133 | + title: "Feedback submitted", |
| 134 | + metadata: { error: "", issueUrl: stdout }, |
| 135 | + output: `Feedback submitted successfully!\n\nIssue URL: ${stdout}`, |
| 136 | + } |
| 137 | + }, |
| 138 | +}) |
0 commit comments