-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathlinksTool.ts
More file actions
104 lines (88 loc) · 2.52 KB
/
linksTool.ts
File metadata and controls
104 lines (88 loc) · 2.52 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { z } from 'zod';
export const PROVIDE_LINKS_DESCRIPTION =
'Surface 1-3 relevant links from the knowledge context for the user. Call this AFTER generating your full text response - never before. Skip it entirely for greetings, off-topic refusals, and pure event-schedule answers (event cards already carry links).';
export const PROVIDE_LINKS_INPUT_SCHEMA = z.object({
links: z
.array(
z.object({
label: z
.string()
.describe(
'Short user-friendly label. Strip prefixes like "FAQ:", "Prize Track:", "Starter Kit:". Max 40 chars.'
),
url: z
.string()
.describe('URL exactly as it appears in the knowledge context.'),
})
)
.max(3)
.describe(
'1-3 links most relevant to the response. Omit any that are only loosely related.'
),
});
function getAllowedHosts(): Set<string> {
const hosts = new Set<string>([
'hackdavis.io',
'hub.hackdavis.io',
'staging-hub.hackdavis.io',
]);
const baseUrl = process.env.BASE_URL;
if (baseUrl) {
try {
hosts.add(new URL(baseUrl).hostname.toLowerCase());
} catch {
// Ignore invalid BASE_URL; fall back to static allow-list.
}
}
return hosts;
}
function normalizeToRelativeHubPath(url: string): string | null {
const raw = url.trim();
if (!raw) return null;
if (raw.startsWith('//')) {
return null;
}
if (raw.startsWith('/')) {
return raw.replace(/^\/+/, '/');
}
if (raw.startsWith('#')) {
return `/${raw}`;
}
let parsed: URL;
try {
parsed = new URL(raw);
} catch {
return null;
}
const hostname = parsed.hostname.toLowerCase();
if (!getAllowedHosts().has(hostname)) {
return null;
}
const path = parsed.pathname || '/';
const search = parsed.search || '';
const hash = parsed.hash || '';
return `${path}${search}${hash}`;
}
export async function executeProvideLinks({
links,
}: {
links: Array<{ label: string; url: string }>;
}) {
const seen = new Set<string>();
const sanitized = links
.map((link) => {
const relativeUrl = normalizeToRelativeHubPath(link.url);
if (!relativeUrl) return null;
const label = link.label.trim().slice(0, 80);
if (!label) return null;
return { label, url: relativeUrl };
})
.filter((link): link is { label: string; url: string } => Boolean(link))
.filter((link) => {
if (seen.has(link.url)) return false;
seen.add(link.url);
return true;
})
.slice(0, 3);
return { links: sanitized };
}