forked from Asyboi/agentic-hack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnimble.ts
More file actions
100 lines (87 loc) · 2.32 KB
/
Copy pathnimble.ts
File metadata and controls
100 lines (87 loc) · 2.32 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
/**
* Nimble policy fetcher — live page extract via Nimble Web API.
* https://docs.nimbleway.com/api-reference/extract/extract
*/
export type FetchedPolicy = {
url: string;
body: string;
fetched_at: string;
status_code?: number;
};
type NimbleExtractResponse = {
status?: string;
status_code?: number;
url?: string;
data?: {
markdown?: string;
html?: string;
};
message?: string;
};
const EXTRACT_URL = "https://sdk.nimbleway.com/v1/extract";
export async function fetchPolicyPage(url: string): Promise<FetchedPolicy> {
const key = process.env.NIMBLE_API_KEY?.trim();
if (!key) {
throw new Error(
"NIMBLE_API_KEY is not set — add it to agentic-hack-1/.env to enable live policy fetching"
);
}
const res = await fetch(EXTRACT_URL, {
method: "POST",
headers: {
Authorization: `Bearer ${key}`,
"Content-Type": "application/json",
},
body: JSON.stringify({
url,
formats: ["markdown"],
render: false,
}),
});
const json = (await res.json()) as NimbleExtractResponse;
if (!res.ok) {
const msg =
typeof json.message === "string"
? json.message
: `Nimble extract failed (${res.status})`;
throw new Error(msg);
}
const body =
json.data?.markdown?.trim() ||
json.data?.html?.trim() ||
"";
if (!body) {
throw new Error(`Nimble returned empty body for ${url}`);
}
return {
url: json.url ?? url,
body: body.slice(0, 50_000),
fetched_at: new Date().toISOString(),
status_code: json.status_code,
};
}
export async function fetchPolicyPages(
urls: string[]
): Promise<FetchedPolicy[]> {
const unique = [...new Set(urls.filter(Boolean))];
if (unique.length === 0) return [];
const results = await Promise.all(
unique.map((url) =>
fetchPolicyPage(url).catch((e) => {
console.warn(`[nimble] fetch failed for ${url}`, e);
return {
url,
body: `[nimble error] ${e instanceof Error ? e.message : String(e)}`,
fetched_at: new Date().toISOString(),
} satisfies FetchedPolicy;
})
)
);
return results;
}
export function hashPolicyContent(bodies: string[]): string {
let h = 0;
const s = bodies.join("\n");
for (let i = 0; i < s.length; i++) h = (h * 31 + s.charCodeAt(i)) | 0;
return Math.abs(h).toString(16);
}