@onereach/step-voice 7.0.41-tooltimeoutresponsemode.2 → 7.0.41-tooltimeoutresponsemode.3
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 +30 -3
- package/dst/Voice Agent.js +105 -34
- 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
|
@@ -33,10 +33,11 @@ interface Handler {
|
|
|
33
33
|
timeout_seconds?: number | string | null;
|
|
34
34
|
response_mode?: ToolResponseMode | string | null;
|
|
35
35
|
}
|
|
36
|
-
interface
|
|
36
|
+
interface SubagentTool {
|
|
37
37
|
name: string;
|
|
38
|
-
description
|
|
39
|
-
parameters
|
|
38
|
+
description: string;
|
|
39
|
+
parameters: string;
|
|
40
|
+
feedback?: PerToolConfig;
|
|
40
41
|
}
|
|
41
42
|
interface AgentServiceConfig {
|
|
42
43
|
provider: string;
|
|
@@ -44,8 +45,25 @@ interface AgentServiceConfig {
|
|
|
44
45
|
api_key?: string;
|
|
45
46
|
options?: Record<string, unknown>;
|
|
46
47
|
}
|
|
48
|
+
type SubagentContextMode = 'own' | 'shared';
|
|
49
|
+
interface Subagent {
|
|
50
|
+
id: string;
|
|
51
|
+
name: string;
|
|
52
|
+
system_instruction: string;
|
|
53
|
+
developer_messages?: string[];
|
|
54
|
+
context?: SubagentContextMode;
|
|
55
|
+
llm?: Partial<AgentServiceConfig> | null;
|
|
56
|
+
tools: SubagentTool[];
|
|
57
|
+
exitId?: string;
|
|
58
|
+
}
|
|
59
|
+
interface AgentSystemToolConfig {
|
|
60
|
+
name: string;
|
|
61
|
+
description?: string;
|
|
62
|
+
parameters?: Record<string, unknown>;
|
|
63
|
+
}
|
|
47
64
|
interface INPUT {
|
|
48
65
|
handlers: Handler[];
|
|
66
|
+
subagents: Subagent[];
|
|
49
67
|
system_instruction: string;
|
|
50
68
|
developer_messages: string[];
|
|
51
69
|
greeting_message: string;
|
|
@@ -75,7 +93,16 @@ interface INPUT {
|
|
|
75
93
|
}
|
|
76
94
|
export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
|
|
77
95
|
runStep(): Promise<void>;
|
|
96
|
+
/**
|
|
97
|
+
* Fork a thread per subagent exit so Subagent Tools Handler steps can
|
|
98
|
+
* register tool_call listeners immediately after agent start.
|
|
99
|
+
*/
|
|
100
|
+
private spawnSubagentHandlerThreads;
|
|
78
101
|
private normalizeIdlePrompts;
|
|
102
|
+
private serializeTool;
|
|
103
|
+
private serializeSubagents;
|
|
104
|
+
private isSubagentExitLabel;
|
|
105
|
+
private isSubagentTool;
|
|
79
106
|
private findExitByLabel;
|
|
80
107
|
private validateToolFeedback;
|
|
81
108
|
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,42 +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
|
-
const validatedTimeout = this.validateToolTimeout(h.timeout_seconds, h.name);
|
|
82
|
-
if (validatedTimeout != null) {
|
|
83
|
-
tool.timeout_seconds = validatedTimeout;
|
|
84
|
-
}
|
|
85
|
-
const validatedResponseMode = this.validateToolResponseMode(h.response_mode, h.name);
|
|
86
|
-
if (validatedResponseMode != null) {
|
|
87
|
-
tool.response_mode = validatedResponseMode;
|
|
88
|
-
}
|
|
89
|
-
return tool;
|
|
90
|
-
});
|
|
63
|
+
const tools = handlers.map(h => this.serializeTool(h));
|
|
91
64
|
const config = {
|
|
92
65
|
system_instruction,
|
|
93
66
|
developer_messages,
|
|
@@ -95,6 +68,10 @@ class VoiceAgent extends voice_1.default {
|
|
|
95
68
|
prevent_greeting_interruption,
|
|
96
69
|
tools
|
|
97
70
|
};
|
|
71
|
+
const serializedSubagents = this.serializeSubagents(subagents) || [];
|
|
72
|
+
if (serializedSubagents.length > 0) {
|
|
73
|
+
config.subagents = serializedSubagents;
|
|
74
|
+
}
|
|
98
75
|
if (this.isStepFlagEnabled(idle_detection_enabled)) {
|
|
99
76
|
const timeoutSecs = Number(idle_timeout_secs);
|
|
100
77
|
const maxRetries = Number(idle_max_retries);
|
|
@@ -160,14 +137,108 @@ class VoiceAgent extends voice_1.default {
|
|
|
160
137
|
params.url = agentCustomUrl;
|
|
161
138
|
}
|
|
162
139
|
await this.sendCommands(call, [{ name: 'startAgent', params }]);
|
|
140
|
+
this.spawnSubagentHandlerThreads(call, subagents);
|
|
163
141
|
return this.exitFlow();
|
|
164
142
|
});
|
|
165
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
|
+
}
|
|
166
169
|
normalizeIdlePrompts(idlePrompts) {
|
|
167
170
|
return (idlePrompts || [])
|
|
168
171
|
.map(item => typeof item === 'string' ? item : item?.prompt ?? '')
|
|
169
172
|
.filter(prompt => !lodash_1.default.isEmpty(lodash_1.default.trim(String(prompt))));
|
|
170
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
|
+
const handler = h;
|
|
197
|
+
const validatedTimeout = this.validateToolTimeout(handler.timeout_seconds, h.name);
|
|
198
|
+
if (validatedTimeout != null) {
|
|
199
|
+
tool.timeout_seconds = validatedTimeout;
|
|
200
|
+
}
|
|
201
|
+
const validatedResponseMode = this.validateToolResponseMode(handler.response_mode, h.name);
|
|
202
|
+
if (validatedResponseMode != null) {
|
|
203
|
+
tool.response_mode = validatedResponseMode;
|
|
204
|
+
}
|
|
205
|
+
return tool;
|
|
206
|
+
}
|
|
207
|
+
serializeSubagents(subagents) {
|
|
208
|
+
if (!Array.isArray(subagents))
|
|
209
|
+
return [];
|
|
210
|
+
return subagents
|
|
211
|
+
.filter(sa => sa != null && !lodash_1.default.isEmpty(lodash_1.default.trim(sa.name)))
|
|
212
|
+
.map(sa => {
|
|
213
|
+
const name = lodash_1.default.trim(sa.name);
|
|
214
|
+
const serialized = {
|
|
215
|
+
name,
|
|
216
|
+
system_instruction: sa.system_instruction || '',
|
|
217
|
+
developer_messages: (sa.developer_messages || [])
|
|
218
|
+
.map(msg => typeof msg === 'string' ? msg : '')
|
|
219
|
+
.filter(msg => !lodash_1.default.isEmpty(lodash_1.default.trim(msg))),
|
|
220
|
+
context: sa.context === 'shared' ? 'shared' : 'own',
|
|
221
|
+
tools: (sa.tools || [])
|
|
222
|
+
.filter(t => t != null && !lodash_1.default.isEmpty(lodash_1.default.trim(t.name)))
|
|
223
|
+
.map(t => this.serializeTool(t))
|
|
224
|
+
};
|
|
225
|
+
const llm = this.buildAgentServiceConfig(`subagent "${name}" llm`, sa.llm || {});
|
|
226
|
+
if (llm)
|
|
227
|
+
serialized.llm = llm;
|
|
228
|
+
return serialized;
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
isSubagentExitLabel(label) {
|
|
232
|
+
const subagents = this.data.subagents || [];
|
|
233
|
+
return subagents.some(sa => {
|
|
234
|
+
const name = lodash_1.default.trim(sa?.name || '');
|
|
235
|
+
return !!name && label === `subagent_${name}`;
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
isSubagentTool(toolName) {
|
|
239
|
+
const subagents = this.data.subagents || [];
|
|
240
|
+
return subagents.some(sa => (sa?.tools || []).some(t => t && lodash_1.default.trim(t.name) === toolName));
|
|
241
|
+
}
|
|
171
242
|
findExitByLabel(label) {
|
|
172
243
|
const exit = lodash_1.default.find(this.step.exits, (e) => e.label === label);
|
|
173
244
|
return exit?.id;
|