qwenproxy-cli 1.0.0 → 1.0.1
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/LICENSE +14 -14
- package/README.md +906 -906
- package/bin/qwenproxy.js +5 -1
- package/package.json +78 -78
- package/src/api/error-classifier.ts +159 -159
- package/src/api/error-helpers.ts +118 -118
- package/src/api/models.ts +261 -261
- package/src/api/server.ts +860 -859
- package/src/cache/memory-cache.ts +385 -385
- package/src/clean-cache.ts +204 -204
- package/src/core/account-concurrency.ts +671 -671
- package/src/core/account-manager.ts +301 -297
- package/src/core/account-priority.ts +163 -163
- package/src/core/accounts.ts +186 -186
- package/src/core/config.ts +383 -383
- package/src/core/crypto-utils.ts +79 -79
- package/src/core/database.ts +276 -276
- package/src/core/errors.ts +118 -118
- package/src/core/logger.ts +269 -269
- package/src/core/memory-usage.ts +84 -84
- package/src/core/metrics.ts +291 -291
- package/src/core/model-alias.ts +77 -77
- package/src/core/model-registry.ts +544 -544
- package/src/core/mutex.ts +119 -119
- package/src/core/paths.ts +199 -199
- package/src/core/prompt-limits.ts +214 -214
- package/src/core/reasoning-effort.ts +102 -102
- package/src/core/stream-registry.ts +96 -96
- package/src/core/waf-isolation.ts +117 -117
- package/src/core/watchdog.ts +195 -195
- package/src/delete-chats.ts +23 -23
- package/src/index.ts +65 -64
- package/src/login.ts +147 -147
- package/src/reset-cooldowns.ts +11 -11
- package/src/routes/anthropic/index.ts +355 -355
- package/src/routes/anthropic/translate.ts +522 -522
- package/src/routes/anthropic/types.ts +154 -154
- package/src/routes/anthropic/validation.ts +144 -144
- package/src/routes/chat/account.ts +1817 -1817
- package/src/routes/chat/context.ts +241 -241
- package/src/routes/chat/errors.ts +85 -85
- package/src/routes/chat/helpers.ts +268 -268
- package/src/routes/chat/index.ts +618 -618
- package/src/routes/chat/media.ts +285 -285
- package/src/routes/chat/retry-policy.ts +754 -754
- package/src/routes/chat/stop.ts +98 -98
- package/src/routes/chat/streaming.ts +2710 -2710
- package/src/routes/chat/validation.ts +526 -526
- package/src/routes/chat.ts +2 -2
- package/src/routes/completions.ts +290 -290
- package/src/routes/images.ts +139 -139
- package/src/routes/responses/adapter.ts +503 -503
- package/src/routes/responses/index.ts +405 -405
- package/src/routes/responses/state.ts +230 -230
- package/src/routes/responses/streaming.ts +528 -528
- package/src/routes/responses/types.ts +285 -285
- package/src/routes/responses/validation.ts +202 -202
- package/src/routes/upload.ts +731 -731
- package/src/routes/videos.ts +214 -214
- package/src/services/auth-playwright.ts +173 -173
- package/src/services/captcha-coordinator.ts +161 -161
- package/src/services/captcha-solver.ts +553 -553
- package/src/services/chat-cleanup.ts +80 -80
- package/src/services/context-meter.ts +317 -317
- package/src/services/fingerprint.ts +242 -242
- package/src/services/human-behavior.ts +173 -173
- package/src/services/media-generation.ts +1748 -1748
- package/src/services/playwright.ts +2800 -2800
- package/src/services/qwen-chat-pool.ts +345 -345
- package/src/services/qwen-errors.ts +133 -133
- package/src/services/qwen-headers.ts +79 -79
- package/src/services/qwen-thread-state.ts +393 -393
- package/src/services/qwen-url.ts +19 -19
- package/src/services/qwen.ts +3126 -3126
- package/src/services/session-keeper.ts +88 -88
- package/src/services/token-estimation-metrics.ts +118 -118
- package/src/sync/claude-code.ts +75 -75
- package/src/sync/codex.ts +123 -123
- package/src/sync/index.ts +362 -362
- package/src/sync/omp.ts +105 -105
- package/src/sync/opencode.ts +214 -214
- package/src/sync/types.ts +53 -53
- package/src/sync/utils.ts +27 -27
- package/src/sync-clients.ts +189 -189
- package/src/tools/instructions.ts +137 -137
- package/src/tools/manifest.ts +81 -81
- package/src/tools/parser.ts +2989 -2989
- package/src/tools/toolcall-tags.ts +142 -142
- package/src/tui/app.ts +264 -264
- package/src/tui/index.ts +61 -61
- package/src/tui/markdown.ts +258 -258
- package/src/tui/proxy-client.ts +331 -326
- package/src/tui/screen.ts +294 -278
- package/src/tui/server-manager.ts +270 -270
- package/src/tui/theme.ts +432 -432
- package/src/tui/types.ts +33 -33
- package/src/tui/views/accounts-view.ts +656 -656
- package/src/tui/views/chat-view.ts +1018 -823
- package/src/tui/views/logs-view.ts +479 -413
- package/src/tui/views/status-view.ts +204 -204
- package/src/tui/views/storage-view.ts +304 -291
- package/src/tui/views/sync-view.ts +409 -409
- package/src/types/ali-oss.d.ts +32 -32
- package/src/utils/context-truncation.ts +84 -84
- package/src/utils/json.ts +380 -380
- package/src/utils/session-id.ts +37 -37
- package/src/utils/tool-call-guard.ts +84 -84
- package/src/utils/types.ts +109 -109
package/src/sync/types.ts
CHANGED
|
@@ -1,53 +1,53 @@
|
|
|
1
|
-
export interface ClientSyncResult {
|
|
2
|
-
client: "claude-code" | "codex" | "opencode" | "omp";
|
|
3
|
-
filePath: string;
|
|
4
|
-
backupPath?: string;
|
|
5
|
-
success: boolean;
|
|
6
|
-
action: "updated" | "created" | "skipped" | "restored" | "failed";
|
|
7
|
-
message?: string;
|
|
8
|
-
error?: string;
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
export interface SyncOptions {
|
|
12
|
-
filePath: string;
|
|
13
|
-
apiKey: string;
|
|
14
|
-
baseUrl: string;
|
|
15
|
-
model?: string;
|
|
16
|
-
setActive?: boolean;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export interface SyncAllOptions {
|
|
20
|
-
apiKey?: string;
|
|
21
|
-
port?: number;
|
|
22
|
-
host?: string;
|
|
23
|
-
setActive?: boolean;
|
|
24
|
-
stateFilePath?: string;
|
|
25
|
-
targets?: ("claude-code" | "codex" | "opencode" | "omp")[];
|
|
26
|
-
customPaths?: {
|
|
27
|
-
claudeCode?: string;
|
|
28
|
-
codex?: string;
|
|
29
|
-
openCode?: string;
|
|
30
|
-
omp?: string;
|
|
31
|
-
};
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
export interface SyncRecord {
|
|
35
|
-
filePath: string;
|
|
36
|
-
backupPath: string;
|
|
37
|
-
existedBefore: boolean;
|
|
38
|
-
syncedAt: number;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
export interface SyncStateFile {
|
|
42
|
-
version: number;
|
|
43
|
-
updatedAt: string;
|
|
44
|
-
apiKey: string;
|
|
45
|
-
port: number;
|
|
46
|
-
host: string;
|
|
47
|
-
clients: {
|
|
48
|
-
claudeCode?: SyncRecord;
|
|
49
|
-
codex?: SyncRecord;
|
|
50
|
-
openCode?: SyncRecord;
|
|
51
|
-
omp?: SyncRecord;
|
|
52
|
-
};
|
|
53
|
-
}
|
|
1
|
+
export interface ClientSyncResult {
|
|
2
|
+
client: "claude-code" | "codex" | "opencode" | "omp";
|
|
3
|
+
filePath: string;
|
|
4
|
+
backupPath?: string;
|
|
5
|
+
success: boolean;
|
|
6
|
+
action: "updated" | "created" | "skipped" | "restored" | "failed";
|
|
7
|
+
message?: string;
|
|
8
|
+
error?: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface SyncOptions {
|
|
12
|
+
filePath: string;
|
|
13
|
+
apiKey: string;
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
model?: string;
|
|
16
|
+
setActive?: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface SyncAllOptions {
|
|
20
|
+
apiKey?: string;
|
|
21
|
+
port?: number;
|
|
22
|
+
host?: string;
|
|
23
|
+
setActive?: boolean;
|
|
24
|
+
stateFilePath?: string;
|
|
25
|
+
targets?: ("claude-code" | "codex" | "opencode" | "omp")[];
|
|
26
|
+
customPaths?: {
|
|
27
|
+
claudeCode?: string;
|
|
28
|
+
codex?: string;
|
|
29
|
+
openCode?: string;
|
|
30
|
+
omp?: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface SyncRecord {
|
|
35
|
+
filePath: string;
|
|
36
|
+
backupPath: string;
|
|
37
|
+
existedBefore: boolean;
|
|
38
|
+
syncedAt: number;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export interface SyncStateFile {
|
|
42
|
+
version: number;
|
|
43
|
+
updatedAt: string;
|
|
44
|
+
apiKey: string;
|
|
45
|
+
port: number;
|
|
46
|
+
host: string;
|
|
47
|
+
clients: {
|
|
48
|
+
claudeCode?: SyncRecord;
|
|
49
|
+
codex?: SyncRecord;
|
|
50
|
+
openCode?: SyncRecord;
|
|
51
|
+
omp?: SyncRecord;
|
|
52
|
+
};
|
|
53
|
+
}
|
package/src/sync/utils.ts
CHANGED
|
@@ -1,27 +1,27 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import path from "node:path";
|
|
3
|
-
|
|
4
|
-
export function createTimestampBackup(filePath: string): string {
|
|
5
|
-
const dir = path.dirname(filePath);
|
|
6
|
-
const ext = path.extname(filePath);
|
|
7
|
-
const base = path.basename(filePath, ext);
|
|
8
|
-
const now = new Date();
|
|
9
|
-
const pad = (n: number) => String(n).padStart(2, "0");
|
|
10
|
-
const timestamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
11
|
-
const backupPath = path.join(dir, `${base}.qwenproxy.${timestamp}${ext}.bak`);
|
|
12
|
-
fs.copyFileSync(filePath, backupPath);
|
|
13
|
-
return backupPath;
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
export function restoreFromBackup(filePath: string, backupPath?: string): boolean {
|
|
17
|
-
if (!backupPath || !fs.existsSync(backupPath)) {
|
|
18
|
-
return false;
|
|
19
|
-
}
|
|
20
|
-
fs.copyFileSync(backupPath, filePath);
|
|
21
|
-
try {
|
|
22
|
-
fs.unlinkSync(backupPath);
|
|
23
|
-
} catch {
|
|
24
|
-
// Ignore cleanup error
|
|
25
|
-
}
|
|
26
|
-
return true;
|
|
27
|
-
}
|
|
1
|
+
import fs from "node:fs";
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
|
|
4
|
+
export function createTimestampBackup(filePath: string): string {
|
|
5
|
+
const dir = path.dirname(filePath);
|
|
6
|
+
const ext = path.extname(filePath);
|
|
7
|
+
const base = path.basename(filePath, ext);
|
|
8
|
+
const now = new Date();
|
|
9
|
+
const pad = (n: number) => String(n).padStart(2, "0");
|
|
10
|
+
const timestamp = `${now.getFullYear()}${pad(now.getMonth() + 1)}${pad(now.getDate())}_${pad(now.getHours())}${pad(now.getMinutes())}${pad(now.getSeconds())}`;
|
|
11
|
+
const backupPath = path.join(dir, `${base}.qwenproxy.${timestamp}${ext}.bak`);
|
|
12
|
+
fs.copyFileSync(filePath, backupPath);
|
|
13
|
+
return backupPath;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function restoreFromBackup(filePath: string, backupPath?: string): boolean {
|
|
17
|
+
if (!backupPath || !fs.existsSync(backupPath)) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
fs.copyFileSync(backupPath, filePath);
|
|
21
|
+
try {
|
|
22
|
+
fs.unlinkSync(backupPath);
|
|
23
|
+
} catch {
|
|
24
|
+
// Ignore cleanup error
|
|
25
|
+
}
|
|
26
|
+
return true;
|
|
27
|
+
}
|
package/src/sync-clients.ts
CHANGED
|
@@ -1,189 +1,189 @@
|
|
|
1
|
-
import "dotenv/config";
|
|
2
|
-
import {
|
|
3
|
-
syncAllClients,
|
|
4
|
-
restoreAllClients,
|
|
5
|
-
normalizeClientName,
|
|
6
|
-
getDefaultPaths,
|
|
7
|
-
inspectClientSyncStatus,
|
|
8
|
-
} from "./sync/index.ts";
|
|
9
|
-
import fs from "node:fs";
|
|
10
|
-
|
|
11
|
-
function parseArgs() {
|
|
12
|
-
const args = process.argv.slice(2);
|
|
13
|
-
const options: {
|
|
14
|
-
restore: boolean;
|
|
15
|
-
list: boolean;
|
|
16
|
-
help: boolean;
|
|
17
|
-
apiKey?: string;
|
|
18
|
-
port?: number;
|
|
19
|
-
host?: string;
|
|
20
|
-
setActive: boolean;
|
|
21
|
-
targets: ("claude-code" | "codex" | "opencode" | "omp")[];
|
|
22
|
-
} = {
|
|
23
|
-
restore: false,
|
|
24
|
-
list: false,
|
|
25
|
-
help: false,
|
|
26
|
-
setActive: true,
|
|
27
|
-
targets: [],
|
|
28
|
-
};
|
|
29
|
-
|
|
30
|
-
for (let i = 0; i < args.length; i++) {
|
|
31
|
-
const arg = args[i];
|
|
32
|
-
if (arg === "--restore" || arg === "restore") {
|
|
33
|
-
options.restore = true;
|
|
34
|
-
} else if (arg === "--list" || arg === "list") {
|
|
35
|
-
options.list = true;
|
|
36
|
-
} else if (arg === "--help" || arg === "-h" || arg === "help") {
|
|
37
|
-
options.help = true;
|
|
38
|
-
} else if (arg === "--api-key" && args[i + 1]) {
|
|
39
|
-
options.apiKey = args[++i];
|
|
40
|
-
} else if (arg === "--port" && args[i + 1]) {
|
|
41
|
-
options.port = parseInt(args[++i], 10);
|
|
42
|
-
} else if (arg === "--host" && args[i + 1]) {
|
|
43
|
-
options.host = args[++i];
|
|
44
|
-
} else if (arg === "--no-active") {
|
|
45
|
-
options.setActive = false;
|
|
46
|
-
} else if (arg === "--client" && args[i + 1]) {
|
|
47
|
-
const normalized = normalizeClientName(args[++i]);
|
|
48
|
-
if (normalized) options.targets.push(normalized);
|
|
49
|
-
} else if (!arg.startsWith("-")) {
|
|
50
|
-
const normalized = normalizeClientName(arg);
|
|
51
|
-
if (normalized) options.targets.push(normalized);
|
|
52
|
-
}
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
return options;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
function printHelp() {
|
|
59
|
-
console.log(`
|
|
60
|
-
Uso:
|
|
61
|
-
npm run sync [clientes...] [opções]
|
|
62
|
-
|
|
63
|
-
Exemplos:
|
|
64
|
-
npm run sync # Sincroniza todos os clientes detectados
|
|
65
|
-
npm run sync claude # Sincroniza apenas o Claude Code
|
|
66
|
-
npm run sync codex # Sincroniza apenas o Codex CLI
|
|
67
|
-
npm run sync opencode # Sincroniza apenas o OpenCode
|
|
68
|
-
npm run sync omp # Sincroniza apenas o OMP (Oh My Pi)
|
|
69
|
-
npm run sync claude codex # Sincroniza Claude Code e Codex
|
|
70
|
-
npm run sync -- --list # Lista os arquivos dos clientes detectados
|
|
71
|
-
npm run sync -- --restore # Restaura as configurações originais (rollback)
|
|
72
|
-
Opções:
|
|
73
|
-
--client <nome> Nome do cliente (claude, codex, opencode, omp)
|
|
74
|
-
--api-key <chave> Sobrescrever chave de API (padrão: lê do .env ou usa sk-qwenproxy-local)
|
|
75
|
-
--port <porta> Sobrescrever porta do servidor (padrão: lê do .env ou usa 7936)
|
|
76
|
-
--host <host> Sobrescrever host do servidor (padrão: 127.0.0.1)
|
|
77
|
-
--no-active Não definir o modelo ativo no Codex (apenas adiciona o provider)
|
|
78
|
-
--restore Desfaz alterações restaurando backups
|
|
79
|
-
--list Mostra status de detecção dos arquivos de configuração
|
|
80
|
-
`);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
async function main() {
|
|
84
|
-
const options = parseArgs();
|
|
85
|
-
|
|
86
|
-
console.log("==================================================");
|
|
87
|
-
console.log(" 🚀 QwenProxy - Client Configuration Sync");
|
|
88
|
-
console.log("==================================================");
|
|
89
|
-
|
|
90
|
-
if (options.help) {
|
|
91
|
-
printHelp();
|
|
92
|
-
return;
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
if (options.list) {
|
|
96
|
-
console.log("\n📁 Status de detecção dos clientes no seu computador:\n");
|
|
97
|
-
const defaultPaths = getDefaultPaths();
|
|
98
|
-
const clients = [
|
|
99
|
-
{ id: "claude-code" as const, name: "Claude Code", path: defaultPaths.claudeCode },
|
|
100
|
-
{ id: "codex" as const, name: "Codex CLI", path: defaultPaths.codex },
|
|
101
|
-
{ id: "opencode" as const, name: "OpenCode", path: defaultPaths.openCode },
|
|
102
|
-
{ id: "omp" as const, name: "OMP (Oh My Pi)", path: defaultPaths.omp },
|
|
103
|
-
];
|
|
104
|
-
|
|
105
|
-
for (const c of clients) {
|
|
106
|
-
const status = inspectClientSyncStatus(c.id, c.path);
|
|
107
|
-
let badge = "";
|
|
108
|
-
let icon = "⚪";
|
|
109
|
-
if (status.synced) {
|
|
110
|
-
icon = "✅";
|
|
111
|
-
badge = status.model ? `[Sincronizado: ${status.model}]` : "[Sincronizado]";
|
|
112
|
-
} else if (status.installed) {
|
|
113
|
-
icon = "⚠️ ";
|
|
114
|
-
badge = "[Detectado - Outro provedor]";
|
|
115
|
-
} else {
|
|
116
|
-
icon = "⚪";
|
|
117
|
-
badge = "[Não instalado]";
|
|
118
|
-
}
|
|
119
|
-
console.log(` ${icon} ${c.name.padEnd(16)} ${badge}`);
|
|
120
|
-
console.log(` ${c.path}`);
|
|
121
|
-
}
|
|
122
|
-
console.log("\nPara sincronizar um ou todos, execute:");
|
|
123
|
-
console.log(" npm run sync");
|
|
124
|
-
console.log(" npm run sync claude\n");
|
|
125
|
-
return;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
if (options.restore) {
|
|
129
|
-
console.log("\n🔄 Restaurando configurações originais a partir dos backups...\n");
|
|
130
|
-
const result = restoreAllClients();
|
|
131
|
-
if (result.restoredCount === 0) {
|
|
132
|
-
console.log("ℹ️ Nenhum backup ou arquivo de estado anterior encontrado para restaurar.");
|
|
133
|
-
} else {
|
|
134
|
-
for (const item of result.details) {
|
|
135
|
-
console.log(` ✓ Restaurado [${item.client}]: ${item.filePath}`);
|
|
136
|
-
}
|
|
137
|
-
console.log(`\n✅ ${result.restoredCount} configuração(ões) restaurada(s) com sucesso!`);
|
|
138
|
-
}
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
const targetNames = options.targets.length > 0
|
|
143
|
-
? options.targets.join(", ")
|
|
144
|
-
: "todos os detectados";
|
|
145
|
-
|
|
146
|
-
console.log(`\n📡 Sincronizando clientes: [${targetNames}]...\n`);
|
|
147
|
-
|
|
148
|
-
const result = syncAllClients({
|
|
149
|
-
apiKey: options.apiKey,
|
|
150
|
-
port: options.port,
|
|
151
|
-
host: options.host,
|
|
152
|
-
setActive: options.setActive,
|
|
153
|
-
targets: options.targets.length > 0 ? options.targets : undefined,
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
console.log(`🔑 Chave API: ${result.apiKey}`);
|
|
157
|
-
console.log(`🌐 Porta: ${result.port}`);
|
|
158
|
-
console.log(`🏠 Host: ${result.host}\n`);
|
|
159
|
-
|
|
160
|
-
let count = 0;
|
|
161
|
-
for (const client of Object.values(result.clients)) {
|
|
162
|
-
if (!client) continue;
|
|
163
|
-
count++;
|
|
164
|
-
const icon = client.success ? "✅" : "❌";
|
|
165
|
-
console.log(`${icon} [${client.client}]`);
|
|
166
|
-
console.log(` Arquivo: ${client.filePath}`);
|
|
167
|
-
if (client.backupPath) {
|
|
168
|
-
console.log(` Backup: ${client.backupPath}`);
|
|
169
|
-
}
|
|
170
|
-
if (client.message) {
|
|
171
|
-
console.log(` Status: ${client.message}`);
|
|
172
|
-
}
|
|
173
|
-
if (client.error) {
|
|
174
|
-
console.log(` Erro: ${client.error}`);
|
|
175
|
-
}
|
|
176
|
-
console.log("");
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
console.log("--------------------------------------------------");
|
|
180
|
-
console.log(`✨ ${count} cliente(s) sincronizado(s) com zero perda de outras configs/provedores!`);
|
|
181
|
-
console.log("💡 Para desfazer e restaurar a qualquer momento:");
|
|
182
|
-
console.log(" npm run sync -- --restore");
|
|
183
|
-
console.log("==================================================\n");
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
main().catch((err) => {
|
|
187
|
-
console.error("❌ Erro fatal durante a sincronização:", err);
|
|
188
|
-
process.exit(1);
|
|
189
|
-
});
|
|
1
|
+
import "dotenv/config";
|
|
2
|
+
import {
|
|
3
|
+
syncAllClients,
|
|
4
|
+
restoreAllClients,
|
|
5
|
+
normalizeClientName,
|
|
6
|
+
getDefaultPaths,
|
|
7
|
+
inspectClientSyncStatus,
|
|
8
|
+
} from "./sync/index.ts";
|
|
9
|
+
import fs from "node:fs";
|
|
10
|
+
|
|
11
|
+
function parseArgs() {
|
|
12
|
+
const args = process.argv.slice(2);
|
|
13
|
+
const options: {
|
|
14
|
+
restore: boolean;
|
|
15
|
+
list: boolean;
|
|
16
|
+
help: boolean;
|
|
17
|
+
apiKey?: string;
|
|
18
|
+
port?: number;
|
|
19
|
+
host?: string;
|
|
20
|
+
setActive: boolean;
|
|
21
|
+
targets: ("claude-code" | "codex" | "opencode" | "omp")[];
|
|
22
|
+
} = {
|
|
23
|
+
restore: false,
|
|
24
|
+
list: false,
|
|
25
|
+
help: false,
|
|
26
|
+
setActive: true,
|
|
27
|
+
targets: [],
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
for (let i = 0; i < args.length; i++) {
|
|
31
|
+
const arg = args[i];
|
|
32
|
+
if (arg === "--restore" || arg === "restore") {
|
|
33
|
+
options.restore = true;
|
|
34
|
+
} else if (arg === "--list" || arg === "list") {
|
|
35
|
+
options.list = true;
|
|
36
|
+
} else if (arg === "--help" || arg === "-h" || arg === "help") {
|
|
37
|
+
options.help = true;
|
|
38
|
+
} else if (arg === "--api-key" && args[i + 1]) {
|
|
39
|
+
options.apiKey = args[++i];
|
|
40
|
+
} else if (arg === "--port" && args[i + 1]) {
|
|
41
|
+
options.port = parseInt(args[++i], 10);
|
|
42
|
+
} else if (arg === "--host" && args[i + 1]) {
|
|
43
|
+
options.host = args[++i];
|
|
44
|
+
} else if (arg === "--no-active") {
|
|
45
|
+
options.setActive = false;
|
|
46
|
+
} else if (arg === "--client" && args[i + 1]) {
|
|
47
|
+
const normalized = normalizeClientName(args[++i]);
|
|
48
|
+
if (normalized) options.targets.push(normalized);
|
|
49
|
+
} else if (!arg.startsWith("-")) {
|
|
50
|
+
const normalized = normalizeClientName(arg);
|
|
51
|
+
if (normalized) options.targets.push(normalized);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return options;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function printHelp() {
|
|
59
|
+
console.log(`
|
|
60
|
+
Uso:
|
|
61
|
+
npm run sync [clientes...] [opções]
|
|
62
|
+
|
|
63
|
+
Exemplos:
|
|
64
|
+
npm run sync # Sincroniza todos os clientes detectados
|
|
65
|
+
npm run sync claude # Sincroniza apenas o Claude Code
|
|
66
|
+
npm run sync codex # Sincroniza apenas o Codex CLI
|
|
67
|
+
npm run sync opencode # Sincroniza apenas o OpenCode
|
|
68
|
+
npm run sync omp # Sincroniza apenas o OMP (Oh My Pi)
|
|
69
|
+
npm run sync claude codex # Sincroniza Claude Code e Codex
|
|
70
|
+
npm run sync -- --list # Lista os arquivos dos clientes detectados
|
|
71
|
+
npm run sync -- --restore # Restaura as configurações originais (rollback)
|
|
72
|
+
Opções:
|
|
73
|
+
--client <nome> Nome do cliente (claude, codex, opencode, omp)
|
|
74
|
+
--api-key <chave> Sobrescrever chave de API (padrão: lê do .env ou usa sk-qwenproxy-local)
|
|
75
|
+
--port <porta> Sobrescrever porta do servidor (padrão: lê do .env ou usa 7936)
|
|
76
|
+
--host <host> Sobrescrever host do servidor (padrão: 127.0.0.1)
|
|
77
|
+
--no-active Não definir o modelo ativo no Codex (apenas adiciona o provider)
|
|
78
|
+
--restore Desfaz alterações restaurando backups
|
|
79
|
+
--list Mostra status de detecção dos arquivos de configuração
|
|
80
|
+
`);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
async function main() {
|
|
84
|
+
const options = parseArgs();
|
|
85
|
+
|
|
86
|
+
console.log("==================================================");
|
|
87
|
+
console.log(" 🚀 QwenProxy - Client Configuration Sync");
|
|
88
|
+
console.log("==================================================");
|
|
89
|
+
|
|
90
|
+
if (options.help) {
|
|
91
|
+
printHelp();
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
if (options.list) {
|
|
96
|
+
console.log("\n📁 Status de detecção dos clientes no seu computador:\n");
|
|
97
|
+
const defaultPaths = getDefaultPaths();
|
|
98
|
+
const clients = [
|
|
99
|
+
{ id: "claude-code" as const, name: "Claude Code", path: defaultPaths.claudeCode },
|
|
100
|
+
{ id: "codex" as const, name: "Codex CLI", path: defaultPaths.codex },
|
|
101
|
+
{ id: "opencode" as const, name: "OpenCode", path: defaultPaths.openCode },
|
|
102
|
+
{ id: "omp" as const, name: "OMP (Oh My Pi)", path: defaultPaths.omp },
|
|
103
|
+
];
|
|
104
|
+
|
|
105
|
+
for (const c of clients) {
|
|
106
|
+
const status = inspectClientSyncStatus(c.id, c.path);
|
|
107
|
+
let badge = "";
|
|
108
|
+
let icon = "⚪";
|
|
109
|
+
if (status.synced) {
|
|
110
|
+
icon = "✅";
|
|
111
|
+
badge = status.model ? `[Sincronizado: ${status.model}]` : "[Sincronizado]";
|
|
112
|
+
} else if (status.installed) {
|
|
113
|
+
icon = "⚠️ ";
|
|
114
|
+
badge = "[Detectado - Outro provedor]";
|
|
115
|
+
} else {
|
|
116
|
+
icon = "⚪";
|
|
117
|
+
badge = "[Não instalado]";
|
|
118
|
+
}
|
|
119
|
+
console.log(` ${icon} ${c.name.padEnd(16)} ${badge}`);
|
|
120
|
+
console.log(` ${c.path}`);
|
|
121
|
+
}
|
|
122
|
+
console.log("\nPara sincronizar um ou todos, execute:");
|
|
123
|
+
console.log(" npm run sync");
|
|
124
|
+
console.log(" npm run sync claude\n");
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
if (options.restore) {
|
|
129
|
+
console.log("\n🔄 Restaurando configurações originais a partir dos backups...\n");
|
|
130
|
+
const result = restoreAllClients();
|
|
131
|
+
if (result.restoredCount === 0) {
|
|
132
|
+
console.log("ℹ️ Nenhum backup ou arquivo de estado anterior encontrado para restaurar.");
|
|
133
|
+
} else {
|
|
134
|
+
for (const item of result.details) {
|
|
135
|
+
console.log(` ✓ Restaurado [${item.client}]: ${item.filePath}`);
|
|
136
|
+
}
|
|
137
|
+
console.log(`\n✅ ${result.restoredCount} configuração(ões) restaurada(s) com sucesso!`);
|
|
138
|
+
}
|
|
139
|
+
return;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const targetNames = options.targets.length > 0
|
|
143
|
+
? options.targets.join(", ")
|
|
144
|
+
: "todos os detectados";
|
|
145
|
+
|
|
146
|
+
console.log(`\n📡 Sincronizando clientes: [${targetNames}]...\n`);
|
|
147
|
+
|
|
148
|
+
const result = syncAllClients({
|
|
149
|
+
apiKey: options.apiKey,
|
|
150
|
+
port: options.port,
|
|
151
|
+
host: options.host,
|
|
152
|
+
setActive: options.setActive,
|
|
153
|
+
targets: options.targets.length > 0 ? options.targets : undefined,
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
console.log(`🔑 Chave API: ${result.apiKey}`);
|
|
157
|
+
console.log(`🌐 Porta: ${result.port}`);
|
|
158
|
+
console.log(`🏠 Host: ${result.host}\n`);
|
|
159
|
+
|
|
160
|
+
let count = 0;
|
|
161
|
+
for (const client of Object.values(result.clients)) {
|
|
162
|
+
if (!client) continue;
|
|
163
|
+
count++;
|
|
164
|
+
const icon = client.success ? "✅" : "❌";
|
|
165
|
+
console.log(`${icon} [${client.client}]`);
|
|
166
|
+
console.log(` Arquivo: ${client.filePath}`);
|
|
167
|
+
if (client.backupPath) {
|
|
168
|
+
console.log(` Backup: ${client.backupPath}`);
|
|
169
|
+
}
|
|
170
|
+
if (client.message) {
|
|
171
|
+
console.log(` Status: ${client.message}`);
|
|
172
|
+
}
|
|
173
|
+
if (client.error) {
|
|
174
|
+
console.log(` Erro: ${client.error}`);
|
|
175
|
+
}
|
|
176
|
+
console.log("");
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
console.log("--------------------------------------------------");
|
|
180
|
+
console.log(`✨ ${count} cliente(s) sincronizado(s) com zero perda de outras configs/provedores!`);
|
|
181
|
+
console.log("💡 Para desfazer e restaurar a qualquer momento:");
|
|
182
|
+
console.log(" npm run sync -- --restore");
|
|
183
|
+
console.log("==================================================\n");
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
main().catch((err) => {
|
|
187
|
+
console.error("❌ Erro fatal durante a sincronização:", err);
|
|
188
|
+
process.exit(1);
|
|
189
|
+
});
|