Files
orca/src/cli/handlers/browser-profile.ts
T
e623372cdb feat(cli): add tab profile controls and automation primitives (#1396)
* feat(cli): add browser tab profile controls

* feat(cli): add tab profile automation primitives

* refactor(cli): narrow tab profile automation scope

* chore: retrigger PR checks

* review: harden tab profile automation CLI

- Wait for tab re-registration after browser.tabSetProfile so a follow-up tab list --show-profile reads the new sessionProfileId from BrowserManager instead of the stale one from the previous webview
- Wait for tab registration after browser.tabProfileClone, matching browser.tabCreate, so the cloned browserPageId is operable when the CLI returns
- Short-circuit browser.tabSetProfile when the tab is already on the requested profile so we do not tear down and remount the webview for a no-op switch
- Switch TabShow.worktree from OptionalPlainString to OptionalString to match every other tab schema; empty --worktree should fall back to the active worktree, not pass through as the empty string
- Add max-lines disable to browser.test.ts (file grew past 300 lines after adding the new tab-profile and tab-show tests)

* review: fix useIpcEvents test setup for tab profile API

CI failure: useIpcEvents.test.ts threw at module load with TypeError: window.addEventListener is not a function. The chain: the rebased useIpcEvents.ts imports destroyPersistentWebview from webview-registry, which calls window.addEventListener at module load. The test stubs window via vi.stubGlobal as a plain object without addEventListener, so the typeof window check passes but the call throws.

- webview-registry.ts: tighten the module-load guard to also check that window.addEventListener is callable, so importing this module from a non-DOM-ish test env (vitest node env with stubbed window) does not throw at module load
- useIpcEvents.test.ts: add the new onRequestTabSetProfile and replyTabSetProfile stubs to all 8 window.api.ui mocks so the new IPC subscription registered by useIpcEvents resolves

* review: restore profile CRUD lost during rebase onto 1397-merged main

The rebase brought commit 3242aa27 (refactor: narrow tab profile automation scope) onto a main that already had the lifecycle CRUD from 1397. The refactor commit removes BrowserProfileList/Create/Delete types, runtime methods, RPC registrations and schemas, plus the help/specs entries, because those were the precursor versions in commit 1 of this branch. Post-rebase those removals land on the hardened versions inherited from main, breaking 1397.

Restore:
- runtime-types.ts: BrowserSessionProfile import; ProfileList/Create/Delete result types
- orca-runtime.ts: ProfileList/Create/Delete result type imports; browserProfileList/Create/Delete methods
- browser-core.ts: ProfileCreate, ProfileDelete schema imports; browser.profileList/profileCreate/profileDelete RPC registrations
- browser-schemas.ts: ProfileCreate, ProfileDelete zod schemas
- help.ts: list/create/delete subcommand lines under Browser Automation
- specs/browser-basic.ts: list/create/delete spec entries

---------

Co-authored-by: Nikolatesla-lj <Nikolatesla-lj@users.noreply.github.com>
Co-authored-by: Jinwoo-H <jinwoo0825@gmail.com>
2026-05-04 23:46:23 -07:00

107 lines
3.9 KiB
TypeScript

import type {
BrowserProfileCreateResult,
BrowserProfileDeleteResult,
BrowserProfileListResult,
BrowserTabProfileCloneResult,
BrowserTabProfileShowResult,
BrowserTabSetProfileResult
} from '../../shared/runtime-types'
import type { CommandHandler } from '../dispatch'
import { getOptionalStringFlag, getRequiredStringFlag } from '../flags'
import {
formatBrowserProfileList,
formatTabProfileClone,
formatTabProfileShow,
printResult
} from '../format'
import { RuntimeClientError } from '../runtime-client'
import { getBrowserCommandTarget } from '../selectors'
function parseScopeFlag(flags: Map<string, string | boolean>): 'isolated' | 'imported' {
const raw = getOptionalStringFlag(flags, 'scope')
if (raw === undefined || raw === 'isolated') {
return 'isolated'
}
if (raw === 'imported') {
return 'imported'
}
throw new RuntimeClientError('invalid_argument', '--scope must be "isolated" or "imported"')
}
export const BROWSER_PROFILE_HANDLERS: Record<string, CommandHandler> = {
'tab profile list': async ({ client, json }) => {
const result = await client.call<BrowserProfileListResult>('browser.profileList')
printResult(result, json, formatBrowserProfileList)
},
'tab profile create': async ({ flags, client, json }) => {
const label = getRequiredStringFlag(flags, 'label')
const scope = parseScopeFlag(flags)
const result = await client.call<BrowserProfileCreateResult>('browser.profileCreate', {
label,
scope
})
if (result.result.profile === null) {
// Why: registry refuses non-isolated/imported scopes; we already validated
// the scope client-side, so a null here means a server-side rejection we
// shouldn't silently report as success.
throw new RuntimeClientError(
'runtime_error',
`Failed to create browser profile (label=${label}, scope=${scope})`
)
}
printResult(
result,
json,
(value) =>
`Created profile ${value.profile?.id ?? 'unknown'} (${value.profile?.label ?? label})`
)
},
'tab profile delete': async ({ flags, client, json }) => {
const profileId = getRequiredStringFlag(flags, 'profile')
const result = await client.call<BrowserProfileDeleteResult>('browser.profileDelete', {
profileId
})
printResult(result, json, (value) =>
value.deleted
? `Deleted profile ${value.profileId}`
: `Profile ${value.profileId} was not deleted`
)
},
'tab profile set': async ({ flags, client, cwd, json }) => {
const profileId = getRequiredStringFlag(flags, 'profile')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<BrowserTabSetProfileResult>('browser.tabSetProfile', {
...target,
profileId
})
printResult(
result,
json,
(value) =>
`Switched ${value.browserPageId} to ${value.profileLabel ?? value.profileId ?? 'default'}`
)
},
'tab profile show': async ({ flags, client, cwd, json }) => {
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<BrowserTabProfileShowResult>('browser.tabProfileShow', target)
printResult(result, json, formatTabProfileShow)
},
'tab profile use-default': async ({ flags, client, cwd, json }) => {
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<BrowserTabSetProfileResult>('browser.tabSetProfile', {
...target,
profileId: 'default'
})
printResult(result, json, (value) => `Switched ${value.browserPageId} to Default`)
},
'tab profile clone': async ({ flags, client, cwd, json }) => {
const profileId = getRequiredStringFlag(flags, 'profile')
const target = await getBrowserCommandTarget(flags, cwd, client)
const result = await client.call<BrowserTabProfileCloneResult>('browser.tabProfileClone', {
...target,
profileId
})
printResult(result, json, formatTabProfileClone)
}
}