fix: revert streaming, add dedup and format enforcement
This commit is contained in:
+43
-3
@@ -213,6 +213,46 @@ export const CreatePullRequestReview = type({
|
||||
.optional(),
|
||||
});
|
||||
|
||||
/**
|
||||
* Remove duplicate inline comments on the same file. Uses Jaccard similarity
|
||||
* on content words — if two comments on the same file share ≥40% of their
|
||||
* significant words they're almost certainly the same finding repeated.
|
||||
*/
|
||||
function deduplicateComments(comments: ReviewCommentInput[]): ReviewCommentInput[] {
|
||||
const STOPWORDS = new Set(["the", "this", "that", "with", "from", "have", "will", "when", "also", "both", "into", "than", "then", "they", "some", "more", "and", "but", "for", "are", "not", "use"]);
|
||||
const keywords = (text: string): Set<string> =>
|
||||
new Set((text ?? "").toLowerCase().split(/\W+/).filter((w) => w.length > 3 && !STOPWORDS.has(w)));
|
||||
|
||||
const jaccard = (a: Set<string>, b: Set<string>): number => {
|
||||
const inter = [...a].filter((w) => b.has(w)).length;
|
||||
const union = new Set([...a, ...b]).size;
|
||||
return union === 0 ? 0 : inter / union;
|
||||
};
|
||||
|
||||
const byPath = new Map<string, ReviewCommentInput[]>();
|
||||
for (const c of comments) {
|
||||
const group = byPath.get(c.path) ?? [];
|
||||
group.push(c);
|
||||
byPath.set(c.path, group);
|
||||
}
|
||||
|
||||
const result: ReviewCommentInput[] = [];
|
||||
for (const [, group] of byPath) {
|
||||
const kept: ReviewCommentInput[] = [];
|
||||
for (const candidate of group) {
|
||||
const ckw = keywords(candidate.body ?? "");
|
||||
const isDup = kept.some((k) => jaccard(ckw, keywords(k.body ?? "")) >= 0.4);
|
||||
if (isDup) {
|
||||
log.info(`deduped inline comment at ${candidate.path}:${candidate.line} — similar to existing comment on same file`);
|
||||
} else {
|
||||
kept.push(candidate);
|
||||
}
|
||||
}
|
||||
result.push(...kept);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/** Assemble the **Reviewed changes** preamble block from structured params. */
|
||||
function assemblePreamble(preamble: string, changes: string[]): string {
|
||||
const lines = [`**Reviewed changes** — ${preamble}`];
|
||||
@@ -350,15 +390,15 @@ export function CreatePullRequestReviewTool(ctx: ToolContext) {
|
||||
|
||||
runDiffCoveragePreflight({ ctx });
|
||||
|
||||
// Build review comments
|
||||
const reviewComments: ReviewCommentInput[] = comments.map((c) => {
|
||||
// Build review comments (deduplicate same-file same-topic comments first)
|
||||
const reviewComments: ReviewCommentInput[] = deduplicateComments(comments.map((c) => {
|
||||
let commentBody = fixDoubleEscapedString(c.body || "");
|
||||
if (c.suggestion !== undefined) {
|
||||
const block = "```suggestion\n" + c.suggestion + "\n```";
|
||||
commentBody = commentBody ? `${commentBody}\n\n${block}` : block;
|
||||
}
|
||||
return { path: c.path, line: c.line, side: c.side || "RIGHT", body: commentBody, start_line: c.start_line };
|
||||
});
|
||||
}));
|
||||
|
||||
let droppedComments: DroppedComment[] = [];
|
||||
let validComments: ReviewCommentInput[] = [];
|
||||
|
||||
Reference in New Issue
Block a user