pi-model-manager 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.en.md CHANGED
@@ -4,42 +4,131 @@ English · [简体中文](./README.md)
4
4
 
5
5
  [![Pi](https://img.shields.io/badge/Pi-%3E%3D0.82.0-6f42c1)](https://github.com/earendil-works/pi)
6
6
  [![License](https://img.shields.io/badge/license-AGPL--3.0-blue.svg)](./LICENSE)
7
- [![Version](https://img.shields.io/badge/version-0.2.0-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
7
+ [![Version](https://img.shields.io/badge/version-0.3.0-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
8
8
 
9
9
  A TUI model and provider manager for [Pi](https://github.com/earendil-works/pi). It keeps Pi's native `models.json` as the single source of truth for model configuration while adding provider/model editing, client-header identities, proxy routing, and protocol compatibility controls.
10
10
 
11
- > The current stable version is `0.2.0` and requires Pi `>=0.82.0`.
11
+ > The current stable version is `0.3.0` and requires Pi `>=0.82.0`.
12
12
 
13
13
  ## Interface preview
14
14
 
15
- ![pi-model-manager interface overview](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/pi-model-manager-preview.png)
15
+ The screens below are rendered by the real components at 88 columns. In practice column widths adapt to your data, the selected row is highlighted with a background colour, and column values are semantically coloured. The TUI copy itself is Simplified Chinese.
16
+
17
+ ```text
18
+ ────────────────────────────────────────────────────────────────────────────────────────
19
+ /model-manager
20
+ 4 接入 · 5 模型 · 0 内置抓包 · 0 自定义请求头
21
+
22
+ 接入 API 模型 请求头 代理 认证 状态
23
+ ❯ OpenAI (openai) Responses 2 Auto→Codex direct env ready
24
+ Claude (claude) Claude 1 ClaudeCode direct env ready
25
+ Gemini (gemini) Gemini 1 Auto→不添加 direct env ready
26
+ Local vLLM (local) Chat 1 Off proxy key ready
27
+
28
+ ────────────────────────────────────────────────────────────────────────────────────────
29
+ OpenAI (openai)
30
+ endpoint https://api.openai.com/v1
31
+ proxy direct
32
+ api Responses · headers Auto→Codex · auth env
33
+ models gpt-5.1, gpt-5.1-mini
34
+
35
+ ↑↓ 选择 Enter 进入 N 新建接入 D 删除接入 H 请求头 Esc 退出 / 搜索
36
+ ────────────────────────────────────────────────────────────────────────────────────────
37
+ ```
16
38
 
17
39
  <details>
18
- <summary>View all four full-size screenshots</summary>
40
+ <summary>Show the other four screens</summary>
19
41
 
20
- ### Provider setup
42
+ ### Models inside a provider
21
43
 
22
- ![Provider setup](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/provider-setup.png)
44
+ ```text
45
+ ────────────────────────────────────────────────────────────────────────────────────────
46
+ /model-manager / OpenAI
47
+ Responses · 2 模型 · headers Auto→Codex · proxy direct · auth env
48
+ endpoint https://api.openai.com/v1
23
49
 
24
- ### Model discovery
50
+ 模型 ID 显示名 输入 Thinking 上下文
51
+ ❯ gpt-5.1 默认 文本,视觉 开 400K
52
+ gpt-5.1-mini 默认 文本,视觉 开 400K
25
53
 
26
- ![Model discovery](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/model-discovery.png)
54
+ ↑↓ 选择 Enter 编辑模型 A 添加模型 E 编辑接入 D 删除模型 Esc 返回 / 搜索
55
+ ────────────────────────────────────────────────────────────────────────────────────────
56
+ ```
27
57
 
28
- ### Provider and model overview
58
+ ### Provider setup
29
59
 
30
- ![Provider and model overview](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/provider-models.png)
60
+ ```text
61
+ ────────────────────────────────────────────────────────────────────────────────────────
62
+ 编辑接入 openai
63
+ API Responses · 请求头 自动推荐(Auto→Codex)
64
+ 接入 ID 必填,且不能与已有或 pi 内置接入重复
65
+ Ctrl+S 保存并同步 models.json;不切换当前会话模型
66
+
67
+ 接入 ID openai
68
+ 显示名称 OpenAI
69
+ API 协议 OpenAI Responses
70
+ ❯ Base URL https://api.openai.com/v1
71
+ 本机代理 关闭
72
+ 代理地址 关闭时不使用
73
+ API key $OPENAI_API_KEY
74
+ 认证头 默认
75
+ 请求头 自动推荐(Auto→Codex)
76
+
77
+ ↑↓ 选择 ←→ 切换选项 Enter 编辑 Ctrl+S 保存并同步 Esc 返回
78
+ ────────────────────────────────────────────────────────────────────────────────────────
79
+ ```
31
80
 
32
81
  ### Model capabilities
33
82
 
34
- ![Model capabilities](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/model-editor.png)
83
+ ```text
84
+ ────────────────────────────────────────────────────────────────────────────────────────
85
+ 编辑模型 gpt-5.1
86
+ 接入 openai · API Responses
87
+ Ctrl+S 保存并启用模型;不切换当前会话模型
88
+
89
+ 模型 ID gpt-5.1
90
+ 重新拉取 上游模型列表
91
+ 显示名称 默认 = 模型 ID
92
+ ❯ 视觉支持 开启
93
+ Thinking 开启
94
+ Fast mode 关闭
95
+ 上下文窗口 400000
96
+ 最大输出 128000
97
+ 请求头 跟随接入(Auto→Codex)
98
+
99
+ ↑↓ 选择 ←→ 切换选项 Enter 编辑 Ctrl+S 保存并启用 Esc 返回
100
+ ────────────────────────────────────────────────────────────────────────────────────────
101
+ ```
102
+
103
+ ### Model discovery
104
+
105
+ ```text
106
+ ────────────────────────────────────────────────────────────────────────────────────────
107
+ 选择模型(openai)
108
+ 搜索: <直接输入搜索>
109
+ 匹配 9 / 9 · 页码 1 / 2
110
+
111
+ dall-e-3
112
+ gpt-4.1
113
+ gpt-4.1-mini
114
+ ❯ gpt-5.1 ← 当前
115
+ gpt-5.1-codex
116
+ gpt-5.1-mini
117
+ o4-mini
118
+ text-embedding-3-large
119
+
120
+ ↑↓ 选择 · ←→/PgUp/PgDn 翻页 · 输入搜索 · Backspace 删除 · Enter 确认 · Esc 取消
121
+ ────────────────────────────────────────────────────────────────────────────────────────
122
+ ```
35
123
 
36
124
  </details>
125
+
37
126
  ## Features
38
127
 
39
128
  - Create, edit, and delete providers and models from the `/model-manager` TUI.
40
129
  - Supports `openai-completions`, `openai-responses`, `anthropic-messages`, and `google-generative-ai`.
41
130
  - Fetch model IDs from compatible upstream APIs or enter them manually.
42
- - Configure context window, maximum output, vision input, and reasoning support.
131
+ - Configure context window, maximum output, vision support, and reasoning support.
43
132
  - Choose Anthropic Adaptive Thinking or Legacy Thinking.
44
133
  - Enable `service_tier=priority` (Fast mode) per OpenAI Responses model.
45
134
  - Route each provider directly or through its own HTTP(S) proxy.
@@ -89,13 +178,15 @@ pi install npm:pi-model-manager
89
178
  | Key | Action |
90
179
  | --- | --- |
91
180
  | `Enter` | Open the selected provider and manage its models |
92
- | `n` | Create a provider and its first model |
93
- | `d` | Delete the selected provider |
94
- | `h` | Manage reusable header profiles |
95
- | `/` | Search the current list |
181
+ | `N` | Create a provider and its first model |
182
+ | `D` | Delete the selected provider |
183
+ | `H` | Manage reusable header profiles |
184
+ | `/` | Search the current list; `Tab` leaves the input while keeping the filter, `Esc` clears it |
96
185
  | `Esc` | Go back or exit |
97
186
 
98
- 4. In an editor, use the arrow keys to select a field and press `Enter` to edit it. Toggle supported fields with `←` / `→`, then press `Ctrl+S` to save.
187
+ Single-letter shortcuts are case-insensitive. The footer hints wrap per item, so they are never truncated away on narrow terminals.
188
+
189
+ 4. In an editor, use `↑` / `↓` to select a field and press `Enter` to edit it. Toggle switch fields in place with `←` / `→`, then press `Ctrl+S` to save.
99
190
 
100
191
  Saving updates and enables the model, but it does not force the current session to switch models.
101
192
 
@@ -111,10 +202,12 @@ Each provider can define:
111
202
 
112
203
  When adding a model, the extension attempts to fetch the upstream model list. The complete discovery flow, including authentication fallbacks, shares one 10-second limit and can be cancelled with `Esc`. You can still enter a model ID manually after failure or cancellation.
113
204
 
205
+ The base URL is normalized as you enter it into the root address each protocol's SDK expects: OpenAI variants get `/v1` appended, Anthropic has `/v1` stripped, and Google gets `/v1beta` on its official host. A non-root path you type explicitly (such as `https://gw.example.com/custom`) is never rewritten. Switching the API protocol re-normalizes the address for the new protocol. As a result `models.json` always stores the actual request root, so Pi can use it directly even when this extension is not loaded.
206
+
114
207
  Model capabilities include:
115
208
 
116
209
  - Display name
117
- - Text or text-and-image input
210
+ - Vision support (text only, or text + image input)
118
211
  - Reasoning support
119
212
  - Anthropic Adaptive/Legacy Thinking
120
213
  - OpenAI Responses Fast mode
@@ -132,8 +225,8 @@ Model capabilities include:
132
225
 
133
226
  The current built-in values were derived from real client requests with authentication fields removed:
134
227
 
135
- - Claude Code `2.1.219`
136
- - Codex TUI `0.145.0`
228
+ - Claude Code `2.1.232`
229
+ - Codex TUI `0.147.0`
137
230
 
138
231
  These headers only help API gateways that require a recognized client identity; they do not replace an API key. The public repository and npm package contain **no request-capture tooling, user captures, authentication headers, or machine-local state**. Disable identity headers or create a custom profile if a built-in profile does not fit your endpoint.
139
232
 
package/README.md CHANGED
@@ -4,42 +4,131 @@
4
4
 
5
5
  [![Pi](https://img.shields.io/badge/Pi-%3E%3D0.82.0-6f42c1)](https://github.com/earendil-works/pi)
6
6
  [![License](https://img.shields.io/badge/license-AGPL--3.0-blue.svg)](./LICENSE)
7
- [![Version](https://img.shields.io/badge/version-0.2.0-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
7
+ [![Version](https://img.shields.io/badge/version-0.3.0-2f81f7.svg)](https://github.com/Qihuanxishini/pi-model-manager)
8
8
 
9
9
  一个面向 [Pi](https://github.com/earendil-works/pi) 的 TUI 模型与接入管理扩展。它以 Pi 原生 `models.json` 为模型配置的唯一权威来源,并提供接入/模型编辑、请求头身份、代理路由和协议兼容配置。
10
10
 
11
- > 当前稳定版为 `0.2.0`,要求 Pi `>=0.82.0`。
11
+ > 当前稳定版为 `0.3.0`,要求 Pi `>=0.82.0`。
12
12
 
13
13
  ## 界面预览
14
14
 
15
- ![pi-model-manager 界面总览](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/pi-model-manager-preview.png)
15
+ 下面的界面文本由真实组件渲染得出(终端宽 88 列)。实际使用时列宽会按当前数据收敛,选中行有背景色,各列状态按语义着色。
16
+
17
+ ```text
18
+ ────────────────────────────────────────────────────────────────────────────────────────
19
+ /model-manager
20
+ 4 接入 · 5 模型 · 0 内置抓包 · 0 自定义请求头
21
+
22
+ 接入 API 模型 请求头 代理 认证 状态
23
+ ❯ OpenAI (openai) Responses 2 Auto→Codex direct env ready
24
+ Claude (claude) Claude 1 ClaudeCode direct env ready
25
+ Gemini (gemini) Gemini 1 Auto→不添加 direct env ready
26
+ Local vLLM (local) Chat 1 Off proxy key ready
27
+
28
+ ────────────────────────────────────────────────────────────────────────────────────────
29
+ OpenAI (openai)
30
+ endpoint https://api.openai.com/v1
31
+ proxy direct
32
+ api Responses · headers Auto→Codex · auth env
33
+ models gpt-5.1, gpt-5.1-mini
34
+
35
+ ↑↓ 选择 Enter 进入 N 新建接入 D 删除接入 H 请求头 Esc 退出 / 搜索
36
+ ────────────────────────────────────────────────────────────────────────────────────────
37
+ ```
16
38
 
17
39
  <details>
18
- <summary>查看四张完整截图</summary>
40
+ <summary>展开其余四个界面</summary>
19
41
 
20
- ### 接入配置
42
+ ### 接入内的模型列表
21
43
 
22
- ![接入配置](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/provider-setup.png)
44
+ ```text
45
+ ────────────────────────────────────────────────────────────────────────────────────────
46
+ /model-manager / OpenAI
47
+ Responses · 2 模型 · headers Auto→Codex · proxy direct · auth env
48
+ endpoint https://api.openai.com/v1
23
49
 
24
- ### 模型发现
50
+ 模型 ID 显示名 输入 Thinking 上下文
51
+ ❯ gpt-5.1 默认 文本,视觉 开 400K
52
+ gpt-5.1-mini 默认 文本,视觉 开 400K
25
53
 
26
- ![模型发现](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/model-discovery.png)
54
+ ↑↓ 选择 Enter 编辑模型 A 添加模型 E 编辑接入 D 删除模型 Esc 返回 / 搜索
55
+ ────────────────────────────────────────────────────────────────────────────────────────
56
+ ```
27
57
 
28
- ### 接入与模型总览
58
+ ### 接入配置
29
59
 
30
- ![接入与模型总览](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/provider-models.png)
60
+ ```text
61
+ ────────────────────────────────────────────────────────────────────────────────────────
62
+ 编辑接入 openai
63
+ API Responses · 请求头 自动推荐(Auto→Codex)
64
+ 接入 ID 必填,且不能与已有或 pi 内置接入重复
65
+ Ctrl+S 保存并同步 models.json;不切换当前会话模型
66
+
67
+ 接入 ID openai
68
+ 显示名称 OpenAI
69
+ API 协议 OpenAI Responses
70
+ ❯ Base URL https://api.openai.com/v1
71
+ 本机代理 关闭
72
+ 代理地址 关闭时不使用
73
+ API key $OPENAI_API_KEY
74
+ 认证头 默认
75
+ 请求头 自动推荐(Auto→Codex)
76
+
77
+ ↑↓ 选择 ←→ 切换选项 Enter 编辑 Ctrl+S 保存并同步 Esc 返回
78
+ ────────────────────────────────────────────────────────────────────────────────────────
79
+ ```
31
80
 
32
81
  ### 模型能力
33
82
 
34
- ![模型能力](https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/screenshots/model-editor.png)
83
+ ```text
84
+ ────────────────────────────────────────────────────────────────────────────────────────
85
+ 编辑模型 gpt-5.1
86
+ 接入 openai · API Responses
87
+ Ctrl+S 保存并启用模型;不切换当前会话模型
88
+
89
+ 模型 ID gpt-5.1
90
+ 重新拉取 上游模型列表
91
+ 显示名称 默认 = 模型 ID
92
+ ❯ 视觉支持 开启
93
+ Thinking 开启
94
+ Fast mode 关闭
95
+ 上下文窗口 400000
96
+ 最大输出 128000
97
+ 请求头 跟随接入(Auto→Codex)
98
+
99
+ ↑↓ 选择 ←→ 切换选项 Enter 编辑 Ctrl+S 保存并启用 Esc 返回
100
+ ────────────────────────────────────────────────────────────────────────────────────────
101
+ ```
102
+
103
+ ### 模型发现
104
+
105
+ ```text
106
+ ────────────────────────────────────────────────────────────────────────────────────────
107
+ 选择模型(openai)
108
+ 搜索: <直接输入搜索>
109
+ 匹配 9 / 9 · 页码 1 / 2
110
+
111
+ dall-e-3
112
+ gpt-4.1
113
+ gpt-4.1-mini
114
+ ❯ gpt-5.1 ← 当前
115
+ gpt-5.1-codex
116
+ gpt-5.1-mini
117
+ o4-mini
118
+ text-embedding-3-large
119
+
120
+ ↑↓ 选择 · ←→/PgUp/PgDn 翻页 · 输入搜索 · Backspace 删除 · Enter 确认 · Esc 取消
121
+ ────────────────────────────────────────────────────────────────────────────────────────
122
+ ```
35
123
 
36
124
  </details>
125
+
37
126
  ## 功能
38
127
 
39
128
  - 在 `/model-manager` TUI 中新增、编辑和删除接入与模型。
40
129
  - 原生支持 `openai-completions`、`openai-responses`、`anthropic-messages` 和 `google-generative-ai`。
41
130
  - 从兼容上游拉取模型 ID,也可手动填写模型。
42
- - 配置上下文窗口、最大输出、视觉输入和 Thinking。
131
+ - 配置上下文窗口、最大输出、视觉支持和 Thinking。
43
132
  - 支持 Anthropic Adaptive Thinking 与 Legacy Thinking。
44
133
  - 可为 OpenAI Responses 模型启用 `service_tier=priority`(Fast mode)。
45
134
  - 为每个接入单独配置直连或 HTTP(S) 代理。
@@ -89,13 +178,15 @@ pi install npm:pi-model-manager
89
178
  | 按键 | 操作 |
90
179
  | --- | --- |
91
180
  | `Enter` | 进入所选接入并管理模型 |
92
- | `n` | 新建接入及其第一个模型 |
93
- | `d` | 删除所选接入 |
94
- | `h` | 管理可复用请求头 |
95
- | `/` | 搜索当前列表 |
181
+ | `N` | 新建接入及其第一个模型 |
182
+ | `D` | 删除所选接入 |
183
+ | `H` | 管理可复用请求头 |
184
+ | `/` | 搜索当前列表;`Tab` 退出输入但保留过滤,`Esc` 清空 |
96
185
  | `Esc` | 返回或退出 |
97
186
 
98
- 4. 在编辑器中使用方向键选择字段,按 `Enter` 编辑;支持切换的字段可用 `←` / `→` 调整;按 `Ctrl+S` 保存。
187
+ 单字母快捷键不区分大小写;底部提示按终端宽度折行,窄终端下也不会丢失。
188
+
189
+ 4. 在编辑器中使用 `↑` / `↓` 选择字段,按 `Enter` 编辑;开关类字段可用 `←` / `→` 就地切换;按 `Ctrl+S` 保存。
99
190
 
100
191
  保存会更新并启用模型,但不会强制切换当前会话正在使用的模型。
101
192
 
@@ -113,12 +204,14 @@ pi install npm:pi-model-manager
113
204
 
114
205
  新建模型时,扩展会尝试从上游读取模型列表;整次发现(包括认证回退)共用一个 10 秒上限,可按 `Esc` 手动取消。失败或取消后仍可手动输入模型 ID。
115
206
 
207
+ Base URL 在填入时就会归一化为各协议 SDK 可直接使用的根地址:OpenAI 系补上 `/v1`,Anthropic 剔除 `/v1`,Google 在官方域名上补 `/v1beta`;你显式填的非根路径(如 `https://gw.example.com/custom`)不会被改写。切换 API 协议时地址会按新协议重新归一化。因此 `models.json` 里存的就是实际请求根地址,本扩展未加载时 Pi 也能直接使用。
208
+
116
209
  ### 模型能力
117
210
 
118
211
  模型编辑器支持:
119
212
 
120
213
  - 显示名称
121
- - 文本或文本+图像输入
214
+ - 视觉支持(仅文本或文本 + 图片输入)
122
215
  - Thinking 开关
123
216
  - Anthropic Adaptive/Legacy Thinking 协议
124
217
  - OpenAI Responses Fast mode
@@ -136,8 +229,8 @@ pi install npm:pi-model-manager
136
229
 
137
230
  当前内置值来自真实客户端请求并已移除认证信息:
138
231
 
139
- - Claude Code `2.1.219`
140
- - Codex TUI `0.145.0`
232
+ - Claude Code `2.1.232`
233
+ - Codex TUI `0.147.0`
141
234
 
142
235
  这些值只用于兼容需要识别客户端身份的 API 中转,不代替 API key。公开仓库和 npm 包**不包含请求捕获工具、用户抓包、认证头或本机状态**。如果内置值不适合你的服务,请关闭身份头或创建自定义请求头。
143
236
 
@@ -1,7 +1,11 @@
1
1
  // 模型管理保存边界:锁内重读最新 models.json/state.json,目标化应用 mutation,
2
2
  // 再以可恢复事务写入两个文件。registry 刷新在释放文件锁后执行。
3
3
 
4
- import type { ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
4
+ import type { ExtensionContext } from "@earendil-works/pi-coding-agent";
5
+
6
+ // [喵喵喵]: 保存后只需要刷新模型 registry,不依赖命令上下文的其余能力;
7
+ // 声明成最小契约,session_start 的 ExtensionContext 也能直接复用这些函数。
8
+ type RegistryRefreshContext = Pick<ExtensionContext, "modelRegistry">;
5
9
  import { unlink } from "node:fs/promises";
6
10
  import { atomicWriteText } from "./atomic-write.ts";
7
11
  import { formatUnknownError, isObjectRecord, stringifyJson } from "./common.ts";
@@ -220,7 +224,7 @@ async function persistConfigurationInsideLock(
220
224
  }
221
225
 
222
226
  async function refreshRegistryAfterPersistence(
223
- ctx: ExtensionCommandContext,
227
+ ctx: RegistryRefreshContext,
224
228
  persist: () => Promise<StateDocument>,
225
229
  ): Promise<StateDocument> {
226
230
  let document: StateDocument;
@@ -239,7 +243,7 @@ async function refreshRegistryAfterPersistence(
239
243
  }
240
244
 
241
245
  export async function persistManagedConfiguration(
242
- ctx: ExtensionCommandContext,
246
+ ctx: RegistryRefreshContext,
243
247
  prepare: PrepareConfigurationChange,
244
248
  ): Promise<StateDocument> {
245
249
  return refreshRegistryAfterPersistence(ctx, () => persistConfigurationInsideLock(
@@ -254,7 +258,7 @@ export async function persistManagedConfiguration(
254
258
  }
255
259
 
256
260
  export async function persistProviderRenameConfiguration(
257
- ctx: ExtensionCommandContext,
261
+ ctx: RegistryRefreshContext,
258
262
  prepare: PrepareConfigurationChange,
259
263
  oldProviderId: string,
260
264
  newProviderId: string,
@@ -271,7 +275,7 @@ export async function persistProviderRenameConfiguration(
271
275
  }
272
276
 
273
277
  export async function persistModelConfiguration(
274
- ctx: ExtensionCommandContext,
278
+ ctx: RegistryRefreshContext,
275
279
  prepare: PrepareConfigurationChange,
276
280
  providerId: string,
277
281
  modelId: string,
@@ -288,7 +292,7 @@ export async function persistModelConfiguration(
288
292
  }
289
293
 
290
294
  export async function persistModelRenameConfiguration(
291
- ctx: ExtensionCommandContext,
295
+ ctx: RegistryRefreshContext,
292
296
  prepare: PrepareConfigurationChange,
293
297
  providerId: string,
294
298
  oldModelId: string,
@@ -307,7 +311,7 @@ export async function persistModelRenameConfiguration(
307
311
  }
308
312
 
309
313
  export async function persistModelDeletionConfiguration(
310
- ctx: ExtensionCommandContext,
314
+ ctx: RegistryRefreshContext,
311
315
  prepare: PrepareConfigurationChange,
312
316
  providerId: string,
313
317
  modelId: string,
package/index.ts CHANGED
@@ -10,8 +10,9 @@
10
10
 
11
11
  import type { ExtensionAPI } from "@earendil-works/pi-coding-agent";
12
12
  import { formatUnknownError } from "./common.ts";
13
- import { recoverPendingConfigurationTransaction } from "./configuration-persistence.ts";
13
+ import { persistManagedConfiguration, recoverPendingConfigurationTransaction } from "./configuration-persistence.ts";
14
14
  import { resetClaudeCodeMetadataSession } from "./claude-code-compat.ts";
15
+ import { findProvidersNeedingBaseUrlNormalization, normalizeProviderBaseUrlsInDocument } from "./state-document.ts";
15
16
  import { closeLocalProxyServer } from "./local-proxy-service.ts";
16
17
  import { registerAllFromState, registerCatalogFromState } from "./provider-registrar.ts";
17
18
  import { createRequestPipeline } from "./request-pipeline.ts";
@@ -52,6 +53,7 @@ export default async function modelManagerExtension(pi: ExtensionAPI): Promise<v
52
53
  pi.on("session_start", async (event, ctx) => {
53
54
  resetClaudeCodeMetadataSession();
54
55
  const transportErrors: string[] = [];
56
+ let migratedBaseUrlProviderIds: string[] = [];
55
57
  try {
56
58
  if (configurationBlocked) {
57
59
  await recoverPendingConfigurationTransaction();
@@ -61,6 +63,17 @@ export default async function modelManagerExtension(pi: ExtensionAPI): Promise<v
61
63
  for (const warning of await registerAllFromState(pi, currentState)) {
62
64
  transportErrors.push(`激活模型接入失败:${warning}`);
63
65
  }
66
+ // [喵喵喵]: 一次性补齐早期版本留在 models.json 里的半成品 baseUrl;
67
+ // 归一化幂等,写完后后续启动检测不到差异,不会重复写盘。
68
+ const staleBaseUrlProviderIds = findProvidersNeedingBaseUrlNormalization(currentState);
69
+ if (staleBaseUrlProviderIds.length > 0) {
70
+ await persistManagedConfiguration(ctx, (latest) => ({
71
+ document: normalizeProviderBaseUrlsInDocument(latest, staleBaseUrlProviderIds),
72
+ changedProviderIds: [...staleBaseUrlProviderIds],
73
+ removedProviderIds: [],
74
+ }));
75
+ migratedBaseUrlProviderIds = staleBaseUrlProviderIds;
76
+ }
64
77
  } catch (error) {
65
78
  configurationBlocked = true;
66
79
  transportErrors.push(`恢复、读取或激活模型配置失败:${formatUnknownError(error)}`);
@@ -75,6 +88,12 @@ export default async function modelManagerExtension(pi: ExtensionAPI): Promise<v
75
88
  for (const error of transportErrors) {
76
89
  ctx.ui.notify(`[pi-model-manager] ${error}`, "error");
77
90
  }
91
+ if (migratedBaseUrlProviderIds.length > 0) {
92
+ ctx.ui.notify(
93
+ `[pi-model-manager] 已补全 ${migratedBaseUrlProviderIds.length} 个接入的请求地址(${migratedBaseUrlProviderIds.join("、")}),现在 models.json 存的就是实际请求根地址`,
94
+ "info",
95
+ );
96
+ }
78
97
  });
79
98
 
80
99
  pi.on("before_provider_request", async (event, ctx) => requestPipeline.transform(event.payload, ctx));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pi-model-manager",
3
- "version": "0.2.0",
3
+ "version": "0.3.0",
4
4
  "description": "A TUI model and provider manager for Pi with native models.json persistence, request-header profiles, proxy routing, and protocol compatibility.",
5
5
  "type": "module",
6
6
  "main": "./index.ts",
@@ -64,8 +64,7 @@
64
64
  "pi": {
65
65
  "extensions": [
66
66
  "./index.ts"
67
- ],
68
- "image": "https://raw.githubusercontent.com/Qihuanxishini/pi-model-manager/main/assets/pi-model-manager-preview.png"
67
+ ]
69
68
  },
70
69
  "peerDependencies": {
71
70
  "@earendil-works/pi-coding-agent": ">=0.82.0",
@@ -1,23 +1,23 @@
1
1
  // 由私有 request-capture-service 根据真实客户端请求生成;公开插件仅消费这些脱敏后的内置值。
2
2
 
3
3
  export const CLAUDE_CODE_CLIENT_HEADERS: Record<string, string> = {
4
- "user-agent": "claude-cli/2.1.219 (external, sdk-cli)",
4
+ "anthropic-beta": "claude-code-20250219,context-1m-2025-08-07,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,effort-2025-11-24",
5
+ "anthropic-dangerous-direct-browser-access": "true",
6
+ "anthropic-version": "2023-06-01",
7
+ "user-agent": "claude-cli/2.1.232 (external, sdk-cli)",
8
+ "x-app": "cli",
5
9
  "x-stainless-arch": "x64",
6
10
  "x-stainless-lang": "js",
7
11
  "x-stainless-os": "Windows",
8
- "x-stainless-package-version": "0.94.0",
12
+ "x-stainless-package-version": "0.112.1",
9
13
  "x-stainless-retry-count": "0",
10
14
  "x-stainless-runtime": "node",
11
15
  "x-stainless-runtime-version": "v26.3.0",
12
16
  "x-stainless-timeout": "300",
13
- "anthropic-beta": "claude-code-20250219,interleaved-thinking-2025-05-14,thinking-token-count-2026-05-13,context-management-2025-06-27,prompt-caching-scope-2026-01-05,mid-conversation-system-2026-04-07,effort-2025-11-24",
14
- "anthropic-dangerous-direct-browser-access": "true",
15
- "anthropic-version": "2023-06-01",
16
- "x-app": "cli",
17
17
  };
18
18
 
19
19
  export const CODEX_CLI_CLIENT_HEADERS: Record<string, string> = {
20
- "user-agent": "codex-tui/0.145.0 (Windows 10.0.26200; x86_64) WindowsTerminal (codex-tui; 0.145.0)",
21
20
  "originator": "codex-tui",
21
+ "user-agent": "codex-tui/0.147.0 (Windows 10.0.26200; x86_64) unknown (codex-tui; 0.147.0)",
22
22
  "x-codex-beta-features": "remote_compaction_v2",
23
23
  };
@@ -1,6 +1,7 @@
1
1
  // 4 种 API 协议的默认预设。新建接入/模型时用,便于一键填好常用字段。
2
2
 
3
3
  import type { ApiKind, ModelInputKind, ProviderDraft } from "../types.ts";
4
+ import { resolveRuntimeBaseUrl } from "../runtime-base-url.ts";
4
5
 
5
6
  export interface ProviderPreset {
6
7
  api: ApiKind;
@@ -98,6 +99,8 @@ export function switchProviderDraftApiPreset(draft: ProviderDraft, nextApi: ApiK
98
99
  const replaceBaseUrl = stillUsesPresetUrl(draft.baseUrl, previousPreset.baseUrl);
99
100
  const replaceApiKey = draft.apiKey === previousPreset.apiKey;
100
101
  draft.api = nextApi;
101
- if (replaceBaseUrl) draft.baseUrl = nextPreset.baseUrl;
102
+ // [喵喵喵]: 版本路径规则随协议而变(OpenAI 要 /v1、Anthropic 不要),
103
+ // 保留的自定义地址必须按新协议重新归一化,否则切完协议就指向错误端点。
104
+ draft.baseUrl = replaceBaseUrl ? nextPreset.baseUrl : resolveRuntimeBaseUrl(nextApi, draft.baseUrl);
102
105
  if (replaceApiKey) draft.apiKey = nextPreset.apiKey;
103
106
  }
@@ -71,9 +71,15 @@ function appendGoogleGenerativeApiVersionForRootUrl(baseUrl: string): string {
71
71
  return parsed.toString();
72
72
  }
73
73
 
74
+ // [喵喵喵]: URL.toString() 对根路径会补出尾斜杠,归一化结果现在会落盘到 models.json,
75
+ // 多一个斜杠会让本来正确的配置被判定为需要迁移,白白改写用户配置。
76
+ function stripRootTrailingSlash(url: string): string {
77
+ return url.replace(/^(https?:\/\/[^/?#]+)\/(?=$|[?#])/i, "$1");
78
+ }
79
+
74
80
  export function resolveRuntimeBaseUrl(api: ApiKind, baseUrl: string): string {
75
- if (api === "anthropic-messages") return stripTrailingV1(baseUrl);
76
- if (api === "openai-completions" || api === "openai-responses") return appendV1ForRootUrl(baseUrl);
77
- if (api === "google-generative-ai") return appendGoogleGenerativeApiVersionForRootUrl(baseUrl);
81
+ if (api === "anthropic-messages") return stripRootTrailingSlash(stripTrailingV1(baseUrl));
82
+ if (api === "openai-completions" || api === "openai-responses") return stripRootTrailingSlash(appendV1ForRootUrl(baseUrl));
83
+ if (api === "google-generative-ai") return stripRootTrailingSlash(appendGoogleGenerativeApiVersionForRootUrl(baseUrl));
78
84
  return trimTrailingSlashes(baseUrl);
79
85
  }
package/state-document.ts CHANGED
@@ -18,6 +18,7 @@ import {
18
18
  import { findPresetForApi } from "./presets/providers.ts";
19
19
  import { normalizeThinkingLevelMap } from "./presets/thinking.ts";
20
20
  import { isSensitiveHeaderName } from "./sensitive-redaction.ts";
21
+ import { resolveRuntimeBaseUrl } from "./runtime-base-url.ts";
21
22
  import type {
22
23
  ApiKind,
23
24
  CompatSettings,
@@ -483,6 +484,28 @@ export function upsertRequestHeaderProfileInDocument(
483
484
  };
484
485
  return next;
485
486
  }
487
+ // [喵喵喵]: 早期版本只在注册时补全 baseUrl,models.json 里留下的是半成品(如 openai 缺 /v1)。
488
+ // 插件未加载时 pi 会回落到 models.json 直接请求,因此需要把存量值一次性归一化。
489
+ // 只处理受管理接入;原生接入的 baseUrl 不是本插件写的,不能代为改写。
490
+ export function findProvidersNeedingBaseUrlNormalization(document: StateDocument): string[] {
491
+ return Object.entries(document.providers)
492
+ .filter(([, provider]) => provider.managed && provider.baseUrl)
493
+ .filter(([, provider]) => resolveRuntimeBaseUrl(provider.api, provider.baseUrl) !== provider.baseUrl)
494
+ .map(([providerId]) => providerId);
495
+ }
496
+
497
+ export function normalizeProviderBaseUrlsInDocument(
498
+ document: StateDocument,
499
+ providerIds: readonly string[],
500
+ ): StateDocument {
501
+ const next: StateDocument = cloneJson(document);
502
+ for (const providerId of providerIds) {
503
+ const provider = next.providers[providerId];
504
+ if (!provider?.managed || !provider.baseUrl) continue;
505
+ provider.baseUrl = resolveRuntimeBaseUrl(provider.api, provider.baseUrl);
506
+ }
507
+ return next;
508
+ }
486
509
 
487
510
  export function deleteRequestHeaderProfileFromDocument(document: StateDocument, profileId: string): StateDocument {
488
511
  const next: StateDocument = cloneJson(document);