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/tui/ui-helpers.ts CHANGED
@@ -20,6 +20,7 @@ import type {
20
20
  StoredProvider,
21
21
  StoredRequestHeaderProfile,
22
22
  } from "../types.ts";
23
+ import type { Theme, ThemeColor } from "@earendil-works/pi-coding-agent";
23
24
 
24
25
  export function maskSecret(value: string | undefined): string {
25
26
  if (!value) return "<未填写>";
@@ -39,6 +40,7 @@ interface FixedColumn {
39
40
  text: string;
40
41
  width: number;
41
42
  align?: ColumnAlign;
43
+ color?: ThemeColor;
42
44
  }
43
45
 
44
46
  export function fitColumn(text: string, columns: number, align: ColumnAlign = "left"): string {
@@ -47,8 +49,15 @@ export function fitColumn(text: string, columns: number, align: ColumnAlign = "l
47
49
  return align === "right" ? pad + clipped : clipped + pad;
48
50
  }
49
51
 
50
- function joinFixedColumns(columns: readonly FixedColumn[], gap = " "): string {
51
- return columns.map((column) => fitColumn(column.text, column.width, column.align)).join(gap).trimEnd();
52
+ // [喵喵喵]: 先按纯文本对齐再着色;反过来会让列宽计算受 ANSI 序列干扰。
53
+ function joinFixedColumns(columns: readonly FixedColumn[], theme?: Theme, gap = " "): string {
54
+ return columns
55
+ .map((column) => {
56
+ const cell = fitColumn(column.text, column.width, column.align);
57
+ return theme && column.color ? theme.fg(column.color, cell) : cell;
58
+ })
59
+ .join(gap)
60
+ .trimEnd();
52
61
  }
53
62
 
54
63
  function formatTableHeader(line: string): string {
@@ -111,45 +120,45 @@ interface ProviderConsoleCells {
111
120
  status: string;
112
121
  }
113
122
 
114
- function getProviderConsoleColumns(cells: ProviderConsoleCells, availableWidth: number): FixedColumn[] {
115
- if (availableWidth >= 81) {
116
- return [
117
- { text: cells.provider, width: 22 },
118
- { text: cells.api, width: 9 },
119
- { text: cells.models, width: 6, align: "right" },
120
- { text: cells.headers, width: 15 },
121
- { text: cells.proxy, width: 6 },
122
- { text: cells.auth, width: 6 },
123
- { text: cells.status, width: 5 },
124
- ];
125
- }
126
- if (availableWidth >= 64) {
127
- return [
128
- { text: cells.provider, width: 20 },
129
- { text: cells.api, width: 9 },
130
- { text: cells.models, width: 6, align: "right" },
131
- { text: cells.proxy, width: 6 },
132
- { text: cells.auth, width: 6 },
133
- { text: cells.status, width: 5 },
134
- ];
135
- }
136
- if (availableWidth >= 48) {
137
- return [
138
- { text: cells.provider, width: 20 },
139
- { text: cells.api, width: 9 },
140
- { text: cells.models, width: 6, align: "right" },
141
- { text: cells.status, width: 5 },
142
- ];
143
- }
144
- return [
145
- { text: cells.provider, width: Math.max(10, availableWidth - 26) },
146
- { text: cells.api, width: 9 },
147
- { text: cells.models, width: 6, align: "right" },
148
- { text: cells.status, width: 5 },
149
- ];
123
+ export interface ProviderTableOptions {
124
+ requestHeaderProfiles?: Record<string, StoredRequestHeaderProfile>;
125
+ theme?: Theme;
126
+ // 按当前数据收敛后的接入列宽;表头与数据行必须传同一个值才能对齐。
127
+ nameWidth?: number;
150
128
  }
151
129
 
152
- export function formatProviderConsoleHeader(menuWidth: number): string {
130
+ const PROVIDER_NAME_COLUMN_LIMIT = 22;
131
+ const PROVIDER_NAME_COLUMN_MIN = 12;
132
+
133
+ export function getProviderDisplayLabel(providerId: string, provider: StoredProvider): string {
134
+ const name = getProviderDisplayName(providerId, provider);
135
+ return name === providerId ? providerId : `${name} (${providerId})`;
136
+ }
137
+
138
+ export function getProviderNameColumnWidth(displayLabels: readonly string[]): number {
139
+ const longest = displayLabels.reduce((max, label) => Math.max(max, visibleWidth(label)), 0);
140
+ return Math.min(PROVIDER_NAME_COLUMN_LIMIT, Math.max(PROVIDER_NAME_COLUMN_MIN, longest));
141
+ }
142
+
143
+ function getAuthColor(auth: string): ThemeColor | undefined {
144
+ return auth === "miss" || auth === "auth?" ? "warning" : undefined;
145
+ }
146
+
147
+ function getProviderConsoleColumns(cells: ProviderConsoleCells, availableWidth: number, nameWidth: number): FixedColumn[] {
148
+ const provider: FixedColumn = { text: cells.provider, width: Math.min(nameWidth, PROVIDER_NAME_COLUMN_LIMIT) };
149
+ const api: FixedColumn = { text: cells.api, width: 9 };
150
+ const models: FixedColumn = { text: cells.models, width: 6, align: "right" };
151
+ const headers: FixedColumn = { text: cells.headers, width: 15, color: cells.headers === "Off" ? "dim" : undefined };
152
+ const proxy: FixedColumn = { text: cells.proxy, width: 6, color: cells.proxy === "proxy" ? "accent" : "dim" };
153
+ const auth: FixedColumn = { text: cells.auth, width: 6, color: getAuthColor(cells.auth) };
154
+ const status: FixedColumn = { text: cells.status, width: 5, color: cells.status === "ready" ? "success" : "warning" };
155
+ if (availableWidth >= 81) return [provider, api, models, headers, proxy, auth, status];
156
+ if (availableWidth >= 64) return [provider, api, models, proxy, auth, status];
157
+ if (availableWidth >= 48) return [provider, api, models, status];
158
+ return [{ ...provider, width: Math.max(10, availableWidth - 26) }, api, models, status];
159
+ }
160
+
161
+ export function formatProviderConsoleHeader(menuWidth: number, options: ProviderTableOptions = {}): string {
153
162
  return formatTableHeader(joinFixedColumns(getProviderConsoleColumns({
154
163
  provider: "接入",
155
164
  api: "API",
@@ -158,42 +167,52 @@ export function formatProviderConsoleHeader(menuWidth: number): string {
158
167
  proxy: "代理",
159
168
  auth: "认证",
160
169
  status: "状态",
161
- }, Math.max(0, menuWidth - 2))));
170
+ }, Math.max(0, menuWidth - 2), options.nameWidth ?? PROVIDER_NAME_COLUMN_LIMIT)));
162
171
  }
163
172
 
164
173
  export function formatProviderConsoleRow(
165
174
  providerId: string,
166
175
  provider: StoredProvider,
167
- requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
168
176
  availableWidth = 81,
177
+ options: ProviderTableOptions = {},
169
178
  ): string {
170
- const name = getProviderDisplayName(providerId, provider);
171
- const displayName = name === providerId ? providerId : `${name} (${providerId})`;
172
179
  return joinFixedColumns(getProviderConsoleColumns({
173
- provider: displayName,
180
+ provider: getProviderDisplayLabel(providerId, provider),
174
181
  api: formatApiShort(provider.api),
175
182
  models: String(provider.models.length),
176
- headers: formatProviderHeaderProfile(provider, requestHeaderProfiles),
183
+ headers: formatProviderHeaderProfile(provider, options.requestHeaderProfiles ?? {}),
177
184
  proxy: getProviderProxyText(provider),
178
185
  auth: getAuthKind(provider.apiKey),
179
186
  status: getProviderStatus(provider),
180
- }, availableWidth));
187
+ }, availableWidth, options.nameWidth ?? PROVIDER_NAME_COLUMN_LIMIT), options.theme);
188
+ }
189
+
190
+ // [喵喵喵]: 详情区分三级(标题/字段名/值),否则整块同一灰度无法扫读;
191
+ // 缩进与标签列宽必须在所有面板一致,否则切面板时详情区会跳动。
192
+ const DETAIL_LABEL_WIDTH = 10;
193
+
194
+ export function formatDetailTitle(title: string, theme?: Theme): string {
195
+ return theme ? theme.fg("text", title) : title;
196
+ }
197
+
198
+ export function formatDetailField(label: string, value: string, theme?: Theme): string {
199
+ const labelCell = ` ${fitColumn(label, DETAIL_LABEL_WIDTH)}`;
200
+ return theme ? `${theme.fg("dim", labelCell)}${theme.fg("text", value)}` : `${labelCell}${value}`;
181
201
  }
182
202
 
183
203
  export function formatProviderDetailLines(
184
204
  providerId: string,
185
205
  provider: StoredProvider,
186
- requestHeaderProfiles: Record<string, StoredRequestHeaderProfile> = {},
206
+ options: ProviderTableOptions = {},
187
207
  ): string[] {
188
- const name = getProviderDisplayName(providerId, provider);
189
- const title = name === providerId ? providerId : `${name} (${providerId})`;
208
+ const { theme, requestHeaderProfiles = {} } = options;
190
209
  const modelIds = provider.models.map((model) => model.id).join(", ") || "<无模型>";
191
210
  return [
192
- title,
193
- ` endpoint ${redactUrlForDisplay(provider.baseUrl)}`,
194
- ` proxy ${provider.httpProxyEnabled ? redactUrlForDisplay(provider.httpProxyUrl ?? "http://127.0.0.1:7890") : "direct"}`,
195
- ` api ${formatApiShort(provider.api)} · headers ${formatProviderHeaderProfile(provider, requestHeaderProfiles)} · auth ${getAuthKind(provider.apiKey)}`,
196
- ` models ${modelIds}`,
211
+ formatDetailTitle(getProviderDisplayLabel(providerId, provider), theme),
212
+ formatDetailField("endpoint", redactUrlForDisplay(provider.baseUrl), theme),
213
+ formatDetailField("proxy", provider.httpProxyEnabled ? redactUrlForDisplay(provider.httpProxyUrl ?? "http://127.0.0.1:7890") : "direct", theme),
214
+ formatDetailField("api", `${formatApiShort(provider.api)} · headers ${formatProviderHeaderProfile(provider, requestHeaderProfiles)} · auth ${getAuthKind(provider.apiKey)}`, theme),
215
+ formatDetailField("models", modelIds, theme),
197
216
  ];
198
217
  }
199
218
 
@@ -231,42 +250,73 @@ interface ModelListCells {
231
250
  fast: string;
232
251
  }
233
252
 
234
- function getModelListColumns(provider: StoredProvider, cells: ModelListCells, availableWidth: number): FixedColumn[] {
253
+ export interface ModelTableOptions {
254
+ theme?: Theme;
255
+ // 按当前数据收敛后的列宽;表头与数据行必须传同一组值才能对齐。
256
+ modelIdWidth?: number;
257
+ nameWidth?: number;
258
+ }
259
+
260
+ const MODEL_ID_COLUMN_LIMIT = 30;
261
+ const MODEL_ID_COLUMN_MIN = 14;
262
+ const MODEL_NAME_COLUMN_LIMIT = 16;
263
+ const MODEL_NAME_COLUMN_MIN = 8;
264
+
265
+ export function getModelColumnWidths(models: readonly StoredModel[]): Required<Pick<ModelTableOptions, "modelIdWidth" | "nameWidth">> {
266
+ let longestId = 0;
267
+ let longestName = 0;
268
+ for (const model of models) {
269
+ longestId = Math.max(longestId, visibleWidth(model.id));
270
+ longestName = Math.max(longestName, visibleWidth(formatModelNameCell(model)));
271
+ }
272
+ return {
273
+ modelIdWidth: Math.min(MODEL_ID_COLUMN_LIMIT, Math.max(MODEL_ID_COLUMN_MIN, longestId)),
274
+ nameWidth: Math.min(MODEL_NAME_COLUMN_LIMIT, Math.max(MODEL_NAME_COLUMN_MIN, longestName)),
275
+ };
276
+ }
277
+
278
+ function getModelListColumns(
279
+ provider: StoredProvider,
280
+ cells: ModelListCells,
281
+ availableWidth: number,
282
+ options: ModelTableOptions,
283
+ ): FixedColumn[] {
284
+ const modelIdWidth = options.modelIdWidth ?? MODEL_ID_COLUMN_LIMIT;
285
+ const nameWidth = options.nameWidth ?? MODEL_NAME_COLUMN_LIMIT;
286
+ const name: FixedColumn = { text: cells.name, width: nameWidth, color: cells.name === "默认" ? "dim" : undefined };
287
+ const input: FixedColumn = { text: cells.input, width: 10, color: cells.input.includes("视觉") ? "accent" : undefined };
288
+ const thinking: FixedColumn = { text: cells.thinking, width: 8, color: cells.thinking === "开" ? "accent" : cells.thinking === "关" ? "dim" : undefined };
289
+ const context: FixedColumn = { text: cells.context, width: 7, align: "right" };
290
+ const output: FixedColumn = { text: cells.output, width: 7, align: "right" };
291
+ // [喵喵喵]: priority 会消耗 Fast 额度,用 warning 提醒而不是当普通开关。
292
+ const fast: FixedColumn = { text: cells.fast, width: 8, color: cells.fast === "priority" ? "warning" : cells.fast === "off" ? "dim" : undefined };
293
+
235
294
  const fullWidth = provider.api === "openai-responses" ? 98 : 88;
236
295
  if (availableWidth >= fullWidth) {
237
296
  const columns: FixedColumn[] = [
238
- { text: cells.modelId, width: 30 },
239
- { text: cells.name, width: 16 },
240
- { text: cells.input, width: 10 },
241
- { text: cells.thinking, width: 8 },
242
- { text: cells.context, width: 7, align: "right" },
243
- { text: cells.output, width: 7, align: "right" },
297
+ { text: cells.modelId, width: Math.min(modelIdWidth, 30) },
298
+ { ...name, width: Math.min(nameWidth, 16) },
299
+ input,
300
+ thinking,
301
+ context,
302
+ output,
244
303
  ];
245
- if (provider.api === "openai-responses") columns.push({ text: cells.fast, width: 8 });
304
+ if (provider.api === "openai-responses") columns.push(fast);
246
305
  return columns;
247
306
  }
248
307
  if (availableWidth >= 75) {
249
308
  return [
250
- { text: cells.modelId, width: 28 },
251
- { text: cells.name, width: 14 },
252
- { text: cells.input, width: 10 },
253
- { text: cells.thinking, width: 8 },
254
- { text: cells.context, width: 7, align: "right" },
309
+ { text: cells.modelId, width: Math.min(modelIdWidth, 28) },
310
+ { ...name, width: Math.min(nameWidth, 14) },
311
+ input,
312
+ thinking,
313
+ context,
255
314
  ];
256
315
  }
257
316
  if (availableWidth >= 57) {
258
- return [
259
- { text: cells.modelId, width: 26 },
260
- { text: cells.input, width: 10 },
261
- { text: cells.thinking, width: 8 },
262
- { text: cells.context, width: 7, align: "right" },
263
- ];
317
+ return [{ text: cells.modelId, width: Math.min(modelIdWidth, 26) }, input, thinking, context];
264
318
  }
265
- return [
266
- { text: cells.modelId, width: Math.max(10, availableWidth - 22) },
267
- { text: cells.input, width: 10 },
268
- { text: cells.thinking, width: 8 },
269
- ];
319
+ return [{ text: cells.modelId, width: Math.max(10, availableWidth - 22) }, input, thinking];
270
320
  }
271
321
 
272
322
  function getModelListCells(model?: StoredModel): ModelListCells {
@@ -291,12 +341,17 @@ function getModelListCells(model?: StoredModel): ModelListCells {
291
341
  };
292
342
  }
293
343
 
294
- export function formatModelListHeader(provider: StoredProvider, menuWidth = 100): string {
295
- return formatTableHeader(joinFixedColumns(getModelListColumns(provider, getModelListCells(), Math.max(0, menuWidth - 2))));
344
+ export function formatModelListHeader(provider: StoredProvider, menuWidth = 100, options: ModelTableOptions = {}): string {
345
+ return formatTableHeader(joinFixedColumns(getModelListColumns(provider, getModelListCells(), Math.max(0, menuWidth - 2), options)));
296
346
  }
297
347
 
298
- export function formatModelListRow(provider: StoredProvider, model: StoredModel, availableWidth = 98): string {
299
- return joinFixedColumns(getModelListColumns(provider, getModelListCells(model), availableWidth));
348
+ export function formatModelListRow(
349
+ provider: StoredProvider,
350
+ model: StoredModel,
351
+ availableWidth = 98,
352
+ options: ModelTableOptions = {},
353
+ ): string {
354
+ return joinFixedColumns(getModelListColumns(provider, getModelListCells(model), availableWidth, options), options.theme);
300
355
  }
301
356
 
302
357
  export const API_CHOICES: { id: ApiKind; label: string }[] = [
@@ -327,13 +382,14 @@ export function describeProfile(
327
382
 
328
383
  export const VISION_INPUT_CHOICES: { enabled: boolean; kinds: ModelInputKind[]; label: string }[] = [
329
384
  { enabled: false, kinds: ["text"], label: "关闭 — 仅文本输入" },
330
- { enabled: true, kinds: ["text", "image"], label: "开启 — 支持视觉(文本 + 图片)" },
385
+ { enabled: true, kinds: ["text", "image"], label: "开启 — 文本 + 图片输入" },
331
386
  ];
332
387
 
333
388
  export function supportsVisionInput(kinds: ModelInputKind[]): boolean {
334
389
  return kinds.includes("image");
335
390
  }
336
391
 
392
+ // 字段名已经说明了含义,值只需要跟 Thinking / Fast mode 一样给出开关状态。
337
393
  export function describeVisionInput(kinds: ModelInputKind[]): string {
338
- return supportsVisionInput(kinds) ? "开启 · 支持视觉" : "关闭 · 仅文本";
394
+ return supportsVisionInput(kinds) ? "开启" : "关闭";
339
395
  }