fixed some stuff...

This commit is contained in:
MaxRobinsonTheGreat 2023-11-05 15:38:52 -06:00
parent 8c5e4406cf
commit 1b09360172
4 changed files with 61 additions and 34 deletions

30
act.js
View file

@ -63,7 +63,15 @@ Me: Sure! I'm on my way.`,
\`\`\` \`\`\`
await skills.goToPlayer(bot, 'user42'); await skills.goToPlayer(bot, 'user42');
\`\`\``, \`\`\``,
]
`user42: execute some code that says "hello world"
Me: Okay, I'll do that now.`,
`I'm going to log "hello world" to the console.
\`\`\`
console.log('hello world');
\`\`\``,
]
} }
@ -85,13 +93,13 @@ export async function executeCode(bot) {
console.log('executing code...\n' + currentCode); console.log('executing code...\n' + currentCode);
try { try {
await (await import('./temp.js')).main(bot); let ouput = await (await import('./temp.js')).main(bot);
console.log(`Code output: *\n${ouput}\n*`);
} catch (err) { } catch (err) {
console.log(err); console.log(err);
currentCode = ''; currentCode = '';
return false; return false;
} }
currentCode = ''; currentCode = '';
return true; return true;
} }
@ -101,26 +109,32 @@ export async function writeCode(bot, username, messages) {
let turns = buildExamples(); let turns = buildExamples();
// For now, get rid of the first 6 example messages // For now, get rid of the first 6 example messages
messages = messages.slice(6); messages = messages.slice(8); // TODO: fix this, very spaghetti
let startIndex = messages.length - 6; let startIndex = messages.length - 6;
if (startIndex < 0) if (startIndex < 0)
startIndex = 0; startIndex = 0;
turns.push(''); let nextTurn = '';
for (let i = startIndex; i < messages.length; i++) { for (let i = startIndex; i < messages.length; i++) {
if (i % 2 == 0) { if (i % 2 == 0) {
turns[turns.length - 1] += `\n\n${username}: ${messages[i]}`; nextTurn += `${username}: ${messages[i]}\n\n`;
} else { } else {
turns[turns.length - 1] += `\n\nMe: ${messages[i]}`; nextTurn += `Me: ${messages[i]}\n\n`;
turns.push(nextTurn);
nextTurn = '';
} }
} }
if (nextTurn)
turns.push(nextTurn);
turns[turns.length - 1] = turns[turns.length - 1].trim(); turns[turns.length - 1] = turns[turns.length - 1].trim();
console.log("Action request input:", turns);
let systemMessage = buildSystemMessage(bot); let systemMessage = buildSystemMessage(bot);
let actResponse = await sendRequest(turns, systemMessage); let actResponse = await sendRequest(turns, systemMessage);
console.log(actResponse); console.log("Action response:", actResponse);
let code = actResponse.split('\`\`\`'); let code = actResponse.split('\`\`\`');
console.log(code);
if (code.length <= 1) if (code.length <= 1)
return code; return code;
if (!code[1].trim()) if (!code[1].trim())

23
chat.js
View file

@ -1,6 +1,6 @@
import { sendRequest } from './utils/gpt.js'; import { sendRequest } from './utils/gpt.js';
import { getHistory, addEvent } from './utils/history.js'; import { getHistory, addEvent } from './utils/history.js';
import { getCommand, getCommandDocs } from './utils/commands.js'; import { containsCommand, getCommand, getCommandDocs } from './utils/commands.js';
function buildSystemMessage(bot) { function buildSystemMessage(bot) {
@ -22,23 +22,24 @@ export async function getChatResponse(bot, user, message) {
let botFinalRes = ''; let botFinalRes = '';
let botEvent = ''; let botEvent = '';
let botRes = null; let botRes = null;
console.log("*recieved chat:", message) console.log("*recieved chat:", message);
for (let i = 0; i < MAX_TURNS; i++) { for (let i = 0; i < MAX_TURNS; i++) {
botRes = await sendRequest(turns, systemMessage, '\`\`\`'); botRes = await sendRequest(turns, systemMessage, '\`\`\`');
console.log(`bot response ${i}:`, botRes); console.log(`bot response ${i}: "${botRes}"`);
let command_name = containsCommand(botRes)
let commandRes = null; if (command_name) {
let firstword = botRes.trim().split(/\s+/)[0]; let command = getCommand(command_name);
let command = getCommand(firstword); let commandRes = await command.perform(bot, user, turns.concat(botRes));
if (command) {
console.log('Executing command:', command.name)
commandRes = await command.perform(bot);
botEvent += `/n${command.name}/n${commandRes}`; botEvent += `/n${command.name}/n${commandRes}`;
if (i == 0) if (i == 0)
turns.push(botEvent); turns.push(botEvent);
else else
turns[turns.length - 1] += botEvent; turns[turns.length - 1] += botEvent;
if (command_name == '!execute') {
botFinalRes = "Executing Code";
break;
}
} else { } else {
botFinalRes = botRes; botFinalRes = botRes;
break; break;
@ -47,6 +48,6 @@ export async function getChatResponse(bot, user, message) {
console.log('*bot response', botFinalRes); console.log('*bot response', botFinalRes);
console.log('*bot event', botEvent); console.log('*bot event', botEvent);
addEvent('bot', botEvent); addEvent('bot', turns[turns.length - 1]);
return botFinalRes.trim(); return botFinalRes.trim();
} }

View file

@ -1,5 +1,5 @@
import { getStats, getInventory, getBlocks, getNearbyEntities, getCraftable } from './context.js'; import { getStats, getInventory, getBlocks, getNearbyEntities, getCraftable } from './context.js';
import { currentCode, executeCode, writeCode } from '../act.js'; import { currentCode, writeCode } from '../act.js';
const pad = (str) => { const pad = (str) => {
return '\n\`\`\`\n' + str + '\n\`\`\`'; return '\n\`\`\`\n' + str + '\n\`\`\`';
@ -9,50 +9,50 @@ const commandsList = [
{ {
name: "!stats", name: "!stats",
description: "Get the bot's stats (name, health, food, saturation, armor, held item, position, velocity, gamemode, experience, level, effects).", description: "Get the bot's stats (name, health, food, saturation, armor, held item, position, velocity, gamemode, experience, level, effects).",
perform: function (bot) { perform: function (bot, user, turns) {
return pad(getStats(bot)); return pad(getStats(bot));
} }
}, },
{ {
name: "!inventory", name: "!inventory",
description: "Get the bot's inventory.", description: "Get the bot's inventory.",
perform: function (bot) { perform: function (bot, user, turns) {
return pad(getInventory(bot)); return pad(getInventory(bot));
} }
}, },
{ {
name: "!blocks", name: "!blocks",
description: "Get the blocks near the bot.", description: "Get the blocks near the bot.",
perform: function (bot) { perform: function (bot, user, turns) {
return pad(getBlocks(bot)); return pad(getBlocks(bot));
} }
}, },
{ {
name: "!craftable", name: "!craftable",
description: "Get the craftable items with the bot's inventory.", description: "Get the craftable items with the bot's inventory.",
perform: function (bot) { perform: function (bot, user, turns) {
return pad(getCraftable(bot)); return pad(getCraftable(bot));
} }
}, },
{ {
name: "!entities", name: "!entities",
description: "Get the nearby players and entities.", description: "Get the nearby players and entities.",
perform: function (bot) { perform: function (bot, user, turns) {
return pad(getNearbyEntities(bot)); return pad(getNearbyEntities(bot));
} }
}, },
{ {
name: "!action", name: "!action",
description: "Get the currently executing code.", description: "Get the currently executing code.",
perform: function (bot) { perform: function (bot, user, turns) {
return pad(currentCode(bot)); return pad(currentCode(bot));
} }
}, },
{ {
name: "!execute", name: "!execute",
description: "Execute actions in the game by writing and calling javascript code with the following command: \n!execute\n\`\`\`\nCODE\n\`\`\`", description: "Write javascript code to move, mine, build, or do anything else in the minecraft world. Example usage: \n!execute\n\`\`\`\nCODE\n\`\`\`",
perform: function (bot) { perform: function (bot, user, turns) {
return writeCode(bot, user, turns.concat(botResponse), botResponse); return writeCode(bot, user, turns);
} }
} }
]; ];
@ -66,13 +66,18 @@ export function getCommand(name) {
return commandsMap[name]; return commandsMap[name];
} }
export function commandExists(name) { export function containsCommand(message) {
return commandsMap[name] != undefined; for (let command of commandsList) {
if (message.includes(command.name)) {
return command.name;
}
}
return null;
} }
export function getCommandDocs() { export function getCommandDocs() {
let docs = `COMMAND DOCS\n***\n You can use the following commands followed by to query for information about the world. let docs = `COMMAND DOCS\n***\n You can use the following commands to query for information about the world.
Some are not implemented yet and will return null. Respond with only the command name to request information.\n`; The first word of your response must be a command name in order to use commands. \n`;
for (let command of commandsList) { for (let command of commandsList) {
docs += command.name + ': ' + command.description + '\n'; docs += command.name + ': ' + command.description + '\n';
} }

View file

@ -29,7 +29,14 @@ while (true) {
await skills.goToPlayer(bot, 'username'); await skills.goToPlayer(bot, 'username');
await skills.DropItem(bot, 'oak_log', 1); await skills.DropItem(bot, 'oak_log', 1);
} }
\`\`\``} \`\`\``},
{'source': 'all', 'message': 'come here'},
{'source': 'bot', 'message': `Sure! I'm on my way.
!execute
\`\`\`
await skills.goToPlayer(bot, 'user42');
\`\`\``},
]; ];