@antipopp/agno-react 0.9.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/dist/index.mjs CHANGED
@@ -1,56 +1,12 @@
1
- // src/context/AgnoContext.tsx
2
- import { createContext, useContext, useMemo, useEffect } from "react";
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/context/ToolHandlerContext.tsx
27
- import { createContext as createContext2, useContext as useContext2, useState, useCallback } from "react";
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/components/GenerativeUIRenderer.tsx
53
- import React3 from "react";
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 = void 0;
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 = useMemo2(() => {
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);
@@ -352,7 +383,13 @@ function useAgnoToolExecution(handlers = {}, autoExecute = true) {
352
383
  if (autoExecute && isPaused && !isExecuting && pendingTools.length > 0) {
353
384
  executeAndContinue();
354
385
  }
355
- }, [autoExecute, isPaused, isExecuting, pendingTools.length, executeAndContinue]);
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 React3.Component {
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("[GenerativeUIRenderer] Error rendering component:", error, errorInfo);
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: "p-4 border border-red-300 rounded-md bg-red-50 text-red-800", children: [
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 mt-1", children: this.state.error?.message || "Unknown error" })
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("div", { className: `p-4 border border-yellow-300 rounded-md bg-yellow-50 text-yellow-800 ${className || ""}`, children: [
413
- /* @__PURE__ */ jsx3("p", { className: "font-semibold", children: "Custom component not available" }),
414
- /* @__PURE__ */ jsx3("p", { className: "text-sm mt-1", children: "The custom render function for this component is not available." })
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 mb-2", children: chartSpec.title }),
424
- chartSpec.description && /* @__PURE__ */ jsx3("p", { className: "text-sm text-gray-600 mb-4", children: chartSpec.description }),
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("div", { className: `p-4 border border-gray-300 rounded-md ${className || ""}`, children: [
429
- /* @__PURE__ */ jsx3("p", { className: "font-semibold mb-2", children: chartSpec.title || "Chart Data" }),
430
- chartSpec.description && /* @__PURE__ */ jsx3("p", { className: "text-sm text-gray-600 mb-2", children: chartSpec.description }),
431
- /* @__PURE__ */ jsx3("pre", { className: "text-xs bg-gray-100 p-2 rounded overflow-auto", children: JSON.stringify(chartSpec.props.data, null, 2) })
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 mb-2", children: cardGridSpec.title }),
440
- cardGridSpec.description && /* @__PURE__ */ jsx3("p", { className: "text-sm text-gray-600 mb-4", children: cardGridSpec.description }),
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 mb-2", children: tableSpec.title }),
451
- tableSpec.description && /* @__PURE__ */ jsx3("p", { className: "text-sm text-gray-600 mb-4", children: tableSpec.description }),
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,109 +516,370 @@ 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: `p-4 border rounded-md ${className || ""}`, children: [
467
- artifactSpec.title && /* @__PURE__ */ jsx3("h3", { className: "font-semibold mb-4", children: artifactSpec.title }),
468
- artifactSpec.description && /* @__PURE__ */ jsx3("p", { className: "text-sm text-gray-600 mb-4", children: artifactSpec.description }),
469
- /* @__PURE__ */ jsx3("div", { className: "space-y-4", children: artifactSpec.props.content?.map((childSpec, index) => /* @__PURE__ */ jsx3(GenerativeUIRenderer, { spec: childSpec, onError }, index)) })
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: `p-4 border border-gray-300 rounded-md ${className || ""}`, children: [
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: "text-sm text-gray-600 mt-1", children: [
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
 
481
- // src/utils/ui-helpers.ts
482
- function createBarChart(data, xKey, bars, options) {
483
- return {
484
- type: "chart",
485
- component: "BarChart",
486
- layout: options?.layout,
487
- title: options?.title,
488
- description: options?.description,
489
- props: {
490
- data,
491
- xKey,
492
- bars: bars.map((bar) => ({
493
- key: bar.key,
494
- label: bar.label || bar.key,
495
- color: bar.color
496
- })),
497
- showLegend: options?.showLegend ?? true,
498
- showGrid: options?.showGrid ?? true,
499
- height: options?.height,
500
- width: options?.width
501
- }
502
- };
503
- }
504
- function createLineChart(data, xKey, lines, options) {
505
- return {
506
- type: "chart",
507
- component: "LineChart",
508
- layout: options?.layout,
509
- title: options?.title,
510
- description: options?.description,
511
- props: {
512
- data,
513
- xKey,
514
- lines: lines.map((line) => ({
515
- key: line.key,
516
- label: line.label || line.key,
517
- color: line.color
518
- })),
519
- showLegend: options?.showLegend ?? true,
520
- showGrid: options?.showGrid ?? true,
521
- height: options?.height,
522
- width: options?.width
523
- }
524
- };
525
- }
526
- function createPieChart(data, dataKey, nameKey, options) {
527
- return {
528
- type: "chart",
529
- component: "PieChart",
530
- layout: options?.layout,
531
- title: options?.title,
532
- description: options?.description,
533
- props: {
534
- data,
535
- pie: {
536
- dataKey,
537
- nameKey,
538
- label: options?.showLabel ?? true
539
- },
540
- showLegend: options?.showLegend ?? true,
541
- height: options?.height || 400,
542
- width: options?.width
543
- }
544
- };
545
- }
546
- function createAreaChart(data, xKey, areas, options) {
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
+ );
547
611
  return {
548
- type: "chart",
549
- component: "AreaChart",
550
- layout: options?.layout,
551
- title: options?.title,
552
- description: options?.description,
553
- props: {
554
- data,
555
- xKey,
556
- areas: areas.map((area) => ({
557
- key: area.key,
558
- label: area.label || area.key,
559
- color: area.color
560
- })),
561
- showLegend: options?.showLegend ?? true,
562
- showGrid: options?.showGrid ?? true,
563
- height: options?.height,
564
- width: options?.width
565
- }
612
+ initialize,
613
+ checkStatus,
614
+ fetchAgents,
615
+ fetchTeams,
616
+ updateConfig,
617
+ isInitializing,
618
+ error
566
619
  };
567
620
  }
568
- function createCardGrid(cards, options) {
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
+
795
+ // src/utils/ui-helpers.ts
796
+ function createBarChart(data, xKey, bars, options) {
797
+ return {
798
+ type: "chart",
799
+ component: "BarChart",
800
+ layout: options?.layout,
801
+ title: options?.title,
802
+ description: options?.description,
803
+ props: {
804
+ data,
805
+ xKey,
806
+ bars: bars.map((bar) => ({
807
+ key: bar.key,
808
+ label: bar.label || bar.key,
809
+ color: bar.color
810
+ })),
811
+ showLegend: options?.showLegend ?? true,
812
+ showGrid: options?.showGrid ?? true,
813
+ height: options?.height,
814
+ width: options?.width
815
+ }
816
+ };
817
+ }
818
+ function createLineChart(data, xKey, lines, options) {
819
+ return {
820
+ type: "chart",
821
+ component: "LineChart",
822
+ layout: options?.layout,
823
+ title: options?.title,
824
+ description: options?.description,
825
+ props: {
826
+ data,
827
+ xKey,
828
+ lines: lines.map((line) => ({
829
+ key: line.key,
830
+ label: line.label || line.key,
831
+ color: line.color
832
+ })),
833
+ showLegend: options?.showLegend ?? true,
834
+ showGrid: options?.showGrid ?? true,
835
+ height: options?.height,
836
+ width: options?.width
837
+ }
838
+ };
839
+ }
840
+ function createPieChart(data, dataKey, nameKey, options) {
841
+ return {
842
+ type: "chart",
843
+ component: "PieChart",
844
+ layout: options?.layout,
845
+ title: options?.title,
846
+ description: options?.description,
847
+ props: {
848
+ data,
849
+ pie: {
850
+ dataKey,
851
+ nameKey,
852
+ label: options?.showLabel ?? true
853
+ },
854
+ showLegend: options?.showLegend ?? true,
855
+ height: options?.height || 400,
856
+ width: options?.width
857
+ }
858
+ };
859
+ }
860
+ function createAreaChart(data, xKey, areas, options) {
861
+ return {
862
+ type: "chart",
863
+ component: "AreaChart",
864
+ layout: options?.layout,
865
+ title: options?.title,
866
+ description: options?.description,
867
+ props: {
868
+ data,
869
+ xKey,
870
+ areas: areas.map((area) => ({
871
+ key: area.key,
872
+ label: area.label || area.key,
873
+ color: area.color
874
+ })),
875
+ showLegend: options?.showLegend ?? true,
876
+ showGrid: options?.showGrid ?? true,
877
+ height: options?.height,
878
+ width: options?.width
879
+ }
880
+ };
881
+ }
882
+ function createCardGrid(cards, options) {
569
883
  return {
570
884
  type: "card-grid",
571
885
  layout: options?.layout,
@@ -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(k.toLowerCase())
961
+ (k) => ["name", "label", "category", "date", "time", "month", "year"].includes(
962
+ k.toLowerCase()
963
+ )
648
964
  ) || keys[0];
649
- const numericKeys = keys.filter((k) => k !== xKey && typeof firstItem[k] === "number");
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, options) => {
826
- setIsLoading(true);
827
- setError(void 0);
828
- try {
829
- const messages = await client.loadSession(sessionId, options);
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 (options) => {
843
- setIsLoading(true);
844
- setError(void 0);
845
- try {
846
- const fetchedSessions = await client.fetchSessions(options);
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 (options) => {
874
- setIsInitializing(true);
875
- setError(void 0);
876
- try {
877
- const result = await client.initialize(options);
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 (options) => {
888
- setError(void 0);
889
- try {
890
- return await client.checkStatus(options);
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 (options) => {
898
- setError(void 0);
899
- try {
900
- return await client.fetchAgents(options);
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 (options) => {
908
- setError(void 0);
909
- try {
910
- return await client.fetchTeams(options);
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,