@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.
- package/LICENSE +21 -0
- package/README.md +275 -0
- package/dist/context.d.ts +90 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +120 -0
- package/dist/context.js.map +1 -0
- package/dist/hooks.d.ts +290 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +442 -0
- package/dist/hooks.js.map +1 -0
- package/dist/index.d.ts +79 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +80 -0
- package/dist/index.js.map +1 -0
- package/dist/types.d.ts +212 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +6 -0
- package/dist/types.js.map +1 -0
- package/package.json +43 -0
- package/src/__tests__/hooks.spec.tsx +426 -0
- package/src/context.tsx +133 -0
- package/src/hooks.ts +612 -0
- package/src/index.ts +123 -0
- package/src/types.ts +271 -0
package/src/index.ts
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @agentick/react - React hooks and components for Agentick
|
|
3
|
+
*
|
|
4
|
+
* Provides React bindings for connecting to Agentick servers.
|
|
5
|
+
* Wraps @agentick/client with idiomatic React patterns.
|
|
6
|
+
*
|
|
7
|
+
* @example Quick start
|
|
8
|
+
* ```tsx
|
|
9
|
+
* import { AgentickProvider, useSession, useStreamingText } from '@agentick/react';
|
|
10
|
+
*
|
|
11
|
+
* function App() {
|
|
12
|
+
* return (
|
|
13
|
+
* <AgentickProvider clientConfig={{ baseUrl: 'https://api.example.com' }}>
|
|
14
|
+
* <Chat />
|
|
15
|
+
* </AgentickProvider>
|
|
16
|
+
* );
|
|
17
|
+
* }
|
|
18
|
+
*
|
|
19
|
+
* function Chat() {
|
|
20
|
+
* const { send } = useSession({ sessionId: 'my-session' });
|
|
21
|
+
* const { text, isStreaming } = useStreamingText();
|
|
22
|
+
* const [input, setInput] = useState('');
|
|
23
|
+
*
|
|
24
|
+
* const handleSend = async () => {
|
|
25
|
+
* await send(input);
|
|
26
|
+
* setInput('');
|
|
27
|
+
* };
|
|
28
|
+
*
|
|
29
|
+
* return (
|
|
30
|
+
* <div>
|
|
31
|
+
* <div className="response">
|
|
32
|
+
* {text}
|
|
33
|
+
* {isStreaming && <span className="cursor">|</span>}
|
|
34
|
+
* </div>
|
|
35
|
+
* <input value={input} onChange={(e) => setInput(e.target.value)} />
|
|
36
|
+
* <button onClick={handleSend}>Send</button>
|
|
37
|
+
* </div>
|
|
38
|
+
* );
|
|
39
|
+
* }
|
|
40
|
+
* ```
|
|
41
|
+
*
|
|
42
|
+
* @example With authentication
|
|
43
|
+
* ```tsx
|
|
44
|
+
* function App() {
|
|
45
|
+
* const { token } = useAuth();
|
|
46
|
+
*
|
|
47
|
+
* return (
|
|
48
|
+
* <AgentickProvider
|
|
49
|
+
* clientConfig={{
|
|
50
|
+
* baseUrl: 'https://api.example.com',
|
|
51
|
+
* token,
|
|
52
|
+
* }}
|
|
53
|
+
* >
|
|
54
|
+
* <Chat />
|
|
55
|
+
* </AgentickProvider>
|
|
56
|
+
* );
|
|
57
|
+
* }
|
|
58
|
+
* ```
|
|
59
|
+
*
|
|
60
|
+
* ## Hooks
|
|
61
|
+
*
|
|
62
|
+
* | Hook | Purpose |
|
|
63
|
+
* |------|---------|
|
|
64
|
+
* | `useClient()` | Direct client access |
|
|
65
|
+
* | `useConnection()` | SSE connection state |
|
|
66
|
+
* | `useSession(opts?)` | Session accessor (send, abort, close) |
|
|
67
|
+
* | `useConnectionState()` | Connection state subscription |
|
|
68
|
+
* | `useEvents(opts?)` | Stream event subscription |
|
|
69
|
+
* | `useStreamingText(opts?)` | Accumulated text from deltas |
|
|
70
|
+
* | `useEvents(opts?)` | Stream event subscription |
|
|
71
|
+
*
|
|
72
|
+
* @module @agentick/react
|
|
73
|
+
*/
|
|
74
|
+
|
|
75
|
+
// Provider
|
|
76
|
+
export { AgentickProvider } from "./context";
|
|
77
|
+
|
|
78
|
+
// Hooks
|
|
79
|
+
export {
|
|
80
|
+
useClient,
|
|
81
|
+
useConnection,
|
|
82
|
+
useSession,
|
|
83
|
+
useConnectionState,
|
|
84
|
+
useEvents,
|
|
85
|
+
useStreamingText,
|
|
86
|
+
useContextInfo,
|
|
87
|
+
type ContextInfo,
|
|
88
|
+
type UseContextInfoOptions,
|
|
89
|
+
type UseContextInfoResult,
|
|
90
|
+
} from "./hooks";
|
|
91
|
+
|
|
92
|
+
// Types
|
|
93
|
+
export type {
|
|
94
|
+
// Provider types
|
|
95
|
+
AgentickProviderProps,
|
|
96
|
+
AgentickContextValue,
|
|
97
|
+
TransportConfig,
|
|
98
|
+
|
|
99
|
+
// Hook types
|
|
100
|
+
UseConnectionOptions,
|
|
101
|
+
UseConnectionResult,
|
|
102
|
+
UseSessionOptions,
|
|
103
|
+
UseSessionResult,
|
|
104
|
+
UseEventsOptions,
|
|
105
|
+
UseEventsResult,
|
|
106
|
+
UseStreamingTextOptions,
|
|
107
|
+
UseStreamingTextResult,
|
|
108
|
+
} from "./types.js";
|
|
109
|
+
|
|
110
|
+
// Re-export client types and factory for convenience
|
|
111
|
+
export type {
|
|
112
|
+
AgentickClient,
|
|
113
|
+
ConnectionState,
|
|
114
|
+
StreamEvent,
|
|
115
|
+
SessionAccessor,
|
|
116
|
+
SendInput,
|
|
117
|
+
ClientExecutionHandle,
|
|
118
|
+
SessionStreamEvent,
|
|
119
|
+
ClientTransport,
|
|
120
|
+
} from "@agentick/client";
|
|
121
|
+
|
|
122
|
+
// Re-export createClient for users who want to create a client manually
|
|
123
|
+
export { createClient } from "@agentick/client";
|
package/src/types.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* React integration types for Agentick.
|
|
3
|
+
*
|
|
4
|
+
* @module @agentick/react/types
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type {
|
|
8
|
+
AgentickClient,
|
|
9
|
+
ConnectionState,
|
|
10
|
+
StreamEvent,
|
|
11
|
+
SendInput,
|
|
12
|
+
SessionAccessor,
|
|
13
|
+
ClientExecutionHandle,
|
|
14
|
+
SessionStreamEvent,
|
|
15
|
+
ClientTransport,
|
|
16
|
+
} from "@agentick/client";
|
|
17
|
+
import type { ContentBlock, Message } from "@agentick/shared";
|
|
18
|
+
import type { ReactNode } from "react";
|
|
19
|
+
|
|
20
|
+
// ============================================================================
|
|
21
|
+
// Provider Types
|
|
22
|
+
// ============================================================================
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Transport configuration for AgentickProvider.
|
|
26
|
+
* Can be a built-in transport type or a custom ClientTransport instance.
|
|
27
|
+
*/
|
|
28
|
+
export type TransportConfig = "sse" | "websocket" | "auto" | ClientTransport;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* Configuration for AgentickProvider.
|
|
32
|
+
*/
|
|
33
|
+
export interface AgentickProviderProps {
|
|
34
|
+
/**
|
|
35
|
+
* Pre-configured client instance.
|
|
36
|
+
* If provided, clientConfig is ignored.
|
|
37
|
+
*/
|
|
38
|
+
client?: AgentickClient;
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* Client configuration (used if client is not provided).
|
|
42
|
+
*/
|
|
43
|
+
clientConfig?: {
|
|
44
|
+
baseUrl: string;
|
|
45
|
+
/**
|
|
46
|
+
* Transport to use for communication.
|
|
47
|
+
* - "sse": HTTP/SSE transport (default for http:// and https:// URLs)
|
|
48
|
+
* - "websocket": WebSocket transport (default for ws:// and wss:// URLs)
|
|
49
|
+
* - "auto": Auto-detect based on URL scheme (default)
|
|
50
|
+
* - ClientTransport instance: Use a custom transport (e.g., SharedTransport for multi-tab)
|
|
51
|
+
*
|
|
52
|
+
* @example
|
|
53
|
+
* ```tsx
|
|
54
|
+
* import { createSharedTransport } from '@agentick/client-multiplexer';
|
|
55
|
+
*
|
|
56
|
+
* <AgentickProvider clientConfig={{
|
|
57
|
+
* baseUrl: 'https://api.example.com',
|
|
58
|
+
* transport: createSharedTransport({ baseUrl: 'https://api.example.com' }),
|
|
59
|
+
* }}>
|
|
60
|
+
* ```
|
|
61
|
+
*/
|
|
62
|
+
transport?: TransportConfig;
|
|
63
|
+
token?: string;
|
|
64
|
+
withCredentials?: boolean;
|
|
65
|
+
timeout?: number;
|
|
66
|
+
paths?: {
|
|
67
|
+
events?: string;
|
|
68
|
+
send?: string;
|
|
69
|
+
subscribe?: string;
|
|
70
|
+
abort?: string;
|
|
71
|
+
close?: string;
|
|
72
|
+
channel?: string;
|
|
73
|
+
};
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
children?: ReactNode;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Context value provided by AgentickProvider.
|
|
81
|
+
*/
|
|
82
|
+
export interface AgentickContextValue {
|
|
83
|
+
client: AgentickClient;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// ============================================================================
|
|
87
|
+
// Session Hooks
|
|
88
|
+
// ============================================================================
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Options for useSession hook.
|
|
92
|
+
*/
|
|
93
|
+
export interface UseSessionOptions {
|
|
94
|
+
/**
|
|
95
|
+
* Session ID to work with.
|
|
96
|
+
* If not provided, send() will use ephemeral sessions.
|
|
97
|
+
*/
|
|
98
|
+
sessionId?: string;
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* Automatically subscribe to the session on mount.
|
|
102
|
+
* Requires sessionId to be provided.
|
|
103
|
+
* @default false
|
|
104
|
+
*/
|
|
105
|
+
autoSubscribe?: boolean;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Return value from useSession hook.
|
|
110
|
+
*/
|
|
111
|
+
export interface UseSessionResult {
|
|
112
|
+
/**
|
|
113
|
+
* Session ID (if provided in options).
|
|
114
|
+
*/
|
|
115
|
+
sessionId?: string;
|
|
116
|
+
|
|
117
|
+
/**
|
|
118
|
+
* Whether this session is subscribed.
|
|
119
|
+
*/
|
|
120
|
+
isSubscribed: boolean;
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* Subscribe to this session (only if sessionId provided).
|
|
124
|
+
*/
|
|
125
|
+
subscribe: () => void;
|
|
126
|
+
|
|
127
|
+
/**
|
|
128
|
+
* Unsubscribe from this session.
|
|
129
|
+
*/
|
|
130
|
+
unsubscribe: () => void;
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Send a message.
|
|
134
|
+
*
|
|
135
|
+
* If sessionId was provided, sends to that session.
|
|
136
|
+
* Otherwise, creates an ephemeral session.
|
|
137
|
+
*/
|
|
138
|
+
send: (
|
|
139
|
+
input: string | ContentBlock | ContentBlock[] | Message | Message[] | SendInput,
|
|
140
|
+
) => ClientExecutionHandle;
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* Abort the session's current execution.
|
|
144
|
+
*/
|
|
145
|
+
abort: (reason?: string) => Promise<void>;
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Close this session.
|
|
149
|
+
*/
|
|
150
|
+
close: () => Promise<void>;
|
|
151
|
+
|
|
152
|
+
/**
|
|
153
|
+
* Session accessor for advanced operations (channels, tool confirmations).
|
|
154
|
+
* Only available if sessionId was provided.
|
|
155
|
+
*/
|
|
156
|
+
accessor?: SessionAccessor;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
// ============================================================================
|
|
160
|
+
// Connection Hooks
|
|
161
|
+
// ============================================================================
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Options for useConnection hook.
|
|
165
|
+
*/
|
|
166
|
+
export interface UseConnectionOptions {}
|
|
167
|
+
|
|
168
|
+
/**
|
|
169
|
+
* Return value from useConnection hook.
|
|
170
|
+
*/
|
|
171
|
+
export interface UseConnectionResult {
|
|
172
|
+
/**
|
|
173
|
+
* Current connection state.
|
|
174
|
+
*/
|
|
175
|
+
state: ConnectionState;
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Whether currently connected.
|
|
179
|
+
*/
|
|
180
|
+
isConnected: boolean;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Whether currently connecting.
|
|
184
|
+
*/
|
|
185
|
+
isConnecting: boolean;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
// ============================================================================
|
|
189
|
+
// Event Hooks
|
|
190
|
+
// ============================================================================
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Options for useEvents hook.
|
|
194
|
+
*/
|
|
195
|
+
export interface UseEventsOptions {
|
|
196
|
+
/**
|
|
197
|
+
* Optional session ID to filter events for.
|
|
198
|
+
* If not provided, receives all events from all sessions.
|
|
199
|
+
*/
|
|
200
|
+
sessionId?: string;
|
|
201
|
+
|
|
202
|
+
/**
|
|
203
|
+
* Optional event type filter.
|
|
204
|
+
* If provided, only events of these types are returned.
|
|
205
|
+
*/
|
|
206
|
+
filter?: Array<StreamEvent["type"] | SessionStreamEvent["type"]>;
|
|
207
|
+
|
|
208
|
+
/**
|
|
209
|
+
* Whether the hook is enabled.
|
|
210
|
+
* If false, no event subscription is created.
|
|
211
|
+
* @default true
|
|
212
|
+
*/
|
|
213
|
+
enabled?: boolean;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Return value from useEvents hook.
|
|
218
|
+
*/
|
|
219
|
+
export interface UseEventsResult {
|
|
220
|
+
/**
|
|
221
|
+
* Latest event received.
|
|
222
|
+
*/
|
|
223
|
+
event: StreamEvent | SessionStreamEvent | undefined;
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Clear the current event.
|
|
227
|
+
*/
|
|
228
|
+
clear: () => void;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// ============================================================================
|
|
232
|
+
// Streaming Text Hooks
|
|
233
|
+
// ============================================================================
|
|
234
|
+
|
|
235
|
+
/**
|
|
236
|
+
* Options for useStreamingText hook.
|
|
237
|
+
*/
|
|
238
|
+
export interface UseStreamingTextOptions {
|
|
239
|
+
/**
|
|
240
|
+
* Optional session ID to filter events for.
|
|
241
|
+
* If not provided, receives text from all sessions.
|
|
242
|
+
*/
|
|
243
|
+
sessionId?: string;
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Whether the hook is enabled.
|
|
247
|
+
* If false, no text accumulation occurs.
|
|
248
|
+
* @default true
|
|
249
|
+
*/
|
|
250
|
+
enabled?: boolean;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
/**
|
|
254
|
+
* Return value from useStreamingText hook.
|
|
255
|
+
*/
|
|
256
|
+
export interface UseStreamingTextResult {
|
|
257
|
+
/**
|
|
258
|
+
* Accumulated text from content_delta events.
|
|
259
|
+
*/
|
|
260
|
+
text: string;
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Whether currently streaming (between tick_start and execution_end).
|
|
264
|
+
*/
|
|
265
|
+
isStreaming: boolean;
|
|
266
|
+
|
|
267
|
+
/**
|
|
268
|
+
* Clear the accumulated text.
|
|
269
|
+
*/
|
|
270
|
+
clear: () => void;
|
|
271
|
+
}
|