Files
faceclaw/app/assistant/system-tools.ts
T
mwlaboratoriesandClaude Opus 5.5 2e7ca866a8 calendar.list_events: include the end of each event
A multi-day all-day event that started yesterday read as yesterday's
("Mon Nov 16 (all day)"), so the assistant left it out of today's plans;
timed events had no end time. Format the span: the last day of an all-day
event (ends are exclusive) and the end time of a timed one.

The date formatting moves into a pure app/assistant/date-format.ts with
unit tests.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-17 23:50:00 +01:00

264 lines
10 KiB
TypeScript

import { readUpcomingEventsAsync } 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";
import { formatDateTime, formatEvent } from "./date-format";
/**
* 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.
*/
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 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 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 };
}