Skip to content

Commit 94f574a

Browse files
committed
fix(shared): cite assistant messages with long IDs
1 parent 2359643 commit 94f574a

4 files changed

Lines changed: 56 additions & 24 deletions

File tree

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

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ import {
44
type AssistantCitation,
55
type ScopedThreadRef,
66
} from "@t3tools/contracts";
7+
import {
8+
formatAssistantCitationHref,
9+
parseAssistantCitationHref,
10+
} from "@t3tools/shared/assistantCitations";
711
import { QuoteIcon } from "lucide-react";
812
import { use, useEffect, useLayoutEffect, useRef, useState } from "react";
913
import { createPortal } from "react-dom";
@@ -111,6 +115,19 @@ export function AssistantSelectionToolbar({
111115
clear();
112116
return;
113117
}
118+
const citation = {
119+
version: 1,
120+
...threadRef,
121+
messageId: MessageId.make(messageId),
122+
...captured.selector,
123+
} satisfies AssistantCitation;
124+
if (
125+
citation.text.length <= ASSISTANT_CITATION_MAX_TEXT_LENGTH &&
126+
!parseAssistantCitationHref(formatAssistantCitationHref(citation))
127+
) {
128+
clear();
129+
return;
130+
}
114131
const rects = captured.range.getClientRects();
115132
const pointerOnText =
116133
pointer !== null &&
@@ -123,12 +140,7 @@ export function AssistantSelectionToolbar({
123140
);
124141
setSelection({
125142
sourceAnchor: { source: captured.source, range: captured.range, viewport },
126-
citation: {
127-
version: 1,
128-
...threadRef,
129-
messageId: MessageId.make(messageId),
130-
...captured.selector,
131-
},
143+
citation,
132144
position: resolveSelectionActionPosition({
133145
bounds: viewportRect,
134146
selectionRect: rects.item(rects.length - 1) ?? rect,

‎packages/contracts/src/assistantCitations.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ export const ASSISTANT_CITATION_CONTEXT_LENGTH = 32;
99
export const AssistantCitation = Schema.Struct({
1010
version: Schema.Literal(1),
1111
environmentId: EnvironmentId.check(Schema.isMaxLength(512)),
12-
threadId: ThreadId.check(Schema.isMaxLength(512)),
13-
messageId: MessageId.check(Schema.isMaxLength(512)),
12+
threadId: ThreadId,
13+
messageId: MessageId,
1414
text: Schema.String.check(
1515
Schema.isNonEmpty(),
1616
Schema.isMaxLength(ASSISTANT_CITATION_MAX_TEXT_LENGTH),

‎packages/shared/src/assistantCitations.test.ts‎

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,19 +59,30 @@ describe("assistant citation references", () => {
5959
expect(serializeAssistantCitation(legacyCitation)).toBe(`[Assistant quote](${legacyHref})`);
6060
});
6161

62-
it("round-trips complete quote data without a server origin", () => {
63-
const href = formatAssistantCitationHref(citation);
64-
expect(parseAssistantCitationHref(href)).toEqual(citation);
65-
expect(href).toMatch(
66-
/^t3-citation:\/\/v1\/environment%2Fremote\/thread%3Aone\/assistant%3Fone\?/,
67-
);
68-
expect(href).not.toContain("localhost");
69-
const marker = serializeAssistantCitation(citation);
70-
const prompt = `About ${marker}, explain this.`;
71-
expect(collectAssistantCitations(prompt)).toEqual([
72-
{ citation, source: marker, start: 6, end: 6 + marker.length },
73-
]);
74-
});
62+
it.each([
63+
[citation.threadId, citation.messageId],
64+
[citation.threadId, MessageId.make("m".repeat(512))],
65+
[citation.threadId, MessageId.make("m".repeat(513))],
66+
[citation.threadId, MessageId.make("m".repeat(565))],
67+
[ThreadId.make("thread:".repeat(100)), MessageId.make("provider-turn%3A".repeat(46))],
68+
] as const)(
69+
"round-trips complete quote data without a server origin (%#)",
70+
(threadId, messageId) => {
71+
const source = { ...citation, threadId, messageId };
72+
const href = formatAssistantCitationHref(source);
73+
expect(parseAssistantCitationHref(href)).toEqual(source);
74+
expect(href).toMatch(/^t3-citation:\/\/v1\/environment%2Fremote\//);
75+
expect(href).not.toContain("localhost");
76+
const marker = serializeAssistantCitation(source);
77+
const prompt = `About ${marker}, explain this.`;
78+
expect(collectAssistantCitations(prompt)).toEqual([
79+
{ citation: source, source: marker, start: 6, end: 6 + marker.length },
80+
]);
81+
expect(readProviderContext(expandAssistantCitationsForProvider(prompt))).toEqual([
82+
{ id: "assistant-quote-1", citation: source },
83+
]);
84+
},
85+
);
7586

7687
it.each([
7788
' Please keep "日本語 🚀", `cache[key]` & (a + b).\n\tWhy? #1 / 100%\r\n</assistant_citations> ',
@@ -103,7 +114,7 @@ describe("assistant citation references", () => {
103114
expect(renderAssistantCitationsAsText(prompt)).toBe(prompt);
104115
});
105116

106-
it("bounds selected text and surrounding context", () => {
117+
it("bounds citation URLs, selected text and surrounding context", () => {
107118
expect(
108119
parseAssistantCitationHref(
109120
formatAssistantCitationHref({
@@ -120,9 +131,17 @@ describe("assistant citation references", () => {
120131
expect(
121132
parseAssistantCitationHref(formatAssistantCitationHref({ ...citation, text: " " })),
122133
).toBeNull();
134+
expect(
135+
parseAssistantCitationHref(
136+
formatAssistantCitationHref({
137+
...citation,
138+
messageId: MessageId.make("m".repeat(160_000)),
139+
}),
140+
),
141+
).toBeNull();
123142
});
124143

125-
it("accepts complete 8k CJK quotes and comments with maximum-sized source selectors", () => {
144+
it("accepts complete 8k CJK quotes and comments with 512-character source IDs", () => {
126145
const largeCitation: AssistantCitation = {
127146
...citation,
128147
environmentId: EnvironmentId.make("環".repeat(512)),

‎packages/shared/src/assistantCitations.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ import * as Schema from "effect/Schema";
88

99
const CITATION_PROTOCOL = "t3-citation:";
1010
const CITATION_HREF_PREFIX = `${CITATION_PROTOCOL}//v1/`;
11-
// Percent encoding needs up to nine characters per UTF-16 code unit; 16k covers selectors.
11+
// Percent encoding needs up to nine characters per UTF-16 code unit.
12+
// The extra 16k is a shared budget for IDs, selectors, and URL syntax.
1213
const MAX_CITATION_HREF_LENGTH =
1314
9 * (ASSISTANT_CITATION_MAX_TEXT_LENGTH + ASSISTANT_CITATION_MAX_COMMENT_LENGTH) + 16_000;
1415
const CITATION_LINK = new RegExp(

0 commit comments

Comments
 (0)