Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | 59x 37x 37x 59x 34x 32x 2x 33x 33x 22x 22x 22x 4x 4x 3x 3x 3x 19x 6x 10x 2x 1x 6x 6x 6x 6x 1x 1x 6x 1x 1x 1x 2x 2x 2x 2x 6x 6x 6x 6x 6x 1x 5x 6x 6x 6x 6x 10x 2x 8x 3x 1x 2x 1x 1x 5x 3x 3x 1x 2x 5x 2x 3x 6x 6x 6x 6x 6x 6x 5x 5x 5x 6x 6x 6x 6x 6x 6x 6x | /**
* Seer Output Formatters
*
* Formatting utilities for Seer Autofix command output. All human-readable
* output is built as markdown and rendered via renderMarkdown().
*/
import type {
AutofixState,
RootCause,
SolutionArtifact,
} from "../../types/seer.js";
import { ApiError, SeerError } from "../errors.js";
import { cyan } from "./colors.js";
import { escapeMarkdownInline, renderMarkdown } from "./markdown.js";
// Spinner Frames
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
/**
* Get a spinner frame for the given tick count.
*
* @param tick - Current animation tick (cycles through frames)
* @returns Single spinner character for display
*/
export function getSpinnerFrame(tick: number): string {
const index = tick % SPINNER_FRAMES.length;
// biome-ignore lint/style/noNonNullAssertion: index is always valid due to modulo
return SPINNER_FRAMES[index]!;
}
// Progress Formatting
/** Maximum length for progress messages to fit in a single terminal line */
const MAX_PROGRESS_LENGTH = 300;
/**
* Truncate a progress message to fit in a single terminal line.
*
* @param message - Progress message to truncate
* @returns Truncated message with ellipsis if needed
*/
export function truncateProgressMessage(message: string): string {
if (message.length <= MAX_PROGRESS_LENGTH) {
return message;
}
return `${message.slice(0, MAX_PROGRESS_LENGTH - 3)}...`;
}
/**
* Format a progress message with spinner.
*
* @param message - Progress message to display
* @param tick - Spinner tick count
* @returns Formatted progress line
*/
export function formatProgressLine(message: string, tick: number): string {
const spinner = cyan(getSpinnerFrame(tick));
return `${spinner} ${message}`;
}
/** Agent block with optional message content */
type BlockWithMessage = { message?: { content?: string | null } };
/**
* Extract the first line of the latest block's message content.
* Returns undefined if no block message is available.
*/
function getBlockProgressMessage(
blocks: BlockWithMessage[]
): string | undefined {
const lastBlock = blocks.at(-1);
if (!lastBlock?.message?.content) {
return;
}
return lastBlock.message.content.split("\n")[0] || undefined;
}
/**
* Extract the latest progress message from autofix state.
*
* Handles both response formats:
* - Agent: message content from the last block's `message.content`
* - Legacy: progress messages from the last step's `progress[]` array
*
* @param state - Current autofix state
* @returns Latest progress message or default
*/
export function getProgressMessage(state: AutofixState): string {
const stateWithBlocks = state as AutofixState & {
blocks?: BlockWithMessage[];
};
Iif (stateWithBlocks.blocks && stateWithBlocks.blocks.length > 0) {
const msg = getBlockProgressMessage(stateWithBlocks.blocks);
if (msg) {
return msg;
}
}
if (state.steps && state.steps.length > 0) {
const currentStep = state.steps.at(-1);
if (currentStep?.progress && currentStep.progress.length > 0) {
const lastProgress = currentStep.progress.at(-1);
Eif (lastProgress?.message) {
return lastProgress.message;
}
}
}
switch (state.status) {
case "PROCESSING":
return "Analyzing issue...";
case "COMPLETED":
return "Analysis complete";
case "ERROR":
return "Analysis failed";
default:
return "Processing...";
}
}
// Root Cause Formatting
/**
* Build a markdown document for a single root cause.
*
* @param cause - Root cause to format
* @param index - Index for display (used as cause ID)
* @returns Markdown string for this cause
*/
function buildRootCauseMarkdown(cause: RootCause, index: number): string {
const lines: string[] = [];
lines.push(
`### Cause #${index}: ${escapeMarkdownInline(cause.description ?? "")}`
);
lines.push("");
if (cause.relevant_repos && cause.relevant_repos.length > 0) {
lines.push(`**Repository:** ${cause.relevant_repos.join(", ")}`);
lines.push("");
}
if (
cause.root_cause_reproduction &&
cause.root_cause_reproduction.length > 0
) {
lines.push("**Reproduction steps:**");
lines.push("");
for (const step of cause.root_cause_reproduction) {
lines.push(`**${escapeMarkdownInline(step.title)}**`);
lines.push("");
// code_snippet_and_analysis may itself contain markdown (code fences,
// inline code, etc.) — pass it through as-is so marked renders it.
lines.push(step.code_snippet_and_analysis);
lines.push("");
}
}
return lines.join("\n");
}
/**
* Format all root causes as rendered terminal output.
*
* @param causes - Array of root causes
* @returns Rendered terminal string
*/
export function formatRootCauseList(causes: RootCause[]): string {
const lines: string[] = [];
lines.push("## Root Cause Analysis Complete");
lines.push("");
if (causes.length === 0) {
lines.push("*No root causes identified.*");
} else {
for (let i = 0; i < causes.length; i++) {
const cause = causes[i];
Eif (cause) {
lines.push(buildRootCauseMarkdown(cause, i));
}
}
}
return renderMarkdown(lines.join("\n"));
}
// Error Messages
/**
* Create a SeerError from an API error status code and detail.
*
* @param status - HTTP status code
* @param detail - Error detail from API
* @param orgSlug - Organization slug for constructing settings URLs
* @returns SeerError if the status code indicates a Seer-specific error, null otherwise
*/
export function createSeerError(
status: number,
detail?: string,
orgSlug?: string
): SeerError | null {
if (status === 402) {
return new SeerError("no_budget", orgSlug);
}
if (status === 403) {
if (detail?.includes("not enabled")) {
return new SeerError("not_enabled", orgSlug);
}
if (detail?.includes("AI features")) {
return new SeerError("ai_disabled", orgSlug);
}
// Unrecognized 403 - return null to preserve original error detail
// (could be permission denied, rate limiting, etc.)
return null;
}
return null;
}
/**
* Convert an API error to a Seer-specific error or an ApiError.
*
* Returns a SeerError for known Seer issues (402/403 with specific detail),
* or preserves the error as an ApiError for other failures. Previously
* returned a plain Error which lost the status code and endpoint — causing
* CLI-N (84 users) where unrecognized 403s became untyped errors.
*
* @param status - HTTP status code
* @param detail - Error detail from API
* @param orgSlug - Organization slug for constructing settings URLs
* @returns SeerError for Seer-specific errors, or ApiError for other API errors
*/
export function handleSeerApiError(
status: number,
detail?: string,
orgSlug?: string
): Error {
const seerError = createSeerError(status, detail, orgSlug);
if (seerError) {
return seerError;
}
return new ApiError(formatAutofixError(status, detail), status, detail);
}
/**
* Format an error message for non-Seer autofix errors.
*
* Note: Seer-specific errors (402, 403) are handled by SeerError which
* provides actionable suggestions. This function handles other API errors.
*
* @param status - HTTP status code
* @param detail - Error detail from API
* @returns User-friendly error message
*/
export function formatAutofixError(status: number, detail?: string): string {
switch (status) {
case 404:
return "Issue not found.";
default:
return detail ?? "An error occurred with the autofix request.";
}
}
// Solution Formatting
/**
* Format a solution artifact as rendered terminal output.
*
* Renders a markdown document:
*
* ## Solution
*
* **Summary:** {one_line_summary}
*
* ### Steps to implement
*
* 1. **{title}**
*
* {description}
*
* @param solution - Solution artifact from autofix
* @returns Rendered terminal string
*/
export function formatSolution(solution: SolutionArtifact): string {
const lines: string[] = [];
lines.push("## Solution");
lines.push("");
lines.push(
`**Summary:** ${escapeMarkdownInline(solution.data.one_line_summary ?? "")}`
);
lines.push("");
if (solution.data.steps.length > 0) {
lines.push("### Steps to implement");
lines.push("");
for (let i = 0; i < solution.data.steps.length; i++) {
const step = solution.data.steps[i];
Eif (step) {
lines.push(`${i + 1}. **${escapeMarkdownInline(step.title)}**`);
lines.push("");
// step.description may contain markdown — pass it through as-is
lines.push(` ${step.description.split("\n").join("\n ")}`);
lines.push("");
}
}
}
return renderMarkdown(lines.join("\n"));
}
|