@sermas/sermas-toolkit-node-sdk 1.0.0-alpha.9 → 1.0.0-alpha.91
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.md +16 -0
- package/index.cjs +2 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +150 -0
- package/index.d.ts +150 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +15 -16
- package/dist/cjs/config/defaults.js +0 -18
- package/dist/cjs/config/logger.js +0 -10
- package/dist/cjs/config/util.js +0 -70
- package/dist/cjs/dto/session.js +0 -2
- package/dist/cjs/index.js +0 -16
- package/dist/cjs/modules/auth.js +0 -238
- package/dist/cjs/modules/openai.js +0 -63
- package/dist/cjs/modules/sermas.js +0 -233
- package/dist/cjs/modules/store.js +0 -177
- package/dist/cjs/modules/tools.js +0 -44
- package/dist/cjs/modules/ui.js +0 -124
- package/dist/esm/config/defaults.js +0 -15
- package/dist/esm/config/logger.js +0 -6
- package/dist/esm/config/util.js +0 -39
- package/dist/esm/dto/session.js +0 -1
- package/dist/esm/index.js +0 -6
- package/dist/esm/modules/auth.js +0 -210
- package/dist/esm/modules/openai.js +0 -57
- package/dist/esm/modules/sermas.js +0 -230
- package/dist/esm/modules/store.js +0 -174
- package/dist/esm/modules/tools.js +0 -41
- package/dist/esm/modules/ui.js +0 -121
- package/dist/types/config/defaults.d.ts +0 -15
- package/dist/types/config/logger.d.ts +0 -1
- package/dist/types/config/util.d.ts +0 -6
- package/dist/types/dto/session.d.ts +0 -7
- package/dist/types/index.d.ts +0 -8
- package/dist/types/modules/auth.d.ts +0 -30
- package/dist/types/modules/openai.d.ts +0 -18
- package/dist/types/modules/sermas.d.ts +0 -36
- package/dist/types/modules/store.d.ts +0 -21
- package/dist/types/modules/tools.d.ts +0 -15
- package/dist/types/modules/ui.d.ts +0 -24
|
@@ -1,233 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.SermasApp = void 0;
|
|
13
|
-
const logger_js_1 = require("../config/logger.js");
|
|
14
|
-
const eventemitter2_1 = require("eventemitter2");
|
|
15
|
-
const api_client_1 = require("@sermas/api-client");
|
|
16
|
-
const defaults_js_1 = require("../config/defaults.js");
|
|
17
|
-
class SermasApp {
|
|
18
|
-
constructor(sermasConfig) {
|
|
19
|
-
this.logger = (0, logger_js_1.createLogger)(SermasApp.name);
|
|
20
|
-
this.app = null;
|
|
21
|
-
this.subs = [];
|
|
22
|
-
this.baseUrl =
|
|
23
|
-
sermasConfig.SERMAS_BASE_URL || defaults_js_1.defaults.sermas.SERMAS_BASE_URL;
|
|
24
|
-
this.appId = sermasConfig.SERMAS_APPID || defaults_js_1.defaults.sermas.SERMAS_CLIENT_ID;
|
|
25
|
-
this.clientId =
|
|
26
|
-
sermasConfig.SERMAS_CLIENT_ID || defaults_js_1.defaults.sermas.SERMAS_CLIENT_SECRET;
|
|
27
|
-
this.clientSecret =
|
|
28
|
-
sermasConfig.SERMAS_CLIENT_SECRET || defaults_js_1.defaults.sermas.SERMAS_APPID;
|
|
29
|
-
console.log("object");
|
|
30
|
-
this.emitter = new eventemitter2_1.EventEmitter2();
|
|
31
|
-
this.initialize();
|
|
32
|
-
this.client = new api_client_1.SermasApiClient({
|
|
33
|
-
baseURL: this.getBaseUrl(),
|
|
34
|
-
appId: this.appId,
|
|
35
|
-
logger: (0, logger_js_1.createLogger)("sermas-api-client"),
|
|
36
|
-
});
|
|
37
|
-
this.getApp();
|
|
38
|
-
}
|
|
39
|
-
getBaseUrl() {
|
|
40
|
-
return this.baseUrl;
|
|
41
|
-
}
|
|
42
|
-
initialize() {
|
|
43
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
44
|
-
// return await this.getClientCredentials();
|
|
45
|
-
yield this.setupSermasClient();
|
|
46
|
-
this.addSub(() => __awaiter(this, void 0, void 0, function* () {
|
|
47
|
-
return yield this.client.events.auth.onUserLogin((ev) => {
|
|
48
|
-
this.logger.debug(`User login: ${ev}`);
|
|
49
|
-
});
|
|
50
|
-
}));
|
|
51
|
-
this.addSub(() => __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
return yield this.client.events.session.onSessionChanged((ev) => {
|
|
53
|
-
this.logger.debug(`session changed ${ev.operation.toUpperCase()} sessionId=${ev.record.sessionId}`);
|
|
54
|
-
this.emitter.emit(`session`, ev);
|
|
55
|
-
});
|
|
56
|
-
}));
|
|
57
|
-
this.addSub(() => __awaiter(this, void 0, void 0, function* () {
|
|
58
|
-
return yield this.client.events.dialogue.onToolTriggered((event) => {
|
|
59
|
-
this.logger.debug("Tool triggered: " + event.name);
|
|
60
|
-
this.emitter.emit(`tool`, event);
|
|
61
|
-
});
|
|
62
|
-
}));
|
|
63
|
-
this.addSub(() => __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
return yield this.client.events.ui.onInteraction((event) => {
|
|
65
|
-
// this.logger.debug('UI interaction: ' + event.interaction);
|
|
66
|
-
this.emitter.emit(`ui.interaction`, event);
|
|
67
|
-
});
|
|
68
|
-
}));
|
|
69
|
-
this.addSub(() => __awaiter(this, void 0, void 0, function* () {
|
|
70
|
-
return yield this.client.events.session.onAgentChanged((event) => {
|
|
71
|
-
this.logger.debug("Agent changed: " + event.operation);
|
|
72
|
-
this.emitter.emit(`agent.changed`, event);
|
|
73
|
-
});
|
|
74
|
-
}));
|
|
75
|
-
this.logger.info("Sermas initialization completed");
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
setupSermasClient() {
|
|
79
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
80
|
-
this.logger.debug("Initializing sermas client.");
|
|
81
|
-
try {
|
|
82
|
-
this.logger.debug(`Loading token`);
|
|
83
|
-
yield this.client.loadToken(this.clientId, this.clientSecret);
|
|
84
|
-
}
|
|
85
|
-
catch (e) {
|
|
86
|
-
this.logger.error(`Failed to load token, retrying`);
|
|
87
|
-
if (e instanceof Error)
|
|
88
|
-
this.logger.debug(e.stack);
|
|
89
|
-
setTimeout(() => this.setupSermasClient(), 1000);
|
|
90
|
-
return;
|
|
91
|
-
}
|
|
92
|
-
this.logger.debug(`sermas client ready`);
|
|
93
|
-
this.emitter.emit("sermas.ready");
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
addSub(sub) {
|
|
97
|
-
sub()
|
|
98
|
-
.then((fn) => {
|
|
99
|
-
this.subs.push(fn);
|
|
100
|
-
})
|
|
101
|
-
.catch((e) => {
|
|
102
|
-
this.logger.error(`Subscribe failed: ${e.stack}`);
|
|
103
|
-
});
|
|
104
|
-
}
|
|
105
|
-
sendChatMessage(ev) {
|
|
106
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
107
|
-
yield this.client.api.dialogue.chatMessage({
|
|
108
|
-
appId: ev.appId,
|
|
109
|
-
sessionId: ev.sessionId,
|
|
110
|
-
requestBody: ev,
|
|
111
|
-
});
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
sendUiContent(ev) {
|
|
115
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
116
|
-
yield this.client.events.ui.content(ev);
|
|
117
|
-
});
|
|
118
|
-
}
|
|
119
|
-
getApp() {
|
|
120
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
121
|
-
if (this.app)
|
|
122
|
-
return this.app;
|
|
123
|
-
this.logger.debug(`Load app ${this.appId}`);
|
|
124
|
-
try {
|
|
125
|
-
this.app = yield this.client.api.platform.readApp({ appId: this.appId });
|
|
126
|
-
return this.app;
|
|
127
|
-
}
|
|
128
|
-
catch (e) {
|
|
129
|
-
if (e instanceof Error)
|
|
130
|
-
this.logger.error(`Failed to load app ${this.appId}: ${e.stack}`);
|
|
131
|
-
return null;
|
|
132
|
-
}
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
updateAppTools(tools) {
|
|
136
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
137
|
-
yield this.client.api.platform.updateAppTools({
|
|
138
|
-
appId: this.appId,
|
|
139
|
-
requestBody: tools,
|
|
140
|
-
});
|
|
141
|
-
this.logger.debug("Updated tools");
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
setTools(repositoryId, tools) {
|
|
145
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
146
|
-
yield this.client.api.dialogue.setTools({
|
|
147
|
-
repositoryId,
|
|
148
|
-
requestBody: {
|
|
149
|
-
repositoryId,
|
|
150
|
-
appId: this.appId,
|
|
151
|
-
tools,
|
|
152
|
-
},
|
|
153
|
-
});
|
|
154
|
-
this.logger.debug("Updated tools");
|
|
155
|
-
});
|
|
156
|
-
}
|
|
157
|
-
addTools(repositoryId, tools, options) {
|
|
158
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
159
|
-
yield this.client.api.dialogue.addTools({
|
|
160
|
-
repositoryId,
|
|
161
|
-
requestBody: { repositoryId, appId: this.appId, tools, options },
|
|
162
|
-
});
|
|
163
|
-
this.logger.debug("Updated tools");
|
|
164
|
-
});
|
|
165
|
-
}
|
|
166
|
-
readSession(sessionId) {
|
|
167
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
168
|
-
try {
|
|
169
|
-
return yield this.client.api.session.readSession({ sessionId });
|
|
170
|
-
}
|
|
171
|
-
catch (e) {
|
|
172
|
-
if (e instanceof Error)
|
|
173
|
-
this.logger.error(`Failed to readSession: ${e.stack}`);
|
|
174
|
-
return null;
|
|
175
|
-
}
|
|
176
|
-
});
|
|
177
|
-
}
|
|
178
|
-
getRecord(storageId) {
|
|
179
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
180
|
-
try {
|
|
181
|
-
return yield this.client.api.session.getRecord({ storageId });
|
|
182
|
-
}
|
|
183
|
-
catch (e) {
|
|
184
|
-
if (e instanceof Error && e.message.indexOf("Not Found") === -1)
|
|
185
|
-
this.logger.error(`Failed to getRecord: ${e.stack}`);
|
|
186
|
-
return null;
|
|
187
|
-
}
|
|
188
|
-
});
|
|
189
|
-
}
|
|
190
|
-
findRecords(query) {
|
|
191
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
192
|
-
try {
|
|
193
|
-
return yield this.client.api.session.findRecords({
|
|
194
|
-
requestBody: query,
|
|
195
|
-
});
|
|
196
|
-
}
|
|
197
|
-
catch (e) {
|
|
198
|
-
if (e instanceof Error)
|
|
199
|
-
this.logger.error(`Failed to findRecords: ${e.stack}`);
|
|
200
|
-
return null;
|
|
201
|
-
}
|
|
202
|
-
});
|
|
203
|
-
}
|
|
204
|
-
setRecord(storage) {
|
|
205
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
206
|
-
try {
|
|
207
|
-
return yield this.client.api.session.setRecord({
|
|
208
|
-
requestBody: storage,
|
|
209
|
-
});
|
|
210
|
-
}
|
|
211
|
-
catch (e) {
|
|
212
|
-
if (e instanceof Error)
|
|
213
|
-
this.logger.error(`Failed to setRecord: ${e.stack}`);
|
|
214
|
-
return null;
|
|
215
|
-
}
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
generateQRCode(data) {
|
|
219
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
220
|
-
try {
|
|
221
|
-
return yield this.client.api.ui.generateQrCode({
|
|
222
|
-
requestBody: { version: 5, data },
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
catch (e) {
|
|
226
|
-
if (e instanceof Error)
|
|
227
|
-
this.logger.error(`Failed to generate QR code: ${e.stack}`);
|
|
228
|
-
return null;
|
|
229
|
-
}
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
exports.SermasApp = SermasApp;
|
|
@@ -1,177 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Store = void 0;
|
|
13
|
-
const logger_js_1 = require("../config/logger.js");
|
|
14
|
-
const util_js_1 = require("../config/util.js");
|
|
15
|
-
class Store {
|
|
16
|
-
constructor(sermas) {
|
|
17
|
-
this.logger = (0, logger_js_1.createLogger)(Store.name);
|
|
18
|
-
this.sessions = {};
|
|
19
|
-
this.locks = {};
|
|
20
|
-
this.sermas = sermas;
|
|
21
|
-
this.sermas.emitter.on("session", this.onSessionChange.bind(this));
|
|
22
|
-
}
|
|
23
|
-
// @OnEvent("session")
|
|
24
|
-
onSessionChange(ev) {
|
|
25
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
26
|
-
if (!ev.record.sessionId)
|
|
27
|
-
return;
|
|
28
|
-
const session = yield this.saveSessionWrapper(ev);
|
|
29
|
-
switch (ev.operation) {
|
|
30
|
-
case "created":
|
|
31
|
-
if (!session) {
|
|
32
|
-
this.logger.warn(`Failed to get session for sessionId=${ev.record.sessionId}`);
|
|
33
|
-
return;
|
|
34
|
-
}
|
|
35
|
-
this.sermas.emitter.emit("session.started", session);
|
|
36
|
-
break;
|
|
37
|
-
case "updated":
|
|
38
|
-
yield this.saveSessionWrapper(ev);
|
|
39
|
-
break;
|
|
40
|
-
case "removed":
|
|
41
|
-
if (!this.sessions[ev.record.sessionId])
|
|
42
|
-
return;
|
|
43
|
-
delete this.sessions[ev.record.sessionId];
|
|
44
|
-
break;
|
|
45
|
-
}
|
|
46
|
-
});
|
|
47
|
-
}
|
|
48
|
-
lock(sessionId, fn) {
|
|
49
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
50
|
-
// eslint-disable-next-line no-async-promise-executor
|
|
51
|
-
this.locks[sessionId] = new Promise((resolve, reject) => __awaiter(this, void 0, void 0, function* () {
|
|
52
|
-
try {
|
|
53
|
-
resolve(yield fn());
|
|
54
|
-
}
|
|
55
|
-
catch (e) {
|
|
56
|
-
reject(e);
|
|
57
|
-
}
|
|
58
|
-
}));
|
|
59
|
-
try {
|
|
60
|
-
const res = yield this.locks[sessionId];
|
|
61
|
-
return res;
|
|
62
|
-
}
|
|
63
|
-
catch (e) {
|
|
64
|
-
if (e instanceof Error)
|
|
65
|
-
this.logger.error(`lock failure: ${e.stack}`);
|
|
66
|
-
return null;
|
|
67
|
-
}
|
|
68
|
-
finally {
|
|
69
|
-
delete this.locks[sessionId];
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
}
|
|
73
|
-
loadSessionWrapper(context) {
|
|
74
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
75
|
-
const opId = `[${performance.now()}] `;
|
|
76
|
-
if (yield this.locks[context.sessionId]) {
|
|
77
|
-
this.logger.debug(`${opId} Wait lock for sessionId=${context.sessionId}`);
|
|
78
|
-
yield this.locks[context.sessionId];
|
|
79
|
-
this.logger.debug(`${opId} Lock released for sessionId=${context.sessionId}`);
|
|
80
|
-
}
|
|
81
|
-
return yield this.lock(context.sessionId, () => __awaiter(this, void 0, void 0, function* () {
|
|
82
|
-
this.logger.debug(`${opId} Loading wrapper for sessionId=${context.sessionId}`);
|
|
83
|
-
const records = yield this.sermas.findRecords({
|
|
84
|
-
appId: "",
|
|
85
|
-
sessionId: [context.sessionId],
|
|
86
|
-
userId: undefined,
|
|
87
|
-
});
|
|
88
|
-
if (records && records.length) {
|
|
89
|
-
this.logger.debug(`${opId} Wrapper exists for sessionId=${context.sessionId}`);
|
|
90
|
-
return records[0].data;
|
|
91
|
-
}
|
|
92
|
-
else {
|
|
93
|
-
this.logger.debug(`${opId} Wrapper not found for sessionId=${context.sessionId}`);
|
|
94
|
-
}
|
|
95
|
-
this.logger.debug(`${opId} Creating new session wrapper store sessionId=${context.sessionId} appId=${context.appId} userId=${context.userId}`);
|
|
96
|
-
const storageId = (0, util_js_1.uuidv4)();
|
|
97
|
-
const sessionWrapper = Object.assign(Object.assign({}, context), { appId: context.appId, sessionId: context.sessionId, userId: context.userId, storageId });
|
|
98
|
-
if (!sessionWrapper.appId || !sessionWrapper.userId) {
|
|
99
|
-
this.logger.debug(`${opId} Load session record sessionId=${sessionWrapper.sessionId}`);
|
|
100
|
-
const session = yield this.sermas.readSession(sessionWrapper.sessionId);
|
|
101
|
-
if (session) {
|
|
102
|
-
if (!sessionWrapper.appId)
|
|
103
|
-
sessionWrapper.appId = session.appId;
|
|
104
|
-
if (!sessionWrapper.userId)
|
|
105
|
-
sessionWrapper.userId = session.userId;
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
this.logger.warn(`${opId} Failed to load sessionId=${sessionWrapper.sessionId}`);
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
const record = {
|
|
112
|
-
appId: sessionWrapper.appId,
|
|
113
|
-
sessionId: sessionWrapper.sessionId,
|
|
114
|
-
data: sessionWrapper,
|
|
115
|
-
userId: sessionWrapper.userId || undefined,
|
|
116
|
-
storageId,
|
|
117
|
-
};
|
|
118
|
-
yield this.sermas.setRecord(record);
|
|
119
|
-
this.logger.debug(`${opId} Saved store storageId=${record.storageId} for sessionId=${record.sessionId} appId=${record.appId} userId=${record.userId}`);
|
|
120
|
-
return sessionWrapper;
|
|
121
|
-
}));
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
saveSession(session) {
|
|
125
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
let record = yield this.sermas.getRecord(session.storageId);
|
|
127
|
-
if (!record) {
|
|
128
|
-
this.logger.warn(`sessionId=${session.sessionId} storageId=${session.storageId} not found, creating new record`);
|
|
129
|
-
const newSession = yield this.loadSessionWrapper(Object.assign({}, session));
|
|
130
|
-
record = yield this.sermas.getRecord(newSession.storageId);
|
|
131
|
-
}
|
|
132
|
-
if (!record) {
|
|
133
|
-
this.logger.error(`Cannot save sessionId=${session.sessionId}, storage not found`);
|
|
134
|
-
return null;
|
|
135
|
-
}
|
|
136
|
-
record.data = Object.assign(Object.assign({}, session), { storageId: record.storageId });
|
|
137
|
-
const res = yield this.sermas.setRecord(record);
|
|
138
|
-
this.logger.debug(`Saved record for sessionId=${record.sessionId} storageId=${record.storageId}`);
|
|
139
|
-
return res === null || res === void 0 ? void 0 : res.data;
|
|
140
|
-
});
|
|
141
|
-
}
|
|
142
|
-
getSessionWrapper(sessionId) {
|
|
143
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
144
|
-
if (!sessionId) {
|
|
145
|
-
this.logger.debug(`sessionId is empty`);
|
|
146
|
-
return;
|
|
147
|
-
}
|
|
148
|
-
if (!this.sessions[sessionId]) {
|
|
149
|
-
const session = yield this.loadSessionWrapper({
|
|
150
|
-
sessionId,
|
|
151
|
-
});
|
|
152
|
-
this.logger.debug(`loaded sessionId=${sessionId}`);
|
|
153
|
-
this.sessions[sessionId] = session;
|
|
154
|
-
}
|
|
155
|
-
return this.sessions[sessionId];
|
|
156
|
-
});
|
|
157
|
-
}
|
|
158
|
-
saveSessionWrapper(ev) {
|
|
159
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
160
|
-
if (!ev.appId)
|
|
161
|
-
return;
|
|
162
|
-
if (!ev.record.sessionId)
|
|
163
|
-
return;
|
|
164
|
-
if (this.sessions[ev.record.sessionId])
|
|
165
|
-
return;
|
|
166
|
-
const wrapper = yield this.loadSessionWrapper({
|
|
167
|
-
userId: ev.userId,
|
|
168
|
-
appId: ev.appId,
|
|
169
|
-
sessionId: ev.record.sessionId,
|
|
170
|
-
});
|
|
171
|
-
this.sessions[wrapper.sessionId] = wrapper;
|
|
172
|
-
this.logger.info(`Added session ${wrapper.sessionId}`);
|
|
173
|
-
return this.sessions[wrapper.sessionId];
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
exports.Store = Store;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.Tools = void 0;
|
|
13
|
-
class Tools {
|
|
14
|
-
constructor(sermasApp) {
|
|
15
|
-
this.sermas = sermasApp;
|
|
16
|
-
}
|
|
17
|
-
reset(session) {
|
|
18
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
19
|
-
yield this.sermas.setTools(session.sessionId, []);
|
|
20
|
-
});
|
|
21
|
-
}
|
|
22
|
-
set(session, tools) {
|
|
23
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
yield this.sermas.setTools(session.sessionId, tools);
|
|
25
|
-
});
|
|
26
|
-
}
|
|
27
|
-
add(session, tools, options) {
|
|
28
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
-
yield this.sermas.addTools(session.sessionId, tools, options);
|
|
30
|
-
});
|
|
31
|
-
}
|
|
32
|
-
getSchemaValues(tool) {
|
|
33
|
-
const values = {};
|
|
34
|
-
if (!tool.schema)
|
|
35
|
-
return values;
|
|
36
|
-
tool.schema.forEach((schema) => (values[schema.parameter] = schema.value));
|
|
37
|
-
return values;
|
|
38
|
-
}
|
|
39
|
-
getSchemaValue(tool, parameterName) {
|
|
40
|
-
const values = this.getSchemaValues(tool);
|
|
41
|
-
return values[parameterName];
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
exports.Tools = Tools;
|
package/dist/cjs/modules/ui.js
DELETED
|
@@ -1,124 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
-
exports.UI = void 0;
|
|
13
|
-
const core_1 = require("openai/core");
|
|
14
|
-
const ulidx_1 = require("ulidx");
|
|
15
|
-
const logger_js_1 = require("../config/logger.js");
|
|
16
|
-
class UI {
|
|
17
|
-
constructor(sermas, store) {
|
|
18
|
-
this.logger = (0, logger_js_1.createLogger)(UI.name);
|
|
19
|
-
this.backMenuOptions = [];
|
|
20
|
-
this.sermas = sermas;
|
|
21
|
-
this.store = store;
|
|
22
|
-
this.sermas.emitter.on("session", this.onSessionChange.bind(this));
|
|
23
|
-
}
|
|
24
|
-
// @OnEvent("session")
|
|
25
|
-
onSessionChange(ev) {
|
|
26
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
-
if (!ev.record.sessionId)
|
|
28
|
-
return;
|
|
29
|
-
const session = yield this.store.getSessionWrapper(ev.record.sessionId);
|
|
30
|
-
switch (ev.operation) {
|
|
31
|
-
case "removed":
|
|
32
|
-
if (!session)
|
|
33
|
-
return;
|
|
34
|
-
yield this.sendClearScreen(session);
|
|
35
|
-
break;
|
|
36
|
-
}
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
createBackButton(value) {
|
|
40
|
-
this.backMenuOptions = Array.from(new Set([...this.backMenuOptions, value]));
|
|
41
|
-
const button = {
|
|
42
|
-
value,
|
|
43
|
-
classes: ["is-light"],
|
|
44
|
-
};
|
|
45
|
-
return button;
|
|
46
|
-
}
|
|
47
|
-
sendBackMenu(session, labels) {
|
|
48
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
49
|
-
labels = typeof labels === "string" ? [labels] : labels;
|
|
50
|
-
yield this.sendButtons(session, "", labels.map((value) => this.createBackButton(value)), undefined, {
|
|
51
|
-
ttsEnabled: false,
|
|
52
|
-
});
|
|
53
|
-
});
|
|
54
|
-
}
|
|
55
|
-
sendMenuButtons(session, buttons) {
|
|
56
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
57
|
-
yield this.sendButtons(session, "", buttons, undefined, {
|
|
58
|
-
ttsEnabled: false,
|
|
59
|
-
});
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
sendAgentMessage(session, message, messageLanguage) {
|
|
63
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
64
|
-
var _a, _b;
|
|
65
|
-
const app = yield this.sermas.getApp();
|
|
66
|
-
if (!app) {
|
|
67
|
-
this.logger.warn(`app is not available`);
|
|
68
|
-
return;
|
|
69
|
-
}
|
|
70
|
-
const filtered = (_b = (_a = app.repository) === null || _a === void 0 ? void 0 : _a.avatars) === null || _b === void 0 ? void 0 : _b.filter((a) => a.name === app.settings.avatar);
|
|
71
|
-
const gender = filtered.length ? filtered[0].gender : undefined;
|
|
72
|
-
yield this.sermas.sendChatMessage(Object.assign(Object.assign({}, session), { text: message, appId: app.appId, actor: "agent", gender, language: messageLanguage, chunkId: this.getChunkId() }));
|
|
73
|
-
});
|
|
74
|
-
}
|
|
75
|
-
sendButtons(session_1, label_1, list_1) {
|
|
76
|
-
return __awaiter(this, arguments, void 0, function* (session, label, list, metadata = {}, options = {
|
|
77
|
-
clearScreen: false,
|
|
78
|
-
ttsEnabled: true,
|
|
79
|
-
stopSpeech: false,
|
|
80
|
-
}) {
|
|
81
|
-
if (!session) {
|
|
82
|
-
this.logger.warn(`[sendButtons] Session is empty`);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
const ev = Object.assign(Object.assign({}, session), { metadata,
|
|
86
|
-
options, content: {
|
|
87
|
-
label,
|
|
88
|
-
list: list.map((button) => {
|
|
89
|
-
if (typeof button === "string")
|
|
90
|
-
return { value: button };
|
|
91
|
-
return button;
|
|
92
|
-
}),
|
|
93
|
-
}, contentType: "buttons", chunkId: this.getChunkId() });
|
|
94
|
-
yield this.sermas.sendUiContent(ev);
|
|
95
|
-
});
|
|
96
|
-
}
|
|
97
|
-
sendClearScreen(session) {
|
|
98
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
99
|
-
const ev = Object.assign(Object.assign({}, session), { metadata: {}, options: {
|
|
100
|
-
clearScreen: true,
|
|
101
|
-
stopSpeech: true,
|
|
102
|
-
}, content: {}, contentType: "clear-screen" });
|
|
103
|
-
yield this.sermas.sendUiContent(ev);
|
|
104
|
-
yield (0, core_1.sleep)(500);
|
|
105
|
-
});
|
|
106
|
-
}
|
|
107
|
-
sendImage(session, data, width, height) {
|
|
108
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
109
|
-
this.logger.info(`Send image sessionId=${session.sessionId} appId=${session.appId}`);
|
|
110
|
-
const ev = Object.assign(Object.assign({}, session), { metadata: {}, options: {
|
|
111
|
-
clearScreen: false,
|
|
112
|
-
stopSpeech: false,
|
|
113
|
-
}, content: { src: data, alt: "no image", width, height }, contentType: "image" });
|
|
114
|
-
yield this.sermas.sendUiContent(ev);
|
|
115
|
-
});
|
|
116
|
-
}
|
|
117
|
-
getChunkId(ts) {
|
|
118
|
-
return (0, ulidx_1.ulid)(ts ? new Date(ts).getTime() : undefined);
|
|
119
|
-
}
|
|
120
|
-
matchBackMenu(selection) {
|
|
121
|
-
return selection && this.backMenuOptions.indexOf(selection) > -1;
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
exports.UI = UI;
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
export const defaults = {
|
|
2
|
-
auth: {
|
|
3
|
-
PUBLIC_AUTH_URL: "http://172.17.0.1:8080/keycloak",
|
|
4
|
-
PUBLIC_AUTH_REALM: "sermas-local",
|
|
5
|
-
PRIVATE_AUTH_CLIENT_ID: "platform",
|
|
6
|
-
PRIVATE_AUTH_CLIENT_SECRET: "platform",
|
|
7
|
-
PRIVATE_API_BASE_URL: "http://api:3000/api",
|
|
8
|
-
},
|
|
9
|
-
sermas: {
|
|
10
|
-
SERMAS_BASE_URL: "http://localhost:8080",
|
|
11
|
-
SERMAS_CLIENT_ID: "sermas-client",
|
|
12
|
-
SERMAS_CLIENT_SECRET: "s3cr3t",
|
|
13
|
-
SERMAS_APPID: "sermas",
|
|
14
|
-
},
|
|
15
|
-
};
|
package/dist/esm/config/util.js
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
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 * as fs from "fs/promises";
|
|
11
|
-
import { createLogger } from "./logger.js";
|
|
12
|
-
export { v4 as uuidv4 } from "uuid";
|
|
13
|
-
const logger = createLogger("util");
|
|
14
|
-
export const saveJSON = (filename, dataset) => fs.writeFile(filename, JSON.stringify(dataset, null, 2));
|
|
15
|
-
export const loadJSON = (filename) => __awaiter(void 0, void 0, void 0, function* () {
|
|
16
|
-
try {
|
|
17
|
-
const raw = yield loadFile(filename);
|
|
18
|
-
if (raw === null)
|
|
19
|
-
return null;
|
|
20
|
-
return JSON.parse(raw);
|
|
21
|
-
}
|
|
22
|
-
catch (e) {
|
|
23
|
-
if (e instanceof Error)
|
|
24
|
-
logger.error(`Failed to load dataset: ${e.stack}`);
|
|
25
|
-
return null;
|
|
26
|
-
}
|
|
27
|
-
});
|
|
28
|
-
export const loadFile = (filename) => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
-
try {
|
|
30
|
-
const raw = yield fs.readFile(filename);
|
|
31
|
-
return raw.toString();
|
|
32
|
-
}
|
|
33
|
-
catch (e) {
|
|
34
|
-
if (e instanceof Error)
|
|
35
|
-
logger.error(`Failed to load dataset: ${e.stack}`);
|
|
36
|
-
return null;
|
|
37
|
-
}
|
|
38
|
-
});
|
|
39
|
-
export const saveFile = (filename, dataset) => fs.writeFile(filename, dataset);
|
package/dist/esm/dto/session.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/dist/esm/index.js
DELETED
|
@@ -1,6 +0,0 @@
|
|
|
1
|
-
export { getAvatarToken, getAvatarTokenWithClientSecret, } from "./modules/auth.js";
|
|
2
|
-
export { OpenAIClient } from "./modules/openai.js";
|
|
3
|
-
export { Store } from "./modules/store.js";
|
|
4
|
-
export { UI } from "./modules/ui.js";
|
|
5
|
-
export { Tools } from "./modules/tools.js";
|
|
6
|
-
export { SermasApp } from "./modules/sermas.js";
|