@sermas/sermas-toolkit-node-sdk 1.0.0-alpha.32 → 1.0.0-alpha.33
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/index.cjs +2 -0
- package/index.cjs.map +1 -0
- package/index.d.cts +148 -0
- package/index.d.ts +148 -0
- package/index.js +2 -0
- package/index.js.map +1 -0
- package/package.json +1 -1
- package/.eslintrc.cjs +0 -32
- package/.github/workflows/npm-publish.yml +0 -22
- package/.prettierrc +0 -0
- package/src/config/defaults.ts +0 -15
- package/src/config/util.ts +0 -35
- package/src/dto/session.ts +0 -7
- package/src/index.ts +0 -14
- package/src/modules/auth.ts +0 -300
- package/src/modules/openai.ts +0 -69
- package/src/modules/sermas.ts +0 -260
- package/src/modules/store.ts +0 -217
- package/src/modules/tools.ts +0 -62
- package/src/modules/ui.ts +0 -204
- package/tsconfig.json +0 -14
- package/tsup.config.ts +0 -14
package/src/modules/ui.ts
DELETED
|
@@ -1,204 +0,0 @@
|
|
|
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
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2015",
|
|
4
|
-
"moduleResolution": "NodeNext",
|
|
5
|
-
"module": "NodeNext",
|
|
6
|
-
"esModuleInterop": true,
|
|
7
|
-
"forceConsistentCasingInFileNames": true,
|
|
8
|
-
"declaration": false,
|
|
9
|
-
"strict": true,
|
|
10
|
-
"skipLibCheck": true,
|
|
11
|
-
"outDir": "./dist"
|
|
12
|
-
},
|
|
13
|
-
"include": ["src"]
|
|
14
|
-
}
|
package/tsup.config.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
}));
|