diff --git a/src/channel.ts b/src/channel.ts index 11a0d9f5e..91dd819bd 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -59,6 +59,7 @@ import { SendMessageOptions, AscDesc, PartialUpdateMemberAPIResponse, + AIState, } from './types'; import { Role } from './permissions'; import { DEFAULT_QUERY_CHANNEL_MESSAGE_LIST_PAGE_SIZE } from './constants'; @@ -787,6 +788,43 @@ export class Channel); + } + + /** + * Sends an event to notify watchers to clear the typing/thinking UI when the AI response starts streaming. + * Typically used by the server connected to the AI service to inform clients that the AI response has started. + */ + async clearAIIndicator() { + await this.sendEvent({ + type: 'ai_indicator.clear', + } as Event); + } + + /** + * Sends an event to stop AI response generation, leaving the message in its current state. + * Triggered by the user to halt the AI response process. + */ + async stopAIResponse() { + await this.sendEvent({ + type: 'ai_indicator.stop', + } as Event); + } + /** * stopTyping - Sets last typing to null and sends the typing.stop event * @see {@link https://getstream.io/chat/docs/typing_indicators/?language=js|Docs} diff --git a/src/events.ts b/src/events.ts index 2bb1f5482..e145074a3 100644 --- a/src/events.ts +++ b/src/events.ts @@ -48,6 +48,10 @@ export const EVENT_MAP = { 'user.updated': true, 'user.watching.start': true, 'user.watching.stop': true, + // AI events + 'ai_indicator.update': true, + 'ai_indicator.stop': true, + 'ai_indicator.clear': true, // local events 'channels.queried': true, diff --git a/src/types.ts b/src/types.ts index 87de0e04c..8a7c01e8c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1252,6 +1252,8 @@ export type ConnectionChangeEvent = { export type Event = StreamChatGenerics['eventType'] & { type: EventTypes; + ai_message?: string; + ai_state?: AIState; channel?: ChannelResponse; channel_id?: string; channel_type?: string; @@ -1270,6 +1272,7 @@ export type Event; member?: ChannelMemberResponse; message?: MessageResponse; + message_id?: string; mode?: string; online?: boolean; parent_id?: string; @@ -3543,3 +3546,10 @@ export type GetUserModerationReportOptions = { include_user_blocks?: boolean; include_user_mutes?: boolean; }; + +export type AIState = + | 'AI_STATE_ERROR' + | 'AI_STATE_CHECKING_SOURCES' + | 'AI_STATE_THINKING' + | 'AI_STATE_GENERATING' + | (string & {});