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/tui/models-json-panel.ts
DELETED
|
@@ -1,269 +0,0 @@
|
|
|
1
|
-
// tui/models-json-panel.ts
|
|
2
|
-
//
|
|
3
|
-
// 二级面板:查看 / 管理 pi 的 ~/.pi/agent/models.json。
|
|
4
|
-
//
|
|
5
|
-
// 说明:models.json 是 provider/model 定义权威源;本面板是高级入口。
|
|
6
|
-
// 删除扩展管理中的同名条目时,会同步清理插件元数据、enabledModels 与当前运行时注册。
|
|
7
|
-
|
|
8
|
-
import type { ExtensionAPI, ExtensionCommandContext } from "@earendil-works/pi-coding-agent";
|
|
9
|
-
import { formatUnknownError } from "../common.ts";
|
|
10
|
-
import {
|
|
11
|
-
MODELS_JSON_PATH,
|
|
12
|
-
readModelsJson,
|
|
13
|
-
type ModelsJsonDocument,
|
|
14
|
-
type ModelsJsonModelEntry,
|
|
15
|
-
type ModelsJsonProviderEntry,
|
|
16
|
-
} from "../models-json-manager.ts";
|
|
17
|
-
import { deleteModelsJsonModelConfiguration, deleteModelsJsonProviderConfiguration } from "../models-json-mutations.ts";
|
|
18
|
-
import { getModelFullId } from "../state-document.ts";
|
|
19
|
-
import { readState } from "../state-store.ts";
|
|
20
|
-
import { showPersistentShortcutMenu, type MenuCursor } from "./persistent-menu.ts";
|
|
21
|
-
import { formatTableHeader, joinFixedColumns } from "./ui-helpers.ts";
|
|
22
|
-
|
|
23
|
-
interface ProviderRow {
|
|
24
|
-
providerId: string;
|
|
25
|
-
label: string;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
interface ProviderMenuRow {
|
|
29
|
-
modelId: string;
|
|
30
|
-
model: ModelsJsonModelEntry;
|
|
31
|
-
label: string;
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function modelCount(entry: ModelsJsonProviderEntry): number {
|
|
35
|
-
return Array.isArray(entry.models) ? entry.models.length : 0;
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
function formatApiLabel(api: string | undefined): string {
|
|
39
|
-
if (api === "openai-responses") return "Responses";
|
|
40
|
-
if (api === "openai-completions") return "Chat";
|
|
41
|
-
if (api === "anthropic-messages") return "Claude";
|
|
42
|
-
if (api === "google-generative-ai") return "Gemini";
|
|
43
|
-
return api ?? "未知";
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
function formatTokenLimit(value: unknown): string {
|
|
47
|
-
if (typeof value !== "number" || !Number.isFinite(value)) return "-";
|
|
48
|
-
if (value >= 1_000_000) return `${(value / 1_000_000).toFixed(value % 1_000_000 === 0 ? 0 : 1)}M`;
|
|
49
|
-
if (value >= 1_000) return `${Math.round(value / 1_000)}K`;
|
|
50
|
-
return String(value);
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function sanitizeEndpoint(value: string | undefined): string {
|
|
54
|
-
return (value ?? "<未配置>").replace(/([?&](?:key|api_key|api-key)=)[^&]+/gi, "$1REDACTED");
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function formatModelInput(input: unknown): string {
|
|
58
|
-
return Array.isArray(input) && input.includes("image") ? "文本,视觉" : "文本";
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
function formatNativeModelNameCell(model: ModelsJsonModelEntry): string {
|
|
62
|
-
return model.name && model.name !== model.id ? model.name : "默认";
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
function formatNativeModelThinkingCell(model: ModelsJsonModelEntry): string {
|
|
66
|
-
return model.reasoning === true ? "开" : "关";
|
|
67
|
-
}
|
|
68
|
-
|
|
69
|
-
const NATIVE_PROVIDER_HEADER = formatTableHeader(joinFixedColumns([
|
|
70
|
-
{ text: "Provider", width: 24 },
|
|
71
|
-
{ text: "API", width: 9 },
|
|
72
|
-
{ text: "Models", width: 6, align: "right" },
|
|
73
|
-
{ text: "Source", width: 8 },
|
|
74
|
-
]));
|
|
75
|
-
|
|
76
|
-
const NATIVE_MODEL_HEADER = formatTableHeader(joinFixedColumns([
|
|
77
|
-
{ text: "模型 ID", width: 30 },
|
|
78
|
-
{ text: "显示名", width: 16 },
|
|
79
|
-
{ text: "输入", width: 10 },
|
|
80
|
-
{ text: "Thinking", width: 8 },
|
|
81
|
-
{ text: "上下文", width: 7, align: "right" },
|
|
82
|
-
{ text: "输出", width: 7, align: "right" },
|
|
83
|
-
{ text: "Headers", width: 8, align: "right" },
|
|
84
|
-
]));
|
|
85
|
-
|
|
86
|
-
function formatNativeModelRow(model: ModelsJsonModelEntry): string {
|
|
87
|
-
return joinFixedColumns([
|
|
88
|
-
{ text: model.id, width: 30 },
|
|
89
|
-
{ text: formatNativeModelNameCell(model), width: 16 },
|
|
90
|
-
{ text: formatModelInput(model.input), width: 10 },
|
|
91
|
-
{ text: formatNativeModelThinkingCell(model), width: 8 },
|
|
92
|
-
{ text: formatTokenLimit(model.contextWindow), width: 7, align: "right" },
|
|
93
|
-
{ text: formatTokenLimit(model.maxTokens), width: 7, align: "right" },
|
|
94
|
-
{ text: String(Object.keys(model.headers ?? {}).length), width: 8, align: "right" },
|
|
95
|
-
]);
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
function formatProviderRow(providerId: string, entry: ModelsJsonProviderEntry, managedInState: boolean): string {
|
|
99
|
-
return joinFixedColumns([
|
|
100
|
-
{ text: providerId, width: 24 },
|
|
101
|
-
{ text: formatApiLabel(entry.api), width: 9 },
|
|
102
|
-
{ text: String(modelCount(entry)), width: 6, align: "right" },
|
|
103
|
-
{ text: managedInState ? "managed" : "native", width: 8 },
|
|
104
|
-
]);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
function formatProviderDetailLines(providerId: string, entry: ModelsJsonProviderEntry, managedInState: boolean): string[] {
|
|
108
|
-
const title = entry.name && entry.name !== providerId ? `${entry.name} (${providerId})` : providerId;
|
|
109
|
-
return [
|
|
110
|
-
title,
|
|
111
|
-
` endpoint ${sanitizeEndpoint(entry.baseUrl)}`,
|
|
112
|
-
` api ${formatApiLabel(entry.api)} · ${modelCount(entry)} models · ${managedInState ? "extension metadata" : "native only"}`,
|
|
113
|
-
` source ${MODELS_JSON_PATH}`,
|
|
114
|
-
];
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function buildProviderRows(document: ModelsJsonDocument, managedIds: ReadonlySet<string>): ProviderRow[] {
|
|
118
|
-
return Object.keys(document.providers)
|
|
119
|
-
.sort((a, b) => a.localeCompare(b))
|
|
120
|
-
.map((providerId) => ({
|
|
121
|
-
providerId,
|
|
122
|
-
label: formatProviderRow(providerId, document.providers[providerId]!, managedIds.has(providerId)),
|
|
123
|
-
}));
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
function buildProviderMenuRows(entry: ModelsJsonProviderEntry): ProviderMenuRow[] {
|
|
127
|
-
return [...(entry.models ?? [])]
|
|
128
|
-
.sort((a, b) => a.id.localeCompare(b.id))
|
|
129
|
-
.map((model) => ({
|
|
130
|
-
modelId: model.id,
|
|
131
|
-
model,
|
|
132
|
-
label: formatNativeModelRow(model),
|
|
133
|
-
}));
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
async function deleteProvider(pi: ExtensionAPI, ctx: ExtensionCommandContext, providerId: string): Promise<void> {
|
|
138
|
-
const ok = await ctx.ui.confirm(`删除 ${providerId}`, "将从 models.json 删除整个接入,并同步清理插件元数据与 enabledModels。");
|
|
139
|
-
if (!ok) return;
|
|
140
|
-
try {
|
|
141
|
-
await deleteModelsJsonProviderConfiguration(pi, ctx, providerId);
|
|
142
|
-
} catch (error) {
|
|
143
|
-
ctx.ui.notify(`删除失败:${formatUnknownError(error)}`, "error");
|
|
144
|
-
}
|
|
145
|
-
}
|
|
146
|
-
|
|
147
|
-
async function deleteModel(pi: ExtensionAPI, ctx: ExtensionCommandContext, providerId: string, modelId: string): Promise<void> {
|
|
148
|
-
const fullId = getModelFullId(providerId, modelId);
|
|
149
|
-
const docForPrompt = await readModelsJson();
|
|
150
|
-
const modelCount = docForPrompt.providers[providerId]?.models?.length ?? 0;
|
|
151
|
-
const ok = await ctx.ui.confirm(
|
|
152
|
-
`删除 ${fullId}`,
|
|
153
|
-
modelCount <= 1 ? "这是该接入下最后一个模型;删除后该接入也会从 models.json 中移除。" : "只从 models.json 的模型列表移除该模型。",
|
|
154
|
-
);
|
|
155
|
-
if (!ok) return;
|
|
156
|
-
try {
|
|
157
|
-
await deleteModelsJsonModelConfiguration(pi, ctx, providerId, modelId);
|
|
158
|
-
} catch (error) {
|
|
159
|
-
ctx.ui.notify(`删除失败:${formatUnknownError(error)}`, "error");
|
|
160
|
-
}
|
|
161
|
-
}
|
|
162
|
-
|
|
163
|
-
type ModelsJsonProviderShortcut = "delete-model";
|
|
164
|
-
|
|
165
|
-
async function showProviderMenu(
|
|
166
|
-
pi: ExtensionAPI,
|
|
167
|
-
ctx: ExtensionCommandContext,
|
|
168
|
-
providerId: string,
|
|
169
|
-
managedInState: boolean,
|
|
170
|
-
): Promise<void> {
|
|
171
|
-
const cursor: MenuCursor = { index: 0 };
|
|
172
|
-
while (true) {
|
|
173
|
-
const doc = await readModelsJson();
|
|
174
|
-
const entry = doc.providers[providerId];
|
|
175
|
-
if (!entry) {
|
|
176
|
-
ctx.ui.notify(`接入不存在:${providerId}`, "warning");
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
const rows = buildProviderMenuRows(entry);
|
|
180
|
-
const action = await showPersistentShortcutMenu<ModelsJsonProviderShortcut>(
|
|
181
|
-
ctx,
|
|
182
|
-
`/model-manager / 原生配置 / ${providerId}`,
|
|
183
|
-
"",
|
|
184
|
-
rows.map((row, index) => ({ id: `${index}`, label: row.label })),
|
|
185
|
-
cursor,
|
|
186
|
-
[{ input: "d", shortcut: "delete-model" }],
|
|
187
|
-
{
|
|
188
|
-
summaryLines: formatProviderDetailLines(providerId, entry, managedInState).slice(1, 3),
|
|
189
|
-
tableHeader: NATIVE_MODEL_HEADER,
|
|
190
|
-
visibleRows: Math.min(12, Math.max(1, rows.length)),
|
|
191
|
-
footer: "↑↓ 选择 d 删除模型 Esc 返回",
|
|
192
|
-
emptyLabel: "暂无模型;返回上一级可删除接入",
|
|
193
|
-
},
|
|
194
|
-
);
|
|
195
|
-
if (action.type === "cancel") return;
|
|
196
|
-
if (action.type === "shortcut") {
|
|
197
|
-
const selectedModelId = rows[cursor.index]?.modelId;
|
|
198
|
-
if (!selectedModelId) {
|
|
199
|
-
ctx.ui.notify("没有可删除的模型;删除接入请返回上一级按 d。", "info");
|
|
200
|
-
continue;
|
|
201
|
-
}
|
|
202
|
-
await deleteModel(pi, ctx, providerId, selectedModelId);
|
|
203
|
-
continue;
|
|
204
|
-
}
|
|
205
|
-
ctx.ui.notify("models.json 模型只能删除;请按 d 删除选中模型。", "info");
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
type ModelsJsonShortcut = "delete-provider";
|
|
210
|
-
|
|
211
|
-
export async function runModelsJsonPanel(pi: ExtensionAPI, ctx: ExtensionCommandContext): Promise<void> {
|
|
212
|
-
const cursor: MenuCursor = { index: 0 };
|
|
213
|
-
while (true) {
|
|
214
|
-
let document: ModelsJsonDocument;
|
|
215
|
-
let managedIds: Set<string>;
|
|
216
|
-
try {
|
|
217
|
-
document = await readModelsJson();
|
|
218
|
-
managedIds = await readState()
|
|
219
|
-
.then((state) => new Set(Object.keys(state.providers)))
|
|
220
|
-
.catch(() => new Set<string>());
|
|
221
|
-
} catch (error) {
|
|
222
|
-
ctx.ui.notify(`读取 models.json 失败:${formatUnknownError(error)}`, "error");
|
|
223
|
-
return;
|
|
224
|
-
}
|
|
225
|
-
|
|
226
|
-
const rows = buildProviderRows(document, managedIds);
|
|
227
|
-
const menuRows = rows.map((row, index) => ({ id: `${index}`, label: row.label }));
|
|
228
|
-
const action = await showPersistentShortcutMenu<ModelsJsonShortcut>(
|
|
229
|
-
ctx,
|
|
230
|
-
"/model-manager / 原生配置",
|
|
231
|
-
"",
|
|
232
|
-
menuRows,
|
|
233
|
-
cursor,
|
|
234
|
-
[{ input: "d", shortcut: "delete-provider" }],
|
|
235
|
-
{
|
|
236
|
-
summaryLines: [
|
|
237
|
-
"Pi / workflow 直接读取的最终模型配置",
|
|
238
|
-
MODELS_JSON_PATH,
|
|
239
|
-
],
|
|
240
|
-
tableHeader: NATIVE_PROVIDER_HEADER,
|
|
241
|
-
getDetailLines: (selectedRow) => {
|
|
242
|
-
const row = rows[Number.parseInt(selectedRow?.id ?? "", 10)];
|
|
243
|
-
const entry = row ? document.providers[row.providerId] : undefined;
|
|
244
|
-
return row && entry ? formatProviderDetailLines(row.providerId, entry, managedIds.has(row.providerId)) : [];
|
|
245
|
-
},
|
|
246
|
-
footer: "↑↓ 选择 Enter 查看接入 d 删除接入 Esc 返回",
|
|
247
|
-
emptyLabel: "models.json 中暂无接入",
|
|
248
|
-
},
|
|
249
|
-
);
|
|
250
|
-
if (action.type === "cancel") return;
|
|
251
|
-
if (action.type === "shortcut") {
|
|
252
|
-
const selectedProviderId = rows[cursor.index]?.providerId;
|
|
253
|
-
if (!selectedProviderId) {
|
|
254
|
-
ctx.ui.notify("没有可删除的接入。", "info");
|
|
255
|
-
continue;
|
|
256
|
-
}
|
|
257
|
-
await deleteProvider(pi, ctx, selectedProviderId);
|
|
258
|
-
continue;
|
|
259
|
-
}
|
|
260
|
-
const index = Number.parseInt(action.id, 10);
|
|
261
|
-
if (!Number.isFinite(index) || index < 0 || index >= rows.length) continue;
|
|
262
|
-
const row = rows[index]!;
|
|
263
|
-
await showProviderMenu(pi, ctx, row.providerId, managedIds.has(row.providerId));
|
|
264
|
-
}
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
export function getModelsJsonPanelPath(): string {
|
|
268
|
-
return MODELS_JSON_PATH;
|
|
269
|
-
}
|