From 7c8952ac0f238151f5c15e31391210f50600d022 Mon Sep 17 00:00:00 2001 From: kanat Date: Wed, 27 Nov 2024 12:56:16 -0500 Subject: [PATCH 1/5] [CHA-334] Add AI related user events --- src/channel.ts | 38 ++++++++++++++++++++++++++++++++++++++ src/events.ts | 4 ++++ src/types.ts | 7 ++++++- 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/channel.ts b/src/channel.ts index bded4c430..4eb7a15e6 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 042c3e81c..e0d3d7214 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?: boolean; + 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; @@ -2145,9 +2148,9 @@ export type Attachment< footer_icon?: string; giphy?: GiphyData; image_url?: string; - mime_type?: string; latitude?: number; longitude?: number; + mime_type?: string; og_scrape_url?: string; original_height?: number; original_width?: number; @@ -3543,3 +3546,5 @@ 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; From db9fad7a5ae440a591816178d9bb8415229b21bc Mon Sep 17 00:00:00 2001 From: Kanat Kiialbaev Date: Wed, 27 Nov 2024 13:56:18 -0500 Subject: [PATCH 2/5] Apply suggestions from code review Co-authored-by: Anton Arnautov <43254280+arnautov-anton@users.noreply.github.com> --- src/channel.ts | 4 ++-- src/types.ts | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/channel.ts b/src/channel.ts index 4eb7a15e6..fb3b13147 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -796,12 +796,12 @@ export class Channel); } diff --git a/src/types.ts b/src/types.ts index e0d3d7214..8667f6cca 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3547,4 +3547,4 @@ export type GetUserModerationReportOptions = { include_user_mutes?: boolean; }; -export type AIState = 'AI_STATE_ERROR|AI_STATE_CHECKING_SOURCES|AI_STATE_THINKING|AI_STATE_GENERATING' | string; +export type AIState = 'AI_STATE_ERROR' | 'AI_STATE_CHECKING_SOURCES' | 'AI_STATE_THINKING' | 'AI_STATE_GENERATING' | (string & {}); From 19bccc9be127fc417acb6f6d5959657d90e94206 Mon Sep 17 00:00:00 2001 From: kanat Date: Wed, 27 Nov 2024 14:17:18 -0500 Subject: [PATCH 3/5] fix updateAIState signature --- src/channel.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/channel.ts b/src/channel.ts index fb3b13147..1267873db 100644 --- a/src/channel.ts +++ b/src/channel.ts @@ -796,7 +796,7 @@ export class Channel Date: Wed, 27 Nov 2024 14:56:24 -0500 Subject: [PATCH 4/5] lint-fix --- src/types.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 8667f6cca..531225494 100644 --- a/src/types.ts +++ b/src/types.ts @@ -3547,4 +3547,9 @@ export type GetUserModerationReportOptions = { include_user_mutes?: boolean; }; -export type AIState = 'AI_STATE_ERROR' | 'AI_STATE_CHECKING_SOURCES' | 'AI_STATE_THINKING' | 'AI_STATE_GENERATING' | (string & {}); +export type AIState = + | 'AI_STATE_ERROR' + | 'AI_STATE_CHECKING_SOURCES' + | 'AI_STATE_THINKING' + | 'AI_STATE_GENERATING' + | (string & {}); From a0e5c39f11bbcadb9aa9fd878cd2d173c24e0d9a Mon Sep 17 00:00:00 2001 From: Kanat Kiialbaev Date: Thu, 28 Nov 2024 14:35:01 -0500 Subject: [PATCH 5/5] Update src/types.ts Co-authored-by: Sahil Kumar --- src/types.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/types.ts b/src/types.ts index 531225494..8a7c01e8c 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1252,7 +1252,7 @@ export type ConnectionChangeEvent = { export type Event = StreamChatGenerics['eventType'] & { type: EventTypes; - ai_message?: boolean; + ai_message?: string; ai_state?: AIState; channel?: ChannelResponse; channel_id?: string;