jackwener/OpenCLI · error · CommandExecutionError

Browser session required for bilibili video

Error message

Browser session required for bilibili video

What it means

The bilibili video command requires a live browser session (page) to call Bilibili's web APIs. If the func callback receives no page object, it throws this CommandExecutionError immediately, because authenticated/browser-context requests cannot proceed.

Source

Thrown at clis/bilibili/video.js:45

  if (value == null) return '';
  if (typeof value === 'string') return value;
  throw new CommandExecutionError(`${label} returned a malformed string`);
}

cli({
  site: 'bilibili',
  name: 'video',
    access: 'read',
  description: 'Get Bilibili video metadata (title, author, duration, stats, etc.)',
  strategy: Strategy.COOKIE,
  args: [
    { name: 'bvid', required: true, positional: true, help: 'BV ID, video URL, or b23.tv short link' },
    { name: 'page', required: false, help: '分P 选集序号(从 1 开始)。多 P 视频指定某一集,title/cid 返回该集;缺省取整集默认(P1)' },
  ],
  columns: ['field', 'value'],
  func: async (page, kwargs) => {
    if (!page) {
      throw new CommandExecutionError('Browser session required for bilibili video');
    }

    // 选集序号(--page):缺省 null = 不下钻分P,保持整集(P1)旧行为。
    const selectedPage = parsePageArg(kwargs.page);

    // Resolve BV ID from three advertised input forms:
    //   1. Bare "BV..." id
    //   2. Full bilibili.com/video/<BV>... URL (with or without query string / www / m.)
    //   3. b23.tv short link (delegated to resolveBvid)
    // resolveBvid() alone handles (1) and (3) but not (2), so we pre-extract
    // from bilibili URLs before falling through.
    const input = String(kwargs.bvid ?? '').trim();
    const bilibiliUrlMatch = input.match(/bilibili\.com\/(?:video|bangumi\/play)\/(BV[A-Za-z0-9]+)/i);
    const bvid = bilibiliUrlMatch ? bilibiliUrlMatch[1] : await resolveBvid(input);

    // Navigate to video page first so subsequent api call shares a primed session.
    await page.goto(`https://www.bilibili.com/video/${bvid}/`);

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Start a browser session with the CLI's session/login command before running the video command
  2. Re-authenticate if the session expired
  3. Check that the browser automation backend (playwright/puppeteer) is installed and functional
  4. Run interactively on a machine with a supported browser instead of a restricted CI environment

Example fix

// before
opencli bilibili video BV1xx411c7mD
// after
opencli session start   # or the CLI's login/browser init command
opencli bilibili video BV1xx411c7mD
Defensive patterns

Strategy: validation

Validate before calling

// before invoking, verify a session exists
const session = await getSession();
if (!session || !session.page) { console.error('Run the session/login command first'); process.exit(1); }

Type guard

null

Try / catch

try { await opencli.video(bvid); }
catch (e) {
  if (e instanceof CommandExecutionError && e.message.includes('Browser session required')) { console.error('Start a browser session: opencli session start'); }
  else throw e;
}

Prevention

When it happens

Trigger: Running the video command without an active browser session — e.g. no session started via the CLI's browser/login flow, a stale/expired session, or invoking the command in an environment where the browser bridge is unavailable.

Common situations: Fresh install with no opencli browser session created; session closed between runs; running in CI/headless environment without browser support; session store corrupted.

Related errors


AI-assisted analysis of jackwener/OpenCLI@49907e53dc (2026-08-29). Data as JSON: /api/errors/3f7a6b9434fc1d11. Report an issue: GitHub.