UNPKG

@xapp/chat-widget

Version:
260 lines (259 loc) 10.3 kB
import 'rollup-plugin-inject-process-env'; import { ChatUserInfo } from "@xapp/stentor-chat-widget"; import { Action } from "@reduxjs/toolkit"; import { Attachment } from "../xapp/Attachment"; import { ChatServerMessage } from "../xapp/ChatServerMessage"; import { VisualState } from "./ChatState"; export type CONNECTION_STATUS_TYPE = "online" | "pending" | "offline"; export type ACCOUNT_STATUS_TYPE = "offline" | "online"; export type ACTION_TYPE = "chat" | "session_id" | "visual_status" | "account_status" | "connection_update" | "department_update" | "visitor_update" | "agent_update" | "error" | "reset" | "sendGreeting" | "ws_button_display" | "ws_button_dismiss"; export interface ActionDetail { timestamp: number; } export interface ChatActionDetail<T> extends ActionDetail { readonly type: T; readonly user: ChatUserInfo; } export interface ChatAction extends Action<"chat"> { detail: ChatDetail; } export interface ChatSendGreetingDetail extends ChatActionDetail<"sendGreeting"> { detail: undefined; } export interface SendGreetingAction extends Action<"sendGreeting"> { detail: ChatSendGreetingDetail; } export interface ConnectionReceiveTokenAction extends Action<"receiveToken"> { readonly detail: ChatConnectionReceiveTokenDetail; } export interface ChatConnectionReceiveTokenDetail extends ChatActionDetail<"receiveToken"> { readonly token: string; readonly timestamp: number; } export interface ChatMemberJoinDetail extends ChatActionDetail<"chat.memberjoin"> { showDivider?: boolean; message?: string; /** When true, hides user name and avatar when avatarPosition is "bottom". Default is false. */ hideUserInfo?: boolean; } export interface ChatMemberLeaveDetail extends ChatActionDetail<"chat.memberleave"> { showDivider?: boolean; message?: string; } export interface ChatMemberPositionDetail extends ChatActionDetail<"chat.queue.position"> { queuePosition: number; } export interface ChatRequestRatingDetail extends ChatActionDetail<"chat.request.rating"> { } export interface ChatRatingDetail extends ChatActionDetail<"chat.rating"> { newRating: string; } export interface ChatFileDetail extends ChatActionDetail<"chat.file"> { readonly attachment: Attachment; } /** * Represents a single tool call with its input and output */ export interface ToolCallInfo { /** Technical name of the tool that was called */ name: string; /** Unique identifier for this tool call (handles concurrent same-name tool calls) */ toolCallId?: string; /** Human-readable tool name (e.g., "Search Knowledge Base") */ displayName?: string; /** Context-sensitive label for current state (e.g., "Searching for answers...") */ label?: string; /** If true, tool call is only shown in debug mode */ hidden?: boolean; /** Input parameters passed to the tool */ input?: unknown; /** Result returned by the tool */ result?: unknown; /** Error message if the tool failed */ error?: string; /** Timestamp when the tool started */ startTime: number; /** Timestamp when the tool completed */ endTime?: number; /** Whether the tool is currently executing */ isExecuting?: boolean; } export interface ChatMsgDetail extends ChatActionDetail<"chat.msg"> { msg: ChatServerMessage; /** * Indicates the message is currently being streamed (SSE streaming response). * When true, a streaming cursor indicator should be shown. */ isStreaming?: boolean; /** * Indicates the message was delivered via streaming. * Used to maintain styling (e.g., no tail, top avatar) after streaming completes. */ wasStreamed?: boolean; /** * Tool calls made during this message (debug mode only). * Shows what tools were called, their inputs, and outputs. */ toolCalls?: ToolCallInfo[]; } export interface ChatFailureMsgDetail extends ChatActionDetail<"chat.failureMsg"> { failureMsg: FailureMessageDetail; } export interface FailureMessageDetail { readonly retry: number; readonly delay: number; readonly text: string; } export interface ChatTypingDetail extends ChatActionDetail<"chat.typing"> { typing: boolean; } export interface ChatOfflineDetail extends ChatActionDetail<"chat.offline"> { } export interface ChatPrechatDetail extends ChatActionDetail<"chat.prechat"> { } export interface ChatSystemMessageDetail extends ChatActionDetail<"chat.systemmessage"> { id: string; message: string; dismissId?: string; } export interface ChatSystemMessageDismissDetail extends ChatActionDetail<"chat.systemmessagedismiss"> { id: string; } export interface ChatStreamStartDetail extends ChatActionDetail<"chat.stream.start"> { messageId: string; } export interface ChatStreamChunkDetail extends ChatActionDetail<"chat.stream.chunk"> { messageId: string; text: string; } export interface ChatStreamToolStartDetail extends ChatActionDetail<"chat.stream.tool_start"> { messageId: string; toolName: string; /** Unique identifier for this tool call (handles concurrent same-name tool calls) */ toolCallId?: string; /** Human-readable tool name */ displayName?: string; /** Active/in-progress label (e.g., "Searching for answers...") */ label?: string; /** If true, tool call is only shown in debug mode */ hidden?: boolean; toolInput?: unknown; } export interface ChatStreamToolEndDetail extends ChatActionDetail<"chat.stream.tool_end"> { messageId: string; toolName: string; /** Unique identifier for this tool call (handles concurrent same-name tool calls) */ toolCallId?: string; /** Human-readable tool name */ displayName?: string; /** Completion label (e.g., "Found 3 results") */ label?: string; /** If true, tool call is only shown in debug mode */ hidden?: boolean; toolResult?: unknown; toolError?: string; } export interface ChatStreamEndDetail extends ChatActionDetail<"chat.stream.end"> { messageId: string; msg: ChatServerMessage; toolCalls?: ToolCallInfo[]; } export interface ChatStreamAbortDetail extends ChatActionDetail<"chat.stream.abort"> { messageId: string; } export interface ChatStreamResumeDetail extends ChatActionDetail<"chat.stream.resume"> { messageId: string; } export interface ConnectionUpdateAction extends Action<"connection_update"> { detail: ConnectionUpdateDetail; } export interface ConnectionUpdateDetail extends ActionDetail { status: CONNECTION_STATUS_TYPE; } export interface AccountStatusAction extends Action<"account_status"> { detail: AccountStatusDetail; } export interface AccountStatusDetail extends ActionDetail { status: ACCOUNT_STATUS_TYPE; } export interface SessionIdAction extends Action<"session_id"> { detail: SessionIdDetail; } export interface SessionIdDetail extends ActionDetail { sessionId: string; } export interface VisualStatusAction extends Action<"visual_status"> { detail: VisualStatusDetail; } export interface VisualStatusDetail extends ActionDetail { status: VisualState; } export interface DepartmentUpdateAction extends Action<"department_update"> { detail: DepartmentUpdateDetail; } export interface DepartmentUpdateDetail extends ActionDetail { id: string; displayName: string; } export interface VisitorUpdateAction extends Action<"visitor_update"> { detail: VisitorUpdateDetail; } export interface VisitorUpdateDetail extends ActionDetail { displayName: string; typing: boolean; } export interface AgentUpdateAction extends Action<"agent_update"> { readonly detail: AgentUpdateDetail; } export interface AgentUpdateDetail extends ActionDetail { readonly nick: string; readonly displayName: string; readonly typing: boolean; readonly avatarPath?: string; readonly joined: boolean; } export interface SyntheticAction extends Action<"synthetic"> { detail: SyntheticMsgDetail | SyntheticFileDetail | SyntheticOptionDetail; } export interface SyntheticMsgDetail extends ActionDetail { readonly type: "write_msg"; readonly msg: ChatServerMessage; readonly user: ChatUserInfo; } export interface SyntheticOptionDetail extends ChatActionDetail<"visitor.send.option"> { msg: ChatServerMessage; } export interface SyntheticFileDetail extends ChatActionDetail<"visitor.send.file"> { attachment: Attachment; /** * Optional request attributes. */ readonly attributes?: Record<string, unknown>; } export interface ResetAction extends Action<"reset"> { } export interface ToggleDebugModeAction extends Action<"toggle_debug_mode"> { } export interface WsButtonDisplayAction extends Action<"ws_button_display"> { detail: WsButtonDisplayDetail; } export interface WsButtonDisplayDetail extends ActionDetail { id: string; text: string; pressBehavior: "disabled" | "spinning" | "none"; isPressed?: boolean; isDismissing?: boolean; } export interface WsButtonDismissAction extends Action<"ws_button_dismiss"> { detail: WsButtonDismissDetail; } export interface WsButtonDismissDetail extends ActionDetail { id: string; } export type ActionType = ChatAction | SessionIdAction | VisualStatusAction | ConnectionUpdateAction | AccountStatusAction | DepartmentUpdateAction | VisitorUpdateAction | AgentUpdateAction | SyntheticAction | ConnectionReceiveTokenAction | ResetAction | SendGreetingAction | ToggleDebugModeAction | WsButtonDisplayAction | WsButtonDismissAction; export type ChatDetail = ChatMemberJoinDetail | ChatMemberLeaveDetail | ChatMemberPositionDetail | ChatRequestRatingDetail | ChatRatingDetail | ChatFileDetail | ChatMsgDetail | ChatFailureMsgDetail | ChatTypingDetail | ChatOfflineDetail | ChatPrechatDetail | ChatSendGreetingDetail | ChatConnectionReceiveTokenDetail | ChatSystemMessageDetail | ChatSystemMessageDismissDetail | ChatStreamStartDetail | ChatStreamChunkDetail | ChatStreamToolStartDetail | ChatStreamToolEndDetail | ChatStreamEndDetail | ChatStreamAbortDetail | ChatStreamResumeDetail; /** * Type guard for ChatMsgDetail. * Use this to safely narrow ChatDetail to ChatMsgDetail. */ export declare function isChatMsgDetail(chat: ChatDetail): chat is ChatMsgDetail;