Files
Om Alve fa7f33bfe8 Case battery: stop showing stale charging; read after the layout
Review fixes for the charging-case battery:

- The case reports whether the glasses are docked. An empty caseInfo
  (the reply out of the case) now parses as "not docked, no level"
  instead of nothing, and undocking is reported even when the level and
  charge state are unchanged. TS keeps the last level but clears
  caseCharging while the glasses are out of the case.
- The phone line shows the charging mark only while it is live: G2 and
  Case only with a link to the glasses (connected or charging), and Case
  only while docked. Levels stay as the last known ones.
- The session-start case read waits until the layout is up, so a
  firmware that ignored it could no longer hold up the first frame. When
  the glasses are already charging at connect, the charging-mode read is
  the only one; the old duplicate read and its "late ACK" log are gone.
- Case dedup state resets on every connect, as wearState does, so each
  session's read reaches TS.
- caseCharging uses > 0 like the headset, and is normalized to 0/1.
- iOS now feeds the reading into the shell battery state, and the
  assistant's describeState includes the case level.
- The batteryLine accessors move above connectionStatusLabel's doc
  comment.
2026-11-02 12:01:12 +05:30

289 lines
11 KiB
TypeScript

import { readUpcomingEventsAsync, type CalendarEvent } from "../native/calendar";
import { hasCalendarPermission } from "../native/calendar-permissions";
import { mediaControllerBridge } from "../native/media-controller";
import { spotifyService } from "../native/spotify";
import { dismissNotification, readActiveNotifications } from "../native/notification-icons";
import { shell } from "../ui/shell/shell";
import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry";
/**
* Registers the always-available system tools into the registry. Called once at
* startup. These wrap shell state and the existing native bridges (calendar,
* media, notifications); nothing here needs an app window to be open.
*/
const WEEKDAYS = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
const MONTHS = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
let registered = false;
export function registerSystemTools(registry: ToolRegistry = toolRegistry): void {
if (registered) return;
registered = true;
// Keep a media listener warm so "what's playing" works without opening Music.
void mediaControllerBridge.start();
registry.registerSystemTool(
{
name: "glasses.get_state",
description:
"Get the current state of the glasses: display on/off, foreground app, headset battery, and the current local time.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
proactive: true,
},
() => ok(describeState()),
);
registry.registerSystemTool(
{
name: "glasses.show_alert",
description:
"Show a short text popup on the glasses display. Use for a brief notice the user should see; keep it to a sentence or two.",
inputSchema: {
type: "object",
properties: { text: { type: "string", description: "The message to display." } },
required: ["text"],
additionalProperties: false,
},
proactive: true,
},
(args) => {
const text = String(args?.text ?? "").trim();
if (!text) return err("show_alert requires non-empty text");
shell.showAlert(text);
return ok("Displayed.");
},
);
registry.registerSystemTool(
{
name: "calendar.list_events",
description:
"List the user's upcoming calendar events. Returns events from now forward, ordered by start time.",
inputSchema: {
type: "object",
properties: {
within_hours: {
type: "number",
description: "Only include events starting within this many hours (default 168 = one week).",
},
max_events: { type: "number", description: "Maximum number of events to return (default 10)." },
},
additionalProperties: false,
},
},
async (args) => {
if (!hasCalendarPermission()) return err("Calendar access is not granted. Open Calendar or Permissions on your phone to allow access.");
const withinHours = clampNumber(args?.within_hours, 1, 24 * 60, 168);
const maxEvents = clampNumber(args?.max_events, 1, 50, 10);
try {
const events = await readUpcomingEventsAsync(maxEvents, withinHours * 60 * 60 * 1000);
if (!events.length) return ok("No upcoming events in that window.");
return ok(events.map(formatEvent).join("\n"));
} catch {
return err("Calendar events could not be read. Check calendar access on your phone and try again.");
}
},
);
registry.registerSystemTool(
{
name: "media.now_playing",
description: "What is currently playing (or paused) in the user's media app, if anything.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
proactive: true,
},
async () => {
// Spotify's state is polled only while something shows it.
await mediaControllerBridge.refresh?.();
return ok(describeNowPlaying());
},
);
registry.registerSystemTool(
{
name: "media.play_pause",
description: "Toggle play/pause for the user's current media session.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
},
async () => {
await mediaControllerBridge.refresh?.();
const state = mediaControllerBridge.snapshot();
if (!state.canPlayPause) return err("No media session can be controlled right now.");
await mediaControllerBridge.playPause();
return ok("Toggled play/pause.");
},
);
registry.registerSystemTool(
{
name: "media.next",
description: "Skip to the next track in the user's current media session.",
inputSchema: { type: "object", properties: {}, additionalProperties: false },
},
async () => {
await mediaControllerBridge.refresh?.();
const state = mediaControllerBridge.snapshot();
if (!state.canSkipNext) return err("The current media session can't skip to the next track.");
await mediaControllerBridge.skipNext();
return ok("Skipped to the next track.");
},
);
const spotify = spotifyService();
if (spotify) {
registry.register({
spec: {
name: "media.spotify_play",
description:
"Search Spotify and start playing the best match on the user's Spotify (this phone or another of their Spotify devices). Use for requests like \"play Daft Punk\" (artist), \"play the album Discovery\" (album) or \"play Harder Better Faster Stronger\" (track).",
inputSchema: {
type: "object",
properties: {
query: { type: "string", description: "What to search for, e.g. an artist, album, song or playlist name." },
kind: {
type: "string",
enum: ["track", "album", "artist", "playlist"],
description: "What kind of thing to play (default track).",
},
},
required: ["query"],
additionalProperties: false,
},
availability: "always",
// Search, then play (with a device lookup when none is active).
timeoutMs: 20_000,
},
handler: async (args) => {
const query = String(args?.query ?? "").trim();
if (!query) return err("media.spotify_play requires a query");
const kind = ["track", "album", "artist", "playlist"].includes(args?.kind) ? args.kind : "track";
try {
return ok(`Playing ${await spotify.playSearch(query, kind)} on Spotify.`);
} catch (error) {
return err(String((error as Error)?.message ?? error));
}
},
// Offered once signed in from the Music app menu.
isAvailable: () => spotify.account() !== null,
});
}
// Android notification tools are still platform-specific.
if (global.isIOS) return;
registry.registerSystemTool(
{
name: "notifications.list",
description: "List the user's current Android notifications mirrored to the glasses.",
inputSchema: {
type: "object",
properties: { max: { type: "number", description: "Maximum notifications to return (default 10)." } },
additionalProperties: false,
},
},
(args) => {
const max = clampNumber(args?.max, 1, 50, 10);
const notifications = readActiveNotifications(max);
if (!notifications.length) return ok("No current notifications.");
return ok(notifications.map(formatNotification).join("\n"));
},
);
registry.registerSystemTool(
{
name: "notifications.dismiss",
description:
"Dismiss a notification by its key (as returned by notifications.list). Returns whether it was dismissed.",
inputSchema: {
type: "object",
properties: { key: { type: "string", description: "The notification key from notifications.list." } },
required: ["key"],
additionalProperties: false,
},
},
(args) => {
const key = String(args?.key ?? "").trim();
if (!key) return err("notifications.dismiss requires a key");
return dismissNotification(key) ? ok("Dismissed.") : err("No notification with that key.");
},
);
}
function describeState(): string {
const parts: string[] = [];
parts.push(`Display: ${shell.isScreenOn() ? "on" : "off"}.`);
const fg = shell.getForegroundApp();
parts.push(fg ? `Foreground app: ${fg.appId} ("${fg.title}").` : "Foreground: launcher (no app open).");
const battery = shell.getBatteryLevels();
if (battery.headset !== null) {
const charging = battery.headsetCharging ? ", charging" : "";
parts.push(`Headset battery: ${battery.headset}%${charging}.`);
}
if (battery.case !== null) {
// caseCharging is null while the glasses are out of the case: the level is the last one seen.
const detail = battery.caseCharging === null ? " (last reading, glasses not in the case)"
: battery.caseCharging ? ", charging" : "";
parts.push(`Charging case battery: ${battery.case}%${detail}.`);
}
parts.push(`Local time: ${formatDateTime(new Date())}.`);
return parts.join(" ");
}
function describeNowPlaying(): string {
const state = mediaControllerBridge.snapshot();
if (!state.accessEnabled) return `Media state is unavailable (${mediaControllerBridge.accessLabel.toLowerCase()}).`;
if (state.playbackState === "playing" || state.playbackState === "paused") {
const verb = state.playbackState === "playing" ? "Playing" : "Paused";
const title = state.title || "(unknown title)";
const artist = state.artist ? ` by ${state.artist}` : "";
const app = state.appName ? ` in ${state.appName}` : "";
return `${verb}: ${title}${artist}${app}.`;
}
return "Nothing is currently playing.";
}
function formatEvent(event: CalendarEvent): string {
const when = event.allDay ? `${formatDate(new Date(event.startMs))} (all day)` : formatDateTime(new Date(event.startMs));
const location = event.location ? ` @ ${event.location}` : "";
return `- ${event.title || "(untitled)"} — ${when}${location}`;
}
function formatNotification(notification: { key: string; appName: string; title: string; text: string; customText?: string[] }): string {
const title = notification.title || notification.customText?.join(" ") || "(no title)";
const body = notification.text ? `: ${notification.text}` : "";
return `- [${notification.appName}] ${title}${body} (key: ${notification.key})`;
}
function formatDateTime(date: Date): string {
return `${formatDate(date)}, ${formatTime(date)}`;
}
function formatDate(date: Date): string {
return `${WEEKDAYS[date.getDay()]} ${MONTHS[date.getMonth()]} ${date.getDate()}`;
}
function formatTime(date: Date): string {
let hours = date.getHours();
const minutes = date.getMinutes().toString().padStart(2, "0");
const meridiem = hours >= 12 ? "PM" : "AM";
hours = hours % 12;
if (hours === 0) hours = 12;
return `${hours}:${minutes} ${meridiem}`;
}
function clampNumber(value: unknown, min: number, max: number, fallback: number): number {
const num = typeof value === "number" ? value : Number(value);
if (!Number.isFinite(num)) return fallback;
return Math.min(max, Math.max(min, Math.round(num)));
}
function ok(content: string): ToolResult {
return { ok: true, content };
}
function err(error: string): ToolResult {
return { ok: false, error };
}