@rws-framework/client 2.0.6

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.
Files changed (116) hide show
  1. package/.bin/add-v.sh +10 -0
  2. package/.eslintrc.json +53 -0
  3. package/.setup/.eslintrc.json +11 -0
  4. package/.setup/tsconfig.json +29 -0
  5. package/README.md +691 -0
  6. package/_rws_externals.js +40 -0
  7. package/_tools.js +317 -0
  8. package/console.js +87 -0
  9. package/declarations.d.ts +9 -0
  10. package/docs/.nojekyll +1 -0
  11. package/docs/assets/26e93147f10415a0ed4a.svg +6 -0
  12. package/docs/assets/75c9471662e97ee24f29.svg +7 -0
  13. package/docs/assets/db90e4df2373980c497d.svg +9 -0
  14. package/docs/assets/hierarchy.css +1 -0
  15. package/docs/assets/hierarchy.js +1 -0
  16. package/docs/assets/highlight.css +120 -0
  17. package/docs/assets/main.js +59 -0
  18. package/docs/assets/navigation.js +1 -0
  19. package/docs/assets/search.js +1 -0
  20. package/docs/assets/style.css +1414 -0
  21. package/docs/classes/ApiServiceInstance.html +20 -0
  22. package/docs/classes/DOMServiceInstance.html +10 -0
  23. package/docs/classes/NotifyServiceInstance.html +12 -0
  24. package/docs/classes/RWSClient.html +11 -0
  25. package/docs/classes/RWSRouter.html +9 -0
  26. package/docs/classes/RWSService.html +7 -0
  27. package/docs/classes/RWSUploader.html +35 -0
  28. package/docs/classes/RWSViewComponent.html +24 -0
  29. package/docs/classes/RouterComponent.html +31 -0
  30. package/docs/classes/RoutingServiceInstance.html +13 -0
  31. package/docs/classes/UtilsServiceInstance.html +11 -0
  32. package/docs/classes/WSServiceInstance.html +32 -0
  33. package/docs/functions/RWSView.html +1 -0
  34. package/docs/functions/ngAttr.html +1 -0
  35. package/docs/functions/registerRWSComponents.html +1 -0
  36. package/docs/functions/renderRouteComponent.html +1 -0
  37. package/docs/hierarchy.html +1 -0
  38. package/docs/index.html +53 -0
  39. package/docs/interfaces/IRWSAssetShowOptions.html +1 -0
  40. package/docs/interfaces/IRWSConfig.html +11 -0
  41. package/docs/interfaces/IRWSDecoratorOptions.html +4 -0
  42. package/docs/interfaces/IRWSHttpRoute.html +3 -0
  43. package/docs/interfaces/IRWSPrefixedHTTProutes.html +3 -0
  44. package/docs/interfaces/IRoutingEvent.html +3 -0
  45. package/docs/modules.html +39 -0
  46. package/docs/types/DOMOutputType.html +1 -0
  47. package/docs/types/IRWSBackendRoute.html +1 -0
  48. package/docs/types/IRWSFrontRoutes.html +1 -0
  49. package/docs/types/IRWSRouteResult.html +1 -0
  50. package/docs/types/IRWSWebsocketStatus.html +1 -0
  51. package/docs/types/NotifyLogType.html +1 -0
  52. package/docs/types/NotifyUiType.html +1 -0
  53. package/docs/types/RWSNotify.html +1 -0
  54. package/docs/types/RouteReturn.html +1 -0
  55. package/docs/variables/ApiService.html +1 -0
  56. package/docs/variables/DOMService.html +1 -0
  57. package/docs/variables/NotifyService.html +1 -0
  58. package/docs/variables/RoutingService.html +1 -0
  59. package/docs/variables/UtilsService.html +1 -0
  60. package/docs/variables/WSService.html +1 -0
  61. package/docs/variables/_ROUTING_EVENT_NAME.html +1 -0
  62. package/package.json +93 -0
  63. package/package.webpack.config.js +17 -0
  64. package/rws.webpack.config.js +260 -0
  65. package/src/client.ts +304 -0
  66. package/src/components/_attrs/angular-attr.ts +64 -0
  67. package/src/components/_attrs/sanitize-html.ts +81 -0
  68. package/src/components/_component.ts +290 -0
  69. package/src/components/_container.ts +16 -0
  70. package/src/components/_decorator.ts +112 -0
  71. package/src/components/_design_system.ts +6 -0
  72. package/src/components/index.ts +9 -0
  73. package/src/components/progress/component.ts +55 -0
  74. package/src/components/progress/styles/layout.scss +91 -0
  75. package/src/components/progress/template.html +25 -0
  76. package/src/components/router/component.ts +67 -0
  77. package/src/components/uploader/component.ts +75 -0
  78. package/src/components/uploader/styles/layout.scss +105 -0
  79. package/src/components/uploader/template.html +17 -0
  80. package/src/hmr.ts +19 -0
  81. package/src/index.ts +98 -0
  82. package/src/interfaces/IRWSConfig.ts +24 -0
  83. package/src/interfaces/IRWSUser.ts +7 -0
  84. package/src/interfaces/IRWSViewComponent.ts +37 -0
  85. package/src/interfaces/RWSWindow.ts +39 -0
  86. package/src/routing/_router.ts +59 -0
  87. package/src/routing/index.ts +7 -0
  88. package/src/run.ts +35 -0
  89. package/src/service_worker/src/_service_worker.ts +88 -0
  90. package/src/service_worker/tsconfig.json +21 -0
  91. package/src/service_worker/webpack.config.js +67 -0
  92. package/src/services/ApiService.ts +241 -0
  93. package/src/services/ConfigService.ts +107 -0
  94. package/src/services/DOMService.ts +91 -0
  95. package/src/services/NotifyService.ts +49 -0
  96. package/src/services/RoutingService.ts +46 -0
  97. package/src/services/ServiceWorkerService.ts +60 -0
  98. package/src/services/UtilsService.ts +103 -0
  99. package/src/services/WSService.ts +218 -0
  100. package/src/services/_service.ts +50 -0
  101. package/src/services/_ws_handlers/ConnectionHandler.ts +52 -0
  102. package/src/services/_ws_handlers/EventHandler.ts +28 -0
  103. package/src/services/_ws_handlers/MessageHandler.ts +49 -0
  104. package/src/styles/includes.scss +102 -0
  105. package/src/types/RWSNotify.ts +7 -0
  106. package/tsconfig.json +27 -0
  107. package/typedoc.json +13 -0
  108. package/webpack/after/copy.js +73 -0
  109. package/webpack/after/sw.js +14 -0
  110. package/webpack/index.js +11 -0
  111. package/webpack/rws_after_plugin.js +80 -0
  112. package/webpack/rws_fast_css_loader.js +16 -0
  113. package/webpack/rws_fast_html_loader.js +4 -0
  114. package/webpack/rws_fast_scss_loader.js +50 -0
  115. package/webpack/rws_fast_ts_loader.js +134 -0
  116. package/webpack/rws_plugin.js +337 -0
package/README.md ADDED
@@ -0,0 +1,691 @@
1
+ # RWS Frontend Framework README
2
+
3
+ ## Table of Contents
4
+
5
+ 1. [Overview](#overview)
6
+ 2. [Getting Started](#getting-started)
7
+ 3. [Key Components: RWSClient & RoutingService](#key-components-rwsclient--routingservice)
8
+ 4. [Component Initialization](#component-initialization)
9
+ 5. [Frontend routes](#frontend-routes)
10
+ 6. [Backend Imports](#backend-imports)
11
+ 7. [Utilizing APIService](#utilizing-apiservice)
12
+ 8. [Notifier](#notifier)
13
+ 9. [Service Worker](#service-worker)
14
+ 10. [Example: WebChat Component](#example-webchat-component)
15
+ 11. [Links](#links)
16
+
17
+ ## Overview
18
+
19
+ The RWS Frontend Framework is designed to create dynamic and responsive web applications. It integrates seamlessly with the backend and provides a robust set of tools for developing comprehensive web solutions.
20
+
21
+ ## Getting Started
22
+
23
+ To get started with the RWS Frontend Framework, ensure you have the necessary environment set up, including Node.js and any other dependencies specific to the framework.
24
+
25
+ from your project dir do:
26
+
27
+ ```bash
28
+ yarn
29
+ ```
30
+
31
+ Initiate cfg files:
32
+ ```bash
33
+ rws-client init
34
+ ```
35
+
36
+ to install once and then to build after preparing compionents:
37
+
38
+ ```bash
39
+ yarn build
40
+ ```
41
+ or to watch for dev
42
+
43
+ ```bash
44
+ yarn watch
45
+ ```
46
+ then start engine in the site javascript (can be inline):
47
+
48
+ ```javascript
49
+ window.RWSClient.start(CFG);
50
+ ```
51
+
52
+ example config with interface:
53
+
54
+ ```javascript
55
+ const CFG = {
56
+ backendUrl: 'http://localhost:1337',
57
+ wsUrl: 'http://localhost:1338'
58
+ }
59
+ ```
60
+
61
+ ```typescript
62
+ export default interface IRWSConfig {
63
+ defaultLayout?: typeof RWSViewComponent;
64
+ backendUrl?: string,
65
+ wsUrl?: string,
66
+ backendRoutes?: any[] // routes from backend
67
+ apiPrefix?: string // f.e /api after host
68
+ routes?: IFrontRoutes, //override front routes
69
+ transports?: string[], //ws transports setup
70
+ user?: any, //user data if logged
71
+ ignoreRWSComponents?: boolean //do not register base RWS components
72
+ }
73
+ ```
74
+
75
+ ## Key Components
76
+
77
+ ### RWSClient
78
+ ##
79
+ `RWSClient` is the heart of the framework, managing configuration and initialization. It sets up routes, backend connections, and other essential framework services.
80
+
81
+
82
+ ### RoutingService
83
+ ##
84
+ `RoutingService` handles the navigation and routing within your application. It ensures that URL changes reflect the correct component rendering.
85
+
86
+ Implementing the Framework
87
+
88
+ **Main File:**
89
+
90
+ The main file (`index.ts`) is where you initialize the RWSClient. Here, you configure your routes, backend routes, and component initializations.
91
+
92
+ Following is example of full usage of the framework
93
+
94
+ ```typescript
95
+ import RWSClient, { NotifyUiType, NotifyLogType } from 'rws-js-client';
96
+ //@ts-ignore
97
+ import alertify from 'alertifyjs';
98
+
99
+ import './styles/main.scss';
100
+
101
+ import routes from './routing/routes';
102
+
103
+ import { backendRoutes } from './backendImport';
104
+
105
+ import initComponents from './application/_initComponents';
106
+ import { provideFASTDesignSystem, allComponents } from '@microsoft/fast-components';
107
+
108
+ async function initializeApp() {
109
+ const theClient = new RWSClient();
110
+
111
+ theClient.setBackendRoutes(backendRoutes());
112
+ theClient.addRoutes(routes);
113
+
114
+ theClient.onInit(async () => {
115
+ initComponents();
116
+ provideFASTDesignSystem().register(allComponents);
117
+ });
118
+
119
+
120
+ theClient.setNotifier((message: string, logType: NotifyLogType, uiType: NotifyUiType = 'notification', onConfirm: (params: any) => void) => {
121
+ switch(uiType){
122
+ case 'notification':
123
+ let notifType = 'success';
124
+
125
+ if(logType === 'error'){
126
+ notifType = 'error';
127
+ }
128
+
129
+ if(logType === 'warning'){
130
+ notifType = 'warning';
131
+ }
132
+
133
+ alertify.notify(message, notifType, 5, onConfirm);
134
+ return;
135
+ case 'alert':
136
+ alertify.alert('Junction AI Notification', message, onConfirm);
137
+ return;
138
+ case 'silent':
139
+ if(logType == 'warning'){
140
+ console.warn(message);
141
+ }else if(logType == 'error'){
142
+ console.error(message);
143
+ }else{
144
+ console.log(message);
145
+ }
146
+ return;
147
+ }
148
+ });
149
+ (window as any).RWSClient = theClient;
150
+ }
151
+
152
+ initializeApp().catch(console.error);
153
+
154
+ ```
155
+
156
+ ## Component Initialization
157
+
158
+ In `application/_initComponents.ts`, you initialize the custom components used in your application. If components added in here will include other components they dont need to be listed here. A component imported in this mode needs to be imported once.
159
+
160
+ ### Default component structure
161
+
162
+ ```
163
+ component-dir/
164
+ component.ts
165
+ template.html
166
+ styles/
167
+ layout.scss
168
+ ```
169
+
170
+ **WARNING** *All html templates refer to variable "T" as to FASTElement templating html scope. It contains all the functions FAST templates uses in html. F.e:* **T.html**, **T.when**, **T.repeat**
171
+
172
+ ```html
173
+ <div class="convo-area-wrap">
174
+ <header>
175
+ <div class="header-inner"></div>
176
+ ${T.when(x => x.noChoose === 'false', (item, index) => T.html`<div>
177
+ <chat-convo-models :chosenModel="${x => x.chosenModel}"></chat-convo-models>
178
+ </div>`)}
179
+ <div>
180
+ <h2>${ x => x.chatContext ? x.chatContext.label : 'loading...' }</h2>
181
+ <h3><strong>${ x => x.messageList.length }</strong> messages in total</h3>
182
+ </div>
183
+ <fast-divider></fast-divider>
184
+ </header>
185
+ <section>
186
+ <div class="scroll-area">
187
+ <div class="scroll-content">
188
+ ${T.repeat(x => x.messageList, (item, index) => T.html`
189
+ <chat-convo-message :contentReturn="${item => item}" :item="${item => item}"/>
190
+ `)}
191
+
192
+ ${T.when(x => !x.messageList.length, (item, index) => T.html`
193
+ <p class="no-chat">No messages</p>
194
+ `)}
195
+ </div>
196
+ </div>
197
+ </section>
198
+
199
+ </div>
200
+ ```
201
+
202
+ ### application/_initComponents.ts
203
+
204
+ ```typescript
205
+ import { ChatNav } from '../components/chat-nav/component';
206
+ import { DefaultLayout } from '../components/default-layout/component';
207
+ import { RWSIcon } from '../components/rws-icon/component';
208
+ import { Loader } from '../components/loader/component';
209
+ import { LineSplitter } from '../components/line-splitter/component';
210
+
211
+ import { registerRWSComponents } from 'rws-js-client';
212
+
213
+ export default () => {
214
+ LineSplitter;
215
+ DefaultLayout;
216
+ ChatNav;
217
+ RWSIcon;
218
+ Loader;
219
+ registerRWSComponents(); //register rws components like <rws-uploader> and other comfy components
220
+ }
221
+
222
+ ```
223
+
224
+ ```typescript
225
+ //index.ts
226
+
227
+ const theClient = new RWSClient();
228
+
229
+
230
+ theClient.addRoutes(routes); //routes are optional
231
+
232
+ theClient.onInit(async () => {
233
+ initComponents(); //user components from _initComponents.ts
234
+ provideFASTDesignSystem().register(allComponents); // @microsoft/fast-components ready components init
235
+ });
236
+
237
+ ```
238
+
239
+ **Component needs to extend RWSViewComponent and use @RWSView decorator**:
240
+
241
+ ```typescript
242
+ import { RWSViewComponent, RWSView, observable, attr } from 'rws-js-client';
243
+
244
+ const options?: RWSDecoratorOptions;
245
+
246
+ @RWSView('tag-name', options)
247
+ class WebChat extends RWSViewComponent {
248
+ ```
249
+
250
+ The decorator options type:
251
+
252
+ ```typescript
253
+ interface RWSDecoratorOptions{
254
+ template?: string, //relative path to HTML template file (default: ./template.html)
255
+ styles?: string //relative path to SCSS file (./styles/layout.scss)
256
+ fastElementOptions?: any //the stuff you would insert into static definition in FASTElement class.
257
+ }
258
+ ```
259
+
260
+ ## Frontend routes
261
+
262
+ if you are passing routes this is example routing file for frontend:
263
+
264
+ ```typescript
265
+ export default {
266
+ '/': renderRouteComponent('Home page', WebChat),
267
+ '/the/path': renderRouteComponent('Component title', ComponentClassName),
268
+ }
269
+ ```
270
+
271
+ Router tag:
272
+
273
+ ```html
274
+ <section>
275
+ <rws-router></rws-router>
276
+ </section>
277
+ ```
278
+
279
+ ## Backend Imports
280
+
281
+ `backendImports.ts` consolidates various backend interfaces, routes, and models, allowing for a synchronized frontend and backend.
282
+
283
+ ```typescript
284
+ import IBook from '../../backend/src/models/interfaces/IBook';
285
+
286
+ import {
287
+ IBookInfo,
288
+ } from '../../backend/src/interfaces/IBookInfo';
289
+
290
+ import backendRoutes from '../../backend/src/routing/routes';
291
+
292
+ export {
293
+ IBook,
294
+ IBookInfo,
295
+ backendRoutes
296
+ }
297
+
298
+ ```
299
+
300
+ usage:
301
+
302
+
303
+ ```typescript
304
+ import { backendRoutes} from '../../backendImport';
305
+
306
+ //index.ts
307
+ const theClient = new RWSClient();
308
+ theClient.setBackendRoutes(backendRoutes());
309
+
310
+ ```
311
+
312
+
313
+ ## Utilizing APIService
314
+
315
+ `APIService` is used for making HTTP requests to the backend. It simplifies the process of interacting with your API endpoints.
316
+
317
+ after control method we have dynamic types those are: <**ResponseType**, **PayloadType**>
318
+
319
+ Example Usage by controller route
320
+
321
+ ```typescript
322
+ const apiPromise: Promise<ITalkApiResponse> = ApiService.back.post<ITalkApiResponse, IApiTalkPayload>('talk:models:prompt', {
323
+ message: msg,
324
+ model: this.chosenModel,
325
+ });
326
+ ```
327
+
328
+ Example Usage by url
329
+
330
+ ```typescript
331
+ const apiPromise: Promise<ITalkApiResponse> = ApiService.post<ITalkApiResponse, IApiTalkPayload>('/api/path/to/action', {
332
+ message: msg,
333
+ model: this.chosenModel,
334
+ });
335
+ ```
336
+
337
+ ## Notifier
338
+
339
+ ### Overview
340
+
341
+ The Notifier feature in the RWS Client is a versatile tool for handling notifications within the application. It allows for different types of user interface interactions like alerts, notifications, and silent logging, with varying levels of visibility and user interaction.
342
+ Usage
343
+
344
+ ### Setting the Notifier
345
+
346
+ ```typescript
347
+ theClient.setNotifier((message: string, logType: NotifyLogType, uiType: NotifyUiType = 'notification', onConfirm: (params: any) => void) => {
348
+ // Implementation based on uiType
349
+ });
350
+
351
+ ```
352
+
353
+ This function allows setting a custom notifier in the RWS Client. It handles the logic based on `uiType`.
354
+
355
+ Alert, Notify, and Silent
356
+
357
+ - alert: Displays an alert dialog with the message.
358
+ - notify: Shows a notification with the message.
359
+ - silent: Silently logs the message to the console.
360
+
361
+ Each method can be configured with a `message`, `logType`, and an optional `onConfirm` callback function.
362
+
363
+ Note
364
+
365
+ Ensure that a notifier is set in the RWS Client to use the `NotifyService` effectively. If no notifier is set, it will default to a warning in the console.
366
+
367
+ ## Service Worker
368
+
369
+ If you pass ```{serviceWorker: 'service_worker_class_path.ts'}``` to RWS Webpack wrapper function param, the code will build ServiceWorker to pubDir.
370
+
371
+ **Remember to have lib field set in tesconfig.json**
372
+
373
+ ```json
374
+ {
375
+ "lib": ["DOM", "ESNext", "WebWorker"]
376
+ }
377
+ ```
378
+
379
+ example ServiceWorker class:
380
+
381
+ ```typescript
382
+ import SWService, { ServiceWorkerServiceInstance } from 'rws-js-client/src/services/ServiceWorkerService'
383
+ import {TimeTracker} from '../services/TimeTrackerService';
384
+ import RWSServiceWorker from 'rws-js-client/src/service_worker/src/_service_worker';
385
+ import { RWSWSService as WSService } from 'rws-js-client/src/services/WSService'
386
+
387
+ declare const self: ServiceWorkerGlobalScope;
388
+
389
+ class ServiceWorker extends RWSServiceWorker {
390
+ ignoredUrls = [
391
+ new RegExp('(.*(?=.[^.]*$).*)/#/login'),
392
+ new RegExp('(.*(?=.[^.]*$).*)/#/logout'),
393
+ ];
394
+
395
+ protected regExTypes = {
396
+ FLASHCARDS_VIEW: new RegExp('.*:\\/\\/.*\\/#\\/([a-z0-9].*)\\/reports\\/flashcards$')
397
+ };
398
+
399
+ async onInit(): Promise<void>
400
+ {
401
+ type ITheUser = any;
402
+
403
+ let THE_USER: ITheUser | null = null;
404
+ const toSync: TimeTracker[] = [];
405
+
406
+ let WS_URL: string | null;
407
+ self.addEventListener('install', () => {
408
+ console.log('Service Worker: Installed');
409
+ });
410
+
411
+ self.addEventListener('activate', () => {
412
+ console.log('[SW] Service Worker: Activated');
413
+
414
+ return self.clients.claim();
415
+ });
416
+
417
+
418
+ // Send a message to the client page
419
+ const sendMessageToClient = (clientId: string, payload: any) => {
420
+ return self.clients.get(clientId)
421
+ .then((client: any) => {
422
+ if (client) {
423
+ client.postMessage(payload);
424
+ }
425
+ });
426
+ };
427
+
428
+ interface MSGEvent{
429
+ data?: {
430
+ command: string,
431
+ asset_type?: string,
432
+ params: any
433
+ }
434
+ }
435
+
436
+ const checkWs = (): void => {
437
+ if(!WSService.socket() && WS_URL){
438
+ WSService.init(WS_URL, THE_USER);
439
+ }
440
+ };
441
+
442
+ // Listen for messages from the client page
443
+ self.addEventListener('message', (event: MSGEvent) => {
444
+ if(!event.data){
445
+ return;
446
+ }
447
+
448
+ if (event.data.command){
449
+ console.log('[SW] OP Message:', event.data);
450
+
451
+ switch (event.data.command) {
452
+ case 'SET_WS_URL':
453
+ WS_URL = event.data.params.url;
454
+ break;
455
+ case 'SET_USER':
456
+ THE_USER = event.data.params;
457
+ checkWs();
458
+ break;
459
+ case 'START_TRACKING':
460
+ checkWs();
461
+ if(!WSService.socket() && THE_USER){
462
+ break;
463
+ }
464
+ SWService.trackActivity(event.data.asset_type, event.data.params.page_location, event.data.params, toSync);
465
+ break;
466
+ case 'TRACKER_SAVED':
467
+ const { clientId, tracker } = event.data.params;
468
+
469
+ sendMessageToClient(clientId, { message: 'TRACKER_SAVED_RESPONSE', data: tracker });
470
+ break;
471
+ }
472
+ }
473
+ });
474
+ }
475
+ }
476
+
477
+ ServiceWorker.create();
478
+ ```
479
+
480
+ ## Example: WebChat Component
481
+
482
+ The WebChat component demonstrates a practical use of `APIService` in a real-world scenario. It shows how to send and receive data from the backend.
483
+
484
+ ### WebChat Component Implementation
485
+
486
+ ```typescript
487
+ import { RWSViewComponent, ApiService, NotifyService, RWSView, WSService } from 'rws-js-client';
488
+ import { observable, css } from '@microsoft/fast-element';
489
+
490
+ import './children/convo-footer/component';
491
+
492
+ import WebChatEvents from './events';
493
+ import { IContext } from './children/left-bar/component';
494
+ import { IMessage } from '../chat-message/component';
495
+ import { ITalkApiResponse, BedrockBaseModel, IHyperParameter,
496
+
497
+ @RWSView('web-chat')
498
+ class WebChat extends RWSViewComponent {
499
+
500
+ @observable chatContext: IContext = null;
501
+ @observable chosenModel: BedrockBaseModel = null;
502
+ @observable injectMessages: IMessage[] = [];
503
+ @observable hyperParameters: { key: string, value: any }[] = [];
504
+
505
+ connectedCallback() {
506
+ super.connectedCallback();
507
+
508
+ this.on<{ item: IContext }>(WebChatEvents.item.click, (event: CustomEvent<{ item: IContext }>) => {
509
+ this.chatContext = event.detail.item;
510
+ });
511
+
512
+ this.on<{ item: BedrockBaseModel }>(WebChatEvents.model.set, (event: CustomEvent<{ item: BedrockBaseModel }>) => {
513
+ if(!event.detail.item){
514
+ this.chosenModel = null;
515
+ return;
516
+ }
517
+
518
+ this.chosenModel = {...event.detail.item};
519
+
520
+ this.setupModel();
521
+ });
522
+
523
+ if(!this.chosenModel){
524
+ this.chosenModel = ClaudeModel;
525
+ this.setupModel();
526
+ }
527
+
528
+ this.on<{ item: IMessage }>(WebChatEvents.message.send, (event: CustomEvent<{ item: IMessage }>) => {
529
+ this.injectMessages = [event.detail.item];
530
+ // this.callStreamApi(event.detail.item);
531
+ this.callTalkApi(event.detail.item);
532
+ });
533
+
534
+ }
535
+ setupModel() {
536
+ // other code
537
+ }
538
+
539
+ setHyperParam(key: string, value: any): void
540
+ {
541
+ // other code
542
+ }
543
+
544
+
545
+
546
+ this.hyperParameters = [
547
+ ...this.hyperParameters,
548
+ {
549
+ key,
550
+ value
551
+ }
552
+ ];
553
+ }
554
+
555
+ private getDefaultParams(provider: string | null)
556
+ {
557
+ // other code
558
+ }
559
+
560
+
561
+ private async callTalkApi(msg: IMessage): Promise<void>
562
+ {
563
+
564
+ type IApiTalkPayload = {
565
+ message: IMessage;
566
+ model: any;
567
+ }
568
+
569
+ try {
570
+ const apiPromise: Promise<ITalkApiResponse> = ApiService.back.post<ITalkApiResponse, IApiTalkPayload>('talk:models:prompt', {
571
+ message: msg,
572
+ model: this.chosenModel,
573
+ });
574
+
575
+ this.injectMessages = [msg, {
576
+ _promise: apiPromise,
577
+ me: false,
578
+ author: this.chosenModel.modelName,
579
+ content: null,
580
+ model: this.chosenModel,
581
+ created_at: new Date()
582
+ }];
583
+
584
+ } catch(e: Error | any) {
585
+ console.error(e);
586
+ }
587
+ }
588
+
589
+ private async callStreamApi(msg: IMessage): Promise<void>
590
+ {
591
+
592
+ type IApiTalkPayload = {
593
+ message: IMessage;
594
+ model: any;
595
+ }
596
+
597
+ const llmStream = new ReadableStream();
598
+
599
+ const sendMsg: IMessage = {
600
+ me: false,
601
+ author: this.chosenModel.modelName,
602
+ content: null,
603
+ model: this.chosenModel,
604
+ created_at: new Date()
605
+ };
606
+
607
+ WSService.sendMessage('send_msg', {
608
+ modelId: this.chosenModel.modelId,
609
+ prompt: msg.content
610
+ });
611
+
612
+ try {
613
+ this.injectMessages = [msg, {
614
+ ...sendMsg,
615
+ _stream: llmStream,
616
+ }];
617
+
618
+ } catch(e: Error | any) {
619
+ console.error(e);
620
+ }
621
+ }
622
+ }
623
+
624
+ WebChat.defineComponent();
625
+
626
+
627
+ export { WebChat }
628
+
629
+ ```
630
+
631
+ ### Controller route
632
+
633
+ The route ApiService.back.get|post|put|delete methods can be found in backend controllers:
634
+
635
+ ```typescript
636
+ @Route('talk:models:prompt', 'POST')
637
+ public async modelTalkAction(params: IRequestParams): Promise<ITalkApiResponse>
638
+ {
639
+ // (...)
640
+ }
641
+ ```
642
+
643
+ and src/config/config
644
+
645
+ ```typescript
646
+ const http_routes = [
647
+ {
648
+ prefix: '/prefix',
649
+ routes: [
650
+ {
651
+ name: 'action:route:name',
652
+ path: '/path/to/action'
653
+ },
654
+ {
655
+ name: 'action:route:name',
656
+ path: '/path/to/action'
657
+ }
658
+ ]
659
+ },
660
+ {
661
+ name: 'home:index',
662
+ path: '/*', //if no routes detected pass request to frontend
663
+ noParams: true, //do not read params from the request leave it to the front
664
+ },
665
+ ]
666
+ ```
667
+
668
+ ### Socket route
669
+
670
+ Socket route from
671
+
672
+ ```typescript
673
+ WSService.sendMessage('send_msg', {
674
+ modelId: this.chosenModel.modelId,
675
+ prompt: msg.content
676
+ });
677
+
678
+ ```
679
+
680
+ are defined in backend/src/config/config
681
+
682
+ ```typescript
683
+ const ws_routes = {
684
+ 'send_msg' : ChatSocket,
685
+ 'process_book' : TrainSocket,
686
+ }
687
+ ```
688
+
689
+ ## Links
690
+
691
+ - https://www.webcomponents.org (open-source WebComponents repository)