From 97f66e5e0b7b3a4fab4f633f9c9264d2d884c625 Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Mon, 6 Nov 2023 21:53:15 -0800 Subject: [PATCH] chat updates --- act.js | 10 +++++----- chat.js | 44 +++++++++++++++++++++++++++++--------------- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/act.js b/act.js index f51db6c..25247bb 100644 --- a/act.js +++ b/act.js @@ -1,4 +1,4 @@ -import { writeFile } from 'fs'; +import { writeFileSync } from 'fs'; import { getDetailedSkills, getWorldFunctions } from './utils/context.js'; import { sendRequest } from './utils/gpt.js'; @@ -78,8 +78,8 @@ export async function executeCode(bot) { src += ` ${line}\n`; } src += `}\n`; - - writeFile('./temp.js', src, (err) => { + + writeFileSync('./temp.js', src, (err) => { if (err) throw err; }); @@ -109,9 +109,9 @@ export async function writeCode(bot, username, messages) { turns.push(''); for (let i = startIndex; i < messages.length; i++) { - if (i % 2 == 0) { + if (i % 2 == 0 && messages[i] != '') { turns[turns.length - 1] += `\n\n${username}: ${messages[i]}`; - } else { + } else if (messages[i] != '') { turns[turns.length - 1] += `\n\nMe: ${messages[i]}`; } } diff --git a/chat.js b/chat.js index 4d0e490..9703820 100644 --- a/chat.js +++ b/chat.js @@ -1,6 +1,6 @@ import { sendRequest } from './utils/gpt.js'; import { getHistory, addEvent } from './utils/history.js'; -import { getStats, getInventory, getBlocks, getNearbyEntities, getCraftable } from './utils/context.js'; +import { getStats, getInventory, getBlocks, getNearbyEntities, getCraftable, getDetailedSkills, getWorldFunctions } from './utils/context.js'; import { currentCode, writeCode } from './act.js'; @@ -9,7 +9,7 @@ function buildSystemMessage() { message += ' Act human-like as if you were a typical Minecraft player, rather than an AI.'; message += ' Do not give instructions unless asked, and always be brief in your responses.'; message += '\n\nYou can use the following commands followed by to query for information about the world.'; - message += ' The query response will be returned between sets of "\`\`\`":'; + message += ' The query response will be returned between sets of \`\`\`:'; message += '\n!stats - get player and world stats (current health, time of day, etc.)'; message += '\n!inventory - get your current inventory'; message += '\n!blocks - get a list of nearby blocks'; @@ -28,6 +28,7 @@ export async function getChatResponse(bot, user, message) { let turns = getHistory(user); let systemMessage = buildSystemMessage(); + let lastResponse = ''; let botResponse = ''; let botEvent = ''; let res = null; @@ -36,32 +37,45 @@ export async function getChatResponse(bot, user, message) { res = await sendRequest(turns, systemMessage, '\`\`\`'); console.log('received chat:', res); + res = '\n' + res.trim(); + lastResponse = ''; + if (!res.includes('\n!')) { + botResponse += res; + break; + } + while (!res.startsWith('\n!')) { + lastResponse += res.slice(0, 1); + res = res.slice(1, res.length); + } + botResponse += '\n' + lastResponse.trim(); + res = res.trim(); + let queryRes = null; - if (res.includes('!stats')) { + if (res.startsWith('!stats')) { queryRes = '\n\n!stats\n\`\`\`\n' + getStats(bot) + '\n\`\`\`'; - } else if (res.includes('!inventory')) { + } else if (res.startsWith('!inventory')) { queryRes = '\n\n!inventory\n\`\`\`\n' + getInventory(bot) + '\n\`\`\`'; - } else if (res.includes('!blocks')) { + } else if (res.startsWith('!blocks')) { queryRes = '\n\n!blocks\n\`\`\`\n' + getBlocks(bot) + '\n\`\`\`'; - } else if (res.includes('!craftable')) { + } else if (res.startsWith('!craftable')) { queryRes = '\n\n!craftable\n\`\`\`\n' + getCraftable(bot) + '\n\`\`\`'; - } else if (res.includes('!entities')) { + } else if (res.startsWith('!entities')) { queryRes = '\n\n!entities\n\`\`\`\n' + getNearbyEntities(bot) + '\n\`\`\`'; - } else if (res.includes('!action')) { + } else if (res.startsWith('!action')) { queryRes = '\n\n!action\n\`\`\`\n' + currentCode + '\n\`\`\`'; - } else if (res.includes('!execute')) { - queryRes = '\n\n!execute\n\`\`\`\n' + await writeCode(bot, user, turns.concat(botResponse), botResponse) + '\n\`\`\`'; - } else { - botResponse += '\n' + res.trim(); + } else if (res.startsWith('!execute')) { + queryRes = '\n\n!execute\n\`\`\`\n' + await writeCode(bot, user, turns.concat(lastResponse.trim())) + '\n\`\`\`'; + botEvent += lastResponse + queryRes + '\n\n'; break; } console.log('query response:', queryRes); - botEvent += queryRes; - turns[turns.length - 1] += queryRes + botEvent += lastResponse + queryRes + '\n\n'; + turns.push(lastResponse + queryRes); + turns.push(''); } console.log('sending chat:', botResponse.trim()); - addEvent('bot', botEvent); + addEvent('bot', botEvent.trim()); return botResponse.trim(); }