-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
67 lines (54 loc) · 2.09 KB
/
index.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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import {getOtid, otterClient, SpeechViewSummary} from './otter'
import {SearchResult, SpeechSummary} from 'otter.ai-api'
import * as caching from './caching'
// @ts-ignore
import * as alfy from 'alfy'
// todo way to invalidate cache?
const cacheTimeMs = parseInt(process.env.searchCacheTimeMs || "180000")
const summaryKey = "summaryKey"
const numberOfEntriesToFetch = parseInt(process.env.numberOfEntriesToFetch || "35")
const allSpeeches = async () =>
await caching.get('allItems', async () => {
const otterApi = await otterClient()
let speeches = await otterApi.getSpeeches({pageSize: numberOfEntriesToFetch})
saveSummary(speeches)
return speeches.map(it => ({
title: it.title || it.summary,
subtitle: renderTime(it.start_time),
arg: argFor(it),
}))
}, cacheTimeMs)
const matchingSpeeches = async (query: string) =>
await caching.get(query, async () => {
const otterApi = await otterClient()
let speeches = await otterApi.speechSearch(query)
saveSummary(speeches)
return speeches.map(it => ({
title: it.matched_transcripts.map(ts => ts.matched_transcript).join(" "),
subtitle: renderTime(it.start_time),
arg: argFor(it),
}))
}, cacheTimeMs)
const saveSummary = (speeches: Array<SpeechSummary | SearchResult>) => {
const summaries = speeches.map(it => buildViewSummary(it))
alfy.cache.set(summaryKey, summaries)
}
const renderTime = (timestamp: number) => new Date(timestamp * 1000).toLocaleString()
const argFor = (speech: SpeechSummary | SearchResult) => JSON.stringify({
selected: buildViewSummary(speech),
summaryKey,
})
const buildViewSummary = (speech: SpeechSummary | SearchResult): SpeechViewSummary => ({
id: speech.speech_id,
startTime: speech.start_time,
displayId: getOtid(speech),
});
(async () => {
// todo open otter web view variation
const query = alfy.input?.trim()
if (query) {
alfy.output(await matchingSpeeches(query))
} else {
alfy.output(await allSpeeches())
}
})()