Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/commands/suggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ export async function suggestCommand(
commit?: boolean;
autoCommit?: boolean;
verbose?: boolean;
showDiff?: boolean;
model?: string;
stream?: boolean;
dryRun?: boolean;
Expand Down Expand Up @@ -176,9 +177,10 @@ export async function suggestCommand(
}

const profile = await buildProfile(config.historySize);
const preview = options.dryRun || options.showDiff ? truncateDiff(diffResult.diff, config.maxDiffSize) : undefined;

if (options.dryRun) {
const { diff: truncatedDiff, info: truncation } = truncateDiff(diffResult.diff, config.maxDiffSize);
const { diff: truncatedDiff, info: truncation } = preview!;
const vars = {
diff: truncatedDiff,
profile: formatProfile(profile),
Expand All @@ -199,6 +201,19 @@ export async function suggestCommand(
return;
}

if (options.showDiff) {
const { diff: truncatedDiff, info: truncation } = preview!;
console.log(pc.bold('Diff being analyzed:'));
console.log(pc.dim(truncatedDiff));
console.log('');
if (truncation.wasTruncated) {
console.log(pc.dim('The diff above is truncated to match maxDiffSize.'));
console.log('');
}
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

const analysisDiff = options.showDiff ? preview!.diff : diffResult.diff;
const analysisTruncation = options.showDiff ? preview!.info : undefined;
let apiKey: string;
try {
apiKey = assertApiKeyAvailable(config);
Expand All @@ -208,7 +223,7 @@ export async function suggestCommand(
}
while (true) {
let suggestions: Suggestion[];
let truncation: TruncationInfo | undefined;
let generatedTruncation: TruncationInfo | undefined;
let model: string;

if (options.stream) {
Expand All @@ -225,9 +240,16 @@ export async function suggestCommand(
model = config.model;
let accumulated = '';
try {
for await (const event of generateSuggestionsStream(config, diffResult.diff, profile, apiKey, streamProvider)) {
for await (const event of generateSuggestionsStream(
config,
analysisDiff,
profile,
apiKey,
streamProvider,
analysisTruncation,
)) {
if (event.kind === 'meta') {
truncation = event.truncation;
generatedTruncation = event.truncation;
continue;
}

Expand Down Expand Up @@ -265,9 +287,9 @@ export async function suggestCommand(
genSpinner.start('Generating commit suggestions...');

try {
const result = await generateSuggestions(config, diffResult.diff, profile, apiKey);
const result = await generateSuggestions(config, analysisDiff, profile, apiKey, analysisTruncation);
suggestions = result.suggestions;
truncation = result.truncation;
generatedTruncation = result.truncation;
model = result.model;
genSpinner.stop(pc.green('Suggestions generated:'));
} catch (err) {
Expand All @@ -279,11 +301,11 @@ export async function suggestCommand(
}

if (options.verbose) {
showVerboseInfo(model, profile, truncation);
showVerboseInfo(model, profile, generatedTruncation);
}

if (truncation) {
showTruncationWarning(truncation);
if (generatedTruncation) {
showTruncationWarning(generatedTruncation);
}

if (!options.stream) {
Expand Down
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ program
.option('--commit', 'Commit the selected suggestion', false)
.option('-y, --yes', 'Automatically select the first suggestion and skip prompts')
.option('-v, --verbose', 'Print diagnostic information about the suggestion request')
.option('-d, --show-diff', 'Print the diff content that will be sent to the LLM')
.option('-m, --model <model>', 'Override the configured LLM model for this invocation')
.option('--stream', 'Stream suggestions as they are generated (progressive output)')
.option('-n, --dry-run', 'Show the LLM input without generating suggestions')
Expand All @@ -83,6 +84,7 @@ program
commit: options.commit,
autoCommit: Boolean(options.yes || options.auto || globalOpts.yes || globalOpts.auto),
verbose: Boolean(options.verbose),
showDiff: Boolean(options.showDiff),
model: options.model,
stream: Boolean(options.stream),
dryRun: Boolean(options.dryRun),
Expand Down
11 changes: 8 additions & 3 deletions src/llm/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ export async function generateSuggestions(
diff: string,
profileParam?: StyleProfile,
apiKeyParam?: string,
precomputedTruncation?: TruncationInfo,
): Promise<{
suggestions: Suggestion[];
profile: StyleProfile;
Expand All @@ -46,8 +47,9 @@ export async function generateSuggestions(
}> {
const profile = profileParam ?? (await buildProfile(config.historySize));

// Truncate diff if it exceeds the configured limit
const { diff: truncatedDiff, info: truncation } = truncateDiff(diff, config.maxDiffSize);
const { diff: truncatedDiff, info: truncation } = precomputedTruncation
? { diff, info: precomputedTruncation }
: truncateDiff(diff, config.maxDiffSize);

const branch = getBranchName();
const profileStr = formatProfile(profile);
Expand Down Expand Up @@ -113,10 +115,13 @@ export async function* generateSuggestionsStream(
profileParam?: StyleProfile,
apiKeyParam?: string,
provider?: Provider,
precomputedTruncation?: TruncationInfo,
): AsyncGenerator<SuggestionStreamEvent> {
const profile = profileParam ?? (await buildProfile(config.historySize));

const { diff: truncatedDiff, info: truncation } = truncateDiff(diff, config.maxDiffSize);
const { diff: truncatedDiff, info: truncation } = precomputedTruncation
? { diff, info: precomputedTruncation }
: truncateDiff(diff, config.maxDiffSize);

const branch = getBranchName();
const profileStr = formatProfile(profile);
Expand Down
Loading