mirror of
https://github.com/stablyai/orca.git
synced 2026-11-19 04:23:57 +00:00
* refactor: move atomic publication to its filesystem owner * refactor: group shared browser definitions by feature * refactor: bind proxy process and session contracts to defining owners * refactor: consolidate direct session contract type imports * refactor: retain atomic publication test timing under moved owner * fix(benchmark): resolve historical tunnel framing source before loading
145 lines
6.0 KiB
TypeScript
145 lines
6.0 KiB
TypeScript
import type { BrowserSnapshotOptions } from '../../shared/rpc-contract/browser-observation-params'
|
|
import type {
|
|
BrowserBackResult,
|
|
BrowserEvalResult,
|
|
BrowserGotoResult,
|
|
BrowserPdfResult,
|
|
BrowserReloadResult,
|
|
BrowserScreenshotResult,
|
|
BrowserScrollResult,
|
|
BrowserSnapshotResult,
|
|
BrowserWaitResult
|
|
} from '../../shared/browser/runtime/runtime-browser-contracts'
|
|
import type { CommandHandler } from '../dispatch'
|
|
import { formatScreenshot, formatSnapshot, saveBrowserScreenshot } from './browser-format'
|
|
import { printResult } from '../format'
|
|
import {
|
|
getOptionalPositiveIntegerFlag,
|
|
getOptionalStringFlag,
|
|
getRequiredStringFlag
|
|
} from '../flags'
|
|
import { RuntimeClientError } from '../runtime/types'
|
|
import { getBrowserCommandTarget } from './browser-command-target'
|
|
import { browserWaitRequestTimeoutMs } from '../../shared/browser-wait-timeout'
|
|
|
|
export const BROWSER_NAV_HANDLERS: Record<string, CommandHandler> = {
|
|
snapshot: async ({ flags, client, cwd, json }) => {
|
|
const options: BrowserSnapshotOptions = {
|
|
interactive: flags.has('interactive') || undefined,
|
|
compact: flags.has('compact') || undefined,
|
|
depth: getOptionalPositiveIntegerFlag(flags, 'depth'),
|
|
selector: getOptionalStringFlag(flags, 'selector'),
|
|
urls: flags.has('urls') || undefined,
|
|
delta: flags.has('delta') || undefined,
|
|
full: flags.has('full') || undefined
|
|
}
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserSnapshotResult>('browser.snapshot', {
|
|
...options,
|
|
...target
|
|
})
|
|
printResult(result, json, formatSnapshot)
|
|
},
|
|
screenshot: async ({ flags, client, cwd, json }) => {
|
|
const format = getOptionalStringFlag(flags, 'format')
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserScreenshotResult>('browser.screenshot', {
|
|
format: format === 'jpeg' ? 'jpeg' : undefined,
|
|
ifChanged: flags.has('if-changed') || undefined,
|
|
annotate: flags.has('annotate') || undefined,
|
|
...target
|
|
})
|
|
printResult(saveBrowserScreenshot(result), json, formatScreenshot)
|
|
},
|
|
goto: async ({ flags, client, cwd, json }) => {
|
|
const url = getRequiredStringFlag(flags, 'url')
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
// Why: navigation waits for network idle which can exceed the default 15s RPC timeout
|
|
const result = await client.call<BrowserGotoResult>(
|
|
'browser.goto',
|
|
{ url, ...target },
|
|
{ timeoutMs: 60_000 }
|
|
)
|
|
printResult(result, json, (v) => `Navigated to ${v.url} — ${v.title}`)
|
|
},
|
|
back: async ({ flags, client, cwd, json }) => {
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserBackResult>('browser.back', target)
|
|
printResult(result, json, (v) => `Back to ${v.url} — ${v.title}`)
|
|
},
|
|
reload: async ({ flags, client, cwd, json }) => {
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserReloadResult>('browser.reload', target, {
|
|
timeoutMs: 60_000
|
|
})
|
|
printResult(result, json, (v) => `Reloaded ${v.url} — ${v.title}`)
|
|
},
|
|
forward: async ({ flags, client, cwd, json }) => {
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<unknown>('browser.forward', target)
|
|
printResult(result, json, (v) => {
|
|
const url = (v as { url?: string } | null | undefined)?.url
|
|
return url ? `Navigated forward to ${url}` : 'Navigated forward'
|
|
})
|
|
},
|
|
eval: async ({ flags, client, cwd, json }) => {
|
|
const expression = getRequiredStringFlag(flags, 'expression')
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserEvalResult>('browser.eval', { expression, ...target })
|
|
printResult(result, json, (v) => v.result)
|
|
},
|
|
scroll: async ({ flags, client, cwd, json }) => {
|
|
const direction = getRequiredStringFlag(flags, 'direction')
|
|
if (direction !== 'up' && direction !== 'down') {
|
|
throw new RuntimeClientError('invalid_argument', '--direction must be "up" or "down"')
|
|
}
|
|
const amount = getOptionalPositiveIntegerFlag(flags, 'amount')
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserScrollResult>('browser.scroll', {
|
|
direction,
|
|
amount,
|
|
...target
|
|
})
|
|
printResult(result, json, (v) => `Scrolled ${v.scrolled}`)
|
|
},
|
|
wait: async ({ flags, client, cwd, json }) => {
|
|
const selector = getOptionalStringFlag(flags, 'selector')
|
|
const timeout = getOptionalPositiveIntegerFlag(flags, 'timeout')
|
|
const text = getOptionalStringFlag(flags, 'text')
|
|
const url = getOptionalStringFlag(flags, 'url')
|
|
const load = getOptionalStringFlag(flags, 'load')
|
|
const fn = getOptionalStringFlag(flags, 'fn')
|
|
const state = getOptionalStringFlag(flags, 'state')
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserWaitResult>(
|
|
'browser.wait',
|
|
{
|
|
selector,
|
|
timeout,
|
|
text,
|
|
url,
|
|
load,
|
|
fn,
|
|
state,
|
|
...target
|
|
},
|
|
{ timeoutMs: browserWaitRequestTimeoutMs(timeout) }
|
|
)
|
|
printResult(result, json, (v) => JSON.stringify(v, null, 2))
|
|
},
|
|
pdf: async ({ flags, client, cwd, json }) => {
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserPdfResult>('browser.pdf', target)
|
|
printResult(result, json, (v) => `PDF exported (${v.data.length} bytes base64)`)
|
|
},
|
|
'full-screenshot': async ({ flags, client, cwd, json }) => {
|
|
const format = getOptionalStringFlag(flags, 'format') === 'jpeg' ? 'jpeg' : 'png'
|
|
const target = await getBrowserCommandTarget(flags, cwd, client)
|
|
const result = await client.call<BrowserScreenshotResult>('browser.fullScreenshot', {
|
|
format,
|
|
...target
|
|
})
|
|
printResult(saveBrowserScreenshot(result), json, formatScreenshot)
|
|
}
|
|
}
|