forked from microsoft/MinecraftCodex
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodexGame.ts
More file actions
333 lines (280 loc) · 10.1 KB
/
Copy pathCodexGame.ts
File metadata and controls
333 lines (280 loc) · 10.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
import {
BlockLocation,
MinecraftBlockTypes,
NavigationResult,
BeforeChatEvent,
world,
Player,
Dimension,
Vector,
Location,
Block,
InventoryComponentContainer,
EntityInventoryComponent,
ItemStack,
} from "@minecraft/server";
import { CodexBot } from "./CodexBot.js";
import { BlockConverter, CodexBlockType } from "./BlockConverter.js";
import Prompt from "./Prompt.js";
import Model from "./Model.js";
import { context } from "./prompts/combinedPrompt.js";
import { SimulatedPlayer, Test } from "@minecraft/server-gametest";
import { GenGrow, BlockGen } from "./worldGrow/BoxGrow.js";
import { TimedQueue } from "./TimedQueue.js";
import { detectSensitiveContent } from "./ContentFilter.js";
let target = null;
let result = null;
class GameState {
foundBlocks: Block[] | undefined = undefined;
navResult: NavigationResult | undefined = undefined;
followInterval: number = 0;
constructor() {}
}
export default class CodexGame {
gameTest: Test; // testing framework GameTest
player: Player; // the player avatar
bot: CodexBot; // our SimulatedPlayer bot
overWorld: Dimension; // the game overworld, where most functions are run
taskStack: TimedQueue = new TimedQueue(); // Used to support setInterval and clearInterval functions
state: GameState = new GameState(); // A state bag for Codex code to set and get values
model = new Model();
prompt = new Prompt(context);
genGrow = new GenGrow();
constructor(test: Test) {
this.setPlayer();
this.player = Array.from(world.getPlayers())[0];
this.gameTest = test;
this.overWorld = world.getDimension("overworld");
this.overWorld.runCommand("say Game Initialized, Codex running");
// Spawn a new CodexBot instance, which wraps SimulatedPlayer, next to the main player
this.bot = new CodexBot(this);
world.events.beforeChat.subscribe((chat: BeforeChatEvent) => {
this.processMessage(chat);
});
}
// Update cycle for the behavior pack, process the taskStack which holds Promise chains
processTick() {
this.taskStack.processTick();
}
getInventory(object: Block | SimulatedPlayer | Player): InventoryComponentContainer | undefined {
let container: InventoryComponentContainer | undefined = undefined;
if (object instanceof SimulatedPlayer) {
container = (object.getComponent("inventory") as EntityInventoryComponent).container;
}
if (object instanceof Block) {
container = object.getComponent("inventory").container;
}
if (object instanceof Player) {
container = (object.getComponent("inventory") as EntityInventoryComponent).container;
}
return container;
}
listInventory(object: Block | SimulatedPlayer | Player, name: string): InventoryComponentContainer | undefined {
let container = this.getInventory(object);
if (!container) return undefined;
let haveItems = false;
for (let i = 0; i < container.size; i++) {
let slotItem = container.getItem(i);
if (slotItem != undefined) {
let itemName = slotItem.typeId.substring(slotItem.typeId.indexOf(":") + 1, slotItem.typeId.length);
let numItems = slotItem.amount;
haveItems = true;
if (itemName != "") {
// special case for bot inventory to be in first person
if (name == "bot") {
let response = "";
response = "I have " + numItems + " " + itemName;
if (numItems > 1 && !response.endsWith("s")) {
response = response + "s";
}
this.bot.chat(response);
this.prompt.addText(response);
}
// other inventories, like chests are second person
else if (name != "") {
let response = "The " + name + " has " + numItems + " " + itemName;
if (numItems > 1) {
response = response + "s";
}
this.bot.chat(response);
this.prompt.addText(response);
}
}
}
}
if (name != "" && !haveItems) {
let response = "";
if (name === "bot") response = "I have no items";
else response = "The " + name + " is empty";
this.bot.chat(response);
this.prompt.addText(response);
}
return container;
}
transferItem(
fromInventory: InventoryComponentContainer,
toInventory: InventoryComponentContainer,
name: string,
numItems: number = -1
): boolean {
let itemName = BlockConverter.ConvertBlockType(name).name;
let slotItem: ItemStack;
let fromSlot = -1;
let toSlot = -1;
itemName = "minecraft:" + itemName;
for (let i = 0; i < fromInventory.size; i++) {
slotItem = fromInventory.getItem(i);
if (slotItem != undefined) {
if (slotItem.typeId == itemName) {
fromSlot = i;
i = fromInventory.size;
}
}
}
for (let j = 0; j < toInventory.size; j++) {
slotItem = toInventory.getItem(j);
if (slotItem === undefined) {
toSlot = j;
j = toInventory.size;
}
}
if (toSlot != -1 && fromSlot != -1) {
let result = fromInventory.transferItem(fromSlot, toSlot, toInventory);
return result;
}
this.bot.chat("The item isn't there");
return false;
}
convertBlockLocToLoc(blockLoc: BlockLocation): Location {
return new Location(blockLoc.x, blockLoc.y, blockLoc.z);
}
async pushGrowBlock(type: string, offsetX: number, offsetY: number, offsetZ: number) {
this.genGrow.addBlock(MinecraftBlockTypes.get("minecraft:" + type), new Vector(offsetX, offsetY, offsetZ));
}
async growItem(location: Vector, offsetX: number, offsetY: number, offsetZ: number) {
this.genGrow.applyToOverworld(new BlockLocation(location.x + offsetX, location.y + offsetY, location.z + offsetZ));
}
//check for the active player being set, and if hasn't been, then set it
setPlayer() {
if (!this.player) {
let players = Array.from(world.getPlayers());
this.player = players[0];
}
}
recordEvent(channel: string, username: string, message: string) {
console.log(channel + ": " + username + " says " + message + "\n");
}
recordException(err: any) {
if (err.message) {
console.warn("ERROR: " + err.message);
} else console.warn("ERROR: " + err);
}
recordInteraction(eventName: string, userName: string, interaction: string, completion: string) {
console.log("ACTION: " + eventName + "|" + userName + "|" + interaction + "|" + completion);
}
// Event subscriptions
async processMessage(chat: BeforeChatEvent) {
let username = chat.sender.name;
const message = chat.message;
const lcMessage = chat.message.toLowerCase();
if (username === this.bot.getName()) return;
if (lcMessage.startsWith("/")) return;
if (lcMessage.startsWith("script ")) {
this.evaluateCode(message.substring(7, message.length));
return;
}
this.recordEvent("chat", username, message);
if (lcMessage.startsWith("reset")) {
this.recordEvent("reset", username, message);
this.prompt.resetInteractions();
this.bot.chat("I'm refreshed now!");
return;
}
if (lcMessage.startsWith("unlearn")) {
this.recordEvent("unlearn", username, message);
this.prompt.removeLastInteraction();
this.bot.chat("I unlearned our last interaction!");
return;
}
let completion;
let player = this.bot.getPlayerByName(username);
if (!player) {
return;
}
target = player;
const prompt = this.prompt.craftPrompt(message);
try {
completion = await this.model.getCompletion(prompt);
} catch (err: any) {
this.recordException(err);
console.log("Error calling Codex", err);
if (err.message.includes("400")) {
this.bot.chat("I had to reset my brain and might have forgotten some context. Sorry about that!");
this.recordEvent("reset", username, message);
this.prompt.resetInteractions();
}
return;
}
if (completion) {
let sensitiveContentFlag = await detectSensitiveContent(message + "\n" + completion);
// The flag can be 0, 1 or 2, corresponding to 'safe', 'sensitive' and 'unsafe'
if (sensitiveContentFlag > 0) {
console.warn(
sensitiveContentFlag === 1
? "Your message or the model's response may have contained sensitive content."
: "Your message or the model's response may have contained unsafe content."
);
this.bot.chat("Let's talk about something else");
return;
}
// Appends the NL -> Code interaction onto our prompt for future turns
this.prompt.addInteraction(message, completion);
// Echoes code to chat window if it isn't already a chat command
if (!completion.startsWith("bot.chat")) {
this.bot.chat(completion);
}
await this.evaluateCode(completion);
}
}
async evaluateCode(code: string, recursive = false) {
try {
await this.evaluate(code, {
setTimeout: this.taskStack.setTimeout.bind(this.taskStack),
sleep: this.taskStack.sleep.bind(this.taskStack),
clearTimeout: this.taskStack.clearTimeout.bind(this.taskStack),
setInterval: this.taskStack.setInterval.bind(this.taskStack),
clearInterval: this.taskStack.clearInterval.bind(this.taskStack),
game: this,
state: this.state,
bot: this.bot.simBot,
});
} catch (err) {
this.handleError(err as Error);
return false;
}
}
/**
* Allows us to pass args that the code eval will have access to
* @param code code string to be evaluated
* @param args context to pass to the eval scope
* @returns
*/
evaluate(code: string, args = {}) {
return function evaluateEval() {
const argsStr = Object.keys(args)
.map((key) => `${key} = this.${key}`)
.join(",");
const argsDef = argsStr ? `let ${argsStr};` : "";
return eval(`(async () => {${argsDef}${code}})();`);
}.call(args);
}
handleError(err: Error) {
this.recordException(err);
console.log("Error evaluating code:", err);
this.bot.chat(err.message);
if (err.name !== "Error") {
this.prompt.removeLastInteraction();
this.bot.chat("I unlearned our last interaction!");
}
}
}