@apia/ai 2.0.9 → 2.0.10

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.
@@ -0,0 +1,58 @@
1
+ import { jsx } from '@apia/theme/jsx-runtime';
2
+ import { Box, getVariant } from '@apia/theme';
3
+ import { useRef, useCallback } from 'react';
4
+
5
+ const AutoscrollContainer = ({ children }) => {
6
+ const observer = useRef(null);
7
+ const unsuscribe = useRef(null);
8
+ const isFullScrolled = useRef(false);
9
+ const hasInited = useRef(false);
10
+ const ref = useCallback((el) => {
11
+ if (!el)
12
+ return;
13
+ if (!hasInited.current) {
14
+ hasInited.current = true;
15
+ el.scrollTo({
16
+ top: el.getBoundingClientRect().height,
17
+ behavior: "auto"
18
+ });
19
+ isFullScrolled.current = true;
20
+ }
21
+ observer.current?.disconnect();
22
+ unsuscribe.current?.();
23
+ observer.current = new MutationObserver(([mutation]) => {
24
+ if (mutation && mutation.target instanceof HTMLElement && mutation.target === el) {
25
+ if (isFullScrolled.current && mutation.target.scrollTop !== mutation.target.scrollHeight - mutation.target.offsetHeight) {
26
+ mutation.target.scrollTo({
27
+ top: mutation.target.scrollHeight,
28
+ behavior: "smooth"
29
+ });
30
+ }
31
+ }
32
+ });
33
+ observer.current.observe(el, { childList: true, subtree: true });
34
+ const handleScroll = (ev) => {
35
+ if (ev.target === el && el instanceof HTMLElement) {
36
+ setTimeout(() => {
37
+ isFullScrolled.current = Math.abs(el.scrollTop - (el.scrollHeight - el.offsetHeight)) < 150;
38
+ }, 100);
39
+ }
40
+ };
41
+ el.addEventListener("scroll", handleScroll);
42
+ unsuscribe.current = () => {
43
+ el.removeEventListener("scroll", handleScroll);
44
+ };
45
+ }, []);
46
+ return /* @__PURE__ */ jsx(
47
+ Box,
48
+ {
49
+ ...getVariant("layout.common.components.autoscrollContainer"),
50
+ className: "autoscrollContainer",
51
+ ref,
52
+ children
53
+ }
54
+ );
55
+ };
56
+
57
+ export { AutoscrollContainer };
58
+ //# sourceMappingURL=AutoscrollContainer.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AutoscrollContainer.js","sources":["../../../src/components/chat/AutoscrollContainer.tsx"],"sourcesContent":["import { getVariant } from '@apia/theme';\r\nimport { ReactNode, useCallback, useRef } from 'react';\r\nimport { Box } from '@apia/theme';\r\n\r\nexport const AutoscrollContainer = ({ children }: { children: ReactNode }) => {\r\n const observer = useRef<MutationObserver | null>(null);\r\n const unsuscribe = useRef<(() => void) | null>(null);\r\n const isFullScrolled = useRef(false);\r\n const hasInited = useRef(false);\r\n\r\n const ref = useCallback((el: HTMLElement) => {\r\n if (!el) return;\r\n\r\n if (!hasInited.current) {\r\n hasInited.current = true;\r\n el.scrollTo({\r\n top: el.getBoundingClientRect().height,\r\n behavior: 'auto',\r\n });\r\n isFullScrolled.current = true;\r\n }\r\n\r\n observer.current?.disconnect();\r\n unsuscribe.current?.();\r\n\r\n observer.current = new MutationObserver(([mutation]) => {\r\n if (\r\n mutation &&\r\n mutation.target instanceof HTMLElement &&\r\n mutation.target === el\r\n ) {\r\n if (\r\n isFullScrolled.current &&\r\n mutation.target.scrollTop !==\r\n mutation.target.scrollHeight - mutation.target.offsetHeight\r\n ) {\r\n // eslint-disable-next-line no-param-reassign\r\n mutation.target.scrollTo({\r\n top: mutation.target.scrollHeight,\r\n behavior: 'smooth',\r\n });\r\n }\r\n }\r\n });\r\n observer.current.observe(el, { childList: true, subtree: true });\r\n\r\n const handleScroll = (ev: Event) => {\r\n if (ev.target === el && el instanceof HTMLElement) {\r\n setTimeout(() => {\r\n isFullScrolled.current =\r\n Math.abs(el.scrollTop - (el.scrollHeight - el.offsetHeight)) < 150;\r\n }, 100);\r\n }\r\n };\r\n el.addEventListener('scroll', handleScroll);\r\n unsuscribe.current = () => {\r\n el.removeEventListener('scroll', handleScroll);\r\n };\r\n }, []);\r\n\r\n return (\r\n <Box\r\n {...getVariant('layout.common.components.autoscrollContainer')}\r\n className=\"autoscrollContainer\"\r\n ref={ref}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CAAA,YAAA;AAAA,YACrB,QAAU,EAAA,QAAA;AAAA,WACX,CAAA,CAAA;AAAA,SACH;AAAA,OACF;AAAA,KACD,CAAA,CAAA;AACD,IAAS,QAAA,CAAA,OAAA,CAAQ,QAAQ,EAAI,EAAA,EAAE,WAAW,IAAM,EAAA,OAAA,EAAS,MAAM,CAAA,CAAA;AAE/D,IAAM,MAAA,YAAA,GAAe,CAAC,EAAc,KAAA;AAClC,MAAA,IAAI,EAAG,CAAA,MAAA,KAAW,EAAM,IAAA,EAAA,YAAc,WAAa,EAAA;AACjD,QAAA,UAAA,CAAW,MAAM;AACf,UAAe,cAAA,CAAA,OAAA,GACb,KAAK,GAAI,CAAA,EAAA,CAAG,aAAa,EAAG,CAAA,YAAA,GAAe,EAAG,CAAA,YAAA,CAAa,CAAI,GAAA,GAAA,CAAA;AAAA,WAChE,GAAG,CAAA,CAAA;AAAA,OACR;AAAA,KACF,CAAA;AACA,IAAG,EAAA,CAAA,gBAAA,CAAiB,UAAU,YAAY,CAAA,CAAA;AAC1C,IAAA,UAAA,CAAW,UAAU,MAAM;AACzB,MAAG,EAAA,CAAA,mBAAA,CAAoB,UAAU,YAAY,CAAA,CAAA;AAAA,KAC/C,CAAA;AAAA,GACF,EAAG,EAAE,CAAA,CAAA;AAEL,EACE,uBAAA,GAAA;AAAA,IAAC,GAAA;AAAA,IAAA;AAAA,MACE,GAAG,WAAW,8CAA8C,CAAA;AAAA,MAC7D,SAAU,EAAA,qBAAA;AAAA,MACV,GAAA;AAAA,MAEC,QAAA;AAAA,KAAA;AAAA,GACH,CAAA;AAEJ;;;;"}
@@ -0,0 +1,38 @@
1
+ import * as react from 'react';
2
+ import { EventEmitter } from '@apia/util';
3
+ import { ChatMessage } from './messages/ChatMessage.js';
4
+
5
+ type TMessageType = 'user' | 'system' | 'warning' | 'error' | 'information' | 'response' | 'multipleChoice';
6
+ declare class ChatController extends EventEmitter<{
7
+ listChange: ChatMessage[];
8
+ messageSubmited?: string;
9
+ }> {
10
+ #private;
11
+ id: string;
12
+ constructor(id: string, welcomeMessage?: string | false);
13
+ components: {
14
+ MessageHistory: () => react.JSX.Element;
15
+ Textarea: {
16
+ ({ hideDeleteButton, isLoading: outIsLoading, onSubmit, preventAppendUserMessages, }: {
17
+ hideDeleteButton?: boolean | undefined;
18
+ isLoading?: boolean | undefined;
19
+ onSubmit: (text: string) => Promise<void>;
20
+ preventAppendUserMessages?: boolean | undefined;
21
+ }): react.JSX.Element;
22
+ displayName: string;
23
+ };
24
+ };
25
+ history: {
26
+ add: (prompt: string) => void;
27
+ next: () => string;
28
+ previous: () => string | undefined;
29
+ useList: () => ChatMessage[];
30
+ };
31
+ messages: {
32
+ add: (message: ChatMessage) => void;
33
+ clear: () => void;
34
+ };
35
+ }
36
+
37
+ export { ChatController, type TMessageType };
38
+ //# sourceMappingURL=ChatController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatController.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,124 @@
1
+ import { jsx } from '@apia/theme/jsx-runtime';
2
+ import { Box, getVariant } from '@apia/theme';
3
+ import { EventEmitter, getLabel } from '@apia/util';
4
+ import { useState, useEffect } from 'react';
5
+ import { AutoscrollContainer } from './AutoscrollContainer.js';
6
+ import { makeTextarea } from './Textarea.js';
7
+ import { ChatMessage } from './messages/ChatMessage.js';
8
+
9
+ var __defProp = Object.defineProperty;
10
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
11
+ var __publicField = (obj, key, value) => {
12
+ __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
13
+ return value;
14
+ };
15
+ var __accessCheck = (obj, member, msg) => {
16
+ if (!member.has(obj))
17
+ throw TypeError("Cannot " + msg);
18
+ };
19
+ var __privateGet = (obj, member, getter) => {
20
+ __accessCheck(obj, member, "read from private field");
21
+ return getter ? getter.call(obj) : member.get(obj);
22
+ };
23
+ var __privateAdd = (obj, member, value) => {
24
+ if (member.has(obj))
25
+ throw TypeError("Cannot add the same private member more than once");
26
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
27
+ };
28
+ var __privateSet = (obj, member, value, setter) => {
29
+ __accessCheck(obj, member, "write to private field");
30
+ member.set(obj, value);
31
+ return value;
32
+ };
33
+ var __privateWrapper = (obj, member, setter, getter) => ({
34
+ set _(value) {
35
+ __privateSet(obj, member, value);
36
+ },
37
+ get _() {
38
+ return __privateGet(obj, member, getter);
39
+ }
40
+ });
41
+ var _messages, _promptsHistory, _currentHistoryIndex;
42
+ class ChatController extends EventEmitter {
43
+ constructor(id, welcomeMessage = getLabel("lblAiDashboardWelcomeMessage").text) {
44
+ super();
45
+ this.id = id;
46
+ __privateAdd(this, _messages, []);
47
+ __publicField(this, "components", {
48
+ MessageHistory: () => {
49
+ const messages = this.history.useList();
50
+ return /* @__PURE__ */ jsx(
51
+ Box,
52
+ {
53
+ ...getVariant("layout.common.components.chat"),
54
+ className: "history",
55
+ children: /* @__PURE__ */ jsx(AutoscrollContainer, { children: messages.map((current) => /* @__PURE__ */ jsx(
56
+ Box,
57
+ {
58
+ as: "pre",
59
+ className: `history__message ${current.messageType}`,
60
+ ...typeof current.message === "string" ? { dangerouslySetInnerHTML: { __html: current.message } } : { children: current.message }
61
+ },
62
+ current.id
63
+ )) })
64
+ }
65
+ );
66
+ },
67
+ Textarea: makeTextarea(this)
68
+ });
69
+ __privateAdd(this, _promptsHistory, []);
70
+ __privateAdd(this, _currentHistoryIndex, -1);
71
+ __publicField(this, "history", {
72
+ add: (prompt) => {
73
+ __privateSet(this, _currentHistoryIndex, -1);
74
+ if (prompt === __privateGet(this, _promptsHistory)[__privateGet(this, _promptsHistory).length]) {
75
+ return;
76
+ }
77
+ __privateSet(this, _promptsHistory, [
78
+ prompt,
79
+ ...__privateGet(this, _promptsHistory).slice(
80
+ Math.max(0, __privateGet(this, _promptsHistory).length - 10)
81
+ )
82
+ ]);
83
+ },
84
+ next: () => {
85
+ if (__privateGet(this, _promptsHistory)[__privateGet(this, _currentHistoryIndex) - 1])
86
+ return __privateGet(this, _promptsHistory)[--__privateWrapper(this, _currentHistoryIndex)._];
87
+ else {
88
+ __privateSet(this, _currentHistoryIndex, -1);
89
+ return "";
90
+ }
91
+ },
92
+ previous: () => {
93
+ if (__privateGet(this, _promptsHistory)[__privateGet(this, _currentHistoryIndex) + 1]) {
94
+ return __privateGet(this, _promptsHistory)[++__privateWrapper(this, _currentHistoryIndex)._];
95
+ }
96
+ },
97
+ useList: () => {
98
+ const [messages, setMessages] = useState(__privateGet(this, _messages));
99
+ useEffect(() => {
100
+ return this.on("listChange", (messages2) => setMessages(messages2));
101
+ }, []);
102
+ return messages;
103
+ }
104
+ });
105
+ __publicField(this, "messages", {
106
+ add: (message) => {
107
+ __privateGet(this, _messages).push(message);
108
+ this.emit("listChange", [...__privateGet(this, _messages)]);
109
+ },
110
+ clear: () => {
111
+ __privateSet(this, _messages, []);
112
+ this.emit("listChange", []);
113
+ }
114
+ });
115
+ if (typeof welcomeMessage === "string")
116
+ __privateGet(this, _messages).push(new ChatMessage(welcomeMessage, "system"));
117
+ }
118
+ }
119
+ _messages = new WeakMap();
120
+ _promptsHistory = new WeakMap();
121
+ _currentHistoryIndex = new WeakMap();
122
+
123
+ export { ChatController };
124
+ //# sourceMappingURL=ChatController.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatController.js","sources":["../../../src/components/chat/ChatController.tsx"],"sourcesContent":["/* eslint-disable no-param-reassign */\r\nimport { getVariant } from '@apia/theme';\r\nimport { EventEmitter, getLabel } from '@apia/util';\r\nimport { useEffect, useState } from 'react';\r\nimport { Box } from '@apia/theme';\r\nimport { AutoscrollContainer } from './AutoscrollContainer';\r\nimport { makeTextarea } from './Textarea';\r\nimport { ChatMessage } from './messages/ChatMessage';\r\n\r\nexport type TMessageType =\r\n | 'user'\r\n | 'system'\r\n | 'warning'\r\n | 'error'\r\n | 'information'\r\n | 'response'\r\n | 'multipleChoice';\r\n\r\nexport class ChatController extends EventEmitter<{\r\n listChange: ChatMessage[];\r\n messageSubmited?: string;\r\n}> {\r\n #messages: ChatMessage[] = [];\r\n\r\n constructor(\r\n public id: string,\r\n welcomeMessage: string | false = getLabel('lblAiDashboardWelcomeMessage')\r\n .text,\r\n ) {\r\n super();\r\n if (typeof welcomeMessage === 'string')\r\n this.#messages.push(new ChatMessage(welcomeMessage, 'system'));\r\n }\r\n\r\n components = {\r\n MessageHistory: () => {\r\n const messages = this.history.useList();\r\n\r\n return (\r\n <Box\r\n {...getVariant('layout.common.components.chat')}\r\n className=\"history\"\r\n >\r\n <AutoscrollContainer>\r\n {messages.map((current) => (\r\n <Box\r\n as=\"pre\"\r\n className={`history__message ${current.messageType}`}\r\n key={current.id}\r\n {...(typeof current.message === 'string'\r\n ? { dangerouslySetInnerHTML: { __html: current.message } }\r\n : { children: current.message })}\r\n />\r\n ))}\r\n </AutoscrollContainer>\r\n </Box>\r\n );\r\n },\r\n Textarea: makeTextarea(this),\r\n };\r\n\r\n #promptsHistory: string[] = [];\r\n #currentHistoryIndex = -1;\r\n\r\n history = {\r\n add: (prompt: string) => {\r\n this.#currentHistoryIndex = -1;\r\n\r\n if (prompt === this.#promptsHistory[this.#promptsHistory.length]) {\r\n return;\r\n }\r\n\r\n this.#promptsHistory = [\r\n prompt,\r\n ...this.#promptsHistory.slice(\r\n Math.max(0, this.#promptsHistory.length - 10),\r\n ),\r\n ];\r\n },\r\n next: () => {\r\n if (this.#promptsHistory[this.#currentHistoryIndex - 1])\r\n return this.#promptsHistory[--this.#currentHistoryIndex];\r\n else {\r\n this.#currentHistoryIndex = -1;\r\n return '';\r\n }\r\n },\r\n previous: () => {\r\n if (this.#promptsHistory[this.#currentHistoryIndex + 1]) {\r\n return this.#promptsHistory[++this.#currentHistoryIndex];\r\n }\r\n },\r\n useList: () => {\r\n const [messages, setMessages] = useState<ChatMessage[]>(this.#messages);\r\n\r\n useEffect(() => {\r\n return this.on('listChange', (messages) => setMessages(messages));\r\n }, []);\r\n\r\n return messages;\r\n },\r\n };\r\n\r\n messages = {\r\n add: (message: ChatMessage) => {\r\n this.#messages.push(message);\r\n this.emit('listChange', [...this.#messages]);\r\n },\r\n clear: () => {\r\n this.#messages = [];\r\n this.emit('listChange', []);\r\n },\r\n };\r\n}\r\n"],"names":["messages"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAA,SAAA,EAAA,eAAA,EAAA,oBAAA,CAAA;AAkBO,MAAM,uBAAuB,YAGjC,CAAA;AAAA,EAGD,YACS,EACP,EAAA,cAAA,GAAiC,QAAS,CAAA,8BAA8B,EACrE,IACH,EAAA;AACA,IAAM,KAAA,EAAA,CAAA;AAJC,IAAA,IAAA,CAAA,EAAA,GAAA,EAAA,CAAA;AAHT,IAAA,YAAA,CAAA,IAAA,EAAA,SAAA,EAA2B,EAAC,CAAA,CAAA;AAY5B,IAAa,aAAA,CAAA,IAAA,EAAA,YAAA,EAAA;AAAA,MACX,gBAAgB,MAAM;AACpB,QAAM,MAAA,QAAA,GAAW,IAAK,CAAA,OAAA,CAAQ,OAAQ,EAAA,CAAA;AAEtC,QACE,uBAAA,GAAA;AAAA,UAAC,GAAA;AAAA,UAAA;AAAA,YACE,GAAG,WAAW,+BAA+B,CAAA;AAAA,YAC9C,SAAU,EAAA,SAAA;AAAA,YAEV,QAAC,kBAAA,GAAA,CAAA,mBAAA,EAAA,EACE,QAAS,EAAA,QAAA,CAAA,GAAA,CAAI,CAAC,OACb,qBAAA,GAAA;AAAA,cAAC,GAAA;AAAA,cAAA;AAAA,gBACC,EAAG,EAAA,KAAA;AAAA,gBACH,SAAA,EAAW,CAAoB,iBAAA,EAAA,OAAA,CAAQ,WAAW,CAAA,CAAA;AAAA,gBAEjD,GAAI,OAAO,OAAA,CAAQ,OAAY,KAAA,QAAA,GAC5B,EAAE,uBAAyB,EAAA,EAAE,MAAQ,EAAA,OAAA,CAAQ,SAAU,EAA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AAA,CAAA;AAlFE,IAAA,IAAI,OAAO,cAAmB,KAAA,QAAA;AAC5B,MAAA,YAAA,CAAA,IAAA,EAAK,WAAU,IAAK,CAAA,IAAI,WAAY,CAAA,cAAA,EAAgB,QAAQ,CAAC,CAAA,CAAA;AAAA,GACjE;AAiFF,CAAA;AA3FE,SAAA,GAAA,IAAA,OAAA,EAAA,CAAA;AAuCA,eAAA,GAAA,IAAA,OAAA,EAAA,CAAA;AACA,oBAAA,GAAA,IAAA,OAAA,EAAA;;;;"}
@@ -0,0 +1,218 @@
1
+ import { jsxs, jsx } from '@apia/theme/jsx-runtime';
2
+ import { useState, useCallback } from 'react';
3
+ import { IconButton, SimpleButton } from '@apia/components';
4
+ import { Box, Textarea } from '@apia/theme';
5
+ import useHandleFileUpload, { WrongFormatError } from './files/useHandleFileUpload.js';
6
+ import { getLabel } from '@apia/util';
7
+ import { ChatMessage } from './messages/ChatMessage.js';
8
+
9
+ const makeTextarea = (gptController) => {
10
+ const NewTextarea = ({
11
+ hideDeleteButton,
12
+ isLoading: outIsLoading,
13
+ onSubmit,
14
+ preventAppendUserMessages
15
+ }) => {
16
+ const [isLoading, setIsLoading] = useState(false);
17
+ const submit = useCallback(
18
+ (text) => {
19
+ if (preventAppendUserMessages !== true)
20
+ gptController.messages.add(new ChatMessage(text, "user"));
21
+ gptController.history.add(text);
22
+ onSubmit(text).finally(() => {
23
+ setIsLoading(false);
24
+ });
25
+ },
26
+ [onSubmit, preventAppendUserMessages]
27
+ );
28
+ const handleSubmit = useCallback(
29
+ (ev) => {
30
+ ev.preventDefault();
31
+ const textarea = document.getElementById(
32
+ `GPTMessage${gptController.id}`
33
+ );
34
+ if (textarea.value) {
35
+ setIsLoading(true);
36
+ submit(textarea.value);
37
+ }
38
+ textarea.focus();
39
+ textarea.value = "";
40
+ },
41
+ [submit]
42
+ );
43
+ const onKeyDown = useCallback((ev) => {
44
+ if (ev.target.readOnly)
45
+ return;
46
+ if (ev.code === "Enter" && !ev.shiftKey) {
47
+ ev.preventDefault();
48
+ ev.target.closest("form")?.querySelector('button[type="submit"]')?.click();
49
+ }
50
+ }, []);
51
+ const { progress, handleFileUpload } = useHandleFileUpload({
52
+ onContent(content) {
53
+ document.getElementById(
54
+ `GPTMessage${gptController.id}`
55
+ ).value = content.join("\n\n");
56
+ }
57
+ });
58
+ const actualIsLoading = isLoading || progress !== 100 || outIsLoading;
59
+ const [dragging, setDragging] = useState(false);
60
+ const handleDragLeave = (ev) => {
61
+ ev.preventDefault();
62
+ setDragging(false);
63
+ };
64
+ const handleDrop = (ev) => {
65
+ ev.preventDefault();
66
+ setDragging(false);
67
+ const fileElement = document.getElementById(
68
+ `gptUploadFile__${gptController.id}`
69
+ );
70
+ fileElement.files = ev.dataTransfer.files;
71
+ const event = new Event("change", { bubbles: true });
72
+ fileElement.dispatchEvent(event);
73
+ };
74
+ const handleDragOver = (ev) => {
75
+ ev.preventDefault();
76
+ };
77
+ const handleDragEnter = (ev) => {
78
+ ev.preventDefault();
79
+ setDragging(true);
80
+ };
81
+ return /* @__PURE__ */ jsxs(
82
+ "form",
83
+ {
84
+ onDrop: handleDrop,
85
+ onDragOver: handleDragOver,
86
+ onDragEnter: handleDragEnter,
87
+ onDragLeave: handleDragLeave,
88
+ onSubmit: handleSubmit,
89
+ className: "gpt__form",
90
+ action: "",
91
+ children: [
92
+ dragging ? /* @__PURE__ */ jsx(
93
+ Box,
94
+ {
95
+ sx: {
96
+ height: "150px",
97
+ border: "3px dashed #aaa",
98
+ width: "100%",
99
+ display: "flex",
100
+ alignItems: "center",
101
+ justifyContent: "center"
102
+ },
103
+ children: getLabel("msgAiDragAndDrop").text
104
+ }
105
+ ) : /* @__PURE__ */ jsx(
106
+ Textarea,
107
+ {
108
+ onKeyDown,
109
+ id: `GPTMessage${gptController.id}`,
110
+ readOnly: actualIsLoading
111
+ }
112
+ ),
113
+ /* @__PURE__ */ jsxs(Box, { className: "gpt__buttonsContainer", children: [
114
+ /* @__PURE__ */ jsx(Box, { sx: { display: "none" }, children: /* @__PURE__ */ jsx(
115
+ "input",
116
+ {
117
+ type: "file",
118
+ className: "gptUploadFile",
119
+ id: `gptUploadFile__${gptController.id}`,
120
+ onChange: (ev) => {
121
+ handleFileUpload(ev).catch((e) => {
122
+ if (e instanceof WrongFormatError) {
123
+ setIsLoading(false);
124
+ gptController.messages.add(
125
+ new ChatMessage(
126
+ getLabel("msgAiFileExtensionError").text,
127
+ "error"
128
+ )
129
+ );
130
+ } else {
131
+ throw e;
132
+ }
133
+ });
134
+ }
135
+ }
136
+ ) }),
137
+ /* @__PURE__ */ jsx(
138
+ IconButton,
139
+ {
140
+ title: getLabel("btnUploadFile").text,
141
+ icon: "File",
142
+ variant: "icon-outline",
143
+ disabled: actualIsLoading,
144
+ onClick: () => {
145
+ document.getElementById(
146
+ `gptUploadFile__${gptController.id}`
147
+ ).click();
148
+ },
149
+ size: "Lg"
150
+ }
151
+ ),
152
+ /* @__PURE__ */ jsx(
153
+ SimpleButton,
154
+ {
155
+ title: getLabel("btnAiPrevious").tooltip,
156
+ variant: "outline",
157
+ type: "button",
158
+ onClick: () => {
159
+ const historyValue = gptController.history.previous();
160
+ if (historyValue)
161
+ document.getElementById(
162
+ `GPTMessage${gptController.id}`
163
+ ).value = historyValue;
164
+ },
165
+ size: "sm",
166
+ children: getLabel("btnAiPrevious").text
167
+ }
168
+ ),
169
+ /* @__PURE__ */ jsx(
170
+ SimpleButton,
171
+ {
172
+ title: getLabel("btnAiNext").tooltip,
173
+ variant: "outline",
174
+ type: "button",
175
+ onClick: () => {
176
+ const historyValue = gptController.history.next();
177
+ document.getElementById(
178
+ `GPTMessage${gptController.id}`
179
+ ).value = historyValue ?? "";
180
+ },
181
+ size: "sm",
182
+ children: getLabel("btnAiNext").text
183
+ }
184
+ ),
185
+ !hideDeleteButton && /* @__PURE__ */ jsx(
186
+ SimpleButton,
187
+ {
188
+ title: getLabel("btnAiDelete").tooltip,
189
+ variant: "outline",
190
+ type: "button",
191
+ onClick: () => document.getElementById(
192
+ `GPTMessage${gptController.id}`
193
+ ).value = "",
194
+ size: "sm",
195
+ children: getLabel("btnAiDelete").text
196
+ }
197
+ ),
198
+ /* @__PURE__ */ jsx(
199
+ SimpleButton,
200
+ {
201
+ title: getLabel("btnAiSend").tooltip,
202
+ isLoading: actualIsLoading,
203
+ type: "submit",
204
+ size: "sm",
205
+ children: getLabel("btnAiSend").text
206
+ }
207
+ )
208
+ ] })
209
+ ]
210
+ }
211
+ );
212
+ };
213
+ NewTextarea.displayName = "";
214
+ return NewTextarea;
215
+ };
216
+
217
+ export { makeTextarea };
218
+ //# sourceMappingURL=Textarea.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"Textarea.js","sources":["../../../src/components/chat/Textarea.tsx"],"sourcesContent":["/* eslint-disable no-param-reassign */\r\nimport { FormEvent, KeyboardEvent, useCallback, useState } from 'react';\r\nimport { ChatController } from './ChatController';\r\nimport { IconButton, SimpleButton } from '@apia/components';\r\nimport { Box, Textarea } from '@apia/theme';\r\nimport useHandleFileUpload, {\r\n WrongFormatError,\r\n} from './files/useHandleFileUpload';\r\nimport { getLabel } from '@apia/util';\r\nimport { ChatMessage } from './messages/ChatMessage';\r\n\r\nexport const makeTextarea = (gptController: ChatController) => {\r\n const NewTextarea = ({\r\n hideDeleteButton,\r\n isLoading: outIsLoading,\r\n onSubmit,\r\n preventAppendUserMessages,\r\n }: {\r\n hideDeleteButton?: boolean;\r\n isLoading?: boolean;\r\n onSubmit: (text: string) => Promise<void>;\r\n preventAppendUserMessages?: boolean;\r\n }) => {\r\n const [isLoading, setIsLoading] = useState(false);\r\n\r\n const submit = useCallback(\r\n (text: string) => {\r\n if (preventAppendUserMessages !== true)\r\n gptController.messages.add(new ChatMessage(text, 'user'));\r\n\r\n gptController.history.add(text);\r\n\r\n onSubmit(text).finally(() => {\r\n setIsLoading(false);\r\n });\r\n },\r\n [onSubmit, preventAppendUserMessages],\r\n );\r\n\r\n const handleSubmit = useCallback(\r\n (ev: FormEvent<HTMLFormElement>) => {\r\n ev.preventDefault();\r\n //setIsLoading(true);\r\n const textarea = document.getElementById(\r\n `GPTMessage${gptController.id}`,\r\n ) as HTMLTextAreaElement;\r\n\r\n if (textarea.value) {\r\n setIsLoading(true);\r\n submit(textarea.value);\r\n }\r\n\r\n textarea.focus();\r\n textarea.value = '';\r\n },\r\n [submit],\r\n );\r\n\r\n const onKeyDown = useCallback((ev: KeyboardEvent) => {\r\n if ((ev.target as HTMLTextAreaElement).readOnly) return;\r\n\r\n if (ev.code === 'Enter' && !ev.shiftKey) {\r\n ev.preventDefault();\r\n (ev.target as HTMLElement)\r\n .closest('form')\r\n ?.querySelector<HTMLButtonElement>('button[type=\"submit\"]')\r\n ?.click();\r\n }\r\n }, []);\r\n\r\n const { progress, handleFileUpload } = useHandleFileUpload({\r\n onContent(content) {\r\n (\r\n document.getElementById(\r\n `GPTMessage${gptController.id}`,\r\n ) as HTMLTextAreaElement\r\n ).value = content.join('\\n\\n');\r\n },\r\n });\r\n\r\n const actualIsLoading = isLoading || progress !== 100 || outIsLoading;\r\n\r\n const [dragging, setDragging] = useState(false);\r\n\r\n const handleDragLeave = (ev: React.DragEvent<HTMLElement>) => {\r\n ev.preventDefault();\r\n setDragging(false);\r\n };\r\n\r\n const handleDrop = (ev: React.DragEvent<HTMLFormElement>) => {\r\n ev.preventDefault();\r\n setDragging(false);\r\n const fileElement = document.getElementById(\r\n `gptUploadFile__${gptController.id}`,\r\n ) as HTMLInputElement;\r\n fileElement.files = ev.dataTransfer.files;\r\n const event = new Event('change', { bubbles: true });\r\n fileElement.dispatchEvent(event);\r\n };\r\n\r\n const handleDragOver = (ev: React.DragEvent<HTMLElement>) => {\r\n ev.preventDefault();\r\n };\r\n\r\n const handleDragEnter = (ev: React.DragEvent<HTMLElement>) => {\r\n ev.preventDefault();\r\n setDragging(true);\r\n };\r\n\r\n return (\r\n <form\r\n onDrop={handleDrop}\r\n onDragOver={handleDragOver}\r\n onDragEnter={handleDragEnter}\r\n onDragLeave={handleDragLeave}\r\n onSubmit={handleSubmit}\r\n className=\"gpt__form\"\r\n action=\"\"\r\n >\r\n {dragging ? (\r\n <Box\r\n sx={{\r\n height: '150px',\r\n border: '3px dashed #aaa',\r\n width: '100%',\r\n display: 'flex',\r\n alignItems: 'center',\r\n justifyContent: 'center',\r\n }}\r\n >\r\n {getLabel('msgAiDragAndDrop').text}\r\n </Box>\r\n ) : (\r\n <Textarea\r\n onKeyDown={onKeyDown}\r\n id={`GPTMessage${gptController.id}`}\r\n readOnly={actualIsLoading}\r\n />\r\n )}\r\n\r\n <Box className=\"gpt__buttonsContainer\">\r\n <Box sx={{ display: 'none' }}>\r\n <input\r\n type=\"file\"\r\n className=\"gptUploadFile\"\r\n id={`gptUploadFile__${gptController.id}`}\r\n onChange={(ev) => {\r\n handleFileUpload(ev).catch((e) => {\r\n if (e instanceof WrongFormatError) {\r\n setIsLoading(false);\r\n gptController.messages.add(\r\n new ChatMessage(\r\n getLabel('msgAiFileExtensionError').text,\r\n 'error',\r\n ),\r\n );\r\n } else {\r\n throw e;\r\n }\r\n });\r\n }}\r\n />\r\n </Box>\r\n <IconButton\r\n title={getLabel('btnUploadFile').text}\r\n icon=\"File\"\r\n variant=\"icon-outline\"\r\n disabled={actualIsLoading}\r\n onClick={() => {\r\n (\r\n document.getElementById(\r\n `gptUploadFile__${gptController.id}`,\r\n ) as HTMLElement\r\n ).click();\r\n }}\r\n size=\"Lg\"\r\n />\r\n <SimpleButton\r\n title={getLabel('btnAiPrevious').tooltip}\r\n variant=\"outline\"\r\n type=\"button\"\r\n onClick={() => {\r\n const historyValue = gptController.history.previous();\r\n if (historyValue)\r\n (\r\n document.getElementById(\r\n `GPTMessage${gptController.id}`,\r\n ) as HTMLTextAreaElement\r\n ).value = historyValue;\r\n }}\r\n size=\"sm\"\r\n >\r\n {getLabel('btnAiPrevious').text}\r\n </SimpleButton>\r\n <SimpleButton\r\n title={getLabel('btnAiNext').tooltip}\r\n variant=\"outline\"\r\n type=\"button\"\r\n onClick={() => {\r\n const historyValue = gptController.history.next();\r\n (\r\n document.getElementById(\r\n `GPTMessage${gptController.id}`,\r\n ) as HTMLTextAreaElement\r\n ).value = historyValue ?? '';\r\n }}\r\n size=\"sm\"\r\n >\r\n {getLabel('btnAiNext').text}\r\n </SimpleButton>\r\n {!hideDeleteButton && (\r\n <SimpleButton\r\n title={getLabel('btnAiDelete').tooltip}\r\n variant=\"outline\"\r\n type=\"button\"\r\n onClick={() =>\r\n ((\r\n document.getElementById(\r\n `GPTMessage${gptController.id}`,\r\n ) as HTMLInputElement\r\n ).value = '')\r\n }\r\n size=\"sm\"\r\n >\r\n {getLabel('btnAiDelete').text}\r\n </SimpleButton>\r\n )}\r\n <SimpleButton\r\n title={getLabel('btnAiSend').tooltip}\r\n isLoading={actualIsLoading}\r\n type=\"submit\"\r\n size=\"sm\"\r\n >\r\n {getLabel('btnAiSend').text}\r\n </SimpleButton>\r\n </Box>\r\n </form>\r\n );\r\n };\r\n\r\n NewTextarea.displayName = '';\r\n\r\n return NewTextarea;\r\n};\r\n"],"names":[],"mappings":";;;;;;;;AAWa,MAAA,YAAA,GAAe,CAAC,aAAkC,KAAA;AAC7D,EAAA,MAAM,cAAc,CAAC;AAAA,IACnB,gBAAA;AAAA,IACA,SAAW,EAAA,YAAA;AAAA,IACX,QAAA;AAAA,IACA,yBAAA;AAAA,GAMI,KAAA;AACJ,IAAA,MAAM,CAAC,SAAA,EAAW,YAAY,CAAA,GAAI,SAAS,KAAK,CAAA,CAAA;AAEhD,IAAA,MAAM,MAAS,GAAA,WAAA;AAAA,MACb,CAAC,IAAiB,KAAA;AAChB,QAAA,IAAI,yBAA8B,KAAA,IAAA;AAChC,UAAA,aAAA,CAAc,SAAS,GAAI,CAAA,IAAI,WAAY,CAAA,IAAA,EAAM,MAAM,CAAC,CAAA,CAAA;AAE1D,QAAc,aAAA,CAAA,OAAA,CAAQ,IAAI,IAAI,CAAA,CAAA;AAE9B,QAAS,QAAA,CAAA,IAAI,CAAE,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AA,eAAA;AAAA,gBACX,IAAK,EAAA,QAAA;AAAA,gBACL,IAAK,EAAA,IAAA;AAAA,gBAEJ,QAAA,EAAA,QAAA,CAAS,WAAW,CAAE,CAAA,IAAA;AAAA,eAAA;AAAA,aACzB;AAAA,WACF,EAAA,CAAA;AAAA,SAAA;AAAA,OAAA;AAAA,KACF,CAAA;AAAA,GAEJ,CAAA;AAEA,EAAA,WAAA,CAAY,WAAc,GAAA,EAAA,CAAA;AAE1B,EAAO,OAAA,WAAA,CAAA;AACT;;;;"}
@@ -0,0 +1,61 @@
1
+ import { useState, useCallback } from 'react';
2
+ import getImageOcrMaker, { isImageFile } from '../../../files/ocr/getImageOcrMaker.js';
3
+ import getPdfReader, { isPdfFile } from '../../../files/pdf/getPdfReader.js';
4
+
5
+ class WrongFormatError extends Error {
6
+ constructor(fileName) {
7
+ super(`File format not supported: ${fileName}`);
8
+ }
9
+ }
10
+ function useHandleFileUpload({
11
+ onContent
12
+ }) {
13
+ const [progress, setProgress] = useState(100);
14
+ const handleFileUpload = useCallback(
15
+ async (ev) => {
16
+ setProgress(0);
17
+ const imageProcessor = getImageOcrMaker();
18
+ const pdfProcessor = getPdfReader();
19
+ await (async () => {
20
+ const obtainedText = [];
21
+ const files = ev.target.files;
22
+ const filesLength = ev.target.files?.length ?? 0;
23
+ if (files) {
24
+ for (let i = 0; i < filesLength; i++) {
25
+ const file = files[i];
26
+ if (!isImageFile(file) && !isPdfFile(file)) {
27
+ setProgress(100);
28
+ throw new WrongFormatError(file.name);
29
+ }
30
+ const handleProgress = (progress2) => {
31
+ setProgress(
32
+ i / filesLength * 100 + 1 / filesLength * 100 * (progress2 / 100)
33
+ );
34
+ };
35
+ let result = "";
36
+ if (imageProcessor.willProcessFile(file)) {
37
+ try {
38
+ result = (await imageProcessor.processFile(file, handleProgress))[0];
39
+ } catch (e) {
40
+ console.error(e);
41
+ }
42
+ } else if (pdfProcessor.willProcessFile(file)) {
43
+ result = (await pdfProcessor.processFile(file, handleProgress)).join("\n\n");
44
+ }
45
+ if (result) {
46
+ obtainedText.push(result);
47
+ }
48
+ setProgress((i + 1) / filesLength * 100);
49
+ }
50
+ }
51
+ onContent(obtainedText);
52
+ })();
53
+ ev.target.value = "";
54
+ },
55
+ [onContent]
56
+ );
57
+ return { progress, handleFileUpload };
58
+ }
59
+
60
+ export { WrongFormatError, useHandleFileUpload as default };
61
+ //# sourceMappingURL=useHandleFileUpload.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useHandleFileUpload.js","sources":["../../../../src/components/chat/files/useHandleFileUpload.ts"],"sourcesContent":["/* eslint-disable no-param-reassign */\r\nimport { ChangeEvent, useCallback, useState } from 'react';\r\nimport getImageOcrMaker, {\r\n isImageFile,\r\n} from '../../../files/ocr/getImageOcrMaker';\r\nimport getPdfReader, { isPdfFile } from '../../../files/pdf/getPdfReader';\r\n\r\nexport class WrongFormatError extends Error {\r\n constructor(fileName: string) {\r\n super(`File format not supported: ${fileName}`);\r\n }\r\n}\r\n\r\nexport default function useHandleFileUpload({\r\n onContent,\r\n}: {\r\n onContent: (content: string[]) => unknown;\r\n}) {\r\n const [progress, setProgress] = useState(100);\r\n\r\n const handleFileUpload = useCallback(\r\n async (ev: ChangeEvent<HTMLInputElement>) => {\r\n setProgress(0);\r\n const imageProcessor = getImageOcrMaker();\r\n const pdfProcessor = getPdfReader();\r\n\r\n await (async () => {\r\n const obtainedText: string[] = [];\r\n const files = ev.target.files;\r\n const filesLength = ev.target.files?.length ?? 0;\r\n if (files) {\r\n for (let i = 0; i < filesLength; i++) {\r\n const file = files[i];\r\n\r\n if (!isImageFile(file) && !isPdfFile(file)) {\r\n setProgress(100);\r\n throw new WrongFormatError(file.name);\r\n }\r\n\r\n const handleProgress = (progress: number) => {\r\n setProgress(\r\n (i / filesLength) * 100 +\r\n (1 / filesLength) * 100 * (progress / 100),\r\n );\r\n };\r\n\r\n let result = '';\r\n if (imageProcessor.willProcessFile(file)) {\r\n try {\r\n result = (\r\n await imageProcessor.processFile(file, handleProgress)\r\n )[0];\r\n } catch (e) {\r\n console.error(e);\r\n }\r\n } else if (pdfProcessor.willProcessFile(file)) {\r\n result = (\r\n await pdfProcessor.processFile(file, handleProgress)\r\n ).join('\\n\\n');\r\n try {\r\n } catch (e) {\r\n console.error(e);\r\n }\r\n }\r\n\r\n if (result) {\r\n obtainedText.push(result);\r\n }\r\n\r\n setProgress(((i + 1) / filesLength) * 100);\r\n }\r\n }\r\n onContent(obtainedText);\r\n })();\r\n\r\n ev.target.value = '';\r\n },\r\n [onContent],\r\n );\r\n\r\n return { progress, handleFileUpload };\r\n}\r\n"],"names":["progress"],"mappings":";;;;AAOO,MAAM,yBAAyB,KAAM,CAAA;AAAA,EAC1C,YAAY,QAAkB,EAAA;AAC5B,IAAM,KAAA,CAAA,CAAA,2BAAA,EAA8B,QAAQ,CAAE,CAAA,CAAA,CAAA;AAAA,GAChD;AACF,CAAA;AAEA,SAAwB,mBAAoB,CAAA;AAAA,EAC1C,SAAA;AACF,CAEG,EAAA;AACD,EAAA,MAAM,CAAC,QAAA,EAAU,WAAW,CAAA,GAAI,SAAS,GAAG,CAAA,CAAA;AAE5C,EAAA,MAAM,gBAAmB,GAAA,WAAA;AAAA,IACvB,OAAO,EAAsC,KAAA;AAC3C,MAAA,WAAA,CAAY,CAAC,CAAA,CAAA;AACb,MAAA,MAAM,iBAAiB,gBAAiB,EAAA,CAAA;AACxC,MAAA,MAAM,eAAe,YAAa,EAAA,CAAA;AAElC,MAAA,MAAA,CAAO,YAAY;AACjB,QAAA,MAAM,eAAyB,EAAC,CAAA;AAChC,QAAM,MAAA,KAAA,GAAQ,GAAG,MAAO,CAAA,KAAA,CAAA;AACxB,QAAA,MAAM,WAAc,GAAA,EAAA,CAAG,MAAO,CAAA,KAAA,EAAO,MAAU,IAAA,CAAA,CAAA;AAC/C,QAAA,IAAI,KAAO,EAAA;AACT,UAAA,KAAA,IAAS,CAAI,GAAA,CAAA,EAAG,CAAI,GAAA,WAAA,EAAa,CAA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AAA;AAEH,MAAA,EAAA,CAAG,OAAO,KAAQ,GAAA,EAAA,CAAA;AAAA,KACpB;AAAA,IACA,CAAC,SAAS,CAAA;AAAA,GACZ,CAAA;AAEA,EAAO,OAAA,EAAE,UAAU,gBAAiB,EAAA,CAAA;AACtC;;;;"}
@@ -0,0 +1,14 @@
1
+ import { ReactNode } from 'react';
2
+ import { TMessageType } from '../ChatController.js';
3
+
4
+ declare class ChatMessage {
5
+ #private;
6
+ messageType: TMessageType;
7
+ id: number;
8
+ parseMessage(message: string): string;
9
+ constructor(message: ReactNode, messageType: TMessageType);
10
+ get message(): ReactNode;
11
+ }
12
+
13
+ export { ChatMessage };
14
+ //# sourceMappingURL=ChatMessage.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatMessage.d.ts","sources":[],"sourcesContent":[],"names":[],"mappings":""}
@@ -0,0 +1,52 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
3
+ var __publicField = (obj, key, value) => {
4
+ __defNormalProp(obj, key + "" , value);
5
+ return value;
6
+ };
7
+ var __accessCheck = (obj, member, msg) => {
8
+ if (!member.has(obj))
9
+ throw TypeError("Cannot " + msg);
10
+ };
11
+ var __privateGet = (obj, member, getter) => {
12
+ __accessCheck(obj, member, "read from private field");
13
+ return getter ? getter.call(obj) : member.get(obj);
14
+ };
15
+ var __privateAdd = (obj, member, value) => {
16
+ if (member.has(obj))
17
+ throw TypeError("Cannot add the same private member more than once");
18
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
19
+ };
20
+ var __privateSet = (obj, member, value, setter) => {
21
+ __accessCheck(obj, member, "write to private field");
22
+ member.set(obj, value);
23
+ return value;
24
+ };
25
+ var _message;
26
+ let maxId = 0;
27
+ class ChatMessage {
28
+ constructor(message, messageType) {
29
+ this.messageType = messageType;
30
+ __publicField(this, "id");
31
+ __privateAdd(this, _message, void 0);
32
+ this.id = maxId++;
33
+ __privateSet(this, _message, message);
34
+ if (messageType === "response")
35
+ this.messageType = "system";
36
+ if (typeof message === "string")
37
+ __privateSet(this, _message, this.parseMessage(message));
38
+ }
39
+ parseMessage(message) {
40
+ let result = message;
41
+ result = result.replaceAll("[strong]", "<strong>");
42
+ result = result.replaceAll("[/strong]", "</strong>");
43
+ return result;
44
+ }
45
+ get message() {
46
+ return __privateGet(this, _message);
47
+ }
48
+ }
49
+ _message = new WeakMap();
50
+
51
+ export { ChatMessage };
52
+ //# sourceMappingURL=ChatMessage.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ChatMessage.js","sources":["../../../../src/components/chat/messages/ChatMessage.tsx"],"sourcesContent":["import { ReactNode } from 'react';\r\nimport { TMessageType } from '../ChatController';\r\n\r\nlet maxId = 0;\r\n\r\nexport class ChatMessage {\r\n id: number;\r\n #message: ReactNode;\r\n\r\n parseMessage(message: string) {\r\n let result = message;\r\n\r\n result = result.replaceAll('[strong]', '<strong>');\r\n result = result.replaceAll('[/strong]', '</strong>');\r\n\r\n return result;\r\n }\r\n\r\n constructor(message: ReactNode, public messageType: TMessageType) {\r\n this.id = maxId++;\r\n this.#message = message;\r\n\r\n if (messageType === 'response') this.messageType = 'system';\r\n if (typeof message === 'string') this.#message = this.parseMessage(message);\r\n }\r\n\r\n get message() {\r\n return this.#message;\r\n }\r\n}\r\n"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;AAAA,IAAA,QAAA,CAAA;AAGA,IAAI,KAAQ,GAAA,CAAA,CAAA;AAEL,MAAM,WAAY,CAAA;AAAA,EAavB,WAAA,CAAY,SAA2B,WAA2B,EAAA;AAA3B,IAAA,IAAA,CAAA,WAAA,GAAA,WAAA,CAAA;AAZvC,IAAA,aAAA,CAAA,IAAA,EAAA,IAAA,CAAA,CAAA;AACA,IAAA,YAAA,CAAA,IAAA,EAAA,QAAA,EAAA,KAAA,CAAA,CAAA,CAAA;AAYE,IAAA,IAAA,CAAK,EAAK,GAAA,KAAA,EAAA,CAAA;AACV,IAAA,YAAA,CAAA,IAAA,EAAK,QAAW,EAAA,OAAA,CAAA,CAAA;AAEhB,IAAA,IAAI,WAAgB,KAAA,UAAA;AAAY,MAAA,IAAA,CAAK,WAAc,GAAA,QAAA,CAAA;AACnD,IAAA,IAAI,OAAO,OAAY,KAAA,QAAA;AAAU,MAAK,YAAA,CAAA,IAAA,EAAA,QAAA,EAAW,IAAK,CAAA,YAAA,CAAa,OAAO,CAAA,CAAA,CAAA;AAAA,GAC5E;AAAA,EAfA,aAAa,OAAiB,EAAA;AAC5B,IAAA,IAAI,MAAS,GAAA,OAAA,CAAA;AAEb,IAAS,MAAA,GAAA,MAAA,CAAO,UAAW,CAAA,UAAA,EAAY,UAAU,CAAA,CAAA;AACjD,IAAS,MAAA,GAAA,MAAA,CAAO,UAAW,CAAA,WAAA,EAAa,WAAW,CAAA,CAAA;AAEnD,IAAO,OAAA,MAAA,CAAA;AAAA,GACT;AAAA,EAUA,IAAI,OAAU,GAAA;AACZ,IAAA,OAAO,YAAK,CAAA,IAAA,EAAA,QAAA,CAAA,CAAA;AAAA,GACd;AACF,CAAA;AAtBE,QAAA,GAAA,IAAA,OAAA,EAAA;;;;"}