opencode-studio-server 1.16.7 → 1.18.0

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 (3) hide show
  1. package/index.js +3484 -3441
  2. package/package.json +1 -1
  3. package/proxy-manager.js +0 -228
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "opencode-studio-server",
3
- "version": "1.16.7",
3
+ "version": "1.18.0",
4
4
  "description": "Backend server for OpenCode Studio - manages opencode configurations",
5
5
  "main": "index.js",
6
6
  "bin": {
package/proxy-manager.js DELETED
@@ -1,228 +0,0 @@
1
- const { spawn, exec, execSync } = require('child_process');
2
- const fs = require('fs');
3
- const path = require('path');
4
- const os = require('os');
5
- const yaml = require('js-yaml');
6
-
7
- const HOME_DIR = os.homedir();
8
- const CONFIG_DIR = path.join(HOME_DIR, '.config', 'opencode-studio');
9
- const PROXY_CONFIG_FILE = path.join(CONFIG_DIR, 'cliproxy.yaml');
10
- const PROXY_AUTH_DIR = path.join(HOME_DIR, '.cli-proxy-api');
11
-
12
- let proxyProcess = null;
13
- let isProxyRunning = false;
14
-
15
- const checkBinary = (cmd) => {
16
- return new Promise((resolve) => {
17
- if (path.isAbsolute(cmd)) {
18
- return resolve(fs.existsSync(cmd));
19
- }
20
- const checkCmd = process.platform === 'win32' ? `where ${cmd}` : `which ${cmd}`;
21
- exec(checkCmd, (err) => {
22
- resolve(!err);
23
- });
24
- });
25
- };
26
-
27
- const getProxyCommand = async () => {
28
- if (process.platform === 'win32') {
29
- const localAppData = process.env.LOCALAPPDATA;
30
- if (localAppData) {
31
- const aliases = ['CLIProxyAPI.exe', 'cli-proxy-api.exe', 'cliproxyapi.exe'];
32
- for (const alias of aliases) {
33
- const wingetPath = path.join(localAppData, 'Microsoft', 'WinGet', 'Links', alias);
34
- if (fs.existsSync(wingetPath)) return wingetPath;
35
- }
36
-
37
- const progPath = path.join(localAppData, 'Programs', 'CLIProxyAPI', 'CLIProxyAPI.exe');
38
- if (fs.existsSync(progPath)) return progPath;
39
- }
40
- }
41
-
42
- if (await checkBinary('cli-proxy-api')) return 'cli-proxy-api';
43
- if (await checkBinary('cliproxyapi')) return 'cliproxyapi';
44
- if (await checkBinary('CLIProxyAPI')) return 'CLIProxyAPI';
45
- if (await checkBinary('cliproxyapi.exe')) return 'cliproxyapi.exe';
46
- if (await checkBinary('cliproxy')) return 'cliproxy';
47
- return null;
48
- };
49
-
50
- const loadProxyConfig = () => {
51
- if (!fs.existsSync(PROXY_CONFIG_FILE)) {
52
- const defaultConfig = {
53
- port: 8317,
54
- cors: true,
55
- "allow-origin": "*",
56
- "auth-dir": PROXY_AUTH_DIR,
57
- "management-key": "",
58
- routing: { strategy: "round-robin" },
59
- "quota-exceeded": {
60
- "switch-project": true,
61
- "switch-preview-model": true
62
- },
63
- "gemini-api-key": []
64
- };
65
- saveProxyConfig(defaultConfig);
66
- return defaultConfig;
67
- }
68
-
69
- try {
70
- const content = fs.readFileSync(PROXY_CONFIG_FILE, 'utf8');
71
- const config = yaml.load(content);
72
- if (config && config['management-key'] === undefined) {
73
- config['management-key'] = "";
74
- saveProxyConfig(config);
75
- }
76
- if (config && config.cors === undefined) {
77
- config.cors = true;
78
- config['allow-origin'] = "*";
79
- saveProxyConfig(config);
80
- }
81
- return config;
82
- } catch (e) {
83
- console.error("Failed to load proxy config:", e);
84
- return {};
85
- }
86
- };
87
-
88
- const saveProxyConfig = (config) => {
89
- try {
90
- if (!fs.existsSync(CONFIG_DIR)) fs.mkdirSync(CONFIG_DIR, { recursive: true });
91
- const content = yaml.dump(config);
92
- fs.writeFileSync(PROXY_CONFIG_FILE, content);
93
- } catch (e) {
94
- console.error("Failed to save proxy config:", e);
95
- }
96
- };
97
-
98
- const startProxy = async () => {
99
- if (isProxyRunning) return { success: true, message: "Already running" };
100
-
101
- const cmd = await getProxyCommand();
102
- if (!cmd) return { success: false, error: "CLIProxyAPI binary not found. Please install it." };
103
-
104
- if (!fs.existsSync(PROXY_CONFIG_FILE)) loadProxyConfig();
105
-
106
- console.log(`Starting proxy with command: ${cmd} -config "${PROXY_CONFIG_FILE}"`);
107
-
108
- try {
109
- proxyProcess = spawn(cmd, ['-config', PROXY_CONFIG_FILE], {
110
- detached: true,
111
- stdio: 'pipe',
112
- windowsHide: true
113
- });
114
-
115
- proxyProcess.unref();
116
-
117
- proxyProcess.stdout.on('data', (data) => {
118
- console.log(`[Proxy] ${data}`);
119
- });
120
-
121
- proxyProcess.stderr.on('data', (data) => {
122
- console.error(`[Proxy Err] ${data}`);
123
- });
124
-
125
- proxyProcess.on('close', (code) => {
126
- console.log(`[Proxy] Exited with code ${code}`);
127
- isProxyRunning = false;
128
- proxyProcess = null;
129
- });
130
-
131
- isProxyRunning = true;
132
- return { success: true, pid: proxyProcess.pid };
133
- } catch (e) {
134
- return { success: false, error: e.message };
135
- }
136
- };
137
-
138
- const stopProxy = () => {
139
- if (proxyProcess) {
140
- proxyProcess.kill();
141
- proxyProcess = null;
142
- isProxyRunning = false;
143
- return { success: true };
144
- }
145
- return { success: false, error: "Not running" };
146
- };
147
-
148
- const getStatus = async () => {
149
- const cmd = await getProxyCommand();
150
-
151
- let portBusy = false;
152
- try {
153
- if (process.platform === 'win32') {
154
- const out = execSync('netstat -ano | findstr :8317 | findstr LISTENING', { encoding: 'utf8' });
155
- portBusy = out.includes('LISTENING');
156
- } else {
157
- const out = execSync('lsof -i :8317 | grep LISTEN', { encoding: 'utf8' });
158
- portBusy = out.length > 0;
159
- }
160
- } catch (e) {
161
- portBusy = false;
162
- }
163
-
164
- if (portBusy) {
165
- isProxyRunning = true;
166
- } else {
167
- isProxyRunning = false;
168
- proxyProcess = null;
169
- }
170
-
171
- return {
172
- running: isProxyRunning,
173
- pid: proxyProcess?.pid,
174
- configFile: PROXY_CONFIG_FILE,
175
- port: 8317,
176
- installed: !!cmd,
177
- binary: cmd
178
- };
179
- };
180
-
181
- const runLogin = async (provider) => {
182
- const cmd = await getProxyCommand();
183
- if (!cmd) return { success: false, error: "Binary not found" };
184
-
185
- let loginFlag = '';
186
- switch(provider) {
187
- case 'google':
188
- case 'antigravity': loginFlag = '-antigravity-login'; break;
189
- case 'openai':
190
- case 'codex': loginFlag = '-codex-login'; break;
191
- case 'anthropic': loginFlag = '-claude-login'; break;
192
- default: return { success: false, error: "Unknown provider" };
193
- }
194
-
195
- const fullCmd = `${cmd} ${loginFlag} -config "${PROXY_CONFIG_FILE}"`;
196
-
197
- return {
198
- success: true,
199
- command: fullCmd,
200
- message: "Terminal launching..."
201
- };
202
- };
203
-
204
- const listAccounts = () => {
205
- if (!fs.existsSync(PROXY_AUTH_DIR)) return [];
206
- try {
207
- return fs.readdirSync(PROXY_AUTH_DIR)
208
- .filter(f => f.endsWith('.json'))
209
- .map(f => {
210
- const parts = f.replace('.json', '').split('-');
211
- const provider = parts[0];
212
- const email = parts.slice(1).join('-').replace(/_/g, '.').replace('.gmail.com', '@gmail.com');
213
- return { id: f, provider, email: email || f };
214
- });
215
- } catch {
216
- return [];
217
- }
218
- };
219
-
220
- module.exports = {
221
- startProxy,
222
- stopProxy,
223
- getStatus,
224
- loadProxyConfig,
225
- saveProxyConfig,
226
- runLogin,
227
- listAccounts
228
- };