@sermas/sermas-toolkit-node-sdk 1.0.0-alpha.3 → 1.0.0-alpha.31
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/.eslintrc.cjs +3 -1
- package/package.json +20 -7
- package/src/config/defaults.ts +6 -0
- package/src/config/util.ts +35 -0
- package/src/dto/session.ts +7 -0
- package/src/index.ts +10 -0
- package/src/modules/auth.ts +9 -8
- package/src/modules/openai.ts +69 -0
- package/src/modules/sermas.ts +260 -0
- package/src/modules/store.ts +217 -0
- package/src/modules/tools.ts +62 -0
- package/src/modules/ui.ts +204 -0
- package/tsconfig.json +1 -1
- package/tsup.config.ts +14 -0
package/.eslintrc.cjs
CHANGED
package/package.json
CHANGED
|
@@ -1,32 +1,45 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@sermas/sermas-toolkit-node-sdk",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.31",
|
|
4
4
|
"description": "Sermas toolkit node SDK",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"exports":
|
|
7
|
-
|
|
8
|
-
|
|
6
|
+
"exports": {
|
|
7
|
+
".": {
|
|
8
|
+
"types": "./index.d.ts",
|
|
9
|
+
"require": "./index.cjs",
|
|
10
|
+
"import": "./index.js"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"main": "./index.js",
|
|
9
14
|
"repository": {
|
|
10
15
|
"type": "git",
|
|
11
16
|
"url": "https://github.com/sermas-eu/sermas-toolkit-node-sdk.git"
|
|
12
17
|
},
|
|
13
18
|
"scripts": {
|
|
14
19
|
"clean": "rm -rf ./dist",
|
|
15
|
-
"build": "npm run clean &&
|
|
16
|
-
"
|
|
20
|
+
"build": "npm run clean && tsup && cp package.json README.md LICENSE dist/",
|
|
21
|
+
"build:watch": "tsup --watch",
|
|
22
|
+
"start": "node index.js",
|
|
17
23
|
"dev": "tsx watch src/index.ts"
|
|
18
24
|
},
|
|
19
25
|
"dependencies": {
|
|
20
|
-
"
|
|
26
|
+
"@sermas/api-client": "^1.0.0-alpha.79",
|
|
27
|
+
"axios": "^1.7.1",
|
|
28
|
+
"eventemitter2": "^6.4.9",
|
|
29
|
+
"openai": "^4.47.2",
|
|
30
|
+
"ulidx": "^2.3.0",
|
|
31
|
+
"uuid": "^9.0.1"
|
|
21
32
|
},
|
|
22
33
|
"devDependencies": {
|
|
23
34
|
"@types/node": "^20.12.12",
|
|
35
|
+
"@types/uuid": "^9.0.8",
|
|
24
36
|
"@typescript-eslint/eslint-plugin": "^7.9.0",
|
|
25
37
|
"@typescript-eslint/parser": "^7.9.0",
|
|
26
38
|
"eslint": "^8.57.0",
|
|
27
39
|
"eslint-config-prettier": "^9.1.0",
|
|
28
40
|
"eslint-plugin-prettier": "^5.1.3",
|
|
29
41
|
"prettier": "3.2.5",
|
|
42
|
+
"tsup": "^8.1.0",
|
|
30
43
|
"tsx": "^4.10.5",
|
|
31
44
|
"typescript": "^5.4.5"
|
|
32
45
|
},
|
package/src/config/defaults.ts
CHANGED
|
@@ -6,4 +6,10 @@ export const defaults = {
|
|
|
6
6
|
PRIVATE_AUTH_CLIENT_SECRET: "platform",
|
|
7
7
|
PRIVATE_API_BASE_URL: "http://api:3000/api",
|
|
8
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
|
+
},
|
|
9
15
|
} as const;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import * as fs from "fs/promises";
|
|
2
|
+
import { Logger } from "@sermas/api-client";
|
|
3
|
+
|
|
4
|
+
export { v4 as uuidv4 } from "uuid";
|
|
5
|
+
|
|
6
|
+
const logger = new Logger("util");
|
|
7
|
+
|
|
8
|
+
export const saveJSON = (filename: string, dataset: Record<string, unknown>) =>
|
|
9
|
+
fs.writeFile(filename, JSON.stringify(dataset, null, 2));
|
|
10
|
+
|
|
11
|
+
export const loadJSON = async <T = Record<string, unknown>>(
|
|
12
|
+
filename: string,
|
|
13
|
+
) => {
|
|
14
|
+
try {
|
|
15
|
+
const raw = await loadFile(filename);
|
|
16
|
+
if (raw === null) return null;
|
|
17
|
+
return JSON.parse(raw) as T;
|
|
18
|
+
} catch (e: unknown) {
|
|
19
|
+
if (e instanceof Error) logger.error(`Failed to load dataset: ${e.stack}`);
|
|
20
|
+
return null;
|
|
21
|
+
}
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const loadFile = async (filename: string) => {
|
|
25
|
+
try {
|
|
26
|
+
const raw = await fs.readFile(filename);
|
|
27
|
+
return raw.toString();
|
|
28
|
+
} catch (e: unknown) {
|
|
29
|
+
if (e instanceof Error) logger.error(`Failed to load dataset: ${e.stack}`);
|
|
30
|
+
return null;
|
|
31
|
+
}
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export const saveFile = (filename: string, dataset: string | Buffer) =>
|
|
35
|
+
fs.writeFile(filename, dataset);
|
package/src/index.ts
CHANGED
|
@@ -2,3 +2,13 @@ export {
|
|
|
2
2
|
getAvatarToken,
|
|
3
3
|
getAvatarTokenWithClientSecret,
|
|
4
4
|
} from "./modules/auth.js";
|
|
5
|
+
export { OpenAIClient } from "./modules/openai.js";
|
|
6
|
+
export { Store } from "./modules/store.js";
|
|
7
|
+
export { UI } from "./modules/ui.js";
|
|
8
|
+
export { Tools } from "./modules/tools.js";
|
|
9
|
+
export { SermasApp } from "./modules/sermas.js";
|
|
10
|
+
|
|
11
|
+
export type { OpenAIConfig } from "./modules/openai.js";
|
|
12
|
+
export type { SermasConfig } from "./modules/sermas.js";
|
|
13
|
+
|
|
14
|
+
export { BaseSessionWrapper } from "./dto/session.js";
|
package/src/modules/auth.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import axios, { isAxiosError } from "axios";
|
|
2
2
|
import * as https from "https";
|
|
3
3
|
import { defaults } from "../config/defaults.js";
|
|
4
|
+
import { Logger } from "@sermas/api-client";
|
|
4
5
|
|
|
5
6
|
export type AuthConfig = {
|
|
6
7
|
PUBLIC_AUTH_URL?: string;
|
|
@@ -29,7 +30,7 @@ export const getAvatarToken = async (
|
|
|
29
30
|
appId: string | null = null,
|
|
30
31
|
authConfig: AuthConfig = defaults.auth,
|
|
31
32
|
) => {
|
|
32
|
-
const logger =
|
|
33
|
+
const logger = new Logger("getAvatarToken");
|
|
33
34
|
const tokens: Record<string, TokenResponse | null> = {};
|
|
34
35
|
let kioskToken: TokenResponse | null = null;
|
|
35
36
|
|
|
@@ -63,7 +64,7 @@ export const getAvatarToken = async (
|
|
|
63
64
|
appId: string,
|
|
64
65
|
clientId = "avatar",
|
|
65
66
|
): Promise<TokenResponse | null> => {
|
|
66
|
-
logger.
|
|
67
|
+
logger.debug(`Fetch app token for appId=${appId} clientId=${clientId}`);
|
|
67
68
|
|
|
68
69
|
const cacheKey = `${appId}-${clientId}`;
|
|
69
70
|
|
|
@@ -77,7 +78,7 @@ export const getAvatarToken = async (
|
|
|
77
78
|
}
|
|
78
79
|
|
|
79
80
|
if (tokens[cacheKey]) {
|
|
80
|
-
logger.
|
|
81
|
+
logger.debug(`Using cached token for ${cacheKey}`);
|
|
81
82
|
return tokens[cacheKey];
|
|
82
83
|
}
|
|
83
84
|
|
|
@@ -98,7 +99,7 @@ export const getAvatarToken = async (
|
|
|
98
99
|
|
|
99
100
|
// expires 1min before
|
|
100
101
|
const expiresIn = res.data.expires_in * 1000;
|
|
101
|
-
logger.
|
|
102
|
+
logger.debug(
|
|
102
103
|
`Token for ${appId} expires in ${Math.round(expiresIn / 60 / 60 / 1000)}min`,
|
|
103
104
|
);
|
|
104
105
|
|
|
@@ -195,7 +196,7 @@ export const getAvatarTokenWithClientSecret = async (
|
|
|
195
196
|
authConfig: AuthConfig = defaults.auth,
|
|
196
197
|
clientSecret: string = "",
|
|
197
198
|
) => {
|
|
198
|
-
const logger =
|
|
199
|
+
const logger = new Logger("getAvatarTokenWithClientSecret");
|
|
199
200
|
const tokens: Record<string, TokenResponse | null> = {};
|
|
200
201
|
|
|
201
202
|
const PRIVATE_API_BASE_URL =
|
|
@@ -208,7 +209,7 @@ export const getAvatarTokenWithClientSecret = async (
|
|
|
208
209
|
clientId = "avatar",
|
|
209
210
|
clientSecret = "",
|
|
210
211
|
): Promise<TokenResponse | null> => {
|
|
211
|
-
logger.
|
|
212
|
+
logger.debug(
|
|
212
213
|
`Fetch app token for appId=${appId} clientId=${clientId} clientSecret=${clientSecret}`,
|
|
213
214
|
);
|
|
214
215
|
|
|
@@ -227,7 +228,7 @@ export const getAvatarTokenWithClientSecret = async (
|
|
|
227
228
|
}
|
|
228
229
|
|
|
229
230
|
if (tokens[cacheKey]) {
|
|
230
|
-
logger.
|
|
231
|
+
logger.debug(`Using cached token for ${cacheKey}`);
|
|
231
232
|
return tokens[cacheKey];
|
|
232
233
|
}
|
|
233
234
|
|
|
@@ -246,7 +247,7 @@ export const getAvatarTokenWithClientSecret = async (
|
|
|
246
247
|
|
|
247
248
|
// expires 1min before
|
|
248
249
|
const expiresIn = res.data.expires_in * 1000;
|
|
249
|
-
logger.
|
|
250
|
+
logger.debug(
|
|
250
251
|
`Token for ${appId} expires in ${Math.round(expiresIn / 60 / 60 / 1000)}min`,
|
|
251
252
|
);
|
|
252
253
|
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
import { Logger } from "@sermas/api-client";
|
|
2
|
+
import OpenAI, { ClientOptions } from "openai";
|
|
3
|
+
|
|
4
|
+
export type OpenAIConfig = {
|
|
5
|
+
apiKey: string;
|
|
6
|
+
} & ClientOptions;
|
|
7
|
+
|
|
8
|
+
type ChatCompletionMessage = {
|
|
9
|
+
messages: Array<OpenAI.Chat.ChatCompletionMessageParam>;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Wrapper for OpenAI client to implement sermas functionalities
|
|
14
|
+
*/
|
|
15
|
+
class OpenAIClient {
|
|
16
|
+
private logger = new Logger(`SERMAS SDK OpenAIClient`);
|
|
17
|
+
private openAI: OpenAI;
|
|
18
|
+
|
|
19
|
+
constructor(openAIConfig: OpenAIConfig) {
|
|
20
|
+
if (!openAIConfig.apiKey)
|
|
21
|
+
this.logger.warn("No API key provided for OpenAIClient");
|
|
22
|
+
this.openAI = new OpenAI(openAIConfig);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async chat(
|
|
26
|
+
body: string | ChatCompletionMessage,
|
|
27
|
+
options?: OpenAI.RequestOptions<unknown>,
|
|
28
|
+
) {
|
|
29
|
+
try {
|
|
30
|
+
const result: OpenAI.Chat.ChatCompletion =
|
|
31
|
+
await this.openAI.chat.completions.create(
|
|
32
|
+
{
|
|
33
|
+
model: "gpt-3.5-turbo",
|
|
34
|
+
...(typeof body === "string"
|
|
35
|
+
? { messages: [{ role: "user", content: body }] }
|
|
36
|
+
: body),
|
|
37
|
+
},
|
|
38
|
+
options,
|
|
39
|
+
);
|
|
40
|
+
return result.choices[0].message.content;
|
|
41
|
+
} catch (e: unknown) {
|
|
42
|
+
if (e instanceof Error)
|
|
43
|
+
this.logger.error(`OpenAI request failed: ${e.stack}`);
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async json<T = Record<string, unknown>>(
|
|
49
|
+
body: string | OpenAI.Chat.ChatCompletionCreateParams,
|
|
50
|
+
options?: OpenAI.RequestOptions<unknown>,
|
|
51
|
+
) {
|
|
52
|
+
let text = await this.chat(body, options);
|
|
53
|
+
if (text === null) return;
|
|
54
|
+
try {
|
|
55
|
+
if (text.startsWith("```json")) {
|
|
56
|
+
text = text.replace("```json", "").replace("```", "");
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
return JSON.parse(text) as T;
|
|
60
|
+
} catch (e: unknown) {
|
|
61
|
+
if (e instanceof Error)
|
|
62
|
+
this.logger.error(`Failed to parse content: ${e.stack}`);
|
|
63
|
+
this.logger.debug(`Content: ${text}`);
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
export { OpenAIClient };
|
|
@@ -0,0 +1,260 @@
|
|
|
1
|
+
import ee2 from "eventemitter2";
|
|
2
|
+
import {
|
|
3
|
+
AgentChangedDto,
|
|
4
|
+
AgentEvaluatePromptDto,
|
|
5
|
+
AppToolsDTO,
|
|
6
|
+
DialogueMessageDto,
|
|
7
|
+
DialogueToolTriggeredEventDto,
|
|
8
|
+
DialogueToolsRepositoryDto,
|
|
9
|
+
PlatformAppDto,
|
|
10
|
+
SermasApiClient,
|
|
11
|
+
SessionChangedDto,
|
|
12
|
+
SessionDto,
|
|
13
|
+
SessionStorageRecordDto,
|
|
14
|
+
SessionStorageSearchDto,
|
|
15
|
+
UIContentDto,
|
|
16
|
+
UIInteractionEventDto,
|
|
17
|
+
Logger,
|
|
18
|
+
} from "@sermas/api-client";
|
|
19
|
+
import { defaults } from "../config/defaults.js";
|
|
20
|
+
|
|
21
|
+
export type SermasConfig = {
|
|
22
|
+
SERMAS_BASE_URL: string;
|
|
23
|
+
SERMAS_CLIENT_ID: string;
|
|
24
|
+
SERMAS_CLIENT_SECRET: string;
|
|
25
|
+
SERMAS_APPID: string;
|
|
26
|
+
};
|
|
27
|
+
|
|
28
|
+
class SermasApp {
|
|
29
|
+
private logger: Logger;
|
|
30
|
+
private baseUrl: string;
|
|
31
|
+
|
|
32
|
+
private app: PlatformAppDto | null = null;
|
|
33
|
+
public client: SermasApiClient;
|
|
34
|
+
|
|
35
|
+
private clientId: string;
|
|
36
|
+
private clientSecret: string;
|
|
37
|
+
private appId: string;
|
|
38
|
+
|
|
39
|
+
public emitter: ee2.EventEmitter2;
|
|
40
|
+
public subs: (() => void)[] = [];
|
|
41
|
+
|
|
42
|
+
constructor(
|
|
43
|
+
sermasConfig: SermasConfig,
|
|
44
|
+
emitter: ee2.EventEmitter2,
|
|
45
|
+
logger: Logger = new Logger("SERMAS SDK"),
|
|
46
|
+
) {
|
|
47
|
+
this.baseUrl =
|
|
48
|
+
sermasConfig.SERMAS_BASE_URL || defaults.sermas.SERMAS_BASE_URL;
|
|
49
|
+
this.appId = sermasConfig.SERMAS_APPID || defaults.sermas.SERMAS_CLIENT_ID;
|
|
50
|
+
this.clientId =
|
|
51
|
+
sermasConfig.SERMAS_CLIENT_ID || defaults.sermas.SERMAS_CLIENT_SECRET;
|
|
52
|
+
this.clientSecret =
|
|
53
|
+
sermasConfig.SERMAS_CLIENT_SECRET || defaults.sermas.SERMAS_APPID;
|
|
54
|
+
|
|
55
|
+
this.logger = logger;
|
|
56
|
+
|
|
57
|
+
this.emitter = emitter || new ee2.EventEmitter2();
|
|
58
|
+
|
|
59
|
+
this.client = new SermasApiClient({
|
|
60
|
+
baseURL: this.getBaseUrl(),
|
|
61
|
+
appId: this.appId,
|
|
62
|
+
logger,
|
|
63
|
+
});
|
|
64
|
+
this.initialize();
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
getBaseUrl() {
|
|
68
|
+
return this.baseUrl;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
async initialize() {
|
|
72
|
+
// return await this.getClientCredentials();
|
|
73
|
+
|
|
74
|
+
await this.setupSermasClient();
|
|
75
|
+
|
|
76
|
+
this.addSub(
|
|
77
|
+
async () =>
|
|
78
|
+
await this.client.events.session.onSessionChanged(
|
|
79
|
+
(ev: SessionChangedDto) => {
|
|
80
|
+
this.logger.debug(
|
|
81
|
+
`session changed ${ev.operation.toUpperCase()} sessionId=${ev.record.sessionId}`,
|
|
82
|
+
);
|
|
83
|
+
this.emitter.emit(`session`, ev);
|
|
84
|
+
},
|
|
85
|
+
),
|
|
86
|
+
);
|
|
87
|
+
|
|
88
|
+
this.addSub(
|
|
89
|
+
async () =>
|
|
90
|
+
await this.client.events.dialogue.onToolTriggered(
|
|
91
|
+
(event: DialogueToolTriggeredEventDto) => {
|
|
92
|
+
this.logger.debug("Tool triggered: " + event.name);
|
|
93
|
+
this.emitter.emit(`tool`, event);
|
|
94
|
+
},
|
|
95
|
+
),
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
this.addSub(
|
|
99
|
+
async () =>
|
|
100
|
+
await this.client.events.ui.onInteraction(
|
|
101
|
+
(event: UIInteractionEventDto) => {
|
|
102
|
+
// this.logger.debug('UI interaction: ' + event.interaction);
|
|
103
|
+
this.emitter.emit(`ui.interaction`, event);
|
|
104
|
+
},
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
this.addSub(
|
|
109
|
+
async () =>
|
|
110
|
+
await this.client.events.session.onAgentChanged(
|
|
111
|
+
(event: AgentChangedDto) => {
|
|
112
|
+
this.logger.debug("Agent changed: " + event.operation);
|
|
113
|
+
this.emitter.emit(`agent.changed`, event);
|
|
114
|
+
},
|
|
115
|
+
),
|
|
116
|
+
);
|
|
117
|
+
|
|
118
|
+
await this.getApp();
|
|
119
|
+
this.logger.debug("Sermas initialization completed");
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
private async setupSermasClient() {
|
|
123
|
+
this.logger.debug("Initializing sermas client.");
|
|
124
|
+
|
|
125
|
+
try {
|
|
126
|
+
this.logger.debug(`Loading token`);
|
|
127
|
+
await this.client.loadToken(this.clientId, this.clientSecret);
|
|
128
|
+
} catch (e: unknown) {
|
|
129
|
+
this.logger.error(`Failed to load token, retrying`);
|
|
130
|
+
if (e instanceof Error) this.logger.debug(e.stack);
|
|
131
|
+
setTimeout(() => this.setupSermasClient(), 1000);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
this.logger.debug(`sermas client ready`);
|
|
136
|
+
this.emitter.emit("sermas.ready");
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
addSub(sub: () => Promise<() => void>) {
|
|
140
|
+
sub()
|
|
141
|
+
.then((fn) => {
|
|
142
|
+
this.subs.push(fn);
|
|
143
|
+
})
|
|
144
|
+
.catch((e) => {
|
|
145
|
+
this.logger.error(`Subscribe failed: ${e.stack}`);
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
async prompt(prompt: AgentEvaluatePromptDto) {
|
|
149
|
+
return await this.client.api.session.prompt({
|
|
150
|
+
requestBody: prompt,
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
async sendChatMessage(ev: DialogueMessageDto) {
|
|
155
|
+
await this.client.api.dialogue.chatMessage({
|
|
156
|
+
appId: ev.appId,
|
|
157
|
+
sessionId: ev.sessionId!,
|
|
158
|
+
requestBody: ev,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
async sendUiContent(ev: UIContentDto) {
|
|
163
|
+
await this.client.events.ui.content(ev);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
async getApp() {
|
|
167
|
+
if (this.app) return this.app;
|
|
168
|
+
this.logger.debug(`Load app ${this.appId}`);
|
|
169
|
+
try {
|
|
170
|
+
this.app = await this.client.api.platform.readApp({ appId: this.appId });
|
|
171
|
+
return this.app;
|
|
172
|
+
} catch (e: unknown) {
|
|
173
|
+
if (e instanceof Error)
|
|
174
|
+
this.logger.error(`Failed to load app ${this.appId}: ${e.stack}`);
|
|
175
|
+
return null;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
async updateAppTools(tools: AppToolsDTO[]) {
|
|
180
|
+
await this.client.api.platform.updateAppTools({
|
|
181
|
+
appId: this.appId,
|
|
182
|
+
requestBody: tools,
|
|
183
|
+
});
|
|
184
|
+
this.logger.debug("Updated tools");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
async setTools(repository: DialogueToolsRepositoryDto) {
|
|
188
|
+
await this.client.api.dialogue.setTools({
|
|
189
|
+
repositoryId: repository.repositoryId!,
|
|
190
|
+
requestBody: repository,
|
|
191
|
+
});
|
|
192
|
+
this.logger.debug("Updated tools");
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
async addTools(repository: DialogueToolsRepositoryDto) {
|
|
196
|
+
await this.client.api.dialogue.addTools({
|
|
197
|
+
repositoryId: repository.repositoryId!,
|
|
198
|
+
requestBody: repository,
|
|
199
|
+
});
|
|
200
|
+
this.logger.debug("Add tools");
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
async readSession(sessionId: string): Promise<SessionDto | null> {
|
|
204
|
+
try {
|
|
205
|
+
return await this.client.api.session.readSession({ sessionId });
|
|
206
|
+
} catch (e: unknown) {
|
|
207
|
+
if (e instanceof Error)
|
|
208
|
+
this.logger.error(`Failed to readSession: ${e.stack}`);
|
|
209
|
+
return null;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
async getRecord(storageId: string) {
|
|
214
|
+
try {
|
|
215
|
+
return await this.client.api.session.getRecord({ storageId });
|
|
216
|
+
} catch (e: unknown) {
|
|
217
|
+
if (e instanceof Error && e.message.indexOf("Not Found") === -1)
|
|
218
|
+
this.logger.error(`Failed to getRecord: ${e.stack}`);
|
|
219
|
+
return null;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async findRecords(query: SessionStorageSearchDto) {
|
|
224
|
+
try {
|
|
225
|
+
return await this.client.api.session.findRecords({
|
|
226
|
+
requestBody: query,
|
|
227
|
+
});
|
|
228
|
+
} catch (e: unknown) {
|
|
229
|
+
if (e instanceof Error)
|
|
230
|
+
this.logger.error(`Failed to findRecords: ${e.stack}`);
|
|
231
|
+
return null;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
async setRecord(storage: SessionStorageRecordDto) {
|
|
236
|
+
try {
|
|
237
|
+
return await this.client.api.session.setRecord({
|
|
238
|
+
requestBody: storage,
|
|
239
|
+
});
|
|
240
|
+
} catch (e: unknown) {
|
|
241
|
+
if (e instanceof Error)
|
|
242
|
+
this.logger.error(`Failed to setRecord: ${e.stack}`);
|
|
243
|
+
return null;
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
async generateQRCode(data: string) {
|
|
248
|
+
try {
|
|
249
|
+
return await this.client.api.ui.generateQrCode({
|
|
250
|
+
requestBody: { version: 5, data },
|
|
251
|
+
});
|
|
252
|
+
} catch (e: unknown) {
|
|
253
|
+
if (e instanceof Error)
|
|
254
|
+
this.logger.error(`Failed to generate QR code: ${e.stack}`);
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
export { SermasApp };
|
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Logger,
|
|
3
|
+
SessionChangedDto,
|
|
4
|
+
SessionStorageRecordDto,
|
|
5
|
+
} from "@sermas/api-client";
|
|
6
|
+
import { SermasApp } from "./sermas.js";
|
|
7
|
+
import { uuidv4 } from "../config/util.js";
|
|
8
|
+
import { BaseSessionWrapper } from "../dto/session.js";
|
|
9
|
+
|
|
10
|
+
class Store {
|
|
11
|
+
private readonly logger: Logger;
|
|
12
|
+
|
|
13
|
+
private sermas: SermasApp;
|
|
14
|
+
private sessions: Record<string, unknown> = {};
|
|
15
|
+
|
|
16
|
+
private locks: { [sessionId: string]: Promise<any> } = {};
|
|
17
|
+
|
|
18
|
+
constructor(sermas: SermasApp, logger: Logger | undefined = undefined) {
|
|
19
|
+
this.sermas = sermas;
|
|
20
|
+
this.logger = logger || new Logger(`SERMAS SDK Store`);
|
|
21
|
+
|
|
22
|
+
this.sermas.emitter.on("session", this.onSessionChange.bind(this));
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
// @OnEvent("session")
|
|
26
|
+
async onSessionChange(ev: SessionChangedDto) {
|
|
27
|
+
if (!ev.record.sessionId) return;
|
|
28
|
+
const session = await this.saveSessionWrapper(ev);
|
|
29
|
+
switch (ev.operation) {
|
|
30
|
+
case "created":
|
|
31
|
+
if (!session) {
|
|
32
|
+
this.logger.warn(
|
|
33
|
+
`Failed to get session for sessionId=${ev.record.sessionId}`,
|
|
34
|
+
);
|
|
35
|
+
return;
|
|
36
|
+
}
|
|
37
|
+
this.sermas.emitter.emit("session.started", session);
|
|
38
|
+
break;
|
|
39
|
+
case "updated":
|
|
40
|
+
await this.saveSessionWrapper(ev);
|
|
41
|
+
break;
|
|
42
|
+
case "removed":
|
|
43
|
+
if (!this.sessions[ev.record.sessionId]) return;
|
|
44
|
+
delete this.sessions[ev.record.sessionId];
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
private async lock<T = any>(sessionId: string, fn: () => Promise<any>) {
|
|
50
|
+
// eslint-disable-next-line no-async-promise-executor
|
|
51
|
+
this.locks[sessionId] = new Promise<T>(async (resolve, reject) => {
|
|
52
|
+
try {
|
|
53
|
+
resolve(await fn());
|
|
54
|
+
} catch (e) {
|
|
55
|
+
reject(e);
|
|
56
|
+
}
|
|
57
|
+
});
|
|
58
|
+
try {
|
|
59
|
+
const res = await this.locks[sessionId];
|
|
60
|
+
return res;
|
|
61
|
+
} catch (e) {
|
|
62
|
+
if (e instanceof Error) this.logger.error(`lock failure: ${e.stack}`);
|
|
63
|
+
return null;
|
|
64
|
+
} finally {
|
|
65
|
+
delete this.locks[sessionId];
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async loadSessionWrapper<SessionWrapper extends BaseSessionWrapper>(context: {
|
|
70
|
+
sessionId: string;
|
|
71
|
+
appId?: string;
|
|
72
|
+
userId?: string;
|
|
73
|
+
}): Promise<SessionWrapper> {
|
|
74
|
+
const opId = `[${performance.now()}] `;
|
|
75
|
+
if (await this.locks[context.sessionId]) {
|
|
76
|
+
this.logger.debug(`${opId} Wait lock for sessionId=${context.sessionId}`);
|
|
77
|
+
await this.locks[context.sessionId];
|
|
78
|
+
this.logger.debug(
|
|
79
|
+
`${opId} Lock released for sessionId=${context.sessionId}`,
|
|
80
|
+
);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
return await this.lock<SessionWrapper>(context.sessionId, async () => {
|
|
84
|
+
this.logger.debug(
|
|
85
|
+
`${opId} Loading wrapper for sessionId=${context.sessionId}`,
|
|
86
|
+
);
|
|
87
|
+
const records = await this.sermas.findRecords({
|
|
88
|
+
appId: "",
|
|
89
|
+
sessionId: [context.sessionId],
|
|
90
|
+
userId: undefined,
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
if (records && records.length) {
|
|
94
|
+
this.logger.debug(
|
|
95
|
+
`${opId} Wrapper exists for sessionId=${context.sessionId}`,
|
|
96
|
+
);
|
|
97
|
+
return records[0].data as SessionWrapper;
|
|
98
|
+
} else {
|
|
99
|
+
this.logger.debug(
|
|
100
|
+
`${opId} Wrapper not found for sessionId=${context.sessionId}`,
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
this.logger.debug(
|
|
105
|
+
`${opId} Creating new session wrapper store sessionId=${context.sessionId} appId=${context.appId} userId=${context.userId}`,
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
const storageId = uuidv4();
|
|
109
|
+
const sessionWrapper = {
|
|
110
|
+
...context,
|
|
111
|
+
appId: context.appId,
|
|
112
|
+
sessionId: context.sessionId,
|
|
113
|
+
userId: context.userId,
|
|
114
|
+
storageId,
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
if (!sessionWrapper.appId || !sessionWrapper.userId) {
|
|
118
|
+
this.logger.debug(
|
|
119
|
+
`${opId} Load session record sessionId=${sessionWrapper.sessionId}`,
|
|
120
|
+
);
|
|
121
|
+
const session = await this.sermas.readSession(sessionWrapper.sessionId);
|
|
122
|
+
if (session) {
|
|
123
|
+
if (!sessionWrapper.appId) sessionWrapper.appId = session.appId;
|
|
124
|
+
if (!sessionWrapper.userId) sessionWrapper.userId = session.userId;
|
|
125
|
+
} else {
|
|
126
|
+
this.logger.warn(
|
|
127
|
+
`${opId} Failed to load sessionId=${sessionWrapper.sessionId}`,
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
const record: SessionStorageRecordDto = {
|
|
133
|
+
appId: sessionWrapper.appId!,
|
|
134
|
+
sessionId: sessionWrapper.sessionId,
|
|
135
|
+
data: sessionWrapper,
|
|
136
|
+
userId: sessionWrapper.userId || undefined,
|
|
137
|
+
storageId,
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
await this.sermas.setRecord(record);
|
|
141
|
+
this.logger.debug(
|
|
142
|
+
`${opId} Saved store storageId=${record.storageId} for sessionId=${record.sessionId} appId=${record.appId} userId=${record.userId}`,
|
|
143
|
+
);
|
|
144
|
+
|
|
145
|
+
return sessionWrapper;
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
async saveSession<SessionWrapper extends BaseSessionWrapper>(
|
|
150
|
+
session: SessionWrapper,
|
|
151
|
+
): Promise<SessionWrapper | null> {
|
|
152
|
+
let record = await this.sermas.getRecord(session.storageId);
|
|
153
|
+
if (!record) {
|
|
154
|
+
this.logger.warn(
|
|
155
|
+
`sessionId=${session.sessionId} storageId=${session.storageId} not found, creating new record`,
|
|
156
|
+
);
|
|
157
|
+
const newSession = await this.loadSessionWrapper({ ...session });
|
|
158
|
+
record = await this.sermas.getRecord(newSession.storageId);
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
if (!record) {
|
|
162
|
+
this.logger.error(
|
|
163
|
+
`Cannot save sessionId=${session.sessionId}, storage not found`,
|
|
164
|
+
);
|
|
165
|
+
return null;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
record.data = {
|
|
169
|
+
...session,
|
|
170
|
+
storageId: record.storageId,
|
|
171
|
+
} as SessionWrapper;
|
|
172
|
+
|
|
173
|
+
const res = await this.sermas.setRecord(record);
|
|
174
|
+
this.logger.debug(
|
|
175
|
+
`Saved record for sessionId=${record.sessionId} storageId=${record.storageId}`,
|
|
176
|
+
);
|
|
177
|
+
return res?.data as SessionWrapper;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
async getSessionWrapper(
|
|
181
|
+
sessionId: string,
|
|
182
|
+
): Promise<BaseSessionWrapper | undefined> {
|
|
183
|
+
if (!sessionId) {
|
|
184
|
+
this.logger.debug(`sessionId is empty`);
|
|
185
|
+
return;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
if (!this.sessions[sessionId]) {
|
|
189
|
+
const session = await this.loadSessionWrapper({
|
|
190
|
+
sessionId,
|
|
191
|
+
});
|
|
192
|
+
this.logger.debug(`loaded sessionId=${sessionId}`);
|
|
193
|
+
this.sessions[sessionId] = session;
|
|
194
|
+
}
|
|
195
|
+
return this.sessions[sessionId] as BaseSessionWrapper;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
async saveSessionWrapper(ev: SessionChangedDto) {
|
|
199
|
+
if (!ev.appId) return;
|
|
200
|
+
if (!ev.record.sessionId) return;
|
|
201
|
+
|
|
202
|
+
if (this.sessions[ev.record.sessionId]) return;
|
|
203
|
+
|
|
204
|
+
const wrapper = await this.loadSessionWrapper({
|
|
205
|
+
userId: ev.userId,
|
|
206
|
+
appId: ev.appId,
|
|
207
|
+
sessionId: ev.record.sessionId,
|
|
208
|
+
});
|
|
209
|
+
|
|
210
|
+
this.sessions[wrapper.sessionId] = wrapper;
|
|
211
|
+
this.logger.debug(`Added session ${wrapper.sessionId}`);
|
|
212
|
+
|
|
213
|
+
return this.sessions[wrapper.sessionId];
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
export { Store };
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { AppToolsDTO, DialogueToolsRepositoryDto } from "@sermas/api-client";
|
|
2
|
+
import { BaseSessionWrapper } from "../dto/session.js";
|
|
3
|
+
import { SermasApp } from "./sermas.js";
|
|
4
|
+
|
|
5
|
+
type ToolsSchemaValues = string | number | boolean | undefined;
|
|
6
|
+
type ToolsSchemaParameters = Record<string, ToolsSchemaValues>;
|
|
7
|
+
|
|
8
|
+
class Tools {
|
|
9
|
+
private sermas: SermasApp;
|
|
10
|
+
|
|
11
|
+
constructor(sermasApp: SermasApp) {
|
|
12
|
+
this.sermas = sermasApp;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
async reset(session: BaseSessionWrapper) {
|
|
16
|
+
await this.sermas.setTools({
|
|
17
|
+
appId: session.appId,
|
|
18
|
+
sessionId: session.sessionId,
|
|
19
|
+
repositoryId: session.sessionId,
|
|
20
|
+
tools: [],
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
async set(
|
|
25
|
+
session: BaseSessionWrapper,
|
|
26
|
+
repository: DialogueToolsRepositoryDto,
|
|
27
|
+
) {
|
|
28
|
+
repository.sessionId = session.sessionId;
|
|
29
|
+
repository.repositoryId =
|
|
30
|
+
(session.repositoryId as string) || session.sessionId;
|
|
31
|
+
await this.sermas.setTools(repository);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async add(
|
|
35
|
+
session: BaseSessionWrapper,
|
|
36
|
+
repository: DialogueToolsRepositoryDto,
|
|
37
|
+
) {
|
|
38
|
+
repository.sessionId = session.sessionId;
|
|
39
|
+
repository.repositoryId =
|
|
40
|
+
(session.repositoryId as string) || session.sessionId;
|
|
41
|
+
await this.sermas.addTools(repository);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
getSchemaValues<T extends ToolsSchemaParameters = ToolsSchemaParameters>(
|
|
45
|
+
tool: AppToolsDTO,
|
|
46
|
+
): T {
|
|
47
|
+
const values: ToolsSchemaParameters = {};
|
|
48
|
+
|
|
49
|
+
if (!tool.schema) return values as T;
|
|
50
|
+
|
|
51
|
+
tool.schema.forEach((schema) => (values[schema.parameter] = schema.value));
|
|
52
|
+
|
|
53
|
+
return values as T;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
getSchemaValue(tool: AppToolsDTO, parameterName: string) {
|
|
57
|
+
const values = this.getSchemaValues(tool);
|
|
58
|
+
return values[parameterName];
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
|
|
62
|
+
export { Tools };
|
|
@@ -0,0 +1,204 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ButtonDto,
|
|
3
|
+
ButtonsUIContentDto,
|
|
4
|
+
Logger,
|
|
5
|
+
SessionChangedDto,
|
|
6
|
+
SupportedContentTypes,
|
|
7
|
+
UIContentDto,
|
|
8
|
+
UIContentOptionsDto,
|
|
9
|
+
} from "@sermas/api-client";
|
|
10
|
+
import { sleep } from "openai/core";
|
|
11
|
+
import { ulid } from "ulidx";
|
|
12
|
+
import { BaseSessionWrapper } from "../dto/session.js";
|
|
13
|
+
import { SermasApp } from "./sermas.js";
|
|
14
|
+
import { Store } from "./store.js";
|
|
15
|
+
|
|
16
|
+
class UI {
|
|
17
|
+
private sermas: SermasApp;
|
|
18
|
+
private store: Store;
|
|
19
|
+
|
|
20
|
+
private readonly logger: Logger;
|
|
21
|
+
private backMenuOptions: string[] = [];
|
|
22
|
+
|
|
23
|
+
constructor(
|
|
24
|
+
sermas: SermasApp,
|
|
25
|
+
store: Store,
|
|
26
|
+
logger: Logger | undefined = undefined,
|
|
27
|
+
) {
|
|
28
|
+
this.sermas = sermas;
|
|
29
|
+
this.store = store;
|
|
30
|
+
|
|
31
|
+
this.logger = logger || new Logger(`SERMAS SDK UI`);
|
|
32
|
+
|
|
33
|
+
this.sermas.emitter.on("session", this.onSessionChange.bind(this));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// @OnEvent("session")
|
|
37
|
+
async onSessionChange(ev: SessionChangedDto) {
|
|
38
|
+
if (!ev.record.sessionId) return;
|
|
39
|
+
const session = await this.store.getSessionWrapper(ev.record.sessionId);
|
|
40
|
+
switch (ev.operation) {
|
|
41
|
+
case "removed":
|
|
42
|
+
if (!session) return;
|
|
43
|
+
await this.sendClearScreen(session);
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
createBackButton(value: string) {
|
|
49
|
+
this.backMenuOptions = Array.from(
|
|
50
|
+
new Set([...this.backMenuOptions, value]),
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
const button: ButtonDto = {
|
|
54
|
+
value,
|
|
55
|
+
classes: ["is-light"],
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
return button;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async sendBackMenu(session: BaseSessionWrapper, labels: string | string[]) {
|
|
62
|
+
labels = typeof labels === "string" ? [labels] : labels;
|
|
63
|
+
await this.sendButtons(
|
|
64
|
+
session,
|
|
65
|
+
"",
|
|
66
|
+
labels.map((value) => this.createBackButton(value)),
|
|
67
|
+
undefined,
|
|
68
|
+
{
|
|
69
|
+
ttsEnabled: false,
|
|
70
|
+
},
|
|
71
|
+
);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async sendMenuButtons(session: BaseSessionWrapper, buttons: ButtonDto[]) {
|
|
75
|
+
await this.sendButtons(session, "", buttons, undefined, {
|
|
76
|
+
ttsEnabled: false,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
async sendAgentMessage(
|
|
81
|
+
session: BaseSessionWrapper,
|
|
82
|
+
message: string,
|
|
83
|
+
messageLanguage: string,
|
|
84
|
+
) {
|
|
85
|
+
const app = await this.sermas.getApp();
|
|
86
|
+
|
|
87
|
+
if (!app) {
|
|
88
|
+
this.logger.warn(`app is not available`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
const filtered = app.repository?.avatars?.filter(
|
|
93
|
+
(a) => a.name === app.settings!.avatar,
|
|
94
|
+
);
|
|
95
|
+
const gender = filtered.length ? filtered[0].gender : undefined;
|
|
96
|
+
|
|
97
|
+
await this.sermas.sendChatMessage({
|
|
98
|
+
...session,
|
|
99
|
+
text: message,
|
|
100
|
+
appId: app.appId,
|
|
101
|
+
actor: "agent",
|
|
102
|
+
gender,
|
|
103
|
+
language: messageLanguage,
|
|
104
|
+
chunkId: this.getChunkId(),
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
async sendButtons(
|
|
109
|
+
session: BaseSessionWrapper,
|
|
110
|
+
label: string,
|
|
111
|
+
list: (string | ButtonDto)[],
|
|
112
|
+
metadata: { [key: string]: unknown } = {},
|
|
113
|
+
options: UIContentOptionsDto = {
|
|
114
|
+
clearScreen: false,
|
|
115
|
+
ttsEnabled: true,
|
|
116
|
+
stopSpeech: false,
|
|
117
|
+
},
|
|
118
|
+
) {
|
|
119
|
+
if (!session) {
|
|
120
|
+
this.logger.warn(`[sendButtons] Session is empty`);
|
|
121
|
+
return;
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const ev: ButtonsUIContentDto = {
|
|
125
|
+
...session,
|
|
126
|
+
metadata,
|
|
127
|
+
options,
|
|
128
|
+
content: {
|
|
129
|
+
label,
|
|
130
|
+
list: list.map((button) => {
|
|
131
|
+
if (typeof button === "string") return { value: button };
|
|
132
|
+
return button;
|
|
133
|
+
}),
|
|
134
|
+
},
|
|
135
|
+
contentType: "buttons",
|
|
136
|
+
chunkId: this.getChunkId(),
|
|
137
|
+
};
|
|
138
|
+
await this.sermas.sendUiContent(ev);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
async sendClearScreen<T extends BaseSessionWrapper>(session: T) {
|
|
142
|
+
const ev: UIContentDto = {
|
|
143
|
+
...session,
|
|
144
|
+
metadata: {},
|
|
145
|
+
options: {
|
|
146
|
+
clearScreen: true,
|
|
147
|
+
stopSpeech: true,
|
|
148
|
+
},
|
|
149
|
+
content: {},
|
|
150
|
+
contentType: "clear-screen",
|
|
151
|
+
};
|
|
152
|
+
await this.sermas.sendUiContent(ev);
|
|
153
|
+
await sleep(500);
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
async sendImage(
|
|
157
|
+
session: BaseSessionWrapper,
|
|
158
|
+
data: string,
|
|
159
|
+
width: number,
|
|
160
|
+
height: number,
|
|
161
|
+
) {
|
|
162
|
+
this.logger.debug(
|
|
163
|
+
`Send image sessionId=${session.sessionId} appId=${session.appId}`,
|
|
164
|
+
);
|
|
165
|
+
const ev: UIContentDto = {
|
|
166
|
+
...session,
|
|
167
|
+
metadata: {},
|
|
168
|
+
options: {
|
|
169
|
+
clearScreen: false,
|
|
170
|
+
stopSpeech: false,
|
|
171
|
+
},
|
|
172
|
+
content: { src: data, alt: "no image", width, height },
|
|
173
|
+
contentType: "image",
|
|
174
|
+
};
|
|
175
|
+
await this.sermas.sendUiContent(ev);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
async sendQrCodeScanner(session: BaseSessionWrapper) {
|
|
179
|
+
this.logger.debug(
|
|
180
|
+
`Send image sessionId=${session.sessionId} appId=${session.appId}`,
|
|
181
|
+
);
|
|
182
|
+
const ev: UIContentDto = {
|
|
183
|
+
...session,
|
|
184
|
+
metadata: {},
|
|
185
|
+
options: {
|
|
186
|
+
clearScreen: false,
|
|
187
|
+
stopSpeech: false,
|
|
188
|
+
},
|
|
189
|
+
content: {},
|
|
190
|
+
contentType: "qrcode-scanner" as SupportedContentTypes,
|
|
191
|
+
};
|
|
192
|
+
await this.sermas.sendUiContent(ev);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
getChunkId(ts?: Date) {
|
|
196
|
+
return ulid(ts ? new Date(ts).getTime() : undefined);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
matchBackMenu(selection: string) {
|
|
200
|
+
return selection && this.backMenuOptions.indexOf(selection) > -1;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
export { UI };
|
package/tsconfig.json
CHANGED
package/tsup.config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { defineConfig } from "tsup";
|
|
2
|
+
|
|
3
|
+
export default defineConfig((options) => ({
|
|
4
|
+
entry: ["./src/index.ts"],
|
|
5
|
+
target: "es2020",
|
|
6
|
+
format: ["cjs", "esm"],
|
|
7
|
+
splitting: false,
|
|
8
|
+
sourcemap: true,
|
|
9
|
+
clean: true,
|
|
10
|
+
minify: !options.watch,
|
|
11
|
+
dts: true,
|
|
12
|
+
silent: !options.watch,
|
|
13
|
+
cjsInterop: false,
|
|
14
|
+
}));
|