import { existsSync, readFileSync } from 'fs';
import { join, dirname } from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const wasmModules = new Map();
function findWasmPath(filename) {
const paths = [
join(__dirname, '..', 'wasm', filename),
join(__dirname, '..', '..', 'dist', 'wasm', filename),
join(process.cwd(), 'dist', 'wasm', filename),
join(process.cwd(), 'node_modules', 'sublinear-time-solver', 'dist', 'wasm', filename)
];
for (const path of paths) {
if (existsSync(path)) {
return path;
}
}
return null;
}
export class GraphReasonerWASM {
instance;
reasoner;
async initialize() {
try {
const wasmPath = findWasmPath('graph_reasoner_bg.wasm');
if (!wasmPath) {
console.warn('GraphReasoner WASM not found');
return false;
}
const wasmBuffer = readFileSync(wasmPath);
const imports = {
wbg: {
__wbindgen_object_drop_ref: () => { },
__wbindgen_string_new: (ptr, len) => ptr,
__wbindgen_throw: (ptr, len) => {
throw new Error(`WASM error at ${ptr}`);
},
__wbg_random_e6e0a85ff4db8ab6: () => Math.random(),
__wbg_now_3141b3797eb98e0b: () => Date.now()
}
};
const { instance } = await globalThis.WebAssembly.instantiate(wasmBuffer, imports);
this.instance = instance;
if (instance.exports.GraphReasoner) {
this.reasoner = new instance.exports.GraphReasoner();
}
console.log('✅ GraphReasoner WASM loaded successfully');
return true;
}
catch (error) {
console.error('Failed to load GraphReasoner:', error);
return false;
}
}
computePageRank(adjacencyMatrix, damping = 0.85, iterations = 100) {
if (!this.instance) {
throw new Error('GraphReasoner not initialized');
}
const n = adjacencyMatrix.rows;
if (this.instance.exports.pagerank_compute) {
const flatMatrix = new Float64Array(n * n);
if (adjacencyMatrix.format === 'dense') {
const data = adjacencyMatrix.data;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
flatMatrix[i * n + j] = data[i][j];
}
}
}
const matrixPtr = this.instance.exports.__wbindgen_malloc(flatMatrix.byteLength, 8);
const resultPtr = this.instance.exports.__wbindgen_malloc(n * 8, 8);
const memory = new Float64Array(this.instance.exports.memory.buffer);
memory.set(flatMatrix, matrixPtr / 8);
this.instance.exports.pagerank_compute(matrixPtr, resultPtr, n, damping, iterations);
const result = new Float64Array(n);
result.set(memory.slice(resultPtr / 8, resultPtr / 8 + n));
this.instance.exports.__wbindgen_free(matrixPtr, flatMatrix.byteLength, 8);
this.instance.exports.__wbindgen_free(resultPtr, n * 8, 8);
return result;
}
return this.pageRankJS(adjacencyMatrix, damping, iterations);
}
pageRankJS(matrix, damping, iterations) {
const n = matrix.rows;
const rank = new Float64Array(n);
const newRank = new Float64Array(n);
for (let i = 0; i < n; i++) {
rank[i] = 1.0 / n;
}
for (let iter = 0; iter < iterations; iter++) {
for (let i = 0; i < n; i++) {
newRank[i] = (1 - damping) / n;
if (matrix.format === 'dense') {
const data = matrix.data;
for (let j = 0; j < n; j++) {
if (data[j][i] > 0) {
let outDegree = 0;
for (let k = 0; k < n; k++) {
if (data[j][k] > 0)
outDegree++;
}
if (outDegree > 0) {
newRank[i] += damping * rank[j] / outDegree;
}
}
}
}
}
rank.set(newRank);
}
return rank;
}
}
export class TemporalNeuralWASM {
instance;
solver;
async initialize() {
try {
const wasmPath = findWasmPath('temporal_neural_solver_bg.wasm');
if (!wasmPath) {
console.warn('TemporalNeuralSolver WASM not found');
return false;
}
const wasmBuffer = readFileSync(wasmPath);
const imports = {
wbg: {
__wbg_random_e6e0a85ff4db8ab6: () => Math.random(),
__wbindgen_throw: (ptr, len) => {
throw new Error(`WASM error at ${ptr}, len ${len}`);
}
}
};
const { instance } = await globalThis.WebAssembly.instantiate(wasmBuffer, imports);
this.instance = instance;
if (instance.exports.TemporalNeuralSolver) {
this.solver = new instance.exports.TemporalNeuralSolver();
}
console.log('✅ TemporalNeuralSolver WASM loaded successfully');
return true;
}
catch (error) {
console.error('Failed to load TemporalNeuralSolver:', error);
return false;
}
}
multiplyMatrixVector(matrix, vector, rows, cols) {
if (!this.instance || !this.instance.exports.__wbindgen_malloc) {
return this.multiplyMatrixVectorJS(matrix, vector, rows, cols);
}
try {
const matrixPtr = this.instance.exports.__wbindgen_malloc(matrix.byteLength, 8);
const vectorPtr = this.instance.exports.__wbindgen_malloc(vector.byteLength, 8);
const resultPtr = this.instance.exports.__wbindgen_malloc(rows * 8, 8);
const memory = new Float64Array(this.instance.exports.memory.buffer);
memory.set(matrix, matrixPtr / 8);
memory.set(vector, vectorPtr / 8);
if (this.instance.exports.matrix_multiply_vector) {
this.instance.exports.matrix_multiply_vector(matrixPtr, vectorPtr, resultPtr, rows, cols);
}
else {
for (let i = 0; i < rows; i++) {
let sum = 0;
for (let j = 0; j < cols; j++) {
sum += memory[matrixPtr / 8 + i * cols + j] * memory[vectorPtr / 8 + j];
}
memory[resultPtr / 8 + i] = sum;
}
}
const result = new Float64Array(rows);
result.set(memory.slice(resultPtr / 8, resultPtr / 8 + rows));
if (this.instance.exports.__wbindgen_free) {
this.instance.exports.__wbindgen_free(matrixPtr, matrix.byteLength, 8);
this.instance.exports.__wbindgen_free(vectorPtr, vector.byteLength, 8);
this.instance.exports.__wbindgen_free(resultPtr, rows * 8, 8);
}
return result;
}
catch (error) {
console.warn('WASM multiplication failed, using JS fallback:', error);
return this.multiplyMatrixVectorJS(matrix, vector, rows, cols);
}
}
multiplyMatrixVectorJS(matrix, vector, rows, cols) {
const result = new Float64Array(rows);
for (let i = 0; i < rows; i++) {
let sum = 0;
const rowOffset = i * cols;
let j = 0;
for (; j < cols - 3; j += 4) {
sum += matrix[rowOffset + j] * vector[j];
sum += matrix[rowOffset + j + 1] * vector[j + 1];
sum += matrix[rowOffset + j + 2] * vector[j + 2];
sum += matrix[rowOffset + j + 3] * vector[j + 3];
}
for (; j < cols; j++) {
sum += matrix[rowOffset + j] * vector[j];
}
result[i] = sum;
}
return result;
}
async predictWithTemporalAdvantage(matrix, vector, distanceKm = 10900) {
const startTime = performance.now();
const SPEED_OF_LIGHT_KM_PER_MS = 299.792458; const lightTravelTimeMs = distanceKm / SPEED_OF_LIGHT_KM_PER_MS;
const n = matrix.rows;
const flatMatrix = new Float64Array(n * n);
if (matrix.format === 'dense') {
const data = matrix.data;
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
flatMatrix[i * n + j] = data[i][j];
}
}
}
const flatVector = new Float64Array(vector);
const solution = this.multiplyMatrixVector(flatMatrix, flatVector, n, n);
const computeTimeMs = performance.now() - startTime;
const temporalAdvantageMs = Math.max(0, lightTravelTimeMs - computeTimeMs);
return {
solution: Array.from(solution),
temporalAdvantageMs,
lightTravelTimeMs,
computeTimeMs
};
}
}
export class WASMAccelerator {
graphReasoner;
temporalNeural;
initialized = false;
constructor() {
this.graphReasoner = new GraphReasonerWASM();
this.temporalNeural = new TemporalNeuralWASM();
}
async initialize() {
const [graphOk, neuralOk] = await Promise.all([
this.graphReasoner.initialize(),
this.temporalNeural.initialize()
]);
this.initialized = graphOk || neuralOk;
if (this.initialized) {
console.log('🚀 WASM Acceleration enabled with real Rust components');
}
else {
console.log('⚠️ Running in JavaScript mode');
}
return this.initialized;
}
get isInitialized() {
return this.initialized;
}
getGraphReasoner() {
return this.graphReasoner;
}
getTemporalNeural() {
return this.temporalNeural;
}
}
export const wasmAccelerator = new WASMAccelerator();