From b4728f4f150ac3ea2185c8b04025cda20aee054b Mon Sep 17 00:00:00 2001 From: James Babcock Date: Sun, 9 Aug 2026 12:14:19 -0700 Subject: [PATCH] Voice assistant can set/check/cancel timers without the timer app being already open --- app/assistant/launch-on-call.ts | 40 +++++++++++++++++ app/assistant/navigate-tools.ts | 30 +------------ app/assistant/roam-tools.ts | 24 ++--------- app/assistant/timer-tools.ts | 76 +++++++++++++++++++++++++++++++++ app/g2/dashboard-controller.ts | 3 ++ 5 files changed, 124 insertions(+), 49 deletions(-) create mode 100644 app/assistant/launch-on-call.ts create mode 100644 app/assistant/timer-tools.ts diff --git a/app/assistant/launch-on-call.ts b/app/assistant/launch-on-call.ts new file mode 100644 index 00000000..5da466f0 --- /dev/null +++ b/app/assistant/launch-on-call.ts @@ -0,0 +1,40 @@ +/** + * Launch-on-call forwarding for shell-side wrapper tools (the "installed" + * tier in notes/voice-assistant-design.md): an app's own tools are + * window-scoped ("open" tier), but commands like "start a 5 minute timer" + * must work with no window open. Wrappers call this to launch the app if its + * tool isn't registered yet, wait for the window's set-tools declaration to + * land in the registry, and forward the call. Used by the nav.*, roam.*, and + * timer.* wrappers; callers should set timeoutMs on their own spec high + * enough to cover the appear-wait plus the forwarded call's budget. + */ +import { type ToolRegistry, type ToolResult } from "./tool-registry"; + +const TOOL_APPEAR_TIMEOUT_MS = 5_000; +const TOOL_APPEAR_POLL_MS = 150; + +/** Launch appId if needed, wait for its unprefixed tool, and forward the call. */ +export async function callAppToolWithLaunch( + registry: ToolRegistry, + launchApp: (appId: string) => Promise, + appId: string, + unprefixedName: string, + args: unknown, +): Promise { + const fullName = `app.${appId}.${unprefixedName}`; + if (!registry.listTools().some((tool) => tool.name === fullName)) { + await launchApp(appId); + const deadline = Date.now() + TOOL_APPEAR_TIMEOUT_MS; + while (!registry.listTools().some((tool) => tool.name === fullName)) { + if (Date.now() > deadline) { + return { ok: false, error: `The ${appId} app did not start in time.` }; + } + await sleep(TOOL_APPEAR_POLL_MS); + } + } + return registry.callTool(fullName, args); +} + +function sleep(ms: number): Promise { + return new Promise((resolve) => setTimeout(resolve, ms)); +} diff --git a/app/assistant/navigate-tools.ts b/app/assistant/navigate-tools.ts index b284418e..b644ac59 100644 --- a/app/assistant/navigate-tools.ts +++ b/app/assistant/navigate-tools.ts @@ -6,10 +6,9 @@ */ import { ensureFineLocationPermission } from "../g2/android-permissions"; import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry"; +import { callAppToolWithLaunch } from "./launch-on-call"; const APP_TOOL_PREFIX = "app.navigate."; -const TOOL_APPEAR_TIMEOUT_MS = 5_000; -const TOOL_APPEAR_POLL_MS = 150; let registered = false; @@ -47,7 +46,7 @@ export function registerNavigateTools( if (!destination) return err("nav.start_navigation requires a destination"); // Fire the permission prompt early; the phone shows it while we work. void ensureFineLocationPermission().catch(() => {}); - const forwarded = await callAppTool(launchApp, registry, "start_route", { + const forwarded = await callAppToolWithLaunch(registry, launchApp, "navigate", "start_route", { query: destination, profile: args?.profile, }); @@ -86,31 +85,6 @@ export function registerNavigateTools( ); } -/** Launch the Navigate app if needed, wait for its tool, and forward the call. */ -async function callAppTool( - launchApp: (appId: string) => Promise, - registry: ToolRegistry, - unprefixedName: string, - args: unknown, -): Promise { - const fullName = `${APP_TOOL_PREFIX}${unprefixedName}`; - if (!registry.listTools().some((tool) => tool.name === fullName)) { - await launchApp("navigate"); - const deadline = Date.now() + TOOL_APPEAR_TIMEOUT_MS; - while (!registry.listTools().some((tool) => tool.name === fullName)) { - if (Date.now() > deadline) { - return err("The Navigate app did not start in time."); - } - await sleep(TOOL_APPEAR_POLL_MS); - } - } - return registry.callTool(fullName, args); -} - -function sleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)); -} - function ok(content: string): ToolResult { return { ok: true, content }; } diff --git a/app/assistant/roam-tools.ts b/app/assistant/roam-tools.ts index 549cd262..f7f02f29 100644 --- a/app/assistant/roam-tools.ts +++ b/app/assistant/roam-tools.ts @@ -6,10 +6,7 @@ */ import { roamApiTokenSetting, roamGraphNameSetting } from "../ui/dashboard-settings"; import { toolRegistry, type ToolRegistry, type ToolResult } from "./tool-registry"; - -const APP_TOOL_PREFIX = "app.roam."; -const TOOL_APPEAR_TIMEOUT_MS = 5_000; -const TOOL_APPEAR_POLL_MS = 150; +import { callAppToolWithLaunch } from "./launch-on-call"; let registered = false; @@ -52,7 +49,7 @@ export function registerRoamTools( ); } -/** Launch the Roam app if needed, wait for its tool, and forward the call. */ +/** Check Roam configuration, then launch-on-call forward to the Roam app. */ async function callAppTool( launchApp: (appId: string) => Promise, registry: ToolRegistry, @@ -62,22 +59,7 @@ async function callAppTool( if (roamGraphNameSetting.get().length === 0 || roamApiTokenSetting.get().length === 0) { return err("Roam is not configured; the user must set the graph name and API token in Settings > Roam."); } - const fullName = `${APP_TOOL_PREFIX}${unprefixedName}`; - if (!registry.listTools().some((tool) => tool.name === fullName)) { - await launchApp("roam"); - const deadline = Date.now() + TOOL_APPEAR_TIMEOUT_MS; - while (!registry.listTools().some((tool) => tool.name === fullName)) { - if (Date.now() > deadline) { - return err("The Roam app did not start in time."); - } - await sleep(TOOL_APPEAR_POLL_MS); - } - } - return registry.callTool(fullName, args); -} - -function sleep(ms: number): Promise { - return new Promise((resolve) => setTimeout(resolve, ms)); + return callAppToolWithLaunch(registry, launchApp, "roam", unprefixedName, args); } function err(error: string): ToolResult { diff --git a/app/assistant/timer-tools.ts b/app/assistant/timer-tools.ts new file mode 100644 index 00000000..088a271a --- /dev/null +++ b/app/assistant/timer-tools.ts @@ -0,0 +1,76 @@ +/** + * Shell-side timer.* assistant tools. The Timer app's own tools are + * window-scoped ("open" tier), but "start a 5 minute timer" must work with no + * Timer window open — so these always-available wrappers launch the app, + * wait for its tools to register, and forward the call. Timer state lives in + * the app's worker, which outlives its window, so list/cancel also route + * through the app rather than answering "no timers" from the shell. + */ +import { toolRegistry, type ToolRegistry } from "./tool-registry"; +import { callAppToolWithLaunch } from "./launch-on-call"; + +let registered = false; + +// Wrapper timeout: covers a first-launch worker spawn (up to the 5 s +// tool-appear wait in callAppToolWithLaunch) plus the forwarded call's own +// 10 s default budget. +const TIMER_WRAPPER_TIMEOUT_MS = 20_000; + +export function registerTimerTools( + launchApp: (appId: string) => Promise, + registry: ToolRegistry = toolRegistry, +): void { + if (registered) return; + registered = true; + + registry.registerSystemTool( + { + name: "timer.set", + description: + "Start a countdown timer on the glasses. Give the duration as hours/minutes/seconds; at least one must be nonzero. Opens the Timer app if it isn't already open. Returns the timer's number and when it will finish.", + inputSchema: { + type: "object", + properties: { + hours: { type: "number", description: "Hours component of the duration (default 0)." }, + minutes: { type: "number", description: "Minutes component of the duration (default 0)." }, + seconds: { type: "number", description: "Seconds component of the duration (default 0)." }, + }, + additionalProperties: false, + }, + timeoutMs: TIMER_WRAPPER_TIMEOUT_MS, + proactive: true, + }, + (args) => callAppToolWithLaunch(registry, launchApp, "timer", "set_timer", args ?? {}), + ); + + registry.registerSystemTool( + { + name: "timer.list", + description: + "List the countdown timers on the glasses: each timer's number, its original duration, and the time remaining (or that it has finished). Opens the Timer app if it isn't already open — timers keep running while its window is closed.", + inputSchema: { type: "object", properties: {}, additionalProperties: false }, + timeoutMs: TIMER_WRAPPER_TIMEOUT_MS, + proactive: true, + }, + () => callAppToolWithLaunch(registry, launchApp, "timer", "list_timers", {}), + ); + + registry.registerSystemTool( + { + name: "timer.cancel", + description: + "Cancel a running countdown timer on the glasses, or dismiss a finished one. Give the timer number from timer.list; it may be omitted when only one timer exists. Pass all=true to clear every timer. Opens the Timer app if it isn't already open.", + inputSchema: { + type: "object", + properties: { + timer: { type: "number", description: "1-based timer number, as shown by timer.list." }, + all: { type: "boolean", description: "Clear all timers instead of a single one." }, + }, + additionalProperties: false, + }, + timeoutMs: TIMER_WRAPPER_TIMEOUT_MS, + proactive: true, + }, + (args) => callAppToolWithLaunch(registry, launchApp, "timer", "cancel_timer", args ?? {}), + ); +} diff --git a/app/g2/dashboard-controller.ts b/app/g2/dashboard-controller.ts index 672b6481..d233eef8 100644 --- a/app/g2/dashboard-controller.ts +++ b/app/g2/dashboard-controller.ts @@ -24,6 +24,7 @@ import { registerNavigateTools } from "../assistant/navigate-tools"; import { registerRoamTools } from "../assistant/roam-tools"; import { assistantBridge } from "../assistant/bridge-client"; import { registerWindowTools } from "../assistant/window-tools"; +import { registerTimerTools } from "../assistant/timer-tools"; import { WorkerAppHost } from "../ui/shell/worker-window"; import { ALL_APPS } from "../apps/all-apps"; import { type AppContext, type AppDefinition, type AppLaunchParams, type TextEditorHost } from "../apps/app-definition"; @@ -218,6 +219,8 @@ class DashboardController { registerNavigateTools((appId) => this.launchApp(appId)); // roam.* tools launch the Roam app on demand likewise. registerRoamTools((appId) => this.launchApp(appId)); + // timer.* tools launch the Timer app on demand likewise. + registerTimerTools((appId) => this.launchApp(appId)); // apps.* tools mirror the launcher grid and sidebar (launch, focus, close). registerWindowTools({ apps: LAUNCHABLE_APPS,