Skip to content
Merged
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: 25 additions & 15 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ import { useComposerMenuItems } from "./useComposerMenuItems";
import { useComposerMultilinePrompt } from "./useComposerMultilinePrompt";
import {
reconcileAttachmentContextReferences,
reconcileReviewCommentReferences,
useRetainedContextPayloads,
} from "./composerContextUndo";
import { ComposerBanner } from "./ComposerBanner";
Expand Down Expand Up @@ -1667,22 +1668,31 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)
setComposerDraftEnvironmentContexts(composerDraftTarget, nextEnvironments);
}

for (const comment of composerReviewComments) {
const contextId = reviewCommentContextId(comment.id);
if (!referenced.has(contextId)) {
retained.reviewComments.set(contextId, comment);
removeComposerDraftReviewComment(composerDraftTarget, comment.id);
}
}
const liveReviewIds = new Set<string>(
composerReviewComments.map((comment) => reviewCommentContextId(comment.id)),
);
for (const contextId of referenced) {
if (liveReviewIds.has(contextId)) continue;
const comment = retained.reviewComments.get(contextId);
if (comment) {
addComposerDraftReviewComment(composerDraftTarget, comment, { appendReference: false });
const reviewChanges = reconcileReviewCommentReferences({
referencedContextIds: referenced,
comments: composerReviewComments,
images: composerImages,
retained: retained.reviewComments,
});
for (const id of reviewChanges.idsToRemove)
removeComposerDraftReviewComment(composerDraftTarget, id);
for (const { comment, image } of reviewChanges.toRestore) {
if (image) {
const previewUrl = URL.createObjectURL(image.file);
if (
addComposerDraftImages(attachmentDraftTarget, [{ ...image, previewUrl }]).length === 0
) {
// A comment whose picture no longer fits stays deleted rather than sending without it.
if (activeThreadId) {
setThreadError(
activeThreadId,
`You can attach up to ${PROVIDER_SEND_TURN_MAX_ATTACHMENTS} files per message.`,
);
}
continue;
}
}
addComposerDraftReviewComment(composerDraftTarget, comment, { appendReference: false });
}

const attachmentChanges = reconcileAttachmentContextReferences({
Expand Down
52 changes: 51 additions & 1 deletion apps/web/src/components/chat/composerContextUndo.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import type { PreviewAnnotationPayload } from "@t3tools/contracts";
import { describe, expect, it } from "vite-plus/test";

import { buildPdfReviewComment } from "../files/PdfComments";
import type { RetainedContextPayloads } from "./composerContextUndo";
import type { ComposerImageAttachment } from "../../composerDraftStore";
import { buildMessageContext, previewAnnotationContextId } from "../../lib/composerContextRecords";
import {
buildMessageContext,
previewAnnotationContextId,
reviewCommentContextId,
} from "../../lib/composerContextRecords";
import {
reconcileAttachmentContextReferences,
reconcileReviewCommentReferences,
type RetainedAttachmentContextPayloads,
} from "./composerContextUndo";

Expand Down Expand Up @@ -89,3 +96,46 @@ describe("reconcileAttachmentContextReferences", () => {
]);
});
});

it("restores a PDF comment and its crop after undo", () => {
const comment = buildPdfReviewComment({
id: image.id,
filePath: "deck.pdf",
mark: { page: 1, x: 0.1, y: 0.2, width: 0.3, height: 0.4 },
quote: "",
text: "Explain the chart",
});
const retainedReviews: RetainedContextPayloads["reviewComments"] = new Map();
expect(
reconcileReviewCommentReferences({
referencedContextIds: new Set(),
comments: [comment],
images: [image],
retained: retainedReviews,
}).idsToRemove,
).toEqual([comment.id]);
const restoredReview = reconcileReviewCommentReferences({
referencedContextIds: new Set([reviewCommentContextId(comment.id)]),
comments: [],
images: [],
retained: retainedReviews,
});
expect(restoredReview.toRestore).toEqual([{ comment, image }]);
expect(
buildMessageContext({
terminalContexts: [],
reviewComments: restoredReview.toRestore.map((entry) => entry.comment),
previewAnnotations: [],
attachments: [
{ attachment: restoredReview.toRestore[0]!.image!, attachmentId: "uploaded-crop" },
],
})?.records,
).toEqual([
expect.objectContaining({
kind: "review-comment",
text: "Explain the chart",
rangeLabel: "p1, region x 10–40%, y 20–60%",
}),
expect.objectContaining({ kind: "image", attachmentId: "uploaded-crop" }),
]);
});
38 changes: 36 additions & 2 deletions apps/web/src/components/chat/composerContextUndo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import type {
import { useEffect, useState } from "react";

import type { ComposerFileAttachment, ComposerImageAttachment } from "../../composerDraftStore";
import { fileContextReference, previewAnnotationContextId } from "../../lib/composerContextRecords";
import {
fileContextReference,
previewAnnotationContextId,
reviewCommentContextId,
} from "../../lib/composerContextRecords";
import type { TerminalContextDraft } from "../../lib/terminalContext";
import type { ReviewCommentContext } from "../../reviewCommentContext";

Expand All @@ -23,7 +27,10 @@ export interface RetainedAttachmentContextPayloads {

export interface RetainedContextPayloads extends RetainedAttachmentContextPayloads {
terminals: Map<string, TerminalContextDraft>;
reviewComments: Map<string, ReviewCommentContext>;
reviewComments: Map<
string,
{ comment: ReviewCommentContext; image: ComposerImageAttachment | undefined }
>;
threads: Map<string, ThreadContextRecord>;
issues: Map<string, IssueContextRecord>;
environments: Map<string, EnvironmentContextRecord>;
Expand Down Expand Up @@ -111,3 +118,30 @@ export function reconcileAttachmentContextReferences(input: {

return { filesToRemove, filesToRestore, annotationIdsToRemove, annotationsToRestore };
}

export function reconcileReviewCommentReferences(input: {
referencedContextIds: ReadonlySet<string>;
comments: readonly ReviewCommentContext[];
images: readonly ComposerImageAttachment[];
retained: RetainedContextPayloads["reviewComments"];
}) {
const liveIds = new Set<string>(
input.comments.map((comment) => reviewCommentContextId(comment.id)),
);
const idsToRemove: string[] = [];
for (const comment of input.comments) {
const contextId = reviewCommentContextId(comment.id);
if (input.referencedContextIds.has(contextId)) continue;
input.retained.set(contextId, {
comment,
image: comment.pdfMark ? input.images.find((image) => image.id === comment.id) : undefined,
});
idsToRemove.push(comment.id);
}
const toRestore = [...input.referencedContextIds].flatMap((contextId) => {
if (liveIds.has(contextId)) return [];
const retained = input.retained.get(contextId);
return retained ? [retained] : [];
});
return { idsToRemove, toRestore };
}
Loading