@onereach/step-voice 7.0.40 → 7.0.41-subagents.1
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/dst/Subagent Tools Handler.d.ts +10 -0
- package/dst/Subagent Tools Handler.js +80 -0
- package/dst/Voice Agent.d.ts +23 -0
- package/dst/Voice Agent.js +85 -26
- package/package.json +1 -1
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import VoiceStep from './voice';
|
|
2
|
+
interface INPUT {
|
|
3
|
+
subagentName?: string;
|
|
4
|
+
}
|
|
5
|
+
export default class SubagentToolsHandler extends VoiceStep<Partial<INPUT>> {
|
|
6
|
+
runStep(): Promise<void>;
|
|
7
|
+
private findExitByLabel;
|
|
8
|
+
exitToThread(): Promise<void>;
|
|
9
|
+
}
|
|
10
|
+
export {};
|
|
@@ -0,0 +1,80 @@
|
|
|
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 SubagentToolsHandler extends voice_1.default {
|
|
7
|
+
async runStep() {
|
|
8
|
+
const call = await this.fetchData();
|
|
9
|
+
let ctx = null;
|
|
10
|
+
try {
|
|
11
|
+
ctx = await this.thread.get(`__voiceAgentContext_${this.thread.id}`);
|
|
12
|
+
}
|
|
13
|
+
catch {
|
|
14
|
+
ctx = null;
|
|
15
|
+
}
|
|
16
|
+
const callId = ctx?.callId ?? call.id;
|
|
17
|
+
const callType = ctx?.callType ?? call.type;
|
|
18
|
+
const callCallback = ctx?.callCallback ?? call.callback;
|
|
19
|
+
const subagentName = ctx?.subagent ?? ctx?.name ?? this.data.subagentName ?? '';
|
|
20
|
+
this.triggers.local(`in/voice/${callId}`, async (event) => {
|
|
21
|
+
switch (event.params.type) {
|
|
22
|
+
case 'tool_call': {
|
|
23
|
+
const eventSubagent = event.params.subagent;
|
|
24
|
+
if (eventSubagent && subagentName && eventSubagent !== subagentName) {
|
|
25
|
+
return;
|
|
26
|
+
}
|
|
27
|
+
const cb = this.thread.takeCallback();
|
|
28
|
+
const name = event.params.name ?? 'tool_call';
|
|
29
|
+
const params = event.params.arguments ?? {};
|
|
30
|
+
const toolCallId = event.params.toolCallId ?? '';
|
|
31
|
+
const exitId = this.findExitByLabel(name);
|
|
32
|
+
if (!exitId) {
|
|
33
|
+
return;
|
|
34
|
+
}
|
|
35
|
+
this.exitStep(exitId, {
|
|
36
|
+
[name]: params,
|
|
37
|
+
name,
|
|
38
|
+
toolCallId,
|
|
39
|
+
subagent: subagentName,
|
|
40
|
+
__voicecb: cb,
|
|
41
|
+
callId,
|
|
42
|
+
callType,
|
|
43
|
+
callCallback
|
|
44
|
+
}, true);
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
case 'hangup': {
|
|
48
|
+
delete this.waits.timeout;
|
|
49
|
+
await this.handleHangup(call);
|
|
50
|
+
return await this.waitConvEnd();
|
|
51
|
+
}
|
|
52
|
+
case 'error': {
|
|
53
|
+
delete this.waits.timeout;
|
|
54
|
+
const exitId = this.getExitStepId('error');
|
|
55
|
+
if (exitId)
|
|
56
|
+
return this.exitStep(exitId, { error: event.params.error });
|
|
57
|
+
return this.throwError(event.params.error);
|
|
58
|
+
}
|
|
59
|
+
case 'cancel':
|
|
60
|
+
return this.end();
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
this.triggers.otherwise(async () => {
|
|
64
|
+
if (lodash_1.default.isEmpty(subagentName)) {
|
|
65
|
+
this.log.warn('Subagent Tools Handler: no subagent context; waiting for tool calls on exits only');
|
|
66
|
+
}
|
|
67
|
+
return this.exitFlow();
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
findExitByLabel(label) {
|
|
71
|
+
const exit = lodash_1.default.find(this.step.exits, (e) => e.label === label);
|
|
72
|
+
return exit?.id;
|
|
73
|
+
}
|
|
74
|
+
async exitToThread() {
|
|
75
|
+
const result = this.state.result;
|
|
76
|
+
await this.thread.set(`__voiceAgentContext_${this.thread.id}`, result);
|
|
77
|
+
this.thread.exitStep(this.state.exitStep, result);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
exports.default = SubagentToolsHandler;
|
package/dst/Voice Agent.d.ts
CHANGED
|
@@ -30,6 +30,19 @@ interface Handler {
|
|
|
30
30
|
parameters: string;
|
|
31
31
|
feedback?: PerToolConfig;
|
|
32
32
|
}
|
|
33
|
+
interface SubagentTool {
|
|
34
|
+
name: string;
|
|
35
|
+
description: string;
|
|
36
|
+
parameters: string;
|
|
37
|
+
feedback?: PerToolConfig;
|
|
38
|
+
}
|
|
39
|
+
interface Subagent {
|
|
40
|
+
id: string;
|
|
41
|
+
name: string;
|
|
42
|
+
system_instruction: string;
|
|
43
|
+
tools: SubagentTool[];
|
|
44
|
+
exitId?: string;
|
|
45
|
+
}
|
|
33
46
|
interface AgentSystemToolConfig {
|
|
34
47
|
name: string;
|
|
35
48
|
description?: string;
|
|
@@ -43,6 +56,7 @@ interface AgentServiceConfig {
|
|
|
43
56
|
}
|
|
44
57
|
interface INPUT {
|
|
45
58
|
handlers: Handler[];
|
|
59
|
+
subagents: Subagent[];
|
|
46
60
|
system_instruction: string;
|
|
47
61
|
developer_messages: string[];
|
|
48
62
|
greeting_message: string;
|
|
@@ -72,7 +86,16 @@ interface INPUT {
|
|
|
72
86
|
}
|
|
73
87
|
export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
|
|
74
88
|
runStep(): Promise<void>;
|
|
89
|
+
/**
|
|
90
|
+
* Fork a thread per subagent exit so Subagent Tools Handler steps can
|
|
91
|
+
* register tool_call listeners immediately after agent start.
|
|
92
|
+
*/
|
|
93
|
+
private spawnSubagentHandlerThreads;
|
|
75
94
|
private normalizeIdlePrompts;
|
|
95
|
+
private serializeTool;
|
|
96
|
+
private serializeSubagents;
|
|
97
|
+
private isSubagentExitLabel;
|
|
98
|
+
private isSubagentTool;
|
|
76
99
|
private findExitByLabel;
|
|
77
100
|
private validateToolFeedback;
|
|
78
101
|
private isStepFlagEnabled;
|
package/dst/Voice Agent.js
CHANGED
|
@@ -11,8 +11,12 @@ class VoiceAgent extends voice_1.default {
|
|
|
11
11
|
case 'tool_call': {
|
|
12
12
|
if (event.params.type !== 'tool_call')
|
|
13
13
|
return;
|
|
14
|
-
const cb = this.thread.takeCallback();
|
|
15
14
|
const name = event.params.name ?? 'tool_call';
|
|
15
|
+
// Subagent tools are handled by pre-spawned Subagent Tools Handler threads.
|
|
16
|
+
if (event.params.subagent || this.isSubagentTool(name) || this.isSubagentExitLabel(name)) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const cb = this.thread.takeCallback();
|
|
16
20
|
const params = event.params.arguments ?? {};
|
|
17
21
|
const toolCallId = event.params.toolCallId ?? '';
|
|
18
22
|
const exitId = this.findExitByLabel(name) ?? this.getExitStepId('tool_call');
|
|
@@ -52,34 +56,11 @@ class VoiceAgent extends voice_1.default {
|
|
|
52
56
|
}
|
|
53
57
|
});
|
|
54
58
|
this.triggers.otherwise(async () => {
|
|
55
|
-
const { handlers = [], system_instruction = '', developer_messages = [], greeting_message = '', prevent_greeting_interruption = false, idle_detection_enabled = false, idle_timeout_secs = 8, idle_max_retries = 3, idle_prompts = [], idle_goodbye_message = '', dtmf_enabled = false, dtmf_timeout = 2, dtmf_termination_digit = '#', dtmf_prefix = 'DTMF: ', custom_config_enabled = false, agent_url_overwritten = false, tool_feedback, soft_timeout, system_tools, agent_url = '', mode = '', stt = {}, llm = {}, tts = {}, realtime = {} } = this.data;
|
|
59
|
+
const { handlers = [], subagents = [], system_instruction = '', developer_messages = [], greeting_message = '', prevent_greeting_interruption = false, idle_detection_enabled = false, idle_timeout_secs = 8, idle_max_retries = 3, idle_prompts = [], idle_goodbye_message = '', dtmf_enabled = false, dtmf_timeout = 2, dtmf_termination_digit = '#', dtmf_prefix = 'DTMF: ', custom_config_enabled = false, agent_url_overwritten = false, tool_feedback, soft_timeout, system_tools, agent_url = '', mode = '', stt = {}, llm = {}, tts = {}, realtime = {} } = this.data;
|
|
56
60
|
if (lodash_1.default.isEmpty(system_instruction)) {
|
|
57
61
|
this.throwError(new Error('Voice Agent: system instruction is required'));
|
|
58
62
|
}
|
|
59
|
-
const tools = handlers.map(h =>
|
|
60
|
-
let parameters = h.parameters;
|
|
61
|
-
if (typeof parameters === 'string') {
|
|
62
|
-
try {
|
|
63
|
-
parameters = JSON.parse(parameters);
|
|
64
|
-
}
|
|
65
|
-
catch {
|
|
66
|
-
parameters = {};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
const tool = {
|
|
70
|
-
type: 'function',
|
|
71
|
-
name: h.name,
|
|
72
|
-
description: h.description,
|
|
73
|
-
parameters: parameters || {}
|
|
74
|
-
};
|
|
75
|
-
if (h.feedback != null) {
|
|
76
|
-
const validatedFeedback = this.validatePerToolFeedback(h.feedback, h.name);
|
|
77
|
-
if (validatedFeedback) {
|
|
78
|
-
tool.feedback = validatedFeedback;
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
return tool;
|
|
82
|
-
});
|
|
63
|
+
const tools = handlers.map(h => this.serializeTool(h));
|
|
83
64
|
const config = {
|
|
84
65
|
system_instruction,
|
|
85
66
|
developer_messages,
|
|
@@ -87,6 +68,10 @@ class VoiceAgent extends voice_1.default {
|
|
|
87
68
|
prevent_greeting_interruption,
|
|
88
69
|
tools
|
|
89
70
|
};
|
|
71
|
+
const serializedSubagents = this.serializeSubagents(subagents) || [];
|
|
72
|
+
if (serializedSubagents.length > 0) {
|
|
73
|
+
config.subagents = serializedSubagents;
|
|
74
|
+
}
|
|
90
75
|
if (this.isStepFlagEnabled(idle_detection_enabled)) {
|
|
91
76
|
const timeoutSecs = Number(idle_timeout_secs);
|
|
92
77
|
const maxRetries = Number(idle_max_retries);
|
|
@@ -152,14 +137,88 @@ class VoiceAgent extends voice_1.default {
|
|
|
152
137
|
params.url = agentCustomUrl;
|
|
153
138
|
}
|
|
154
139
|
await this.sendCommands(call, [{ name: 'startAgent', params }]);
|
|
140
|
+
this.spawnSubagentHandlerThreads(call, subagents);
|
|
155
141
|
return this.exitFlow();
|
|
156
142
|
});
|
|
157
143
|
}
|
|
144
|
+
/**
|
|
145
|
+
* Fork a thread per subagent exit so Subagent Tools Handler steps can
|
|
146
|
+
* register tool_call listeners immediately after agent start.
|
|
147
|
+
*/
|
|
148
|
+
spawnSubagentHandlerThreads(call, subagents) {
|
|
149
|
+
if (!Array.isArray(subagents) || subagents.length === 0)
|
|
150
|
+
return;
|
|
151
|
+
for (const sa of subagents) {
|
|
152
|
+
const name = lodash_1.default.trim(sa?.name || '');
|
|
153
|
+
if (!name)
|
|
154
|
+
continue;
|
|
155
|
+
const exitId = sa.exitId || this.findExitByLabel(`subagent_${name}`);
|
|
156
|
+
if (!exitId) {
|
|
157
|
+
this.log.warn('Voice Agent: no exit for subagent; handler thread not started', { name });
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
this.exitStep(exitId, {
|
|
161
|
+
name,
|
|
162
|
+
subagent: name,
|
|
163
|
+
callId: call.id,
|
|
164
|
+
callType: call.type,
|
|
165
|
+
callCallback: call.callback
|
|
166
|
+
}, true);
|
|
167
|
+
}
|
|
168
|
+
}
|
|
158
169
|
normalizeIdlePrompts(idlePrompts) {
|
|
159
170
|
return (idlePrompts || [])
|
|
160
171
|
.map(item => typeof item === 'string' ? item : item?.prompt ?? '')
|
|
161
172
|
.filter(prompt => !lodash_1.default.isEmpty(lodash_1.default.trim(String(prompt))));
|
|
162
173
|
}
|
|
174
|
+
serializeTool(h) {
|
|
175
|
+
let parameters = h.parameters;
|
|
176
|
+
if (typeof parameters === 'string') {
|
|
177
|
+
try {
|
|
178
|
+
parameters = JSON.parse(parameters);
|
|
179
|
+
}
|
|
180
|
+
catch {
|
|
181
|
+
parameters = {};
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const tool = {
|
|
185
|
+
type: 'function',
|
|
186
|
+
name: h.name,
|
|
187
|
+
description: h.description,
|
|
188
|
+
parameters: parameters || {}
|
|
189
|
+
};
|
|
190
|
+
if (h.feedback != null) {
|
|
191
|
+
const validatedFeedback = this.validatePerToolFeedback(h.feedback, h.name);
|
|
192
|
+
if (validatedFeedback) {
|
|
193
|
+
tool.feedback = validatedFeedback;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
return tool;
|
|
197
|
+
}
|
|
198
|
+
serializeSubagents(subagents) {
|
|
199
|
+
if (!Array.isArray(subagents))
|
|
200
|
+
return [];
|
|
201
|
+
return subagents
|
|
202
|
+
.filter(sa => sa != null && !lodash_1.default.isEmpty(lodash_1.default.trim(sa.name)))
|
|
203
|
+
.map(sa => ({
|
|
204
|
+
name: lodash_1.default.trim(sa.name),
|
|
205
|
+
system_instruction: sa.system_instruction || '',
|
|
206
|
+
tools: (sa.tools || [])
|
|
207
|
+
.filter(t => t != null && !lodash_1.default.isEmpty(lodash_1.default.trim(t.name)))
|
|
208
|
+
.map(t => this.serializeTool(t))
|
|
209
|
+
}));
|
|
210
|
+
}
|
|
211
|
+
isSubagentExitLabel(label) {
|
|
212
|
+
const subagents = this.data.subagents || [];
|
|
213
|
+
return subagents.some(sa => {
|
|
214
|
+
const name = lodash_1.default.trim(sa?.name || '');
|
|
215
|
+
return !!name && label === `subagent_${name}`;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
isSubagentTool(toolName) {
|
|
219
|
+
const subagents = this.data.subagents || [];
|
|
220
|
+
return subagents.some(sa => (sa?.tools || []).some(t => t && lodash_1.default.trim(t.name) === toolName));
|
|
221
|
+
}
|
|
163
222
|
findExitByLabel(label) {
|
|
164
223
|
const exit = lodash_1.default.find(this.step.exits, (e) => e.label === label);
|
|
165
224
|
return exit?.id;
|