Skip to content

Commit

Permalink
✨ feat(core): add utils folder
Browse files Browse the repository at this point in the history
  • Loading branch information
angelespejo committed Sep 19, 2024
1 parent f5cec79 commit 73d0257
Show file tree
Hide file tree
Showing 5 changed files with 199 additions and 52 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# smartplant

## 0.1.3

### Patch Changes

- add utils folder and fix issues

## 0.1.2

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "brainvat",
"version": "0.1.2",
"version": "0.1.3",
"description": "Library for crafting and sustaining AI personalities",
"keywords": [
"ia",
Expand Down
67 changes: 16 additions & 51 deletions src/main.js
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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 => {
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 ) {
Expand All @@ -404,15 +378,6 @@ class ResponseGenerator {

}

async executeCommand( command ) {

return spawnSync( command, {
shell : true,
stdio : 'inherit',
} )

}

}

class ConversationManager {
Expand Down Expand Up @@ -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' ) {

Expand Down
5 changes: 5 additions & 0 deletions src/utils/color.js
Original file line number Diff line number Diff line change
@@ -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' )
171 changes: 171 additions & 0 deletions src/utils/process.js
Original file line number Diff line number Diff line change
@@ -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' ) )

} )

} )

}

0 comments on commit 73d0257

Please sign in to comment.