@nextclaw/companion 0.1.1-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/LICENSE +21 -0
- package/dist/src/companion-session-view.service.test.js +27 -0
- package/dist/src/launcher/index.js +22 -0
- package/dist/src/main.js +22 -0
- package/dist/src/preload/index.js +13 -0
- package/dist/src/services/companion-application.service.js +68 -0
- package/dist/src/services/companion-runtime-client.service.js +88 -0
- package/dist/src/services/companion-session-view.service.js +52 -0
- package/dist/src/services/companion-tray.service.js +41 -0
- package/dist/src/services/companion-window.service.js +106 -0
- package/dist/src/stores/companion-runtime-state.store.js +21 -0
- package/dist/src/stores/companion-window-position.store.js +30 -0
- package/dist/src/types/companion.types.js +2 -0
- package/dist/src/utils/companion-renderer-html.utils.js +194 -0
- package/package.json +31 -0
- package/scripts/smoke.mjs +20 -0
- package/src/companion-session-view.service.test.ts +37 -0
- package/src/launcher/index.ts +24 -0
- package/src/main.ts +23 -0
- package/src/preload/index.ts +13 -0
- package/src/services/companion-application.service.ts +76 -0
- package/src/services/companion-runtime-client.service.ts +118 -0
- package/src/services/companion-session-view.service.ts +63 -0
- package/src/services/companion-tray.service.ts +44 -0
- package/src/services/companion-window.service.ts +115 -0
- package/src/stores/companion-runtime-state.store.ts +23 -0
- package/src/stores/companion-window-position.store.ts +31 -0
- package/src/types/companion.types.ts +37 -0
- package/src/utils/companion-renderer-html.utils.ts +192 -0
- package/tsconfig.json +13 -0
package/CHANGELOG.md
ADDED
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 NextClaw contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const vitest_1 = require("vitest");
|
|
4
|
+
const companion_session_view_service_js_1 = require("./services/companion-session-view.service.js");
|
|
5
|
+
(0, vitest_1.describe)("CompanionSessionViewService", () => {
|
|
6
|
+
(0, vitest_1.it)("prefers the most recently updated running session", () => {
|
|
7
|
+
const service = new companion_session_view_service_js_1.CompanionSessionViewService("http://127.0.0.1:55667", (agentId) => `http://127.0.0.1:55667/api/agents/${agentId}/avatar`);
|
|
8
|
+
const view = service.selectView({
|
|
9
|
+
agents: [{ id: "writer", displayName: "Writer" }],
|
|
10
|
+
sessions: [
|
|
11
|
+
{ sessionId: "older", agentId: "writer", messageCount: 1, updatedAt: "2026-05-05T00:00:00.000Z", status: "running" },
|
|
12
|
+
{ sessionId: "newer", agentId: "writer", messageCount: 2, updatedAt: "2026-05-06T00:00:00.000Z", status: "running" }
|
|
13
|
+
]
|
|
14
|
+
});
|
|
15
|
+
(0, vitest_1.expect)(view.sessionId).toBe("newer");
|
|
16
|
+
(0, vitest_1.expect)(view.title).toBe("Writer");
|
|
17
|
+
});
|
|
18
|
+
(0, vitest_1.it)("falls back to an idle shell view when no running session exists", () => {
|
|
19
|
+
const service = new companion_session_view_service_js_1.CompanionSessionViewService("http://127.0.0.1:55667", () => "http://127.0.0.1:55667/api/agents/default/avatar");
|
|
20
|
+
const view = service.selectView({
|
|
21
|
+
agents: [],
|
|
22
|
+
sessions: []
|
|
23
|
+
});
|
|
24
|
+
(0, vitest_1.expect)(view.state).toBe("idle");
|
|
25
|
+
(0, vitest_1.expect)(view.subtitle).toBe("No active agent");
|
|
26
|
+
});
|
|
27
|
+
});
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
const node_child_process_1 = require("node:child_process");
|
|
5
|
+
const node_module_1 = require("node:module");
|
|
6
|
+
const node_path_1 = require("node:path");
|
|
7
|
+
const loadModule = (0, node_module_1.createRequire)(__filename);
|
|
8
|
+
const electronBinary = loadModule("electron");
|
|
9
|
+
const appRoot = (0, node_path_1.resolve)(__dirname, "..", "..", "..");
|
|
10
|
+
const env = { ...process.env };
|
|
11
|
+
delete env.ELECTRON_RUN_AS_NODE;
|
|
12
|
+
const child = (0, node_child_process_1.spawn)(electronBinary, [appRoot, ...process.argv.slice(2)], {
|
|
13
|
+
stdio: "inherit",
|
|
14
|
+
env
|
|
15
|
+
});
|
|
16
|
+
child.on("exit", (code, signal) => {
|
|
17
|
+
if (signal) {
|
|
18
|
+
process.kill(process.pid, signal);
|
|
19
|
+
return;
|
|
20
|
+
}
|
|
21
|
+
process.exit(code ?? 0);
|
|
22
|
+
});
|
package/dist/src/main.js
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const companion_application_service_js_1 = require("./services/companion-application.service.js");
|
|
4
|
+
function resolveBaseUrl(argv) {
|
|
5
|
+
const baseUrlFromCli = argv.find((value) => value.startsWith("--base-url="));
|
|
6
|
+
if (baseUrlFromCli) {
|
|
7
|
+
return baseUrlFromCli.slice("--base-url=".length);
|
|
8
|
+
}
|
|
9
|
+
const baseUrlFlagIndex = argv.findIndex((value) => value === "--base-url");
|
|
10
|
+
if (baseUrlFlagIndex >= 0 && argv[baseUrlFlagIndex + 1]) {
|
|
11
|
+
return argv[baseUrlFlagIndex + 1];
|
|
12
|
+
}
|
|
13
|
+
return process.env.NEXTCLAW_COMPANION_BASE_URL?.trim() || "http://127.0.0.1:55667";
|
|
14
|
+
}
|
|
15
|
+
async function main() {
|
|
16
|
+
const application = new companion_application_service_js_1.CompanionApplicationService({
|
|
17
|
+
baseUrl: resolveBaseUrl(process.argv.slice(1)),
|
|
18
|
+
runtimeStatePath: process.env.NEXTCLAW_COMPANION_RUNTIME_STATE_PATH?.trim() || undefined
|
|
19
|
+
});
|
|
20
|
+
await application.run();
|
|
21
|
+
}
|
|
22
|
+
void main();
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const electron_1 = require("electron");
|
|
4
|
+
electron_1.contextBridge.exposeInMainWorld("nextclawCompanion", {
|
|
5
|
+
onView: (listener) => {
|
|
6
|
+
electron_1.ipcRenderer.on("companion:view", (_event, view) => {
|
|
7
|
+
listener(view);
|
|
8
|
+
});
|
|
9
|
+
},
|
|
10
|
+
open: () => electron_1.ipcRenderer.invoke("companion:open"),
|
|
11
|
+
quit: () => electron_1.ipcRenderer.invoke("companion:quit"),
|
|
12
|
+
ready: () => electron_1.ipcRenderer.send("companion:ready")
|
|
13
|
+
});
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionApplicationService = void 0;
|
|
4
|
+
const electron_1 = require("electron");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
const companion_runtime_state_store_js_1 = require("../stores/companion-runtime-state.store.js");
|
|
7
|
+
const companion_window_position_store_js_1 = require("../stores/companion-window-position.store.js");
|
|
8
|
+
const companion_runtime_client_service_js_1 = require("./companion-runtime-client.service.js");
|
|
9
|
+
const companion_tray_service_js_1 = require("./companion-tray.service.js");
|
|
10
|
+
const companion_window_service_js_1 = require("./companion-window.service.js");
|
|
11
|
+
class CompanionApplicationService {
|
|
12
|
+
options;
|
|
13
|
+
runtimeClient;
|
|
14
|
+
runtimeStateStore;
|
|
15
|
+
windowService;
|
|
16
|
+
trayService;
|
|
17
|
+
quitting = false;
|
|
18
|
+
constructor(options) {
|
|
19
|
+
this.options = options;
|
|
20
|
+
const preloadPath = (0, node_path_1.resolve)(__dirname, "..", "preload", "index.js");
|
|
21
|
+
this.runtimeClient = new companion_runtime_client_service_js_1.CompanionRuntimeClientService(options.baseUrl);
|
|
22
|
+
this.runtimeStateStore = options.runtimeStatePath
|
|
23
|
+
? new companion_runtime_state_store_js_1.CompanionRuntimeStateStore(options.runtimeStatePath)
|
|
24
|
+
: null;
|
|
25
|
+
this.windowService = new companion_window_service_js_1.CompanionWindowService(preloadPath, companion_window_position_store_js_1.CompanionWindowPositionStore.fromUserData(electron_1.app.getPath("userData")));
|
|
26
|
+
this.trayService = new companion_tray_service_js_1.CompanionTrayService(options.baseUrl, () => this.windowService.toggleVisibility(), () => this.quit());
|
|
27
|
+
}
|
|
28
|
+
run = async () => {
|
|
29
|
+
if (!electron_1.app.requestSingleInstanceLock()) {
|
|
30
|
+
electron_1.app.quit();
|
|
31
|
+
return;
|
|
32
|
+
}
|
|
33
|
+
electron_1.app.on("second-instance", () => {
|
|
34
|
+
this.windowService.show();
|
|
35
|
+
});
|
|
36
|
+
electron_1.app.on("window-all-closed", () => undefined);
|
|
37
|
+
electron_1.app.on("before-quit", () => {
|
|
38
|
+
this.quitting = true;
|
|
39
|
+
this.runtimeClient.stop();
|
|
40
|
+
this.trayService.destroy();
|
|
41
|
+
this.windowService.destroy();
|
|
42
|
+
this.runtimeStateStore?.clear();
|
|
43
|
+
});
|
|
44
|
+
electron_1.app.on("activate", () => {
|
|
45
|
+
this.windowService.show();
|
|
46
|
+
});
|
|
47
|
+
await electron_1.app.whenReady();
|
|
48
|
+
this.runtimeStateStore?.write({
|
|
49
|
+
pid: process.pid,
|
|
50
|
+
startedAt: new Date().toISOString(),
|
|
51
|
+
baseUrl: this.options.baseUrl
|
|
52
|
+
});
|
|
53
|
+
await this.windowService.create();
|
|
54
|
+
this.windowService.show();
|
|
55
|
+
this.trayService.create();
|
|
56
|
+
await this.runtimeClient.start((view) => {
|
|
57
|
+
this.windowService.updateView(view);
|
|
58
|
+
});
|
|
59
|
+
};
|
|
60
|
+
quit = () => {
|
|
61
|
+
if (this.quitting) {
|
|
62
|
+
return;
|
|
63
|
+
}
|
|
64
|
+
this.quitting = true;
|
|
65
|
+
electron_1.app.quit();
|
|
66
|
+
};
|
|
67
|
+
}
|
|
68
|
+
exports.CompanionApplicationService = CompanionApplicationService;
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionRuntimeClientService = void 0;
|
|
4
|
+
const companion_session_view_service_js_1 = require("./companion-session-view.service.js");
|
|
5
|
+
class CompanionRuntimeClientService {
|
|
6
|
+
baseUrl;
|
|
7
|
+
client = null;
|
|
8
|
+
viewService = null;
|
|
9
|
+
refreshTimer = null;
|
|
10
|
+
subscription = null;
|
|
11
|
+
constructor(baseUrl) {
|
|
12
|
+
this.baseUrl = baseUrl;
|
|
13
|
+
}
|
|
14
|
+
start = async (onView) => {
|
|
15
|
+
await this.ensureClient();
|
|
16
|
+
await this.refresh(onView);
|
|
17
|
+
const client = await this.ensureClient();
|
|
18
|
+
this.subscription = client.sessions.subscribe(async () => {
|
|
19
|
+
await this.refresh(onView);
|
|
20
|
+
}, {
|
|
21
|
+
reconnectDelayMs: 1000,
|
|
22
|
+
onError: async () => {
|
|
23
|
+
await this.refresh(onView);
|
|
24
|
+
}
|
|
25
|
+
});
|
|
26
|
+
this.refreshTimer = setInterval(() => {
|
|
27
|
+
void this.refresh(onView);
|
|
28
|
+
}, 10000);
|
|
29
|
+
};
|
|
30
|
+
stop = () => {
|
|
31
|
+
this.subscription?.close();
|
|
32
|
+
this.subscription = null;
|
|
33
|
+
if (this.refreshTimer !== null) {
|
|
34
|
+
clearInterval(this.refreshTimer);
|
|
35
|
+
this.refreshTimer = null;
|
|
36
|
+
}
|
|
37
|
+
};
|
|
38
|
+
refresh = async (onView) => {
|
|
39
|
+
try {
|
|
40
|
+
const client = await this.ensureClient();
|
|
41
|
+
const [agents, sessions] = await Promise.all([
|
|
42
|
+
client.agents.list(),
|
|
43
|
+
client.sessions.list()
|
|
44
|
+
]);
|
|
45
|
+
onView(this.ensureViewService(client).selectView({
|
|
46
|
+
agents,
|
|
47
|
+
sessions: sessions.sessions
|
|
48
|
+
}));
|
|
49
|
+
}
|
|
50
|
+
catch (error) {
|
|
51
|
+
console.error("[companion] refresh failed", error);
|
|
52
|
+
onView(this.createOfflineView(error instanceof Error
|
|
53
|
+
? { summary: this.summarizeOfflineError(error.message) }
|
|
54
|
+
: undefined));
|
|
55
|
+
}
|
|
56
|
+
};
|
|
57
|
+
ensureClient = async () => {
|
|
58
|
+
if (this.client) {
|
|
59
|
+
return this.client;
|
|
60
|
+
}
|
|
61
|
+
const sdkModule = await import("@nextclaw/client-sdk");
|
|
62
|
+
this.client = sdkModule.createNextClawClient({ baseUrl: this.baseUrl });
|
|
63
|
+
return this.client;
|
|
64
|
+
};
|
|
65
|
+
ensureViewService = (client) => {
|
|
66
|
+
if (this.viewService) {
|
|
67
|
+
return this.viewService;
|
|
68
|
+
}
|
|
69
|
+
this.viewService = new companion_session_view_service_js_1.CompanionSessionViewService(this.baseUrl, client.agents.resolveAvatarUrl);
|
|
70
|
+
return this.viewService;
|
|
71
|
+
};
|
|
72
|
+
createOfflineView = (reason) => {
|
|
73
|
+
const viewService = this.viewService ??
|
|
74
|
+
new companion_session_view_service_js_1.CompanionSessionViewService(this.baseUrl, (agentId) => `${this.baseUrl}/api/agents/${encodeURIComponent(agentId)}/avatar`);
|
|
75
|
+
return viewService.createOfflineView(reason);
|
|
76
|
+
};
|
|
77
|
+
summarizeOfflineError = (message) => {
|
|
78
|
+
if (/fetch failed/i.test(message)) {
|
|
79
|
+
return "Cannot reach runtime";
|
|
80
|
+
}
|
|
81
|
+
if (/timed out/i.test(message)) {
|
|
82
|
+
return "Runtime timeout";
|
|
83
|
+
}
|
|
84
|
+
const trimmed = message.trim();
|
|
85
|
+
return trimmed.length > 28 ? `${trimmed.slice(0, 25)}...` : trimmed || "Runtime unavailable";
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
exports.CompanionRuntimeClientService = CompanionRuntimeClientService;
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionSessionViewService = void 0;
|
|
4
|
+
class CompanionSessionViewService {
|
|
5
|
+
baseUrl;
|
|
6
|
+
resolveAvatarUrl;
|
|
7
|
+
constructor(baseUrl, resolveAvatarUrl) {
|
|
8
|
+
this.baseUrl = baseUrl;
|
|
9
|
+
this.resolveAvatarUrl = resolveAvatarUrl;
|
|
10
|
+
}
|
|
11
|
+
selectView = (input) => {
|
|
12
|
+
const runningSession = [...input.sessions]
|
|
13
|
+
.filter((session) => session.status === "running")
|
|
14
|
+
.sort((left, right) => right.updatedAt.localeCompare(left.updatedAt))[0];
|
|
15
|
+
if (!runningSession) {
|
|
16
|
+
return {
|
|
17
|
+
state: "idle",
|
|
18
|
+
title: "NextClaw",
|
|
19
|
+
subtitle: "No active agent",
|
|
20
|
+
openUrl: this.baseUrl
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
const agent = this.findAgent(input.agents, runningSession.agentId);
|
|
24
|
+
return {
|
|
25
|
+
state: "running",
|
|
26
|
+
title: agent?.displayName?.trim() || runningSession.agentId || "Active Agent",
|
|
27
|
+
subtitle: runningSession.sessionId,
|
|
28
|
+
avatarUrl: runningSession.agentId ? this.resolveAvatar(agent, runningSession.agentId) : undefined,
|
|
29
|
+
sessionId: runningSession.sessionId,
|
|
30
|
+
agentId: runningSession.agentId,
|
|
31
|
+
openUrl: this.baseUrl
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
createOfflineView = (reason) => {
|
|
35
|
+
return {
|
|
36
|
+
state: "offline",
|
|
37
|
+
title: "NextClaw",
|
|
38
|
+
subtitle: reason?.summary?.trim() || "Runtime unavailable",
|
|
39
|
+
openUrl: this.baseUrl
|
|
40
|
+
};
|
|
41
|
+
};
|
|
42
|
+
findAgent = (agents, agentId) => {
|
|
43
|
+
if (!agentId) {
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
return agents.find((agent) => agent.id === agentId) ?? null;
|
|
47
|
+
};
|
|
48
|
+
resolveAvatar = (agent, agentId) => {
|
|
49
|
+
return agent?.avatarUrl?.trim() || this.resolveAvatarUrl(agentId);
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
exports.CompanionSessionViewService = CompanionSessionViewService;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionTrayService = void 0;
|
|
4
|
+
const electron_1 = require("electron");
|
|
5
|
+
class CompanionTrayService {
|
|
6
|
+
baseUrl;
|
|
7
|
+
onToggleWindow;
|
|
8
|
+
onQuit;
|
|
9
|
+
tray = null;
|
|
10
|
+
constructor(baseUrl, onToggleWindow, onQuit) {
|
|
11
|
+
this.baseUrl = baseUrl;
|
|
12
|
+
this.onToggleWindow = onToggleWindow;
|
|
13
|
+
this.onQuit = onQuit;
|
|
14
|
+
}
|
|
15
|
+
create = () => {
|
|
16
|
+
if (this.tray) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
this.tray = new electron_1.Tray(this.createTrayIcon());
|
|
20
|
+
this.tray.setToolTip("NextClaw Companion");
|
|
21
|
+
this.tray.on("click", this.onToggleWindow);
|
|
22
|
+
this.tray.setContextMenu(electron_1.Menu.buildFromTemplate([
|
|
23
|
+
{ label: "Show Companion", click: this.onToggleWindow },
|
|
24
|
+
{ label: "Open NextClaw", click: () => void electron_1.shell.openExternal(this.baseUrl) },
|
|
25
|
+
{ type: "separator" },
|
|
26
|
+
{ label: "Quit", click: this.onQuit }
|
|
27
|
+
]));
|
|
28
|
+
};
|
|
29
|
+
destroy = () => {
|
|
30
|
+
this.tray?.destroy();
|
|
31
|
+
this.tray = null;
|
|
32
|
+
};
|
|
33
|
+
createTrayIcon = () => {
|
|
34
|
+
const svg = encodeURIComponent(`<svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 20 20">
|
|
35
|
+
<rect x="1" y="1" width="18" height="18" rx="6" fill="#16324f"/>
|
|
36
|
+
<circle cx="10" cy="10" r="4" fill="#f5f8fb"/>
|
|
37
|
+
</svg>`);
|
|
38
|
+
return electron_1.nativeImage.createFromDataURL(`data:image/svg+xml;charset=utf-8,${svg}`);
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
exports.CompanionTrayService = CompanionTrayService;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionWindowService = void 0;
|
|
4
|
+
const electron_1 = require("electron");
|
|
5
|
+
const companion_renderer_html_utils_js_1 = require("../utils/companion-renderer-html.utils.js");
|
|
6
|
+
class CompanionWindowService {
|
|
7
|
+
preloadPath;
|
|
8
|
+
positionStore;
|
|
9
|
+
window = null;
|
|
10
|
+
currentView = null;
|
|
11
|
+
constructor(preloadPath, positionStore) {
|
|
12
|
+
this.preloadPath = preloadPath;
|
|
13
|
+
this.positionStore = positionStore;
|
|
14
|
+
}
|
|
15
|
+
create = async () => {
|
|
16
|
+
if (this.window) {
|
|
17
|
+
return;
|
|
18
|
+
}
|
|
19
|
+
const bounds = this.positionStore.read();
|
|
20
|
+
this.window = new electron_1.BrowserWindow({
|
|
21
|
+
width: 112,
|
|
22
|
+
height: 132,
|
|
23
|
+
x: bounds?.x,
|
|
24
|
+
y: bounds?.y,
|
|
25
|
+
frame: false,
|
|
26
|
+
transparent: true,
|
|
27
|
+
resizable: false,
|
|
28
|
+
movable: true,
|
|
29
|
+
alwaysOnTop: true,
|
|
30
|
+
skipTaskbar: true,
|
|
31
|
+
hasShadow: false,
|
|
32
|
+
webPreferences: {
|
|
33
|
+
preload: this.preloadPath,
|
|
34
|
+
contextIsolation: true,
|
|
35
|
+
nodeIntegration: false
|
|
36
|
+
}
|
|
37
|
+
});
|
|
38
|
+
this.window.on("close", () => {
|
|
39
|
+
this.persistBounds();
|
|
40
|
+
});
|
|
41
|
+
this.window.on("move", () => {
|
|
42
|
+
this.persistBounds();
|
|
43
|
+
});
|
|
44
|
+
this.window.on("moved", () => {
|
|
45
|
+
this.persistBounds();
|
|
46
|
+
});
|
|
47
|
+
this.window.on("closed", () => {
|
|
48
|
+
this.window = null;
|
|
49
|
+
});
|
|
50
|
+
electron_1.ipcMain.removeHandler("companion:open");
|
|
51
|
+
electron_1.ipcMain.handle("companion:open", async () => {
|
|
52
|
+
const openUrl = this.currentView?.openUrl;
|
|
53
|
+
if (openUrl) {
|
|
54
|
+
await electron_1.shell.openExternal(openUrl);
|
|
55
|
+
}
|
|
56
|
+
return null;
|
|
57
|
+
});
|
|
58
|
+
electron_1.ipcMain.removeHandler("companion:quit");
|
|
59
|
+
electron_1.ipcMain.handle("companion:quit", async () => {
|
|
60
|
+
electron_1.app.quit();
|
|
61
|
+
return null;
|
|
62
|
+
});
|
|
63
|
+
electron_1.ipcMain.removeAllListeners("companion:ready");
|
|
64
|
+
electron_1.ipcMain.on("companion:ready", () => {
|
|
65
|
+
if (this.currentView) {
|
|
66
|
+
this.window?.webContents.send("companion:view", this.currentView);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
await this.window.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent((0, companion_renderer_html_utils_js_1.renderCompanionHtml)())}`);
|
|
70
|
+
};
|
|
71
|
+
show = () => {
|
|
72
|
+
this.window?.showInactive();
|
|
73
|
+
};
|
|
74
|
+
toggleVisibility = () => {
|
|
75
|
+
if (!this.window) {
|
|
76
|
+
return;
|
|
77
|
+
}
|
|
78
|
+
if (this.window.isVisible()) {
|
|
79
|
+
this.window.hide();
|
|
80
|
+
return;
|
|
81
|
+
}
|
|
82
|
+
this.window.showInactive();
|
|
83
|
+
};
|
|
84
|
+
updateView = (view) => {
|
|
85
|
+
this.currentView = view;
|
|
86
|
+
this.window?.webContents.send("companion:view", view);
|
|
87
|
+
};
|
|
88
|
+
destroy = () => {
|
|
89
|
+
electron_1.ipcMain.removeHandler("companion:open");
|
|
90
|
+
electron_1.ipcMain.removeHandler("companion:quit");
|
|
91
|
+
electron_1.ipcMain.removeAllListeners("companion:ready");
|
|
92
|
+
this.window?.destroy();
|
|
93
|
+
this.window = null;
|
|
94
|
+
};
|
|
95
|
+
persistBounds = () => {
|
|
96
|
+
const bounds = this.window?.getBounds();
|
|
97
|
+
if (!bounds) {
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
this.positionStore.write({
|
|
101
|
+
x: bounds.x,
|
|
102
|
+
y: bounds.y
|
|
103
|
+
});
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
exports.CompanionWindowService = CompanionWindowService;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionRuntimeStateStore = void 0;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
class CompanionRuntimeStateStore {
|
|
7
|
+
filePath;
|
|
8
|
+
constructor(filePath) {
|
|
9
|
+
this.filePath = filePath;
|
|
10
|
+
}
|
|
11
|
+
write = (state) => {
|
|
12
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(this.filePath), { recursive: true });
|
|
13
|
+
(0, node_fs_1.writeFileSync)(this.filePath, JSON.stringify(state, null, 2));
|
|
14
|
+
};
|
|
15
|
+
clear = () => {
|
|
16
|
+
if ((0, node_fs_1.existsSync)(this.filePath)) {
|
|
17
|
+
(0, node_fs_1.rmSync)(this.filePath, { force: true });
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
exports.CompanionRuntimeStateStore = CompanionRuntimeStateStore;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompanionWindowPositionStore = void 0;
|
|
4
|
+
const node_fs_1 = require("node:fs");
|
|
5
|
+
const node_path_1 = require("node:path");
|
|
6
|
+
class CompanionWindowPositionStore {
|
|
7
|
+
filePath;
|
|
8
|
+
constructor(filePath) {
|
|
9
|
+
this.filePath = filePath;
|
|
10
|
+
}
|
|
11
|
+
read = () => {
|
|
12
|
+
if (!(0, node_fs_1.existsSync)(this.filePath)) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse((0, node_fs_1.readFileSync)(this.filePath, "utf-8"));
|
|
17
|
+
}
|
|
18
|
+
catch {
|
|
19
|
+
return null;
|
|
20
|
+
}
|
|
21
|
+
};
|
|
22
|
+
write = (bounds) => {
|
|
23
|
+
(0, node_fs_1.mkdirSync)((0, node_path_1.dirname)(this.filePath), { recursive: true });
|
|
24
|
+
(0, node_fs_1.writeFileSync)(this.filePath, JSON.stringify(bounds, null, 2));
|
|
25
|
+
};
|
|
26
|
+
static fromUserData = (userDataPath) => {
|
|
27
|
+
return new CompanionWindowPositionStore((0, node_path_1.resolve)(userDataPath, "companion-window.json"));
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
exports.CompanionWindowPositionStore = CompanionWindowPositionStore;
|