From 4fd6aa2021ea5fa7b51f4ca263cb09102424536a Mon Sep 17 00:00:00 2001 From: Kolby Nottingham Date: Tue, 23 Apr 2024 20:47:01 -0700 Subject: [PATCH] prompted goals init --- andy_npc.json | 2 ++ src/agent/commands/actions.js | 14 ++++++++++++++ src/agent/npc/controller.js | 36 +++++++++++++++++++++++++++++++---- src/agent/npc/data.js | 9 +++++++++ src/agent/npc/item_goal.js | 7 ++++--- src/agent/prompter.js | 5 +++++ 6 files changed, 66 insertions(+), 7 deletions(-) diff --git a/andy_npc.json b/andy_npc.json index a392812..a8be5b0 100644 --- a/andy_npc.json +++ b/andy_npc.json @@ -9,6 +9,8 @@ "saving_memory": "You are a minecraft bot named $NAME that has been talking and playing minecraft by using commands. Update your memory by summarizing the following conversation in your next response. Store information that will help you improve as a Minecraft bot. Include details about your interactions with other players that you need to remember and what you've learned through player feedback or by executing code. Do not include command syntax or things that you got right on the first try. Be extremely brief and use as few words as possible.\nOld Memory: '$MEMORY'\nRecent conversation: \n$TO_SUMMARIZE\nSummarize your old memory and recent conversation into a new memory, and respond only with the memory text: ", + "goal_setting": "You are a Minecraft bot named $NAME that has the ability to set in-game goals that are then executed programatically. Goals must be either and item or block name or a blueprint of a specific building. Any minecraft item or block name is valid. However, only names from the following list are valid blueprints: $BLUEPRINTS. Given any recent conversation and the most recently attempted goals, set a new goal to achieve. Fromat your response as a json object with the fields \"name\" and \"quantity\". Note that the quantity for a blueprint should always be one. Example:\n```json\n{\"name\": \"iron_pickaxe\", \"quantity\": 1}\n```", + "npc": { "goals": [ "wooden_pickaxe", diff --git a/src/agent/commands/actions.js b/src/agent/commands/actions.js index a57a464..4cd0a1e 100644 --- a/src/agent/commands/actions.js +++ b/src/agent/commands/actions.js @@ -36,6 +36,7 @@ export const actionsList = [ await agent.coder.stop(); agent.coder.clear(); agent.coder.cancelResume(); + agent.bot.emit('idle'); return 'Agent stopped.'; } }, @@ -196,5 +197,18 @@ export const actionsList = [ perform: wrapExecution(async (agent) => { await skills.stay(agent.bot); }) + }, + { + name: '!goal', + description: 'Set a goal to automatically work towards.', + params: { + 'name': '(string) The name of the goal to set. Can be item or building name. If empty will automatically choose a goal.', + 'quantity': '(number) The quantity of the goal to set. Default is 1.' + }, + perform: async function (agent, name=null, quantity=1) { + if (!agent.npc.data) return 'NPC module is not loaded.'; + await agent.npc.setGoal(name, quantity); + return 'Set goal: ' + agent.npc.data.curr_goal.name; + } } ]; diff --git a/src/agent/npc/controller.js b/src/agent/npc/controller.js index c474efd..ecb831a 100644 --- a/src/agent/npc/controller.js +++ b/src/agent/npc/controller.js @@ -16,6 +16,7 @@ export class NPCContoller { this.item_goal = new ItemGoal(agent, this.data); this.build_goal = new BuildGoal(agent); this.constructions = {}; + this.last_goals = {}; } getBuiltPositions() { @@ -79,13 +80,29 @@ export class NPCContoller { }); } + setGoal(name=null, quantity=1) { + this.last_goals = {}; + if (name) { + this.data.curr_goal = {name: name, quantity: quantity}; + return; + } + + let res = this.agent.prompter.promptGoalSetting(this.agent.history.getHistory(), this.last_goals); + if (res) { + this.data.curr_goal = res; + console.log('Set new goal: ', res.name, ' x', res.quantity); + } else { + console.log('Error setting new goal.'); + } + } + async executeNext() { if (!this.agent.isIdle()) return; await this.agent.coder.execute(async () => { await skills.moveAway(this.agent.bot, 2); }); - if (this.agent.bot.time.timeOfDay < 13000) { + if (!this.data.do_routine || this.agent.bot.time.timeOfDay < 13000) { // Exit any buildings let building = this.currentBuilding(); if (building == this.data.home) { @@ -123,15 +140,18 @@ export class NPCContoller { async executeGoal() { // If we need more blocks to complete a building, get those first - let goals = this.temp_goals.concat(this.data.goals); + let goals = this.temp_goals.concat(this.data.goals).concat([this.data.curr_goal]); this.temp_goals = []; + let acted = false; for (let goal of goals) { // Obtain goal item or block if (this.constructions[goal.name] === undefined) { if (!itemSatisfied(this.agent.bot, goal.name, goal.quantity)) { - await this.item_goal.executeNext(goal.name, goal.quantity); + let res = await this.item_goal.executeNext(goal.name, goal.quantity); + this.last_goals[goal.name] = res; + acted = true; break; } } @@ -162,9 +182,17 @@ export class NPCContoller { quantity: res.missing[block_name] }) } - if (res.acted) break; + if (res.acted) { + acted = true; + this.last_goals[goal.name] = Object.keys(res.missing).length === 0; + break; + } } } + + if (!acted) { + this.setGoal(); + } } currentBuilding() { diff --git a/src/agent/npc/data.js b/src/agent/npc/data.js index 70b59a4..f6b382f 100644 --- a/src/agent/npc/data.js +++ b/src/agent/npc/data.js @@ -1,18 +1,23 @@ export class NPCData { constructor() { this.goals = []; + this.curr_goal = null; this.built = {}; this.home = null; + this.do_routine = true; } toObject() { let obj = {}; if (this.goals.length > 0) obj.goals = this.goals; + if (this.curr_goal) + obj.curr_goal = this.curr_goal; if (Object.keys(this.built).length > 0) obj.built = this.built; if (this.home) obj.home = this.home; + obj.do_routine = this.do_routine; return obj; } @@ -28,10 +33,14 @@ export class NPCData { npc.goals.push({name: goal.name, quantity: goal.quantity}); } } + if (obj.curr_goal) + npc.curr_goal = obj.curr_goal; if (obj.built) npc.built = obj.built; if (obj.home) npc.home = obj.home; + if (obj.do_routine !== undefined) + npc.do_routine = obj.do_routine; return npc; } } \ No newline at end of file diff --git a/src/agent/npc/item_goal.js b/src/agent/npc/item_goal.js index db19dc9..0550657 100644 --- a/src/agent/npc/item_goal.js +++ b/src/agent/npc/item_goal.js @@ -309,7 +309,7 @@ export class ItemGoal { let next_info = this.goal.getNext(item_quantity); if (!next_info) { console.log(`Invalid item goal ${this.goal.name}`); - return; + return false; } let next = next_info.node; let quantity = next_info.quantity; @@ -330,12 +330,12 @@ export class ItemGoal { await new Promise((resolve) => setTimeout(resolve, 500)); this.agent.bot.emit('idle'); } - return; + return false; } // Wait for the bot to be idle before attempting to execute the next goal if (!this.agent.isIdle()) - return; + return false; // Execute the next goal let init_quantity = world.getInventoryCounts(this.agent.bot)[next.name] || 0; @@ -350,5 +350,6 @@ export class ItemGoal { } else { console.log(`Failed to obtain ${next.name} for goal ${this.goal.name}`); } + return final_quantity > init_quantity; } } diff --git a/src/agent/prompter.js b/src/agent/prompter.js index 03679ad..31e8270 100644 --- a/src/agent/prompter.js +++ b/src/agent/prompter.js @@ -95,4 +95,9 @@ export class Prompter { prompt = await this.replaceStrings(prompt, null, null, prev_mem, to_summarize); return await this.model.sendRequest([], prompt); } + + async promptGoalSetting(messages, last_goals) { + // TODO + return {name: '', quantity: 0}; + } } \ No newline at end of file