Skip to content

Commit e9e7372

Browse files
committed
fix(web): show citation comment URL limit errors
1 parent fe752f5 commit e9e7372

2 files changed

Lines changed: 50 additions & 14 deletions

File tree

‎apps/web/src/components/ComposerPromptEditorTiptap.citationDraft.browser.test.tsx‎

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
22
import {
3+
MAX_CITATION_HREF_LENGTH,
34
formatAssistantCitationHref,
45
parseAssistantCitationHref,
56
serializeAssistantCitation,
@@ -105,11 +106,25 @@ it("keeps an unsaved citation comment when a question takes the composer", async
105106
await expect.element(page.getByLabelText("Edit citation comment")).toBeInTheDocument();
106107
});
107108

108-
it.each(["save", "send", "question"] as const)(
109-
"rejects a comment exceeding the complete citation URL budget on %s",
110-
async (action) => {
111-
const sourceCitation = { ...citation, messageId: MessageId.make("界".repeat(17_750)) };
112-
const oversizedComment = "界".repeat(100);
109+
it.each([
110+
["save", 100],
111+
["send", 100],
112+
["question", 100],
113+
["save", 0],
114+
] as const)(
115+
"rejects a comment exceeding the citation URL budget on %s (%i characters available)",
116+
async (action, availableSpace) => {
117+
const source = { ...citation, messageId: MessageId.make("界".repeat(17_750)) };
118+
const sourceCitation = {
119+
...source,
120+
messageId: MessageId.make(
121+
source.messageId +
122+
"m".repeat(
123+
MAX_CITATION_HREF_LENGTH - formatAssistantCitationHref(source).length - availableSpace,
124+
),
125+
),
126+
};
127+
const oversizedComment = availableSpace === 0 ? "x" : "界".repeat(100);
113128
const prompt = `See ${serializeAssistantCitation(sourceCitation)} `;
114129
expect(parseAssistantCitationHref(formatAssistantCitationHref(sourceCitation))).toEqual(
115130
sourceCitation,
@@ -144,6 +159,12 @@ it.each(["save", "send", "question"] as const)(
144159
const comment = page.getByLabelText("Comment on selected text");
145160
await expect.element(comment).toBeVisible();
146161
await comment.fill(oversizedComment);
162+
await expect.element(comment).toHaveAttribute("aria-invalid", "true");
163+
const error = page.getByText(
164+
"The citation link is too long. Shorten the comment or quoted text.",
165+
);
166+
await expect.element(error).toBeVisible();
167+
await expect.element(page.getByRole("button", { name: "Save", exact: true })).toBeDisabled();
147168

148169
if (action === "question") {
149170
await screen.rerender(
@@ -162,11 +183,15 @@ it.each(["save", "send", "question"] as const)(
162183
});
163184
if (action !== "question") {
164185
await expect.element(comment).toHaveValue(oversizedComment);
165-
await comment.fill("ok");
186+
const acceptedComment = availableSpace === 0 ? "" : "ok";
187+
await comment.fill(acceptedComment);
188+
await expect.element(comment).not.toHaveAttribute("aria-invalid", "true");
189+
await expect.element(error).not.toBeInTheDocument();
190+
await expect.element(page.getByRole("button", { name: "Save", exact: true })).toBeEnabled();
166191
await userEvent.keyboard(action === "send" ? "{Control>}{Enter}{/Control}" : "{Enter}");
167192
await vi.waitFor(() => {
168193
expect(screen.container.querySelector("output")?.dataset.prompt).toBe(
169-
`See ${serializeAssistantCitation(withAssistantCitationComment(sourceCitation, "ok"))} `,
194+
`See ${serializeAssistantCitation(withAssistantCitationComment(sourceCitation, acceptedComment))} `,
170195
);
171196
expect(onSend).toHaveBeenCalledTimes(action === "send" ? 1 : 0);
172197
});

‎apps/web/src/components/chat/AssistantCitationCommentEditor.tsx‎

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11
import { ASSISTANT_CITATION_MAX_COMMENT_LENGTH, type AssistantCitation } from "@t3tools/contracts";
2+
import {
3+
formatAssistantCitationHref,
4+
parseAssistantCitationHref,
5+
withAssistantCitationComment,
6+
} from "@t3tools/shared/assistantCitations";
27
import { useState, type Ref } from "react";
38

49
import { Button } from "../ui/button";
@@ -20,11 +25,18 @@ export function AssistantCitationCommentEditor({
2025
}) {
2126
const [comment, setComment] = useState(citation.comment ?? "");
2227
const commentTooLong = comment.length > ASSISTANT_CITATION_MAX_COMMENT_LENGTH;
28+
const commentError = commentTooLong
29+
? `Comments can contain up to ${ASSISTANT_CITATION_MAX_COMMENT_LENGTH.toLocaleString()} characters.`
30+
: !parseAssistantCitationHref(
31+
formatAssistantCitationHref(withAssistantCitationComment(citation, comment)),
32+
)
33+
? "The citation link is too long. Shorten the comment or quoted text."
34+
: null;
2335
const submit = () => {
24-
if (!commentTooLong) onSubmit(comment);
36+
if (!commentError) onSubmit(comment);
2537
};
2638
const submitAndSend = () => {
27-
if (commentTooLong) return;
39+
if (commentError) return;
2840
if (onSubmitAndSend) {
2941
onSubmitAndSend(comment);
3042
} else {
@@ -48,7 +60,7 @@ export function AssistantCitationCommentEditor({
4860
ref={inputRef}
4961
aria-label="Comment on selected text"
5062
aria-description="Enter to save the citation comment; Command/Ctrl+Enter to save and send; Shift+Enter for a new line."
51-
aria-invalid={commentTooLong || undefined}
63+
aria-invalid={!!commentError || undefined}
5264
placeholder="Add an optional comment..."
5365
rows={2}
5466
className="field-sizing-content block max-h-40 min-h-16 w-full resize-none bg-transparent px-1 py-1.5 text-base outline-none placeholder:text-muted-foreground sm:text-sm"
@@ -73,10 +85,9 @@ export function AssistantCitationCommentEditor({
7385
}
7486
}}
7587
/>
76-
{commentTooLong ? (
88+
{commentError ? (
7789
<p role="status" className="pt-1 text-xs text-destructive">
78-
Comments can contain up to {ASSISTANT_CITATION_MAX_COMMENT_LENGTH.toLocaleString()}{" "}
79-
characters.
90+
{commentError}
8091
</p>
8192
) : null}
8293
<div className="mt-2 flex items-center justify-end gap-2">
@@ -90,7 +101,7 @@ export function AssistantCitationCommentEditor({
90101
</Button>
91102
<Button
92103
size="xs"
93-
disabled={commentTooLong}
104+
disabled={!!commentError}
94105
onPointerDown={(event) => event.preventDefault()}
95106
onClick={submit}
96107
>

0 commit comments

Comments
 (0)