@myrialabs/clopen 0.2.20 → 0.2.21
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.
|
@@ -6,14 +6,16 @@
|
|
|
6
6
|
*
|
|
7
7
|
* Strategy:
|
|
8
8
|
* 1. Check DB for previously stored server URL → health-check → reuse if alive
|
|
9
|
-
* 2. Otherwise spawn `opencode serve`
|
|
9
|
+
* 2. Otherwise spawn `opencode serve` via Bun.spawn (port 0, OS-assigned) and persist URL
|
|
10
10
|
* 3. On every ensureClient() call, verify the server is still alive.
|
|
11
11
|
* If it died mid-session, automatically recover through the same flow.
|
|
12
12
|
*/
|
|
13
13
|
|
|
14
14
|
import type { OpencodeClient } from '@opencode-ai/sdk';
|
|
15
|
+
import type { Subprocess } from 'bun';
|
|
15
16
|
import { getOpenCodeMcpConfig } from '../../../mcp';
|
|
16
17
|
import { settingsQueries } from '../../../database/queries';
|
|
18
|
+
import { resolveCommand } from '../../../ws/engine/utils';
|
|
17
19
|
import { debug } from '$shared/utils/logger';
|
|
18
20
|
|
|
19
21
|
const OPENCODE_HOST = '127.0.0.1';
|
|
@@ -21,6 +23,7 @@ const DB_KEY = 'opencode.server.url';
|
|
|
21
23
|
const HEALTH_TIMEOUT = 1500;
|
|
22
24
|
const SERVER_START_TIMEOUT = 30_000; // 30s — generous for slow devices
|
|
23
25
|
|
|
26
|
+
let serverProc: Subprocess | null = null;
|
|
24
27
|
let serverHandle: { url: string; close(): void } | null = null;
|
|
25
28
|
let client: OpencodeClient | null = null;
|
|
26
29
|
let initPromise: Promise<void> | null = null;
|
|
@@ -31,6 +34,7 @@ function clearClientState(): void {
|
|
|
31
34
|
client = null;
|
|
32
35
|
ready = false;
|
|
33
36
|
serverHandle = null;
|
|
37
|
+
serverProc = null;
|
|
34
38
|
ownsProcess = false;
|
|
35
39
|
}
|
|
36
40
|
|
|
@@ -113,8 +117,9 @@ async function init(): Promise<void> {
|
|
|
113
117
|
settingsQueries.delete(DB_KEY);
|
|
114
118
|
}
|
|
115
119
|
|
|
116
|
-
// 2. Spawn a new server
|
|
117
|
-
const
|
|
120
|
+
// 2. Spawn a new server via Bun.spawn — uses same binary resolution as detectCLI
|
|
121
|
+
const command = await resolveCommand('opencode');
|
|
122
|
+
const args = [command, 'serve', `--hostname=${OPENCODE_HOST}`, '--port=0'];
|
|
118
123
|
|
|
119
124
|
// Build MCP config — but only inject if the MCP endpoint is actually reachable.
|
|
120
125
|
// The opencode binary may initialize MCP connections synchronously before printing
|
|
@@ -137,25 +142,81 @@ async function init(): Promise<void> {
|
|
|
137
142
|
}
|
|
138
143
|
}
|
|
139
144
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
145
|
+
const configContent = Object.keys(mcpConfig).length > 0
|
|
146
|
+
? JSON.stringify({ mcp: mcpConfig })
|
|
147
|
+
: '{}';
|
|
148
|
+
|
|
149
|
+
debug.log('engine', `Spawning: ${args.join(' ')}`);
|
|
150
|
+
|
|
151
|
+
const proc = Bun.spawn(args, {
|
|
152
|
+
stdout: 'pipe',
|
|
153
|
+
stderr: 'pipe',
|
|
154
|
+
env: {
|
|
155
|
+
...process.env,
|
|
156
|
+
OPENCODE_CONFIG_CONTENT: configContent,
|
|
157
|
+
},
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
// Parse server URL from stdout — opencode prints "opencode server listening on <url>"
|
|
161
|
+
const url = await new Promise<string>((resolve, reject) => {
|
|
162
|
+
const timeout = setTimeout(() => {
|
|
163
|
+
proc.kill();
|
|
164
|
+
reject(new Error(`Timeout waiting for opencode server to start after ${SERVER_START_TIMEOUT}ms`));
|
|
165
|
+
}, SERVER_START_TIMEOUT);
|
|
166
|
+
|
|
167
|
+
let output = '';
|
|
168
|
+
|
|
169
|
+
const readStream = async (stream: ReadableStream<Uint8Array>, label: string) => {
|
|
170
|
+
const reader = stream.getReader();
|
|
171
|
+
const decoder = new TextDecoder();
|
|
172
|
+
try {
|
|
173
|
+
while (true) {
|
|
174
|
+
const { done, value } = await reader.read();
|
|
175
|
+
if (done) break;
|
|
176
|
+
const chunk = decoder.decode(value, { stream: true });
|
|
177
|
+
output += chunk;
|
|
178
|
+
|
|
179
|
+
if (label === 'stdout') {
|
|
180
|
+
for (const line of output.split('\n')) {
|
|
181
|
+
if (line.startsWith('opencode server listening')) {
|
|
182
|
+
const match = line.match(/on\s+(https?:\/\/[^\s]+)/);
|
|
183
|
+
if (match) {
|
|
184
|
+
clearTimeout(timeout);
|
|
185
|
+
resolve(match[1]);
|
|
186
|
+
return;
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} catch {
|
|
193
|
+
// stream closed
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
readStream(proc.stdout as ReadableStream<Uint8Array>, 'stdout');
|
|
198
|
+
readStream(proc.stderr as ReadableStream<Uint8Array>, 'stderr');
|
|
199
|
+
|
|
200
|
+
proc.exited.then((code) => {
|
|
201
|
+
clearTimeout(timeout);
|
|
202
|
+
let msg = `opencode server exited with code ${code}`;
|
|
203
|
+
if (output.trim()) msg += `\nServer output: ${output}`;
|
|
204
|
+
reject(new Error(msg));
|
|
205
|
+
});
|
|
150
206
|
});
|
|
151
207
|
|
|
152
|
-
|
|
153
|
-
|
|
208
|
+
serverProc = proc;
|
|
209
|
+
const { createOpencodeClient } = await import('@opencode-ai/sdk');
|
|
210
|
+
client = createOpencodeClient({ baseUrl: url });
|
|
211
|
+
serverHandle = {
|
|
212
|
+
url,
|
|
213
|
+
close() { proc.kill(); },
|
|
214
|
+
};
|
|
154
215
|
ownsProcess = true;
|
|
155
216
|
ready = true;
|
|
156
217
|
|
|
157
|
-
settingsQueries.set(DB_KEY,
|
|
158
|
-
debug.log('engine', `Open Code client ready (server: ${
|
|
218
|
+
settingsQueries.set(DB_KEY, url);
|
|
219
|
+
debug.log('engine', `Open Code client ready (server: ${url})`);
|
|
159
220
|
}
|
|
160
221
|
|
|
161
222
|
export function getClient(): OpencodeClient | null {
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* Converts Open Code messages/events → SDKMessage (Claude format)
|
|
6
6
|
* so stream-manager and frontend remain unchanged.
|
|
7
7
|
*
|
|
8
|
-
* Server lifecycle is managed by ./server.ts (
|
|
8
|
+
* Server lifecycle is managed by ./server.ts (Bun.spawn).
|
|
9
9
|
* This file only contains the OpenCodeEngine class (per-project instance).
|
|
10
10
|
*/
|
|
11
11
|
|
package/bin/clopen.ts
CHANGED
|
@@ -359,10 +359,8 @@ async function clearAllData() {
|
|
|
359
359
|
// ============================================================
|
|
360
360
|
|
|
361
361
|
async function setupEnvironment() {
|
|
362
|
-
if (
|
|
363
|
-
|
|
364
|
-
copyFileSync(ENV_EXAMPLE, ENV_FILE);
|
|
365
|
-
}
|
|
362
|
+
if (existsSync(ENV_EXAMPLE)) {
|
|
363
|
+
copyFileSync(ENV_EXAMPLE, ENV_FILE);
|
|
366
364
|
}
|
|
367
365
|
}
|
|
368
366
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myrialabs/clopen",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.21",
|
|
4
4
|
"description": "All-in-one web workspace for Claude Code & OpenCode — chat, terminal, git, browser preview, checkpoints, and real-time collaboration",
|
|
5
5
|
"author": "Myria Labs",
|
|
6
6
|
"license": "MIT",
|