@xpert-ai/chatkit-react 0.0.3 → 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/README.md ADDED
@@ -0,0 +1,362 @@
1
+ # ChatKit React
2
+
3
+ React bindings for ChatKit, providing lightweight components and hooks to integrate conversational AI interfaces into React applications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @xpert-ai/chatkit-react
9
+ # or
10
+ pnpm add @xpert-ai/chatkit-react
11
+ # or
12
+ yarn add @xpert-ai/chatkit-react
13
+ ```
14
+
15
+ ## Quick Start
16
+
17
+ ```tsx
18
+ import { useChatKit, ChatKit } from '@xpert-ai/chatkit-react';
19
+
20
+ function App() {
21
+ const chatkit = useChatKit({
22
+ frameUrl: '<url-to-chatkit-frame>',
23
+ api: {
24
+ apiUrl: 'https://api.xpertai.cn',
25
+ xpertId: 'your-assistant-id',
26
+ getClientSecret: async () => {
27
+ const response = await fetch('/api/create-session', {
28
+ method: 'POST',
29
+ headers: { 'Content-Type': 'application/json' },
30
+ });
31
+ const data = await response.json();
32
+ return {
33
+ secret: data.client_secret,
34
+ organizationId: data.organization_id,
35
+ };
36
+ }
37
+ },
38
+ onReady: () => {
39
+ console.log('ChatKit is ready');
40
+ }
41
+ });
42
+
43
+ return (
44
+ <div className="h-screen w-full">
45
+ <ChatKit control={chatkit.control} />
46
+ </div>
47
+ );
48
+ }
49
+ ```
50
+
51
+ ## API Reference
52
+
53
+ ### `useChatKit(options)`
54
+
55
+ A React hook that initializes and manages a ChatKit instance.
56
+
57
+ #### Parameters
58
+
59
+ ```typescript
60
+ interface UseChatKitOptions extends Partial<ChatKitOptions> {
61
+ frameUrl: string;
62
+ api: {
63
+ apiUrl: string;
64
+ xpertId: string;
65
+ getClientSecret: (
66
+ currentClientSecret: string | null,
67
+ ) => Promise<string | { secret: string; organizationId?: string }>;
68
+ };
69
+ locale?: SupportedLocale; // 'en-US' | 'zh-Hans'
70
+ theme?: ThemeOptions;
71
+ composer?: ComposerOptions;
72
+ startScreen?: StartScreenOptions;
73
+ onClientTool?: (params: ClientToolParams) => Promise<ClientToolMessageInput>;
74
+ onError?: (error: Error) => void;
75
+ onReady?: () => void;
76
+ onThreadChange?: (data: { threadId: string }) => void;
77
+ onEffect?: (data: { name: string; data: any }) => void;
78
+ }
79
+ ```
80
+
81
+ #### Returns
82
+
83
+ ```typescript
84
+ interface ChatKitInstance {
85
+ control: ChatKitControl; // Pass to <ChatKit> component
86
+ sendUserMessage: (params: SendMessageParams) => Promise<void>;
87
+ setThreadId: (threadId: string) => Promise<void>;
88
+ // ... other methods
89
+ }
90
+ ```
91
+
92
+ `getClientSecret` may keep returning the legacy `string`, or return
93
+ `{ secret, organizationId }` to have ChatKit send `organization-id` on hosted
94
+ API requests.
95
+
96
+ ### `<ChatKit />` Component
97
+
98
+ The main UI component that renders the chat interface.
99
+
100
+ #### Props
101
+
102
+ ```typescript
103
+ interface ChatKitProps {
104
+ control: ChatKitControl;
105
+ className?: string;
106
+ }
107
+ ```
108
+
109
+ ## Configuration Examples
110
+
111
+ ### Theme Customization
112
+
113
+ ```tsx
114
+ const chatkit = useChatKit({
115
+ frameUrl: 'https://chatkit.studio/frame',
116
+ api: { /* ... */ },
117
+ theme: {
118
+ colorScheme: 'light', // or 'dark'
119
+ radius: 'pill', // 'none' | 'small' | 'medium' | 'large' | 'pill'
120
+ density: 'normal', // 'compact' | 'normal' | 'comfortable'
121
+ color: {
122
+ grayscale: {
123
+ hue: 120,
124
+ tint: 6
125
+ },
126
+ accent: {
127
+ primary: '#83e58a',
128
+ level: 2
129
+ }
130
+ },
131
+ typography: {
132
+ baseSize: 16,
133
+ fontFamily: 'Inter, sans-serif',
134
+ }
135
+ }
136
+ });
137
+ ```
138
+
139
+ ### Composer Configuration
140
+
141
+ ```tsx
142
+ const chatkit = useChatKit({
143
+ frameUrl: '<url-to-chatkit-frame>',
144
+ api: { /* ... */ },
145
+ composer: {
146
+ attachments: {
147
+ enabled: true,
148
+ maxCount: 5,
149
+ maxSize: 10485760 // 10MB
150
+ },
151
+ tools: [
152
+ {
153
+ id: 'search_docs',
154
+ label: 'Search Documentation',
155
+ shortLabel: 'Search',
156
+ placeholderOverride: 'Ask about our docs...',
157
+ icon: 'book-open',
158
+ pinned: false
159
+ }
160
+ ]
161
+ }
162
+ });
163
+ ```
164
+
165
+ ### Start Screen with Prompts
166
+
167
+ ```tsx
168
+ const chatkit = useChatKit({
169
+ frameUrl: '<url-to-chatkit-frame>',
170
+ api: { /* ... */ },
171
+ startScreen: {
172
+ greeting: 'Welcome! How can I help you today?',
173
+ prompts: [
174
+ {
175
+ icon: 'circle-question',
176
+ label: 'What is ChatKit?',
177
+ prompt: 'What is ChatKit?'
178
+ },
179
+ {
180
+ icon: 'lightbulb',
181
+ label: 'Show me examples',
182
+ prompt: 'Can you show me some examples?'
183
+ }
184
+ ]
185
+ }
186
+ });
187
+ ```
188
+
189
+ ### Handling Client Tools
190
+
191
+ ```tsx
192
+ const chatkit = useChatKit({
193
+ frameUrl: '<url-to-chatkit-frame>',
194
+ api: { /* ... */ },
195
+ onClientTool: async ({ name, params, id, tool_call_id }) => {
196
+ console.log(`Tool invoked: ${name}`, params);
197
+
198
+ // Perform tool action
199
+ let result;
200
+ if (name === 'get_weather') {
201
+ result = await fetchWeather(params.location);
202
+ }
203
+
204
+ return {
205
+ tool_call_id: tool_call_id || id,
206
+ name: name,
207
+ status: 'success',
208
+ content: JSON.stringify(result)
209
+ };
210
+ }
211
+ });
212
+ ```
213
+
214
+ ### Error Handling and Lifecycle Events
215
+
216
+ ```tsx
217
+ const chatkit = useChatKit({
218
+ frameUrl: '<url-to-chatkit-frame>',
219
+ api: { /* ... */ },
220
+ onError: (error) => {
221
+ console.error('ChatKit error:', error);
222
+ // Handle error (show toast, etc.)
223
+ },
224
+ onReady: () => {
225
+ console.log('ChatKit initialized successfully');
226
+ },
227
+ onThreadChange: ({ threadId }) => {
228
+ console.log('Thread changed:', threadId);
229
+ // Save thread ID, update URL, etc.
230
+ },
231
+ onEffect: ({ name, data }) => {
232
+ console.log(`Effect triggered: ${name}`, data);
233
+ // Handle custom effects from assistant
234
+ }
235
+ });
236
+ ```
237
+
238
+ ## Common Usage Patterns
239
+
240
+ ### Sending Messages Programmatically
241
+
242
+ ```tsx
243
+ function App() {
244
+ const chatkit = useChatKit({ /* ... */ });
245
+
246
+ const handleSendMessage = () => {
247
+ chatkit.sendUserMessage({
248
+ text: 'Hello, ChatKit!',
249
+ newThread: true
250
+ });
251
+ };
252
+
253
+ const handleSendWithState = () => {
254
+ chatkit.sendUserMessage({
255
+ text: 'Greet me',
256
+ newThread: true,
257
+ state: {
258
+ user_name: 'Alice',
259
+ preferences: { theme: 'dark' }
260
+ }
261
+ });
262
+ };
263
+
264
+ return (
265
+ <div>
266
+ <button onClick={handleSendMessage}>Start Conversation</button>
267
+ <button onClick={handleSendWithState}>Send with Context</button>
268
+ <ChatKit control={chatkit.control} />
269
+ </div>
270
+ );
271
+ }
272
+ ```
273
+
274
+ ### Thread Management
275
+
276
+ ```tsx
277
+ function App() {
278
+ const [threads, setThreads] = useState<string[]>([]);
279
+ const chatkit = useChatKit({
280
+ /* ... */
281
+ onThreadChange: ({ threadId }) => {
282
+ if (threadId && !threads.includes(threadId)) {
283
+ setThreads(prev => [...prev, threadId]);
284
+ }
285
+ }
286
+ });
287
+
288
+ const switchThread = (threadId: string) => {
289
+ chatkit.setThreadId(threadId);
290
+ };
291
+
292
+ return (
293
+ <div>
294
+ <aside>
295
+ <h3>Threads</h3>
296
+ {threads.map(id => (
297
+ <button key={id} onClick={() => switchThread(id)}>
298
+ {id}
299
+ </button>
300
+ ))}
301
+ </aside>
302
+ <ChatKit control={chatkit.control} />
303
+ </div>
304
+ );
305
+ }
306
+ ```
307
+
308
+ ### Internationalization
309
+
310
+ ```tsx
311
+ import { useState } from 'react';
312
+ import type { SupportedLocale } from '@xpert-ai/chatkit-types';
313
+
314
+ function App() {
315
+ const [locale, setLocale] = useState<SupportedLocale>('en-US');
316
+
317
+ const chatkit = useChatKit({
318
+ locale,
319
+ /* ... */
320
+ });
321
+
322
+ return (
323
+ <div>
324
+ <select
325
+ value={locale}
326
+ onChange={(e) => setLocale(e.target.value as SupportedLocale)}
327
+ >
328
+ <option value="en-US">English</option>
329
+ <option value="zh-Hans">简体中文</option>
330
+ </select>
331
+ <ChatKit control={chatkit.control} />
332
+ </div>
333
+ );
334
+ }
335
+ ```
336
+
337
+ ## TypeScript Support
338
+
339
+ This package is written in TypeScript and includes full type definitions.
340
+
341
+ ```typescript
342
+ import type {
343
+ ChatKitOptions,
344
+ ClientToolMessageInput,
345
+ SupportedLocale
346
+ } from '@xpert-ai/chatkit-types';
347
+ ```
348
+
349
+ ## Requirements
350
+
351
+ - React >= 18
352
+ - React DOM >= 18
353
+
354
+ ## Related Packages
355
+
356
+ - `@xpert-ai/chatkit` - Core ChatKit library
357
+ - `@xpert-ai/chatkit-types` - TypeScript type definitions
358
+ - `@xpert-ai/chatkit-ui` - Pre-built UI components
359
+
360
+ ## License
361
+
362
+ See LICENSE file in the repository root.
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
- import { ChatKitEvents } from '@xpert-ai/chatkit-types';
2
- import { ChatKitOptions } from '@xpert-ai/chatkit-types';
1
+ import { ChatKitEvents } from '../../chatkit/src/index.ts';
2
+ import { ChatKitOptions } from '../../chatkit/src/index.ts';
3
3
  import * as React_2 from 'react';
4
- import { XpertAIChatKit } from '@xpert-ai/chatkit-types';
4
+ import { XpertAIChatKit } from '../../chatkit/src/index.ts';
5
5
 
6
6
  export declare const ChatKit: React_2.ForwardRefExoticComponent<ChatKitProps & React_2.RefAttributes<XpertAIChatKit>>;
7
7
 
@@ -13,7 +13,7 @@ declare type ChatKitControl = {
13
13
  handlers: ChatKitEventHandlers;
14
14
  };
15
15
 
16
- declare type ChatKitEventHandlers = Partial<{
16
+ export declare type ChatKitEventHandlers = Partial<{
17
17
  [K in keyof ChatKitEvents as ToEventHandlerKey<K>]: ChatKitEvents[K] extends CustomEvent<infer Detail> ? Detail extends undefined ? () => void : (event: Detail) => void : never;
18
18
  }>;
19
19
 
@@ -29,7 +29,7 @@ declare interface ChatKitProps extends React_2.HTMLAttributes<XpertAIChatKit> {
29
29
 
30
30
  declare type DotToCamelCase<S extends string> = S extends `${infer Head}.${infer Tail}` ? `${Head}${Capitalize<DotToCamelCase<Tail>>}` : S;
31
31
 
32
- declare type ToEventHandlerKey<K extends keyof ChatKitEvents> = DotToCamelCase<K> extends `chatkit${infer EventName}` ? `on${Capitalize<EventName>}` : never;
32
+ export declare type ToEventHandlerKey<K extends keyof ChatKitEvents> = DotToCamelCase<K> extends `chatkit${infer EventName}` ? `on${Capitalize<EventName>}` : never;
33
33
 
34
34
  export declare function useChatKit(options: UseChatKitOptions): UseChatKitReturn;
35
35