Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 44 additions & 13 deletions apps/hanzo-desktop/src/__tests__/keybindings.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
createMicLeaseArbitrator,
DESKTOP_GLOBAL_KEYS,
DesktopSettings,
windowKey,
} from '../desktop-shortcuts';

describe('Hanzo Desktop & @hanzo/app keybindings', () => {
Expand All @@ -25,9 +26,9 @@ describe('Hanzo Desktop & @hanzo/app keybindings', () => {
it('lists every core Web + Desktop shortcut in KEYS and DESKTOP_GLOBAL_KEYS', () => {
const macChords = KEYS.map((k) => k.mac);
expect(macChords).toContain('⌘K');
expect(macChords).toContain('⌘N / ⇧⌘O');
expect(macChords).toContain('⌘N / ⌘⇧O');
expect(macChords).toContain('⌘B');
expect(macChords).toContain('⇧⌘M');
expect(macChords).toContain('⌘⇧M');
expect(macChords).toContain('⌘,');
expect(macChords).toContain('⌘/');
expect(macChords).toContain('⌘.');
Expand Down Expand Up @@ -176,17 +177,47 @@ describe('Hanzo Desktop & @hanzo/app keybindings', () => {
expect(onBack).toHaveBeenCalledTimes(2);
});

it('verifies patched @hanzo/app wires ⌘N, ⇧⌘O, ⌘B, ⇧⌘M, ⌘, and guest ⌘/ shortcuts', () => {
const appPath = resolve(__dirname, '../../../../node_modules/@hanzo/app/dist/App.js');
const mePath = resolve(__dirname, '../../../../node_modules/@hanzo/app/dist/Me.js');
const appJs = readFileSync(appPath, 'utf8');
const meJs = readFileSync(mePath, 'utf8');

expect(appJs).toContain("(bar && !e.shiftKey && lower === 'n')");
expect(appJs).toContain("(e.shiftKey && lower === 'o')");
expect(appJs).toContain("lower === 'b'");
expect(appJs).toContain("lower === 'm'");
expect(meJs).toContain('if (!host.person)\n return _jsx(Shortcuts,');
it('answers the window keys @hanzo/app leaves to it: ⌘N and ⌘⇧M', () => {
const press = (key: string, more: Partial<KeyboardEvent> = {}) =>
windowKey({
key,
metaKey: false,
ctrlKey: false,
shiftKey: false,
altKey: false,
repeat: false,
isComposing: false,
defaultPrevented: false,
...more,
});
expect(press('n', { metaKey: true })).toBe('new-chat');
expect(press('N', { ctrlKey: true })).toBe('new-chat');
expect(press('M', { metaKey: true, shiftKey: true })).toBe('voice');
expect(press('m', { ctrlKey: true, shiftKey: true })).toBe('voice');
// ⌘⇧O, ⌘B and ⌘, are @hanzo/app's own (keys.js); the window does not answer them twice.
expect(press('o', { metaKey: true, shiftKey: true })).toBeNull();
expect(press('b', { metaKey: true })).toBeNull();
expect(press('n')).toBeNull();
expect(press('n', { metaKey: true, altKey: true })).toBeNull();
expect(press('n', { metaKey: true, repeat: true })).toBeNull();
expect(press('n', { metaKey: true, isComposing: true })).toBeNull();
expect(press('n', { metaKey: true, defaultPrevented: true })).toBeNull();
});

it('runs the patched @hanzo/app: a signed-out window chats with the local engine', () => {
const dist = resolve(__dirname, '../../../../node_modules/@hanzo/app/dist');
const chat = readFileSync(resolve(dist, 'Chat.js'), 'utf8');
const pane = readFileSync(resolve(dist, 'pane.js'), 'utf8');
const setup = readFileSync(resolve(dist, 'Setup.js'), 'utf8');

// The signed-out lane is the signed-in one (history, the picked model, no turn cap).
expect(chat).toContain('HANZO DESKTOP ANSWERS ITS SIGNED-OUT READER ITSELF');
expect(chat).toContain('const sent = anonymous ? wanted || ENSO : wanted;');
expect(chat).toContain('"Select / Download Local Model"');
// Files a signed-out reader attaches are read in the window, not refused.
expect(pane).toContain('localText');
expect(pane).not.toContain('Sign in to attach files');
expect(setup).toContain('const workspace = false;');
});

it('arbitrates cross-window microphone lease so concurrent Hanzo apps never fight over the mic', () => {
Expand Down
7 changes: 7 additions & 0 deletions apps/hanzo-desktop/src/app.css
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,13 @@ html:root {
height: 28px !important;
}

/* Folded, @hanzo/app 0.3 sizes the bar's start to the rail, which leaves no
room for the traffic lights' inset and the toggle, Back and Forward after it:
the start takes what they need, so they never sit over the page's title. */
[data-slot='title-bar-start'] {
min-width: max-content !important;
}

[data-slot='title-bar'] > :nth-child(5) {
margin-inline-end: 136px;
}
Expand Down
25 changes: 25 additions & 0 deletions apps/hanzo-desktop/src/desktop-shortcuts.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,31 @@ export const DESKTOP_GLOBAL_KEYS = [
{ does: 'Hands-free voice toggle', mac: 'Double-tap Fn', pc: 'Double-tap Fn' },
] as const;

/** What a key press asks of the window itself (`windowKey`). */
export type WindowAct = 'new-chat' | 'voice';

/**
* The window's own keys, past the ones @hanzo/app answers (its keys.js: ⌘K,
* ⌘⇧O, ⌘B, ⌘, and the rest): ⌘N opens a new chat, as File ▸ New does in a
* desktop app, and ⌘⇧M turns the composer's microphone on and off. Ctrl on
* Windows and Linux. A press something already answered, a repeat, or one
* still composing a character is not a command. Both keys are in @hanzo/app's
* KEYS (patches/@hanzo__app@0.3.107.patch), so its shortcut list names them.
*/
export function windowKey(
e: Pick<
KeyboardEvent,
'key' | 'metaKey' | 'ctrlKey' | 'shiftKey' | 'altKey' | 'repeat' | 'isComposing' | 'defaultPrevented'
>,
): WindowAct | null {
if (e.defaultPrevented || e.isComposing || e.repeat || e.altKey) return null;
if (!(e.metaKey || e.ctrlKey)) return null;
const key = (e.key ?? '').toLowerCase();
if (!e.shiftKey && key === 'n') return 'new-chat';
if (e.shiftKey && key === 'm') return 'voice';
return null;
}

export function DesktopSettings({
isAuthenticated,
siteUrl = 'https://hanzo.ai',
Expand Down
103 changes: 81 additions & 22 deletions apps/hanzo-desktop/src/host.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { useEffect, useState, useSyncExternalStore, type MouseEvent } from 'react';
import { useEffect, useRef, useState, useSyncExternalStore, type MouseEvent } from 'react';
import { getBrand } from '@hanzo/desktop-brand-config';
import { ORG_KEY } from '@hanzo/event';
import { GuiProvider } from '@hanzo/gui';
import { IamProvider, useIam } from '@hanzo/iam/react';
import { configure, Look, Rooms, type Link, type Router } from '@hanzo/app';
import { App, type Bar } from '@hanzo/app/App';
import { App, type Titlebar } from '@hanzo/app/App';
import { empty } from '@hanzo/app/open';
import { useScheme } from '@hanzo/appearance';
import { Button, Text } from '@hanzo/ui';
import { openUrl } from '@tauri-apps/plugin-opener';
import { ErrorBoundary } from 'react-error-boundary';

Expand All @@ -15,12 +16,14 @@ import {
createFnVoiceController,
createMicLeaseArbitrator,
DesktopSettings,
windowKey,
} from './desktop-shortcuts';
import { keychain, open as openKeychain, written } from './keychain';
import {
buildLocalModelsCatalogResponse,
handleLocalChatCompletion,
LocalAiManager,
openLocalAiManager,
} from './local-ai';
import BenchmarkPage, { LSA_URL, ZEN_27B_TREE_PRESET } from './pages/benchmark';
import { Welcome } from './Welcome';
Expand Down Expand Up @@ -57,24 +60,56 @@ configure({
// The session is in the keychain, never the webview's localStorage: the rooms
// read the token, its expiry and its claims there.
session: keychain,
// THE WINDOW IS THE APP, so there is no site behind it to send a guest to:
// the rooms draw their one room (Chat's free lane) for a signed-out reader,
// and sign-in is one control in the app rather than a wall in front of it.
// Patched into @hanzo/app (patches/@hanzo__app@0.2.63.patch, Orgs.js).
guest: true,
} as Parameters<typeof configure>[0]);
});

// THE WINDOW IS THE APP, so there is no site behind it to send a guest to: a
// signed-out reader who chose "Not now" gets Chat, answered by the local engine
// (the fetch bridge below), and sign-in is one control in the app rather than a
// wall in front of it. @hanzo/app's Gate lets a reader without a session into
// Chat when `__hanzoAnonChat` is set (`guest()`, `stayGuest()`) and the address
// is /chat. A bundled window opens on its own index (windows/mod.rs `main_url`),
// which is the app's front door, so that address is named for what it is.
if (window.location.pathname === '/' || window.location.pathname === '/index.html') {
window.history.replaceState(null, '', `/chat${window.location.search}${window.location.hash}`);
}

/** The bar's own control: the local models and node (local-ai.tsx), from any page. */
function LocalAi() {
return (
<Button
size="xs"
variant="outline"
data-slot="header-local-ai"
aria-label="Local AI & Node"
height={24}
px="$2.5"
rounded="$3"
onPress={() => openLocalAiManager('models')}
>
<Text fontSize="$1" fontWeight="600" color="$ink">
Local AI
</Text>
</Button>
);
}

// THE WINDOW'S BAR IS THE APP'S. There is no browser around this window, so
// the app draws the bar a browser would: the sidebar toggle, Back and Forward
// through the router below. macOS paints its traffic lights over the page's
// top-left corner (tauri.conf.json: titleBarStyle Overlay), so the bar keeps
// 78px clear there; Windows and Linux keep their native title bar above the
// page and need none. The bar drags the window.
const BAR: Bar = {
// page and need none. The bar drags the window. Its controls are the window's:
// Local AI, in place of the minimise, maximise and close a frameless window
// would draw, which the native title bar already has.
const BAR: Titlebar = {
inset: /Mac/.test(navigator.userAgent) ? 78 : 0,
drag: { 'data-tauri-drag-region': true },
controls: <LocalAi />,
};

/** The composer's microphone, on whatever page draws it. */
const findMic = () => document.querySelector('button[data-state][aria-pressed]') as HTMLButtonElement | null;

const IAM = {
serverUrl: API,
proxyBaseUrl: API,
Expand Down Expand Up @@ -470,21 +505,47 @@ function Gate() {
return () => clearTimeout(timer);
}, []);

// A new chat, from the global ⇧⌘I or the window's ⌘N: the composer, empty and
// focused. Signed out, it is the same as "Not now" (a guest, in Chat).
const startChat = () => {
if (!isAuthenticated) {
stayGuest();
setVisiting(true);
}
empty();
window.history.pushState(null, '', '/chat');
requestAnimationFrame(() => {
const field = document.querySelector('[data-slot="composer-field"], textarea') as HTMLElement | null;
field?.focus?.();
});
};
const started = useRef(startChat);
started.current = startChat;

// The window's own keys (desktop-shortcuts `windowKey`): ⌘N and ⌘⇧M.
useEffect(() => {
const onKey = (e: KeyboardEvent) => {
const act = windowKey(e);
if (act === 'new-chat') {
e.preventDefault();
started.current();
} else if (act === 'voice') {
const mic = findMic();
if (!mic || mic.disabled) return;
e.preventDefault();
mic.click();
}
};
window.addEventListener('keydown', onKey);
return () => window.removeEventListener('keydown', onKey);
}, []);

// Wire Tauri desktop global shortcuts ('create-chat' on ⇧⌘I and 'navigate-to-path' from Spotlight)
useEffect(() => {
let unlistenCreate: (() => void) | undefined;
let unlistenNavigate: (() => void) | undefined;

void safeListen('create-chat', () => {
stayGuest();
setVisiting(true);
empty();
window.history.pushState(null, '', '/chat');
requestAnimationFrame(() => {
const field = document.querySelector('[data-slot="composer-field"], textarea') as HTMLElement | null;
field?.focus?.();
});
}).then((fn) => {
void safeListen('create-chat', () => started.current()).then((fn) => {
unlistenCreate = fn ?? undefined;
});

Expand All @@ -506,8 +567,6 @@ function Gate() {
// Wire Desktop `Fn` key push-to-talk (hold Fn) and hands-free voice (double-tap Fn)
// with cross-window / cross-app mic lease arbitration so multiple open Hanzo apps never fight over the mic.
useEffect(() => {
const findMic = () =>
document.querySelector('button[data-state][aria-pressed]') as HTMLButtonElement | null;
const instanceId = `${brand.brand}-${Math.random().toString(36).slice(2, 10)}`;
const lease = createMicLeaseArbitrator({
instanceId,
Expand Down
9 changes: 5 additions & 4 deletions hanzo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ test:
# 2026-07-27 and never re-homed, so since then nothing has asserted it.
#
# It also holds the file to being the ONLY declaration: tauri.conf.json must
# keep POINTING at it rather than restating a number, Cargo.toml must stay the
# inert 0.0.0 the pointer overrides, and apps/hanzo-desktop/package.json must
# declare none (it carried a second copy that agreed only by luck). Each is a
# place a bundle could come to report a version this repo never released.
# keep POINTING at it rather than restating a number, Cargo.toml must repeat
# it exactly (src-tauri/build.rs refuses a build where they differ), and
# apps/hanzo-desktop/package.json must declare none (it carried a second copy
# that agreed only by luck). Each is a place a bundle could come to report a
# version this repo never released.
- name: version-drift
run: |
set -e
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@hanzo/event": "catalog:",
"@hanzo/font": "catalog:",
"@hanzo/gui": "catalog:",
"@hanzo/i18n": "catalog:",
"@hanzo/iam": "catalog:",
"@hanzo/logo": "catalog:",
"@hanzo/pay": "catalog:",
Expand Down
Loading
Loading