@evandu/super-pi 0.1.208 → 0.1.210

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.
Files changed (39) hide show
  1. package/dist/client/.super-pi-client-sources.json +6 -5
  2. package/dist/client/assets/{CodeMirrorEditor-DwrcXEYY.js → CodeMirrorEditor-RP3pl9t8.js} +2 -2
  3. package/dist/client/assets/{dist-DkQ7IzzE.js → dist-1W4-h-5G.js} +1 -1
  4. package/dist/client/assets/{dist-BJxViUrs.js → dist-2DERC2jc.js} +1 -1
  5. package/dist/client/assets/{dist-BiBhoaIj.js → dist-64GDKUQ4.js} +1 -1
  6. package/dist/client/assets/{dist-CJTPl8e8.js → dist-B2WPjdZt.js} +1 -1
  7. package/dist/client/assets/{dist-DzeUf4I1.js → dist-B45IvMTD.js} +1 -1
  8. package/dist/client/assets/{dist-BjPiwDOH.js → dist-BGDz6R25.js} +1 -1
  9. package/dist/client/assets/{dist-BZWoEHZB.js → dist-BUyUzi7c.js} +1 -1
  10. package/dist/client/assets/{dist-CPbgZWr6.js → dist-BofYNQAM.js} +1 -1
  11. package/dist/client/assets/{dist-BtkTzJx-.js → dist-C85BNyGX.js} +1 -1
  12. package/dist/client/assets/{dist-BOEgsxxx.js → dist-CVocw3YE.js} +1 -1
  13. package/dist/client/assets/{dist-CkiHQydv.js → dist-ChsiqcHp.js} +1 -1
  14. package/dist/client/assets/{dist-DgCku0zO.js → dist-Cq0g7kQV.js} +1 -1
  15. package/dist/client/assets/{dist-xtg_Vigb.js → dist-Crt85wjU.js} +1 -1
  16. package/dist/client/assets/{dist-DcrHAVus.js → dist-CzWmai6Y.js} +1 -1
  17. package/dist/client/assets/{dist-BvQXb6Gm.js → dist-DAs0MAI3.js} +1 -1
  18. package/dist/client/assets/{dist-4ak44NZQ.js → dist-DXMAcK9V.js} +1 -1
  19. package/dist/client/assets/{dist-k2Zy_gh4.js → dist-DgYOQRQo.js} +1 -1
  20. package/dist/client/assets/{dist-CmyNC3S-.js → dist-DlB-kPiV.js} +1 -1
  21. package/dist/client/assets/{dist-D8SigXvW.js → dist-Dm7RlgKJ.js} +1 -1
  22. package/dist/client/assets/{dist-B6zrM0u4.js → dist-UVUBSxjO.js} +1 -1
  23. package/dist/client/assets/{dist-DrCHg03L.js → dist-eaFlPvVW.js} +1 -1
  24. package/dist/client/assets/{dist-ZOHc7msq.js → dist-wwor4lbi.js} +1 -1
  25. package/dist/client/assets/{index-1gs_N5Ez.css → index-C2JwfhYR.css} +1 -1
  26. package/dist/client/assets/{index-BWUV4T-z.js → index-DKISE-AV.js} +34 -34
  27. package/dist/client/index.html +2 -2
  28. package/dist/server/application/provider-service.d.ts +2 -0
  29. package/dist/server/application/provider-service.js +11 -1
  30. package/dist/server/http-server.js +22 -0
  31. package/dist/server/provider-network.d.ts +16 -0
  32. package/dist/server/provider-network.js +106 -0
  33. package/dist/server/provider-proxy.d.ts +8 -0
  34. package/dist/server/provider-proxy.js +28 -3
  35. package/dist/server/providers.d.ts +12 -0
  36. package/dist/server/providers.js +52 -3
  37. package/dist/shared/provider-network.d.ts +30 -0
  38. package/dist/shared/provider-network.js +1 -0
  39. package/package.json +3 -1
@@ -8,8 +8,8 @@
8
8
  <link rel="alternate icon" href="/favicon.ico" />
9
9
  <link rel="apple-touch-icon" href="/favicon.svg" />
10
10
  <title>Super Pi</title>
11
- <script type="module" crossorigin src="/assets/index-BWUV4T-z.js"></script>
12
- <link rel="stylesheet" crossorigin href="/assets/index-1gs_N5Ez.css">
11
+ <script type="module" crossorigin src="/assets/index-DKISE-AV.js"></script>
12
+ <link rel="stylesheet" crossorigin href="/assets/index-C2JwfhYR.css">
13
13
  </head>
14
14
  <body>
15
15
  <div id="root"></div>
@@ -70,6 +70,8 @@ export declare class ProviderService {
70
70
  fixed: string[];
71
71
  providers: PublicProvider[];
72
72
  }>;
73
+ network(): Promise<import("../../shared/provider-network.js").SharedProviderProxySettings>;
74
+ setNetwork(input: unknown): Promise<import("../../shared/provider-network.js").SharedProviderProxySettings>;
73
75
  /**
74
76
  * Adds a provider, or adds a model to one, or updates one.
75
77
  *
@@ -1,6 +1,7 @@
1
1
  import { KNOWN_APIS, publicProviders, validateProvider, fetchRemoteModels } from "../providers.js";
2
2
  import { readProviderProxyEnv } from "../provider-proxy.js";
3
3
  import { piAgentDir } from "../providers.js";
4
+ import { hasStoredCredential } from "../provider-network.js";
4
5
  /**
5
6
  * THE PROVIDERS A PERSON ADDED, as a service.
6
7
  *
@@ -86,7 +87,7 @@ export class ProviderService {
86
87
  ...(entry.name ? { name: entry.name } : {}),
87
88
  baseUrl: typeof entry.modelsEntry?.baseUrl === "string" ? entry.modelsEntry.baseUrl : "",
88
89
  api: typeof entry.modelsEntry?.api === "string" ? entry.modelsEntry.api : "",
89
- hasAuthKey: Boolean(entry.credential),
90
+ hasAuthKey: hasStoredCredential(entry.credential),
90
91
  ...(entry.modelsEntry ? {} : { builtin: true }),
91
92
  disabled: true,
92
93
  models: entry.models,
@@ -153,6 +154,12 @@ export class ProviderService {
153
154
  await this.afterChange();
154
155
  return { fixed: result.fixed, providers: await this.list() };
155
156
  }
157
+ async network() { return this.registry.sharedNetwork(await this.list()); }
158
+ async setNetwork(input) {
159
+ const settings = await this.registry.setSharedNetwork(input, await this.list());
160
+ await this.afterChange();
161
+ return settings;
162
+ }
156
163
  /**
157
164
  * Adds a provider, or adds a model to one, or updates one.
158
165
  *
@@ -234,12 +241,14 @@ export class ProviderService {
234
241
  const disabled = (await this.registry.loadDisabled()).some((entry) => entry.id === providerId);
235
242
  if (disabled) {
236
243
  await this.registry.forgetDisabled(providerId);
244
+ await this.registry.clearNetworkOverride(providerId);
237
245
  await this.afterChange();
238
246
  return this.list();
239
247
  }
240
248
  const builtin = (await this.registry.loadCredentialOnly()).some((provider) => provider.id === providerId);
241
249
  if (builtin) {
242
250
  await this.registry.removeCredential(providerId);
251
+ await this.registry.clearNetworkOverride(providerId);
243
252
  await this.afterChange([{ provider: providerId }]);
244
253
  return this.list();
245
254
  }
@@ -248,6 +257,7 @@ export class ProviderService {
248
257
  const next = previous.filter((provider) => provider.id !== providerId);
249
258
  await this.registry.applyToPi(previous, next);
250
259
  await this.registry.save(next);
260
+ await this.registry.clearNetworkOverride(providerId);
251
261
  await this.afterChange([{ provider: providerId }]);
252
262
  return this.list();
253
263
  }
@@ -762,6 +762,28 @@ export function createHttpServer(options) {
762
762
  }
763
763
  return;
764
764
  }
765
+ if (url.pathname === "/api/providers/network") {
766
+ try {
767
+ if (request.method === "GET") {
768
+ json(200, await context.providerService.network());
769
+ return;
770
+ }
771
+ if (request.method !== "PATCH") {
772
+ json(405, { error: "网络设置仅支持 GET / PATCH" });
773
+ return;
774
+ }
775
+ const asked = await readRequestText(request, 262144);
776
+ if (asked.tooLarge) {
777
+ json(413, { error: "网络设置过大" });
778
+ return;
779
+ }
780
+ json(200, await context.providerService.setNetwork(JSON.parse(asked.text || "{}")));
781
+ }
782
+ catch (error) {
783
+ json(400, { error: error instanceof SyntaxError ? "代理设置格式不正确" : error instanceof Error ? error.message : "网络设置保存失败" });
784
+ }
785
+ return;
786
+ }
765
787
  if (url.pathname === "/api/providers/fetch-models") {
766
788
  if (request.method !== "POST") {
767
789
  response.writeHead(405).end();
@@ -0,0 +1,16 @@
1
+ import type { ProviderNetworkSettings } from "../shared/provider-network.js";
2
+ type Credential = {
3
+ type?: string;
4
+ env?: Record<string, string>;
5
+ [key: string]: unknown;
6
+ };
7
+ export declare class ProviderNetworkInputError extends Error {
8
+ }
9
+ export declare const hasStoredCredential: (value?: {
10
+ type?: string;
11
+ key?: unknown;
12
+ }) => boolean;
13
+ /** Only connection settings are returned; credentials and proxy userinfo never reach the browser. */
14
+ export declare function publicProviderNetwork(credential?: Credential): ProviderNetworkSettings;
15
+ export declare function updateProviderNetwork(current: Credential | undefined, input: unknown): Credential | undefined;
16
+ export {};
@@ -0,0 +1,106 @@
1
+ export class ProviderNetworkInputError extends Error {
2
+ }
3
+ export const hasStoredCredential = (value) => Boolean(value && (value.type === "oauth" || typeof value.key === "string" && value.key.length > 0));
4
+ const PROXY_KEYS = ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"];
5
+ const read = (env, key) => {
6
+ const value = env[key.toLowerCase()] ?? env[key];
7
+ return typeof value === "string" ? value : "";
8
+ };
9
+ function visibleAddress(value) {
10
+ if (!value)
11
+ return { address: "", hasAuth: false };
12
+ try {
13
+ const url = new URL(value);
14
+ const hasAuth = Boolean(url.username || url.password || url.search || url.hash || url.pathname !== "/");
15
+ url.username = "";
16
+ url.password = "";
17
+ url.search = "";
18
+ url.hash = "";
19
+ url.pathname = "/";
20
+ return { address: url.href, hasAuth };
21
+ }
22
+ catch {
23
+ return { address: "", hasAuth: true };
24
+ }
25
+ }
26
+ /** Only connection settings are returned; credentials and proxy userinfo never reach the browser. */
27
+ export function publicProviderNetwork(credential) {
28
+ const env = credential?.env ?? {};
29
+ const fallback = read(env, "ALL_PROXY");
30
+ const http = visibleAddress(read(env, "HTTP_PROXY") || fallback);
31
+ const https = visibleAddress(read(env, "HTTPS_PROXY") || fallback);
32
+ const noProxy = read(env, "NO_PROXY");
33
+ const httpAddress = read(env, "HTTP_PROXY") || fallback;
34
+ const httpsAddress = read(env, "HTTPS_PROXY") || fallback;
35
+ return { mode: noProxy.trim() === "*" ? "direct" : http.address || https.address || http.hasAuth || https.hasAuth ? "proxy" : "inherit",
36
+ httpProxy: http.address, httpsProxy: https.address, noProxy, httpProxyHasAuth: http.hasAuth, httpsProxyHasAuth: https.hasAuth,
37
+ httpsSameAsHttp: Boolean(httpAddress && (!httpsAddress || httpsAddress === httpAddress)) };
38
+ }
39
+ function address(value) {
40
+ if (value === undefined)
41
+ return undefined;
42
+ if (typeof value !== "string" || value.length > 4096)
43
+ throw new ProviderNetworkInputError("代理地址必须是最多 4096 字符的 HTTP / HTTPS URL");
44
+ const text = value.trim();
45
+ if (!text)
46
+ return "";
47
+ try {
48
+ const parsed = new URL(text);
49
+ if (!["http:", "https:"].includes(parsed.protocol) || !parsed.hostname || parsed.hash || parsed.search || parsed.pathname !== "/")
50
+ throw new Error();
51
+ return parsed.href;
52
+ }
53
+ catch {
54
+ throw new ProviderNetworkInputError("代理地址仅支持 HTTP / HTTPS,例如 http://127.0.0.1:7890");
55
+ }
56
+ }
57
+ export function updateProviderNetwork(current, input) {
58
+ const value = input;
59
+ if (!value || !["inherit", "proxy", "direct"].includes(value.mode ?? ""))
60
+ throw new ProviderNetworkInputError("请选择继承环境、单独代理或直连");
61
+ if (value.mode === "proxy" && value.noProxy !== undefined && (typeof value.noProxy !== "string" || value.noProxy.length > 4096 || /[\r\n\u0000]/.test(value.noProxy)))
62
+ throw new ProviderNetworkInputError("绕过代理的地址须为最多 4096 字符的逗号分隔列表");
63
+ const before = current?.env ?? {};
64
+ const env = { ...before };
65
+ for (const key of PROXY_KEYS) {
66
+ delete env[key];
67
+ delete env[key.toLowerCase()];
68
+ }
69
+ if (value.mode === "direct") {
70
+ const keepAddress = (key) => { try {
71
+ return address(read(before, key) || read(before, "ALL_PROXY")) || "";
72
+ }
73
+ catch {
74
+ return "";
75
+ } };
76
+ // Keep usable addresses for switching back, while explicitly blocking inherited proxy values.
77
+ env.HTTP_PROXY = env.http_proxy = keepAddress("HTTP_PROXY");
78
+ env.HTTPS_PROXY = env.https_proxy = keepAddress("HTTPS_PROXY");
79
+ env.ALL_PROXY = env.all_proxy = "";
80
+ env.NO_PROXY = env.no_proxy = "*";
81
+ }
82
+ if (value.mode === "proxy") {
83
+ const fallback = read(before, "ALL_PROXY");
84
+ const http = address(value.httpProxy ?? (read(before, "HTTP_PROXY") || fallback)) ?? "";
85
+ const https = address(value.httpsProxy ?? (read(before, "HTTPS_PROXY") || fallback)) ?? "";
86
+ if (!http && !https)
87
+ throw new ProviderNetworkInputError("请填写至少一个 HTTP / HTTPS 代理地址");
88
+ env.HTTP_PROXY = env.http_proxy = http || https;
89
+ env.HTTPS_PROXY = env.https_proxy = https || http;
90
+ const previousBypass = read(before, "NO_PROXY");
91
+ const noProxy = value.noProxy?.trim() ?? (previousBypass === "*" ? "" : previousBypass);
92
+ if (noProxy.length > 4096 || /[\r\n\u0000]/.test(noProxy))
93
+ throw new ProviderNetworkInputError("绕过代理的地址格式不正确");
94
+ if (noProxy === "*")
95
+ throw new ProviderNetworkInputError("全部直连请选择“直连”模式");
96
+ env.NO_PROXY = env.no_proxy = noProxy;
97
+ }
98
+ if (!current && !Object.keys(env).length)
99
+ return undefined;
100
+ const next = { ...(current ?? { type: "api_key" }) };
101
+ if (Object.keys(env).length)
102
+ next.env = env;
103
+ else
104
+ delete next.env;
105
+ return next;
106
+ }
@@ -1,3 +1,11 @@
1
+ export type SharedProviderProxyRecord = {
2
+ env: Record<string, string>;
3
+ providers: string[];
4
+ excludedProviders: string[];
5
+ };
6
+ export declare function readSharedProviderProxy(agentDir: string): Promise<SharedProviderProxyRecord | undefined>;
7
+ /** A shared address is applied at request time, without touching any provider credential. */
8
+ export declare function sharedProviderProxyEnv(shared: SharedProviderProxyRecord | undefined, providerId: string): Record<string, string> | undefined;
1
9
  /** Read only proxy keys; provider secrets must never become child process environment variables. */
2
10
  export declare function readProviderProxyEnv(agentDir: string, providerId?: string): Promise<Record<string, string>>;
3
11
  type Env = Record<string, string | undefined>;
@@ -2,6 +2,28 @@ import { EnvHttpProxyAgent } from "undici";
2
2
  import { readFile } from "node:fs/promises";
3
3
  import { join } from "node:path";
4
4
  import { AsyncLocalStorage } from "node:async_hooks";
5
+ export async function readSharedProviderProxy(agentDir) {
6
+ let data;
7
+ try {
8
+ data = JSON.parse(await readFile(join(agentDir, "shared-provider-proxy.json"), "utf8"));
9
+ }
10
+ catch (error) {
11
+ if (error.code === "ENOENT")
12
+ return undefined;
13
+ throw new Error("公共代理配置无法读取,请检查格式和权限");
14
+ }
15
+ if (!data || !data.env || typeof data.env !== "object" || Array.isArray(data.env) || Object.entries(data.env).some(([key, value]) => !/^(http_proxy|https_proxy|all_proxy|no_proxy)$/i.test(key) || typeof value !== "string") || !Array.isArray(data.providers) || !data.providers.every(id => typeof id === "string") || !Array.isArray(data.excludedProviders) || !data.excludedProviders.every(id => typeof id === "string"))
16
+ throw new Error("公共代理配置格式不正确");
17
+ return data;
18
+ }
19
+ /** A shared address is applied at request time, without touching any provider credential. */
20
+ export function sharedProviderProxyEnv(shared, providerId) {
21
+ if (!shared || shared.excludedProviders.includes(providerId) || ["google", "google-vertex"].includes(providerId))
22
+ return undefined;
23
+ if (shared.providers.includes(providerId))
24
+ return shared.env;
25
+ return { HTTP_PROXY: "", http_proxy: "", HTTPS_PROXY: "", https_proxy: "", ALL_PROXY: "", all_proxy: "", NO_PROXY: "*", no_proxy: "*" };
26
+ }
5
27
  /** Read only proxy keys; provider secrets must never become child process environment variables. */
6
28
  export async function readProviderProxyEnv(agentDir, providerId) {
7
29
  const read = async (file) => {
@@ -17,7 +39,7 @@ export async function readProviderProxyEnv(agentDir, providerId) {
17
39
  const id = providerId ?? (await read("settings.json")).defaultProvider;
18
40
  if (!id)
19
41
  return {};
20
- const env = (await read("auth.json"))[id]?.env ?? {};
42
+ const env = mergeEnv((await read("auth.json"))[id]?.env, sharedProviderProxyEnv(await readSharedProviderProxy(agentDir), id));
21
43
  const result = {};
22
44
  for (const name of ["HTTP_PROXY", "HTTPS_PROXY", "ALL_PROXY", "NO_PROXY"]) {
23
45
  const value = env[name.toLowerCase()] ?? env[name];
@@ -120,6 +142,7 @@ export async function installProviderProxy(runtime, agentDir) {
120
142
  if (typeof target.getAuth !== "function")
121
143
  return;
122
144
  let stored = {};
145
+ let shared;
123
146
  if (agentDir) {
124
147
  try {
125
148
  stored = JSON.parse(await readFile(join(agentDir, "auth.json"), "utf8"));
@@ -128,14 +151,16 @@ export async function installProviderProxy(runtime, agentDir) {
128
151
  if (error.code !== "ENOENT")
129
152
  throw error;
130
153
  }
154
+ shared = await readSharedProviderProxy(agentDir);
131
155
  }
132
- const providerEnv = (model) => stored[typeof model === "string" ? model : model?.provider ?? ""]?.env;
156
+ const providerId = (model) => typeof model === "string" ? model : model?.provider ?? "";
157
+ const providerEnv = (model) => mergeEnv(stored[providerId(model)]?.env, sharedProviderProxyEnv(shared, providerId(model)));
133
158
  const getAuth = target.getAuth.bind(target);
134
159
  target.getAuth = (model, options) => {
135
160
  const env = mergeEnv(providerEnv(model), options?.env);
136
161
  return withAuthProxy(env, async () => {
137
162
  const auth = await getAuth(model, Object.keys(env).length ? { ...options, env } : options);
138
- const resolvedEnv = mergeEnv(providerEnv(model), auth?.env, options?.env);
163
+ const resolvedEnv = mergeEnv(providerEnv(model), auth?.env, sharedProviderProxyEnv(shared, providerId(model)), options?.env);
139
164
  return auth && Object.keys(resolvedEnv).length ? { ...auth, env: resolvedEnv } : auth;
140
165
  }, typeof model === "string" ? model : model?.provider);
141
166
  };
@@ -1,4 +1,5 @@
1
1
  import type { DiscoveredModel, PublicProvider } from "./types.js";
2
+ import type { SharedProviderProxySettings } from "../shared/provider-network.js";
2
3
  /**
3
4
  * THE PROVIDERS A PERSON ADDED, and the two files that make Pi know about them.
4
5
  *
@@ -172,6 +173,17 @@ export declare class ProviderRegistry {
172
173
  * accident - it has to be given one.
173
174
  */
174
175
  constructor(dataDir: string, piDir?: string);
176
+ private networkQueue;
177
+ private sharedNetworkRecord;
178
+ sharedNetwork(known: ReadonlyArray<{
179
+ id: string;
180
+ api: string;
181
+ }>): Promise<SharedProviderProxySettings>;
182
+ setSharedNetwork(input: unknown, known: ReadonlyArray<{
183
+ id: string;
184
+ api: string;
185
+ }>): Promise<SharedProviderProxySettings>;
186
+ clearNetworkOverride(providerId: string): Promise<void>;
175
187
  /** Super Pi's own registry: the providers it manages, with their keys. */
176
188
  private get registryPath();
177
189
  /** The registry file as one object: the managed providers AND the ones turned off. */
@@ -3,7 +3,10 @@ import { mkdir, readFile, rename, stat, writeFile } from "node:fs/promises";
3
3
  import { dirname, join } from "node:path";
4
4
  import { readModelStore, storeWindowFor } from "./models-store.js";
5
5
  import { SUPER_PI_DEFAULT_MAX_TOKENS, SUPER_PI_GUESSED_MAX_TOKENS as GUESSED_MAX_TOKENS } from "./model-context-window.js";
6
- import { providerProxyFetch } from "./provider-proxy.js";
6
+ import { providerProxyFetch, readSharedProviderProxy } from "./provider-proxy.js";
7
+ import { hasStoredCredential, publicProviderNetwork, updateProviderNetwork, ProviderNetworkInputError } from "./provider-network.js";
8
+ import { supportsProviderProxy } from "../shared/provider-network.js";
9
+ const unsupportedProxy = (provider) => !supportsProviderProxy(provider);
7
10
  /**
8
11
  * The API adapters Pi ships, as the ids a provider may name.
9
12
  *
@@ -261,6 +264,52 @@ export class ProviderRegistry {
261
264
  this.dataDir = dataDir;
262
265
  this.piDir = piDir;
263
266
  }
267
+ networkQueue = Promise.resolve();
268
+ async sharedNetworkRecord(known) {
269
+ const saved = await readSharedProviderProxy(this.piDir);
270
+ if (saved)
271
+ return saved;
272
+ // Seed the common address from existing manual settings without rewriting their credentials.
273
+ const [registry, auth] = await Promise.all([this.readRegistry(), readJsonFile(piAuthPath(this.piDir))])
274
+ .catch(() => { throw new Error("无法读取网络设置,请检查 Pi 配置文件的格式和读取权限"); });
275
+ const excludedProviders = known.filter(provider => unsupportedProxy(provider)).map(provider => provider.id);
276
+ const entries = known.filter(provider => !excludedProviders.includes(provider.id)).map(provider => {
277
+ const credential = registry.disabled.find(entry => entry.id === provider.id)?.credential ?? auth?.[provider.id];
278
+ return { id: provider.id, credential: credential };
279
+ }).filter(entry => publicProviderNetwork(entry.credential).mode === "proxy");
280
+ return { env: entries[0]?.credential?.env ?? {}, providers: entries.map(entry => entry.id), excludedProviders };
281
+ }
282
+ async sharedNetwork(known) {
283
+ const record = await this.sharedNetworkRecord(known);
284
+ const visible = publicProviderNetwork({ env: record.env });
285
+ return { httpProxy: visible.httpProxy || visible.httpsProxy, hasAuth: visible.httpProxyHasAuth || visible.httpsProxyHasAuth, providers: record.providers.filter(id => known.some(provider => provider.id === id)) };
286
+ }
287
+ async setSharedNetwork(input, known) {
288
+ const operation = this.networkQueue.catch(() => undefined).then(async () => {
289
+ const value = input;
290
+ if (!value || !Array.isArray(value.providers) || value.providers.length > 1000 || value.providers.some(id => typeof id !== "string" || !known.some(provider => provider.id === id && !unsupportedProxy(provider))))
291
+ throw new ProviderNetworkInputError("请选择支持代理的供应商");
292
+ if (value.httpProxy !== undefined && typeof value.httpProxy !== "string")
293
+ throw new ProviderNetworkInputError("请填写 HTTP / HTTPS 代理地址");
294
+ const current = await this.sharedNetworkRecord(known);
295
+ const address = value.httpProxy ?? current.env.http_proxy ?? current.env.HTTP_PROXY ?? current.env.https_proxy ?? current.env.HTTPS_PROXY ?? current.env.all_proxy ?? current.env.ALL_PROXY ?? "";
296
+ const next = updateProviderNetwork(undefined, !address && !value.providers.length
297
+ ? { mode: "direct" }
298
+ : { mode: "proxy", httpProxy: address, httpsProxy: "", noProxy: "" });
299
+ if (next?.env)
300
+ next.env.ALL_PROXY = next.env.all_proxy = "";
301
+ const record = { env: next?.env ?? {}, providers: [...new Set(value.providers)], excludedProviders: known.filter(unsupportedProxy).map(provider => provider.id) };
302
+ await writeJsonAtomic(join(this.piDir, "shared-provider-proxy.json"), record);
303
+ return this.sharedNetwork(known);
304
+ });
305
+ this.networkQueue = operation;
306
+ return operation;
307
+ }
308
+ async clearNetworkOverride(providerId) {
309
+ const record = await readSharedProviderProxy(this.piDir);
310
+ if (record?.providers.includes(providerId))
311
+ await writeJsonAtomic(join(this.piDir, "shared-provider-proxy.json"), { ...record, providers: record.providers.filter(id => id !== providerId) });
312
+ }
264
313
  /** Super Pi's own registry: the providers it manages, with their keys. */
265
314
  get registryPath() {
266
315
  return join(this.dataDir, "providers.json");
@@ -342,7 +391,7 @@ export class ProviderRegistry {
342
391
  // ANY credential counts, not only a `key`. An OAuth provider keeps `access`/`refresh` in
343
392
  // this file, and reporting "no key" for a provider Pi is happily using is a lie the panel
344
393
  // would then show next to a working model.
345
- hasAuthKey: Boolean(piAuth[pId] || backup?.apiKey),
394
+ hasAuthKey: Boolean(hasStoredCredential(piAuth[pId]) || backup?.apiKey),
346
395
  // A KEY WRITTEN IN models.json IS A CREDENTIAL TOO, and it is the one Pi's own docs lead
347
396
  // with (`"apiKey": "$MY_KEY"`). Without this, such a provider is reported as having no
348
397
  // credential - and the panel then says "Pi will not offer it", which is wrong exactly when
@@ -385,7 +434,7 @@ export class ProviderRegistry {
385
434
  const offIds = new Set(registry.disabled.map((entry) => entry.id));
386
435
  return Object.keys(piAuth)
387
436
  .filter((id) => !piModels[id] && !offIds.has(id))
388
- .map((id) => ({ id, name: id, baseUrl: "", api: "", hasAuthKey: true, builtin: true, models: [] }));
437
+ .map((id) => ({ id, name: id, baseUrl: "", api: "", hasAuthKey: hasStoredCredential(piAuth[id]), builtin: true, models: [] }));
389
438
  }
390
439
  /** The providers that are switched off, as the panel needs to see them. */
391
440
  async loadDisabled() {
@@ -0,0 +1,30 @@
1
+ export type ProviderNetworkMode = "inherit" | "proxy" | "direct";
2
+ export type ProviderNetworkSettings = {
3
+ mode: ProviderNetworkMode;
4
+ httpProxy: string;
5
+ httpsProxy: string;
6
+ noProxy: string;
7
+ httpProxyHasAuth: boolean;
8
+ httpsProxyHasAuth: boolean;
9
+ httpsSameAsHttp: boolean;
10
+ };
11
+ /** Missing addresses preserve stored values; an explicit empty address clears that field. */
12
+ export type ProviderNetworkUpdate = {
13
+ mode: ProviderNetworkMode;
14
+ httpProxy?: string;
15
+ httpsProxy?: string;
16
+ noProxy?: string;
17
+ };
18
+ export type SharedProviderProxySettings = {
19
+ httpProxy: string;
20
+ hasAuth: boolean;
21
+ providers: string[];
22
+ };
23
+ export type SharedProviderProxyUpdate = {
24
+ httpProxy?: string;
25
+ providers: string[];
26
+ };
27
+ export declare const supportsProviderProxy: (provider: {
28
+ id: string;
29
+ api: string;
30
+ }) => boolean;
@@ -0,0 +1 @@
1
+ export const supportsProviderProxy = (provider) => !["google", "google-vertex"].includes(provider.id) && !["google-generative-ai", "google-vertex"].includes(provider.api);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@evandu/super-pi",
3
- "version": "0.1.208",
3
+ "version": "0.1.210",
4
4
  "description": "Web control plane and companion for Pi",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -43,6 +43,7 @@
43
43
  "test": "node scripts/test.mjs"
44
44
  },
45
45
  "check:logic": [
46
+ "scripts/provider-network.test.mjs",
46
47
  "scripts/project-overview.test.mjs",
47
48
  "scripts/team-handoff-view.test.mjs",
48
49
  "scripts/project-presentation.test.mjs",
@@ -281,6 +282,7 @@
281
282
  "scripts/session-reminder-pmo-rights.test.mjs"
282
283
  ],
283
284
  "check:browser": [
285
+ "scripts/provider-network-browser.test.mjs",
284
286
  "scripts/project-presentation-browser.test.mjs",
285
287
  "scripts/team-context-browser.test.mjs",
286
288
  "scripts/message-media-browser.test.mjs",