jackwener/OpenCLI · error · CommandExecutionError

Could not attach topic "${topic}": failed to type inline top

Error message

Could not attach topic "${topic}": failed to type inline topic query

What it means

CommandExecutionError from addTopics: the page.insertText(`#<topic>`) call itself threw, so the inline topic query could not be typed into the editor. The error wraps the failure with per-topic context so you know which topic failed.

Source

Thrown at clis/xiaohongshu/publish.js:597

        // Separate this topic from the preceding text so the dropdown is clean.
        if (typeof page.pressKey === 'function') {
            try {
                await page.pressKey('Enter');
            }
            catch { /* non-fatal */ }
        }
        // Type the inline "#<topic>" query so XHS pops the inline suggestion
        // dropdown. We must use `page.insertText` (CDP) rather than the legacy
        // `execCommand` path, otherwise XHS's editor doesn't fire its keyup
        // listener and no dropdown appears.
        if (typeof page.insertText !== 'function') {
            throw new CommandExecutionError(`Could not attach topic "${topic}": page.insertText is unavailable`);
        }
        try {
            await page.insertText(`#${topic}`);
        }
        catch {
            throw new CommandExecutionError(`Could not attach topic "${topic}": failed to type inline topic query`);
        }
        await page.wait({ time: 1.2 }); // Let the suggestion dropdown render.
        // The suggestion dropdown lives inside the editor's closed shadow root,
        // so light-DOM queries cannot enumerate its items. XHS auto-highlights
        // the first matching suggestion as soon as the query is typed, so
        // pressing Enter accepts it directly. `page.nativeClick` would also
        // work but is not always wired up in the browser-bridge wrapper.
        if (typeof page.pressKey !== 'function') {
            throw new CommandExecutionError(`Could not attach topic "${topic}": page.pressKey is unavailable`);
        }
        try {
            await page.pressKey('Enter');
        }
        catch (err) {
            throw new CommandExecutionError(`Could not attach topic "${topic}": failed to accept suggestion (${err && err.message || err})`);
        }
        await page.wait({ time: 0.8 });
        // Verify the topic chip actually rendered. The chip itself lives in a

View on GitHub (pinned to 49907e53dc)

Solutions

  1. Retry the publish — transient focus/re-render races often succeed on a second run.
  2. Ensure no dialogs/overlays steal focus and the page stays on the publisher URL.
  3. Slow things down: add waits between publish steps if the environment supports it.
  4. Update the CLI/bridge if insertText failures persist; capture a screenshot to diagnose.

Example fix

// before
await xhs.publish({ topics: ['travel', 'food'], ... });
// after
// retry wrapper
async function publishWithRetry(opts, n = 2) {
  try { return await xhs.publish(opts); }
  catch (e) {
    if (n > 0 && e.message.includes('failed to type inline topic query')) return publishWithRetry(opts, n - 1);
    throw e;
  }
}
Defensive patterns

Strategy: retry

Try / catch

try {
  await xhs.publish(opts);
} catch (err) {
  if (err.message.includes('failed to type inline topic query')) {
    return retryPublish(opts, 2); // transient focus/injection races
  }
  throw err;
}

Prevention

When it happens

Trigger: Editor lost focus between focus and typing (overlay, re-render); CDP insertText failing because the target frame/page detached; the element became non-editable; navigation mid-publish.

Common situations: Flaky sessions where XHS re-renders the editor while typing; browser tab crashed or navigated; topic text containing characters that break input injection (rare).

Related errors


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