-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsummarize.ts
46 lines (34 loc) · 1.09 KB
/
summarize.ts
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
import storage, { getCachedData, setCachedData } from "~storage";
type SummaryType = "summary" | "takeaway"
async function summarize(
url: string,
summaryType: SummaryType = "summary"
): Promise<string> {
const kagiToken = await storage.get("kagiToken")
const engine = (await storage.get("kagiSummarizerEngine")) || "daphne"
console.log(
`fetching summary for ${url} with engine ${engine} and summary type ${summaryType}`
)
const key = `${url}-${engine}-${summaryType}`
const cachedData = await getCachedData(key)
if (cachedData) {
return cachedData
}
const headers = {
"Content-Type": "application/json",
Authorization: `Bot ${kagiToken}`
}
const response = await fetch("https://kagi.com/api/v0/summarize", {
method: "POST",
headers,
body: JSON.stringify({ url, engine, summary_type: summaryType })
})
if (!response.ok) {
throw new Error(`Failed to fetch summary: ${response.statusText}`)
}
const data = await response.json()
const output = data.data.output
await setCachedData(key, output)
return output
}
export default summarize