-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenclaw_quick_config_gui.py
More file actions
2450 lines (2153 loc) · 96.1 KB
/
openclaw_quick_config_gui.py
File metadata and controls
2450 lines (2153 loc) · 96.1 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
#!/usr/bin/env python3
import argparse
import contextlib
import importlib.util
import io
import json
import os
import pathlib
import shutil
import shlex
import subprocess
import sys
import threading
import time
import traceback
import urllib.error
import urllib.request
from typing import Any, Dict, List, Optional, Tuple
tk: Any
ttk: Any
filedialog: Any
messagebox: Any
scrolledtext: Any
TK_IMPORT_ERROR: Optional[Exception] = None
try:
import tkinter as tk # noqa: F401
from tkinter import filedialog, messagebox, scrolledtext, ttk # noqa: F401
except Exception as exc:
TK_IMPORT_ERROR = exc
DEFAULT_CONFIG_PATH = pathlib.Path.home() / ".openclaw" / "openclaw.json"
I18N = {
"app_title": {
"zh": "OpenClaw 快速配置管理器",
"en": "OpenClaw Quick Config Manager",
},
"status_ready": {"zh": "就绪", "en": "Ready"},
"label_config": {"zh": "配置文件", "en": "Config"},
"btn_browse": {"zh": "浏览", "en": "Browse"},
"btn_refresh": {"zh": "刷新", "en": "Refresh"},
"label_primary": {"zh": "默认模型", "en": "Primary"},
"label_language": {"zh": "语言", "en": "Language"},
"provider_list": {"zh": "提供商列表", "en": "Providers"},
"model_list": {"zh": "模型列表(当前提供商)", "en": "Models (selected provider)"},
"label_provider_id": {"zh": "提供商 ID", "en": "Provider ID"},
"label_api_mode": {"zh": "API 模式", "en": "API Mode"},
"label_base_url": {"zh": "基础地址", "en": "Base URL"},
"label_api_key": {"zh": "API 密钥", "en": "API Key"},
"label_model_id": {"zh": "模型 ID", "en": "Model ID"},
"label_model_name": {"zh": "模型名称", "en": "Model Name"},
"label_inputs": {"zh": "输入类型", "en": "Inputs"},
"label_context_window": {"zh": "上下文窗口", "en": "Context Window"},
"label_max_tokens": {"zh": "最大 Tokens", "en": "Max Tokens"},
"check_reasoning": {"zh": "推理模型", "en": "Reasoning"},
"check_auth_header": {"zh": "启用 authHeader", "en": "Enable authHeader"},
"label_headers": {
"zh": "请求头(每行 KEY=VALUE)",
"en": "Headers (KEY=VALUE per line)",
},
"label_fallbacks": {
"zh": "备用模型(逗号分隔 provider/model)",
"en": "Fallbacks (comma-separated provider/model)",
},
"check_set_primary": {"zh": "设为默认模型", "en": "Set as primary"},
"check_restart_gateway": {
"zh": "写入后重启网关",
"en": "Restart gateway after write",
},
"check_probe_after": {"zh": "写入后探测可用性", "en": "Probe after write"},
"btn_upsert": {"zh": "保存提供商和模型", "en": "Upsert Provider + Model"},
"btn_set_primary": {"zh": "将当前选择设为默认", "en": "Set Selected As Primary"},
"btn_probe": {"zh": "探测模型", "en": "Probe Models"},
"btn_restart_gateway": {"zh": "重启网关", "en": "Restart Gateway"},
"btn_wizard": {"zh": "新增模型向导", "en": "New Model Wizard"},
"label_primary_ref": {"zh": "默认模型引用", "en": "Primary Ref"},
"label_log": {"zh": "日志", "en": "Log"},
"dialog_select_config": {"zh": "选择 openclaw.json", "en": "Select openclaw.json"},
"filetype_json": {"zh": "JSON 文件", "en": "JSON"},
"filetype_all": {"zh": "所有文件", "en": "All"},
"status_load_failed": {
"zh": "读取配置失败: {error}",
"en": "Read config failed: {error}",
},
"log_load_failed": {
"zh": "读取配置错误: {error}",
"en": "ERROR reading config: {error}",
},
"status_loaded": {"zh": "配置已加载", "en": "Config loaded"},
"log_loaded": {"zh": "已从配置加载提供商", "en": "Loaded providers from config"},
"err_header_line": {
"zh": "无效请求头 '{line}',应为 KEY=VALUE",
"en": "Invalid header line '{line}', expected KEY=VALUE",
},
"err_header_key": {
"zh": "无效请求头键名: '{line}'",
"en": "Invalid header key in line '{line}'",
},
"status_cmd_failed": {
"zh": "命令执行失败 ({code})",
"en": "Command failed ({code})",
},
"status_cmd_ok": {"zh": "命令执行成功", "en": "Command succeeded"},
"err_provider_required": {
"zh": "提供商 ID 不能为空",
"en": "Provider ID is required",
},
"err_base_url_required": {"zh": "Base URL 不能为空", "en": "Base URL is required"},
"err_api_key_required": {"zh": "API Key 不能为空", "en": "API Key is required"},
"err_model_id_required": {"zh": "模型 ID 不能为空", "en": "Model ID is required"},
"title_success": {"zh": "成功", "en": "Success"},
"title_failed": {"zh": "失败", "en": "Failed"},
"title_error": {"zh": "错误", "en": "Error"},
"msg_saved": {
"zh": "提供商和模型已保存。",
"en": "Provider/model upserted successfully.",
},
"msg_cmd_failed": {
"zh": "命令执行失败,退出码 {code}。",
"en": "Command failed with exit code {code}.",
},
"log_error": {"zh": "错误: {error}", "en": "ERROR: {error}"},
"msg_primary_required": {
"zh": "需要填写默认模型引用(provider/model)。",
"en": "Primary ref is required (provider/model).",
},
"msg_primary_set": {
"zh": "默认模型已设置为 {primary_ref}。",
"en": "Primary set to {primary_ref}.",
},
"title_probe": {"zh": "探测", "en": "Probe"},
"msg_probe_done": {"zh": "模型探测完成。", "en": "Probe completed successfully."},
"title_probe_failed": {"zh": "探测失败", "en": "Probe Failed"},
"msg_probe_failed": {
"zh": "模型探测失败,退出码 {code}。",
"en": "Probe failed with exit code {code}.",
},
"title_gateway": {"zh": "网关", "en": "Gateway"},
"msg_gateway_done": {
"zh": "网关重启命令已执行。",
"en": "Gateway restart command executed.",
},
"title_gateway_failed": {"zh": "网关失败", "en": "Gateway Failed"},
"msg_gateway_failed": {
"zh": "重启失败,退出码 {code}。",
"en": "Restart failed with exit code {code}.",
},
"tk_unavailable": {
"zh": "Tkinter 不可用: {error}",
"en": "Tkinter is unavailable: {error}",
},
"cli_missing": {
"zh": "未找到所需 CLI 脚本: {path}",
"en": "Required CLI script not found: {path}",
},
"log_gui_started": {"zh": "界面已启动", "en": "GUI started"},
"argparse_desc": {
"zh": "OpenClaw 快速配置图形界面",
"en": "OpenClaw Quick Config GUI",
},
"argparse_help_config": {"zh": "openclaw.json 路径", "en": "Path to openclaw.json"},
"title_info": {"zh": "提示", "en": "Info"},
"msg_busy": {
"zh": "有任务正在执行,请稍候。",
"en": "A task is running, please wait.",
},
"status_running_generic": {"zh": "处理中", "en": "Processing"},
"status_running_save": {"zh": "正在保存配置", "en": "Saving configuration"},
"status_running_primary": {"zh": "正在设置默认模型", "en": "Setting primary model"},
"status_running_probe": {"zh": "正在探测模型", "en": "Probing models"},
"status_running_gateway": {"zh": "正在重启网关", "en": "Restarting gateway"},
"status_running_gateway_refresh": {
"zh": "正在刷新网关状态",
"en": "Refreshing gateway status",
},
"status_running_gateway_start_service": {
"zh": "正在后台启动网关",
"en": "Starting gateway service",
},
"status_running_gateway_start_window": {
"zh": "正在当前窗口启动网关",
"en": "Starting gateway in this window",
},
"status_running_gateway_stop": {
"zh": "正在关闭网关",
"en": "Stopping gateway",
},
"status_running_test": {"zh": "正在测试模型", "en": "Testing model"},
"label_gateway_panel": {"zh": "网关状态监听", "en": "Gateway Monitor"},
"label_gateway_state": {"zh": "状态", "en": "State"},
"label_gateway_detail": {"zh": "详情", "en": "Details"},
"gateway_state_unknown": {"zh": "未知", "en": "Unknown"},
"gateway_state_checking": {"zh": "检查中", "en": "Checking"},
"gateway_state_running_service": {
"zh": "运行中(后台服务)",
"en": "Running (service)",
},
"gateway_state_running_window": {
"zh": "运行中(当前窗口)",
"en": "Running (this window)",
},
"gateway_state_stopped": {"zh": "已停止", "en": "Stopped"},
"gateway_state_error": {"zh": "异常", "en": "Error"},
"check_gateway_auto_monitor": {"zh": "自动监听", "en": "Auto Monitor"},
"btn_gateway_refresh": {"zh": "立即刷新", "en": "Refresh Now"},
"btn_gateway_start_service": {"zh": "后台持久化启动", "en": "Start Service"},
"btn_gateway_start_window": {"zh": "当前窗口启动", "en": "Start In Window"},
"btn_gateway_stop": {"zh": "关闭网关", "en": "Stop Gateway"},
"msg_gateway_started_service": {
"zh": "已执行后台持久化启动。",
"en": "Service start executed.",
},
"msg_gateway_started_window": {
"zh": "已在当前窗口启动网关。",
"en": "Gateway started in current window.",
},
"msg_gateway_stopped": {
"zh": "已执行网关关闭。",
"en": "Gateway stop executed.",
},
"msg_gateway_window_already": {
"zh": "当前窗口网关已在运行。",
"en": "Gateway is already running in this window.",
},
"label_test_panel": {"zh": "模型测试", "en": "Model Test"},
"label_test_prompt": {"zh": "测试提示词", "en": "Prompt"},
"label_test_tokens": {"zh": "输出 Tokens", "en": "Output Tokens"},
"btn_test_model": {"zh": "测试当前模型", "en": "Test Current Model"},
"label_command_panel": {"zh": "指令复制区", "en": "Command Copy Area"},
"label_command_category": {"zh": "指令分类", "en": "Category"},
"label_command_select": {"zh": "常用指令", "en": "Command Template"},
"btn_copy_command": {"zh": "复制当前指令", "en": "Copy Command"},
"btn_copy_all_commands": {"zh": "复制指令大全", "en": "Copy All"},
"msg_copied_command": {"zh": "已复制当前指令", "en": "Command copied"},
"msg_copied_all": {"zh": "已复制指令大全", "en": "All commands copied"},
"msg_copy_failed": {"zh": "复制失败: {error}", "en": "Copy failed: {error}"},
"category_all": {"zh": "全部", "en": "All"},
"category_model": {"zh": "模型切换", "en": "Model Switching"},
"category_gateway": {"zh": "网关运维", "en": "Gateway Ops"},
"category_diagnostic": {"zh": "诊断校验", "en": "Diagnostics"},
"category_chat": {"zh": "聊天指令", "en": "Chat Prompts"},
"cmd_switch_primary": {"zh": "切换默认模型", "en": "Switch primary model"},
"cmd_show_primary": {"zh": "查看当前默认模型", "en": "Show current primary"},
"cmd_list_provider": {"zh": "查看当前提供商模型列表", "en": "List provider models"},
"cmd_probe": {"zh": "模型可用性探测", "en": "Probe model availability"},
"cmd_restart_gateway": {
"zh": "重启网关并查看状态",
"en": "Restart gateway + status",
},
"cmd_show_provider": {"zh": "查看当前提供商配置", "en": "Show provider config"},
"cmd_tool_set_primary": {"zh": "用工具切换默认模型", "en": "Switch model via tool"},
"cmd_chat_switch": {
"zh": "给 AI 的切模指令模板",
"en": "Chat prompt for model switch",
},
"title_test": {"zh": "模型测试", "en": "Model Test"},
"title_test_failed": {"zh": "模型测试失败", "en": "Model Test Failed"},
"default_test_prompt": {"zh": "请只回复: ok", "en": "Reply with: ok"},
"msg_test_missing": {
"zh": "请先填写以下字段: {fields}",
"en": "Please fill required fields first: {fields}",
},
"msg_test_bad_tokens": {
"zh": "输出 Tokens 必须是正整数",
"en": "Output tokens must be a positive integer",
},
"msg_test_success": {
"zh": "测试成功\n模型: {model_ref}\n耗时: {latency_ms} ms\n\n输出:\n{preview}",
"en": "Test succeeded\nModel: {model_ref}\nLatency: {latency_ms} ms\n\nOutput:\n{preview}",
},
"msg_test_failed": {
"zh": "测试失败\n模型: {model_ref}\n状态码: {status}\n错误: {error}\n\n返回:\n{preview}",
"en": "Test failed\nModel: {model_ref}\nStatus: {status}\nError: {error}\n\nResponse:\n{preview}",
},
"wizard_title": {"zh": "新增模型向导", "en": "New Model Wizard"},
"wizard_step": {"zh": "步骤 {index}/3", "en": "Step {index}/3"},
"wizard_s1_title": {"zh": "1. 渠道信息", "en": "1. Provider"},
"wizard_s2_title": {"zh": "2. 模型信息", "en": "2. Model"},
"wizard_s3_title": {"zh": "3. 策略与验证", "en": "3. Policy & Verify"},
"wizard_provider_hint": {"zh": "提供商 ID", "en": "Provider ID"},
"wizard_base_url_hint": {"zh": "基础地址", "en": "Base URL"},
"wizard_api_key_hint": {"zh": "API 密钥", "en": "API Key"},
"wizard_api_mode_hint": {"zh": "API 模式", "en": "API Mode"},
"wizard_model_id_hint": {"zh": "模型 ID", "en": "Model ID"},
"wizard_model_name_hint": {"zh": "模型名称", "en": "Model Name"},
"wizard_inputs_hint": {"zh": "输入类型(text,image)", "en": "Inputs (text,image)"},
"wizard_context_hint": {"zh": "上下文窗口", "en": "Context Window"},
"wizard_tokens_hint": {"zh": "最大 Tokens", "en": "Max Tokens"},
"wizard_headers_hint": {
"zh": "请求头(每行 KEY=VALUE)",
"en": "Headers (KEY=VALUE per line)",
},
"wizard_fallback_hint": {
"zh": "备用模型(逗号分隔 provider/model)",
"en": "Fallbacks (comma-separated provider/model)",
},
"wizard_prev": {"zh": "上一步", "en": "Back"},
"wizard_next": {"zh": "下一步", "en": "Next"},
"wizard_finish": {"zh": "完成并保存", "en": "Finish & Save"},
"wizard_cancel": {"zh": "取消", "en": "Cancel"},
}
def _resolve_cli_script_path() -> pathlib.Path:
if getattr(sys, "frozen", False):
meipass = pathlib.Path(
getattr(sys, "_MEIPASS", pathlib.Path(sys.executable).parent)
)
bundled = meipass / "openclaw_quick_config.py"
if bundled.exists():
return bundled
return pathlib.Path(__file__).with_name("openclaw_quick_config.py")
def _resolve_openclaw_executable() -> str:
env_value = os.environ.get("OPENCLAW_BIN", "").strip()
if env_value:
env_path = pathlib.Path(env_value).expanduser()
if env_path.exists():
return str(env_path)
discovered = shutil.which("openclaw")
if discovered:
return discovered
candidates = [
pathlib.Path.home() / ".local" / "bin" / "openclaw",
pathlib.Path.home() / ".volta" / "bin" / "openclaw",
pathlib.Path.home() / "Library" / "pnpm" / "openclaw",
pathlib.Path("/opt/homebrew/bin/openclaw"),
pathlib.Path("/usr/local/bin/openclaw"),
pathlib.Path("/usr/bin/openclaw"),
]
nvm_root = pathlib.Path.home() / ".nvm" / "versions" / "node"
if nvm_root.exists():
nvm_bins = sorted(nvm_root.glob("*/bin/openclaw"), reverse=True)
candidates.extend(nvm_bins)
if os.name == "nt":
candidates.extend(
[
pathlib.Path.home()
/ "AppData"
/ "Local"
/ "Programs"
/ "openclaw"
/ "openclaw.exe",
pathlib.Path("C:/Program Files/openclaw/openclaw.exe"),
]
)
for candidate in candidates:
if candidate.exists():
return str(candidate)
raise FileNotFoundError(
"openclaw command not found; set OPENCLAW_BIN or add openclaw to PATH"
)
class QuickConfigGUI:
def __init__(
self, root: Any, config_path: pathlib.Path, cli_script: pathlib.Path
) -> None:
self.root = root
self.language_code = "zh"
self.root.title(self._t("app_title"))
self.root.geometry("1280x820")
self.config_path_var = tk.StringVar(value=str(config_path))
self.primary_var = tk.StringVar(value="-")
self.status_var = tk.StringVar(value=self._t("status_ready"))
self.language_display_var = tk.StringVar(value="中文")
self.cli_script = cli_script
self.provider_var = tk.StringVar()
self.base_url_var = tk.StringVar()
self.api_var = tk.StringVar(value="openai-completions")
self.api_key_var = tk.StringVar()
self.model_id_var = tk.StringVar()
self.model_name_var = tk.StringVar()
self.inputs_var = tk.StringVar(value="text")
self.context_window_var = tk.StringVar(value="200000")
self.max_tokens_var = tk.StringVar(value="8192")
self.reasoning_var = tk.BooleanVar(value=False)
self.auth_header_var = tk.BooleanVar(value=True)
self.set_primary_var = tk.BooleanVar(value=True)
self.restart_gateway_var = tk.BooleanVar(value=False)
self.probe_after_write_var = tk.BooleanVar(value=True)
self.primary_ref_var = tk.StringVar()
self.fallbacks_var = tk.StringVar()
self.test_max_tokens_var = tk.StringVar(value="64")
self.gateway_state_var = tk.StringVar(value=self._t("gateway_state_unknown"))
self.gateway_detail_var = tk.StringVar(value="-")
self.gateway_auto_monitor_var = tk.BooleanVar(value=True)
self.command_category_var = tk.StringVar()
self.command_choice_var = tk.StringVar()
self.command_category_select: Any = None
self.providers_cache: Dict[str, dict] = {}
self._widget_texts: Dict[str, Any] = {}
self._action_widgets: List[Any] = []
self._command_specs: List[Dict[str, str]] = []
self._visible_command_specs: List[Dict[str, str]] = []
self._command_category_label_to_id: Dict[str, str] = {}
self._command_label_to_id: Dict[str, str] = {}
self._busy = False
self._busy_label_text = ""
self._busy_tick = 0
self._busy_after_id: Optional[str] = None
self._gateway_state_key = "gateway_state_unknown"
self._gateway_monitor_after_id: Optional[str] = None
self._gateway_monitor_busy = False
self._gateway_fg_process: Any = None
self._build_ui()
self.refresh_all()
self._gateway_poll_once(log_command=False)
def _t(self, key: str, **kwargs: Any) -> str:
lang = "en" if self.language_code == "en" else "zh"
template = I18N.get(key, {}).get(lang, key)
if kwargs:
return template.format(**kwargs)
return template
def _set_widget_texts(self) -> None:
self.root.title(self._t("app_title"))
for key, widget in self._widget_texts.items():
widget.configure(text=self._t(key))
self.language_display_var.set(
"English" if self.language_code == "en" else "中文"
)
self._set_gateway_state(self._gateway_state_key, self.gateway_detail_var.get())
def _on_language_change(self, _event: object = None) -> None:
selected = self.language_display_var.get()
self.language_code = "en" if selected == "English" else "zh"
self._set_widget_texts()
self._refresh_command_choices()
def _build_command_specs(self) -> List[Dict[str, str]]:
return [
{
"id": "switch_primary",
"category": "model",
"label_key": "cmd_switch_primary",
"template": 'openclaw config set agents.defaults.model.primary "{model_ref}"',
},
{
"id": "show_primary",
"category": "model",
"label_key": "cmd_show_primary",
"template": "openclaw config get agents.defaults.model",
},
{
"id": "list_provider",
"category": "diagnostic",
"label_key": "cmd_list_provider",
"template": "openclaw models list --provider {provider} --all",
},
{
"id": "probe_models",
"category": "diagnostic",
"label_key": "cmd_probe",
"template": "openclaw models status --probe",
},
{
"id": "restart_gateway",
"category": "gateway",
"label_key": "cmd_restart_gateway",
"template": "openclaw gateway restart && openclaw gateway status --json",
},
{
"id": "show_provider",
"category": "diagnostic",
"label_key": "cmd_show_provider",
"template": "openclaw config get models.providers.{provider}",
},
{
"id": "tool_set_primary",
"category": "model",
"label_key": "cmd_tool_set_primary",
"template": 'python3 "{tool_path}" set-primary --model-ref "{model_ref}" --probe',
},
{
"id": "chat_switch",
"category": "chat",
"label_key": "cmd_chat_switch",
"template": "请把 OpenClaw 默认模型切换到 {model_ref},并执行 openclaw config get agents.defaults.model 校验。",
},
]
def _command_context(self) -> Dict[str, str]:
provider = self.provider_var.get().strip() or "<provider>"
model = self.model_id_var.get().strip() or "<model>"
desktop_tool = (
pathlib.Path.home()
/ "Desktop"
/ "OpenClaw-Quick-Config-Tool"
/ "openclaw_quick_config.py"
)
tool_path = (
desktop_tool
if desktop_tool.exists()
else pathlib.Path(__file__).with_name("openclaw_quick_config.py")
)
return {
"provider": provider,
"model": model,
"model_ref": f"{provider}/{model}",
"tool_path": str(tool_path),
}
def _render_command_text(self, command_id: str) -> str:
context = self._command_context()
for spec in self._command_specs:
if spec.get("id") == command_id:
template = str(spec.get("template", ""))
try:
return template.format(**context)
except Exception:
return template
return ""
def _set_command_preview(self, text: str) -> None:
self.command_preview_text.configure(state="normal")
self.command_preview_text.delete("1.0", tk.END)
self.command_preview_text.insert("1.0", text)
self.command_preview_text.configure(state="disabled")
def _refresh_command_categories(self) -> None:
options = [
("all", self._t("category_all")),
("model", self._t("category_model")),
("gateway", self._t("category_gateway")),
("diagnostic", self._t("category_diagnostic")),
("chat", self._t("category_chat")),
]
labels = [label for _, label in options]
self._command_category_label_to_id = {
label: category_id for category_id, label in options
}
self.command_category_select.configure(values=labels)
current = self.command_category_var.get().strip()
if current not in labels and labels:
self.command_category_var.set(labels[0])
def _on_command_category_change(self, _event: object = None) -> None:
self._refresh_command_choices()
def _refresh_command_choices(self) -> None:
self._refresh_command_categories()
selected_category_label = self.command_category_var.get().strip()
selected_category_id = self._command_category_label_to_id.get(
selected_category_label,
"all",
)
if selected_category_id == "all":
filtered_specs = list(self._command_specs)
else:
filtered_specs = [
spec
for spec in self._command_specs
if str(spec.get("category", "")) == selected_category_id
]
self._visible_command_specs = filtered_specs
labels: List[str] = []
self._command_label_to_id = {}
for spec in filtered_specs:
label = self._t(str(spec.get("label_key", "")))
labels.append(label)
self._command_label_to_id[label] = str(spec.get("id", ""))
self.command_select.configure(values=labels)
current = self.command_choice_var.get().strip()
if current not in labels and labels:
self.command_choice_var.set(labels[0])
self._refresh_command_preview()
def _refresh_command_preview(self, _event: object = None) -> None:
label = self.command_choice_var.get().strip()
command_id = self._command_label_to_id.get(label, "")
text = self._render_command_text(command_id)
self._set_command_preview(text)
def _copy_current_command(self) -> None:
try:
text = self.command_preview_text.get("1.0", tk.END).strip()
if not text:
return
self.root.clipboard_clear()
self.root.clipboard_append(text)
self._set_status(self._t("msg_copied_command"), ok=True)
except Exception as exc:
self._set_status(self._t("msg_copy_failed", error=exc), ok=False)
def _copy_all_commands(self) -> None:
try:
specs = self._visible_command_specs or self._command_specs
lines: List[str] = []
for spec in specs:
label = self._t(str(spec.get("label_key", "")))
command_text = self._render_command_text(str(spec.get("id", "")))
lines.append(f"# {label}\n{command_text}")
full_text = "\n\n".join(lines).strip()
if not full_text:
return
self.root.clipboard_clear()
self.root.clipboard_append(full_text)
self._set_status(self._t("msg_copied_all"), ok=True)
except Exception as exc:
self._set_status(self._t("msg_copy_failed", error=exc), ok=False)
def _build_ui(self) -> None:
self.root.columnconfigure(0, weight=1)
self.root.rowconfigure(1, weight=1)
self.root.rowconfigure(2, weight=1)
top = ttk.Frame(self.root, padding=10)
top.grid(row=0, column=0, sticky="ew")
top.columnconfigure(1, weight=1)
top.columnconfigure(6, weight=1)
self.lbl_config = ttk.Label(top, text="")
self.lbl_config.grid(row=0, column=0, sticky="w")
ttk.Entry(top, textvariable=self.config_path_var).grid(
row=0, column=1, sticky="ew", padx=(6, 6)
)
self.btn_browse = ttk.Button(top, text="", command=self._browse_config)
self.btn_browse.grid(row=0, column=2, padx=(0, 6))
self.btn_refresh = ttk.Button(top, text="", command=self.refresh_all)
self.btn_refresh.grid(row=0, column=3, padx=(0, 12))
self.lbl_primary = ttk.Label(top, text="")
self.lbl_primary.grid(row=0, column=4, sticky="w")
ttk.Label(top, textvariable=self.primary_var).grid(
row=0, column=5, sticky="w", padx=(6, 0)
)
self.lbl_status = ttk.Label(
top, textvariable=self.status_var, foreground="#2e7d32"
)
self.lbl_status.grid(row=0, column=6, sticky="e")
self.lbl_language = ttk.Label(top, text="")
self.lbl_language.grid(row=0, column=7, sticky="e", padx=(8, 6))
self.cmb_language = ttk.Combobox(
top,
textvariable=self.language_display_var,
values=["中文", "English"],
state="readonly",
width=10,
)
self.cmb_language.grid(row=0, column=8, sticky="e")
self.cmb_language.bind("<<ComboboxSelected>>", self._on_language_change)
self.progress = ttk.Progressbar(top, mode="indeterminate")
self.progress.grid(row=1, column=0, columnspan=9, sticky="ew", pady=(8, 0))
self.progress.grid_remove()
self._widget_texts.update(
{
"label_config": self.lbl_config,
"btn_browse": self.btn_browse,
"btn_refresh": self.btn_refresh,
"label_primary": self.lbl_primary,
"label_language": self.lbl_language,
}
)
self._action_widgets.extend(
[
self.btn_browse,
self.btn_refresh,
self.cmb_language,
]
)
paned = ttk.Panedwindow(self.root, orient=tk.HORIZONTAL)
paned.grid(row=1, column=0, sticky="nsew", padx=10, pady=(0, 10))
left = ttk.Frame(paned, padding=8)
left.columnconfigure(0, weight=1)
left.rowconfigure(1, weight=1)
left.rowconfigure(3, weight=1)
self.lbl_providers = ttk.Label(left, text="")
self.lbl_providers.grid(row=0, column=0, sticky="w")
self.providers_list = tk.Listbox(left, exportselection=False)
self.providers_list.grid(row=1, column=0, sticky="nsew", pady=(4, 8))
self.providers_list.bind("<<ListboxSelect>>", self._on_provider_select)
self.lbl_models = ttk.Label(left, text="")
self.lbl_models.grid(row=2, column=0, sticky="w")
self.models_list = tk.Listbox(left, exportselection=False)
self.models_list.grid(row=3, column=0, sticky="nsew", pady=(4, 0))
self.models_list.bind("<<ListboxSelect>>", self._on_model_select)
self._action_widgets.extend([self.providers_list, self.models_list])
self._widget_texts.update(
{
"provider_list": self.lbl_providers,
"model_list": self.lbl_models,
}
)
paned.add(left, weight=1)
right = ttk.Frame(paned, padding=8)
right.columnconfigure(1, weight=1)
right.columnconfigure(3, weight=1)
r = 0
self.lbl_provider_id = ttk.Label(right, text="")
self.lbl_provider_id.grid(row=r, column=0, sticky="w")
ttk.Entry(right, textvariable=self.provider_var).grid(
row=r, column=1, sticky="ew", padx=(6, 10)
)
self.lbl_api_mode = ttk.Label(right, text="")
self.lbl_api_mode.grid(row=r, column=2, sticky="w")
self.api_combo = ttk.Combobox(
right,
textvariable=self.api_var,
values=["openai-completions", "openai-responses"],
state="readonly",
)
self.api_combo.grid(row=r, column=3, sticky="ew", padx=(6, 0))
self._action_widgets.append(self.api_combo)
r += 1
self.lbl_base_url = ttk.Label(right, text="")
self.lbl_base_url.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.base_url_var).grid(
row=r, column=1, columnspan=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
self.lbl_api_key = ttk.Label(right, text="")
self.lbl_api_key.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.api_key_var, show="*").grid(
row=r, column=1, columnspan=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
self.lbl_model_id = ttk.Label(right, text="")
self.lbl_model_id.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.model_id_var).grid(
row=r, column=1, sticky="ew", padx=(6, 10), pady=(8, 0)
)
self.lbl_model_name = ttk.Label(right, text="")
self.lbl_model_name.grid(row=r, column=2, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.model_name_var).grid(
row=r, column=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
self.lbl_inputs = ttk.Label(right, text="")
self.lbl_inputs.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.inputs_var).grid(
row=r, column=1, sticky="ew", padx=(6, 10), pady=(8, 0)
)
self.lbl_context_window = ttk.Label(right, text="")
self.lbl_context_window.grid(row=r, column=2, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.context_window_var).grid(
row=r, column=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
self.lbl_max_tokens = ttk.Label(right, text="")
self.lbl_max_tokens.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.max_tokens_var).grid(
row=r, column=1, sticky="ew", padx=(6, 10), pady=(8, 0)
)
self.chk_reasoning = ttk.Checkbutton(
right, text="", variable=self.reasoning_var
)
self.chk_reasoning.grid(row=r, column=2, sticky="w", pady=(8, 0))
self.chk_auth_header = ttk.Checkbutton(
right, text="", variable=self.auth_header_var
)
self.chk_auth_header.grid(row=r, column=3, sticky="w", pady=(8, 0))
r += 1
self.lbl_headers = ttk.Label(right, text="")
self.lbl_headers.grid(row=r, column=0, sticky="nw", pady=(8, 0))
self.headers_text = scrolledtext.ScrolledText(right, height=5)
self.headers_text.grid(
row=r, column=1, columnspan=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
self.lbl_fallbacks = ttk.Label(right, text="")
self.lbl_fallbacks.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.fallbacks_var).grid(
row=r, column=1, columnspan=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
options = ttk.Frame(right)
options.grid(row=r, column=0, columnspan=4, sticky="w", pady=(8, 0))
self.chk_set_primary = ttk.Checkbutton(
options, text="", variable=self.set_primary_var
)
self.chk_set_primary.pack(side=tk.LEFT)
self.chk_restart_gateway = ttk.Checkbutton(
options,
text="",
variable=self.restart_gateway_var,
)
self.chk_restart_gateway.pack(side=tk.LEFT, padx=(16, 0))
self.chk_probe_after = ttk.Checkbutton(
options, text="", variable=self.probe_after_write_var
)
self.chk_probe_after.pack(side=tk.LEFT, padx=(16, 0))
r += 1
actions = ttk.Frame(right)
actions.grid(row=r, column=0, columnspan=4, sticky="ew", pady=(10, 0))
actions.columnconfigure(0, weight=1)
actions.columnconfigure(1, weight=1)
actions.columnconfigure(2, weight=1)
actions.columnconfigure(3, weight=1)
actions.columnconfigure(4, weight=1)
self.btn_wizard = ttk.Button(actions, text="", command=self._open_model_wizard)
self.btn_wizard.grid(row=0, column=0, sticky="ew", padx=(0, 6))
self.btn_upsert = ttk.Button(
actions, text="", command=self._upsert_provider_model
)
self.btn_upsert.grid(row=0, column=1, sticky="ew", padx=(0, 6))
self.btn_set_primary = ttk.Button(actions, text="", command=self._set_primary)
self.btn_set_primary.grid(row=0, column=2, sticky="ew", padx=(0, 6))
self.btn_probe = ttk.Button(actions, text="", command=self._probe_models)
self.btn_probe.grid(row=0, column=3, sticky="ew", padx=(0, 6))
self.btn_restart_gateway = ttk.Button(
actions, text="", command=self._restart_gateway
)
self.btn_restart_gateway.grid(row=0, column=4, sticky="ew")
self._action_widgets.extend(
[
self.chk_set_primary,
self.chk_restart_gateway,
self.chk_probe_after,
self.btn_wizard,
self.btn_upsert,
self.btn_set_primary,
self.btn_probe,
self.btn_restart_gateway,
]
)
r += 1
self.lbl_primary_ref = ttk.Label(right, text="")
self.lbl_primary_ref.grid(row=r, column=0, sticky="w", pady=(8, 0))
ttk.Entry(right, textvariable=self.primary_ref_var).grid(
row=r, column=1, columnspan=3, sticky="ew", padx=(6, 0), pady=(8, 0)
)
r += 1
self.test_frame = ttk.LabelFrame(right, text="")
self.test_frame.grid(row=r, column=0, columnspan=4, sticky="nsew", pady=(10, 0))
self.test_frame.columnconfigure(1, weight=1)
self.lbl_test_prompt = ttk.Label(self.test_frame, text="")
self.lbl_test_prompt.grid(
row=0, column=0, sticky="nw", padx=(8, 8), pady=(8, 8)
)
self.test_prompt_text = scrolledtext.ScrolledText(self.test_frame, height=4)
self.test_prompt_text.grid(
row=0, column=1, sticky="ew", padx=(0, 8), pady=(8, 8)
)
self.test_prompt_text.insert("1.0", self._t("default_test_prompt"))
self.lbl_test_tokens = ttk.Label(self.test_frame, text="")
self.lbl_test_tokens.grid(row=1, column=0, sticky="w", padx=(8, 8), pady=(0, 8))
ttk.Entry(
self.test_frame, textvariable=self.test_max_tokens_var, width=12
).grid(row=1, column=1, sticky="w", padx=(0, 8), pady=(0, 8))
self.btn_test_model = ttk.Button(
self.test_frame, text="", command=self._test_current_model
)
self.btn_test_model.grid(row=1, column=1, sticky="e", padx=(0, 8), pady=(0, 8))
self._action_widgets.append(self.btn_test_model)
r += 1
self.command_frame = ttk.LabelFrame(right, text="")
self.command_frame.grid(
row=r, column=0, columnspan=4, sticky="nsew", pady=(10, 0)
)
self.command_frame.columnconfigure(1, weight=1)
self.lbl_command_category = ttk.Label(self.command_frame, text="")
self.lbl_command_category.grid(
row=0, column=0, sticky="w", padx=(8, 8), pady=(8, 8)
)
self.command_category_select = ttk.Combobox(
self.command_frame,
textvariable=self.command_category_var,
state="readonly",
)
self.command_category_select.grid(
row=0, column=1, sticky="ew", padx=(0, 8), pady=(8, 8)
)
self.command_category_select.bind(
"<<ComboboxSelected>>",
self._on_command_category_change,
)
self.lbl_command_select = ttk.Label(self.command_frame, text="")
self.lbl_command_select.grid(
row=1, column=0, sticky="w", padx=(8, 8), pady=(0, 8)
)
self.command_select = ttk.Combobox(
self.command_frame,
textvariable=self.command_choice_var,
state="readonly",
)
self.command_select.grid(row=1, column=1, sticky="ew", padx=(0, 8), pady=(0, 8))
self.command_select.bind("<<ComboboxSelected>>", self._refresh_command_preview)
self.command_preview_text = scrolledtext.ScrolledText(
self.command_frame, height=6
)
self.command_preview_text.grid(
row=2, column=0, columnspan=2, sticky="nsew", padx=8, pady=(0, 8)
)
self.command_preview_text.configure(state="disabled")
command_actions = ttk.Frame(self.command_frame)
command_actions.grid(
row=3, column=0, columnspan=2, sticky="e", padx=8, pady=(0, 8)
)
self.btn_copy_command = ttk.Button(
command_actions, text="", command=self._copy_current_command
)
self.btn_copy_command.grid(row=0, column=0, padx=(0, 8))
self.btn_copy_all_commands = ttk.Button(
command_actions, text="", command=self._copy_all_commands
)
self.btn_copy_all_commands.grid(row=0, column=1)
self._action_widgets.extend(
[
self.command_category_select,
self.command_select,
self.btn_copy_command,
self.btn_copy_all_commands,
]
)
r += 1
self.gateway_frame = ttk.LabelFrame(right, text="")
self.gateway_frame.grid(
row=r, column=0, columnspan=4, sticky="nsew", pady=(10, 0)
)
self.gateway_frame.columnconfigure(1, weight=1)
self.lbl_gateway_state = ttk.Label(self.gateway_frame, text="")
self.lbl_gateway_state.grid(
row=0, column=0, sticky="w", padx=(8, 8), pady=(8, 6)
)
self.lbl_gateway_state_value = ttk.Label(
self.gateway_frame,
textvariable=self.gateway_state_var,
foreground="#1565c0",
)
self.lbl_gateway_state_value.grid(
row=0, column=1, sticky="w", padx=(0, 8), pady=(8, 6)
)
self.lbl_gateway_detail = ttk.Label(self.gateway_frame, text="")
self.lbl_gateway_detail.grid(
row=1, column=0, sticky="nw", padx=(8, 8), pady=(0, 8)
)
self.lbl_gateway_detail_value = ttk.Label(
self.gateway_frame,
textvariable=self.gateway_detail_var,
justify="left",
wraplength=620,