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
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,15 @@ The agent reads, edits and runs in that folder only. What it said shows under th
composer in words (`plainAnswer`: the folder's path is its name, `~` is "your home
folder"), what it did only in Developer mode (`Done.process`), and the first run
says "Setting up Hanzo Dev on this computer…" while the host fetches the agent
(`run`'s `setup` callback).
(`run`'s `setup` callback). A host that can
also open a site (Hanzo Desktop hands `site`, site.ts) offers to redesign one from
its address: an address pasted alone, or with a request to redesign it, or Redesign
a site… in the composer's commands. The host opens the site from the computer's own
connection and saves a copy of it into the folder (`.hanzo/reference`), serves the
folder's `redesign/` live and shows it in Chrome; the agent, which has no network,
reads the copy, answers with a critique and builds the new site there as static
files, and the preview follows each file it saves. An ask that only holds a link is
a normal run.

**A run.** The transcript as it streams, drawn by what each part is: what the
agent says as markdown, each command it runs as a card that opens onto its
Expand Down
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.137",
"version": "0.2.138",
"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
37 changes: 35 additions & 2 deletions src/e2e/host.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
* ?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
* ?site=<mode> the host can redesign a site (Hanzo Desktop's `site`; needs ?local): `ok`, or `fail` where the site will
* not open. Every step it is asked for is written out under the sidebar (Redesign steps)
*/
import '@hanzo/font/css'
import '@hanzo/ui/theme.css'
Expand All @@ -27,7 +29,7 @@ import { Sidebar, SidebarItem } from '@hanzo/ui/chat'
import { StrictMode, useState } from 'react'
import { createRoot } from 'react-dom/client'

import { administers, Builder, DevSection, Grip, type Folder, type Host, type Local } from '../index.ts'
import { administers, Builder, DevSection, Grip, type Folder, type Host, type Local, type Site } from '../index.ts'

const ORG = 'acme'
const bearer = () => window.localStorage.getItem('hanzo_iam_access_token')
Expand Down Expand Up @@ -68,11 +70,36 @@ function computer(n: number, ran: (line: string) => void, names: string[], found
})
fetching?.(100)
}
return { ok: true, output: `Edited README.md in ${folder}`, process: `exec bash -lc 'ls ~' in ${folder}\n succeeded in 12ms` }
return {
ok: true,
output: prompt.startsWith('Redesign the website') ? 'The page is cluttered. I rebuilt it around one clear call to action.' : `Edited README.md in ${folder}`,
process: `exec bash -lc 'ls ~' in ${folder}\n succeeded in 12ms`,
}
},
}
}

/** A host that can redesign a site, as Hanzo Desktop's `site` answers: `fail` is a site that will not open. */
function website(mode: string, said: (line: string) => void): Site {
return {
folder: async (name) => {
said(`folder ${name}`)
return { path: `/Users/dave/Documents/Hanzo/${name}`, name }
},
capture: async (url, folder) => {
said(`capture ${url} -> ${folder}`)
if (mode === 'fail') throw new Error('The site took too long to answer.')
return { title: 'Acme Plumbing', url, files: ['page.html', 'text.md', 'style-1.css'], screenshot: false }
},
preview: async (folder) => {
said(`preview ${folder}`)
return { url: 'http://127.0.0.1:9933/' }
},
stop: async (folder) => said(`stop ${folder}`),
show: async (url) => said(`show ${url}`),
}
}

function Page() {
const q = new URLSearchParams(window.location.search)
const [at, setAt] = useState(q.get('at') ?? '')
Expand All @@ -87,6 +114,8 @@ function Page() {
? 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 [steps, setSteps] = useState<string[]>([])
const [site] = useState(() => (q.has('site') ? website(q.get('site') ?? 'ok', (line) => setSteps((s) => [...s, line])) : undefined))
const token = bearer()
const host: Host = {
name: 'Hanzo',
Expand All @@ -103,6 +132,7 @@ function Page() {
links: { github: '/dev/-/sync', customize: '/dev/-/customize', settings: '/settings', home: '/' },
open: () => {},
...(local ? { local } : {}),
...(site ? { site } : {}),
}
const label = q.get('label')
if (shut)
Expand Down Expand Up @@ -134,6 +164,9 @@ function Page() {
{ran.join(' | ')}
</SizableText>
{setup ? <SidebarItem onPress={() => setup.finish()}>Finish setup</SidebarItem> : null}
<SizableText size="$1" color="$soft" aria-label="Redesign steps">
{steps.join(' | ')}
</SizableText>
</Sidebar>
<Builder host={host} rail={false} />
</XStack>
Expand Down
156 changes: 156 additions & 0 deletions src/e2e/redesign.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
/**
* Redesign a site from its address (hanzo-inc/app issue 312, hanzoai/desktop issue 15): on a host that has the computer
* and can open a site, an address pasted in New is offered as a redesign; one press has the host open the site and save a
* copy, serve the redesign live and show it in Chrome, and the agent critique and build it in a new folder. An ask that
* only holds a link is sent as it is. Driven through the host page (/src/e2e/host.html): `?local=<n>` hands the builder
* the computer, `?site=<mode>` the host's way of redesigning a site.
*/
import type { Page } from '@playwright/test'

import { expect, test } from './fixture.ts'
import { signIn, type Sent } from './signed.ts'

const HOST = '/src/e2e/host.html'
const ask = (page: Page) => page.getByRole('textbox', { name: 'Describe a task or ask a question' })
const runs = (page: Page) => page.getByLabel('Runs on this computer')
const steps = (page: Page) => page.getByLabel('Redesign steps')
const offer = (page: Page) => page.locator('[data-slot="redesign-offer"]')
const card = (page: Page) => page.locator('[data-slot="redesign"]')
const platform = (sent: Sent[]) => sent.filter((s) => s.method === 'POST' && (s.path === '/v1/agent/turns' || s.path === '/v1/agent/coding'))

const quiet = (page: Page) =>
signIn(page, ({ path }) => {
if (path === '/v1/agent/sessions') return { json: { sessions: [], next: '' } }
if (path === '/v1/agent/sessions/stream') return { text: '', type: 'text/event-stream' }
if (path === '/v1/agent/targets') return { json: { targets: [] } }
return undefined
})

test.describe('Redesign a site', () => {
test('an address pasted alone is offered, and one press opens the site, serves the redesign and has the agent build it', async ({ page }, info) => {
const sent = await quiet(page)
await page.goto(`${HOST}?local=2&site=ok`)
await ask(page).fill('https://www.acme.test/plumbing', { timeout: 45_000 })
await expect(offer(page)).toContainText('Redesign acme.test?')
await expect(offer(page)).toContainText('Hanzo opens it, reviews it, and builds a new version here while you watch it in Chrome.')
await page.screenshot({ path: info.outputPath('redesign-offer.png') })

await offer(page).getByRole('button', { name: 'Redesign this site' }).click()
await expect(card(page)).toContainText('Redesigned acme.test')
// The steps, in order: a new folder named for the site, the site saved into it, the redesign served, then shown.
await expect(steps(page)).toHaveText(
'folder acme-redesign | capture https://www.acme.test/plumbing -> /Users/dave/Documents/Hanzo/acme-redesign | preview /Users/dave/Documents/Hanzo/acme-redesign | show http://127.0.0.1:9933/',
)
// The agent is sent to that folder with what was saved and what to do.
const run = await runs(page).textContent()
expect(run).toContain('/Users/dave/Documents/Hanzo/acme-redesign <- Redesign the website https://www.acme.test/plumbing (Acme Plumbing).')
expect(run).toContain('- .hanzo/reference/text.md:')
expect(run).toContain('- .hanzo/reference/style-1.css: one of its stylesheets')
expect(run).not.toContain('screenshot')
expect(run).toContain('Do not start a server and do not use the network')
// The card says where it is, how to see it, and what the agent said; the ask was taken.
await expect(card(page)).toContainText('Live preview: http://127.0.0.1:9933/')
await expect(card(page)).toContainText('/Users/dave/Documents/Hanzo/acme-redesign')
await expect(card(page)).toContainText('The page is cluttered. I rebuilt it around one clear call to action.')
await expect(ask(page)).toHaveValue('')
await expect(offer(page)).toHaveCount(0)
await page.screenshot({ path: info.outputPath('redesign-done.png') })

// Open in Chrome shows the same address again.
await card(page).getByRole('button', { name: 'Open in Chrome' }).click()
await expect(steps(page)).toContainText('show http://127.0.0.1:9933/ | show http://127.0.0.1:9933/')
// The redesign's folder is now the one Auto picks, so the next ask goes to it and the preview follows its files.
await expect(page.getByRole('button', { name: /^Folder: / })).toHaveAccessibleName('Folder: Auto · acme-redesign')
// Nothing went to the platform.
expect(platform(sent)).toHaveLength(0)
})

test('Enter on an address alone starts it too, and "redesign <address> and …" keeps what was said', async ({ page }) => {
await quiet(page)
await page.goto(`${HOST}?local=1&site=ok`)
await ask(page).fill('Redesign shop.acme.com and make it darker', { timeout: 45_000 })
await expect(offer(page)).toContainText('Redesign shop.acme.com?')
await ask(page).press('Enter')
await expect(card(page)).toContainText('Redesigned shop.acme.com')
await expect(steps(page)).toContainText('folder shop-acme-redesign')
await expect(runs(page)).toContainText('The person added: “Redesign and make it darker”')
})

test('an ask that only holds a link is a normal run, with no offer', async ({ page }) => {
await quiet(page)
await page.goto(`${HOST}?local=1&site=ok`)
await ask(page).fill('What does https://acme.test say about pricing?', { timeout: 45_000 })
await expect(offer(page)).toHaveCount(0)
await ask(page).press('Enter')
await expect(runs(page)).toHaveText('/Users/dave/f1 <- What does https://acme.test say about pricing?')
await expect(steps(page)).toHaveText('')
await expect(card(page)).toHaveCount(0)
})

test('a folder the person chose is built in, and no new one is made', async ({ page }) => {
await quiet(page)
await page.goto(`${HOST}?local=2&site=ok`)
await page.getByRole('button', { name: /^Folder: / }).click({ timeout: 45_000 })
await page.getByRole('listbox', { name: 'Folder' }).getByRole('option', { name: /^f1/ }).click()
await ask(page).fill('acme.com')
await ask(page).press('Enter')
await expect(card(page)).toContainText('Redesigned acme.com')
await expect(steps(page)).toHaveText('capture https://acme.com/ -> /Users/dave/f1 | preview /Users/dave/f1 | show http://127.0.0.1:9933/')
await expect(runs(page)).toContainText('/Users/dave/f1 <- Redesign the website')
})

test('a site that will not open is said in plain words, nothing is built, and the person can try again', async ({ page }, info) => {
await quiet(page)
await page.goto(`${HOST}?local=1&site=fail`)
await ask(page).fill('https://slow.test', { timeout: 45_000 })
await ask(page).press('Enter')
await expect(card(page)).toContainText('Could not redesign slow.test')
await expect(card(page)).toContainText('The site took too long to answer.')
await expect(steps(page)).toContainText('capture https://slow.test/')
await expect(steps(page)).not.toContainText('preview')
await expect(runs(page)).toHaveText('')
await expect(card(page).getByRole('button', { name: 'Open in Chrome' })).toHaveCount(0)
await page.screenshot({ path: info.outputPath('redesign-failed.png') })
// Nothing is stuck: the composer takes another ask.
await expect(ask(page)).toBeEditable()
await ask(page).fill('Fix the typo')
await ask(page).press('Enter')
await expect(runs(page)).toHaveText('/Users/dave/f1 <- Fix the typo')
})

test('Redesign a site… asks for the address, and refuses what is not one', async ({ page }, info) => {
await quiet(page)
await page.goto(`${HOST}?local=1&site=ok`)
await ask(page).focus({ timeout: 45_000 })
await page.keyboard.press('Control+k')
await page.getByRole('listbox', { name: 'Commands' }).getByRole('option', { name: /Redesign a site/ }).click()
const dialog = page.getByRole('dialog', { name: 'Redesign a site' })
await expect(dialog).toBeVisible()
await page.screenshot({ path: info.outputPath('redesign-ask.png') })
await dialog.getByRole('textbox', { name: 'Site address' }).fill('not a site')
await dialog.getByRole('button', { name: 'Redesign', exact: true }).click()
await expect(dialog).toContainText('Paste a site’s address, like https://example.com.')
await dialog.getByRole('textbox', { name: 'Site address' }).fill('example.com')
await dialog.getByRole('textbox', { name: 'Site address' }).press('Enter')
await expect(card(page)).toContainText('Redesigned example.com')
await expect(steps(page)).toContainText('folder example-redesign')
})

test('a host that cannot open a site offers nothing of it, and neither does a computer on another place', async ({ page }) => {
await quiet(page)
// The computer, but no way to open a site.
await page.goto(`${HOST}?local=1`)
await ask(page).fill('https://acme.test', { timeout: 45_000 })
await expect(offer(page)).toHaveCount(0)
await ask(page).focus()
await page.keyboard.press('Control+k')
await expect(page.getByRole('listbox', { name: 'Commands' }).getByRole('option', { name: /Redesign a site/ })).toHaveCount(0)
await page.keyboard.press('Escape')
// A site-capable host, with the run sent to the cloud workspace instead of this computer.
await page.goto(`${HOST}?local=1&site=ok`)
await page.getByRole('button', { name: /^Where the run runs: / }).click({ timeout: 45_000 })
await page.getByRole('listbox', { name: 'Where the run runs' }).getByRole('option', { name: /Auto \(Cloud\)/ }).click()
await ask(page).fill('https://acme.test')
await expect(offer(page)).toHaveCount(0)
})
})
11 changes: 11 additions & 0 deletions src/host.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import type { Cast } from './api/agents.ts'
import type { Target } from './api/call.ts'
import type { Theme } from './api/pref.ts'
import type { Local } from './local.ts'
import type { Site } from './site.ts'

export interface Person {
name: string
Expand Down Expand Up @@ -123,6 +124,11 @@ export interface Host {
* offers This computer with Auto picking the folder used last. A browser page has no folder of its own: absent.
*/
local?: Local
/**
* Opening a site, saving a copy of it for the agent, and serving its redesign live in the person's browser (site.ts):
* Hanzo Desktop can, so New on This computer offers to redesign a site from its address. A browser page cannot: absent.
*/
site?: Site
}

const Context = createContext<Host | null>(null)
Expand Down Expand Up @@ -152,6 +158,11 @@ export function useLocal(): Local | null {
return useContext(Context)?.local ?? null
}

/** The host's way of redesigning a site, where the host runs on a computer that can (Hanzo Desktop). */
export function useSite(): Site | null {
return useContext(Context)?.site ?? null
}

/** The host's browser, where one is mounted and has one. */
export function useBrowser(): Browser | null {
return useContext(Context)?.browser ?? null
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export type { Limits } from './api/limits.ts'
export type { Target } from './api/call.ts'
export type { Browse, Browser, Host, Person, Shown } from './host.tsx'
export type { Done, Folder, Local } from './local.ts'
export type { Live, Saved, Site } from './site.ts'
export type { Session, Status } from './api/sessions.ts'
export { mark as markRun, marks as runMarks, useMarks as useRunMarks, type RunMark, type RunMarks } from './runmeta.ts'
export { talks } from './api/talk.ts'
Expand Down
Loading
Loading