Skip to content

Commit efe33fb

Browse files
committed
fix(web): navigate citations up to the URL limit
1 parent a81b449 commit efe33fb

3 files changed

Lines changed: 56 additions & 21 deletions

File tree

‎apps/web/src/lib/assistantCitationNavigation.test.ts‎

Lines changed: 50 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
1-
import { EnvironmentId, MessageId, ThreadId, type AssistantCitation } from "@t3tools/contracts";
1+
import {
2+
ASSISTANT_CITATION_MAX_COMMENT_LENGTH,
3+
ASSISTANT_CITATION_MAX_TEXT_LENGTH,
4+
EnvironmentId,
5+
MessageId,
6+
ThreadId,
7+
type AssistantCitation,
8+
} from "@t3tools/contracts";
9+
import {
10+
MAX_CITATION_HREF_LENGTH,
11+
formatAssistantCitationHref,
12+
parseAssistantCitationHref,
13+
} from "@t3tools/shared/assistantCitations";
214
import {
315
createMemoryHistory,
416
createRootRoute,
@@ -24,6 +36,21 @@ const citation: AssistantCitation = {
2436
suffix: "\nAfter.",
2537
};
2638

39+
const largeCitation = {
40+
...citation,
41+
text: "引".repeat(ASSISTANT_CITATION_MAX_TEXT_LENGTH),
42+
comment: "注".repeat(ASSISTANT_CITATION_MAX_COMMENT_LENGTH),
43+
start: 0,
44+
end: ASSISTANT_CITATION_MAX_TEXT_LENGTH,
45+
};
46+
const maximumCitation = {
47+
...largeCitation,
48+
messageId: MessageId.make(
49+
largeCitation.messageId +
50+
"m".repeat(MAX_CITATION_HREF_LENGTH - formatAssistantCitationHref(largeCitation).length),
51+
),
52+
};
53+
2754
function createCitationRouter(initialEntry = "/environment-one/thread-one") {
2855
const root = createRootRoute();
2956
const thread = createRoute({ getParentRoute: () => root, path: "/$environmentId/$threadId" });
@@ -34,25 +61,29 @@ function createCitationRouter(initialEntry = "/environment-one/thread-one") {
3461
}
3562

3663
describe("assistant citation navigation", () => {
37-
it("preserves encoded quote whitespace through router navigation and reload", async () => {
38-
const router = createCitationRouter();
39-
await router.navigate(assistantCitationNavigation(citation));
64+
it.each([citation, maximumCitation])(
65+
"preserves encoded quotes through router navigation and reload (%#)",
66+
async (citation) => {
67+
expect(parseAssistantCitationHref(formatAssistantCitationHref(citation))).toEqual(citation);
68+
const router = createCitationRouter();
69+
await router.navigate(assistantCitationNavigation(citation));
4070

41-
expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
42-
let href = router.history.location.href;
43-
for (let reload = 0; reload < 3; reload++) {
44-
const reloaded = createCitationRouter(href);
45-
await reloaded.load();
46-
await reloaded.navigate({
47-
to: "/$environmentId/$threadId",
48-
params: { environmentId: citation.environmentId, threadId: citation.threadId },
49-
hash: true,
50-
replace: true,
51-
});
52-
href = reloaded.history.location.href;
53-
expect(assistantCitationFromLocation(href)).toEqual(citation);
54-
}
55-
});
71+
expect(assistantCitationFromLocation(router.state.location.href)).toEqual(citation);
72+
let href = router.history.location.href;
73+
for (let reload = 0; reload < 3; reload++) {
74+
const reloaded = createCitationRouter(href);
75+
await reloaded.load();
76+
await reloaded.navigate({
77+
to: "/$environmentId/$threadId",
78+
params: { environmentId: citation.environmentId, threadId: citation.threadId },
79+
hash: true,
80+
replace: true,
81+
});
82+
href = reloaded.history.location.href;
83+
expect(assistantCitationFromLocation(href)).toEqual(citation);
84+
}
85+
},
86+
);
5687

5788
it("reactivates an identical citation instead of deduplicating its navigation", async () => {
5889
const router = createCitationRouter();

‎apps/web/src/lib/assistantCitationNavigation.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { pipe } from "effect/Function";
22
import type { AssistantCitation } from "@t3tools/contracts";
33
import {
4+
MAX_CITATION_HREF_LENGTH,
45
formatAssistantCitationHref,
56
parseAssistantCitationHref,
67
} from "@t3tools/shared/assistantCitations";
@@ -16,6 +17,9 @@ declare module "@tanstack/react-router" {
1617
}
1718

1819
const CITATION_HASH_PREFIX = "assistant-citation=";
20+
// Formatted citation URLs are ASCII, so byte and character lengths match.
21+
const MAX_CITATION_HASH_LENGTH =
22+
CITATION_HASH_PREFIX.length + 4 * Math.ceil(MAX_CITATION_HREF_LENGTH / 3);
1923

2024
/** Base64url keeps router hash normalization from decoding quote whitespace or source IDs. */
2125
export function assistantCitationHash(citation: AssistantCitation) {
@@ -26,7 +30,7 @@ export function assistantCitationFromLocation(href: string) {
2630
const hashIndex = href.indexOf("#");
2731
if (hashIndex === -1) return null;
2832
const hash = href.slice(hashIndex + 1);
29-
if (!hash.startsWith(CITATION_HASH_PREFIX) || hash.length > 140_000) return null;
33+
if (!hash.startsWith(CITATION_HASH_PREFIX) || hash.length > MAX_CITATION_HASH_LENGTH) return null;
3034
try {
3135
return pipe(
3236
hash.slice(CITATION_HASH_PREFIX.length),

‎packages/shared/src/assistantCitations.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const CITATION_PROTOCOL = "t3-citation:";
1010
const CITATION_HREF_PREFIX = `${CITATION_PROTOCOL}//v1/`;
1111
// Percent encoding needs up to nine characters per UTF-16 code unit.
1212
// The extra 16k is a shared budget for IDs, selectors, and URL syntax.
13-
const MAX_CITATION_HREF_LENGTH =
13+
export const MAX_CITATION_HREF_LENGTH =
1414
9 * (ASSISTANT_CITATION_MAX_TEXT_LENGTH + ASSISTANT_CITATION_MAX_COMMENT_LENGTH) + 16_000;
1515
const CITATION_LINK = new RegExp(
1616
String.raw`\[Assistant quote\]\((${CITATION_HREF_PREFIX}[^\s)]{1,${MAX_CITATION_HREF_LENGTH - CITATION_HREF_PREFIX.length}})\)`,

0 commit comments

Comments
 (0)