2025-08-29 15:39:07 -07:00
|
|
|
/**
|
2025-12-18 10:31:02 -08:00
|
|
|
* Model Configuration
|
|
|
|
|
* Brainy uses Q8 WASM embeddings - no configuration needed (zero-config)
|
2025-08-29 15:39:07 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
import { isBrowser, isNode } from '../utils/environment.js'
|
|
|
|
|
|
|
|
|
|
interface ModelConfigResult {
|
2025-12-18 10:31:02 -08:00
|
|
|
precision: 'q8'
|
2025-08-29 15:39:07 -07:00
|
|
|
reason: string
|
|
|
|
|
autoSelected: boolean
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 10:31:02 -08:00
|
|
|
* Get model precision configuration
|
|
|
|
|
* Always returns Q8 - the optimal balance of size and accuracy
|
2025-08-29 15:39:07 -07:00
|
|
|
*/
|
2025-12-18 10:31:02 -08:00
|
|
|
export function getModelPrecision(): ModelConfigResult {
|
2025-09-11 16:23:32 -07:00
|
|
|
return {
|
|
|
|
|
precision: 'q8',
|
2025-12-18 10:31:02 -08:00
|
|
|
reason: 'Q8 WASM (23MB bundled, no downloads)',
|
2025-08-29 15:39:07 -07:00
|
|
|
autoSelected: true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Check if running in a serverless environment
|
|
|
|
|
*/
|
|
|
|
|
function isServerlessEnvironment(): boolean {
|
|
|
|
|
if (!isNode()) return false
|
2025-12-18 10:31:02 -08:00
|
|
|
|
2025-08-29 15:39:07 -07:00
|
|
|
return !!(
|
|
|
|
|
process.env.AWS_LAMBDA_FUNCTION_NAME ||
|
|
|
|
|
process.env.VERCEL ||
|
|
|
|
|
process.env.NETLIFY ||
|
|
|
|
|
process.env.CLOUDFLARE_WORKERS ||
|
|
|
|
|
process.env.FUNCTIONS_WORKER_RUNTIME ||
|
|
|
|
|
process.env.K_SERVICE // Google Cloud Run
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 10:31:02 -08:00
|
|
|
* Check if models need to be downloaded
|
|
|
|
|
* With bundled WASM model, this is rarely needed
|
2025-08-29 15:39:07 -07:00
|
|
|
*/
|
|
|
|
|
export function shouldAutoDownloadModels(): boolean {
|
2025-12-18 10:31:02 -08:00
|
|
|
// Model is bundled - no downloads needed in normal operation
|
|
|
|
|
// This flag exists for edge cases only
|
2025-08-29 15:39:07 -07:00
|
|
|
const explicitlyDisabled = process.env.BRAINY_ALLOW_REMOTE_MODELS === 'false'
|
2025-12-18 10:31:02 -08:00
|
|
|
return !explicitlyDisabled
|
2025-08-29 15:39:07 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2025-12-18 10:31:02 -08:00
|
|
|
* Get the model path
|
|
|
|
|
* With bundled WASM model, this points to the package assets
|
2025-08-29 15:39:07 -07:00
|
|
|
*/
|
|
|
|
|
export function getModelPath(): string {
|
2025-12-18 10:31:02 -08:00
|
|
|
// Check if user explicitly set a path (for advanced users)
|
2025-08-29 15:39:07 -07:00
|
|
|
if (process.env.BRAINY_MODELS_PATH) {
|
|
|
|
|
return process.env.BRAINY_MODELS_PATH
|
|
|
|
|
}
|
2025-12-18 10:31:02 -08:00
|
|
|
|
|
|
|
|
// Browser - use cache API or IndexedDB
|
2025-08-29 15:39:07 -07:00
|
|
|
if (isBrowser()) {
|
|
|
|
|
return 'browser-cache'
|
|
|
|
|
}
|
2025-12-18 10:31:02 -08:00
|
|
|
|
2025-08-29 15:39:07 -07:00
|
|
|
// Serverless - use /tmp for ephemeral storage
|
|
|
|
|
if (isServerlessEnvironment()) {
|
|
|
|
|
return '/tmp/.brainy/models'
|
|
|
|
|
}
|
2025-12-18 10:31:02 -08:00
|
|
|
|
2025-08-29 15:39:07 -07:00
|
|
|
// Node.js - use home directory for persistent storage
|
|
|
|
|
if (isNode()) {
|
|
|
|
|
const homeDir = process.env.HOME || process.env.USERPROFILE || '~'
|
|
|
|
|
return `${homeDir}/.brainy/models`
|
|
|
|
|
}
|
2025-12-18 10:31:02 -08:00
|
|
|
|
2025-08-29 15:39:07 -07:00
|
|
|
// Fallback
|
|
|
|
|
return './.brainy/models'
|
|
|
|
|
}
|