Repository navigation
Expand file tree
/
Copy pathindex.ts
More file actions
181 lines (159 loc) · 4.3 KB
/
Copy pathindex.ts
File metadata and controls
181 lines (159 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
import type { Plugin } from "@opencode-ai/plugin";
import {
existsSync,
mkdirSync,
readdirSync,
readFileSync,
writeFileSync,
unlinkSync,
} from "fs";
const TMP_DIR = "/tmp/opencode-wakelock";
const SESSIONS_DIR = `${TMP_DIR}/sessions`;
const CAFFEINATE_PID_FILE = `${TMP_DIR}/caffeinate.pid`;
function isProcessAlive(pid: number): boolean {
try {
process.kill(pid, 0);
return true;
} catch {
return false;
}
}
function ensureDirs() {
mkdirSync(SESSIONS_DIR, { recursive: true });
}
function getActiveSessions(): string[] {
if (!existsSync(SESSIONS_DIR)) return [];
const files = readdirSync(SESSIONS_DIR);
const active: string[] = [];
for (const sessionID of files) {
const filePath = `${SESSIONS_DIR}/${sessionID}`;
try {
const pid = parseInt(readFileSync(filePath, "utf8").trim(), 10);
if (isProcessAlive(pid))
active.push(sessionID);
else
unlinkSync(filePath);
} catch {}
}
return active;
}
function isCaffeinateRunning(): boolean {
if (!existsSync(CAFFEINATE_PID_FILE)) return false;
try {
const pid = parseInt(readFileSync(CAFFEINATE_PID_FILE, "utf8").trim(), 10);
if (isProcessAlive(pid)) return true;
unlinkSync(CAFFEINATE_PID_FILE);
return false;
} catch {
return false;
}
}
function startCaffeinate() {
const proc = Bun.spawn(["caffeinate", "-i"], {
stdio: ["ignore", "ignore", "ignore"],
});
writeFileSync(CAFFEINATE_PID_FILE, String(proc.pid));
}
function stopCaffeinate() {
if (!existsSync(CAFFEINATE_PID_FILE)) return;
try {
const pid = parseInt(readFileSync(CAFFEINATE_PID_FILE, "utf8").trim(), 10);
process.kill(pid, "SIGTERM");
} catch {}
try {
unlinkSync(CAFFEINATE_PID_FILE);
} catch {}
}
function acquire(sessionID: string) {
if (process.platform !== "darwin") return;
ensureDirs();
writeFileSync(`${SESSIONS_DIR}/${sessionID}`, String(process.pid));
if (!isCaffeinateRunning())
startCaffeinate();
}
function release(sessionID: string) {
if (process.platform !== "darwin") return;
try {
unlinkSync(`${SESSIONS_DIR}/${sessionID}`);
} catch {}
const remaining = getActiveSessions();
if (remaining.length === 0)
stopCaffeinate();
}
function startupCleanup() {
if (process.platform !== "darwin") return;
ensureDirs();
const active = getActiveSessions();
if (active.length === 0)
stopCaffeinate();
}
export const WakelockPlugin: Plugin = async ({ client }) => {
await client.app.log({
body: {
level: "info",
service: "wakelock",
message: "Plugin initializing",
extra: { platform: process.platform, pid: process.pid },
},
});
startupCleanup();
await client.app.log({
body: {
level: "info",
service: "wakelock",
message: "Startup cleanup complete",
},
});
return {
event: async ({ event }) => {
await client.app.log({
body: {
level: "debug",
service: "wakelock",
message: "Event received",
extra: { type: event.type },
},
});
// Handle session becoming busy (actively working)
if (event.type === "session.status" && event.properties.status.type === "busy") {
const { sessionID } = (event as any).properties;
acquire(sessionID);
await client.app.log({
body: {
level: "info",
service: "wakelock",
extra: { sessionID },
message: "Acquired wakelock (session busy)",
},
});
}
// Handle session becoming idle (completed)
if (event.type === "session.idle") {
const { sessionID } = (event as any).properties;
release(sessionID);
await client.app.log({
body: {
level: "info",
service: "wakelock",
extra: { sessionID },
message: "Released wakelock (session idle)",
},
});
}
// Handle session error
if (event.type === "session.error") {
const { sessionID } = (event as any).properties;
release(sessionID);
await client.app.log({
body: {
level: "info",
service: "wakelock",
extra: { sessionID },
message: "Released wakelock (session error)",
},
});
}
},
};
};
export default WakelockPlugin;