mirror of
https://github.com/kolbytn/mindcraft.git
synced 2025-04-21 21:52:07 +02:00
removed obsolete files
This commit is contained in:
parent
eb885c02b5
commit
e22b1e6e4a
3 changed files with 0 additions and 286 deletions
148
act.js
148
act.js
|
@ -1,148 +0,0 @@
|
|||
import { writeFileSync } from 'fs';
|
||||
|
||||
import { getDetailedSkills, getWorldFunctions } from './utils/context.js';
|
||||
import { sendRequest } from './utils/gpt.js';
|
||||
|
||||
|
||||
function buildSystemMessage(bot) {
|
||||
let message = 'You are a helpful Minecraft bot. Given the dialogue, reflect on what you are doing and generate javascript code to accomplish that goal. Use only functions listed below to write your code.';
|
||||
message += "\n\n" + getDetailedSkills();
|
||||
message += "\n\n" + getWorldFunctions();
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
function buildExamples() {
|
||||
return [
|
||||
`mr_steve2: Will you help me collect wood?
|
||||
|
||||
!blocks
|
||||
\`\`\`
|
||||
NEARBY_BLOCKS
|
||||
- oak_log
|
||||
- dirt
|
||||
- cobblestone
|
||||
\`\`\`
|
||||
|
||||
Me: I'd be glad to help you collect wood.`,
|
||||
`I'm going to help mr_steve2 collect wood. The type of wood block nearby is 'oak_log'. I'll adjust my code to collect an 'oak_log' for mr_steve2.
|
||||
\`\`\`
|
||||
await skills.collectBlock(bot, 'oak_log');
|
||||
await skills.giveToPlayer(bot, 'oak_log', 'mr_steve2');
|
||||
\`\`\``,
|
||||
`sally32: What are you doing?
|
||||
|
||||
!action
|
||||
\`\`\`
|
||||
await skills.equipItem(bot, 'wooden_pickaxe');
|
||||
while (world.getInventory(bot).coal_ore < 10) {
|
||||
await skills.collectBlock(bot, 'coal_ore');
|
||||
}
|
||||
\`\`\`
|
||||
|
||||
Me: I'm looking for coal. Have you seen any?
|
||||
|
||||
sally32: Yes, there's some in this cave, follow me.`,
|
||||
`I'm going to follow sally32 to the cave and collect coal. I'll adjust my code to follow sally32 until I find coal_ore and then I'll mine it.
|
||||
\`\`\`
|
||||
while (true) {
|
||||
await skills.goToPlayer(bot, 'sally32');
|
||||
if (world.getNearbyBlocks(bot).includes('coal_ore')) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
await skills.equipItem(bot, 'wooden_pickaxe');
|
||||
while (world.getInventory(bot).coal_ore < 10) {
|
||||
await skills.collectBlock(bot, 'coal_ore');
|
||||
}
|
||||
\`\`\``,
|
||||
`user42: come here
|
||||
|
||||
Me: Sure! I'm on my way.`,
|
||||
`I'm going to navigate to 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');
|
||||
\`\`\``,
|
||||
]
|
||||
}
|
||||
|
||||
|
||||
export var currentCode = '';
|
||||
|
||||
|
||||
export async function executeCode(bot) {
|
||||
let src = "import * as skills from './utils/skills.js';";
|
||||
src += "\nimport * as world from './utils/world.js';"
|
||||
src += `\n\nexport async function main(bot) {\n`;
|
||||
for (let line of currentCode.split('\n')) {
|
||||
src += ` ${line}\n`;
|
||||
}
|
||||
src += `}\n`;
|
||||
|
||||
writeFileSync('./temp.js', src, (err) => {
|
||||
if (err) throw err;
|
||||
});
|
||||
|
||||
console.log('executing code...\n' + currentCode);
|
||||
try {
|
||||
let ouput = await (await import('./temp.js')).main(bot);
|
||||
console.log(`Code output: *\n${ouput}\n*`);
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
currentCode = '';
|
||||
return false;
|
||||
}
|
||||
currentCode = '';
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
export async function writeCode(bot, username, messages) {
|
||||
let turns = buildExamples();
|
||||
|
||||
// For now, get rid of the first 6 example messages
|
||||
messages = messages.slice(8); // TODO: fix this, very spaghetti
|
||||
|
||||
let startIndex = messages.length - 6;
|
||||
if (startIndex < 0)
|
||||
startIndex = 0;
|
||||
|
||||
let nextTurn = '';
|
||||
for (let i = startIndex; i < messages.length; i++) {
|
||||
if (i % 2 == 0) {
|
||||
nextTurn += `${username}: ${messages[i]}\n\n`;
|
||||
} else {
|
||||
nextTurn += `Me: ${messages[i]}\n\n`;
|
||||
turns.push(nextTurn);
|
||||
nextTurn = '';
|
||||
}
|
||||
}
|
||||
if (nextTurn)
|
||||
turns.push(nextTurn);
|
||||
turns[turns.length - 1] = turns[turns.length - 1].trim();
|
||||
console.log("Action request input:", turns);
|
||||
let systemMessage = buildSystemMessage(bot);
|
||||
let actResponse = await sendRequest(turns, systemMessage);
|
||||
console.log("Action response:", actResponse);
|
||||
|
||||
let code = actResponse.split('\`\`\`');
|
||||
console.log(code);
|
||||
if (code.length <= 1)
|
||||
return code;
|
||||
if (!code[1].trim())
|
||||
return code;
|
||||
|
||||
currentCode = code[1].trim();
|
||||
if (currentCode.slice(0, 10) == 'javascript')
|
||||
currentCode = currentCode.slice(10).trim();
|
||||
|
||||
return currentCode;
|
||||
}
|
53
chat.js
53
chat.js
|
@ -1,53 +0,0 @@
|
|||
import { sendRequest } from './utils/gpt.js';
|
||||
import { getHistory, addEvent } from './utils/history.js';
|
||||
import { containsCommand, getCommand, getCommandDocs } from './utils/commands.js';
|
||||
|
||||
|
||||
function buildSystemMessage(bot) {
|
||||
let message = `You are a playful Minecraft bot that can communicate with players and move within and interact with the world.';
|
||||
Act human-like as if you were a typical Minecraft player, rather than an AI.';
|
||||
Do not give instructions unless asked, and always be brief in your responses.\n`;
|
||||
message += getCommandDocs();
|
||||
return message;
|
||||
}
|
||||
|
||||
|
||||
|
||||
const MAX_TURNS = 5;
|
||||
export async function getChatResponse(bot, user, message) {
|
||||
addEvent(user, message);
|
||||
let turns = getHistory(user);
|
||||
let systemMessage = buildSystemMessage();
|
||||
|
||||
let botFinalRes = '';
|
||||
let botEvent = '';
|
||||
let botRes = null;
|
||||
console.log("*recieved chat:", message);
|
||||
for (let i = 0; i < MAX_TURNS; i++) {
|
||||
botRes = await sendRequest(turns, systemMessage, '\`\`\`');
|
||||
console.log(`bot response ${i}: "${botRes}"`);
|
||||
let command_name = containsCommand(botRes)
|
||||
if (command_name) {
|
||||
let command = getCommand(command_name);
|
||||
let commandRes = await command.perform(bot, user, turns.concat(botRes));
|
||||
|
||||
botEvent += `/n${command.name}/n${commandRes}`;
|
||||
if (i == 0)
|
||||
turns.push(botEvent);
|
||||
else
|
||||
turns[turns.length - 1] += botEvent;
|
||||
if (command_name == '!execute') {
|
||||
botFinalRes = "Executing Code";
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
botFinalRes = botRes;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
console.log('*bot response', botFinalRes);
|
||||
console.log('*bot event', botEvent);
|
||||
addEvent('bot', turns[turns.length - 1]);
|
||||
return botFinalRes.trim();
|
||||
}
|
|
@ -1,85 +0,0 @@
|
|||
import { getStats, getInventory, getBlocks, getNearbyEntities, getCraftable } from './context.js';
|
||||
import { currentCode, writeCode } from '../act.js';
|
||||
|
||||
const pad = (str) => {
|
||||
return '\n\`\`\`\n' + str + '\n\`\`\`';
|
||||
}
|
||||
|
||||
const commandsList = [
|
||||
{
|
||||
name: "!stats",
|
||||
description: "Get the bot's stats (name, health, food, saturation, armor, held item, position, velocity, gamemode, experience, level, effects).",
|
||||
perform: function (bot, user, turns) {
|
||||
return pad(getStats(bot));
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!inventory",
|
||||
description: "Get the bot's inventory.",
|
||||
perform: function (bot, user, turns) {
|
||||
return pad(getInventory(bot));
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!blocks",
|
||||
description: "Get the blocks near the bot.",
|
||||
perform: function (bot, user, turns) {
|
||||
return pad(getBlocks(bot));
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!craftable",
|
||||
description: "Get the craftable items with the bot's inventory.",
|
||||
perform: function (bot, user, turns) {
|
||||
return pad(getCraftable(bot));
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!entities",
|
||||
description: "Get the nearby players and entities.",
|
||||
perform: function (bot, user, turns) {
|
||||
return pad(getNearbyEntities(bot));
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!action",
|
||||
description: "Get the currently executing code.",
|
||||
perform: function (bot, user, turns) {
|
||||
return pad(currentCode(bot));
|
||||
}
|
||||
},
|
||||
{
|
||||
name: "!execute",
|
||||
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, user, turns) {
|
||||
return writeCode(bot, user, turns);
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
const commandsMap = {};
|
||||
for (let command of commandsList) {
|
||||
commandsMap[command.name] = command;
|
||||
}
|
||||
|
||||
export function getCommand(name) {
|
||||
return commandsMap[name];
|
||||
}
|
||||
|
||||
export function containsCommand(message) {
|
||||
for (let command of commandsList) {
|
||||
if (message.includes(command.name)) {
|
||||
return command.name;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
export function getCommandDocs() {
|
||||
let docs = `COMMAND DOCS\n***\n You can use the following commands to query for information about the world.
|
||||
The first word of your response must be a command name in order to use commands. \n`;
|
||||
for (let command of commandsList) {
|
||||
docs += command.name + ': ' + command.description + '\n';
|
||||
}
|
||||
return docs + '\n***\n';
|
||||
}
|
Loading…
Add table
Reference in a new issue