Ask AI Widget - Build Instructions#
đ Project Structure#
docs/sphinx_doc/source/_static/
âââ ask-ai-modules/ # Modularized source code
â âââ ask-ai-i18n.js # Internationalization configuration
â âââ ask-ai-api.js # API communication layer
â âââ ask-ai-ui.js # UI rendering and interaction
â âââ ask-ai-widget.js # Main controller
âââ ask-ai-widget.js # Bundled single file (for production)
âââ ask-ai-widget.js.backup # Original file backup
âââ package.json # Node.js dependency configuration
âââ rollup.config.js # Rollup bundling configuration
âââ BUILD.md # This document
đ Quick Start#
1. Install Dependencies#
Run the following command in the docs/sphinx_doc/source/_static/ directory:
npm install
2. Build and Bundle#
npm run build
This will:
Read all modules in the
ask-ai-modules/directoryBundle them into a single file using Rollup
Output to
ask-ai-widget.js(overwriting the original file)
3. Development Mode (Watch for File Changes)#
npm run watch
In development mode, any modifications to files in ask-ai-modules/ will automatically trigger a rebuild.
đ Module Descriptions#
ask-ai-i18n.js#
Purpose: Internationalization configuration
Exports:
I18Nobject anddetectLanguage()functionSize: ~2KB
ask-ai-api.js#
Purpose: API communication layer
Exports:
AskAIApiclassResponsibilities:
API connection verification
Session history loading
Streaming response handling
Session clearing
Size: ~8KB
ask-ai-ui.js#
Purpose: UI rendering and interaction
Exports:
AskAIUIclassResponsibilities:
DOM creation and management
Event binding
Message rendering
Theme switching
Markdown rendering
Size: ~12KB
ask-ai-widget.js#
Purpose: Main controller
Exports:
AskAIWidgetclass (default export)Responsibilities:
Integration of all modules
Initialization process
Business logic coordination
Size: ~4KB
đ§ Modifying Code#
Modification Workflow#
Edit Module Files: Modify the corresponding module in the
ask-ai-modules/directoryRebuild: Run
npm run buildTest: Test the bundled file in your browser
Adding New Features#
If you need to add new functionality:
Determine which module the feature belongs to (I18N, API, UI, or main controller)
Add code to the corresponding module
If cross-module calls are needed, implement them through export/import mechanisms
Rebuild and test
đĻ Bundling Information#
Bundling Configuration Explanation#
rollup.config.js configuration:
{
input: 'ask-ai-modules/ask-ai-widget.js', // Entry file
output: {
file: 'ask-ai-widget.js', // Output file
format: 'iife', // Immediately Invoked Function Expression
name: 'AskAIWidget', // Global variable name
globals: { marked: 'marked' } // External dependency mapping
},
external: ['marked'] // Don't bundle marked library
}
đ Troubleshooting#
Build Failures#
If the build fails, check:
Whether Node.js version is >= 14
Whether dependencies are correctly installed (
npm install)Whether there are syntax errors in the module files
Function Anomalies#
If functionality is abnormal after bundling:
Check the browser console for errors
Verify that marked.js is correctly loaded