@agentick/react 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,133 @@
1
+ /**
2
+ * React context for Agentick client.
3
+ *
4
+ * @module @agentick/react/context
5
+ */
6
+
7
+ import { createContext, useMemo, useEffect } from "react";
8
+ import { createClient } from "@agentick/client";
9
+ import type { AgentickProviderProps, AgentickContextValue } from "./types";
10
+
11
+ // ============================================================================
12
+ // Context
13
+ // ============================================================================
14
+
15
+ export const AgentickContext = createContext<AgentickContextValue | null>(null);
16
+
17
+ // ============================================================================
18
+ // Provider
19
+ // ============================================================================
20
+
21
+ /**
22
+ * Provider for Agentick client context.
23
+ *
24
+ * Either provide a pre-configured client or clientConfig to create one.
25
+ *
26
+ * @example With client config
27
+ * ```tsx
28
+ * import { AgentickProvider } from '@agentick/react';
29
+ *
30
+ * function App() {
31
+ * return (
32
+ * <AgentickProvider
33
+ * clientConfig={{
34
+ * baseUrl: 'https://api.example.com',
35
+ * token: 'my-token',
36
+ * }}
37
+ * >
38
+ * <Chat />
39
+ * </AgentickProvider>
40
+ * );
41
+ * }
42
+ * ```
43
+ *
44
+ * @example With pre-configured client
45
+ * ```tsx
46
+ * import { AgentickProvider } from '@agentick/react';
47
+ * import { createClient } from '@agentick/client';
48
+ *
49
+ * const client = createClient({
50
+ * baseUrl: 'https://api.example.com',
51
+ * token: 'my-token',
52
+ * });
53
+ *
54
+ * function App() {
55
+ * return (
56
+ * <AgentickProvider client={client}>
57
+ * <Chat />
58
+ * </AgentickProvider>
59
+ * );
60
+ * }
61
+ * ```
62
+ *
63
+ * @example Multiple agents with separate instances
64
+ * ```tsx
65
+ * // Each provider creates its own client and connection
66
+ * function App() {
67
+ * return (
68
+ * <div className="dashboard">
69
+ * <AgentickProvider clientConfig={{ baseUrl: '/api/support-agent' }}>
70
+ * <SupportChat />
71
+ * </AgentickProvider>
72
+ *
73
+ * <AgentickProvider clientConfig={{ baseUrl: '/api/sales-agent' }}>
74
+ * <SalesChat />
75
+ * </AgentickProvider>
76
+ * </div>
77
+ * );
78
+ * }
79
+ * ```
80
+ *
81
+ * @example Sharing a client between components
82
+ * ```tsx
83
+ * // Create one client, share across multiple providers
84
+ * const sharedClient = createClient({ baseUrl: '/api/agent' });
85
+ *
86
+ * function App() {
87
+ * return (
88
+ * <>
89
+ * <AgentickProvider client={sharedClient}>
90
+ * <MainChat />
91
+ * </AgentickProvider>
92
+ *
93
+ * {/* Both providers share the same client and connection * /}
94
+ * <AgentickProvider client={sharedClient}>
95
+ * <ChatSidebar />
96
+ * </AgentickProvider>
97
+ * </>
98
+ * );
99
+ * }
100
+ * ```
101
+ */
102
+ export function AgentickProvider({
103
+ client: providedClient,
104
+ clientConfig,
105
+ children,
106
+ }: AgentickProviderProps) {
107
+ // Create client from config if not provided
108
+ const client = useMemo(() => {
109
+ if (providedClient) {
110
+ return providedClient;
111
+ }
112
+
113
+ if (!clientConfig) {
114
+ throw new Error("AgentickProvider requires either a client or clientConfig prop");
115
+ }
116
+
117
+ return createClient(clientConfig);
118
+ }, [providedClient, clientConfig]);
119
+
120
+ // Cleanup on unmount (only if we created the client)
121
+ useEffect(() => {
122
+ // Only destroy if we created it (not provided)
123
+ if (!providedClient) {
124
+ return () => {
125
+ client.destroy();
126
+ };
127
+ }
128
+ }, [client, providedClient]);
129
+
130
+ const value = useMemo<AgentickContextValue>(() => ({ client }), [client]);
131
+
132
+ return <AgentickContext.Provider value={value}>{children}</AgentickContext.Provider>;
133
+ }