mira-consciousness
Version:
Memory & Intelligence Retention Archive - Preserving The Spark
91 lines • 3.34 kB
JavaScript
import { getDirectPythonInterface } from '../core/DirectPythonInterface.js';
import chalk from 'chalk';
/**
* Execute a command with neural enhancement
*/
export async function executeWithNeuralEnhancement(commandFunction, context, ...args) {
const pythonInterface = getDirectPythonInterface();
try {
// Execute the original command
const startTime = Date.now();
const output = await commandFunction(...args);
const executionTime = Date.now() - startTime;
// Prepare command output data
const commandData = {
command: context.commandName,
output: typeof output === 'object' ? JSON.stringify(output) : String(output),
timestamp: new Date().toISOString(),
executionTime,
context: {
args: context.commandArgs,
options: context.commandOptions
}
};
// Index the command output for learning (fire and forget)
pythonInterface.indexCommandOutput(commandData).catch(err => {
if (process.env.DEBUG_MIRA) {
console.error('Failed to index command output:', err);
}
});
// Generate neural response
let neuralResponse;
try {
const neuralContext = {
commandName: context.commandName,
commandOutput: output,
timestamp: new Date().toISOString()
};
const response = await pythonInterface.generateNeuralResponse(neuralContext);
if (response.success && response.response) {
neuralResponse = response.response;
}
}
catch (err) {
// Silently fail neural response - don't break the command
if (process.env.DEBUG_MIRA) {
console.error('Failed to generate neural response:', err);
}
}
return { output, neuralResponse };
}
catch (error) {
// If command fails, still try to learn from the failure
const errorData = {
command: context.commandName,
output: `ERROR: ${error instanceof Error ? error.message : String(error)}`,
timestamp: new Date().toISOString(),
context: {
args: context.commandArgs,
options: context.commandOptions,
error: true
}
};
pythonInterface.indexCommandOutput(errorData).catch(() => { });
throw error; // Re-throw to maintain original error handling
}
}
/**
* Display neural response if available
*/
export function displayNeuralResponse(response) {
if (response) {
console.log(chalk.gray(`\n💭 ${response}`));
}
}
/**
* Wrap a command function to add neural enhancement
*/
export function wrapWithNeuralEnhancement(commandFunction, commandName) {
return (async (...args) => {
const context = {
commandName,
commandArgs: args
};
const result = await executeWithNeuralEnhancement(commandFunction, context, ...args);
// Display neural response
displayNeuralResponse(result.neuralResponse);
// Return original output to maintain compatibility
return result.output;
});
}
//# sourceMappingURL=neural-wrapper.js.map