@chenpu17/cc-gw 0.6.2 → 0.6.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@chenpu17/cc-gw",
3
- "version": "0.6.2",
3
+ "version": "0.6.4",
4
4
  "private": false,
5
5
  "type": "module",
6
6
  "scripts": {
package/LICENSE DELETED
@@ -1,21 +0,0 @@
1
- MIT License
2
-
3
- Copyright (c) 2025 chenpu17
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
@@ -1 +0,0 @@
1
- #!/usr/bin/env node
@@ -1,329 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- // index.ts
4
- import { Command } from "commander";
5
- import { spawn } from "child_process";
6
- import fs from "fs";
7
- import { promises as fsp } from "fs";
8
- import os from "os";
9
- import path from "path";
10
- import process from "process";
11
- import { fileURLToPath } from "url";
12
- import { green, yellow } from "colorette";
13
- import { createRequire } from "module";
14
- var program = new Command();
15
- var DEFAULT_PORT = 4100;
16
- var HOME_DIR = path.join(os.homedir(), ".cc-gw");
17
- var PID_FILE = path.join(HOME_DIR, "cc-gw.pid");
18
- var LOG_DIR = path.join(HOME_DIR, "logs");
19
- var LOG_FILE = path.join(LOG_DIR, "cc-gw.log");
20
- var CONFIG_FILE = path.join(HOME_DIR, "config.json");
21
- var require2 = createRequire(import.meta.url);
22
- function resolvePackageVersion() {
23
- const __filename = fileURLToPath(import.meta.url);
24
- const __dirname = path.dirname(__filename);
25
- const candidates = [
26
- path.resolve(__dirname, "../../package.json"),
27
- path.resolve(__dirname, "../package.json"),
28
- path.resolve(__dirname, "../../../package.json")
29
- ];
30
- for (const candidate of candidates) {
31
- try {
32
- const pkg = require2(candidate);
33
- if (pkg && typeof pkg.version === "string") {
34
- return pkg.version;
35
- }
36
- } catch {
37
- }
38
- }
39
- return "0.0.0";
40
- }
41
- var CLI_VERSION = resolvePackageVersion();
42
- async function readConfiguredPort() {
43
- try {
44
- const raw = await fsp.readFile(CONFIG_FILE, "utf-8");
45
- const parsed = JSON.parse(raw);
46
- if (parsed && typeof parsed.port === "number" && Number.isFinite(parsed.port)) {
47
- return parsed.port;
48
- }
49
- } catch {
50
- }
51
- return null;
52
- }
53
- function resolveServerEntry() {
54
- const __filename = fileURLToPath(import.meta.url);
55
- const __dirname = path.dirname(__filename);
56
- const candidates = [
57
- // Workspace install(src/cli/dist -> src/server/dist)
58
- path.resolve(__dirname, "../../server/dist/index.js"),
59
- path.resolve(__dirname, "../server/dist/index.js"),
60
- path.resolve(__dirname, "../../../src/server/dist/index.js"),
61
- // Release bundle(cli -> server)
62
- path.resolve(__dirname, "../server/index.js"),
63
- path.resolve(__dirname, "../../server/index.js"),
64
- path.resolve(__dirname, "../../../server/index.js")
65
- ];
66
- for (const candidate of candidates) {
67
- if (fs.existsSync(candidate)) {
68
- return candidate;
69
- }
70
- }
71
- throw new Error("Server bundle not found. \u8BF7\u5148\u6784\u5EFA @cc-gw/server (pnpm --filter @cc-gw/server build)");
72
- }
73
- function resolveWebDist() {
74
- const __filename = fileURLToPath(import.meta.url);
75
- const __dirname = path.dirname(__filename);
76
- const candidates = [
77
- // Release bundle(cli -> web)
78
- path.resolve(__dirname, "../web"),
79
- path.resolve(__dirname, "../../web"),
80
- // Workspace install(src/cli/dist -> src/web/dist)
81
- path.resolve(__dirname, "../web/dist"),
82
- path.resolve(__dirname, "../../web/dist"),
83
- path.resolve(__dirname, "../../../src/web/dist"),
84
- path.resolve(process.cwd(), "src/web/dist")
85
- ];
86
- for (const candidate of candidates) {
87
- if (fs.existsSync(candidate)) {
88
- return candidate;
89
- }
90
- }
91
- return null;
92
- }
93
- async function ensureHomeDir() {
94
- await fsp.mkdir(LOG_DIR, { recursive: true });
95
- }
96
- async function ensureConfigTemplate(port) {
97
- try {
98
- await fsp.access(CONFIG_FILE);
99
- return false;
100
- } catch {
101
- const selectedPort = port ? Number.parseInt(port, 10) || DEFAULT_PORT : DEFAULT_PORT;
102
- const baseDefaults = {
103
- completion: null,
104
- reasoning: null,
105
- background: null,
106
- longContextThreshold: 6e4
107
- };
108
- const template = {
109
- http: {
110
- enabled: true,
111
- port: selectedPort,
112
- host: "127.0.0.1"
113
- },
114
- https: {
115
- enabled: false,
116
- port: 4443,
117
- host: "127.0.0.1",
118
- keyPath: "",
119
- certPath: "",
120
- caPath: ""
121
- },
122
- // 保留旧字段以兼容
123
- host: "127.0.0.1",
124
- port: selectedPort,
125
- providers: [],
126
- defaults: { ...baseDefaults },
127
- storeRequestPayloads: true,
128
- storeResponsePayloads: true,
129
- enableRoutingFallback: false,
130
- endpointRouting: {
131
- anthropic: {
132
- defaults: { ...baseDefaults },
133
- modelRoutes: {}
134
- },
135
- openai: {
136
- defaults: { ...baseDefaults },
137
- modelRoutes: {}
138
- }
139
- },
140
- logRetentionDays: 30,
141
- modelRoutes: {},
142
- logLevel: "info",
143
- requestLogging: true,
144
- responseLogging: true,
145
- bodyLimit: 10 * 1024 * 1024,
146
- webAuth: {
147
- enabled: false,
148
- username: "",
149
- passwordHash: "",
150
- passwordSalt: ""
151
- }
152
- };
153
- await fsp.mkdir(path.dirname(CONFIG_FILE), { recursive: true });
154
- await fsp.writeFile(CONFIG_FILE, JSON.stringify(template, null, 2), "utf-8");
155
- return true;
156
- }
157
- }
158
- async function readPid() {
159
- try {
160
- const raw = await fsp.readFile(PID_FILE, "utf-8");
161
- const pid = Number.parseInt(raw.trim(), 10);
162
- return Number.isNaN(pid) ? null : pid;
163
- } catch {
164
- return null;
165
- }
166
- }
167
- async function writePid(pid) {
168
- if (pid == null)
169
- return;
170
- await fsp.writeFile(PID_FILE, String(pid), "utf-8");
171
- }
172
- async function removePid() {
173
- try {
174
- await fsp.unlink(PID_FILE);
175
- } catch {
176
- }
177
- }
178
- function isProcessAlive(pid) {
179
- try {
180
- process.kill(pid, 0);
181
- return true;
182
- } catch {
183
- return false;
184
- }
185
- }
186
- async function isServiceRunning() {
187
- const pid = await readPid();
188
- if (!pid)
189
- return { running: false };
190
- if (isProcessAlive(pid)) {
191
- return { running: true, pid };
192
- }
193
- await removePid();
194
- return { running: false };
195
- }
196
- async function handleStart(options) {
197
- await ensureHomeDir();
198
- const { running, pid } = await isServiceRunning();
199
- if (running && pid) {
200
- console.log(yellow(`cc-gw \u5DF2\u5728\u8FD0\u884C (pid: ${pid})`));
201
- return;
202
- }
203
- const configCreated = await ensureConfigTemplate(options.port);
204
- const serverEntry = resolveServerEntry();
205
- const env = { ...process.env };
206
- if (options.port) {
207
- env.PORT = options.port;
208
- }
209
- if (!env.CC_GW_UI_ROOT) {
210
- const uiRoot = resolveWebDist();
211
- if (uiRoot) {
212
- env.CC_GW_UI_ROOT = uiRoot;
213
- }
214
- }
215
- const daemonMode = options.foreground ? false : options.daemon !== false;
216
- const spawnOptions = {
217
- env,
218
- detached: daemonMode,
219
- stdio: daemonMode ? [
220
- "ignore",
221
- fs.openSync(LOG_FILE, "a"),
222
- fs.openSync(LOG_FILE, "a")
223
- ] : "inherit"
224
- };
225
- const child = spawn(process.execPath, [serverEntry], spawnOptions);
226
- child.on("error", (err) => {
227
- console.error("\u542F\u52A8 cc-gw \u5931\u8D25:", err);
228
- });
229
- await writePid(child.pid);
230
- if (daemonMode) {
231
- child.unref();
232
- console.log(green(`cc-gw \u5DF2\u4EE5\u5B88\u62A4\u8FDB\u7A0B\u65B9\u5F0F\u542F\u52A8 (pid: ${child.pid})`));
233
- }
234
- let effectivePort;
235
- if (options.port) {
236
- const parsed = Number.parseInt(options.port, 10);
237
- effectivePort = Number.isFinite(parsed) ? parsed : DEFAULT_PORT;
238
- } else {
239
- const configured = await readConfiguredPort();
240
- effectivePort = configured ?? DEFAULT_PORT;
241
- }
242
- if (configCreated) {
243
- console.log(green(`\u5DF2\u5728 ${CONFIG_FILE} \u751F\u6210\u9ED8\u8BA4\u914D\u7F6E`));
244
- console.log(yellow(`\u9996\u6B21\u542F\u52A8\uFF1A\u5F85\u670D\u52A1\u5C31\u7EEA\u540E\uFF0C\u8BF7\u8BBF\u95EE\u4EE5\u4E0B\u5730\u5740\u8FDB\u884C\u914D\u7F6E:`));
245
- console.log(yellow(` HTTP: http://127.0.0.1:${effectivePort}/ui`));
246
- console.log(yellow(` HTTPS: https://127.0.0.1:4443/ui (\u9700\u5148\u751F\u6210\u8BC1\u4E66)`));
247
- }
248
- if (daemonMode) {
249
- console.log(green(`\u670D\u52A1\u5DF2\u542F\u52A8:`));
250
- console.log(green(` HTTP: http://127.0.0.1:${effectivePort}/ui`));
251
- console.log(green(` HTTPS: https://127.0.0.1:4443/ui (\u5982\u5DF2\u542F\u7528)`));
252
- }
253
- if (!daemonMode) {
254
- const forwardSignal = (signal) => {
255
- if (!child.killed) {
256
- try {
257
- child.kill(signal);
258
- } catch {
259
- }
260
- }
261
- };
262
- process.on("SIGINT", forwardSignal);
263
- process.on("SIGTERM", forwardSignal);
264
- await new Promise((resolve) => {
265
- child.on("exit", () => resolve());
266
- child.on("close", () => resolve());
267
- });
268
- process.off("SIGINT", forwardSignal);
269
- process.off("SIGTERM", forwardSignal);
270
- await removePid();
271
- if (child.exitCode && child.exitCode !== 0) {
272
- process.exitCode = child.exitCode;
273
- }
274
- }
275
- }
276
- async function handleStop() {
277
- const pid = await readPid();
278
- if (!pid) {
279
- console.log(yellow("cc-gw \u672A\u5728\u8FD0\u884C\u3002"));
280
- return;
281
- }
282
- if (!isProcessAlive(pid)) {
283
- console.log(yellow("\u68C0\u6D4B\u5230\u9648\u65E7\u7684 PID \u6587\u4EF6\uFF0C\u5DF2\u6E05\u7406\u3002"));
284
- await removePid();
285
- return;
286
- }
287
- try {
288
- process.kill(pid, "SIGTERM");
289
- console.log(green(`\u5DF2\u5411\u8FDB\u7A0B ${pid} \u53D1\u9001 SIGTERM`));
290
- } catch (err) {
291
- console.error(`\u505C\u6B62 cc-gw \u5931\u8D25: ${err.message}`);
292
- } finally {
293
- await removePid();
294
- }
295
- }
296
- async function handleStatus() {
297
- const { running, pid } = await isServiceRunning();
298
- if (running && pid) {
299
- console.log(green(`cc-gw \u6B63\u5728\u8FD0\u884C (pid: ${pid})`));
300
- } else {
301
- console.log(yellow("cc-gw \u672A\u5728\u8FD0\u884C\u3002"));
302
- }
303
- console.log(`PID \u6587\u4EF6: ${PID_FILE}`);
304
- console.log(`\u65E5\u5FD7\u76EE\u5F55: ${LOG_DIR}`);
305
- console.log(`\u914D\u7F6E\u6587\u4EF6: ${CONFIG_FILE}`);
306
- }
307
- program.name("cc-gw").description("Claude Code Gateway CLI").version(CLI_VERSION);
308
- program.command("version").description("\u663E\u793A\u5F53\u524D cc-gw CLI \u7248\u672C").action(() => {
309
- console.log(`cc-gw CLI \u7248\u672C: ${CLI_VERSION}`);
310
- });
311
- program.command("start").description("\u542F\u52A8 cc-gw \u670D\u52A1").option("--daemon", "\u4EE5\u5B88\u62A4\u8FDB\u7A0B\u65B9\u5F0F\u8FD0\u884C\uFF08\u9ED8\u8BA4\uFF09").option("--foreground", "\u4EE5\u524D\u53F0\u6A21\u5F0F\u8FD0\u884C\u5E76\u4FDD\u6301\u63A7\u5236\u53F0\u8F93\u51FA").option("--port <port>", "\u6307\u5B9A\u670D\u52A1\u76D1\u542C\u7AEF\u53E3").action(async (options) => {
312
- try {
313
- await handleStart(options);
314
- } catch (err) {
315
- console.error(err.message);
316
- process.exitCode = 1;
317
- }
318
- });
319
- program.command("stop").description("\u505C\u6B62 cc-gw \u670D\u52A1").action(async () => {
320
- await handleStop();
321
- });
322
- program.command("restart").description("\u91CD\u542F cc-gw \u670D\u52A1").option("--daemon", "\u4EE5\u5B88\u62A4\u8FDB\u7A0B\u65B9\u5F0F\u8FD0\u884C").option("--port <port>", "\u6307\u5B9A\u670D\u52A1\u76D1\u542C\u7AEF\u53E3").action(async (options) => {
323
- await handleStop();
324
- await handleStart(options);
325
- });
326
- program.command("status").description("\u67E5\u770B cc-gw \u8FD0\u884C\u72B6\u6001").action(async () => {
327
- await handleStatus();
328
- });
329
- program.parseAsync(process.argv);