@sermas/sermas-toolkit-node-sdk 1.0.0-alpha.32 → 1.0.0-alpha.34
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 +2 -2
- 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/sermas.ts
DELETED
|
@@ -1,260 +0,0 @@
|
|
|
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 };
|
package/src/modules/store.ts
DELETED
|
@@ -1,217 +0,0 @@
|
|
|
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 };
|
package/src/modules/tools.ts
DELETED
|
@@ -1,62 +0,0 @@
|
|
|
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 };
|