@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,233 @@
|
|
|
1
|
+
import { dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, state, updateComposerMetrics } from './state.js';
|
|
3
|
+
import { isHtmlOrSvgPath } from './validators.js';
|
|
4
|
+
import { renderMarkdownInto } from './markdown.js';
|
|
5
|
+
import { openStudio, setStudioTabDynamicTitle } from './studioShell.js';
|
|
6
|
+
let callbacks = {
|
|
7
|
+
attachWorkspaceFile: () => { },
|
|
8
|
+
saveCurrentSessionWorkspace: () => { },
|
|
9
|
+
refreshDiffIfOpen: () => { }
|
|
10
|
+
};
|
|
11
|
+
export function configureFilesAndCanvasCallbacks(cbs) {
|
|
12
|
+
callbacks = cbs;
|
|
13
|
+
}
|
|
14
|
+
export function syncArtifactVersionSelect(relPath) {
|
|
15
|
+
const list = state.artifactVersions.get(relPath || '') || [];
|
|
16
|
+
dom.artifactVersionSelect.textContent = '';
|
|
17
|
+
if (list.length <= 1) {
|
|
18
|
+
dom.artifactVersionSelect.style.display = 'none';
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
dom.artifactVersionSelect.style.display = 'inline-block';
|
|
22
|
+
for (const item of list) {
|
|
23
|
+
const opt = document.createElement('option');
|
|
24
|
+
opt.value = String(item.version);
|
|
25
|
+
opt.textContent = `v${item.version}`;
|
|
26
|
+
dom.artifactVersionSelect.appendChild(opt);
|
|
27
|
+
}
|
|
28
|
+
const latest = list[list.length - 1];
|
|
29
|
+
if (latest)
|
|
30
|
+
dom.artifactVersionSelect.value = String(latest.version);
|
|
31
|
+
}
|
|
32
|
+
export function recordArtifactVersion(relPath, content) {
|
|
33
|
+
const key = relPath || 'draft.idoc.md';
|
|
34
|
+
const list = state.artifactVersions.get(key) || [];
|
|
35
|
+
const last = list[list.length - 1];
|
|
36
|
+
if (!last || last.content !== content) {
|
|
37
|
+
list.push({ version: list.length + 1, content, at: Date.now() });
|
|
38
|
+
state.artifactVersions.set(key, list);
|
|
39
|
+
}
|
|
40
|
+
syncArtifactVersionSelect(key);
|
|
41
|
+
return list.length;
|
|
42
|
+
}
|
|
43
|
+
export function renderCanvasPreview() {
|
|
44
|
+
const pathVal = (dom.filePathInput.value || '').trim();
|
|
45
|
+
const content = dom.fileContentInput.value || '';
|
|
46
|
+
if (isHtmlOrSvgPath(pathVal)) {
|
|
47
|
+
dom.canvasPreviewEl.style.display = 'none';
|
|
48
|
+
dom.canvasHtmlFrame.style.display = 'block';
|
|
49
|
+
const trimmed = content.trim();
|
|
50
|
+
if (trimmed.startsWith('<svg')) {
|
|
51
|
+
dom.canvasHtmlFrame.srcdoc = `<!DOCTYPE html><html><body style="margin:0;display:grid;place-items:center;min-height:100vh;background:#141714">${content}</body></html>`;
|
|
52
|
+
return;
|
|
53
|
+
}
|
|
54
|
+
// For saved multi-file workspace HTML documents, pack relative CSS/JS assets cleanly
|
|
55
|
+
if (!state.canvasDirty && pathVal && !pathVal.startsWith('/') && /\.html?$/i.test(pathVal)) {
|
|
56
|
+
dom.canvasHtmlFrame.srcdoc = content;
|
|
57
|
+
void fetch(apiUrl(`/api/file/pack-html?path=${encodeURIComponent(pathVal)}`), { headers: authHeaders() })
|
|
58
|
+
.then((res) => (res.ok ? res.json() : null))
|
|
59
|
+
.then((data) => {
|
|
60
|
+
if (data && typeof data.html === 'string' && state.canvasPreviewMode && (dom.filePathInput.value || '').trim() === pathVal) {
|
|
61
|
+
dom.canvasHtmlFrame.srcdoc = data.html;
|
|
62
|
+
if (typeof data.packedAssets === 'number' && data.packedAssets > 0) {
|
|
63
|
+
dom.fileStatusEl.textContent = `Preview · ${pathVal} (${data.packedAssets} local asset${data.packedAssets === 1 ? '' : 's'} bundled)`;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
})
|
|
67
|
+
.catch(() => { });
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
dom.canvasHtmlFrame.srcdoc = content;
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
dom.canvasHtmlFrame.style.display = 'none';
|
|
74
|
+
dom.canvasPreviewEl.style.display = 'block';
|
|
75
|
+
renderMarkdownInto(dom.canvasPreviewEl, content, false);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
export function setCanvasPreviewMode(previewOn) {
|
|
79
|
+
state.canvasPreviewMode = previewOn;
|
|
80
|
+
dom.canvasCodeBtn.classList.toggle('active', !previewOn);
|
|
81
|
+
dom.canvasModeBtn.classList.toggle('active', previewOn);
|
|
82
|
+
dom.fileContentInput.style.display = previewOn ? 'none' : 'block';
|
|
83
|
+
if (previewOn) {
|
|
84
|
+
renderCanvasPreview();
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
dom.canvasPreviewEl.style.display = 'none';
|
|
88
|
+
dom.canvasHtmlFrame.style.display = 'none';
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
export function openInCanvas(suggestedPath, content, preferPreview) {
|
|
92
|
+
openStudio('canvas');
|
|
93
|
+
const targetPath = suggestedPath || dom.filePathInput.value || 'draft.idoc.md';
|
|
94
|
+
dom.filePathInput.value = targetPath;
|
|
95
|
+
dom.fileContentInput.value = content || '';
|
|
96
|
+
const shortName = targetPath.split('/').pop() || targetPath;
|
|
97
|
+
setStudioTabDynamicTitle('canvas', shortName);
|
|
98
|
+
const ver = recordArtifactVersion(targetPath, content || '');
|
|
99
|
+
dom.fileStatusEl.textContent = `Canvas · ${targetPath} (v${ver})`;
|
|
100
|
+
setCanvasPreviewMode(Boolean(preferPreview));
|
|
101
|
+
}
|
|
102
|
+
export async function loadFiles(dir, query) {
|
|
103
|
+
const params = new URLSearchParams();
|
|
104
|
+
if (dir)
|
|
105
|
+
params.set('dir', dir);
|
|
106
|
+
if (query)
|
|
107
|
+
params.set('q', query);
|
|
108
|
+
const qs = params.toString() ? (`?${params.toString()}`) : '';
|
|
109
|
+
const res = await fetch(apiUrl(`/api/files${qs}`), { headers: authHeaders() });
|
|
110
|
+
if (!res.ok)
|
|
111
|
+
return;
|
|
112
|
+
const data = await res.json();
|
|
113
|
+
state.currentDir = data.dir || '';
|
|
114
|
+
state.parentDir = data.parentDir || '';
|
|
115
|
+
dom.fileUpBtn.disabled = !state.currentDir;
|
|
116
|
+
dom.fileListEl.textContent = '';
|
|
117
|
+
const entries = data.entries || [];
|
|
118
|
+
if (entries.length === 0) {
|
|
119
|
+
const empty = document.createElement('div');
|
|
120
|
+
empty.className = 'session-empty';
|
|
121
|
+
empty.textContent = 'No files in this directory.';
|
|
122
|
+
dom.fileListEl.appendChild(empty);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
for (const item of entries) {
|
|
126
|
+
const row = document.createElement('div');
|
|
127
|
+
row.className = 'file-row';
|
|
128
|
+
const openBtn = document.createElement('button');
|
|
129
|
+
openBtn.type = 'button';
|
|
130
|
+
openBtn.className = 'file-open-btn';
|
|
131
|
+
openBtn.textContent = (item.type === 'dir' ? '[dir] ' : '') + item.path;
|
|
132
|
+
openBtn.addEventListener('click', () => {
|
|
133
|
+
if (item.type === 'dir')
|
|
134
|
+
void loadFiles(item.path, '');
|
|
135
|
+
else
|
|
136
|
+
void openFile(item.path);
|
|
137
|
+
});
|
|
138
|
+
row.appendChild(openBtn);
|
|
139
|
+
if (item.type === 'file') {
|
|
140
|
+
const atBtn = document.createElement('button');
|
|
141
|
+
atBtn.type = 'button';
|
|
142
|
+
atBtn.className = 'file-attach-btn';
|
|
143
|
+
atBtn.textContent = '+ @';
|
|
144
|
+
atBtn.title = 'Attach to prompt';
|
|
145
|
+
atBtn.addEventListener('click', () => callbacks.attachWorkspaceFile(item.path));
|
|
146
|
+
row.appendChild(atBtn);
|
|
147
|
+
}
|
|
148
|
+
dom.fileListEl.appendChild(row);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
export async function openFile(relPath) {
|
|
152
|
+
const seq = state.sessionSwitchSeq;
|
|
153
|
+
const res = await fetch(apiUrl(`/api/file?path=${encodeURIComponent(relPath)}`), { headers: authHeaders() });
|
|
154
|
+
if (seq !== state.sessionSwitchSeq)
|
|
155
|
+
return;
|
|
156
|
+
const data = await res.json();
|
|
157
|
+
if (!res.ok) {
|
|
158
|
+
dom.fileStatusEl.textContent = data.error || 'Read failed';
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
state.canvasDirty = false;
|
|
162
|
+
const targetPath = data.path || relPath;
|
|
163
|
+
openInCanvas(targetPath, data.content || '', isHtmlOrSvgPath(targetPath));
|
|
164
|
+
dom.fileStatusEl.textContent = `Loaded ${targetPath} (${data.size || 0} B)`;
|
|
165
|
+
callbacks.saveCurrentSessionWorkspace();
|
|
166
|
+
}
|
|
167
|
+
export async function saveFile() {
|
|
168
|
+
const relPath = (dom.filePathInput.value || '').trim();
|
|
169
|
+
if (!relPath) {
|
|
170
|
+
dom.fileStatusEl.textContent = 'Enter a workspace relative path first.';
|
|
171
|
+
return;
|
|
172
|
+
}
|
|
173
|
+
const content = dom.fileContentInput.value || '';
|
|
174
|
+
const res = await fetch(apiUrl('/api/file'), {
|
|
175
|
+
method: 'POST',
|
|
176
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
177
|
+
body: JSON.stringify({ path: relPath, content, sessionId: state.sessionId || undefined })
|
|
178
|
+
});
|
|
179
|
+
const data = await res.json();
|
|
180
|
+
if (!res.ok) {
|
|
181
|
+
dom.fileStatusEl.textContent = data.error || 'Write failed';
|
|
182
|
+
return;
|
|
183
|
+
}
|
|
184
|
+
state.canvasDirty = false;
|
|
185
|
+
const savedPath = data.path || relPath;
|
|
186
|
+
const ver = recordArtifactVersion(savedPath, content);
|
|
187
|
+
dom.fileStatusEl.textContent = `Saved ${savedPath} v${ver} (${data.bytes || 0} B)`;
|
|
188
|
+
callbacks.saveCurrentSessionWorkspace();
|
|
189
|
+
void loadFiles(state.currentDir, dom.fileFilterEl.value || '');
|
|
190
|
+
callbacks.refreshDiffIfOpen();
|
|
191
|
+
}
|
|
192
|
+
export function bindFilesAndCanvasEvents() {
|
|
193
|
+
dom.fileFilterEl.addEventListener('input', () => { void loadFiles(state.currentDir, dom.fileFilterEl.value || ''); });
|
|
194
|
+
dom.fileUpBtn.addEventListener('click', () => { void loadFiles(state.parentDir, ''); });
|
|
195
|
+
dom.refreshFilesBtn.addEventListener('click', () => { void loadFiles(state.currentDir, dom.fileFilterEl.value || ''); });
|
|
196
|
+
dom.saveFileBtn.addEventListener('click', () => { void saveFile(); });
|
|
197
|
+
dom.canvasCodeBtn.addEventListener('click', () => setCanvasPreviewMode(false));
|
|
198
|
+
dom.canvasModeBtn.addEventListener('click', () => setCanvasPreviewMode(true));
|
|
199
|
+
dom.fileContentInput.addEventListener('input', () => {
|
|
200
|
+
state.canvasDirty = true;
|
|
201
|
+
const p = (dom.filePathInput.value || '').trim();
|
|
202
|
+
dom.fileStatusEl.textContent = (p ? (`${p} · `) : '') + 'Unsaved';
|
|
203
|
+
});
|
|
204
|
+
dom.artifactVersionSelect.addEventListener('change', () => {
|
|
205
|
+
const list = state.artifactVersions.get((dom.filePathInput.value || '').trim()) || [];
|
|
206
|
+
const chosen = list.find((item) => String(item.version) === dom.artifactVersionSelect.value);
|
|
207
|
+
if (chosen) {
|
|
208
|
+
dom.fileContentInput.value = chosen.content;
|
|
209
|
+
if (state.canvasPreviewMode)
|
|
210
|
+
renderCanvasPreview();
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
dom.canvasDownloadBtn.addEventListener('click', () => {
|
|
214
|
+
const name = ((dom.filePathInput.value || 'causyne-artifact.md').split('/').pop()) || 'artifact.md';
|
|
215
|
+
const blob = new Blob([dom.fileContentInput.value || ''], { type: 'text/plain;charset=utf-8' });
|
|
216
|
+
const url = URL.createObjectURL(blob);
|
|
217
|
+
const a = document.createElement('a');
|
|
218
|
+
a.href = url;
|
|
219
|
+
a.download = name;
|
|
220
|
+
a.click();
|
|
221
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
222
|
+
});
|
|
223
|
+
dom.canvasRefineBtn.addEventListener('click', () => {
|
|
224
|
+
const p = (dom.filePathInput.value || '').trim();
|
|
225
|
+
if (p)
|
|
226
|
+
callbacks.attachWorkspaceFile(p);
|
|
227
|
+
dom.promptInput.value = p
|
|
228
|
+
? `Review and improve ${p} in this workspace.`
|
|
229
|
+
: 'Improve the open Canvas draft.';
|
|
230
|
+
updateComposerMetrics();
|
|
231
|
+
dom.promptInput.focus();
|
|
232
|
+
});
|
|
233
|
+
}
|
|
@@ -0,0 +1,240 @@
|
|
|
1
|
+
import { dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, state, updateComposerMetrics } from './state.js';
|
|
3
|
+
import { isHtmlOrSvgPath } from './validators.js';
|
|
4
|
+
import { renderMarkdownInto } from './markdown.js';
|
|
5
|
+
import { openStudio, setStudioTabDynamicTitle } from './studioShell.js';
|
|
6
|
+
import type { WorkspaceEntry } from './types.js';
|
|
7
|
+
|
|
8
|
+
export interface FilesAndCanvasCallbacks {
|
|
9
|
+
attachWorkspaceFile(relPath: string): void;
|
|
10
|
+
saveCurrentSessionWorkspace(): void;
|
|
11
|
+
refreshDiffIfOpen(): void;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
let callbacks: FilesAndCanvasCallbacks = {
|
|
15
|
+
attachWorkspaceFile: () => {},
|
|
16
|
+
saveCurrentSessionWorkspace: () => {},
|
|
17
|
+
refreshDiffIfOpen: () => {}
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export function configureFilesAndCanvasCallbacks(cbs: FilesAndCanvasCallbacks): void {
|
|
21
|
+
callbacks = cbs;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function syncArtifactVersionSelect(relPath: string): void {
|
|
25
|
+
const list = state.artifactVersions.get(relPath || '') || [];
|
|
26
|
+
dom.artifactVersionSelect.textContent = '';
|
|
27
|
+
if (list.length <= 1) {
|
|
28
|
+
dom.artifactVersionSelect.style.display = 'none';
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
dom.artifactVersionSelect.style.display = 'inline-block';
|
|
32
|
+
for (const item of list) {
|
|
33
|
+
const opt = document.createElement('option');
|
|
34
|
+
opt.value = String(item.version);
|
|
35
|
+
opt.textContent = `v${item.version}`;
|
|
36
|
+
dom.artifactVersionSelect.appendChild(opt);
|
|
37
|
+
}
|
|
38
|
+
const latest = list[list.length - 1];
|
|
39
|
+
if (latest) dom.artifactVersionSelect.value = String(latest.version);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export function recordArtifactVersion(relPath: string, content: string): number {
|
|
43
|
+
const key = relPath || 'draft.idoc.md';
|
|
44
|
+
const list = state.artifactVersions.get(key) || [];
|
|
45
|
+
const last = list[list.length - 1];
|
|
46
|
+
if (!last || last.content !== content) {
|
|
47
|
+
list.push({ version: list.length + 1, content, at: Date.now() });
|
|
48
|
+
state.artifactVersions.set(key, list);
|
|
49
|
+
}
|
|
50
|
+
syncArtifactVersionSelect(key);
|
|
51
|
+
return list.length;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function renderCanvasPreview(): void {
|
|
55
|
+
const pathVal = (dom.filePathInput.value || '').trim();
|
|
56
|
+
const content = dom.fileContentInput.value || '';
|
|
57
|
+
if (isHtmlOrSvgPath(pathVal)) {
|
|
58
|
+
dom.canvasPreviewEl.style.display = 'none';
|
|
59
|
+
dom.canvasHtmlFrame.style.display = 'block';
|
|
60
|
+
const trimmed = content.trim();
|
|
61
|
+
if (trimmed.startsWith('<svg')) {
|
|
62
|
+
dom.canvasHtmlFrame.srcdoc = `<!DOCTYPE html><html><body style="margin:0;display:grid;place-items:center;min-height:100vh;background:#141714">${content}</body></html>`;
|
|
63
|
+
return;
|
|
64
|
+
}
|
|
65
|
+
// For saved multi-file workspace HTML documents, pack relative CSS/JS assets cleanly
|
|
66
|
+
if (!state.canvasDirty && pathVal && !pathVal.startsWith('/') && /\.html?$/i.test(pathVal)) {
|
|
67
|
+
dom.canvasHtmlFrame.srcdoc = content;
|
|
68
|
+
void fetch(apiUrl(`/api/file/pack-html?path=${encodeURIComponent(pathVal)}`), { headers: authHeaders() })
|
|
69
|
+
.then((res) => (res.ok ? res.json() : null))
|
|
70
|
+
.then((data: { html?: string; packedAssets?: number } | null) => {
|
|
71
|
+
if (data && typeof data.html === 'string' && state.canvasPreviewMode && (dom.filePathInput.value || '').trim() === pathVal) {
|
|
72
|
+
dom.canvasHtmlFrame.srcdoc = data.html;
|
|
73
|
+
if (typeof data.packedAssets === 'number' && data.packedAssets > 0) {
|
|
74
|
+
dom.fileStatusEl.textContent = `Preview · ${pathVal} (${data.packedAssets} local asset${data.packedAssets === 1 ? '' : 's'} bundled)`;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
})
|
|
78
|
+
.catch(() => { /* keep direct srcdoc fallback */ });
|
|
79
|
+
return;
|
|
80
|
+
}
|
|
81
|
+
dom.canvasHtmlFrame.srcdoc = content;
|
|
82
|
+
} else {
|
|
83
|
+
dom.canvasHtmlFrame.style.display = 'none';
|
|
84
|
+
dom.canvasPreviewEl.style.display = 'block';
|
|
85
|
+
renderMarkdownInto(dom.canvasPreviewEl, content, false);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function setCanvasPreviewMode(previewOn: boolean): void {
|
|
90
|
+
state.canvasPreviewMode = previewOn;
|
|
91
|
+
dom.canvasCodeBtn.classList.toggle('active', !previewOn);
|
|
92
|
+
dom.canvasModeBtn.classList.toggle('active', previewOn);
|
|
93
|
+
dom.fileContentInput.style.display = previewOn ? 'none' : 'block';
|
|
94
|
+
if (previewOn) {
|
|
95
|
+
renderCanvasPreview();
|
|
96
|
+
} else {
|
|
97
|
+
dom.canvasPreviewEl.style.display = 'none';
|
|
98
|
+
dom.canvasHtmlFrame.style.display = 'none';
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export function openInCanvas(suggestedPath: string, content: string, preferPreview: boolean): void {
|
|
103
|
+
openStudio('canvas');
|
|
104
|
+
const targetPath = suggestedPath || dom.filePathInput.value || 'draft.idoc.md';
|
|
105
|
+
dom.filePathInput.value = targetPath;
|
|
106
|
+
dom.fileContentInput.value = content || '';
|
|
107
|
+
const shortName = targetPath.split('/').pop() || targetPath;
|
|
108
|
+
setStudioTabDynamicTitle('canvas', shortName);
|
|
109
|
+
const ver = recordArtifactVersion(targetPath, content || '');
|
|
110
|
+
dom.fileStatusEl.textContent = `Canvas · ${targetPath} (v${ver})`;
|
|
111
|
+
setCanvasPreviewMode(Boolean(preferPreview));
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
export async function loadFiles(dir: string, query: string): Promise<void> {
|
|
115
|
+
const params = new URLSearchParams();
|
|
116
|
+
if (dir) params.set('dir', dir);
|
|
117
|
+
if (query) params.set('q', query);
|
|
118
|
+
const qs = params.toString() ? (`?${params.toString()}`) : '';
|
|
119
|
+
const res = await fetch(apiUrl(`/api/files${qs}`), { headers: authHeaders() });
|
|
120
|
+
if (!res.ok) return;
|
|
121
|
+
const data = await res.json() as { dir?: string; parentDir?: string; entries?: WorkspaceEntry[] };
|
|
122
|
+
state.currentDir = data.dir || '';
|
|
123
|
+
state.parentDir = data.parentDir || '';
|
|
124
|
+
dom.fileUpBtn.disabled = !state.currentDir;
|
|
125
|
+
dom.fileListEl.textContent = '';
|
|
126
|
+
const entries = data.entries || [];
|
|
127
|
+
if (entries.length === 0) {
|
|
128
|
+
const empty = document.createElement('div');
|
|
129
|
+
empty.className = 'session-empty';
|
|
130
|
+
empty.textContent = 'No files in this directory.';
|
|
131
|
+
dom.fileListEl.appendChild(empty);
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
for (const item of entries) {
|
|
135
|
+
const row = document.createElement('div');
|
|
136
|
+
row.className = 'file-row';
|
|
137
|
+
const openBtn = document.createElement('button');
|
|
138
|
+
openBtn.type = 'button';
|
|
139
|
+
openBtn.className = 'file-open-btn';
|
|
140
|
+
openBtn.textContent = (item.type === 'dir' ? '[dir] ' : '') + item.path;
|
|
141
|
+
openBtn.addEventListener('click', () => {
|
|
142
|
+
if (item.type === 'dir') void loadFiles(item.path, '');
|
|
143
|
+
else void openFile(item.path);
|
|
144
|
+
});
|
|
145
|
+
row.appendChild(openBtn);
|
|
146
|
+
if (item.type === 'file') {
|
|
147
|
+
const atBtn = document.createElement('button');
|
|
148
|
+
atBtn.type = 'button';
|
|
149
|
+
atBtn.className = 'file-attach-btn';
|
|
150
|
+
atBtn.textContent = '+ @';
|
|
151
|
+
atBtn.title = 'Attach to prompt';
|
|
152
|
+
atBtn.addEventListener('click', () => callbacks.attachWorkspaceFile(item.path));
|
|
153
|
+
row.appendChild(atBtn);
|
|
154
|
+
}
|
|
155
|
+
dom.fileListEl.appendChild(row);
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
export async function openFile(relPath: string): Promise<void> {
|
|
160
|
+
const seq = state.sessionSwitchSeq;
|
|
161
|
+
const res = await fetch(apiUrl(`/api/file?path=${encodeURIComponent(relPath)}`), { headers: authHeaders() });
|
|
162
|
+
if (seq !== state.sessionSwitchSeq) return;
|
|
163
|
+
const data = await res.json() as { path?: string; content?: string; size?: number; error?: string };
|
|
164
|
+
if (!res.ok) {
|
|
165
|
+
dom.fileStatusEl.textContent = data.error || 'Read failed';
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
state.canvasDirty = false;
|
|
169
|
+
const targetPath = data.path || relPath;
|
|
170
|
+
openInCanvas(targetPath, data.content || '', isHtmlOrSvgPath(targetPath));
|
|
171
|
+
dom.fileStatusEl.textContent = `Loaded ${targetPath} (${data.size || 0} B)`;
|
|
172
|
+
callbacks.saveCurrentSessionWorkspace();
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export async function saveFile(): Promise<void> {
|
|
176
|
+
const relPath = (dom.filePathInput.value || '').trim();
|
|
177
|
+
if (!relPath) {
|
|
178
|
+
dom.fileStatusEl.textContent = 'Enter a workspace relative path first.';
|
|
179
|
+
return;
|
|
180
|
+
}
|
|
181
|
+
const content = dom.fileContentInput.value || '';
|
|
182
|
+
const res = await fetch(apiUrl('/api/file'), {
|
|
183
|
+
method: 'POST',
|
|
184
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
185
|
+
body: JSON.stringify({ path: relPath, content, sessionId: state.sessionId || undefined })
|
|
186
|
+
});
|
|
187
|
+
const data = await res.json() as { path?: string; bytes?: number; error?: string };
|
|
188
|
+
if (!res.ok) {
|
|
189
|
+
dom.fileStatusEl.textContent = data.error || 'Write failed';
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
state.canvasDirty = false;
|
|
193
|
+
const savedPath = data.path || relPath;
|
|
194
|
+
const ver = recordArtifactVersion(savedPath, content);
|
|
195
|
+
dom.fileStatusEl.textContent = `Saved ${savedPath} v${ver} (${data.bytes || 0} B)`;
|
|
196
|
+
callbacks.saveCurrentSessionWorkspace();
|
|
197
|
+
void loadFiles(state.currentDir, dom.fileFilterEl.value || '');
|
|
198
|
+
callbacks.refreshDiffIfOpen();
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
export function bindFilesAndCanvasEvents(): void {
|
|
202
|
+
dom.fileFilterEl.addEventListener('input', () => { void loadFiles(state.currentDir, dom.fileFilterEl.value || ''); });
|
|
203
|
+
dom.fileUpBtn.addEventListener('click', () => { void loadFiles(state.parentDir, ''); });
|
|
204
|
+
dom.refreshFilesBtn.addEventListener('click', () => { void loadFiles(state.currentDir, dom.fileFilterEl.value || ''); });
|
|
205
|
+
dom.saveFileBtn.addEventListener('click', () => { void saveFile(); });
|
|
206
|
+
dom.canvasCodeBtn.addEventListener('click', () => setCanvasPreviewMode(false));
|
|
207
|
+
dom.canvasModeBtn.addEventListener('click', () => setCanvasPreviewMode(true));
|
|
208
|
+
dom.fileContentInput.addEventListener('input', () => {
|
|
209
|
+
state.canvasDirty = true;
|
|
210
|
+
const p = (dom.filePathInput.value || '').trim();
|
|
211
|
+
dom.fileStatusEl.textContent = (p ? (`${p} · `) : '') + 'Unsaved';
|
|
212
|
+
});
|
|
213
|
+
dom.artifactVersionSelect.addEventListener('change', () => {
|
|
214
|
+
const list = state.artifactVersions.get((dom.filePathInput.value || '').trim()) || [];
|
|
215
|
+
const chosen = list.find((item) => String(item.version) === dom.artifactVersionSelect.value);
|
|
216
|
+
if (chosen) {
|
|
217
|
+
dom.fileContentInput.value = chosen.content;
|
|
218
|
+
if (state.canvasPreviewMode) renderCanvasPreview();
|
|
219
|
+
}
|
|
220
|
+
});
|
|
221
|
+
dom.canvasDownloadBtn.addEventListener('click', () => {
|
|
222
|
+
const name = ((dom.filePathInput.value || 'causyne-artifact.md').split('/').pop()) || 'artifact.md';
|
|
223
|
+
const blob = new Blob([dom.fileContentInput.value || ''], { type: 'text/plain;charset=utf-8' });
|
|
224
|
+
const url = URL.createObjectURL(blob);
|
|
225
|
+
const a = document.createElement('a');
|
|
226
|
+
a.href = url;
|
|
227
|
+
a.download = name;
|
|
228
|
+
a.click();
|
|
229
|
+
setTimeout(() => URL.revokeObjectURL(url), 1000);
|
|
230
|
+
});
|
|
231
|
+
dom.canvasRefineBtn.addEventListener('click', () => {
|
|
232
|
+
const p = (dom.filePathInput.value || '').trim();
|
|
233
|
+
if (p) callbacks.attachWorkspaceFile(p);
|
|
234
|
+
dom.promptInput.value = p
|
|
235
|
+
? `Review and improve ${p} in this workspace.`
|
|
236
|
+
: 'Improve the open Canvas draft.';
|
|
237
|
+
updateComposerMetrics();
|
|
238
|
+
dom.promptInput.focus();
|
|
239
|
+
});
|
|
240
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { announceAlert, announceStatus, dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, setLiveness, state } from './state.js';
|
|
3
|
+
export async function loadGameStatus() {
|
|
4
|
+
if (!dom.navGameBtn || !dom.navGameBadge)
|
|
5
|
+
return;
|
|
6
|
+
try {
|
|
7
|
+
const r = await fetch(apiUrl('/api/game/status'), { headers: authHeaders() });
|
|
8
|
+
if (!r.ok)
|
|
9
|
+
return;
|
|
10
|
+
const d = await r.json();
|
|
11
|
+
state.gameStatus = d;
|
|
12
|
+
if (d && d.running) {
|
|
13
|
+
dom.navGameBadge.textContent = 'Live';
|
|
14
|
+
dom.navGameBtn.classList.add('active');
|
|
15
|
+
dom.navGameBtn.title = `Open Causyne Game (${d.url || 'running'})`;
|
|
16
|
+
}
|
|
17
|
+
else if (d && d.installed && d.ready) {
|
|
18
|
+
dom.navGameBadge.textContent = '';
|
|
19
|
+
dom.navGameBtn.classList.remove('active');
|
|
20
|
+
dom.navGameBtn.title = 'Launch Causyne Game Studio';
|
|
21
|
+
}
|
|
22
|
+
else {
|
|
23
|
+
dom.navGameBadge.textContent = 'Setup';
|
|
24
|
+
dom.navGameBtn.classList.remove('active');
|
|
25
|
+
dom.navGameBtn.title = 'Install or launch Causyne Game Studio';
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
catch { /* ignore */ }
|
|
29
|
+
}
|
|
30
|
+
export async function launchCausyneGameFromUi() {
|
|
31
|
+
if (!dom.navGameBtn || !dom.navGameBadge)
|
|
32
|
+
return;
|
|
33
|
+
if (state.gameLaunching)
|
|
34
|
+
return;
|
|
35
|
+
state.gameLaunching = true;
|
|
36
|
+
const prevLabel = dom.navGameBadge.textContent;
|
|
37
|
+
dom.navGameBadge.textContent = 'Opening…';
|
|
38
|
+
try {
|
|
39
|
+
const r = await fetch(apiUrl('/api/game/launch'), {
|
|
40
|
+
method: 'POST',
|
|
41
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
42
|
+
body: JSON.stringify({ openBrowser: true })
|
|
43
|
+
});
|
|
44
|
+
const d = await r.json().catch(() => ({}));
|
|
45
|
+
if (r.ok && d && d.ok && d.url) {
|
|
46
|
+
state.gameStatus = d;
|
|
47
|
+
dom.navGameBadge.textContent = 'Live';
|
|
48
|
+
dom.navGameBtn.classList.add('active');
|
|
49
|
+
dom.navGameBtn.title = `Open Causyne Game (${d.url})`;
|
|
50
|
+
announceStatus(`Causyne Game running at ${d.url}`);
|
|
51
|
+
if (!d.openedBrowser) {
|
|
52
|
+
try {
|
|
53
|
+
window.open(d.url, '_blank', 'noopener,noreferrer');
|
|
54
|
+
}
|
|
55
|
+
catch { /* ignore */ }
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
dom.navGameBadge.textContent = prevLabel || 'Setup';
|
|
60
|
+
const errText = d?.error || 'Could not launch Causyne Game. Run causyne game install --yes.';
|
|
61
|
+
announceAlert(errText);
|
|
62
|
+
setLiveness(false, errText);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch (err) {
|
|
66
|
+
dom.navGameBadge.textContent = prevLabel || '';
|
|
67
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
68
|
+
announceAlert(`Causyne Game launch failed: ${msg}`);
|
|
69
|
+
}
|
|
70
|
+
finally {
|
|
71
|
+
state.gameLaunching = false;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
export function bindGameLauncherEvents() {
|
|
75
|
+
if (dom.navGameBtn) {
|
|
76
|
+
dom.navGameBtn.addEventListener('click', () => { void launchCausyneGameFromUi(); });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { announceAlert, announceStatus, dom } from './dom.js';
|
|
2
|
+
import { apiUrl, authHeaders, setLiveness, state } from './state.js';
|
|
3
|
+
|
|
4
|
+
interface GameStatusPayload {
|
|
5
|
+
ok?: boolean;
|
|
6
|
+
running?: boolean;
|
|
7
|
+
installed?: boolean;
|
|
8
|
+
ready?: boolean;
|
|
9
|
+
url?: string;
|
|
10
|
+
openedBrowser?: boolean;
|
|
11
|
+
error?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export async function loadGameStatus(): Promise<void> {
|
|
15
|
+
if (!dom.navGameBtn || !dom.navGameBadge) return;
|
|
16
|
+
try {
|
|
17
|
+
const r = await fetch(apiUrl('/api/game/status'), { headers: authHeaders() });
|
|
18
|
+
if (!r.ok) return;
|
|
19
|
+
const d = await r.json() as GameStatusPayload;
|
|
20
|
+
state.gameStatus = d as Record<string, unknown>;
|
|
21
|
+
if (d && d.running) {
|
|
22
|
+
dom.navGameBadge.textContent = 'Live';
|
|
23
|
+
dom.navGameBtn.classList.add('active');
|
|
24
|
+
dom.navGameBtn.title = `Open Causyne Game (${d.url || 'running'})`;
|
|
25
|
+
} else if (d && d.installed && d.ready) {
|
|
26
|
+
dom.navGameBadge.textContent = '';
|
|
27
|
+
dom.navGameBtn.classList.remove('active');
|
|
28
|
+
dom.navGameBtn.title = 'Launch Causyne Game Studio';
|
|
29
|
+
} else {
|
|
30
|
+
dom.navGameBadge.textContent = 'Setup';
|
|
31
|
+
dom.navGameBtn.classList.remove('active');
|
|
32
|
+
dom.navGameBtn.title = 'Install or launch Causyne Game Studio';
|
|
33
|
+
}
|
|
34
|
+
} catch { /* ignore */ }
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export async function launchCausyneGameFromUi(): Promise<void> {
|
|
38
|
+
if (!dom.navGameBtn || !dom.navGameBadge) return;
|
|
39
|
+
if (state.gameLaunching) return;
|
|
40
|
+
state.gameLaunching = true;
|
|
41
|
+
const prevLabel = dom.navGameBadge.textContent;
|
|
42
|
+
dom.navGameBadge.textContent = 'Opening…';
|
|
43
|
+
try {
|
|
44
|
+
const r = await fetch(apiUrl('/api/game/launch'), {
|
|
45
|
+
method: 'POST',
|
|
46
|
+
headers: authHeaders({ 'Content-Type': 'application/json' }),
|
|
47
|
+
body: JSON.stringify({ openBrowser: true })
|
|
48
|
+
});
|
|
49
|
+
const d = await r.json().catch(() => ({})) as GameStatusPayload;
|
|
50
|
+
if (r.ok && d && d.ok && d.url) {
|
|
51
|
+
state.gameStatus = d as Record<string, unknown>;
|
|
52
|
+
dom.navGameBadge.textContent = 'Live';
|
|
53
|
+
dom.navGameBtn.classList.add('active');
|
|
54
|
+
dom.navGameBtn.title = `Open Causyne Game (${d.url})`;
|
|
55
|
+
announceStatus(`Causyne Game running at ${d.url}`);
|
|
56
|
+
if (!d.openedBrowser) {
|
|
57
|
+
try { window.open(d.url, '_blank', 'noopener,noreferrer'); } catch { /* ignore */ }
|
|
58
|
+
}
|
|
59
|
+
} else {
|
|
60
|
+
dom.navGameBadge.textContent = prevLabel || 'Setup';
|
|
61
|
+
const errText = d?.error || 'Could not launch Causyne Game. Run causyne game install --yes.';
|
|
62
|
+
announceAlert(errText);
|
|
63
|
+
setLiveness(false, errText);
|
|
64
|
+
}
|
|
65
|
+
} catch (err) {
|
|
66
|
+
dom.navGameBadge.textContent = prevLabel || '';
|
|
67
|
+
const msg = err instanceof Error ? err.message : String(err);
|
|
68
|
+
announceAlert(`Causyne Game launch failed: ${msg}`);
|
|
69
|
+
} finally {
|
|
70
|
+
state.gameLaunching = false;
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function bindGameLauncherEvents(): void {
|
|
75
|
+
if (dom.navGameBtn) {
|
|
76
|
+
dom.navGameBtn.addEventListener('click', () => { void launchCausyneGameFromUi(); });
|
|
77
|
+
}
|
|
78
|
+
}
|