triflux 10.31.0 → 10.33.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/hooks/codex-session-hook.mjs +113 -0
- package/hub/hub-lifecycle.mjs +242 -0
- package/hub/mac-focus.mjs +94 -0
- package/hub/mac-tray.swift +133 -0
- package/hub/public/tray.html +594 -0
- package/hub/server.mjs +218 -29
- package/hub/team/cto-auto-collect.mjs +109 -0
- package/hub/tray-lifecycle.mjs +131 -0
- package/hub/tray-runtime.mjs +265 -0
- package/hub/tray-state.mjs +448 -0
- package/hub/tray.mjs +93 -4
- package/package.json +1 -1
- package/scripts/__tests__/tfx-route-bash-node-parity.test.mjs +10 -0
- package/scripts/ensure-codex-hooks.mjs +282 -0
- package/scripts/hub-ensure.mjs +11 -5
- package/scripts/lib/env-probe.mjs +35 -8
- package/scripts/pack.mjs +31 -2
- package/scripts/setup.mjs +20 -0
- package/scripts/tfx-route.sh +21 -0
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
import { argv, exit, stdin, stdout } from "node:process";
|
|
4
|
+
import { pathToFileURL } from "node:url";
|
|
5
|
+
import { drainPendingSynapse as defaultDrainPendingSynapse } from "../hub/team/synapse-http.mjs";
|
|
6
|
+
import {
|
|
7
|
+
heartbeatInteractiveSession as defaultHeartbeatInteractiveSession,
|
|
8
|
+
registerInteractiveSession as defaultRegisterInteractiveSession,
|
|
9
|
+
} from "./session-start-fast.mjs";
|
|
10
|
+
|
|
11
|
+
// hub-ensure is loaded lazily so the byte-identical packages/core mirror of this
|
|
12
|
+
// file loads cleanly. packages/core mirrors scripts/lib only (not scripts/*), so a
|
|
13
|
+
// static `../scripts/hub-ensure.mjs` import would make the core copy throw
|
|
14
|
+
// ERR_MODULE_NOT_FOUND at load time. The core copy is published-but-dormant — the
|
|
15
|
+
// live codex hook always runs from the installed triflux package where the path
|
|
16
|
+
// resolves. This mirrors session-start-fast.mjs's dynamic script-import pattern.
|
|
17
|
+
async function defaultHubEnsureRun(stdinData) {
|
|
18
|
+
const { run } = await import(
|
|
19
|
+
new URL("../scripts/hub-ensure.mjs", import.meta.url).href
|
|
20
|
+
);
|
|
21
|
+
return run(stdinData);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parsePayload(stdinData) {
|
|
25
|
+
try {
|
|
26
|
+
const raw = typeof stdinData === "string" ? stdinData : "";
|
|
27
|
+
return raw.trim()
|
|
28
|
+
? { ok: true, payload: JSON.parse(raw) }
|
|
29
|
+
: { ok: false, payload: {} };
|
|
30
|
+
} catch {
|
|
31
|
+
return { ok: false, payload: {} };
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function normalizeMode(mode, payload) {
|
|
36
|
+
const direct = String(mode || "")
|
|
37
|
+
.trim()
|
|
38
|
+
.toLowerCase();
|
|
39
|
+
if (direct === "register" || direct === "heartbeat") return direct;
|
|
40
|
+
|
|
41
|
+
const eventName = String(payload?.hook_event_name || "")
|
|
42
|
+
.trim()
|
|
43
|
+
.toLowerCase()
|
|
44
|
+
.replace(/-/g, "_");
|
|
45
|
+
if (eventName === "session_start" || eventName === "sessionstart") {
|
|
46
|
+
return "register";
|
|
47
|
+
}
|
|
48
|
+
if (eventName === "user_prompt_submit" || eventName === "userpromptsubmit") {
|
|
49
|
+
return "heartbeat";
|
|
50
|
+
}
|
|
51
|
+
return "";
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export async function runCodexSessionHook(stdinData, opts = {}) {
|
|
55
|
+
const output = "{}\n";
|
|
56
|
+
const parsed = parsePayload(stdinData);
|
|
57
|
+
const mode = parsed.ok
|
|
58
|
+
? normalizeMode(opts.argvMode ?? argv[2], parsed.payload)
|
|
59
|
+
: "";
|
|
60
|
+
const hubEnsureRun = opts.hubEnsureRun || defaultHubEnsureRun;
|
|
61
|
+
const registerInteractiveSession =
|
|
62
|
+
opts.registerInteractiveSession || defaultRegisterInteractiveSession;
|
|
63
|
+
const heartbeatInteractiveSession =
|
|
64
|
+
opts.heartbeatInteractiveSession || defaultHeartbeatInteractiveSession;
|
|
65
|
+
const drainPendingSynapse =
|
|
66
|
+
opts.drainPendingSynapse || defaultDrainPendingSynapse;
|
|
67
|
+
|
|
68
|
+
try {
|
|
69
|
+
if (mode === "register") {
|
|
70
|
+
try {
|
|
71
|
+
await hubEnsureRun(stdinData);
|
|
72
|
+
} catch {}
|
|
73
|
+
try {
|
|
74
|
+
registerInteractiveSession(stdinData);
|
|
75
|
+
} catch {}
|
|
76
|
+
try {
|
|
77
|
+
await drainPendingSynapse(1000);
|
|
78
|
+
} catch {}
|
|
79
|
+
} else if (mode === "heartbeat") {
|
|
80
|
+
try {
|
|
81
|
+
heartbeatInteractiveSession(stdinData);
|
|
82
|
+
} catch {}
|
|
83
|
+
try {
|
|
84
|
+
await drainPendingSynapse(500);
|
|
85
|
+
} catch {}
|
|
86
|
+
}
|
|
87
|
+
} catch {
|
|
88
|
+
// Codex session hooks are observational and must never block the session.
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (opts.writeStdout !== false) {
|
|
92
|
+
stdout.write(output);
|
|
93
|
+
}
|
|
94
|
+
return output;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function readStdin() {
|
|
98
|
+
return new Promise((resolve) => {
|
|
99
|
+
let data = "";
|
|
100
|
+
stdin.setEncoding("utf8");
|
|
101
|
+
stdin.on("data", (chunk) => {
|
|
102
|
+
data += chunk;
|
|
103
|
+
});
|
|
104
|
+
stdin.on("end", () => resolve(data));
|
|
105
|
+
stdin.on("error", () => resolve(data));
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (argv[1] && import.meta.url === pathToFileURL(argv[1]).href) {
|
|
110
|
+
const stdinData = await readStdin();
|
|
111
|
+
await runCodexSessionHook(stdinData);
|
|
112
|
+
exit(0);
|
|
113
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
// hub/hub-lifecycle.mjs — Hub process lifecycle helpers
|
|
2
|
+
|
|
3
|
+
import { execFileSync } from "node:child_process";
|
|
4
|
+
import { readFileSync } from "node:fs";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { join } from "node:path";
|
|
7
|
+
|
|
8
|
+
export const HUB_DEFAULT_PORT = 27888;
|
|
9
|
+
|
|
10
|
+
const HUB_PID_FILE = join(homedir(), ".claude", "cache", "tfx-hub", "hub.pid");
|
|
11
|
+
const EPHEMERAL_ENV_KEYS = [
|
|
12
|
+
"TFX_WORKER_SANDBOX_SCOPE",
|
|
13
|
+
"TFX_WORKER_INDEX",
|
|
14
|
+
"TFX_TEAM_TASK_ID",
|
|
15
|
+
"TFX_TEAM_AGENT_NAME",
|
|
16
|
+
"TFX_EPHEMERAL",
|
|
17
|
+
];
|
|
18
|
+
|
|
19
|
+
function parsePositiveInt(value) {
|
|
20
|
+
const parsed = Number.parseInt(String(value ?? ""), 10);
|
|
21
|
+
return Number.isFinite(parsed) && parsed > 0 ? parsed : null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function parsePortFromUrl(value) {
|
|
25
|
+
try {
|
|
26
|
+
return parsePositiveInt(new URL(String(value)).port);
|
|
27
|
+
} catch {
|
|
28
|
+
return null;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function isHubServerCommand(command) {
|
|
33
|
+
return /(^|[\\/,\s])hub[\\/]server\.mjs(?=$|[\s"'`])/i.test(
|
|
34
|
+
String(command || ""),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
export function isWorktreeOrEphemeralHubContext({
|
|
39
|
+
cwd = process.cwd(),
|
|
40
|
+
env = process.env,
|
|
41
|
+
} = {}) {
|
|
42
|
+
const normalizedCwd = String(cwd || "").replace(/\\/g, "/");
|
|
43
|
+
if (
|
|
44
|
+
normalizedCwd.includes("/.claude/worktrees/") ||
|
|
45
|
+
normalizedCwd.includes("/.worktrees/") ||
|
|
46
|
+
normalizedCwd.includes("/.codex-swarm/wt-") ||
|
|
47
|
+
/(^|\/)wt-[^/]+(?:\/|$)/u.test(normalizedCwd)
|
|
48
|
+
) {
|
|
49
|
+
return true;
|
|
50
|
+
}
|
|
51
|
+
return EPHEMERAL_ENV_KEYS.some((key) => String(env?.[key] || "").length > 0);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function resolveHubPortForContext({
|
|
55
|
+
port,
|
|
56
|
+
env = process.env,
|
|
57
|
+
cwd = process.cwd(),
|
|
58
|
+
defaultPort = HUB_DEFAULT_PORT,
|
|
59
|
+
} = {}) {
|
|
60
|
+
const envPort = parsePositiveInt(port) ?? parsePositiveInt(env?.TFX_HUB_PORT);
|
|
61
|
+
const urlPort = parsePortFromUrl(env?.TFX_HUB_URL);
|
|
62
|
+
const resolvedPort = envPort ?? urlPort ?? defaultPort;
|
|
63
|
+
if (
|
|
64
|
+
resolvedPort !== defaultPort &&
|
|
65
|
+
isWorktreeOrEphemeralHubContext({ cwd, env })
|
|
66
|
+
) {
|
|
67
|
+
return defaultPort;
|
|
68
|
+
}
|
|
69
|
+
return resolvedPort;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
export function collectHubProcesses(
|
|
73
|
+
psOutput = "",
|
|
74
|
+
{ currentPid = process.pid } = {},
|
|
75
|
+
) {
|
|
76
|
+
const ownPid = Number(currentPid);
|
|
77
|
+
return String(psOutput)
|
|
78
|
+
.split(/\r?\n/u)
|
|
79
|
+
.flatMap((line) => {
|
|
80
|
+
const match = line.match(/^\s*(\d+)\s+(\d+)\s+(.+)$/u);
|
|
81
|
+
if (!match) return [];
|
|
82
|
+
const pid = Number.parseInt(match[1], 10);
|
|
83
|
+
const ppid = Number.parseInt(match[2], 10);
|
|
84
|
+
const command = match[3].trim();
|
|
85
|
+
if (!Number.isFinite(pid) || pid === ownPid) return [];
|
|
86
|
+
if (!/\bnode(?:\s|$)|\/node(?:\s|$)/u.test(command)) return [];
|
|
87
|
+
if (!isHubServerCommand(command)) return [];
|
|
88
|
+
return [{ pid, ppid, command }];
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function readPidFilePid({
|
|
93
|
+
pidFilePath = HUB_PID_FILE,
|
|
94
|
+
readFile = readFileSync,
|
|
95
|
+
} = {}) {
|
|
96
|
+
try {
|
|
97
|
+
const info = JSON.parse(readFile(pidFilePath, "utf8"));
|
|
98
|
+
const pid = Number(info?.pid);
|
|
99
|
+
return Number.isFinite(pid) && pid > 0 ? pid : null;
|
|
100
|
+
} catch {
|
|
101
|
+
return null;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
function findListeningPidsForPort(
|
|
106
|
+
port = HUB_DEFAULT_PORT,
|
|
107
|
+
{ execFileSyncFn = execFileSync } = {},
|
|
108
|
+
) {
|
|
109
|
+
try {
|
|
110
|
+
const output = execFileSyncFn(
|
|
111
|
+
"lsof",
|
|
112
|
+
["-nP", `-iTCP:${port}`, "-sTCP:LISTEN", "-t"],
|
|
113
|
+
{
|
|
114
|
+
encoding: "utf8",
|
|
115
|
+
timeout: 1000,
|
|
116
|
+
maxBuffer: 1024 * 1024,
|
|
117
|
+
},
|
|
118
|
+
);
|
|
119
|
+
return String(output)
|
|
120
|
+
.split(/\r?\n/u)
|
|
121
|
+
.map((line) => Number.parseInt(line.trim(), 10))
|
|
122
|
+
.filter((pid) => Number.isFinite(pid) && pid > 0);
|
|
123
|
+
} catch {
|
|
124
|
+
return [];
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
async function waitForExit(
|
|
129
|
+
pid,
|
|
130
|
+
{ killFn = process.kill, graceMs = 5000, pollMs = 100 } = {},
|
|
131
|
+
) {
|
|
132
|
+
const deadline = Date.now() + Math.max(0, graceMs);
|
|
133
|
+
while (Date.now() <= deadline) {
|
|
134
|
+
try {
|
|
135
|
+
killFn(pid, 0);
|
|
136
|
+
} catch {
|
|
137
|
+
return true;
|
|
138
|
+
}
|
|
139
|
+
await new Promise((resolve) => setTimeout(resolve, pollMs));
|
|
140
|
+
}
|
|
141
|
+
try {
|
|
142
|
+
killFn(pid, 0);
|
|
143
|
+
return false;
|
|
144
|
+
} catch {
|
|
145
|
+
return true;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export async function reapExistingHubProcesses({
|
|
150
|
+
currentPid = process.pid,
|
|
151
|
+
pidFilePath = HUB_PID_FILE,
|
|
152
|
+
execFileSyncFn = execFileSync,
|
|
153
|
+
killFn = process.kill,
|
|
154
|
+
readPidFileFn,
|
|
155
|
+
findListeningPidsForPortFn,
|
|
156
|
+
waitForExitFn,
|
|
157
|
+
graceMs = 5000,
|
|
158
|
+
pollMs = 100,
|
|
159
|
+
} = {}) {
|
|
160
|
+
let output = "";
|
|
161
|
+
try {
|
|
162
|
+
output = execFileSyncFn("ps", ["-axo", "pid,ppid,command"], {
|
|
163
|
+
encoding: "utf8",
|
|
164
|
+
timeout: 1000,
|
|
165
|
+
maxBuffer: 1024 * 1024,
|
|
166
|
+
});
|
|
167
|
+
} catch {
|
|
168
|
+
return {
|
|
169
|
+
candidates: [],
|
|
170
|
+
reaped: [],
|
|
171
|
+
preserved: {
|
|
172
|
+
currentPid: Number(currentPid),
|
|
173
|
+
pidFilePid: null,
|
|
174
|
+
defaultPortPids: [],
|
|
175
|
+
},
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const current = Number(currentPid);
|
|
180
|
+
const pidFileInfo =
|
|
181
|
+
typeof readPidFileFn === "function"
|
|
182
|
+
? readPidFileFn()
|
|
183
|
+
: { pid: readPidFilePid({ pidFilePath }) };
|
|
184
|
+
const pidFilePid = parsePositiveInt(pidFileInfo?.pid);
|
|
185
|
+
const defaultPortPids = [
|
|
186
|
+
...new Set(
|
|
187
|
+
(typeof findListeningPidsForPortFn === "function"
|
|
188
|
+
? findListeningPidsForPortFn(HUB_DEFAULT_PORT)
|
|
189
|
+
: findListeningPidsForPort(HUB_DEFAULT_PORT, { execFileSyncFn })
|
|
190
|
+
).map(Number),
|
|
191
|
+
),
|
|
192
|
+
].filter((pid) => Number.isFinite(pid) && pid > 0);
|
|
193
|
+
|
|
194
|
+
const preservedPids = new Set(
|
|
195
|
+
[current, pidFilePid, ...defaultPortPids].filter(
|
|
196
|
+
(pid) => Number.isFinite(pid) && pid > 0,
|
|
197
|
+
),
|
|
198
|
+
);
|
|
199
|
+
const candidates = collectHubProcesses(output, { currentPid: current });
|
|
200
|
+
const reaped = [];
|
|
201
|
+
const failed = [];
|
|
202
|
+
|
|
203
|
+
for (const processInfo of candidates) {
|
|
204
|
+
if (preservedPids.has(processInfo.pid)) continue;
|
|
205
|
+
try {
|
|
206
|
+
killFn(processInfo.pid, "SIGTERM");
|
|
207
|
+
const exited =
|
|
208
|
+
typeof waitForExitFn === "function"
|
|
209
|
+
? await waitForExitFn(processInfo.pid, { killFn, graceMs, pollMs })
|
|
210
|
+
: await waitForExit(processInfo.pid, { killFn, graceMs, pollMs });
|
|
211
|
+
if (!exited) {
|
|
212
|
+
try {
|
|
213
|
+
killFn(processInfo.pid, "SIGKILL");
|
|
214
|
+
} catch (error) {
|
|
215
|
+
failed.push({
|
|
216
|
+
pid: processInfo.pid,
|
|
217
|
+
signal: "SIGKILL",
|
|
218
|
+
error: String(error?.message || error),
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
reaped.push(processInfo);
|
|
223
|
+
} catch (error) {
|
|
224
|
+
failed.push({
|
|
225
|
+
pid: processInfo.pid,
|
|
226
|
+
signal: "SIGTERM",
|
|
227
|
+
error: String(error?.message || error),
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
return {
|
|
233
|
+
candidates,
|
|
234
|
+
reaped,
|
|
235
|
+
failed,
|
|
236
|
+
preserved: {
|
|
237
|
+
currentPid: current,
|
|
238
|
+
pidFilePid,
|
|
239
|
+
defaultPortPids,
|
|
240
|
+
},
|
|
241
|
+
};
|
|
242
|
+
}
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
|
|
3
|
+
export const FOCUS_SCRIPT_TIMEOUT_MS = 5_000;
|
|
4
|
+
|
|
5
|
+
const DEFAULT_FOCUS_CANDIDATES = [
|
|
6
|
+
"iTerm2",
|
|
7
|
+
"Terminal",
|
|
8
|
+
"Codex",
|
|
9
|
+
"Claude",
|
|
10
|
+
"Antigravity",
|
|
11
|
+
"Code",
|
|
12
|
+
];
|
|
13
|
+
|
|
14
|
+
function normalizeAgentId(value) {
|
|
15
|
+
const id = String(value || "")
|
|
16
|
+
.trim()
|
|
17
|
+
.toLowerCase();
|
|
18
|
+
if (id === "agy" || id === "anti") return "antigravity";
|
|
19
|
+
return id;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function getFocusCandidates({ agentId } = {}) {
|
|
23
|
+
switch (normalizeAgentId(agentId)) {
|
|
24
|
+
case "codex":
|
|
25
|
+
return ["Codex", "iTerm2", "Terminal", "Code"];
|
|
26
|
+
case "claude":
|
|
27
|
+
return ["Claude", "iTerm2", "Terminal"];
|
|
28
|
+
case "antigravity":
|
|
29
|
+
return ["Antigravity", "iTerm2", "Terminal"];
|
|
30
|
+
default:
|
|
31
|
+
return [...DEFAULT_FOCUS_CANDIDATES];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function appleScriptList(values) {
|
|
36
|
+
return `{${values.map((value) => JSON.stringify(String(value))).join(", ")}}`;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export function buildFocusScript({ agentId, appCandidates } = {}) {
|
|
40
|
+
const candidates = Array.isArray(appCandidates)
|
|
41
|
+
? appCandidates.map(String).filter(Boolean)
|
|
42
|
+
: getFocusCandidates({ agentId });
|
|
43
|
+
return `
|
|
44
|
+
set appCandidates to ${appleScriptList(candidates)}
|
|
45
|
+
tell application "System Events"
|
|
46
|
+
set runningNames to name of every process
|
|
47
|
+
end tell
|
|
48
|
+
repeat with appName in appCandidates
|
|
49
|
+
set appText to appName as text
|
|
50
|
+
if runningNames contains appText then
|
|
51
|
+
tell application appText to activate
|
|
52
|
+
return "activated " & appText
|
|
53
|
+
end if
|
|
54
|
+
end repeat
|
|
55
|
+
return "no focus candidate running"
|
|
56
|
+
`;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export function focusSessionOnMac(target = {}, maybeUdsId = null) {
|
|
60
|
+
const normalizedTarget =
|
|
61
|
+
target && typeof target === "object"
|
|
62
|
+
? target
|
|
63
|
+
: { sessionId: target, udsId: maybeUdsId };
|
|
64
|
+
const script = buildFocusScript({ agentId: normalizedTarget.agentId });
|
|
65
|
+
return new Promise((resolve) => {
|
|
66
|
+
execFile(
|
|
67
|
+
"osascript",
|
|
68
|
+
["-e", script],
|
|
69
|
+
{ timeout: FOCUS_SCRIPT_TIMEOUT_MS },
|
|
70
|
+
(err, stdout = "", stderr = "") => {
|
|
71
|
+
const output = String(stdout || stderr || "").trim();
|
|
72
|
+
if (err) {
|
|
73
|
+
console.error("mac-focus error:", err?.message || err);
|
|
74
|
+
resolve({
|
|
75
|
+
ok: false,
|
|
76
|
+
sessionId: normalizedTarget.sessionId || null,
|
|
77
|
+
pid: Number(normalizedTarget.pid) || null,
|
|
78
|
+
agentId: normalizedTarget.agentId || null,
|
|
79
|
+
error: err?.message || String(err),
|
|
80
|
+
output,
|
|
81
|
+
});
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
resolve({
|
|
85
|
+
ok: true,
|
|
86
|
+
sessionId: normalizedTarget.sessionId || null,
|
|
87
|
+
pid: Number(normalizedTarget.pid) || null,
|
|
88
|
+
agentId: normalizedTarget.agentId || null,
|
|
89
|
+
output,
|
|
90
|
+
});
|
|
91
|
+
},
|
|
92
|
+
);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
import Cocoa
|
|
2
|
+
import WebKit
|
|
3
|
+
|
|
4
|
+
class AppDelegate: NSObject, NSApplicationDelegate, NSPopoverDelegate {
|
|
5
|
+
var statusItem: NSStatusItem!
|
|
6
|
+
var popover: NSPopover!
|
|
7
|
+
var webVC: WebViewController!
|
|
8
|
+
var port: String = "27888"
|
|
9
|
+
|
|
10
|
+
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
|
11
|
+
if CommandLine.arguments.count > 1 {
|
|
12
|
+
port = CommandLine.arguments[1]
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
statusItem = NSStatusBar.system.statusItem(withLength: NSStatusItem.variableLength)
|
|
16
|
+
if let button = statusItem.button {
|
|
17
|
+
button.title = "T"
|
|
18
|
+
button.target = self
|
|
19
|
+
button.action = #selector(togglePopover(_:))
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
webVC = WebViewController()
|
|
23
|
+
webVC.port = port
|
|
24
|
+
webVC.onFocusComplete = { [weak self] in
|
|
25
|
+
self?.popover.performClose(nil)
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
popover = NSPopover()
|
|
29
|
+
popover.contentViewController = webVC
|
|
30
|
+
popover.behavior = .transient
|
|
31
|
+
popover.animates = false
|
|
32
|
+
popover.delegate = self
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
@objc func togglePopover(_ sender: Any?) {
|
|
36
|
+
if popover.isShown {
|
|
37
|
+
popover.performClose(sender)
|
|
38
|
+
} else {
|
|
39
|
+
if let button = statusItem.button {
|
|
40
|
+
webVC.reloadForPopover()
|
|
41
|
+
popover.show(relativeTo: button.bounds, of: button, preferredEdge: .minY)
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
class WebViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
|
|
48
|
+
var webView: WKWebView!
|
|
49
|
+
var port: String = "27888"
|
|
50
|
+
var retryWorkItem: DispatchWorkItem?
|
|
51
|
+
var onFocusComplete: (() -> Void)?
|
|
52
|
+
|
|
53
|
+
func logError(_ msg: String) {
|
|
54
|
+
let path = "/tmp/mac-tray.log"
|
|
55
|
+
if let outputStream = OutputStream(toFileAtPath: path, append: true) {
|
|
56
|
+
outputStream.open()
|
|
57
|
+
let text = "\(Date()): \(msg)\n"
|
|
58
|
+
let bytesWritten = outputStream.write(text, maxLength: text.utf8.count)
|
|
59
|
+
outputStream.close()
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
override func loadView() {
|
|
64
|
+
let webConfiguration = WKWebViewConfiguration()
|
|
65
|
+
webConfiguration.userContentController.add(self, name: "tray")
|
|
66
|
+
let initialFrame = NSRect(x: 0, y: 0, width: 320, height: 520)
|
|
67
|
+
webView = WKWebView(frame: initialFrame, configuration: webConfiguration)
|
|
68
|
+
|
|
69
|
+
// Transparent background for Glassmorphism
|
|
70
|
+
webView.setValue(false, forKey: "drawsBackground")
|
|
71
|
+
|
|
72
|
+
self.view = NSView(frame: initialFrame)
|
|
73
|
+
webView.frame = self.view.bounds
|
|
74
|
+
webView.autoresizingMask = [.width, .height]
|
|
75
|
+
self.view.addSubview(webView)
|
|
76
|
+
|
|
77
|
+
self.preferredContentSize = initialFrame.size
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
override func viewDidLoad() {
|
|
81
|
+
super.viewDidLoad()
|
|
82
|
+
webView.navigationDelegate = self
|
|
83
|
+
loadTray()
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
func loadTray() {
|
|
87
|
+
retryWorkItem?.cancel()
|
|
88
|
+
if let url = URL(string: "http://127.0.0.1:\(port)/tray.html?t=\(Date().timeIntervalSince1970)") {
|
|
89
|
+
let request = URLRequest(url: url)
|
|
90
|
+
webView.load(request)
|
|
91
|
+
} else {
|
|
92
|
+
logError("Invalid URL")
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
func reloadForPopover() {
|
|
97
|
+
if isViewLoaded {
|
|
98
|
+
loadTray()
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
|
|
103
|
+
logError("Failed to load: \(error.localizedDescription)")
|
|
104
|
+
let retry = DispatchWorkItem { [weak self] in
|
|
105
|
+
self?.loadTray()
|
|
106
|
+
}
|
|
107
|
+
retryWorkItem = retry
|
|
108
|
+
DispatchQueue.main.asyncAfter(deadline: .now() + 0.35, execute: retry)
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
|
|
112
|
+
retryWorkItem?.cancel()
|
|
113
|
+
retryWorkItem = nil
|
|
114
|
+
logError("Successfully loaded URL")
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
|
|
118
|
+
guard message.name == "tray" else { return }
|
|
119
|
+
if let body = message.body as? [String: Any],
|
|
120
|
+
let type = body["type"] as? String,
|
|
121
|
+
type == "focus-complete" {
|
|
122
|
+
DispatchQueue.main.async { [weak self] in
|
|
123
|
+
self?.onFocusComplete?()
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
let app = NSApplication.shared
|
|
130
|
+
let delegate = AppDelegate()
|
|
131
|
+
app.delegate = delegate
|
|
132
|
+
app.setActivationPolicy(.accessory)
|
|
133
|
+
app.run()
|