Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

notificationPayload.ts

114 lines3.6 KB

1function dataFromNotificationResponse(response: unknown): Record<string, unknown> | null {
2 if (typeof response !== "object" || response === null) {
3 return null;
4 }
5 const notification = (response as { readonly notification?: unknown }).notification;
6 if (typeof notification !== "object" || notification === null) {
7 return null;
8 }
9 const request = (notification as { readonly request?: unknown }).request;
10 if (typeof request !== "object" || request === null) {
11 return null;
12 }
13 const content = (request as { readonly content?: unknown }).content;
14 if (typeof content !== "object" || content === null) {
15 return null;
16 }
17 const data = (content as { readonly data?: unknown }).data;
18 return typeof data === "object" && data !== null ? (data as Record<string, unknown>) : null;
20​
21function identifierFromNotificationResponse(response: unknown): string | null {
22 if (typeof response !== "object" || response === null) {
23 return null;
24 }
25 const notification = (response as { readonly notification?: unknown }).notification;
26 if (typeof notification !== "object" || notification === null) {
27 return null;
28 }
29 const request = (notification as { readonly request?: unknown }).request;
30 if (typeof request !== "object" || request === null) {
31 return null;
32 }
33 const identifier = (request as { readonly identifier?: unknown }).identifier;
34 return typeof identifier === "string" ? identifier : null;
36​
37function encodeThreadDeepLink(input: {
38 readonly environmentId: string;
39 readonly threadId: string;
40}): string | null {
41 if (input.environmentId.length === 0 || input.threadId.length === 0) {
42 return null;
43 }
44 return `/threads/${encodeURIComponent(input.environmentId)}/${encodeURIComponent(input.threadId)}`;
46​
47function normalizeThreadDeepLink(value: string): string | null {
48 if (
49 value.trim() !== value ||
50 value.startsWith("//") ||
51 value.includes("?") ||
52 value.includes("#")
53 ) {
54 return null;
55 }
56​
57 const parts = value.split("/");
58 if (parts.length !== 4 || parts[0] !== "" || parts[1] !== "threads") {
59 return null;
60 }
61​
62 try {
63 return encodeThreadDeepLink({
64 environmentId: decodeURIComponent(parts[2] ?? ""),
65 threadId: decodeURIComponent(parts[3] ?? ""),
66 });
67 } catch {
68 return null;
69 }
71​
72export function threadDeepLinkOnScreen(pathname: string): string | null {
73 const match = /^\/threads\/([^/]+)\/([^/]+)(?:\/|$)/.exec(pathname);
74 if (!match?.[1] || !match[2]) {
75 return null;
76 }
77 return `/threads/${match[1]}/${match[2]}`;
79​
80export function extractAgentNotificationDeepLink(response: unknown): string | null {
81 const data = dataFromNotificationResponse(response);
82 const deepLink = data?.deepLink;
83 if (typeof deepLink === "string") {
84 const normalizedDeepLink = normalizeThreadDeepLink(deepLink);
85 if (normalizedDeepLink) {
86 return normalizedDeepLink;
87 }
88 }
89​
90 const environmentId = data?.environmentId;
91 const threadId = data?.threadId;
92 if (typeof environmentId === "string" && typeof threadId === "string") {
93 return encodeThreadDeepLink({ environmentId, threadId });
94 }
95 return null;
97​
98export function routeAgentNotificationResponseOnce(input: {
99 readonly handledResponseIds: Set<string>;
100 readonly response: unknown;
101 readonly navigate: (deepLink: string) => void;
102}): void {
103 const responseId = identifierFromNotificationResponse(input.response);
104 if (responseId && input.handledResponseIds.has(responseId)) {
105 return;
106 }
107 if (responseId) {
108 input.handledResponseIds.add(responseId);
109 }
110 const deepLink = extractAgentNotificationDeepLink(input.response);
111 if (deepLink) {
112 input.navigate(deepLink);
113 }