@antipopp/agno-react 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -4
- package/dist/index.d.mts +111 -80
- package/dist/index.d.ts +111 -80
- package/dist/index.js +481 -383
- package/dist/index.mjs +407 -310
- package/package.json +13 -5
package/dist/index.mjs
CHANGED
|
@@ -1,56 +1,12 @@
|
|
|
1
|
-
// src/
|
|
2
|
-
import
|
|
3
|
-
import { AgnoClient } from "@antipopp/agno-client";
|
|
4
|
-
import { jsx } from "react/jsx-runtime";
|
|
5
|
-
var AgnoContext = createContext(null);
|
|
6
|
-
function AgnoProvider({ config, children }) {
|
|
7
|
-
const client = useMemo(() => new AgnoClient(config), []);
|
|
8
|
-
useEffect(() => {
|
|
9
|
-
client.updateConfig(config);
|
|
10
|
-
}, [client, config]);
|
|
11
|
-
useEffect(() => {
|
|
12
|
-
return () => {
|
|
13
|
-
client.removeAllListeners();
|
|
14
|
-
};
|
|
15
|
-
}, [client]);
|
|
16
|
-
return /* @__PURE__ */ jsx(AgnoContext.Provider, { value: client, children });
|
|
17
|
-
}
|
|
18
|
-
function useAgnoClient() {
|
|
19
|
-
const client = useContext(AgnoContext);
|
|
20
|
-
if (!client) {
|
|
21
|
-
throw new Error("useAgnoClient must be used within an AgnoProvider");
|
|
22
|
-
}
|
|
23
|
-
return client;
|
|
24
|
-
}
|
|
1
|
+
// src/components/GenerativeUIRenderer.tsx
|
|
2
|
+
import React from "react";
|
|
25
3
|
|
|
26
|
-
// src/
|
|
27
|
-
import {
|
|
28
|
-
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
29
|
-
var ToolHandlerContext = createContext2(null);
|
|
30
|
-
function ToolHandlerProvider({ handlers: initialHandlers = {}, children }) {
|
|
31
|
-
const [handlers, setHandlers] = useState(initialHandlers);
|
|
32
|
-
const registerHandler = useCallback((name, handler) => {
|
|
33
|
-
setHandlers((prev) => ({ ...prev, [name]: handler }));
|
|
34
|
-
}, []);
|
|
35
|
-
const unregisterHandler = useCallback((name) => {
|
|
36
|
-
setHandlers((prev) => {
|
|
37
|
-
const { [name]: _, ...rest } = prev;
|
|
38
|
-
return rest;
|
|
39
|
-
});
|
|
40
|
-
}, []);
|
|
41
|
-
const value = {
|
|
42
|
-
handlers,
|
|
43
|
-
registerHandler,
|
|
44
|
-
unregisterHandler
|
|
45
|
-
};
|
|
46
|
-
return /* @__PURE__ */ jsx2(ToolHandlerContext.Provider, { value, children });
|
|
47
|
-
}
|
|
48
|
-
function useToolHandlers() {
|
|
49
|
-
return useContext2(ToolHandlerContext);
|
|
50
|
-
}
|
|
4
|
+
// src/hooks/useAgnoToolExecution.ts
|
|
5
|
+
import { useCallback as useCallback2, useEffect as useEffect2, useMemo, useState as useState2 } from "react";
|
|
51
6
|
|
|
52
|
-
// src/
|
|
53
|
-
import
|
|
7
|
+
// src/context/AgnoContext.tsx
|
|
8
|
+
import { AgnoClient } from "@antipopp/agno-client";
|
|
9
|
+
import { createContext, useContext, useEffect, useRef } from "react";
|
|
54
10
|
|
|
55
11
|
// src/utils/component-registry.ts
|
|
56
12
|
var ComponentRegistry = class _ComponentRegistry {
|
|
@@ -110,6 +66,17 @@ var ComponentRegistry = class _ComponentRegistry {
|
|
|
110
66
|
clear() {
|
|
111
67
|
this.components.clear();
|
|
112
68
|
}
|
|
69
|
+
/**
|
|
70
|
+
* Reset the singleton instance.
|
|
71
|
+
* Call this during cleanup (e.g., when AgnoProvider unmounts) to prevent memory leaks.
|
|
72
|
+
* After calling this, getInstance() will create a fresh instance.
|
|
73
|
+
*/
|
|
74
|
+
static resetInstance() {
|
|
75
|
+
if (_ComponentRegistry.instance) {
|
|
76
|
+
_ComponentRegistry.instance.clear();
|
|
77
|
+
_ComponentRegistry.instance = void 0;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
113
80
|
};
|
|
114
81
|
function getComponentRegistry() {
|
|
115
82
|
return ComponentRegistry.getInstance();
|
|
@@ -121,8 +88,65 @@ function getChartComponent(name) {
|
|
|
121
88
|
return getComponentRegistry().get(`chart:${name}`);
|
|
122
89
|
}
|
|
123
90
|
|
|
91
|
+
// src/context/AgnoContext.tsx
|
|
92
|
+
import { jsx } from "react/jsx-runtime";
|
|
93
|
+
var AgnoContext = createContext(null);
|
|
94
|
+
function AgnoProvider({ config, children }) {
|
|
95
|
+
const clientRef = useRef(null);
|
|
96
|
+
if (clientRef.current === null) {
|
|
97
|
+
clientRef.current = new AgnoClient(config);
|
|
98
|
+
}
|
|
99
|
+
const client = clientRef.current;
|
|
100
|
+
useEffect(() => {
|
|
101
|
+
client.updateConfig(config);
|
|
102
|
+
}, [client, config]);
|
|
103
|
+
useEffect(() => {
|
|
104
|
+
return () => {
|
|
105
|
+
client.dispose();
|
|
106
|
+
clearCustomRenderRegistry();
|
|
107
|
+
ComponentRegistry.resetInstance();
|
|
108
|
+
};
|
|
109
|
+
}, [client]);
|
|
110
|
+
return /* @__PURE__ */ jsx(AgnoContext.Provider, { value: client, children });
|
|
111
|
+
}
|
|
112
|
+
function useAgnoClient() {
|
|
113
|
+
const client = useContext(AgnoContext);
|
|
114
|
+
if (!client) {
|
|
115
|
+
throw new Error("useAgnoClient must be used within an AgnoProvider");
|
|
116
|
+
}
|
|
117
|
+
return client;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// src/context/ToolHandlerContext.tsx
|
|
121
|
+
import { createContext as createContext2, useCallback, useContext as useContext2, useState } from "react";
|
|
122
|
+
import { jsx as jsx2 } from "react/jsx-runtime";
|
|
123
|
+
var ToolHandlerContext = createContext2(null);
|
|
124
|
+
function ToolHandlerProvider({
|
|
125
|
+
handlers: initialHandlers = {},
|
|
126
|
+
children
|
|
127
|
+
}) {
|
|
128
|
+
const [handlers, setHandlers] = useState(initialHandlers);
|
|
129
|
+
const registerHandler = useCallback((name, handler) => {
|
|
130
|
+
setHandlers((prev) => ({ ...prev, [name]: handler }));
|
|
131
|
+
}, []);
|
|
132
|
+
const unregisterHandler = useCallback((name) => {
|
|
133
|
+
setHandlers((prev) => {
|
|
134
|
+
const { [name]: _, ...rest } = prev;
|
|
135
|
+
return rest;
|
|
136
|
+
});
|
|
137
|
+
}, []);
|
|
138
|
+
const value = {
|
|
139
|
+
handlers,
|
|
140
|
+
registerHandler,
|
|
141
|
+
unregisterHandler
|
|
142
|
+
};
|
|
143
|
+
return /* @__PURE__ */ jsx2(ToolHandlerContext.Provider, { value, children });
|
|
144
|
+
}
|
|
145
|
+
function useToolHandlers() {
|
|
146
|
+
return useContext2(ToolHandlerContext);
|
|
147
|
+
}
|
|
148
|
+
|
|
124
149
|
// src/hooks/useAgnoToolExecution.ts
|
|
125
|
-
import { useState as useState2, useEffect as useEffect2, useCallback as useCallback2, useMemo as useMemo2 } from "react";
|
|
126
150
|
var customRenderRegistry = /* @__PURE__ */ new Map();
|
|
127
151
|
function registerCustomRender(renderFn) {
|
|
128
152
|
const key = `custom-${Date.now()}-${Math.random().toString(36).substr(2, 9)}`;
|
|
@@ -132,6 +156,9 @@ function registerCustomRender(renderFn) {
|
|
|
132
156
|
function getCustomRender(key) {
|
|
133
157
|
return customRenderRegistry.get(key);
|
|
134
158
|
}
|
|
159
|
+
function clearCustomRenderRegistry() {
|
|
160
|
+
customRenderRegistry.clear();
|
|
161
|
+
}
|
|
135
162
|
function isToolHandlerResult(value) {
|
|
136
163
|
return value && typeof value === "object" && ("data" in value || "ui" in value);
|
|
137
164
|
}
|
|
@@ -141,7 +168,7 @@ function isUIComponentSpec(value) {
|
|
|
141
168
|
function processToolResult(result, _tool) {
|
|
142
169
|
if (isToolHandlerResult(result)) {
|
|
143
170
|
const { data, ui } = result;
|
|
144
|
-
let uiComponent
|
|
171
|
+
let uiComponent;
|
|
145
172
|
if (ui) {
|
|
146
173
|
if (ui.type === "custom" && typeof ui.render === "function") {
|
|
147
174
|
const renderKey = registerCustomRender(ui.render);
|
|
@@ -193,7 +220,7 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
|
|
|
193
220
|
);
|
|
194
221
|
}
|
|
195
222
|
}, [isTeamMode]);
|
|
196
|
-
const mergedHandlers =
|
|
223
|
+
const mergedHandlers = useMemo(() => {
|
|
197
224
|
const globalHandlers = toolHandlerContext?.handlers || {};
|
|
198
225
|
return { ...globalHandlers, ...handlers };
|
|
199
226
|
}, [toolHandlerContext?.handlers, handlers]);
|
|
@@ -280,14 +307,17 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
|
|
|
280
307
|
const handleSessionLoaded = async (_sessionId) => {
|
|
281
308
|
const messages = client.getMessages();
|
|
282
309
|
for (const message of messages) {
|
|
283
|
-
if (!message.tool_calls)
|
|
310
|
+
if (!message.tool_calls) {
|
|
284
311
|
continue;
|
|
312
|
+
}
|
|
285
313
|
for (const tool of message.tool_calls) {
|
|
286
|
-
if (tool.ui_component)
|
|
314
|
+
if (tool.ui_component) {
|
|
287
315
|
continue;
|
|
316
|
+
}
|
|
288
317
|
const handler = mergedHandlers[tool.tool_name];
|
|
289
|
-
if (!handler)
|
|
318
|
+
if (!handler) {
|
|
290
319
|
continue;
|
|
320
|
+
}
|
|
291
321
|
try {
|
|
292
322
|
const result = await handler(tool.tool_args);
|
|
293
323
|
const { uiComponent } = processToolResult(result, tool);
|
|
@@ -310,8 +340,9 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
|
|
|
310
340
|
return Promise.all(
|
|
311
341
|
tools.map(async (tool) => {
|
|
312
342
|
const handler = mergedHandlers[tool.tool_name];
|
|
313
|
-
if (!handler)
|
|
343
|
+
if (!handler) {
|
|
314
344
|
return tool;
|
|
345
|
+
}
|
|
315
346
|
try {
|
|
316
347
|
const result = await handler(tool.tool_args);
|
|
317
348
|
const { resultData, uiComponent } = processToolResult(result, tool);
|
|
@@ -334,13 +365,13 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
|
|
|
334
365
|
[mergedHandlers]
|
|
335
366
|
);
|
|
336
367
|
const continueWithResults = useCallback2(
|
|
337
|
-
async (tools) => {
|
|
368
|
+
async (tools, options) => {
|
|
338
369
|
if (!isPaused) {
|
|
339
370
|
throw new Error("No paused run to continue");
|
|
340
371
|
}
|
|
341
372
|
setIsExecuting(true);
|
|
342
373
|
try {
|
|
343
|
-
await client.continueRun(tools);
|
|
374
|
+
await client.continueRun(tools, options);
|
|
344
375
|
} catch (error) {
|
|
345
376
|
setIsExecuting(false);
|
|
346
377
|
throw error;
|
|
@@ -352,7 +383,13 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
|
|
|
352
383
|
if (autoExecute && isPaused && !isExecuting && pendingTools.length > 0) {
|
|
353
384
|
executeAndContinue();
|
|
354
385
|
}
|
|
355
|
-
}, [
|
|
386
|
+
}, [
|
|
387
|
+
autoExecute,
|
|
388
|
+
isPaused,
|
|
389
|
+
isExecuting,
|
|
390
|
+
pendingTools.length,
|
|
391
|
+
executeAndContinue
|
|
392
|
+
]);
|
|
356
393
|
return {
|
|
357
394
|
/** Whether the run is currently paused awaiting tool execution */
|
|
358
395
|
isPaused,
|
|
@@ -373,7 +410,7 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
|
|
|
373
410
|
|
|
374
411
|
// src/components/GenerativeUIRenderer.tsx
|
|
375
412
|
import { jsx as jsx3, jsxs } from "react/jsx-runtime";
|
|
376
|
-
var UIErrorBoundary = class extends
|
|
413
|
+
var UIErrorBoundary = class extends React.Component {
|
|
377
414
|
constructor(props) {
|
|
378
415
|
super(props);
|
|
379
416
|
this.state = { hasError: false };
|
|
@@ -382,14 +419,18 @@ var UIErrorBoundary = class extends React3.Component {
|
|
|
382
419
|
return { hasError: true, error };
|
|
383
420
|
}
|
|
384
421
|
componentDidCatch(error, errorInfo) {
|
|
385
|
-
console.error(
|
|
422
|
+
console.error(
|
|
423
|
+
"[GenerativeUIRenderer] Error rendering component:",
|
|
424
|
+
error,
|
|
425
|
+
errorInfo
|
|
426
|
+
);
|
|
386
427
|
this.props.onError?.(error);
|
|
387
428
|
}
|
|
388
429
|
render() {
|
|
389
430
|
if (this.state.hasError) {
|
|
390
|
-
return this.props.fallback || /* @__PURE__ */ jsxs("div", { className: "
|
|
431
|
+
return this.props.fallback || /* @__PURE__ */ jsxs("div", { className: "rounded-md border border-red-300 bg-red-50 p-4 text-red-800", children: [
|
|
391
432
|
/* @__PURE__ */ jsx3("p", { className: "font-semibold", children: "Failed to render UI component" }),
|
|
392
|
-
/* @__PURE__ */ jsx3("p", { className: "text-sm
|
|
433
|
+
/* @__PURE__ */ jsx3("p", { className: "mt-1 text-sm", children: this.state.error?.message || "Unknown error" })
|
|
393
434
|
] });
|
|
394
435
|
}
|
|
395
436
|
return this.props.children;
|
|
@@ -409,10 +450,16 @@ function GenerativeUIRenderer({
|
|
|
409
450
|
return /* @__PURE__ */ jsx3(UIErrorBoundary, { onError, children: /* @__PURE__ */ jsx3("div", { className, children: renderFn(customSpec.props || {}) }) });
|
|
410
451
|
}
|
|
411
452
|
}
|
|
412
|
-
return /* @__PURE__ */ jsxs(
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
453
|
+
return /* @__PURE__ */ jsxs(
|
|
454
|
+
"div",
|
|
455
|
+
{
|
|
456
|
+
className: `rounded-md border border-yellow-300 bg-yellow-50 p-4 text-yellow-800 ${className || ""}`,
|
|
457
|
+
children: [
|
|
458
|
+
/* @__PURE__ */ jsx3("p", { className: "font-semibold", children: "Custom component not available" }),
|
|
459
|
+
/* @__PURE__ */ jsx3("p", { className: "mt-1 text-sm", children: "The custom render function for this component is not available." })
|
|
460
|
+
]
|
|
461
|
+
}
|
|
462
|
+
);
|
|
416
463
|
}
|
|
417
464
|
if (spec.type === "chart") {
|
|
418
465
|
const chartSpec = spec;
|
|
@@ -420,24 +467,30 @@ function GenerativeUIRenderer({
|
|
|
420
467
|
if (registry.has(chartType)) {
|
|
421
468
|
const ChartRenderer = registry.get(chartType);
|
|
422
469
|
return /* @__PURE__ */ jsx3(UIErrorBoundary, { onError, children: /* @__PURE__ */ jsxs("div", { className, children: [
|
|
423
|
-
chartSpec.title && /* @__PURE__ */ jsx3("h3", { className: "font-semibold
|
|
424
|
-
chartSpec.description && /* @__PURE__ */ jsx3("p", { className: "
|
|
470
|
+
chartSpec.title && /* @__PURE__ */ jsx3("h3", { className: "mb-2 font-semibold", children: chartSpec.title }),
|
|
471
|
+
chartSpec.description && /* @__PURE__ */ jsx3("p", { className: "mb-4 text-gray-600 text-sm", children: chartSpec.description }),
|
|
425
472
|
/* @__PURE__ */ jsx3(ChartRenderer, { ...chartSpec.props })
|
|
426
473
|
] }) });
|
|
427
474
|
}
|
|
428
|
-
return /* @__PURE__ */ jsxs(
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
475
|
+
return /* @__PURE__ */ jsxs(
|
|
476
|
+
"div",
|
|
477
|
+
{
|
|
478
|
+
className: `rounded-md border border-gray-300 p-4 ${className || ""}`,
|
|
479
|
+
children: [
|
|
480
|
+
/* @__PURE__ */ jsx3("p", { className: "mb-2 font-semibold", children: chartSpec.title || "Chart Data" }),
|
|
481
|
+
chartSpec.description && /* @__PURE__ */ jsx3("p", { className: "mb-2 text-gray-600 text-sm", children: chartSpec.description }),
|
|
482
|
+
/* @__PURE__ */ jsx3("pre", { className: "overflow-auto rounded bg-gray-100 p-2 text-xs", children: JSON.stringify(chartSpec.props.data, null, 2) })
|
|
483
|
+
]
|
|
484
|
+
}
|
|
485
|
+
);
|
|
433
486
|
}
|
|
434
487
|
if (spec.type === "card-grid") {
|
|
435
488
|
const cardGridSpec = spec;
|
|
436
489
|
if (registry.has("card-grid")) {
|
|
437
490
|
const CardGridRenderer = registry.get("card-grid");
|
|
438
491
|
return /* @__PURE__ */ jsx3(UIErrorBoundary, { onError, children: /* @__PURE__ */ jsxs("div", { className, children: [
|
|
439
|
-
cardGridSpec.title && /* @__PURE__ */ jsx3("h3", { className: "font-semibold
|
|
440
|
-
cardGridSpec.description && /* @__PURE__ */ jsx3("p", { className: "
|
|
492
|
+
cardGridSpec.title && /* @__PURE__ */ jsx3("h3", { className: "mb-2 font-semibold", children: cardGridSpec.title }),
|
|
493
|
+
cardGridSpec.description && /* @__PURE__ */ jsx3("p", { className: "mb-4 text-gray-600 text-sm", children: cardGridSpec.description }),
|
|
441
494
|
/* @__PURE__ */ jsx3(CardGridRenderer, { ...cardGridSpec.props })
|
|
442
495
|
] }) });
|
|
443
496
|
}
|
|
@@ -447,8 +500,8 @@ function GenerativeUIRenderer({
|
|
|
447
500
|
if (registry.has("table")) {
|
|
448
501
|
const TableRenderer = registry.get("table");
|
|
449
502
|
return /* @__PURE__ */ jsx3(UIErrorBoundary, { onError, children: /* @__PURE__ */ jsxs("div", { className, children: [
|
|
450
|
-
tableSpec.title && /* @__PURE__ */ jsx3("h3", { className: "font-semibold
|
|
451
|
-
tableSpec.description && /* @__PURE__ */ jsx3("p", { className: "
|
|
503
|
+
tableSpec.title && /* @__PURE__ */ jsx3("h3", { className: "mb-2 font-semibold", children: tableSpec.title }),
|
|
504
|
+
tableSpec.description && /* @__PURE__ */ jsx3("p", { className: "mb-4 text-gray-600 text-sm", children: tableSpec.description }),
|
|
452
505
|
/* @__PURE__ */ jsx3(TableRenderer, { ...tableSpec.props })
|
|
453
506
|
] }) });
|
|
454
507
|
}
|
|
@@ -463,21 +516,282 @@ function GenerativeUIRenderer({
|
|
|
463
516
|
}
|
|
464
517
|
if (spec.type === "artifact") {
|
|
465
518
|
const artifactSpec = spec;
|
|
466
|
-
return /* @__PURE__ */ jsx3(UIErrorBoundary, { onError, children: /* @__PURE__ */ jsxs("div", { className: `
|
|
467
|
-
artifactSpec.title && /* @__PURE__ */ jsx3("h3", { className: "font-semibold
|
|
468
|
-
artifactSpec.description && /* @__PURE__ */ jsx3("p", { className: "
|
|
469
|
-
/* @__PURE__ */ jsx3("div", { className: "space-y-4", children: artifactSpec.props.content?.map(
|
|
519
|
+
return /* @__PURE__ */ jsx3(UIErrorBoundary, { onError, children: /* @__PURE__ */ jsxs("div", { className: `rounded-md border p-4 ${className || ""}`, children: [
|
|
520
|
+
artifactSpec.title && /* @__PURE__ */ jsx3("h3", { className: "mb-4 font-semibold", children: artifactSpec.title }),
|
|
521
|
+
artifactSpec.description && /* @__PURE__ */ jsx3("p", { className: "mb-4 text-gray-600 text-sm", children: artifactSpec.description }),
|
|
522
|
+
/* @__PURE__ */ jsx3("div", { className: "space-y-4", children: artifactSpec.props.content?.map(
|
|
523
|
+
(childSpec, index) => /* @__PURE__ */ jsx3(
|
|
524
|
+
GenerativeUIRenderer,
|
|
525
|
+
{
|
|
526
|
+
onError,
|
|
527
|
+
spec: childSpec
|
|
528
|
+
},
|
|
529
|
+
index
|
|
530
|
+
)
|
|
531
|
+
) })
|
|
470
532
|
] }) });
|
|
471
533
|
}
|
|
472
|
-
return /* @__PURE__ */ jsxs("div", { className: `
|
|
534
|
+
return /* @__PURE__ */ jsxs("div", { className: `rounded-md border border-gray-300 p-4 ${className || ""}`, children: [
|
|
473
535
|
/* @__PURE__ */ jsx3("p", { className: "font-semibold", children: "Unsupported UI component" }),
|
|
474
|
-
/* @__PURE__ */ jsxs("p", { className: "
|
|
536
|
+
/* @__PURE__ */ jsxs("p", { className: "mt-1 text-gray-600 text-sm", children: [
|
|
475
537
|
"Component type: ",
|
|
476
538
|
spec.type
|
|
477
539
|
] })
|
|
478
540
|
] });
|
|
479
541
|
}
|
|
480
542
|
|
|
543
|
+
// src/hooks/useAgnoActions.ts
|
|
544
|
+
import { useCallback as useCallback3, useState as useState3 } from "react";
|
|
545
|
+
function useAgnoActions() {
|
|
546
|
+
const client = useAgnoClient();
|
|
547
|
+
const [isInitializing, setIsInitializing] = useState3(false);
|
|
548
|
+
const [error, setError] = useState3();
|
|
549
|
+
const initialize = useCallback3(
|
|
550
|
+
async (options) => {
|
|
551
|
+
setIsInitializing(true);
|
|
552
|
+
setError(void 0);
|
|
553
|
+
try {
|
|
554
|
+
const result = await client.initialize(options);
|
|
555
|
+
return result;
|
|
556
|
+
} catch (err) {
|
|
557
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
558
|
+
setError(errorMessage);
|
|
559
|
+
throw err;
|
|
560
|
+
} finally {
|
|
561
|
+
setIsInitializing(false);
|
|
562
|
+
}
|
|
563
|
+
},
|
|
564
|
+
[client]
|
|
565
|
+
);
|
|
566
|
+
const checkStatus = useCallback3(
|
|
567
|
+
async (options) => {
|
|
568
|
+
setError(void 0);
|
|
569
|
+
try {
|
|
570
|
+
return await client.checkStatus(options);
|
|
571
|
+
} catch (err) {
|
|
572
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
573
|
+
setError(errorMessage);
|
|
574
|
+
return false;
|
|
575
|
+
}
|
|
576
|
+
},
|
|
577
|
+
[client]
|
|
578
|
+
);
|
|
579
|
+
const fetchAgents = useCallback3(
|
|
580
|
+
async (options) => {
|
|
581
|
+
setError(void 0);
|
|
582
|
+
try {
|
|
583
|
+
return await client.fetchAgents(options);
|
|
584
|
+
} catch (err) {
|
|
585
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
586
|
+
setError(errorMessage);
|
|
587
|
+
throw err;
|
|
588
|
+
}
|
|
589
|
+
},
|
|
590
|
+
[client]
|
|
591
|
+
);
|
|
592
|
+
const fetchTeams = useCallback3(
|
|
593
|
+
async (options) => {
|
|
594
|
+
setError(void 0);
|
|
595
|
+
try {
|
|
596
|
+
return await client.fetchTeams(options);
|
|
597
|
+
} catch (err) {
|
|
598
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
599
|
+
setError(errorMessage);
|
|
600
|
+
throw err;
|
|
601
|
+
}
|
|
602
|
+
},
|
|
603
|
+
[client]
|
|
604
|
+
);
|
|
605
|
+
const updateConfig = useCallback3(
|
|
606
|
+
(updates) => {
|
|
607
|
+
client.updateConfig(updates);
|
|
608
|
+
},
|
|
609
|
+
[client]
|
|
610
|
+
);
|
|
611
|
+
return {
|
|
612
|
+
initialize,
|
|
613
|
+
checkStatus,
|
|
614
|
+
fetchAgents,
|
|
615
|
+
fetchTeams,
|
|
616
|
+
updateConfig,
|
|
617
|
+
isInitializing,
|
|
618
|
+
error
|
|
619
|
+
};
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
// src/hooks/useAgnoChat.ts
|
|
623
|
+
import { useCallback as useCallback4, useEffect as useEffect3, useState as useState4 } from "react";
|
|
624
|
+
function useAgnoChat() {
|
|
625
|
+
const client = useAgnoClient();
|
|
626
|
+
const [messages, setMessages] = useState4(client.getMessages());
|
|
627
|
+
const [state, setState] = useState4(client.getState());
|
|
628
|
+
const [error, setError] = useState4();
|
|
629
|
+
useEffect3(() => {
|
|
630
|
+
const handleMessageUpdate = (updatedMessages) => {
|
|
631
|
+
setMessages(updatedMessages);
|
|
632
|
+
};
|
|
633
|
+
const handleMessageComplete = (updatedMessages) => {
|
|
634
|
+
setMessages(updatedMessages);
|
|
635
|
+
};
|
|
636
|
+
const handleMessageRefreshed = (updatedMessages) => {
|
|
637
|
+
setMessages(updatedMessages);
|
|
638
|
+
};
|
|
639
|
+
const handleMessageError = (errorMessage) => {
|
|
640
|
+
setError(errorMessage);
|
|
641
|
+
};
|
|
642
|
+
const handleStateChange = (newState) => {
|
|
643
|
+
setState(newState);
|
|
644
|
+
};
|
|
645
|
+
const handleUIRender = (event) => {
|
|
646
|
+
const { tools } = event;
|
|
647
|
+
for (const tool of tools) {
|
|
648
|
+
if (tool.ui_component) {
|
|
649
|
+
client.hydrateToolCallUI(
|
|
650
|
+
tool.tool_call_id,
|
|
651
|
+
tool.ui_component
|
|
652
|
+
);
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
};
|
|
656
|
+
client.on("message:update", handleMessageUpdate);
|
|
657
|
+
client.on("message:complete", handleMessageComplete);
|
|
658
|
+
client.on("message:refreshed", handleMessageRefreshed);
|
|
659
|
+
client.on("message:error", handleMessageError);
|
|
660
|
+
client.on("state:change", handleStateChange);
|
|
661
|
+
client.on("ui:render", handleUIRender);
|
|
662
|
+
setMessages(client.getMessages());
|
|
663
|
+
setState(client.getState());
|
|
664
|
+
return () => {
|
|
665
|
+
client.off("message:update", handleMessageUpdate);
|
|
666
|
+
client.off("message:complete", handleMessageComplete);
|
|
667
|
+
client.off("message:refreshed", handleMessageRefreshed);
|
|
668
|
+
client.off("message:error", handleMessageError);
|
|
669
|
+
client.off("state:change", handleStateChange);
|
|
670
|
+
client.off("ui:render", handleUIRender);
|
|
671
|
+
};
|
|
672
|
+
}, [client]);
|
|
673
|
+
const sendMessage = useCallback4(
|
|
674
|
+
async (message, options) => {
|
|
675
|
+
setError(void 0);
|
|
676
|
+
try {
|
|
677
|
+
await client.sendMessage(message, options);
|
|
678
|
+
} catch (err) {
|
|
679
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
680
|
+
setError(errorMessage);
|
|
681
|
+
throw err;
|
|
682
|
+
}
|
|
683
|
+
},
|
|
684
|
+
[client]
|
|
685
|
+
);
|
|
686
|
+
const cancelRun = useCallback4(async () => {
|
|
687
|
+
try {
|
|
688
|
+
await client.cancelRun();
|
|
689
|
+
} catch (err) {
|
|
690
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
691
|
+
setError(errorMessage);
|
|
692
|
+
throw err;
|
|
693
|
+
}
|
|
694
|
+
}, [client]);
|
|
695
|
+
const clearMessages = useCallback4(() => {
|
|
696
|
+
client.clearMessages();
|
|
697
|
+
setMessages([]);
|
|
698
|
+
setError(void 0);
|
|
699
|
+
}, [client]);
|
|
700
|
+
return {
|
|
701
|
+
messages,
|
|
702
|
+
sendMessage,
|
|
703
|
+
clearMessages,
|
|
704
|
+
cancelRun,
|
|
705
|
+
isStreaming: state.isStreaming,
|
|
706
|
+
isRefreshing: state.isRefreshing,
|
|
707
|
+
isPaused: state.isPaused,
|
|
708
|
+
isCancelling: state.isCancelling,
|
|
709
|
+
currentRunId: state.currentRunId,
|
|
710
|
+
error,
|
|
711
|
+
state
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
|
|
715
|
+
// src/hooks/useAgnoSession.ts
|
|
716
|
+
import { useCallback as useCallback5, useEffect as useEffect4, useState as useState5 } from "react";
|
|
717
|
+
function useAgnoSession() {
|
|
718
|
+
const client = useAgnoClient();
|
|
719
|
+
const [sessions, setSessions] = useState5([]);
|
|
720
|
+
const [currentSessionId, setCurrentSessionId] = useState5(
|
|
721
|
+
client.getConfig().sessionId
|
|
722
|
+
);
|
|
723
|
+
const [isLoading, setIsLoading] = useState5(false);
|
|
724
|
+
const [error, setError] = useState5();
|
|
725
|
+
useEffect4(() => {
|
|
726
|
+
const handleSessionLoaded = (sessionId) => {
|
|
727
|
+
setCurrentSessionId(sessionId);
|
|
728
|
+
};
|
|
729
|
+
const handleSessionCreated = (session) => {
|
|
730
|
+
setSessions((prev) => [session, ...prev]);
|
|
731
|
+
setCurrentSessionId(session.session_id);
|
|
732
|
+
};
|
|
733
|
+
const handleStateChange = () => {
|
|
734
|
+
const config = client.getConfig();
|
|
735
|
+
setCurrentSessionId(config.sessionId);
|
|
736
|
+
setSessions(client.getState().sessions);
|
|
737
|
+
};
|
|
738
|
+
client.on("session:loaded", handleSessionLoaded);
|
|
739
|
+
client.on("session:created", handleSessionCreated);
|
|
740
|
+
client.on("state:change", handleStateChange);
|
|
741
|
+
setSessions(client.getState().sessions);
|
|
742
|
+
setCurrentSessionId(client.getConfig().sessionId);
|
|
743
|
+
return () => {
|
|
744
|
+
client.off("session:loaded", handleSessionLoaded);
|
|
745
|
+
client.off("session:created", handleSessionCreated);
|
|
746
|
+
client.off("state:change", handleStateChange);
|
|
747
|
+
};
|
|
748
|
+
}, [client]);
|
|
749
|
+
const loadSession = useCallback5(
|
|
750
|
+
async (sessionId, options) => {
|
|
751
|
+
setIsLoading(true);
|
|
752
|
+
setError(void 0);
|
|
753
|
+
try {
|
|
754
|
+
const messages = await client.loadSession(sessionId, options);
|
|
755
|
+
setCurrentSessionId(sessionId);
|
|
756
|
+
return messages;
|
|
757
|
+
} catch (err) {
|
|
758
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
759
|
+
setError(errorMessage);
|
|
760
|
+
throw err;
|
|
761
|
+
} finally {
|
|
762
|
+
setIsLoading(false);
|
|
763
|
+
}
|
|
764
|
+
},
|
|
765
|
+
[client]
|
|
766
|
+
);
|
|
767
|
+
const fetchSessions = useCallback5(
|
|
768
|
+
async (options) => {
|
|
769
|
+
setIsLoading(true);
|
|
770
|
+
setError(void 0);
|
|
771
|
+
try {
|
|
772
|
+
const fetchedSessions = await client.fetchSessions(options);
|
|
773
|
+
setSessions(fetchedSessions);
|
|
774
|
+
return fetchedSessions;
|
|
775
|
+
} catch (err) {
|
|
776
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
777
|
+
setError(errorMessage);
|
|
778
|
+
throw err;
|
|
779
|
+
} finally {
|
|
780
|
+
setIsLoading(false);
|
|
781
|
+
}
|
|
782
|
+
},
|
|
783
|
+
[client]
|
|
784
|
+
);
|
|
785
|
+
return {
|
|
786
|
+
sessions,
|
|
787
|
+
currentSessionId,
|
|
788
|
+
loadSession,
|
|
789
|
+
fetchSessions,
|
|
790
|
+
isLoading,
|
|
791
|
+
error
|
|
792
|
+
};
|
|
793
|
+
}
|
|
794
|
+
|
|
481
795
|
// src/utils/ui-helpers.ts
|
|
482
796
|
function createBarChart(data, xKey, bars, options) {
|
|
483
797
|
return {
|
|
@@ -644,9 +958,13 @@ function createSmartChart(data, options) {
|
|
|
644
958
|
const firstItem = data[0];
|
|
645
959
|
const keys = Object.keys(firstItem);
|
|
646
960
|
const xKey = options?.xKey || keys.find(
|
|
647
|
-
(k) => ["name", "label", "category", "date", "time", "month", "year"].includes(
|
|
961
|
+
(k) => ["name", "label", "category", "date", "time", "month", "year"].includes(
|
|
962
|
+
k.toLowerCase()
|
|
963
|
+
)
|
|
648
964
|
) || keys[0];
|
|
649
|
-
const numericKeys = keys.filter(
|
|
965
|
+
const numericKeys = keys.filter(
|
|
966
|
+
(k) => k !== xKey && typeof firstItem[k] === "number"
|
|
967
|
+
);
|
|
650
968
|
const yKeys = options?.yKeys || numericKeys;
|
|
651
969
|
if (options?.preferredType) {
|
|
652
970
|
switch (options.preferredType) {
|
|
@@ -708,233 +1026,12 @@ function resultWithCardGrid(cards, options) {
|
|
|
708
1026
|
function resultWithTable(data, columns, options) {
|
|
709
1027
|
return createToolResult(data, createTable(data, columns, options));
|
|
710
1028
|
}
|
|
711
|
-
|
|
712
|
-
// src/hooks/useAgnoChat.ts
|
|
713
|
-
import { useState as useState3, useEffect as useEffect3, useCallback as useCallback3 } from "react";
|
|
714
|
-
function useAgnoChat() {
|
|
715
|
-
const client = useAgnoClient();
|
|
716
|
-
const [messages, setMessages] = useState3(client.getMessages());
|
|
717
|
-
const [state, setState] = useState3(client.getState());
|
|
718
|
-
const [error, setError] = useState3();
|
|
719
|
-
useEffect3(() => {
|
|
720
|
-
const handleMessageUpdate = (updatedMessages) => {
|
|
721
|
-
setMessages(updatedMessages);
|
|
722
|
-
};
|
|
723
|
-
const handleMessageComplete = (updatedMessages) => {
|
|
724
|
-
setMessages(updatedMessages);
|
|
725
|
-
};
|
|
726
|
-
const handleMessageRefreshed = (updatedMessages) => {
|
|
727
|
-
setMessages(updatedMessages);
|
|
728
|
-
};
|
|
729
|
-
const handleMessageError = (errorMessage) => {
|
|
730
|
-
setError(errorMessage);
|
|
731
|
-
};
|
|
732
|
-
const handleStateChange = (newState) => {
|
|
733
|
-
setState(newState);
|
|
734
|
-
};
|
|
735
|
-
const handleUIRender = (event) => {
|
|
736
|
-
const { tools } = event;
|
|
737
|
-
for (const tool of tools) {
|
|
738
|
-
if (tool.ui_component) {
|
|
739
|
-
client.hydrateToolCallUI(tool.tool_call_id, tool.ui_component);
|
|
740
|
-
}
|
|
741
|
-
}
|
|
742
|
-
};
|
|
743
|
-
client.on("message:update", handleMessageUpdate);
|
|
744
|
-
client.on("message:complete", handleMessageComplete);
|
|
745
|
-
client.on("message:refreshed", handleMessageRefreshed);
|
|
746
|
-
client.on("message:error", handleMessageError);
|
|
747
|
-
client.on("state:change", handleStateChange);
|
|
748
|
-
client.on("ui:render", handleUIRender);
|
|
749
|
-
setMessages(client.getMessages());
|
|
750
|
-
setState(client.getState());
|
|
751
|
-
return () => {
|
|
752
|
-
client.off("message:update", handleMessageUpdate);
|
|
753
|
-
client.off("message:complete", handleMessageComplete);
|
|
754
|
-
client.off("message:refreshed", handleMessageRefreshed);
|
|
755
|
-
client.off("message:error", handleMessageError);
|
|
756
|
-
client.off("state:change", handleStateChange);
|
|
757
|
-
client.off("ui:render", handleUIRender);
|
|
758
|
-
};
|
|
759
|
-
}, [client]);
|
|
760
|
-
const sendMessage = useCallback3(
|
|
761
|
-
async (message, options) => {
|
|
762
|
-
setError(void 0);
|
|
763
|
-
try {
|
|
764
|
-
await client.sendMessage(message, options);
|
|
765
|
-
} catch (err) {
|
|
766
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
767
|
-
setError(errorMessage);
|
|
768
|
-
throw err;
|
|
769
|
-
}
|
|
770
|
-
},
|
|
771
|
-
[client]
|
|
772
|
-
);
|
|
773
|
-
const clearMessages = useCallback3(() => {
|
|
774
|
-
client.clearMessages();
|
|
775
|
-
setMessages([]);
|
|
776
|
-
setError(void 0);
|
|
777
|
-
}, [client]);
|
|
778
|
-
return {
|
|
779
|
-
messages,
|
|
780
|
-
sendMessage,
|
|
781
|
-
clearMessages,
|
|
782
|
-
isStreaming: state.isStreaming,
|
|
783
|
-
isRefreshing: state.isRefreshing,
|
|
784
|
-
isPaused: state.isPaused,
|
|
785
|
-
error,
|
|
786
|
-
state
|
|
787
|
-
};
|
|
788
|
-
}
|
|
789
|
-
|
|
790
|
-
// src/hooks/useAgnoSession.ts
|
|
791
|
-
import { useState as useState4, useEffect as useEffect4, useCallback as useCallback4 } from "react";
|
|
792
|
-
function useAgnoSession() {
|
|
793
|
-
const client = useAgnoClient();
|
|
794
|
-
const [sessions, setSessions] = useState4([]);
|
|
795
|
-
const [currentSessionId, setCurrentSessionId] = useState4(
|
|
796
|
-
client.getConfig().sessionId
|
|
797
|
-
);
|
|
798
|
-
const [isLoading, setIsLoading] = useState4(false);
|
|
799
|
-
const [error, setError] = useState4();
|
|
800
|
-
useEffect4(() => {
|
|
801
|
-
const handleSessionLoaded = (sessionId) => {
|
|
802
|
-
setCurrentSessionId(sessionId);
|
|
803
|
-
};
|
|
804
|
-
const handleSessionCreated = (session) => {
|
|
805
|
-
setSessions((prev) => [session, ...prev]);
|
|
806
|
-
setCurrentSessionId(session.session_id);
|
|
807
|
-
};
|
|
808
|
-
const handleStateChange = () => {
|
|
809
|
-
const config = client.getConfig();
|
|
810
|
-
setCurrentSessionId(config.sessionId);
|
|
811
|
-
setSessions(client.getState().sessions);
|
|
812
|
-
};
|
|
813
|
-
client.on("session:loaded", handleSessionLoaded);
|
|
814
|
-
client.on("session:created", handleSessionCreated);
|
|
815
|
-
client.on("state:change", handleStateChange);
|
|
816
|
-
setSessions(client.getState().sessions);
|
|
817
|
-
setCurrentSessionId(client.getConfig().sessionId);
|
|
818
|
-
return () => {
|
|
819
|
-
client.off("session:loaded", handleSessionLoaded);
|
|
820
|
-
client.off("session:created", handleSessionCreated);
|
|
821
|
-
client.off("state:change", handleStateChange);
|
|
822
|
-
};
|
|
823
|
-
}, [client]);
|
|
824
|
-
const loadSession = useCallback4(
|
|
825
|
-
async (sessionId) => {
|
|
826
|
-
setIsLoading(true);
|
|
827
|
-
setError(void 0);
|
|
828
|
-
try {
|
|
829
|
-
const messages = await client.loadSession(sessionId);
|
|
830
|
-
setCurrentSessionId(sessionId);
|
|
831
|
-
return messages;
|
|
832
|
-
} catch (err) {
|
|
833
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
834
|
-
setError(errorMessage);
|
|
835
|
-
throw err;
|
|
836
|
-
} finally {
|
|
837
|
-
setIsLoading(false);
|
|
838
|
-
}
|
|
839
|
-
},
|
|
840
|
-
[client]
|
|
841
|
-
);
|
|
842
|
-
const fetchSessions = useCallback4(async () => {
|
|
843
|
-
setIsLoading(true);
|
|
844
|
-
setError(void 0);
|
|
845
|
-
try {
|
|
846
|
-
const fetchedSessions = await client.fetchSessions();
|
|
847
|
-
setSessions(fetchedSessions);
|
|
848
|
-
return fetchedSessions;
|
|
849
|
-
} catch (err) {
|
|
850
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
851
|
-
setError(errorMessage);
|
|
852
|
-
throw err;
|
|
853
|
-
} finally {
|
|
854
|
-
setIsLoading(false);
|
|
855
|
-
}
|
|
856
|
-
}, [client]);
|
|
857
|
-
return {
|
|
858
|
-
sessions,
|
|
859
|
-
currentSessionId,
|
|
860
|
-
loadSession,
|
|
861
|
-
fetchSessions,
|
|
862
|
-
isLoading,
|
|
863
|
-
error
|
|
864
|
-
};
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
// src/hooks/useAgnoActions.ts
|
|
868
|
-
import { useState as useState5, useCallback as useCallback5 } from "react";
|
|
869
|
-
function useAgnoActions() {
|
|
870
|
-
const client = useAgnoClient();
|
|
871
|
-
const [isInitializing, setIsInitializing] = useState5(false);
|
|
872
|
-
const [error, setError] = useState5();
|
|
873
|
-
const initialize = useCallback5(async () => {
|
|
874
|
-
setIsInitializing(true);
|
|
875
|
-
setError(void 0);
|
|
876
|
-
try {
|
|
877
|
-
const result = await client.initialize();
|
|
878
|
-
return result;
|
|
879
|
-
} catch (err) {
|
|
880
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
881
|
-
setError(errorMessage);
|
|
882
|
-
throw err;
|
|
883
|
-
} finally {
|
|
884
|
-
setIsInitializing(false);
|
|
885
|
-
}
|
|
886
|
-
}, [client]);
|
|
887
|
-
const checkStatus = useCallback5(async () => {
|
|
888
|
-
setError(void 0);
|
|
889
|
-
try {
|
|
890
|
-
return await client.checkStatus();
|
|
891
|
-
} catch (err) {
|
|
892
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
893
|
-
setError(errorMessage);
|
|
894
|
-
return false;
|
|
895
|
-
}
|
|
896
|
-
}, [client]);
|
|
897
|
-
const fetchAgents = useCallback5(async () => {
|
|
898
|
-
setError(void 0);
|
|
899
|
-
try {
|
|
900
|
-
return await client.fetchAgents();
|
|
901
|
-
} catch (err) {
|
|
902
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
903
|
-
setError(errorMessage);
|
|
904
|
-
throw err;
|
|
905
|
-
}
|
|
906
|
-
}, [client]);
|
|
907
|
-
const fetchTeams = useCallback5(async () => {
|
|
908
|
-
setError(void 0);
|
|
909
|
-
try {
|
|
910
|
-
return await client.fetchTeams();
|
|
911
|
-
} catch (err) {
|
|
912
|
-
const errorMessage = err instanceof Error ? err.message : String(err);
|
|
913
|
-
setError(errorMessage);
|
|
914
|
-
throw err;
|
|
915
|
-
}
|
|
916
|
-
}, [client]);
|
|
917
|
-
const updateConfig = useCallback5(
|
|
918
|
-
(updates) => {
|
|
919
|
-
client.updateConfig(updates);
|
|
920
|
-
},
|
|
921
|
-
[client]
|
|
922
|
-
);
|
|
923
|
-
return {
|
|
924
|
-
initialize,
|
|
925
|
-
checkStatus,
|
|
926
|
-
fetchAgents,
|
|
927
|
-
fetchTeams,
|
|
928
|
-
updateConfig,
|
|
929
|
-
isInitializing,
|
|
930
|
-
error
|
|
931
|
-
};
|
|
932
|
-
}
|
|
933
1029
|
export {
|
|
934
1030
|
AgnoProvider,
|
|
935
1031
|
ComponentRegistry,
|
|
936
1032
|
GenerativeUIRenderer,
|
|
937
1033
|
ToolHandlerProvider,
|
|
1034
|
+
clearCustomRenderRegistry,
|
|
938
1035
|
createAreaChart,
|
|
939
1036
|
createArtifact,
|
|
940
1037
|
createBarChart,
|