Spaces:
Runtime error
Runtime error
File size: 25,215 Bytes
f044ea5 ddac6a2 24caae0 2316bca 24caae0 2316bca fa4edd5 2316bca 24caae0 f044ea5 fa4edd5 24caae0 5def997 24caae0 f044ea5 ddac6a2 fa4edd5 24caae0 fa4edd5 f044ea5 fa4edd5 24caae0 fa4edd5 2316bca 24caae0 fa4edd5 24caae0 fa4edd5 24caae0 fa4edd5 f044ea5 24caae0 f044ea5 24caae0 ddac6a2 fa4edd5 2316bca 24caae0 fa4edd5 ddac6a2 24caae0 fa4edd5 2316bca 24caae0 fa4edd5 24caae0 fa4edd5 24caae0 fa4edd5 5def997 fa4edd5 24caae0 fa4edd5 5def997 fa4edd5 ddac6a2 fa4edd5 2316bca 24caae0 2316bca 24caae0 2316bca 24caae0 2316bca 24caae0 2316bca 24caae0 2316bca 24caae0 2316bca 24caae0 4218b1b 24caae0 4218b1b 24caae0 4218b1b 24caae0 f044ea5 24caae0 f044ea5 24caae0 f044ea5 24caae0 f044ea5 24caae0 f044ea5 24caae0 f044ea5 24caae0 f044ea5 24caae0 f044ea5 24caae0 fa4edd5 ddac6a2 5def997 |
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 |
import { useState, useEffect } from 'react';
function Settings({
profiles,
activeProfileId,
onSaveProfiles,
onChangeActiveProfile,
onCloseSettings,
onSaveSummarizationProfile,
summarizationProfile,
mcpServers,
onSaveMcpServers
}) {
const [localProfiles, setLocalProfiles] = useState(profiles);
const [localSummarizationProfile, setLocalSummarizationProfile] = useState(summarizationProfile || {
id: 'default-summarization-profile',
apiEndpoint: '',
apiKey: '',
model: 'DeepSeek-R1'
});
const [localMcpServers, setLocalMcpServers] = useState(mcpServers || []);
// 当 mcpServers 变化时更新本地状态
useEffect(() => {
console.log('mcpServers changed:', mcpServers);
setLocalMcpServers(mcpServers || []);
}, [mcpServers]);
const [editingProfileId, setEditingProfileId] = useState(activeProfileId);
const [editingMcpServerId, setEditingMcpServerId] = useState(null);
const [isHintExpanded, setIsHintExpanded] = useState(false);
const [isMcpHintExpanded, setIsMcpHintExpanded] = useState(false);
const [builtInServers, setBuiltInServers] = useState([]);
const [isLoadingServers, setIsLoadingServers] = useState(false);
const [serverTestResult, setServerTestResult] = useState(null);
const [isTestingConnection, setIsTestingConnection] = useState(false);
const editingProfile = localProfiles.find(p => p.id === editingProfileId) || localProfiles[0];
const editingMcpServer = localMcpServers.find(s => s.id === editingMcpServerId) || null;
// 获取可用的内置 MCP 服务器
useEffect(() => {
const fetchBuiltInServers = async () => {
setIsLoadingServers(true);
try {
console.log('Fetching built-in MCP servers...');
const response = await fetch('/api/mcp/servers/available', {
method: 'GET',
headers: {
'Cache-Control': 'no-cache',
'Pragma': 'no-cache'
}
});
if (response.ok) {
const data = await response.json();
console.log('Received built-in MCP servers:', data);
setBuiltInServers(data.servers || []);
} else {
console.error('Failed to fetch built-in MCP servers:', response.status, response.statusText);
// 尝试读取错误消息
const errorText = await response.text();
console.error('Error details:', errorText);
}
} catch (error) {
console.error('Error fetching built-in MCP servers:', error);
} finally {
setIsLoadingServers(false);
}
};
fetchBuiltInServers();
// 每 5 秒刷新一次服务器状态
const intervalId = setInterval(fetchBuiltInServers, 5000);
// 清理定时器
return () => clearInterval(intervalId);
}, []);
const handleProfileChange = (updatedProfile) => {
setLocalProfiles(localProfiles.map(p =>
p.id === updatedProfile.id ? updatedProfile : p
));
};
const handleSummarizationProfileChange = (updatedProfile) => {
setLocalSummarizationProfile(updatedProfile);
};
const handleMcpServerChange = (updatedServer) => {
setLocalMcpServers(localMcpServers.map(s =>
s.id === updatedServer.id ? updatedServer : s
));
};
const handleAddProfile = () => {
const newId = `profile-${Date.now()}`;
const newProfile = {
id: newId,
name: `New Profile ${localProfiles.length + 1}`,
apiEndpoint: '',
apiKey: '',
model: 'DeepSeek-R1'
};
const updatedProfiles = [...localProfiles, newProfile];
setLocalProfiles(updatedProfiles);
setEditingProfileId(newId);
};
const handleDeleteProfile = (profileId) => {
if (localProfiles.length <= 1) {
alert("Cannot delete the last profile");
return;
}
const updatedProfiles = localProfiles.filter(p => p.id !== profileId);
setLocalProfiles(updatedProfiles);
if (editingProfileId === profileId) {
setEditingProfileId(updatedProfiles[0].id);
}
};
// 测试 MCP 服务器连接
const testMcpServerConnection = async (endpoint, authToken) => {
if (!endpoint) return;
setIsTestingConnection(true);
setServerTestResult(null);
try {
const response = await fetch('/api/mcp/servers/test-connection', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ endpoint, authToken })
});
const result = await response.json();
setServerTestResult(result);
return result.success;
} catch (error) {
console.error('Error testing MCP server connection:', error);
setServerTestResult({
success: false,
message: `Connection error: ${error.message}`,
error: error.message
});
return false;
} finally {
setIsTestingConnection(false);
}
};
// 启动内置 MCP 服务器
const startBuiltInServer = async (serverId) => {
try {
const response = await fetch('/api/mcp/servers/start', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ serverId })
});
const result = await response.json();
if (result.success) {
// 检查是否已经添加了这个服务器
const existingServer = localMcpServers.find(s =>
s.isBuiltIn && s.builtInId === serverId
);
if (!existingServer) {
// 添加到本地 MCP 服务器列表
const builtInServer = builtInServers.find(s => s.id === serverId);
const newServer = {
id: `built-in-${serverId}-${Date.now()}`,
name: builtInServer ? builtInServer.name : `Built-in Server ${serverId}`,
endpoint: result.endpoint,
authToken: '',
description: builtInServer ? builtInServer.description : 'Built-in MCP server',
isBuiltIn: true,
builtInId: serverId,
isRunning: true
};
const updatedServers = [...localMcpServers, newServer];
setLocalMcpServers(updatedServers);
setEditingMcpServerId(newServer.id);
} else {
// 更新现有服务器的端点
const updatedServers = localMcpServers.map(s =>
s.id === existingServer.id
? { ...s, endpoint: result.endpoint, isRunning: true }
: s
);
setLocalMcpServers(updatedServers);
setEditingMcpServerId(existingServer.id);
}
// 刷新内置服务器列表
const response = await fetch('/api/mcp/servers/available');
if (response.ok) {
const data = await response.json();
setBuiltInServers(data.servers || []);
}
return true;
} else {
console.error('Failed to start built-in MCP server:', result.error);
return false;
}
} catch (error) {
console.error('Error starting built-in MCP server:', error);
return false;
}
};
// 停止内置 MCP 服务器
const stopBuiltInServer = async (serverId) => {
try {
const response = await fetch('/api/mcp/servers/stop', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ serverId })
});
const result = await response.json();
if (result.success) {
// 更新内置服务器的状态
const updatedServers = localMcpServers.map(s =>
s.isBuiltIn && s.builtInId === serverId
? { ...s, isRunning: false }
: s
);
setLocalMcpServers(updatedServers);
// 刷新内置服务器列表
const response = await fetch('/api/mcp/servers/available');
if (response.ok) {
const data = await response.json();
setBuiltInServers(data.servers || []);
}
return true;
} else {
console.error('Failed to stop built-in MCP server:', result.error);
return false;
}
} catch (error) {
console.error('Error stopping built-in MCP server:', error);
return false;
}
};
const handleAddMcpServer = () => {
const newId = `mcp-server-${Date.now()}`;
const newServer = {
id: newId,
name: `MCP Server ${localMcpServers.length + 1}`,
endpoint: '',
authToken: '',
description: '',
isBuiltIn: false
};
const updatedServers = [...localMcpServers, newServer];
setLocalMcpServers(updatedServers);
setEditingMcpServerId(newId);
};
const handleDeleteMcpServer = (serverId) => {
const updatedServers = localMcpServers.filter(s => s.id !== serverId);
setLocalMcpServers(updatedServers);
if (editingMcpServerId === serverId) {
setEditingMcpServerId(updatedServers.length > 0 ? updatedServers[0].id : null);
}
};
const handleSubmit = (e) => {
e.preventDefault();
onSaveProfiles(localProfiles);
onSaveSummarizationProfile(localSummarizationProfile);
if (onSaveMcpServers) {
onSaveMcpServers(localMcpServers);
}
onChangeActiveProfile(editingProfileId);
onCloseSettings();
};
return (
<div className="settings-panel">
<h2>Settings</h2>
<div className="profiles-section">
<div className="profiles-header">
<h3>Chat Profiles</h3>
<button
type="button"
className="add-profile-button"
onClick={handleAddProfile}
>
+ Add Profile
</button>
</div>
<div className="profiles-list">
{localProfiles.map(profile => (
<div
key={profile.id}
className={`profile-item ${profile.id === editingProfileId ? 'active' : ''}`}
onClick={() => setEditingProfileId(profile.id)}
>
<span>{profile.name}</span>
{localProfiles.length > 1 && (
<button
className="delete-profile-button"
onClick={(e) => {
e.stopPropagation();
handleDeleteProfile(profile.id);
}}
>
×
</button>
)}
</div>
))}
</div>
</div>
<form onSubmit={handleSubmit}>
{editingProfile && (
<div className="current-profile-section">
<h3>Edit Chat Profile: {editingProfile.name}</h3>
<div className="setting-item">
<label>Profile Name:</label>
<input
type="text"
value={editingProfile.name}
onChange={(e) => handleProfileChange({
...editingProfile,
name: e.target.value
})}
placeholder="Enter profile name"
/>
</div>
<div className="setting-item">
<label>API Endpoint:</label>
<input
type="text"
value={editingProfile.apiEndpoint}
onChange={(e) => handleProfileChange({
...editingProfile,
apiEndpoint: e.target.value
})}
placeholder="Enter API endpoint"
/>
<div className="setting-hint">
<button
type="button"
className="hint-toggle"
onClick={() => setIsHintExpanded(!isHintExpanded)}
>
{isHintExpanded ? 'Hide' : 'Show'} API Endpoint Format Examples
</button>
<div className={`hint-content ${isHintExpanded ? 'expanded' : ''}`}>
<p>API Endpoint format examples:</p>
<ul>
<li>Ends with / → /chat/completions will be appended</li>
<li>Ends with # → # will be removed</li>
<li>Other cases → /v1/chat/completions will be appended</li>
</ul>
</div>
</div>
</div>
<div className="setting-item">
<label>API Key:</label>
<input
type="password"
value={editingProfile.apiKey}
onChange={(e) => handleProfileChange({
...editingProfile,
apiKey: e.target.value
})}
placeholder="Enter your API key"
/>
</div>
<div className="setting-item">
<label>Model:</label>
<input
type="text"
value={editingProfile.model}
onChange={(e) => handleProfileChange({
...editingProfile,
model: e.target.value
})}
placeholder="Enter model name (e.g., DeepSeek-R1)"
/>
</div>
</div>
)}
<div className="profiles-section">
<h3>Summarization Profile</h3>
<div className="current-profile-section">
<div className="setting-item">
<label>API Endpoint:</label>
<input
type="text"
value={localSummarizationProfile.apiEndpoint}
onChange={(e) => handleSummarizationProfileChange({
...localSummarizationProfile,
apiEndpoint: e.target.value
})}
placeholder="Enter API endpoint"
/>
</div>
<div className="setting-item">
<label>API Key:</label>
<input
type="password"
value={localSummarizationProfile.apiKey}
onChange={(e) => handleSummarizationProfileChange({
...localSummarizationProfile,
apiKey: e.target.value
})}
placeholder="Enter your API key"
/>
</div>
<div className="setting-item">
<label>Model:</label>
<input
type="text"
value={localSummarizationProfile.model}
onChange={(e) => handleSummarizationProfileChange({
...localSummarizationProfile,
model: e.target.value
})}
placeholder="Enter model name (e.g., DeepSeek-R1)"
/>
</div>
</div>
</div>
{/* MCP Servers Section */}
<div className="profiles-section">
<div className="profiles-header">
<h3>MCP Servers</h3>
<button
type="button"
className="add-profile-button"
onClick={handleAddMcpServer}
>
+ Add MCP Server
</button>
</div>
{/* Built-in MCP Servers */}
<div className="built-in-servers-section">
<h4>Built-in MCP Servers</h4>
<div className="built-in-servers-list">
{isLoadingServers ? (
<div className="loading-indicator">Loading built-in servers...</div>
) : builtInServers.length > 0 ? (
builtInServers.map(server => {
// 检查这个内置服务器是否已经添加到用户的服务器列表中
const isAdded = localMcpServers.some(s => s.isBuiltIn && s.builtInId === server.id);
return (
<div key={server.id} className="built-in-server-item">
<div className="server-info">
<span className="server-name">{server.name}</span>
<span className={`server-status ${server.isRunning ? 'running' : 'stopped'}`}>
{server.isRunning ? 'Running' : 'Stopped'}
</span>
</div>
<div className="server-actions">
{isAdded ? (
<button
className="server-action-button"
onClick={() => {
const addedServer = localMcpServers.find(s => s.isBuiltIn && s.builtInId === server.id);
if (addedServer) {
setEditingMcpServerId(addedServer.id);
}
}}
>
View
</button>
) : (
<button
className="server-action-button"
onClick={() => startBuiltInServer(server.id)}
disabled={server.isRunning}
>
Add & Start
</button>
)}
{server.isRunning ? (
<button
className="server-action-button stop"
onClick={() => stopBuiltInServer(server.id)}
>
Stop
</button>
) : (
<button
className="server-action-button start"
onClick={() => startBuiltInServer(server.id)}
>
Start
</button>
)}
</div>
</div>
);
})
) : (
<div className="empty-state">
<p>No built-in MCP servers available.</p>
</div>
)}
</div>
</div>
{/* User-added MCP Servers */}
<h4>Your MCP Servers</h4>
<div className="profiles-list">
{localMcpServers.map(server => (
<div
key={server.id}
className={`profile-item ${server.id === editingMcpServerId ? 'active' : ''} ${server.isBuiltIn && server.isRunning ? 'running' : ''}`}
onClick={() => setEditingMcpServerId(server.id)}
>
<span>{server.name}</span>
{server.isBuiltIn && server.isRunning && (
<span className="server-badge running">Running</span>
)}
{server.isBuiltIn && !server.isRunning && (
<span className="server-badge stopped">Stopped</span>
)}
<button
className="delete-profile-button"
onClick={(e) => {
e.stopPropagation();
handleDeleteMcpServer(server.id);
}}
>
×
</button>
</div>
))}
{localMcpServers.length === 0 && (
<div className="empty-state">
<p>No MCP servers added yet. Add a server to enable Model Context Protocol capabilities.</p>
</div>
)}
</div>
{editingMcpServer && (
<div className="current-profile-section">
<h3>Edit MCP Server: {editingMcpServer.name}</h3>
<div className="setting-item">
<label>Server Name:</label>
<input
type="text"
value={editingMcpServer.name}
onChange={(e) => handleMcpServerChange({
...editingMcpServer,
name: e.target.value
})}
placeholder="Enter server name"
disabled={editingMcpServer.isBuiltIn}
/>
</div>
<div className="setting-item">
<label>Endpoint URL:</label>
<div className="input-with-button">
<input
type="text"
value={editingMcpServer.endpoint}
onChange={(e) => handleMcpServerChange({
...editingMcpServer,
endpoint: e.target.value
})}
placeholder="Enter MCP server endpoint URL"
disabled={editingMcpServer.isBuiltIn}
/>
<button
type="button"
className="test-connection-button"
onClick={() => testMcpServerConnection(editingMcpServer.endpoint, editingMcpServer.authToken)}
disabled={!editingMcpServer.endpoint || isTestingConnection}
>
{isTestingConnection ? 'Testing...' : 'Test Connection'}
</button>
</div>
{serverTestResult && (
<div className={`connection-test-result ${serverTestResult.success ? 'success' : 'error'}`}>
{serverTestResult.message}
{serverTestResult.success && serverTestResult.tools && (
<div className="available-tools">
<p>Available tools: {serverTestResult.tools.length}</p>
<ul>
{serverTestResult.tools.map((tool, index) => (
<li key={index}>{tool.name} - {tool.description}</li>
))}
</ul>
</div>
)}
</div>
)}
</div>
<div className="setting-item">
<label>Authentication Token:</label>
<input
type="password"
value={editingMcpServer.authToken}
onChange={(e) => handleMcpServerChange({
...editingMcpServer,
authToken: e.target.value
})}
placeholder="Enter authentication token (if required)"
disabled={editingMcpServer.isBuiltIn}
/>
</div>
<div className="setting-item">
<label>Description:</label>
<textarea
value={editingMcpServer.description}
onChange={(e) => handleMcpServerChange({
...editingMcpServer,
description: e.target.value
})}
placeholder="Enter server description"
rows="3"
disabled={editingMcpServer.isBuiltIn}
/>
</div>
{editingMcpServer.isBuiltIn && (
<div className="built-in-server-controls">
<p className="built-in-server-note">
This is a built-in MCP server managed by the application.
{editingMcpServer.isRunning
? ' It is currently running.'
: ' It is currently stopped.'}
</p>
{editingMcpServer.isRunning ? (
<button
type="button"
className="server-control-button stop"
onClick={() => stopBuiltInServer(editingMcpServer.builtInId)}
>
Stop Server
</button>
) : (
<button
type="button"
className="server-control-button start"
onClick={() => startBuiltInServer(editingMcpServer.builtInId)}
>
Start Server
</button>
)}
</div>
)}
<div className="setting-hint">
<button
type="button"
className="hint-toggle"
onClick={() => setIsMcpHintExpanded(!isMcpHintExpanded)}
>
{isMcpHintExpanded ? 'Hide' : 'Show'} MCP Information
</button>
<div className={`hint-content ${isMcpHintExpanded ? 'expanded' : ''}`}>
<p>Model Context Protocol (MCP) allows AI models to access external tools and data sources.</p>
<ul>
<li>MCP servers provide specialized capabilities to AI models</li>
<li>Each server can offer different tools like web search, data retrieval, etc.</li>
<li>The model will automatically use available MCP servers when needed</li>
</ul>
</div>
</div>
</div>
)}
</div>
<div className="settings-actions">
<button type="submit" className="save-button">Save Settings</button>
<button type="button" className="cancel-button" onClick={onCloseSettings}>Cancel</button>
</div>
</form>
</div>
);
}
export default Settings;
|