|
| 1 | +import type { OptionValues } from 'commander' |
| 2 | + |
| 3 | +import { chalk, log, logAndThrowError } from '../../utils/command-helpers.js' |
| 4 | +import { startSpinner, stopSpinner } from '../../lib/spinner.js' |
| 5 | +import type BaseCommand from '../base-command.js' |
| 6 | +import { createAgentsApi, type AgentsApi } from './api.js' |
| 7 | +import { formatDiff } from './utils.js' |
| 8 | + |
| 9 | +interface AgentDiffOptions extends OptionValues { |
| 10 | + page?: string |
| 11 | + perPage?: string |
| 12 | + session?: string |
| 13 | + cumulative?: boolean |
| 14 | + stripBinary?: boolean |
| 15 | + color?: boolean |
| 16 | +} |
| 17 | + |
| 18 | +const parsePositiveInt = (input: string | undefined, name: string): number | undefined => { |
| 19 | + if (input === undefined) return undefined |
| 20 | + if (!/^[1-9]\d*$/.test(input)) { |
| 21 | + throw new Error(`--${name} must be a positive integer`) |
| 22 | + } |
| 23 | + return Number.parseInt(input, 10) |
| 24 | +} |
| 25 | + |
| 26 | +const verifyRunnerExists = async (api: AgentsApi, id: string): Promise<void> => { |
| 27 | + try { |
| 28 | + await api.getAgentRunner(id) |
| 29 | + } catch (error_) { |
| 30 | + const error = error_ as Error & { status?: number } |
| 31 | + if (error.status === 404) { |
| 32 | + throw new Error(`Agent run not found: ${id}`) |
| 33 | + } |
| 34 | + throw error |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +export const agentsDiff = async (id: string, options: AgentDiffOptions, command: BaseCommand) => { |
| 39 | + if (!id) return logAndThrowError('Agent run ID is required') |
| 40 | + await command.authenticate() |
| 41 | + const api = createAgentsApi(command.netlify) |
| 42 | + |
| 43 | + const useColor = options.color !== false && process.stdout.isTTY |
| 44 | + |
| 45 | + if (options.session) { |
| 46 | + const kind = options.cumulative ? 'cumulative' : 'result' |
| 47 | + const spinner = startSpinner({ text: `Fetching session ${kind} diff...` }) |
| 48 | + try { |
| 49 | + const diff = options.cumulative |
| 50 | + ? await api.getSessionCumulativeDiff(id, options.session) |
| 51 | + : await api.getSessionResultDiff(id, options.session) |
| 52 | + stopSpinner({ spinner }) |
| 53 | + if (!diff) { |
| 54 | + await verifyRunnerExists(api, id) |
| 55 | + log(chalk.yellow('No diff available for this session.')) |
| 56 | + return |
| 57 | + } |
| 58 | + process.stdout.write(useColor ? formatDiff(diff) : diff) |
| 59 | + if (!diff.endsWith('\n')) process.stdout.write('\n') |
| 60 | + return |
| 61 | + } catch (error_) { |
| 62 | + stopSpinner({ spinner, error: true }) |
| 63 | + const error = error_ as Error |
| 64 | + if (error.message.startsWith('Agent run not found:')) { |
| 65 | + return logAndThrowError(error.message) |
| 66 | + } |
| 67 | + return logAndThrowError(`Failed to fetch diff: ${error.message}`) |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + let page: number | undefined |
| 72 | + let perPage: number | undefined |
| 73 | + try { |
| 74 | + page = parsePositiveInt(options.page, 'page') ?? 1 |
| 75 | + perPage = parsePositiveInt(options.perPage, 'per-page') |
| 76 | + } catch (error_) { |
| 77 | + return logAndThrowError((error_ as Error).message) |
| 78 | + } |
| 79 | + |
| 80 | + const spinner = startSpinner({ text: 'Fetching agent run diff...' }) |
| 81 | + try { |
| 82 | + const result = await api.getAgentRunnerDiff(id, { |
| 83 | + page, |
| 84 | + per_page: perPage, |
| 85 | + strip_binary: options.stripBinary !== false, |
| 86 | + }) |
| 87 | + stopSpinner({ spinner }) |
| 88 | + |
| 89 | + if (!result.data) { |
| 90 | + await verifyRunnerExists(api, id) |
| 91 | + log(chalk.yellow('No diff available for this agent run.')) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + process.stdout.write(useColor ? formatDiff(result.data) : result.data) |
| 96 | + if (!result.data.endsWith('\n')) process.stdout.write('\n') |
| 97 | + |
| 98 | + log() |
| 99 | + log(chalk.dim(formatFooter(result.page, result.perPage, result.total, result.hasNext))) |
| 100 | + return result |
| 101 | + } catch (error_) { |
| 102 | + stopSpinner({ spinner, error: true }) |
| 103 | + const error = error_ as Error |
| 104 | + if (error.message.startsWith('Agent run not found:')) { |
| 105 | + return logAndThrowError(error.message) |
| 106 | + } |
| 107 | + return logAndThrowError(`Failed to fetch diff: ${error.message}`) |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +const formatFooter = (page: number, perPage: number, total: number | undefined, hasNext: boolean): string => { |
| 112 | + const parts: string[] = [] |
| 113 | + if (total != null) { |
| 114 | + const start = (page - 1) * perPage + 1 |
| 115 | + const end = Math.min(page * perPage, total) |
| 116 | + parts.push(`Showing files ${start.toString()}-${end.toString()} of ${total.toString()}`) |
| 117 | + } else { |
| 118 | + parts.push(`Showing page ${page.toString()}`) |
| 119 | + } |
| 120 | + if (hasNext) { |
| 121 | + parts.push(`Use --page ${(page + 1).toString()} for the next page`) |
| 122 | + } |
| 123 | + return parts.join(' • ') |
| 124 | +} |
0 commit comments