diff --git a/pkg/app/package.json b/pkg/app/package.json index 85eae2b..ae90c08 100644 --- a/pkg/app/package.json +++ b/pkg/app/package.json @@ -1,6 +1,6 @@ { "name": "@hanzo/app", - "version": "0.3.111", + "version": "0.3.112", "description": "Hanzo's app: the shell and its apps (Home, Chat, Inbox, Contacts, Meet, Calendar, Drive, Board, Work, Bots), once, for every Hanzo surface.", "type": "module", "main": "./dist/index.js", @@ -59,7 +59,7 @@ "peerDependencies": { "@hanzo/ai": ">=0.7.0", "@hanzo/appearance": ">=0.2.15", - "@hanzo/build": ">=0.2.135", + "@hanzo/build": ">=0.2.136", "@hanzo/composer": ">=0.2.0", "@hanzo/design": ">=0.5.39", "@hanzo/event": ">=0.3.63", diff --git a/pkg/app/src/lib/local-fs.test.ts b/pkg/app/src/lib/local-fs.test.ts index 001d738..c20199c 100644 --- a/pkg/app/src/lib/local-fs.test.ts +++ b/pkg/app/src/lib/local-fs.test.ts @@ -37,17 +37,100 @@ describe('This computer for Dev (hanzo-inc/app issues 309 and 310)', () => { ]) }) - it('runs the dev agent in the folder and reads the desktop’s own answer', async () => { - const invoke = vi.fn(async () => ({ session_id: 'local_1', output: 'Edited README.md', success: true })) + it('runs the dev agent in the folder and reads the desktop’s own answer: what it said, and what it did', async () => { + const invoke = vi.fn(async (cmd: string) => + cmd === 'local_workspace_run_dev' ? { session_id: 'local_1', output: 'Edited README.md', process: "exec bash -lc 'ls ~'", success: true } : null, + ) desktop(invoke) - await expect(desktopLocal()!.run('/Users/me/api', 'Fix the typo')).resolves.toEqual({ ok: true, output: 'Edited README.md' }) + await expect(desktopLocal()!.run('/Users/me/api', 'Fix the typo')).resolves.toEqual({ ok: true, output: 'Edited README.md', process: "exec bash -lc 'ls ~'" }) expect(invoke).toHaveBeenCalledWith('local_workspace_run_dev', { workspacePath: '/Users/me/api', prompt: 'Fix the typo' }) - await expect(runLocalDev('/Users/me/api', 'x')).resolves.toMatchObject({ sessionId: 'local_1', ok: true }) + await expect(runLocalDev('/Users/me/api', 'x')).resolves.toMatchObject({ sessionId: 'local_1', ok: true, process: "exec bash -lc 'ls ~'" }) }) it('says a run the agent failed is not ok, with what it printed', async () => { - desktop(async () => ({ session_id: 'local_2', output: 'error: not signed in', success: false })) - await expect(desktopLocal()!.run('/Users/me/api', 'x')).resolves.toEqual({ ok: false, output: 'error: not signed in' }) + desktop(async (cmd) => (cmd === 'local_workspace_run_dev' ? { session_id: 'local_2', output: 'error: not signed in', success: false } : null)) + await expect(desktopLocal()!.run('/Users/me/api', 'x')).resolves.toEqual({ ok: false, output: 'error: not signed in', process: '' }) + }) + + it('hears the desktop fetch the agent while a run waits, and stops listening once it is done', async () => { + let heard: ((e: { payload: { percent: number } }) => void) | null = null + const calls: string[] = [] + const invoke = vi.fn(async (cmd: string) => { + calls.push(cmd) + if (cmd === 'plugin:event|listen') return 7 + if (cmd === 'local_workspace_run_dev') { + heard?.({ payload: { percent: 40 } }) + heard?.({ payload: { percent: 100 } }) + return { session_id: 'local_3', output: 'Done.', success: true } + } + return null + }) + desktop(invoke) + const internals = (globalThis as unknown as { window: { __TAURI_INTERNALS__: Record } }).window.__TAURI_INTERNALS__ + internals.transformCallback = (cb: (e: { payload: { percent: number } }) => void) => { + heard = cb + return 1 + } + const percents: number[] = [] + await desktopLocal()!.run('/Users/me/api', 'x', (p) => percents.push(p)) + expect(percents).toEqual([40, 100]) + expect(invoke).toHaveBeenCalledWith('plugin:event|listen', expect.objectContaining({ event: 'local-dev-setup' })) + expect(invoke).toHaveBeenCalledWith('plugin:event|unlisten', { event: 'local-dev-setup', eventId: 7 }) + expect(calls.indexOf('plugin:event|listen')).toBeLessThan(calls.indexOf('local_workspace_run_dev')) + }) + + it('runs without listening in a window that has no event plugin', async () => { + const invoke = vi.fn(async (cmd: string) => (cmd === 'local_workspace_run_dev' ? { session_id: 'local_4', output: 'Done.', success: true } : null)) + desktop(invoke) + await expect(desktopLocal()!.run('/Users/me/api', 'x', () => {})).resolves.toMatchObject({ ok: true }) + expect(invoke).not.toHaveBeenCalledWith('plugin:event|listen', expect.anything()) + }) + + it('adds the project folders the desktop finds, after the ones opened, and says when they arrive', async () => { + desktop( + async (cmd) => + cmd === 'local_workspace_discover' + ? [ + { name: 'shop', path: '/Users/me/work/shop', modified: 900 }, + { name: 'site', path: '/Users/me/site', modified: 800 }, + { name: 'blog', path: '/Users/me/blog', modified: 700 }, + ] + : null, + { 'hanzo.local_workspaces': two }, + ) + const here = desktopLocal()! + // Before the answer: only the ones opened. + expect(here.folders().map((f) => f.name)).toEqual(['site', 'api']) + const arrived = vi.fn() + const stop = here.changed!(arrived) + await vi.waitFor(() => expect(arrived).toHaveBeenCalled()) + // A folder found that was also opened is listed once, as opened; the rest follow in the order found. + expect(here.folders()).toEqual([ + { path: '/Users/me/site', name: 'site', used: 100 }, + { path: '/Users/me/api', name: 'api', used: 300 }, + { path: '/Users/me/work/shop', name: 'shop' }, + { path: '/Users/me/blog', name: 'blog' }, + ]) + stop() + }) + + it('opens a folder that was only found, so the next run there is the one used last', async () => { + desktop(async (cmd) => (cmd === 'local_workspace_discover' ? [{ name: 'shop', path: '/Users/me/work/shop', modified: 900 }] : null), { 'hanzo.local_workspaces': two }) + const here = desktopLocal()! + await vi.waitFor(() => expect(here.folders().map((f) => f.name)).toContain('shop')) + here.use('/Users/me/work/shop') + expect(getLocalWorkspaces().map((w) => w.path)).toContain('/Users/me/work/shop') + expect(here.folders().find((f) => f.name === 'shop')!.used).toBeGreaterThan(300) + }) + + it('offers only the folders it opened when the desktop cannot look', async () => { + desktop(async (cmd) => { + if (cmd === 'local_workspace_discover') throw new Error('Command not found') + return null + }, { 'hanzo.local_workspaces': two }) + const here = desktopLocal()! + await Promise.resolve() + expect(here.folders().map((f) => f.name)).toEqual(['site', 'api']) }) it('takes a folder from the window’s own dialog, keeps it, and makes it the one used last', async () => { diff --git a/pkg/app/src/lib/local-fs.ts b/pkg/app/src/lib/local-fs.ts index 516dd28..bddb07c 100644 --- a/pkg/app/src/lib/local-fs.ts +++ b/pkg/app/src/lib/local-fs.ts @@ -9,9 +9,10 @@ * 5. Run local coding tasks against the workspace with `hanzoai/dev`. * * Dev's This computer is `desktopLocal()` (hanzo-inc/app issues 309 and 310): on Hanzo Desktop it hands @hanzo/build - * the folders opened here, the window's folder dialog and the `dev` runner, so a run works in a folder with no - * repository. Auto there is the folder used most recently (`lastOpenedAt`). A browser page has no folder of its own, - * and gets none. + * the folders opened here and the project folders the desktop finds on the computer, the window's folder dialog and + * the `dev` runner, so a run works in a folder with no repository. Auto there is the folder the ask is about, else + * the one used most recently (`lastOpenedAt`), as @hanzo/build's `route` reads them. A browser page has no folder of + * its own, and gets none. */ import type { Folder, Local } from '@hanzo/build' @@ -280,32 +281,81 @@ export async function deleteLocalPath(path: string): Promise { } /** - * Run local coding task with `dev` CLI. Hanzo Desktop answers `{session_id, output, success}` (its Rust struct's own - * names); `ok` is false only when it said the agent failed. + * Run local coding task with `dev` CLI. Hanzo Desktop answers `{session_id, output, process, success}` (its Rust struct's own + * names): `output` is the agent's final message, `process` what it did on the way. `ok` is false only when it said the agent + * failed. The desktop fetches the agent itself when the computer has none. */ -export async function runLocalDev(workspacePath: string, prompt: string): Promise<{ sessionId: string; output?: string; ok: boolean }> { +export async function runLocalDev(workspacePath: string, prompt: string): Promise<{ sessionId: string; output?: string; process?: string; ok: boolean }> { if (isTauri()) { - const r = await tauriInvoke<{ sessionId?: string; session_id?: string; output?: string; success?: boolean }>('local_workspace_run_dev', { + const r = await tauriInvoke<{ sessionId?: string; session_id?: string; output?: string; process?: string; success?: boolean }>('local_workspace_run_dev', { workspacePath, prompt, }) - return { sessionId: r?.sessionId ?? r?.session_id ?? '', output: r?.output, ok: r?.success !== false } + return { sessionId: r?.sessionId ?? r?.session_id ?? '', output: r?.output, process: r?.process, ok: r?.success !== false } } throw new Error('Local Dev agent execution requires Hanzo Desktop') } +/** A project folder the desktop found on the computer, as `local_workspace_discover` answers it. */ +interface Found { + name: string + path: string + modified: number +} + +/** + * Listen to an event Hanzo Desktop emits, through the window's own IPC (what `@tauri-apps/api/event`'s `listen` does, + * without the package). Answers how to stop listening; a window without the event plugin hears nothing. + */ +async function tauriListen(event: string, handler: (payload: T) => void): Promise<() => void> { + const win = window as unknown as { + __TAURI_INTERNALS__?: { transformCallback?: (cb: (e: { payload: T }) => void) => number; invoke: (cmd: string, args?: Record) => Promise } + __TAURI_EVENT_PLUGIN_INTERNALS__?: { unregisterListener?: (event: string, id: number) => void } + } + const internals = win.__TAURI_INTERNALS__ + if (!internals?.transformCallback) return () => {} + const id = (await internals.invoke('plugin:event|listen', { + event, + target: { kind: 'Any' }, + handler: internals.transformCallback((e) => handler(e.payload)), + })) as number + return () => { + win.__TAURI_EVENT_PLUGIN_INTERNALS__?.unregisterListener?.(event, id) + void internals.invoke('plugin:event|unlisten', { event, eventId: id }).catch(() => {}) + } +} + +/** The event the desktop emits while it fetches Hanzo Dev for a computer that has none: `{ percent }`, 0 to 100. */ +const SETUP_EVENT = 'local-dev-setup' + /** A workspace as Dev's Folder pill reads it. */ const folderOf = (w: LocalWorkspace): Folder => ({ path: w.path, name: w.name, used: w.lastOpenedAt || w.addedAt || 0 }) /** - * This computer, as @hanzo/build's Dev takes it (`Host.local`): the folders Hanzo Desktop opened, its folder dialog, - * and a run of the `dev` agent in one folder (`dev exec -C `, its workspace-write sandbox). Null outside the - * desktop window, so a browser page offers the cloud workspace and the org's machines only. + * This computer, as @hanzo/build's Dev takes it (`Host.local`): the folders Hanzo Desktop opened and the project folders it + * finds on the computer (asked once, when this is made, and announced through `changed` when they arrive), its folder + * dialog, and a run of the `dev` agent in one folder (`dev exec -C `, its workspace-write sandbox). Null outside + * the desktop window, so a browser page offers the cloud workspace and the org's machines only. */ export function desktopLocal(): Local | null { if (!isTauri()) return null + let found: Folder[] = [] + tauriInvoke('local_workspace_discover').then( + (list) => { + found = (Array.isArray(list) ? list : []).filter((f) => f && typeof f.path === 'string' && f.path).map((f) => ({ path: f.path, name: f.name || f.path.split(/[/\\]/).filter(Boolean).pop() || f.path })) + if (found.length) notify() + }, + () => { + // A desktop that cannot look offers the folders it opened, as before. + }, + ) return { - folders: () => getLocalWorkspaces().map(folderOf), + folders: () => { + const opened = getLocalWorkspaces().map(folderOf) + const known = new Set(opened.map((f) => f.path)) + return [...opened, ...found.filter((f) => !known.has(f.path))] + }, + changed: subscribeLocalFs, pick: async () => { const ws = await pickLocalFolder({ browser: false }) return ws ? folderOf(ws) : null @@ -315,9 +365,14 @@ export function desktopLocal(): Local | null { if (ws) setActiveWorkspace(ws) else addLocalWorkspace(path) }, - run: async (folder, prompt) => { - const r = await runLocalDev(folder, prompt) - return { ok: r.ok, output: r.output ?? '' } + run: async (folder, prompt, setup) => { + const stop = setup ? await tauriListen<{ percent: number }>(SETUP_EVENT, (p) => setup(p.percent)).catch(() => () => {}) : () => {} + try { + const r = await runLocalDev(folder, prompt) + return { ok: r.ok, output: r.output ?? '', process: r.process ?? '' } + } finally { + stop() + } }, } } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a5781b..e85f3a0 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -13,8 +13,8 @@ catalogs: specifier: 0.2.15 version: 0.2.15 '@hanzo/build': - specifier: 0.2.135 - version: 0.2.135 + specifier: 0.2.136 + version: 0.2.136 '@hanzo/composer': specifier: 0.2.0 version: 0.2.0 @@ -137,7 +137,7 @@ importers: version: 0.2.15(react@19.2.8) '@hanzo/build': specifier: 'catalog:' - version: 0.2.135(4ad4e3c852ede235ac12cd2c3e40a847) + version: 0.2.136(4ad4e3c852ede235ac12cd2c3e40a847) '@hanzo/composer': specifier: 'catalog:' version: 0.2.0(@hanzo/gui@8.3.5(react-dom@19.2.8(react@19.2.8))(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8))(react@19.2.8) @@ -161,7 +161,7 @@ importers: version: 1.0.26(react@19.2.8) '@hanzo/studio': specifier: 'catalog:' - version: 0.1.12(ad3ebc400030252a980e00092a1f19b4) + version: 0.1.12(55f61c60f0ef9e74a1ba4544998ebcaa) '@hanzo/tabs': specifier: 'catalog:' version: https://codeload.github.com/hanzo-inc/tabs/tar.gz/26a30cbc5fac232e5b11d5d77939f9573ca0c8d8(@hanzo/gui@8.3.5(react-dom@19.2.8(react@19.2.8))(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8))(@hanzo/ui@8.33.6(patch_hash=55c0ec6cbb062072f0fb16726890e144dc6564d5e5a7e9ce62c5fb86faa38649)(c331da2469a6af62a53d9754cbbbaf69))(lucide-react@1.42.0(react@19.2.8))(react-dom@19.2.8(react@19.2.8))(react@19.2.8) @@ -654,8 +654,8 @@ packages: react: optional: true - '@hanzo/build@0.2.135': - resolution: {integrity: sha512-a6kBC2hWorbPpzgD6cv7NgXbjFQJ4LXHuhV1dwQlBNokqDiEk0YssEWrpDwqJPks+uVkDC8F3HNrOyPwuBrjAQ==} + '@hanzo/build@0.2.136': + resolution: {integrity: sha512-H/pDSB2P22JN/dVQb4EprV6ZO6RIacD0I70ETuIUED4jiFB94CKAmLnj1/3OsSqKesCZ69mTvsqOI9Blb9J5gw==} engines: {node: '>=22.18'} peerDependencies: '@hanzo/composer': '>=0.2.0' @@ -3884,7 +3884,7 @@ snapshots: optionalDependencies: react: 19.2.8 - '@hanzo/build@0.2.135(4ad4e3c852ede235ac12cd2c3e40a847)': + '@hanzo/build@0.2.136(4ad4e3c852ede235ac12cd2c3e40a847)': dependencies: '@hanzo/composer': 0.2.0(@hanzo/gui@8.3.5(react-dom@19.2.8(react@19.2.8))(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8))(react@19.2.8) '@hanzo/gui': 8.3.5(react-dom@19.2.8(react@19.2.8))(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8) @@ -4037,9 +4037,9 @@ snapshots: '@hanzogui/lucide-icons-2': 8.3.5(react-dom@19.2.8(react@19.2.8))(react-native-svg@15.15.5(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8))(react@19.2.8) react: 19.2.8 - '@hanzo/studio@0.1.12(ad3ebc400030252a980e00092a1f19b4)': + '@hanzo/studio@0.1.12(55f61c60f0ef9e74a1ba4544998ebcaa)': dependencies: - '@hanzo/build': 0.2.135(4ad4e3c852ede235ac12cd2c3e40a847) + '@hanzo/build': 0.2.136(4ad4e3c852ede235ac12cd2c3e40a847) '@hanzo/composer': 0.2.0(@hanzo/gui@8.3.5(react-dom@19.2.8(react@19.2.8))(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8))(react@19.2.8) '@hanzo/gui': 8.3.5(react-dom@19.2.8(react@19.2.8))(react-native@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(@react-native/metro-config@0.87.1(@babel/core@7.29.7(supports-color@8.1.1))(supports-color@8.1.1))(@types/react@19.2.18)(react@19.2.8)(supports-color@8.1.1))(react@19.2.8) '@hanzo/i18n': 0.5.5(react@19.2.8) diff --git a/pnpm-workspace.yaml b/pnpm-workspace.yaml index ad75f4e..9d54b15 100644 --- a/pnpm-workspace.yaml +++ b/pnpm-workspace.yaml @@ -33,7 +33,7 @@ minimumReleaseAge: 0 catalog: '@hanzo/ai': 0.7.0 '@hanzo/appearance': 0.2.15 - '@hanzo/build': 0.2.135 + '@hanzo/build': 0.2.136 '@hanzo/composer': 0.2.0 '@hanzo/design': 0.5.39 '@hanzo/event': 0.3.67