Skip to content

Commit 8894e98

Browse files
committed
support ask ai
1 parent c401127 commit 8894e98

File tree

5 files changed

+69
-31
lines changed

5 files changed

+69
-31
lines changed

frontend/src/components/chat/chat-bottombar.tsx

Lines changed: 51 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { motion, AnimatePresence } from 'framer-motion';
44
import TextareaAutosize from 'react-textarea-autosize';
55
import { PaperclipIcon, Send, X, Code, Wand2 } from 'lucide-react';
66
import { cn } from '@/lib/utils';
7-
import { Message } from '../../const/MessageType';
7+
import { Message, ChatRequestOptions } from '../../const/MessageType';
88
import Image from 'next/image';
99
import {
1010
Tooltip,
@@ -19,7 +19,10 @@ interface ChatBottombarProps {
1919
messages: Message[];
2020
input: string;
2121
handleInputChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
22-
handleSubmit: (e: React.FormEvent<HTMLFormElement>) => void;
22+
handleSubmit: (
23+
e: React.FormEvent<HTMLFormElement>,
24+
chatRequestOptions?: ChatRequestOptions
25+
) => void;
2326
stop: () => void;
2427
formRef: React.RefObject<HTMLFormElement>;
2528
setInput?: React.Dispatch<React.SetStateAction<string>>;
@@ -59,6 +62,7 @@ export default function ChatBottombar({
5962
const [isFocused, setIsFocused] = useState(false);
6063
const [attachments, setAttachments] = useState<File[]>([]);
6164
const [isComponentMode, setIsComponentMode] = useState(false);
65+
const [componentData, setComponentData] = useState<string>('');
6266
const inputRef = useRef<HTMLTextAreaElement>(null);
6367
const fileInputRef = useRef<HTMLInputElement>(null);
6468

@@ -98,16 +102,12 @@ export default function ChatBottombar({
98102
setAttachments((prev) => prev.filter((_, i) => i !== index));
99103
};
100104

101-
const submitWithAttachments = (e: React.FormEvent<HTMLFormElement>) => {
102-
// Here you would normally handle attachments with your form submission
103-
// For this example, we'll just clear them after submission
104-
handleSubmit(e);
105-
setAttachments([]);
106-
};
107-
108105
const populateChatInput = (content: string) => {
109106
if (setInput) {
110-
setInput(content);
107+
// Store the component data in state instead of showing it in the input
108+
setComponentData(content);
109+
// Keep input clean - don't prefill any text
110+
setInput("");
111111
setIsComponentMode(true);
112112
// Focus the input after populating
113113
setTimeout(() => {
@@ -116,12 +116,11 @@ export default function ChatBottombar({
116116
}
117117
};
118118

119-
// Check if input still contains component text
119+
// Check if we're still in component mode
120120
useEffect(() => {
121-
if (input.includes('Help me modify this component:')) {
122-
setIsComponentMode(true);
123-
} else {
121+
if (!input || input === "") {
124122
setIsComponentMode(false);
123+
setComponentData('');
125124
}
126125
}, [input]);
127126

@@ -130,7 +129,41 @@ export default function ChatBottombar({
130129
if (setInput) {
131130
setInput('');
132131
setIsComponentMode(false);
132+
setComponentData('');
133+
}
134+
};
135+
136+
// Modify submit to include component data if in component mode
137+
const submitWithAttachments = (e: React.FormEvent<HTMLFormElement>) => {
138+
e.preventDefault();
139+
140+
// If in component mode, append the hidden component data
141+
if (isComponentMode && componentData && setInput) {
142+
// Get user input
143+
const userInput = input || "";
144+
145+
// Create the full prompt with component data first, then user input
146+
const fullPrompt = componentData + "\n\n" + userInput;
147+
148+
// Call handleSubmit with the content in ChatRequestOptions
149+
handleSubmit(e, { content: fullPrompt });
150+
151+
// Reset component mode
152+
setIsComponentMode(false);
153+
setComponentData('');
154+
setAttachments([]);
155+
156+
// Clean up input after submission
157+
setTimeout(() => {
158+
setInput("");
159+
}, 50);
160+
161+
return;
133162
}
163+
164+
// Regular submission flow
165+
handleSubmit(e);
166+
setAttachments([]);
134167
};
135168

136169
useEffect(() => {
@@ -252,8 +285,8 @@ export default function ChatBottombar({
252285
className="flex items-center gap-1 text-[10px] py-0.5 px-2 bg-purple-50 text-purple-600 border-purple-200 dark:bg-purple-950/60 dark:text-purple-300 dark:border-purple-800/50 shadow-sm"
253286
>
254287
<Wand2 className="w-2.5 h-2.5" />
255-
<span>Component Mode</span>
256-
</Badge>
288+
<span>Component Selected</span>
289+
</Badge>
257290
</motion.div>
258291
<button
259292
type="button"
@@ -420,11 +453,8 @@ export default function ChatBottombar({
420453
onFocus={() => setIsFocused(true)}
421454
onBlur={() => setIsFocused(false)}
422455
name="message"
423-
placeholder={isComponentMode ? "Describe what you want to change..." : "Message Agent..."}
424-
className={cn(
425-
"resize-none px-2 w-full focus:outline-none bg-transparent text-gray-800 dark:text-zinc-200 text-sm placeholder:text-gray-400 dark:placeholder:text-zinc-400",
426-
isComponentMode ? "pt-4 pb-2.5" : "py-2.5"
427-
)}
456+
placeholder={isComponentMode ? "Message Agent... (component details will be included)" : "Message Agent..."}
457+
className="resize-none px-2 w-full focus:outline-none bg-transparent text-gray-800 dark:text-zinc-200 text-sm placeholder:text-gray-400 dark:placeholder:text-zinc-400 py-2.5"
428458
maxRows={5}
429459
/>
430460
</div>

frontend/src/components/chat/chat-panel.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export interface ChatProps {
1616
handleInputChange: (e: React.ChangeEvent<HTMLTextAreaElement>) => void;
1717
handleSubmit: (
1818
e: React.FormEvent<HTMLFormElement>,
19-
chatRequestOptions?: ChatRequestOptions
19+
chatRequestOptions?: ChatRequestOptions,
2020
) => void;
2121
loadingSubmit?: boolean;
2222
stop: () => void;

frontend/src/components/chat/code-engine/component-inspector/index.tsx

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -329,13 +329,11 @@ export function ComponentInspector({
329329
<Button
330330
onClick={() => {
331331
if (populateChatInput && selectedComponent) {
332-
const componentSummary = `Help me modify this component:
333-
334-
Element: ${selectedComponent.tagName?.toLowerCase() || 'component'}
335-
ID: ${selectedComponent.id || 'none'}
336-
${selectedComponent.attributes && Object.keys(selectedComponent.attributes).length > 0 ?
337-
`Attributes: ${Object.entries(selectedComponent.attributes).map(([key, value]) => `${key}="${value}"`).join(', ')}` : ''}
338-
What I want to change: `;
332+
const componentSummary = `
333+
\`\`\`User Selected Component
334+
File path: ${selectedComponent.path || 'none'}
335+
\`\`\`
336+
`;
339337

340338
populateChatInput(componentSummary);
341339
if (setIsInspectMode) {

frontend/src/const/MessageType.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,5 @@ export interface ChatRequestOptions {
1414
selectedModel?: string;
1515
images?: string[];
1616
attachments?: Attachment[];
17+
content?: string;
1718
}

frontend/src/hooks/useChatStream.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { startChatStream } from '@/api/ChatStreamAPI';
99
import { ProjectContext } from '@/components/chat/code-engine/project-context';
1010
import { ChatInputType } from '@/graphql/type';
1111
import { managerAgent } from './multi-agent/managerAgent';
12+
import { ChatRequestOptions } from '@/const/MessageType';
1213

1314
export interface UseChatStreamProps {
1415
chatId: string;
@@ -117,10 +118,15 @@ export const useChatStream = ({
117118
}
118119
};
119120

120-
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => {
121+
const handleSubmit = async (
122+
e: React.FormEvent<HTMLFormElement>,
123+
chatRequestOptions?: ChatRequestOptions
124+
) => {
121125
e.preventDefault();
122126

123-
const content = input;
127+
// If chatRequestOptions has content property, use that, otherwise use input
128+
const content = chatRequestOptions?.content || input;
129+
console.log('handleSubmit content', content);
124130

125131
if (!content.trim() || loadingSubmit) return;
126132

@@ -133,6 +139,9 @@ export const useChatStream = ({
133139
content: content,
134140
createdAt: new Date().toISOString(),
135141
};
142+
143+
console.log('newMessage', newMessage);
144+
136145
setMessages((prev) => [...prev, newMessage]);
137146

138147
if (!currentChatId) {

0 commit comments

Comments
 (0)