Add tokens and faceclaw-input script, which allows using a CLI utility as an input device for the glasses

This commit is contained in:
James Babcock committed 2026-09-20 00:34:58 -07:00
1 parent 313ba85708
commit ccb320c66f
32 files changed
+1785 -12

No files matched your search

+90
View File
@@ -0,0 +1,90 @@
import { remoteTokens, remoteInputStatus, remoteInterfaceSelection, selectRemoteInterface } from '../../remote/service';
import { PERMISSIONS, type Permission } from '../../remote/protocol';
import { isTunnelInterface } from '../../remote/listeners';
import { copyRemoteToken, remoteInterfaces } from '../../native/remote-input';
import { TextViewerLayer } from '../../apps/files/text-viewer';
import { ConfigSettingString, textSettingMenuItem } from '../dashboard-settings';
import { getDefaultSmallFont } from '../../graphics/ui-fonts';
import { drawRightValueMenuItem, type MenuItem } from '../menu';
import { type LayerContext } from '../layers';
import { openSettingsSubMenu } from './settings-panel';
const labels: Record<Permission, string> = { input: 'Provide input', text: 'Text to foreground window', assistant: 'Text to voice assistant' };
const nameSetting = new ConfigSettingString({ id: 'remoteInputNewName', storageKey: 'remoteInput.newName',
label: 'Token name', defaultValue: 'My app', normalize: value => (value ?? '').slice(0, 80) });
function permissionItem(permission: Permission, get: () => Permission[], set: (values: Permission[]) => void): MenuItem {
return { label: labels[permission], onSelect: ctx => {
const current = get();
set(current.includes(permission) ? current.filter(p => p !== permission) : [...current, permission]);
ctx.actions.requestRender();
}, render: ({ image, x, y, width }) => drawRightValueMenuItem(image, getDefaultSmallFont(), x, y, width,
labels[permission], get().includes(permission) ? 'On' : 'Off') };
}
function showError(ctx: LayerContext, error: unknown): void {
ctx.stack.push(new TextViewerLayer(error instanceof Error ? error.message : 'Could not update token.', 'Input tokens'));
}
function tokenDetails(ctx: LayerContext, id: string): void {
const record = remoteTokens.list().find(r => r.id === id);
if (!record) return;
openSettingsSubMenu(ctx, record.name, [
...PERMISSIONS.map(p => permissionItem(p, () => remoteTokens.list().find(r => r.id === id)?.permissions ?? [],
values => remoteTokens.permissions(id, values))),
{ label: 'Revoke token', onSelect: inner => openSettingsSubMenu(inner, `Revoke ${record.name}?`, [
{ label: 'Cancel', onSelect: c => { c.stack.pop(); } },
{ label: 'Revoke', onSelect: c => {
remoteTokens.revoke(id);
c.stack.pop(); c.stack.pop(); c.stack.pop();
openTokens(c);
} },
]) },
]);
}
function createToken(ctx: LayerContext): void {
let permissions: Permission[] = ['input'];
openSettingsSubMenu(ctx, 'Create input token', [
textSettingMenuItem(nameSetting),
...PERMISSIONS.map(p => permissionItem(p, () => permissions, values => { permissions = values; })),
{ label: 'Generate token', onSelect: inner => {
try {
const { token } = remoteTokens.create(nameSetting.get(), permissions);
inner.stack.pop(); inner.stack.pop();
openTokens(inner);
// The secret lives only in this temporary menu; it cannot be recovered after closing it.
openSettingsSubMenu(inner, 'Save your token now', [
{ label: 'Copy token to phone clipboard', onSelect: c => {
try { copyRemoteToken(token); c.stack.push(new TextViewerLayer('Token copied. Save it in the calling app or FACECLAW_TOKEN. The secret cannot be shown again after leaving this menu.', 'Copied')); }
catch (error) { showError(c, error); }
} },
{ label: 'Show token (only available now)', onSelect: c => c.stack.push(new TextViewerLayer(token, 'Input token')) },
{ label: 'Done', onSelect: c => { c.stack.pop(); } },
]);
} catch (error) { showError(inner, error); }
} },
]);
}
function interfaceMenu(ctx: LayerContext): void {
const interfaces = remoteInterfaces().filter(isTunnelInterface);
const names = [...new Set(interfaces.map(item => item.name))];
const choices = [
{ value: 'tailscale', label: 'Localhost + Tailscale (auto)' },
{ value: 'localhost', label: 'Localhost / USB only' },
...names.map(name => ({ value: `interface:${name}`, label: `${name}: ${interfaces.filter(item => item.name === name).map(item => item.address).join(', ')}` })),
];
openSettingsSubMenu(ctx, 'Listening interfaces', choices.map(choice => ({
label: `${choice.label}${remoteInterfaceSelection() === choice.value ? ' *' : ''}`,
onSelect: inner => { selectRemoteInterface(choice.value); inner.stack.pop(); },
})));
}
function openTokens(ctx: LayerContext): void {
openSettingsSubMenu(ctx, 'Input tokens', [
{ label: 'Create token', onSelect: createToken },
{ label: 'Listening interfaces', onSelect: interfaceMenu },
{ label: 'Connection information', onSelect: c => c.stack.push(new TextViewerLayer(
`${remoteInputStatus()}\n\nAndroid USB: adb forward tcp:8791 tcp:8791\n\niOS USB: iproxy 8791 8791\n\nTailscale: use --host with the displayed tunnel IP. Listening interfaces lets you select a tunnel if auto detection fails. Wi-Fi/LAN addresses are never selected.\n\nUse scripts/faceclaw-input.cjs with FACECLAW_TOKEN set. Tokens grant only the selected permissions. Revoking a token takes effect on the next request. Text is blocked while the glasses are locked.`, 'Input API')) },
...remoteTokens.list().map(record => ({ label: record.name, onSelect: (c: LayerContext) => tokenDetails(c, record.id) })),
]);
}
export function remoteInputMenuItem(): MenuItem {
return { label: 'Input tokens', description: 'Allow another app to provide ring/watch input, type into the foreground window, or send messages to the voice assistant. Create tokens with individual permissions and revoke them here.', onSelect: openTokens };
}
+2
View File
@@ -1,3 +1,4 @@
import { remoteInputMenuItem } from "./remote-input-menu";
import { knownFolders } from "@nativescript/core";
import { getDefaultSmallFont } from "../../graphics/ui-fonts";
import type { GrayImage } from "../../graphics/image";
@@ -144,6 +145,7 @@ function settingsSections(): SettingsSection[] {
{
label: "API Keys",
items: [
remoteInputMenuItem(),
textSettingMenuItem(elevenLabsApiKeySetting),
textSettingMenuItem(openAiApiKeySetting),
textSettingMenuItem(sonioxApiKeySetting),
+3 -3
View File
@@ -137,7 +137,7 @@ export type ShellWindow = {
* Deliver a text string to the window (e.g. finalized voice input). Optional:
* only windows that consume typed text (the terminal) implement it.
*/
receiveTextInput?: (text: string) => void;
receiveTextInput?: (text: string, options?: { submit?: boolean }) => void;
/** Foreground state changed: this window's surface is (not) the visible one. */
setForeground?: (foreground: boolean) => void;
/**
@@ -1105,8 +1105,8 @@ class Shell {
}
/** Deliver a text string to the foreground window (e.g. finalized voice input). */
sendTextToForegroundWindow(text: string): void {
this.foregroundWindow()?.receiveTextInput?.(text);
sendTextToForegroundWindow(text: string, options?: { submit?: boolean }): void {
this.foregroundWindow()?.receiveTextInput?.(text, options);
}
/**
+3 -3
View File
@@ -20,7 +20,7 @@ export type WorkerAppMessage =
| { type: "resize-window"; windowId: string; viewport: { width: number; height: number } }
| { type: "close-window"; windowId: string }
| { type: "input"; windowId: string; event: unknown; frameId: number; focused: boolean }
| { type: "text-input"; windowId: string; text: string }
| { type: "text-input"; windowId: string; text: string; submit?: boolean }
| { type: "render"; windowId: string; focused: boolean }
| { type: "foreground"; windowId: string; foreground: boolean; focused: boolean }
/**
@@ -441,8 +441,8 @@ export class WorkerAppHost {
requestRender: () => {
this.post({ type: "render", windowId: spec.windowId, focused: shell.isWindowFocused(spec.windowId) });
},
receiveTextInput: (text) => {
this.post({ type: "text-input", windowId: spec.windowId, text });
receiveTextInput: (text, options) => {
this.post({ type: "text-input", windowId: spec.windowId, text, ...(options?.submit === undefined ? {} : { submit: options.submit }) });
},
setForeground: (foreground) => {
this.options.setSurfaceVisible(surfaceId, foreground);