@assistant-ui/react 0.0.19 → 0.0.21

Sign up to get free protection for your applications and to get access to all the features.
package/dist/index.d.mts CHANGED
@@ -2,10 +2,22 @@ import * as react from 'react';
2
2
  import { FC, ReactNode, PropsWithChildren, ComponentType } from 'react';
3
3
  import { TextareaAutosizeProps } from 'react-textarea-autosize';
4
4
  import { UseChatHelpers, UseAssistantHelpers } from 'ai/react';
5
+ import { Message, LanguageModel } from 'ai';
5
6
  import * as react_jsx_runtime from 'react/jsx-runtime';
6
- import { LanguageModel, Message } from 'ai';
7
7
  import { UseBoundStore, StoreApi } from 'zustand';
8
8
 
9
+ declare const useCopyMessage: ({ copiedDuration }: {
10
+ copiedDuration?: number | undefined;
11
+ }) => (() => void) | null;
12
+
13
+ declare const useReloadMessage: () => (() => void) | null;
14
+
15
+ declare const useBeginMessageEdit: () => (() => void) | null;
16
+
17
+ declare const useGoToNextBranch: () => (() => void) | null;
18
+
19
+ declare const useGoToPreviousBranch: () => (() => void) | null;
20
+
9
21
  declare const ThreadRoot: react.ForwardRefExoticComponent<Pick<Omit<react.DetailedHTMLProps<react.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
10
22
  ref?: ((instance: HTMLDivElement | null) => void) | react.RefObject<HTMLDivElement> | null | undefined;
11
23
  } & {
@@ -103,16 +115,23 @@ declare namespace index$4 {
103
115
  export { ComposerCancel as Cancel, ComposerIf as If, ComposerInput as Input, ComposerRoot as Root, ComposerSend as Send };
104
116
  }
105
117
 
106
- type BaseComposerState = Readonly<{
107
- value: string;
108
- setValue: (value: string) => void;
109
- }>;
110
- type MessageComposerState = BaseComposerState & Readonly<{
111
- isEditing: boolean;
112
- edit: () => void;
113
- send: () => void;
114
- cancel: () => boolean;
115
- }>;
118
+ declare const MessageRoot: react.ForwardRefExoticComponent<Pick<Omit<react.DetailedHTMLProps<react.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
119
+ ref?: ((instance: HTMLDivElement | null) => void) | react.RefObject<HTMLDivElement> | null | undefined;
120
+ } & {
121
+ asChild?: boolean;
122
+ }, "key" | keyof react.HTMLAttributes<HTMLDivElement> | "asChild"> & react.RefAttributes<HTMLDivElement>>;
123
+
124
+ type MessageIfFilters = {
125
+ user: boolean | undefined;
126
+ assistant: boolean | undefined;
127
+ hasBranches: boolean | undefined;
128
+ copied: boolean | undefined;
129
+ lastOrHover: boolean | undefined;
130
+ };
131
+ type MessageIfProps = RequireAtLeastOne<MessageIfFilters> & {
132
+ children: ReactNode;
133
+ };
134
+ declare const MessageIf: FC<MessageIfProps>;
116
135
 
117
136
  type TextContentPart = {
118
137
  type: "text";
@@ -154,30 +173,6 @@ type AppendMessage = {
154
173
  };
155
174
  type ThreadMessage = UserMessage | AssistantMessage;
156
175
 
157
- type MessageProviderProps = PropsWithChildren<{
158
- message: ThreadMessage;
159
- parentId: string | null;
160
- }>;
161
- declare const MessageProvider: FC<MessageProviderProps>;
162
-
163
- declare const MessageRoot: react.ForwardRefExoticComponent<Pick<Omit<react.DetailedHTMLProps<react.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
164
- ref?: ((instance: HTMLDivElement | null) => void) | react.RefObject<HTMLDivElement> | null | undefined;
165
- } & {
166
- asChild?: boolean;
167
- }, "key" | keyof react.HTMLAttributes<HTMLDivElement> | "asChild"> & react.RefAttributes<HTMLDivElement>>;
168
-
169
- type MessageIfFilters = {
170
- user: boolean | undefined;
171
- assistant: boolean | undefined;
172
- hasBranches: boolean | undefined;
173
- copied: boolean | undefined;
174
- lastOrHover: boolean | undefined;
175
- };
176
- type MessageIfProps = RequireAtLeastOne<MessageIfFilters> & {
177
- children: ReactNode;
178
- };
179
- declare const MessageIf: FC<MessageIfProps>;
180
-
181
176
  type MessageContentProps = {
182
177
  components?: {
183
178
  Text?: ComponentType<{
@@ -201,14 +196,14 @@ type MessageContentProps = {
201
196
  };
202
197
  declare const MessageContent: FC<MessageContentProps>;
203
198
 
204
- declare const MessageInProgress: react.ForwardRefExoticComponent<Pick<Omit<react.DetailedHTMLProps<react.HTMLAttributes<HTMLDivElement>, HTMLDivElement>, "ref"> & {
205
- ref?: ((instance: HTMLDivElement | null) => void) | react.RefObject<HTMLDivElement> | null | undefined;
199
+ declare const MessageInProgress: react.ForwardRefExoticComponent<Pick<Omit<react.DetailedHTMLProps<react.HTMLAttributes<HTMLSpanElement>, HTMLSpanElement>, "ref"> & {
200
+ ref?: ((instance: HTMLSpanElement | null) => void) | react.RefObject<HTMLSpanElement> | null | undefined;
206
201
  } & {
207
202
  asChild?: boolean;
208
- }, "key" | keyof react.HTMLAttributes<HTMLDivElement> | "asChild"> & react.RefAttributes<HTMLDivElement>>;
203
+ }, "key" | "asChild" | keyof react.HTMLAttributes<HTMLSpanElement>> & react.RefAttributes<HTMLSpanElement>>;
209
204
 
210
205
  declare namespace index$3 {
211
- export { MessageContent as Content, MessageIf as If, MessageInProgress as InProgress, MessageProvider as Provider, MessageRoot as Root };
206
+ export { MessageContent as Content, MessageIf as If, MessageInProgress as InProgress, MessageRoot as Root };
212
207
  }
213
208
 
214
209
  declare const BranchPickerNext: react.ForwardRefExoticComponent<Pick<Omit<react.DetailedHTMLProps<react.ButtonHTMLAttributes<HTMLButtonElement>, HTMLButtonElement>, "ref"> & {
@@ -274,64 +269,124 @@ declare namespace index$1 {
274
269
  export { ActionBarCopy as Copy, ActionBarEdit as Edit, ActionBarReload as Reload, ActionBarRoot as Root };
275
270
  }
276
271
 
277
- type ContentPartProviderProps = PropsWithChildren<{
278
- part: ThreadMessage["content"][number];
279
- status: "in_progress" | "done" | "error";
280
- }>;
281
- declare const ContentPartProvider: FC<ContentPartProviderProps>;
282
-
283
272
  declare const ContentPartInProgressIndicator: FC;
284
273
 
285
274
  declare namespace index {
286
- export { ContentPartInProgressIndicator as InProgressIndicator, ContentPartProvider as Provider };
275
+ export { ContentPartInProgressIndicator as InProgressIndicator };
287
276
  }
288
277
 
289
- type VercelAIAssistantProviderProps = PropsWithChildren<{
290
- chat: UseChatHelpers;
291
- } | {
292
- assistant: UseAssistantHelpers;
293
- }>;
294
- declare const VercelAIAssistantProvider: FC<VercelAIAssistantProviderProps>;
295
-
296
278
  type VercelRSCMessage = {
297
279
  id: string;
298
280
  role: "user" | "assistant";
299
281
  display: ReactNode;
300
282
  createdAt?: Date;
301
283
  };
302
- type VercelRSCAssistantProviderBaseProps<T> = PropsWithChildren<{
284
+
285
+ type RSCMessageConverter<T> = {
286
+ convertMessage: (message: T) => VercelRSCMessage;
287
+ };
288
+ type VercelRSCAdapterBase<T> = {
303
289
  messages: T[];
304
290
  append: (message: AppendMessage) => Promise<void>;
305
291
  edit?: (message: AppendMessage) => Promise<void>;
306
292
  reload?: (parentId: string | null) => Promise<void>;
307
293
  convertMessage?: (message: T) => VercelRSCMessage;
308
- }>;
309
- type RSCMessageConverter<T> = {
310
- convertMessage: (message: T) => VercelRSCMessage;
311
294
  };
312
- type VercelRSCAssistantProviderProps<T = VercelRSCMessage> = VercelRSCAssistantProviderBaseProps<T> & (T extends VercelRSCMessage ? object : RSCMessageConverter<T>);
313
- declare const VercelRSCAssistantProvider: <T extends WeakKey = VercelRSCMessage>({ children, convertMessage, messages: vercelMessages, append: appendCallback, edit, reload, }: VercelRSCAssistantProviderProps<T>) => react_jsx_runtime.JSX.Element;
295
+ type VercelRSCAdapter<T = VercelRSCMessage> = VercelRSCAdapterBase<T> & (T extends VercelRSCMessage ? object : RSCMessageConverter<T>);
314
296
 
315
- type MessageUpdateCallback = (parentId: string | null, message: ThreadMessage) => void;
316
- type StatusUpdateCallback = (isRunning: boolean) => void;
317
- type Unsubscribe = () => void;
318
- type ThreadRuntime = {
319
- append(message: AppendMessage): Promise<{
320
- parentId: string;
321
- id: string;
322
- }>;
323
- startRun(parentId: string | null): Promise<{
324
- id: string;
325
- }>;
326
- cancelRun(): void;
327
- subscribeToMessageUpdates(callback: MessageUpdateCallback): Unsubscribe;
328
- subscribeToStatusUpdates(callback: StatusUpdateCallback): Unsubscribe;
297
+ type ThreadState = {
298
+ messages: ThreadMessage[];
299
+ isRunning: boolean;
300
+ getBranches: (messageId: string) => readonly string[];
301
+ switchToBranch: (branchId: string) => void;
302
+ append: (message: AppendMessage) => void;
303
+ startRun: (parentId: string | null) => void;
304
+ cancelRun: () => void;
329
305
  };
330
306
 
331
- type AssistantProviderProps = {
332
- runtime: ThreadRuntime;
307
+ type Unsubscribe = () => void;
308
+ type ThreadRuntime = Readonly<ThreadState> & {
309
+ subscribe: (callback: () => void) => Unsubscribe;
310
+ };
311
+ type ReactThreadRuntime = Readonly<ThreadState> & {
312
+ unstable_synchronizer?: ComponentType;
333
313
  };
334
- declare const AssistantProvider: FC<PropsWithChildren<AssistantProviderProps>>;
314
+
315
+ type AssistantRuntime = ThreadRuntime;
316
+
317
+ declare class VercelRSCRuntime<T extends WeakKey = VercelRSCMessage> implements AssistantRuntime, ReactThreadRuntime {
318
+ adapter: VercelRSCAdapter<T>;
319
+ private useAdapter;
320
+ private _subscriptions;
321
+ isRunning: boolean;
322
+ messages: ThreadMessage[];
323
+ constructor(adapter: VercelRSCAdapter<T>);
324
+ private withRunning;
325
+ getBranches(): readonly string[];
326
+ switchToBranch(): void;
327
+ append(message: AppendMessage): Promise<void>;
328
+ startRun(parentId: string | null): Promise<void>;
329
+ cancelRun(): void;
330
+ subscribe(callback: () => void): Unsubscribe;
331
+ onAdapterUpdated(): void;
332
+ private updateData;
333
+ unstable_synchronizer: () => null;
334
+ }
335
+
336
+ declare const useVercelRSCRuntime: <T extends WeakKey>(adapter: VercelRSCAdapter<T>) => VercelRSCRuntime<T>;
337
+
338
+ declare const getVercelRSCMessage: <T>(message: ThreadMessage) => T | undefined;
339
+
340
+ type VercelHelpers = UseChatHelpers | UseAssistantHelpers;
341
+
342
+ declare class VercelAIRuntime implements AssistantRuntime, ReactThreadRuntime {
343
+ vercel: VercelHelpers;
344
+ private _subscriptions;
345
+ private repository;
346
+ private assistantOptimisticId;
347
+ private useVercel;
348
+ messages: ThreadMessage[];
349
+ isRunning: boolean;
350
+ constructor(vercel: VercelHelpers);
351
+ getBranches(messageId: string): string[];
352
+ switchToBranch(branchId: string): void;
353
+ append(message: AppendMessage): Promise<void>;
354
+ startRun(parentId: string | null): Promise<void>;
355
+ cancelRun(): void;
356
+ subscribe(callback: () => void): Unsubscribe;
357
+ private updateVercelMessages;
358
+ onVercelUpdated(): void;
359
+ private updateData;
360
+ unstable_synchronizer: () => null;
361
+ }
362
+
363
+ declare const useVercelUseChatRuntime: (chatHelpers: UseChatHelpers) => VercelAIRuntime;
364
+
365
+ declare const useVercelUseAssistantRuntime: (assistantHelpers: UseAssistantHelpers) => VercelAIRuntime;
366
+
367
+ declare const getVercelAIMessage: (message: ThreadMessage) => Message | undefined;
368
+
369
+ /**
370
+ * @deprecated Will be removed in 0.1.0.
371
+ */
372
+ type VercelAIAssistantProviderProps = PropsWithChildren<{
373
+ chat: UseChatHelpers;
374
+ } | {
375
+ assistant: UseAssistantHelpers;
376
+ }>;
377
+ /**
378
+ * @deprecated `const runtime = useVercelUseChatRuntime(chat)` and `<AssistantRuntimeProvider runtime={...} />`. Will be removed in 0.1.0.
379
+ */
380
+ declare const VercelAIAssistantProvider: FC<VercelAIAssistantProviderProps>;
381
+
382
+ /**
383
+ * @deprecated Will be removed in 0.1.0.
384
+ */
385
+ type VercelRSCAssistantProviderProps<T> = PropsWithChildren<VercelRSCAdapter<T>>;
386
+ /**
387
+ * @deprecated Replaced with `const runtime = useVercelRSCRuntime({ messages, append })` and `<AssistantRuntimeProvider runtime={runtime} />`. Will be removed in 0.1.0.
388
+ */
389
+ declare const VercelRSCAssistantProvider: <T extends WeakKey = VercelRSCMessage>({ children, ...adapter }: VercelRSCAssistantProviderProps<T>) => react_jsx_runtime.JSX.Element;
335
390
 
336
391
  type ChatModelRunResult = {
337
392
  content: AssistantContentPart[];
@@ -345,25 +400,21 @@ type ChatModelAdapter = {
345
400
  run: (options: ChatModelRunOptions) => Promise<ChatModelRunResult>;
346
401
  };
347
402
 
348
- declare class LocalRuntime implements ThreadRuntime {
403
+ declare class LocalRuntime implements AssistantRuntime {
349
404
  adapter: ChatModelAdapter;
350
- private _messageUpdateCallbacks;
351
- private _statusUpdateCallbacks;
405
+ private _subscriptions;
352
406
  private abortController;
353
407
  private repository;
408
+ get messages(): ThreadMessage[];
409
+ get isRunning(): boolean;
354
410
  constructor(adapter: ChatModelAdapter);
355
- append(message: AppendMessage): Promise<{
356
- parentId: string;
357
- id: string;
358
- }>;
359
- startRun(parentId: string | null): Promise<{
360
- id: string;
361
- }>;
362
- private addOrUpdateMessage;
363
- private run;
411
+ getBranches(messageId: string): string[];
412
+ switchToBranch(branchId: string): void;
413
+ append(message: AppendMessage): Promise<void>;
414
+ startRun(parentId: string | null): Promise<void>;
364
415
  cancelRun(): void;
365
- subscribeToMessageUpdates(callback: MessageUpdateCallback): Unsubscribe;
366
- subscribeToStatusUpdates(callback: StatusUpdateCallback): Unsubscribe;
416
+ private notifySubscribers;
417
+ subscribe(callback: () => void): Unsubscribe;
367
418
  }
368
419
 
369
420
  declare const useLocalRuntime: (adapter: ChatModelAdapter) => LocalRuntime;
@@ -376,8 +427,15 @@ declare class VercelModelAdapter implements ChatModelAdapter {
376
427
  }>;
377
428
  }
378
429
 
379
- declare const getVercelMessage: (message: ThreadMessage) => Message | undefined;
380
- declare const getVercelRSCMessage: <T>(message: ThreadMessage) => T | undefined;
430
+ type AssistantRuntimeProviderProps = {
431
+ runtime: AssistantRuntime;
432
+ };
433
+ declare const AssistantRuntimeProvider: react.NamedExoticComponent<PropsWithChildren<AssistantRuntimeProviderProps>>;
434
+
435
+ type ContentPartState = Readonly<{
436
+ status: "in_progress" | "done" | "error";
437
+ part: ThreadMessage["content"][number];
438
+ }>;
381
439
 
382
440
  type MessageState = Readonly<{
383
441
  message: Readonly<ThreadMessage>;
@@ -391,23 +449,53 @@ type MessageState = Readonly<{
391
449
  isHovering: boolean;
392
450
  setIsHovering: (value: boolean) => void;
393
451
  }>;
394
- type MessageStore = {
395
- useMessage: UseBoundStore<StoreApi<MessageState>>;
396
- useComposer: UseBoundStore<StoreApi<MessageComposerState>>;
397
- };
398
452
 
399
- declare const useMessageContext: () => MessageStore;
453
+ type BaseComposerState = Readonly<{
454
+ value: string;
455
+ setValue: (value: string) => void;
456
+ }>;
400
457
 
401
- declare const useCopyMessage: ({ copiedDuration }: {
402
- copiedDuration?: number | undefined;
403
- }) => (() => void) | null;
458
+ type EditComposerState = BaseComposerState & Readonly<{
459
+ isEditing: boolean;
460
+ edit: () => void;
461
+ send: () => void;
462
+ cancel: () => boolean;
463
+ }>;
404
464
 
405
- declare const useReloadMessage: () => (() => void) | null;
465
+ type ComposerState = BaseComposerState & Readonly<{
466
+ isEditing: true;
467
+ send: () => void;
468
+ cancel: () => boolean;
469
+ }>;
406
470
 
407
- declare const useBeginMessageEdit: () => (() => void) | null;
471
+ type ThreadViewportState = {
472
+ isAtBottom: boolean;
473
+ scrollToBottom: () => void;
474
+ onScrollToBottom: (callback: () => void) => () => void;
475
+ };
408
476
 
409
- declare const useGoToNextBranch: () => (() => void) | null;
477
+ type ThreadContextValue = {
478
+ useThread: UseBoundStore<StoreApi<ThreadState>>;
479
+ useComposer: UseBoundStore<StoreApi<ComposerState>>;
480
+ useViewport: UseBoundStore<StoreApi<ThreadViewportState>>;
481
+ };
482
+ declare const useThreadContext: () => ThreadContextValue;
410
483
 
411
- declare const useGoToPreviousBranch: () => (() => void) | null;
484
+ type ComposerContextValue = {
485
+ useComposer: UseBoundStore<StoreApi<EditComposerState | ComposerState>>;
486
+ type: "edit" | "new";
487
+ };
488
+ declare const useComposerContext: () => ComposerContextValue;
489
+
490
+ type MessageContextValue = {
491
+ useMessage: UseBoundStore<StoreApi<MessageState>>;
492
+ useComposer: UseBoundStore<StoreApi<EditComposerState>>;
493
+ };
494
+ declare const useMessageContext: () => MessageContextValue;
495
+
496
+ type ContentPartContextValue = {
497
+ useContentPart: UseBoundStore<StoreApi<ContentPartState>>;
498
+ };
499
+ declare const useContentPartContext: () => ContentPartContextValue;
412
500
 
413
- export { index$1 as ActionBarPrimitive, type AppendContentPart, type AppendMessage, index$2 as BranchPickerPrimitive, index$4 as ComposerPrimitive, index as ContentPartPrimitive, type ImageContentPart, index$3 as MessagePrimitive, type VercelRSCMessage as RSCMessage, type TextContentPart, index$5 as ThreadPrimitive, VercelAIAssistantProvider, type VercelAIAssistantProviderProps, VercelRSCAssistantProvider, type VercelRSCAssistantProviderProps, AssistantProvider as unstable_AssistantProvider, type ChatModelAdapter as unstable_ChatModelAdapter, type ChatModelRunOptions as unstable_ChatModelRunOptions, VercelModelAdapter as unstable_VercelModelAdapter, getVercelMessage as unstable_getVercelMessage, getVercelRSCMessage as unstable_getVercelRSCMessage, useLocalRuntime as unstable_useLocalRuntime, useMessageContext as unstable_useMessageContext, useBeginMessageEdit, useCopyMessage, useGoToNextBranch, useGoToPreviousBranch, useReloadMessage };
501
+ export { index$1 as ActionBarPrimitive, type AppendContentPart, type AppendMessage, type AssistantContentPart, type AssistantMessage, AssistantRuntimeProvider, index$2 as BranchPickerPrimitive, index$4 as ComposerPrimitive, index as ContentPartPrimitive, index$3 as MessagePrimitive, type VercelRSCMessage as RSCMessage, type TextContentPart, type ThreadMessage, index$5 as ThreadPrimitive, type UserContentPart, type UserMessage, VercelAIAssistantProvider, type VercelAIAssistantProviderProps, type VercelRSCAdapter, VercelRSCAssistantProvider, type VercelRSCAssistantProviderProps, type VercelRSCMessage, getVercelAIMessage, getVercelRSCMessage, type ChatModelAdapter as unstable_ChatModelAdapter, type ChatModelRunOptions as unstable_ChatModelRunOptions, type ComposerContextValue as unstable_ComposerContextValue, type ComposerState as unstable_ComposerState, type ContentPartContextValue as unstable_ContentPartContextValue, type ContentPartState as unstable_ContentPartState, type EditComposerState as unstable_EditComposerState, type ImageContentPart as unstable_ImageContentPart, type MessageContextValue as unstable_MessageContextValue, type MessageState as unstable_MessageState, type ThreadContextValue as unstable_ThreadContextValue, type ThreadState as unstable_ThreadState, type ThreadViewportState as unstable_ThreadViewportState, type ToolCallContentPart as unstable_ToolCallContentPart, type UIContentPart as unstable_UIContentPart, VercelModelAdapter as unstable_VercelModelAdapter, useComposerContext as unstable_useComposerContext, useContentPartContext as unstable_useContentPartContext, useLocalRuntime as unstable_useLocalRuntime, useMessageContext as unstable_useMessageContext, useThreadContext as unstable_useThreadContext, useBeginMessageEdit, useCopyMessage, useGoToNextBranch, useGoToPreviousBranch, useReloadMessage, useVercelRSCRuntime, useVercelUseAssistantRuntime, useVercelUseChatRuntime };