Skip to content
Merged
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
52 changes: 44 additions & 8 deletions packages/user-intent-kit/src/adapters/desktop.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
// SPDX-License-Identifier: AGPL-3.0

import { execSync } from 'node:child_process';
import { execSync, execFileSync } from 'node:child_process';
import { platform } from 'node:os';

/** Idle longer than this and the user is not at this machine. */
const IDLE_AFTER_SEC = 300;
import { collectHostTelemetry } from '../host-telemetry.js';

/**
Expand Down Expand Up @@ -61,13 +64,42 @@ export class DesktopAdapter {
this.#client.stopHeartbeat();
}

/**
* Seconds since the user last touched this machine, or undefined if the OS
* will not say. macOS keeps it in the HID system as nanoseconds.
*/
#idleSeconds() {
if (platform() !== 'darwin') return undefined;
try {
const out = execFileSync('/usr/sbin/ioreg', ['-c', 'IOHIDSystem'], {
encoding: 'utf8',
timeout: 3000,
stdio: ['ignore', 'pipe', 'ignore'],
});
const ns = Number((out.match(/"HIDIdleTime"\s*=\s*(\d+)/) || [])[1]);
return Number.isFinite(ns) ? Math.round(ns / 1e9) : undefined;
} catch {
return undefined;
}
}

#detectState() {
// Vitals ride along with the desktop card so a machine's row shows what it
// is and how it is doing, the same fields the Pi publishes — otherwise a
// Mac appears in the fleet as a name and nothing else.
const idleSec = this.#idleSeconds();

// `screen_active` used to be hardcoded true. The derived state picks the
// most recently updated device whose screen is active as the one the user
// is on, and this daemon republishes every 30s — so an unattended Mac
// always won, and the dashboard told Petrus he was working at the Mac mini
// while he was on his phone. It had been untouched for 18 hours.
const active = idleSec === undefined ? true : idleSec < IDLE_AFTER_SEC;

const state = {
screen_active: true,
context: 'active',
screen_active: active,
context: active ? 'active' : 'idle',
...(idleSec === undefined ? {} : { idle_sec: idleSec }),
...collectHostTelemetry({ machine: this.#machine, kind: this.#kind }),
};

Expand All @@ -79,11 +111,15 @@ export class DesktopAdapter {
).trim();
state.active_app = app.toLowerCase();

// Infer context from active app
if (['zoom', 'microsoft teams', 'google meet', 'facetime', 'webex'].some(a => state.active_app.includes(a))) {
state.context = 'meeting';
} else if (['claude', 'codex', 'terminal', 'iterm', 'warp', 'code', 'cursor'].some(a => state.active_app.includes(a))) {
state.context = 'coding';
// Infer context from active app — but only when someone is actually
// at the machine. A frontmost editor on a box nobody has touched since
// yesterday is not "coding".
if (state.screen_active) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

P1 Badge Preserve meeting context during input-idle periods

During a Zoom, Teams, FaceTime, or Webex call where the user listens for five minutes without touching the keyboard or mouse, screen_active becomes false and this guard suppresses the existing meeting inference. The adapter then publishes context: "idle", so derived intent can stop applying meeting-specific text-only/audio-suppression behavior even though the call remains foreground. HID idle measures input inactivity rather than call participation; meeting applications should be detected before this gate or exempted from it.

Useful? React with 👍 / 👎.

if (['zoom', 'microsoft teams', 'google meet', 'facetime', 'webex'].some(a => state.active_app.includes(a))) {
state.context = 'meeting';
} else if (['claude', 'codex', 'terminal', 'iterm', 'warp', 'code', 'cursor'].some(a => state.active_app.includes(a))) {
state.context = 'coding';
}
}
}
} catch {
Expand Down
Loading