@guava-ai/guava-sdk 0.1.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.
- package/bin/example-runner.js +24 -0
- package/dist/examples/property-insurance.d.ts +1 -0
- package/dist/examples/property-insurance.js +41 -0
- package/dist/examples/property-insurance.js.map +1 -0
- package/dist/package.json +40 -0
- package/dist/src/action_item.d.ts +41 -0
- package/dist/src/action_item.js +29 -0
- package/dist/src/action_item.js.map +1 -0
- package/dist/src/commands.d.ts +181 -0
- package/dist/src/commands.js +69 -0
- package/dist/src/commands.js.map +1 -0
- package/dist/src/events.d.ts +170 -0
- package/dist/src/events.js +109 -0
- package/dist/src/events.js.map +1 -0
- package/dist/src/example_data.d.ts +1 -0
- package/dist/src/example_data.js +610 -0
- package/dist/src/example_data.js.map +1 -0
- package/dist/src/helpers/openai.d.ts +12 -0
- package/dist/src/helpers/openai.js +111 -0
- package/dist/src/helpers/openai.js.map +1 -0
- package/dist/src/index.d.ts +58 -0
- package/dist/src/index.js +400 -0
- package/dist/src/index.js.map +1 -0
- package/dist/src/logging.d.ts +9 -0
- package/dist/src/logging.js +26 -0
- package/dist/src/logging.js.map +1 -0
- package/examples/property-insurance.ts +49 -0
- package/package.json +40 -0
- package/src/action_item.ts +42 -0
- package/src/commands.ts +94 -0
- package/src/events.ts +142 -0
- package/src/example_data.ts +609 -0
- package/src/helpers/openai.ts +105 -0
- package/src/index.ts +463 -0
- package/src/logging.ts +38 -0
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
11
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
|
12
|
+
var m = o[Symbol.asyncIterator], i;
|
|
13
|
+
return m ? m.call(o) : (o = typeof __values === "function" ? __values(o) : o[Symbol.iterator](), i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i);
|
|
14
|
+
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
|
15
|
+
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
|
16
|
+
};
|
|
17
|
+
import OpenAI, { toFile } from "openai";
|
|
18
|
+
// from beta.py
|
|
19
|
+
// TODO: Remove after beta
|
|
20
|
+
function beta_create_openai_client(logger) {
|
|
21
|
+
var _a;
|
|
22
|
+
const baseUrl = (_a = process.env.GUAVA_BASE_URL) !== null && _a !== void 0 ? _a : "wss://guava-dev.gridspace.com/guava/";
|
|
23
|
+
// to get it working with OpenAI TS/JS client
|
|
24
|
+
const basedUrl = new URL("openai/v1/", baseUrl);
|
|
25
|
+
basedUrl.protocol = /^wss:\/\//.test(basedUrl.toString())
|
|
26
|
+
? "https:"
|
|
27
|
+
: "http:";
|
|
28
|
+
logger.info(`Creating beta OpenAI client`);
|
|
29
|
+
return new OpenAI({
|
|
30
|
+
baseURL: basedUrl.toString(),
|
|
31
|
+
apiKey: process.env.GUAVA_API_KEY,
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
export class IntentRecognizer {
|
|
35
|
+
}
|
|
36
|
+
export class DocumentQA {
|
|
37
|
+
constructor(vector_store_name, document, logger, client) {
|
|
38
|
+
this.client = client !== null && client !== void 0 ? client : beta_create_openai_client(logger);
|
|
39
|
+
this.vector_store = this.getOrCreateVectorStore(vector_store_name, document);
|
|
40
|
+
this.logger = logger;
|
|
41
|
+
}
|
|
42
|
+
getOrCreateVectorStore(vector_store_name, document) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
var _a, e_1, _b, _c;
|
|
45
|
+
const encoder = new TextEncoder();
|
|
46
|
+
const document_buffer = encoder.encode(document);
|
|
47
|
+
const document_hash_buffer = yield crypto.subtle.digest("SHA-256", document_buffer);
|
|
48
|
+
const u8view = new Uint8Array(document_hash_buffer);
|
|
49
|
+
const document_hash = Array.from(u8view)
|
|
50
|
+
.map((b) => b.toString(16).padStart(2, "0"))
|
|
51
|
+
.join("");
|
|
52
|
+
try {
|
|
53
|
+
for (var _d = true, _e = __asyncValues(this.client.vectorStores.list()), _f; _f = yield _e.next(), _a = _f.done, !_a; _d = true) {
|
|
54
|
+
_c = _f.value;
|
|
55
|
+
_d = false;
|
|
56
|
+
const vs = _c;
|
|
57
|
+
if (vs.name == vector_store_name &&
|
|
58
|
+
vs.metadata &&
|
|
59
|
+
vs.metadata["document_hash"] == document_hash) {
|
|
60
|
+
this.logger.info("Re-using existing vector store...");
|
|
61
|
+
return vs;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
66
|
+
finally {
|
|
67
|
+
try {
|
|
68
|
+
if (!_d && !_a && (_b = _e.return)) yield _b.call(_e);
|
|
69
|
+
}
|
|
70
|
+
finally { if (e_1) throw e_1.error; }
|
|
71
|
+
}
|
|
72
|
+
this.logger.info("Creating vector store...");
|
|
73
|
+
const vector_store = yield this.client.vectorStores.create({
|
|
74
|
+
name: vector_store_name,
|
|
75
|
+
expires_after: {
|
|
76
|
+
anchor: "last_active_at",
|
|
77
|
+
days: 7,
|
|
78
|
+
},
|
|
79
|
+
});
|
|
80
|
+
this.logger.info("Uploading file...");
|
|
81
|
+
yield this.client.vectorStores.files.uploadAndPoll(vector_store.id, yield toFile(new Blob([document], { type: "text/plain" }), "document.txt"));
|
|
82
|
+
this.logger.info("Updating vector store metadata...");
|
|
83
|
+
yield this.client.vectorStores.update(vector_store.id, {
|
|
84
|
+
metadata: {
|
|
85
|
+
document_hash,
|
|
86
|
+
},
|
|
87
|
+
});
|
|
88
|
+
return vector_store;
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
ask(question) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const response = yield this.client.responses.create({
|
|
94
|
+
model: "gpt-5-mini",
|
|
95
|
+
instructions: "You are a virtual contact center agent. Your task is to answer questions using the provided supporting document. Just answer the question - do not offer any follow-ups.",
|
|
96
|
+
input: question,
|
|
97
|
+
tools: [
|
|
98
|
+
{
|
|
99
|
+
type: "file_search",
|
|
100
|
+
vector_store_ids: [(yield this.vector_store).id],
|
|
101
|
+
},
|
|
102
|
+
],
|
|
103
|
+
reasoning: {
|
|
104
|
+
effort: "low",
|
|
105
|
+
},
|
|
106
|
+
});
|
|
107
|
+
return response.output_text;
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
//# sourceMappingURL=openai.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../../src/helpers/openai.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,OAAO,MAAM,EAAE,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAGxC,eAAe;AACf,0BAA0B;AAC1B,SAAS,yBAAyB,CAAC,MAAc;;IAC/C,MAAM,OAAO,GACX,MAAA,OAAO,CAAC,GAAG,CAAC,cAAc,mCAAI,sCAAsC,CAAC;IACvE,6CAA6C;IAC7C,MAAM,QAAQ,GAAG,IAAI,GAAG,CAAC,YAAY,EAAE,OAAO,CAAC,CAAC;IAChD,QAAQ,CAAC,QA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IAEK,GAAG,CAAC,QAAgB;;YACxB,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,MAAM,CAAC;gBAClD,KAAK,EAAE,YAAY;gBACnB,YAAY,EACV,0KAA0K;gBAC5K,KAAK,EAAE,QAAQ;gBACf,KAAK,EAAE;oBACL;wBACE,IAAI,EAAE,aAAa;wBACnB,gBAAgB,EAAE,CAAC,CAAC,MAAM,IAAI,CAAC,YAAY,CAAC,CAAC,EAAE,CAAC;qBACjD;iBACF;gBACD,SAAS,EAAE;oBACT,MAAM,EAAE,KAAK;iBACd;aACF,CAAC,CAAC;YACH,OAAO,QAAQ,CAAC,WAAW,CAAC;QAC9B,CAAC;KAAA;CACF"}
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import WebSocket from "ws";
|
|
2
|
+
import { type Logger } from "./logging.ts";
|
|
3
|
+
import { type Command } from "./commands.ts";
|
|
4
|
+
import { type GuavaEvent, type CallerSpeechEvent, type AgentSpeechEvent } from "./events.ts";
|
|
5
|
+
import type { Field, Say } from "./action_item.ts";
|
|
6
|
+
export declare class CallController {
|
|
7
|
+
private _commandQueue;
|
|
8
|
+
private _on_complete_current_task?;
|
|
9
|
+
private _current_task_id?;
|
|
10
|
+
private _logger;
|
|
11
|
+
private _drain?;
|
|
12
|
+
constructor(logger: Logger);
|
|
13
|
+
setDrain(newDrain: (_: Command[]) => Promise<void>): void;
|
|
14
|
+
protected acceptCall(): Promise<void>;
|
|
15
|
+
protected readScript(script: string): Promise<void>;
|
|
16
|
+
protected rejectCall(): Promise<void>;
|
|
17
|
+
protected addInfo(info: string): Promise<void>;
|
|
18
|
+
protected sendInstruction(instruction: string): Promise<void>;
|
|
19
|
+
protected setPersona(organization_name?: string, agent_name?: string, agent_purpose?: string): Promise<void>;
|
|
20
|
+
protected setTask(objective?: string, checklist?: (Field | Say | string)[], on_complete?: (...c: any[]) => void, ...args: any[]): Promise<void>;
|
|
21
|
+
protected hangup(final_instructions?: string): Promise<void>;
|
|
22
|
+
protected transfer(to_number: string, transfer_message?: string): void;
|
|
23
|
+
private sendCommand;
|
|
24
|
+
private flush;
|
|
25
|
+
onEvent(event: GuavaEvent): Promise<void>;
|
|
26
|
+
/**
|
|
27
|
+
* @requires super call
|
|
28
|
+
*/
|
|
29
|
+
onIncomingCall(from_number?: string): Promise<void>;
|
|
30
|
+
onCallStart(): Promise<void>;
|
|
31
|
+
onCallerSpeech(event: CallerSpeechEvent): Promise<void>;
|
|
32
|
+
onAgentSpeech(event: AgentSpeechEvent): Promise<void>;
|
|
33
|
+
onIntent(intent: string): Promise<string | null>;
|
|
34
|
+
onQuestion(question: string): Promise<string>;
|
|
35
|
+
}
|
|
36
|
+
export type InboundConnection = {
|
|
37
|
+
agent_number: string;
|
|
38
|
+
} | {
|
|
39
|
+
webrtc_code: string;
|
|
40
|
+
};
|
|
41
|
+
export declare class Client {
|
|
42
|
+
private _apiKey;
|
|
43
|
+
private _baseUrl;
|
|
44
|
+
private _logger;
|
|
45
|
+
private _ws?;
|
|
46
|
+
private _controller?;
|
|
47
|
+
private messageHandler?;
|
|
48
|
+
constructor(apiKey?: string, baseUrl?: string, logger?: Logger);
|
|
49
|
+
private getHttpBase;
|
|
50
|
+
private getWebsocketBase;
|
|
51
|
+
private headers;
|
|
52
|
+
createOutbound(fromNumber: string | undefined, toNumber: string, callControllerFactory?: (logger: Logger) => CallController): void;
|
|
53
|
+
replaceHandler(newHandler?: (_: WebSocket.MessageEvent) => void): void;
|
|
54
|
+
private uninitializedOutbound;
|
|
55
|
+
private initializedOutbound;
|
|
56
|
+
setInboundHandler(agent_number: string, public_url: string, inbound_token: string): void;
|
|
57
|
+
listenInbound<U extends CallController>(conn: InboundConnection, controller_class: U): void;
|
|
58
|
+
}
|
|
@@ -0,0 +1,400 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
+
});
|
|
9
|
+
};
|
|
10
|
+
import WebSocket from "ws";
|
|
11
|
+
import { getConsoleLogger } from "./logging.js";
|
|
12
|
+
import { acceptInboundCallCommand, startOutboundCallCommand, setPersona, setTaskCommand, answerQuestionCommand, sendInstructionCommand, readScriptCommand, rejectInboundCallCommand, transferCommand, listenInboundCommand, } from "./commands.js";
|
|
13
|
+
import * as z from "zod";
|
|
14
|
+
import { errorEvent, sessionStartedEvent, decodeEvent, } from "./events.js";
|
|
15
|
+
import pkgdata from "../package.json" with { type: "json" };
|
|
16
|
+
import os from "node:os";
|
|
17
|
+
const DEFAULT_BASE_URL = "wss://guava-dev.gridspace.com/guava/";
|
|
18
|
+
const DEFAULT_LOG_LEVEL = "info";
|
|
19
|
+
/**
|
|
20
|
+
* @description convenience function for stringifying data according to a schema
|
|
21
|
+
*/
|
|
22
|
+
function stringifyZod(schema, data) {
|
|
23
|
+
return JSON.stringify(schema.parse(data));
|
|
24
|
+
}
|
|
25
|
+
export class CallController {
|
|
26
|
+
constructor(logger) {
|
|
27
|
+
this._commandQueue = [];
|
|
28
|
+
// Set up the default logger.
|
|
29
|
+
this._logger = logger;
|
|
30
|
+
}
|
|
31
|
+
setDrain(newDrain) {
|
|
32
|
+
this._drain = newDrain;
|
|
33
|
+
this.flush();
|
|
34
|
+
}
|
|
35
|
+
acceptCall() {
|
|
36
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
37
|
+
yield this.sendCommand(acceptInboundCallCommand, {
|
|
38
|
+
command_type: "accept-inbound",
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
readScript(script) {
|
|
43
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
+
yield this.sendCommand(readScriptCommand, {
|
|
45
|
+
command_type: "read-script",
|
|
46
|
+
script: script,
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
rejectCall() {
|
|
51
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
52
|
+
yield this.sendCommand(rejectInboundCallCommand, {
|
|
53
|
+
command_type: "reject-inbound",
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
addInfo(info) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
throw new Error("not implemeneted");
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
sendInstruction(instruction) {
|
|
63
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
+
yield this.sendCommand(sendInstructionCommand, {
|
|
65
|
+
command_type: "send-instruction",
|
|
66
|
+
instruction: instruction,
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
setPersona(organization_name, agent_name, agent_purpose) {
|
|
71
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
72
|
+
yield this.sendCommand(setPersona, {
|
|
73
|
+
command_type: "set-persona",
|
|
74
|
+
organization_name: organization_name,
|
|
75
|
+
agent_name: agent_name,
|
|
76
|
+
agent_purpose: agent_purpose,
|
|
77
|
+
});
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
setTask() {
|
|
81
|
+
return __awaiter(this, arguments, void 0, function* (objective = "", checklist, on_complete = () => { }, ...args) {
|
|
82
|
+
if (!objective && !checklist) {
|
|
83
|
+
throw new Error("At least one of args ['objective','checklist'] must be provided.");
|
|
84
|
+
}
|
|
85
|
+
this._current_task_id = Math.random().toString(16).substring(2, 8);
|
|
86
|
+
checklist = checklist !== null && checklist !== void 0 ? checklist : [];
|
|
87
|
+
const action_items = checklist.map((item) => typeof item == "string"
|
|
88
|
+
? {
|
|
89
|
+
item_type: "todo",
|
|
90
|
+
description: item,
|
|
91
|
+
}
|
|
92
|
+
: item);
|
|
93
|
+
this._on_complete_current_task = on_complete.apply(this, args);
|
|
94
|
+
this.sendCommand(setTaskCommand, {
|
|
95
|
+
command_type: "set-task",
|
|
96
|
+
task_id: this._current_task_id,
|
|
97
|
+
objective: objective,
|
|
98
|
+
action_items: action_items,
|
|
99
|
+
});
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
hangup() {
|
|
103
|
+
return __awaiter(this, arguments, void 0, function* (final_instructions = "") {
|
|
104
|
+
let instructions;
|
|
105
|
+
if (final_instructions) {
|
|
106
|
+
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.`;
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
instructions = "Naturally end the conversation and hang up the call.";
|
|
110
|
+
}
|
|
111
|
+
this.sendInstruction(instructions);
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
transfer(to_number, transfer_message) {
|
|
115
|
+
const message = transfer_message !== null && transfer_message !== void 0 ? transfer_message : "I'm transferring you now";
|
|
116
|
+
this.sendCommand(transferCommand, {
|
|
117
|
+
command_type: "transfer-call",
|
|
118
|
+
transfer_message: message,
|
|
119
|
+
to_number: to_number,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
sendCommand(schema, data) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
const command = schema.parse(data);
|
|
125
|
+
this._commandQueue.push(command);
|
|
126
|
+
this._logger.debug(`Command queued: ${JSON.stringify(command)}`);
|
|
127
|
+
yield this.flush();
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
flush() {
|
|
131
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
132
|
+
var _a;
|
|
133
|
+
yield ((_a = this._drain) === null || _a === void 0 ? void 0 : _a.call(this, this._commandQueue));
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
onEvent(event) {
|
|
137
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
138
|
+
if (event.event_type == "caller-speech") {
|
|
139
|
+
this.onCallerSpeech(event);
|
|
140
|
+
}
|
|
141
|
+
else if (event.event_type == "agent-speech") {
|
|
142
|
+
this.onAgentSpeech(event);
|
|
143
|
+
}
|
|
144
|
+
else if (event.event_type == "agent-question") {
|
|
145
|
+
try {
|
|
146
|
+
this._logger.info(`Received question from bot: ${event.question}`);
|
|
147
|
+
const answer = yield this.onQuestion(event.question);
|
|
148
|
+
yield this.sendCommand(answerQuestionCommand, {
|
|
149
|
+
command_type: "answer-question",
|
|
150
|
+
question_id: event.question_id,
|
|
151
|
+
answer: answer,
|
|
152
|
+
});
|
|
153
|
+
}
|
|
154
|
+
catch (e) {
|
|
155
|
+
this._logger.error("Error occured while answering question.");
|
|
156
|
+
yield this.sendCommand(answerQuestionCommand, {
|
|
157
|
+
command_type: "answer-question",
|
|
158
|
+
question_id: event.question_id,
|
|
159
|
+
answer: "An error occured and the question could not be answered.",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
else if (event.event_type == "intent") {
|
|
164
|
+
this._logger.info(`Received intent ${event.intent_id} from bot: ${event.intent_summary}`);
|
|
165
|
+
const intent_response = yield this.onIntent(event.intent_summary);
|
|
166
|
+
if (intent_response) {
|
|
167
|
+
const response_str = `Responding to intent ${event.intent_id}: ${event.intent_summary}`;
|
|
168
|
+
this._logger.info(response_str);
|
|
169
|
+
this.sendInstruction(response_str);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
else if (event.event_type == "task-done") {
|
|
173
|
+
// ignore obsolete task_completed events
|
|
174
|
+
if (event.task_id == this._current_task_id) {
|
|
175
|
+
// assertion is implied
|
|
176
|
+
const on_complete = this._on_complete_current_task;
|
|
177
|
+
this._on_complete_current_task = undefined;
|
|
178
|
+
if (on_complete) {
|
|
179
|
+
on_complete();
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
else if (event.event_type == "action-item-done") {
|
|
184
|
+
// self._field_values...
|
|
185
|
+
}
|
|
186
|
+
else if (event.event_type == "inbound-call") {
|
|
187
|
+
this.onIncomingCall(event.caller_number);
|
|
188
|
+
}
|
|
189
|
+
else if (event.event_type == "outbound-call-connected" ||
|
|
190
|
+
event.event_type == "bot-session-ended") {
|
|
191
|
+
// no-op, don't warn
|
|
192
|
+
}
|
|
193
|
+
else if (event.event_type == "error") {
|
|
194
|
+
this._logger.error(`The Guava agent reported an error: ${event.content}`);
|
|
195
|
+
}
|
|
196
|
+
else {
|
|
197
|
+
this._logger.warn(`Unhandled event: ${JSON.stringify(event)}`);
|
|
198
|
+
}
|
|
199
|
+
});
|
|
200
|
+
}
|
|
201
|
+
// callbacks
|
|
202
|
+
/**
|
|
203
|
+
* @requires super call
|
|
204
|
+
*/
|
|
205
|
+
onIncomingCall(from_number) {
|
|
206
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
207
|
+
yield this.onCallStart();
|
|
208
|
+
});
|
|
209
|
+
}
|
|
210
|
+
onCallStart() {
|
|
211
|
+
return __awaiter(this, void 0, void 0, function* () { });
|
|
212
|
+
}
|
|
213
|
+
onCallerSpeech(event) {
|
|
214
|
+
return __awaiter(this, void 0, void 0, function* () { });
|
|
215
|
+
}
|
|
216
|
+
onAgentSpeech(event) {
|
|
217
|
+
return __awaiter(this, void 0, void 0, function* () { });
|
|
218
|
+
}
|
|
219
|
+
onIntent(intent) {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
return "Unfortunately I'm not able to help with that.";
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
onQuestion(question) {
|
|
225
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
226
|
+
return "I don't have an answer to that question.";
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
const ws_start = /^ws:\/\//;
|
|
231
|
+
const wss_start = /^wss:\/\//;
|
|
232
|
+
export class Client {
|
|
233
|
+
constructor(apiKey, baseUrl, logger) {
|
|
234
|
+
// Set up the default logger.
|
|
235
|
+
if (logger) {
|
|
236
|
+
this._logger = logger;
|
|
237
|
+
}
|
|
238
|
+
else {
|
|
239
|
+
this._logger = getConsoleLogger(DEFAULT_LOG_LEVEL);
|
|
240
|
+
}
|
|
241
|
+
// Resolve the API base URL.
|
|
242
|
+
if (baseUrl) {
|
|
243
|
+
this._baseUrl = baseUrl;
|
|
244
|
+
}
|
|
245
|
+
else if (process.env.GUAVA_BASE_URL) {
|
|
246
|
+
this._baseUrl = process.env.GUAVA_BASE_URL;
|
|
247
|
+
}
|
|
248
|
+
else {
|
|
249
|
+
this._baseUrl = DEFAULT_BASE_URL;
|
|
250
|
+
}
|
|
251
|
+
// Resolve the API key.
|
|
252
|
+
if (apiKey) {
|
|
253
|
+
this._apiKey = apiKey;
|
|
254
|
+
}
|
|
255
|
+
else if (process.env.GUAVA_API_KEY) {
|
|
256
|
+
this._apiKey = process.env.GUAVA_API_KEY;
|
|
257
|
+
}
|
|
258
|
+
else {
|
|
259
|
+
throw new Error("Guava API key must be provided either as argument to client constructor, or in environment variable GUAVA_API_KEY.");
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
getHttpBase() {
|
|
263
|
+
if (ws_start.test(this._baseUrl)) {
|
|
264
|
+
return `ws://${this._baseUrl.substring("ws://".length)}`;
|
|
265
|
+
}
|
|
266
|
+
else if (wss_start.test(this._baseUrl)) {
|
|
267
|
+
return `wss://${this._baseUrl.substring("wss://".length)}`;
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
throw new Error(`Invalid base URL: ${this._baseUrl}}`);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
getWebsocketBase() {
|
|
274
|
+
return this._baseUrl;
|
|
275
|
+
}
|
|
276
|
+
headers() {
|
|
277
|
+
return {
|
|
278
|
+
Authorization: `Bearer ${this._apiKey}`,
|
|
279
|
+
"x-guava-platform": os.platform(),
|
|
280
|
+
"x-guava-runtime": process.release.name,
|
|
281
|
+
"x-guava-runtime-version": process.version,
|
|
282
|
+
"x-guava-sdk": "typescript-sdk",
|
|
283
|
+
"x-guava-sdk-version": pkgdata.version,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
createOutbound(fromNumber, toNumber, callControllerFactory) {
|
|
287
|
+
const url = new URL("create-outbound", this.getWebsocketBase()).toString();
|
|
288
|
+
const ws = new WebSocket(url, {
|
|
289
|
+
headers: this.headers(),
|
|
290
|
+
});
|
|
291
|
+
const callController = (callControllerFactory !== null && callControllerFactory !== void 0 ? callControllerFactory : ((_) => undefined))(this._logger);
|
|
292
|
+
ws.addEventListener("open", (_ev) => __awaiter(this, void 0, void 0, function* () {
|
|
293
|
+
ws.send(stringifyZod(startOutboundCallCommand, {
|
|
294
|
+
command_type: "start-outbound",
|
|
295
|
+
to_number: toNumber,
|
|
296
|
+
from_number: fromNumber,
|
|
297
|
+
}));
|
|
298
|
+
yield (callController === null || callController === void 0 ? void 0 : callController.onCallStart());
|
|
299
|
+
}));
|
|
300
|
+
ws.addEventListener("close", (_ev) => {
|
|
301
|
+
// we are closing the socket, so don't trigger any other listeners
|
|
302
|
+
ws.removeAllListeners();
|
|
303
|
+
this._ws = undefined;
|
|
304
|
+
this._controller = undefined;
|
|
305
|
+
});
|
|
306
|
+
this._ws = ws;
|
|
307
|
+
this._controller = callController;
|
|
308
|
+
this.replaceHandler(this.uninitializedOutbound.bind(this));
|
|
309
|
+
// set the callController drain function to send all commands
|
|
310
|
+
// through the websocket
|
|
311
|
+
callController === null || callController === void 0 ? void 0 : callController.setDrain((commands) => __awaiter(this, void 0, void 0, function* () {
|
|
312
|
+
for (const command of commands.splice(0)) {
|
|
313
|
+
this._logger.debug(`Sending command ${JSON.stringify(command)}`);
|
|
314
|
+
ws.send(JSON.stringify(command));
|
|
315
|
+
}
|
|
316
|
+
}));
|
|
317
|
+
}
|
|
318
|
+
replaceHandler(newHandler) {
|
|
319
|
+
var _a, _b;
|
|
320
|
+
if (this.messageHandler) {
|
|
321
|
+
(_a = this._ws) === null || _a === void 0 ? void 0 : _a.removeEventListener("message", this.messageHandler);
|
|
322
|
+
}
|
|
323
|
+
if (newHandler) {
|
|
324
|
+
(_b = this._ws) === null || _b === void 0 ? void 0 : _b.addEventListener("message", newHandler);
|
|
325
|
+
}
|
|
326
|
+
this.messageHandler = newHandler;
|
|
327
|
+
}
|
|
328
|
+
// eventlistener handlers for server events
|
|
329
|
+
// (a state machine in functions)
|
|
330
|
+
uninitializedOutbound(ev) {
|
|
331
|
+
// for correctness (and type correctness)
|
|
332
|
+
if (!this._ws) {
|
|
333
|
+
throw new Error("[internal] Uninitialized WebSocket");
|
|
334
|
+
}
|
|
335
|
+
const session_started = z
|
|
336
|
+
.union([sessionStartedEvent, errorEvent])
|
|
337
|
+
.parse(JSON.parse(ev.data.toString("utf8")));
|
|
338
|
+
if (session_started.event_type == "error") {
|
|
339
|
+
throw new Error(`Outbound call failed: ${session_started.content}`);
|
|
340
|
+
}
|
|
341
|
+
else {
|
|
342
|
+
this._logger.info(`Started session with ID: ${session_started.session_id}`);
|
|
343
|
+
// move to next state
|
|
344
|
+
this.replaceHandler(this.initializedOutbound.bind(this));
|
|
345
|
+
}
|
|
346
|
+
// this._controller?.flush();
|
|
347
|
+
}
|
|
348
|
+
initializedOutbound(ev) {
|
|
349
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
350
|
+
// for correctness (and type correctness)
|
|
351
|
+
if (!this._ws) {
|
|
352
|
+
throw new Error("[internal] Uninitialized WebSocket");
|
|
353
|
+
}
|
|
354
|
+
// handle the received event
|
|
355
|
+
const event = decodeEvent(ev.data);
|
|
356
|
+
if (event) {
|
|
357
|
+
if (this._controller) {
|
|
358
|
+
yield this._controller.onEvent(event);
|
|
359
|
+
}
|
|
360
|
+
if (event.event_type == "outbound-call-failed" ||
|
|
361
|
+
event.event_type == "bot-session-ended") {
|
|
362
|
+
// shutdown the websocket
|
|
363
|
+
this._ws.close();
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
// this._controller?.flush();
|
|
367
|
+
});
|
|
368
|
+
}
|
|
369
|
+
setInboundHandler(agent_number, public_url, inbound_token) {
|
|
370
|
+
// TODO
|
|
371
|
+
}
|
|
372
|
+
// why use a typevar here and not for outbound?
|
|
373
|
+
listenInbound(conn, controller_class) {
|
|
374
|
+
const call_controllers = {};
|
|
375
|
+
const url = new URL("listen-inbound", this.getWebsocketBase()).toString();
|
|
376
|
+
const ws = new WebSocket(url, {
|
|
377
|
+
headers: this.headers(),
|
|
378
|
+
});
|
|
379
|
+
let agent_number;
|
|
380
|
+
let webrtc_code;
|
|
381
|
+
if ("agent_number" in conn) {
|
|
382
|
+
agent_number = conn.agent_number;
|
|
383
|
+
}
|
|
384
|
+
else {
|
|
385
|
+
webrtc_code = conn.webrtc_code;
|
|
386
|
+
}
|
|
387
|
+
ws.send(stringifyZod(listenInboundCommand, {
|
|
388
|
+
command_type: "listen-inbound",
|
|
389
|
+
agent_number: agent_number,
|
|
390
|
+
webrtc_code: webrtc_code,
|
|
391
|
+
}));
|
|
392
|
+
this._logger.info(`Listening for calls to ${agent_number !== null && agent_number !== void 0 ? agent_number : webrtc_code}`);
|
|
393
|
+
if (webrtc_code) {
|
|
394
|
+
// converted to print, but should be logger?
|
|
395
|
+
const debugurl = new URL(`debug-webrtc?webrtc_code=${webrtc_code}`, this.getHttpBase());
|
|
396
|
+
this._logger.debug(`WebRTC DebugURL: ${new URL("debug-webrtc?we")}`);
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,SAAS,MAAM,IAAI,CAA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AAC,YAAY,CAAC;QACnC,CAAC;aAAM,CAAC;YACN,WAAW,GAAG,IAAI,CAAC,WAAW,CAAC;QACjC,CAAC;QAED,EAAE,CAAC,IAAI,CACL,YAAY,CAAC,oBAAoB,EAAE;YACjC,YAAY,EAAE,gBAAgB;YAC9B,YAAY,EAAE,YAAY;YAC1B,WAAW,EAAE,WAAW;SACzB,CAAC,CACH,CAAC;QAEF,IAAI,CAAC,OAAO,CAAC,IAAI,CAAC,0BAA0B,YAAY,aAAZ,YAAY,cAAZ,YAAY,GAAI,WAAW,EAAE,CAAC,CAAC;QAE3E,IAAI,WAAW,EAAE,CAAC;YAChB,4CAA4C;YAC5C,MAAM,QAAQ,GAAG,IAAI,GAAG,CACtB,4BAA4B,WAAW,EAAE,EACzC,IAAI,CAAC,WAAW,EAAE,CACnB,CAAC;YACF,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,oBAAoB,IAAI,GAAG,CAAC,iBAAiB,CAAC,EAAE,CAAC,CAAC;QACvE,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
type LogLevel = "off" | "error" | "warn" | "info" | "debug";
|
|
2
|
+
export interface Logger {
|
|
3
|
+
debug(format: string, ...args: unknown[]): any;
|
|
4
|
+
info(format: string, ...args: unknown[]): any;
|
|
5
|
+
warn(format: string, ...args: unknown[]): any;
|
|
6
|
+
error(format: string, ...args: unknown[]): any;
|
|
7
|
+
}
|
|
8
|
+
export declare function getConsoleLogger(loggerLevel: LogLevel): Logger;
|
|
9
|
+
export {};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
const LOG_RANKS = {
|
|
2
|
+
off: 0,
|
|
3
|
+
error: 1,
|
|
4
|
+
warn: 2,
|
|
5
|
+
info: 3,
|
|
6
|
+
debug: 4,
|
|
7
|
+
};
|
|
8
|
+
function shouldLog(messageLevel, loggerLevel) {
|
|
9
|
+
if (!LOG_RANKS.hasOwnProperty(messageLevel)) {
|
|
10
|
+
throw new Error(`Invalid log level: ${String(messageLevel)}`);
|
|
11
|
+
}
|
|
12
|
+
if (!LOG_RANKS.hasOwnProperty(loggerLevel)) {
|
|
13
|
+
throw new Error(`Invalid logger level: ${String(loggerLevel)}`);
|
|
14
|
+
}
|
|
15
|
+
return LOG_RANKS[messageLevel] <= LOG_RANKS[loggerLevel];
|
|
16
|
+
}
|
|
17
|
+
function noop(format, ...args) { }
|
|
18
|
+
export function getConsoleLogger(loggerLevel) {
|
|
19
|
+
return {
|
|
20
|
+
debug: shouldLog("debug", loggerLevel) ? console.debug.bind(console) : noop,
|
|
21
|
+
info: shouldLog("info", loggerLevel) ? console.info.bind(console) : noop,
|
|
22
|
+
warn: shouldLog("warn", loggerLevel) ? console.warn.bind(console) : noop,
|
|
23
|
+
error: shouldLog("error", loggerLevel) ? console.error.bind(console) : noop,
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
//# sourceMappingURL=logging.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"logging.js","sourceRoot":"","sources":["../../src/logging.ts"],"names":[],"mappings":"AAEA,MAAM,SAAS,GAA6B;IAC1C,GAAG,EAAE,CAAC;IACN,KAAK,EAAE,CAAC;IACR,IAAI,EAAE,CAAC;IACP,IAAI,EAAE,CAAC;IACP,KAAK,EAAE,CAAC;CACT,CAAC;AASF,SAAS,SAAS,CAAC,YAAsB,EAAE,WAAqB;IAC9D,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,YAAY,CAAC,EAAE,CAAC;QAC5C,MAAM,IAAI,KAAK,CAAC,sBAAsB,MAAM,CAAC,YAAY,CAAC,EAAE,CAAC,CAAC;IAChE,CAAC;IACD,IAAI,CAAC,SAAS,CAAC,cAAc,CAAC,WAAW,CAAC,EAAE,CAAC;QAC3C,MAAM,IAAI,KAAK,CAAC,yBAAyB,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,CAAC;IAClE,CAAC;IAED,OAAO,SAAS,CAAC,YAAY,CAAC,IAAI,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