-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathapi.js
211 lines (175 loc) · 5.77 KB
/
api.js
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import express from 'express';
import { handleError, sanitize } from '../helpers/routing.js';
import { contextHeader, getAppContext } from '../helpers/cipher.js';
import { recallFetch } from '../helpers/recall.js';
import session from '../session.js';
import { zoomApp } from '../config.js';
import db from '../helpers/database.js';
import { anthropicFetch } from '../helpers/anthropic.js';
const router = express.Router();
/*
* Gets the context of the Zoom App
*/
router.get('/context', async (req, res, next) => {
try {
sanitize(req);
const header = req.header(contextHeader);
const isZoom = !!(header && getAppContext(header));
return res.json({
isZoom,
});
} catch (e) {
next(handleError(e));
}
});
const validateAppContext = (req) => {
const header = req.header(contextHeader);
if (!header || !getAppContext(header)) {
const e = new Error('Unauthorized');
e.code = 401;
throw e;
}
};
/*
* Send's a Recall Bot to start recording the call
*/
router.post('/start-recording', session, async (req, res, next) => {
try {
sanitize(req);
validateAppContext(req);
if (!req.body.meetingUrl) {
return res.status(400).json({ error: 'Missing meetingUrl' });
}
console.log('recall bot start recording', req.body.meetingUrl);
// @see https://recallai.readme.io/reference/bot_create
const bot = await recallFetch('/api/v1/bot', {
method: 'POST',
body: JSON.stringify({
bot_name: 'AI Notetaker',
meeting_url: req.body.meetingUrl,
transcription_options: {
provider: 'default',
},
real_time_transcription: {
destination_url: `${zoomApp.publicUrl}/webhook/transcription?secret=${zoomApp.webhookSecret}`,
partial_results: true,
},
zoom: {
request_recording_permission_on_host_join: true,
require_recording_permission: true,
},
}),
});
console.log('recall bot', bot);
req.session.botId = bot.id;
return res.json({
botId: bot.id,
});
} catch (e) {
next(handleError(e));
}
});
/*
* Tells the Recall Bot to stop recording the call
*/
router.post('/stop-recording', session, async (req, res, next) => {
try {
sanitize(req);
validateAppContext(req);
if (!req.session.botId) {
return res.status(400).json({ error: 'Missing botId' });
}
await recallFetch(`/api/v1/bot/${req.session.botId}/leave_call`, {
method: 'POST',
});
console.log('recall bot stopped');
return res.json({});
} catch (e) {
next(handleError(e));
}
});
/*
* Gets the current state of the Recall Bot
*/
router.get('/recording-state', session, async (req, res, next) => {
try {
sanitize(req);
validateAppContext(req);
const botId = req.session.botId;
if (!botId) {
return res.status(400).json({ error: 'Missing botId' });
}
const bot = await recallFetch(`/api/v1/bot/${botId}`, {
method: 'GET',
});
const latestStatus = bot.status_changes.slice(-1)[0].code;
return res.json({
state: latestStatus,
transcript: db.transcripts[botId] || [],
});
} catch (e) {
next(handleError(e));
}
});
const PROMPTS = {
_template: `
Human: You are a virtual assistant, and you are taking notes for a meeting.
You are diligent, polite and slightly humerous at times.
Human: Here is the a transcript of the meeting, including the speaker's name:
Human: <transcript>
{{transcript}}
Human: </transcript>
Human: Only answer the following question directly, do not add any additional comments or information.
Human: {{prompt}}
Assistant:`,
general_summary: 'Can you summarize the meeting? Please be concise.',
action_items: 'What are the action items from the meeting?',
decisions: 'What decisions were made in the meeting?',
next_steps: 'What are the next steps?',
key_takeaways: 'What are the key takeaways?',
};
/*
* Gets a summary of the transcript using Anthropic's Claude model.
*/
router.post('/summarize', session, async (req, res, next) => {
try {
sanitize(req);
validateAppContext(req);
const botId = req.session.botId;
const prompt = PROMPTS[req.body.prompt];
if (!botId) {
return res.status(400).json({ error: 'Missing botId' });
}
if (!prompt) {
return res.status(400).json({ error: 'Missing prompt' });
}
const transcript = db.transcripts[botId] || [];
const finalTranscript = transcript
.filter((utterance) => utterance.is_final)
.map(
(utterance) =>
`Human: ${utterance.speaker || 'Unknown'}: ${utterance.words
.map((w) => w.text)
.join(' ')}`
)
.join('\n');
const completePrompt = PROMPTS._template
.replace('{{transcript}}', finalTranscript)
.replace('{{prompt}}', prompt);
console.log('completePrompt', completePrompt);
const data = await anthropicFetch('/v1/complete', {
method: 'POST',
body: JSON.stringify({
model: 'claude-2',
prompt: completePrompt,
max_tokens_to_sample: 1024,
}),
});
return res.json({
summary: data.completion,
});
} catch (e) {
next(handleError(e));
}
});
export default router;