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

+67
View File
@@ -0,0 +1,67 @@
'use strict';
const { Input } = require('enquirer');
// Enquirer owns the editable line and its rendering. The remote's single
// keypress listener routes keys here so starting/closing a prompt cannot leak
// an Enter, arrow or Esc into the watch controls, even within one input chunk.
class MessageInput extends Input {
start() {} // Keep terminal ownership (raw mode and signals) with the remote.
cursorHide() {
if (this.cursorHidden) return;
this.cursorHidden = true;
this.stdout.write('\x1b[?25l');
const show = () => this.cursorShow();
// Enquirer's default cursor cleanup installs signal handlers that call
// process.exit immediately. Let the remote restore raw mode first instead.
process.once('exit', show);
this.once('close', () => { show(); process.removeListener('exit', show); });
}
render() {
this.paint = (this.paint || Promise.resolve()).then(() => super.render());
return this.paint;
}
home() { return this.first(); }
end() { return this.last(); }
last() { this.cursor = this.input.length; return this.render(); }
async cancel() {
this.input = this.value = '';
this.cursor = 0;
this.state.clipboard = [];
return super.cancel();
}
}
function createEditor(action, input, output) {
const prompt = new MessageInput({
name: action, message: action === 'text' ? 'Foreground text' : 'Assistant message',
footer: 'Enter: send | Esc: discard | Ctrl-C: quit',
stdin: input, stdout: output, initial: '',
validate: value => value.length > 8000 ? 'Maximum 8000 characters.' : value.includes('\0') ? 'NUL is not allowed.' : true,
});
let resolve, reject, closing = false;
const result = new Promise((yes, no) => { resolve = yes; reject = no; });
prompt.once('submit', text => {
resolve(text);
prompt.input = prompt.value = '';
prompt.state.clipboard = [];
});
prompt.once('cancel', () => resolve(null));
// Queue keys during async initialization, including typing/paste that arrives
// in the same terminal chunk as the mode-switching 'i' or 'a'.
let keys = prompt.initialize().catch(reject);
return {
result,
keypress(text, key) {
keys = keys.then(async () => {
if (closing || prompt.state.closed) return;
await prompt.keypress(text, key);
await prompt.paint;
}).catch(reject);
},
cancel() {
closing = true;
return keys.then(() => prompt.state.closed ? undefined : prompt.cancel()).catch(reject);
},
};
}
module.exports = { createEditor };
+204
View File
@@ -0,0 +1,204 @@
#!/usr/bin/env node
'use strict';
const net = require('node:net');
const readline = require('node:readline');
const fs = require('node:fs');
const GESTURES = new Set(['click', 'double-click', 'long-press', 'short-then-long-press', 'scroll-up', 'scroll-down',
'swipe-up', 'swipe-down', 'swipe-left', 'swipe-right']);
const ALIASES = { tap: 'click', 'double-tap': 'double-click', up: 'swipe-up', down: 'swipe-down', left: 'swipe-left', right: 'swipe-right' };
const HELP = `Usage: faceclaw-input [--host HOST] [--port PORT] [--token-file FILE] COMMAND
input GESTURE [--source watch|ring] Send one gesture (default source: watch)
text [-n] MESSAGE Type into the foreground window (-n: no Enter)
assistant MESSAGE Send a message to the voice assistant
interactive Use this terminal as a watch input device
Gestures: tap, double-tap, long-press, short-then-long-press,
up, down, left, right, scroll-up, scroll-down
Interactive: arrows = watch directions; Enter = tap; Esc = double-tap;
Tab = long-press; i = compose text; a = compose assistant message;
Enter sends a draft; Esc discards it; Ctrl-C quits in either mode.
Token: FACECLAW_TOKEN or --token-file FILE. Host/port default to 127.0.0.1:8791
(or FACECLAW_HOST/FACECLAW_PORT). Android USB: adb forward tcp:8791 tcp:8791
Keep Faceclaw running. Each token needs the permission for its command.
`;
function parseArgs(args, env = process.env) {
const options = { host: env.FACECLAW_HOST || '127.0.0.1', port: Number(env.FACECLAW_PORT || 8791), token: env.FACECLAW_TOKEN || '', source: 'watch' };
const positional = [];
let noSubmit = false;
for (let i = 0; i < args.length; i++) {
const arg = args[i];
if (arg === '--') { positional.push(...args.slice(i + 1)); break; }
if (arg === '-n') { noSubmit = true; continue; }
if (arg === '--help' || arg === '-h') return { help: true };
if (['--host', '--port', '--token-file', '--source'].includes(arg)) {
const value = args[++i];
if (!value || value.startsWith('--')) throw new Error(`Missing value for ${arg}.`);
if (arg === '--port') options.port = Number(value);
else if (arg === '--token-file') options.token = fs.readFileSync(value, 'utf8').trim();
else options[arg.slice(2)] = value;
} else if (arg.startsWith('--')) throw new Error(`Unknown option: ${arg}`);
else positional.push(arg);
}
if (!Number.isInteger(options.port) || options.port < 1 || options.port > 65535) throw new Error('Invalid port.');
if (!/^fc1_[a-f0-9]{64}$/.test(options.token)) throw new Error('Set FACECLAW_TOKEN or --token-file to a generated Faceclaw token.');
if (!['watch', 'ring'].includes(options.source)) throw new Error('Source must be watch or ring.');
const [command, ...values] = positional;
if (noSubmit && command !== 'text') throw new Error('-n is only valid with text.');
if (command === 'interactive') {
if (values.length || options.source !== 'watch') throw new Error('Interactive mode uses watch input and takes no message.');
return { options, interactive: true };
}
let payload;
if (command === 'input') {
const gesture = ALIASES[values[0]] || values[0];
if (values.length !== 1 || !GESTURES.has(gesture)) throw new Error('Expected one valid gesture. See --help.');
if (options.source === 'ring' && gesture.startsWith('swipe-')) throw new Error('Ring input uses scroll-up/down; directions require watch input.');
payload = { action: 'input', gesture, source: options.source };
} else if (command === 'text' || command === 'assistant') {
const text = values.join(' ');
if (!text.trim() || text.length > 8000 || text.includes('\0')) throw new Error('Message must contain 1–8000 characters without NUL.');
payload = { action: command, text, ...(noSubmit ? { submit: false } : {}) };
} else throw new Error('Expected input, text, assistant or interactive. See --help.');
return { options, payload };
}
function send(options, payload, signal) {
return new Promise((resolve, reject) => {
const socket = net.createConnection({ host: options.host, port: options.port });
let buffer = Buffer.alloc(0), settled = false;
const abort = () => finish(new Error('Request cancelled.'));
const deadline = setTimeout(() => finish(new Error('Faceclaw request timed out.')), 6500);
function finish(error, value) {
if (settled) return;
settled = true; clearTimeout(deadline); signal?.removeEventListener('abort', abort); socket.destroy();
if (error) reject(error); else resolve(value);
}
socket.on('connect', () => socket.write(JSON.stringify({ ...payload, version: 1, token: options.token }) + '\n'));
socket.on('error', error => finish(new Error(`Could not reach Faceclaw: ${error.code || 'connection error'}. Check the app, host address and USB forwarding or Tailscale.`)));
socket.on('data', chunk => {
buffer = Buffer.concat([buffer, chunk]);
if (buffer.length > 65536) return finish(new Error('Oversized Faceclaw response.'));
const end = buffer.indexOf(10);
if (end < 0) return;
let response;
try { response = JSON.parse(buffer.subarray(0, end).toString('utf8')); }
catch { return finish(new Error('Invalid Faceclaw response.')); }
if (response?.ok !== true) {
const error = new Error(response?.message || 'Faceclaw rejected the request.');
error.code = response?.error;
return finish(error);
}
finish(null, response);
});
socket.on('end', () => { if (!settled) finish(new Error('Faceclaw closed the connection without a response.')); });
signal?.addEventListener('abort', abort, { once: true });
if (signal?.aborted) abort();
});
}
function keyGesture(key) {
return { up: 'swipe-up', down: 'swipe-down', left: 'swipe-left', right: 'swipe-right',
return: 'click', enter: 'click', escape: 'double-click', tab: 'long-press' }[key?.name];
}
async function interactive(options, input = process.stdin, output = process.stdout, transmit = send) {
if (!input.isTTY || typeof input.setRawMode !== 'function') throw new Error('Interactive mode requires a terminal.');
const wasRaw = !!input.isRaw;
readline.emitKeypressEvents(input, { escapeCodeTimeout: 75 });
const controls = 'Arrows: directions | Enter: tap | Esc: double-tap | Tab: long-press | i: text | a: assistant | Ctrl-C: quit\n';
output.write(controls);
input.setRawMode(true);
await new Promise(resolve => {
let active = true, sending = false, connected = false;
let heartbeat = null, editor = null;
const deferredStatus = [];
const status = message => editor ? deferredStatus.push(message) : output.write(message);
const queue = [];
const abort = new AbortController();
const stop = () => {
if (!active) return;
active = false; queue.length = 0; abort.abort();
if (heartbeat !== null) clearInterval(heartbeat);
input.removeListener('keypress', onKey); input.removeListener('end', stop);
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) process.removeListener(signal, stop);
Promise.resolve(editor?.cancel()).catch(() => {}).finally(() => {
input.setRawMode(wasRaw); input.pause(); output.write('\n'); resolve();
});
};
const reportFailure = error => {
if (!active) return;
const disconnected = !['forbidden', 'locked', 'unavailable', 'bad_request', 'failed'].includes(error.code);
if (disconnected) connected = false;
queue.length = 0;
status(`\n${error.message}${disconnected ? ' Waiting for connection; Ctrl-C to quit.' : ''}\n`);
};
const checkConnection = async () => {
if (!active || sending || editor) return;
sending = true;
try {
await transmit(options, { action: 'ping' }, abort.signal);
if (!active) return;
if (!connected) status('Connected.\n');
connected = true;
} catch (error) { reportFailure(error); }
finally { sending = false; if (active && connected && queue.length) void drain(); }
};
const drain = async () => {
if (sending || !connected) return;
sending = true;
try {
while (active && queue.length) {
const payload = queue.shift();
await transmit(options, payload, abort.signal);
if (active && payload.action !== 'input') status(payload.action === 'text' ? 'Sent to foreground window.\n' : 'Sent to assistant.\n');
}
} catch (error) { reportFailure(error); }
finally { sending = false; }
};
const compose = action => {
try {
editor = require('./faceclaw-input-editor.cjs').createEditor(action, input, output);
} catch (error) { output.write(`Could not open editor: ${error.message}\nRun npm install in the Faceclaw checkout.\n`); return; }
editor.result.then(text => {
editor = null;
if (!active) return;
for (const message of deferredStatus.splice(0)) output.write(message);
if (text === null) output.write('Draft discarded.\n');
else if (!text.trim()) output.write('Empty draft; nothing sent.\n');
else if (!connected) output.write('Not sent: connection was lost. Reconnect before sending a new message.\n');
else { queue.push({ action, text }); void drain(); }
output.write(controls);
}).catch(error => {
editor = null;
if (active) { for (const message of deferredStatus.splice(0)) output.write(message); output.write(`Editor failed: ${error.message}\n${controls}`); }
});
};
const onKey = (text, key) => {
if (key?.ctrl && key.name === 'c') return stop();
if (editor) { editor.keypress(text, key); return; }
// Node marks a standalone Esc as meta=true as well as name='escape'.
if (key?.ctrl || (key?.meta && key.name !== 'escape')) return;
if (connected && !key?.shift && (key?.name === 'i' || key?.name === 'a')) {
compose(key.name === 'i' ? 'text' : 'assistant'); return;
}
const gesture = keyGesture(key);
// Bound key repeat lag; discard excess while the remote is slow.
if (gesture && connected && queue.length < 16) { queue.push({ action: 'input', gesture, source: 'watch' }); void drain(); }
};
input.on('keypress', onKey); input.on('end', stop);
for (const signal of ['SIGINT', 'SIGTERM', 'SIGHUP']) process.on(signal, stop);
input.resume();
heartbeat = setInterval(() => { void checkConnection(); }, 3000);
output.write('Connecting...\n');
void checkConnection();
});
}
async function main() {
try {
const args = parseArgs(process.argv.slice(2));
if (args.help) return process.stdout.write(HELP);
if (args.interactive) await interactive(args.options);
else { await send(args.options, args.payload); process.stdout.write('Sent.\n'); }
} catch (error) { process.stderr.write(`${error.message}\n`); process.exitCode = 1; }
}
module.exports = { parseArgs, send, keyGesture, interactive };
if (require.main === module) void main();
+162
View File
@@ -0,0 +1,162 @@
# Faceclaw input tokens and CLI
In **Settings → API Keys → Input tokens**, choose **Create token**, give it a
name, select its permissions, and choose **Generate token**. Copy or save the
secret before leaving the result menu: Faceclaw stores only its SHA-256 hash.
Select an existing token to change permissions or revoke it. A token with no
permissions is disabled. Up to 32 tokens can exist at once.
Permissions are independent:
- **Provide input** simulates watch or ring gestures, including navigation,
taps and the system menu. It has the same reach as those input devices.
- **Text to foreground window** delivers text to the selected app's text input.
- **Text to voice assistant** submits a query to the configured assistant.
Text delivery is rejected while the glasses are locked. Gestures pass through
the existing lock-screen and sleep behavior. The assistant must be configured;
the selected window must accept text. Android requires connected glasses; iOS
also allows the active phone preview. An assistant acknowledgment means the
query was submitted, not that the assistant has finished answering.
## Connect
While at least one token exists, Faceclaw listens on **127.0.0.1:8791** and,
by default, detected **Tailscale addresses**. It never binds a wildcard or a
Wi-Fi/LAN address. Under **Input tokens → Listening interfaces**, choose:
- **Localhost + Tailscale (auto)**: detect a named Tailscale tunnel or a tunnel
with Tailscale's dedicated IPv6 prefix; bind its tailnet IPv4/IPv6 addresses.
- **Localhost / USB only**: disable remote network access.
- A specific **tunnel interface** (such as `tun0` or `utun3`): use this when
automatic identification is unavailable, including IPv4-only VPN setups.
Automatic detection uses the prefixes in [Tailscale's address documentation](https://tailscale.com/docs/concepts/ip-and-dns-addresses).
A CGNAT IPv4 address by itself is not sufficient to identify Tailscale. The
interface picker shows names and addresses of tunnel interfaces; choose the
one belonging to Tailscale. Physical Wi-Fi, Ethernet, and cellular interfaces
are excluded. Interface selection is saved; addresses are refreshed every
three seconds after a tunnel connects, disconnects, or changes address.
Localhost remains available even when the selected tunnel is absent.
**Connection information** shows the actual listening addresses. From a
computer on your tailnet, pass the phone's displayed Tailscale address:
```sh
node scripts/faceclaw-input.cjs --host 100.x.y.z interactive
```
Apps on the same phone can connect directly to localhost. From a computer,
you can also forward the port over USB:
```sh
# Android (ADB must already be paired/authorized)
adb forward tcp:8791 tcp:8791
# iOS, using libimobiledevice's iproxy with a trusted, connected phone
iproxy 8791 8791
```
Keep Faceclaw running; iOS
can suspend it when backgrounded without an active glasses session. Revoking
the last token stops the listener.
## Command-line utility
The utility requires Node.js 18+. Run `npm install` in this checkout to install
its [Enquirer](https://github.com/enquirer/enquirer) text editor, then run the
CLI below. For use on another computer, copy both `faceclaw-input.cjs` and
`faceclaw-input-editor.cjs` into one directory and run
`npm install enquirer@2.4.1` there. One-off commands do not load the editor.
```sh
export FACECLAW_TOKEN='fc1_…' # Paste the generated token
node scripts/faceclaw-input.cjs input tap
node scripts/faceclaw-input.cjs input up
node scripts/faceclaw-input.cjs input scroll-down --source ring
node scripts/faceclaw-input.cjs text 'Text for the foreground app'
node scripts/faceclaw-input.cjs text -n 'Type without pressing Enter'
node scripts/faceclaw-input.cjs assistant 'What is on my calendar?'
node scripts/faceclaw-input.cjs interactive
```
`npm run input -- …` is also available. Use `--token-file FILE` instead of the
environment variable to read a token from a file. `--host` and `--port` (or
`FACECLAW_HOST` and `FACECLAW_PORT`) select a forwarded endpoint. Messages
starting with `--` can follow an option terminator, e.g. `text -- '--help'`.
For terminal windows, text normally appends Enter; `text -n` suppresses that
submission, like `echo -n`. Embedded newlines in the message are preserved.
Other apps receive the text unchanged. Assistant messages do not accept `-n`.
One-off commands return a nonzero exit code on rejection or connection failure.
The CLI never automatically replays an input or message after a failure,
since delivery may already have occurred.
Interactive mode connects and authenticates immediately, without waiting for a
keypress. It validates the token using a side-effect-free `ping`, then checks
connectivity periodically while idle. Each action checks its own permission,
so a token with only text or assistant permission can also use composition. If a request fails, it reports
the error, drops queued keys, and keeps trying to reconnect until Ctrl-C.
It never replays a possibly-delivered gesture. Each request uses a fresh socket,
kept open until the reply arrives (including when using USB forwarding).
Update the phone app alongside the CLI so it supports `ping`.
Interactive mode maps arrow keys to watch directions, Enter to tap, Esc to
double-tap, and Tab to a complete long-press (press and release). Ctrl-C exits
and restores terminal settings. Esc uses a short disambiguation delay to tell
it apart from arrow-key escape sequences. Events are serialized, with a bounded
queue to prevent excessive key-repeat lag.
### Compose text or an assistant message
From interactive controls, press **i** to compose text for the foreground
window, or **a** to compose a message for the assistant. A labeled Enquirer
prompt replaces the gesture controls until you finish:
- **Enter** sends the draft once and returns to controls. Foreground text uses
the normal submission behavior, including Enter in terminal windows.
- **Esc** discards and clears the draft, then returns to controls.
- **Ctrl-C** discards the draft and quits the utility.
While composing, arrows edit the line; Tab, Enter and Esc never send watch
gestures. The letters `i` and `a` are ordinary text. The editor supports
Left/Right, Home/End, Backspace/Delete, Ctrl-A/E (start/end), Ctrl-W (delete the
previous word), and Ctrl-K (delete to the end). Input is a single line, with
an 8000-character limit; an empty draft sends nothing. Every new draft starts
empty, and no message history is stored. Connection-status output is deferred
while the editor is visible so it cannot corrupt the prompt.
The token needs **Text to foreground window** for `i` and **Text to voice
assistant** for `a`. A permission rejection is reported and returns to controls;
it does not end the interactive session or resend the message.
## Protocol for other apps
Open a TCP connection, send one UTF-8 JSON object terminated by a newline, and
read a newline-terminated JSON reply. Each connection carries one request.
Authentication and current permissions are checked for every request.
```json
{"version":1,"token":"fc1_…","action":"input","gesture":"click","source":"watch"}
{"version":1,"token":"fc1_…","action":"text","text":"hello","submit":false}
{"version":1,"token":"fc1_…","action":"ping","permission":"input"}
{"version":1,"token":"fc1_…","action":"assistant","text":"Hello"}
```
Input gestures: `click`, `double-click`, `long-press`, `short-then-long-press`,
`scroll-up`, `scroll-down`, `swipe-up`, `swipe-down`, `swipe-left`, `swipe-right`.
Source defaults to `watch`; `ring` supports all except the four swipes.
The CLI also accepts `tap`, `double-tap`, `up`, `down`, `left`, `right` aliases.
`submit` is an optional boolean for `text`, defaulting to `true`; `false`
suppresses the terminal app's appended Enter. `ping` validates the token and,
if supplied, the requested `permission`, without dispatching any input or
requiring connected glasses.
Successful reply: `{"ok":true}`. Rejections contain `ok:false`, `error`, and
`message`. Error codes: `bad_request`, `unauthorized`, `forbidden`, `locked`,
`unavailable`, `failed`, `timeout`. Malformed framing closes the connection.
Frames are limited to 64 KiB; text to 8000 JavaScript UTF-16 code units and must
contain non-whitespace text without NUL. Whitespace in valid text is preserved.
Connections have a five-second read deadline and five-second dispatch deadline.
Each listening address serves one connection at a time with a bounded socket backlog.
Token values and request text are never written to the server log.