Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions apps/code/src/main/platform-adapters/electron-power-manager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import { execFile } from "node:child_process";
import { readdir } from "node:fs/promises";
import { promisify } from "node:util";
import type { IPowerManager } from "@posthog/platform/power-manager";
import { powerMonitor, powerSaveBlocker } from "electron";
import { injectable } from "inversify";

const execFileAsync = promisify(execFile);

@injectable()
export class ElectronPowerManager implements IPowerManager {
public onResume(handler: () => void): () => void {
Expand All @@ -17,4 +22,38 @@ export class ElectronPowerManager implements IPowerManager {
}
};
}

public hasBuiltInBattery(): Promise<boolean> {
memoizedBuiltInBattery ??= detectBuiltInBattery().catch(() => false);
return memoizedBuiltInBattery;
}
}

let memoizedBuiltInBattery: Promise<boolean> | null = null;

async function detectBuiltInBattery(): Promise<boolean> {
switch (process.platform) {
case "darwin": {
const { stdout } = await execFileAsync("ioreg", [
"-rc",
"AppleSmartBattery",
]);
return stdout.includes("AppleSmartBattery");
}
case "win32": {
const { stdout } = await execFileAsync("powershell", [
"-NoProfile",
"-NonInteractive",
"-Command",
"[bool](Get-CimInstance -ClassName Win32_Battery)",
]);
return stdout.trim().toLowerCase() === "true";
}
case "linux": {
const supplies = await readdir("/sys/class/power_supply");
return supplies.some((name) => name.startsWith("BAT"));
}
default:
return false;
}
}
1 change: 1 addition & 0 deletions packages/core/src/auth/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ vi.mock("@posthog/shared", async (importOriginal) => {
const mockPowerManager = vi.hoisted(() => ({
onResume: vi.fn(() => () => {}),
preventSleep: vi.fn(() => () => {}),
hasBuiltInBattery: vi.fn(async () => false),
}));

function createSessionPort(): IAuthSessionStore {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/sleep/sleep.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ function createDeps(preventSleepInitially = true) {
const powerManager: IPowerManager = {
onResume: vi.fn(() => () => {}),
preventSleep: vi.fn(() => release),
hasBuiltInBattery: vi.fn(async () => false),
};

let stored = preventSleepInitially;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/sleep/sleep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,10 @@ export class SleepService {
return this.enabled;
}

hasBuiltInBattery(): Promise<boolean> {
return this.powerManager.hasBuiltInBattery();
}

acquire(activityId: string): void {
this.activeActivities.add(activityId);
this.updateBlocker();
Expand Down
6 changes: 6 additions & 0 deletions packages/host-router/src/routers/sleep.router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,10 @@ export const sleepRouter = router({
.mutation(({ ctx, input }) => {
ctx.container.get<SleepService>(SLEEP_SERVICE).setEnabled(input.enabled);
}),

hasBuiltInBattery: publicProcedure
.output(z.boolean())
.query(({ ctx }) =>
ctx.container.get<SleepService>(SLEEP_SERVICE).hasBuiltInBattery(),
),
});
1 change: 1 addition & 0 deletions packages/platform/src/power-manager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export interface IPowerManager {
onResume(handler: () => void): () => void;
preventSleep(reason: string): () => void;
hasBuiltInBattery(): Promise<boolean>;
}

export const POWER_MANAGER_SERVICE = Symbol.for(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ export function GeneralSettings() {
const { data: serverPreventSleep } = useQuery(
hostTRPC.sleep.getEnabled.queryOptions(),
);
const { data: hasBuiltInBattery } = useQuery(
hostTRPC.sleep.hasBuiltInBattery.queryOptions(),
);
const preventSleepMutation = useMutation(
hostTRPC.sleep.setEnabled.mutationOptions(),
);
Expand Down Expand Up @@ -434,7 +437,11 @@ export function GeneralSettings() {

<SettingRow
label="Keep awake while agents work"
description="Prevent your computer from sleeping while the agent is running a task"
description={
hasBuiltInBattery
? "Prevent your computer from going to sleep on its own while the agent is running a task. Closing the lid will still put it to sleep."
: "Prevent your computer from going to sleep on its own while the agent is running a task"
}
Comment on lines +440 to +444

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 The base description text is duplicated across both branches of the ternary — the only difference is the extra sentence appended for laptops. Extracting the shared portion satisfies the OnceAndOnlyOnce simplicity rule and makes future copy changes a single-site edit.

Suggested change
description={
hasBuiltInBattery
? "Prevent your computer from going to sleep on its own while the agent is running a task. Closing the lid will still put it to sleep."
: "Prevent your computer from going to sleep on its own while the agent is running a task"
}
description={`Prevent your computer from going to sleep on its own while the agent is running a task.${hasBuiltInBattery ? " Closing the lid will still put it to sleep." : ""}`}

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer it the original way

noBorder
>
<Switch
Expand Down
1 change: 1 addition & 0 deletions packages/workspace-server/src/services/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ function createMockDependencies() {
powerManager: {
onResume: vi.fn(() => () => {}),
preventSleep: vi.fn(() => () => {}),
hasBuiltInBattery: vi.fn(async () => false),
},
bundledResources: {
resolve: vi.fn((rel: string) => `/mock/appPath/${rel}`),
Expand Down
Loading