@chainlit/react-client 0.0.1 → 0.0.4

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
@@ -41,7 +41,11 @@ This hook is responsible for managing the chat session's connection to the WebSo
41
41
  #### Example
42
42
 
43
43
  ```jsx
44
- import { useChatSession } from '@chainlit/react-client';
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);
45
49
 
46
50
  const ChatComponent = () => {
47
51
  const { connect, disconnect, chatProfile, setChatProfile } = useChatSession();
@@ -49,11 +53,11 @@ const ChatComponent = () => {
49
53
  // Connect to the WebSocket server
50
54
  useEffect(() => {
51
55
  connect({
52
- wsEndpoint: 'YOUR_WEBSOCKET_ENDPOINT', // Your Chainlit server url
56
+ client: apiClient,
53
57
  userEnv: {
54
58
  /* user environment variables */
55
59
  },
56
- accessToken: 'YOUR_ACCESS_TOKEN' // Optional Chainlit auth token
60
+ accessToken: 'Bearer YOUR_ACCESS_TOKEN' // Optional Chainlit auth token
57
61
  });
58
62
 
59
63
  return () => {
@@ -140,7 +144,7 @@ This hook provides methods to interact with the chat, such as sending messages,
140
144
  - `replyMessage`: Replies to a message.
141
145
  - `sendMessage`: Sends a message.
142
146
  - `stopTask`: Stops the current task.
143
- - `setIdToResume`: Sets the ID to resume a conversation.
147
+ - `setIdToResume`: Sets the ID to resume a thread.
144
148
  - `updateChatSettings`: Updates the chat settings.
145
149
 
146
150
  #### Example
package/dist/index.d.mts CHANGED
@@ -1,7 +1,7 @@
1
1
  import * as recoil from 'recoil';
2
- import * as lodash from 'lodash';
3
2
  import { Socket } from 'socket.io-client';
4
3
  export { Socket } from 'socket.io-client';
4
+ import * as lodash from 'lodash';
5
5
  import * as swr__internal from 'swr/_internal';
6
6
  import { SWRConfiguration } from 'swr';
7
7
 
@@ -23,57 +23,56 @@ type IElementSize = 'small' | 'medium' | 'large';
23
23
  interface TElement<T> {
24
24
  id: string;
25
25
  type: T;
26
- conversationId?: string;
27
- forIds?: string[];
26
+ threadId?: string;
27
+ forId: string;
28
+ mime?: string;
28
29
  url?: string;
30
+ chainlitKey?: string;
29
31
  }
30
32
  interface TMessageElement<T> extends TElement<T> {
31
33
  name: string;
32
34
  display: 'inline' | 'side' | 'page';
33
35
  }
34
36
  interface IImageElement extends TMessageElement<'image'> {
35
- content?: ArrayBuffer;
36
37
  size?: IElementSize;
37
38
  }
38
39
  interface IAvatarElement extends TElement<'avatar'> {
39
40
  name: string;
40
- content?: ArrayBuffer;
41
41
  }
42
42
  interface ITextElement extends TMessageElement<'text'> {
43
- content?: string;
44
43
  language?: string;
45
44
  }
46
45
  interface IPdfElement extends TMessageElement<'pdf'> {
47
- content?: string;
46
+ page?: number;
48
47
  }
49
48
  interface IAudioElement extends TMessageElement<'audio'> {
50
- content?: ArrayBuffer;
51
49
  }
52
50
  interface IVideoElement extends TMessageElement<'video'> {
53
- content?: ArrayBuffer;
54
51
  size?: IElementSize;
55
52
  }
56
53
  interface IFileElement extends TMessageElement<'file'> {
57
54
  type: 'file';
58
- mime?: string;
59
- content?: ArrayBuffer;
60
55
  }
61
56
  interface IPlotlyElement extends TMessageElement<'plotly'> {
62
- content?: string;
63
57
  }
64
58
  interface ITasklistElement extends TElement<'tasklist'> {
65
- content?: string;
66
59
  }
67
60
 
68
- interface IBaseTemplate {
69
- template?: string;
70
- formatted?: string;
71
- template_format: string;
61
+ interface IFeedback {
62
+ id?: string;
63
+ forId?: string;
64
+ comment?: string;
65
+ strategy: 'BINARY';
66
+ value: number;
72
67
  }
73
- type PromptMessageRole = 'system' | 'assistant' | 'user' | 'function';
68
+
69
+ type GenerationMessageRole = 'system' | 'assistant' | 'user' | 'function' | 'tool';
74
70
  type ILLMSettings = Record<string, string | string[] | number | boolean>;
75
- interface IPromptMessage extends IBaseTemplate {
76
- role: PromptMessageRole;
71
+ interface IGenerationMessage {
72
+ template?: string;
73
+ formatted?: string;
74
+ templateFormat: string;
75
+ role: GenerationMessageRole;
77
76
  name?: string;
78
77
  }
79
78
  interface IFunction {
@@ -91,38 +90,49 @@ interface ITool {
91
90
  type: string;
92
91
  function: IFunction;
93
92
  }
94
- interface IPrompt extends IBaseTemplate {
93
+ interface IBaseGeneration {
95
94
  provider: string;
96
95
  id?: string;
97
96
  inputs?: Record<string, string>;
98
97
  completion?: string;
99
98
  settings?: ILLMSettings;
100
99
  functions?: IFunction[];
101
- messages?: IPromptMessage[];
100
+ tokenCount?: number;
102
101
  }
103
- interface IMessage {
104
- author: string;
105
- authorIsUser?: boolean;
106
- content?: string;
107
- createdAt: number | string;
108
- disableHumanFeedback?: boolean;
109
- elements?: IFileElement[];
110
- humanFeedback?: number;
111
- humanFeedbackComment?: string;
102
+ interface ICompletionGeneration extends IBaseGeneration {
103
+ type: 'COMPLETION';
104
+ template?: string;
105
+ formatted?: string;
106
+ templateFormat: string;
107
+ }
108
+ interface IChatGeneration extends IBaseGeneration {
109
+ type: 'CHAT';
110
+ messages?: IGenerationMessage[];
111
+ }
112
+ type IGeneration = ICompletionGeneration | IChatGeneration;
113
+
114
+ type StepType = 'assistant_message' | 'user_message' | 'system_message' | 'run' | 'tool' | 'llm' | 'embedding' | 'retrieval' | 'rerank' | 'undefined';
115
+ interface IStep {
112
116
  id: string;
113
- indent?: number;
117
+ name: string;
118
+ type: StepType;
119
+ threadId?: string;
120
+ parentId?: string;
114
121
  isError?: boolean;
122
+ showInput?: boolean | string;
123
+ waitForAnswer?: boolean;
124
+ input?: string;
125
+ output: string;
126
+ createdAt: number | string;
127
+ start?: number | string;
128
+ end?: number | string;
129
+ disableFeedback?: boolean;
130
+ feedback?: IFeedback;
115
131
  language?: string;
116
- parentId?: string;
117
- prompt?: IPrompt;
118
132
  streaming?: boolean;
119
- waitForAnswer?: boolean;
120
- subMessages?: IMessage[];
121
- }
122
- interface IMessageContent {
123
- elements: IMessageElement[];
124
- message: IMessage;
125
- preserveSize?: boolean;
133
+ generation?: IGeneration;
134
+ steps?: IStep[];
135
+ indent?: number;
126
136
  }
127
137
 
128
138
  interface FileSpec {
@@ -133,57 +143,51 @@ interface FileSpec {
133
143
  interface ActionSpec {
134
144
  keys?: string[];
135
145
  }
136
- interface IFileResponse {
137
- name: string;
138
- path?: string;
139
- size: number;
140
- type: string;
141
- content: ArrayBuffer;
146
+ interface IFileRef {
147
+ id: string;
142
148
  }
143
149
  interface IAsk {
144
- callback: (payload: IMessage | IFileResponse[] | IAction) => void;
150
+ callback: (payload: IStep | IFileRef[] | IAction) => void;
145
151
  spec: {
146
152
  type: 'text' | 'file' | 'action';
147
153
  timeout: number;
148
154
  } & FileSpec & ActionSpec;
149
155
  }
150
156
 
151
- type Role = 'USER' | 'ADMIN' | 'OWNER' | 'ANONYMOUS';
152
- type AppUserProvider = 'credentials' | 'header' | 'github' | 'google' | 'azure-ad';
153
- interface IAppUser {
154
- id: string;
155
- username: string;
156
- role: Role;
157
+ type AuthProvider = 'credentials' | 'header' | 'github' | 'google' | 'azure-ad';
158
+ interface IUserMetadata extends Record<string, any> {
157
159
  tags?: string[];
158
160
  image?: string;
159
- provider?: AppUserProvider;
161
+ provider?: AuthProvider;
162
+ }
163
+ interface IUser {
164
+ id: string;
165
+ identifier: string;
166
+ metadata: IUserMetadata;
160
167
  }
161
168
 
162
- interface IConversation {
169
+ interface IThread {
163
170
  id: string;
164
171
  createdAt: number | string;
165
- appUser?: IAppUser;
172
+ user?: IUser;
166
173
  metadata?: Record<string, any>;
167
- messages: IMessage[];
168
- elements: IElement[];
174
+ steps: IStep[];
175
+ elements?: IElement[];
169
176
  }
170
177
 
171
- type MessageHistory = {
178
+ type UserInput = {
172
179
  content: string;
173
180
  createdAt: number;
174
181
  };
175
- type ConversationsHistory = {
176
- conversations?: IConversation[];
177
- currentConversationId?: string;
178
- groupedConversations?: {
179
- [key: string]: IConversation[];
182
+ type ThreadHistory = {
183
+ threads?: IThread[];
184
+ currentThreadId?: string;
185
+ timeGroupedThreads?: {
186
+ [key: string]: IThread[];
180
187
  };
181
188
  pageInfo?: IPageInfo;
182
189
  };
183
190
 
184
- interface IMessageUpdate extends IMessage {
185
- newId?: string;
186
- }
187
191
  interface IToken {
188
192
  id: number | string;
189
193
  token: string;
@@ -204,55 +208,14 @@ declare const useChatData: () => {
204
208
  tasklists: ITasklistElement[];
205
209
  };
206
210
 
207
- declare const useChatInteract: () => {
208
- callAction: (action: IAction) => void;
209
- clear: () => void;
210
- replyMessage: (message: IMessage) => void;
211
- sendMessage: (message: IMessage, files?: IFileElement[]) => void;
212
- stopTask: () => void;
213
- setIdToResume: recoil.SetterOrUpdater<string | undefined>;
214
- updateChatSettings: (values: object) => void;
215
- };
216
-
217
- declare const useChatMessages: () => {
218
- messages: IMessage[];
219
- firstUserMessage: IMessage | undefined;
220
- };
221
-
222
- declare const useChatSession: () => {
223
- connect: lodash.DebouncedFunc<({ wsEndpoint, userEnv, accessToken }: {
224
- wsEndpoint: string;
225
- userEnv: Record<string, string>;
226
- accessToken?: string | undefined;
227
- }) => void>;
228
- disconnect: () => void;
229
- chatProfile: string | undefined;
230
- setChatProfile: recoil.SetterOrUpdater<string | undefined>;
231
- };
232
-
233
211
  declare const useAuth: (apiClient: ChainlitAPI) => {
234
- data: {
235
- requireLogin: boolean;
236
- passwordAuth: boolean;
237
- headerAuth: boolean;
238
- oauthProviders: string[];
239
- };
240
- user: null;
241
- role: string;
242
- isReady: boolean;
243
- isAuthenticated: boolean;
244
- accessToken: string;
245
- logout: () => void;
246
- setAccessToken: () => void;
247
- } | {
248
212
  data: {
249
213
  requireLogin: boolean;
250
214
  passwordAuth: boolean;
251
215
  headerAuth: boolean;
252
216
  oauthProviders: string[];
253
217
  } | undefined;
254
- user: IAppUser | null;
255
- role: Role | undefined;
218
+ user: IUser | null;
256
219
  isAuthenticated: boolean;
257
220
  isReady: boolean;
258
221
  accessToken: string | undefined;
@@ -263,8 +226,7 @@ declare const useAuth: (apiClient: ChainlitAPI) => {
263
226
  declare const fetcher: (client: ChainlitAPI, endpoint: string, token?: string) => Promise<any>;
264
227
  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>;
265
228
 
266
- interface IConversationsFilters {
267
- authorEmail?: string;
229
+ interface IThreadFilters {
268
230
  search?: string;
269
231
  feedback?: number;
270
232
  }
@@ -299,27 +261,62 @@ declare class APIBase {
299
261
  declare class ChainlitAPI extends APIBase {
300
262
  headerAuth(): Promise<any>;
301
263
  passwordAuth(data: FormData): Promise<any>;
302
- getCompletion(prompt: IPrompt, userEnv: {} | undefined, controller: AbortController, accessToken?: string, tokenCb?: (done: boolean, token: string) => void): Promise<ReadableStream<any>>;
303
- setHumanFeedback(messageId: string, feedback: number, feedbackComment?: string, accessToken?: string): Promise<void>;
304
- getConversations(pagination: IPagination, filter: IConversationsFilters, accessToken?: string): Promise<{
264
+ getGeneration(generation: IGeneration, userEnv: {} | undefined, controller: AbortController, accessToken?: string, tokenCb?: (done: boolean, token: string) => void): Promise<ReadableStream<any>>;
265
+ setFeedback(feedback: IFeedback, accessToken?: string): Promise<{
266
+ success: boolean;
267
+ feedbackId: string;
268
+ }>;
269
+ listThreads(pagination: IPagination, filter: IThreadFilters, accessToken?: string): Promise<{
305
270
  pageInfo: IPageInfo;
306
- data: IConversation[];
271
+ data: IThread[];
307
272
  }>;
308
- deleteConversation(conversationId: string, accessToken?: string): Promise<any>;
273
+ deleteThread(threadId: string, accessToken?: string): Promise<any>;
274
+ uploadFile(file: File, onProgress: (progress: number) => void, sessionId: string, token?: string): {
275
+ xhr: XMLHttpRequest;
276
+ promise: Promise<{
277
+ id: string;
278
+ }>;
279
+ };
280
+ getElementUrl(id: string, sessionId: string, accessToken?: string): string;
309
281
  getLogoEndpoint(theme: string): string;
310
282
  getOAuthEndpoint(provider: string): string;
311
283
  }
312
284
 
285
+ declare const useChatInteract: () => {
286
+ uploadFile: (client: ChainlitAPI, file: File, onProgress: (progress: number) => void) => {
287
+ xhr: XMLHttpRequest;
288
+ promise: Promise<{
289
+ id: string;
290
+ }>;
291
+ };
292
+ callAction: (action: IAction) => Promise<{
293
+ id: string;
294
+ status: boolean;
295
+ response?: string | undefined;
296
+ }> | undefined;
297
+ clear: () => void;
298
+ replyMessage: (message: IStep) => void;
299
+ sendMessage: (message: IStep, fileReferences?: IFileRef[]) => void;
300
+ stopTask: () => void;
301
+ setIdToResume: recoil.SetterOrUpdater<string | undefined>;
302
+ updateChatSettings: (values: object) => void;
303
+ };
304
+
305
+ declare const useChatMessages: () => {
306
+ messages: IStep[];
307
+ firstInteraction: string | undefined;
308
+ };
309
+
313
310
  interface ISession {
314
311
  socket: Socket;
315
312
  error?: boolean;
316
313
  }
317
- declare const conversationIdToResumeState: recoil.RecoilState<string | undefined>;
314
+ declare const threadIdToResumeState: recoil.RecoilState<string | undefined>;
318
315
  declare const chatProfileState: recoil.RecoilState<string | undefined>;
319
316
  declare const sessionIdState: recoil.RecoilState<string>;
320
317
  declare const sessionState: recoil.RecoilState<ISession | undefined>;
321
318
  declare const actionState: recoil.RecoilState<IAction[]>;
322
- declare const messagesState: recoil.RecoilState<IMessage[]>;
319
+ declare const messagesState: recoil.RecoilState<IStep[]>;
323
320
  declare const tokenCountState: recoil.RecoilState<number>;
324
321
  declare const loadingState: recoil.RecoilState<boolean>;
325
322
  declare const askUserState: recoil.RecoilState<IAsk | undefined>;
@@ -329,19 +326,31 @@ declare const chatSettingsValueState: recoil.RecoilState<any>;
329
326
  declare const elementState: recoil.RecoilState<IMessageElement[]>;
330
327
  declare const avatarState: recoil.RecoilState<IAvatarElement[]>;
331
328
  declare const tasklistState: recoil.RecoilState<ITasklistElement[]>;
332
- declare const firstUserMessageState: recoil.RecoilState<IMessage | undefined>;
329
+ declare const firstUserInteraction: recoil.RecoilState<string | undefined>;
333
330
  declare const accessTokenState: recoil.RecoilState<string | undefined>;
334
- declare const roleState: recoil.RecoilState<Role>;
335
- declare const userState: recoil.RecoilState<IAppUser | null>;
336
- declare const conversationsHistoryState: recoil.RecoilState<ConversationsHistory | undefined>;
331
+ declare const userState: recoil.RecoilState<IUser | null>;
332
+ declare const threadHistoryState: recoil.RecoilState<ThreadHistory | undefined>;
333
+
334
+ declare const useChatSession: () => {
335
+ connect: lodash.DebouncedFunc<({ client, userEnv, accessToken }: {
336
+ client: ChainlitAPI;
337
+ userEnv: Record<string, string>;
338
+ accessToken?: string | undefined;
339
+ }) => void>;
340
+ disconnect: () => void;
341
+ session: ISession | undefined;
342
+ chatProfile: string | undefined;
343
+ idToResume: string | undefined;
344
+ setChatProfile: recoil.SetterOrUpdater<string | undefined>;
345
+ };
337
346
 
338
- declare const nestMessages: (messages: IMessage[]) => IMessage[];
339
- declare const isLastMessage: (messages: IMessage[], index: number) => boolean;
340
- declare const addMessage: (messages: IMessage[], message: IMessage) => IMessage[];
341
- declare const addMessageToParent: (messages: IMessage[], parentId: string, newMessage: IMessage) => IMessage[];
342
- declare const hasMessageById: (messages: IMessage[], messageId: string) => boolean;
343
- declare const updateMessageById: (messages: IMessage[], messageId: string, updatedMessage: IMessage) => IMessage[];
344
- declare const deleteMessageById: (messages: IMessage[], messageId: string) => IMessage[];
345
- declare const updateMessageContentById: (messages: IMessage[], messageId: number | string, updatedContent: string, isSequence: boolean) => IMessage[];
347
+ declare const nestMessages: (messages: IStep[]) => IStep[];
348
+ declare const isLastMessage: (messages: IStep[], index: number) => boolean;
349
+ declare const addMessage: (messages: IStep[], message: IStep) => IStep[];
350
+ declare const addMessageToParent: (messages: IStep[], parentId: string, newMessage: IStep) => IStep[];
351
+ declare const hasMessageById: (messages: IStep[], messageId: string) => boolean;
352
+ declare const updateMessageById: (messages: IStep[], messageId: string, updatedMessage: IStep) => IStep[];
353
+ declare const deleteMessageById: (messages: IStep[], messageId: string) => IStep[];
354
+ declare const updateMessageContentById: (messages: IStep[], messageId: number | string, updatedContent: string, isSequence: boolean) => IStep[];
346
355
 
347
- export { APIBase, ActionSpec, AppUserProvider, ChainlitAPI, ClientError, ConversationsHistory, ElementType, FileSpec, IAction, IAppUser, IAsk, IAudioElement, IAvatarElement, IConversation, IConversationsFilters, IElement, IElementSize, IFileElement, IFileResponse, IFunction, IImageElement, ILLMSettings, IMessage, IMessageContent, IMessageElement, IMessageUpdate, IPageInfo, IPagination, IPdfElement, IPlotlyElement, IPrompt, IPromptMessage, ISession, ITasklistElement, ITextElement, IToken, ITool, IVideoElement, MessageHistory, PromptMessageRole, Role, accessTokenState, actionState, addMessage, addMessageToParent, askUserState, avatarState, chatProfileState, chatSettingsDefaultValueSelector, chatSettingsInputsState, chatSettingsValueState, conversationIdToResumeState, conversationsHistoryState, deleteMessageById, elementState, fetcher, firstUserMessageState, hasMessageById, isLastMessage, loadingState, messagesState, nestMessages, roleState, sessionIdState, sessionState, tasklistState, tokenCountState, updateMessageById, updateMessageContentById, useApi, useAuth, useChatData, useChatInteract, useChatMessages, useChatSession, userState };
356
+ export { APIBase, ActionSpec, AuthProvider, ChainlitAPI, ClientError, ElementType, FileSpec, GenerationMessageRole, IAction, IAsk, IAudioElement, IAvatarElement, IBaseGeneration, 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, chatProfileState, chatSettingsDefaultValueSelector, chatSettingsInputsState, chatSettingsValueState, deleteMessageById, elementState, fetcher, firstUserInteraction, hasMessageById, isLastMessage, loadingState, messagesState, nestMessages, sessionIdState, sessionState, tasklistState, threadHistoryState, threadIdToResumeState, tokenCountState, updateMessageById, updateMessageContentById, useApi, useAuth, useChatData, useChatInteract, useChatMessages, useChatSession, userState };