@ctchealth/plato-sdk 0.0.15 → 0.0.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1420 -0
- package/eslint.config.cjs +121 -0
- package/jest.config.ts +10 -0
- package/package.json +3 -4
- package/project.json +24 -0
- package/src/lib/{constants.d.ts → constants.ts} +3 -3
- package/src/lib/plato-intefaces.ts +431 -0
- package/src/lib/plato-sdk.ts +789 -0
- package/src/lib/utils.ts +72 -0
- package/tsconfig.json +22 -0
- package/tsconfig.lib.json +11 -0
- package/tsconfig.spec.json +10 -0
- package/src/index.js +0 -18
- package/src/index.js.map +0 -1
- package/src/lib/constants.js +0 -20
- package/src/lib/constants.js.map +0 -1
- package/src/lib/plato-intefaces.d.ts +0 -361
- package/src/lib/plato-intefaces.js +0 -172
- package/src/lib/plato-intefaces.js.map +0 -1
- package/src/lib/plato-sdk.d.ts +0 -193
- package/src/lib/plato-sdk.js +0 -643
- package/src/lib/plato-sdk.js.map +0 -1
- package/src/lib/utils.d.ts +0 -24
- package/src/lib/utils.js +0 -70
- package/src/lib/utils.js.map +0 -1
- /package/src/{index.d.ts → index.ts} +0 -0
|
@@ -0,0 +1,789 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) 2025 ctcHealth. All rights reserved.
|
|
3
|
+
*
|
|
4
|
+
* This file is part of the ctcHealth Plato Platform, a proprietary software system developed by ctcHealth.
|
|
5
|
+
*
|
|
6
|
+
* This source code and all related materials are confidential and proprietary to ctcHealth.
|
|
7
|
+
* Unauthorized access, use, copying, modification, distribution, or disclosure is strictly prohibited
|
|
8
|
+
* and may result in disciplinary action and civil and/or criminal penalties.
|
|
9
|
+
*
|
|
10
|
+
* This software is intended solely for authorized use within ctcHealth and its designated partners.
|
|
11
|
+
*
|
|
12
|
+
* For internal use only.
|
|
13
|
+
*/
|
|
14
|
+
import axios, { AxiosInstance } from 'axios';
|
|
15
|
+
import {
|
|
16
|
+
CallCreateDto,
|
|
17
|
+
CallDTO,
|
|
18
|
+
CreateSimulationDto,
|
|
19
|
+
CreationPhase,
|
|
20
|
+
SimulationRecordingsDto,
|
|
21
|
+
SimulationRecordingsQueryDto,
|
|
22
|
+
SimulationDetailsDto,
|
|
23
|
+
RecommendationsResponseDto,
|
|
24
|
+
RequestPdfUploadResponse,
|
|
25
|
+
PdfSlidesDto,
|
|
26
|
+
PdfSlideDto,
|
|
27
|
+
PdfSlidesAnalysisQueryDto,
|
|
28
|
+
CheckPdfStatusResponse,
|
|
29
|
+
AssistantImageDto,
|
|
30
|
+
ActiveCallState,
|
|
31
|
+
} from './plato-intefaces';
|
|
32
|
+
import Vapi from '@vapi-ai/web';
|
|
33
|
+
import { Call } from '@vapi-ai/web/dist/api';
|
|
34
|
+
import { checkFile, calculateHash, getPdfPageCount } from './utils';
|
|
35
|
+
import { MAX_PDF_FILE_SIZE, ALLOWED_PDF_MIME_TYPES, MAX_PDF_PAGES } from './constants';
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Encodes API key and user into a ClientToken (base64 JSON) for x-client-token header.
|
|
39
|
+
* The API decodes this to extract Token and User for validation.
|
|
40
|
+
*/
|
|
41
|
+
function encodeApiKeyClientToken(token: string, user: string): string {
|
|
42
|
+
const payload = {
|
|
43
|
+
Token: token,
|
|
44
|
+
User: user,
|
|
45
|
+
UserId: user,
|
|
46
|
+
Exp: Math.floor(Date.now() / 1000) + 86400, // 24h
|
|
47
|
+
};
|
|
48
|
+
const json = JSON.stringify(payload);
|
|
49
|
+
if (typeof Buffer !== 'undefined') {
|
|
50
|
+
return Buffer.from(json, 'utf8').toString('base64');
|
|
51
|
+
}
|
|
52
|
+
const bytes = new TextEncoder().encode(json);
|
|
53
|
+
let binary = '';
|
|
54
|
+
for (let i = 0; i < bytes.length; i++) {
|
|
55
|
+
binary += String.fromCharCode(bytes[i]);
|
|
56
|
+
}
|
|
57
|
+
return btoa(binary);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export interface ApiClientConfig {
|
|
61
|
+
baseUrl: string;
|
|
62
|
+
token?: string;
|
|
63
|
+
user?: string;
|
|
64
|
+
jwtToken?: string;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export interface ToolCall {
|
|
68
|
+
function: {
|
|
69
|
+
name: string;
|
|
70
|
+
arguments: {
|
|
71
|
+
slideNumber?: number;
|
|
72
|
+
progressObjectiveNumber?: number;
|
|
73
|
+
[key: string]: unknown;
|
|
74
|
+
};
|
|
75
|
+
};
|
|
76
|
+
id: string;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Event map for call events, providing strict typing for each event's payload.
|
|
81
|
+
* There are no inputs for the events and all of them return void.
|
|
82
|
+
*/
|
|
83
|
+
export interface CallEventMap {
|
|
84
|
+
'call-start': undefined;
|
|
85
|
+
'call-end': undefined;
|
|
86
|
+
'speech-start': undefined;
|
|
87
|
+
'speech-end': undefined;
|
|
88
|
+
error: Error;
|
|
89
|
+
message: {
|
|
90
|
+
type: string;
|
|
91
|
+
role: string;
|
|
92
|
+
transcript?: string;
|
|
93
|
+
transcriptType: 'final' | 'partial';
|
|
94
|
+
toolCallList?: ToolCall[]; // used in real-time messages to the client
|
|
95
|
+
toolCalls?: ToolCall[]; // used in conversation history
|
|
96
|
+
[key: string]: unknown;
|
|
97
|
+
};
|
|
98
|
+
'volume-level': number;
|
|
99
|
+
'call-details-ready': CallDTO;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export type CallEventNames = keyof CallEventMap;
|
|
103
|
+
|
|
104
|
+
export type CallEventListener<K extends CallEventNames> = (payload: CallEventMap[K]) => void;
|
|
105
|
+
|
|
106
|
+
export class PlatoApiClient {
|
|
107
|
+
private static readonly ACTIVE_CALL_STORAGE_KEY = 'plato_active_call';
|
|
108
|
+
|
|
109
|
+
private http: AxiosInstance;
|
|
110
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-function-type
|
|
111
|
+
private eventListeners: Partial<Record<CallEventNames, Function[]>> = {};
|
|
112
|
+
private callControllerInstance?: Vapi;
|
|
113
|
+
private eventsAttached = false;
|
|
114
|
+
private currentCallId?: string;
|
|
115
|
+
// Vapi-native events (events that Vapi SDK supports)
|
|
116
|
+
// Exclude our custom SDK events from this list
|
|
117
|
+
private vapiEventNames: Exclude<CallEventNames, 'call-details-ready'>[] = [
|
|
118
|
+
'call-start',
|
|
119
|
+
'call-end',
|
|
120
|
+
'speech-start',
|
|
121
|
+
'speech-end',
|
|
122
|
+
'error',
|
|
123
|
+
'message',
|
|
124
|
+
'volume-level',
|
|
125
|
+
];
|
|
126
|
+
// All event names including SDK-specific events
|
|
127
|
+
eventNames: CallEventNames[] = [
|
|
128
|
+
'call-start',
|
|
129
|
+
'call-end',
|
|
130
|
+
'speech-start',
|
|
131
|
+
'speech-end',
|
|
132
|
+
'error',
|
|
133
|
+
'message',
|
|
134
|
+
'volume-level',
|
|
135
|
+
'call-details-ready',
|
|
136
|
+
];
|
|
137
|
+
|
|
138
|
+
constructor(private config: ApiClientConfig) {
|
|
139
|
+
if (!config.baseUrl) {
|
|
140
|
+
throw new Error('baseUrl is required');
|
|
141
|
+
}
|
|
142
|
+
const token = config.token;
|
|
143
|
+
const user = config.user;
|
|
144
|
+
if (!token || !user) {
|
|
145
|
+
throw new Error('token and user are required');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
if (config.baseUrl.endsWith('/')) {
|
|
149
|
+
config.baseUrl = config.baseUrl.slice(0, -1);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
const clientToken = config.jwtToken ? config.jwtToken : encodeApiKeyClientToken(token, user);
|
|
153
|
+
|
|
154
|
+
this.http = axios.create({
|
|
155
|
+
baseURL: config.baseUrl,
|
|
156
|
+
headers: {
|
|
157
|
+
'x-client-token': clientToken,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* Update the JWT token for all subsequent requests.
|
|
164
|
+
* Useful when the token is refreshed or when switching user context.
|
|
165
|
+
*/
|
|
166
|
+
setJwtToken(jwtToken: string): void {
|
|
167
|
+
this.http.defaults.headers.common['x-client-token'] = jwtToken;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* Remove the JWT token from subsequent requests.
|
|
172
|
+
*/
|
|
173
|
+
clearJwtToken(): void {
|
|
174
|
+
delete this.http.defaults.headers.common['x-client-token'];
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Register a listener for a call event with strict typing.
|
|
179
|
+
* @param event Event name
|
|
180
|
+
* @param listener Listener function
|
|
181
|
+
*/
|
|
182
|
+
private on<K extends CallEventNames>(event: K, listener: CallEventListener<K>): void {
|
|
183
|
+
if (!this.eventListeners[event]) {
|
|
184
|
+
this.eventListeners[event] = [];
|
|
185
|
+
}
|
|
186
|
+
const listeners = this.eventListeners[event];
|
|
187
|
+
|
|
188
|
+
if (listeners) {
|
|
189
|
+
listeners.push(listener);
|
|
190
|
+
}
|
|
191
|
+
if (this.callControllerInstance && !this.eventsAttached) {
|
|
192
|
+
this.attachEvents();
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/**
|
|
197
|
+
* Remove a listener for a call event with strict typing.
|
|
198
|
+
* @param event Event name
|
|
199
|
+
* @param listener Listener function
|
|
200
|
+
*/
|
|
201
|
+
private off<K extends CallEventNames>(event: K, listener: CallEventListener<K>): void {
|
|
202
|
+
const listeners = this.eventListeners[event];
|
|
203
|
+
|
|
204
|
+
if (!listeners) return;
|
|
205
|
+
this.eventListeners[event] = listeners.filter(l => l !== listener);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Internal: Attach event listeners and propagate to registered listeners.
|
|
210
|
+
*/
|
|
211
|
+
private attachEvents(): void {
|
|
212
|
+
if (this.eventsAttached || !this.callControllerInstance) return;
|
|
213
|
+
this.eventsAttached = true;
|
|
214
|
+
const vapi = this.callControllerInstance;
|
|
215
|
+
|
|
216
|
+
// Only attach Vapi-native events to the Vapi instance
|
|
217
|
+
this.vapiEventNames.forEach(event => {
|
|
218
|
+
vapi.on(event, (payload: CallEventMap[CallEventNames]) => {
|
|
219
|
+
(this.eventListeners[event] || []).forEach(listener => listener(payload));
|
|
220
|
+
});
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Internal: Emit SDK-specific events that are not part of Vapi.
|
|
226
|
+
*/
|
|
227
|
+
private emit<K extends CallEventNames>(event: K, payload: CallEventMap[K]): void {
|
|
228
|
+
const listeners = this.eventListeners[event];
|
|
229
|
+
if (listeners) {
|
|
230
|
+
listeners.forEach(listener => listener(payload));
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/**
|
|
235
|
+
* Store active call state in localStorage for recovery purposes.
|
|
236
|
+
* @private
|
|
237
|
+
*/
|
|
238
|
+
private storeCallState(state: ActiveCallState): void {
|
|
239
|
+
try {
|
|
240
|
+
localStorage.setItem(PlatoApiClient.ACTIVE_CALL_STORAGE_KEY, JSON.stringify(state));
|
|
241
|
+
} catch (error) {
|
|
242
|
+
console.warn('Failed to store call state:', error);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
/**
|
|
247
|
+
* Retrieve active call state from localStorage.
|
|
248
|
+
* Validates the stored data and clears it if invalid.
|
|
249
|
+
* @private
|
|
250
|
+
* @returns The stored call state or null if not found or invalid
|
|
251
|
+
*/
|
|
252
|
+
private getStoredCallState(): ActiveCallState | null {
|
|
253
|
+
try {
|
|
254
|
+
const stored = localStorage.getItem(PlatoApiClient.ACTIVE_CALL_STORAGE_KEY);
|
|
255
|
+
if (!stored) {
|
|
256
|
+
return null;
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
const parsed = JSON.parse(stored) as ActiveCallState;
|
|
260
|
+
|
|
261
|
+
// Validate required fields
|
|
262
|
+
if (!parsed.callId || !parsed.externalCallId || !parsed.simulationId || !parsed.startedAt) {
|
|
263
|
+
console.warn('Invalid stored call state, clearing');
|
|
264
|
+
this.clearCallState();
|
|
265
|
+
return null;
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
return parsed;
|
|
269
|
+
} catch (error) {
|
|
270
|
+
console.warn('Failed to retrieve call state:', error);
|
|
271
|
+
return null;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Clear active call state from localStorage.
|
|
277
|
+
* @private
|
|
278
|
+
*/
|
|
279
|
+
private clearCallState(): void {
|
|
280
|
+
try {
|
|
281
|
+
localStorage.removeItem(PlatoApiClient.ACTIVE_CALL_STORAGE_KEY);
|
|
282
|
+
} catch (error) {
|
|
283
|
+
console.warn('Failed to clear call state:', error);
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* Check if a stored call is considered abandoned based on age.
|
|
289
|
+
* Calls older than 5 minutes are considered abandoned.
|
|
290
|
+
* @private
|
|
291
|
+
* @param state The call state to check
|
|
292
|
+
* @returns true if the call is abandoned, false otherwise
|
|
293
|
+
*/
|
|
294
|
+
private isCallAbandoned(state: ActiveCallState): boolean {
|
|
295
|
+
const ABANDONMENT_THRESHOLD_MS = 5 * 60 * 1000; // 5 minutes
|
|
296
|
+
const startedAt = new Date(state.startedAt).getTime();
|
|
297
|
+
const now = Date.now();
|
|
298
|
+
const age = now - startedAt;
|
|
299
|
+
|
|
300
|
+
return age > ABANDONMENT_THRESHOLD_MS;
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
/**
|
|
304
|
+
* Recover and clean up any abandoned calls from previous sessions.
|
|
305
|
+
*
|
|
306
|
+
* This method should be called during application initialization,
|
|
307
|
+
* typically in ngOnInit() or useEffect(). It detects calls that were
|
|
308
|
+
* active when the page was last refreshed and notifies the backend
|
|
309
|
+
* to process them if they're older than 5 minutes.
|
|
310
|
+
*
|
|
311
|
+
* The backend endpoint is idempotent, so calling this method multiple
|
|
312
|
+
* times for the same call is safe.
|
|
313
|
+
*
|
|
314
|
+
* @returns Promise<boolean> - true if an abandoned call was recovered and processed
|
|
315
|
+
*
|
|
316
|
+
* @example
|
|
317
|
+
* // In Angular component
|
|
318
|
+
* async ngOnInit(): Promise<void> {
|
|
319
|
+
* const recovered = await this.platoClient.recoverAbandonedCall();
|
|
320
|
+
* if (recovered) {
|
|
321
|
+
* console.log('Recovered abandoned call from previous session');
|
|
322
|
+
* }
|
|
323
|
+
* }
|
|
324
|
+
*
|
|
325
|
+
* @example
|
|
326
|
+
* // In React component
|
|
327
|
+
* useEffect(() => {
|
|
328
|
+
* platoClient.recoverAbandonedCall()
|
|
329
|
+
* .then(recovered => {
|
|
330
|
+
* if (recovered) {
|
|
331
|
+
* console.log('Recovered abandoned call');
|
|
332
|
+
* }
|
|
333
|
+
* })
|
|
334
|
+
* .catch(console.error);
|
|
335
|
+
* }, []);
|
|
336
|
+
*/
|
|
337
|
+
async recoverAbandonedCall(): Promise<boolean> {
|
|
338
|
+
try {
|
|
339
|
+
const storedState = this.getStoredCallState();
|
|
340
|
+
|
|
341
|
+
if (!storedState) {
|
|
342
|
+
return false;
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
if (!this.isCallAbandoned(storedState)) {
|
|
346
|
+
return false;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
console.log('Detected abandoned call, notifying backend:', storedState.callId);
|
|
350
|
+
|
|
351
|
+
try {
|
|
352
|
+
const response = await this.http.post('/api/v1/postcall/call-ended', {
|
|
353
|
+
callId: storedState.callId,
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
console.log('Backend notified of abandoned call:', response.data);
|
|
357
|
+
} catch (error) {
|
|
358
|
+
console.error('Failed to notify backend of abandoned call:', error);
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
this.clearCallState();
|
|
362
|
+
|
|
363
|
+
return true;
|
|
364
|
+
} catch (error) {
|
|
365
|
+
console.error('Error during call recovery:', error);
|
|
366
|
+
this.clearCallState();
|
|
367
|
+
return false;
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async createSimulation(createSimulationParams: CreateSimulationDto): Promise<{
|
|
372
|
+
simulationId: string;
|
|
373
|
+
phase: CreationPhase;
|
|
374
|
+
}> {
|
|
375
|
+
try {
|
|
376
|
+
const res = await this.http.post('/api/v1/simulation', {
|
|
377
|
+
...createSimulationParams,
|
|
378
|
+
});
|
|
379
|
+
return {
|
|
380
|
+
simulationId: res.data.simulationId,
|
|
381
|
+
phase: res.data.phase,
|
|
382
|
+
};
|
|
383
|
+
} catch (e) {
|
|
384
|
+
if (axios.isAxiosError(e)) {
|
|
385
|
+
console.error('Error creating simulation:', e.response?.data.message);
|
|
386
|
+
}
|
|
387
|
+
throw e;
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
async checkSimulationStatus(simulationId: string): Promise<{
|
|
392
|
+
phase: CreationPhase;
|
|
393
|
+
}> {
|
|
394
|
+
const res = await this.http.get(`/api/v1/simulation/status/${simulationId}`);
|
|
395
|
+
|
|
396
|
+
return res.data;
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
async getUserSimulations(): Promise<Array<{ simulationId: string; phase: CreationPhase }>> {
|
|
400
|
+
try {
|
|
401
|
+
const res = await this.http.get('/api/v1/simulation/user/simulations');
|
|
402
|
+
return res.data;
|
|
403
|
+
} catch (e) {
|
|
404
|
+
if (axios.isAxiosError(e)) {
|
|
405
|
+
console.error('Error getting user simulations:', e.response?.data.message);
|
|
406
|
+
}
|
|
407
|
+
throw e;
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
async getSimulationDetails(simulationId: string): Promise<SimulationDetailsDto> {
|
|
412
|
+
try {
|
|
413
|
+
const res = await this.http.get(`/api/v1/simulation/details/${simulationId}`);
|
|
414
|
+
return res.data as SimulationDetailsDto;
|
|
415
|
+
} catch (e) {
|
|
416
|
+
if (axios.isAxiosError(e)) {
|
|
417
|
+
console.error('Error getting simulation details:', e.response?.data.message);
|
|
418
|
+
}
|
|
419
|
+
throw e;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
|
|
423
|
+
/**
|
|
424
|
+
* Deletes a simulation (assistant) from the server and Vapi.
|
|
425
|
+
* @param simulationId - MongoDB ObjectId of the simulation
|
|
426
|
+
* @throws 404 if simulation not found
|
|
427
|
+
*/
|
|
428
|
+
async deleteSimulation(simulationId: string): Promise<void> {
|
|
429
|
+
try {
|
|
430
|
+
await this.http.delete(`/api/v1/simulation/${simulationId}`);
|
|
431
|
+
} catch (e) {
|
|
432
|
+
if (axios.isAxiosError(e)) {
|
|
433
|
+
console.error('Error deleting simulation:', e.response?.data.message);
|
|
434
|
+
}
|
|
435
|
+
throw e;
|
|
436
|
+
}
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
async getCallDetails(callId: string): Promise<CallDTO> {
|
|
440
|
+
try {
|
|
441
|
+
const res = await this.http.get(`/api/v1/simulation/call/${callId}`);
|
|
442
|
+
return res.data as CallDTO;
|
|
443
|
+
} catch (e) {
|
|
444
|
+
if (axios.isAxiosError(e)) {
|
|
445
|
+
console.error('Error getting call details:', e.response?.data.message);
|
|
446
|
+
}
|
|
447
|
+
throw e;
|
|
448
|
+
}
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async getCallRecordings(
|
|
452
|
+
queryParams: SimulationRecordingsQueryDto
|
|
453
|
+
): Promise<SimulationRecordingsDto[]> {
|
|
454
|
+
try {
|
|
455
|
+
const res = await this.http.get(`/api/v1/simulation/call/recordings`, {
|
|
456
|
+
params: queryParams,
|
|
457
|
+
});
|
|
458
|
+
return res.data as SimulationRecordingsDto[];
|
|
459
|
+
} catch (e) {
|
|
460
|
+
if (axios.isAxiosError(e)) {
|
|
461
|
+
console.error('Error getting call recordings:', e.response?.data.message);
|
|
462
|
+
}
|
|
463
|
+
throw e;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
async getCallRecording(callId: string): Promise<string> {
|
|
468
|
+
try {
|
|
469
|
+
const res = await this.http.get(`/api/v1/simulation/call/recordings/${callId}`);
|
|
470
|
+
return res.data as string;
|
|
471
|
+
} catch (e) {
|
|
472
|
+
if (axios.isAxiosError(e)) {
|
|
473
|
+
console.error('Error getting call recording:', e.response?.data.message);
|
|
474
|
+
}
|
|
475
|
+
throw e;
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/**
|
|
480
|
+
* Remove all listeners for all call events.
|
|
481
|
+
*/
|
|
482
|
+
private removeAllEventListeners(): void {
|
|
483
|
+
if (!this.callControllerInstance) return;
|
|
484
|
+
this.eventNames.forEach(event => {
|
|
485
|
+
(this.eventListeners[event] || []).forEach(listener => {
|
|
486
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
487
|
+
// @ts-expect-error
|
|
488
|
+
this.callControllerInstance?.off(event, listener);
|
|
489
|
+
});
|
|
490
|
+
this.eventListeners[event] = [];
|
|
491
|
+
});
|
|
492
|
+
this.eventsAttached = false;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
async startCall(simulationId: string) {
|
|
496
|
+
// Check for any previous call state before starting new call
|
|
497
|
+
// If found, notify backend to ensure it gets processed
|
|
498
|
+
const storedState = this.getStoredCallState();
|
|
499
|
+
if (storedState) {
|
|
500
|
+
console.log(
|
|
501
|
+
'Found previous call state, notifying backend before starting new call:',
|
|
502
|
+
storedState.callId
|
|
503
|
+
);
|
|
504
|
+
try {
|
|
505
|
+
await this.http.post('/api/v1/postcall/call-ended', {
|
|
506
|
+
callId: storedState.callId,
|
|
507
|
+
});
|
|
508
|
+
console.log('Backend notified of previous call');
|
|
509
|
+
} catch (error) {
|
|
510
|
+
console.error('Failed to notify backend of previous call:', error);
|
|
511
|
+
}
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
// Now clear the state before starting new call
|
|
515
|
+
this.clearCallState();
|
|
516
|
+
|
|
517
|
+
this.callControllerInstance = new Vapi(
|
|
518
|
+
'f07d17ec-d4e6-487d-a0b9-0539c01aecbb',
|
|
519
|
+
'https://db41aykk1gw9e.cloudfront.net' // base url
|
|
520
|
+
);
|
|
521
|
+
if (!this.eventsAttached) {
|
|
522
|
+
this.attachEvents();
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
// Internal call-end listener
|
|
526
|
+
this.callControllerInstance.on('call-end', () => {
|
|
527
|
+
this.onCallEnded().catch(error => {
|
|
528
|
+
console.error('Error in onCallEnded: ', error);
|
|
529
|
+
});
|
|
530
|
+
});
|
|
531
|
+
|
|
532
|
+
const { data } = await this.http.get(`/api/v1/simulation/${simulationId}`);
|
|
533
|
+
const assistantId = data as string;
|
|
534
|
+
const call: Call | null = await this.callControllerInstance.start(assistantId);
|
|
535
|
+
|
|
536
|
+
if (!call || !call.assistantId) {
|
|
537
|
+
throw new Error('Cannot start a call, please try again later');
|
|
538
|
+
}
|
|
539
|
+
try {
|
|
540
|
+
const apiCall = await this.createCall({
|
|
541
|
+
callId: call.id,
|
|
542
|
+
assistantId: call.assistantId,
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
// Store the callId for use in onCallEnded
|
|
546
|
+
this.currentCallId = apiCall._id;
|
|
547
|
+
|
|
548
|
+
// Store call state in localStorage for recovery
|
|
549
|
+
this.storeCallState({
|
|
550
|
+
callId: apiCall._id,
|
|
551
|
+
externalCallId: call.id,
|
|
552
|
+
simulationId: simulationId,
|
|
553
|
+
startedAt: new Date().toISOString(),
|
|
554
|
+
version: 1,
|
|
555
|
+
});
|
|
556
|
+
|
|
557
|
+
// Return stopCall, callId, and event subscription methods with strict typing
|
|
558
|
+
return {
|
|
559
|
+
stopCall: () => {
|
|
560
|
+
this.callControllerInstance?.stop();
|
|
561
|
+
this.removeAllEventListeners();
|
|
562
|
+
this.clearCallState();
|
|
563
|
+
},
|
|
564
|
+
callId: apiCall._id,
|
|
565
|
+
/**
|
|
566
|
+
* Subscribe to call events for this call with strict typing.
|
|
567
|
+
* @param event Event name
|
|
568
|
+
* @param listener Listener function
|
|
569
|
+
*/
|
|
570
|
+
on: <K extends CallEventNames>(event: K, listener: CallEventListener<K>) =>
|
|
571
|
+
this.on(event, listener),
|
|
572
|
+
/**
|
|
573
|
+
* Unsubscribe from call events for this call with strict typing.
|
|
574
|
+
* @param event Event name
|
|
575
|
+
* @param listener Listener function
|
|
576
|
+
*/
|
|
577
|
+
off: <K extends CallEventNames>(event: K, listener: CallEventListener<K>) =>
|
|
578
|
+
this.off(event, listener),
|
|
579
|
+
};
|
|
580
|
+
} catch (e) {
|
|
581
|
+
this.callControllerInstance?.stop();
|
|
582
|
+
this.removeAllEventListeners();
|
|
583
|
+
this.clearCallState();
|
|
584
|
+
throw e;
|
|
585
|
+
}
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
private async onCallEnded(): Promise<void> {
|
|
589
|
+
let callIdForRetry: string | undefined;
|
|
590
|
+
try {
|
|
591
|
+
if (!this.currentCallId) {
|
|
592
|
+
return;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
callIdForRetry = this.currentCallId;
|
|
596
|
+
|
|
597
|
+
// First attempt
|
|
598
|
+
let response = await this.http.post('/api/v1/postcall/call-ended', {
|
|
599
|
+
callId: callIdForRetry,
|
|
600
|
+
});
|
|
601
|
+
|
|
602
|
+
// If status is "processing", retry once immediately
|
|
603
|
+
if (response.data?.status === 'processing') {
|
|
604
|
+
response = await this.http.post('/api/v1/postcall/call-ended', {
|
|
605
|
+
callId: callIdForRetry,
|
|
606
|
+
});
|
|
607
|
+
}
|
|
608
|
+
|
|
609
|
+
// After onCallEnded completes successfully, fetch and emit call details
|
|
610
|
+
try {
|
|
611
|
+
const callDetails = await this.getCallDetails(callIdForRetry);
|
|
612
|
+
this.emit('call-details-ready', callDetails);
|
|
613
|
+
} catch (error) {
|
|
614
|
+
console.error('Error fetching call details after call ended:', error);
|
|
615
|
+
// Don't throw - we don't want to break the onCallEnded flow
|
|
616
|
+
}
|
|
617
|
+
} catch {
|
|
618
|
+
// Silently handle errors in post-call processing
|
|
619
|
+
} finally {
|
|
620
|
+
// Clean up the callId after processing (success or failure)
|
|
621
|
+
this.currentCallId = undefined;
|
|
622
|
+
// Clear stored call state after normal call end
|
|
623
|
+
this.clearCallState();
|
|
624
|
+
}
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
private async createCall(payload: CallCreateDto) {
|
|
628
|
+
const response = await this.http.post('/api/v1/simulation/call', {
|
|
629
|
+
...payload,
|
|
630
|
+
});
|
|
631
|
+
|
|
632
|
+
return response.data as CallDTO;
|
|
633
|
+
}
|
|
634
|
+
|
|
635
|
+
async uploadPdfSlides(file: File | Blob): Promise<string> {
|
|
636
|
+
try {
|
|
637
|
+
const checkResult = checkFile(file, MAX_PDF_FILE_SIZE, ALLOWED_PDF_MIME_TYPES);
|
|
638
|
+
|
|
639
|
+
if (checkResult !== true) {
|
|
640
|
+
throw new Error(checkResult);
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
// Check PDF page count before proceeding
|
|
644
|
+
const pageCount = await getPdfPageCount(file);
|
|
645
|
+
if (pageCount > MAX_PDF_PAGES) {
|
|
646
|
+
throw new Error(
|
|
647
|
+
`PDF has ${pageCount} pages, which exceeds the maximum allowed page count of ${MAX_PDF_PAGES} pages.`
|
|
648
|
+
);
|
|
649
|
+
}
|
|
650
|
+
|
|
651
|
+
const contentHash = await calculateHash(file);
|
|
652
|
+
const filename = file instanceof File ? file.name : (file as { filename?: string }).filename;
|
|
653
|
+
|
|
654
|
+
if (!filename) {
|
|
655
|
+
throw new Error(
|
|
656
|
+
'Invalid input: could not extract filename from the provided file or blob.'
|
|
657
|
+
);
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
if (/[^a-zA-Z0-9._ -]/.test(filename)) {
|
|
661
|
+
throw new Error(
|
|
662
|
+
'Filename contains invalid characters. Only English letters, numbers, dots, hyphens, and underscores are allowed.'
|
|
663
|
+
);
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
const { presignedPost, pdfId } = (
|
|
667
|
+
await this.http.post<RequestPdfUploadResponse>('/api/v1/pdfSlides/request-upload', {
|
|
668
|
+
contentHash,
|
|
669
|
+
filename,
|
|
670
|
+
})
|
|
671
|
+
).data;
|
|
672
|
+
|
|
673
|
+
const formData = new FormData();
|
|
674
|
+
Object.entries(presignedPost.fields).forEach(([key, value]) => {
|
|
675
|
+
formData.append(key, value);
|
|
676
|
+
});
|
|
677
|
+
formData.append('file', file);
|
|
678
|
+
|
|
679
|
+
try {
|
|
680
|
+
await axios.post(presignedPost.url, formData, {
|
|
681
|
+
headers: {
|
|
682
|
+
'Content-Type': 'multipart/form-data',
|
|
683
|
+
},
|
|
684
|
+
});
|
|
685
|
+
} catch (uploadError) {
|
|
686
|
+
// S3 upload failed - clean up the MongoDB record created during request-upload
|
|
687
|
+
try {
|
|
688
|
+
await this.deleteSlideAnalysis(pdfId);
|
|
689
|
+
} catch (deleteError) {
|
|
690
|
+
console.error('Failed to clean up PDF record after upload failure:', deleteError);
|
|
691
|
+
}
|
|
692
|
+
throw uploadError;
|
|
693
|
+
}
|
|
694
|
+
|
|
695
|
+
/*
|
|
696
|
+
// replace const { presignedPost, pdfId } with const { presignedPost, objectKey, pdfId }
|
|
697
|
+
// Uncomment this block if you are testing locally without AWS EventBridge
|
|
698
|
+
await this.http.post('/api/v1/pdfSlides/test-event-bridge', {
|
|
699
|
+
s3Name: objectKey,
|
|
700
|
+
});
|
|
701
|
+
*/
|
|
702
|
+
|
|
703
|
+
return pdfId;
|
|
704
|
+
} catch (e) {
|
|
705
|
+
if (axios.isAxiosError(e)) {
|
|
706
|
+
console.error('Error uploading PDF slides:', e.response?.data?.message || e.message);
|
|
707
|
+
} else {
|
|
708
|
+
console.error(
|
|
709
|
+
'Error uploading PDF slides:',
|
|
710
|
+
e instanceof Error ? e.message : 'Unknown error'
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
throw e;
|
|
714
|
+
}
|
|
715
|
+
}
|
|
716
|
+
|
|
717
|
+
async getRecommendations(): Promise<RecommendationsResponseDto> {
|
|
718
|
+
try {
|
|
719
|
+
const res = await this.http.get<RecommendationsResponseDto>('/api/v1/recommendations');
|
|
720
|
+
return res.data;
|
|
721
|
+
} catch (e) {
|
|
722
|
+
if (axios.isAxiosError(e)) {
|
|
723
|
+
console.error('Error getting recommendations:', e.response?.data.message);
|
|
724
|
+
}
|
|
725
|
+
throw e;
|
|
726
|
+
}
|
|
727
|
+
}
|
|
728
|
+
|
|
729
|
+
async getSlidesAnalysis(queryParams: PdfSlidesAnalysisQueryDto): Promise<PdfSlidesDto[]> {
|
|
730
|
+
try {
|
|
731
|
+
const res = await this.http.get<PdfSlidesDto[]>('/api/v1/pdfSlides', {
|
|
732
|
+
params: queryParams,
|
|
733
|
+
});
|
|
734
|
+
return res.data;
|
|
735
|
+
} catch (e) {
|
|
736
|
+
if (axios.isAxiosError(e)) {
|
|
737
|
+
console.error('Error getting PDF slides analysis:', e.response?.data.message);
|
|
738
|
+
}
|
|
739
|
+
throw e;
|
|
740
|
+
}
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
async getSlideAnalysis(id: string): Promise<PdfSlideDto> {
|
|
744
|
+
try {
|
|
745
|
+
const res = await this.http.get<PdfSlideDto>(`/api/v1/pdfSlides/${id}`);
|
|
746
|
+
return res.data;
|
|
747
|
+
} catch (e) {
|
|
748
|
+
if (axios.isAxiosError(e)) {
|
|
749
|
+
console.error('Error getting PDF slide analysis by ID:', e.response?.data.message);
|
|
750
|
+
}
|
|
751
|
+
throw e;
|
|
752
|
+
}
|
|
753
|
+
}
|
|
754
|
+
|
|
755
|
+
async deleteSlideAnalysis(id: string): Promise<void> {
|
|
756
|
+
try {
|
|
757
|
+
await this.http.delete(`/api/v1/pdfSlides/${id}`);
|
|
758
|
+
} catch (e) {
|
|
759
|
+
if (axios.isAxiosError(e)) {
|
|
760
|
+
console.error('Error deleting PDF slide analysis:', e.response?.data.message);
|
|
761
|
+
}
|
|
762
|
+
throw e;
|
|
763
|
+
}
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
async checkPdfStatus(id: string): Promise<CheckPdfStatusResponse> {
|
|
767
|
+
try {
|
|
768
|
+
const res = await this.http.get<CheckPdfStatusResponse>(`/api/v1/pdfSlides/status/${id}`);
|
|
769
|
+
return res.data;
|
|
770
|
+
} catch (e) {
|
|
771
|
+
if (axios.isAxiosError(e)) {
|
|
772
|
+
console.error('Error checking PDF status:', e.response?.data.message);
|
|
773
|
+
}
|
|
774
|
+
throw e;
|
|
775
|
+
}
|
|
776
|
+
}
|
|
777
|
+
|
|
778
|
+
async getAssistantImages(): Promise<AssistantImageDto[]> {
|
|
779
|
+
try {
|
|
780
|
+
const res = await this.http.get<AssistantImageDto[]>('/api/v1/assistant-images');
|
|
781
|
+
return res.data;
|
|
782
|
+
} catch (e) {
|
|
783
|
+
if (axios.isAxiosError(e)) {
|
|
784
|
+
console.error('Error getting assistant images:', e.response?.data.message);
|
|
785
|
+
}
|
|
786
|
+
throw e;
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|