2025-07-14 11:12:51 -07:00
|
|
|
#!/usr/bin/env node
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Example CLI wrapper for Brainy that properly handles TensorFlow.js initialization
|
2025-07-15 11:53:42 -07:00
|
|
|
*
|
2025-07-14 11:12:51 -07:00
|
|
|
* This example demonstrates how to create a CLI tool that uses Brainy
|
|
|
|
|
* while ensuring TensorFlow.js is properly initialized in Node.js environments.
|
2025-07-15 11:53:42 -07:00
|
|
|
*
|
2025-07-14 11:12:51 -07:00
|
|
|
* Usage:
|
|
|
|
|
* node cli-wrapper-example.js
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
// CRITICAL: Apply the TensorFlow.js patch before any other imports
|
|
|
|
|
// This prevents the "TextEncoder is not a constructor" error
|
|
|
|
|
try {
|
|
|
|
|
// For CommonJS environments
|
|
|
|
|
if (typeof require === 'function') {
|
|
|
|
|
// First require the setup module to apply the patch
|
2025-07-15 11:53:42 -07:00
|
|
|
require('../dist/setup.js')
|
|
|
|
|
console.log('Applied TensorFlow.js patch via CommonJS require')
|
2025-07-14 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
} catch (error) {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.warn('Failed to apply TensorFlow.js patch via require:', error)
|
2025-07-14 11:12:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ES Modules approach - this will be used if the above fails or if using ES modules
|
|
|
|
|
import('../dist/setup.js')
|
|
|
|
|
.then(() => {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.log('Applied TensorFlow.js patch via ES modules import')
|
|
|
|
|
return import('../dist/unified.js')
|
2025-07-14 11:12:51 -07:00
|
|
|
})
|
|
|
|
|
.then((brainy) => {
|
|
|
|
|
// Now it's safe to use Brainy and TensorFlow.js
|
2025-07-15 11:53:42 -07:00
|
|
|
console.log('Brainy loaded successfully')
|
|
|
|
|
|
2025-07-14 11:12:51 -07:00
|
|
|
// Example: Create a BrainyData instance
|
|
|
|
|
const db = new brainy.BrainyData({
|
|
|
|
|
name: 'cli-example',
|
|
|
|
|
storage: 'memory'
|
2025-07-15 11:53:42 -07:00
|
|
|
})
|
|
|
|
|
|
2025-07-14 11:12:51 -07:00
|
|
|
// Example: Add some data
|
|
|
|
|
db.addItem('Hello world', { id: '1', metadata: { type: 'greeting' } })
|
|
|
|
|
.then(() => {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.log('Added item to database')
|
|
|
|
|
|
2025-07-14 11:12:51 -07:00
|
|
|
// Example: Search for similar items
|
2025-07-15 11:53:42 -07:00
|
|
|
return db.search('Hello', 1)
|
2025-07-14 11:12:51 -07:00
|
|
|
})
|
|
|
|
|
.then((results) => {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.log('Search results:', results)
|
|
|
|
|
|
2025-07-14 11:12:51 -07:00
|
|
|
// Clean up
|
2025-07-15 11:53:42 -07:00
|
|
|
return db.close()
|
2025-07-14 11:12:51 -07:00
|
|
|
})
|
|
|
|
|
.then(() => {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.log('Database closed')
|
|
|
|
|
process.exit(0)
|
2025-07-14 11:12:51 -07:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.error('Error in Brainy operations:', error)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
})
|
2025-07-14 11:12:51 -07:00
|
|
|
})
|
|
|
|
|
.catch((error) => {
|
2025-07-15 11:53:42 -07:00
|
|
|
console.error('Failed to load Brainy:', error)
|
|
|
|
|
process.exit(1)
|
|
|
|
|
})
|