-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathPromptActions.jsx
More file actions
305 lines (283 loc) · 9.32 KB
/
PromptActions.jsx
File metadata and controls
305 lines (283 loc) · 9.32 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
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import { memo, useCallback, useEffect, useMemo } from "react";
import {
Space,
Typography,
Select,
Switch,
Segmented,
Tooltip,
message,
} from "antd";
import {
ConsoleSqlOutlined,
DatabaseOutlined,
MessageOutlined,
RetweetOutlined,
WalletOutlined,
} from "@ant-design/icons";
import PropTypes from "prop-types";
import Cookies from "js-cookie";
import { CHAT_INTENTS } from "./helper";
import CircularTokenDisplay from "./CircularTokenDisplay";
import { useTokenStore } from "../../store/token-store";
import { useSessionStore } from "../../store/session-store";
import { useProjectStore } from "../../store/project-store";
import { orgStore } from "../../store/org-store";
import { useAxiosPrivate } from "../../service/axios-service";
// Define hidden intents and a fixed order array
const HIDDEN_CHAT_INTENTS = ["AUTO", "NOTA", "INFO"];
const CHAT_INTENTS_ORDER = ["TRANSFORM", "SQL"];
const CHAT_INTENTS_ICONS = {
TRANSFORM: <RetweetOutlined rotate={90} />,
SQL: <ConsoleSqlOutlined />,
INFO: <MessageOutlined />,
};
const DEFAULT_CHAT_INTENT = "TRANSFORM";
const HIDE_EDITOR_SELECTOR = true;
const IS_MODELS_UNIFIED = true; // Use the same model for both Architect and Coder
const PromptActions = memo(function PromptActions({
useMonaco,
onUseMonacoSwitch,
chatIntents,
selectedChatIntent,
setSelectedChatIntent,
llmModels = [],
selectedLlmModel,
setSelectedLlmModel,
selectedCoderLlmModel,
setSelectedCoderLlmModel,
selectedChatId,
selectedChatIntentName,
isNewChat = false,
isOnboardingMode = false,
isTypingPrompt = false,
onBuyTokens,
}) {
// Get token balance from store
const { tokenBalance, isLoading: isTokenLoading } = useTokenStore();
const isCloud = useSessionStore((state) => state.sessionDetails?.is_cloud);
const currentSchema = useProjectStore((state) => state.currentSchema);
const setCurrentSchema = useProjectStore((state) => state.setCurrentSchema);
const schemaList = useProjectStore((state) => state.schemaList);
const projectId = useProjectStore((state) => state.projectId);
const { selectedOrgId } = orgStore();
const axios = useAxiosPrivate();
const schemaOptions = useMemo(
() => schemaList.map((s) => ({ label: s, value: s })),
[schemaList]
);
const handleSchemaChange = useCallback(
(value) => {
const csrfToken = Cookies.get("csrftoken");
axios({
url: `/api/v1/visitran/${
selectedOrgId || "default_org"
}/project/${projectId}/set_schema`,
method: "POST",
data: { schema_name: value },
headers: { "X-CSRFToken": csrfToken },
})
.then(() => {
setCurrentSchema(value);
message.success("Schema updated successfully");
})
.catch((error) => {
console.error(error);
message.error("Failed to update schema");
});
},
[axios, selectedOrgId, projectId, setCurrentSchema]
);
const llmOptions = useMemo(
() =>
llmModels.map((m) => ({
label: m.display_name,
value: m.model,
})),
[llmModels]
);
// If there's no selected LLM, pick the default
useEffect(() => {
if (selectedLlmModel || !llmModels.length) return;
const defaultModel = llmModels.find((m) => m.default);
if (defaultModel?.model) {
setSelectedLlmModel(defaultModel.model);
}
}, [llmModels, selectedLlmModel]);
// If there's no selected LLM, pick the default
useEffect(() => {
if (selectedCoderLlmModel || !llmModels.length) return;
const defaultModel = llmModels.find((m) => m.default);
if (defaultModel?.model) {
setSelectedCoderLlmModel(defaultModel.model);
}
}, [llmModels, selectedCoderLlmModel]);
// If there's no selected Chat Intent, pick the default
useEffect(() => {
if (selectedChatIntent || !chatIntents.length) return;
const defaultChatIntent = chatIntents.find(
(intent) => intent.name === DEFAULT_CHAT_INTENT
);
if (defaultChatIntent?.chat_intent_id) {
setSelectedChatIntent(defaultChatIntent.chat_intent_id);
}
}, [chatIntents, selectedChatIntent]);
// Filter out hidden intents, then sort by CHAT_INTENTS_ORDER
const filteredAndSortedIntents = useMemo(() => {
return chatIntents
.filter((intent) => !HIDDEN_CHAT_INTENTS.includes(intent.name))
.sort(
(a, b) =>
CHAT_INTENTS_ORDER.indexOf(a.name) -
CHAT_INTENTS_ORDER.indexOf(b.name)
);
}, [chatIntents]);
return (
<div className="chat-ai-prompt-actions-container">
<Space>
<Space size={0}>
<Typography.Text type="secondary" className="font-size-12">
{selectedChatIntentName === CHAT_INTENTS.TRANSFORM &&
!IS_MODELS_UNIFIED
? "Architect:"
: "Model:"}
</Typography.Text>
<Select
showSearch
size="small"
placeholder="LLM model"
optionFilterProp="label"
options={llmOptions}
value={selectedLlmModel}
onChange={setSelectedLlmModel}
variant="borderless"
dropdownClassName="small-font-dropdown"
className="chat-ai-prompt-actions-model-select"
/>
</Space>
{selectedChatIntentName === CHAT_INTENTS.TRANSFORM &&
!IS_MODELS_UNIFIED && (
<Space size={0}>
<Typography.Text type="secondary" className="font-size-12">
Coder:
</Typography.Text>
<Select
showSearch
size="small"
placeholder="LLM model"
optionFilterProp="label"
options={llmOptions}
value={selectedCoderLlmModel}
onChange={setSelectedCoderLlmModel}
variant="borderless"
dropdownClassName="small-font-dropdown"
className="chat-ai-prompt-actions-model-select"
/>
</Space>
)}
</Space>
<Space>
{/* Cloud: full credit display | OSS: link to billing page */}
{isCloud ? (
<CircularTokenDisplay
tokenData={tokenBalance}
onBuyTokens={onBuyTokens}
isLoading={isTokenLoading}
/>
) : (
<a
href="https://us.app.visitran.com/project/setting/subscriptions"
target="_blank"
rel="noopener noreferrer"
className="chat-ai-manage-credits-link"
>
<WalletOutlined />
<span>Manage Credits</span>
</a>
)}
{/* Schema selector */}
{schemaList.length > 0 && (
<div className="chat-ai-info-chip chat-ai-info-chip-clickable">
<DatabaseOutlined className="chat-ai-info-chip-icon" />
<Select
size="small"
variant="borderless"
showSearch
placeholder="Schema"
value={currentSchema || undefined}
onChange={handleSchemaChange}
options={schemaOptions}
popupMatchSelectWidth={false}
className="chat-ai-schema-select"
/>
</div>
)}
</Space>
<div>
<Tooltip
title={
selectedChatId &&
"Chat intent is locked for this conversation. Please start a new chat to use a different intent."
}
>
<Segmented
className="chat-ai-custom-segmented"
size="small"
shape="round"
value={selectedChatIntent}
onChange={setSelectedChatIntent}
disabled={selectedChatId}
options={filteredAndSortedIntents.map((intent) => ({
label: (
<Typography.Text className="chat-ai-prompt-actions-monaco-font-size-10">
{intent.display_name}
</Typography.Text>
),
value: intent.chat_intent_id,
icon: CHAT_INTENTS_ICONS[intent.name],
}))}
/>
</Tooltip>
{!HIDE_EDITOR_SELECTOR && (
<Space size={5}>
<Switch
size="small"
checked={useMonaco}
onChange={onUseMonacoSwitch}
disabled={isOnboardingMode && isTypingPrompt}
/>
<Typography.Text
className="chat-ai-prompt-actions-monaco-font-size-10"
type="secondary"
style={{
opacity: isOnboardingMode && isTypingPrompt ? 0.5 : 1,
}}
>
Use Editor
</Typography.Text>
</Space>
)}
</div>
</div>
);
});
PromptActions.propTypes = {
useMonaco: PropTypes.bool.isRequired,
onUseMonacoSwitch: PropTypes.func.isRequired,
chatIntents: PropTypes.array.isRequired,
selectedChatIntent: PropTypes.string,
setSelectedChatIntent: PropTypes.func.isRequired,
llmModels: PropTypes.array,
selectedLlmModel: PropTypes.string,
setSelectedLlmModel: PropTypes.func.isRequired,
selectedCoderLlmModel: PropTypes.string,
setSelectedCoderLlmModel: PropTypes.func.isRequired,
selectedChatId: PropTypes.string,
selectedChatIntentName: PropTypes.string,
isNewChat: PropTypes.bool,
isOnboardingMode: PropTypes.bool,
isTypingPrompt: PropTypes.bool,
onBuyTokens: PropTypes.func,
};
PromptActions.displayName = "PromptActions";
export { PromptActions };