Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { EnvironmentId, ThreadId } from "@akeru/contracts";
import { useEffect } from "react";
export type HardwareKeyboardCommand = "newTask" | "focusSearch" | "back" | "toggleSidebar";
type CommandHandler = () => boolean | void;
const handlers = new Map<HardwareKeyboardCommand, Set<CommandHandler>>();
const registrationListeners = new Set<() => void>();
let registrationVersion = 0;
/**
* Registers a context-specific hardware-keyboard action. The most recently mounted handler gets
* the first chance to consume the command, allowing focused screens to override app defaults.
*/
export function useHardwareKeyboardCommand(
command: HardwareKeyboardCommand,
handler: CommandHandler,
): void {
useEffect(() => {
const commandHandlers = handlers.get(command) ?? new Set<CommandHandler>();
commandHandlers.add(handler);
handlers.set(command, commandHandlers);
registrationVersion += 1;
registrationListeners.forEach((listener) => listener());
return () => {
commandHandlers.delete(handler);
if (commandHandlers.size === 0) handlers.delete(command);
registrationVersion += 1;
registrationListeners.forEach((listener) => listener());
};
}, [command, handler]);
}
export function getRegisteredHardwareKeyboardCommands(): ReadonlySet<HardwareKeyboardCommand> {
return new Set(handlers.keys());
}
export function getHardwareKeyboardCommandRegistrationVersion(): number {
return registrationVersion;
}
export function subscribeToHardwareKeyboardCommandRegistrations(listener: () => void): () => void {
registrationListeners.add(listener);
return () => registrationListeners.delete(listener);
}
export function dispatchHardwareKeyboardCommand(command: HardwareKeyboardCommand): boolean {
const commandHandlers = handlers.get(command);
if (!commandHandlers) return false;
// Visit newest handlers first without ES2023 APIs absent from Hermes.
const newestHandlers = [...commandHandlers];
for (let index = newestHandlers.length - 1; index >= 0; index -= 1) {
const handler = newestHandlers[index]!;
if (handler() !== false) return true;
}
return false;
}
export function parseActiveThreadPath(pathname: string): {
readonly environmentId: EnvironmentId;
readonly threadId: ThreadId;
} | null {
const match = /^\/threads\/([^/]+)\/([^/]+)(?:\/|$)/.exec(pathname);
if (!match?.[1] || !match[2]) return null;
try {
return {
environmentId: EnvironmentId.make(decodeURIComponent(match[1])),
threadId: ThreadId.make(decodeURIComponent(match[2])),
};
} catch {
return null;
}
}