Repository navigation
Expand file tree
/
Copy pathassistantCitationNavigation.ts
More file actions
51 lines (45 loc) · 1.65 KB
/
Copy pathassistantCitationNavigation.ts
File metadata and controls
51 lines (45 loc) · 1.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { pipe } from "effect/Function";
import type { AssistantCitation } from "@t3tools/contracts";
import {
formatAssistantCitationHref,
parseAssistantCitationHref,
} from "@t3tools/shared/assistantCitations";
import * as Base64Url from "effect/encoding/Base64Url";
import * as Result from "effect/Result";
import { randomUUID } from "./utils";
declare module "@tanstack/react-router" {
interface HistoryState {
assistantCitationActivation?: string;
}
}
const CITATION_HASH_PREFIX = "assistant-citation=";
/** Base64url keeps router hash normalization from decoding quote whitespace or source IDs. */
export function assistantCitationHash(citation: AssistantCitation) {
return `${CITATION_HASH_PREFIX}${Base64Url.encode(formatAssistantCitationHref(citation))}`;
}
export function assistantCitationFromLocation(href: string) {
const hashIndex = href.indexOf("#");
if (hashIndex === -1) return null;
const hash = href.slice(hashIndex + 1);
if (!hash.startsWith(CITATION_HASH_PREFIX) || hash.length > 140_000) return null;
try {
return pipe(
hash.slice(CITATION_HASH_PREFIX.length),
Base64Url.decodeString,
Result.getOrThrow,
parseAssistantCitationHref,
);
} catch {
return null;
}
}
/** A fresh activation lets the same link reveal its source again after dismissal. */
export function assistantCitationNavigation(citation: AssistantCitation) {
return {
to: "/$environmentId/$threadId" as const,
params: { environmentId: citation.environmentId, threadId: citation.threadId },
hash: assistantCitationHash(citation),
resetScroll: false,
state: { assistantCitationActivation: randomUUID() },
};
}