@botonic/webchat-core 2.23.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +247 -0
- package/PR-LOCALE-SUPPORT.md +156 -0
- package/README.md +11 -0
- package/package.json +29 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +15 -0
- package/src/index.js.map +1 -0
- package/src/lib/controllers/index.d.ts +2 -0
- package/src/lib/controllers/index.js +3 -0
- package/src/lib/controllers/index.js.map +1 -0
- package/src/lib/controllers/public.d.ts +383 -0
- package/src/lib/controllers/public.js +194 -0
- package/src/lib/controllers/public.js.map +1 -0
- package/src/lib/controllers/webchat.d.ts +414 -0
- package/src/lib/controllers/webchat.js +255 -0
- package/src/lib/controllers/webchat.js.map +1 -0
- package/src/lib/domain/index.d.ts +50 -0
- package/src/lib/domain/index.js +11 -0
- package/src/lib/domain/index.js.map +1 -0
- package/src/lib/infra/clients/hubtype-api.d.ts +53 -0
- package/src/lib/infra/clients/hubtype-api.js +189 -0
- package/src/lib/infra/clients/hubtype-api.js.map +1 -0
- package/src/lib/infra/clients/index.d.ts +3 -0
- package/src/lib/infra/clients/index.js +4 -0
- package/src/lib/infra/clients/index.js.map +1 -0
- package/src/lib/infra/clients/realtime.d.ts +92 -0
- package/src/lib/infra/clients/realtime.js +277 -0
- package/src/lib/infra/clients/realtime.js.map +1 -0
- package/src/lib/infra/clients/types.d.ts +21 -0
- package/src/lib/infra/clients/types.js +2 -0
- package/src/lib/infra/clients/types.js.map +1 -0
- package/src/lib/infra/repositories/index.d.ts +1 -0
- package/src/lib/infra/repositories/index.js +2 -0
- package/src/lib/infra/repositories/index.js.map +1 -0
- package/src/lib/infra/repositories/storage/base-storage.d.ts +23 -0
- package/src/lib/infra/repositories/storage/base-storage.js +102 -0
- package/src/lib/infra/repositories/storage/base-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/browser-storage.d.ts +9 -0
- package/src/lib/infra/repositories/storage/browser-storage.js +46 -0
- package/src/lib/infra/repositories/storage/browser-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/factory.d.ts +2 -0
- package/src/lib/infra/repositories/storage/factory.js +20 -0
- package/src/lib/infra/repositories/storage/factory.js.map +1 -0
- package/src/lib/infra/repositories/storage/in-memory.d.ts +9 -0
- package/src/lib/infra/repositories/storage/in-memory.js +21 -0
- package/src/lib/infra/repositories/storage/in-memory.js.map +1 -0
- package/src/lib/infra/repositories/storage/index.d.ts +7 -0
- package/src/lib/infra/repositories/storage/index.js +7 -0
- package/src/lib/infra/repositories/storage/index.js.map +1 -0
- package/src/lib/infra/repositories/storage/local-storage.d.ts +4 -0
- package/src/lib/infra/repositories/storage/local-storage.js +7 -0
- package/src/lib/infra/repositories/storage/local-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/session-storage.d.ts +4 -0
- package/src/lib/infra/repositories/storage/session-storage.js +7 -0
- package/src/lib/infra/repositories/storage/session-storage.js.map +1 -0
- package/src/lib/infra/repositories/storage/types.d.ts +30 -0
- package/src/lib/infra/repositories/storage/types.js +2 -0
- package/src/lib/infra/repositories/storage/types.js.map +1 -0
- package/src/lib/machines/message.d.ts +200 -0
- package/src/lib/machines/message.js +210 -0
- package/src/lib/machines/message.js.map +1 -0
- package/src/lib/machines/overlay.d.ts +299 -0
- package/src/lib/machines/overlay.js +217 -0
- package/src/lib/machines/overlay.js.map +1 -0
- package/src/lib/machines/webchat.d.ts +641 -0
- package/src/lib/machines/webchat.js +768 -0
- package/src/lib/machines/webchat.js.map +1 -0
- package/src/lib/services/webchat-service.d.ts +125 -0
- package/src/lib/services/webchat-service.js +339 -0
- package/src/lib/services/webchat-service.js.map +1 -0
- package/src/lib/utils/locale-detection.d.ts +2 -0
- package/src/lib/utils/locale-detection.js +20 -0
- package/src/lib/utils/locale-detection.js.map +1 -0
- package/src/lib/utils/timezone-to-country.d.ts +1 -0
- package/src/lib/utils/timezone-to-country.js +105 -0
- package/src/lib/utils/timezone-to-country.js.map +1 -0
- package/src/lib/webview/constants.d.ts +10 -0
- package/src/lib/webview/constants.js +10 -0
- package/src/lib/webview/constants.js.map +1 -0
- package/src/lib/webview/index.d.ts +3 -0
- package/src/lib/webview/index.js +4 -0
- package/src/lib/webview/index.js.map +1 -0
- package/src/lib/webview/types.d.ts +35 -0
- package/src/lib/webview/types.js +16 -0
- package/src/lib/webview/types.js.map +1 -0
- package/src/lib/webview/utils.d.ts +9 -0
- package/src/lib/webview/utils.js +33 -0
- package/src/lib/webview/utils.js.map +1 -0
|
@@ -0,0 +1,277 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
import PusherWithEncryption from 'pusher-js/with-encryption';
|
|
3
|
+
import { ConnectionStatus } from '../../domain';
|
|
4
|
+
/**
|
|
5
|
+
* Improved platform-agnostic realtime client
|
|
6
|
+
* Handles WebSocket connections via Pusher with encryption
|
|
7
|
+
*/
|
|
8
|
+
export class RealtimeClient {
|
|
9
|
+
constructor() {
|
|
10
|
+
this.subscribedChannels = new Map();
|
|
11
|
+
this.eventCallbacks = new Map();
|
|
12
|
+
this.isInitialized = false;
|
|
13
|
+
this._connectionState = {
|
|
14
|
+
status: ConnectionStatus.Disconnected,
|
|
15
|
+
connectionAttempts: 0,
|
|
16
|
+
};
|
|
17
|
+
this.maxReconnectionAttempts = 5;
|
|
18
|
+
this.reconnectionDelay = 1000;
|
|
19
|
+
this.enableHeartbeat = true;
|
|
20
|
+
}
|
|
21
|
+
initialize(config, authContext) {
|
|
22
|
+
var _a;
|
|
23
|
+
this.authContext = authContext;
|
|
24
|
+
if (this.pusher) {
|
|
25
|
+
console.warn('RealtimeClient already initialized');
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
this.maxReconnectionAttempts = config.maxReconnectionAttempts || 5;
|
|
29
|
+
this.reconnectionDelay = config.reconnectionDelay || 1000;
|
|
30
|
+
this.enableHeartbeat = (_a = config.enableHeartbeat) !== null && _a !== void 0 ? _a : false;
|
|
31
|
+
const pusherConfig = {
|
|
32
|
+
cluster: config.pusherCluster,
|
|
33
|
+
activityTimeout: config.activityTimeout || 15000,
|
|
34
|
+
pongTimeout: config.pongTimeout || 10000,
|
|
35
|
+
authorizer: ((channel) => {
|
|
36
|
+
return {
|
|
37
|
+
authorize: (socketId, callback) => __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
try {
|
|
39
|
+
const pusherAuthResponse = yield config.customAuthorizer(socketId, channel.name, this.authContext);
|
|
40
|
+
if (config.onAuthData) {
|
|
41
|
+
config.onAuthData(pusherAuthResponse);
|
|
42
|
+
}
|
|
43
|
+
callback(null, pusherAuthResponse);
|
|
44
|
+
}
|
|
45
|
+
catch (err) {
|
|
46
|
+
callback(err instanceof Error ? err : new Error(String(err)), {
|
|
47
|
+
auth: '',
|
|
48
|
+
shared_secret: '',
|
|
49
|
+
channel_data: '',
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
}),
|
|
53
|
+
};
|
|
54
|
+
}),
|
|
55
|
+
enableStats: false,
|
|
56
|
+
forceTLS: true,
|
|
57
|
+
};
|
|
58
|
+
this.pusher = new PusherWithEncryption(config.pusherAppKey, pusherConfig);
|
|
59
|
+
this.isInitialized = true;
|
|
60
|
+
this.setupConnectionHandlers();
|
|
61
|
+
if (this.enableHeartbeat) {
|
|
62
|
+
this.startHeartbeat();
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
setupConnectionHandlers() {
|
|
66
|
+
if (!this.pusher)
|
|
67
|
+
return;
|
|
68
|
+
this.pusher.connection.bind('state_change', (states) => {
|
|
69
|
+
const newState = states.current;
|
|
70
|
+
const previousState = states.previous;
|
|
71
|
+
console.log(`🔄 Pusher state change: ${previousState} → ${newState}`);
|
|
72
|
+
this.updateConnectionState(newState);
|
|
73
|
+
if (newState === ConnectionStatus.Unavailable &&
|
|
74
|
+
previousState === ConnectionStatus.Connecting) {
|
|
75
|
+
console.log('⚠️ Connection became unavailable after connecting attempt');
|
|
76
|
+
}
|
|
77
|
+
if (newState === ConnectionStatus.Connected) {
|
|
78
|
+
this._connectionState.lastConnected = new Date();
|
|
79
|
+
this._connectionState.connectionAttempts = 0;
|
|
80
|
+
console.log('Pusher connected');
|
|
81
|
+
if (this.reconnectionTimer) {
|
|
82
|
+
clearTimeout(this.reconnectionTimer);
|
|
83
|
+
this.reconnectionTimer = undefined;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
else if (newState === ConnectionStatus.Disconnected ||
|
|
87
|
+
newState === ConnectionStatus.Failed) {
|
|
88
|
+
if (this._connectionState.connectionAttempts <
|
|
89
|
+
this.maxReconnectionAttempts) {
|
|
90
|
+
this.scheduleReconnection();
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
});
|
|
94
|
+
this.pusher.connection.bind('error', (error) => {
|
|
95
|
+
this._connectionState.lastError = error;
|
|
96
|
+
console.error('Pusher connection error:', JSON.stringify(error));
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
updateConnectionState(newState, error) {
|
|
100
|
+
const previousState = this._connectionState.status;
|
|
101
|
+
this._connectionState.status = newState;
|
|
102
|
+
if (error) {
|
|
103
|
+
this._connectionState.lastError = error;
|
|
104
|
+
}
|
|
105
|
+
if (this.onConnectionStateChange && previousState !== newState) {
|
|
106
|
+
this.onConnectionStateChange(this._connectionState);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
scheduleReconnection() {
|
|
110
|
+
if (this.reconnectionTimer)
|
|
111
|
+
return;
|
|
112
|
+
this._connectionState.connectionAttempts++;
|
|
113
|
+
const delay = this.reconnectionDelay *
|
|
114
|
+
Math.pow(2, this._connectionState.connectionAttempts - 1); // Exponential backoff
|
|
115
|
+
console.log(`Scheduling reconnection attempt ${this._connectionState.connectionAttempts} in ${delay}ms`);
|
|
116
|
+
this.reconnectionTimer = setTimeout(() => {
|
|
117
|
+
this.updateConnectionState(ConnectionStatus.Reconnecting);
|
|
118
|
+
this.attemptReconnection();
|
|
119
|
+
}, delay);
|
|
120
|
+
}
|
|
121
|
+
attemptReconnection() {
|
|
122
|
+
if (!this.pusher)
|
|
123
|
+
return;
|
|
124
|
+
console.log('Attempting to reconnect...');
|
|
125
|
+
this.pusher.connect();
|
|
126
|
+
}
|
|
127
|
+
startHeartbeat() {
|
|
128
|
+
if (this.heartbeatInterval)
|
|
129
|
+
return;
|
|
130
|
+
const heartbeatFrequency = 5000; // Every 5 seconds - good balance between speed and quota usage
|
|
131
|
+
this.heartbeatInterval = setInterval(() => {
|
|
132
|
+
if (this.isConnected && this.pusher) {
|
|
133
|
+
const connection = this.pusher.connection;
|
|
134
|
+
const isHealthy = connection.state === ConnectionStatus.Connected;
|
|
135
|
+
if (!isHealthy) {
|
|
136
|
+
console.warn('Connection health check failed - state:', connection.state);
|
|
137
|
+
if (connection.state === ConnectionStatus.Disconnected ||
|
|
138
|
+
connection.state === ConnectionStatus.Failed) {
|
|
139
|
+
this.attemptReconnection();
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
console.debug('Connection health check passed');
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
console.warn('Heartbeat detected disconnected state, attempting reconnection');
|
|
148
|
+
this.attemptReconnection();
|
|
149
|
+
}
|
|
150
|
+
}, heartbeatFrequency);
|
|
151
|
+
}
|
|
152
|
+
stopHeartbeat() {
|
|
153
|
+
if (this.heartbeatInterval) {
|
|
154
|
+
clearInterval(this.heartbeatInterval);
|
|
155
|
+
this.heartbeatInterval = undefined;
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Subscribe to a channel - matches original pattern
|
|
160
|
+
*/
|
|
161
|
+
subscribe(channelName, subscriptionCallback) {
|
|
162
|
+
if (!this.pusher) {
|
|
163
|
+
console.error('Pusher client not initialized. Call initialize() first.');
|
|
164
|
+
return undefined;
|
|
165
|
+
}
|
|
166
|
+
const subscribedChannel = this.pusher.subscribe(channelName);
|
|
167
|
+
this.subscribedChannels.set(channelName, {
|
|
168
|
+
name: channelName,
|
|
169
|
+
channel: subscribedChannel,
|
|
170
|
+
isSubscribed: false,
|
|
171
|
+
});
|
|
172
|
+
if (subscriptionCallback && typeof subscriptionCallback === 'function') {
|
|
173
|
+
subscribedChannel.bind('pusher:subscription_succeeded', () => {
|
|
174
|
+
const realtimeChannel = this.subscribedChannels.get(channelName);
|
|
175
|
+
if (realtimeChannel) {
|
|
176
|
+
realtimeChannel.isSubscribed = true;
|
|
177
|
+
}
|
|
178
|
+
subscriptionCallback();
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return subscribedChannel;
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* Bind an event to a channel - matches original pattern
|
|
185
|
+
*/
|
|
186
|
+
bind(channelName, eventName, callback) {
|
|
187
|
+
if (!this.pusher) {
|
|
188
|
+
console.error('Pusher client not initialized. Call initialize() first.');
|
|
189
|
+
return;
|
|
190
|
+
}
|
|
191
|
+
const channel = this.subscribedChannels.get(channelName) ||
|
|
192
|
+
this.pusher.channel(channelName);
|
|
193
|
+
if (!channel) {
|
|
194
|
+
console.error(`Channel "${channelName}" does not exist or has not been subscribed to.`);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const actualChannel = 'channel' in channel ? channel.channel : channel;
|
|
198
|
+
actualChannel.bind(eventName, callback);
|
|
199
|
+
}
|
|
200
|
+
/**
|
|
201
|
+
* Unbind an event from a channel - matches original pattern
|
|
202
|
+
*/
|
|
203
|
+
unbind(channelName, eventName, callback) {
|
|
204
|
+
if (!this.pusher)
|
|
205
|
+
return;
|
|
206
|
+
const channel = this.subscribedChannels.get(channelName) ||
|
|
207
|
+
this.pusher.channel(channelName);
|
|
208
|
+
if (channel) {
|
|
209
|
+
const actualChannel = 'channel' in channel ? channel.channel : channel;
|
|
210
|
+
actualChannel.unbind(eventName, callback);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
/**
|
|
214
|
+
* Bind event listener to a channel
|
|
215
|
+
*/
|
|
216
|
+
bindChannelEvent(channelName, eventName, callback) {
|
|
217
|
+
this.bind(channelName, eventName, (data) => callback(data.message));
|
|
218
|
+
}
|
|
219
|
+
/**
|
|
220
|
+
* Unbind event listener from a channel
|
|
221
|
+
*/
|
|
222
|
+
unbindChannelEvent(channelName, eventName, callback) {
|
|
223
|
+
if (callback) {
|
|
224
|
+
this.unbind(channelName, eventName, callback);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
/**
|
|
228
|
+
* Unsubscribe from a channel
|
|
229
|
+
*/
|
|
230
|
+
unsubscribeFromChannel(channelName) {
|
|
231
|
+
var _a;
|
|
232
|
+
const realtimeChannel = this.subscribedChannels.get(channelName);
|
|
233
|
+
if (realtimeChannel) {
|
|
234
|
+
(_a = this.pusher) === null || _a === void 0 ? void 0 : _a.unsubscribe(channelName);
|
|
235
|
+
this.subscribedChannels.delete(channelName);
|
|
236
|
+
this.eventCallbacks.delete(channelName);
|
|
237
|
+
console.log(`Unsubscribed from channel: ${channelName}`);
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
get connectionState() {
|
|
241
|
+
return this._connectionState.status;
|
|
242
|
+
}
|
|
243
|
+
get isConnected() {
|
|
244
|
+
return this._connectionState.status === ConnectionStatus.Connected;
|
|
245
|
+
}
|
|
246
|
+
/**
|
|
247
|
+
* Update the last message ID in auth context for reconnection
|
|
248
|
+
*/
|
|
249
|
+
updateLastMessageId(messageId) {
|
|
250
|
+
if (this.authContext) {
|
|
251
|
+
this.authContext.lastMessageId = messageId;
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
/**
|
|
255
|
+
* Disconnect and cleanup
|
|
256
|
+
*/
|
|
257
|
+
disconnect() {
|
|
258
|
+
if (this.reconnectionTimer) {
|
|
259
|
+
clearTimeout(this.reconnectionTimer);
|
|
260
|
+
this.reconnectionTimer = undefined;
|
|
261
|
+
}
|
|
262
|
+
this.stopHeartbeat();
|
|
263
|
+
if (this.pusher) {
|
|
264
|
+
this.pusher.disconnect();
|
|
265
|
+
this.subscribedChannels.clear();
|
|
266
|
+
this.eventCallbacks.clear();
|
|
267
|
+
this.isInitialized = false;
|
|
268
|
+
this.pusher = undefined;
|
|
269
|
+
this._connectionState = {
|
|
270
|
+
status: ConnectionStatus.Disconnected,
|
|
271
|
+
connectionAttempts: 0,
|
|
272
|
+
};
|
|
273
|
+
console.log('RealtimeClient disconnected and cleaned up');
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
//# sourceMappingURL=realtime.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"realtime.js","sourceRoot":"","sources":["../../../../../../../../libs/botonic/webchat-core/src/lib/infra/clients/realtime.ts"],"names":[],"mappings":";AAEA,OAAO,oBAAoB,MAAM,2BAA2B,CAAA;AAE5D,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAA;AAyC/C;;;GAGG;AACH,MAAM,OAAO,cAAc;IAA3B;QAIU,uBAAkB,GAAiC,IAAI,GAAG,EAAE,CAAA;QAC5D,mBAAc,GACpB,IAAI,GAAG,EAAE,CAAA;QACJ,kBAAa,GAAG,KAAK,CAAA;QAEpB,qBAAgB,GAAoB;YAC1C,MAAM,EAAE,gBAAgB,CAAC,YAAY;YACrC,kBAAkB,EAAE,CAAC;SACtB,CAAA;QAEO,4BAAuB,GAAG,CAAC,CAAA;QAC3B,sBAAiB,GAAG,IAAI,CAAA;QAGxB,oBAAe,GAAG,IAAI,CAAA;IAqWhC,CAAC;IAjWC,UAAU,CACR,MAA4B,EAC5B,WAAwC;;QAExC,IAAI,CAAC,WAAW,GAAG,WAAW,CAAA;QAE9B,IAAI,IAAI,CAAC,MAAM,EAAE,CAAC;YAChB,OAAO,CAAC,IAAI,CAAC,oCAAoC,CAAC,CAAA;YAClD,OAAM;QACR,CAAC;QAED,IAAI,CAAC,uBAAuB,GAAG,MAAM,CAAC,uBAAuB,IAAI,CAAC,CAAA;QAClE,IAAI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YAC3B,IAAI,CAAC,aAAa,GAAG,KAAK,CAAA;YAC1B,IAAI,CAAC,MAAM,GAAG,SAAS,CAAA;YAEvB,IAAI,CAAC,gBAAgB,GAAG;gBACtB,MAAM,EAAE,gBAAgB,CAAC,YAAY;gBACrC,kBAAkB,EAAE,CAAC;aACtB,CAAA;YAED,OAAO,CAAC,GAAG,CAAC,4CAA4C,CAAC,CAAA;QAC3D,CAAC;IACH,CAAC;CACF"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import type { WebchatRealtimeAuthContext } from './hubtype-api';
|
|
2
|
+
export interface PusherAuthResponse {
|
|
3
|
+
auth: string;
|
|
4
|
+
channel_data: string;
|
|
5
|
+
shared_secret?: string;
|
|
6
|
+
}
|
|
7
|
+
export interface PusherAuthParsedChannelData {
|
|
8
|
+
botId: string;
|
|
9
|
+
chatId: string;
|
|
10
|
+
}
|
|
11
|
+
export type PusherCustomAuthorizerFunction = (socketId: string, channelName: string, authContext?: WebchatRealtimeAuthContext) => Promise<PusherAuthResponse>;
|
|
12
|
+
/** Matches backend _PusherAppSerializer (key, cluster, status_channel) */
|
|
13
|
+
export interface PusherAppSettings {
|
|
14
|
+
key: string;
|
|
15
|
+
cluster: string;
|
|
16
|
+
status_channel?: string;
|
|
17
|
+
}
|
|
18
|
+
/** Matches backend WebchatSettingsSerializer response */
|
|
19
|
+
export interface WebchatSettingsResponse {
|
|
20
|
+
pusher: PusherAppSettings;
|
|
21
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../../../../../../../libs/botonic/webchat-core/src/lib/infra/clients/types.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './storage';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAA"}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { type HubtypeUser } from '@botonic/shared';
|
|
2
|
+
import { ConversationData, StorageRepository, StoredMessage } from './types';
|
|
3
|
+
export declare abstract class BaseStorageRepository implements StorageRepository {
|
|
4
|
+
protected keyPrefix: string;
|
|
5
|
+
constructor(keyPrefix?: string);
|
|
6
|
+
protected getConversationKey(appId: string, userId: string): string;
|
|
7
|
+
protected getAppStoragePrefix(appId: string): string;
|
|
8
|
+
protected abstract getData(key: string): ConversationData | null;
|
|
9
|
+
protected abstract setData(key: string, data: ConversationData): void;
|
|
10
|
+
protected abstract deleteData(key: string): void;
|
|
11
|
+
protected abstract getKeysWithPrefix(prefix: string): string[];
|
|
12
|
+
protected getConversationData(appId: string, userId: string): ConversationData;
|
|
13
|
+
protected saveConversationData(appId: string, userId: string, data: ConversationData): void;
|
|
14
|
+
saveMessage(appId: string, userId: string, message: StoredMessage): Promise<void>;
|
|
15
|
+
saveAllMessages(appId: string, userId: string, messages: StoredMessage[]): Promise<void>;
|
|
16
|
+
findMessagesByConversation(appId: string, userId: string): Promise<StoredMessage[]>;
|
|
17
|
+
saveUser(appId: string, user: HubtypeUser): Promise<void>;
|
|
18
|
+
findUserById(appId: string, userId: string): Promise<HubtypeUser | null>;
|
|
19
|
+
saveLastUserSession(appId: string, user: HubtypeUser): Promise<void>;
|
|
20
|
+
findLastUserSession(appId: string): Promise<HubtypeUser | null>;
|
|
21
|
+
clearConversation(appId: string, userId: string): Promise<void>;
|
|
22
|
+
clearAppData(appId: string): Promise<void>;
|
|
23
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { __awaiter } from "tslib";
|
|
2
|
+
const DEFAULT_KEY_PREFIX = 'botonic-webchat';
|
|
3
|
+
export class BaseStorageRepository {
|
|
4
|
+
constructor(keyPrefix = DEFAULT_KEY_PREFIX) {
|
|
5
|
+
this.keyPrefix = keyPrefix;
|
|
6
|
+
}
|
|
7
|
+
getConversationKey(appId, userId) {
|
|
8
|
+
return `${this.keyPrefix}:${appId}:${userId}`;
|
|
9
|
+
}
|
|
10
|
+
getAppStoragePrefix(appId) {
|
|
11
|
+
return `${this.keyPrefix}:${appId}:`;
|
|
12
|
+
}
|
|
13
|
+
getConversationData(appId, userId) {
|
|
14
|
+
const key = this.getConversationKey(appId, userId);
|
|
15
|
+
const data = this.getData(key);
|
|
16
|
+
return data !== null && data !== void 0 ? data : { messages: [], user: null };
|
|
17
|
+
}
|
|
18
|
+
saveConversationData(appId, userId, data) {
|
|
19
|
+
const key = this.getConversationKey(appId, userId);
|
|
20
|
+
this.setData(key, data);
|
|
21
|
+
}
|
|
22
|
+
saveMessage(appId, userId, message) {
|
|
23
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
24
|
+
const data = this.getConversationData(appId, userId);
|
|
25
|
+
const existingIndex = data.messages.findIndex(sm => sm.id === message.id);
|
|
26
|
+
if (existingIndex >= 0) {
|
|
27
|
+
data.messages[existingIndex] = message;
|
|
28
|
+
}
|
|
29
|
+
else {
|
|
30
|
+
data.messages.push(message);
|
|
31
|
+
}
|
|
32
|
+
data.messages.sort((a, b) => b.id.localeCompare(a.id));
|
|
33
|
+
this.saveConversationData(appId, userId, data);
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
saveAllMessages(appId, userId, messages) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const data = this.getConversationData(appId, userId);
|
|
39
|
+
data.messages = messages;
|
|
40
|
+
this.saveConversationData(appId, userId, data);
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
findMessagesByConversation(appId, userId) {
|
|
44
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
45
|
+
const data = this.getConversationData(appId, userId);
|
|
46
|
+
return data.messages;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
saveUser(appId, user) {
|
|
50
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
51
|
+
const data = this.getConversationData(appId, user.id);
|
|
52
|
+
const webchatUser = Object.assign(Object.assign({}, user), { lastActiveAt: new Date().toISOString() });
|
|
53
|
+
data.user = webchatUser;
|
|
54
|
+
this.saveConversationData(appId, user.id, data);
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
findUserById(appId, userId) {
|
|
58
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
const data = this.getConversationData(appId, userId);
|
|
60
|
+
return data.user;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
saveLastUserSession(appId, user) {
|
|
64
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
65
|
+
return this.saveUser(appId, user);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
findLastUserSession(appId) {
|
|
69
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
70
|
+
var _a;
|
|
71
|
+
const prefix = this.getAppStoragePrefix(appId);
|
|
72
|
+
const keys = this.getKeysWithPrefix(prefix);
|
|
73
|
+
let mostRecentUser = null;
|
|
74
|
+
let mostRecentTimestamp = null;
|
|
75
|
+
for (const key of keys) {
|
|
76
|
+
const data = this.getData(key);
|
|
77
|
+
if ((_a = data === null || data === void 0 ? void 0 : data.user) === null || _a === void 0 ? void 0 : _a.lastActiveAt) {
|
|
78
|
+
if (!mostRecentTimestamp ||
|
|
79
|
+
new Date(data.user.lastActiveAt) > new Date(mostRecentTimestamp)) {
|
|
80
|
+
mostRecentUser = data.user;
|
|
81
|
+
mostRecentTimestamp = data.user.lastActiveAt;
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return mostRecentUser;
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
clearConversation(appId, userId) {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
const key = this.getConversationKey(appId, userId);
|
|
91
|
+
this.deleteData(key);
|
|
92
|
+
});
|
|
93
|
+
}
|
|
94
|
+
clearAppData(appId) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
const prefix = this.getAppStoragePrefix(appId);
|
|
97
|
+
const keys = this.getKeysWithPrefix(prefix);
|
|
98
|
+
keys.forEach(key => this.deleteData(key));
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
//# sourceMappingURL=base-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"base-storage.js","sourceRoot":"","sources":["../../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/storage/base-storage.ts"],"names":[],"mappings":";AASA,MAAM,kBAAkB,GAAG,iBAAiB,CAAA;AAE5C,MAAM,OAAgB,qBAAqB;IAGzC,YAAY,SAAS,GAAG,kBAAkB;QACxC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAA;IAC5B,CAAC;IAES,kBAAkB,CAAC,KAAa,EAAE,MAAc;QACxD,OAAO,GAAG,IAAI,CAAC,SAAS,IAAI,KAAK,IAAI,MAAM,EAAE,CAAA;IAC/C,CAAC;IAES,mBAAmB,CAAC,KAAa;QACzC,OAAO,GAAG,IAAI,CAAC,SAAS,IAAI,KAAK,GAAG,CAAA;IACtC,CAAC;IAOS,mBAAmB,CAC3B,KAAa,EACb,MAAc;QAEd,MAAM,GAAG,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAClD,MAAM,IAAI,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAC9B,OAAO,IAAI,aAAJ,IAAI,cAAJ,IAAI,GAAI,EAAE,QAAQ,EAAE,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAA;IAC7C,CAAC;IAES,oBAAoB,CAC5B,KAAa,EACb,MAAc,EACd,IAAsB;QAEtB,MAAM,GAAG,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;QAClD,IAAI,CAAC,OAAO,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;IAC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AI,CAAC,IAAI,CAAA;wBAC1B,mBAAmB,GAAG,IAAI,CAAC,IAAI,CAAC,YAAY,CAAA;oBAC9C,CAAC;gBACH,CAAC;YACH,CAAC;YAED,OAAO,cAAc,CAAA;QACvB,CAAC;KAAA;IAEK,iBAAiB,CAAC,KAAa,EAAE,MAAc;;YACnD,MAAM,GAAG,GAAG,IAAI,CAAC,kBAAkB,CAAC,KAAK,EAAE,MAAM,CAAC,CAAA;YAClD,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAA;QACtB,CAAC;KAAA;IAEK,YAAY,CAAC,KAAa;;YAC9B,MAAM,MAAM,GAAG,IAAI,CAAC,mBAAmB,CAAC,KAAK,CAAC,CAAA;YAC9C,MAAM,IAAI,GAAG,IAAI,CAAC,iBAAiB,CAAC,MAAM,CAAC,CAAA;YAC3C,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE,CAAC,IAAI,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,CAAA;QAC3C,CAAC;KAAA;CACF"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseStorageRepository } from './base-storage';
|
|
2
|
+
import { ConversationData } from './types';
|
|
3
|
+
export declare abstract class BrowserStorageRepository extends BaseStorageRepository {
|
|
4
|
+
protected abstract get storage(): Storage;
|
|
5
|
+
protected getData(key: string): ConversationData | null;
|
|
6
|
+
protected setData(key: string, data: ConversationData): void;
|
|
7
|
+
protected deleteData(key: string): void;
|
|
8
|
+
protected getKeysWithPrefix(prefix: string): string[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { BaseStorageRepository } from './base-storage';
|
|
2
|
+
export class BrowserStorageRepository extends BaseStorageRepository {
|
|
3
|
+
getData(key) {
|
|
4
|
+
const stored = this.storage.getItem(key);
|
|
5
|
+
if (stored) {
|
|
6
|
+
try {
|
|
7
|
+
const parsed = JSON.parse(stored);
|
|
8
|
+
return {
|
|
9
|
+
messages: Array.isArray(parsed.messages) ? parsed.messages : [],
|
|
10
|
+
user: parsed.user || null,
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
console.error('Failed to parse conversation data:', error);
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
setData(key, data) {
|
|
20
|
+
try {
|
|
21
|
+
this.storage.setItem(key, JSON.stringify(data));
|
|
22
|
+
}
|
|
23
|
+
catch (error) {
|
|
24
|
+
const isQuotaError = error instanceof Error &&
|
|
25
|
+
(error.name === 'QuotaExceededError' || error.message.includes('quota'));
|
|
26
|
+
if (isQuotaError) {
|
|
27
|
+
console.warn('Storage quota exceeded, unable to save conversation data');
|
|
28
|
+
}
|
|
29
|
+
throw error;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
deleteData(key) {
|
|
33
|
+
this.storage.removeItem(key);
|
|
34
|
+
}
|
|
35
|
+
getKeysWithPrefix(prefix) {
|
|
36
|
+
const keys = [];
|
|
37
|
+
for (let i = 0; i < this.storage.length; i++) {
|
|
38
|
+
const key = this.storage.key(i);
|
|
39
|
+
if (key && key.startsWith(prefix)) {
|
|
40
|
+
keys.push(key);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
return keys;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
//# sourceMappingURL=browser-storage.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"browser-storage.js","sourceRoot":"","sources":["../../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/storage/browser-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAGtD,MAAM,OAAgB,wBAAyB,SAAQ,qBAAqB;IAGhE,OAAO,CAAC,GAAW;QAC3B,MAAM,MAAM,GAAG,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,GAAG,CAAC,CAAA;QAExC,IAAI,MAAM,EAAE,CAA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,GAAG,GAAG,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC,CAAC,CAAA;YAC/B,IAAI,GAAG,IAAI,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;gBAClC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAA;YAChB,CAAC;QACH,CAAC;QACD,OAAO,IAAI,CAAA;IACb,CAAC;CACF"}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { InMemoryRepository } from './in-memory';
|
|
2
|
+
import { LocalStorageRepository } from './local-storage';
|
|
3
|
+
import { SessionStorageRepository } from './session-storage';
|
|
4
|
+
export function createStorageRepository(config) {
|
|
5
|
+
const { type = 'localStorage', keyPrefix = 'botonic-webchat' } = config || {};
|
|
6
|
+
if (type === 'inMemory') {
|
|
7
|
+
return new InMemoryRepository();
|
|
8
|
+
}
|
|
9
|
+
if (typeof window === 'undefined') {
|
|
10
|
+
return new InMemoryRepository();
|
|
11
|
+
}
|
|
12
|
+
if (type === 'sessionStorage' && window.sessionStorage) {
|
|
13
|
+
return new SessionStorageRepository(keyPrefix);
|
|
14
|
+
}
|
|
15
|
+
if (type === 'localStorage' && window.localStorage) {
|
|
16
|
+
return new LocalStorageRepository(keyPrefix);
|
|
17
|
+
}
|
|
18
|
+
return new InMemoryRepository();
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=factory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"factory.js","sourceRoot":"","sources":["../../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/storage/factory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;AAG5D,MAAM,UAAU,uBAAuB,CACrC,MAAsB;IAEtB,MAAM,EAAE,IAAI,GAAG,cAAc,EAAE,SAAS,GAAG,iBAAiB,EAAE,GAAG,MAAM,IAAI,EAAE,CAAA;IAE7E,IAAI,IAAI,KAAK,UAAU,EAAE,CAAC;QACxB,OAAO,IAAI,kBAAkB,EAAE,CAAA;IACjC,CAAC;IAED,IAAI,OAAO,MAAM,KAAK,WAAW,EAAE,CAAC;QAClC,OAAO,IAAI,kBAAkB,EAAE,CAAA;IACjC,CAAC;IAED,IAAI,IAAI,KAAK,gBAAgB,IAAI,MAAM,CAAC,cAAc,EAAE,CAAC;QACvD,OAAO,IAAI,wBAAwB,CAAC,SAAS,CAAC,CAAA;IAChD,CAAC;IAED,IAAI,IAAI,KAAK,cAAc,IAAI,MAAM,CAAC,YAAY,EAAE,CAAC;QACnD,OAAO,IAAI,sBAAsB,CAAC,SAAS,CAAC,CAAA;IAC9C,CAAC;IAED,OAAO,IAAI,kBAAkB,EAAE,CAAA;AACjC,CAAC"}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { BaseStorageRepository } from './base-storage';
|
|
2
|
+
import { ConversationData } from './types';
|
|
3
|
+
export declare class InMemoryRepository extends BaseStorageRepository {
|
|
4
|
+
private storage;
|
|
5
|
+
protected getData(key: string): ConversationData | null;
|
|
6
|
+
protected setData(key: string, data: ConversationData): void;
|
|
7
|
+
protected deleteData(key: string): void;
|
|
8
|
+
protected getKeysWithPrefix(prefix: string): string[];
|
|
9
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { BaseStorageRepository } from './base-storage';
|
|
2
|
+
export class InMemoryRepository extends BaseStorageRepository {
|
|
3
|
+
constructor() {
|
|
4
|
+
super(...arguments);
|
|
5
|
+
this.storage = new Map();
|
|
6
|
+
}
|
|
7
|
+
getData(key) {
|
|
8
|
+
var _a;
|
|
9
|
+
return (_a = this.storage.get(key)) !== null && _a !== void 0 ? _a : null;
|
|
10
|
+
}
|
|
11
|
+
setData(key, data) {
|
|
12
|
+
this.storage.set(key, data);
|
|
13
|
+
}
|
|
14
|
+
deleteData(key) {
|
|
15
|
+
this.storage.delete(key);
|
|
16
|
+
}
|
|
17
|
+
getKeysWithPrefix(prefix) {
|
|
18
|
+
return Array.from(this.storage.keys()).filter(key => key.startsWith(prefix));
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=in-memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"in-memory.js","sourceRoot":"","sources":["../../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/storage/in-memory.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AAGtD,MAAM,OAAO,kBAAmB,SAAQ,qBAAqB;IAA7D;;QACU,YAAO,GAAG,IAAI,GAAG,EAA4B,CAAA;IAiBvD,CAAC;IAfW,OAAO,CAAC,GAAW;;QAC3B,OAAO,MAAA,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,mCAAI,IAAI,CAAA;IACtC,CAAC;IAES,OAAO,CAAC,GAAW,EAAE,IAAsB;QACnD,IAAI,CAAC,OAAO,CAAC,GAAG,CAAC,GAAG,EAAE,IAAI,CAAC,CAAA;IAC7B,CAAC;IAES,UAAU,CAAC,GAAW;QAC9B,IAAI,CAAC,OAAO,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;IAC1B,CAAC;IAES,iBAAiB,CAAC,MAAc;QACxC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,OAAO,CAAC,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAA;IAC9E,CAAC;CACF"}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { BaseStorageRepository } from './base-storage';
|
|
2
|
+
export { BrowserStorageRepository } from './browser-storage';
|
|
3
|
+
export { createStorageRepository } from './factory';
|
|
4
|
+
export { InMemoryRepository } from './in-memory';
|
|
5
|
+
export { LocalStorageRepository } from './local-storage';
|
|
6
|
+
export { SessionStorageRepository } from './session-storage';
|
|
7
|
+
export type { ConversationData, StorageConfig, StorageRepository, StorageType, StoredMessage, WebchatHubtypeUser, } from './types';
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { BaseStorageRepository } from './base-storage';
|
|
2
|
+
export { BrowserStorageRepository } from './browser-storage';
|
|
3
|
+
export { createStorageRepository } from './factory';
|
|
4
|
+
export { InMemoryRepository } from './in-memory';
|
|
5
|
+
export { LocalStorageRepository } from './local-storage';
|
|
6
|
+
export { SessionStorageRepository } from './session-storage';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/storage/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,gBAAgB,CAAA;AACtD,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;AAC5D,OAAO,EAAE,uBAAuB,EAAE,MAAM,WAAW,CAAA;AACnD,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAA;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,iBAAiB,CAAA;AACxD,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"local-storage.js","sourceRoot":"","sources":["../../../../../../../../../libs/botonic/webchat-core/src/lib/infra/repositories/storage/local-storage.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,wBAAwB,EAAE,MAAM,mBAAmB,CAAA;AAE5D,MAAM,OAAO,sBAAuB,SAAQ,wBAAwB;IAClE,IAAc,OAAO;QACnB,OAAO,YAAY,CAAA;IACrB,CAAC;CACF"}
|