@chainlit/react-client 0.0.602 → 0.1.2

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 CHANGED
@@ -17,11 +17,19 @@ import React from 'react';
17
17
  import ReactDOM from 'react-dom/client';
18
18
  import { RecoilRoot } from 'recoil';
19
19
 
20
+ import { ChainlitAPI, ChainlitContext } from '@chainlit/react-client';
21
+
22
+ const CHAINLIT_SERVER_URL = 'http://localhost:8000';
23
+
24
+ const apiClient = new ChainlitAPI(CHAINLIT_SERVER_URL, 'webapp');
25
+
20
26
  ReactDOM.createRoot(document.getElementById('root') as HTMLElement).render(
21
27
  <React.StrictMode>
22
- <RecoilRoot>
23
- <MyApp />
24
- </RecoilRoot>
28
+ <ChainlitContext.Provider value={apiClient}>
29
+ <RecoilRoot>
30
+ <MyApp />
31
+ </RecoilRoot>
32
+ </ChainlitContext.Provider>
25
33
  </React.StrictMode>
26
34
  );
27
35
  ```
@@ -41,11 +49,7 @@ This hook is responsible for managing the chat session's connection to the WebSo
41
49
  #### Example
42
50
 
43
51
  ```jsx
44
- import { ChainlitAPI, useChatSession } from '@chainlit/react-client';
45
-
46
- const CHAINLIT_SERVER_URL = 'http://localhost:8000';
47
-
48
- const apiClient = new ChainlitAPI(CHAINLIT_SERVER_URL, 'app');
52
+ import { useChatSession } from '@chainlit/react-client';
49
53
 
50
54
  const ChatComponent = () => {
51
55
  const { connect, disconnect, chatProfile, setChatProfile } = useChatSession();
@@ -53,7 +57,6 @@ const ChatComponent = () => {
53
57
  // Connect to the WebSocket server
54
58
  useEffect(() => {
55
59
  connect({
56
- client: apiClient,
57
60
  userEnv: {
58
61
  /* user environment variables */
59
62
  },
@@ -63,7 +66,7 @@ const ChatComponent = () => {
63
66
  return () => {
64
67
  disconnect();
65
68
  };
66
- }, [connect, disconnect]);
69
+ }, []);
67
70
 
68
71
  // Rest of your component logic
69
72
  };
package/dist/index.d.mts CHANGED
@@ -2,6 +2,7 @@ import * as recoil from 'recoil';
2
2
  import { Socket } from 'socket.io-client';
3
3
  export { Socket } from 'socket.io-client';
4
4
  import * as lodash from 'lodash';
5
+ import * as react from 'react';
5
6
  import * as swr__internal from 'swr/_internal';
6
7
  import { SWRConfiguration } from 'swr';
7
8
 
@@ -21,7 +22,7 @@ interface ICallFn {
21
22
  args: Record<string, any>;
22
23
  }
23
24
 
24
- type IElement = IImageElement | ITextElement | IPdfElement | IAvatarElement | ITasklistElement | IAudioElement | IVideoElement | IFileElement | IPlotlyElement;
25
+ type IElement = IImageElement | ITextElement | IPdfElement | ITasklistElement | IAudioElement | IVideoElement | IFileElement | IPlotlyElement;
25
26
  type IMessageElement = IImageElement | ITextElement | IPdfElement | IAudioElement | IVideoElement | IFileElement | IPlotlyElement;
26
27
  type ElementType = IElement['type'];
27
28
  type IElementSize = 'small' | 'medium' | 'large';
@@ -41,9 +42,6 @@ interface TMessageElement<T> extends TElement<T> {
41
42
  interface IImageElement extends TMessageElement<'image'> {
42
43
  size?: IElementSize;
43
44
  }
44
- interface IAvatarElement extends TElement<'avatar'> {
45
- name: string;
46
- }
47
45
  interface ITextElement extends TMessageElement<'text'> {
48
46
  language?: string;
49
47
  }
@@ -51,9 +49,11 @@ interface IPdfElement extends TMessageElement<'pdf'> {
51
49
  page?: number;
52
50
  }
53
51
  interface IAudioElement extends TMessageElement<'audio'> {
52
+ autoPlay?: boolean;
54
53
  }
55
54
  interface IVideoElement extends TMessageElement<'video'> {
56
55
  size?: IElementSize;
56
+ playerConfig?: object;
57
57
  }
58
58
  interface IFileElement extends TMessageElement<'file'> {
59
59
  type: 'file';
@@ -66,56 +66,11 @@ interface ITasklistElement extends TElement<'tasklist'> {
66
66
  interface IFeedback {
67
67
  id?: string;
68
68
  forId?: string;
69
+ threadId?: string;
69
70
  comment?: string;
70
71
  value: number;
71
72
  }
72
73
 
73
- type GenerationMessageRole = 'system' | 'assistant' | 'user' | 'function' | 'tool';
74
- type ILLMSettings = Record<string, string | string[] | number | boolean>;
75
- interface IGenerationMessage {
76
- content: string;
77
- role: GenerationMessageRole;
78
- name?: string;
79
- tool_calls?: any[];
80
- }
81
- interface IFunction {
82
- name: string;
83
- description: string;
84
- parameters: {
85
- required: string[];
86
- properties: Record<string, {
87
- title: string;
88
- type: string;
89
- }>;
90
- };
91
- }
92
- interface ITool {
93
- type: string;
94
- function: IFunction;
95
- }
96
- interface IBaseGeneration {
97
- provider: string;
98
- model?: string;
99
- error?: string;
100
- id?: string;
101
- variables?: Record<string, string>;
102
- tags?: string[];
103
- settings?: ILLMSettings;
104
- tools?: ITool[];
105
- tokenCount?: number;
106
- }
107
- interface ICompletionGeneration extends IBaseGeneration {
108
- type: 'COMPLETION';
109
- prompt?: string;
110
- completion?: string;
111
- }
112
- interface IChatGeneration extends IBaseGeneration {
113
- type: 'CHAT';
114
- messages?: IGenerationMessage[];
115
- messageCompletion?: IGenerationMessage;
116
- }
117
- type IGeneration = ICompletionGeneration | IChatGeneration;
118
-
119
74
  type StepType = 'assistant_message' | 'user_message' | 'system_message' | 'run' | 'tool' | 'llm' | 'embedding' | 'retrieval' | 'rerank' | 'undefined';
120
75
  interface IStep {
121
76
  id: string;
@@ -135,7 +90,6 @@ interface IStep {
135
90
  feedback?: IFeedback;
136
91
  language?: string;
137
92
  streaming?: boolean;
138
- generation?: IGeneration;
139
93
  steps?: IStep[];
140
94
  indent?: number;
141
95
  }
@@ -159,7 +113,7 @@ interface IAsk {
159
113
  } & FileSpec & ActionSpec;
160
114
  }
161
115
 
162
- type AuthProvider = 'credentials' | 'header' | 'github' | 'google' | 'azure-ad';
116
+ type AuthProvider = 'credentials' | 'header' | 'github' | 'google' | 'azure-ad' | 'azure-ad-hybrid';
163
117
  interface IUserMetadata extends Record<string, any> {
164
118
  tags?: string[];
165
119
  image?: string;
@@ -195,15 +149,69 @@ type ThreadHistory = {
195
149
  pageInfo?: IPageInfo;
196
150
  };
197
151
 
152
+ interface IStarter {
153
+ label: string;
154
+ message: string;
155
+ icon?: string;
156
+ }
157
+ interface ChatProfile {
158
+ default: boolean;
159
+ icon?: string;
160
+ name: string;
161
+ markdown_description: string;
162
+ starters?: IStarter[];
163
+ }
164
+ interface IAudioConfig {
165
+ enabled: boolean;
166
+ min_decibels: number;
167
+ initial_silence_timeout: number;
168
+ silence_timeout: number;
169
+ chunk_duration: number;
170
+ max_duration: number;
171
+ }
172
+ interface IChainlitConfig {
173
+ markdown?: string;
174
+ ui: {
175
+ name: string;
176
+ description?: string;
177
+ default_collapse_content?: boolean;
178
+ github?: string;
179
+ theme: any;
180
+ custom_css?: string;
181
+ custom_js?: string;
182
+ custom_font?: string;
183
+ custom_meta_image_url?: string;
184
+ };
185
+ features: {
186
+ spontaneous_file_upload?: {
187
+ enabled?: boolean;
188
+ max_size_mb?: number;
189
+ max_files?: number;
190
+ accept?: string[] | Record<string, string[]>;
191
+ };
192
+ audio: IAudioConfig;
193
+ unsafe_allow_html?: boolean;
194
+ latex?: boolean;
195
+ };
196
+ debugUrl?: string;
197
+ userEnv: string[];
198
+ dataPersistence: boolean;
199
+ threadResumable: boolean;
200
+ chatProfiles: ChatProfile[];
201
+ starters?: IStarter[];
202
+ translation: object;
203
+ }
204
+
198
205
  interface IToken {
199
206
  id: number | string;
200
207
  token: string;
201
208
  isSequence: boolean;
209
+ isInput: boolean;
202
210
  }
203
211
  declare const useChatData: () => {
204
212
  actions: IAction[];
205
213
  askUser: IAsk | undefined;
206
- avatars: IAvatarElement[];
214
+ callFn: ICallFn | undefined;
207
215
  chatSettingsDefaultValue: any;
208
216
  chatSettingsInputs: any;
209
217
  chatSettingsValue: any;
@@ -215,7 +223,93 @@ declare const useChatData: () => {
215
223
  tasklists: ITasklistElement[];
216
224
  };
217
225
 
218
- declare const useAuth: (apiClient: ChainlitAPI) => {
226
+ type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
227
+ declare const useChatInteract: () => {
228
+ uploadFile: (file: File, onProgress: (progress: number) => void) => {
229
+ xhr: XMLHttpRequest;
230
+ promise: Promise<{
231
+ id: string;
232
+ }>;
233
+ };
234
+ callAction: (action: IAction) => Promise<{
235
+ id: string;
236
+ status: boolean;
237
+ response?: string | undefined;
238
+ }> | undefined;
239
+ clear: () => void;
240
+ replyMessage: (message: IStep) => void;
241
+ sendMessage: (message: PartialBy<IStep, 'createdAt' | 'id'>, fileReferences?: IFileRef[]) => void;
242
+ sendAudioChunk: (isStart: boolean, mimeType: string, elapsedTime: number, data: Blob) => void;
243
+ endAudioStream: (fileReferences?: IFileRef[]) => void;
244
+ stopTask: () => void;
245
+ setIdToResume: recoil.SetterOrUpdater<string | undefined>;
246
+ updateChatSettings: (values: object) => void;
247
+ };
248
+
249
+ declare const useChatMessages: () => {
250
+ threadId: string | undefined;
251
+ messages: IStep[];
252
+ firstInteraction: string | undefined;
253
+ };
254
+
255
+ interface ISession {
256
+ socket: Socket;
257
+ error?: boolean;
258
+ }
259
+ declare const threadIdToResumeState: recoil.RecoilState<string | undefined>;
260
+ declare const chatProfileState: recoil.RecoilState<string | undefined>;
261
+ declare const sessionIdState: recoil.RecoilState<string>;
262
+ declare const sessionState: recoil.RecoilState<ISession | undefined>;
263
+ declare const actionState: recoil.RecoilState<IAction[]>;
264
+ declare const messagesState: recoil.RecoilState<IStep[]>;
265
+ declare const tokenCountState: recoil.RecoilState<number>;
266
+ declare const loadingState: recoil.RecoilState<boolean>;
267
+ declare const askUserState: recoil.RecoilState<IAsk | undefined>;
268
+ declare const callFnState: recoil.RecoilState<ICallFn | undefined>;
269
+ declare const chatSettingsInputsState: recoil.RecoilState<any>;
270
+ declare const chatSettingsDefaultValueSelector: recoil.RecoilValueReadOnly<any>;
271
+ declare const chatSettingsValueState: recoil.RecoilState<any>;
272
+ declare const elementState: recoil.RecoilState<IMessageElement[]>;
273
+ declare const tasklistState: recoil.RecoilState<ITasklistElement[]>;
274
+ declare const firstUserInteraction: recoil.RecoilState<string | undefined>;
275
+ declare const accessTokenState: recoil.RecoilState<string | undefined>;
276
+ declare const userState: recoil.RecoilState<IUser | null>;
277
+ declare const configState: recoil.RecoilState<IChainlitConfig | undefined>;
278
+ declare const threadHistoryState: recoil.RecoilState<ThreadHistory | undefined>;
279
+ declare const sideViewState: recoil.RecoilState<IMessageElement | undefined>;
280
+ declare const currentThreadIdState: recoil.RecoilState<string | undefined>;
281
+
282
+ declare const useChatSession: () => {
283
+ connect: lodash.DebouncedFunc<({ userEnv, accessToken }: {
284
+ userEnv: Record<string, string>;
285
+ accessToken?: string | undefined;
286
+ }) => void>;
287
+ disconnect: () => void;
288
+ session: ISession | undefined;
289
+ sessionId: string;
290
+ chatProfile: string | undefined;
291
+ idToResume: string | undefined;
292
+ setChatProfile: recoil.SetterOrUpdater<string | undefined>;
293
+ };
294
+
295
+ declare const useAudio: (config?: IAudioConfig) => {
296
+ startRecording: (fileReferences?: IFileRef[]) => void;
297
+ stopRecording: () => void;
298
+ cancelRecording: () => void;
299
+ isRecording: boolean;
300
+ isSpeaking: boolean;
301
+ isRecordingFinished: boolean;
302
+ error: string | undefined;
303
+ };
304
+
305
+ declare const useConfig: (accessToken?: string) => {
306
+ config: IChainlitConfig | undefined;
307
+ error: Error | undefined;
308
+ isLoading: boolean;
309
+ language: string;
310
+ };
311
+
312
+ declare const useAuth: () => {
219
313
  data: {
220
314
  requireLogin: boolean;
221
315
  passwordAuth: boolean;
@@ -244,7 +338,9 @@ declare const useAuth: (apiClient: ChainlitAPI) => {
244
338
  };
245
339
 
246
340
  declare const fetcher: (client: ChainlitAPI, endpoint: string, token?: string) => Promise<any>;
247
- declare function useApi<T>(client: ChainlitAPI, path?: string | null, options?: SWRConfiguration): swr__internal.SWRResponse<T, Error, Partial<swr__internal.PublicConfiguration<T, Error, swr__internal.BareFetcher<T>>> | undefined>;
341
+ declare function useApi<T>(path?: string | null, { token, ...swrConfig }?: SWRConfiguration & {
342
+ token?: string;
343
+ }): swr__internal.SWRResponse<T, Error, Partial<swr__internal.PublicConfiguration<T, Error, swr__internal.BareFetcher<T>>> | undefined>;
248
344
 
249
345
  interface IThreadFilters {
250
346
  search?: string;
@@ -266,10 +362,10 @@ declare class ClientError extends Error {
266
362
  type Payload = FormData | any;
267
363
  declare class APIBase {
268
364
  httpEndpoint: string;
269
- type: 'app' | 'copilot' | 'teams' | 'slack';
365
+ type: 'webapp' | 'copilot' | 'teams' | 'slack' | 'discord';
270
366
  on401?: (() => void) | undefined;
271
367
  onError?: ((error: ClientError) => void) | undefined;
272
- constructor(httpEndpoint: string, type: 'app' | 'copilot' | 'teams' | 'slack', on401?: (() => void) | undefined, onError?: ((error: ClientError) => void) | undefined);
368
+ constructor(httpEndpoint: string, type: 'webapp' | 'copilot' | 'teams' | 'slack' | 'discord', on401?: (() => void) | undefined, onError?: ((error: ClientError) => void) | undefined);
273
369
  buildEndpoint(path: string): string;
274
370
  checkToken(token: string): string;
275
371
  fetch(method: string, path: string, token?: string, data?: Payload, signal?: AbortSignal): Promise<Response>;
@@ -283,7 +379,6 @@ declare class ChainlitAPI extends APIBase {
283
379
  headerAuth(): Promise<any>;
284
380
  passwordAuth(data: FormData): Promise<any>;
285
381
  logout(): Promise<any>;
286
- getGeneration(generation: IGeneration, userEnv: {} | undefined, controller: AbortController, accessToken?: string, tokenCb?: (done: boolean, token: string) => void): Promise<ReadableStream<any>>;
287
382
  setFeedback(feedback: IFeedback, accessToken?: string): Promise<{
288
383
  success: boolean;
289
384
  feedbackId: string;
@@ -307,70 +402,8 @@ declare class ChainlitAPI extends APIBase {
307
402
  getOAuthEndpoint(provider: string): string;
308
403
  }
309
404
 
310
- declare const useChatInteract: () => {
311
- uploadFile: (client: ChainlitAPI, file: File, onProgress: (progress: number) => void) => {
312
- xhr: XMLHttpRequest;
313
- promise: Promise<{
314
- id: string;
315
- }>;
316
- };
317
- callAction: (action: IAction) => Promise<{
318
- id: string;
319
- status: boolean;
320
- response?: string | undefined;
321
- }> | undefined;
322
- clear: () => void;
323
- replyMessage: (message: IStep) => void;
324
- sendMessage: (message: IStep, fileReferences?: IFileRef[]) => void;
325
- stopTask: () => void;
326
- setIdToResume: recoil.SetterOrUpdater<string | undefined>;
327
- updateChatSettings: (values: object) => void;
328
- };
329
-
330
- declare const useChatMessages: () => {
331
- messages: IStep[];
332
- firstInteraction: string | undefined;
333
- };
334
-
335
- interface ISession {
336
- socket: Socket;
337
- error?: boolean;
338
- }
339
- declare const threadIdToResumeState: recoil.RecoilState<string | undefined>;
340
- declare const chatProfileState: recoil.RecoilState<string | undefined>;
341
- declare const sessionIdState: recoil.RecoilState<string>;
342
- declare const sessionState: recoil.RecoilState<ISession | undefined>;
343
- declare const actionState: recoil.RecoilState<IAction[]>;
344
- declare const messagesState: recoil.RecoilState<IStep[]>;
345
- declare const tokenCountState: recoil.RecoilState<number>;
346
- declare const loadingState: recoil.RecoilState<boolean>;
347
- declare const askUserState: recoil.RecoilState<IAsk | undefined>;
348
- declare const callFnState: recoil.RecoilState<ICallFn | undefined>;
349
- declare const chatSettingsInputsState: recoil.RecoilState<any>;
350
- declare const chatSettingsDefaultValueSelector: recoil.RecoilValueReadOnly<any>;
351
- declare const chatSettingsValueState: recoil.RecoilState<any>;
352
- declare const elementState: recoil.RecoilState<IMessageElement[]>;
353
- declare const avatarState: recoil.RecoilState<IAvatarElement[]>;
354
- declare const tasklistState: recoil.RecoilState<ITasklistElement[]>;
355
- declare const firstUserInteraction: recoil.RecoilState<string | undefined>;
356
- declare const accessTokenState: recoil.RecoilState<string | undefined>;
357
- declare const userState: recoil.RecoilState<IUser | null>;
358
- declare const threadHistoryState: recoil.RecoilState<ThreadHistory | undefined>;
359
- declare const sideViewState: recoil.RecoilState<IMessageElement | undefined>;
360
-
361
- declare const useChatSession: () => {
362
- connect: lodash.DebouncedFunc<({ client, userEnv, accessToken }: {
363
- client: ChainlitAPI;
364
- userEnv: Record<string, string>;
365
- accessToken?: string | undefined;
366
- }) => void>;
367
- disconnect: () => void;
368
- session: ISession | undefined;
369
- sessionId: string;
370
- chatProfile: string | undefined;
371
- idToResume: string | undefined;
372
- setChatProfile: recoil.SetterOrUpdater<string | undefined>;
373
- };
405
+ declare const defaultChainlitContext: undefined;
406
+ declare const ChainlitContext: react.Context<ChainlitAPI>;
374
407
 
375
408
  declare const nestMessages: (messages: IStep[]) => IStep[];
376
409
  declare const isLastMessage: (messages: IStep[], index: number) => boolean;
@@ -379,6 +412,6 @@ declare const addMessageToParent: (messages: IStep[], parentId: string, newMessa
379
412
  declare const hasMessageById: (messages: IStep[], messageId: string) => boolean;
380
413
  declare const updateMessageById: (messages: IStep[], messageId: string, updatedMessage: IStep) => IStep[];
381
414
  declare const deleteMessageById: (messages: IStep[], messageId: string) => IStep[];
382
- declare const updateMessageContentById: (messages: IStep[], messageId: number | string, updatedContent: string, isSequence: boolean) => IStep[];
415
+ declare const updateMessageContentById: (messages: IStep[], messageId: number | string, updatedContent: string, isSequence: boolean, isInput: boolean) => IStep[];
383
416
 
384
- export { APIBase, ActionSpec, AuthProvider, ChainlitAPI, ClientError, ElementType, FileSpec, GenerationMessageRole, IAction, IAsk, IAudioElement, IAvatarElement, IBaseGeneration, ICallFn, IChatGeneration, ICompletionGeneration, IElement, IElementSize, IFeedback, IFileElement, IFileRef, IFunction, IGeneration, IGenerationMessage, IImageElement, ILLMSettings, IMessageElement, IPageInfo, IPagination, IPdfElement, IPlotlyElement, ISession, IStep, ITasklistElement, ITextElement, IThread, IThreadFilters, IToken, ITool, IUser, IUserMetadata, IVideoElement, ThreadHistory, UserInput, accessTokenState, actionState, addMessage, addMessageToParent, askUserState, avatarState, callFnState, chatProfileState, chatSettingsDefaultValueSelector, chatSettingsInputsState, chatSettingsValueState, deleteMessageById, elementState, fetcher, firstUserInteraction, hasMessageById, isLastMessage, loadingState, messagesState, nestMessages, sessionIdState, sessionState, sideViewState, tasklistState, threadHistoryState, threadIdToResumeState, tokenCountState, updateMessageById, updateMessageContentById, useApi, useAuth, useChatData, useChatInteract, useChatMessages, useChatSession, userState };
417
+ export { APIBase, ActionSpec, AuthProvider, ChainlitAPI, ChainlitContext, ChatProfile, ClientError, ElementType, FileSpec, IAction, IAsk, IAudioConfig, IAudioElement, ICallFn, IChainlitConfig, IElement, IElementSize, IFeedback, IFileElement, IFileRef, IImageElement, IMessageElement, IPageInfo, IPagination, IPdfElement, IPlotlyElement, ISession, IStarter, IStep, ITasklistElement, ITextElement, IThread, IThreadFilters, IToken, IUser, IUserMetadata, IVideoElement, ThreadHistory, UserInput, accessTokenState, actionState, addMessage, addMessageToParent, askUserState, callFnState, chatProfileState, chatSettingsDefaultValueSelector, chatSettingsInputsState, chatSettingsValueState, configState, currentThreadIdState, defaultChainlitContext, deleteMessageById, elementState, fetcher, firstUserInteraction, hasMessageById, isLastMessage, loadingState, messagesState, nestMessages, sessionIdState, sessionState, sideViewState, tasklistState, threadHistoryState, threadIdToResumeState, tokenCountState, updateMessageById, updateMessageContentById, useApi, useAudio, useAuth, useChatData, useChatInteract, useChatMessages, useChatSession, useConfig, userState };