**chore(scripts): add utility scripts and demo for database checks, CLI handling, and model bundling**

- Added `check-database.js`:
  - Utility script to check database health and statistics, including nouns, verbs, and sample query results.
  - Includes test item addition for cases with no data.

- Added `cli-wrapper.js`:
  - Wrapper script ensuring proper argument passing and CLI script availability.
  - Handles version flag, force flag addition, and automatic CLI building in local development contexts.

- Added `demo-optional-model-bundling.js`:
  - Demonstration script showcasing optional model bundling benefits for reliability and offline support in embedding workflows.
  - Includes examples of compression, package structures, and use-case comparisons.

**Purpose**: Introduce utility and demonstration scripts
This commit is contained in:
David Snelling 2025-08-01 16:23:15 -07:00
parent e476d45fac
commit 90eccd75aa
9 changed files with 867 additions and 2 deletions

View file

@ -0,0 +1,85 @@
#!/usr/bin/env node
/**
* CLI Wrapper Script
*
* This script serves as a wrapper for the Brainy CLI, ensuring that command-line arguments
* are properly passed to the CLI when invoked through npm scripts.
*/
import { spawn, execSync } from 'child_process'
import { fileURLToPath } from 'url'
import { dirname, join } from 'path'
import fs from 'fs'
// Get the directory of the current module
const __filename = fileURLToPath(import.meta.url)
const __dirname = dirname(__filename)
// Path to the actual CLI script
const cliPath = join(__dirname, 'dist', 'cli.js')
// Check if the CLI script exists
if (!fs.existsSync(cliPath)) {
// Check if we're running in a global installation context
const isGlobalInstall = __dirname.includes('node_modules') && !__dirname.includes('node_modules/.')
if (isGlobalInstall) {
console.error(`Error: CLI script not found at ${cliPath}`)
console.error('This is likely because the CLI was not built during package installation.')
console.error('Please reinstall the package with:')
console.error('npm uninstall -g @soulcraft/brainy')
console.error('npm install -g @soulcraft/brainy --legacy-peer-deps')
process.exit(1)
} else {
// In a local development context, try to build the CLI
console.log(`CLI script not found at ${cliPath}. Building CLI...`)
try {
// Run the build:cli script
execSync('npm run build:cli', { stdio: 'inherit' })
// Check again if the CLI script exists after building
if (!fs.existsSync(cliPath)) {
console.error(`Error: Failed to build CLI script at ${cliPath}`)
process.exit(1)
}
console.log('CLI built successfully.')
} catch (error) {
console.error(`Error building CLI: ${error.message}`)
console.error('Make sure you have the necessary dependencies installed.')
process.exit(1)
}
}
}
// Special handling for version flags
if (process.argv.includes('--version') || process.argv.includes('-V')) {
// Read version directly from package.json to ensure it's always correct
try {
const packageJsonPath = join(__dirname, 'package.json')
const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf8'))
console.log(packageJson.version)
process.exit(0)
} catch (error) {
console.error('Error loading version information:', error.message)
process.exit(1)
}
}
// Forward all arguments to the CLI script
const args = process.argv.slice(2)
// Check if npm is passing --force flag
// When npm runs with --force, it sets the npm_config_force environment variable
if (process.env.npm_config_force === 'true' && args.includes('clear') && !args.includes('--force') && !args.includes('-f')) {
args.push('--force')
}
const cli = spawn('node', [cliPath, ...args], { stdio: 'inherit' })
cli.on('close', (code) => {
process.exit(code)
})

File diff suppressed because one or more lines are too long

View file

@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brainy Interactive Demo - Redirecting...</title>
<link rel="icon" href="brainy.png" type="image/png">
<meta http-equiv="refresh" content="0;url=demo/index.html">
<script>
window.location.href = 'demo/index.html'
</script>
</head>
<body>
<p>Redirecting to <a href="demo/index.html">Brainy Interactive Demo</a>...</p>
<p>If you are not redirected automatically, please click the link above.</p>
</body>
</html>

View file

@ -0,0 +1,78 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brainy Cache Detection Browser Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
#results {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
.success {
color: green;
font-weight: bold;
}
.error {
color: red;
font-weight: bold;
}
</style>
</head>
<body>
<h1>Brainy Cache Detection Browser Test</h1>
<p>This page tests if Brainy's cache detection works properly in browser environments.</p>
<button id="runTest">Run Test</button>
<div id="results">
<p>Test results will appear here...</p>
</div>
<script type="module">
import { BrainyData } from './dist/unified.js';
document.getElementById('runTest').addEventListener('click', async () => {
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '<p>Running test...</p>';
try {
console.log('Creating BrainyData instance...');
resultsDiv.innerHTML += '<p>Creating BrainyData instance...</p>';
const brainy = new BrainyData();
console.log('BrainyData instance created successfully!');
resultsDiv.innerHTML += '<p class="success">BrainyData instance created successfully!</p>';
// Initialize to make sure all components are created
await brainy.init();
console.log('BrainyData initialized successfully!');
resultsDiv.innerHTML += '<p class="success">BrainyData initialized successfully!</p>';
// Add a simple item to verify everything works
const id = await brainy.add("Test item for browser cache detection");
console.log('Added test item with ID:', id);
resultsDiv.innerHTML += `<p class="success">Added test item with ID: ${id}</p>`;
resultsDiv.innerHTML += '<p class="success">✅ Test completed successfully! Cache detection works in browser environment.</p>';
} catch (error) {
console.error('Error during cache detection test:', error);
resultsDiv.innerHTML += `<p class="error">❌ Error during test: ${error.message}</p>`;
resultsDiv.innerHTML += `<p class="error">Stack trace: ${error.stack}</p>`;
}
});
</script>
</body>
</html>

View file

@ -0,0 +1,130 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Brainy Cache Detection Worker Test</title>
<style>
body {
font-family: Arial, sans-serif;
max-width: 800px;
margin: 0 auto;
padding: 20px;
}
#results {
margin-top: 20px;
padding: 10px;
border: 1px solid #ccc;
border-radius: 5px;
background-color: #f9f9f9;
}
.success {
color: green;
font-weight: bold;
}
.error {
color: red;
font-weight: bold;
}
.log {
color: #333;
margin: 5px 0;
}
</style>
</head>
<body>
<h1>Brainy Cache Detection Worker Test</h1>
<p>This page tests if Brainy's cache detection works properly in Web Worker environments.</p>
<button id="runTest">Run Test</button>
<div id="results">
<p>Test results will appear here...</p>
</div>
<script type="module">
document.getElementById('runTest').addEventListener('click', () => {
const resultsDiv = document.getElementById('results');
resultsDiv.innerHTML = '<p>Starting worker test...</p>';
try {
// Create a blob URL for the worker script
const workerScript = `
// Worker script for testing cache detection
import { BrainyData } from './dist/unified.js';
// Listen for messages from the main thread
self.onmessage = async (event) => {
if (event.data === 'start-test') {
try {
self.postMessage({ type: 'log', message: 'Creating BrainyData instance...' });
const brainy = new BrainyData();
self.postMessage({ type: 'log', message: 'BrainyData instance created successfully!' });
// Initialize to make sure all components are created
await brainy.init();
self.postMessage({ type: 'log', message: 'BrainyData initialized successfully!' });
// Add a simple item to verify everything works
const id = await brainy.add("Test item for worker cache detection");
self.postMessage({ type: 'log', message: \`Added test item with ID: \${id}\` });
self.postMessage({
type: 'success',
message: 'Test completed successfully! Cache detection works in worker environment.'
});
} catch (error) {
self.postMessage({
type: 'error',
message: \`Error during test: \${error.message}\`,
stack: error.stack
});
}
}
};
// Notify that the worker is ready
self.postMessage({ type: 'ready' });
`;
const blob = new Blob([workerScript], { type: 'application/javascript' });
const workerUrl = URL.createObjectURL(blob);
// Create the worker
const worker = new Worker(workerUrl, { type: 'module' });
// Handle messages from the worker
worker.onmessage = (event) => {
const data = event.data;
if (data.type === 'ready') {
resultsDiv.innerHTML += '<p class="log">Worker is ready. Starting test...</p>';
worker.postMessage('start-test');
} else if (data.type === 'log') {
resultsDiv.innerHTML += `<p class="log">[Worker] ${data.message}</p>`;
} else if (data.type === 'success') {
resultsDiv.innerHTML += `<p class="success">✅ ${data.message}</p>`;
} else if (data.type === 'error') {
resultsDiv.innerHTML += `<p class="error">❌ ${data.message}</p>`;
if (data.stack) {
resultsDiv.innerHTML += `<p class="error">Stack trace: ${data.stack}</p>`;
}
}
};
// Handle worker errors
worker.onerror = (error) => {
resultsDiv.innerHTML += `<p class="error">❌ Worker error: ${error.message}</p>`;
};
} catch (error) {
resultsDiv.innerHTML += `<p class="error">❌ Error creating worker: ${error.message}</p>`;
console.error('Error creating worker:', error);
}
});
</script>
</body>
</html>