@antipopp/agno-react 0.1.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.js ADDED
@@ -0,0 +1,416 @@
1
+ "use strict";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
+ var __getOwnPropNames = Object.getOwnPropertyNames;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ var __export = (target, all) => {
7
+ for (var name in all)
8
+ __defProp(target, name, { get: all[name], enumerable: true });
9
+ };
10
+ var __copyProps = (to, from, except, desc) => {
11
+ if (from && typeof from === "object" || typeof from === "function") {
12
+ for (let key of __getOwnPropNames(from))
13
+ if (!__hasOwnProp.call(to, key) && key !== except)
14
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
+ }
16
+ return to;
17
+ };
18
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+
20
+ // src/index.ts
21
+ var src_exports = {};
22
+ __export(src_exports, {
23
+ AgnoProvider: () => AgnoProvider,
24
+ useAgnoActions: () => useAgnoActions,
25
+ useAgnoChat: () => useAgnoChat,
26
+ useAgnoClient: () => useAgnoClient,
27
+ useAgnoSession: () => useAgnoSession,
28
+ useAgnoToolExecution: () => useAgnoToolExecution
29
+ });
30
+ module.exports = __toCommonJS(src_exports);
31
+
32
+ // src/context/AgnoContext.tsx
33
+ var import_react = require("react");
34
+ var import_agno_client = require("@antipopp/agno-client");
35
+ var import_jsx_runtime = require("react/jsx-runtime");
36
+ var AgnoContext = (0, import_react.createContext)(null);
37
+ function AgnoProvider({ config, children }) {
38
+ const client = (0, import_react.useMemo)(() => new import_agno_client.AgnoClient(config), []);
39
+ (0, import_react.useEffect)(() => {
40
+ return () => {
41
+ client.removeAllListeners();
42
+ };
43
+ }, [client]);
44
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(AgnoContext.Provider, { value: client, children });
45
+ }
46
+ function useAgnoClient() {
47
+ const client = (0, import_react.useContext)(AgnoContext);
48
+ if (!client) {
49
+ throw new Error("useAgnoClient must be used within an AgnoProvider");
50
+ }
51
+ return client;
52
+ }
53
+
54
+ // src/hooks/useAgnoChat.ts
55
+ var import_react2 = require("react");
56
+ function useAgnoChat() {
57
+ const client = useAgnoClient();
58
+ const [messages, setMessages] = (0, import_react2.useState)(client.getMessages());
59
+ const [state, setState] = (0, import_react2.useState)(client.getState());
60
+ const [error, setError] = (0, import_react2.useState)();
61
+ (0, import_react2.useEffect)(() => {
62
+ const handleMessageUpdate = (updatedMessages) => {
63
+ console.log("[useAgnoChat] message:update event received, messages:", updatedMessages.length);
64
+ setMessages(updatedMessages);
65
+ };
66
+ const handleMessageComplete = (updatedMessages) => {
67
+ console.log("[useAgnoChat] message:complete event received, messages:", updatedMessages.length);
68
+ setMessages(updatedMessages);
69
+ };
70
+ const handleMessageError = (errorMessage) => {
71
+ console.log("[useAgnoChat] message:error event received:", errorMessage);
72
+ setError(errorMessage);
73
+ };
74
+ const handleStateChange = (newState) => {
75
+ console.log("[useAgnoChat] state:change event received");
76
+ setState(newState);
77
+ };
78
+ client.on("message:update", handleMessageUpdate);
79
+ client.on("message:complete", handleMessageComplete);
80
+ client.on("message:error", handleMessageError);
81
+ client.on("state:change", handleStateChange);
82
+ console.log("[useAgnoChat] Initializing with messages:", client.getMessages().length);
83
+ setMessages(client.getMessages());
84
+ setState(client.getState());
85
+ return () => {
86
+ client.off("message:update", handleMessageUpdate);
87
+ client.off("message:complete", handleMessageComplete);
88
+ client.off("message:error", handleMessageError);
89
+ client.off("state:change", handleStateChange);
90
+ };
91
+ }, [client]);
92
+ const sendMessage = (0, import_react2.useCallback)(
93
+ async (message, options) => {
94
+ setError(void 0);
95
+ try {
96
+ await client.sendMessage(message, options);
97
+ } catch (err) {
98
+ const errorMessage = err instanceof Error ? err.message : String(err);
99
+ setError(errorMessage);
100
+ throw err;
101
+ }
102
+ },
103
+ [client]
104
+ );
105
+ const clearMessages = (0, import_react2.useCallback)(() => {
106
+ client.clearMessages();
107
+ setMessages([]);
108
+ setError(void 0);
109
+ }, [client]);
110
+ return {
111
+ messages,
112
+ sendMessage,
113
+ clearMessages,
114
+ isStreaming: state.isStreaming,
115
+ isPaused: state.isPaused,
116
+ error,
117
+ state
118
+ };
119
+ }
120
+
121
+ // src/hooks/useAgnoSession.ts
122
+ var import_react3 = require("react");
123
+ function useAgnoSession() {
124
+ const client = useAgnoClient();
125
+ const [sessions, setSessions] = (0, import_react3.useState)([]);
126
+ const [currentSessionId, setCurrentSessionId] = (0, import_react3.useState)(
127
+ client.getConfig().sessionId
128
+ );
129
+ const [isLoading, setIsLoading] = (0, import_react3.useState)(false);
130
+ const [error, setError] = (0, import_react3.useState)();
131
+ (0, import_react3.useEffect)(() => {
132
+ const handleSessionLoaded = (sessionId) => {
133
+ setCurrentSessionId(sessionId);
134
+ };
135
+ const handleSessionCreated = (session) => {
136
+ setSessions((prev) => [session, ...prev]);
137
+ setCurrentSessionId(session.session_id);
138
+ };
139
+ const handleStateChange = () => {
140
+ const config = client.getConfig();
141
+ setCurrentSessionId(config.sessionId);
142
+ setSessions(client.getState().sessions);
143
+ };
144
+ client.on("session:loaded", handleSessionLoaded);
145
+ client.on("session:created", handleSessionCreated);
146
+ client.on("state:change", handleStateChange);
147
+ setSessions(client.getState().sessions);
148
+ setCurrentSessionId(client.getConfig().sessionId);
149
+ return () => {
150
+ client.off("session:loaded", handleSessionLoaded);
151
+ client.off("session:created", handleSessionCreated);
152
+ client.off("state:change", handleStateChange);
153
+ };
154
+ }, [client]);
155
+ const loadSession = (0, import_react3.useCallback)(
156
+ async (sessionId) => {
157
+ setIsLoading(true);
158
+ setError(void 0);
159
+ try {
160
+ const messages = await client.loadSession(sessionId);
161
+ setCurrentSessionId(sessionId);
162
+ return messages;
163
+ } catch (err) {
164
+ const errorMessage = err instanceof Error ? err.message : String(err);
165
+ setError(errorMessage);
166
+ throw err;
167
+ } finally {
168
+ setIsLoading(false);
169
+ }
170
+ },
171
+ [client]
172
+ );
173
+ const fetchSessions = (0, import_react3.useCallback)(async () => {
174
+ setIsLoading(true);
175
+ setError(void 0);
176
+ try {
177
+ const fetchedSessions = await client.fetchSessions();
178
+ setSessions(fetchedSessions);
179
+ return fetchedSessions;
180
+ } catch (err) {
181
+ const errorMessage = err instanceof Error ? err.message : String(err);
182
+ setError(errorMessage);
183
+ throw err;
184
+ } finally {
185
+ setIsLoading(false);
186
+ }
187
+ }, [client]);
188
+ return {
189
+ sessions,
190
+ currentSessionId,
191
+ loadSession,
192
+ fetchSessions,
193
+ isLoading,
194
+ error
195
+ };
196
+ }
197
+
198
+ // src/hooks/useAgnoActions.ts
199
+ var import_react4 = require("react");
200
+ function useAgnoActions() {
201
+ const client = useAgnoClient();
202
+ const [isInitializing, setIsInitializing] = (0, import_react4.useState)(false);
203
+ const [error, setError] = (0, import_react4.useState)();
204
+ const initialize = (0, import_react4.useCallback)(async () => {
205
+ setIsInitializing(true);
206
+ setError(void 0);
207
+ try {
208
+ const result = await client.initialize();
209
+ return result;
210
+ } catch (err) {
211
+ const errorMessage = err instanceof Error ? err.message : String(err);
212
+ setError(errorMessage);
213
+ throw err;
214
+ } finally {
215
+ setIsInitializing(false);
216
+ }
217
+ }, [client]);
218
+ const checkStatus = (0, import_react4.useCallback)(async () => {
219
+ setError(void 0);
220
+ try {
221
+ return await client.checkStatus();
222
+ } catch (err) {
223
+ const errorMessage = err instanceof Error ? err.message : String(err);
224
+ setError(errorMessage);
225
+ return false;
226
+ }
227
+ }, [client]);
228
+ const fetchAgents = (0, import_react4.useCallback)(async () => {
229
+ setError(void 0);
230
+ try {
231
+ return await client.fetchAgents();
232
+ } catch (err) {
233
+ const errorMessage = err instanceof Error ? err.message : String(err);
234
+ setError(errorMessage);
235
+ throw err;
236
+ }
237
+ }, [client]);
238
+ const fetchTeams = (0, import_react4.useCallback)(async () => {
239
+ setError(void 0);
240
+ try {
241
+ return await client.fetchTeams();
242
+ } catch (err) {
243
+ const errorMessage = err instanceof Error ? err.message : String(err);
244
+ setError(errorMessage);
245
+ throw err;
246
+ }
247
+ }, [client]);
248
+ const updateConfig = (0, import_react4.useCallback)(
249
+ (updates) => {
250
+ client.updateConfig(updates);
251
+ },
252
+ [client]
253
+ );
254
+ return {
255
+ initialize,
256
+ checkStatus,
257
+ fetchAgents,
258
+ fetchTeams,
259
+ updateConfig,
260
+ isInitializing,
261
+ error
262
+ };
263
+ }
264
+
265
+ // src/hooks/useAgnoToolExecution.ts
266
+ var import_react5 = require("react");
267
+ function useAgnoToolExecution(handlers, autoExecute = true) {
268
+ const client = useAgnoClient();
269
+ const [pendingTools, setPendingTools] = (0, import_react5.useState)([]);
270
+ const [isPaused, setIsPaused] = (0, import_react5.useState)(false);
271
+ const [isExecuting, setIsExecuting] = (0, import_react5.useState)(false);
272
+ const [executionError, setExecutionError] = (0, import_react5.useState)();
273
+ (0, import_react5.useEffect)(() => {
274
+ const handleRunPaused = (event) => {
275
+ console.log("[useAgnoToolExecution] Run paused, tools:", event.tools);
276
+ setIsPaused(true);
277
+ setPendingTools(event.tools);
278
+ setExecutionError(void 0);
279
+ };
280
+ const handleRunContinued = () => {
281
+ console.log("[useAgnoToolExecution] Run continued");
282
+ setIsPaused(false);
283
+ setPendingTools([]);
284
+ setIsExecuting(false);
285
+ setExecutionError(void 0);
286
+ };
287
+ client.on("run:paused", handleRunPaused);
288
+ client.on("run:continued", handleRunContinued);
289
+ return () => {
290
+ client.off("run:paused", handleRunPaused);
291
+ client.off("run:continued", handleRunContinued);
292
+ };
293
+ }, [client]);
294
+ const executeAndContinue = (0, import_react5.useCallback)(async () => {
295
+ if (!isPaused || pendingTools.length === 0) {
296
+ console.warn("[useAgnoToolExecution] Cannot execute: no pending tools");
297
+ return;
298
+ }
299
+ setIsExecuting(true);
300
+ setExecutionError(void 0);
301
+ try {
302
+ console.log("[useAgnoToolExecution] Executing", pendingTools.length, "tools");
303
+ const updatedTools = await Promise.all(
304
+ pendingTools.map(async (tool) => {
305
+ const handler = handlers[tool.tool_name];
306
+ if (!handler) {
307
+ console.warn(`[useAgnoToolExecution] No handler for tool: ${tool.tool_name}`);
308
+ return {
309
+ ...tool,
310
+ result: JSON.stringify({
311
+ error: `No handler registered for ${tool.tool_name}`
312
+ })
313
+ };
314
+ }
315
+ try {
316
+ console.log(`[useAgnoToolExecution] Executing tool: ${tool.tool_name}`, tool.tool_args);
317
+ const result = await handler(tool.tool_args);
318
+ console.log(`[useAgnoToolExecution] Tool result:`, result);
319
+ return {
320
+ ...tool,
321
+ result: typeof result === "string" ? result : JSON.stringify(result)
322
+ };
323
+ } catch (error) {
324
+ console.error(`[useAgnoToolExecution] Tool execution error:`, error);
325
+ return {
326
+ ...tool,
327
+ result: JSON.stringify({
328
+ error: error instanceof Error ? error.message : String(error)
329
+ })
330
+ };
331
+ }
332
+ })
333
+ );
334
+ console.log("[useAgnoToolExecution] Continuing run with results");
335
+ await client.continueRun(updatedTools);
336
+ } catch (error) {
337
+ const errorMessage = error instanceof Error ? error.message : String(error);
338
+ console.error("[useAgnoToolExecution] Failed to continue run:", errorMessage);
339
+ setExecutionError(errorMessage);
340
+ setIsExecuting(false);
341
+ throw error;
342
+ }
343
+ }, [client, handlers, isPaused, pendingTools]);
344
+ const executeTools = (0, import_react5.useCallback)(
345
+ async (tools) => {
346
+ return Promise.all(
347
+ tools.map(async (tool) => {
348
+ const handler = handlers[tool.tool_name];
349
+ if (!handler)
350
+ return tool;
351
+ try {
352
+ const result = await handler(tool.tool_args);
353
+ return {
354
+ ...tool,
355
+ result: typeof result === "string" ? result : JSON.stringify(result)
356
+ };
357
+ } catch (error) {
358
+ return {
359
+ ...tool,
360
+ result: JSON.stringify({
361
+ error: error instanceof Error ? error.message : String(error)
362
+ })
363
+ };
364
+ }
365
+ })
366
+ );
367
+ },
368
+ [handlers]
369
+ );
370
+ const continueWithResults = (0, import_react5.useCallback)(
371
+ async (tools) => {
372
+ if (!isPaused) {
373
+ throw new Error("No paused run to continue");
374
+ }
375
+ setIsExecuting(true);
376
+ try {
377
+ await client.continueRun(tools);
378
+ } catch (error) {
379
+ setIsExecuting(false);
380
+ throw error;
381
+ }
382
+ },
383
+ [client, isPaused]
384
+ );
385
+ (0, import_react5.useEffect)(() => {
386
+ if (autoExecute && isPaused && !isExecuting && pendingTools.length > 0) {
387
+ console.log("[useAgnoToolExecution] Auto-executing tools");
388
+ executeAndContinue();
389
+ }
390
+ }, [autoExecute, isPaused, isExecuting, pendingTools.length, executeAndContinue]);
391
+ return {
392
+ /** Whether the run is currently paused awaiting tool execution */
393
+ isPaused,
394
+ /** Whether tools are currently being executed */
395
+ isExecuting,
396
+ /** Tools awaiting execution */
397
+ pendingTools,
398
+ /** Execute all pending tools and continue the run */
399
+ executeAndContinue,
400
+ /** Execute specific tools and return results without continuing */
401
+ executeTools,
402
+ /** Continue the run with manually provided tool results */
403
+ continueWithResults,
404
+ /** Error from tool execution, if any */
405
+ executionError
406
+ };
407
+ }
408
+ // Annotate the CommonJS export names for ESM import in node:
409
+ 0 && (module.exports = {
410
+ AgnoProvider,
411
+ useAgnoActions,
412
+ useAgnoChat,
413
+ useAgnoClient,
414
+ useAgnoSession,
415
+ useAgnoToolExecution
416
+ });