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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@hanzo/build",
"version": "0.2.135",
"version": "0.2.136",
"type": "module",
"license": "Apache-2.0 OR MIT",
"description": "Hanzo Build — say what you want, watch it built, deployed and running. The builder as a component, mounted by the Hanzo app at hanzo.ai.",
Expand Down
38 changes: 33 additions & 5 deletions src/e2e/host.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@
* ?at=<path> the address to start at
* ?local=<n> the host has the computer (Hanzo Desktop's `local`), with n folders opened already; its folder dialog
* answers /Users/dave/picked, and every run it is asked for is written out under the sidebar (Runs on this computer)
* ?folders=a,b instead of f1..fn: the computer's folders by name, the last one used most recently
* ?found=a,b folders the computer finds a moment after the first read (it says so through `changed`), never opened in Hanzo
* ?setup=1 the first run fetches the agent: it says 40% and waits for "Finish setup" before it says 100% and runs
*/
import '@hanzo/font/css'
import '@hanzo/ui/theme.css'
Expand All @@ -30,23 +33,42 @@ const ORG = 'acme'
const bearer = () => window.localStorage.getItem('hanzo_iam_access_token')

/** A computer with `n` folders opened (the last one used most recently), as Hanzo Desktop's `local` answers. */
function computer(n: number, ran: (line: string) => void): Local {
let folders: Folder[] = Array.from({ length: n }, (_, i) => ({ path: `/Users/dave/f${i + 1}`, name: `f${i + 1}`, used: i + 1 }))
function computer(n: number, ran: (line: string) => void, names: string[], found: string[], setup: { finish: () => void } | null): Local {
const listeners = new Set<() => void>()
let folders: Folder[] = (names.length ? names : Array.from({ length: n }, (_, i) => `f${i + 1}`)).map((name, i) => ({ path: `/Users/dave/${name}`, name, used: i + 1 }))
if (found.length) {
// Found on the computer, never opened in Hanzo: listed after a moment, in the order the host found them.
setTimeout(() => {
folders = [...folders, ...found.map((name) => ({ path: `/Users/dave/${name}`, name }))]
for (const l of listeners) l()
}, 300)
}
const use = (path: string) => {
const top = Math.max(0, ...folders.map((f) => f.used ?? 0)) + 1
const known = folders.some((f) => f.path === path)
folders = known ? folders.map((f) => (f.path === path ? { ...f, used: top } : f)) : [...folders, { path, name: path.split('/').pop() ?? path, used: top }]
}
return {
folders: () => folders,
changed: (listener) => {
listeners.add(listener)
return () => listeners.delete(listener)
},
pick: async () => {
use('/Users/dave/picked')
return folders.find((f) => f.path === '/Users/dave/picked') ?? null
},
use,
run: async (folder, prompt) => {
run: async (folder, prompt, fetching) => {
ran(`${folder} <- ${prompt}`)
return { ok: true, output: `Edited README.md in ${folder}` }
if (setup) {
fetching?.(40)
await new Promise<void>((resolve) => {
setup.finish = resolve
})
fetching?.(100)
}
return { ok: true, output: `Edited README.md in ${folder}`, process: `exec bash -lc 'ls ~' in ${folder}\n succeeded in 12ms` }
},
}
}
Expand All @@ -59,7 +81,12 @@ function Page() {
const [width, setWidth] = useState(264)
const [shut, setShut] = useState(false)
const [ran, setRan] = useState<string[]>([])
const [local] = useState(() => (q.has('local') ? computer(Number(q.get('local')) || 0, (line) => setRan((r) => [...r, line])) : undefined))
const [setup] = useState(() => (q.get('setup') === '1' ? { finish: () => {} } : null))
const [local] = useState(() =>
q.has('local')
? computer(Number(q.get('local')) || 0, (line) => setRan((r) => [...r, line]), (q.get('folders') ?? '').split(',').filter(Boolean), (q.get('found') ?? '').split(',').filter(Boolean), setup)
: undefined,
)
const token = bearer()
const host: Host = {
name: 'Hanzo',
Expand Down Expand Up @@ -106,6 +133,7 @@ function Page() {
<SizableText size="$1" color="$soft" aria-label="Runs on this computer">
{ran.join(' | ')}
</SizableText>
{setup ? <SidebarItem onPress={() => setup.finish()}>Finish setup</SidebarItem> : null}
</Sidebar>
<Builder host={host} rail={false} />
</XStack>
Expand Down
70 changes: 66 additions & 4 deletions src/e2e/local.spec.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
/**
* This computer (hanzo-inc/app issues 309 and 310): on a host that has the computer, New offers it first, with Auto
* picking the folder used last; a run there needs no repository and no GitHub, and goes to the host's own runner,
* never to the platform. Driven through the host page (/src/e2e/host.html), whose `?local=<n>` hands the builder a
* computer with n folders.
* picking the folder the ask is about, else the one used last; a run there needs no repository and no GitHub, and
* goes to the host's own runner, never to the platform. What the agent said is drawn in words, and what it did only in
* Developer mode. Driven through the host page (/src/e2e/host.html), whose `?local=<n>` hands the builder a computer
* with n folders.
*/
import type { Page } from '@playwright/test'

Expand Down Expand Up @@ -37,7 +38,7 @@ test.describe('This computer', () => {
await ask(page).press('Enter')
await expect(runs(page)).toHaveText('/Users/dave/f2 <- Fix the typo in the README')
await expect(page.locator('[data-slot="ran-here"]')).toContainText('Done in f2')
await expect(page.locator('[data-slot="ran-here"]')).toContainText('Edited README.md in /Users/dave/f2')
await expect(page.locator('[data-slot="ran-here"]')).toContainText('Edited README.md in f2')
await expect(ask(page)).toHaveValue('')
// Nothing went to the platform: the run is the computer's.
expect(platform(sent)).toHaveLength(0)
Expand Down Expand Up @@ -86,4 +87,65 @@ test.describe('This computer', () => {
await chip(page, 'Where the run runs').click({ timeout: 45_000 })
await expect(list(page, 'Where the run runs').getByRole('option', { name: /This computer/ })).toHaveCount(0)
})

test('Auto follows the ask: the pill names the folder the words are about, live, and Send goes there', async ({ page }) => {
const sent = await quiet(page)
await page.goto(`${HOST}?local=0&folders=shop,docs,blog`)
// Nothing typed: the folder used last.
await expect(chip(page, 'Folder')).toHaveAccessibleName('Folder: Auto · blog', { timeout: 45_000 })
await ask(page).fill('Fix the checkout button in shop')
await expect(chip(page, 'Folder')).toHaveAccessibleName('Folder: Auto · shop')
await ask(page).fill('Fix the typo in the docs')
await expect(chip(page, 'Folder')).toHaveAccessibleName('Folder: Auto · docs')
await ask(page).press('Enter')
await expect(runs(page)).toHaveText('/Users/dave/docs <- Fix the typo in the docs')
// A folder the person chose is not moved by the words.
await chip(page, 'Folder').click()
await list(page, 'Folder').getByRole('option', { name: /^shop/ }).click()
await ask(page).fill('Write a post on my blog')
await expect(chip(page, 'Folder')).toHaveAccessibleName('Folder: shop')
expect(platform(sent)).toHaveLength(0)
})

test('the folders the computer finds later are offered, and Auto can pick one the person never opened', async ({ page }) => {
await quiet(page)
await page.goto(`${HOST}?local=0&folders=blog&found=portfolio`)
await expect(chip(page, 'Folder')).toHaveAccessibleName('Folder: Auto · blog', { timeout: 45_000 })
await ask(page).fill('Redo my portfolio homepage')
await expect(chip(page, 'Folder')).toHaveAccessibleName('Folder: Auto · portfolio')
await ask(page).press('Enter')
await expect(runs(page)).toHaveText('/Users/dave/portfolio <- Redo my portfolio homepage')
})

test('the answer is in words, and the process only in Developer mode', async ({ page }) => {
await quiet(page)
await page.goto(`${HOST}?local=1`)
await ask(page).fill('Add a checkout page')
await ask(page).press('Enter')
const here = page.locator('[data-slot="ran-here"]')
await expect(here).toContainText('Done in f1', { timeout: 45_000 })
await expect(here.locator('[data-slot="ran-answer"]')).toHaveText('Edited README.md in f1')
await expect(here).not.toContainText('bash -lc')
await expect(here).not.toContainText('~')
await expect(here.locator('[data-slot="ran-process"]')).toHaveCount(0)
// Developer mode shows what the agent did, as it ran it.
await page.evaluate(() => {
window.localStorage.setItem('hanzo.dev_mode', 'true')
window.dispatchEvent(new CustomEvent('hanzo:dev-mode', { detail: { enabled: true } }))
})
await expect(here.locator('[data-slot="ran-process"]')).toContainText("exec bash -lc 'ls ~' in /Users/dave/f1")
})

test('the first run says it is setting Hanzo Dev up, then runs', async ({ page }) => {
await quiet(page)
await page.goto(`${HOST}?local=1&setup=1`)
await ask(page).fill('Add a checkout page')
await ask(page).press('Enter')
const here = page.locator('[data-slot="ran-here"]')
await expect(here.locator('[data-slot="ran-setup"]')).toHaveText('Setting up Hanzo Dev on this computer… 40%', { timeout: 45_000 })
await expect(here).toContainText('Working in f1…')
await page.getByRole('button', { name: 'Finish setup' }).click()
await expect(here).toContainText('Done in f1')
await expect(here.locator('[data-slot="ran-setup"]')).toHaveCount(0)
})
})
71 changes: 53 additions & 18 deletions src/landing.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ import { useDevMode } from './dev-mode.ts'
import { takeHandoff } from './handoff.ts'
import { useHost, useLocal, useTarget } from './host.tsx'
import { failed, HERE, recent, where, type Folder } from './local.ts'
import { plainAnswer } from './plain.ts'
import { level, pacing, useMind } from './mind.ts'
import { usePrefs } from './prefs.tsx'
import { Publish, type Source } from './publish.tsx'
Expand Down Expand Up @@ -93,13 +94,16 @@ const FIRST: Kept = { repo: null, branch: '', place: '', folder: '', mode: 'buil
/** This computer, as a place: always there on a host that has it. */
const HERE_PLACE: Place = { id: HERE, label: 'This computer', kind: 'here', status: 'online', capacity: '' }

/** What a run on this computer is doing, or did: its folder, its ask, and once it finished, what it printed. */
/** What a run on this computer is doing, or did: its folder, its ask, and once it finished, what the agent said and did. */
interface Ran {
folder: Folder
ask: string
going: boolean
ok: boolean
/** What the agent said at the end. */
output: string
/** What it did on the way: shown in Developer mode only. */
process: string
}

/**
Expand Down Expand Up @@ -146,14 +150,20 @@ export function hold(u: Unsent): void {
}
}

/** A run on this computer: where it works, what it was asked, and once it ends, what the agent printed. */
function RanHere({ ran }: { ran: Ran }) {
/**
* A run on this computer: where it works, and once it ends, what the agent said, in words (`plainAnswer`). What it did
* on the way (its commands and what they printed) is drawn only in Developer mode; the first run waits while the host
* fetches the agent, and says so.
*/
function RanHere({ ran, setup, devMode }: { ran: Ran; setup: number | null; devMode: boolean }) {
const { t } = useI18n()
const fetching = ran.going && setup !== null && setup < 100
const head = ran.going
? t('Working in {name}…', { name: ran.folder.name })
: ran.ok
? t('Done in {name}', { name: ran.folder.name })
: t('Stopped in {name}', { name: ran.folder.name })
const said = ran.output ? plainAnswer(ran.output, ran.folder, t) : ''
return (
<YStack data-slot="ran-here" role="status" aria-live="polite" gap="$1.5" mx="$3" px="$3" py="$2.5" rounded="$4" borderWidth={1} borderColor="$borderColor" bg="$background" minW={0}>
<XStack items="center" gap="$2" minW={0}>
Expand All @@ -162,13 +172,29 @@ function RanHere({ ran }: { ran: Ran }) {
{head}
</SizableText>
</XStack>
<SizableText size="$1" color="$soft" numberOfLines={1} data-i18n-ltr="">
{ran.folder.path}
</SizableText>
{ran.output ? (
{fetching ? (
<SizableText data-slot="ran-setup" size="$1" color="$soft">
{t('Setting up Hanzo Dev on this computer… {percent}%', { percent: setup })}
</SizableText>
) : null}
{said ? (
<YStack maxH={320} overflowY="auto" minW={0}>
<SizableText size="$1" color="$ink" fontFamily="$mono" whiteSpace="pre-wrap" wordWrap="break-word" data-i18n-ltr="">
{ran.output}
<SizableText data-slot="ran-answer" size="$2" color="$ink" whiteSpace="pre-wrap" wordWrap="break-word">
{said}
</SizableText>
</YStack>
) : !ran.going && !ran.ok ? (
<SizableText data-slot="ran-answer" size="$2" color="$ink">
{t('Hanzo Dev stopped before it finished. Try again.')}
</SizableText>
) : null}
{devMode && ran.process ? (
<YStack data-slot="ran-process" gap="$1" minW={0}>
<SizableText size="$1" color="$soft" fontWeight="500">
{t('What Hanzo ran')}
</SizableText>
<SizableText size="$1" color="$soft" fontFamily="$mono" whiteSpace="pre-wrap" wordWrap="break-word" maxH={240} overflowY="auto" data-i18n-ltr="">
{ran.process}
</SizableText>
</YStack>
) : null}
Expand Down Expand Up @@ -272,8 +298,14 @@ export function Landing({ onStarted }: { onStarted: (session: string) => void })
const place: Place = here ? SANDBOX : (places.value.find((p) => p.id === kept.place) ?? SANDBOX)
// The folders this computer opened, most recent first, and the one a run here goes to: the one chosen, else Auto's.
const folders = local ? recent(local.folders()) : []
const folderAt = where(folders, kept.folder)
// The folder Auto picks is the one the words typed so far are about (local.ts `route`), so the pill says where Send goes.
const folderAt = where(folders, kept.folder, draft)
const [ran, setRan] = useState<Ran | null>(null)
// How far the agent's first-run setup has got, while the host fetches it.
const [fetched, setFetched] = useState<number | null>(null)
// The folders found on the computer arrive after the first read.
const [, folded] = useState(0)
useEffect(() => local?.changed?.(() => folded((n) => n + 1)), [local])
// Why this mode cannot run where it is sent: a plan is the sandbox's, on a machine and on this computer alike.
const refused = here ? (mode === 'plan' ? t('A plan runs in the cloud workspace. On this computer, switch to Build.') : '') : unhonoured(mode, place.id, t)

Expand Down Expand Up @@ -416,16 +448,17 @@ export function Landing({ onStarted }: { onStarted: (session: string) => void })
}

/**
* A run on this computer: in the folder chosen, else Auto's (the one used last), else one the person picks now, once.
* The agent reads, edits and runs there and nowhere else; what it printed is shown under the composer when it ends.
* A run on this computer: in the folder chosen, else Auto's (the one the ask is about, else the one used last), else one
* the person picks now, once. The agent reads, edits and runs there and nowhere else; what it said is shown under the
* composer when it ends, and what it did only in Developer mode.
*/
const sendHere = async (said: string) => {
if (!local || busy) return
if (refused) {
setNote(refused)
return
}
let folder = folderAt.folder
let folder = where(folders, kept.folder, said).folder
if (!folder) {
setNote(t('Choose a folder for this run'))
folder = await local.pick().catch(() => null)
Expand All @@ -437,16 +470,18 @@ export function Landing({ onStarted }: { onStarted: (session: string) => void })
const prompt = [carry.text, compose(ask, files)].filter(Boolean).join('\n\n')
setBusy(true)
setNote('')
setRan({ folder, ask, going: true, ok: true, output: '' })
setRan({ folder, ask, going: true, ok: true, output: '', process: '' })
setFetched(null)
try {
const done = await local.run(folder.path, prompt)
setRan({ folder, ask, going: false, ok: done.ok, output: done.output.trim() })
const done = await local.run(folder.path, prompt, setFetched)
setRan({ folder, ask, going: false, ok: done.ok, output: done.output.trim(), process: (done.process ?? '').trim() })
setDraft('')
setFiles([])
} catch (e) {
setRan(null)
setNote(failed(e, t))
} finally {
setFetched(null)
setBusy(false)
}
}
Expand Down Expand Up @@ -591,7 +626,7 @@ export function Landing({ onStarted }: { onStarted: (session: string) => void })
{
id: AUTO,
label: t('Auto'),
hint: folders[0] ? t('Last used: {name}', { name: folders[0].name }) : t('The folder you used last'),
hint: t('The folder your ask is about, else the one you used last'),
icon: <FolderOpen size={13} color="$soft" />,
},
...folders.map((f) => ({ id: f.path, label: f.name, hint: f.path, icon: <FolderOpen size={13} color="$soft" /> })),
Expand Down Expand Up @@ -868,7 +903,7 @@ export function Landing({ onStarted }: { onStarted: (session: string) => void })
{note || refused || mind.note}
</SizableText>
) : null}
{ran ? <RanHere ran={ran} /> : null}
{ran ? <RanHere ran={ran} setup={fetched} devMode={devMode} /> : null}
<Composer
column
value={draft}
Expand Down
Loading
Loading