Elffuss BraidHub

kikocis/auto-code-rover public

main 路 13.9 KiBRawPermalink
1'use client'
2import clsx from 'clsx';
3import { BsStars } from "react-icons/bs";
4import { FaWpforms } from "react-icons/fa6";
5import { v4 as uuidv4 } from 'uuid';
6import React, { useState, useEffect, useRef } from 'react'
7import { FaHandsHelping } from "react-icons/fa";
8import { Prism as SyntaxHighlighter } from 'react-syntax-highlighter';
9import { vs } from "react-syntax-highlighter/dist/esm/styles/prism";
10import { PiWheelchair } from "react-icons/pi";
11import toast, { Toaster } from 'react-hot-toast';
12import { FieldValues, useForm } from 'react-hook-form';
13import { FaAnglesDown, FaAnglesUp } from "react-icons/fa6";
14import Markdown from 'marked-react';
15
16interface Message {
17 id: string
18 category: string,
19 is_open: boolean,
20 // receive_time: Date
21};
22
23interface AutoCodeRoverMessage extends Message {
24 title: string,
25 message: string,
26 category: 'auto_code_rover'
27};
28
29interface ContextRetrievalAgentMessage extends Message {
30 title: string,
31 message: string,
32 category: 'context_retrieval_agent'
33};
34
35interface PatchGenerationMessage extends Message {
36 title: string,
37 message: string,
38 category: 'patch_generation'
39};
40
41interface IssueInfo extends Message {
42 problem_statement: string,
43 category: 'issue_info'
44};
45
46type AnyMessage = AutoCodeRoverMessage | ContextRetrievalAgentMessage | IssueInfo | PatchGenerationMessage;
47
48function MarkdownRender({
49 markdown,
50 baseURL
51}: {
52 markdown: string,
53 baseURL?: string
54}) {
55 const uuid = uuidv4();
56 const renderer = {
57 code(snippet: string, lang: string) {
58 return <div>
59 {/* monoBlue */}
60 <SyntaxHighlighter language={lang} style={vs}>
61 {snippet}
62 </SyntaxHighlighter>
63 </div>
64 },
65 };
66 return (
67 <article className='prose'>
68 <Markdown
69 value={markdown}
70 baseURL={baseURL ? baseURL : ''}
71 renderer={renderer}
72 gfm
73 />
74 </article >
75 )
76}
77
78const LoadingDiv = () => {
79 return (
80 <div
81 className='bg-gray-200 bg-opacity-30 p-4 rounded-2xl flex space-x-3
82 group hover:bg-slate-200
83 '
84 >
85 <div className="flex-auto py-0.5 text-lg leading-normal text-gray-800">
86 <div
87 className="font-medium text-gray-900 flex items-center space-x-2 justify-around"
88 >
89 <span>Wating for more Agent search and actions ... </span>
90 </div>
91 </div>
92 </div>
93 );
94};
95
96const MessageDiv = ({
97 message,
98 setIsOpen,
99 icon: Icon
100}: {
101 message: AutoCodeRoverMessage | ContextRetrievalAgentMessage | PatchGenerationMessage,
102 setIsOpen: (isOpen: boolean) => void,
103 icon: React.ReactNode
104}) => {
105
106 return (
107 <>
108 {
109 message.is_open &&
110 <div className='bg-gray-200 bg-opacity-30 p-4 pr-12 rounded-2xl flex space-x-3'>
111 <div>
112 <div className="relative flex h-6 w-6 flex-none items-center justify-center bg-white">
113 {Icon}
114 </div>
115 </div>
116 <div className="flex-auto py-0.5 text-lg leading-normal text-gray-800">
117 <button
118 className="font-medium text-gray-900 flex items-center space-x-2
119 hover:bg-slate-200 px-2 rounded-md
120 "
121 onClick={() => setIsOpen(false)}
122 >
123 <span>{message.title}</span>
124 <FaAnglesUp className='h-3 w-3' />
125
126 </button>
127 <div
128 className='mt-2'
129 >
130 <MarkdownRender markdown={message.message} />
131 </div>
132 </div>
133 {/* <time dateTime='time' className="flex-none py-0.5 text-xs leading-5 text-gray-950">
134 time
135 </time> */}
136 </div>
137 }
138 {
139 !message.is_open &&
140 <button
141 className='bg-gray-200 bg-opacity-30 p-4 rounded-2xl flex space-x-3
142 group hover:bg-slate-200
143 '
144 onClick={() => setIsOpen(true)}
145 >
146 <div>
147 <div className="relative flex h-6 w-6 flex-none items-center justify-center bg-white">
148 {Icon}
149 </div>
150 </div>
151 <div className="flex-auto py-0.5 text-lg leading-normal text-gray-800">
152 <div
153 className="font-medium text-gray-900 flex items-center space-x-2
154 "
155 >
156 <span>{message.title}</span>
157 <FaAnglesDown className='h-3 w-3' />
158 </div>
159 </div>
160 {/* <time dateTime='time' className="flex-none py-0.5 text-xs leading-5 text-gray-950">
161 time
162 </time> */}
163 </button>
164 }
165
166 </>
167 );
168};
169
170export default function App() {
171
172 const {
173 register,
174 handleSubmit,
175 setValue
176 } = useForm<FieldValues>();
177 const [messages, setMessage] = useState<Array<AnyMessage>>([]);
178 const [problemStatement, setProblemStatement] = useState<string | undefined>(undefined);
179 const messagesEndRef = useRef<HTMLDivElement>(null);
180 useEffect(() => {
181 messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
182 }, [messages]);
183
184 // state indecating the process:
185 // 0: before starting
186 // 1: waiting for servering cloning the git issue
187 // 2: communicating with server and show the agent actions
188 // 3: done
189 const [loadingState, setLoadingState] = useState<number>(0);
190 const [toastId, setToastId] = useState<string>();
191
192 const demo_form_callback = () => {
193 setValue('repository_link', 'https://github.com/langchain-ai/langchain.git');
194 setValue('commit_hash', 'cb6e5e5');
195 setValue('issue_link', 'https://github.com/langchain-ai/langchain/issues/20453');
196 };
197
198 const clear_form_callback = () => {
199 setValue('repository_link', '');
200 setValue('commit_hash', '');
201 setValue('issue_link', '');
202 setLoadingState(0);
203 setProblemStatement('');
204 setMessage([]);
205 toast.dismiss();
206 };
207
208 const onsubmmit_callback = async (data: FieldValues) => {
209 const call_server = fetch('http://localhost:5000/api/run_github_issue', {
210 method: 'POST',
211 headers: {
212 'Content-Type': 'application/json'
213 },
214 body: JSON.stringify(data)
215 })
216
217 toast.loading('downloading issue and repository ...');
218
219 call_server.then(response => {
220 if (response.status !== 200) {
221 return response.json().then(data => {
222 toast.error(data.message);
223 throw new Error(data.message);
224 });
225 }
226
227 if (!response.body)
228 throw Error('???');
229 setLoadingState(1);
230 const reader = response.body.getReader();
231
232 function read(): Promise<void> {
233 return reader.read().then(({ done, value }) => {
234 if (done) {
235 console.log('Stream completed');
236 toast.dismiss();
237 toast.success('Done !');
238 setLoadingState(0);
239 return;
240 }
241 const text = new TextDecoder().decode(value);
242 text.split('</////json_end>').forEach(item => {
243 if (item) {
244 const message: AnyMessage = JSON.parse(item);
245 message.is_open = true;
246 message.id = uuidv4();
247 console.log(message);
248 if (message.category === 'issue_info') {
249 setIssueInfo(message);
250 setLoadingState(2);
251 toast.dismiss();
252 toast.success('Git issue clone successfully !');
253 toast.loading('Agent running');
254 } else {
255 setMessage(prevMessages => {
256 const newMessages = prevMessages.map((m, index) => {
257 if (index === prevMessages.length - 1) {
258 return { ...m, is_open: false };
259 } else {
260 return m;
261 }
262 });
263 newMessages.push(message);
264 return newMessages;
265 });
266 }
267 }
268 })
269 return read();
270 });
271 }
272 return read();
273 })
274 .catch(error => {
275 console.error('Error:', error);
276 });
277 };
278
279 const setIssueInfo = (message: IssueInfo) => {
280 const s = message.problem_statement;
281 const newlineIndex = s.indexOf('\n');
282 const firstLine = newlineIndex !== -1 ? s.substring(0, newlineIndex) : s;
283 const lastLine = newlineIndex !== -1 ? s.substring(newlineIndex + 1) : s;
284 // setProblemStatement('## ' + firstLine + ''message.problem_statement);
285 setProblemStatement(`## ${firstLine} \n ${lastLine}`)
286 }
287
288 const setOpen = (isOpen: boolean, set_message_id: string) => {
289 const newItems = messages.map(message__ => {
290 if (message__.id === set_message_id) {
291 return { ...message__, is_open: isOpen };
292 }
293 return message__;
294 });
295 setMessage(newItems);
296 };
297
298 return (
299 <div className='w-full h-[100vh] flex flex-row'>
300 <Toaster
301 position="bottom-right"
302 reverseOrder={false}
303 />
304
305 <div className='
306 w-[30%] h-[100vh] lg:p-6 flex flex-col space-y-3
307 bg-gray-100
308 '>
309
310 <label className='text-2xl'>
311 Input the meta infomation of issue here
312 </label>
313
314 <form
315 className='flex flex-col space-y-3'>
316
317 <div className='flex items-center space-x-3'>
318 <label className='w-32'>
319 repository
320 </label>
321 <input
322 {...register('repository_link')}
323 type="text"
324 className='p-2 border w-[80%] rounded-sm'
325 placeholder='the link to the repository'
326 />
327 </div>
328
329 <div className='flex items-center space-x-3'>
330 <label className='w-32'>
331 commit hash
332 </label>
333 <input
334 {...register('commit_hash')}
335 type="text"
336 className='p-2 border w-[80%] rounded-sm'
337 placeholder='the commit hash to checkout'
338 />
339 </div>
340
341 <div className='flex items-center space-x-3'>
342 <label className='w-32'>
343 issue
344 </label>
345 <input
346 {...register('issue_link')}
347 type="text"
348 className='p-2 border w-[80%] rounded-sm'
349 placeholder='The link to the issue'
350 />
351 </div>
352
353 <div className='flex justify-around text-sm'>
354
355 <button
356 className='w-[40%] m-1 py-4 p-2 border rounded-xl bg-slate-200 hover:bg-slate-300 hover:border-slate-50'
357 onClick={(event: React.MouseEvent) => {
358 event.preventDefault();
359 clear_form_callback();
360 }}>
361 馃Ч Clear all ! 馃Ч
362 </button>
363
364 <button
365 className='w-[40%] m-1 py-4 p-2 border rounded-xl bg-slate-200 hover:bg-slate-300 hover:border-slate-50'
366 type='submit'
367 onClick={handleSubmit(onsubmmit_callback)}>
368 馃殌 Boot Now ! 馃殌
369 </button>
370 </div>
371 </form>
372
373 <div className={clsx(
374 'py-2 overflow-y-auto border-2 border-t-2 border-gray-400 border-t-gray-400 rounded-xl border-dashed w-full h-full',
375 !problemStatement && 'flex items-center justify-around'
376 )}>
377 {
378 !problemStatement && (
379 <div className='flex flex-col items-center space-y-2'>
380 <FaWpforms className='w-20 h-20' />
381 <span className='font-semibold'>No Issues</span>
382 <span className='text-gray-500'>
383 Try one example we prepare for you.
384 </span>
385 <button
386 className='m-1 py-4 p-2 border rounded-xl bg-slate-200 hover:bg-slate-300 hover:border-slate-50'
387 onClick={(event: React.MouseEvent) => {
388 event.preventDefault();
389 demo_form_callback();
390 }}>
391 馃挕 Try example ? 馃挕
392 </button>
393 </div>
394 )
395 }
396 {
397 problemStatement && (
398 <div className='p-4 py-2'>
399 <MarkdownRender
400 markdown={problemStatement}
401 >
402 </MarkdownRender>
403 </div>
404 )
405 }
406 </div>
407 </div>
408
409 <div className='w-[70%] h-[100vh] overflow-y-auto'>
410 <ul
411 role="list"
412 className="
413 space-y-6
414 p-6
415 "
416 >
417 {messages.map((message, index) => (
418 <li key={message.id} className="relative flex gap-x-4 overflow-x-auto">
419 {
420 !(message.category === 'issue_info') &&
421 <MessageDiv
422 message={message as AutoCodeRoverMessage | ContextRetrievalAgentMessage | PatchGenerationMessage}
423 setIsOpen={(isopen: boolean) => setOpen(isopen, message.id)}
424 icon={
425 <>
426 {
427 message.category === 'auto_code_rover' && (
428 <FaHandsHelping className='h-6 w-6 text-indigo-600 bg-gray-200 bg-opacity-30'
429 aria-hidden="true"
430 />
431 )
432 }
433 {
434 message.category === 'context_retrieval_agent' && (
435
436 <PiWheelchair className='h-6 w-6 text-indigo-600 bg-gray-200 bg-opacity-30'
437 aria-hidden="true"
438 />
439 )
440 }
441 {
442 message.category === 'patch_generation' && (
443 <BsStars className='h-6 w-6 text-indigo-600 bg-gray-200 bg-opacity-30'
444 aria-hidden="true"
445 />
446 )
447 }
448 </>
449 }
450 />
451 }
452
453 </li>
454 ))}
455 {
456 loadingState === 1 &&
457 <div ref={messagesEndRef}>
458 <LoadingDiv />
459 </div>
460 }
461 </ul>
462 </div>
463 </div >
464 )
465}