mirror of
https://git.undefined-dev.fr/Blendman974/faceclaw.git
synced 2026-11-19 21:03:09 +00:00
Voice assistant can set/check/cancel timers without the timer app being already open
This commit is contained in:
1 parent
2562761038
commit
b4728f4f15
5 files changed
+124
-49
No files matched your search
@@ -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<void>,
|
||||
appId: string,
|
||||
unprefixedName: string,
|
||||
args: unknown,
|
||||
): Promise<ToolResult> {
|
||||
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<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
@@ -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<void>,
|
||||
registry: ToolRegistry,
|
||||
unprefixedName: string,
|
||||
args: unknown,
|
||||
): Promise<ToolResult> {
|
||||
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<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function ok(content: string): ToolResult {
|
||||
return { ok: true, content };
|
||||
}
|
||||
|
||||
@@ -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<void>,
|
||||
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<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
return callAppToolWithLaunch(registry, launchApp, "roam", unprefixedName, args);
|
||||
}
|
||||
|
||||
function err(error: string): ToolResult {
|
||||
|
||||
@@ -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<void>,
|
||||
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 ?? {}),
|
||||
);
|
||||
}
|
||||
@@ -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,
|
||||
|
||||
Reference in new issue
Block a user