@causyne/agent 1.20.124 → 1.20.128
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/bin/causyne.js +1 -1
- package/docs/THIRD_PARTY_NOTICES.md +36 -2
- package/package.json +6 -4
- package/src/acp/protocol.ts +19 -1
- package/src/acp/server.js +282 -8
- package/src/acp/server.ts +284 -8
- package/src/agent/abortableDelay.js +2 -2
- package/src/agent/abortableDelay.ts +2 -2
- package/src/agent/errorClassifier.js +9 -1
- package/src/agent/errorClassifier.ts +8 -1
- package/src/agentLoop.js +142 -53
- package/src/agentLoop.ts +151 -50
- package/src/approvalPolicy.js +34 -10
- package/src/approvalPolicy.ts +42 -11
- package/src/browser/causyneBrowser.js +61 -3
- package/src/browser/causyneBrowser.ts +65 -3
- package/src/browser/urlPolicy.js +14 -1
- package/src/browser/urlPolicy.ts +12 -1
- package/src/cliMode.js +21 -9
- package/src/cliMode.ts +20 -8
- package/src/commands.js +365 -45
- package/src/commands.ts +403 -45
- package/src/config.js +19 -4
- package/src/config.ts +18 -4
- package/src/core/contextCompactor.js +54 -7
- package/src/core/contextCompactor.ts +63 -7
- package/src/core/goalLoop.js +257 -0
- package/src/core/goalLoop.ts +310 -0
- package/src/entrypoint.js +135 -3
- package/src/entrypoint.ts +136 -3
- package/src/game/causyneCoreBridge.js +686 -0
- package/src/game/causyneCoreBridge.ts +702 -0
- package/src/game/gameLauncher.js +287 -0
- package/src/game/gameLauncher.ts +352 -0
- package/src/game/gameProvisioner.js +425 -0
- package/src/game/gameProvisioner.ts +488 -0
- package/src/headlessCli.js +19 -12
- package/src/headlessCli.ts +22 -8
- package/src/infra/atomicWriter.js +40 -4
- package/src/infra/atomicWriter.ts +36 -4
- package/src/launchIntent.js +73 -3
- package/src/launchIntent.ts +76 -3
- package/src/lsp/lspClient.js +83 -12
- package/src/lsp/lspClient.ts +75 -12
- package/src/mcp/mcpRegistry.js +75 -8
- package/src/mcp/mcpRegistry.ts +70 -7
- package/src/memory/contextBudget.js +2 -0
- package/src/memory/contextBudget.ts +1 -0
- package/src/memory/episodicStore.js +40 -0
- package/src/memory/episodicStore.ts +46 -0
- package/src/memory.js +67 -0
- package/src/memory.ts +65 -0
- package/src/modelVariants.js +45 -0
- package/src/modelVariants.ts +54 -0
- package/src/outputRecovery.js +3 -2
- package/src/outputRecovery.ts +4 -2
- package/src/permissionPresets.js +327 -0
- package/src/permissionPresets.ts +361 -0
- package/src/personalization.js +365 -0
- package/src/personalization.ts +471 -0
- package/src/projectInstructions.js +30 -10
- package/src/projectInstructions.ts +31 -12
- package/src/providers/acp.js +3 -2
- package/src/providers/acp.ts +3 -2
- package/src/providers/antigravity.js +64 -9
- package/src/providers/antigravity.ts +75 -9
- package/src/providers/byokHttp.js +7 -1
- package/src/providers/byokHttp.ts +6 -1
- package/src/providers/byokStream.js +21 -8
- package/src/providers/byokStream.ts +17 -7
- package/src/providers/dashscope-byok.js +21 -5
- package/src/providers/dashscope-byok.ts +29 -11
- package/src/providers/moonshot.js +26 -7
- package/src/providers/moonshot.ts +34 -13
- package/src/providers/muse.js +6 -0
- package/src/providers/muse.ts +8 -0
- package/src/providers/ollama.js +39 -7
- package/src/providers/ollama.ts +36 -7
- package/src/providers/openai-compatible.js +16 -3
- package/src/providers/openai-compatible.ts +13 -3
- package/src/providers/openai.js +62 -4
- package/src/providers/openai.ts +50 -4
- package/src/providers/openrouter.js +19 -4
- package/src/providers/openrouter.ts +21 -4
- package/src/providers/qwen-byok.js +21 -5
- package/src/providers/qwen-byok.ts +29 -11
- package/src/providers/xai.js +26 -10
- package/src/providers/xai.ts +34 -16
- package/src/reporting.js +4 -2
- package/src/reporting.ts +4 -2
- package/src/sandbox.js +34 -0
- package/src/sandbox.ts +43 -0
- package/src/scheduler/scheduler.js +372 -0
- package/src/scheduler/scheduler.ts +433 -0
- package/src/sessionSearchIndex.js +267 -0
- package/src/sessionSearchIndex.ts +311 -0
- package/src/skillsManager.js +2 -3
- package/src/skillsManager.ts +2 -3
- package/src/toolPacks.js +6 -0
- package/src/toolPacks.ts +6 -0
- package/src/tools/applyPatch.js +1 -1
- package/src/tools/applyPatch.ts +1 -1
- package/src/tools/generateMediaViaApi.js +4 -3
- package/src/tools/generateMediaViaApi.ts +4 -3
- package/src/tools/memoryWrite.js +15 -1
- package/src/tools/memoryWrite.ts +16 -1
- package/src/tools/nativeEngine.js +2 -2
- package/src/tools/nativeEngine.ts +2 -2
- package/src/tools/openSourceToolProvisioner.js +2 -2
- package/src/tools/openSourceToolProvisioner.ts +2 -2
- package/src/tools/osSandbox.js +1 -1
- package/src/tools/osSandbox.ts +1 -1
- package/src/tools/payloadSanitizer.js +0 -1
- package/src/tools/payloadSanitizer.ts +0 -1
- package/src/tools/pdfReader.js +122 -20
- package/src/tools/pdfReader.ts +111 -20
- package/src/tools/presentDeliverable.js +103 -0
- package/src/tools/presentDeliverable.ts +127 -0
- package/src/tools/ptcRuntime.js +444 -0
- package/src/tools/ptcRuntime.ts +490 -0
- package/src/tools/ptyTerminal.js +1108 -0
- package/src/tools/ptyTerminal.ts +1245 -0
- package/src/tools/registry.js +21 -12
- package/src/tools/registry.ts +21 -12
- package/src/tools/toolOutputStore.js +26 -4
- package/src/tools/toolOutputStore.ts +22 -4
- package/src/tools/webFetch.js +158 -11
- package/src/tools/webFetch.ts +145 -11
- package/src/tools/webSearch.js +123 -36
- package/src/tools/webSearch.ts +124 -33
- package/src/tui/banner.js +5 -1
- package/src/tui/banner.ts +6 -1
- package/src/tui/chatRenderer.js +1 -2
- package/src/tui/chatRenderer.ts +1 -2
- package/src/tui/completion.js +1 -0
- package/src/tui/completion.ts +1 -0
- package/src/tui/composerControls.js +111 -27
- package/src/tui/composerControls.ts +124 -28
- package/src/tui/dialogs.js +3 -16
- package/src/tui/dialogs.ts +3 -16
- package/src/tui/home.js +1 -0
- package/src/tui/home.ts +1 -0
- package/src/tui/invocation.js +1 -2
- package/src/tui/invocation.ts +1 -2
- package/src/tui/modelPicker.js +22 -12
- package/src/tui/modelPicker.ts +22 -12
- package/src/tui/overlay.js +2 -5
- package/src/tui/overlay.ts +2 -5
- package/src/tui/prompt.js +6 -4
- package/src/tui/prompt.ts +6 -4
- package/src/tui/selectList.js +4 -12
- package/src/tui/selectList.ts +4 -12
- package/src/tui/setup.js +22 -9
- package/src/tui/setup.ts +26 -9
- package/src/tui/statusLine.ts +2 -0
- package/src/web/client/browserPanel.js +905 -0
- package/src/web/client/browserPanel.ts +872 -0
- package/src/web/client/chatStream.js +321 -0
- package/src/web/client/chatStream.ts +297 -0
- package/src/web/client/cliPanel.js +165 -0
- package/src/web/client/cliPanel.ts +171 -0
- package/src/web/client/composer.js +204 -0
- package/src/web/client/composer.ts +209 -0
- package/src/web/client/diffPanel.js +176 -0
- package/src/web/client/diffPanel.ts +203 -0
- package/src/web/client/dom.js +271 -0
- package/src/web/client/dom.ts +272 -0
- package/src/web/client/filesAndCanvas.js +233 -0
- package/src/web/client/filesAndCanvas.ts +240 -0
- package/src/web/client/gameLauncherUi.js +78 -0
- package/src/web/client/gameLauncherUi.ts +78 -0
- package/src/web/client/main.js +316 -0
- package/src/web/client/main.ts +391 -0
- package/src/web/client/markdown.js +364 -0
- package/src/web/client/markdown.ts +373 -0
- package/src/web/client/modelPicker.js +455 -0
- package/src/web/client/modelPicker.ts +451 -0
- package/src/web/client/permissionPresetsUi.js +307 -0
- package/src/web/client/permissionPresetsUi.ts +333 -0
- package/src/web/client/sessions.js +353 -0
- package/src/web/client/sessions.ts +372 -0
- package/src/web/client/settingsModal.js +824 -0
- package/src/web/client/settingsModal.ts +959 -0
- package/src/web/client/state.js +280 -0
- package/src/web/client/state.ts +259 -0
- package/src/web/client/streamSmoother.js +127 -0
- package/src/web/client/streamSmoother.ts +119 -0
- package/src/web/client/studioLayout.js +89 -0
- package/src/web/client/studioLayout.ts +103 -0
- package/src/web/client/studioShell.js +660 -0
- package/src/web/client/studioShell.ts +672 -0
- package/src/web/client/studioStart.js +77 -0
- package/src/web/client/studioStart.ts +89 -0
- package/src/web/client/terminalPanel.js +606 -0
- package/src/web/client/terminalPanel.ts +632 -0
- package/src/web/client/terminalTheme.js +249 -0
- package/src/web/client/terminalTheme.ts +258 -0
- package/src/web/client/turnView.js +934 -0
- package/src/web/client/turnView.ts +981 -0
- package/src/web/client/types.js +1 -0
- package/src/web/client/types.ts +322 -0
- package/src/web/client/validators.js +147 -0
- package/src/web/client/validators.ts +140 -0
- package/src/web/server/activeRuns.js +152 -0
- package/src/web/server/activeRuns.ts +171 -0
- package/src/web/server/routesBrowser.js +409 -0
- package/src/web/server/routesBrowser.ts +435 -0
- package/src/web/server/routesChat.js +304 -0
- package/src/web/server/routesChat.ts +347 -0
- package/src/web/server/routesCore.js +472 -0
- package/src/web/server/routesCore.ts +522 -0
- package/src/web/server/routesSettings.js +344 -0
- package/src/web/server/routesSettings.ts +397 -0
- package/src/web/server/routesTerminal.js +162 -0
- package/src/web/server/routesTerminal.ts +182 -0
- package/src/web/server/routesWorkspace.js +510 -0
- package/src/web/server/routesWorkspace.ts +546 -0
- package/src/web/server/security.js +98 -0
- package/src/web/server/security.ts +108 -0
- package/src/web/server/sessionTranscript.js +586 -0
- package/src/web/server/sessionTranscript.ts +566 -0
- package/src/web/server/staticAssets.js +104 -0
- package/src/web/server/staticAssets.ts +116 -0
- package/src/web/server/types.js +1 -0
- package/src/web/server/types.ts +135 -0
- package/src/web/server/workspaceFiles.js +368 -0
- package/src/web/server/workspaceFiles.ts +363 -0
- package/src/web/skybridgeServer.js +78 -2448
- package/src/web/skybridgeServer.ts +120 -2600
- package/src/web/skybridgeUi.js +20 -450
- package/src/web/skybridgeUi.ts +24 -455
- package/src/web/styles/agent.css +380 -0
- package/src/web/styles/composer.css +193 -0
- package/src/web/styles/overlays.css +935 -0
- package/src/web/styles/shell.css +387 -0
- package/src/web/styles/studio.css +951 -0
- package/src/web/styles/thread.css +221 -0
- package/src/web/styles/tokens.css +94 -0
- package/src/web/ui/brandMark.js +73 -0
- package/src/web/ui/brandMark.ts +82 -0
- package/src/web/ui/modalsTemplate.js +388 -0
- package/src/web/ui/modalsTemplate.ts +388 -0
- package/src/web/ui/sidebarTemplate.js +63 -0
- package/src/web/ui/sidebarTemplate.ts +64 -0
- package/src/web/ui/studioTemplate.js +256 -0
- package/src/web/ui/studioTemplate.ts +256 -0
- package/src/web/ui/workspaceTemplate.js +72 -0
- package/src/web/ui/workspaceTemplate.ts +73 -0
- package/src/worktreeManager.js +16 -1
- package/src/worktreeManager.ts +22 -1
- package/src/web/skybridgeClientScript.js +0 -3951
- package/src/web/skybridgeClientScript.ts +0 -3952
- package/src/web/skybridgeStyles.js +0 -827
- package/src/web/skybridgeStyles.ts +0 -828
|
@@ -0,0 +1,287 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-scoped launcher for Causyne Game (`/game`, `causyne game`, and Web Studio).
|
|
3
|
+
*
|
|
4
|
+
* Launches the separate Causyne Game browser workspace strictly on localhost
|
|
5
|
+
* (`127.0.0.1`), injects `CAUSYNE_BIN` pointing directly to this Causyne Agent
|
|
6
|
+
* installation so Causyne Agent operates as Causyne Game's primary agent, waits
|
|
7
|
+
* for `/api/health`, opens the browser via hardened `openBrowserUrl()`, and
|
|
8
|
+
* shuts down cleanly on `/game stop` or CLI exit (zero hidden daemons).
|
|
9
|
+
*/
|
|
10
|
+
import fsSync from 'node:fs';
|
|
11
|
+
import net from 'node:net';
|
|
12
|
+
import path from 'node:path';
|
|
13
|
+
import { spawn } from 'node:child_process';
|
|
14
|
+
import { fileURLToPath } from 'node:url';
|
|
15
|
+
import { openBrowserUrl } from '../utils/browserOpen.js';
|
|
16
|
+
import { writeClipboardSync } from '../tui/clipboardSync.js';
|
|
17
|
+
import { defaultGameManifestUrl, gameDataDir, locateGameStudio } from './gameProvisioner.js';
|
|
18
|
+
import { startCausyneCoreBridge } from './causyneCoreBridge.js';
|
|
19
|
+
let activeGame = null;
|
|
20
|
+
let exitHookInstalled = false;
|
|
21
|
+
function causyneCliBinPath() {
|
|
22
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
23
|
+
return path.resolve(here, '..', '..', 'bin', 'causyne.js');
|
|
24
|
+
}
|
|
25
|
+
function isProcessAlive(child) {
|
|
26
|
+
return child.exitCode === null && child.signalCode === null && !child.killed;
|
|
27
|
+
}
|
|
28
|
+
function ensureExitHook() {
|
|
29
|
+
if (exitHookInstalled)
|
|
30
|
+
return;
|
|
31
|
+
exitHookInstalled = true;
|
|
32
|
+
process.once('exit', () => {
|
|
33
|
+
cleanupGameStudioSync();
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
export function cleanupGameStudioSync() {
|
|
37
|
+
const current = activeGame;
|
|
38
|
+
activeGame = null;
|
|
39
|
+
if (!current)
|
|
40
|
+
return;
|
|
41
|
+
try {
|
|
42
|
+
if (isProcessAlive(current.child))
|
|
43
|
+
current.child.kill('SIGTERM');
|
|
44
|
+
}
|
|
45
|
+
catch {
|
|
46
|
+
// Best-effort cleanup on process exit.
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
export function getGameStudioStatus() {
|
|
50
|
+
if (!activeGame || !isProcessAlive(activeGame.child)) {
|
|
51
|
+
activeGame = null;
|
|
52
|
+
return { running: false };
|
|
53
|
+
}
|
|
54
|
+
return {
|
|
55
|
+
running: true,
|
|
56
|
+
url: activeGame.url,
|
|
57
|
+
origin: activeGame.origin,
|
|
58
|
+
port: activeGame.port,
|
|
59
|
+
dir: activeGame.dir,
|
|
60
|
+
source: activeGame.source,
|
|
61
|
+
...(activeGame.version ? { version: activeGame.version } : {}),
|
|
62
|
+
dataDir: activeGame.dataDir,
|
|
63
|
+
...(typeof activeGame.child.pid === 'number' ? { pid: activeGame.child.pid } : {})
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
export async function stopGameStudio() {
|
|
67
|
+
const current = activeGame;
|
|
68
|
+
activeGame = null;
|
|
69
|
+
if (!current)
|
|
70
|
+
return false;
|
|
71
|
+
try {
|
|
72
|
+
if (isProcessAlive(current.child)) {
|
|
73
|
+
await new Promise((resolve) => {
|
|
74
|
+
let settled = false;
|
|
75
|
+
const done = () => {
|
|
76
|
+
if (settled)
|
|
77
|
+
return;
|
|
78
|
+
settled = true;
|
|
79
|
+
clearTimeout(forceTimer);
|
|
80
|
+
resolve();
|
|
81
|
+
};
|
|
82
|
+
const forceTimer = setTimeout(() => {
|
|
83
|
+
try {
|
|
84
|
+
if (isProcessAlive(current.child))
|
|
85
|
+
current.child.kill('SIGKILL');
|
|
86
|
+
}
|
|
87
|
+
catch {
|
|
88
|
+
// Ignore already-exited child.
|
|
89
|
+
}
|
|
90
|
+
done();
|
|
91
|
+
}, 2500);
|
|
92
|
+
if (typeof forceTimer.unref === 'function')
|
|
93
|
+
forceTimer.unref();
|
|
94
|
+
current.child.once('exit', done);
|
|
95
|
+
try {
|
|
96
|
+
current.child.kill('SIGTERM');
|
|
97
|
+
}
|
|
98
|
+
catch {
|
|
99
|
+
done();
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
finally {
|
|
105
|
+
await current.coreBridge?.close().catch(() => { });
|
|
106
|
+
}
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
async function allocateLoopbackPort(preferredPort) {
|
|
110
|
+
const tryBind = (port) => new Promise((resolve) => {
|
|
111
|
+
const server = net.createServer();
|
|
112
|
+
server.unref();
|
|
113
|
+
server.once('error', () => {
|
|
114
|
+
resolve(null);
|
|
115
|
+
});
|
|
116
|
+
server.listen(port, '127.0.0.1', () => {
|
|
117
|
+
const address = server.address();
|
|
118
|
+
const bound = address && typeof address === 'object' ? address.port : null;
|
|
119
|
+
server.close(() => resolve(bound));
|
|
120
|
+
});
|
|
121
|
+
});
|
|
122
|
+
if (preferredPort > 0) {
|
|
123
|
+
const bound = await tryBind(preferredPort);
|
|
124
|
+
if (bound !== null)
|
|
125
|
+
return bound;
|
|
126
|
+
throw new Error(`Port ${preferredPort} is already in use on 127.0.0.1.`);
|
|
127
|
+
}
|
|
128
|
+
for (let candidate = 4173; candidate < 4183; candidate++) {
|
|
129
|
+
const bound = await tryBind(candidate);
|
|
130
|
+
if (bound !== null)
|
|
131
|
+
return bound;
|
|
132
|
+
}
|
|
133
|
+
const ephemeral = await tryBind(0);
|
|
134
|
+
if (ephemeral !== null)
|
|
135
|
+
return ephemeral;
|
|
136
|
+
throw new Error('Could not allocate a free loopback port on 127.0.0.1.');
|
|
137
|
+
}
|
|
138
|
+
async function waitForHealth(origin, child, getStderr, timeoutMs) {
|
|
139
|
+
const deadline = Date.now() + timeoutMs;
|
|
140
|
+
while (Date.now() < deadline) {
|
|
141
|
+
if (!isProcessAlive(child)) {
|
|
142
|
+
const detail = getStderr().trim().split('\n').filter(Boolean).slice(-4).join(' | ');
|
|
143
|
+
throw new Error(detail ? `Causyne Game exited during startup: ${detail}` : 'Causyne Game server exited during startup.');
|
|
144
|
+
}
|
|
145
|
+
try {
|
|
146
|
+
const res = await fetch(`${origin}/api/health`, {
|
|
147
|
+
signal: AbortSignal.timeout(1500)
|
|
148
|
+
});
|
|
149
|
+
if (res.ok)
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
catch {
|
|
153
|
+
// Server still booting.
|
|
154
|
+
}
|
|
155
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
156
|
+
}
|
|
157
|
+
throw new Error(`Causyne Game did not become ready at ${origin}/api/health within ${Math.round(timeoutMs / 1000)}s.`);
|
|
158
|
+
}
|
|
159
|
+
function assertRunnableInstallation(located, explicitDir) {
|
|
160
|
+
if (!located) {
|
|
161
|
+
const hint = explicitDir
|
|
162
|
+
? `No Causyne Game installation found at "${explicitDir}" (missing server.ts or package.json).`
|
|
163
|
+
: `Causyne Game is not installed yet. Run "causyne game install --yes" (or "/game install --yes") to download it from ${defaultGameManifestUrl()}, or set CAUSYNE_GAME_DIR.`;
|
|
164
|
+
throw new Error(hint);
|
|
165
|
+
}
|
|
166
|
+
if (!located.hasNodeModules) {
|
|
167
|
+
throw new Error(`Causyne Game at "${located.dir}" is missing dependencies. Run "npm ci" inside "${located.dir}".`);
|
|
168
|
+
}
|
|
169
|
+
if (!located.hasStudioBuild) {
|
|
170
|
+
throw new Error(`Causyne Game at "${located.dir}" has not been built yet. Run "npm run build:studio" inside "${located.dir}".`);
|
|
171
|
+
}
|
|
172
|
+
return located;
|
|
173
|
+
}
|
|
174
|
+
export async function launchGameStudio(options = {}) {
|
|
175
|
+
const located = assertRunnableInstallation(locateGameStudio(options.dir), options.dir);
|
|
176
|
+
const targetDataDir = options.dataDir ? path.resolve(options.dataDir) : gameDataDir();
|
|
177
|
+
const requestedPort = typeof options.port === 'number' && Number.isInteger(options.port) && options.port >= 0 && options.port <= 65535
|
|
178
|
+
? options.port
|
|
179
|
+
: 0;
|
|
180
|
+
const openFn = options.opener ?? ((url) => openBrowserUrl(url, { noOpen: options.openBrowser === false }));
|
|
181
|
+
let reused = false;
|
|
182
|
+
if (activeGame && isProcessAlive(activeGame.child)) {
|
|
183
|
+
const sameDir = path.resolve(activeGame.dir) === path.resolve(located.dir);
|
|
184
|
+
const samePort = requestedPort === 0 || activeGame.port === requestedPort;
|
|
185
|
+
if (sameDir && samePort) {
|
|
186
|
+
reused = true;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
await stopGameStudio();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
activeGame = null;
|
|
194
|
+
}
|
|
195
|
+
if (!activeGame) {
|
|
196
|
+
fsSync.mkdirSync(targetDataDir, { recursive: true, mode: 0o700 });
|
|
197
|
+
const port = await allocateLoopbackPort(requestedPort);
|
|
198
|
+
const origin = `http://127.0.0.1:${port}`;
|
|
199
|
+
const url = `${origin}/`;
|
|
200
|
+
const cliBin = causyneCliBinPath();
|
|
201
|
+
const coreBridge = await startCausyneCoreBridge();
|
|
202
|
+
let child;
|
|
203
|
+
try {
|
|
204
|
+
child = spawn(process.execPath, ['--experimental-strip-types', located.serverEntry], {
|
|
205
|
+
cwd: located.dir,
|
|
206
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
207
|
+
windowsHide: true,
|
|
208
|
+
env: {
|
|
209
|
+
...process.env,
|
|
210
|
+
HOST: '127.0.0.1',
|
|
211
|
+
MOTION_BIND_HOST: '127.0.0.1',
|
|
212
|
+
MOTION_SHARED_EDITOR: '0',
|
|
213
|
+
PORT: String(port),
|
|
214
|
+
CAUSYNE_GAME_DATA_DIR: targetDataDir,
|
|
215
|
+
MOTION_DATA_DIR: targetDataDir,
|
|
216
|
+
CAUSYNE_BIN: cliBin,
|
|
217
|
+
CAUSYNE_CORE_URL: coreBridge.url,
|
|
218
|
+
CAUSYNE_CORE_TOKEN: coreBridge.token,
|
|
219
|
+
MOTION_PARENT_PID: String(process.pid),
|
|
220
|
+
MOTION_HEADLESS: '1'
|
|
221
|
+
}
|
|
222
|
+
});
|
|
223
|
+
}
|
|
224
|
+
catch (spawnErr) {
|
|
225
|
+
await coreBridge.close().catch(() => { });
|
|
226
|
+
throw spawnErr;
|
|
227
|
+
}
|
|
228
|
+
let stderrBuf = '';
|
|
229
|
+
child.stderr?.setEncoding('utf8');
|
|
230
|
+
child.stderr?.on('data', (chunk) => {
|
|
231
|
+
stderrBuf = (stderrBuf + chunk).slice(-4096);
|
|
232
|
+
});
|
|
233
|
+
child.stdout?.resume();
|
|
234
|
+
ensureExitHook();
|
|
235
|
+
const record = {
|
|
236
|
+
child,
|
|
237
|
+
coreBridge,
|
|
238
|
+
url,
|
|
239
|
+
origin,
|
|
240
|
+
port,
|
|
241
|
+
dir: located.dir,
|
|
242
|
+
source: located.source,
|
|
243
|
+
...(located.version ? { version: located.version } : {}),
|
|
244
|
+
dataDir: targetDataDir
|
|
245
|
+
};
|
|
246
|
+
activeGame = record;
|
|
247
|
+
child.once('exit', () => {
|
|
248
|
+
if (activeGame === record) {
|
|
249
|
+
activeGame = null;
|
|
250
|
+
void record.coreBridge?.close().catch(() => { });
|
|
251
|
+
}
|
|
252
|
+
});
|
|
253
|
+
try {
|
|
254
|
+
await waitForHealth(origin, child, () => stderrBuf, options.startupTimeoutMs ?? 25_000);
|
|
255
|
+
}
|
|
256
|
+
catch (error) {
|
|
257
|
+
await stopGameStudio();
|
|
258
|
+
throw error;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const running = activeGame;
|
|
262
|
+
const openResult = options.openBrowser === false
|
|
263
|
+
? { opened: false, reason: 'disabled' }
|
|
264
|
+
: await Promise.resolve(openFn(running.url));
|
|
265
|
+
let copiedToClipboard = false;
|
|
266
|
+
if (options.copyToClipboard !== false && process.env.CI !== 'true' && process.env.CI !== '1' && process.env.NODE_ENV !== 'test') {
|
|
267
|
+
try {
|
|
268
|
+
copiedToClipboard = Boolean(writeClipboardSync(running.url));
|
|
269
|
+
}
|
|
270
|
+
catch {
|
|
271
|
+
copiedToClipboard = false;
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
return {
|
|
275
|
+
url: running.url,
|
|
276
|
+
origin: running.origin,
|
|
277
|
+
port: running.port,
|
|
278
|
+
dir: running.dir,
|
|
279
|
+
source: running.source,
|
|
280
|
+
...(running.version ? { version: running.version } : {}),
|
|
281
|
+
dataDir: running.dataDir,
|
|
282
|
+
reused,
|
|
283
|
+
openedBrowser: openResult.opened,
|
|
284
|
+
browserHint: openResult.hint,
|
|
285
|
+
copiedToClipboard
|
|
286
|
+
};
|
|
287
|
+
}
|
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Process-scoped launcher for Causyne Game (`/game`, `causyne game`, and Web Studio).
|
|
3
|
+
*
|
|
4
|
+
* Launches the separate Causyne Game browser workspace strictly on localhost
|
|
5
|
+
* (`127.0.0.1`), injects `CAUSYNE_BIN` pointing directly to this Causyne Agent
|
|
6
|
+
* installation so Causyne Agent operates as Causyne Game's primary agent, waits
|
|
7
|
+
* for `/api/health`, opens the browser via hardened `openBrowserUrl()`, and
|
|
8
|
+
* shuts down cleanly on `/game stop` or CLI exit (zero hidden daemons).
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
import fsSync from 'node:fs';
|
|
12
|
+
import net from 'node:net';
|
|
13
|
+
import path from 'node:path';
|
|
14
|
+
import { spawn, type ChildProcess } from 'node:child_process';
|
|
15
|
+
import { fileURLToPath } from 'node:url';
|
|
16
|
+
import { openBrowserUrl, type OpenBrowserResult } from '../utils/browserOpen.js';
|
|
17
|
+
import { writeClipboardSync } from '../tui/clipboardSync.js';
|
|
18
|
+
import {
|
|
19
|
+
defaultGameManifestUrl,
|
|
20
|
+
gameDataDir,
|
|
21
|
+
locateGameStudio,
|
|
22
|
+
type GameInstallSource,
|
|
23
|
+
type LocatedGameStudio
|
|
24
|
+
} from './gameProvisioner.js';
|
|
25
|
+
import {
|
|
26
|
+
startCausyneCoreBridge,
|
|
27
|
+
type CausyneCoreBridgeHandle
|
|
28
|
+
} from './causyneCoreBridge.js';
|
|
29
|
+
|
|
30
|
+
export interface ActiveGameStudioStatus {
|
|
31
|
+
running: boolean;
|
|
32
|
+
url?: string | undefined;
|
|
33
|
+
origin?: string | undefined;
|
|
34
|
+
port?: number | undefined;
|
|
35
|
+
dir?: string | undefined;
|
|
36
|
+
source?: GameInstallSource | undefined;
|
|
37
|
+
version?: string | undefined;
|
|
38
|
+
dataDir?: string | undefined;
|
|
39
|
+
pid?: number | undefined;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export interface LaunchGameStudioOptions {
|
|
43
|
+
dir?: string | undefined;
|
|
44
|
+
port?: number | undefined;
|
|
45
|
+
dataDir?: string | undefined;
|
|
46
|
+
openBrowser?: boolean | undefined;
|
|
47
|
+
copyToClipboard?: boolean | undefined;
|
|
48
|
+
startupTimeoutMs?: number | undefined;
|
|
49
|
+
opener?: ((url: string) => Promise<OpenBrowserResult> | OpenBrowserResult) | undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export interface LaunchGameStudioResult {
|
|
53
|
+
url: string;
|
|
54
|
+
origin: string;
|
|
55
|
+
port: number;
|
|
56
|
+
dir: string;
|
|
57
|
+
source: GameInstallSource;
|
|
58
|
+
version?: string | undefined;
|
|
59
|
+
dataDir: string;
|
|
60
|
+
reused: boolean;
|
|
61
|
+
openedBrowser: boolean;
|
|
62
|
+
browserHint?: string | undefined;
|
|
63
|
+
copiedToClipboard: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
interface ActiveGameProcess {
|
|
67
|
+
child: ChildProcess;
|
|
68
|
+
coreBridge?: CausyneCoreBridgeHandle | undefined;
|
|
69
|
+
url: string;
|
|
70
|
+
origin: string;
|
|
71
|
+
port: number;
|
|
72
|
+
dir: string;
|
|
73
|
+
source: GameInstallSource;
|
|
74
|
+
version?: string | undefined;
|
|
75
|
+
dataDir: string;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
let activeGame: ActiveGameProcess | null = null;
|
|
79
|
+
let exitHookInstalled = false;
|
|
80
|
+
|
|
81
|
+
function causyneCliBinPath(): string {
|
|
82
|
+
const here = path.dirname(fileURLToPath(import.meta.url));
|
|
83
|
+
return path.resolve(here, '..', '..', 'bin', 'causyne.js');
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function isProcessAlive(child: ChildProcess): boolean {
|
|
87
|
+
return child.exitCode === null && child.signalCode === null && !child.killed;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function ensureExitHook(): void {
|
|
91
|
+
if (exitHookInstalled) return;
|
|
92
|
+
exitHookInstalled = true;
|
|
93
|
+
process.once('exit', () => {
|
|
94
|
+
cleanupGameStudioSync();
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function cleanupGameStudioSync(): void {
|
|
99
|
+
const current = activeGame;
|
|
100
|
+
activeGame = null;
|
|
101
|
+
if (!current) return;
|
|
102
|
+
try {
|
|
103
|
+
if (isProcessAlive(current.child)) current.child.kill('SIGTERM');
|
|
104
|
+
} catch {
|
|
105
|
+
// Best-effort cleanup on process exit.
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
export function getGameStudioStatus(): ActiveGameStudioStatus {
|
|
110
|
+
if (!activeGame || !isProcessAlive(activeGame.child)) {
|
|
111
|
+
activeGame = null;
|
|
112
|
+
return { running: false };
|
|
113
|
+
}
|
|
114
|
+
return {
|
|
115
|
+
running: true,
|
|
116
|
+
url: activeGame.url,
|
|
117
|
+
origin: activeGame.origin,
|
|
118
|
+
port: activeGame.port,
|
|
119
|
+
dir: activeGame.dir,
|
|
120
|
+
source: activeGame.source,
|
|
121
|
+
...(activeGame.version ? { version: activeGame.version } : {}),
|
|
122
|
+
dataDir: activeGame.dataDir,
|
|
123
|
+
...(typeof activeGame.child.pid === 'number' ? { pid: activeGame.child.pid } : {})
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
export async function stopGameStudio(): Promise<boolean> {
|
|
128
|
+
const current = activeGame;
|
|
129
|
+
activeGame = null;
|
|
130
|
+
if (!current) return false;
|
|
131
|
+
try {
|
|
132
|
+
if (isProcessAlive(current.child)) {
|
|
133
|
+
await new Promise<void>((resolve) => {
|
|
134
|
+
let settled = false;
|
|
135
|
+
const done = () => {
|
|
136
|
+
if (settled) return;
|
|
137
|
+
settled = true;
|
|
138
|
+
clearTimeout(forceTimer);
|
|
139
|
+
resolve();
|
|
140
|
+
};
|
|
141
|
+
const forceTimer = setTimeout(() => {
|
|
142
|
+
try {
|
|
143
|
+
if (isProcessAlive(current.child)) current.child.kill('SIGKILL');
|
|
144
|
+
} catch {
|
|
145
|
+
// Ignore already-exited child.
|
|
146
|
+
}
|
|
147
|
+
done();
|
|
148
|
+
}, 2500);
|
|
149
|
+
if (typeof forceTimer.unref === 'function') forceTimer.unref();
|
|
150
|
+
current.child.once('exit', done);
|
|
151
|
+
try {
|
|
152
|
+
current.child.kill('SIGTERM');
|
|
153
|
+
} catch {
|
|
154
|
+
done();
|
|
155
|
+
}
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
} finally {
|
|
159
|
+
await current.coreBridge?.close().catch(() => {});
|
|
160
|
+
}
|
|
161
|
+
return true;
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
async function allocateLoopbackPort(preferredPort: number): Promise<number> {
|
|
165
|
+
const tryBind = (port: number): Promise<number | null> =>
|
|
166
|
+
new Promise((resolve) => {
|
|
167
|
+
const server = net.createServer();
|
|
168
|
+
server.unref();
|
|
169
|
+
server.once('error', () => {
|
|
170
|
+
resolve(null);
|
|
171
|
+
});
|
|
172
|
+
server.listen(port, '127.0.0.1', () => {
|
|
173
|
+
const address = server.address();
|
|
174
|
+
const bound = address && typeof address === 'object' ? address.port : null;
|
|
175
|
+
server.close(() => resolve(bound));
|
|
176
|
+
});
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
if (preferredPort > 0) {
|
|
180
|
+
const bound = await tryBind(preferredPort);
|
|
181
|
+
if (bound !== null) return bound;
|
|
182
|
+
throw new Error(`Port ${preferredPort} is already in use on 127.0.0.1.`);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
for (let candidate = 4173; candidate < 4183; candidate++) {
|
|
186
|
+
const bound = await tryBind(candidate);
|
|
187
|
+
if (bound !== null) return bound;
|
|
188
|
+
}
|
|
189
|
+
const ephemeral = await tryBind(0);
|
|
190
|
+
if (ephemeral !== null) return ephemeral;
|
|
191
|
+
throw new Error('Could not allocate a free loopback port on 127.0.0.1.');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
async function waitForHealth(
|
|
195
|
+
origin: string,
|
|
196
|
+
child: ChildProcess,
|
|
197
|
+
getStderr: () => string,
|
|
198
|
+
timeoutMs: number
|
|
199
|
+
): Promise<void> {
|
|
200
|
+
const deadline = Date.now() + timeoutMs;
|
|
201
|
+
while (Date.now() < deadline) {
|
|
202
|
+
if (!isProcessAlive(child)) {
|
|
203
|
+
const detail = getStderr().trim().split('\n').filter(Boolean).slice(-4).join(' | ');
|
|
204
|
+
throw new Error(detail ? `Causyne Game exited during startup: ${detail}` : 'Causyne Game server exited during startup.');
|
|
205
|
+
}
|
|
206
|
+
try {
|
|
207
|
+
const res = await fetch(`${origin}/api/health`, {
|
|
208
|
+
signal: AbortSignal.timeout(1500)
|
|
209
|
+
});
|
|
210
|
+
if (res.ok) return;
|
|
211
|
+
} catch {
|
|
212
|
+
// Server still booting.
|
|
213
|
+
}
|
|
214
|
+
await new Promise((resolve) => setTimeout(resolve, 250));
|
|
215
|
+
}
|
|
216
|
+
throw new Error(`Causyne Game did not become ready at ${origin}/api/health within ${Math.round(timeoutMs / 1000)}s.`);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
function assertRunnableInstallation(located: LocatedGameStudio | null, explicitDir?: string): LocatedGameStudio {
|
|
220
|
+
if (!located) {
|
|
221
|
+
const hint = explicitDir
|
|
222
|
+
? `No Causyne Game installation found at "${explicitDir}" (missing server.ts or package.json).`
|
|
223
|
+
: `Causyne Game is not installed yet. Run "causyne game install --yes" (or "/game install --yes") to download it from ${defaultGameManifestUrl()}, or set CAUSYNE_GAME_DIR.`;
|
|
224
|
+
throw new Error(hint);
|
|
225
|
+
}
|
|
226
|
+
if (!located.hasNodeModules) {
|
|
227
|
+
throw new Error(`Causyne Game at "${located.dir}" is missing dependencies. Run "npm ci" inside "${located.dir}".`);
|
|
228
|
+
}
|
|
229
|
+
if (!located.hasStudioBuild) {
|
|
230
|
+
throw new Error(`Causyne Game at "${located.dir}" has not been built yet. Run "npm run build:studio" inside "${located.dir}".`);
|
|
231
|
+
}
|
|
232
|
+
return located;
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
export async function launchGameStudio(options: LaunchGameStudioOptions = {}): Promise<LaunchGameStudioResult> {
|
|
236
|
+
const located = assertRunnableInstallation(locateGameStudio(options.dir), options.dir);
|
|
237
|
+
const targetDataDir = options.dataDir ? path.resolve(options.dataDir) : gameDataDir();
|
|
238
|
+
const requestedPort = typeof options.port === 'number' && Number.isInteger(options.port) && options.port >= 0 && options.port <= 65535
|
|
239
|
+
? options.port
|
|
240
|
+
: 0;
|
|
241
|
+
const openFn = options.opener ?? ((url: string) => openBrowserUrl(url, { noOpen: options.openBrowser === false }));
|
|
242
|
+
|
|
243
|
+
let reused = false;
|
|
244
|
+
if (activeGame && isProcessAlive(activeGame.child)) {
|
|
245
|
+
const sameDir = path.resolve(activeGame.dir) === path.resolve(located.dir);
|
|
246
|
+
const samePort = requestedPort === 0 || activeGame.port === requestedPort;
|
|
247
|
+
if (sameDir && samePort) {
|
|
248
|
+
reused = true;
|
|
249
|
+
} else {
|
|
250
|
+
await stopGameStudio();
|
|
251
|
+
}
|
|
252
|
+
} else {
|
|
253
|
+
activeGame = null;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
if (!activeGame) {
|
|
257
|
+
fsSync.mkdirSync(targetDataDir, { recursive: true, mode: 0o700 });
|
|
258
|
+
const port = await allocateLoopbackPort(requestedPort);
|
|
259
|
+
const origin = `http://127.0.0.1:${port}`;
|
|
260
|
+
const url = `${origin}/`;
|
|
261
|
+
const cliBin = causyneCliBinPath();
|
|
262
|
+
const coreBridge = await startCausyneCoreBridge();
|
|
263
|
+
|
|
264
|
+
let child: ChildProcess;
|
|
265
|
+
try {
|
|
266
|
+
child = spawn(process.execPath, ['--experimental-strip-types', located.serverEntry], {
|
|
267
|
+
cwd: located.dir,
|
|
268
|
+
stdio: ['ignore', 'pipe', 'pipe'],
|
|
269
|
+
windowsHide: true,
|
|
270
|
+
env: {
|
|
271
|
+
...process.env,
|
|
272
|
+
HOST: '127.0.0.1',
|
|
273
|
+
MOTION_BIND_HOST: '127.0.0.1',
|
|
274
|
+
MOTION_SHARED_EDITOR: '0',
|
|
275
|
+
PORT: String(port),
|
|
276
|
+
CAUSYNE_GAME_DATA_DIR: targetDataDir,
|
|
277
|
+
MOTION_DATA_DIR: targetDataDir,
|
|
278
|
+
CAUSYNE_BIN: cliBin,
|
|
279
|
+
CAUSYNE_CORE_URL: coreBridge.url,
|
|
280
|
+
CAUSYNE_CORE_TOKEN: coreBridge.token,
|
|
281
|
+
MOTION_PARENT_PID: String(process.pid),
|
|
282
|
+
MOTION_HEADLESS: '1'
|
|
283
|
+
}
|
|
284
|
+
});
|
|
285
|
+
} catch (spawnErr) {
|
|
286
|
+
await coreBridge.close().catch(() => {});
|
|
287
|
+
throw spawnErr;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
let stderrBuf = '';
|
|
291
|
+
child.stderr?.setEncoding('utf8');
|
|
292
|
+
child.stderr?.on('data', (chunk: string) => {
|
|
293
|
+
stderrBuf = (stderrBuf + chunk).slice(-4096);
|
|
294
|
+
});
|
|
295
|
+
child.stdout?.resume();
|
|
296
|
+
|
|
297
|
+
ensureExitHook();
|
|
298
|
+
const record: ActiveGameProcess = {
|
|
299
|
+
child,
|
|
300
|
+
coreBridge,
|
|
301
|
+
url,
|
|
302
|
+
origin,
|
|
303
|
+
port,
|
|
304
|
+
dir: located.dir,
|
|
305
|
+
source: located.source,
|
|
306
|
+
...(located.version ? { version: located.version } : {}),
|
|
307
|
+
dataDir: targetDataDir
|
|
308
|
+
};
|
|
309
|
+
activeGame = record;
|
|
310
|
+
child.once('exit', () => {
|
|
311
|
+
if (activeGame === record) {
|
|
312
|
+
activeGame = null;
|
|
313
|
+
void record.coreBridge?.close().catch(() => {});
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
|
|
317
|
+
try {
|
|
318
|
+
await waitForHealth(origin, child, () => stderrBuf, options.startupTimeoutMs ?? 25_000);
|
|
319
|
+
} catch (error) {
|
|
320
|
+
await stopGameStudio();
|
|
321
|
+
throw error;
|
|
322
|
+
}
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
const running = activeGame!;
|
|
326
|
+
const openResult = options.openBrowser === false
|
|
327
|
+
? { opened: false as const, reason: 'disabled' as const }
|
|
328
|
+
: await Promise.resolve(openFn(running.url));
|
|
329
|
+
|
|
330
|
+
let copiedToClipboard = false;
|
|
331
|
+
if (options.copyToClipboard !== false && process.env.CI !== 'true' && process.env.CI !== '1' && process.env.NODE_ENV !== 'test') {
|
|
332
|
+
try {
|
|
333
|
+
copiedToClipboard = Boolean(writeClipboardSync(running.url));
|
|
334
|
+
} catch {
|
|
335
|
+
copiedToClipboard = false;
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return {
|
|
340
|
+
url: running.url,
|
|
341
|
+
origin: running.origin,
|
|
342
|
+
port: running.port,
|
|
343
|
+
dir: running.dir,
|
|
344
|
+
source: running.source,
|
|
345
|
+
...(running.version ? { version: running.version } : {}),
|
|
346
|
+
dataDir: running.dataDir,
|
|
347
|
+
reused,
|
|
348
|
+
openedBrowser: openResult.opened,
|
|
349
|
+
browserHint: openResult.hint,
|
|
350
|
+
copiedToClipboard
|
|
351
|
+
};
|
|
352
|
+
}
|