pi-model-manager 0.1.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 +119 -23
- package/README.md +119 -23
- package/atomic-write.ts +4 -32
- package/common.ts +0 -17
- package/configuration-lock.ts +136 -0
- package/configuration-persistence.ts +289 -38
- package/file-snapshot.ts +76 -0
- package/header-profile-mutations.ts +29 -7
- package/index.ts +43 -10
- package/local-proxy-service.ts +93 -30
- package/model-mutations.ts +58 -32
- package/models-json-manager.ts +43 -79
- package/models-json-sync.ts +117 -93
- package/openai-responses-payload.ts +1 -1
- package/package.json +12 -7
- package/presets/builtin-client-headers.ts +7 -7
- package/presets/client-headers.ts +2 -15
- package/presets/providers.ts +30 -4
- package/presets/thinking.ts +5 -13
- package/provider-registrar.ts +9 -2
- package/runtime-base-url.ts +64 -33
- package/sensitive-redaction.ts +46 -0
- package/state-cache.ts +2 -1
- package/state-document.ts +37 -14
- package/state-metadata-store.ts +65 -31
- package/state-store.ts +85 -31
- package/tui/dashboard.ts +60 -21
- package/tui/editor-model.ts +74 -99
- package/tui/editor-provider.ts +69 -68
- package/tui/header-profiles-panel.ts +96 -24
- package/tui/model-list-fetch.ts +136 -114
- package/tui/model-list-validation.ts +79 -0
- package/tui/model-picker.ts +5 -3
- package/tui/persistent-menu.ts +309 -84
- package/tui/ui-helpers.ts +217 -111
- package/types.ts +7 -14
- package/models-json-mutations.ts +0 -90
- package/tui/models-json-panel.ts +0 -269
package/README.en.md
CHANGED
|
@@ -4,48 +4,137 @@ English · [简体中文](./README.md)
|
|
|
4
4
|
|
|
5
5
|
[](https://github.com/earendil-works/pi)
|
|
6
6
|
[](./LICENSE)
|
|
7
|
-
[](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.
|
|
11
|
+
> The current stable version is `0.3.0` and requires Pi `>=0.82.0`.
|
|
12
12
|
|
|
13
13
|
## Interface preview
|
|
14
14
|
|
|
15
|
-
|
|
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>
|
|
40
|
+
<summary>Show the other four screens</summary>
|
|
19
41
|
|
|
20
|
-
###
|
|
42
|
+
### Models inside a provider
|
|
21
43
|
|
|
22
|
-
|
|
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
|
-
|
|
54
|
+
↑↓ 选择 Enter 编辑模型 A 添加模型 E 编辑接入 D 删除模型 Esc 返回 / 搜索
|
|
55
|
+
────────────────────────────────────────────────────────────────────────────────────────
|
|
56
|
+
```
|
|
27
57
|
|
|
28
|
-
### Provider
|
|
58
|
+
### Provider setup
|
|
29
59
|
|
|
30
|
-
|
|
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
|
-
|
|
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
|
|
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.
|
|
46
135
|
- Use recommended, disabled, Claude Code, Codex, or custom client-header profiles.
|
|
47
136
|
- Reference API keys as literals, `$ENV_VAR` / `${ENV_VAR}`, or Pi `!command` values.
|
|
48
|
-
- Persist configuration
|
|
137
|
+
- Persist configuration with a cross-process lock and recoverable two-file transactions, then re-register managed providers after saving.
|
|
49
138
|
|
|
50
139
|
## Installation
|
|
51
140
|
|
|
@@ -89,12 +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
|
-
| `
|
|
93
|
-
| `
|
|
94
|
-
| `
|
|
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 |
|
|
95
185
|
| `Esc` | Go back or exit |
|
|
96
186
|
|
|
97
|
-
|
|
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.
|
|
98
190
|
|
|
99
191
|
Saving updates and enables the model, but it does not force the current session to switch models.
|
|
100
192
|
|
|
@@ -108,12 +200,14 @@ Each provider can define:
|
|
|
108
200
|
- Provider-specific HTTP(S) proxy
|
|
109
201
|
- One or more models
|
|
110
202
|
|
|
111
|
-
When adding a model, the extension attempts to fetch the upstream model list
|
|
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.
|
|
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.
|
|
112
206
|
|
|
113
207
|
Model capabilities include:
|
|
114
208
|
|
|
115
209
|
- Display name
|
|
116
|
-
-
|
|
210
|
+
- Vision support (text only, or text + image input)
|
|
117
211
|
- Reasoning support
|
|
118
212
|
- Anthropic Adaptive/Legacy Thinking
|
|
119
213
|
- OpenAI Responses Fast mode
|
|
@@ -131,8 +225,8 @@ Model capabilities include:
|
|
|
131
225
|
|
|
132
226
|
The current built-in values were derived from real client requests with authentication fields removed:
|
|
133
227
|
|
|
134
|
-
- Claude Code `2.1.
|
|
135
|
-
- Codex TUI `0.
|
|
228
|
+
- Claude Code `2.1.232`
|
|
229
|
+
- Codex TUI `0.147.0`
|
|
136
230
|
|
|
137
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.
|
|
138
232
|
|
|
@@ -145,7 +239,9 @@ Custom profiles reject authentication-related sensitive headers. Put authenticat
|
|
|
145
239
|
| `~/.pi/agent/models.json` | Native Pi provider and model definitions; the single source of truth for model configuration |
|
|
146
240
|
| `~/.pi/agent/extensions/pi-model-manager/state.json` | Extension metadata such as header choices, custom profiles, proxy switches, and Fast mode |
|
|
147
241
|
|
|
148
|
-
The extension
|
|
242
|
+
The extension generates client headers, proxy routes, and dynamic registrations only for explicitly managed providers. Ownership requires both a managed ID in `state.json` and a `piModelManager.managed` marker on the Provider node in `models.json`; this prevents a deleted Provider ID from silently taking ownership of an unrelated native Provider that later reuses the same ID. Native providers without this ownership evidence remain unmanaged: saving unrelated settings does not rewrite their existing headers or unknown native fields. Built-in Pi providers cannot be edited or deleted through this extension.
|
|
243
|
+
|
|
244
|
+
Plugin configuration writes are serialized with a cross-process lock, and `enabledModels` also honors Pi's `proper-lockfile` lock. An intent journal makes the `models.json` / `state.json` pair recoverable after interruption, and readers reject an in-progress half-written pair. External editors do not honor these locks, so content hashes are still checked before saving; an external change cancels the save instead of being overwritten.
|
|
149
245
|
|
|
150
246
|
## Secrets and security
|
|
151
247
|
|
|
@@ -203,4 +299,4 @@ Thanks to the [LINUX DO](https://linux.do/) community for discussion, sharing, a
|
|
|
203
299
|
|
|
204
300
|
## License
|
|
205
301
|
|
|
206
|
-
|
|
302
|
+
This project is licensed under the [GNU Affero General Public License v3.0 only](./LICENSE). Modified distributions must provide the corresponding source, remain under AGPL-3.0, and identify their changes; they must not represent themselves as official releases. See [NOTICE](./NOTICE).
|
package/README.md
CHANGED
|
@@ -4,48 +4,137 @@
|
|
|
4
4
|
|
|
5
5
|
[](https://github.com/earendil-works/pi)
|
|
6
6
|
[](./LICENSE)
|
|
7
|
-
[](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.
|
|
11
|
+
> 当前稳定版为 `0.3.0`,要求 Pi `>=0.82.0`。
|
|
12
12
|
|
|
13
13
|
## 界面预览
|
|
14
14
|
|
|
15
|
-
|
|
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
|
|
40
|
+
<summary>展开其余四个界面</summary>
|
|
19
41
|
|
|
20
|
-
###
|
|
42
|
+
### 接入内的模型列表
|
|
21
43
|
|
|
22
|
-
|
|
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
|
-
|
|
54
|
+
↑↓ 选择 Enter 编辑模型 A 添加模型 E 编辑接入 D 删除模型 Esc 返回 / 搜索
|
|
55
|
+
────────────────────────────────────────────────────────────────────────────────────────
|
|
56
|
+
```
|
|
27
57
|
|
|
28
|
-
###
|
|
58
|
+
### 接入配置
|
|
29
59
|
|
|
30
|
-
|
|
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
|
-
|
|
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
|
-
-
|
|
131
|
+
- 配置上下文窗口、最大输出、视觉支持和 Thinking。
|
|
43
132
|
- 支持 Anthropic Adaptive Thinking 与 Legacy Thinking。
|
|
44
133
|
- 可为 OpenAI Responses 模型启用 `service_tier=priority`(Fast mode)。
|
|
45
134
|
- 为每个接入单独配置直连或 HTTP(S) 代理。
|
|
46
135
|
- 提供自动推荐、禁用、Claude Code、Codex 和自定义请求头模式。
|
|
47
136
|
- API key 支持字面值、`$ENV_VAR` / `${ENV_VAR}` 和 Pi 的 `!command` 引用。
|
|
48
|
-
-
|
|
137
|
+
- 使用跨进程锁与可恢复双文件事务持久化配置,并在保存后重新注册受管理的 Provider。
|
|
49
138
|
|
|
50
139
|
## 安装
|
|
51
140
|
|
|
@@ -89,12 +178,15 @@ pi install npm:pi-model-manager
|
|
|
89
178
|
| 按键 | 操作 |
|
|
90
179
|
| --- | --- |
|
|
91
180
|
| `Enter` | 进入所选接入并管理模型 |
|
|
92
|
-
| `
|
|
93
|
-
| `
|
|
94
|
-
| `
|
|
181
|
+
| `N` | 新建接入及其第一个模型 |
|
|
182
|
+
| `D` | 删除所选接入 |
|
|
183
|
+
| `H` | 管理可复用请求头 |
|
|
184
|
+
| `/` | 搜索当前列表;`Tab` 退出输入但保留过滤,`Esc` 清空 |
|
|
95
185
|
| `Esc` | 返回或退出 |
|
|
96
186
|
|
|
97
|
-
|
|
187
|
+
单字母快捷键不区分大小写;底部提示按终端宽度折行,窄终端下也不会丢失。
|
|
188
|
+
|
|
189
|
+
4. 在编辑器中使用 `↑` / `↓` 选择字段,按 `Enter` 编辑;开关类字段可用 `←` / `→` 就地切换;按 `Ctrl+S` 保存。
|
|
98
190
|
|
|
99
191
|
保存会更新并启用模型,但不会强制切换当前会话正在使用的模型。
|
|
100
192
|
|
|
@@ -110,14 +202,16 @@ pi install npm:pi-model-manager
|
|
|
110
202
|
- 接入级 HTTP(S) 代理
|
|
111
203
|
- 一个或多个模型
|
|
112
204
|
|
|
113
|
-
|
|
205
|
+
新建模型时,扩展会尝试从上游读取模型列表;整次发现(包括认证回退)共用一个 10 秒上限,可按 `Esc` 手动取消。失败或取消后仍可手动输入模型 ID。
|
|
206
|
+
|
|
207
|
+
Base URL 在填入时就会归一化为各协议 SDK 可直接使用的根地址:OpenAI 系补上 `/v1`,Anthropic 剔除 `/v1`,Google 在官方域名上补 `/v1beta`;你显式填的非根路径(如 `https://gw.example.com/custom`)不会被改写。切换 API 协议时地址会按新协议重新归一化。因此 `models.json` 里存的就是实际请求根地址,本扩展未加载时 Pi 也能直接使用。
|
|
114
208
|
|
|
115
209
|
### 模型能力
|
|
116
210
|
|
|
117
211
|
模型编辑器支持:
|
|
118
212
|
|
|
119
213
|
- 显示名称
|
|
120
|
-
-
|
|
214
|
+
- 视觉支持(仅文本或文本 + 图片输入)
|
|
121
215
|
- Thinking 开关
|
|
122
216
|
- Anthropic Adaptive/Legacy Thinking 协议
|
|
123
217
|
- OpenAI Responses Fast mode
|
|
@@ -135,8 +229,8 @@ pi install npm:pi-model-manager
|
|
|
135
229
|
|
|
136
230
|
当前内置值来自真实客户端请求并已移除认证信息:
|
|
137
231
|
|
|
138
|
-
- Claude Code `2.1.
|
|
139
|
-
- Codex TUI `0.
|
|
232
|
+
- Claude Code `2.1.232`
|
|
233
|
+
- Codex TUI `0.147.0`
|
|
140
234
|
|
|
141
235
|
这些值只用于兼容需要识别客户端身份的 API 中转,不代替 API key。公开仓库和 npm 包**不包含请求捕获工具、用户抓包、认证头或本机状态**。如果内置值不适合你的服务,请关闭身份头或创建自定义请求头。
|
|
142
236
|
|
|
@@ -149,7 +243,9 @@ pi install npm:pi-model-manager
|
|
|
149
243
|
| `~/.pi/agent/models.json` | Pi 原生接入与模型定义;模型配置的唯一权威来源 |
|
|
150
244
|
| `~/.pi/agent/extensions/pi-model-manager/state.json` | 请求头选择、自定义请求头、代理开关和 Fast mode 等扩展私有元数据 |
|
|
151
245
|
|
|
152
|
-
|
|
246
|
+
扩展只会为明确受管理的 Provider 生成请求头、代理路由和动态注册配置。所有权由 `state.json` 的受管理 ID 与 `models.json` Provider 节点中的 `piModelManager.managed` 标记共同确认,防止已删除的 Provider ID 在日后被同名原生配置复用时遭到插件接管。没有这些所有权信息的原生 Provider 保持未管理,其已有 Header 和未知原生字段不会因保存其它配置而被改写;Pi 内置 Provider 不在本扩展中提供编辑或删除入口。
|
|
247
|
+
|
|
248
|
+
插件自身的配置写入由跨进程锁串行化,`enabledModels` 还同时遵守 Pi 的 `proper-lockfile` 锁;`models.json` 与 `state.json` 通过事务意图文件在中断后恢复,读取方不会采用事务进行中的半完成组合。外部编辑器不受这些锁约束,因此保存前仍会校验内容签名;检测到外部修改时会取消保存而不是覆盖。
|
|
153
249
|
|
|
154
250
|
## 密钥与安全
|
|
155
251
|
|
|
@@ -207,4 +303,4 @@ npm pack --dry-run
|
|
|
207
303
|
|
|
208
304
|
## 许可证
|
|
209
305
|
|
|
210
|
-
|
|
306
|
+
本项目采用 [GNU Affero General Public License v3.0 only](./LICENSE)。分发修改版本时必须公开对应源码、继续使用 AGPL-3.0,并明确标注改动;修改版不得冒充官方发行。详见 [NOTICE](./NOTICE)。
|
package/atomic-write.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
// Windows 友好的配置文件持久化写入。
|
|
2
2
|
//
|
|
3
3
|
// 目标文件可能被正在运行的 pi、编辑器、杀软或索引器短暂占用。
|
|
4
|
-
// 直接 rename(tmp, target) 在 Windows
|
|
5
|
-
//
|
|
4
|
+
// 直接 rename(tmp, target) 在 Windows 上可能因短暂占用失败;这里只进行有限重试,
|
|
5
|
+
// 不以非原子的 copy 覆盖冒充成功,最终失败时保留完整临时文件供恢复。
|
|
6
6
|
|
|
7
7
|
import { randomUUID } from "node:crypto";
|
|
8
|
-
import {
|
|
8
|
+
import { mkdir, rename, unlink, writeFile } from "node:fs/promises";
|
|
9
9
|
import { dirname } from "node:path";
|
|
10
10
|
|
|
11
11
|
const REPLACE_RETRY_DELAYS_MS = [25, 50, 100, 200, 400, 800] as const;
|
|
@@ -53,35 +53,7 @@ async function renameWithRetry(sourcePath: string, targetPath: string): Promise<
|
|
|
53
53
|
throw lastError;
|
|
54
54
|
}
|
|
55
55
|
|
|
56
|
-
async function copyOverTargetWithRetry(sourcePath: string, targetPath: string): Promise<void> {
|
|
57
|
-
let lastError: unknown;
|
|
58
|
-
for (let attempt = 0; attempt <= REPLACE_RETRY_DELAYS_MS.length; attempt += 1) {
|
|
59
|
-
try {
|
|
60
|
-
await copyFile(sourcePath, targetPath);
|
|
61
|
-
await removeTempFile(sourcePath);
|
|
62
|
-
return;
|
|
63
|
-
} catch (error) {
|
|
64
|
-
lastError = error;
|
|
65
|
-
if (!isTransientReplaceError(error) || attempt === REPLACE_RETRY_DELAYS_MS.length) break;
|
|
66
|
-
await sleep(REPLACE_RETRY_DELAYS_MS[attempt]!);
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
throw lastError;
|
|
70
|
-
}
|
|
71
56
|
|
|
72
|
-
async function replaceTargetFile(sourcePath: string, targetPath: string): Promise<void> {
|
|
73
|
-
try {
|
|
74
|
-
await renameWithRetry(sourcePath, targetPath);
|
|
75
|
-
} catch (renameError) {
|
|
76
|
-
if (process.platform !== "win32" || !isTransientReplaceError(renameError)) throw renameError;
|
|
77
|
-
// [喵喵喵]: Windows 上 rename 覆盖会被短暂文件锁拦截;重试后降级 copyFile,避免配置保存停留在 tmp (2026-06-21)
|
|
78
|
-
try {
|
|
79
|
-
await copyOverTargetWithRetry(sourcePath, targetPath);
|
|
80
|
-
} catch (copyError) {
|
|
81
|
-
throw new Error(`rename 失败:${formatError(renameError)};copy 覆盖也失败:${formatError(copyError)}`);
|
|
82
|
-
}
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
57
|
|
|
86
58
|
export async function atomicWriteText(targetPath: string, content: string): Promise<void> {
|
|
87
59
|
await mkdir(dirname(targetPath), { recursive: true });
|
|
@@ -91,7 +63,7 @@ export async function atomicWriteText(targetPath: string, content: string): Prom
|
|
|
91
63
|
try {
|
|
92
64
|
await writeFile(tempPath, content, "utf8");
|
|
93
65
|
tempHasCompleteContent = true;
|
|
94
|
-
await
|
|
66
|
+
await renameWithRetry(tempPath, targetPath);
|
|
95
67
|
} catch (error) {
|
|
96
68
|
if (!tempHasCompleteContent) await removeTempFile(tempPath);
|
|
97
69
|
const recoveryNote = tempHasCompleteContent ? `;完整内容已保留在临时文件:${tempPath}` : "";
|
package/common.ts
CHANGED
|
@@ -16,15 +16,6 @@ export function trimOrFallback(value: string | undefined, fallback: string): str
|
|
|
16
16
|
return trimmed ? trimmed : fallback;
|
|
17
17
|
}
|
|
18
18
|
|
|
19
|
-
export function trimSingleLine(value: string): string {
|
|
20
|
-
return value.replace(/[\r\n]+/g, " ").trim();
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export function trimSingleLineOrFallback(value: string | undefined, fallback: string): string {
|
|
24
|
-
const singleLine = value?.replace(/[\r\n]+/g, " ").trim();
|
|
25
|
-
return singleLine ? singleLine : fallback;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
19
|
export function formatUnknownError(error: unknown): string {
|
|
29
20
|
return error instanceof Error ? error.message : String(error);
|
|
30
21
|
}
|
|
@@ -56,11 +47,3 @@ export function stringifyJson(value: unknown): string {
|
|
|
56
47
|
export function cloneJson<T>(value: T): T {
|
|
57
48
|
return value === undefined ? value : JSON.parse(JSON.stringify(value));
|
|
58
49
|
}
|
|
59
|
-
|
|
60
|
-
export function slugifyName(name: string): string {
|
|
61
|
-
return name
|
|
62
|
-
.trim()
|
|
63
|
-
.toLowerCase()
|
|
64
|
-
.replace(/[^a-z0-9._-]+/g, "-")
|
|
65
|
-
.replace(/^-+|-+$/g, "");
|
|
66
|
-
}
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
// 跨 Pi 进程的配置 mutation 锁。只约束遵守本锁的插件写入,外部编辑器冲突另由内容签名检测。
|
|
2
|
+
|
|
3
|
+
import { hostname } from "node:os";
|
|
4
|
+
import { open, mkdir, readFile, stat, unlink, type FileHandle } from "node:fs/promises";
|
|
5
|
+
import { join } from "node:path";
|
|
6
|
+
import { randomUUID } from "node:crypto";
|
|
7
|
+
import { STATE_DIR } from "./state-metadata-store.ts";
|
|
8
|
+
|
|
9
|
+
interface ConfigurationLockRecord {
|
|
10
|
+
token: string;
|
|
11
|
+
pid: number;
|
|
12
|
+
hostname: string;
|
|
13
|
+
startedAt: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
interface HeldConfigurationLock {
|
|
17
|
+
handle: FileHandle;
|
|
18
|
+
record: ConfigurationLockRecord;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const LOCK_PATH = join(STATE_DIR, "config.lock");
|
|
22
|
+
const LOCK_RETRY_DELAYS_MS = [50, 100, 200, 400, 800] as const;
|
|
23
|
+
const INVALID_LOCK_STALE_MS = 30_000;
|
|
24
|
+
|
|
25
|
+
function sleep(ms: number): Promise<void> {
|
|
26
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function getErrorCode(error: unknown): string | undefined {
|
|
30
|
+
return error instanceof Error && "code" in error
|
|
31
|
+
? String((error as NodeJS.ErrnoException).code)
|
|
32
|
+
: undefined;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function parseLockRecord(source: string): ConfigurationLockRecord | undefined {
|
|
36
|
+
try {
|
|
37
|
+
const parsed = JSON.parse(source) as Partial<ConfigurationLockRecord>;
|
|
38
|
+
if (typeof parsed.token !== "string" || !parsed.token) return undefined;
|
|
39
|
+
if (!Number.isInteger(parsed.pid) || (parsed.pid ?? 0) <= 0) return undefined;
|
|
40
|
+
if (typeof parsed.hostname !== "string" || !parsed.hostname) return undefined;
|
|
41
|
+
if (typeof parsed.startedAt !== "string" || !parsed.startedAt) return undefined;
|
|
42
|
+
return parsed as ConfigurationLockRecord;
|
|
43
|
+
} catch {
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
function isSameHostProcessRunning(record: ConfigurationLockRecord): boolean {
|
|
49
|
+
if (record.hostname !== hostname()) return true;
|
|
50
|
+
try {
|
|
51
|
+
process.kill(record.pid, 0);
|
|
52
|
+
return true;
|
|
53
|
+
} catch (error) {
|
|
54
|
+
return getErrorCode(error) !== "ESRCH";
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function removeInactiveLock(): Promise<boolean> {
|
|
59
|
+
let source: string;
|
|
60
|
+
try {
|
|
61
|
+
source = await readFile(LOCK_PATH, "utf8");
|
|
62
|
+
} catch (error) {
|
|
63
|
+
return getErrorCode(error) === "ENOENT";
|
|
64
|
+
}
|
|
65
|
+
const record = parseLockRecord(source);
|
|
66
|
+
if (record && isSameHostProcessRunning(record)) return false;
|
|
67
|
+
if (!record) {
|
|
68
|
+
try {
|
|
69
|
+
const lockStats = await stat(LOCK_PATH);
|
|
70
|
+
if (Date.now() - lockStats.mtimeMs < INVALID_LOCK_STALE_MS) return false;
|
|
71
|
+
} catch (error) {
|
|
72
|
+
return getErrorCode(error) === "ENOENT";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
try {
|
|
76
|
+
await unlink(LOCK_PATH);
|
|
77
|
+
return true;
|
|
78
|
+
} catch (error) {
|
|
79
|
+
return getErrorCode(error) === "ENOENT";
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function acquireConfigurationLock(): Promise<HeldConfigurationLock> {
|
|
84
|
+
await mkdir(STATE_DIR, { recursive: true });
|
|
85
|
+
const record: ConfigurationLockRecord = {
|
|
86
|
+
token: randomUUID(),
|
|
87
|
+
pid: process.pid,
|
|
88
|
+
hostname: hostname(),
|
|
89
|
+
startedAt: new Date().toISOString(),
|
|
90
|
+
};
|
|
91
|
+
|
|
92
|
+
for (let attempt = 0; attempt <= LOCK_RETRY_DELAYS_MS.length; attempt += 1) {
|
|
93
|
+
try {
|
|
94
|
+
const handle = await open(LOCK_PATH, "wx");
|
|
95
|
+
try {
|
|
96
|
+
await handle.writeFile(`${JSON.stringify(record)}\n`, "utf8");
|
|
97
|
+
await handle.sync();
|
|
98
|
+
return { handle, record };
|
|
99
|
+
} catch (error) {
|
|
100
|
+
await handle.close().catch(() => undefined);
|
|
101
|
+
await unlink(LOCK_PATH).catch(() => undefined);
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
} catch (error) {
|
|
105
|
+
if (getErrorCode(error) !== "EEXIST") throw error;
|
|
106
|
+
if (await removeInactiveLock()) continue;
|
|
107
|
+
if (attempt === LOCK_RETRY_DELAYS_MS.length) break;
|
|
108
|
+
await sleep(LOCK_RETRY_DELAYS_MS[attempt]!);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
throw new Error(`另一个 Pi 进程正在保存模型配置;无法在限定时间内获取锁:${LOCK_PATH}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async function releaseConfigurationLock(lock: HeldConfigurationLock): Promise<void> {
|
|
115
|
+
await lock.handle.close();
|
|
116
|
+
let current: ConfigurationLockRecord | undefined;
|
|
117
|
+
try {
|
|
118
|
+
current = parseLockRecord(await readFile(LOCK_PATH, "utf8"));
|
|
119
|
+
} catch (error) {
|
|
120
|
+
if (getErrorCode(error) === "ENOENT") return;
|
|
121
|
+
throw error;
|
|
122
|
+
}
|
|
123
|
+
if (current?.token !== lock.record.token) {
|
|
124
|
+
throw new Error(`配置锁所有权在释放前发生变化,已保留锁文件供检查:${LOCK_PATH}`);
|
|
125
|
+
}
|
|
126
|
+
await unlink(LOCK_PATH);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
export async function withConfigurationLock<T>(operation: () => Promise<T>): Promise<T> {
|
|
130
|
+
const lock = await acquireConfigurationLock();
|
|
131
|
+
try {
|
|
132
|
+
return await operation();
|
|
133
|
+
} finally {
|
|
134
|
+
await releaseConfigurationLock(lock);
|
|
135
|
+
}
|
|
136
|
+
}
|