2023-12-21 15:11:38 -07:00
import { AgentProcess } from './src/process/agent-process.js' ;
2024-05-29 21:49:45 -05:00
import settings from './settings.js' ;
2024-09-08 21:07:25 -04:00
import yargs from 'yargs' ;
2024-12-08 22:10:21 -08:00
import { loadTask } from './src/utils/tasks.js' ;
2024-09-08 21:07:25 -04:00
import { hideBin } from 'yargs/helpers' ;
2024-12-08 22:10:21 -08:00
import { readFileSync , writeFileSync } from 'fs' ;
2024-11-18 23:02:37 -06:00
import { createMindServer } from './src/server/mind_server.js' ;
2023-12-09 21:40:53 -06:00
2024-09-08 21:07:25 -04:00
function parseArguments ( ) {
return yargs ( hideBin ( process . argv ) )
. option ( 'profiles' , {
type : 'array' ,
describe : 'List of agent profile paths' ,
} )
2024-12-08 22:10:21 -08:00
. option ( 'task' , {
type : 'string' ,
describe : 'Task ID to execute'
} )
. option ( 'model' , {
type : 'string' ,
describe : 'LLM model to use' ,
} )
2024-09-08 21:07:25 -04:00
. help ( )
. alias ( 'help' , 'h' )
. parse ( ) ;
}
2024-02-25 14:13:32 -06:00
2024-12-08 22:10:21 -08:00
function updateProfile ( profile , args ) {
var temp _profile = JSON . parse ( readFileSync ( profile , 'utf8' ) ) ;
temp _profile . model = args . model ;
writeFileSync ( profile , JSON . stringify ( temp _profile , null , 2 ) ) ;
return profile ;
}
//todo: modify for multiple agents
2024-09-08 21:07:25 -04:00
function getProfiles ( args ) {
2024-12-08 22:10:21 -08:00
if ( args . task ) {
var task = loadTask ( args . task ) ;
}
if ( args . model ) {
if ( ! args . task ) {
settings . profiles = settings . profiles . map ( x => updateProfile ( x , args ) ) ;
}
else {
if ( 'agent_number' in task && task . agent _number > 1 ) {
updateProfile ( './multiagent_prompt_desc.json' , args ) ;
}
else {
updateProfile ( './task_andy.json' , args ) ;
}
}
}
if ( args . task ) {
var task = loadTask ( args . task ) ;
if ( 'agent_number' in task && task . agent _number > 1 ) {
var profile = JSON . parse ( readFileSync ( './multiagent_prompt_desc.json' , 'utf8' ) ) ;
var agent _names = task . agent _names ;
var filenames = [ ] ;
for ( let i = 0 ; i < task . agent _number ; i ++ ) {
let temp _profile = profile ;
temp _profile . name = agent _names [ i ] ;
var filename = ` multi_agent_task_ ${ agent _names [ i ] } .json ` ;
writeFileSync ( filename , JSON . stringify ( temp _profile , null , 2 ) ) ;
filenames . push ( filename ) ;
}
return filenames ;
} else {
return [ './task_andy.json' ] ;
}
}
2024-10-06 21:36:12 -04:00
return args . profiles || settings . profiles ;
2024-09-08 21:07:25 -04:00
}
2024-12-08 22:10:21 -08:00
function determine _init _message ( task , agent _index ) {
if ( task ) {
if ( 'agent_number' in task && task . agent _number > 1 ) {
if ( agent _index == 0 ) {
// first agent gets this init message
return "Immediately start a conversation and collaborate together to complete the task. Share resources and skill sets. Use the !startConversation function if needed."
} // all other agents get this init message
return "Collaborate together to complete the task. Share resources and skill sets."
}
return "Announce your task to everyone and get started with it immediately, set a goal if needed, if cheats are enabled then feel free to use newAction commands, no need to collect or mine or gather any items"
}
return settings . init _message ;
}
2024-11-25 17:15:23 -06:00
async function main ( ) {
2024-12-08 22:10:21 -08:00
2024-11-19 22:21:17 -06:00
if ( settings . host _mindserver ) {
const mindServer = createMindServer ( ) ;
}
2024-09-08 21:07:25 -04:00
const args = parseArguments ( ) ;
2024-12-08 22:10:21 -08:00
if ( args . task ) {
var task = loadTask ( args . task ) ;
// Inject task information into process.env for the agent to access
process . env . MINECRAFT _TASK _GOAL = task . goal ;
if ( 'agent_number' in task && task . agent _number > 1 ) {
process . env . ALL _AGENT _NAMES = task . agent _names ;
console . log ( ` All agents for this task are ${ process . env . ALL _AGENT _NAMES } ` ) ;
}
}
// todo: do inventory
2024-09-08 21:07:25 -04:00
const profiles = getProfiles ( args ) ;
2024-12-08 22:10:21 -08:00
2024-10-06 21:36:12 -04:00
console . log ( profiles ) ;
2024-12-08 22:10:21 -08:00
// var { load_memory, init_message } = settings;
var load _memory = settings . load _memory ;
var init _message = settings . init _message ;
2024-09-08 21:07:25 -04:00
2024-10-10 17:37:02 -05:00
for ( let i = 0 ; i < profiles . length ; i ++ ) {
2024-12-08 22:10:21 -08:00
try {
const agent = new AgentProcess ( ) ;
if ( args . task ) {
init _message = determine _init _message ( task , i ) ;
}
agent . start ( profiles [ i ] , load _memory , init _message , i , args . task ) ;
await new Promise ( resolve => setTimeout ( resolve , 1000 ) ) ;
} catch ( err ) {
console . error ( ` Failed to start agent ${ profiles [ i ] } : ` , err ) ;
}
2024-09-08 21:07:25 -04:00
}
2024-12-08 22:10:21 -08:00
2024-09-08 21:07:25 -04:00
}
try {
main ( ) ;
} catch ( error ) {
console . error ( 'An error occurred:' , error ) ;
process . exit ( 1 ) ;
}