-
Notifications
You must be signed in to change notification settings - Fork 324
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: ai-bot integration poc (#2819)
* feat: add StreamingMessageView to kick off ai feature * fix: issues with message view * feat: add AITypingIndicatorView * feat: make send message button react to ai state * fix: improve typewriter animation * fix: improvements in ui and typewriter * chore: add customizations to StreamingMessageView * fix: hook deps * chore: extract logic in hook * fix: custom events * fix: revert the type change in favor of changes in the LLC * feat: codeblock scrollable view * feat: table initial reimpl * feat: finish table impl * fix: horizontal scroll list performance issues * feat: add markdown parsing fixes, optimistic code capture and various improvements * fix: theme prop and theming in general * fix: remove edited lalbel for ai messages * fix: bug with stop streaming button and types * fix: colors in md rendering * fix: rename custom scrollview * chore: translations * fix: remove TODO * chore: extract indicator styles in theme * fix: safeguard if channel does not exist * fix: get rid of enum and introduce proper type * fix: allow only message overrides * chore: update event names as per the changes * fix: bump stream-chat-js version to v8.46.0 * fix: use channel method for sending events * fix: cover background mode case * fix: use type from LLC * chore: add jsdocs * fix: move check to checker fn * fix: add overrides for StreamingMessageView * chore: add override for stop streaming button
- Loading branch information
1 parent
6d5a51b
commit 770bb11
Showing
38 changed files
with
608 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6878,6 +6878,21 @@ [email protected]: | |
jsonwebtoken "~9.0.0" | ||
ws "^7.5.10" | ||
|
||
[email protected]: | ||
version "8.46.0" | ||
resolved "https://registry.yarnpkg.com/stream-chat/-/stream-chat-8.46.0.tgz#416b325e05b144d0937a3527d1e622463113d605" | ||
integrity sha512-HQVCRVldrfQFAvsBOHiHR0TKYf+wpsg/cAzRojeZY+buy1vG6eoqk09h6Fl4k2eG3zFLoA0G9W6o7o45jyFE1g== | ||
dependencies: | ||
"@babel/runtime" "^7.16.3" | ||
"@types/jsonwebtoken" "~9.0.0" | ||
"@types/ws" "^7.4.0" | ||
axios "^1.6.0" | ||
base64-js "^1.5.1" | ||
form-data "^4.0.0" | ||
isomorphic-ws "^4.0.1" | ||
jsonwebtoken "~9.0.0" | ||
ws "^7.5.10" | ||
|
||
strict-uri-encode@^2.0.0: | ||
version "2.0.0" | ||
resolved "https://registry.yarnpkg.com/strict-uri-encode/-/strict-uri-encode-2.0.0.tgz#b9c7330c7042862f6b142dc274bbcc5866ce3546" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
package/src/components/AITypingIndicatorView/AITypingIndicatorView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import React from 'react'; | ||
|
||
import { StyleSheet, Text, View } from 'react-native'; | ||
|
||
import { Channel } from 'stream-chat'; | ||
|
||
import { AIStates, useAIState } from './hooks/useAIState'; | ||
|
||
import { useChannelContext, useTheme, useTranslationContext } from '../../contexts'; | ||
import type { DefaultStreamChatGenerics } from '../../types/types'; | ||
|
||
export type AITypingIndicatorViewProps< | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, | ||
> = { | ||
channel?: Channel<StreamChatGenerics>; | ||
}; | ||
|
||
export const AITypingIndicatorView = < | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, | ||
>({ | ||
channel: channelFromProps, | ||
}: AITypingIndicatorViewProps<StreamChatGenerics>) => { | ||
const { t } = useTranslationContext(); | ||
const { channel: channelFromContext } = useChannelContext<StreamChatGenerics>(); | ||
const channel = channelFromProps || channelFromContext; | ||
const { aiState } = useAIState(channel); | ||
const allowedStates = { | ||
[AIStates.Thinking]: t('Thinking...'), | ||
[AIStates.Generating]: t('Generating...'), | ||
}; | ||
|
||
const { | ||
theme: { | ||
aiTypingIndicatorView: { container, text }, | ||
colors: { black, grey_gainsboro }, | ||
}, | ||
} = useTheme(); | ||
|
||
return aiState in allowedStates ? ( | ||
<View style={[styles.container, { backgroundColor: grey_gainsboro }, container]}> | ||
<Text style={[{ color: black }, text]}>{allowedStates[aiState]}</Text> | ||
</View> | ||
) : null; | ||
}; | ||
|
||
AITypingIndicatorView.displayName = 'AITypingIndicatorView{messageSimple{content}}'; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { paddingHorizontal: 16, paddingVertical: 18 }, | ||
}); |
68 changes: 68 additions & 0 deletions
68
package/src/components/AITypingIndicatorView/hooks/useAIState.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { useEffect, useState } from 'react'; | ||
|
||
import { AIState, Channel, Event } from 'stream-chat'; | ||
|
||
import { useChatContext } from '../../../contexts'; | ||
import type { DefaultStreamChatGenerics } from '../../../types/types'; | ||
import { useIsOnline } from '../../Chat/hooks/useIsOnline'; | ||
|
||
export const AIStates = { | ||
Error: 'AI_STATE_ERROR', | ||
ExternalSources: 'AI_STATE_EXTERNAL_SOURCES', | ||
Generating: 'AI_STATE_GENERATING', | ||
Idle: 'AI_STATE_IDLE', | ||
Thinking: 'AI_STATE_THINKING', | ||
}; | ||
|
||
/** | ||
* A hook that returns the current state of the AI. | ||
* @param {Channel} channel - The channel for which we want to know the AI state. | ||
* @returns {{ aiState: AIState }} The current AI state for the given channel. | ||
*/ | ||
export const useAIState = < | ||
StreamChatGenerics extends DefaultStreamChatGenerics = DefaultStreamChatGenerics, | ||
>( | ||
channel?: Channel<StreamChatGenerics>, | ||
): { aiState: AIState } => { | ||
const { client } = useChatContext<StreamChatGenerics>(); | ||
const { isOnline } = useIsOnline<StreamChatGenerics>(client); | ||
|
||
const [aiState, setAiState] = useState<AIState>(AIStates.Idle); | ||
|
||
useEffect(() => { | ||
if (!isOnline) { | ||
setAiState(AIStates.Idle); | ||
} | ||
}, [isOnline]); | ||
|
||
useEffect(() => { | ||
if (!channel) { | ||
return; | ||
} | ||
|
||
const indicatorChangedListener = channel.on( | ||
'ai_indicator.update', | ||
(event: Event<StreamChatGenerics>) => { | ||
const { cid } = event; | ||
const state = event.ai_state as AIState; | ||
if (channel.cid === cid) { | ||
setAiState(state); | ||
} | ||
}, | ||
); | ||
|
||
const indicatorClearedListener = channel.on('ai_indicator.clear', (event) => { | ||
const { cid } = event; | ||
if (channel.cid === cid) { | ||
setAiState(AIStates.Idle); | ||
} | ||
}); | ||
|
||
return () => { | ||
indicatorChangedListener.unsubscribe(); | ||
indicatorClearedListener.unsubscribe(); | ||
}; | ||
}, [channel]); | ||
|
||
return { aiState }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './AITypingIndicatorView'; | ||
export * from './hooks/useAIState'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.