Open History to see the last commit
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;
19}
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;
35}
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)}`;
45}
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 }
70}
71
72export function extractAgentNotificationDeepLink(response: unknown): string | null {
73 const data = dataFromNotificationResponse(response);
74 const deepLink = data?.deepLink;
75 if (typeof deepLink === "string") {
76 const normalizedDeepLink = normalizeThreadDeepLink(deepLink);
77 if (normalizedDeepLink) {
78 return normalizedDeepLink;
79 }
80 }
81
82 const environmentId = data?.environmentId;
83 const threadId = data?.threadId;
84 if (typeof environmentId === "string" && typeof threadId === "string") {
85 return encodeThreadDeepLink({ environmentId, threadId });
86 }
87 return null;
88}
89
90export function routeAgentNotificationResponseOnce(input: {
91 readonly handledResponseIds: Set<string>;
92 readonly response: unknown;
93 readonly navigate: (deepLink: string) => void;
94}): void {
95 const responseId = identifierFromNotificationResponse(input.response);
96 if (responseId && input.handledResponseIds.has(responseId)) {
97 return;
98 }
99 if (responseId) {
100 input.handledResponseIds.add(responseId);
101 }
102 const deepLink = extractAgentNotificationDeepLink(input.response);
103 if (deepLink) {
104 input.navigate(deepLink);
105 }
106}