@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,290 @@
1
+ /**
2
+ * React hooks for Agentick.
3
+ *
4
+ * @module @agentick/react/hooks
5
+ */
6
+ import type { AgentickClient, ConnectionState } from "@agentick/client";
7
+ import type { UseSessionOptions, UseSessionResult, UseEventsOptions, UseEventsResult, UseStreamingTextOptions, UseStreamingTextResult, UseConnectionOptions, UseConnectionResult } from "./types";
8
+ /**
9
+ * Access the Agentick client from context.
10
+ *
11
+ * @throws If used outside of AgentickProvider
12
+ *
13
+ * @example
14
+ * ```tsx
15
+ * import { useClient } from '@agentick/react';
16
+ *
17
+ * function MyComponent() {
18
+ * const client = useClient();
19
+ *
20
+ * // Direct client access for advanced use cases
21
+ * const handleCustomChannel = () => {
22
+ * const session = client.session('conv-123');
23
+ * const channel = session.channel('custom');
24
+ * channel.publish('event', { data: 'value' });
25
+ * };
26
+ *
27
+ * return <button onClick={handleCustomChannel}>Send</button>;
28
+ * }
29
+ * ```
30
+ */
31
+ export declare function useClient(): AgentickClient;
32
+ /**
33
+ * Subscribe to connection state changes.
34
+ *
35
+ * @example
36
+ * ```tsx
37
+ * import { useConnectionState } from '@agentick/react';
38
+ *
39
+ * function ConnectionIndicator() {
40
+ * const state = useConnectionState();
41
+ *
42
+ * return (
43
+ * <div className={`indicator ${state}`}>
44
+ * {state === 'connected' ? 'Online' : 'Offline'}
45
+ * </div>
46
+ * );
47
+ * }
48
+ * ```
49
+ */
50
+ export declare function useConnectionState(): ConnectionState;
51
+ /**
52
+ * Read the SSE connection state.
53
+ */
54
+ export declare function useConnection(_options?: UseConnectionOptions): UseConnectionResult;
55
+ /**
56
+ * Work with a specific session.
57
+ *
58
+ * @example Basic usage with session ID
59
+ * ```tsx
60
+ * import { useSession } from '@agentick/react';
61
+ *
62
+ * function Chat({ sessionId }: { sessionId: string }) {
63
+ * const { send, isSubscribed, subscribe } = useSession({ sessionId });
64
+ * const [input, setInput] = useState('');
65
+ *
66
+ * // Subscribe on mount
67
+ * useEffect(() => {
68
+ * subscribe();
69
+ * }, [subscribe]);
70
+ *
71
+ * const handleSend = async () => {
72
+ * await send(input);
73
+ * setInput('');
74
+ * };
75
+ *
76
+ * return (
77
+ * <div>
78
+ * <input value={input} onChange={(e) => setInput(e.target.value)} />
79
+ * <button onClick={handleSend}>Send</button>
80
+ * </div>
81
+ * );
82
+ * }
83
+ * ```
84
+ *
85
+ * @example Ephemeral session (no sessionId)
86
+ * ```tsx
87
+ * function QuickChat() {
88
+ * const { send } = useSession();
89
+ *
90
+ * // Each send creates/uses an ephemeral session
91
+ * const handleSend = () => send('Hello!');
92
+ *
93
+ * return <button onClick={handleSend}>Ask</button>;
94
+ * }
95
+ * ```
96
+ *
97
+ * @example Auto-subscribe
98
+ * ```tsx
99
+ * function Chat({ sessionId }: { sessionId: string }) {
100
+ * const { send, isSubscribed } = useSession({
101
+ * sessionId,
102
+ * autoSubscribe: true,
103
+ * });
104
+ *
105
+ * if (!isSubscribed) return <div>Subscribing...</div>;
106
+ *
107
+ * return <ChatInterface />;
108
+ * }
109
+ * ```
110
+ */
111
+ export declare function useSession(options?: UseSessionOptions): UseSessionResult;
112
+ /**
113
+ * Subscribe to stream events.
114
+ *
115
+ * Returns the latest event (not accumulated). Use useStreamingText
116
+ * for accumulated text from content_delta events.
117
+ *
118
+ * @example
119
+ * ```tsx
120
+ * import { useEvents } from '@agentick/react';
121
+ *
122
+ * function EventLog() {
123
+ * const { event } = useEvents();
124
+ *
125
+ * useEffect(() => {
126
+ * if (event) {
127
+ * console.log('Event:', event.type, event);
128
+ * }
129
+ * }, [event]);
130
+ *
131
+ * return <div>Latest: {event?.type}</div>;
132
+ * }
133
+ * ```
134
+ *
135
+ * @example With filter
136
+ * ```tsx
137
+ * function ToolCalls() {
138
+ * const { event } = useEvents({ filter: ['tool_call', 'tool_result'] });
139
+ *
140
+ * if (!event) return null;
141
+ *
142
+ * return <div>Tool: {event.type === 'tool_call' ? event.name : 'result'}</div>;
143
+ * }
144
+ * ```
145
+ *
146
+ * @example Session-specific events
147
+ * ```tsx
148
+ * function SessionEvents({ sessionId }: { sessionId: string }) {
149
+ * const { event } = useEvents({ sessionId });
150
+ * // Only receives events for this session
151
+ * return <div>{event?.type}</div>;
152
+ * }
153
+ * ```
154
+ */
155
+ export declare function useEvents(options?: UseEventsOptions): UseEventsResult;
156
+ /**
157
+ * Subscribe to streaming text from the client.
158
+ *
159
+ * Uses the client's built-in streaming text accumulation which handles
160
+ * tick_start, content_delta, tick_end, and execution_end events.
161
+ *
162
+ * @example
163
+ * ```tsx
164
+ * import { useStreamingText } from '@agentick/react';
165
+ *
166
+ * function StreamingResponse() {
167
+ * const { text, isStreaming } = useStreamingText();
168
+ *
169
+ * return (
170
+ * <div>
171
+ * <p>{text}</p>
172
+ * {isStreaming && <span className="cursor">|</span>}
173
+ * </div>
174
+ * );
175
+ * }
176
+ * ```
177
+ */
178
+ export declare function useStreamingText(options?: UseStreamingTextOptions): UseStreamingTextResult;
179
+ /**
180
+ * Context utilization info from the server.
181
+ * Updated after each tick with token usage and model capabilities.
182
+ */
183
+ export interface ContextInfo {
184
+ /** Model ID (e.g., "gpt-4o", "claude-3-5-sonnet-20241022") */
185
+ modelId: string;
186
+ /** Human-readable model name */
187
+ modelName?: string;
188
+ /** Provider name (e.g., "openai", "anthropic") */
189
+ provider?: string;
190
+ /** Context window size in tokens */
191
+ contextWindow?: number;
192
+ /** Input tokens used this tick */
193
+ inputTokens: number;
194
+ /** Output tokens generated this tick */
195
+ outputTokens: number;
196
+ /** Total tokens this tick */
197
+ totalTokens: number;
198
+ /** Context utilization percentage (0-100) */
199
+ utilization?: number;
200
+ /** Max output tokens for this model */
201
+ maxOutputTokens?: number;
202
+ /** Model capabilities */
203
+ supportsVision?: boolean;
204
+ supportsToolUse?: boolean;
205
+ isReasoningModel?: boolean;
206
+ /** Current tick number */
207
+ tick: number;
208
+ /** Cumulative usage across all ticks in this execution */
209
+ cumulativeUsage?: {
210
+ inputTokens: number;
211
+ outputTokens: number;
212
+ totalTokens: number;
213
+ ticks: number;
214
+ };
215
+ }
216
+ /**
217
+ * Options for useContextInfo hook.
218
+ */
219
+ export interface UseContextInfoOptions {
220
+ /**
221
+ * Optional session ID to filter events for.
222
+ * If not provided, receives context info from any session.
223
+ */
224
+ sessionId?: string;
225
+ /**
226
+ * Whether the hook is enabled.
227
+ * If false, no context info subscription is created.
228
+ * @default true
229
+ */
230
+ enabled?: boolean;
231
+ }
232
+ /**
233
+ * Return value from useContextInfo hook.
234
+ */
235
+ export interface UseContextInfoResult {
236
+ /**
237
+ * Latest context info (null before first tick completes).
238
+ */
239
+ contextInfo: ContextInfo | null;
240
+ /**
241
+ * Clear the current context info.
242
+ */
243
+ clear: () => void;
244
+ }
245
+ /**
246
+ * Subscribe to context utilization info from the server.
247
+ *
248
+ * Receives context_update events after each tick with:
249
+ * - Token usage (input, output, total)
250
+ * - Context utilization percentage
251
+ * - Model capabilities (vision, tools, reasoning)
252
+ * - Cumulative usage across ticks
253
+ *
254
+ * @example Basic usage
255
+ * ```tsx
256
+ * import { useContextInfo } from '@agentick/react';
257
+ *
258
+ * function ContextBar() {
259
+ * const { contextInfo } = useContextInfo();
260
+ *
261
+ * if (!contextInfo) return null;
262
+ *
263
+ * return (
264
+ * <div className="context-bar">
265
+ * <span>{contextInfo.modelId}</span>
266
+ * <span>{contextInfo.utilization?.toFixed(1)}% used</span>
267
+ * <progress value={contextInfo.utilization} max={100} />
268
+ * </div>
269
+ * );
270
+ * }
271
+ * ```
272
+ *
273
+ * @example Session-specific context info
274
+ * ```tsx
275
+ * function SessionContext({ sessionId }: { sessionId: string }) {
276
+ * const { contextInfo } = useContextInfo({ sessionId });
277
+ *
278
+ * if (!contextInfo) return <span>No context yet</span>;
279
+ *
280
+ * return (
281
+ * <span>
282
+ * {contextInfo.inputTokens.toLocaleString()} /
283
+ * {contextInfo.contextWindow?.toLocaleString() ?? '?'} tokens
284
+ * </span>
285
+ * );
286
+ * }
287
+ * ```
288
+ */
289
+ export declare function useContextInfo(options?: UseContextInfoOptions): UseContextInfoResult;
290
+ //# sourceMappingURL=hooks.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hooks.d.ts","sourceRoot":"","sources":["../src/hooks.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAWH,OAAO,KAAK,EACV,cAAc,EACd,eAAe,EAKhB,MAAM,kBAAkB,CAAC;AAE1B,OAAO,KAAK,EACV,iBAAiB,EACjB,gBAAgB,EAChB,gBAAgB,EAChB,eAAe,EACf,uBAAuB,EACvB,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACpB,MAAM,SAAS,CAAC;AAMjB;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,wBAAgB,SAAS,IAAI,cAAc,CAQ1C;AAMD;;;;;;;;;;;;;;;;;GAiBG;AACH,wBAAgB,kBAAkB,IAAI,eAAe,CAcpD;AAMD;;GAEG;AACH,wBAAgB,aAAa,CAAC,QAAQ,GAAE,oBAAyB,GAAG,mBAAmB,CActF;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAuDG;AACH,wBAAgB,UAAU,CAAC,OAAO,GAAE,iBAAsB,GAAG,gBAAgB,CAkG5E;AAMD;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA0CG;AACH,wBAAgB,SAAS,CAAC,OAAO,GAAE,gBAAqB,GAAG,eAAe,CAqCzE;AAMD;;;;;;;;;;;;;;;;;;;;;GAqBG;AACH,wBAAgB,gBAAgB,CAAC,OAAO,GAAE,uBAA4B,GAAG,sBAAsB,CAsB9F;AAMD;;;GAGG;AACH,MAAM,WAAW,WAAW;IAC1B,8DAA8D;IAC9D,OAAO,EAAE,MAAM,CAAC;IAChB,gCAAgC;IAChC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,kDAAkD;IAClD,Q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