@onereach/step-voice 7.0.43-VOIC1775agentstepui.0 → 7.0.43
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,11 @@
|
|
|
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 loadToolCallContext;
|
|
8
|
+
private findExitByLabel;
|
|
9
|
+
exitToThread(): Promise<void>;
|
|
10
|
+
}
|
|
11
|
+
export {};
|
|
@@ -0,0 +1,73 @@
|
|
|
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
|
+
const ctx = await this.loadToolCallContext();
|
|
10
|
+
const toolName = lodash_1.default.trim(String(ctx.name ?? ''));
|
|
11
|
+
const subagentName = lodash_1.default.trim(String(ctx.subagent ?? this.data.subagentName ?? ''));
|
|
12
|
+
const arguments_ = (ctx.arguments && typeof ctx.arguments === 'object')
|
|
13
|
+
? ctx.arguments
|
|
14
|
+
: (ctx[toolName] ?? {});
|
|
15
|
+
const exitId = toolName ? this.findExitByLabel(toolName) : undefined;
|
|
16
|
+
this.log.info('Subagent Tools Handler: incoming tool_call', {
|
|
17
|
+
toolName,
|
|
18
|
+
subagent: subagentName,
|
|
19
|
+
exitId,
|
|
20
|
+
threadId: this.thread.id,
|
|
21
|
+
hasContext: !lodash_1.default.isEmpty(ctx)
|
|
22
|
+
});
|
|
23
|
+
if (!exitId) {
|
|
24
|
+
this.log.warn('Subagent Tools Handler: no exit for tool_call', {
|
|
25
|
+
name: toolName,
|
|
26
|
+
subagent: subagentName,
|
|
27
|
+
exits: (this.step.exits || []).map((e) => e.label)
|
|
28
|
+
});
|
|
29
|
+
this.exitFlow();
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
32
|
+
// Same hop as Voice Agent → tool: fork so Voice Agent Tool Result still
|
|
33
|
+
// finds __voiceAgentContext_ on the child thread. Do not exitFlow() after
|
|
34
|
+
// this — that would tear down the parent before the tool thread starts.
|
|
35
|
+
await this.exitStep(exitId, {
|
|
36
|
+
[toolName]: arguments_,
|
|
37
|
+
name: toolName,
|
|
38
|
+
toolCallId: ctx.toolCallId ?? '',
|
|
39
|
+
arguments: arguments_,
|
|
40
|
+
subagent: subagentName,
|
|
41
|
+
__voicecb: ctx.__voicecb,
|
|
42
|
+
callId: ctx.callId ?? call.id,
|
|
43
|
+
callType: ctx.callType ?? call.type,
|
|
44
|
+
callCallback: ctx.callCallback ?? call.callback
|
|
45
|
+
}, true);
|
|
46
|
+
}
|
|
47
|
+
async loadToolCallContext() {
|
|
48
|
+
const fromState = (this.state?.result && typeof this.state.result === 'object')
|
|
49
|
+
? this.state.result
|
|
50
|
+
: {};
|
|
51
|
+
let fromThread = {};
|
|
52
|
+
try {
|
|
53
|
+
const stored = await this.thread.get(`__voiceAgentContext_${this.thread.id}`);
|
|
54
|
+
if (stored && typeof stored === 'object')
|
|
55
|
+
fromThread = stored;
|
|
56
|
+
}
|
|
57
|
+
catch {
|
|
58
|
+
// key absent on this thread — Voice Agent payload may still be in state.result
|
|
59
|
+
}
|
|
60
|
+
return { ...fromState, ...fromThread };
|
|
61
|
+
}
|
|
62
|
+
findExitByLabel(label) {
|
|
63
|
+
const needle = lodash_1.default.trim(label);
|
|
64
|
+
const exit = lodash_1.default.find(this.step.exits, (e) => lodash_1.default.trim(e.label || '') === needle);
|
|
65
|
+
return exit?.id;
|
|
66
|
+
}
|
|
67
|
+
async exitToThread() {
|
|
68
|
+
const result = this.state.result;
|
|
69
|
+
await this.thread.set(`__voiceAgentContext_${this.thread.id}`, result);
|
|
70
|
+
this.thread.exitStep(this.state.exitStep, result);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
exports.default = SubagentToolsHandler;
|
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
import VoiceStep from './voice';
|
|
2
2
|
type SystemInstructionMode = 'append' | 'replace' | 'off';
|
|
3
|
+
type ToolResponseMode = 'sync' | 'async' | 'none';
|
|
3
4
|
interface Tool {
|
|
4
5
|
name: string;
|
|
5
6
|
description: string;
|
|
6
7
|
parameters: string | Record<string, unknown>;
|
|
7
8
|
exitId?: string;
|
|
9
|
+
timeout_seconds?: number | string | null;
|
|
10
|
+
response_mode?: ToolResponseMode | string | null;
|
|
8
11
|
}
|
|
9
12
|
interface AgentServiceConfig {
|
|
10
13
|
provider: string;
|
|
@@ -139,12 +139,19 @@ class UpdateVoiceAgent extends voice_1.default {
|
|
|
139
139
|
else if (t.parameters && typeof t.parameters === 'object') {
|
|
140
140
|
parameters = t.parameters;
|
|
141
141
|
}
|
|
142
|
-
|
|
142
|
+
const tool = {
|
|
143
143
|
type: 'function',
|
|
144
144
|
name: lodash_1.default.trim(t.name),
|
|
145
145
|
description: t.description || '',
|
|
146
146
|
parameters,
|
|
147
147
|
};
|
|
148
|
+
if (t.timeout_seconds != null && t.timeout_seconds !== '') {
|
|
149
|
+
tool.timeout_seconds = Number(t.timeout_seconds);
|
|
150
|
+
}
|
|
151
|
+
if (t.response_mode) {
|
|
152
|
+
tool.response_mode = t.response_mode;
|
|
153
|
+
}
|
|
154
|
+
return tool;
|
|
148
155
|
});
|
|
149
156
|
}
|
|
150
157
|
buildOptionalServiceOverride(label, service) {
|
package/dst/Voice Agent.d.ts
CHANGED
|
@@ -24,16 +24,22 @@ interface SoftTimeoutConfig {
|
|
|
24
24
|
phrases: string[];
|
|
25
25
|
mode: SoftTimeoutMode;
|
|
26
26
|
}
|
|
27
|
+
type ToolResponseMode = 'sync' | 'async' | 'none';
|
|
27
28
|
interface Handler {
|
|
28
29
|
name: string;
|
|
29
30
|
description: string;
|
|
30
31
|
parameters: string;
|
|
31
32
|
feedback?: PerToolConfig;
|
|
33
|
+
timeout_seconds?: number | string | null;
|
|
34
|
+
response_mode?: ToolResponseMode | string | null;
|
|
32
35
|
}
|
|
33
|
-
interface
|
|
36
|
+
interface SubagentTool {
|
|
34
37
|
name: string;
|
|
35
|
-
description
|
|
36
|
-
parameters
|
|
38
|
+
description: string;
|
|
39
|
+
parameters: string;
|
|
40
|
+
feedback?: PerToolConfig;
|
|
41
|
+
timeout_seconds?: number | string | null;
|
|
42
|
+
response_mode?: ToolResponseMode | string | null;
|
|
37
43
|
}
|
|
38
44
|
interface AgentServiceConfig {
|
|
39
45
|
provider: string;
|
|
@@ -41,8 +47,25 @@ interface AgentServiceConfig {
|
|
|
41
47
|
api_key?: string;
|
|
42
48
|
options?: Record<string, unknown>;
|
|
43
49
|
}
|
|
50
|
+
type SubagentContextMode = 'own' | 'shared';
|
|
51
|
+
interface Subagent {
|
|
52
|
+
id: string;
|
|
53
|
+
name: string;
|
|
54
|
+
system_instruction: string;
|
|
55
|
+
developer_messages?: string[];
|
|
56
|
+
context?: SubagentContextMode;
|
|
57
|
+
llm?: Partial<AgentServiceConfig> | null;
|
|
58
|
+
tools: SubagentTool[];
|
|
59
|
+
exitId?: string;
|
|
60
|
+
}
|
|
61
|
+
interface AgentSystemToolConfig {
|
|
62
|
+
name: string;
|
|
63
|
+
description?: string;
|
|
64
|
+
parameters?: Record<string, unknown>;
|
|
65
|
+
}
|
|
44
66
|
interface INPUT {
|
|
45
67
|
handlers: Handler[];
|
|
68
|
+
subagents: Subagent[];
|
|
46
69
|
system_instruction: string;
|
|
47
70
|
developer_messages: string[];
|
|
48
71
|
greeting_message: string;
|
|
@@ -73,6 +96,11 @@ interface INPUT {
|
|
|
73
96
|
export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
|
|
74
97
|
runStep(): Promise<void>;
|
|
75
98
|
private normalizeIdlePrompts;
|
|
99
|
+
private serializeTool;
|
|
100
|
+
private serializeSubagents;
|
|
101
|
+
private findSubagentForTool;
|
|
102
|
+
private findSubagentByName;
|
|
103
|
+
private resolveSubagentExit;
|
|
76
104
|
private findExitByLabel;
|
|
77
105
|
private validateToolFeedback;
|
|
78
106
|
private isStepFlagEnabled;
|
package/dst/Voice Agent.js
CHANGED
|
@@ -11,21 +11,48 @@ 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
|
+
const cb = this.thread.takeCallback();
|
|
16
16
|
const params = event.params.arguments ?? {};
|
|
17
17
|
const toolCallId = event.params.toolCallId ?? '';
|
|
18
|
+
const payload = {
|
|
19
|
+
[name]: params,
|
|
20
|
+
name,
|
|
21
|
+
toolCallId,
|
|
22
|
+
__voicecb: cb,
|
|
23
|
+
callId: call.id,
|
|
24
|
+
callType: call.type,
|
|
25
|
+
callCallback: call.callback
|
|
26
|
+
};
|
|
27
|
+
const subagent = this.findSubagentForTool(name)
|
|
28
|
+
|| this.findSubagentByName(event.params.subagent);
|
|
29
|
+
if (subagent) {
|
|
30
|
+
const exitId = this.resolveSubagentExit(subagent);
|
|
31
|
+
if (exitId) {
|
|
32
|
+
this.log.info('Voice Agent: routing tool_call to subagent', {
|
|
33
|
+
name,
|
|
34
|
+
subagent: subagent.name,
|
|
35
|
+
exitId
|
|
36
|
+
});
|
|
37
|
+
this.exitStep(exitId, {
|
|
38
|
+
...payload,
|
|
39
|
+
arguments: params,
|
|
40
|
+
subagent: subagent.name
|
|
41
|
+
}, true);
|
|
42
|
+
}
|
|
43
|
+
else {
|
|
44
|
+
this.log.warn('no exit for subagent tool_call', {
|
|
45
|
+
name,
|
|
46
|
+
subagent: subagent.name,
|
|
47
|
+
exitId: subagent.exitId,
|
|
48
|
+
exits: (this.step.exits || []).map((e) => ({ id: e.id, label: e.label }))
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
18
53
|
const exitId = this.findExitByLabel(name) ?? this.getExitStepId('tool_call');
|
|
19
54
|
if (exitId) {
|
|
20
|
-
this.exitStep(exitId,
|
|
21
|
-
[name]: params,
|
|
22
|
-
name,
|
|
23
|
-
toolCallId,
|
|
24
|
-
__voicecb: cb,
|
|
25
|
-
callId: call.id,
|
|
26
|
-
callType: call.type,
|
|
27
|
-
callCallback: call.callback
|
|
28
|
-
}, true);
|
|
55
|
+
this.exitStep(exitId, payload, true);
|
|
29
56
|
}
|
|
30
57
|
else {
|
|
31
58
|
this.log.warn('no exit for tool_call', { name });
|
|
@@ -52,34 +79,11 @@ class VoiceAgent extends voice_1.default {
|
|
|
52
79
|
}
|
|
53
80
|
});
|
|
54
81
|
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;
|
|
82
|
+
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
83
|
if (lodash_1.default.isEmpty(system_instruction)) {
|
|
57
84
|
this.throwError(new Error('Voice Agent: system instruction is required'));
|
|
58
85
|
}
|
|
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
|
-
});
|
|
86
|
+
const tools = handlers.map(h => this.serializeTool(h));
|
|
83
87
|
const config = {
|
|
84
88
|
system_instruction,
|
|
85
89
|
developer_messages,
|
|
@@ -87,6 +91,10 @@ class VoiceAgent extends voice_1.default {
|
|
|
87
91
|
prevent_greeting_interruption,
|
|
88
92
|
tools
|
|
89
93
|
};
|
|
94
|
+
const serializedSubagents = this.serializeSubagents(subagents) || [];
|
|
95
|
+
if (serializedSubagents.length > 0) {
|
|
96
|
+
config.subagents = serializedSubagents;
|
|
97
|
+
}
|
|
90
98
|
if (this.isStepFlagEnabled(idle_detection_enabled)) {
|
|
91
99
|
const timeoutSecs = Number(idle_timeout_secs);
|
|
92
100
|
const maxRetries = Number(idle_max_retries);
|
|
@@ -160,8 +168,83 @@ class VoiceAgent extends voice_1.default {
|
|
|
160
168
|
.map(item => typeof item === 'string' ? item : item?.prompt ?? '')
|
|
161
169
|
.filter(prompt => !lodash_1.default.isEmpty(lodash_1.default.trim(String(prompt))));
|
|
162
170
|
}
|
|
171
|
+
serializeTool(h) {
|
|
172
|
+
let parameters = h.parameters;
|
|
173
|
+
if (typeof parameters === 'string') {
|
|
174
|
+
try {
|
|
175
|
+
parameters = JSON.parse(parameters);
|
|
176
|
+
}
|
|
177
|
+
catch {
|
|
178
|
+
parameters = {};
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
const tool = {
|
|
182
|
+
type: 'function',
|
|
183
|
+
name: h.name,
|
|
184
|
+
description: h.description,
|
|
185
|
+
parameters: parameters || {}
|
|
186
|
+
};
|
|
187
|
+
if (h.feedback != null) {
|
|
188
|
+
const validatedFeedback = this.validatePerToolFeedback(h.feedback, h.name);
|
|
189
|
+
if (validatedFeedback) {
|
|
190
|
+
tool.feedback = validatedFeedback;
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
if (h.timeout_seconds != null && h.timeout_seconds !== '') {
|
|
194
|
+
tool.timeout_seconds = Number(h.timeout_seconds);
|
|
195
|
+
}
|
|
196
|
+
if (h.response_mode) {
|
|
197
|
+
tool.response_mode = h.response_mode;
|
|
198
|
+
}
|
|
199
|
+
return tool;
|
|
200
|
+
}
|
|
201
|
+
serializeSubagents(subagents) {
|
|
202
|
+
if (!Array.isArray(subagents))
|
|
203
|
+
return [];
|
|
204
|
+
return subagents
|
|
205
|
+
.filter(sa => sa != null && !lodash_1.default.isEmpty(lodash_1.default.trim(sa.name)))
|
|
206
|
+
.map(sa => {
|
|
207
|
+
const name = lodash_1.default.trim(sa.name);
|
|
208
|
+
const serialized = {
|
|
209
|
+
name,
|
|
210
|
+
system_instruction: sa.system_instruction || '',
|
|
211
|
+
developer_messages: (sa.developer_messages || [])
|
|
212
|
+
.map(msg => typeof msg === 'string' ? msg : '')
|
|
213
|
+
.filter(msg => !lodash_1.default.isEmpty(lodash_1.default.trim(msg))),
|
|
214
|
+
context: sa.context === 'shared' ? 'shared' : 'own',
|
|
215
|
+
tools: (sa.tools || [])
|
|
216
|
+
.filter(t => t != null && !lodash_1.default.isEmpty(lodash_1.default.trim(t.name)))
|
|
217
|
+
.map(t => this.serializeTool(t))
|
|
218
|
+
};
|
|
219
|
+
const llm = this.buildAgentServiceConfig(`subagent "${name}" llm`, sa.llm || {});
|
|
220
|
+
if (llm)
|
|
221
|
+
serialized.llm = llm;
|
|
222
|
+
return serialized;
|
|
223
|
+
});
|
|
224
|
+
}
|
|
225
|
+
findSubagentForTool(toolName) {
|
|
226
|
+
const needle = lodash_1.default.trim(toolName);
|
|
227
|
+
if (!needle)
|
|
228
|
+
return undefined;
|
|
229
|
+
const subagents = this.data.subagents || [];
|
|
230
|
+
return subagents.find(sa => (sa?.tools || []).some(t => t && lodash_1.default.trim(String(t.name || '')) === needle));
|
|
231
|
+
}
|
|
232
|
+
findSubagentByName(name) {
|
|
233
|
+
if (typeof name !== 'string' || lodash_1.default.isEmpty(lodash_1.default.trim(name)))
|
|
234
|
+
return undefined;
|
|
235
|
+
const trimmed = lodash_1.default.trim(name);
|
|
236
|
+
return (this.data.subagents || []).find(sa => lodash_1.default.trim(sa?.name || '') === trimmed);
|
|
237
|
+
}
|
|
238
|
+
resolveSubagentExit(subagent) {
|
|
239
|
+
if (subagent.exitId && this.getExitStepId(subagent.exitId, false)) {
|
|
240
|
+
return subagent.exitId;
|
|
241
|
+
}
|
|
242
|
+
const name = lodash_1.default.trim(subagent.name || '');
|
|
243
|
+
return this.findExitByLabel(`subagent_${name}`) || this.findExitByLabel(name);
|
|
244
|
+
}
|
|
163
245
|
findExitByLabel(label) {
|
|
164
|
-
const
|
|
246
|
+
const needle = lodash_1.default.trim(label);
|
|
247
|
+
const exit = lodash_1.default.find(this.step.exits, (e) => lodash_1.default.trim(e.label || '') === needle);
|
|
165
248
|
return exit?.id;
|
|
166
249
|
}
|
|
167
250
|
validateToolFeedback(config) {
|