@onereach/step-voice 7.0.20 → 7.0.21-voiceagent.0
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.
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const step_1 = tslib_1.__importDefault(require("@onereach/flow-sdk/dst/step"));
|
|
5
|
+
class VoiceAgentFunctionResponse extends step_1.default {
|
|
6
|
+
async runStep() {
|
|
7
|
+
const ctx = await this.thread.get('__voiceAgentContext') ?? {};
|
|
8
|
+
const { callId, callCallback, name: functionName } = ctx;
|
|
9
|
+
const result = this.data.result ?? '';
|
|
10
|
+
if (!callId) {
|
|
11
|
+
throw new Error('Voice Agent Function Response: missing call context (must be placed on a Voice Agent function exit)');
|
|
12
|
+
}
|
|
13
|
+
const event = {
|
|
14
|
+
target: this.helpers.providersAccountId,
|
|
15
|
+
name: 'out/voice/function-result',
|
|
16
|
+
params: {
|
|
17
|
+
id: callId,
|
|
18
|
+
commands: [{ name: 'function_result', params: { name: functionName, result } }]
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
await this.thread.eventManager.emit(event, {
|
|
22
|
+
target: callCallback,
|
|
23
|
+
invocationType: 'async',
|
|
24
|
+
timeout: 5000
|
|
25
|
+
});
|
|
26
|
+
this.exitStep('next');
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.default = VoiceAgentFunctionResponse;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import VoiceStep from './voice';
|
|
2
|
+
interface Handler {
|
|
3
|
+
name: string;
|
|
4
|
+
description: string;
|
|
5
|
+
parameters: string;
|
|
6
|
+
}
|
|
7
|
+
interface INPUT {
|
|
8
|
+
url: string;
|
|
9
|
+
handlers: Handler[];
|
|
10
|
+
}
|
|
11
|
+
export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
|
|
12
|
+
runStep(): Promise<void>;
|
|
13
|
+
private findExitByLabel;
|
|
14
|
+
exitToThread(): void;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const tslib_1 = require("tslib");
|
|
4
|
+
const lodash_1 = tslib_1.__importDefault(require("lodash"));
|
|
5
|
+
const voice_1 = tslib_1.__importDefault(require("./voice"));
|
|
6
|
+
class VoiceAgent extends voice_1.default {
|
|
7
|
+
async runStep() {
|
|
8
|
+
const { url = 'wss://bot.svc.devvoice-voiceone.api.onereach.ai/ws', handlers = [] } = this.data;
|
|
9
|
+
const call = await this.fetchData();
|
|
10
|
+
const tools = handlers.map(h => ({
|
|
11
|
+
type: 'function',
|
|
12
|
+
name: h.name,
|
|
13
|
+
description: h.description,
|
|
14
|
+
parameters: typeof h.parameters === 'string' ? JSON.parse(h.parameters) : h.parameters
|
|
15
|
+
}));
|
|
16
|
+
this.triggers.local(`in/voice/${call.id}`, async (event) => {
|
|
17
|
+
switch (event.params.type) {
|
|
18
|
+
case 'recognition': {
|
|
19
|
+
if (event.params.event !== 'function_call')
|
|
20
|
+
return;
|
|
21
|
+
const cb = this.thread.takeCallback();
|
|
22
|
+
const name = event.params.name ?? 'function_call';
|
|
23
|
+
const params = event.params.params ?? {};
|
|
24
|
+
const exitId = this.findExitByLabel(name) ?? this.getExitStepId('function_call');
|
|
25
|
+
if (exitId) {
|
|
26
|
+
this.exitStep(exitId, { name, params, __voicecb: cb, callId: call.id, callType: call.type, callCallback: call.callback }, true);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
this.log.warn('no exit for function_call', { name });
|
|
30
|
+
}
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
case 'hangup':
|
|
34
|
+
await this.handleHangup(call);
|
|
35
|
+
return await this.waitConvEnd();
|
|
36
|
+
case 'error':
|
|
37
|
+
return this.throwError(event.params.error);
|
|
38
|
+
case 'cancel':
|
|
39
|
+
return this.end();
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
this.triggers.otherwise(async () => {
|
|
43
|
+
await this.sendCommands(call, [{ name: 'ws_pipe', params: { url, tools } }]);
|
|
44
|
+
return this.exitFlow();
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
findExitByLabel(label) {
|
|
48
|
+
const exit = lodash_1.default.find(this.step.exits, (e) => e.label === label);
|
|
49
|
+
return exit?.id;
|
|
50
|
+
}
|
|
51
|
+
exitToThread() {
|
|
52
|
+
// Persist function call data to thread local so downstream steps can access it
|
|
53
|
+
this.thread.set('__voiceAgentContext', this.state.result);
|
|
54
|
+
this.thread.exitStep(this.state.exitStep, this.state.result);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
exports.default = VoiceAgent;
|