copilot-api-plus 1.0.9 → 1.0.10
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.md +476 -579
- package/dist/main.js +238 -1
- package/dist/main.js.map +1 -1
- package/package.json +2 -1
package/dist/main.js
CHANGED
|
@@ -11,6 +11,8 @@ import { defineCommand, runMain } from "citty";
|
|
|
11
11
|
import consola from "consola";
|
|
12
12
|
import fs from "node:fs/promises";
|
|
13
13
|
import os from "node:os";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
import * as p from "@clack/prompts";
|
|
14
16
|
import clipboard from "clipboardy";
|
|
15
17
|
import { serve } from "srvx";
|
|
16
18
|
import invariant from "tiny-invariant";
|
|
@@ -271,6 +273,238 @@ const logout = defineCommand({
|
|
|
271
273
|
}
|
|
272
274
|
});
|
|
273
275
|
|
|
276
|
+
//#endregion
|
|
277
|
+
//#region src/lib/config.ts
|
|
278
|
+
const CONFIG_FILENAME = "config.json";
|
|
279
|
+
/**
|
|
280
|
+
* Get the path to the config file
|
|
281
|
+
*/
|
|
282
|
+
function getConfigPath() {
|
|
283
|
+
return path.join(PATHS.DATA_DIR, CONFIG_FILENAME);
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* Load configuration from file
|
|
287
|
+
*/
|
|
288
|
+
async function loadConfig() {
|
|
289
|
+
try {
|
|
290
|
+
const configPath = getConfigPath();
|
|
291
|
+
const content = await fs.readFile(configPath, "utf-8");
|
|
292
|
+
return JSON.parse(content);
|
|
293
|
+
} catch {
|
|
294
|
+
return {};
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
/**
|
|
298
|
+
* Save configuration to file
|
|
299
|
+
*/
|
|
300
|
+
async function saveConfig(config) {
|
|
301
|
+
const configPath = getConfigPath();
|
|
302
|
+
await fs.writeFile(configPath, JSON.stringify(config, null, 2), "utf-8");
|
|
303
|
+
consola.debug(`Configuration saved to ${configPath}`);
|
|
304
|
+
}
|
|
305
|
+
/**
|
|
306
|
+
* Get proxy configuration
|
|
307
|
+
*/
|
|
308
|
+
async function getProxyConfig() {
|
|
309
|
+
return (await loadConfig()).proxy;
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Save proxy configuration
|
|
313
|
+
*/
|
|
314
|
+
async function saveProxyConfig(proxyConfig) {
|
|
315
|
+
const config = await loadConfig();
|
|
316
|
+
config.proxy = proxyConfig;
|
|
317
|
+
await saveConfig(config);
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
320
|
+
* Clear proxy configuration
|
|
321
|
+
*/
|
|
322
|
+
async function clearProxyConfig() {
|
|
323
|
+
const config = await loadConfig();
|
|
324
|
+
delete config.proxy;
|
|
325
|
+
await saveConfig(config);
|
|
326
|
+
}
|
|
327
|
+
/**
|
|
328
|
+
* Apply saved proxy configuration to environment variables
|
|
329
|
+
* This should be called at startup to restore proxy settings
|
|
330
|
+
*/
|
|
331
|
+
async function applyProxyConfig() {
|
|
332
|
+
const proxyConfig = await getProxyConfig();
|
|
333
|
+
if (!proxyConfig || !proxyConfig.enabled) return false;
|
|
334
|
+
if (proxyConfig.httpProxy) {
|
|
335
|
+
process.env.HTTP_PROXY = proxyConfig.httpProxy;
|
|
336
|
+
process.env.http_proxy = proxyConfig.httpProxy;
|
|
337
|
+
}
|
|
338
|
+
if (proxyConfig.httpsProxy) {
|
|
339
|
+
process.env.HTTPS_PROXY = proxyConfig.httpsProxy;
|
|
340
|
+
process.env.https_proxy = proxyConfig.httpsProxy;
|
|
341
|
+
}
|
|
342
|
+
if (proxyConfig.noProxy) {
|
|
343
|
+
process.env.NO_PROXY = proxyConfig.noProxy;
|
|
344
|
+
process.env.no_proxy = proxyConfig.noProxy;
|
|
345
|
+
}
|
|
346
|
+
consola.info("Proxy configuration loaded from saved settings");
|
|
347
|
+
if (proxyConfig.httpProxy) consola.info(` HTTP_PROXY: ${proxyConfig.httpProxy}`);
|
|
348
|
+
if (proxyConfig.httpsProxy) consola.info(` HTTPS_PROXY: ${proxyConfig.httpsProxy}`);
|
|
349
|
+
if (proxyConfig.noProxy) consola.info(` NO_PROXY: ${proxyConfig.noProxy}`);
|
|
350
|
+
return true;
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/proxy-config.ts
|
|
355
|
+
const proxy = defineCommand({
|
|
356
|
+
meta: {
|
|
357
|
+
name: "proxy",
|
|
358
|
+
description: "Configure proxy settings (persistent)"
|
|
359
|
+
},
|
|
360
|
+
args: {
|
|
361
|
+
set: {
|
|
362
|
+
type: "boolean",
|
|
363
|
+
default: false,
|
|
364
|
+
description: "Set proxy configuration interactively"
|
|
365
|
+
},
|
|
366
|
+
enable: {
|
|
367
|
+
type: "boolean",
|
|
368
|
+
default: false,
|
|
369
|
+
description: "Enable saved proxy configuration"
|
|
370
|
+
},
|
|
371
|
+
disable: {
|
|
372
|
+
type: "boolean",
|
|
373
|
+
default: false,
|
|
374
|
+
description: "Disable proxy (keep settings)"
|
|
375
|
+
},
|
|
376
|
+
clear: {
|
|
377
|
+
type: "boolean",
|
|
378
|
+
default: false,
|
|
379
|
+
description: "Clear all proxy settings"
|
|
380
|
+
},
|
|
381
|
+
show: {
|
|
382
|
+
type: "boolean",
|
|
383
|
+
default: false,
|
|
384
|
+
description: "Show current proxy configuration"
|
|
385
|
+
},
|
|
386
|
+
"http-proxy": {
|
|
387
|
+
type: "string",
|
|
388
|
+
description: "HTTP proxy URL (e.g., http://proxy:8080)"
|
|
389
|
+
},
|
|
390
|
+
"https-proxy": {
|
|
391
|
+
type: "string",
|
|
392
|
+
description: "HTTPS proxy URL (e.g., http://proxy:8080)"
|
|
393
|
+
},
|
|
394
|
+
"no-proxy": {
|
|
395
|
+
type: "string",
|
|
396
|
+
description: "Comma-separated list of hosts to bypass proxy"
|
|
397
|
+
}
|
|
398
|
+
},
|
|
399
|
+
async run({ args }) {
|
|
400
|
+
await ensurePaths();
|
|
401
|
+
if (args.show || !args.set && !args.enable && !args.disable && !args.clear && !args["http-proxy"] && !args["https-proxy"]) {
|
|
402
|
+
const config = await getProxyConfig();
|
|
403
|
+
if (!config) {
|
|
404
|
+
consola.info("No proxy configuration saved.");
|
|
405
|
+
consola.info("");
|
|
406
|
+
consola.info("To configure proxy, use one of:");
|
|
407
|
+
consola.info(" copilot-api-plus proxy --set # Interactive setup");
|
|
408
|
+
consola.info(" copilot-api-plus proxy --http-proxy http://proxy:8080 # Direct set");
|
|
409
|
+
return;
|
|
410
|
+
}
|
|
411
|
+
consola.info("Current proxy configuration:");
|
|
412
|
+
consola.info(` Status: ${config.enabled ? "✅ Enabled" : "❌ Disabled"}`);
|
|
413
|
+
if (config.httpProxy) consola.info(` HTTP_PROXY: ${config.httpProxy}`);
|
|
414
|
+
if (config.httpsProxy) consola.info(` HTTPS_PROXY: ${config.httpsProxy}`);
|
|
415
|
+
if (config.noProxy) consola.info(` NO_PROXY: ${config.noProxy}`);
|
|
416
|
+
return;
|
|
417
|
+
}
|
|
418
|
+
if (args.clear) {
|
|
419
|
+
await clearProxyConfig();
|
|
420
|
+
consola.success("Proxy configuration cleared.");
|
|
421
|
+
return;
|
|
422
|
+
}
|
|
423
|
+
if (args.enable) {
|
|
424
|
+
const config = await getProxyConfig();
|
|
425
|
+
if (!config) {
|
|
426
|
+
consola.error("No proxy configuration saved. Use --set to configure first.");
|
|
427
|
+
return;
|
|
428
|
+
}
|
|
429
|
+
config.enabled = true;
|
|
430
|
+
await saveProxyConfig(config);
|
|
431
|
+
consola.success("Proxy enabled. It will be used on next server start.");
|
|
432
|
+
return;
|
|
433
|
+
}
|
|
434
|
+
if (args.disable) {
|
|
435
|
+
const config = await getProxyConfig();
|
|
436
|
+
if (!config) {
|
|
437
|
+
consola.info("No proxy configuration to disable.");
|
|
438
|
+
return;
|
|
439
|
+
}
|
|
440
|
+
config.enabled = false;
|
|
441
|
+
await saveProxyConfig(config);
|
|
442
|
+
consola.success("Proxy disabled. Settings are preserved.");
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
if (args["http-proxy"] || args["https-proxy"]) {
|
|
446
|
+
const newConfig = {
|
|
447
|
+
enabled: true,
|
|
448
|
+
httpProxy: args["http-proxy"],
|
|
449
|
+
httpsProxy: args["https-proxy"] || args["http-proxy"],
|
|
450
|
+
noProxy: args["no-proxy"]
|
|
451
|
+
};
|
|
452
|
+
await saveProxyConfig(newConfig);
|
|
453
|
+
consola.success("Proxy configuration saved and enabled.");
|
|
454
|
+
consola.info(` HTTP_PROXY: ${newConfig.httpProxy || "(not set)"}`);
|
|
455
|
+
consola.info(` HTTPS_PROXY: ${newConfig.httpsProxy || "(not set)"}`);
|
|
456
|
+
if (newConfig.noProxy) consola.info(` NO_PROXY: ${newConfig.noProxy}`);
|
|
457
|
+
return;
|
|
458
|
+
}
|
|
459
|
+
if (args.set) {
|
|
460
|
+
p.intro("Proxy Configuration");
|
|
461
|
+
const existingConfig = await getProxyConfig();
|
|
462
|
+
const httpProxy = await p.text({
|
|
463
|
+
message: "HTTP Proxy URL",
|
|
464
|
+
placeholder: "http://proxy:8080",
|
|
465
|
+
initialValue: existingConfig?.httpProxy || ""
|
|
466
|
+
});
|
|
467
|
+
if (p.isCancel(httpProxy)) {
|
|
468
|
+
p.cancel("Configuration cancelled.");
|
|
469
|
+
return;
|
|
470
|
+
}
|
|
471
|
+
const httpsProxy = await p.text({
|
|
472
|
+
message: "HTTPS Proxy URL (leave empty to use HTTP proxy)",
|
|
473
|
+
placeholder: "http://proxy:8080",
|
|
474
|
+
initialValue: existingConfig?.httpsProxy || ""
|
|
475
|
+
});
|
|
476
|
+
if (p.isCancel(httpsProxy)) {
|
|
477
|
+
p.cancel("Configuration cancelled.");
|
|
478
|
+
return;
|
|
479
|
+
}
|
|
480
|
+
const noProxy = await p.text({
|
|
481
|
+
message: "No Proxy (comma-separated hosts to bypass)",
|
|
482
|
+
placeholder: "localhost,127.0.0.1,.local",
|
|
483
|
+
initialValue: existingConfig?.noProxy || ""
|
|
484
|
+
});
|
|
485
|
+
if (p.isCancel(noProxy)) {
|
|
486
|
+
p.cancel("Configuration cancelled.");
|
|
487
|
+
return;
|
|
488
|
+
}
|
|
489
|
+
const enable = await p.confirm({
|
|
490
|
+
message: "Enable proxy now?",
|
|
491
|
+
initialValue: true
|
|
492
|
+
});
|
|
493
|
+
if (p.isCancel(enable)) {
|
|
494
|
+
p.cancel("Configuration cancelled.");
|
|
495
|
+
return;
|
|
496
|
+
}
|
|
497
|
+
await saveProxyConfig({
|
|
498
|
+
enabled: enable,
|
|
499
|
+
httpProxy: httpProxy || void 0,
|
|
500
|
+
httpsProxy: httpsProxy || httpProxy || void 0,
|
|
501
|
+
noProxy: noProxy || void 0
|
|
502
|
+
});
|
|
503
|
+
p.outro(`Proxy configuration saved${enable ? " and enabled" : ""}.`);
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
});
|
|
507
|
+
|
|
274
508
|
//#endregion
|
|
275
509
|
//#region src/lib/proxy.ts
|
|
276
510
|
function initProxyFromEnv() {
|
|
@@ -2114,7 +2348,9 @@ server.route("/antigravity/v1/messages", antigravityMessagesRoute);
|
|
|
2114
2348
|
* - zenApiKey: OpenCode Zen API key (optional; if omitted will prompt for setup)
|
|
2115
2349
|
*/
|
|
2116
2350
|
async function runServer(options) {
|
|
2351
|
+
const savedProxyApplied = await applyProxyConfig();
|
|
2117
2352
|
if (options.proxyEnv) initProxyFromEnv();
|
|
2353
|
+
else if (savedProxyApplied) initProxyFromEnv();
|
|
2118
2354
|
if (options.verbose) {
|
|
2119
2355
|
consola.level = 5;
|
|
2120
2356
|
consola.info("Verbose logging enabled");
|
|
@@ -2345,7 +2581,8 @@ const main = defineCommand({
|
|
|
2345
2581
|
start,
|
|
2346
2582
|
"check-usage": checkUsage,
|
|
2347
2583
|
debug,
|
|
2348
|
-
logout
|
|
2584
|
+
logout,
|
|
2585
|
+
proxy
|
|
2349
2586
|
}
|
|
2350
2587
|
});
|
|
2351
2588
|
await runMain(main);
|