@brainfish-ai/web-widget

中文文档
1.25.7发布于 2026/11/05MITTypeScriptES Module近 30 天下载 2.4万

Brainfish Widgets Manager

原始使用文档
文档目录17 节
  1. Installation
  2. Directly in your HTML
  3. Popup Widget
  4. Basic Usage
  5. Widget Control API
  6. User Management
  7. Customization with Overrides
  8. Custom Positioning
  9. Contextual Help Feature
  10. Event Listeners
  11. Analytics Events
  12. Build
  13. Testing locally
  14. Start the local server
  15. Open the Playground
  16. Test Different Environments
  17. Agent Widget Development

Brainfish In-App Widget

This package provides a simple way to integrate a Brainfish in-app widget in your website or web application. It supports popup widgets with contextual help functionality.

Installation

npm install @brainfish-ai/web-widget

Directly in your HTML

<script type="module">
    import Brainfish from "https://cdn.jsdelivr.net/npm/@brainfish-ai/web-widget@latest/dist/web.js"
    Brainfish.Widgets.init({ widgetKey: "your-key" });
</script>

Popup Widget

The popup widget provides a floating button that opens a search interface in a modal overlay.

Basic Usage

<!-- The widget will automatically create a floating button -->
<script>
    Brainfish.Widgets.init({
        widgetKey: "your-widget-key",
        environment: "production" // or "staging", "local"
    });
</script>

Widget Control API

// Open the widget
Brainfish.Widgets.open();

// Open the widget with custom positioning
Brainfish.Widgets.open({
    position: 'bottom-right' // 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right'
});

// Close the widget
Brainfish.Widgets.close();

// Create a new conversation thread
// If widget is open, resets the current conversation
// If widget is closed, opens it with a new conversation
Brainfish.Widgets.createNewThread();

// Check if widget is ready
if (Brainfish.Widgets.isReady) {
    // Widget is ready to use
}

User Management

// Identify a user
Brainfish.Widgets.identify({
    name: "John Doe",
    email: "[email protected]",
    userId: "user_123"
});

// Set secret attributes (for internal use)
Brainfish.Widgets.setSecretAttributes("internal_data_123");

Customization with Overrides

Customize your widget's appearance and behavior using the overrides configuration:

Brainfish.Widgets.init({
    widgetKey: "your-widget-key",
    overrides: {
        // Suggested questions that appear when widget opens
        suggestions: [
            'How do I reset my password?',
            'How do I contact support?',
            'What are your pricing plans?'
        ],

        // Theme customization
        theme: {
            inputButtonBgColor: '#3b82f6',
            inputButtonColor: '#ffffff'
        },

        // Widget settings
        panelTitle: 'Help Center',
        placeholder: 'Ask a question...',

        // Action buttons
        bodyActionButtons: [
            {
                icon: 'Calendar',
                type: 'link',
                label: 'Book a Demo',
                value: 'https://calendly.com/your-company'
            }
        ]
    }
});

📚 For complete customization options, see:

Custom Positioning

Both open() and onContextHelp() methods support custom positioning to control where the widget appears on screen. This is useful for creating contextual experiences or avoiding UI conflicts.

Available Positions:

  • 'bottom-left' - Widget appears in bottom-left corner (default)
  • 'bottom-right' - Widget appears in bottom-right corner
  • 'top-left' - Widget appears in top-left corner
  • 'top-right' - Widget appears in top-right corner

Usage Examples:

// Open widget in different positions
Brainfish.Widgets.open({ position: 'top-right' });
Brainfish.Widgets.open({ position: 'bottom-left' });

// Contextual help with positioning
Brainfish.Widgets.onContextHelp("How do I reset my password?", {
    position: 'top-left'
});

// Dynamic positioning based on user interaction
function showHelpNearElement(question, element) {
    const rect = element.getBoundingClientRect();
    const position = rect.left < window.innerWidth / 2 ? 'bottom-left' : 'bottom-right';

    Brainfish.Widgets.onContextHelp(question, { position });
}

Contextual Help Feature

The contextual help feature allows you to open the widget with a pre-filled question based on user interaction.

// Trigger contextual help with a specific question
Brainfish.Widgets.onContextHelp("How do I reset my password?");

// Trigger contextual help with custom positioning
Brainfish.Widgets.onContextHelp("How do I reset my password?", {
    position: 'top-left' // 'bottom-left' | 'bottom-right' | 'top-left' | 'top-right'
});

Implementation Examples

HTML with Help Icons:

<div class="form-field">
    <label>Email Address</label>
    <input type="email" value="[email protected]">
    <button class="help-icon"
            data-question="How do I change my email address?"
            data-position="top-left">
        <i class="ph ph-question"></i>
    </button>
</div>

<script>
// Add event listeners to help icons with custom positioning
document.querySelectorAll('.help-icon').forEach(icon => {
    icon.addEventListener('click', (e) => {
        const question = e.target.dataset.question;
        const position = e.target.dataset.position || 'bottom-right';
        Brainfish.Widgets.onContextHelp(question, { position });
    });
});
</script>

Direct Button Integration:

<button onclick="Brainfish.Widgets.onContextHelp('How do I reset my password?', { position: 'top-right' })">
    Reset Password
</button>

Programmatic Usage:

function showHelp(question, position = 'bottom-right') {
    Brainfish.Widgets.onContextHelp(question, { position });
}

// Usage with different positions
showHelp("How do I update my profile?", 'top-left');
showHelp("How do I contact support?", 'bottom-right');

Event Listeners

The widget emits events when it opens and closes. You can listen to these events:

// Set up the abort controller
const controller = new AbortController();
const { signal } = controller;

// Listen for widget events
window.addEventListener('onBrainfishWidgetOpen', (event) => {
    console.log('Widget opened');
    // Your custom logic here
}, { signal });

window.addEventListener('onBrainfishWidgetClose', (event) => {
    console.log('Widget closed');
    // Your custom logic here
}, { signal });

// Clean up event listeners when no longer needed
controller.abort();

Analytics Events

The widget automatically tracks analytics events:

  • Open Widget - When the widget is opened
  • Open Widget - Contextual Help - When opened via contextual help
  • Open Widget - Custom Trigger - When opened with custom trigger
  • Close Widget - When the widget is closed

Build

Run the following command to build the widget:

yarn build:web-widget

Testing locally

Start the local server

cd packages/web-widget
npx serve ./ -p 8000 -C

Open the Playground

The playground provides a comprehensive testing environment for the Brainfish widget:

open http://localhost:8000/playground/index.html

The playground includes:

  • Widget Configuration - Set environment, API key, and widget settings
  • Contextual Help Testing - Test the new contextual help feature with sample questions
  • Real-world Demo - See how customers would implement contextual help in their apps
  • API Testing - Test all widget methods and functionality
  • Live Logs - Monitor widget events and debugging information

Test Different Environments

You can test the widget in different environments by passing the env parameter:

# Local environment
open http://localhost:8000/playground/index.html?env=local

# Staging environment
open http://localhost:8000/playground/index.html?env=staging

# Production environment
open http://localhost:8000/playground/index.html?env=prod

Agent Widget Development

If you want to test with the agent widget (Next.js app), start it locally:

cd packages/agent-widget
npm run dev

This will run the agent widget on port 3000, which the playground will automatically connect to in local development mode.

版本与发布记录

@brainfish-ai/web-widget 共发布 257 个版本,首次发布于 2024/08/05;过去 12 个月发布了 32 个版本。

版本发布日期距今
1.25.7latest2026/11/053 天前
1.25.62026/09/241 个月前
1.25.52026/09/241 个月前
1.25.42026/09/211 个月前
1.25.32026/09/141 个月前
1.25.22026/08/202 个月前

其他发布通道:beta → 1.15.0-beta.2,alpha → 1.20.0-alpha.6,rc → 1.20.2-rc.1。

关于 @brainfish-ai/web-widget 的常见问题

@brainfish-ai/web-widget 是什么?

Brainfish Widgets Manager

如何安装 @brainfish-ai/web-widget?

在项目目录运行 npm install @brainfish-ai/web-widget,或使用 pnpm add / yarn add / bun add。

@brainfish-ai/web-widget 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@brainfish-ai/web-widget 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@brainfish-ai/web-widget 可以商用吗?

@brainfish-ai/web-widget 使用 MIT 许可。宽松许可:允许商业使用、修改和再分发,通常只需在分发时保留原版权与许可声明。以上为概括,不构成法律意见,请以许可原文为准。

@brainfish-ai/web-widget 的下载量有多少?

根据 npm 官方下载统计,@brainfish-ai/web-widget 在 2026/10/09 – 2026/11/07 这 30 天内被下载 24,406 次,日均约 814 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入,适合横向比较热度,不能直接等同于使用人数。

@brainfish-ai/web-widget 还在维护吗?

最新版本 1.25.7 发布于 2026/11/05(3 天前);至今共发布 257 个版本,其中过去 12 个月发布了 32 个;首次发布于 2024/08/05。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。