-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAPP.JS
More file actions
1632 lines (1470 loc) · 62.7 KB
/
Copy pathAPP.JS
File metadata and controls
1632 lines (1470 loc) · 62.7 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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// src/App.js - 메인 애플리케이션 컴포넌트
import React from 'react';
import { BrowserRouter as Router, Routes, Route } from 'react-router-dom';
import ZAVISHomepage from './components/ZAVISHomepage';
import Dashboard from './pages/Dashboard';
import Login from './pages/Login';
import Register from './pages/Register';
import ChatInterface from './pages/ChatInterface';
import Profile from './pages/Profile';
import { AuthProvider } from './contexts/AuthContext';
import PrivateRoute from './components/PrivateRoute';
import NotFound from './pages/NotFound';
function App() {
return (
<AuthProvider>
<Router>
<Routes>
<Route path="/" element={<ZAVISHomepage />} />
<Route path="/login" element={<Login />} />
<Route path="/register" element={<Register />} />
<Route path="/dashboard" element={
<PrivateRoute>
<Dashboard />
</PrivateRoute>
} />
<Route path="/chat" element={
<PrivateRoute>
<ChatInterface />
</PrivateRoute>
} />
<Route path="/profile" element={
<PrivateRoute>
<Profile />
</PrivateRoute>
} />
<Route path="*" element={<NotFound />} />
</Routes>
</Router>
</AuthProvider>
);
}
export default App;
// src/contexts/AuthContext.js - 인증 컨텍스트 구현
import React, { createContext, useState, useEffect } from 'react';
import api from '../services/api';
export const AuthContext = createContext();
export const AuthProvider = ({ children }) => {
const [user, setUser] = useState(null);
const [loading, setLoading] = useState(true);
const [error, setError] = useState(null);
useEffect(() => {
// 저장된 토큰으로 사용자 정보 불러오기
const loadUser = async () => {
const token = localStorage.getItem('token');
if (token) {
try {
const res = await api.get('/auth/user');
setUser(res.data.user);
} catch (err) {
// 토큰이 유효하지 않으면 로그아웃
localStorage.removeItem('token');
setUser(null);
setError('세션이 만료되었습니다. 다시 로그인해주세요.');
}
}
setLoading(false);
};
loadUser();
}, []);
// 로그인 함수
const login = async (email, password) => {
try {
const res = await api.post('/auth/login', { email, password });
localStorage.setItem('token', res.data.token);
setUser(res.data.user);
setError(null);
return true;
} catch (err) {
setError(err.response?.data?.error || '로그인 중 오류가 발생했습니다.');
return false;
}
};
// 회원가입 함수
const register = async (name, email, password) => {
try {
const res = await api.post('/auth/register', { name, email, password });
localStorage.setItem('token', res.data.token);
setUser(res.data.user);
setError(null);
return true;
} catch (err) {
setError(err.response?.data?.error || '회원가입 중 오류가 발생했습니다.');
return false;
}
};
// 로그아웃 함수
const logout = () => {
localStorage.removeItem('token');
setUser(null);
};
return (
<AuthContext.Provider value={{ user, loading, error, login, register, logout, setError }}>
{children}
</AuthContext.Provider>
);
};
export default Dashboard;
// src/components/DashboardLayout.js - 대시보드 레이아웃
import React, { useState, useContext } from 'react';
import { Link, useNavigate, useLocation } from 'react-router-dom';
import { AuthContext } from '../contexts/AuthContext';
import Logo from './Logo';
const DashboardLayout = ({ children }) => {
const [sidebarOpen, setSidebarOpen] = useState(false);
const { user, logout } = useContext(AuthContext);
const navigate = useNavigate();
const location = useLocation();
const handleLogout = () => {
logout();
navigate('/');
};
const navigation = [
{ name: '대시보드', href: '/dashboard', icon: '📊' },
{ name: 'AI 채팅', href: '/chat', icon: '💬' },
{ name: '프로필', href: '/profile', icon: '👤' },
];
return (
<div className="min-h-screen bg-gray-100 dark:bg-gray-900">
{/* 모바일 사이드바 */}
<div className={`fixed inset-0 flex z-40 md:hidden transform ${sidebarOpen ? 'translate-x-0' : '-translate-x-full'} transition-transform duration-300 ease-in-out`}>
<div className="relative flex-1 flex flex-col max-w-xs w-full bg-white dark:bg-gray-800 shadow-xl">
<div className="absolute top-0 right-0 -mr-12 pt-2">
<button
type="button"
className="ml-1 flex items-center justify-center h-10 w-10 rounded-full focus:outline-none focus:ring-2 focus:ring-inset focus:ring-white"
onClick={() => setSidebarOpen(false)}
>
<span className="sr-only">사이드바 닫기</span>
<span className="text-white text-2xl">✕</span>
</button>
</div>
<div className="flex-1 h-0 pt-5 pb-4 overflow-y-auto">
<div className="flex-shrink-0 flex items-center px-4">
<Logo />
</div>
<nav className="mt-5 px-2 space-y-1">
{navigation.map((item) => (
<Link
key={item.name}
to={item.href}
className={`group flex items-center px-2 py-2 text-base font-medium rounded-md ${
location.pathname === item.href
? 'bg-blue-100 text-blue-900 dark:bg-blue-900 dark:text-blue-100'
: 'text-gray-600 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-700'
}`}
>
<span className="mr-3 flex-shrink-0 h-6 w-6 flex items-center justify-center">{item.icon}</span>
{item.name}
</Link>
))}
</nav>
</div>
<div className="flex-shrink-0 flex border-t border-gray-200 dark:border-gray-700 p-4">
<div className="flex items-center">
<div className="flex-shrink-0">
<div className="h-10 w-10 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold">
{user?.name?.charAt(0)}
</div>
</div>
<div className="ml-3">
<p className="text-sm text-green-700 dark:text-green-300">{passwordSuccess}</p>
</div>
)}
{passwordError && (
<div className="mb-4 bg-red-50 dark:bg-red-900/30 border-l-4 border-red-500 p-4">
<p className="text-sm text-red-700 dark:text-red-300">{passwordError}</p>
</div>
)}
<form onSubmit={handlePasswordChange} className="space-y-4">
<div>
<label htmlFor="currentPassword" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
현재 비밀번호
</label>
<div className="mt-1">
<input
id="currentPassword"
type="password"
value={currentPassword}
onChange={(e) => setCurrentPassword(e.target.value)}
required
className="appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm dark:bg-gray-700 dark:text-white"
/>
</div>
</div>
<div>
<label htmlFor="newPassword" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
새 비밀번호
</label>
<div className="mt-1">
<input
id="newPassword"
type="password"
value={newPassword}
onChange={(e) => setNewPassword(e.target.value)}
required
className="appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm dark:bg-gray-700 dark:text-white"
/>
</div>
</div>
<div>
<label htmlFor="confirmPassword" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
새 비밀번호 확인
</label>
<div className="mt-1">
<input
id="confirmPassword"
type="password"
value={confirmPassword}
onChange={(e) => setConfirmPassword(e.target.value)}
required
className="appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm dark:bg-gray-700 dark:text-white"
/>
</div>
</div>
<div>
<button
type="submit"
disabled={isLoading.password}
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
>
{isLoading.password ? (
<span className="flex items-center">
<svg className="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
변경 중...
</span>
) : (
'비밀번호 변경'
)}
</button>
</div>
</form>
</div>
{/* API 키 설정 */}
<div className="bg-white dark:bg-gray-800 shadow rounded-lg p-6">
<h2 className="text-xl font-semibold mb-4">API 키 설정</h2>
{apiKeysSuccess && (
<div className="mb-4 bg-green-50 dark:bg-green-900/30 border-l-4 border-green-500 p-4">
<p className="text-sm text-green-700 dark:text-green-300">{apiKeysSuccess}</p>
</div>
)}
{apiKeysError && (
<div className="mb-4 bg-red-50 dark:bg-red-900/30 border-l-4 border-red-500 p-4">
<p className="text-sm text-red-700 dark:text-red-300">{apiKeysError}</p>
</div>
)}
<div className="mb-4 bg-blue-50 dark:bg-blue-900/30 border-l-4 border-blue-500 p-4">
<p className="text-sm text-blue-700 dark:text-blue-300">
API 키는 서버에서 안전하게 암호화되어 저장됩니다. 키를 입력하지 않으면 서버에 구성된 기본 API 키가 사용됩니다.
</p>
</div>
<form onSubmit={handleApiKeysSave} className="space-y-4">
<div>
<label htmlFor="openaiKey" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
OpenAI API 키
</label>
<div className="mt-1">
<input
id="openaiKey"
type="password"
value={openaiKey}
onChange={(e) => setOpenaiKey(e.target.value)}
placeholder="sk-..."
className="appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm dark:bg-gray-700 dark:text-white"
/>
</div>
</div>
<div>
<label htmlFor="claudeKey" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
Claude API 키
</label>
<div className="mt-1">
<input
id="claudeKey"
type="password"
value={claudeKey}
onChange={(e) => setClaudeKey(e.target.value)}
placeholder="sk-ant-api..."
className="appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm dark:bg-gray-700 dark:text-white"
/>
</div>
</div>
<div>
<button
type="submit"
disabled={isLoading.apiKeys}
className="w-full flex justify-center py-2 px-4 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
>
{isLoading.apiKeys ? (
<span className="flex items-center">
<svg className="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
저장 중...
</span>
) : (
'API 키 저장'
)}
</button>
</div>
</form>
</div>
</div>
</div>
</DashboardLayout>
);
};
export default Profile;
// src/pages/NotFound.js - 404 페이지
import React from 'react';
import { Link } from 'react-router-dom';
import Logo from '../components/Logo';
const NotFound = () => {
return (
<div className="min-h-screen bg-gray-50 dark:bg-gray-900 flex flex-col justify-center py-12 sm:px-6 lg:px-8">
<div className="mt-8 sm:mx-auto sm:w-full sm:max-w-md">
<div className="text-center mb-6">
<Link to="/" className="inline-block">
<Logo />
</Link>
</div>
<div className="bg-white dark:bg-gray-800 py-8 px-4 shadow sm:rounded-lg sm:px-10 text-center">
<h2 className="text-6xl font-extrabold text-blue-600">404</h2>
<h1 className="mt-4 text-2xl font-bold text-gray-900 dark:text-white">페이지를 찾을 수 없습니다</h1>
<p className="mt-2 text-base text-gray-600 dark:text-gray-400">
요청하신 페이지가 존재하지 않거나 이동되었을 수 있습니다.
</p>
<div className="mt-6">
<Link
to="/"
className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500"
>
홈으로 돌아가기
</Link>
</div>
</div>
</div>
</div>
);
};
export default NotFound;
// src/components/Logo.js - 로고 컴포넌트
import React from 'react';
const Logo = () => {
return (
<div className="flex items-center">
<div className="text-2xl font-bold bg-blue-600 text-white px-2 py-1 rounded">
ZAVIS
</div>
<div className="text-xl font-bold ml-1 dark:text-white">
Engine
</div>
</div>
);
};
export default Logo;
// 서버 측 코드 (.env 파일)
/*
PORT=5000
MONGODB_URI=mongodb://localhost:27017/zavis-engine
JWT_SECRET=your_jwt_secret_here
OPENAI_API_KEY=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
CLAUDE_API_KEY=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
NODE_ENV=production
FRONTEND_URL=https://zavis-engine.com
*/
// server.js에 추가할 API 키 관리 엔드포인트
/*
// /backend/routes/auth.js에 추가 - API 키 관리 엔드포인트
router.put('/api-keys', authenticateUser, asyncHandler(async (req, res) => {
const { openaiKey, claudeKey } = req.body;
// 사용자 API 키 설정 업데이트
const user = await User.findById(req.user._id);
// 키가 제공된 경우에만 업데이트
if (openaiKey) {
// 실제 환경에서는 키를 암호화하여 저장해야 함
user.openaiKey = encrypt(openaiKey);
}
if (claudeKey) {
// 실제 환경에서는 키를 암호화하여 저장해야 함
user.claudeKey = encrypt(claudeKey);
}
await user.save();
res.json({ message: 'API 키가 성공적으로 업데이트되었습니다.' });
}));
// 프로필 업데이트 엔드포인트
router.put('/profile', authenticateUser, asyncHandler(async (req, res) => {
const { name } = req.body;
// 사용자 정보 업데이트
const user = await User.findById(req.user._id);
user.name = name;
await user.save();
res.json({
user: {
id: user._id,
name: user.name,
email: user.email
}
});
}));
*/
// services/aiService.js에 수정할 부분
/*
// /backend/services/aiService.js 수정 - 사용자별 API 키 지원
// API 키 가져오기 (사용자 커스텀 키 또는 기본 키)
const getApiKey = async (userId, provider) => {
// 사용자 조회
const user = await User.findById(userId);
// 사용자가 해당 서비스의 API 키를 설정했는지 확인
if (provider === 'openai' && user.openaiKey) {
// 암호화된 키 복호화
return decrypt(user.openaiKey);
} else if (provider === 'claude' && user.claudeKey) {
return decrypt(user.claudeKey);
}
// 사용자별 키가 없으면 기본 환경 변수 키 사용
return {
openai: process.env.OPENAI_API_KEY,
claude: process.env.CLAUDE_API_KEY
}[provider];
};
// OpenAI API 호출 (사용자별 키 지원)
const callOpenAI = async (prompt, model = 'gpt-4o', userId) => {
try {
const apiKey = await getApiKey(userId, 'openai');
const response = await axios.post('https://api.openai.com/v1/chat/completions', {
model,
messages: [{ role: 'user', content: prompt }],
temperature: 0.7,
max_tokens: 1000
}, {
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${apiKey}`
}
});
return {
text: response.data.choices[0].message.content,
model: response.data.model,
usage: response.data.usage
};
} catch (error) {
console.error('OpenAI API 호출 오류:', error.response?.data || error.message);
throw {
statusCode: error.response?.status || 500,
message: error.response?.data?.error?.message || 'OpenAI API 호출 중 오류가 발생했습니다.'
};
}
};
// Claude API 호출 (사용자별 키 지원)
const callClaude = async (prompt, model = 'claude-3-5-sonnet-20240620', userId) => {
try {
const apiKey = await getApiKey(userId, 'claude');
const response = await axios.post('https://api.anthropic.com/v1/messages', {
model,
messages: [{ role: 'user', content: prompt }],
max_tokens: 1000
}, {
headers: {
'Content-Type': 'application/json',
'x-api-key': apiKey,
'anthropic-version': '2023-06-01'
}
});
return {
text: response.data.content[0].text,
model: response.data.model,
usage: {
input_tokens: response.data.usage?.input_tokens,
output_tokens: response.data.usage?.output_tokens
}
};
} catch (error) {
console.error('Claude API 호출 오류:', error.response?.data || error.message);
throw {
statusCode: error.response?.status || 500,
message: error.response?.data?.error?.message || 'Claude API 호출 중 오류가 발생했습니다.'
};
}
};
*/
// MongoDB 스키마 수정 - API 키 필드 추가
/*
// /backend/models/user.js 수정 - API 키 필드 추가
const userSchema = new mongoose.Schema({
name: {
type: String,
required: true,
trim: true
},
email: {
type: String,
required: true,
unique: true,
trim: true,
lowercase: true
},
password: {
type: String,
required: true
},
openaiKey: {
type: String,
default: null
},
claudeKey: {
type: String,
default: null
},
createdAt: {
type: Date,
default: Date.now
}
});
*/="text-base font-medium text-gray-700 dark:text-gray-200">{user?.name}</p>
<button
onClick={handleLogout}
className="text-sm font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
>
로그아웃
</button>
</div>
</div>
</div>
</div>
<div className="flex-shrink-0 w-14" aria-hidden="true">
{/* 포스 사이드바 클릭 영역을 닫기 위한 공백 */}
</div>
</div>
{/* 데스크톱 사이드바 */}
<div className="hidden md:flex md:w-64 md:flex-col md:fixed md:inset-y-0">
<div className="flex-1 flex flex-col min-h-0 bg-white dark:bg-gray-800 shadow">
<div className="flex-1 flex flex-col pt-5 pb-4 overflow-y-auto">
<div className="flex items-center flex-shrink-0 px-4">
<Logo />
</div>
<nav className="mt-8 flex-1 px-2 space-y-1">
{navigation.map((item) => (
<Link
key={item.name}
to={item.href}
className={`group flex items-center px-2 py-2 text-sm font-medium rounded-md ${
location.pathname === item.href
? 'bg-blue-100 text-blue-900 dark:bg-blue-900 dark:text-blue-100'
: 'text-gray-600 hover:bg-gray-100 dark:text-gray-300 dark:hover:bg-gray-700'
}`}
>
<span className="mr-3 flex-shrink-0 h-6 w-6 flex items-center justify-center">{item.icon}</span>
{item.name}
</Link>
))}
</nav>
</div>
<div className="flex-shrink-0 flex border-t border-gray-200 dark:border-gray-700 p-4">
<div className="flex items-center">
<div className="flex-shrink-0">
<div className="h-10 w-10 rounded-full bg-blue-500 flex items-center justify-center text-white font-bold">
{user?.name?.charAt(0)}
</div>
</div>
<div className="ml-3">
<p className="text-sm font-medium text-gray-700 dark:text-gray-200">{user?.name}</p>
<button
onClick={handleLogout}
className="text-xs font-medium text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200"
>
로그아웃
</button>
</div>
</div>
</div>
</div>
</div>
{/* 메인 콘텐츠 */}
<div className="md:pl-64 flex flex-col flex-1">
<div className="sticky top-0 z-10 md:hidden pl-1 pt-1 sm:pl-3 sm:pt-3 bg-white dark:bg-gray-900 shadow">
<button
type="button"
className="-ml-0.5 -mt-0.5 h-12 w-12 inline-flex items-center justify-center rounded-md text-gray-500 hover:text-gray-900 dark:text-gray-400 dark:hover:text-gray-100 focus:outline-none focus:ring-2 focus:ring-inset focus:ring-blue-500"
onClick={() => setSidebarOpen(true)}
>
<span className="sr-only">사이드바 열기</span>
<span className="text-2xl">☰</span>
</button>
</div>
<main className="flex-1">
{children}
</main>
</div>
</div>
);
};
export default DashboardLayout;
// src/pages/ChatInterface.js - AI 채팅 인터페이스
import React, { useState, useRef, useEffect } from 'react';
import DashboardLayout from '../components/DashboardLayout';
import { callOpenAI, callClaude } from '../services/aiService';
const ChatInterface = () => {
const [messages, setMessages] = useState([
{
role: 'assistant',
content: '안녕하세요! ZAVIS Engine에 오신 것을 환영합니다. GPT-4o와 Claude 3.5를 활용한 지능형 AI 어시스턴트입니다. 무엇을 도와드릴까요?',
model: 'system'
}
]);
const [input, setInput] = useState('');
const [isProcessing, setIsProcessing] = useState(false);
const [selectedModel, setSelectedModel] = useState('gpt-4o');
const [error, setError] = useState(null);
const messagesEndRef = useRef(null);
// 메시지 영역 자동 스크롤
useEffect(() => {
messagesEndRef.current?.scrollIntoView({ behavior: 'smooth' });
}, [messages]);
const handleInputChange = (e) => {
setInput(e.target.value);
};
const handleModelChange = (e) => {
setSelectedModel(e.target.value);
};
const handleSubmit = async (e) => {
e.preventDefault();
if (input.trim() === '' || isProcessing) return;
// 사용자 메시지 추가
const userMessage = {
role: 'user',
content: input,
timestamp: new Date().toISOString()
};
setMessages(prev => [...prev, userMessage]);
setInput('');
setIsProcessing(true);
setError(null);
try {
let response;
// 선택된 모델에 따라 API 호출
if (selectedModel.startsWith('gpt')) {
response = await callOpenAI(input, selectedModel);
} else {
response = await callClaude(input, selectedModel);
}
// AI 응답 추가
const aiMessage = {
role: 'assistant',
content: response.text,
model: response.model,
timestamp: new Date().toISOString()
};
setMessages(prev => [...prev, aiMessage]);
} catch (err) {
console.error('AI 응답 오류:', err);
setError(typeof err === 'string' ? err : '요청 처리 중 오류가 발생했습니다.');
} finally {
setIsProcessing(false);
}
};
return (
<DashboardLayout>
<div className="flex flex-col h-screen overflow-hidden">
{/* 헤더 */}
<div className="bg-white dark:bg-gray-800 shadow p-4">
<div className="flex justify-between items-center">
<h1 className="text-xl font-bold">AI 채팅</h1>
<div className="flex items-center">
<label htmlFor="modelSelect" className="mr-2 text-sm">모델:</label>
<select
id="modelSelect"
value={selectedModel}
onChange={handleModelChange}
className="bg-white dark:bg-gray-700 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm py-1 px-3 text-sm focus:outline-none focus:ring-blue-500 focus:border-blue-500"
disabled={isProcessing}
>
<optgroup label="OpenAI">
<option value="gpt-4o">GPT-4o</option>
<option value="gpt-4-turbo">GPT-4 Turbo</option>
<option value="gpt-3.5-turbo">GPT-3.5 Turbo</option>
</optgroup>
<optgroup label="Anthropic">
<option value="claude-3-5-sonnet-20240620">Claude 3.5 Sonnet</option>
<option value="claude-3-opus-20240229">Claude 3 Opus</option>
<option value="claude-3-haiku-20240307">Claude 3 Haiku</option>
</optgroup>
</select>
</div>
</div>
</div>
{/* 메시지 영역 */}
<div className="flex-1 overflow-y-auto p-4 bg-gray-50 dark:bg-gray-900">
{error && (
<div className="mb-4 bg-red-50 dark:bg-red-900/30 border-l-4 border-red-500 p-4">
<p className="text-sm text-red-700 dark:text-red-300">{error}</p>
</div>
)}
<div className="space-y-4">
{messages.map((msg, index) => (
<div
key={index}
className={`flex ${msg.role === 'user' ? 'justify-end' : 'justify-start'}`}
>
<div
className={`max-w-3xl rounded-lg p-4 ${
msg.role === 'user'
? 'bg-blue-600 text-white rounded-br-none'
: 'bg-white dark:bg-gray-800 shadow rounded-bl-none'
}`}
>
<div className="whitespace-pre-wrap">{msg.content}</div>
{msg.model && msg.role === 'assistant' && (
<div className="mt-2 text-xs opacity-75 flex justify-end">
{msg.model === 'system' ? 'ZAVIS Engine' : msg.model}
</div>
)}
</div>
</div>
))}
<div ref={messagesEndRef} />
</div>
</div>
{/* 입력 영역 */}
<div className="bg-white dark:bg-gray-800 p-4 border-t border-gray-200 dark:border-gray-700">
<form onSubmit={handleSubmit} className="flex space-x-2">
<input
type="text"
value={input}
onChange={handleInputChange}
placeholder={isProcessing ? "AI가 응답을 생성 중입니다..." : "메시지를 입력하세요..."}
disabled={isProcessing}
className="flex-1 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm py-2 px-3 focus:outline-none focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:text-white"
/>
<button
type="submit"
disabled={isProcessing || input.trim() === ''}
className="inline-flex items-center px-4 py-2 border border-transparent rounded-md shadow-sm text-sm font-medium text-white bg-blue-600 hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-blue-500 disabled:opacity-50"
>
{isProcessing ? (
<svg className="animate-spin -ml-1 mr-2 h-4 w-4 text-white" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
<circle className="opacity-25" cx="12" cy="12" r="10" stroke="currentColor" strokeWidth="4"></circle>
<path className="opacity-75" fill="currentColor" d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"></path>
</svg>
) : (
'전송'
)}
</button>
</form>
</div>
</div>
</DashboardLayout>
);
};
export default ChatInterface;
// src/pages/Profile.js - 프로필 관리 페이지
import React, { useState, useContext } from 'react';
import DashboardLayout from '../components/DashboardLayout';
import { AuthContext } from '../contexts/AuthContext';
import api from '../services/api';
const Profile = () => {
const { user, setError: setAuthError } = useContext(AuthContext);
const [name, setName] = useState(user?.name || '');
const [currentPassword, setCurrentPassword] = useState('');
const [newPassword, setNewPassword] = useState('');
const [confirmPassword, setConfirmPassword] = useState('');
const [openaiKey, setOpenaiKey] = useState('');
const [claudeKey, setClaudeKey] = useState('');
const [profileSuccess, setProfileSuccess] = useState(null);
const [passwordSuccess, setPasswordSuccess] = useState(null);
const [apiKeysSuccess, setApiKeysSuccess] = useState(null);
const [profileError, setProfileError] = useState(null);
const [passwordError, setPasswordError] = useState(null);
const [apiKeysError, setApiKeysError] = useState(null);
const [isLoading, setIsLoading] = useState({
profile: false,
password: false,
apiKeys: false
});
// 프로필 정보 업데이트
const handleProfileUpdate = async (e) => {
e.preventDefault();
setProfileError(null);
setProfileSuccess(null);
setIsLoading(prev => ({ ...prev, profile: true }));
try {
await api.put('/auth/profile', { name });
setProfileSuccess('프로필 정보가 성공적으로 업데이트되었습니다.');
} catch (err) {
setProfileError(err.response?.data?.error || '프로필 업데이트 중 오류가 발생했습니다.');
} finally {
setIsLoading(prev => ({ ...prev, profile: false }));
}
};
// 비밀번호 변경
const handlePasswordChange = async (e) => {
e.preventDefault();
setPasswordError(null);
setPasswordSuccess(null);
if (newPassword !== confirmPassword) {
setPasswordError('새 비밀번호가 일치하지 않습니다.');
return;
}
setIsLoading(prev => ({ ...prev, password: true }));
try {
await api.put('/auth/password', { currentPassword, newPassword });
setPasswordSuccess('비밀번호가 성공적으로 변경되었습니다.');
setCurrentPassword('');
setNewPassword('');
setConfirmPassword('');
} catch (err) {
setPasswordError(err.response?.data?.error || '비밀번호 변경 중 오류가 발생했습니다.');
} finally {
setIsLoading(prev => ({ ...prev, password: false }));
}
};
// API 키 저장
const handleApiKeysSave = async (e) => {
e.preventDefault();
setApiKeysError(null);
setApiKeysSuccess(null);
setIsLoading(prev => ({ ...prev, apiKeys: true }));
try {
await api.put('/auth/api-keys', {
openaiKey: openaiKey || undefined,
claudeKey: claudeKey || undefined
});
setApiKeysSuccess('API 키가 성공적으로 저장되었습니다.');
// 입력 필드 마스킹 처리
setOpenaiKey(openaiKey ? '••••••••' : '');
setClaudeKey(claudeKey ? '••••••••' : '');
} catch (err) {
setApiKeysError(err.response?.data?.error || 'API 키 저장 중 오류가 발생했습니다.');
} finally {
setIsLoading(prev => ({ ...prev, apiKeys: false }));
}
};
return (
<DashboardLayout>
<div className="p-6 max-w-4xl mx-auto">
<h1 className="text-2xl font-bold mb-8">프로필 관리</h1>
<div className="space-y-8">
{/* 프로필 정보 */}
<div className="bg-white dark:bg-gray-800 shadow rounded-lg p-6">
<h2 className="text-xl font-semibold mb-4">프로필 정보</h2>
{profileSuccess && (
<div className="mb-4 bg-green-50 dark:bg-green-900/30 border-l-4 border-green-500 p-4">
<p className="text-sm text-green-700 dark:text-green-300">{profileSuccess}</p>
</div>
)}
{profileError && (
<div className="mb-4 bg-red-50 dark:bg-red-900/30 border-l-4 border-red-500 p-4">
<p className="text-sm text-red-700 dark:text-red-300">{profileError}</p>
</div>
)}
<form onSubmit={handleProfileUpdate} className="space-y-4">
<div>
<label htmlFor="email" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
이메일 주소
</label>
<div className="mt-1">
<input
id="email"
type="email"
value={user?.email || ''}
readOnly
className="bg-gray-100 dark:bg-gray-700 appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm text-gray-500 dark:text-gray-400 sm:text-sm"
/>
<p className="mt-1 text-xs text-gray-500 dark:text-gray-400">이메일 주소는 변경할 수 없습니다.</p>
</div>
</div>
<div>
<label htmlFor="name" className="block text-sm font-medium text-gray-700 dark:text-gray-300">
이름
</label>
<div className="mt-1">
<input
id="name"
type="text"
value={name}
onChange={(e) => setName(e.target.value)}
required
className="appearance-none block w-full px-3 py-2 border border-gray-300 dark:border-gray-600 rounded-md shadow-sm placeholder-gray-400 focus:outline-none focus:ring-blue-500 focus:border-blue-500 sm:text-sm dark:bg-gray-700 dark:text-white"
/>
</div>
</div>
<div>