@plteam/chat-ui
Version:
CUI Kit is a free and open-source library for creating AI assistant chat interfaces, built with React, Material UI, and TypeScript
116 lines (115 loc) • 4.23 kB
TypeScript
import * as React from 'react';
import { Thread, Message, MessageModel, MessageUserContent } from '../../models';
import { IdType } from '../../types';
import { ThreadMessages } from '../../models/ThreadMessages';
import { ObservableReactValue } from '../../utils/observers';
import { ThreadListenersMap } from '../thread/ThreadListenersMap';
import { EventsEmitterMethods } from '../../models/EventsEmitter';
export type ApiRefType<DM extends Message = any, DD extends Thread<DM> = any> = {
/**
* Get all messages from current thread
*/
getAllMessages: () => DM[];
/**
* Get all threads
*/
getAllThreads: () => DD[];
/**
* Get current thread
*/
getCurrentThread: () => DD | undefined;
/**
* Get thread by id
*/
getThreadById: (id: IdType) => DD | undefined;
/**
* Represents an instance of an event emitter used to handle chat events
*/
emitter: EventsEmitterMethods;
/**
* Get messages from current branch
* @see https://docs.playliner.com/introduction/branching/
*/
getBranchMessages: () => DM[];
/**
* Delete thread by id
*/
deleteThread: (threadId: IdType) => void;
/**
* Rename thread by id
*/
renameThread: (threadId: IdType, title: string) => void;
/**
* Changes the value of renameItem; if there is a value, a rename dialog opens.
*/
setRenameItem: (v: DD | undefined) => void;
/**
* Triggered when thread is renamed.
*/
onThreadRenamed?: (v: {
thread: DD;
oldTitle: string;
newTitle: string;
}) => void;
/**
* Send message to conversation
*/
sendUserMessage: (content: Message['content']) => Promise<boolean>;
/**
* Triggered when another thread is opened.
*/
onChangeThread: (threadId: IdType) => void;
/**
* Triggered when a new thread is opened.
* @param thread
*/
openNewThread: (thread?: DD) => void;
/**
* Set the waiting status for a message. If there is no `messageId`, the change
* is applied to the assistant's last message in the current branch.
* `isTyping` toggles the typing state only when passed explicitly.
*/
setMessageStatus: (status: string, isTyping?: boolean, messageId?: IdType) => void;
/**
* Set text for a message. If there is no `messageId`, the change is applied to
* the last message in the current branch.
*/
setMessageText: (text: string, messageId?: IdType) => void;
/**
* Change thread branch.
*/
handleChangeBranch: ThreadMessages<DM>['handleChangeBranch'];
/**
* Changes the state of opening the drawer menu.
*/
setMenuDrawerOpen: (v: boolean) => void;
/**
* Changes the value of deleteItem; if there is a value, a confirm dialog opens.
*/
setDeleteItem: (v: DD | undefined) => void;
/**
* Set active tool. If there is no `threadId`, change the value of the current thread.
*/
setActiveTool: (v: string | undefined, threadId?: IdType) => void;
/**
* Create new thread.
*/
handleCreateNewThread?: () => DD;
/**
* Change current thread.
*/
onChangeCurrentThread?: (v: DD) => void;
/**
* Triggered when thread is deleted.
*/
onThreadDeleted?: (v: DD) => void;
};
export type PrivateApiRefType<DM extends Message = any, DD extends Thread<DM> = any> = {
allMessages: ObservableReactValue<Readonly<MessageModel<DM>[]>>;
branch: ObservableReactValue<Readonly<MessageModel<DM>[]>>;
getListener: <K extends keyof ThreadListenersMap<DM>>(key: K) => ThreadListenersMap<DM>[K] | undefined;
updateScrollButtonState: () => void;
onEditMessage: (newContent: MessageUserContent, messageEdit: MessageModel) => Promise<MessageModel | undefined>;
getConversationBlockHeight: () => number;
} & ApiRefType<DM, DD>;
export declare const useApiRef: <DM extends Message, DD extends Thread<DM>>(userApiRef: React.MutableRefObject<ApiRefType | null> | undefined) => React.MutableRefObject<PrivateApiRefType<DM, DD>>;