@fonoster/autopilot 0.5.1 → 0.5.2

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.
@@ -22,6 +22,7 @@ export declare class Cerebro {
22
22
  effects: EffectsManager;
23
23
  interactionsTimer: NodeJS.Timeout;
24
24
  isCallHandover: boolean;
25
+ isDead: boolean;
25
26
  constructor(config: CerebroConfig);
26
27
  wake(): Promise<void>;
27
28
  sleep(): Promise<void>;
@@ -37,4 +38,5 @@ export declare class Cerebro {
37
38
  startInteractionTimer(): void;
38
39
  resetInteractionTimer(): void;
39
40
  stopPlayback(): Promise<void>;
41
+ cleanup(): Promise<void>;
40
42
  }
@@ -47,6 +47,7 @@ class Cerebro {
47
47
  effects;
48
48
  interactionsTimer;
49
49
  isCallHandover = false;
50
+ isDead = false;
50
51
  constructor(config) {
51
52
  this.voiceResponse = config.voiceResponse;
52
53
  this.voiceRequest = config.voiceRequest;
@@ -128,12 +129,17 @@ class Cerebro {
128
129
  this.startActiveTimer();
129
130
  }
130
131
  }
132
+ }, () => {
133
+ logger.verbose("invokeEffects cleanup callback", {
134
+ sessionId: this.voiceRequest.sessionId
135
+ });
136
+ this.cleanup();
131
137
  });
132
- logger.verbose("cerebro finished processing intent effects", {
133
- sessionId: this.voiceRequest.sessionId
134
- });
135
- if (this.isCallHandover) {
138
+ if (this.isDead || this.isCallHandover) {
136
139
  try {
140
+ logger.verbose("the call was handover or cerebro was cleaned up", {
141
+ sessionId: this.voiceRequest.sessionId
142
+ });
137
143
  this.voiceResponse.hangup();
138
144
  }
139
145
  catch (e) {
@@ -141,6 +147,9 @@ class Cerebro {
141
147
  }
142
148
  return;
143
149
  }
150
+ logger.verbose("cerebro finished processing intent effects", {
151
+ sessionId: this.voiceRequest.sessionId
152
+ });
144
153
  // Reset the interactions timer
145
154
  if (!this.config.activationIntentId) {
146
155
  this.resetInteractionTimer();
@@ -207,6 +216,11 @@ class Cerebro {
207
216
  logger.verbose("invokeEffects callback", {
208
217
  sessionId: this.voiceRequest.sessionId
209
218
  });
219
+ }, () => {
220
+ logger.verbose("invokeEffects cleanup callback", {
221
+ sessionId: this.voiceRequest.sessionId
222
+ });
223
+ this.cleanup();
210
224
  });
211
225
  }, this.activationTimeout);
212
226
  }
@@ -237,5 +251,14 @@ class Cerebro {
237
251
  }
238
252
  }
239
253
  }
254
+ // Cleanup all timers and events
255
+ async cleanup() {
256
+ this.isDead = true;
257
+ await this.voiceResponse.closeMediaPipe();
258
+ this.stream.close();
259
+ this.cerebroEvents.removeAllListeners();
260
+ clearTimeout(this.activeTimer);
261
+ clearTimeout(this.interactionsTimer);
262
+ }
240
263
  }
241
264
  exports.Cerebro = Cerebro;
@@ -5,7 +5,7 @@ export declare class EffectsManager {
5
5
  voice: VoiceResponse;
6
6
  config: EffectsManagerConfig;
7
7
  constructor(config: EffectsManagerConfig);
8
- invokeEffects(intent: Intent, status: CerebroStatus, activateCallback: () => void): Promise<void>;
8
+ invokeEffects(intent: Intent, status: CerebroStatus, activateCallback: () => void, cleanupCallback: () => void): Promise<void>;
9
9
  run(effect: Effect): Promise<void>;
10
10
  transferEffect(effect: Effect): Promise<void>;
11
11
  }
@@ -15,7 +15,7 @@ class EffectsManager {
15
15
  this.voice = config.voice;
16
16
  this.config = config;
17
17
  }
18
- async invokeEffects(intent, status, activateCallback) {
18
+ async invokeEffects(intent, status, activateCallback, cleanupCallback) {
19
19
  activateCallback();
20
20
  logger.verbose("intent received", { intentRef: intent.ref });
21
21
  if (this.config.activationIntentId === intent.ref) {
@@ -31,8 +31,18 @@ class EffectsManager {
31
31
  }
32
32
  // eslint-disable-next-line no-loops/no-loops
33
33
  for (const e of intent.effects) {
34
- logger.verbose("effects running", { type: e.type });
35
- await this.run(e);
34
+ try {
35
+ logger.verbose("effects running", { type: e.type });
36
+ await this.run(e);
37
+ }
38
+ catch (e) {
39
+ const axiosError = e;
40
+ if (axiosError.response?.status !== 404) {
41
+ logger.error("error running effect", { error: e });
42
+ return;
43
+ }
44
+ cleanupCallback();
45
+ }
36
46
  }
37
47
  }
38
48
  async run(effect) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fonoster/autopilot",
3
- "version": "0.5.1",
3
+ "version": "0.5.2",
4
4
  "license": "MIT",
5
5
  "main": "dist/index",
6
6
  "types": "dist/index",
@@ -37,5 +37,5 @@
37
37
  "publishConfig": {
38
38
  "access": "public"
39
39
  },
40
- "gitHead": "0f7b0ed84ded17ea6b3aa593fafc97914478f3a6"
40
+ "gitHead": "a6726245c50fc08aec29e04f7da04207b578a305"
41
41
  }