forked from RooCodeInc/Roo-Code
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathcreator.tsx
More file actions
189 lines (170 loc) · 5.38 KB
/
creator.tsx
File metadata and controls
189 lines (170 loc) · 5.38 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
import { useState, useCallback, useEffect, useRef } from "react"
import { vscode } from "../utils/vscode"
import { WebviewMessage } from "../../../src/shared/WebviewMessage"
import { ExtensionMessage, ClineMessage } from "../../../src/shared/ExtensionMessage"
import { Markdown } from "../components/ui/markdown"
interface Message {
role: "user" | "assistant"
content: string
timestamp: number
}
const Creator = () => {
const [prompt, setPrompt] = useState("")
const [isProcessing, setIsProcessing] = useState(false)
const [response, setResponse] = useState<ClineMessage | null>(null)
const [messages, setMessages] = useState<Message[]>([])
const [isLoading, setIsLoading] = useState(false)
const [inputValue, setInputValue] = useState("")
const messagesEndRef = useRef<HTMLDivElement>(null)
const handleSubmit = async (e: React.FormEvent) => {
e.preventDefault()
if (!prompt.trim() || isProcessing) return
setIsProcessing(true)
try {
const message: WebviewMessage = {
type: "creator",
text: prompt.trim(),
}
await vscode.postMessage(message)
setPrompt("")
} finally {
setIsProcessing(false)
}
}
const handleMessage = useCallback((e: MessageEvent<ExtensionMessage>) => {
const message = e.data
if (message.type === "partialMessage" && message.partialMessage) {
setResponse(message.partialMessage)
}
}, [])
const scrollToBottom = () => {
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" })
}
useEffect(() => {
scrollToBottom()
}, [messages])
const handleSendMessage = useCallback((text: string) => {
if (!text.trim()) return
// Add user message to chat
setMessages((prev) => [
...prev,
{
role: "user",
content: text.trim(),
timestamp: Date.now(),
},
])
setIsLoading(true)
// Send message to extension
vscode.postMessage({
type: "newTask",
text: text.trim(),
})
}, [])
// Add message event listener
useEffect(() => {
const handleMessage = (event: MessageEvent) => {
const message = event.data
if (message.type === "partialMessage" && message.partialMessage) {
// Handle streaming response
setMessages((prev) => {
const lastMessage = prev[prev.length - 1]
if (lastMessage?.role === "assistant") {
// Update existing assistant message
return [
...prev.slice(0, -1),
{
...lastMessage,
content: lastMessage.content + message.partialMessage.text,
},
]
} else {
// Add new assistant message
return [
...prev,
{
role: "assistant",
content: message.partialMessage.text || "",
timestamp: Date.now(),
},
]
}
})
setIsLoading(false)
}
}
window.addEventListener("message", handleMessage)
return () => window.removeEventListener("message", handleMessage)
}, [])
return (
<div className="flex flex-col h-screen p-4 bg-background text-foreground">
{/* Messages container with scrolling */}
<div className="flex-1 overflow-y-auto space-y-4 mb-4">
{messages.map((msg, i) => (
<div
key={msg.timestamp}
className={`max-w-[80%] rounded-lg p-4 animate-fade-in transition-all duration-200 ease-in-out hover:shadow-lg ${
msg.role === "user"
? "ml-auto bg-[#E64C9E] text-white hover:bg-[#D33C8E]"
: "bg-editor-background border border-input-border hover:border-[#E64C9E]"
}`}>
{/* Use Markdown component for messages */}
<Markdown content={msg.content} />
</div>
))}
{isLoading && (
<div className="text-muted-foreground text-center py-4 animate-pulse">
<div className="flex items-center justify-center gap-2">
<div className="w-2 h-2 bg-[#E64C9E] rounded-full animate-bounce"></div>
<div className="w-2 h-2 bg-[#E64C9E] rounded-full animate-bounce delay-100"></div>
<div className="w-2 h-2 bg-[#E64C9E] rounded-full animate-bounce delay-200"></div>
</div>
</div>
)}
{/* Scroll anchor */}
<div ref={messagesEndRef} />
</div>
{/* Input container */}
<div className="flex items-end gap-4 mt-auto p-4 bg-editor-background rounded-lg border border-input-border">
<div className="flex-1">
<textarea
value={inputValue}
onChange={(e) => setInputValue(e.target.value)}
onKeyPress={(e) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault()
handleSendMessage(inputValue)
setInputValue("")
}
}}
placeholder="Type your message..."
rows={1}
className="w-full px-4 py-2 rounded-lg bg-input-background text-input-foreground border-none focus:outline-none focus:ring-2 focus:ring-[#E64C9E] resize-none"
/>
</div>
<button
onClick={() => {
handleSendMessage(inputValue)
setInputValue("")
}}
disabled={isLoading}
className="px-6 py-2 bg-[#E64C9E] text-white rounded-lg flex items-center gap-2 hover:bg-[#D33C8E] disabled:opacity-50 disabled:cursor-not-allowed transition-all duration-200 ease-in-out transform hover:scale-105 active:scale-95">
<span>Send</span>
<svg
className="w-4 h-4 transition-transform duration-200 group-hover:translate-x-1"
fill="none"
stroke="currentColor"
viewBox="0 0 24 24">
<path
strokeLinecap="round"
strokeLinejoin="round"
strokeWidth={2}
d="M14 5l7 7m0 0l-7 7m7-7H3"
/>
</svg>
</button>
</div>
</div>
)
}
export default Creator