This repository was archived by the owner on Feb 28, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChat.tsx
More file actions
176 lines (157 loc) · 5.28 KB
/
Chat.tsx
File metadata and controls
176 lines (157 loc) · 5.28 KB
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
import React, { useCallback, useEffect, useState } from "react";
import { ChatForm } from "../ChatForm";
import { ChatContent } from "../ChatContent";
import { Flex, Button, Text, Card } from "@radix-ui/themes";
import {
useAppSelector,
useAppDispatch,
useSendChatRequest,
useAutoSend,
useGetCapsQuery,
useCapsForToolUse,
useAgentUsage,
} from "../../hooks";
import { type Config } from "../../features/Config/configSlice";
import {
enableSend,
selectIsStreaming,
selectIsWaiting,
selectPreventSend,
selectChatId,
selectMessages,
getSelectedToolUse,
selectThreadNewChatSuggested,
} from "../../features/Chat/Thread";
import { ThreadHistoryButton } from "../Buttons";
import { push } from "../../features/Pages/pagesSlice";
import { DropzoneProvider } from "../Dropzone";
import { AgentUsage } from "../../features/AgentUsage";
import { useCheckpoints } from "../../hooks/useCheckpoints";
import { Checkpoints } from "../../features/Checkpoints";
import { SuggestNewChat } from "../ChatForm/SuggestNewChat";
export type ChatProps = {
host: Config["host"];
tabbed: Config["tabbed"];
backFromChat: () => void;
style?: React.CSSProperties;
unCalledTools: boolean;
};
export const Chat: React.FC<ChatProps> = ({ style, unCalledTools }) => {
const dispatch = useAppDispatch();
const [isViewingRawJSON, setIsViewingRawJSON] = useState(false);
const isStreaming = useAppSelector(selectIsStreaming);
const isWaiting = useAppSelector(selectIsWaiting);
const caps = useGetCapsQuery();
const chatId = useAppSelector(selectChatId);
const { submit, abort, retryFromIndex } = useSendChatRequest();
const chatToolUse = useAppSelector(getSelectedToolUse);
const threadNewChatSuggested = useAppSelector(selectThreadNewChatSuggested);
const messages = useAppSelector(selectMessages);
const capsForToolUse = useCapsForToolUse();
const { disableInput } = useAgentUsage();
const { shouldCheckpointsPopupBeShown } = useCheckpoints();
const [isDebugChatHistoryVisible, setIsDebugChatHistoryVisible] =
useState(false);
const preventSend = useAppSelector(selectPreventSend);
const onEnableSend = () => dispatch(enableSend({ id: chatId }));
const handleSummit = useCallback(
(value: string) => {
submit({ question: value });
if (isViewingRawJSON) {
setIsViewingRawJSON(false);
}
},
[submit, isViewingRawJSON],
);
const focusTextarea = useCallback(() => {
const textarea = document.querySelector<HTMLTextAreaElement>(
'[data-testid="chat-form-textarea"]',
);
if (textarea) {
textarea.focus();
}
}, []);
const handleThreadHistoryPage = useCallback(() => {
dispatch(push({ name: "thread history page", chatId }));
}, [chatId, dispatch]);
useEffect(() => {
if (!isWaiting && !isStreaming) {
focusTextarea();
}
}, [isWaiting, isStreaming, focusTextarea]);
useAutoSend();
return (
<DropzoneProvider asChild>
<Flex
style={style}
direction="column"
flexGrow="1"
width="100%"
overflowY="auto"
justify="between"
px="1"
>
<ChatContent
key={`chat-content-${chatId}`}
onRetry={retryFromIndex}
onStopStreaming={abort}
/>
{shouldCheckpointsPopupBeShown && <Checkpoints />}
<AgentUsage />
<SuggestNewChat
shouldBeVisible={
threadNewChatSuggested.wasSuggested &&
!threadNewChatSuggested.wasRejectedByUser
}
/>
{!isStreaming && preventSend && unCalledTools && (
<Flex py="4">
<Card style={{ width: "100%" }}>
<Flex direction="column" align="center" gap="2" width="100%">
Chat was interrupted with uncalled tools calls.
<Button onClick={onEnableSend} disabled={disableInput}>
Resume
</Button>
</Flex>
</Card>
</Flex>
)}
<ChatForm
key={chatId} // TODO: think of how can we not trigger re-render on chatId change (checkboxes)
onSubmit={handleSummit}
unCalledTools={unCalledTools}
/>
<Flex justify="between" pl="1" pr="1" pt="1">
{/* Two flexboxes are left for the future UI element on the right side */}
{messages.length > 0 && (
<Flex align="center" justify="between" width="100%">
<Flex align="center" gap="1">
<Text size="1">
model:{" "}
{capsForToolUse.currentModel ||
caps.data?.code_chat_default_model}{" "}
</Text>{" "}
•{" "}
<Text
size="1"
onClick={() => setIsDebugChatHistoryVisible((prev) => !prev)}
>
mode: {chatToolUse}{" "}
</Text>
</Flex>
{messages.length !== 0 &&
!isStreaming &&
isDebugChatHistoryVisible && (
<ThreadHistoryButton
title="View history of current thread"
size="1"
onClick={handleThreadHistoryPage}
/>
)}
</Flex>
)}
</Flex>
</Flex>
</DropzoneProvider>
);
};