Skip to content

Commit 3f987b8

Browse files
author
Aqsa Aqeel
committed
feat: handle large diffs by truncating before LLM input
1 parent 2ccd8c8 commit 3f987b8

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

cmd/cli/createMsg.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,33 @@ func CreateCommitMsg(Store *store.StoreMethods, dryRun bool, autoCommit bool) {
9191
return
9292
}
9393

94+
// Large diff handling
95+
const maxDiffChars = 8000 // can change as needed
96+
const maxDiffLines = 300
97+
98+
diffLines := strings.Split(changes, "\n")
99+
diffTooLarge := len(changes) > maxDiffChars || len(diffLines) > maxDiffLines
100+
101+
if diffTooLarge {
102+
pterm.Warning.Println("The diff is very large and may exceed the LLM's context window.")
103+
pterm.Info.Printf("Diff size: %d lines, %d characters.\n", len(diffLines), len(changes))
104+
pterm.Info.Println("Only the first part of the diff will be used for commit message generation.")
105+
106+
// Truncate the diff for LLM input
107+
truncatedLines := diffLines
108+
if len(diffLines) > maxDiffLines {
109+
truncatedLines = diffLines[:maxDiffLines]
110+
}
111+
truncatedDiff := strings.Join(truncatedLines, "\n")
112+
if len(truncatedDiff) > maxDiffChars {
113+
truncatedDiff = truncatedDiff[:maxDiffChars]
114+
}
115+
changes = truncatedDiff
116+
117+
pterm.Info.Printf("Truncated diff to %d lines, %d characters.\n", len(strings.Split(changes, "\n")), len(changes))
118+
pterm.Info.Println("Consider committing smaller changes for more accurate commit messages.")
119+
}
120+
94121
// Handle dry-run mode: display what would be sent to LLM without making API call
95122
if dryRun {
96123
pterm.Println()

0 commit comments

Comments
 (0)