@onereach/step-voice 7.0.45 → 7.0.47-VOIC1732knowledgeretrieval.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.
@@ -22,8 +22,8 @@ class IdleMusic extends voice_1.default {
22
22
  async runStep() {
23
23
  const call = await this.fetchData();
24
24
  const { idleDuration, idleMusicDelay, idleMusicEnabled, customIdleMusic, idleMusicUrl } = this.data;
25
- const idleDurationMs = (0, timestring_1.default)(lodash_1.default.get(idleDuration, 'input', '30s'), lodash_1.default.get(idleDuration, 'outputMode', 'ms'));
26
- const idleMusicDelayMs = (0, timestring_1.default)(lodash_1.default.get(idleMusicDelay, 'input', '3s'), lodash_1.default.get(idleMusicDelay, 'outputMode', 'ms'));
25
+ const idleDurationMs = (0, timestring_1.default)(lodash_1.default.get(idleDuration, 'input', '30s'), lodash_1.default.get(idleDuration, 'outputMode', 'ms'), {});
26
+ const idleMusicDelayMs = (0, timestring_1.default)(lodash_1.default.get(idleMusicDelay, 'input', '3s'), lodash_1.default.get(idleMusicDelay, 'outputMode', 'ms'), {});
27
27
  const { error: validationError } = await schema.validateAsync({
28
28
  idleMusicEnabled,
29
29
  customIdleMusic,
@@ -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
- return {
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) {
@@ -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 AgentSystemToolConfig {
36
+ interface SubagentTool {
34
37
  name: string;
35
- description?: string;
36
- parameters?: Record<string, unknown>;
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,30 @@ 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
+ }
66
+ type KnowledgeMode = 'tool' | 'pipeline';
67
+ interface AgentKnowledgeConfig {
68
+ id: string;
69
+ mode: KnowledgeMode;
70
+ }
44
71
  interface INPUT {
45
72
  handlers: Handler[];
73
+ subagents: Subagent[];
46
74
  system_instruction: string;
47
75
  developer_messages: string[];
48
76
  greeting_message: string;
@@ -69,10 +97,16 @@ interface INPUT {
69
97
  tool_feedback?: ToolCallFeedbackConfig;
70
98
  soft_timeout?: SoftTimeoutConfig;
71
99
  system_tools?: AgentSystemToolConfig[];
100
+ knowledge?: AgentKnowledgeConfig[];
72
101
  }
73
102
  export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
74
103
  runStep(): Promise<void>;
75
104
  private normalizeIdlePrompts;
105
+ private serializeTool;
106
+ private serializeSubagents;
107
+ private findSubagentForTool;
108
+ private findSubagentByName;
109
+ private resolveSubagentExit;
76
110
  private findExitByLabel;
77
111
  private validateToolFeedback;
78
112
  private isStepFlagEnabled;
@@ -81,6 +115,7 @@ export default class VoiceAgent extends VoiceStep<Partial<INPUT>> {
81
115
  private validatePerToolFeedback;
82
116
  private validateSoftTimeout;
83
117
  private validateSystemTools;
118
+ private validateKnowledge;
84
119
  private parseAgentServiceOptions;
85
120
  exitToThread(): Promise<void>;
86
121
  }
@@ -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, knowledge = [], 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);
@@ -147,6 +155,9 @@ class VoiceAgent extends voice_1.default {
147
155
  if (system_tools != null) {
148
156
  config.system_tools = this.validateSystemTools(system_tools);
149
157
  }
158
+ if (!lodash_1.default.isEmpty(knowledge)) {
159
+ config.knowledge = this.validateKnowledge(knowledge);
160
+ }
150
161
  const params = { config };
151
162
  if (agentCustomUrl) {
152
163
  params.url = agentCustomUrl;
@@ -160,8 +171,83 @@ class VoiceAgent extends voice_1.default {
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
+ if (h.timeout_seconds != null && h.timeout_seconds !== '') {
197
+ tool.timeout_seconds = Number(h.timeout_seconds);
198
+ }
199
+ if (h.response_mode) {
200
+ tool.response_mode = h.response_mode;
201
+ }
202
+ return tool;
203
+ }
204
+ serializeSubagents(subagents) {
205
+ if (!Array.isArray(subagents))
206
+ return [];
207
+ return subagents
208
+ .filter(sa => sa != null && !lodash_1.default.isEmpty(lodash_1.default.trim(sa.name)))
209
+ .map(sa => {
210
+ const name = lodash_1.default.trim(sa.name);
211
+ const serialized = {
212
+ name,
213
+ system_instruction: sa.system_instruction || '',
214
+ developer_messages: (sa.developer_messages || [])
215
+ .map(msg => typeof msg === 'string' ? msg : '')
216
+ .filter(msg => !lodash_1.default.isEmpty(lodash_1.default.trim(msg))),
217
+ context: sa.context === 'shared' ? 'shared' : 'own',
218
+ tools: (sa.tools || [])
219
+ .filter(t => t != null && !lodash_1.default.isEmpty(lodash_1.default.trim(t.name)))
220
+ .map(t => this.serializeTool(t))
221
+ };
222
+ const llm = this.buildAgentServiceConfig(`subagent "${name}" llm`, sa.llm || {});
223
+ if (llm)
224
+ serialized.llm = llm;
225
+ return serialized;
226
+ });
227
+ }
228
+ findSubagentForTool(toolName) {
229
+ const needle = lodash_1.default.trim(toolName);
230
+ if (!needle)
231
+ return undefined;
232
+ const subagents = this.data.subagents || [];
233
+ return subagents.find(sa => (sa?.tools || []).some(t => t && lodash_1.default.trim(String(t.name || '')) === needle));
234
+ }
235
+ findSubagentByName(name) {
236
+ if (typeof name !== 'string' || lodash_1.default.isEmpty(lodash_1.default.trim(name)))
237
+ return undefined;
238
+ const trimmed = lodash_1.default.trim(name);
239
+ return (this.data.subagents || []).find(sa => lodash_1.default.trim(sa?.name || '') === trimmed);
240
+ }
241
+ resolveSubagentExit(subagent) {
242
+ if (subagent.exitId && this.getExitStepId(subagent.exitId, false)) {
243
+ return subagent.exitId;
244
+ }
245
+ const name = lodash_1.default.trim(subagent.name || '');
246
+ return this.findExitByLabel(`subagent_${name}`) || this.findExitByLabel(name);
247
+ }
163
248
  findExitByLabel(label) {
164
- const exit = lodash_1.default.find(this.step.exits, (e) => e.label === label);
249
+ const needle = lodash_1.default.trim(label);
250
+ const exit = lodash_1.default.find(this.step.exits, (e) => lodash_1.default.trim(e.label || '') === needle);
165
251
  return exit?.id;
166
252
  }
167
253
  validateToolFeedback(config) {
@@ -333,6 +419,20 @@ class VoiceAgent extends voice_1.default {
333
419
  description: lodash_1.default.isEmpty(entry.description) ? undefined : entry.description
334
420
  }));
335
421
  }
422
+ validateKnowledge(knowledge) {
423
+ if (!Array.isArray(knowledge)) {
424
+ this.throwError(new Error('Voice Agent: knowledge must be an array'));
425
+ }
426
+ const modes = ['tool', 'pipeline'];
427
+ return lodash_1.default.uniqBy(knowledge
428
+ .filter(entry => entry != null && typeof entry === 'object')
429
+ .map((entry) => ({
430
+ id: lodash_1.default.trim(String(entry.id ?? '')),
431
+ // An unrecognised mode degrades to tool search rather than failing a live call.
432
+ mode: lodash_1.default.includes(modes, entry.mode) ? entry.mode : 'tool'
433
+ }))
434
+ .filter(entry => !lodash_1.default.isEmpty(entry.id)), entry => entry.id);
435
+ }
336
436
  parseAgentServiceOptions(label, options) {
337
437
  if (options == null || options === '')
338
438
  return undefined;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@onereach/step-voice",
3
- "version": "7.0.45",
3
+ "version": "7.0.47-VOIC1732knowledgeretrieval.0",
4
4
  "author": "Roman Zolotarov <roman.zolotarov@onereach.com>",
5
5
  "contributors": [
6
6
  "Roman Zolotarov",
@@ -16,6 +16,7 @@
16
16
  "@onereach/step-conversation": "^1.0.40",
17
17
  "lodash": "^4.17.21",
18
18
  "nanoid": "^3.3.4",
19
+ "timestring": "^6.0.0",
19
20
  "uuid": "^9.0.0"
20
21
  },
21
22
  "devDependencies": {