@onereach/step-voice 7.0.41-subagents.3 → 7.0.41-subagents.4

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.
@@ -4,6 +4,7 @@ interface INPUT {
4
4
  }
5
5
  export default class SubagentToolsHandler extends VoiceStep<Partial<INPUT>> {
6
6
  runStep(): Promise<void>;
7
+ private loadToolCallContext;
7
8
  private findExitByLabel;
8
9
  exitToThread(): Promise<void>;
9
10
  }
@@ -6,69 +6,62 @@ const voice_1 = tslib_1.__importDefault(require("./voice"));
6
6
  class SubagentToolsHandler extends voice_1.default {
7
7
  async runStep() {
8
8
  const call = await this.fetchData();
9
- let ctx = null;
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 = {};
10
52
  try {
11
- ctx = await this.thread.get(`__voiceAgentContext_${this.thread.id}`);
53
+ const stored = await this.thread.get(`__voiceAgentContext_${this.thread.id}`);
54
+ if (stored && typeof stored === 'object')
55
+ fromThread = stored;
12
56
  }
13
57
  catch {
14
- ctx = null;
58
+ // key absent on this thread — Voice Agent payload may still be in state.result
15
59
  }
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
- });
60
+ return { ...fromState, ...fromThread };
69
61
  }
70
62
  findExitByLabel(label) {
71
- const exit = lodash_1.default.find(this.step.exits, (e) => e.label === 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);
72
65
  return exit?.id;
73
66
  }
74
67
  async exitToThread() {
@@ -90,16 +90,12 @@ interface INPUT {
90
90
  }
91
91
  export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
92
92
  runStep(): Promise<void>;
93
- /**
94
- * Fork a thread per subagent exit so Subagent Tools Handler steps can
95
- * register tool_call listeners immediately after agent start.
96
- */
97
- private spawnSubagentHandlerThreads;
98
93
  private normalizeIdlePrompts;
99
94
  private serializeTool;
100
95
  private serializeSubagents;
101
- private isSubagentExitLabel;
102
- private isSubagentTool;
96
+ private findSubagentForTool;
97
+ private findSubagentByName;
98
+ private resolveSubagentExit;
103
99
  private findExitByLabel;
104
100
  private validateToolFeedback;
105
101
  private isStepFlagEnabled;
@@ -12,24 +12,47 @@ class VoiceAgent extends voice_1.default {
12
12
  if (event.params.type !== 'tool_call')
13
13
  return;
14
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
15
  const cb = this.thread.takeCallback();
20
16
  const params = event.params.arguments ?? {};
21
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
+ }
22
53
  const exitId = this.findExitByLabel(name) ?? this.getExitStepId('tool_call');
23
54
  if (exitId) {
24
- this.exitStep(exitId, {
25
- [name]: params,
26
- name,
27
- toolCallId,
28
- __voicecb: cb,
29
- callId: call.id,
30
- callType: call.type,
31
- callCallback: call.callback
32
- }, true);
55
+ this.exitStep(exitId, payload, true);
33
56
  }
34
57
  else {
35
58
  this.log.warn('no exit for tool_call', { name });
@@ -137,35 +160,9 @@ class VoiceAgent extends voice_1.default {
137
160
  params.url = agentCustomUrl;
138
161
  }
139
162
  await this.sendCommands(call, [{ name: 'startAgent', params }]);
140
- this.spawnSubagentHandlerThreads(call, subagents);
141
163
  return this.exitFlow();
142
164
  });
143
165
  }
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
- }
169
166
  normalizeIdlePrompts(idlePrompts) {
170
167
  return (idlePrompts || [])
171
168
  .map(item => typeof item === 'string' ? item : item?.prompt ?? '')
@@ -219,19 +216,29 @@ class VoiceAgent extends voice_1.default {
219
216
  return serialized;
220
217
  });
221
218
  }
222
- isSubagentExitLabel(label) {
219
+ findSubagentForTool(toolName) {
220
+ const needle = lodash_1.default.trim(toolName);
221
+ if (!needle)
222
+ return undefined;
223
223
  const subagents = this.data.subagents || [];
224
- return subagents.some(sa => {
225
- const name = lodash_1.default.trim(sa?.name || '');
226
- return !!name && label === `subagent_${name}`;
227
- });
224
+ return subagents.find(sa => (sa?.tools || []).some(t => t && lodash_1.default.trim(String(t.name || '')) === needle));
228
225
  }
229
- isSubagentTool(toolName) {
230
- const subagents = this.data.subagents || [];
231
- return subagents.some(sa => (sa?.tools || []).some(t => t && lodash_1.default.trim(t.name) === toolName));
226
+ findSubagentByName(name) {
227
+ if (typeof name !== 'string' || lodash_1.default.isEmpty(lodash_1.default.trim(name)))
228
+ return undefined;
229
+ const trimmed = lodash_1.default.trim(name);
230
+ return (this.data.subagents || []).find(sa => lodash_1.default.trim(sa?.name || '') === trimmed);
231
+ }
232
+ resolveSubagentExit(subagent) {
233
+ if (subagent.exitId && this.getExitStepId(subagent.exitId, false)) {
234
+ return subagent.exitId;
235
+ }
236
+ const name = lodash_1.default.trim(subagent.name || '');
237
+ return this.findExitByLabel(`subagent_${name}`) || this.findExitByLabel(name);
232
238
  }
233
239
  findExitByLabel(label) {
234
- const exit = lodash_1.default.find(this.step.exits, (e) => e.label === label);
240
+ const needle = lodash_1.default.trim(label);
241
+ const exit = lodash_1.default.find(this.step.exits, (e) => lodash_1.default.trim(e.label || '') === needle);
235
242
  return exit?.id;
236
243
  }
237
244
  validateToolFeedback(config) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onereach/step-voice",
3
- "version": "7.0.41-subagents.3",
3
+ "version": "7.0.41-subagents.4",
4
4
  "author": "Roman Zolotarov <roman.zolotarov@onereach.com>",
5
5
  "contributors": [
6
6
  "Roman Zolotarov",