agentswitch 0.6.0

一个通用的 Code Agent 工具配置切换器,支持将任意 OpenAI/Anthropic 协议模型接入到主流 Code Agent CLI 工具中
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
# 快速开始指南: 用户体验优化与高级功能

**功能分支**: `004-ux-optimization`
**创建日期**: 2026-03-10
**文档版本**: 1.0.0

## 概述

本文档为 Spec 004 的用户提供快速上手指南,涵盖交互式配置向导、工具检测、Shell 补全和 Git 同步的常见使用场景。

---

## 前置要求

### 系统要求

- **操作系统**: Linux、macOS 或 Windows
- **Rust**: 最新稳定版(如需从源码编译)
- **Git**: 2.0+(仅用于配置同步功能)
- **Shell**: Bash、Zsh 或 Fish(用于自动补全)

### 安装 AgentSwitch

```bash
# 从源码编译
cargo install --path .

# 或使用预编译二进制
wget https://github.com/Yu-Xiao-Sheng/agentswitch/releases/latest/download/agentswitch-linux
chmod +x agentswitch-linux
sudo mv agentswitch-linux /usr/local/bin/asw
```

---

## 场景 1: 首次使用 - 交互式配置向导

### 1.1 启动向导

首次安装 AgentSwitch 后,运行以下命令启动配置向导:

```bash
asw init
```

### 1.2 向导流程

向导将引导你完成以下步骤:

#### 步骤 1: 欢迎和说明

```
Welcome to AgentSwitch configuration wizard!

This wizard will guide you through setting up your first model configuration.

Press Ctrl+C at any time to exit (progress will be saved).
```

#### 步骤 2: 输入模型配置名称

```
? Model configuration name glm
```

**提示**:
- 选择一个易记的名称(如 `glm``gpt-4``minimax`- 名称将用于后续的 `asw switch` 命令

#### 步骤 3: 输入 Base URL

```
? Base URL https://open.bigmodel.cn/api/v1
```

**常见 Base URL**:
- 智谱 GLM: `https://open.bigmodel.cn/api/v1`
- OpenAI: `https://api.openai.com/v1`
- MiniMax: `https://api.minimax.chat/v1`
- 本地模型: `http://localhost:11434/v1` (Ollama)

#### 步骤 4: 输入 API Key

```
? API Key ****************************
```

**提示**:
- API Key 会以掩码形式显示
- 输入长度至少 32 个字符

#### 步骤 5: 输入 Model ID

```
? Model ID glm-4
```

**常见 Model ID**:
- 智谱: `glm-4``glm-4-turbo`
- OpenAI: `gpt-4``gpt-3.5-turbo`
- MiniMax: `abab6.5s-chat`

#### 步骤 6: 确认并保存

```
Configuration summary:
  Name: glm
  Base URL: https://open.bigmodel.cn/api/v1
  API Key: sk-***abc123
  Model ID: glm-4

? Save this configuration? Yes
```

### 1.3 完成配置

```
✓ Configuration saved successfully!

Next steps:
  - Run 'asw model list' to see all configured models
  - Run 'asw doctor' to detect installed tools
  - Run 'asw switch <tool> <model>' to apply a model to a tool
```

---

## 场景 2: 添加更多模型配置

### 2.1 使用向导添加

```bash
asw wizard
```

向导流程与首次配置相同。

### 2.2 查看已配置的模型

```bash
asw model list
```

**输出示例**:
```
Configured Models:
==================

glm
  Base URL: https://open.bigmodel.cn/api/v1
  API Key: sk-***abc123
  Model ID: glm-4

gpt-4
  Base URL: https://api.openai.com/v1
  API Key: sk-***xyz789
  Model ID: gpt-4
```

---

## 场景 3: 工具检测和诊断

### 3.1 检测已安装工具

```bash
asw detect
```

**输出示例**:
```
Installed tools:
  - Claude Code (v1.2.3)
  - Codex (v2.1.0)
  - Qwen CLI (v1.0.5)
```

### 3.2 运行完整诊断

```bash
asw doctor
```

**输出示例**:
```
AgentSwitch Tool Diagnostic Report
==================================

System Information:
  OS: Linux 6.8.0-101-generic
  Arch: x86_64
  Shell: /bin/zsh
  Git: git version 2.43.0

Tool Status:
-------------
✓ Claude Code    v1.2.3    Installed (Healthy)
  Config: ~/.claude/config.json
  Status: Configuration valid

✓ Codex          v2.1.0    Installed (Healthy)
  Config: ~/.codex/config.toml
  Status: Configuration valid

✗ Gemini CLI     -         Not Installed
  Suggestion: Install via 'npm install -g gemini-cli'

⚠ Qwen CLI       v1.0.5    Installed (Warning)
  Config: ~/.qwen/config.env
  Issue: API Key not set
  Suggestion: Run 'asw switch qwen-cli <model>' to configure

Summary:
  Installed: 3
  Healthy: 2
  Warnings: 1
  Errors: 0

Run 'asw doctor --fix' to attempt automatic repairs.
```

### 3.3 自动修复问题

```bash
asw doctor --fix
```

**可自动修复的问题**:
- 配置文件权限问题
- 缺失的配置文件(创建空模板)
- 格式错误的配置(尝试修复)

---

## 场景 4: Shell 自动补全

### 4.1 安装 Bash 补全

```bash
asw completion install bash
```

**输出**:
```
✓ Bash completion script installed to: ~/.local/share/bash-completion/completions/asw.bash
✓ Added source line to ~/.bashrc

Restart your shell or run: source ~/.bashrc
```

### 4.2 安装 Zsh 补全

```bash
asw completion install zsh
```

**输出**:
```
✓ Zsh completion script installed to: ~/.zsh/completion/_asw
✓ Added completion setup to ~/.zshrc

Restart your shell or run: source ~/.zshrc
```

### 4.3 安装 Fish 补全

```bash
asw completion install fish
```

**输出**:
```
✓ Fish completion script installed to: ~/.config/fish/completions/asw.fish

Completion will be available automatically in new Fish sessions.
```

### 4.4 使用补全

安装补全后,可以使用 Tab 键补全命令:

```bash
# 补全子命令
asw sw<TAB>  # 补全为 asw switch

# 补全工具名称
asw switch <TAB>  # 显示: claude-code, codex, gemini-cli

# 补全模型名称
asw switch claude-code <TAB>  # 显示: glm, gpt-4, minimax
```

### 4.5 卸载补全

```bash
asw completion uninstall bash
```

---

## 场景 5: 配置同步 (Git)

### 5.1 初始化 Git 仓库

```bash
asw sync init --encrypt
```

**输出**:
```
Initializing Git repository...

✓ Created .gitignore
✓ Initialized repository

Encryption settings:
? Enable encryption for API Keys? Yes
? Choose encryption method:
  > AES-GCM (password)
    git-crypt (requires git-crypt installation)

? Enter encryption password: ********
? Confirm password: ********

✓ Encryption configured (AES-GCM)
✓ Created initial commit

Next steps:
  - Add a remote: asw sync remote add <url>
  - Push to remote: asw sync push
```

### 5.2 添加远程仓库

```bash
asw sync remote add https://github.com/user/agentswitch-config.git
```

### 5.3 推送到远程

```bash
asw sync push
```

**输出**:
```
Preparing to push...

✓ Detected 2 modified files
✓ Encrypted 3 API Keys
✓ Created commit: feat: update models and presets

Pushing to origin/main...
✓ Push successful

Changes pushed:
  - models.toml (encrypted)
  - presets.toml
```

### 5.4 在另一台机器上拉取配置

```bash
# 克隆配置仓库
git clone https://github.com/user/agentswitch-config.git ~/.agentswitch

# 或使用拉取命令(如果已有配置)
asw sync pull
```

**输出**:
```
Pulling from origin/main...

✓ Fetched 2 new commits

Changes:
  - models.toml: Added new model "gpt-4"
  - presets.toml: Updated preset "production"

✓ Pull successful

Models added:
  - gpt-4 (OpenAI GPT-4)
```

### 5.5 查看同步状态

```bash
asw sync status
```

**输出**:
```
Sync Status:
============

Repository: ✓ Initialized (Git 2.43.0)
Remote:    origin → https://github.com/user/agentswitch-config.git
Branch:    main

Divergence:
  Your branch is ahead of 'origin/main' by 1 commit.
  (use "asw sync push" to publish)

Encryption:
  Method: AES-GCM (password)
  Status: ✓ Active
  Encrypted files: 1 (models.toml)

Last sync: 2026-03-10 10:30:00 UTC
```

### 5.6 处理冲突

当本地和远程配置有冲突时:

```bash
asw sync pull
```

**冲突输出**:
```
Pulling from origin/main...
✗ Conflict detected

Conflicting files:
  - models.toml

Resolution options:
  1. Keep local version (discard remote)
  2. Use remote version (discard local)
  3. Manual merge (open in editor)

? Choose resolution strategy: 2

✓ Applied remote version
✓ Resolved conflict

Run 'asw sync push' to push the merged result.
```

---

## 场景 6: 完整工作流程

### 6.1 新用户设置流程

```bash
# 1. 初始化配置
asw init

# 2. 检测已安装工具
asw doctor

# 3. 安装 Shell 补全(可选)
asw completion install zsh

# 4. 应用模型到工具
asw switch claude-code glm

# 5. 验证配置
asw status
```

### 6.2 多机器同步流程

**机器 A(首次设置)**:
```bash
# 1. 配置模型
asw init

# 2. 初始化 Git 同步
asw sync init --encrypt
asw sync remote add https://github.com/user/agentswitch-config.git

# 3. 推送到远程
asw sync push
```

**机器 B(拉取配置)**:
```bash
# 1. 克隆配置
git clone https://github.com/user/agentswitch-config.git ~/.agentswitch

# 2. 验证配置
asw model list
asw doctor

# 3. 使用配置
asw switch claude-code glm
```

### 6.3 日常更新流程

```bash
# 添加新模型
asw wizard

# 应用新模型到工具
asw switch codex gpt-4

# 同步到远程
asw sync push

# 在其他机器上拉取更新
asw sync pull
```

---

## 场景 7: 高级用法

### 7.1 JSON 输出(脚本使用)

```bash
# 获取工具列表为 JSON
asw detect --json

# 获取诊断报告为 JSON
asw doctor --json
```

### 7.2 非交互式模式(脚本)

```bash
# 设置环境变量
export ASW_NON_INTERACTIVE=1

# 运行命令(如果需要交互会失败)
asw init || echo "Init failed, need interactive mode"
```

### 7.3 自定义配置目录

```bash
# 使用自定义目录
export ASW_CONFIG_DIR=/custom/path/agentswitch

# 初始化配置
asw init
```

### 7.4 生成补全脚本到文件

```bash
# 生成补全脚本
asw completion generate bash > /usr/share/bash-completion/completions/asw
```

---

## 故障排除

### 问题 1: 向导无法启动

**错误信息**:
```
✗ Not running in an interactive terminal
```

**解决方案**:
- 确保在真实的终端中运行(不是脚本或管道)
- 如果在脚本中需要自动化,使用环境变量 `ASW_NON_INTERACTIVE=1`

### 问题 2: Git 未安装

**错误信息**:
```
✗ Git is not installed
```

**解决方案**:
```bash
# Ubuntu/Debian
sudo apt-get install git

# macOS
brew install git

# 验证安装
git --version
```

### 问题 3: 补全不生效

**原因**: Shell 配置文件未重新加载

**解决方案**:
```bash
# 重新加载配置
source ~/.bashrc      # Bash
source ~/.zshrc       # Zsh
source ~/.config/fish/config.fish  # Fish

# 或重启终端
```

### 问题 4: 加密密码忘记

**解决方案**:
```bash
# 重置加密配置
rm ~/.agentswitch/sync.toml
asw sync init --encrypt
```

### 问题 5: 工具检测失败

**错误信息**:
```
✗ Failed to detect tool: Permission denied
```

**解决方案**:
```bash
# 检查文件权限
ls -la ~/.codex/config.toml

# 修复权限
chmod 600 ~/.codex/config.toml
```

---

## 最佳实践

### 1. 定期同步配置

```bash
# 设置定时同步(可选)
# 添加到 crontab
0 */6 * * * /usr/local/bin/asw sync pull
```

### 2. 备份配置

```bash
# 导出配置
asw preset export-all --output backup.json

# 或使用 Git 历史
git log --oneline ~/.agentswitch/
```

### 3. 使用预设管理多环境

```bash
# 开发环境预设
asw preset create development --description "开发环境配置"

# 生产环境预设
asw preset create production --description "生产环境配置"

# 切换环境
asw preset apply development
```

### 4. 定期运行诊断

```bash
# 每周运行一次
asw doctor --fix
```

---

## 下一步

- 查看 [完整命令参考]./contracts/CLI.md
- 阅读 [数据模型文档]./data-model.md
- 了解 [项目架构]../../README.md

---

## 获取帮助

```bash
# 查看命令帮助
asw --help
asw init --help

# 查看版本
asw --version

# 报告问题
https://github.com/Yu-Xiao-Sheng/agentswitch/issues
```

---

**文档完成日期**: 2026-03-10
**文档版本**: 1.0.0