@guava-ai/guava-sdk 0.3.0 → 0.4.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/README.internal.md +13 -0
- package/README.md +50 -8
- package/bin/example-runner.js +16 -7
- package/dist/examples/credit-card-activation.js +94 -112
- package/dist/examples/credit-card-activation.js.map +1 -1
- package/dist/examples/property-insurance.js +12 -26
- package/dist/examples/property-insurance.js.map +1 -1
- package/dist/examples/scheduling-outbound.d.ts +1 -0
- package/dist/examples/scheduling-outbound.js +77 -0
- package/dist/examples/scheduling-outbound.js.map +1 -0
- package/dist/examples/thai-palace.js +25 -43
- package/dist/examples/thai-palace.js.map +1 -1
- package/dist/package.json +9 -5
- package/dist/src/action_item.d.ts +34 -12
- package/dist/src/action_item.js +34 -7
- package/dist/src/action_item.js.map +1 -1
- package/dist/src/call-controller.d.ts +137 -0
- package/dist/src/call-controller.js +433 -0
- package/dist/src/call-controller.js.map +1 -0
- package/dist/src/commands.d.ts +67 -27
- package/dist/src/commands.js +41 -27
- package/dist/src/commands.js.map +1 -1
- package/dist/src/events.d.ts +47 -30
- package/dist/src/events.js +42 -36
- package/dist/src/events.js.map +1 -1
- package/dist/src/example_data.d.ts +1 -0
- package/dist/src/example_data.js +33 -0
- package/dist/src/example_data.js.map +1 -1
- package/dist/src/helpers/openai.d.ts +12 -1
- package/dist/src/helpers/openai.js +168 -68
- package/dist/src/helpers/openai.js.map +1 -1
- package/dist/src/index.d.ts +6 -121
- package/dist/src/index.js +249 -483
- package/dist/src/index.js.map +1 -1
- package/dist/src/logging.d.ts +2 -1
- package/dist/src/logging.js +32 -7
- package/dist/src/logging.js.map +1 -1
- package/dist/src/telemetry.d.ts +23 -0
- package/dist/src/telemetry.js +98 -0
- package/dist/src/telemetry.js.map +1 -0
- package/dist/src/utils.d.ts +3 -0
- package/dist/src/utils.js +28 -0
- package/dist/src/utils.js.map +1 -0
- package/examples/biome.json +5 -0
- package/examples/credit-card-activation.ts +20 -26
- package/examples/property-insurance.ts +6 -16
- package/examples/scheduling-outbound.ts +97 -0
- package/examples/thai-palace.ts +10 -13
- package/package.json +9 -5
- package/src/action_item.ts +53 -13
- package/src/call-controller.ts +451 -0
- package/src/commands.ts +58 -42
- package/src/events.ts +66 -51
- package/src/example_data.ts +42 -0
- package/src/helpers/openai.ts +73 -18
- package/src/index.ts +81 -403
- package/src/logging.ts +39 -7
- package/src/telemetry.ts +125 -0
- package/src/utils.ts +32 -0
package/src/action_item.ts
CHANGED
|
@@ -1,23 +1,26 @@
|
|
|
1
1
|
import * as z from "zod";
|
|
2
2
|
|
|
3
|
-
export const
|
|
4
|
-
(["text", "date", "datetime", "integer", "multiple_choice"] as const).map(
|
|
3
|
+
export const FieldItemType = z.union(
|
|
4
|
+
(["text", "date", "datetime", "integer", "multiple_choice", "calendar_slot"] as const).map(
|
|
5
5
|
(val) => z.literal(val),
|
|
6
6
|
),
|
|
7
7
|
);
|
|
8
|
-
export type FieldItemType = z.input<typeof
|
|
8
|
+
export type FieldItemType = z.input<typeof FieldItemType>;
|
|
9
9
|
|
|
10
|
-
export
|
|
10
|
+
export type ChoiceGenerator = (query: string) => Promise<[string[], string[]]>;
|
|
11
|
+
|
|
12
|
+
export const FieldItem = z
|
|
11
13
|
.object({
|
|
12
14
|
item_type: z.literal("field"),
|
|
13
15
|
key: z.string(),
|
|
14
16
|
description: z.string(),
|
|
15
|
-
field_type:
|
|
17
|
+
field_type: FieldItemType,
|
|
16
18
|
required: z.boolean().default(true),
|
|
17
19
|
choices: z.array(z.string()).default([]),
|
|
20
|
+
choiceGenerator: z.custom<ChoiceGenerator>((val) => typeof val === "function").optional(),
|
|
18
21
|
})
|
|
19
22
|
.refine((field) => {
|
|
20
|
-
if (field.field_type
|
|
23
|
+
if (field.field_type === "multiple_choice" && field.choices.length > 10) {
|
|
21
24
|
process.emitWarning(
|
|
22
25
|
"Performance degrades with large number of choices for multiple choice field.",
|
|
23
26
|
"ACTION_ITEM",
|
|
@@ -25,21 +28,58 @@ export const fieldItem = z
|
|
|
25
28
|
}
|
|
26
29
|
return true;
|
|
27
30
|
});
|
|
28
|
-
export type
|
|
31
|
+
export type FieldItem = z.input<typeof FieldItem>;
|
|
32
|
+
|
|
33
|
+
export const SerializableFieldItem = z.object({
|
|
34
|
+
item_type: z.literal("field"),
|
|
35
|
+
key: z.string(),
|
|
36
|
+
description: z.string(),
|
|
37
|
+
field_type: FieldItemType,
|
|
38
|
+
required: z.boolean().default(true),
|
|
39
|
+
choices: z.array(z.string()).default([]),
|
|
40
|
+
is_search_field: z.boolean().default(false),
|
|
41
|
+
});
|
|
42
|
+
export type SerializableFieldItem = z.input<typeof SerializableFieldItem>;
|
|
29
43
|
|
|
30
|
-
export const
|
|
44
|
+
export const SayItem = z.object({
|
|
31
45
|
item_type: z.literal("say"),
|
|
32
46
|
statement: z.string(),
|
|
33
47
|
key: z.string().default(() => Math.random().toString(16).substring(2, 6)),
|
|
34
48
|
});
|
|
35
|
-
export type
|
|
49
|
+
export type SayItem = z.input<typeof SayItem>;
|
|
36
50
|
|
|
37
|
-
export const
|
|
51
|
+
export const TodoItem = z.object({
|
|
38
52
|
item_type: z.literal("todo"),
|
|
39
53
|
description: z.string(),
|
|
40
54
|
key: z.string().default(() => Math.random().toString(16).substring(2, 6)),
|
|
41
55
|
});
|
|
42
|
-
export type
|
|
56
|
+
export type TodoItem = z.input<typeof TodoItem>;
|
|
43
57
|
|
|
44
|
-
export const
|
|
45
|
-
export type ActionItem = z.input<typeof
|
|
58
|
+
export const ActionItem = z.union([SerializableFieldItem, SayItem, TodoItem]);
|
|
59
|
+
export type ActionItem = z.input<typeof ActionItem>;
|
|
60
|
+
|
|
61
|
+
export function Field(options: {
|
|
62
|
+
key: string;
|
|
63
|
+
description: string;
|
|
64
|
+
fieldType: FieldItemType;
|
|
65
|
+
required?: boolean;
|
|
66
|
+
choices?: string[];
|
|
67
|
+
choiceGenerator?: ChoiceGenerator;
|
|
68
|
+
}): FieldItem {
|
|
69
|
+
return FieldItem.parse({
|
|
70
|
+
item_type: "field",
|
|
71
|
+
key: options.key,
|
|
72
|
+
description: options.description,
|
|
73
|
+
field_type: options.fieldType,
|
|
74
|
+
required: options.required,
|
|
75
|
+
choices: options.choices,
|
|
76
|
+
choiceGenerator: options.choiceGenerator,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export function Say(statement: string): SayItem {
|
|
81
|
+
return SayItem.parse({
|
|
82
|
+
item_type: "say",
|
|
83
|
+
statement: statement,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
@@ -0,0 +1,451 @@
|
|
|
1
|
+
import { type Logger, getDefaultLogger } from "./logging.ts";
|
|
2
|
+
import {
|
|
3
|
+
AcceptInboundCallCommand,
|
|
4
|
+
type Command,
|
|
5
|
+
SetPersona,
|
|
6
|
+
SetTaskCommand,
|
|
7
|
+
AnswerQuestionCommand,
|
|
8
|
+
SendInstructionCommand,
|
|
9
|
+
ReadScriptCommand,
|
|
10
|
+
RejectInboundCallCommand,
|
|
11
|
+
TransferCommand,
|
|
12
|
+
ChoiceResultCommand,
|
|
13
|
+
} from "./commands.ts";
|
|
14
|
+
import type * as z from "zod";
|
|
15
|
+
import type { GuavaEvent, CallerSpeechEvent, AgentSpeechEvent } from "./events.ts";
|
|
16
|
+
import {
|
|
17
|
+
type ActionItem,
|
|
18
|
+
type ChoiceGenerator,
|
|
19
|
+
type FieldItem,
|
|
20
|
+
type SayItem,
|
|
21
|
+
type SerializableFieldItem,
|
|
22
|
+
Say,
|
|
23
|
+
type TodoItem,
|
|
24
|
+
} from "./action_item.ts";
|
|
25
|
+
import { telemetryClient } from "./telemetry.ts";
|
|
26
|
+
|
|
27
|
+
export type TaskObjective =
|
|
28
|
+
| { objective: string }
|
|
29
|
+
| { objective?: string; checklist: (FieldItem | SayItem | string)[] };
|
|
30
|
+
|
|
31
|
+
export type ReachPersonOutcome = {
|
|
32
|
+
key: string;
|
|
33
|
+
onOutcome: () => void;
|
|
34
|
+
description?: string;
|
|
35
|
+
nextActionPreview?: string;
|
|
36
|
+
};
|
|
37
|
+
|
|
38
|
+
type ReachPersonOptions =
|
|
39
|
+
| { onSuccess: () => void; onFailure: () => void; greeting?: string }
|
|
40
|
+
| { outcomes: ReachPersonOutcome[]; greeting?: string };
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Interface between Guava services and user-supplied code
|
|
44
|
+
*/
|
|
45
|
+
@telemetryClient.trackClass()
|
|
46
|
+
export class CallController {
|
|
47
|
+
private _commandQueue: Command[] = [];
|
|
48
|
+
private _on_complete_current_task?: () => void;
|
|
49
|
+
// private _field_values: Record<string, any>;
|
|
50
|
+
private _current_task_id?: string;
|
|
51
|
+
/**
|
|
52
|
+
* @protected
|
|
53
|
+
* @description logger used to emit diagnostics
|
|
54
|
+
*/
|
|
55
|
+
protected logger: Logger;
|
|
56
|
+
// drain functions are expected to cleanup
|
|
57
|
+
// the part of the queue that is successfully sent from its
|
|
58
|
+
// input (mutating it) (i.e. _drain should use Array.splice)
|
|
59
|
+
private _drain?: (_: Command[]) => Promise<void>;
|
|
60
|
+
private _fieldValues: Record<string, unknown> = {};
|
|
61
|
+
private _searchFunctionsByKey: Record<string, ChoiceGenerator> = {};
|
|
62
|
+
|
|
63
|
+
constructor(logger: Logger = getDefaultLogger()) {
|
|
64
|
+
// Set up the default logger.
|
|
65
|
+
this.logger = logger;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* @description Supply a function used to consume commands from the internal command queue.
|
|
70
|
+
*
|
|
71
|
+
* The function is expected to remove from the argument array commands that it has handled (iterating
|
|
72
|
+
* through the result of `Array.splice(0)` is sufficient)
|
|
73
|
+
*/
|
|
74
|
+
setDrain(newDrain: (_: Command[]) => Promise<void>) {
|
|
75
|
+
this._drain = newDrain;
|
|
76
|
+
this.flush();
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* @description [inbound] receive a call, and process further.
|
|
81
|
+
*/
|
|
82
|
+
protected async acceptCall() {
|
|
83
|
+
await this.sendCommand(AcceptInboundCallCommand, {
|
|
84
|
+
command_type: "accept-inbound",
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* @description read a span of text verbatim
|
|
90
|
+
*/
|
|
91
|
+
protected async readScript(script: string) {
|
|
92
|
+
await this.sendCommand(ReadScriptCommand, {
|
|
93
|
+
command_type: "read-script",
|
|
94
|
+
script: script,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* @description [inbound] reject a call
|
|
100
|
+
*/
|
|
101
|
+
protected async rejectCall() {
|
|
102
|
+
await this.sendCommand(RejectInboundCallCommand, {
|
|
103
|
+
command_type: "reject-inbound",
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
protected async addInfo(_info: string) {
|
|
108
|
+
throw new Error("not implemeneted");
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* @description read a span of text non-verbatim
|
|
113
|
+
*/
|
|
114
|
+
protected async sendInstruction(instruction: string) {
|
|
115
|
+
await this.sendCommand(SendInstructionCommand, {
|
|
116
|
+
command_type: "send-instruction",
|
|
117
|
+
instruction: instruction,
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* @description provide identifiers the agent will use to identify the virtual agent
|
|
123
|
+
*/
|
|
124
|
+
protected async setPersona(args: {
|
|
125
|
+
organizationName?: string;
|
|
126
|
+
agentName?: string;
|
|
127
|
+
agentPurpose?: string;
|
|
128
|
+
voice?: string;
|
|
129
|
+
}) {
|
|
130
|
+
await this.sendCommand(SetPersona, {
|
|
131
|
+
command_type: "set-persona",
|
|
132
|
+
organization_name: args.organizationName,
|
|
133
|
+
agent_name: args.agentName,
|
|
134
|
+
agent_purpose: args.agentPurpose,
|
|
135
|
+
voice: args.voice,
|
|
136
|
+
});
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
/**
|
|
140
|
+
* @description direct the agent to collect information
|
|
141
|
+
* @param goal {} an objective string and/or a checklist of information to collect
|
|
142
|
+
* @param on_complete {} a callback to call once the information is available from the agent
|
|
143
|
+
* @param args {} arguments to pass through to the `on_complete` callback
|
|
144
|
+
*/
|
|
145
|
+
protected setTask(
|
|
146
|
+
goal: TaskObjective,
|
|
147
|
+
on_complete: (...c: any[]) => void = () => {},
|
|
148
|
+
...args: any[]
|
|
149
|
+
) {
|
|
150
|
+
this._current_task_id = Math.random().toString(16).substring(2, 8);
|
|
151
|
+
this._on_complete_current_task = on_complete.bind(this, ...args);
|
|
152
|
+
if (!("checklist" in goal)) {
|
|
153
|
+
this.sendCommand(SetTaskCommand, {
|
|
154
|
+
command_type: "set-task",
|
|
155
|
+
task_id: this._current_task_id,
|
|
156
|
+
objective: goal.objective,
|
|
157
|
+
action_items: [],
|
|
158
|
+
});
|
|
159
|
+
} else {
|
|
160
|
+
const action_items = goal.checklist.map((item): ActionItem => {
|
|
161
|
+
if (typeof item === "string") {
|
|
162
|
+
return { item_type: "todo", description: item } satisfies TodoItem;
|
|
163
|
+
}
|
|
164
|
+
if (item.item_type === "field" && item.choiceGenerator) {
|
|
165
|
+
this._searchFunctionsByKey[item.key] = item.choiceGenerator;
|
|
166
|
+
const { choiceGenerator: _, ...fieldData } = item;
|
|
167
|
+
return {
|
|
168
|
+
...fieldData,
|
|
169
|
+
is_search_field: true,
|
|
170
|
+
} satisfies SerializableFieldItem;
|
|
171
|
+
}
|
|
172
|
+
return item;
|
|
173
|
+
});
|
|
174
|
+
this.sendCommand(SetTaskCommand, {
|
|
175
|
+
command_type: "set-task",
|
|
176
|
+
task_id: this._current_task_id,
|
|
177
|
+
objective: goal.objective ?? "",
|
|
178
|
+
action_items,
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* @description direct the agent to collect information, continuing execution once the agent has collected the information
|
|
185
|
+
* @param goal {} an objective string and/or a checklist of information to collect
|
|
186
|
+
*/
|
|
187
|
+
protected async awaitTask(goal: TaskObjective): Promise<void> {
|
|
188
|
+
return new Promise((resolve) => {
|
|
189
|
+
this.setTask(goal, (_args) => {
|
|
190
|
+
resolve();
|
|
191
|
+
});
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* @description retrieve a piece of information that the agent has collected
|
|
197
|
+
* @param key {string} key of the field checklist item
|
|
198
|
+
*/
|
|
199
|
+
protected getField(key: string) {
|
|
200
|
+
if (key in this._fieldValues) {
|
|
201
|
+
return this._fieldValues[key];
|
|
202
|
+
} else {
|
|
203
|
+
return null;
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* @description [inbound] hang up an accepted call
|
|
209
|
+
*/
|
|
210
|
+
protected async hangup(final_instructions: string = "") {
|
|
211
|
+
let instructions: string;
|
|
212
|
+
if (final_instructions) {
|
|
213
|
+
instructions = `Start ending the conversation. Here are your final instructions: ${final_instructions} Once you've completed the final instructions, naturally end the conversation and hang up the call.`;
|
|
214
|
+
} else {
|
|
215
|
+
instructions = "Naturally end the conversation and hang up the call.";
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
this.sendInstruction(instructions);
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* @description helper for reaching a specific contact on an outbound call and recording their availability.
|
|
223
|
+
*/
|
|
224
|
+
protected reachPerson(contactFullName: string, options: ReachPersonOptions) {
|
|
225
|
+
let outcomes: ReachPersonOutcome[];
|
|
226
|
+
if ("outcomes" in options) {
|
|
227
|
+
outcomes = options.outcomes;
|
|
228
|
+
} else {
|
|
229
|
+
outcomes = [
|
|
230
|
+
{
|
|
231
|
+
key: "contact_available",
|
|
232
|
+
onOutcome: options.onSuccess,
|
|
233
|
+
description: "The contact is available to speak.",
|
|
234
|
+
},
|
|
235
|
+
{
|
|
236
|
+
key: "contact_unavailable",
|
|
237
|
+
onOutcome: options.onFailure,
|
|
238
|
+
description:
|
|
239
|
+
"The contact is not available to speak. This includes reaching a wrong number.",
|
|
240
|
+
},
|
|
241
|
+
];
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
const outcomeHandlers = Object.fromEntries(outcomes.map((o) => [o.key, o.onOutcome]));
|
|
245
|
+
|
|
246
|
+
const initialGreeting: FieldItem | SayItem | string =
|
|
247
|
+
options.greeting !== undefined
|
|
248
|
+
? Say(options.greeting)
|
|
249
|
+
: `Greet the person who answered the phone. Notify them who you are calling on behalf of and the purpose of the call. Ask to speak with ${contactFullName}`;
|
|
250
|
+
|
|
251
|
+
const availabilityDescription =
|
|
252
|
+
`The availability of ${contactFullName}` +
|
|
253
|
+
(outcomes.some((o) => o.description)
|
|
254
|
+
? "\nDetailed descriptions of each choice:\n" +
|
|
255
|
+
outcomes
|
|
256
|
+
.filter((o) => o.description)
|
|
257
|
+
.map((o) => ` - ${o.key}: ${o.description}`)
|
|
258
|
+
.join("\n")
|
|
259
|
+
: "");
|
|
260
|
+
|
|
261
|
+
const nextActionLines = outcomes
|
|
262
|
+
.filter((o) => o.nextActionPreview)
|
|
263
|
+
.map((o) => `- ${o.key} → ${o.nextActionPreview}`);
|
|
264
|
+
const checklist: (FieldItem | SayItem | string)[] = [
|
|
265
|
+
initialGreeting,
|
|
266
|
+
{
|
|
267
|
+
item_type: "field",
|
|
268
|
+
key: "contact_availability",
|
|
269
|
+
field_type: "multiple_choice",
|
|
270
|
+
description: availabilityDescription,
|
|
271
|
+
choices: outcomes.map((o) => o.key),
|
|
272
|
+
},
|
|
273
|
+
];
|
|
274
|
+
if (nextActionLines.length > 0) {
|
|
275
|
+
checklist.push(
|
|
276
|
+
"If a next action is defined below for the value of `contact_availability`, briefly ask the contact to wait just a second while you perform it.\n" +
|
|
277
|
+
nextActionLines.join("\n"),
|
|
278
|
+
);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const objective = `\
|
|
282
|
+
OBJECTIVE:
|
|
283
|
+
Your goal is to reach ${contactFullName} and determine their availability to proceed with this call.
|
|
284
|
+
|
|
285
|
+
RULES:
|
|
286
|
+
1. If the initial respondent is NOT ${contactFullName}:
|
|
287
|
+
- Politely ask to speak with ${contactFullName}
|
|
288
|
+
- Wait to be transferred or for ${contactFullName} to come to the phone
|
|
289
|
+
2. Once you have ${contactFullName} on the line:
|
|
290
|
+
- Briefly restate who you are and the purpose of your call
|
|
291
|
+
- Determine and record their current availability status
|
|
292
|
+
3. DO NOT hang up the call under any circumstances, unless it's a wrong number.
|
|
293
|
+
|
|
294
|
+
TASK COMPLETION REQUIREMENTS:
|
|
295
|
+
- The availability of ${contactFullName} must be recorded in \`contact_availability\`.`;
|
|
296
|
+
|
|
297
|
+
this.setTask({ objective, checklist }, () => {
|
|
298
|
+
const availability = this.getField("contact_availability") as string;
|
|
299
|
+
const handler = outcomeHandlers[availability];
|
|
300
|
+
if (!handler) {
|
|
301
|
+
this.logger.error(`Unhandled contact_availability value: ${availability}`);
|
|
302
|
+
return;
|
|
303
|
+
}
|
|
304
|
+
this.logger.info(`Contact availability recorded: ${availability}`);
|
|
305
|
+
handler();
|
|
306
|
+
});
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* @description transfer an accepted call
|
|
311
|
+
*/
|
|
312
|
+
protected transfer(to_number: string, transfer_message?: string) {
|
|
313
|
+
const message = transfer_message ?? "I'm transferring you now";
|
|
314
|
+
this.sendCommand(TransferCommand, {
|
|
315
|
+
command_type: "transfer-call",
|
|
316
|
+
transfer_message: message,
|
|
317
|
+
to_number: to_number,
|
|
318
|
+
});
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
private async sendCommand<C extends Command, Schema extends z.ZodType<C>>(
|
|
322
|
+
schema: Schema,
|
|
323
|
+
data: z.input<Schema>,
|
|
324
|
+
) {
|
|
325
|
+
const command = schema.parse(data);
|
|
326
|
+
this._commandQueue.push(command);
|
|
327
|
+
await this.flush();
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
private async flush() {
|
|
331
|
+
await this._drain?.call(this, this._commandQueue);
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
async onEvent(event: GuavaEvent) {
|
|
335
|
+
this.logger.debug(`Event received: ${JSON.stringify(event)}`);
|
|
336
|
+
if (event.event_type === "caller-speech") {
|
|
337
|
+
this.onCallerSpeech(event);
|
|
338
|
+
} else if (event.event_type === "agent-speech") {
|
|
339
|
+
this.onAgentSpeech(event);
|
|
340
|
+
} else if (event.event_type === "agent-question") {
|
|
341
|
+
try {
|
|
342
|
+
this.logger.info(`Received question from bot: ${event.question}`);
|
|
343
|
+
const answer = await this.onQuestion(event.question);
|
|
344
|
+
await this.sendCommand(AnswerQuestionCommand, {
|
|
345
|
+
command_type: "answer-question",
|
|
346
|
+
question_id: event.question_id,
|
|
347
|
+
answer: answer,
|
|
348
|
+
});
|
|
349
|
+
} catch (e) {
|
|
350
|
+
this.logger.error("Error occured while answering question.");
|
|
351
|
+
await this.sendCommand(AnswerQuestionCommand, {
|
|
352
|
+
command_type: "answer-question",
|
|
353
|
+
question_id: event.question_id,
|
|
354
|
+
answer: "An error occured and the question could not be answered.",
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
} else if (event.event_type === "intent") {
|
|
358
|
+
this.logger.info(`Received intent ${event.intent_id} from bot: ${event.intent_summary}`);
|
|
359
|
+
const intent_response = await this.onIntent(event.intent_summary);
|
|
360
|
+
if (intent_response) {
|
|
361
|
+
const response_str = `Responding to intent ${event.intent_id}: ${intent_response}`;
|
|
362
|
+
this.logger.info(response_str);
|
|
363
|
+
this.sendInstruction(intent_response);
|
|
364
|
+
}
|
|
365
|
+
} else if (event.event_type === "task-done") {
|
|
366
|
+
// ignore obsolete task_completed events
|
|
367
|
+
if (event.task_id === this._current_task_id) {
|
|
368
|
+
// assertion is implied
|
|
369
|
+
const on_complete = this._on_complete_current_task;
|
|
370
|
+
this._on_complete_current_task = undefined;
|
|
371
|
+
if (on_complete) {
|
|
372
|
+
on_complete();
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
} else if (event.event_type === "choice-query") {
|
|
376
|
+
this.logger.info(`Received choice query for field ${event.field_key}: ${event.query}`);
|
|
377
|
+
const choiceGenerator = this._searchFunctionsByKey[event.field_key];
|
|
378
|
+
if (!choiceGenerator) {
|
|
379
|
+
this.logger.warn(
|
|
380
|
+
`Choice query for field '${event.field_key}' arrived but has no choice generator attached.`,
|
|
381
|
+
);
|
|
382
|
+
} else {
|
|
383
|
+
const [matchedChoices, otherChoices] = await choiceGenerator(event.query);
|
|
384
|
+
await this.sendCommand(ChoiceResultCommand, {
|
|
385
|
+
command_type: "choice-query-result",
|
|
386
|
+
field_key: event.field_key,
|
|
387
|
+
query_id: event.query_id,
|
|
388
|
+
matched_choices: matchedChoices,
|
|
389
|
+
other_choices: otherChoices,
|
|
390
|
+
});
|
|
391
|
+
}
|
|
392
|
+
} else if (event.event_type === "action-item-done") {
|
|
393
|
+
this._fieldValues[event.key] = event.payload;
|
|
394
|
+
if (event.key && event.payload) {
|
|
395
|
+
this.logger.info(`Field ${event.key} updated with value: ${event.payload}`);
|
|
396
|
+
}
|
|
397
|
+
} else if (event.event_type === "inbound-call") {
|
|
398
|
+
this.onIncomingCall(event.caller_number);
|
|
399
|
+
} else if (event.event_type === "bot-session-ended") {
|
|
400
|
+
this.onSessionDone();
|
|
401
|
+
} else if (event.event_type === "outbound-call-connected") {
|
|
402
|
+
// no-op, don't warn
|
|
403
|
+
} else if (event.event_type === "error") {
|
|
404
|
+
this.logger.error(`The Guava agent reported an error: ${event.content}`);
|
|
405
|
+
} else {
|
|
406
|
+
this.logger.warn(`Unhandled event: ${JSON.stringify(event)}`);
|
|
407
|
+
}
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
// callbacks
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* @description called when an inbound call is received. The overriding function must start
|
|
414
|
+
* with `await super.onIncomingCall(from_number)`
|
|
415
|
+
*/
|
|
416
|
+
async onIncomingCall(from_number?: string) {
|
|
417
|
+
await this.onCallStart();
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* @description called when a call is connected by the API, whether inbound or outbound
|
|
422
|
+
*/
|
|
423
|
+
async onCallStart(): Promise<void> {}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* @description called when the caller speaks to the agent.
|
|
427
|
+
*/
|
|
428
|
+
async onCallerSpeech(event: CallerSpeechEvent) {}
|
|
429
|
+
/**
|
|
430
|
+
* @description called when the agent speaks to the caller.
|
|
431
|
+
*/
|
|
432
|
+
async onAgentSpeech(event: AgentSpeechEvent) {}
|
|
433
|
+
/**
|
|
434
|
+
* @description called when the caller expresses a task they wish to execute
|
|
435
|
+
*/
|
|
436
|
+
async onIntent(intent: string): Promise<string | null> {
|
|
437
|
+
return "Unfortunately I'm not able to help with that.";
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* @description called when the agent needs to respond to a question that it doesn't know
|
|
441
|
+
* the answer to.
|
|
442
|
+
*/
|
|
443
|
+
async onQuestion(question: string): Promise<string> {
|
|
444
|
+
return "I don't have an answer to that question.";
|
|
445
|
+
}
|
|
446
|
+
|
|
447
|
+
/**
|
|
448
|
+
* @description called when the bot session has ended.
|
|
449
|
+
*/
|
|
450
|
+
onSessionDone(): void {}
|
|
451
|
+
}
|