@myrialabs/clopen 0.2.18 → 0.2.20
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.
|
@@ -19,6 +19,7 @@ import { debug } from '$shared/utils/logger';
|
|
|
19
19
|
const OPENCODE_HOST = '127.0.0.1';
|
|
20
20
|
const DB_KEY = 'opencode.server.url';
|
|
21
21
|
const HEALTH_TIMEOUT = 1500;
|
|
22
|
+
const SERVER_START_TIMEOUT = 30_000; // 30s — generous for slow devices
|
|
22
23
|
|
|
23
24
|
let serverHandle: { url: string; close(): void } | null = null;
|
|
24
25
|
let client: OpencodeClient | null = null;
|
|
@@ -26,10 +27,9 @@ let initPromise: Promise<void> | null = null;
|
|
|
26
27
|
let ready = false;
|
|
27
28
|
let ownsProcess = false;
|
|
28
29
|
|
|
29
|
-
function
|
|
30
|
+
function clearClientState(): void {
|
|
30
31
|
client = null;
|
|
31
32
|
ready = false;
|
|
32
|
-
initPromise = null;
|
|
33
33
|
serverHandle = null;
|
|
34
34
|
ownsProcess = false;
|
|
35
35
|
}
|
|
@@ -51,22 +51,36 @@ async function isServerAlive(url: string): Promise<boolean> {
|
|
|
51
51
|
* If the server died (bun --watch restart, crash, etc.), state is reset and
|
|
52
52
|
* init() is re-invoked — the same DB-check → reuse-or-spawn flow handles
|
|
53
53
|
* recovery automatically.
|
|
54
|
+
*
|
|
55
|
+
* Race-condition guard: after any `await` (health check), we re-check
|
|
56
|
+
* `initPromise` before starting a new init — another caller may have
|
|
57
|
+
* already kicked one off during our async gap.
|
|
54
58
|
*/
|
|
55
59
|
export async function ensureClient(): Promise<OpencodeClient> {
|
|
60
|
+
// Wait for any in-progress init first
|
|
61
|
+
if (initPromise) {
|
|
62
|
+
try { await initPromise; } catch { /* handled below */ }
|
|
63
|
+
if (client && ready) return client;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Fast path: server is up and healthy
|
|
56
67
|
if (client && ready && serverHandle) {
|
|
57
68
|
if (await isServerAlive(serverHandle.url)) return client;
|
|
58
69
|
|
|
59
|
-
// Server disconnected — purge stale DB entry
|
|
70
|
+
// Server disconnected — purge stale DB entry
|
|
71
|
+
// Do NOT clear initPromise — another caller may have started one
|
|
60
72
|
debug.log('engine', 'Open Code server disconnected, recovering...');
|
|
61
73
|
settingsQueries.delete(DB_KEY);
|
|
62
|
-
|
|
74
|
+
clearClientState();
|
|
63
75
|
}
|
|
64
76
|
|
|
77
|
+
// Re-check after async gap — another caller may have started init
|
|
65
78
|
if (initPromise) {
|
|
66
|
-
await initPromise;
|
|
67
|
-
return client
|
|
79
|
+
try { await initPromise; } catch { /* handled below */ }
|
|
80
|
+
if (client && ready) return client;
|
|
68
81
|
}
|
|
69
82
|
|
|
83
|
+
// Start init — no other caller is initializing at this point
|
|
70
84
|
initPromise = init();
|
|
71
85
|
try {
|
|
72
86
|
await initPromise;
|
|
@@ -102,17 +116,34 @@ async function init(): Promise<void> {
|
|
|
102
116
|
// 2. Spawn a new server — port 0 lets opencode pick an OS-assigned port
|
|
103
117
|
const { createOpencode } = await import('@opencode-ai/sdk');
|
|
104
118
|
|
|
105
|
-
|
|
119
|
+
// Build MCP config — but only inject if the MCP endpoint is actually reachable.
|
|
120
|
+
// The opencode binary may initialize MCP connections synchronously before printing
|
|
121
|
+
// "opencode server listening", so an unreachable/slow MCP endpoint can stall startup.
|
|
122
|
+
let mcpConfig = getOpenCodeMcpConfig();
|
|
106
123
|
if (Object.keys(mcpConfig).length > 0) {
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
124
|
+
const mcpUrls = Object.values(mcpConfig).map(c => (c as any).url).filter(Boolean);
|
|
125
|
+
const reachable = await Promise.all(
|
|
126
|
+
mcpUrls.map(url => isServerAlive(url))
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
if (reachable.every(Boolean)) {
|
|
130
|
+
debug.log('engine', `Open Code server: injecting ${Object.keys(mcpConfig).length} MCP server(s)`);
|
|
131
|
+
for (const [name, config] of Object.entries(mcpConfig)) {
|
|
132
|
+
debug.log('engine', ` → ${name}: ${config.type} (${(config as any).url || (config as any).command?.join(' ')})`);
|
|
133
|
+
}
|
|
134
|
+
} else {
|
|
135
|
+
debug.warn('engine', 'Open Code server: MCP endpoint not reachable, starting without MCP');
|
|
136
|
+
mcpConfig = {};
|
|
110
137
|
}
|
|
111
138
|
}
|
|
112
139
|
|
|
140
|
+
// No retry — SDK does not kill the child process on timeout, so retrying
|
|
141
|
+
// would leave zombie `opencode serve` processes running in the background.
|
|
142
|
+
// 30s timeout is generous enough for slow devices.
|
|
113
143
|
const result = await createOpencode({
|
|
114
144
|
hostname: OPENCODE_HOST,
|
|
115
145
|
port: 0,
|
|
146
|
+
timeout: SERVER_START_TIMEOUT,
|
|
116
147
|
...(Object.keys(mcpConfig).length > 0 && {
|
|
117
148
|
config: { mcp: mcpConfig },
|
|
118
149
|
}),
|
|
@@ -152,6 +183,7 @@ export async function disposeOpenCodeClient(): Promise<void> {
|
|
|
152
183
|
}
|
|
153
184
|
}
|
|
154
185
|
|
|
155
|
-
|
|
186
|
+
clearClientState();
|
|
187
|
+
initPromise = null;
|
|
156
188
|
debug.log('engine', 'Open Code client disposed');
|
|
157
189
|
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Browser, BrowserContext, Page } from 'puppeteer';
|
|
2
2
|
import { EventEmitter } from 'events';
|
|
3
|
-
import { getViewportDimensions } from '$
|
|
3
|
+
import { getViewportDimensions } from '$shared/constants/preview.js';
|
|
4
4
|
import type { BrowserTab, BrowserTabInfo, DeviceSize, Rotation } from './types';
|
|
5
5
|
import { DEFAULT_STREAMING_CONFIG } from './types';
|
|
6
6
|
import { browserPool } from './browser-pool';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { Browser, BrowserContext, Page } from 'puppeteer';
|
|
2
|
-
import type { DeviceSize, Rotation } from '$
|
|
2
|
+
import type { DeviceSize, Rotation } from '$shared/constants/preview.js';
|
|
3
3
|
|
|
4
4
|
// Re-export types from preview config
|
|
5
5
|
export type { DeviceSize, Rotation };
|
package/bin/clopen.ts
CHANGED
|
@@ -383,15 +383,43 @@ async function startServer(options: CLIOptions) {
|
|
|
383
383
|
if (options.host) env.CLOPEN_HOST = options.host;
|
|
384
384
|
if (options.log) env.CLOPEN_DEBUG = 'true';
|
|
385
385
|
|
|
386
|
+
startSpinner('Starting server...');
|
|
387
|
+
|
|
386
388
|
const serverProc = Bun.spawn(['bun', startScript], {
|
|
387
389
|
cwd: __dirname,
|
|
388
|
-
stdout: '
|
|
389
|
-
stderr: '
|
|
390
|
+
stdout: 'pipe',
|
|
391
|
+
stderr: 'pipe',
|
|
390
392
|
stdin: 'inherit',
|
|
391
393
|
env,
|
|
392
394
|
});
|
|
393
395
|
|
|
394
|
-
|
|
396
|
+
// Forward subprocess output, stopping spinner on first chunk
|
|
397
|
+
let spinnerActive = true;
|
|
398
|
+
|
|
399
|
+
async function pipeStream(stream: ReadableStream<Uint8Array>, target: NodeJS.WriteStream) {
|
|
400
|
+
const reader = stream.getReader();
|
|
401
|
+
try {
|
|
402
|
+
while (true) {
|
|
403
|
+
const { done, value } = await reader.read();
|
|
404
|
+
if (done) break;
|
|
405
|
+
if (spinnerActive) {
|
|
406
|
+
stopSpinner();
|
|
407
|
+
spinnerActive = false;
|
|
408
|
+
}
|
|
409
|
+
target.write(value);
|
|
410
|
+
}
|
|
411
|
+
} finally {
|
|
412
|
+
reader.releaseLock();
|
|
413
|
+
}
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
await Promise.all([
|
|
417
|
+
pipeStream(serverProc.stdout, process.stdout),
|
|
418
|
+
pipeStream(serverProc.stderr, process.stderr),
|
|
419
|
+
serverProc.exited,
|
|
420
|
+
]);
|
|
421
|
+
|
|
422
|
+
if (spinnerActive) stopSpinner();
|
|
395
423
|
}
|
|
396
424
|
|
|
397
425
|
// ============================================================
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@myrialabs/clopen",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.20",
|
|
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",
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// Preview Configuration
|
|
2
|
+
// Centralized configuration for preview components
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Device viewport dimensions
|
|
6
|
+
* These are the physical pixel dimensions for each device type.
|
|
7
|
+
* Desktop and laptop are naturally landscape (width > height).
|
|
8
|
+
* Tablet and mobile are naturally portrait (height > width).
|
|
9
|
+
*/
|
|
10
|
+
export const DEVICE_VIEWPORTS = {
|
|
11
|
+
desktop: { width: 1920, height: 1080 }, // Naturally landscape
|
|
12
|
+
laptop: { width: 1280, height: 800 }, // Naturally landscape
|
|
13
|
+
tablet: { width: 820, height: 1050 }, // Naturally portrait
|
|
14
|
+
mobile: { width: 393, height: 740 } // Naturally portrait
|
|
15
|
+
} as const;
|
|
16
|
+
|
|
17
|
+
// Type exports
|
|
18
|
+
export type DeviceSize = keyof typeof DEVICE_VIEWPORTS;
|
|
19
|
+
export type Rotation = 'portrait' | 'landscape';
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Get viewport dimensions based on device size and rotation
|
|
23
|
+
*
|
|
24
|
+
* SIMPLE RULE:
|
|
25
|
+
* - Portrait: height > width (vertical orientation)
|
|
26
|
+
* - Landscape: width > height (horizontal orientation)
|
|
27
|
+
*/
|
|
28
|
+
export function getViewportDimensions(
|
|
29
|
+
deviceSize: DeviceSize,
|
|
30
|
+
rotation: Rotation
|
|
31
|
+
): { width: number; height: number } {
|
|
32
|
+
const viewport = DEVICE_VIEWPORTS[deviceSize];
|
|
33
|
+
|
|
34
|
+
// Get the larger and smaller dimension
|
|
35
|
+
const larger = Math.max(viewport.width, viewport.height);
|
|
36
|
+
const smaller = Math.min(viewport.width, viewport.height);
|
|
37
|
+
|
|
38
|
+
if (rotation === 'portrait') {
|
|
39
|
+
// Portrait: height > width (vertical)
|
|
40
|
+
return { width: smaller, height: larger };
|
|
41
|
+
} else {
|
|
42
|
+
// Landscape: width > height (horizontal)
|
|
43
|
+
return { width: larger, height: smaller };
|
|
44
|
+
}
|
|
45
|
+
}
|