-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathadmin-kiro.html
More file actions
760 lines (651 loc) · 25.7 KB
/
Copy pathadmin-kiro.html
File metadata and controls
760 lines (651 loc) · 25.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
<!DOCTYPE html>
<html lang="zh-CN">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>AI API 代理 - Kiro 管理面板</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
min-height: 100vh;
padding: 20px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
.header {
background: white;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.header h1 {
color: #667eea;
margin-bottom: 10px;
}
.card {
background: white;
border-radius: 10px;
padding: 20px;
margin-bottom: 20px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.card h2 {
color: #333;
margin-bottom: 15px;
border-bottom: 2px solid #667eea;
padding-bottom: 10px;
}
.form-group {
margin-bottom: 15px;
}
.form-group label {
display: block;
margin-bottom: 5px;
color: #333;
font-weight: 500;
}
.form-group input,
.form-group select,
.form-group textarea {
width: 100%;
padding: 10px;
border: 1px solid #ddd;
border-radius: 5px;
font-size: 14px;
}
.form-group textarea {
min-height: 100px;
font-family: 'Courier New', monospace;
}
.btn {
padding: 10px 20px;
border: none;
border-radius: 5px;
font-size: 14px;
cursor: pointer;
transition: all 0.3s;
}
.btn-primary {
background: #667eea;
color: white;
}
.btn-primary:hover {
background: #5568d3;
}
.btn-danger {
background: #f56565;
color: white;
}
.btn-success {
background: #48bb78;
color: white;
}
.auth-type-selector {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
margin-bottom: 20px;
}
.auth-type-btn {
padding: 15px;
border: 2px solid #ddd;
border-radius: 8px;
background: white;
cursor: pointer;
transition: all 0.3s;
text-align: center;
}
.auth-type-btn:hover {
border-color: #667eea;
background: #f7fafc;
}
.auth-type-btn.active {
border-color: #667eea;
background: #edf2f7;
}
.auth-type-btn h3 {
color: #667eea;
margin-bottom: 5px;
}
.auth-type-btn p {
color: #666;
font-size: 12px;
}
.auth-fields {
display: none;
}
.auth-fields.active {
display: block;
}
.account-list {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 15px;
margin-top: 20px;
}
.account-item {
background: #f7fafc;
border: 1px solid #e2e8f0;
border-radius: 8px;
padding: 15px;
border-left: 4px solid #ff9500;
}
.account-item.enabled {
border-left-color: #48bb78;
}
.account-item.disabled {
border-left-color: #f56565;
opacity: 0.7;
}
.account-item h3 {
color: #333;
margin-bottom: 10px;
}
.account-info {
font-size: 13px;
color: #666;
margin-bottom: 5px;
}
.status-badge {
display: inline-block;
padding: 3px 8px;
border-radius: 3px;
font-size: 12px;
font-weight: 500;
}
.status-badge.kiro {
background: #fed7aa;
color: #7c2d12;
}
.usage-bar {
margin-top: 10px;
height: 6px;
background: #e2e8f0;
border-radius: 3px;
overflow: hidden;
}
.usage-bar-fill {
height: 100%;
background: linear-gradient(90deg, #48bb78, #38a169);
transition: width 0.3s;
}
.usage-text {
font-size: 11px;
color: #666;
margin-top: 5px;
}
.alert {
padding: 12px;
border-radius: 5px;
margin-bottom: 15px;
display: none;
}
.alert.success {
background: #c6f6d5;
color: #22543d;
}
.alert.error {
background: #fed7d7;
color: #742a2a;
}
.alert.show {
display: block;
}
.help-text {
font-size: 12px;
color: #666;
margin-top: 5px;
line-height: 1.5;
}
.code-block {
background: #2d3748;
color: #e2e8f0;
padding: 15px;
border-radius: 5px;
font-family: 'Courier New', monospace;
font-size: 13px;
overflow-x: auto;
margin-top: 10px;
}
.tabs {
display: flex;
gap: 10px;
margin-bottom: 20px;
}
.tab {
padding: 10px 20px;
background: white;
border: none;
border-radius: 5px;
cursor: pointer;
transition: all 0.3s;
}
.tab.active {
background: #667eea;
color: white;
}
.tab-content {
display: none;
}
.tab-content.active {
display: block;
}
</style>
</head>
<body>
<div class="container">
<div class="header">
<h1>🚀 AI API 代理 - Kiro 管理面板</h1>
<p>支持 OpenAI、Anthropic、Gemini 和 Amazon Q (Kiro)</p>
</div>
<div class="alert" id="alert"></div>
<div class="tabs">
<button class="tab active" onclick="switchTab('add-kiro')">添加 Kiro 账号</button>
<button class="tab" onclick="switchTab('accounts')">账号管理</button>
<button class="tab" onclick="switchTab('usage')">使用说明</button>
<button class="tab" onclick="switchTab('settings')">设置</button>
</div>
<!-- 添加 Kiro 账号 -->
<div id="add-kiro" class="tab-content active">
<div class="card">
<h2>➕ 添加 Kiro (Amazon Q) 账号</h2>
<div class="auth-type-selector">
<div class="auth-type-btn active" onclick="selectAuthType('sso')">
<h3>SSO Token</h3>
<p>AWS IAM Identity Center</p>
</div>
<div class="auth-type-btn" onclick="selectAuthType('oidc')">
<h3>OIDC</h3>
<p>Builder ID / GitHub / Google</p>
</div>
<div class="auth-type-btn" onclick="selectAuthType('bearer')">
<h3>Bearer Token</h3>
<p>直接使用 Access Token</p>
</div>
</div>
<form id="addKiroForm">
<div class="form-group">
<label>邮箱</label>
<input type="email" id="email" required placeholder="your-email@example.com">
</div>
<div class="form-group">
<label>AWS 区域</label>
<select id="region">
<option value="us-east-1">美东 (us-east-1)</option>
<option value="us-west-2">美西 (us-west-2)</option>
<option value="eu-west-1">欧洲 (eu-west-1)</option>
<option value="ap-northeast-1">东京 (ap-northeast-1)</option>
</select>
</div>
<!-- SSO Token 字段 -->
<div id="sso-fields" class="auth-fields active">
<div class="form-group">
<label>SSO Token</label>
<textarea id="ssoToken" placeholder="eyJ..."></textarea>
<p class="help-text">从 Kiro IDE 配置文件中获取 SSO Token</p>
</div>
</div>
<!-- OIDC 字段 -->
<div id="oidc-fields" class="auth-fields">
<div class="form-group">
<label>Client ID</label>
<input type="text" id="clientId" placeholder="xxxxxxxxxx">
</div>
<div class="form-group">
<label>Client Secret</label>
<input type="text" id="clientSecret" placeholder="xxxxxxxxxx">
</div>
<div class="form-group">
<label>Refresh Token</label>
<textarea id="refreshToken" placeholder="用于自动刷新"></textarea>
</div>
<div class="form-group">
<label>Access Token (可选)</label>
<textarea id="oidcAccessToken" placeholder="如果有当前有效的 token"></textarea>
</div>
<p class="help-text">OIDC 认证支持自动刷新 Token</p>
</div>
<!-- Bearer Token 字段 -->
<div id="bearer-fields" class="auth-fields">
<div class="form-group">
<label>Access Token</label>
<textarea id="bearerToken" placeholder="eyJ..."></textarea>
<p class="help-text">直接使用 Bearer Token,无法自动刷新</p>
</div>
<div class="form-group">
<label>过期时间 (可选)</label>
<input type="datetime-local" id="expiresAt">
</div>
</div>
<button type="submit" class="btn btn-primary">添加账号</button>
</form>
</div>
<div class="card">
<h2>📖 如何获取 Kiro Token?</h2>
<p style="margin-bottom: 10px;">Kiro Token 位于配置文件中,路径取决于操作系统:</p>
<h3 style="color: #667eea; margin: 15px 0 10px;">Windows:</h3>
<div class="code-block">%APPDATA%\Code\User\globalStorage\amazon.q-for-vscode\sso\token_cache.json</div>
<h3 style="color: #667eea; margin: 15px 0 10px;">macOS:</h3>
<div class="code-block">~/Library/Application Support/Code/User/globalStorage/amazon.q-for-vscode/sso/token_cache.json</div>
<h3 style="color: #667eea; margin: 15px 0 10px;">Linux:</h3>
<div class="code-block">~/.config/Code/User/globalStorage/amazon.q-for-vscode/sso/token_cache.json</div>
<p style="margin-top: 15px; color: #666;">打开文件后,查找 <code>accessToken</code> 或 <code>ssoToken</code> 字段</p>
</div>
</div>
<!-- 账号管理 -->
<div id="accounts" class="tab-content">
<div class="card">
<h2>📋 Kiro 账号列表</h2>
<div id="kiroAccountList" class="account-list">
<div class="loading">正在加载...</div>
</div>
</div>
</div>
<!-- 使用说明 -->
<div id="usage" class="tab-content">
<div class="card">
<h2>📖 Kiro API 使用说明</h2>
<h3 style="color: #667eea; margin: 15px 0 10px;">1. 流式对话</h3>
<div class="code-block">curl https://your-worker.workers.dev/kiro/api/v1/streaming-conversations \
-H "Content-Type: application/json" \
-d '{
"conversationState": {
"currentMessage": {
"userInputMessage": {
"content": "Hello, how are you?"
}
},
"chatTriggerType": "MANUAL"
}
}'</div>
<h3 style="color: #667eea; margin: 15px 0 10px;">2. 在代码中使用</h3>
<div class="code-block">// 使用 Kiro API
const response = await fetch(
'https://your-worker.workers.dev/kiro/api/v1/streaming-conversations',
{
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
conversationState: {
currentMessage: {
userInputMessage: { content: 'Your question here' }
},
chatTriggerType: 'MANUAL'
}
})
}
);</div>
<h3 style="color: #667eea; margin: 15px 0 10px;">3. 可用端点</h3>
<ul style="line-height: 2; color: #666;">
<li><code>/kiro/api/v1/streaming-conversations</code> - 流式对话</li>
<li><code>/kiro/api/v1/conversations</code> - 对话列表</li>
<li><code>/kiro/api/v1/generate-plan</code> - 生成计划</li>
<li><code>/kiro/api/v1/generate-code-snippet</code> - 生成代码片段</li>
<li><code>/kiro/api/v1/models</code> - 可用模型列表</li>
</ul>
</div>
</div>
<!-- 设置 -->
<div id="settings" class="tab-content">
<div class="card">
<h2>⚙️ 系统设置</h2>
<div class="form-group">
<label>管理员密钥</label>
<input type="password" id="adminKey" placeholder="用于 API 调用认证">
<p class="help-text">此密钥需要与 Worker 环境变量 ADMIN_KEY 一致</p>
</div>
<div class="form-group">
<label>Worker 地址</label>
<input type="text" id="workerUrl" placeholder="https://your-worker.workers.dev">
</div>
<button class="btn btn-success" onclick="saveSettings()">保存设置</button>
<button class="btn btn-danger" onclick="logout()" style="margin-left: 10px;">退出登录</button>
</div>
</div>
</div>
<script>
let config = {
adminKey: localStorage.getItem('adminKey') || '',
workerUrl: localStorage.getItem('workerUrl') || window.location.origin
};
let currentAuthType = 'sso';
let isAuthenticated = false;
// 初始化
document.addEventListener('DOMContentLoaded', () => {
checkAuthentication();
});
// 检查认证状态
function checkAuthentication() {
const savedPassword = localStorage.getItem('adminPassword');
if (!savedPassword) {
showLoginPrompt();
} else {
// 验证保存的密码
config.adminKey = savedPassword;
verifyAuthentication();
}
}
// 显示登录提示
function showLoginPrompt() {
const password = prompt('请输入管理员密码以访问后台:');
if (!password) {
document.body.innerHTML = '<div style="text-align:center;padding:50px;"><h2>访问被拒绝</h2><p>需要管理员密码才能访问此页面</p></div>';
return;
}
config.adminKey = password;
localStorage.setItem('adminPassword', password);
verifyAuthentication();
}
// 验证认证
async function verifyAuthentication() {
try {
// 尝试获取账号列表来验证密码
const response = await fetch(`${config.workerUrl}/admin/accounts`, {
headers: {
'Authorization': `Bearer ${config.adminKey}`
}
});
if (response.ok) {
isAuthenticated = true;
loadSettings();
loadKiroAccounts();
} else {
// 密码错误
localStorage.removeItem('adminPassword');
alert('密码错误,请重新输入');
showLoginPrompt();
}
} catch (error) {
console.error('认证失败:', error);
alert('无法连接到服务器,请检查 Worker 地址');
loadSettings();
}
}
// 切换标签页
function switchTab(tabName) {
document.querySelectorAll('.tab').forEach(tab => tab.classList.remove('active'));
document.querySelectorAll('.tab-content').forEach(content => content.classList.remove('active'));
event.target.classList.add('active');
document.getElementById(tabName).classList.add('active');
if (tabName === 'accounts') {
loadKiroAccounts();
}
}
// 选择认证类型
function selectAuthType(type) {
currentAuthType = type;
// 更新按钮状态
document.querySelectorAll('.auth-type-btn').forEach(btn => {
btn.classList.remove('active');
});
event.currentTarget.classList.add('active');
// 显示对应字段
document.querySelectorAll('.auth-fields').forEach(fields => {
fields.classList.remove('active');
});
document.getElementById(`${type}-fields`).classList.add('active');
}
// 加载设置
function loadSettings() {
document.getElementById('adminKey').value = config.adminKey;
document.getElementById('workerUrl').value = config.workerUrl;
}
// 保存设置
function saveSettings() {
config.adminKey = document.getElementById('adminKey').value;
config.workerUrl = document.getElementById('workerUrl').value;
localStorage.setItem('adminKey', config.adminKey);
localStorage.setItem('workerUrl', config.workerUrl);
showAlert('设置已保存', 'success');
}
// API 请求
async function apiRequest(endpoint, options = {}) {
const url = `${config.workerUrl}${endpoint}`;
const headers = {
'Authorization': `Bearer ${config.adminKey}`,
'Content-Type': 'application/json',
...options.headers
};
try {
const response = await fetch(url, { ...options, headers });
const data = await response.json();
if (!response.ok) {
throw new Error(data.error || '请求失败');
}
return data;
} catch (error) {
showAlert(error.message, 'error');
throw error;
}
}
// 添加 Kiro 账号
document.getElementById('addKiroForm').addEventListener('submit', async (e) => {
e.preventDefault();
const email = document.getElementById('email').value;
const region = document.getElementById('region').value;
let accountData = {
email,
region
};
// 根据认证类型添加字段
if (currentAuthType === 'sso') {
accountData.ssoToken = document.getElementById('ssoToken').value;
} else if (currentAuthType === 'oidc') {
accountData.clientId = document.getElementById('clientId').value;
accountData.clientSecret = document.getElementById('clientSecret').value;
accountData.refreshToken = document.getElementById('refreshToken').value;
const accessToken = document.getElementById('oidcAccessToken').value;
if (accessToken) accountData.accessToken = accessToken;
} else if (currentAuthType === 'bearer') {
accountData.accessToken = document.getElementById('bearerToken').value;
const expiresAt = document.getElementById('expiresAt').value;
if (expiresAt) {
accountData.expiresAt = new Date(expiresAt).getTime();
}
}
try {
await apiRequest('/admin/accounts/kiro', {
method: 'POST',
body: JSON.stringify(accountData)
});
showAlert('Kiro 账号添加成功', 'success');
e.target.reset();
loadKiroAccounts();
} catch (error) {
// Error already shown
}
});
// 加载 Kiro 账号列表
async function loadKiroAccounts() {
try {
const accounts = await apiRequest('/admin/accounts');
const kiroAccounts = accounts.filter(acc => acc.provider === 'kiro');
displayKiroAccounts(kiroAccounts);
} catch (error) {
document.getElementById('kiroAccountList').innerHTML =
'<div class="loading">加载失败,请检查设置</div>';
}
}
// 显示 Kiro 账号
function displayKiroAccounts(accounts) {
const container = document.getElementById('kiroAccountList');
if (accounts.length === 0) {
container.innerHTML = '<div class="loading">暂无 Kiro 账号</div>';
return;
}
container.innerHTML = accounts.map(account => {
const usage = account.usage || { currentMonth: 0, limit: 100000 };
const usagePercent = (usage.currentMonth / usage.limit * 100).toFixed(1);
return `
<div class="account-item ${account.enabled ? 'enabled' : 'disabled'}">
<h3>${account.email}</h3>
<div class="account-info">
<span class="status-badge kiro">Kiro (Amazon Q)</span>
</div>
<div class="account-info">区域:${account.region || 'us-east-1'}</div>
<div class="account-info">ID:${account.id}</div>
${account.lastUsed ? `<div class="account-info">最后使用:${new Date(account.lastUsed).toLocaleString()}</div>` : ''}
<div class="usage-bar">
<div class="usage-bar-fill" style="width: ${usagePercent}%"></div>
</div>
<div class="usage-text">
本月使用:${usage.currentMonth.toLocaleString()} / ${usage.limit.toLocaleString()} (${usagePercent}%)
</div>
<div style="margin-top: 10px; display: flex; gap: 10px;">
<button class="btn btn-success" onclick="refreshKiroToken('${account.id}')">刷新Token</button>
<button class="btn btn-danger" onclick="deleteAccount('${account.id}')">删除</button>
</div>
</div>
`;
}).join('');
}
// 刷新 Token
async function refreshKiroToken(id) {
try {
await apiRequest(`/admin/refresh/${id}`, { method: 'POST' });
showAlert('Token 刷新成功', 'success');
loadKiroAccounts();
} catch (error) {
// Error already shown
}
}
// 删除账号
async function deleteAccount(id) {
if (!confirm('确定要删除此 Kiro 账号吗?')) return;
try {
await apiRequest(`/admin/accounts/${id}`, { method: 'DELETE' });
showAlert('账号已删除', 'success');
loadKiroAccounts();
} catch (error) {
// Error already shown
}
}
// 显示提示
function showAlert(message, type) {
const alert = document.getElementById('alert');
alert.textContent = message;
alert.className = `alert ${type} show`;
setTimeout(() => {
alert.classList.remove('show');
}, 3000);
}
// 退出登录
function logout() {
if (confirm('确定要退出登录吗?')) {
localStorage.removeItem('adminPassword');
localStorage.removeItem('adminKey');
location.reload();
}
}
</script>
</body>
</html>