Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 | 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 16x 4x 4x 4x 4x 4x 4x 4x 1x 1x 3x 16x 16x 1x 16x 16x 16x 16x 16x 16x 16x 16x 16x 32x | import React, { useCallback, useEffect, useMemo } from "react";
import Animated, { useSharedValue, withTiming } from "react-native-reanimated";
import { useColorScheme, View } from "react-native";
import { makeStyles } from "@repo/ui/themes/makeStyles";
import {
darkTheme,
darkV2Theme,
lightTheme,
lightV2Theme,
ThemeContext,
} from "@repo/ui/themes/ThemeContext";
import { Composer } from "@/components/Composer";
import { Header } from "@/components/Header";
import { MessageList } from "@/components/MessageList";
import { Welcome } from "@/components/Welcome";
import {
ChatbotRuntimeProvider,
createChatbotRuntimeValue,
} from "@/contexts/ChatbotRuntimeContext";
import { ChatbotContextProvider } from "@/contexts/ChatContext";
import { useChatBox } from "@/hooks/useChatBox";
import { useChatScroll } from "@/hooks/useChatScroll";
import { useComposer } from "@/hooks/useComposer";
import { useConversationView } from "@/hooks/useConversationView";
import { isSendActionDisabled } from "@/utils/sendState";
import { getMeaningfulMessage } from "@/utils/suggestions";
import { SuggestionChip } from "@/types/chat";
import { ChatbotRemoteProps } from "@/types/chatbot";
/**
* The conversation itself. Split from the screen because every hook below
* reads the chat context, which the screen is the one mounting.
*/
const Conversation = () => {
const styles = useStyles();
const {
messages,
isStreaming,
isInitializing,
initError,
processingActionIds,
selectedImage,
setSelectedImage,
inputRef,
handlePickImage,
handleLaunchCamera,
onSend: sendMessage,
handleCancel,
onReset,
handleAction,
initThread,
userName,
} = useChatBox();
const { listRef, animatedChatViewStyle } = useChatScroll(messages);
const { text, onChangeText, onSend, sendArrowY, sendArrowOpacity } = useComposer({
inputRef,
hasImage: !!selectedImage,
send: sendMessage,
});
const {
visibleMessages,
isInitialising,
isShowingGreeting,
welcomeDescription,
activeSuggestions,
} = useConversationView({ messages, isInitializing });
const streamingProgress = useSharedValue(isStreaming ? 1 : 0);
useEffect(() => {
streamingProgress.value = withTiming(isStreaming ? 1 : 0, { duration: 250 });
}, [isStreaming, streamingProgress]);
useEffect(() => {
initThread();
}, [initThread]);
// A confirm/cancel chip resolves the pending action directly instead of
// being sent as a new message the agent would have to interpret again.
const onSelectSuggestion = useCallback(
(item: string | SuggestionChip) => {
const label = typeof item === "string" ? item : item.label;
const value = typeof item === "string" ? item : item.value;
const lastMessage = visibleMessages[visibleMessages.length - 1];
const pendingAction = lastMessage?.pendingAction;
const decision = label.toLowerCase().trim();
const isPendingDecision =
(decision === "confirm" || decision === "cancel") &&
pendingAction?.id &&
pendingAction?.status === "awaiting_confirmation";
if (isPendingDecision) {
handleAction(lastMessage._id as string, pendingAction.id, decision as "confirm" | "cancel");
return;
}
onSend(getMeaningfulMessage(value));
},
[visibleMessages, handleAction, onSend],
);
const isSendDisabled = isSendActionDisabled({
isStreaming,
isInitializing,
text,
hasImage: !!selectedImage,
});
return (
<View style={styles.conversation}>
<Animated.View style={[styles.chatView, animatedChatViewStyle]}>
{isShowingGreeting ? (
<Welcome
userName={userName}
description={welcomeDescription}
initError={initError}
isInitialising={isInitialising}
isInitializing={isInitializing}
onReset={onReset}
/>
) : (
<MessageList
listRef={listRef}
messages={visibleMessages}
processingActionIds={processingActionIds}
isThinking={messages.length > 0 && isInitializing}
onReset={onReset}
handleAction={handleAction}
/>
)}
</Animated.View>
<Composer
inputRef={inputRef}
text={text}
onChangeText={onChangeText}
onSend={onSend}
onCancel={handleCancel}
isStreaming={isStreaming}
isInitializing={isInitializing}
isSendDisabled={isSendDisabled}
imageUri={selectedImage?.uri || null}
onRemoveImage={() => setSelectedImage(null)}
onPickImage={handlePickImage}
onLaunchCamera={handleLaunchCamera}
suggestions={activeSuggestions}
onSelectSuggestion={onSelectSuggestion}
streamingProgress={streamingProgress}
sendArrowY={sendArrowY}
sendArrowOpacity={sendArrowOpacity}
/>
</View>
);
};
const Chatbot = (props: ChatbotRemoteProps) => {
const runtimeValue = useMemo(() => createChatbotRuntimeValue(props), [props]);
const styles = useStyles();
const isNewTheme = props.featureFlags?.isNewTheme ?? false;
const colorScheme = useColorScheme();
const resolvedMode: "light" | "dark" = colorScheme === "dark" ? "dark" : "light";
const theme = isNewTheme
? resolvedMode === "dark"
? darkV2Theme
: lightV2Theme
: resolvedMode === "dark"
? darkTheme
: lightTheme;
const themeContextValue = useMemo(
() => ({
theme,
mode: "system" as const,
resolvedMode,
isDark: resolvedMode === "dark",
isNewTheme,
}),
[theme, resolvedMode, isNewTheme],
);
return (
<ThemeContext.Provider value={themeContextValue}>
<ChatbotRuntimeProvider value={runtimeValue}>
<ChatbotContextProvider>
<View style={styles.container}>
<Header />
<Conversation />
</View>
</ChatbotContextProvider>
</ChatbotRuntimeProvider>
</ThemeContext.Provider>
);
};
const useStyles = makeStyles((theme) => ({
container: {
flex: 1,
backgroundColor: theme.colors.background.secondary,
},
conversation: { flex: 1, backgroundColor: theme.colors.white },
chatView: { flex: 1 },
}));
export default Chatbot;
|