From 73d02578edb0dc3a7c87c42b19d13ad8fbd68bde Mon Sep 17 00:00:00 2001 From: angelespejo Date: Thu, 19 Sep 2024 19:26:44 +0200 Subject: [PATCH] :sparkles: feat(core): add utils folder --- CHANGELOG.md | 6 ++ package.json | 2 +- src/main.js | 67 ++++------------- src/utils/color.js | 5 ++ src/utils/process.js | 171 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 199 insertions(+), 52 deletions(-) create mode 100644 src/utils/color.js create mode 100644 src/utils/process.js diff --git a/CHANGELOG.md b/CHANGELOG.md index f50130b..ebf35a4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # smartplant +## 0.1.3 + +### Patch Changes + +- add utils folder and fix issues + ## 0.1.2 ### Patch Changes diff --git a/package.json b/package.json index 4210245..ae61c73 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "brainvat", - "version": "0.1.2", + "version": "0.1.3", "description": "Library for crafting and sustaining AI personalities", "keywords": [ "ia", diff --git a/src/main.js b/src/main.js index bb46838..4b833bf 100644 --- a/src/main.js +++ b/src/main.js @@ -1,31 +1,17 @@ -import color from 'chalk' + +import natural from 'natural' + import { - spawnSync, - execSync, -} from 'child_process' -import natural from 'natural' -import readline from 'readline' - -const BOLD = color.bold -const REFLEXION_COLOR = color.hex( '#ef8da8' ) -const RESPONSE_COLOR = color.hex( '#15b3ad' ) -const createReadLine = () => { - - const rl = readline.createInterface( { - input : process.stdin, - output : process.stdout, - } ) - readline.cursorTo( process.stdout, 0, 0 ) - readline.clearScreenDown( process.stdout ) - rl.resume() - rl.on( 'close', () => { - - console.warn( '\n\nBye bye! šŸ‘‹\n' ) - - } ) - return rl + BOLD, + REFLEXION_COLOR, + RESPONSE_COLOR, +} from './utils/color.js' +import { + createReadLine, + execChild, + execTemp, +} from './utils/process.js' -} const setTimeString = elapsedTime => `${( elapsedTime / 1000 ).toFixed( 2 )} s` class AIDetector { @@ -34,7 +20,7 @@ class AIDetector { try { - const output = execSync( 'ollama list', { encoding: 'utf-8' } ) + const output = await execChild( 'ollama list' ) const lines = output.split( '\n' ).filter( line => line.trim() ) const models = lines.slice( 1 ).map( line => { @@ -288,7 +274,7 @@ class ReflectionEngine { try { - const reflectionResponse = await this.executeCommand( `ollama run ${aiModel} "${this.sanitizeInput( reflectionPrompt )}"` ) + const reflectionResponse = await execTemp( `ollama run ${aiModel} "${this.sanitizeInput( reflectionPrompt )}"` ) const endTime = Date.now() const elapsedTime = endTime - startTime @@ -333,18 +319,6 @@ class ReflectionEngine { } - async executeCommand( command ) { - - return spawnSync( command, { - shell : true, - stdio : 'inherit', - } ) - - } - // async executeCommandChild(command) { - // return execSync(command, { encoding: 'utf-8' }).trim(); - // } - } class ResponseGenerator { @@ -386,7 +360,7 @@ class ResponseGenerator { try { - const response = await this.executeCommand( `ollama run ${aiModel} "${this.sanitizeInput( responsePrompt )}"` ) + const response = await execTemp( `ollama run ${aiModel} "${this.sanitizeInput( responsePrompt )}"` ) return response } catch ( error ) { @@ -404,15 +378,6 @@ class ResponseGenerator { } - async executeCommand( command ) { - - return spawnSync( command, { - shell : true, - stdio : 'inherit', - } ) - - } - } class ConversationManager { @@ -541,7 +506,7 @@ class ConversationManager { const askQuestion = () => { - this.rl.question( 'Escribe tu pregunta (o "exit" para terminar): ', async userPrompt => { + this.rl.question( 'Write your question (or "exit" to finish):', async userPrompt => { if ( userPrompt.toLowerCase() === 'exit' ) { diff --git a/src/utils/color.js b/src/utils/color.js new file mode 100644 index 0000000..be3dd4b --- /dev/null +++ b/src/utils/color.js @@ -0,0 +1,5 @@ +import color from 'chalk' + +export const BOLD = color.bold +export const REFLEXION_COLOR = color.hex( '#ef8da8' ) +export const RESPONSE_COLOR = color.hex( '#15b3ad' ) diff --git a/src/utils/process.js b/src/utils/process.js new file mode 100644 index 0000000..54bc4e1 --- /dev/null +++ b/src/utils/process.js @@ -0,0 +1,171 @@ +import { + spawn, + spawnSync, +} from 'child_process' +import readline from 'readline' + +export const createReadLine = () => { + + const rl = readline.createInterface( { + input : process.stdin, + output : process.stdout, + } ) + readline.cursorTo( process.stdout, 0, 0 ) + readline.clearScreenDown( process.stdout ) + rl.resume() + rl.on( 'close', () => { + + console.warn( '\n\nBye bye! šŸ‘‹\n' ) + + } ) + return rl + +} +// TODO Change funct fro execChildAndParent +export const execTemp = command =>{ + + return spawnSync( command, { + shell : true, + stdio : 'inherit', + } ) + +} +export const execChild = async command => { + + return new Promise( ( resolve, reject ) => { + + const process = spawn( command, { + shell : true, + stdio : 'pipe', // Cambiado de 'inherit' a 'pipe' para capturar la salida + } ) + + let output = '', + errorOutput = '' + + // Captura la salida estĆ”ndar + process.stdout.on( 'data', data => { + + output += data.toString() + + } ) + + // Captura la salida de errores + process.stderr.on( 'data', data => { + + errorOutput += data.toString() + + } ) + + process.on( 'close', code => { + + if ( code === 0 ) { + + resolve( output ) // Resuelve la promesa con la salida del comando + + } else { + + reject( new Error( `Command failed with code ${code}: ${errorOutput}` ) ) + + } + + } ) + + process.on( 'error', err => { + + reject( new Error( `Failed to start command: ${err.message}` ) ) + + } ) + + // Maneja seƱales de interrupciĆ³n + process.on( 'SIGINT', () => { + + console.log( 'Process interrupted' ) + process.kill() // EnvĆ­a una seƱal para terminar el proceso hijo + reject( new Error( 'Process was interrupted' ) ) + + } ) + + process.on( 'SIGTERM', () => { + + console.log( 'Process terminated' ) + process.kill() // EnvĆ­a una seƱal para terminar el proceso hijo + reject( new Error( 'Process was terminated' ) ) + + } ) + + } ) + +} + +export const execChildAndParent = async command => { + + return new Promise( ( resolve, reject ) => { + + const child = spawn( command, { + shell : true, + stdio : [ + 'pipe', + 'pipe', + 'pipe', + ], + } ) + + let output = '', + errorOutput = '' + + child.stdout.on( 'data', data => { + + process.stdout.write( data ) // Muestra en la terminal + output += data.toString() // Guarda en la variable + + } ) + + // Captura la salida de errores y la muestra en la terminal + child.stderr.on( 'data', data => { + + process.stderr.write( data ) // Muestra en la terminal + errorOutput += data.toString() // Guarda en la variable + + } ) + + child.on( 'close', code => { + + console.log( code ) + if ( code === 0 ) { + + resolve( output ) + + } else { + + reject( new Error( `Command failed with code ${code}: ${errorOutput}` ) ) + + } + + } ) + + child.on( 'error', err => { + + reject( new Error( `Failed to start command: ${err.message}` ) ) + + } ) + + // Maneja seƱales de interrupciĆ³n + child.on( 'SIGINT', () => { + + console.log( 'Process interrupted' ) + child.kill() + reject( new Error( 'Process was interrupted' ) ) + + } ) + + child.on( 'SIGTERM', () => { + + console.log( 'Process terminated' ) + child.kill() + reject( new Error( 'Process was terminated' ) ) + + } ) + + } ) + +}