pi-model-manager 0.4.5 → 0.4.7

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/dashboard.ts CHANGED
@@ -125,17 +125,17 @@ async function saveProviderDraft(
125
125
  draft: ReturnType<typeof createProviderDraft>,
126
126
  oldProviderId: string | undefined,
127
127
  ): Promise<boolean> {
128
- const state = await readState();
129
- const builtInIds = await getBuiltinProviderIds();
130
- const errors = validateProviderDraft(draft, state, builtInIds, oldProviderId);
131
- if (errors.length > 0) {
132
- notifyValidationErrors(ctx, t("接入配置无效"), errors);
133
- return false;
134
- }
135
- const current = oldProviderId ? state.providers[oldProviderId] : state.providers[draft.providerId];
136
- if (!(await confirmWarnings(ctx, getProviderChangeWarnings(current, draft)))) return false;
137
-
138
128
  try {
129
+ const state = await readState();
130
+ const builtInIds = await getBuiltinProviderIds();
131
+ const errors = validateProviderDraft(draft, state, builtInIds, oldProviderId);
132
+ if (errors.length > 0) {
133
+ notifyValidationErrors(ctx, t("接入配置无效"), errors);
134
+ return false;
135
+ }
136
+ const current = oldProviderId ? state.providers[oldProviderId] : state.providers[draft.providerId];
137
+ if (!(await confirmWarnings(ctx, getProviderChangeWarnings(current, draft)))) return false;
138
+
139
139
  await saveProviderConfiguration(pi, ctx, state, draft, oldProviderId);
140
140
  return true;
141
141
  } catch (error) {
@@ -150,13 +150,13 @@ async function saveModelDraft(
150
150
  draft: ReturnType<typeof createModelDraftForStoredProvider>,
151
151
  replacedModelId: string | undefined,
152
152
  ): Promise<boolean> {
153
- const state = await readState();
154
- const errors = validateModelDraft(draft, state, replacedModelId);
155
- if (errors.length > 0) {
156
- notifyValidationErrors(ctx, t("模型配置无效"), errors);
157
- return false;
158
- }
159
153
  try {
154
+ const state = await readState();
155
+ const errors = validateModelDraft(draft, state, replacedModelId);
156
+ if (errors.length > 0) {
157
+ notifyValidationErrors(ctx, t("模型配置无效"), errors);
158
+ return false;
159
+ }
160
160
  await saveModelConfiguration(pi, ctx, state, draft, replacedModelId);
161
161
  return true;
162
162
  } catch (error) {
@@ -171,20 +171,20 @@ async function saveNewProviderAndModelDraft(
171
171
  providerDraft: ReturnType<typeof createProviderDraft>,
172
172
  modelDraft: ReturnType<typeof createModelDraftForStoredProvider>,
173
173
  ): Promise<boolean> {
174
- const state = await readState();
175
- const builtInIds = await getBuiltinProviderIds();
176
- const providerErrors = validateProviderDraft(providerDraft, state, builtInIds, undefined);
177
- if (providerErrors.length > 0) {
178
- notifyValidationErrors(ctx, t("接入配置无效"), providerErrors);
179
- return false;
180
- }
181
- const providerState = upsertProviderInDocument(state, undefined, providerDraft);
182
- const modelErrors = validateModelDraft(modelDraft, providerState, undefined);
183
- if (modelErrors.length > 0) {
184
- notifyValidationErrors(ctx, t("模型配置无效"), modelErrors);
185
- return false;
186
- }
187
174
  try {
175
+ const state = await readState();
176
+ const builtInIds = await getBuiltinProviderIds();
177
+ const providerErrors = validateProviderDraft(providerDraft, state, builtInIds, undefined);
178
+ if (providerErrors.length > 0) {
179
+ notifyValidationErrors(ctx, t("接入配置无效"), providerErrors);
180
+ return false;
181
+ }
182
+ const providerState = upsertProviderInDocument(state, undefined, providerDraft);
183
+ const modelErrors = validateModelDraft(modelDraft, providerState, undefined);
184
+ if (modelErrors.length > 0) {
185
+ notifyValidationErrors(ctx, t("模型配置无效"), modelErrors);
186
+ return false;
187
+ }
188
188
  await saveNewProviderWithModelConfiguration(pi, ctx, providerState, providerDraft, modelDraft);
189
189
  return true;
190
190
  } catch (error) {
@@ -31,7 +31,7 @@ interface FieldRow {
31
31
 
32
32
 
33
33
  function shouldShowOpenAIServiceTier(draft: ModelDraft): boolean {
34
- return draft.api === "openai-responses";
34
+ return draft.providerManaged && draft.api === "openai-responses";
35
35
  }
36
36
 
37
37
  function shouldShowAnthropicThinkingProtocol(draft: ModelDraft): boolean {
@@ -182,6 +182,13 @@ async function pickModelFromUpstream(
182
182
  ): Promise<void> {
183
183
  const params = {
184
184
  providerId: draft.providerId,
185
+ providerManaged: draft.providerManaged,
186
+ providerHeaders: draft.providerHeaders,
187
+ modelHeaders: draft.modelHeaders,
188
+ compat: {
189
+ ...draft.compat,
190
+ ...(draft.api === "anthropic-messages" ? { forceAdaptiveThinking: draft.anthropicThinkingProtocol === "adaptive" } : {}),
191
+ },
185
192
  api: draft.api,
186
193
  baseUrl: draft.baseUrl,
187
194
  apiKey: draft.apiKey,
@@ -360,6 +367,9 @@ export async function editModel(
360
367
  summaryLines: [
361
368
  t("接入 {providerId} · API {api}", { providerId: draft.providerId, api: formatApiShort(draft.api) }),
362
369
  t("Ctrl+S 保存并启用模型;不切换当前会话模型"),
370
+ ...(!draft.providerManaged && draft.api === "openai-responses"
371
+ ? [t("Fast mode 需要先接管该接入;请先保存接入配置。")]
372
+ : []),
363
373
  ],
364
374
  onAdjust: (id, direction) => {
365
375
  if (!applyHorizontalToggle(draft, id, direction)) return undefined;
@@ -262,13 +262,13 @@ async function saveProfile(
262
262
  draft: RequestHeaderProfileDraft,
263
263
  oldProfileId: string | undefined,
264
264
  ): Promise<boolean> {
265
- const state = await readState();
266
- const errors = validateRequestHeaderProfileDraft(draft, state, oldProfileId);
267
- if (errors.length > 0) {
268
- notifyValidationErrors(ctx, errors);
269
- return false;
270
- }
271
265
  try {
266
+ const state = await readState();
267
+ const errors = validateRequestHeaderProfileDraft(draft, state, oldProfileId);
268
+ if (errors.length > 0) {
269
+ notifyValidationErrors(ctx, errors);
270
+ return false;
271
+ }
272
272
  await saveRequestHeaderProfileConfiguration(pi, ctx, state, draft, oldProfileId);
273
273
  } catch (error) {
274
274
  ctx.ui.notify(t("保存失败:{error}", { error: formatUnknownError(error) }), "error");
@@ -5,10 +5,11 @@ import { ModelRuntime, type ProviderConfig } from "@earendil-works/pi-coding-age
5
5
  import { formatUnknownError } from "../common.ts";
6
6
  import { t } from "../i18n.ts";
7
7
  import { openTemporaryLocalProxyRoute, type TemporaryLocalProxyRoute } from "../local-proxy-service.ts";
8
- import { getClientHeadersForProfile, mergeModelRequestHeaders } from "../presets/client-headers.ts";
8
+ import { mergeModelRequestHeaders } from "../presets/client-headers.ts";
9
+ import { buildModelRequestHeaders } from "../provider-registrar.ts";
9
10
  import { appendUrlPath, resolveRuntimeBaseUrl, validateRequestBaseUrl } from "../runtime-base-url.ts";
10
11
  import { isSensitiveHeaderName, redactSensitiveText } from "../sensitive-redaction.ts";
11
- import type { ApiKind, BuiltInClientHeaderProfileId, ClientHeaderProfileId, ModelListFetchOutcome, StoredClientHeaderCapture } from "../types.ts";
12
+ import type { ApiKind, BuiltInClientHeaderProfileId, ClientHeaderProfileId, CompatSettings, ModelListFetchOutcome, StoredClientHeaderCapture } from "../types.ts";
12
13
  import { extractValidatedModelIds, readBoundedResponseText } from "./model-list-validation.ts";
13
14
 
14
15
  const MODEL_LIST_TIMEOUT_MS = 10_000;
@@ -61,7 +62,7 @@ function collectSensitiveHeaderValues(headers: Record<string, string> | undefine
61
62
 
62
63
  async function resolveFetchAuth(
63
64
  params: FetchModelIdsParams,
64
- profileHeaders: Record<string, string> | undefined,
65
+ modelHeaders: Record<string, string> | undefined,
65
66
  signal: AbortSignal,
66
67
  ): Promise<ResolvedFetchAuth> {
67
68
  const providerConfig: ProviderConfig = {
@@ -70,11 +71,12 @@ async function resolveFetchAuth(
70
71
  apiKey: params.apiKey.trim(),
71
72
  api: params.api,
72
73
  authHeader: params.authHeader,
73
- headers: profileHeaders,
74
+ headers: params.providerHeaders,
74
75
  models: [{
75
76
  id: TEMP_MODEL_ID,
76
77
  name: TEMP_MODEL_ID,
77
78
  api: params.api,
79
+ headers: modelHeaders,
78
80
  reasoning: false,
79
81
  input: ["text"],
80
82
  cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
@@ -162,6 +164,10 @@ async function requestModelIds(
162
164
 
163
165
  export interface FetchModelIdsParams {
164
166
  providerId: string;
167
+ providerManaged?: boolean;
168
+ providerHeaders?: Record<string, string>;
169
+ modelHeaders?: Record<string, string>;
170
+ compat?: CompatSettings;
165
171
  api: ApiKind;
166
172
  baseUrl: string;
167
173
  apiKey: string;
@@ -180,13 +186,20 @@ async function fetchModelIdsWithSignal(
180
186
  ): Promise<ModelListFetchOutcome> {
181
187
  throwIfAborted(signal);
182
188
  validateRequestBaseUrl(params.baseUrl);
183
- const profileHeaders = getClientHeadersForProfile(
184
- params.clientHeaderProfile,
185
- params.api,
186
- params.customClientHeaders,
189
+ const modelHeaders = buildModelRequestHeaders(
190
+ {
191
+ api: params.api,
192
+ managed: params.providerManaged ?? true,
193
+ headers: params.providerHeaders,
194
+ compat: params.compat,
195
+ clientHeaderProfile: params.clientHeaderProfile,
196
+ customClientHeaders: params.customClientHeaders,
197
+ },
198
+ { headers: params.modelHeaders },
199
+ {},
187
200
  params.clientHeaderCaptures ?? {},
188
201
  );
189
- const auth = await resolveFetchAuth(params, profileHeaders, signal);
202
+ const auth = await resolveFetchAuth(params, modelHeaders, signal);
190
203
  redactionSecrets.push(...auth.redactionSecrets);
191
204
  const defaults: Record<string, string> = { accept: "application/json" };
192
205
  if (params.api === "anthropic-messages") {
@@ -223,7 +236,12 @@ export async function fetchModelIds(
223
236
  timedOut = true;
224
237
  controller.abort(new Error(t("模型列表请求总计超时(10 秒)")));
225
238
  }, MODEL_LIST_TIMEOUT_MS);
226
- const redactionSecrets = [params.apiKey.trim()].filter(Boolean);
239
+ const redactionSecrets = [
240
+ params.apiKey.trim(),
241
+ ...collectSensitiveHeaderValues(params.providerHeaders),
242
+ ...collectSensitiveHeaderValues(params.modelHeaders),
243
+ ...collectSensitiveHeaderValues(params.customClientHeaders),
244
+ ].filter(Boolean);
227
245
  try {
228
246
  return await fetchModelIdsWithSignal(params, controller.signal, redactionSecrets);
229
247
  } catch (error) {
@@ -49,6 +49,7 @@ export function extractValidatedModelIds(envelope: unknown, api: ApiKind): strin
49
49
  export async function readBoundedResponseText(response: Response, signal: AbortSignal): Promise<string> {
50
50
  const declaredLength = Number.parseInt(response.headers.get("content-length") ?? "", 10);
51
51
  if (Number.isFinite(declaredLength) && declaredLength > MAX_RESPONSE_BYTES) {
52
+ if (response.body) await waitWithSignal(response.body.cancel("response too large"), signal).catch(() => {});
52
53
  throw new Error(t("模型列表响应体超过 {maxBytes} 字节上限", { maxBytes: MAX_RESPONSE_BYTES }));
53
54
  }
54
55
  if (!response.body) {
package/types.ts CHANGED
@@ -130,6 +130,11 @@ export interface ProviderDraft {
130
130
  export interface ModelDraft {
131
131
  providerId: string;
132
132
  providerName: string;
133
+ providerManaged: boolean;
134
+ /** 模型发现沿用的原生头与兼容参数,不作为表单可编辑字段。 */
135
+ providerHeaders?: Record<string, string>;
136
+ modelHeaders?: Record<string, string>;
137
+ compat?: CompatSettings;
133
138
  api: ApiKind;
134
139
  baseUrl: string;
135
140
  apiKey: string;