@agentica/chat 0.43.3 → 0.44.0-dev.20260313
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/LICENSE +21 -21
- package/README.md +218 -218
- package/dist/assets/{Divider-DX1xhf0M.js → Divider-Cos-Ru5Z.js} +1 -1
- package/dist/assets/VendorConfigurationMovie-C1-X-OfL.js +2126 -0
- package/dist/assets/_accessExpressionAsString-CTLekF-M.js +1 -0
- package/dist/assets/bbs/index-BQdHwlZB.js +41 -0
- package/dist/assets/client-B8OTM7em.js +141 -0
- package/dist/assets/{index-BIyKUwoU.js → index-BtWQqbio.js} +1 -1
- package/dist/assets/shopping/index.html-BG-EZSvQ.js +39 -0
- package/dist/assets/uploader/index.html-xsq3E3-m.js +17 -0
- package/dist/bbs/index.html +32 -32
- package/dist/index.html +31 -31
- package/dist/shopping/index.html +33 -33
- package/dist/uploader/index.html +32 -32
- package/lib/examples/shopping/ShoppingChatApplication.d.ts +1 -1
- package/lib/examples/uploader/AgenticaChatUploaderMovie.d.ts +1 -1
- package/lib/index.mjs +26 -26
- package/lib/index.mjs.map +1 -1
- package/package.json +7 -6
- package/src/AgenticaChatApplication.tsx +13 -13
- package/src/components/MarkdownViewer.tsx +32 -32
- package/src/index.ts +1 -1
- package/src/movies/AgenticaChatMovie.tsx +381 -381
- package/src/movies/messages/AgenticaChatAssistantMessageMovie.tsx +44 -44
- package/src/movies/messages/AgenticaChatDescribeMessageMovie.tsx +63 -63
- package/src/movies/messages/AgenticaChatExecuteMessageMovie.tsx +46 -46
- package/src/movies/messages/AgenticaChatMessageMovie.tsx +43 -43
- package/src/movies/messages/AgenticaChatSelectMessageMovie.tsx +72 -72
- package/src/movies/messages/AgenticaChatSystemMessageMovie.tsx +44 -44
- package/src/movies/messages/AgenticaChatUserMessageMovie.tsx +56 -56
- package/src/movies/sides/AgenticaChatFunctionStackSideMovie.tsx +48 -48
- package/src/movies/sides/AgenticaChatSideMovie.tsx +75 -75
- package/src/movies/sides/AgenticaChatTokenUsageSideMovie.tsx +86 -86
- package/dist/assets/VendorConfigurationMovie-Cpx1jhtO.js +0 -2325
- package/dist/assets/_accessExpressionAsString-J5NhZ9FO.js +0 -1
- package/dist/assets/bbs/index-DxVHwWX6.js +0 -41
- package/dist/assets/client-B3aq8qpT.js +0 -141
- package/dist/assets/shopping/index.html-B8CDZvK4.js +0 -3
- package/dist/assets/uploader/index.html-taNoaQTw.js +0 -17
|
@@ -1,381 +1,381 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
AgenticaAssistantMessageEvent,
|
|
3
|
-
AgenticaDescribeEvent,
|
|
4
|
-
AgenticaHistory,
|
|
5
|
-
AgenticaJsonParseErrorEvent,
|
|
6
|
-
AgenticaOperationSelection,
|
|
7
|
-
AgenticaSelectEvent,
|
|
8
|
-
AgenticaTokenUsage,
|
|
9
|
-
AgenticaUserMessageEvent,
|
|
10
|
-
AgenticaValidateEvent,
|
|
11
|
-
MicroAgentica,
|
|
12
|
-
} from "@agentica/core";
|
|
13
|
-
import type {
|
|
14
|
-
Theme,
|
|
15
|
-
} from "@mui/material";
|
|
16
|
-
import type { ReactElement } from "react";
|
|
17
|
-
|
|
18
|
-
import {
|
|
19
|
-
Agentica,
|
|
20
|
-
} from "@agentica/core";
|
|
21
|
-
import AddAPhotoIcon from "@mui/icons-material/AddAPhoto";
|
|
22
|
-
import ReceiptLongIcon from "@mui/icons-material/ReceiptLong";
|
|
23
|
-
import SendIcon from "@mui/icons-material/Send";
|
|
24
|
-
import {
|
|
25
|
-
AppBar,
|
|
26
|
-
Button,
|
|
27
|
-
Container,
|
|
28
|
-
Drawer,
|
|
29
|
-
IconButton,
|
|
30
|
-
Input,
|
|
31
|
-
Toolbar,
|
|
32
|
-
Typography,
|
|
33
|
-
useMediaQuery,
|
|
34
|
-
useTheme,
|
|
35
|
-
} from "@mui/material";
|
|
36
|
-
import { toPng } from "html-to-image";
|
|
37
|
-
import React, { useEffect, useRef, useState } from "react";
|
|
38
|
-
|
|
39
|
-
import { AgenticaChatMessageMovie } from "./messages/AgenticaChatMessageMovie";
|
|
40
|
-
import { AgenticaChatSideMovie } from "./sides/AgenticaChatSideMovie";
|
|
41
|
-
|
|
42
|
-
const SIDE_WIDTH = 450;
|
|
43
|
-
|
|
44
|
-
export function AgenticaChatMovie({
|
|
45
|
-
agent,
|
|
46
|
-
title,
|
|
47
|
-
}: AgenticaChatMovie.IProps) {
|
|
48
|
-
// ----
|
|
49
|
-
// VARIABLES
|
|
50
|
-
// ----
|
|
51
|
-
// REFERENCES
|
|
52
|
-
const upperDivRef = useRef<HTMLDivElement>(null);
|
|
53
|
-
const middleDivRef = useRef<HTMLDivElement>(null);
|
|
54
|
-
const bottomDivRef = useRef<HTMLDivElement>(null);
|
|
55
|
-
const bodyContainerRef = useRef<HTMLDivElement>(null);
|
|
56
|
-
const inputRef = useRef<HTMLInputElement>(null);
|
|
57
|
-
|
|
58
|
-
// STATES
|
|
59
|
-
const [error, setError] = useState<Error | null>(null);
|
|
60
|
-
const [text, setText] = useState("");
|
|
61
|
-
const [histories, setHistories] = useState<AgenticaHistory[]>(
|
|
62
|
-
agent.getHistories().slice(),
|
|
63
|
-
);
|
|
64
|
-
const [tokenUsage, setTokenUsage] = useState<AgenticaTokenUsage>(
|
|
65
|
-
JSON.parse(JSON.stringify(agent.getTokenUsage())) as AgenticaTokenUsage,
|
|
66
|
-
);
|
|
67
|
-
const [height, setHeight] = useState(122);
|
|
68
|
-
const [enabled, setEnabled] = useState(true);
|
|
69
|
-
const [selections, setSelections] = useState<
|
|
70
|
-
AgenticaOperationSelection[]
|
|
71
|
-
>([]);
|
|
72
|
-
const [openSide, setOpenSide] = useState(false);
|
|
73
|
-
|
|
74
|
-
// ----
|
|
75
|
-
// EVENT INTERACTIONS
|
|
76
|
-
// ----
|
|
77
|
-
// EVENT LISTENERS
|
|
78
|
-
const handleUserMessage = async (event: AgenticaUserMessageEvent) => {
|
|
79
|
-
setHistories(prev => [...prev, event.toHistory()]);
|
|
80
|
-
};
|
|
81
|
-
const handleAssistantMessage = async (event: AgenticaAssistantMessageEvent) => {
|
|
82
|
-
await event.join(); // @todo Jaxtyn: streaming
|
|
83
|
-
setHistories(prev => [...prev, event.toHistory()]);
|
|
84
|
-
};
|
|
85
|
-
const handleDescribe = async (event: AgenticaDescribeEvent) => {
|
|
86
|
-
await event.join(); // @todo Jaxtyn: streaming
|
|
87
|
-
setHistories(prev => [...prev, event.toHistory()]);
|
|
88
|
-
};
|
|
89
|
-
const handleSelect = (evevnt: AgenticaSelectEvent) => {
|
|
90
|
-
setHistories(prev => [...prev, evevnt.toHistory()]);
|
|
91
|
-
setSelections(prev => [...prev, evevnt.selection]);
|
|
92
|
-
};
|
|
93
|
-
const handleValidate = (event: AgenticaValidateEvent) => {
|
|
94
|
-
console.error(event);
|
|
95
|
-
};
|
|
96
|
-
const handleJsonParseError = (event: AgenticaJsonParseErrorEvent) => {
|
|
97
|
-
console.error(event);
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
// INITIALIZATION
|
|
101
|
-
useEffect(() => {
|
|
102
|
-
if (inputRef.current !== null) {
|
|
103
|
-
inputRef.current.select();
|
|
104
|
-
}
|
|
105
|
-
setTokenUsage(agent.getTokenUsage());
|
|
106
|
-
if (agent instanceof Agentica) {
|
|
107
|
-
agent.on("select", handleSelect);
|
|
108
|
-
agent.on("assistantMessage", handleAssistantMessage);
|
|
109
|
-
agent.on("userMessage", handleUserMessage);
|
|
110
|
-
agent.on("describe", handleDescribe);
|
|
111
|
-
agent.on("validate", handleValidate);
|
|
112
|
-
agent.on("jsonParseError", handleJsonParseError);
|
|
113
|
-
}
|
|
114
|
-
else {
|
|
115
|
-
agent.on("assistantMessage", handleAssistantMessage);
|
|
116
|
-
agent.on("userMessage", handleUserMessage);
|
|
117
|
-
agent.on("describe", handleDescribe);
|
|
118
|
-
agent.on("validate", handleValidate);
|
|
119
|
-
agent.on("jsonParseError", handleJsonParseError);
|
|
120
|
-
}
|
|
121
|
-
return () => {
|
|
122
|
-
if (agent instanceof Agentica) {
|
|
123
|
-
agent.off("select", handleSelect);
|
|
124
|
-
agent.off("assistantMessage", handleAssistantMessage);
|
|
125
|
-
agent.off("userMessage", handleUserMessage);
|
|
126
|
-
agent.off("describe", handleDescribe);
|
|
127
|
-
agent.off("validate", handleValidate);
|
|
128
|
-
agent.off("jsonParseError", handleJsonParseError);
|
|
129
|
-
}
|
|
130
|
-
else {
|
|
131
|
-
agent.off("assistantMessage", handleAssistantMessage);
|
|
132
|
-
agent.off("userMessage", handleUserMessage);
|
|
133
|
-
agent.off("describe", handleDescribe);
|
|
134
|
-
agent.off("validate", handleValidate);
|
|
135
|
-
agent.off("jsonParseError", handleJsonParseError);
|
|
136
|
-
}
|
|
137
|
-
};
|
|
138
|
-
}, []);
|
|
139
|
-
|
|
140
|
-
// EVENT HANDLERS
|
|
141
|
-
const handleResize = () => {
|
|
142
|
-
setTimeout(() => {
|
|
143
|
-
if (
|
|
144
|
-
upperDivRef.current === null
|
|
145
|
-
|| middleDivRef.current === null
|
|
146
|
-
|| bottomDivRef.current === null
|
|
147
|
-
) { return; }
|
|
148
|
-
const newHeight: number
|
|
149
|
-
= upperDivRef.current.clientHeight + bottomDivRef.current.clientHeight;
|
|
150
|
-
if (newHeight !== height) {
|
|
151
|
-
setHeight(newHeight);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
};
|
|
155
|
-
useEffect(() => {
|
|
156
|
-
if (histories.length !== 0) {
|
|
157
|
-
bodyContainerRef.current?.scrollIntoView({
|
|
158
|
-
behavior: "smooth",
|
|
159
|
-
block: "end",
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
}, [histories.length]);
|
|
163
|
-
|
|
164
|
-
const conversate = async () => {
|
|
165
|
-
setText("");
|
|
166
|
-
setEnabled(false);
|
|
167
|
-
handleResize();
|
|
168
|
-
try {
|
|
169
|
-
await agent.conversate(text);
|
|
170
|
-
}
|
|
171
|
-
catch (error) {
|
|
172
|
-
if (error instanceof Error) {
|
|
173
|
-
alert(error.message);
|
|
174
|
-
setError(error);
|
|
175
|
-
}
|
|
176
|
-
else { setError(new Error("Unknown error")); }
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
|
|
180
|
-
histories.splice(0, histories.length);
|
|
181
|
-
histories.push(...agent.getHistories());
|
|
182
|
-
setHistories(histories);
|
|
183
|
-
setTokenUsage(agent.getTokenUsage());
|
|
184
|
-
setEnabled(true);
|
|
185
|
-
|
|
186
|
-
const selections: AgenticaOperationSelection[] = agent
|
|
187
|
-
.getHistories()
|
|
188
|
-
.filter(h => h.type === "select")
|
|
189
|
-
.map(h => h.selection);
|
|
190
|
-
for (const cancel of agent
|
|
191
|
-
.getHistories()
|
|
192
|
-
.filter(h => h.type === "cancel")
|
|
193
|
-
.map(h => h.selection)) {
|
|
194
|
-
const index: number = selections.findIndex(
|
|
195
|
-
s =>
|
|
196
|
-
s.operation.protocol === cancel.operation.protocol
|
|
197
|
-
&& s.operation.controller.name === cancel.operation.controller.name
|
|
198
|
-
&& s.operation.function.name === cancel.operation.function.name,
|
|
199
|
-
);
|
|
200
|
-
if (index !== -1) {
|
|
201
|
-
selections.splice(index, 1);
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
setSelections(selections);
|
|
205
|
-
};
|
|
206
|
-
|
|
207
|
-
const handleKeyUp = async (event: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
208
|
-
if (event.key === "Enter" && event.shiftKey === false) {
|
|
209
|
-
if (enabled === false) {
|
|
210
|
-
event.preventDefault();
|
|
211
|
-
}
|
|
212
|
-
else {
|
|
213
|
-
await conversate();
|
|
214
|
-
}
|
|
215
|
-
}
|
|
216
|
-
};
|
|
217
|
-
|
|
218
|
-
const capture = async () => {
|
|
219
|
-
if (bodyContainerRef.current === null) {
|
|
220
|
-
return;
|
|
221
|
-
}
|
|
222
|
-
|
|
223
|
-
const dataUrl = await toPng(bodyContainerRef.current, {});
|
|
224
|
-
const link = document.createElement("a");
|
|
225
|
-
link.download = "nestia-chat-screenshot.png";
|
|
226
|
-
link.href = dataUrl;
|
|
227
|
-
link.click();
|
|
228
|
-
link.remove();
|
|
229
|
-
};
|
|
230
|
-
|
|
231
|
-
// ----
|
|
232
|
-
// RENDERERS
|
|
233
|
-
// ----
|
|
234
|
-
const theme: Theme = useTheme();
|
|
235
|
-
const isMobile: boolean = useMediaQuery(theme.breakpoints.down("lg"));
|
|
236
|
-
const bodyMovie = (): ReactElement => (
|
|
237
|
-
<div
|
|
238
|
-
style={{
|
|
239
|
-
overflowY: "auto",
|
|
240
|
-
height: "100%",
|
|
241
|
-
width: isMobile ? "100%" : `calc(100% - ${SIDE_WIDTH}px)`,
|
|
242
|
-
margin: 0,
|
|
243
|
-
backgroundColor: "lightblue",
|
|
244
|
-
}}
|
|
245
|
-
>
|
|
246
|
-
<Container
|
|
247
|
-
style={{
|
|
248
|
-
paddingBottom: 50,
|
|
249
|
-
width: "100%",
|
|
250
|
-
minHeight: "100%",
|
|
251
|
-
backgroundColor: "lightblue",
|
|
252
|
-
margin: 0,
|
|
253
|
-
}}
|
|
254
|
-
ref={bodyContainerRef}
|
|
255
|
-
>
|
|
256
|
-
{histories
|
|
257
|
-
.map((prompt, index) => <AgenticaChatMessageMovie key={index} prompt={prompt} />)
|
|
258
|
-
.filter(elem => elem !== null)}
|
|
259
|
-
</Container>
|
|
260
|
-
</div>
|
|
261
|
-
);
|
|
262
|
-
const sideMovie = (): ReactElement => (
|
|
263
|
-
<div
|
|
264
|
-
style={{
|
|
265
|
-
width: isMobile ? undefined : SIDE_WIDTH,
|
|
266
|
-
height: "100%",
|
|
267
|
-
overflowY: "auto",
|
|
268
|
-
backgroundColor: "#eeeeee",
|
|
269
|
-
}}
|
|
270
|
-
>
|
|
271
|
-
<Container
|
|
272
|
-
maxWidth={false}
|
|
273
|
-
onClick={isMobile ? () => setOpenSide(false) : undefined}
|
|
274
|
-
>
|
|
275
|
-
<AgenticaChatSideMovie
|
|
276
|
-
vendor={agent.getVendor()}
|
|
277
|
-
config={agent.getConfig()}
|
|
278
|
-
usage={tokenUsage}
|
|
279
|
-
selections={selections}
|
|
280
|
-
error={error}
|
|
281
|
-
/>
|
|
282
|
-
</Container>
|
|
283
|
-
</div>
|
|
284
|
-
);
|
|
285
|
-
return (
|
|
286
|
-
<div style={{ width: "100%", height: "100%" }}>
|
|
287
|
-
<AppBar ref={upperDivRef} position="relative" component="div">
|
|
288
|
-
<Toolbar>
|
|
289
|
-
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
|
|
290
|
-
{title ?? "Agentica Chatbot"}
|
|
291
|
-
</Typography>
|
|
292
|
-
{isMobile
|
|
293
|
-
? (
|
|
294
|
-
<>
|
|
295
|
-
<IconButton onClick={() => void capture().catch(() => {})}>
|
|
296
|
-
<AddAPhotoIcon />
|
|
297
|
-
</IconButton>
|
|
298
|
-
<IconButton onClick={() => setOpenSide(true)}>
|
|
299
|
-
<ReceiptLongIcon />
|
|
300
|
-
</IconButton>
|
|
301
|
-
</>
|
|
302
|
-
)
|
|
303
|
-
: (
|
|
304
|
-
<Button
|
|
305
|
-
color="inherit"
|
|
306
|
-
startIcon={<AddAPhotoIcon />}
|
|
307
|
-
onClick={() => void capture().catch(() => {})}
|
|
308
|
-
>
|
|
309
|
-
Screenshot Capture
|
|
310
|
-
</Button>
|
|
311
|
-
)}
|
|
312
|
-
</Toolbar>
|
|
313
|
-
</AppBar>
|
|
314
|
-
<div
|
|
315
|
-
ref={middleDivRef}
|
|
316
|
-
style={{
|
|
317
|
-
width: "100%",
|
|
318
|
-
height: `calc(100% - ${height}px)`,
|
|
319
|
-
display: "flex",
|
|
320
|
-
flexDirection: "row",
|
|
321
|
-
}}
|
|
322
|
-
>
|
|
323
|
-
{isMobile
|
|
324
|
-
? (
|
|
325
|
-
<>
|
|
326
|
-
{bodyMovie()}
|
|
327
|
-
<Drawer
|
|
328
|
-
anchor="right"
|
|
329
|
-
open={openSide}
|
|
330
|
-
onClose={() => setOpenSide(false)}
|
|
331
|
-
>
|
|
332
|
-
{sideMovie()}
|
|
333
|
-
</Drawer>
|
|
334
|
-
</>
|
|
335
|
-
)
|
|
336
|
-
: (
|
|
337
|
-
<>
|
|
338
|
-
{bodyMovie()}
|
|
339
|
-
{sideMovie()}
|
|
340
|
-
</>
|
|
341
|
-
)}
|
|
342
|
-
</div>
|
|
343
|
-
<AppBar
|
|
344
|
-
ref={bottomDivRef}
|
|
345
|
-
position="static"
|
|
346
|
-
component="div"
|
|
347
|
-
color="inherit"
|
|
348
|
-
>
|
|
349
|
-
<Toolbar>
|
|
350
|
-
<Input
|
|
351
|
-
inputRef={inputRef}
|
|
352
|
-
fullWidth
|
|
353
|
-
placeholder="Conversate with AI Chatbot"
|
|
354
|
-
value={text}
|
|
355
|
-
multiline={true}
|
|
356
|
-
onKeyUp={e => void handleKeyUp(e).catch(() => {})}
|
|
357
|
-
onChange={(e) => {
|
|
358
|
-
setText(e.target.value);
|
|
359
|
-
handleResize();
|
|
360
|
-
}}
|
|
361
|
-
/>
|
|
362
|
-
<Button
|
|
363
|
-
variant="contained"
|
|
364
|
-
style={{ marginLeft: 10 }}
|
|
365
|
-
startIcon={<SendIcon />}
|
|
366
|
-
disabled={!enabled}
|
|
367
|
-
onClick={() => void conversate().catch(() => {})}
|
|
368
|
-
>
|
|
369
|
-
Send
|
|
370
|
-
</Button>
|
|
371
|
-
</Toolbar>
|
|
372
|
-
</AppBar>
|
|
373
|
-
</div>
|
|
374
|
-
);
|
|
375
|
-
}
|
|
376
|
-
export namespace AgenticaChatMovie {
|
|
377
|
-
export interface IProps {
|
|
378
|
-
agent: Agentica | MicroAgentica;
|
|
379
|
-
title?: string;
|
|
380
|
-
}
|
|
381
|
-
}
|
|
1
|
+
import type {
|
|
2
|
+
AgenticaAssistantMessageEvent,
|
|
3
|
+
AgenticaDescribeEvent,
|
|
4
|
+
AgenticaHistory,
|
|
5
|
+
AgenticaJsonParseErrorEvent,
|
|
6
|
+
AgenticaOperationSelection,
|
|
7
|
+
AgenticaSelectEvent,
|
|
8
|
+
AgenticaTokenUsage,
|
|
9
|
+
AgenticaUserMessageEvent,
|
|
10
|
+
AgenticaValidateEvent,
|
|
11
|
+
MicroAgentica,
|
|
12
|
+
} from "@agentica/core";
|
|
13
|
+
import type {
|
|
14
|
+
Theme,
|
|
15
|
+
} from "@mui/material";
|
|
16
|
+
import type { ReactElement } from "react";
|
|
17
|
+
|
|
18
|
+
import {
|
|
19
|
+
Agentica,
|
|
20
|
+
} from "@agentica/core";
|
|
21
|
+
import AddAPhotoIcon from "@mui/icons-material/AddAPhoto";
|
|
22
|
+
import ReceiptLongIcon from "@mui/icons-material/ReceiptLong";
|
|
23
|
+
import SendIcon from "@mui/icons-material/Send";
|
|
24
|
+
import {
|
|
25
|
+
AppBar,
|
|
26
|
+
Button,
|
|
27
|
+
Container,
|
|
28
|
+
Drawer,
|
|
29
|
+
IconButton,
|
|
30
|
+
Input,
|
|
31
|
+
Toolbar,
|
|
32
|
+
Typography,
|
|
33
|
+
useMediaQuery,
|
|
34
|
+
useTheme,
|
|
35
|
+
} from "@mui/material";
|
|
36
|
+
import { toPng } from "html-to-image";
|
|
37
|
+
import React, { useEffect, useRef, useState } from "react";
|
|
38
|
+
|
|
39
|
+
import { AgenticaChatMessageMovie } from "./messages/AgenticaChatMessageMovie";
|
|
40
|
+
import { AgenticaChatSideMovie } from "./sides/AgenticaChatSideMovie";
|
|
41
|
+
|
|
42
|
+
const SIDE_WIDTH = 450;
|
|
43
|
+
|
|
44
|
+
export function AgenticaChatMovie({
|
|
45
|
+
agent,
|
|
46
|
+
title,
|
|
47
|
+
}: AgenticaChatMovie.IProps) {
|
|
48
|
+
// ----
|
|
49
|
+
// VARIABLES
|
|
50
|
+
// ----
|
|
51
|
+
// REFERENCES
|
|
52
|
+
const upperDivRef = useRef<HTMLDivElement>(null);
|
|
53
|
+
const middleDivRef = useRef<HTMLDivElement>(null);
|
|
54
|
+
const bottomDivRef = useRef<HTMLDivElement>(null);
|
|
55
|
+
const bodyContainerRef = useRef<HTMLDivElement>(null);
|
|
56
|
+
const inputRef = useRef<HTMLInputElement>(null);
|
|
57
|
+
|
|
58
|
+
// STATES
|
|
59
|
+
const [error, setError] = useState<Error | null>(null);
|
|
60
|
+
const [text, setText] = useState("");
|
|
61
|
+
const [histories, setHistories] = useState<AgenticaHistory[]>(
|
|
62
|
+
agent.getHistories().slice(),
|
|
63
|
+
);
|
|
64
|
+
const [tokenUsage, setTokenUsage] = useState<AgenticaTokenUsage>(
|
|
65
|
+
JSON.parse(JSON.stringify(agent.getTokenUsage())) as AgenticaTokenUsage,
|
|
66
|
+
);
|
|
67
|
+
const [height, setHeight] = useState(122);
|
|
68
|
+
const [enabled, setEnabled] = useState(true);
|
|
69
|
+
const [selections, setSelections] = useState<
|
|
70
|
+
AgenticaOperationSelection[]
|
|
71
|
+
>([]);
|
|
72
|
+
const [openSide, setOpenSide] = useState(false);
|
|
73
|
+
|
|
74
|
+
// ----
|
|
75
|
+
// EVENT INTERACTIONS
|
|
76
|
+
// ----
|
|
77
|
+
// EVENT LISTENERS
|
|
78
|
+
const handleUserMessage = async (event: AgenticaUserMessageEvent) => {
|
|
79
|
+
setHistories(prev => [...prev, event.toHistory()]);
|
|
80
|
+
};
|
|
81
|
+
const handleAssistantMessage = async (event: AgenticaAssistantMessageEvent) => {
|
|
82
|
+
await event.join(); // @todo Jaxtyn: streaming
|
|
83
|
+
setHistories(prev => [...prev, event.toHistory()]);
|
|
84
|
+
};
|
|
85
|
+
const handleDescribe = async (event: AgenticaDescribeEvent) => {
|
|
86
|
+
await event.join(); // @todo Jaxtyn: streaming
|
|
87
|
+
setHistories(prev => [...prev, event.toHistory()]);
|
|
88
|
+
};
|
|
89
|
+
const handleSelect = (evevnt: AgenticaSelectEvent) => {
|
|
90
|
+
setHistories(prev => [...prev, evevnt.toHistory()]);
|
|
91
|
+
setSelections(prev => [...prev, evevnt.selection]);
|
|
92
|
+
};
|
|
93
|
+
const handleValidate = (event: AgenticaValidateEvent) => {
|
|
94
|
+
console.error(event);
|
|
95
|
+
};
|
|
96
|
+
const handleJsonParseError = (event: AgenticaJsonParseErrorEvent) => {
|
|
97
|
+
console.error(event);
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// INITIALIZATION
|
|
101
|
+
useEffect(() => {
|
|
102
|
+
if (inputRef.current !== null) {
|
|
103
|
+
inputRef.current.select();
|
|
104
|
+
}
|
|
105
|
+
setTokenUsage(agent.getTokenUsage());
|
|
106
|
+
if (agent instanceof Agentica) {
|
|
107
|
+
agent.on("select", handleSelect);
|
|
108
|
+
agent.on("assistantMessage", handleAssistantMessage);
|
|
109
|
+
agent.on("userMessage", handleUserMessage);
|
|
110
|
+
agent.on("describe", handleDescribe);
|
|
111
|
+
agent.on("validate", handleValidate);
|
|
112
|
+
agent.on("jsonParseError", handleJsonParseError);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
agent.on("assistantMessage", handleAssistantMessage);
|
|
116
|
+
agent.on("userMessage", handleUserMessage);
|
|
117
|
+
agent.on("describe", handleDescribe);
|
|
118
|
+
agent.on("validate", handleValidate);
|
|
119
|
+
agent.on("jsonParseError", handleJsonParseError);
|
|
120
|
+
}
|
|
121
|
+
return () => {
|
|
122
|
+
if (agent instanceof Agentica) {
|
|
123
|
+
agent.off("select", handleSelect);
|
|
124
|
+
agent.off("assistantMessage", handleAssistantMessage);
|
|
125
|
+
agent.off("userMessage", handleUserMessage);
|
|
126
|
+
agent.off("describe", handleDescribe);
|
|
127
|
+
agent.off("validate", handleValidate);
|
|
128
|
+
agent.off("jsonParseError", handleJsonParseError);
|
|
129
|
+
}
|
|
130
|
+
else {
|
|
131
|
+
agent.off("assistantMessage", handleAssistantMessage);
|
|
132
|
+
agent.off("userMessage", handleUserMessage);
|
|
133
|
+
agent.off("describe", handleDescribe);
|
|
134
|
+
agent.off("validate", handleValidate);
|
|
135
|
+
agent.off("jsonParseError", handleJsonParseError);
|
|
136
|
+
}
|
|
137
|
+
};
|
|
138
|
+
}, []);
|
|
139
|
+
|
|
140
|
+
// EVENT HANDLERS
|
|
141
|
+
const handleResize = () => {
|
|
142
|
+
setTimeout(() => {
|
|
143
|
+
if (
|
|
144
|
+
upperDivRef.current === null
|
|
145
|
+
|| middleDivRef.current === null
|
|
146
|
+
|| bottomDivRef.current === null
|
|
147
|
+
) { return; }
|
|
148
|
+
const newHeight: number
|
|
149
|
+
= upperDivRef.current.clientHeight + bottomDivRef.current.clientHeight;
|
|
150
|
+
if (newHeight !== height) {
|
|
151
|
+
setHeight(newHeight);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
};
|
|
155
|
+
useEffect(() => {
|
|
156
|
+
if (histories.length !== 0) {
|
|
157
|
+
bodyContainerRef.current?.scrollIntoView({
|
|
158
|
+
behavior: "smooth",
|
|
159
|
+
block: "end",
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
}, [histories.length]);
|
|
163
|
+
|
|
164
|
+
const conversate = async () => {
|
|
165
|
+
setText("");
|
|
166
|
+
setEnabled(false);
|
|
167
|
+
handleResize();
|
|
168
|
+
try {
|
|
169
|
+
await agent.conversate(text);
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
if (error instanceof Error) {
|
|
173
|
+
alert(error.message);
|
|
174
|
+
setError(error);
|
|
175
|
+
}
|
|
176
|
+
else { setError(new Error("Unknown error")); }
|
|
177
|
+
return;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
histories.splice(0, histories.length);
|
|
181
|
+
histories.push(...agent.getHistories());
|
|
182
|
+
setHistories(histories);
|
|
183
|
+
setTokenUsage(agent.getTokenUsage());
|
|
184
|
+
setEnabled(true);
|
|
185
|
+
|
|
186
|
+
const selections: AgenticaOperationSelection[] = agent
|
|
187
|
+
.getHistories()
|
|
188
|
+
.filter(h => h.type === "select")
|
|
189
|
+
.map(h => h.selection);
|
|
190
|
+
for (const cancel of agent
|
|
191
|
+
.getHistories()
|
|
192
|
+
.filter(h => h.type === "cancel")
|
|
193
|
+
.map(h => h.selection)) {
|
|
194
|
+
const index: number = selections.findIndex(
|
|
195
|
+
s =>
|
|
196
|
+
s.operation.protocol === cancel.operation.protocol
|
|
197
|
+
&& s.operation.controller.name === cancel.operation.controller.name
|
|
198
|
+
&& s.operation.function.name === cancel.operation.function.name,
|
|
199
|
+
);
|
|
200
|
+
if (index !== -1) {
|
|
201
|
+
selections.splice(index, 1);
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
setSelections(selections);
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
const handleKeyUp = async (event: React.KeyboardEvent<HTMLInputElement | HTMLTextAreaElement>) => {
|
|
208
|
+
if (event.key === "Enter" && event.shiftKey === false) {
|
|
209
|
+
if (enabled === false) {
|
|
210
|
+
event.preventDefault();
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
await conversate();
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
};
|
|
217
|
+
|
|
218
|
+
const capture = async () => {
|
|
219
|
+
if (bodyContainerRef.current === null) {
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
const dataUrl = await toPng(bodyContainerRef.current, {});
|
|
224
|
+
const link = document.createElement("a");
|
|
225
|
+
link.download = "nestia-chat-screenshot.png";
|
|
226
|
+
link.href = dataUrl;
|
|
227
|
+
link.click();
|
|
228
|
+
link.remove();
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
// ----
|
|
232
|
+
// RENDERERS
|
|
233
|
+
// ----
|
|
234
|
+
const theme: Theme = useTheme();
|
|
235
|
+
const isMobile: boolean = useMediaQuery(theme.breakpoints.down("lg"));
|
|
236
|
+
const bodyMovie = (): ReactElement => (
|
|
237
|
+
<div
|
|
238
|
+
style={{
|
|
239
|
+
overflowY: "auto",
|
|
240
|
+
height: "100%",
|
|
241
|
+
width: isMobile ? "100%" : `calc(100% - ${SIDE_WIDTH}px)`,
|
|
242
|
+
margin: 0,
|
|
243
|
+
backgroundColor: "lightblue",
|
|
244
|
+
}}
|
|
245
|
+
>
|
|
246
|
+
<Container
|
|
247
|
+
style={{
|
|
248
|
+
paddingBottom: 50,
|
|
249
|
+
width: "100%",
|
|
250
|
+
minHeight: "100%",
|
|
251
|
+
backgroundColor: "lightblue",
|
|
252
|
+
margin: 0,
|
|
253
|
+
}}
|
|
254
|
+
ref={bodyContainerRef}
|
|
255
|
+
>
|
|
256
|
+
{histories
|
|
257
|
+
.map((prompt, index) => <AgenticaChatMessageMovie key={index} prompt={prompt} />)
|
|
258
|
+
.filter(elem => elem !== null)}
|
|
259
|
+
</Container>
|
|
260
|
+
</div>
|
|
261
|
+
);
|
|
262
|
+
const sideMovie = (): ReactElement => (
|
|
263
|
+
<div
|
|
264
|
+
style={{
|
|
265
|
+
width: isMobile ? undefined : SIDE_WIDTH,
|
|
266
|
+
height: "100%",
|
|
267
|
+
overflowY: "auto",
|
|
268
|
+
backgroundColor: "#eeeeee",
|
|
269
|
+
}}
|
|
270
|
+
>
|
|
271
|
+
<Container
|
|
272
|
+
maxWidth={false}
|
|
273
|
+
onClick={isMobile ? () => setOpenSide(false) : undefined}
|
|
274
|
+
>
|
|
275
|
+
<AgenticaChatSideMovie
|
|
276
|
+
vendor={agent.getVendor()}
|
|
277
|
+
config={agent.getConfig()}
|
|
278
|
+
usage={tokenUsage}
|
|
279
|
+
selections={selections}
|
|
280
|
+
error={error}
|
|
281
|
+
/>
|
|
282
|
+
</Container>
|
|
283
|
+
</div>
|
|
284
|
+
);
|
|
285
|
+
return (
|
|
286
|
+
<div style={{ width: "100%", height: "100%" }}>
|
|
287
|
+
<AppBar ref={upperDivRef} position="relative" component="div">
|
|
288
|
+
<Toolbar>
|
|
289
|
+
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
|
|
290
|
+
{title ?? "Agentica Chatbot"}
|
|
291
|
+
</Typography>
|
|
292
|
+
{isMobile
|
|
293
|
+
? (
|
|
294
|
+
<>
|
|
295
|
+
<IconButton onClick={() => void capture().catch(() => {})}>
|
|
296
|
+
<AddAPhotoIcon />
|
|
297
|
+
</IconButton>
|
|
298
|
+
<IconButton onClick={() => setOpenSide(true)}>
|
|
299
|
+
<ReceiptLongIcon />
|
|
300
|
+
</IconButton>
|
|
301
|
+
</>
|
|
302
|
+
)
|
|
303
|
+
: (
|
|
304
|
+
<Button
|
|
305
|
+
color="inherit"
|
|
306
|
+
startIcon={<AddAPhotoIcon />}
|
|
307
|
+
onClick={() => void capture().catch(() => {})}
|
|
308
|
+
>
|
|
309
|
+
Screenshot Capture
|
|
310
|
+
</Button>
|
|
311
|
+
)}
|
|
312
|
+
</Toolbar>
|
|
313
|
+
</AppBar>
|
|
314
|
+
<div
|
|
315
|
+
ref={middleDivRef}
|
|
316
|
+
style={{
|
|
317
|
+
width: "100%",
|
|
318
|
+
height: `calc(100% - ${height}px)`,
|
|
319
|
+
display: "flex",
|
|
320
|
+
flexDirection: "row",
|
|
321
|
+
}}
|
|
322
|
+
>
|
|
323
|
+
{isMobile
|
|
324
|
+
? (
|
|
325
|
+
<>
|
|
326
|
+
{bodyMovie()}
|
|
327
|
+
<Drawer
|
|
328
|
+
anchor="right"
|
|
329
|
+
open={openSide}
|
|
330
|
+
onClose={() => setOpenSide(false)}
|
|
331
|
+
>
|
|
332
|
+
{sideMovie()}
|
|
333
|
+
</Drawer>
|
|
334
|
+
</>
|
|
335
|
+
)
|
|
336
|
+
: (
|
|
337
|
+
<>
|
|
338
|
+
{bodyMovie()}
|
|
339
|
+
{sideMovie()}
|
|
340
|
+
</>
|
|
341
|
+
)}
|
|
342
|
+
</div>
|
|
343
|
+
<AppBar
|
|
344
|
+
ref={bottomDivRef}
|
|
345
|
+
position="static"
|
|
346
|
+
component="div"
|
|
347
|
+
color="inherit"
|
|
348
|
+
>
|
|
349
|
+
<Toolbar>
|
|
350
|
+
<Input
|
|
351
|
+
inputRef={inputRef}
|
|
352
|
+
fullWidth
|
|
353
|
+
placeholder="Conversate with AI Chatbot"
|
|
354
|
+
value={text}
|
|
355
|
+
multiline={true}
|
|
356
|
+
onKeyUp={e => void handleKeyUp(e).catch(() => {})}
|
|
357
|
+
onChange={(e) => {
|
|
358
|
+
setText(e.target.value);
|
|
359
|
+
handleResize();
|
|
360
|
+
}}
|
|
361
|
+
/>
|
|
362
|
+
<Button
|
|
363
|
+
variant="contained"
|
|
364
|
+
style={{ marginLeft: 10 }}
|
|
365
|
+
startIcon={<SendIcon />}
|
|
366
|
+
disabled={!enabled}
|
|
367
|
+
onClick={() => void conversate().catch(() => {})}
|
|
368
|
+
>
|
|
369
|
+
Send
|
|
370
|
+
</Button>
|
|
371
|
+
</Toolbar>
|
|
372
|
+
</AppBar>
|
|
373
|
+
</div>
|
|
374
|
+
);
|
|
375
|
+
}
|
|
376
|
+
export namespace AgenticaChatMovie {
|
|
377
|
+
export interface IProps {
|
|
378
|
+
agent: Agentica | MicroAgentica;
|
|
379
|
+
title?: string;
|
|
380
|
+
}
|
|
381
|
+
}
|