-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathapp.js
More file actions
1614 lines (1473 loc) · 66.3 KB
/
app.js
File metadata and controls
1614 lines (1473 loc) · 66.3 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
/* =========================================
API Tester — App Logic
Provider-Centric · AES-GCM · Batch Test
========================================= */
const STORAGE_KEY = 'api-tester-providers';
const CRYPTO_KEY_NAME = 'api-tester-ck';
let currentExportData = { content: '', filename: '' };
let currentLang = 'zh';
let onboardingStep = 0;
let expandedProviders = new Set();
let fetchedModelsCache = []; // temp cache for current fetch
// ══════════════════════════════════
// i18n
// ══════════════════════════════════
const i18n = {
zh: {
hero_title: '测试你的 OpenAI 兼容 API',
hero_desc: '输入 Base URL 和 API Key,一键验证连通性。纯静态页面,密钥加密存储,不离开浏览器。',
security_badge: 'AES-GCM 加密存储 · 密钥不离开浏览器',
config_title: '接口配置',
label_base_url: 'Base URL',
label_api_key: 'API Key',
label_encrypted: '已加密',
label_model: '模型',
label_prompt: '测试 Prompt',
hint_base_url: 'API 提供商的接口地址,通常以 /v1 结尾',
hint_api_key: '你的 API 密钥,保存时会使用 AES-GCM 加密',
hint_model: '手动输入或点击右侧按钮自动获取可用模型',
models_available: '可用模型',
models_fetching: '正在获取模型列表…',
models_none: '未找到可用模型',
models_error: '获取模型列表失败',
testall_title: '批量连通测试',
testall_empty: '没有保存的服务商,请先保存至少一个',
testall_running: '批量测试中…',
testall_done: '批量测试完成',
testall_pass: '通过',
testall_fail: '失败',
testall_fetching: '获取模型…',
testall_testing: '测试中…',
testall_nomodels: '无可用模型',
testall_skip: '跳过(无模型)',
btn_test: '测试连接',
btn_ping: '连通检测',
btn_save: '保存服务商',
btn_testall: '测试全部',
btn_export_all: '导出全部',
btn_import: '导入',
btn_delete_all: '删除全部',
btn_copy: '复制',
btn_download: '下载',
saved_title: '服务商',
empty_saved: '暂无保存的服务商',
result_title: '测试结果',
idle_text: '等待测试…',
loading_text: '正在连接…',
success_text: '连接成功',
error_text: '连接失败',
stat_latency: '延迟',
stat_tokens: 'Tokens',
stat_model: '模型',
export_title: '导出配置',
export_desc: '将当前配置导出为各平台通用格式',
footer_text: '纯静态 · AES-GCM 加密 · 无追踪',
footer_star: '喜欢的话点个',
smart_paste_label: '智能识别',
smart_paste_placeholder: '粘贴配置文本,自动识别 Base URL、API Key 和模型名...',
smart_paste_btn: '识别并填入',
onboarding_skip: '跳过',
onboarding_next: '下一步',
toast_saved: '✓ 已保存(密钥已加密)',
toast_deleted: '✓ 已删除',
toast_deleted_all: '✓ 已清空全部服务商',
toast_loaded: '✓ 已加载',
toast_copied: '✓ 已复制',
toast_imported: '✓ 导入并保存成功',
toast_import_fail: '✗ 导入失败',
toast_need_url: '请填写 Base URL',
toast_need_key: '请填写 API Key',
toast_need_model: '请填写模型名',
toast_need_config: '请至少填写 Base URL 和 API Key',
toast_renamed: '✓ 已重命名',
toast_fetching_models: '正在获取模型列表…',
prompt_name: '为该服务商命名:',
prompt_rename: '输入新名称:',
confirm_delete: '确认删除该服务商及其所有配置?',
confirm_delete_all: '确认删除全部已保存服务商?',
status_ok: '正常',
status_fail: '失败',
ping_dns: 'DNS 解析',
ping_tls: 'HTTPS 连接',
ping_auth: '身份验证',
ping_model: '模型可用性',
ping_ok: '正常',
ping_fail: '失败',
ping_wait: '等待中',
ping_running: '检测中…',
ping_dns_ok: '域名解析成功',
ping_tls_ok: '安全连接已建立',
ping_auth_ok: '认证通过',
ping_model_ok: '模型可用',
provider_models_count: '个模型',
provider_no_models: '尚未获取模型',
provider_refresh: '刷新模型列表',
probe_title: '模型探测',
probe_search: '搜索',
probe_placeholder: '输入模型名称,如 gpt-4o, claude-3...',
probe_hint: '搜索指定模型,查看哪些已保存服务商支持该模型并测试连通性',
probe_no_match: '未在任何服务商中找到匹配模型',
probe_found: '个服务商匹配',
probe_all: '全部探测',
probe_ok: '通',
probe_fail: '不通',
probe_wait: '待测',
probe_running: '探测中…',
probe_need_input: '请输入模型名称',
onboard: [
{ title: '👋 欢迎使用 API Tester', desc: '这是一个纯静态的 OpenAI 兼容 API 测试工具。你的 API Key 使用 AES-GCM 加密存储在浏览器中,不会发送到任何第三方服务器。\n\n让我带你快速了解各个功能。' },
{ title: '① 填写 Base URL', desc: 'API 提供商给你的接口地址。通常格式为 https://xxx.com/v1 。如果不确定,可以先试试填入提供商给的地址。', target: 'field-base-url' },
{ title: '② 填写 API Key', desc: '你的 API 密钥。输入框默认隐藏内容,点击右侧 👁 可切换显示。保存时会自动加密,不会以明文存储。', target: 'field-api-key' },
{ title: '③ 选择模型', desc: '手动填写模型名称,或点击输入框右侧的刷新按钮自动获取提供商的全部可用模型列表。', target: 'field-model' },
{ title: '④ 测试连接', desc: '• 「测试连接」会发送一条消息并获取完整回复\n• 「连通检测」会逐步检查 DNS、HTTPS、认证和模型\n• 「保存服务商」会自动获取所有可用模型并加密保存', target: 'card-actions' },
{ title: '⑤ 管理服务商', desc: '保存的服务商会列在这里,展开可看到全部模型。点击模型即可加载。支持重命名、删除和一键批量测试。还可导入 .json / .env / .toml / .yaml 文件(导入自动保存)。', target: 'card-saved' },
{ title: '⑥ 导出配置', desc: '你可以将当前配置一键导出为 OpenAI .env、Codex CLI、Claude Code、cURL、Python 等多种主流格式。', target: 'card-export' },
{ title: '🎉 准备就绪!', desc: '你已经了解了所有功能。现在就填写你的 API 信息,开始测试吧!\n\n提示:右上角的 ❓ 按钮可以随时重新打开引导。' },
],
},
en: {
hero_title: 'Test Your OpenAI-Compatible API',
hero_desc: 'Enter your Base URL and API Key, one click to verify connectivity. Fully static, keys encrypted locally.',
security_badge: 'AES-GCM Encrypted · Keys Never Leave Browser',
config_title: 'CONFIGURATION',
label_base_url: 'Base URL',
label_api_key: 'API Key',
label_encrypted: 'Encrypted',
label_model: 'Model',
label_prompt: 'Test Prompt',
hint_base_url: 'Your API provider endpoint, usually ending with /v1',
hint_api_key: 'Your API key — saved with AES-GCM encryption',
hint_model: 'Type manually or click the refresh button to auto-fetch',
models_available: 'Available Models',
models_fetching: 'Fetching model list…',
models_none: 'No models found',
models_error: 'Failed to fetch models',
testall_title: 'Batch Connectivity Test',
testall_empty: 'No saved providers. Save at least one first.',
testall_running: 'Running batch test…',
testall_done: 'Batch test complete',
testall_pass: 'Pass',
testall_fail: 'Fail',
testall_fetching: 'Fetching…',
testall_testing: 'Testing…',
testall_nomodels: 'No models available',
testall_skip: 'Skipped (no models)',
btn_test: 'Test',
btn_ping: 'Ping',
btn_save: 'Save Provider',
btn_testall: 'Test All',
btn_export_all: 'Export All',
btn_import: 'Import',
btn_delete_all: 'Delete All',
btn_copy: 'Copy',
btn_download: 'Download',
saved_title: 'PROVIDERS',
empty_saved: 'No saved providers',
result_title: 'RESULTS',
idle_text: 'Waiting for test…',
loading_text: 'Connecting…',
success_text: 'Connected',
error_text: 'Failed',
stat_latency: 'Latency',
stat_tokens: 'Tokens',
stat_model: 'Model',
export_title: 'EXPORT',
export_desc: 'Export current config for various platforms',
footer_text: 'Static · AES-GCM Encrypted · No Tracking',
footer_star: 'Like it? Star on',
smart_paste_label: 'Smart Paste',
smart_paste_placeholder: 'Paste config text to auto-fill Base URL, API Key and Model...',
smart_paste_btn: 'Extract & Fill',
onboarding_skip: 'Skip',
onboarding_next: 'Next',
toast_saved: '✓ Saved (key encrypted)',
toast_deleted: '✓ Deleted',
toast_deleted_all: '✓ All providers cleared',
toast_loaded: '✓ Loaded',
toast_copied: '✓ Copied',
toast_imported: '✓ Imported & saved',
toast_import_fail: '✗ Import failed',
toast_need_url: 'Base URL required',
toast_need_key: 'API Key required',
toast_need_model: 'Model required',
toast_need_config: 'Need at least Base URL and API Key',
toast_renamed: '✓ Renamed',
toast_fetching_models: 'Fetching models…',
prompt_name: 'Name this provider:',
prompt_rename: 'Enter new name:',
confirm_delete: 'Delete this provider and all its config?',
confirm_delete_all: 'Delete all saved providers?',
status_ok: 'OK',
status_fail: 'FAIL',
ping_dns: 'DNS Resolution',
ping_tls: 'HTTPS Connection',
ping_auth: 'Authentication',
ping_model: 'Model Availability',
ping_ok: 'OK',
ping_fail: 'Failed',
ping_wait: 'Waiting',
ping_running: 'Checking…',
ping_dns_ok: 'Domain resolved',
ping_tls_ok: 'Secure connection established',
ping_auth_ok: 'Authenticated',
ping_model_ok: 'Model available',
provider_models_count: ' models',
provider_no_models: 'Models not fetched',
provider_refresh: 'Refresh model list',
probe_title: 'Model Probe',
probe_search: 'Search',
probe_placeholder: 'Enter model name, e.g. gpt-4o, claude-3...',
probe_hint: 'Search a model across all saved providers and test connectivity',
probe_no_match: 'No matching model found in any provider',
probe_found: ' providers match',
probe_all: 'Probe All',
probe_ok: 'OK',
probe_fail: 'Fail',
probe_wait: 'Wait',
probe_running: 'Probing…',
probe_need_input: 'Enter a model name',
onboard: [
{ title: '👋 Welcome to API Tester', desc: 'A fully static OpenAI-compatible API testing tool. Your API Key is encrypted with AES-GCM and stored only in your browser.\n\nLet me walk you through the features.' },
{ title: '① Base URL', desc: 'The endpoint URL from your API provider. Usually something like https://api.openai.com/v1. If unsure, paste the URL your provider gave you.', target: 'field-base-url' },
{ title: '② API Key', desc: 'Your secret API key. The input is masked by default — click the 👁 icon to toggle visibility. Keys are encrypted before saving.', target: 'field-api-key' },
{ title: '③ Model', desc: 'Type a model name manually, or click the refresh icon to auto-fetch ALL available models from the provider.', target: 'field-model' },
{ title: '④ Test & Save', desc: '• "Test" sends a message and gets a full response\n• "Ping" runs a step-by-step connectivity check\n• "Save Provider" auto-fetches all models and saves encrypted', target: 'card-actions' },
{ title: '⑤ Providers', desc: 'Saved providers appear here with all their models. Click a model to load it. You can rename, delete, refresh models, or batch-test all providers. Importing a config auto-saves it.', target: 'card-saved' },
{ title: '⑥ Export', desc: 'Export your config in multiple formats: OpenAI .env, Codex CLI, Claude Code, cURL, Python and more.', target: 'card-export' },
{ title: '🎉 Ready!', desc: 'You\'re all set! Fill in your API details and start testing.\n\nTip: Click the ❓ button in the header to restart this guide anytime.' },
],
}
};
function t(key) { return i18n[currentLang][key] || key; }
function applyLang() {
document.querySelectorAll('[data-i18n]').forEach(el => {
const key = el.getAttribute('data-i18n');
if (i18n[currentLang][key]) el.textContent = i18n[currentLang][key];
});
document.querySelectorAll('[data-i18n-title]').forEach(el => {
const key = el.getAttribute('data-i18n-title');
if (i18n[currentLang][key]) el.title = i18n[currentLang][key];
});
document.querySelectorAll('[data-i18n-placeholder]').forEach(el => {
const key = el.getAttribute('data-i18n-placeholder');
if (i18n[currentLang][key]) el.placeholder = i18n[currentLang][key];
});
document.getElementById('lang-label').textContent = currentLang === 'zh' ? 'EN' : '中文';
document.documentElement.lang = currentLang === 'zh' ? 'zh-CN' : 'en';
localStorage.setItem('api-tester-lang', currentLang);
}
function toggleLang() {
currentLang = currentLang === 'zh' ? 'en' : 'zh';
applyLang();
renderProviders();
}
// ══════════════════════════════════
// INIT
// ══════════════════════════════════
document.addEventListener('DOMContentLoaded', async () => {
currentLang = localStorage.getItem('api-tester-lang') || 'zh';
applyLang();
await initCrypto();
migrateOldData();
renderProviders();
setupListeners();
loadLastUsed();
if (!localStorage.getItem('api-tester-onboarded')) {
setTimeout(() => startOnboarding(), 600);
}
});
function setupListeners() {
document.getElementById('toggle-key').addEventListener('click', () => {
const inp = document.getElementById('api-key');
inp.type = inp.type === 'password' ? 'text' : 'password';
});
document.getElementById('import-file').addEventListener('change', handleImportFile);
document.getElementById('export-modal').addEventListener('click', e => {
if (e.target === e.currentTarget) closeModal();
});
document.getElementById('testall-modal').addEventListener('click', e => {
if (e.target === e.currentTarget) closeTestAllModal();
});
document.addEventListener('keydown', e => {
if (e.key === 'Escape') { closeModal(); closeTestAllModal(); cancelPromptModal(); cancelConfirmModal(); closeProviderExportModal(); }
if (e.key === 'Enter' && !document.getElementById('prompt-modal').classList.contains('hidden')) { confirmPromptModal(); }
});
document.addEventListener('click', e => {
const dd = document.getElementById('model-dropdown');
const field = document.getElementById('field-model');
if (!dd.classList.contains('hidden') && !field.contains(e.target)) {
dd.classList.add('hidden');
}
});
// Smart paste: auto-extract on paste into the dedicated textarea
document.getElementById('smart-paste-input').addEventListener('paste', () => {
setTimeout(() => runSmartPaste(), 50);
});
}
// ══════════════════════════════════
// SMART PASTE — dedicated input area
// ══════════════════════════════════
function runSmartPaste() {
const raw = document.getElementById('smart-paste-input').value;
if (!raw.trim()) return;
const fields = {
url: document.getElementById('base-url'),
key: document.getElementById('api-key'),
model: document.getElementById('model-name'),
};
let filled = [];
// Extract API key: sk-... (20+ chars)
const keyMatch = raw.match(/\b(sk-[a-zA-Z0-9_-]{20,})\b/);
if (keyMatch) {
fields.key.value = keyMatch[1];
filled.push('API Key');
}
// Extract base URL: https://...
const urlMatch = raw.match(/https?:\/\/[^\s"',\]})\\]+/);
if (urlMatch) {
let url = urlMatch[0].replace(/\/+$/, '');
// Remove /chat/completions or /models suffix
url = url.replace(/\/(chat\/completions|models|completions)$/, '');
fields.url.value = url;
filled.push('Base URL');
}
// Extract model name
const modelMatch = raw.match(/(?:model|model_name|MODEL|review_model)\s*[:=]\s*["']?([a-zA-Z0-9][\w.\-]*(?:\/[\w.\-]+)?)["']?/i);
if (modelMatch) {
fields.model.value = modelMatch[1];
filled.push(currentLang === 'zh' ? '模型' : 'Model');
}
if (filled.length > 0) {
// Clear the textarea after successful extraction
document.getElementById('smart-paste-input').value = '';
showToast(currentLang === 'zh'
? `✓ 智能识别:已填入 ${filled.join('、')}`
: `✓ Smart paste: filled ${filled.join(', ')}`);
} else {
showToast(currentLang === 'zh'
? '✗ 未识别到有效配置信息'
: '✗ No valid config found');
}
}
// Migrate old format (single model) to new format (models array)
function migrateOldData() {
const raw = getRawProviders();
let changed = false;
for (const p of raw) {
if (!p.models) {
p.models = p.model ? [p.model] : [];
changed = true;
}
}
if (changed) setProviders(raw);
}
// ══════════════════════════════════
// AES-GCM ENCRYPTION
// ══════════════════════════════════
let cryptoKey = null;
async function initCrypto() {
try {
let rawKey = localStorage.getItem(CRYPTO_KEY_NAME);
if (!rawKey) {
const keyData = crypto.getRandomValues(new Uint8Array(32));
rawKey = arrayToBase64(keyData);
localStorage.setItem(CRYPTO_KEY_NAME, rawKey);
}
const keyBytes = base64ToArray(rawKey);
cryptoKey = await crypto.subtle.importKey(
'raw', keyBytes, { name: 'AES-GCM' }, false, ['encrypt', 'decrypt']
);
} catch (e) {
console.warn('Web Crypto not available, falling back to obfuscation', e);
cryptoKey = null;
}
}
async function encryptText(plaintext) {
if (!cryptoKey) return btoa(unescape(encodeURIComponent(plaintext)));
const enc = new TextEncoder();
const iv = crypto.getRandomValues(new Uint8Array(12));
const ct = await crypto.subtle.encrypt({ name: 'AES-GCM', iv }, cryptoKey, enc.encode(plaintext));
const combined = new Uint8Array(iv.length + ct.byteLength);
combined.set(iv);
combined.set(new Uint8Array(ct), iv.length);
return 'enc:' + arrayToBase64(combined);
}
async function decryptText(stored) {
if (!stored) return '';
if (!stored.startsWith('enc:')) {
try { return decodeURIComponent(escape(atob(stored))); } catch { return stored; }
}
if (!cryptoKey) return '';
try {
const data = base64ToArray(stored.slice(4));
const iv = data.slice(0, 12);
const ct = data.slice(12);
const dec = await crypto.subtle.decrypt({ name: 'AES-GCM', iv }, cryptoKey, ct);
return new TextDecoder().decode(dec);
} catch (e) {
console.warn('Decryption failed', e);
return '[decryption error]';
}
}
function arrayToBase64(arr) { return btoa(String.fromCharCode(...new Uint8Array(arr))); }
function base64ToArray(b64) {
const bin = atob(b64);
const arr = new Uint8Array(bin.length);
for (let i = 0; i < bin.length; i++) arr[i] = bin.charCodeAt(i);
return arr;
}
// ══════════════════════════════════
// TOAST & CONFIG HELPERS
// ══════════════════════════════════
function showToast(msg) {
const toast = document.getElementById('toast');
toast.textContent = msg;
toast.classList.add('show');
clearTimeout(toast._timer);
toast._timer = setTimeout(() => toast.classList.remove('show'), 2200);
}
function getConfig() {
return {
baseUrl: document.getElementById('base-url').value.trim(),
apiKey: document.getElementById('api-key').value.trim(),
model: document.getElementById('model-name').value.trim(),
prompt: document.getElementById('test-prompt').value.trim()
};
}
function saveLastUsed() {
const c = getConfig();
localStorage.setItem('api-tester-last', JSON.stringify({
baseUrl: c.baseUrl, model: c.model, prompt: c.prompt
}));
}
function loadLastUsed() {
try {
const d = JSON.parse(localStorage.getItem('api-tester-last'));
if (d) {
if (d.baseUrl) document.getElementById('base-url').value = d.baseUrl;
if (d.model) document.getElementById('model-name').value = d.model;
if (d.prompt) document.getElementById('test-prompt').value = d.prompt;
}
} catch {}
}
function normalizeBase(url) {
let base = url.trim().replace(/\/+$/, '');
// Strip common trailing path fragments that users might paste
// e.g. /chat/completions, /models
base = base.replace(/\/(chat\/completions|models|completions)$/i, '');
// If the URL already contains /v1 somewhere, keep it
if (/\/v1($|\/)/i.test(base)) {
// Trim to the /v1 part
base = base.replace(/(\/v1)\/.*/i, '$1');
return base;
}
// Otherwise, append /v1
try {
const u = new URL(base);
if (u.pathname === '/' || u.pathname === '') base += '/v1';
else if (!u.pathname.includes('/v')) base += '/v1';
} catch {}
return base;
}
function extractHost(url) { try { return new URL(url).hostname; } catch { return 'provider'; } }
// Always use proxy when deployed (relay services don't support CORS)
function getProxyUrl() {
if (location.protocol === 'https:') return '/api/proxy';
return null;
}
async function proxyFetch(url, options = {}) {
const proxyUrl = getProxyUrl();
if (!proxyUrl) {
// Direct fetch (will fail on CORS in some cases)
return fetch(url, options);
}
// Route through Netlify proxy
const proxyBody = {
targetUrl: url,
method: options.method || 'GET',
headers: {},
};
if (options.headers) {
// Extract headers from Headers object or plain object
if (options.headers instanceof Headers) {
options.headers.forEach((v, k) => { proxyBody.headers[k] = v; });
} else {
proxyBody.headers = { ...options.headers };
}
}
if (options.body) {
proxyBody.body = options.body;
}
return fetch(proxyUrl, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(proxyBody),
});
}
// ══════════════════════════════════
// PROVIDER STORAGE (encrypted)
// ══════════════════════════════════
// Data model: { id, name, baseUrl, apiKey (encrypted), models: string[], ts }
function getRawProviders() {
try { return JSON.parse(localStorage.getItem(STORAGE_KEY)) || []; } catch { return []; }
}
function setProviders(arr) {
localStorage.setItem(STORAGE_KEY, JSON.stringify(arr));
}
async function getProviders() {
const raw = getRawProviders();
const out = [];
for (const item of raw) {
out.push({
...item,
_key: await decryptText(item.apiKey),
});
}
return out;
}
// Save current config as a new provider (auto-fetches models)
async function saveConfig() {
const c = getConfig();
if (!c.baseUrl || !c.apiKey) { showToast(t('toast_need_config')); return; }
showPromptModal(t('prompt_name'), extractHost(c.baseUrl), async (name) => {
if (!name) return;
showToast(t('toast_fetching_models'));
const models = await fetchModelsRaw(c.baseUrl, c.apiKey);
const modelList = models.length > 0 ? models : (c.model ? [c.model] : []);
const encKey = await encryptText(c.apiKey);
const raw = getRawProviders();
raw.push({
id: Date.now(),
name,
baseUrl: c.baseUrl,
apiKey: encKey,
models: modelList,
ts: new Date().toISOString()
});
setProviders(raw);
renderProviders();
showToast(t('toast_saved') + ` — ${modelList.length} ${currentLang === 'zh' ? '个模型' : 'models'}`);
});
}
// ══════════════════════════════════
// RENDER PROVIDERS LIST
// ══════════════════════════════════
async function renderProviders() {
refreshProbeDropdown(); // keep model dropdown in sync
const providers = await getProviders();
const container = document.getElementById('saved-list');
if (!providers.length) {
container.innerHTML = `<div class="empty-state"><svg width="32" height="32" viewBox="0 0 32 32" fill="none" stroke="currentColor" stroke-width="1" opacity="0.3"><rect x="4" y="4" width="24" height="24" rx="3"/><path d="M10 16h12M16 10v12" stroke-dasharray="2 2"/></svg><p>${t('empty_saved')}</p></div>`;
return;
}
const maskKey = (k) => k ? k.substring(0, 5) + '•••' + k.substring(k.length - 3) : '•••';
container.innerHTML = providers.map(p => {
const isExpanded = expandedProviders.has(p.id);
const modelCount = (p.models || []).length;
return `
<div class="provider-card ${isExpanded ? 'expanded' : ''}">
<div class="provider-header" onclick="toggleProvider(${p.id})">
<div class="provider-expand-icon">${isExpanded ? '▾' : '▸'}</div>
<div class="provider-info">
<div class="provider-name">${esc(p.name)}</div>
<div class="provider-meta">
${esc(extractHost(p.baseUrl))} · ${modelCount} ${t('provider_models_count')} · Key: ${maskKey(p._key)}
</div>
</div>
<div class="provider-actions" onclick="event.stopPropagation()">
<button class="btn-icon btn-icon-sm" onclick="testProviderAllModels(${p.id})" title="${t('btn_testall')}">
<svg width="11" height="11" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2 8h12"/><path d="M9 3l5 5-5 5"/></svg>
</button>
<button class="btn-icon btn-icon-sm" onclick="refreshProviderModels(${p.id})" title="${t('provider_refresh')}">
<svg width="11" height="11" viewBox="0 0 16 16" fill="none" stroke="currentColor" stroke-width="1.5"><path d="M2 8a6 6 0 0111.5-2.3"/><path d="M14 8a6 6 0 01-11.5 2.3"/><path d="M13 2v4h-4"/><path d="M3 14v-4h4"/></svg>
</button>
<button class="btn-icon btn-icon-sm" onclick="exportProvider(${p.id})" title="Export">
<svg width="11" height="11" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.2"><path d="M6 1v7M3 5l3 3 3-3"/><path d="M1 9v2h10V9"/></svg>
</button>
<button class="btn-icon btn-icon-sm" onclick="renameProvider(${p.id})" title="Rename">
<svg width="11" height="11" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.2"><path d="M7.5 2.5l2 2M2 8l5-5 2 2-5 5H2V8z"/></svg>
</button>
<button class="btn-icon btn-icon-sm" onclick="deleteProvider(${p.id})" title="Delete">
<svg width="11" height="11" viewBox="0 0 12 12" fill="none" stroke="currentColor" stroke-width="1.2"><path d="M2 3h8M4.5 3V2h3v1M3 3v7.5a.5.5 0 00.5.5h5a.5.5 0 00.5-.5V3"/></svg>
</button>
</div>
</div>
${isExpanded ? `
<div class="provider-models">
${modelCount === 0
? `<div class="provider-model-empty">${t('provider_no_models')}</div>`
: (p.models || []).map(m => `
<button class="provider-model-item" onclick="loadProviderModel(${p.id}, '${esc(m)}')">
<span class="provider-model-name">${esc(m)}</span>
<svg width="10" height="10" viewBox="0 0 10 10" fill="none" stroke="currentColor" stroke-width="1.2"><path d="M1 5h6M5 2l3 3-3 3"/></svg>
</button>
`).join('')
}
</div>
` : ''}
</div>
`;
}).join('');
}
function toggleProvider(id) {
if (expandedProviders.has(id)) expandedProviders.delete(id);
else expandedProviders.add(id);
renderProviders();
}
async function loadProviderModel(providerId, model) {
const providers = await getProviders();
const p = providers.find(x => x.id === providerId);
if (!p) return;
document.getElementById('base-url').value = p.baseUrl;
document.getElementById('api-key').value = p._key;
document.getElementById('model-name').value = model;
showToast(t('toast_loaded') + ` — ${model}`);
}
async function renameProvider(id) {
const raw = getRawProviders();
const p = raw.find(x => x.id === id);
if (!p) return;
showPromptModal(t('prompt_rename'), p.name, (newName) => {
if (!newName || newName === p.name) return;
p.name = newName;
setProviders(raw);
renderProviders();
showToast(t('toast_renamed'));
});
}
async function deleteProvider(id) {
showConfirmModal(t('confirm_delete'), () => {
const raw = getRawProviders().filter(p => p.id !== id);
expandedProviders.delete(id);
setProviders(raw);
renderProviders();
syncProbeMatchesAfterProviderChange();
showToast(t('toast_deleted'));
});
}
function deleteAllProviders() {
const raw = getRawProviders();
if (!raw.length) return;
showConfirmModal(t('confirm_delete_all'), () => {
expandedProviders.clear();
setProviders([]);
renderProviders();
syncProbeMatchesAfterProviderChange();
showToast(t('toast_deleted_all'));
});
}
async function refreshProviderModels(id) {
const providers = await getProviders();
const p = providers.find(x => x.id === id);
if (!p) return;
showToast(t('toast_fetching_models'));
const models = await fetchModelsRaw(p.baseUrl, p._key);
if (models.length > 0) {
const raw = getRawProviders();
const rp = raw.find(x => x.id === id);
if (rp) { rp.models = models; setProviders(raw); }
expandedProviders.add(id);
renderProviders();
showToast(`✓ ${models.length} ${t('provider_models_count')}`);
} else {
showToast(t('models_error'));
}
}
// ══════════════════════════════════
// FETCH MODELS
// ══════════════════════════════════
async function fetchModelsRaw(baseUrl, apiKey) {
const base = normalizeBase(baseUrl);
// Try multiple URL patterns — some providers use /v1/models, some don't
const urlsToTry = [base + '/models'];
if (base.endsWith('/v1')) {
urlsToTry.push(base.replace(/\/v1$/, '') + '/models');
} else {
urlsToTry.push(base + '/v1/models');
}
for (const url of urlsToTry) {
try {
const res = await proxyFetch(url, {
headers: { 'Authorization': `Bearer ${apiKey}` }
});
if (!res.ok) continue;
const data = await res.json();
const models = (data.data || []).map(m => m.id).sort();
if (models.length > 0) return models;
} catch { /* try next */ }
}
return [];
}
// UI fetch models → show dropdown
async function fetchModels() {
const baseUrl = document.getElementById('base-url').value.trim();
const apiKey = document.getElementById('api-key').value.trim();
if (!baseUrl) { showToast(t('toast_need_url')); return; }
if (!apiKey) { showToast(t('toast_need_key')); return; }
const btn = document.getElementById('btn-fetch-models');
const dropdown = document.getElementById('model-dropdown');
const list = document.getElementById('model-list');
const countEl = document.getElementById('model-count');
btn.classList.add('spinning');
dropdown.classList.remove('hidden');
list.innerHTML = `<div class="model-loading">${t('models_fetching')}</div>`;
countEl.textContent = '';
const models = await fetchModelsRaw(baseUrl, apiKey);
fetchedModelsCache = models;
if (models.length === 0) {
const corsHint = location.protocol === 'file:'
? (currentLang === 'zh' ? '\n提示:本地文件打开可能因 CORS 限制无法获取,请使用在线版本' : '\nTip: Local file may be blocked by CORS. Use the online version.')
: '';
list.innerHTML = `<div class="model-loading">${t('models_none')}${corsHint}</div>`;
countEl.textContent = '0';
} else {
const current = document.getElementById('model-name').value.trim();
countEl.textContent = models.length;
list.innerHTML = models.map(id => `
<button class="model-option${id === current ? ' selected' : ''}" onclick="selectModel('${esc(id)}')">
<span class="model-option-id">${esc(id)}</span>
</button>
`).join('');
}
btn.classList.remove('spinning');
}
function selectModel(id) {
document.getElementById('model-name').value = id;
document.getElementById('model-dropdown').classList.add('hidden');
showToast('✓ ' + id);
}
// ══════════════════════════════════
// FULL TEST (chat completions)
// ══════════════════════════════════
async function testAPI() {
const c = getConfig();
if (!c.baseUrl) { showToast(t('toast_need_url')); return; }
if (!c.apiKey) { showToast(t('toast_need_key')); return; }
if (!c.model) { showToast(t('toast_need_model')); return; }
saveLastUsed();
showResultState('loading');
const t0 = performance.now();
try {
const base = normalizeBase(c.baseUrl);
const res = await proxyFetch(`${base}/chat/completions`, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Authorization': `Bearer ${c.apiKey}` },
body: JSON.stringify({
model: c.model,
messages: [{ role: 'user', content: c.prompt || 'Hello!' }],
max_tokens: 256
})
});
const elapsed = Math.round(performance.now() - t0);
if (!res.ok) {
const txt = await res.text();
let msg = `HTTP ${res.status} ${res.statusText}`;
try { const j = JSON.parse(txt); if (j.error) msg += '\n' + (j.error.message || JSON.stringify(j.error)); } catch { msg += '\n' + txt.substring(0, 500); }
showResultState('error', msg, elapsed);
return;
}
const data = await res.json();
const content = data.choices?.[0]?.message?.content || '(empty)';
const model = data.model || c.model;
const tokens = data.usage?.total_tokens || '—';
showResultState('success', content, elapsed, tokens, model);
} catch (err) {
showResultState('error', `${err.message}\n\nAPI 端点可能不支持浏览器跨域 (CORS)`, Math.round(performance.now() - t0));
}
}
function showResultState(state, content, latency, tokens, model) {
['idle', 'loading', 'success', 'error', 'ping'].forEach(s => {
document.getElementById(`state-${s}`).classList.toggle('hidden', s !== state);
});
const badge = document.getElementById('status-badge');
const statsRow = document.getElementById('stats-row');
if (state === 'success') {
document.getElementById('result-output').textContent = content;
badge.className = 'status-badge success'; badge.classList.remove('hidden');
document.getElementById('status-text').textContent = t('status_ok');
statsRow.classList.remove('hidden');
document.getElementById('stat-latency').textContent = latency + 'ms';
document.getElementById('stat-tokens').textContent = tokens;
document.getElementById('stat-model').textContent = model;
} else if (state === 'error') {
document.getElementById('error-output').textContent = content;
badge.className = 'status-badge error'; badge.classList.remove('hidden');
document.getElementById('status-text').textContent = t('status_fail');
statsRow.classList.add('hidden');
} else {
badge.classList.add('hidden'); statsRow.classList.add('hidden');
}
}
// ══════════════════════════════════
// PING (step-by-step connectivity)
// ══════════════════════════════════
async function pingAPI() {
const c = getConfig();
if (!c.baseUrl) { showToast(t('toast_need_url')); return; }
if (!c.apiKey) { showToast(t('toast_need_key')); return; }
saveLastUsed();
const steps = [
{ id: 'dns', label: t('ping_dns'), status: 'wait' },
{ id: 'tls', label: t('ping_tls'), status: 'wait' },
{ id: 'auth', label: t('ping_auth'), status: 'wait' },
{ id: 'model', label: t('ping_model'), status: 'wait' },
];
['idle', 'loading', 'success', 'error', 'ping'].forEach(s => {
document.getElementById(`state-${s}`).classList.toggle('hidden', s !== 'ping');
});
document.getElementById('status-badge').classList.add('hidden');
document.getElementById('stats-row').classList.add('hidden');
const container = document.getElementById('ping-results');
function renderSteps() {
container.innerHTML = steps.map(s => `
<div class="ping-step step-${s.status}">
<div class="ping-icon">${stepIcon(s.status)}</div>
<div class="ping-info">
<div class="ping-label">${s.label}</div>
${s.detail ? `<div class="ping-detail">${esc(s.detail)}</div>` : ''}
</div>
</div>
`).join('');
}
renderSteps();
const base = normalizeBase(c.baseUrl);
let allOk = true;
steps[0].status = 'running'; steps[0].detail = t('ping_running'); renderSteps();
try {
const t0 = performance.now();
await proxyFetch(base, { method: 'HEAD' });
steps[0].status = 'ok'; steps[0].detail = t('ping_dns_ok') + ` (${Math.round(performance.now() - t0)}ms)`;
} catch (e) { steps[0].status = 'fail'; steps[0].detail = e.message; allOk = false; }
renderSteps();
if (!allOk) { finishPing(false); return; }
steps[1].status = 'running'; steps[1].detail = t('ping_running'); renderSteps();
try {
const t0 = performance.now();
const res = await proxyFetch(`${base}/models`, { headers: { 'Authorization': `Bearer ${c.apiKey}` } });
steps[1].status = 'ok'; steps[1].detail = t('ping_tls_ok') + ` (${Math.round(performance.now() - t0)}ms)`;
steps[2].status = 'running'; steps[2].detail = t('ping_running'); renderSteps();
if (res.status === 401 || res.status === 403) {
steps[2].status = 'fail'; steps[2].detail = `HTTP ${res.status} — ` + (currentLang === 'zh' ? 'API Key 无效或已过期' : 'Invalid or expired API Key'); allOk = false;
} else {
steps[2].status = 'ok'; steps[2].detail = t('ping_auth_ok') + ` (HTTP ${res.status})`;
steps[3].status = 'running'; steps[3].detail = t('ping_running'); renderSteps();
if (res.ok && c.model) {
try {
const data = await res.json();
const models = data.data?.map(m => m.id) || [];
if (models.length === 0 || models.includes(c.model)) {
steps[3].status = 'ok'; steps[3].detail = t('ping_model_ok') + ` — ${c.model}`;
} else {
steps[3].status = 'fail'; steps[3].detail = (currentLang === 'zh' ? '模型不在列表: ' : 'Not in list: ') + models.slice(0, 8).join(', '); allOk = false;
}
} catch { steps[3].status = 'ok'; steps[3].detail = currentLang === 'zh' ? '连接正常' : 'Connection OK'; }
} else {
steps[3].status = 'ok'; steps[3].detail = currentLang === 'zh' ? '连接正常' : 'Connection OK';
}
}
} catch (e) { steps[1].status = 'fail'; steps[1].detail = e.message; allOk = false; }
renderSteps(); finishPing(allOk);
}
function finishPing(ok) {
const badge = document.getElementById('status-badge');
badge.className = ok ? 'status-badge success' : 'status-badge error';
badge.classList.remove('hidden');
document.getElementById('status-text').textContent = ok ? t('status_ok') : t('status_fail');
}
function stepIcon(s) {
if (s === 'ok') return '✓'; if (s === 'fail') return '✗';
if (s === 'running') return '<div class="mini-spinner"></div>'; return '○';
}
// ══════════════════════════════════
// TEST ALL PROVIDERS (batch)
// ══════════════════════════════════
let testAllAbort = false;
async function testAllProviders() {
const providers = await getProviders();
if (!providers.length) { showToast(t('testall_empty')); return; }
await runProviderModelTests(providers.map(p => ({
id: p.id,
name: p.name,
baseUrl: p.baseUrl,
apiKey: p._key,
models: (p.models || []).slice()
})));
}
async function testProviderAllModels(id) {
const providers = await getProviders();
const p = providers.find(x => x.id === id);
if (!p) return;
await runProviderModelTests([{
id: p.id,
name: p.name,
baseUrl: p.baseUrl,
apiKey: p._key,
models: (p.models || []).slice()
}]);
}
async function runProviderModelTests(providerList) {
if (!providerList.length) { showToast(t('testall_empty')); return; }
testAllAbort = false;
document.getElementById('testall-modal').classList.remove('hidden');
const container = document.getElementById('testall-results');
const summaryEl = document.getElementById('testall-summary');
let totalOk = 0, totalFail = 0, totalSkip = 0;
const pData = providerList.map(p => ({
id: p.id, name: p.name, baseUrl: p.baseUrl, apiKey: p._key,
models: (p.models || []).slice(), // ALL models, no limit
status: 'run', results: []
}));
function render() {
container.innerHTML = pData.map(p => `
<div class="testall-provider">
<div class="testall-provider-header">
<span class="testall-provider-name">${esc(p.name)}</span>
<span class="testall-provider-url">${esc(extractHost(p.baseUrl))}</span>
<span class="testall-provider-count">${p.models.length} ${t('provider_models_count')}</span>
<span class="testall-provider-status s-${p.status}">${providerStatusText(p.status)}</span>
</div>