Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multi-Language Support #165

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"dependencies": {
"@anthropic-ai/sdk": "^0.17.1",
"@google/generative-ai": "^0.2.1",
"google-translate-api-x": "^10.7.1",
"minecraft-data": "^3.46.2",
"mineflayer": "^4.20.0",
"mineflayer-armor-manager": "^2.0.1",
Expand Down
4 changes: 3 additions & 1 deletion settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export default

"profiles": [
"./andy.json",

// add more profiles here, check ./profiles/ for more
// more than 1 profile will require you to /msg each bot indivually
],
Expand All @@ -19,4 +20,5 @@ export default
"max_commands": -1, // max number of commands to use in a response. -1 for no limit
"verbose_commands": true, // show full command syntax
"narrate_behavior": true, // chat simple automatic actions ('Picking up item!')
}
"language": "spanish", // the bot will respond/message in this language. All language names are based on google translate's names.
}
22 changes: 14 additions & 8 deletions src/agent/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { NPCContoller } from './npc/controller.js';
import { MemoryBank } from './memory_bank.js';
import { SelfPrompter } from './self_prompter.js';
import settings from '../../settings.js';

import { handleTranslation, handleEnglishTranslation } from './translator.js';

export class Agent {
async start(profile_fp, load_mem=false, init_message=null) {
Expand Down Expand Up @@ -48,16 +48,18 @@ export class Agent {
"Gamerule "
];
const eventname = settings.profiles.length > 1 ? 'whisper' : 'chat';
this.bot.on(eventname, (username, message) => {
this.bot.on(eventname, async (username, message) => {
if (username === this.name) return;

if (ignore_messages.some((m) => message.startsWith(m))) return;

console.log('received message from', username, ':', message);
let translation = await handleEnglishTranslation(message);

console.log('received message from', username, ':', translation);

this.shut_up = false;

this.handleMessage(username, message);
this.handleMessage(username, translation);
});

// set the bot to automatically eat food when hungry
Expand All @@ -77,18 +79,23 @@ export class Agent {
this.handleMessage('system', init_message, 2);
}
else {
this.bot.chat('Hello world! I am ' + this.name);
const translation = await handleTranslation("Hello world! I am");
this.bot.chat(translation+" "+this.name);
this.bot.emit('finished_executing');

}

this.startEvents();
});
}

cleanChat(message) {

async cleanChat(message) {
// newlines are interpreted as separate chats, which triggers spam filters. replace them with spaces
message = message.replaceAll('\n', ' ');
return this.bot.chat(message);
const preferred_lang = settings.preferred_language;
let translation = await handleTranslation(message);
return this.bot.chat(translation);
}

shutUp() {
Expand Down Expand Up @@ -280,4 +287,3 @@ export class Agent {
process.exit(1);
}
}

3 changes: 2 additions & 1 deletion src/agent/commands/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -285,5 +285,6 @@ export const actionsList = [
agent.bot.emit('idle'); // to trigger the goal
return 'Set npc goal: ' + agent.npc.data.curr_goal.name;
}
},
}

];
10 changes: 8 additions & 2 deletions src/agent/modes.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@ import * as skills from './library/skills.js';
import * as world from './library/world.js';
import * as mc from '../utils/mcdata.js';
import settings from '../../settings.js'
import { handleTranslation, handleEnglishTranslation } from './translator.js';

function say(agent, message) {



async function say(agent, message) {
const preferred_lang = settings.preferred_language;
if (agent.shut_up || !settings.narrate_behavior) return;
agent.bot.chat(message);
var translation = await handleTranslation(message);
agent.bot.chat(translation);
}

// a mode is a function that is called every tick to respond immediately to the world
Expand Down
32 changes: 32 additions & 0 deletions src/agent/translator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import translate from 'google-translate-api-x';
import settings from '../../settings.js';


const preferred_lang = settings.language;


export async function handleTranslation(message) {
try {
if (preferred_lang.toLowerCase() === 'en' || preferred_lang.toLowerCase() === 'english') {
return message;
} else {
const lang = String(preferred_lang); // Ensure lang is a string

const translation = await translate(message, { to: lang });
return translation.text || message; // Ensure translation.text is a string
}
} catch (error) {
console.error('Error translating message:', error);
return message; // Fallback to the original message if translation fails
}
}

export async function handleEnglishTranslation(message) {
try {
const translation = await translate(message, { to: 'english' });
return translation.text || message; // Ensures translation.text is a string
} catch (error) {
console.error('Error translating message:', error);
return message; // Fallback to the original message if translation fails
}
}