@websolutespa/bom-mixer-llm 0.0.1

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 (216) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/README.md +11 -0
  3. package/dist/cjs/index.css +2264 -0
  4. package/dist/cjs/index.js +3566 -0
  5. package/dist/cjs/index.js.map +1 -0
  6. package/dist/esm/index.css +2264 -0
  7. package/dist/esm/index.js +5 -0
  8. package/dist/esm/index.js.map +1 -0
  9. package/dist/esm/llm/components/Background/background.js +76 -0
  10. package/dist/esm/llm/components/Background/background.js.map +1 -0
  11. package/dist/esm/llm/components/BusyIndicator/busyindicator.js +23 -0
  12. package/dist/esm/llm/components/BusyIndicator/busyindicator.js.map +1 -0
  13. package/dist/esm/llm/components/Canvas/canvas.js +223 -0
  14. package/dist/esm/llm/components/Canvas/canvas.js.map +1 -0
  15. package/dist/esm/llm/components/Chunk/chunk.js +65 -0
  16. package/dist/esm/llm/components/Chunk/chunk.js.map +1 -0
  17. package/dist/esm/llm/components/Cta/cta.js +33 -0
  18. package/dist/esm/llm/components/Cta/cta.js.map +1 -0
  19. package/dist/esm/llm/components/Event/event.js +90 -0
  20. package/dist/esm/llm/components/Event/event.js.map +1 -0
  21. package/dist/esm/llm/components/EventGroup/eventgroup.js +61 -0
  22. package/dist/esm/llm/components/EventGroup/eventgroup.js.map +1 -0
  23. package/dist/esm/llm/components/FormRecap/FormRecap.js +240 -0
  24. package/dist/esm/llm/components/FormRecap/FormRecap.js.map +1 -0
  25. package/dist/esm/llm/components/FormRecapError/FormRecapError.js +13 -0
  26. package/dist/esm/llm/components/FormRecapError/FormRecapError.js.map +1 -0
  27. package/dist/esm/llm/components/FormRecapSuccess/FormRecapSuccess.js +13 -0
  28. package/dist/esm/llm/components/FormRecapSuccess/FormRecapSuccess.js.map +1 -0
  29. package/dist/esm/llm/components/FormRequest/FormRequest.js +34 -0
  30. package/dist/esm/llm/components/FormRequest/FormRequest.js.map +1 -0
  31. package/dist/esm/llm/components/Header/header.js +51 -0
  32. package/dist/esm/llm/components/Header/header.js.map +1 -0
  33. package/dist/esm/llm/components/Intro/intro.js +77 -0
  34. package/dist/esm/llm/components/Intro/intro.js.map +1 -0
  35. package/dist/esm/llm/components/Main/main.js +118 -0
  36. package/dist/esm/llm/components/Main/main.js.map +1 -0
  37. package/dist/esm/llm/components/Poi/poi.js +78 -0
  38. package/dist/esm/llm/components/Poi/poi.js.map +1 -0
  39. package/dist/esm/llm/components/PoiGroup/poigroup.js +14 -0
  40. package/dist/esm/llm/components/PoiGroup/poigroup.js.map +1 -0
  41. package/dist/esm/llm/components/Prompt/prompt.js +86 -0
  42. package/dist/esm/llm/components/Prompt/prompt.js.map +1 -0
  43. package/dist/esm/llm/components/Prompt/suggestions.js +45 -0
  44. package/dist/esm/llm/components/Prompt/suggestions.js.map +1 -0
  45. package/dist/esm/llm/components/QuickLinks/quicklinks.js +66 -0
  46. package/dist/esm/llm/components/QuickLinks/quicklinks.js.map +1 -0
  47. package/dist/esm/llm/components/Stream/stream.js +43 -0
  48. package/dist/esm/llm/components/Stream/stream.js.map +1 -0
  49. package/dist/esm/llm/components/Text/text.js +34 -0
  50. package/dist/esm/llm/components/Text/text.js.map +1 -0
  51. package/dist/esm/llm/components/Thread/thread.js +26 -0
  52. package/dist/esm/llm/components/Thread/thread.js.map +1 -0
  53. package/dist/esm/llm/components/Trigger/blob.js +122 -0
  54. package/dist/esm/llm/components/Trigger/blob.js.map +1 -0
  55. package/dist/esm/llm/components/Trigger/trigger.js +63 -0
  56. package/dist/esm/llm/components/Trigger/trigger.js.map +1 -0
  57. package/dist/esm/llm/components/Tripadvisor/tripadvisor.js +94 -0
  58. package/dist/esm/llm/components/Tripadvisor/tripadvisor.js.map +1 -0
  59. package/dist/esm/llm/hooks/useAutosizeTextArea.js +13 -0
  60. package/dist/esm/llm/hooks/useAutosizeTextArea.js.map +1 -0
  61. package/dist/esm/llm/llm.js +48 -0
  62. package/dist/esm/llm/llm.js.map +1 -0
  63. package/dist/esm/llm/utils.js +15 -0
  64. package/dist/esm/llm/utils.js.map +1 -0
  65. package/dist/esm/node_modules/tslib/tslib.es6.js +135 -0
  66. package/dist/esm/node_modules/tslib/tslib.es6.js.map +1 -0
  67. package/dist/esm/useLlm/emitter.js +32 -0
  68. package/dist/esm/useLlm/emitter.js.map +1 -0
  69. package/dist/esm/useLlm/message.service.js +308 -0
  70. package/dist/esm/useLlm/message.service.js.map +1 -0
  71. package/dist/esm/useLlm/mock.js +687 -0
  72. package/dist/esm/useLlm/mock.js.map +1 -0
  73. package/dist/esm/useLlm/speech.js +306 -0
  74. package/dist/esm/useLlm/speech.js.map +1 -0
  75. package/dist/esm/useLlm/useLlm.js +460 -0
  76. package/dist/esm/useLlm/useLlm.js.map +1 -0
  77. package/dist/types/index.d.ts +6 -0
  78. package/dist/types/index.d.ts.map +1 -0
  79. package/dist/types/llm/components/Background/background.d.ts +34 -0
  80. package/dist/types/llm/components/Background/background.d.ts.map +1 -0
  81. package/dist/types/llm/components/BusyIndicator/busyindicator.d.ts +2 -0
  82. package/dist/types/llm/components/BusyIndicator/busyindicator.d.ts.map +1 -0
  83. package/dist/types/llm/components/Canvas/canvas.d.ts +12 -0
  84. package/dist/types/llm/components/Canvas/canvas.d.ts.map +1 -0
  85. package/dist/types/llm/components/Chunk/chunk.d.ts +9 -0
  86. package/dist/types/llm/components/Chunk/chunk.d.ts.map +1 -0
  87. package/dist/types/llm/components/Cta/cta.d.ts +10 -0
  88. package/dist/types/llm/components/Cta/cta.d.ts.map +1 -0
  89. package/dist/types/llm/components/Event/event.d.ts +3 -0
  90. package/dist/types/llm/components/Event/event.d.ts.map +1 -0
  91. package/dist/types/llm/components/EventGroup/eventgroup.d.ts +3 -0
  92. package/dist/types/llm/components/EventGroup/eventgroup.d.ts.map +1 -0
  93. package/dist/types/llm/components/FormRecap/FormRecap.d.ts +10 -0
  94. package/dist/types/llm/components/FormRecap/FormRecap.d.ts.map +1 -0
  95. package/dist/types/llm/components/FormRecapError/FormRecapError.d.ts +2 -0
  96. package/dist/types/llm/components/FormRecapError/FormRecapError.d.ts.map +1 -0
  97. package/dist/types/llm/components/FormRecapSuccess/FormRecapSuccess.d.ts +2 -0
  98. package/dist/types/llm/components/FormRecapSuccess/FormRecapSuccess.d.ts.map +1 -0
  99. package/dist/types/llm/components/FormRequest/FormRequest.d.ts +4 -0
  100. package/dist/types/llm/components/FormRequest/FormRequest.d.ts.map +1 -0
  101. package/dist/types/llm/components/Header/header.d.ts +4 -0
  102. package/dist/types/llm/components/Header/header.d.ts.map +1 -0
  103. package/dist/types/llm/components/Intro/intro.d.ts +2 -0
  104. package/dist/types/llm/components/Intro/intro.d.ts.map +1 -0
  105. package/dist/types/llm/components/Main/main.d.ts +2 -0
  106. package/dist/types/llm/components/Main/main.d.ts.map +1 -0
  107. package/dist/types/llm/components/Poi/poi.d.ts +3 -0
  108. package/dist/types/llm/components/Poi/poi.d.ts.map +1 -0
  109. package/dist/types/llm/components/PoiGroup/poigroup.d.ts +3 -0
  110. package/dist/types/llm/components/PoiGroup/poigroup.d.ts.map +1 -0
  111. package/dist/types/llm/components/Prompt/prompt.d.ts +2 -0
  112. package/dist/types/llm/components/Prompt/prompt.d.ts.map +1 -0
  113. package/dist/types/llm/components/Prompt/suggestions.d.ts +2 -0
  114. package/dist/types/llm/components/Prompt/suggestions.d.ts.map +1 -0
  115. package/dist/types/llm/components/QuickLinks/quicklinks.d.ts +3 -0
  116. package/dist/types/llm/components/QuickLinks/quicklinks.d.ts.map +1 -0
  117. package/dist/types/llm/components/Stream/stream.d.ts +4 -0
  118. package/dist/types/llm/components/Stream/stream.d.ts.map +1 -0
  119. package/dist/types/llm/components/Text/text.d.ts +7 -0
  120. package/dist/types/llm/components/Text/text.d.ts.map +1 -0
  121. package/dist/types/llm/components/Thread/thread.d.ts +4 -0
  122. package/dist/types/llm/components/Thread/thread.d.ts.map +1 -0
  123. package/dist/types/llm/components/Trigger/blob.d.ts +2 -0
  124. package/dist/types/llm/components/Trigger/blob.d.ts.map +1 -0
  125. package/dist/types/llm/components/Trigger/trigger.d.ts +2 -0
  126. package/dist/types/llm/components/Trigger/trigger.d.ts.map +1 -0
  127. package/dist/types/llm/components/Tripadvisor/tripadvisor.d.ts +4 -0
  128. package/dist/types/llm/components/Tripadvisor/tripadvisor.d.ts.map +1 -0
  129. package/dist/types/llm/hooks/useAutosizeTextArea.d.ts +3 -0
  130. package/dist/types/llm/hooks/useAutosizeTextArea.d.ts.map +1 -0
  131. package/dist/types/llm/llm.d.ts +5 -0
  132. package/dist/types/llm/llm.d.ts.map +1 -0
  133. package/dist/types/llm/utils.d.ts +7 -0
  134. package/dist/types/llm/utils.d.ts.map +1 -0
  135. package/dist/types/types.d.ts +163 -0
  136. package/dist/types/types.d.ts.map +1 -0
  137. package/dist/types/useLlm/cors.d.ts +16 -0
  138. package/dist/types/useLlm/cors.d.ts.map +1 -0
  139. package/dist/types/useLlm/emitter.d.ts +8 -0
  140. package/dist/types/useLlm/emitter.d.ts.map +1 -0
  141. package/dist/types/useLlm/message.service.d.ts +24 -0
  142. package/dist/types/useLlm/message.service.d.ts.map +1 -0
  143. package/dist/types/useLlm/mock.d.ts +5 -0
  144. package/dist/types/useLlm/mock.d.ts.map +1 -0
  145. package/dist/types/useLlm/speech.d.ts +57 -0
  146. package/dist/types/useLlm/speech.d.ts.map +1 -0
  147. package/dist/types/useLlm/useLlm.d.ts +83 -0
  148. package/dist/types/useLlm/useLlm.d.ts.map +1 -0
  149. package/package.json +86 -0
  150. package/src/__tests/example.test.ts +8 -0
  151. package/src/index.ts +6 -0
  152. package/src/llm/components/Background/_style.scss +24 -0
  153. package/src/llm/components/Background/background.tsx +89 -0
  154. package/src/llm/components/BusyIndicator/_style.scss +73 -0
  155. package/src/llm/components/BusyIndicator/busyindicator.tsx +13 -0
  156. package/src/llm/components/Canvas/_style.scss +10 -0
  157. package/src/llm/components/Canvas/canvas.tsx +257 -0
  158. package/src/llm/components/Chunk/chunk.tsx +80 -0
  159. package/src/llm/components/Cta/_style.scss +72 -0
  160. package/src/llm/components/Cta/cta.tsx +33 -0
  161. package/src/llm/components/Event/_style.scss +144 -0
  162. package/src/llm/components/Event/event.tsx +70 -0
  163. package/src/llm/components/EventGroup/_style.scss +60 -0
  164. package/src/llm/components/EventGroup/eventgroup.tsx +59 -0
  165. package/src/llm/components/FormRecap/FormRecap.tsx +221 -0
  166. package/src/llm/components/FormRecap/_style.scss +101 -0
  167. package/src/llm/components/FormRecapError/FormRecapError.tsx +10 -0
  168. package/src/llm/components/FormRecapError/_style.scss +11 -0
  169. package/src/llm/components/FormRecapSuccess/FormRecapSuccess.tsx +10 -0
  170. package/src/llm/components/FormRecapSuccess/_style.scss +11 -0
  171. package/src/llm/components/FormRequest/FormRequest.tsx +21 -0
  172. package/src/llm/components/FormRequest/_style.scss +21 -0
  173. package/src/llm/components/Header/_style.scss +72 -0
  174. package/src/llm/components/Header/header.tsx +38 -0
  175. package/src/llm/components/Intro/_style.scss +36 -0
  176. package/src/llm/components/Intro/intro.tsx +65 -0
  177. package/src/llm/components/Main/_style.scss +75 -0
  178. package/src/llm/components/Main/main.tsx +125 -0
  179. package/src/llm/components/Poi/_style.scss +127 -0
  180. package/src/llm/components/Poi/poi.tsx +58 -0
  181. package/src/llm/components/PoiGroup/_style.scss +14 -0
  182. package/src/llm/components/PoiGroup/poigroup.tsx +15 -0
  183. package/src/llm/components/Prompt/_style.scss +132 -0
  184. package/src/llm/components/Prompt/prompt.tsx +73 -0
  185. package/src/llm/components/Prompt/suggestions.tsx +39 -0
  186. package/src/llm/components/QuickLinks/_style.scss +61 -0
  187. package/src/llm/components/QuickLinks/quicklinks.tsx +58 -0
  188. package/src/llm/components/Stream/stream.tsx +37 -0
  189. package/src/llm/components/Text/_style.scss +89 -0
  190. package/src/llm/components/Text/text.tsx +28 -0
  191. package/src/llm/components/Thread/_style.scss +73 -0
  192. package/src/llm/components/Thread/thread.tsx +27 -0
  193. package/src/llm/components/Trigger/_style.scss +104 -0
  194. package/src/llm/components/Trigger/blob.tsx +42 -0
  195. package/src/llm/components/Trigger/trigger.tsx +50 -0
  196. package/src/llm/components/Tripadvisor/_style.scss +113 -0
  197. package/src/llm/components/Tripadvisor/tripadvisor.tsx +72 -0
  198. package/src/llm/hooks/useAutosizeTextArea.tsx +15 -0
  199. package/src/llm/llm.tsx +48 -0
  200. package/src/llm/scss/_base.scss +20 -0
  201. package/src/llm/scss/_breakpoints.scss +8 -0
  202. package/src/llm/scss/_layout.scss +39 -0
  203. package/src/llm/scss/_mixin.scss +67 -0
  204. package/src/llm/scss/_reset.scss +104 -0
  205. package/src/llm/scss/_spacing.scss +17 -0
  206. package/src/llm/scss/_typography.scss +153 -0
  207. package/src/llm/scss/_vars.scss +23 -0
  208. package/src/llm/scss/style.scss +29 -0
  209. package/src/llm/utils.tsx +12 -0
  210. package/src/types.ts +215 -0
  211. package/src/useLlm/cors.ts +124 -0
  212. package/src/useLlm/emitter.ts +29 -0
  213. package/src/useLlm/message.service.ts +336 -0
  214. package/src/useLlm/mock.ts +801 -0
  215. package/src/useLlm/speech.ts +363 -0
  216. package/src/useLlm/useLlm.tsx +474 -0
@@ -0,0 +1,29 @@
1
+ @import './vars';
2
+ @import './mixin';
3
+ @import './typography';
4
+ @import './spacing';
5
+ @import './breakpoints';
6
+ @import './reset';
7
+ @import './base';
8
+ @import './layout';
9
+ @import '../components/Trigger/style';
10
+ @import '../components/Background/style';
11
+ @import '../components/Main/style';
12
+ @import '../components/Header/style';
13
+ @import '../components/Intro/style';
14
+ @import '../components/QuickLinks/style';
15
+ @import '../components/Prompt/style';
16
+ @import '../components/Text/style';
17
+ @import '../components/Event/style';
18
+ @import '../components/EventGroup/style';
19
+ @import '../components/Cta/style';
20
+ @import '../components/Poi/style';
21
+ @import '../components/PoiGroup/style';
22
+ @import '../components/Thread/style';
23
+ @import '../components/Tripadvisor/style';
24
+ @import '../components/Canvas/style';
25
+ @import '../components/FormRequest/style';
26
+ @import '../components/FormRecap/style';
27
+ @import '../components/FormRecapSuccess/style';
28
+ @import '../components/FormRecapError/style';
29
+ @import '../components/BusyIndicator/style';
@@ -0,0 +1,12 @@
1
+ type Nl2BrProps = {
2
+ text?: string
3
+ };
4
+
5
+ export const Nl2Br = (props: Nl2BrProps) => {
6
+ const lines = props.text?.split('\\n').map(function(line, n){
7
+ return (n == 0) ? [line] : [<br key={n} />, line];
8
+ });
9
+ return <span>{lines}</span>;
10
+ };
11
+
12
+ export const clamp = (min:number, max:number) => Math.min(Math.max(min + (window.innerWidth - 375) / (1920 - 375) * (max - min), min), max);
package/src/types.ts ADDED
@@ -0,0 +1,215 @@
1
+ import { IMedia } from '@websolutespa/bom-core';
2
+
3
+ export type LlmChunkLog = Record<string, unknown> & {
4
+ type: 'log';
5
+ };
6
+
7
+ export type LlmChunkError = {
8
+ type: 'error';
9
+ error: unknown;
10
+ };
11
+
12
+ export type LlmChunkInfo = {
13
+ type: 'info';
14
+ threadId: string;
15
+ };
16
+
17
+ export type LlmChunkEnd = {
18
+ type: 'end';
19
+ };
20
+
21
+ export type LlmChunkString = {
22
+ type: 'string';
23
+ content: string;
24
+ };
25
+
26
+ export type LlmChunkImage = {
27
+ type: 'image';
28
+ id: string;
29
+ src: string;
30
+ };
31
+
32
+ export type LlmChunkEvent = {
33
+ type: 'event';
34
+ id: string;
35
+ title: string;
36
+ abstract: string;
37
+ place: string;
38
+ url: string;
39
+ image: string;
40
+ date?: string;
41
+ time?: string;
42
+ };
43
+
44
+ export type LlmChunkEventItem = Omit<LlmChunkEvent, 'type'> & { type: 'eventItem' };
45
+
46
+ export type LlmChunkEventGroup = {
47
+ type: 'eventGroup';
48
+ items: LlmChunkEventItem[];
49
+ };
50
+
51
+ export type LlmChunkPoi = {
52
+ type: 'poi';
53
+ id: string;
54
+ title: string;
55
+ abstract: string;
56
+ time?: string;
57
+ place: string;
58
+ url: string;
59
+ image: string;
60
+ };
61
+
62
+ export type LlmChunkPoiItem = Omit<LlmChunkPoi, 'type'> & { type: 'poiItem' };
63
+
64
+ export type LlmChunkPoiGroup = {
65
+ type: 'poiGroup';
66
+ items: LlmChunkPoiItem[];
67
+ };
68
+
69
+ export type LlmChunkTripadvisor = {
70
+ type: 'tripadvisor';
71
+ id: string;
72
+ title: string;
73
+ rating: number;
74
+ reviews: number;
75
+ priceRange: string;
76
+ cuisine: string;
77
+ address: string;
78
+ url: string;
79
+ book: string;
80
+ image: string;
81
+ };
82
+
83
+ export type LlmChunkTripadvisorItem = Omit<LlmChunkTripadvisor, 'type'> & { type: 'tripadvisorItem' };
84
+
85
+ export type LlmChunkTripadvisorGroup = {
86
+ type: 'tripadvisorGroup';
87
+ items: LlmChunkTripadvisorItem[];
88
+ };
89
+
90
+ export type LlmChunkFormRequest = {
91
+ type: 'formRequest';
92
+ };
93
+
94
+ export type LlmChunkFormRecap = {
95
+ type: 'formRecap';
96
+ };
97
+
98
+ export type LlmChunkFormRecapSuccess = {
99
+ type: 'formRecapSuccess';
100
+ };
101
+
102
+ export type LlmChunkFormRecapError = {
103
+ type: 'formRecapError';
104
+ };
105
+
106
+ export type LlmChunkKnownItem =
107
+ LlmChunkEnd
108
+ | LlmChunkEvent
109
+ | LlmChunkEventGroup
110
+ | LlmChunkEventItem
111
+ | LlmChunkError
112
+ | LlmChunkFormRequest
113
+ | LlmChunkFormRecap
114
+ | LlmChunkFormRecapSuccess
115
+ | LlmChunkFormRecapError
116
+ | LlmChunkImage
117
+ | LlmChunkInfo
118
+ | LlmChunkLog
119
+ | LlmChunkPoi
120
+ | LlmChunkPoiGroup
121
+ | LlmChunkPoiItem
122
+ | LlmChunkString
123
+ | LlmChunkTripadvisor
124
+ | LlmChunkTripadvisorGroup
125
+ | LlmChunkTripadvisorItem;
126
+
127
+ export type LlmChunkUnknown = Record<string, unknown> & { type: Exclude<string, LlmChunkKnownItem['type']> };
128
+
129
+ export type LlmChunkItem = LlmChunkKnownItem;
130
+
131
+ export type LlmChunkType = Exclude<LlmChunkKnownItem['type'], 'string' | 'info' | 'end' | 'formRequest' | 'formRecap' | 'formRecapSuccess' | 'formRecapError'>;
132
+
133
+ export type LlmItem = Extract<LlmChunkItem, { type: LlmChunkType }>;
134
+
135
+ // export type ToDiscriminatedUnion<T> = { [K in keyof T]: { type: K, value: T[K] } }[keyof T];
136
+ // type StuctureMappedType<T extends LlmChunkType> = { [K in T]: Array<LlmChunkItem<K>> };
137
+ // export type LlmChunkItems<T extends LlmChunkType = LlmChunkType> = Record<T, (item: Extract<LlmChunkItem, { type: T }>) => string>;
138
+
139
+ export type LlmChunk = string | LlmChunkItem;
140
+
141
+ export type LlmCredentials = {
142
+ apiKey: string;
143
+ appKey: string;
144
+ };
145
+
146
+ export type LlmMessage = {
147
+ content: string;
148
+ role: string; // assistant, user
149
+ };
150
+
151
+ export type LlmDecodedMessage = LlmMessage & {
152
+ chunks: LlmChunkItem[];
153
+ };
154
+
155
+ export type StreamRequest = LlmCredentials & {
156
+ messages: LlmMessage[];
157
+ threadId?: string;
158
+ };
159
+
160
+ export type StreamResponse = {
161
+ chunks: LlmChunkItem[];
162
+ threadId: string;
163
+ };
164
+
165
+ export type LlmCard = {
166
+ icon?: IMedia;
167
+ body: string;
168
+ title: string;
169
+ message?: string;
170
+ };
171
+
172
+ export type LlmSampleInputText = {
173
+ id: string;
174
+ sampleInputText: string;
175
+ };
176
+
177
+ export type LlmContents = {
178
+ collapsedWelcomeText: string;
179
+ collapsedWelcomeTextHover?: string;
180
+ extendedWelcomeText: string;
181
+ layoutBuilder: LlmCard[];
182
+ logo?: IMedia;
183
+ logoMini?: IMedia;
184
+ sampleInputTexts: LlmSampleInputText[];
185
+ shortWelcomeText: string;
186
+ };
187
+
188
+ export type LlmThread = {
189
+ messages: LlmMessage[];
190
+ threadId: string;
191
+ };
192
+
193
+ export type LlmDecodedThread = {
194
+ messages: LlmDecodedMessage[];
195
+ threadId: string;
196
+ };
197
+
198
+ export type LlmWebhook = {
199
+ webhook: string;
200
+ };
201
+
202
+ export type LlmWebhooks = LlmWebhook[];
203
+
204
+ export type LlmMode = 'site' | 'chat';
205
+
206
+ export type LlmApp = {
207
+ mode: LlmMode;
208
+ contents: LlmContents;
209
+ thread?: LlmThread;
210
+ webhooks?: LlmWebhooks;
211
+ };
212
+
213
+ export type LlmUiStatus = {
214
+ open: boolean;
215
+ };
@@ -0,0 +1,124 @@
1
+ type StaticOrigin = boolean | string | RegExp | (boolean | string | RegExp)[];
2
+
3
+ type OriginFn = (origin: string | undefined, req: Request) => StaticOrigin | Promise<StaticOrigin>;
4
+
5
+ interface CorsOptions {
6
+ allowedHeaders?: string | string[]
7
+ credentials?: boolean
8
+ exposedHeaders?: string | string[]
9
+ maxAge?: number
10
+ methods?: string | string[]
11
+ optionsSuccessStatus?: number
12
+ origin?: StaticOrigin | OriginFn
13
+ preflightContinue?: boolean
14
+ }
15
+
16
+ const defaultOptions: CorsOptions = {
17
+ methods: 'GET,HEAD,PUT,PATCH,POST,DELETE',
18
+ optionsSuccessStatus: 204,
19
+ origin: '*',
20
+ preflightContinue: false,
21
+ };
22
+
23
+ export async function cors(request: Request, response: Response, userOptions?: CorsOptions) {
24
+ const options = { ...defaultOptions, ...userOptions };
25
+ const { headers } = response;
26
+ const originHeaders = await originHeadersFromReq(request, options.origin ?? false);
27
+ if (!originHeaders) {
28
+ return response;
29
+ }
30
+ // setting headers
31
+ const mergeHeaders = (v: string, k: string) => {
32
+ if (k === 'Vary') {
33
+ headers.append(k, v);
34
+ } else {
35
+ headers.set(k, v);
36
+ }
37
+ };
38
+ originHeaders.forEach(mergeHeaders);
39
+ if (options.credentials) {
40
+ headers.set('Access-Control-Allow-Credentials', 'true');
41
+ }
42
+ const exposed = Array.isArray(options.exposedHeaders)
43
+ ? options.exposedHeaders.join(',')
44
+ : options.exposedHeaders;
45
+ if (exposed) {
46
+ headers.set('Access-Control-Expose-Headers', exposed);
47
+ }
48
+ // case method options;
49
+ if (request.method === 'OPTIONS') {
50
+ if (options.methods) {
51
+ const methods = Array.isArray(options.methods)
52
+ ? options.methods.join(',')
53
+ : options.methods;
54
+ headers.set('Access-Control-Allow-Methods', methods);
55
+ }
56
+ getAllowedHeaders(request, options.allowedHeaders).forEach(mergeHeaders);
57
+ if (typeof options.maxAge === 'number') {
58
+ headers.set('Access-Control-Max-Age', String(options.maxAge));
59
+ }
60
+ if (options.preflightContinue) {
61
+ return response;
62
+ }
63
+ headers.set('Content-Length', '0');
64
+ return new Response(null, { status: options.optionsSuccessStatus, headers });
65
+ }
66
+ // case method <> option
67
+ return response;
68
+ }
69
+
70
+ export function initCors(options?: CorsOptions) {
71
+ return (request: Request, response: Response) => cors(request, response, options);
72
+ }
73
+
74
+ function isOriginAllowed(origin: string, allowedOrigin: StaticOrigin): boolean {
75
+ return Array.isArray(allowedOrigin)
76
+ ? allowedOrigin.some((x) => isOriginAllowed(origin, x))
77
+ : typeof allowedOrigin === 'string'
78
+ ? origin === allowedOrigin
79
+ : allowedOrigin instanceof RegExp
80
+ ? allowedOrigin.test(origin)
81
+ : !!allowedOrigin;
82
+ }
83
+
84
+ function getOriginHeaders(origin: string | undefined, allowedOrigin: StaticOrigin) {
85
+ const headers = new Headers();
86
+ if (allowedOrigin === '*') {
87
+ headers.set('Access-Control-Allow-Origin', '*');
88
+ } else if (typeof allowedOrigin === 'string') {
89
+ headers.set('Access-Control-Allow-Origin', allowedOrigin);
90
+ headers.append('Vary', 'Origin');
91
+ } else {
92
+ const allowed = isOriginAllowed(origin ?? '', allowedOrigin);
93
+ if (allowed && origin) {
94
+ headers.set('Access-Control-Allow-Origin', origin);
95
+ }
96
+ headers.append('Vary', 'Origin');
97
+ }
98
+ return headers;
99
+ }
100
+
101
+ async function originHeadersFromReq(request: Request, allowedOrigin: StaticOrigin | OriginFn) {
102
+ const origin = request.headers.get('Origin') || undefined;
103
+ allowedOrigin = typeof allowedOrigin === 'function'
104
+ ? await allowedOrigin(origin, request)
105
+ : allowedOrigin;
106
+ if (!allowedOrigin) {
107
+ return;
108
+ }
109
+ return getOriginHeaders(origin, allowedOrigin);
110
+ }
111
+
112
+ function getAllowedHeaders(request: Request, allowed?: string | string[]) {
113
+ const headers = new Headers();
114
+ if (!allowed) {
115
+ allowed = request.headers.get('Access-Control-Request-Headers')!;
116
+ headers.append('Vary', 'Access-Control-Request-Headers');
117
+ } else if (Array.isArray(allowed)) {
118
+ allowed = allowed.join(',');
119
+ }
120
+ if (allowed) {
121
+ headers.set('Access-Control-Allow-Headers', allowed);
122
+ }
123
+ return headers;
124
+ }
@@ -0,0 +1,29 @@
1
+ export type EventCallback = (...data: any[]) => void;
2
+
3
+ export class Emitter {
4
+ events = new Map<string, EventCallback[]>();
5
+
6
+ on(event: string, callback: EventCallback) {
7
+ const callbacks = this.events.get(event) || [];
8
+ callbacks.push(callback);
9
+ if (!this.events.has(event)) {
10
+ this.events.set(event, callbacks);
11
+ }
12
+ }
13
+
14
+ off(event: string, callback: EventCallback) {
15
+ if (this.events.has(event)) {
16
+ const callbacks = this.events.get(event)!.filter((c) => c !== callback);
17
+ this.events.set(event, callbacks);
18
+ }
19
+ }
20
+
21
+ emit(event: string, ...data: unknown[]) {
22
+ if (this.events.has(event)) {
23
+ this.events.get(event)!.forEach((callback) => {
24
+ callback(...data);
25
+ // setTimeout(() => callback(...data), 0);
26
+ });
27
+ }
28
+ }
29
+ }