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
1 change: 1 addition & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"bench:gate": "bun apps/bench/src/gate/main.ts",
"test": "bun test --parallel=4 --timeout 60000 ./apps ./packages ./plugins",
"test:upgrade": "bun test --timeout 1200000 ./plugins/host/daemon/test/upgrade/upgrade.ts",
"test:phone": "bun test --parallel --timeout 30000 ./plugins/ui/look/kit/test/gallery/gallery.ts ./plugins/ui/shell/shell/test/phone/window.ts ./plugins/ui/shell/shell/test/phone/drawers.ts ./plugins/ui/shell/shell/test/phone/keyboard.ts ./plugins/core/features/test/web/matrix.ts ./plugins/ui/chat/chat/test/web/follow.ts ./plugins/ui/chat/chat/test/web/catch-up.ts ./plugins/ui/chat/chat/test/web/draft.ts ./plugins/ui/chat/chat/test/web/early.ts ./plugins/ui/chat/chat/test/web/reply.ts ./plugins/ui/chat/chat/test/web/upload.ts ./plugins/bots/views/test/web/history.ts ./plugins/bots/views/test/web/tabs.ts ./plugins/core/server/test/web/hello.ts ./plugins/ui/shell/shell/test/web/parts.ts"
"test:phone": "bun test --parallel --timeout 30000 ./plugins/ui/look/kit/test/gallery/gallery.ts ./plugins/ui/shell/shell/test/phone/window.ts ./plugins/ui/shell/shell/test/phone/drawers.ts ./plugins/ui/shell/shell/test/phone/keyboard.ts ./plugins/core/features/test/web/matrix.ts ./plugins/ui/chat/chat/test/web/follow.ts ./plugins/ui/chat/chat/test/web/catch-up.ts ./plugins/ui/chat/chat/test/web/draft.ts ./plugins/ui/chat/chat/test/web/early.ts ./plugins/ui/chat/chat/test/web/reply.ts ./plugins/ui/chat/chat/test/web/upload.ts ./plugins/bots/views/test/web/history.ts ./plugins/bots/views/test/web/tabs.ts ./plugins/core/server/test/web/hello.ts ./plugins/ui/shell/shell/test/web/parts.ts ./plugins/ui/chat/html/test/web/zoom.ts"
},
"devDependencies": {
"@sand/boot": "workspace:*",
Expand Down
3 changes: 2 additions & 1 deletion plugins/ui/chat/html/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"zod": "^4.6.5"
},
"devDependencies": {
"@sand/harness": "workspace:*"
"@sand/harness": "workspace:*",
"playwright": "^1.64.1"
}
}
2 changes: 2 additions & 0 deletions plugins/ui/chat/html/src/page/integrations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { forwardLinks } from './links'
import { reportSize } from './size'
import { relaySwipes } from './swipe'
import { followTheme } from './theme'
import { relayZoom } from './zoom'

if (window.parent !== window) {
followTheme()
Expand All @@ -12,4 +13,5 @@ if (window.parent !== window) {
forwardLinks()
reportSize()
relaySwipes()
relayZoom()
}
14 changes: 14 additions & 0 deletions plugins/ui/chat/html/src/page/integrations/zoom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { zoomGesture } from '@sand/ui-kit/contract'
import { listen } from '@vaakx-dev/vrui'
import { zoomable, zoomBy, zoomToggle } from '../protocol'
import { post } from './post'

const relay = () => zoomGesture(window, { zoom: change => post(zoomBy, change), toggle: at => post(zoomToggle, { at }) }, { wheel: 'scrolls', surface: 'scaled' })

export const relayZoom = () => {
const stop = listen<MessageEvent>(window, 'message', ({ source, data }) => {
if (source !== window.parent || data?.method !== zoomable) return
stop()
relay()
})
}
3 changes: 3 additions & 0 deletions plugins/ui/chat/html/src/page/protocol.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,6 @@ export const showPage = 'sand/show-page'
export const swipeStart = 'sand/swipe-start'
export const swipeMove = 'sand/swipe-move'
export const swipeEnd = 'sand/swipe-end'
export const zoomable = 'sand/zoomable'
export const zoomBy = 'sand/zoom-by'
export const zoomToggle = 'sand/zoom-toggle'
14 changes: 13 additions & 1 deletion plugins/ui/chat/html/src/web/expand.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,28 @@
import type { SheetSpec } from '@sand/ui-shell/contract'
import type { Point, ZoomTarget } from '@sand/ui-kit/contract'
import type { HtmlRenderEntry } from '../contract'
import { columnWidth } from '../page/limits'
import { hostPath } from '../page/protocol'
import type { FrameContext } from './context'
import { sandboxedFrame } from './integrations/frame'
import { connectPage } from './page'

const onScreen = (frame: HTMLIFrameElement, { x, y }: Point): Point => {
const box = frame.getBoundingClientRect()
const scale = box.width / columnWidth
return { x: box.left + x * scale, y: box.top + y * scale }
}

const zoomFrom = (frame: HTMLIFrameElement, zoom: ZoomTarget): ZoomTarget => ({
zoom: ({ factor, from, to }) => zoom.zoom({ factor, from: onScreen(frame, from), to: onScreen(frame, to) }),
toggle: at => zoom.toggle(onScreen(frame, at)),
})

export const expandedPage = (ctx: FrameContext, data: HtmlRenderEntry, html: string, height: number): SheetSpec => {
const { kit } = ctx
const zoom = kit.fitZoom({ width: columnWidth, height })
const frame = sandboxedFrame({ 'aria-label': data.title, src: hostPath, class: 'block h-full w-full border-0', onLoad: () => connection.show(html) })
const connection = connectPage(ctx, frame, {})
const connection = connectPage(ctx, frame, { zoom: zoomFrom(frame, zoom) })

return {
label: `Full screen ${data.title}`,
Expand Down
48 changes: 46 additions & 2 deletions plugins/ui/chat/html/src/web/integrations/frame.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,40 @@
import type { Swipe, Way } from '@sand/ui-shell/contract'
import type { Point, ZoomTarget } from '@sand/ui-kit/contract'
import { el, listen, type Props } from '@vaakx-dev/vrui'
import { hostContext, openImage, openLink, pressKey, shortcutKeys, showPage, sizeChanged, swipeEnd, swipeMove, swipeStart } from '../../page/protocol'
import {
hostContext,
openImage,
openLink,
pressKey,
shortcutKeys,
showPage,
sizeChanged,
swipeEnd,
swipeMove,
swipeStart,
zoomable,
zoomBy,
zoomToggle,
} from '../../page/protocol'

interface Message {
jsonrpc?: unknown
id?: unknown
method?: unknown
params?: { height?: unknown; url?: unknown; delta?: unknown; way?: unknown; velocity?: unknown; src?: unknown; name?: unknown; key?: unknown }
params?: {
height?: unknown
url?: unknown
delta?: unknown
way?: unknown
velocity?: unknown
src?: unknown
name?: unknown
key?: unknown
factor?: unknown
from?: unknown
to?: unknown
at?: unknown
}
}

export interface FrameEvents {
Expand All @@ -15,8 +43,14 @@ export interface FrameEvents {
image(src: string, name: string): void
key(key: string): void
swipe?(way: Way): Swipe | undefined
zoom?: ZoomTarget
}

const isPoint = (value: unknown): value is Point =>
typeof value === 'object' && value !== null && 'x' in value && 'y' in value && Number.isFinite(value.x) && Number.isFinite(value.y)

const isFactor = (value: unknown): value is number => typeof value === 'number' && Number.isFinite(value) && value > 0

const isWebUrl = (url: unknown): url is string => typeof url === 'string' && /^https?:\/\//i.test(url)

const clickedIn = (frame: HTMLIFrameElement) => document.activeElement === frame && navigator.userActivation?.isActive !== false
Expand All @@ -32,6 +66,8 @@ export const sendTheme = (frame: HTMLIFrameElement, css: string) => post(frame,

export const sendKeys = (frame: HTMLIFrameElement, keys: string[]) => post(frame, { method: shortcutKeys, params: { keys } })

export const sendZoomable = (frame: HTMLIFrameElement) => post(frame, { method: zoomable })

const WAYS: readonly unknown[] = ['left', 'right', 'up', 'down'] satisfies Way[]

const isWay = (value: unknown): value is Way => WAYS.includes(value)
Expand Down Expand Up @@ -62,6 +98,14 @@ export const listenToFrame = (frame: HTMLIFrameElement, events: FrameEvents) =>
events.key(params.key)
return
}
if (message.method === zoomBy && isFactor(params.factor) && isPoint(params.from) && isPoint(params.to)) {
events.zoom?.zoom({ factor: params.factor, from: params.from, to: params.to })
return
}
if (message.method === zoomToggle && isPoint(params.at)) {
events.zoom?.toggle(params.at)
return
}
if (!clickedIn(frame)) return
if (message.method === openLink && isWebUrl(params.url)) {
events.open(params.url)
Expand Down
5 changes: 3 additions & 2 deletions plugins/ui/chat/html/src/web/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import { keys } from '@sand/keys/contract'
import { theme, type Theme } from '@sand/ui-theme/contract'
import { joinDisposers } from '@sand/util'
import type { FrameContext } from './context'
import { listenToFrame, sendKeys, sendTheme, showPageIn, type FrameEvents } from './integrations/frame'
import { listenToFrame, sendKeys, sendTheme, sendZoomable, showPageIn, type FrameEvents } from './integrations/frame'
import { themeCss } from './theme'

export const connectPage = (ctx: FrameContext, frame: HTMLIFrameElement, events: Pick<FrameEvents, 'resize' | 'swipe'>) => {
export const connectPage = (ctx: FrameContext, frame: HTMLIFrameElement, events: Pick<FrameEvents, 'resize' | 'swipe' | 'zoom'>) => {
const paint = (current: Theme | undefined) => {
if (current) sendTheme(frame, themeCss(current))
}
Expand All @@ -15,6 +15,7 @@ export const connectPage = (ctx: FrameContext, frame: HTMLIFrameElement, events:
showPageIn(frame, html)
paint(ctx.get(theme))
sendKeys(frame, ctx.get(keys)?.passthroughKeys() ?? [])
if (events.zoom) sendZoomable(frame)
},
listen: () =>
joinDisposers(
Expand Down
47 changes: 47 additions & 0 deletions plugins/ui/chat/html/test/web/sandbox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { mkdtempSync, rmSync } from 'node:fs'
import { tmpdir } from 'node:os'
import { join } from 'node:path'

const main = join(import.meta.dir, '../../../../../../apps/sand/src/main.ts')

const sand = async (home: string, ...args: string[]) => {
const run = Bun.spawn([process.execPath, main, '--home', home, ...args], { stdout: 'pipe', stderr: 'pipe' })
const [out, err, code] = await Promise.all([new Response(run.stdout).text(), new Response(run.stderr).text(), run.exited])
if (code !== 0) throw new Error(`sand ${args.join(' ')} failed: ${err || out}`)
return out
}

const printed = (out: string, pattern: RegExp) => {
const found = out.match(pattern)?.[1]?.trim()
if (!found) throw new Error(`sand sandbox printed no ${pattern}:\n${out}`)
return found
}

const settings = (title: string, html: string) =>
[
'[features]',
'',
'[[plugins.replay.sources]]',
'id = "replay"',
'models = ["script"]',
`replies = [[{ type = "call", name = "html_render", input = { title = ${JSON.stringify(title)}, html = ${JSON.stringify(html)} } }], [{ type = "text", text = "Here it is" }]]`,
'',
'[plugins.thread-titles]',
'enabled = false',
'',
].join('\n')

export const startSandbox = async (title: string, html: string) => {
const home = mkdtempSync(join(tmpdir(), 'sand-html-'))
await Bun.write(join(home, 'sandbox.toml'), settings(title, html))
const out = await sand(home, 'sandbox')
rmSync(home, { recursive: true, force: true })
const box = printed(out, /throwaway sand \(pid \d+\) in (.+)$/m)
return {
url: printed(out, /open it paired: (\S+)/),
async stop() {
await sand(box, 'stop')
rmSync(box, { recursive: true, force: true })
},
}
}
Loading
Loading