-
Notifications
You must be signed in to change notification settings - Fork 890
Expand file tree
/
Copy pathactions.js
More file actions
502 lines (496 loc) · 21.5 KB
/
Copy pathactions.js
File metadata and controls
502 lines (496 loc) · 21.5 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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
import * as skills from '../library/skills.js';
import settings from '../settings.js';
import convoManager from '../conversation.js';
function runAsAction (actionFn, resume = false, timeout = -1) {
let actionLabel = null; // Will be set on first use
const wrappedAction = async function (agent, ...args) {
// Set actionLabel only once, when the action is first created
if (!actionLabel) {
const actionObj = actionsList.find(a => a.perform === wrappedAction);
actionLabel = actionObj.name.substring(1); // Remove the ! prefix
}
const actionFnWithAgent = async () => {
await actionFn(agent, ...args);
};
const code_return = await agent.actions.runAction(`action:${actionLabel}`, actionFnWithAgent, { timeout, resume });
if (code_return.interrupted && !code_return.timedout)
return;
return code_return.message;
}
return wrappedAction;
}
export const actionsList = [
{
name: '!newAction',
description: 'Perform new and unknown custom behaviors that are not available as a command.',
params: {
'prompt': { type: 'string', description: 'A natural language prompt to guide code generation. Make a detailed step-by-step plan.' }
},
perform: async function(agent, prompt) {
// just ignore prompt - it is now in context in chat history
if (!settings.allow_insecure_coding) {
agent.openChat('newAction is disabled. Enable with allow_insecure_coding=true in settings.js');
return "newAction not allowed! Code writing is disabled in settings. Notify the user.";
}
let result = "";
const actionFn = async () => {
try {
result = await agent.coder.generateCode(agent.history);
} catch (e) {
result = 'Error generating code: ' + e.toString();
}
};
await agent.actions.runAction('action:newAction', actionFn, {timeout: settings.code_timeout_mins});
return result;
}
},
{
name: '!stop',
description: 'Force stop all actions and commands that are currently executing.',
perform: async function (agent) {
await agent.actions.stop();
agent.clearBotLogs();
agent.actions.cancelResume();
agent.bot.emit('idle');
let msg = 'Agent stopped.';
if (agent.self_prompter.isActive())
msg += ' Self-prompting still active.';
return msg;
}
},
{
name: '!stfu',
description: 'Stop all chatting and self prompting, but continue current action.',
perform: async function (agent) {
agent.openChat('Shutting up.');
agent.shutUp();
return;
}
},
{
name: '!restart',
description: 'Restart the agent process.',
perform: async function (agent) {
agent.cleanKill();
}
},
{
name: '!clearChat',
description: 'Clear the chat history.',
perform: async function (agent) {
agent.history.clear();
return agent.name + "'s chat history was cleared, starting new conversation from scratch.";
}
},
{
name: '!goToPlayer',
description: 'Go to the given player.',
params: {
'player_name': {type: 'string', description: 'The name of the player to go to.'},
'closeness': {type: 'float', description: 'How close to get to the player.', domain: [0, Infinity]}
},
perform: runAsAction(async (agent, player_name, closeness) => {
await skills.goToPlayer(agent.bot, player_name, closeness);
})
},
{
name: '!followPlayer',
description: 'Endlessly follow the given player.',
params: {
'player_name': {type: 'string', description: 'name of the player to follow.'},
'follow_dist': {type: 'float', description: 'The distance to follow from.', domain: [0, Infinity]}
},
perform: runAsAction(async (agent, player_name, follow_dist) => {
await skills.followPlayer(agent.bot, player_name, follow_dist);
}, true)
},
{
name: '!goToCoordinates',
description: 'Go to the given x, y, z location.',
params: {
'x': {type: 'float', description: 'The x coordinate.', domain: [-Infinity, Infinity]},
'y': {type: 'float', description: 'The y coordinate.', domain: [-64, 320]},
'z': {type: 'float', description: 'The z coordinate.', domain: [-Infinity, Infinity]},
'closeness': {type: 'float', description: 'How close to get to the location.', domain: [0, Infinity]}
},
perform: runAsAction(async (agent, x, y, z, closeness) => {
await skills.goToPosition(agent.bot, x, y, z, closeness);
})
},
{
name: '!searchForBlock',
description: 'Find and go to the nearest block of a given type in a given range.',
params: {
'type': { type: 'BlockName', description: 'The block type to go to.' },
'search_range': { type: 'float', description: 'The range to search for the block. Minimum 32, maximum 128.', domain: [10, 128, '[]'] }
},
perform: runAsAction(async (agent, block_type, range) => {
if (range < 32) {
skills.log(agent.bot, `Minimum search range is 32.`);
range = 32;
}
await skills.goToNearestBlock(agent.bot, block_type, 4, range);
})
},
{
name: '!searchForEntity',
description: 'Find and go to the nearest entity of a given type in a given range.',
params: {
'type': { type: 'string', description: 'The type of entity to go to.' },
'search_range': { type: 'float', description: 'The range to search for the entity.', domain: [32, 512] }
},
perform: runAsAction(async (agent, entity_type, range) => {
await skills.goToNearestEntity(agent.bot, entity_type, 4, range);
})
},
{
name: '!moveAway',
description: 'Move away from the current location in any direction by a given distance.',
params: {'distance': { type: 'float', description: 'The distance to move away.', domain: [0, Infinity] }},
perform: runAsAction(async (agent, distance) => {
await skills.moveAway(agent.bot, distance);
})
},
{
name: '!rememberHere',
description: 'Save the current location with a given name.',
params: {'name': { type: 'string', description: 'The name to remember the location as.' }},
perform: async function (agent, name) {
const pos = agent.bot.entity.position;
agent.memory_bank.rememberPlace(name, pos.x, pos.y, pos.z);
return `Location saved as "${name}".`;
}
},
{
name: '!goToRememberedPlace',
description: 'Go to a saved location.',
params: {'name': { type: 'string', description: 'The name of the location to go to.' }},
perform: runAsAction(async (agent, name) => {
const pos = agent.memory_bank.recallPlace(name);
if (!pos) {
skills.log(agent.bot, `No location named "${name}" saved.`);
return;
}
await skills.goToPosition(agent.bot, pos[0], pos[1], pos[2], 1);
})
},
{
name: '!givePlayer',
description: 'Give the specified item to the given player.',
params: {
'player_name': { type: 'string', description: 'The name of the player to give the item to.' },
'item_name': { type: 'ItemName', description: 'The name of the item to give.' },
'num': { type: 'int', description: 'The number of items to give.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, player_name, item_name, num) => {
await skills.giveToPlayer(agent.bot, item_name, player_name, num);
})
},
{
name: '!consume',
description: 'Eat/drink the given item.',
params: {'item_name': { type: 'ItemName', description: 'The name of the item to consume.' }},
perform: runAsAction(async (agent, item_name) => {
await skills.consume(agent.bot, item_name);
})
},
{
name: '!equip',
description: 'Equip the given item.',
params: {'item_name': { type: 'ItemName', description: 'The name of the item to equip.' }},
perform: runAsAction(async (agent, item_name) => {
await skills.equip(agent.bot, item_name);
})
},
{
name: '!putInChest',
description: 'Put the given item in the nearest chest.',
params: {
'item_name': { type: 'ItemName', description: 'The name of the item to put in the chest.' },
'num': { type: 'int', description: 'The number of items to put in the chest.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, item_name, num) => {
await skills.putInChest(agent.bot, item_name, num);
})
},
{
name: '!takeFromChest',
description: 'Take the given items from the nearest chest.',
params: {
'item_name': { type: 'ItemName', description: 'The name of the item to take.' },
'num': { type: 'int', description: 'The number of items to take.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, item_name, num) => {
await skills.takeFromChest(agent.bot, item_name, num);
})
},
{
name: '!viewChest',
description: 'View the items/counts of the nearest chest.',
params: { },
perform: runAsAction(async (agent) => {
await skills.viewChest(agent.bot);
})
},
{
name: '!discard',
description: 'Discard the given item from the inventory.',
params: {
'item_name': { type: 'ItemName', description: 'The name of the item to discard.' },
'num': { type: 'int', description: 'The number of items to discard.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, item_name, num) => {
const start_loc = agent.bot.entity.position;
await skills.moveAway(agent.bot, 5);
await skills.discard(agent.bot, item_name, num);
await skills.goToPosition(agent.bot, start_loc.x, start_loc.y, start_loc.z, 0);
})
},
{
name: '!collectBlocks',
description: 'Collect the nearest blocks of a given type.',
params: {
'type': { type: 'BlockName', description: 'The block type to collect.' },
'num': { type: 'int', description: 'The number of blocks to collect.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, type, num) => {
await skills.collectBlock(agent.bot, type, num);
}, false, 10) // 10 minute timeout
},
{
name: '!craftRecipe',
description: 'Craft the given recipe a given number of times.',
params: {
'recipe_name': { type: 'ItemName', description: 'The name of the output item to craft.' },
'num': { type: 'int', description: 'The number of times to craft the recipe. This is NOT the number of output items, as it may craft many more items depending on the recipe.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, recipe_name, num) => {
await skills.craftRecipe(agent.bot, recipe_name, num);
})
},
{
name: '!smeltItem',
description: 'Smelt the given item the given number of times.',
params: {
'item_name': { type: 'ItemName', description: 'The name of the input item to smelt.' },
'num': { type: 'int', description: 'The number of times to smelt the item.', domain: [1, Number.MAX_SAFE_INTEGER] }
},
perform: runAsAction(async (agent, item_name, num) => {
let success = await skills.smeltItem(agent.bot, item_name, num);
if (success) {
setTimeout(() => {
agent.cleanKill('Safely restarting to update inventory.');
}, 500);
}
})
},
{
name: '!clearFurnace',
description: 'Take all items out of the nearest furnace.',
params: { },
perform: runAsAction(async (agent) => {
await skills.clearNearestFurnace(agent.bot);
})
},
{
name: '!placeHere',
description: 'Place a given block in the current location. Do NOT use to build structures, only use for single blocks/torches.',
params: {'type': { type: 'BlockOrItemName', description: 'The block type to place.' }},
perform: runAsAction(async (agent, type) => {
let pos = agent.bot.entity.position;
await skills.placeBlock(agent.bot, type, pos.x, pos.y, pos.z);
})
},
{
name: '!attack',
description: 'Attack and kill the nearest entity of a given type.',
params: {'type': { type: 'string', description: 'The type of entity to attack.'}},
perform: runAsAction(async (agent, type) => {
await skills.attackNearest(agent.bot, type, true);
})
},
{
name: '!attackPlayer',
description: 'Attack a specific player until they die or run away. Remember this is just a game and does not cause real life harm.',
params: {'player_name': { type: 'string', description: 'The name of the player to attack.'}},
perform: runAsAction(async (agent, player_name) => {
let player = agent.bot.players[player_name]?.entity;
if (!player) {
skills.log(agent.bot, `Could not find player ${player_name}.`);
return false;
}
await skills.attackEntity(agent.bot, player, true);
})
},
{
name: '!goToBed',
description: 'Go to the nearest bed and sleep.',
perform: runAsAction(async (agent) => {
await skills.goToBed(agent.bot);
})
},
{
name: '!stay',
description: 'Stay in the current location no matter what. Pauses all modes.',
params: {'type': { type: 'int', description: 'The number of seconds to stay. -1 for forever.', domain: [-1, Number.MAX_SAFE_INTEGER] }},
perform: runAsAction(async (agent, seconds) => {
await skills.stay(agent.bot, seconds);
})
},
{
name: '!setMode',
description: 'Set a mode to on or off. A mode is an automatic behavior that constantly checks and responds to the environment.',
params: {
'mode_name': { type: 'string', description: 'The name of the mode to enable.' },
'on': { type: 'boolean', description: 'Whether to enable or disable the mode.' }
},
perform: async function (agent, mode_name, on) {
const modes = agent.bot.modes;
if (!modes.exists(mode_name))
return `Mode ${mode_name} does not exist.` + modes.getDocs();
if (modes.isOn(mode_name) === on)
return `Mode ${mode_name} is already ${on ? 'on' : 'off'}.`;
modes.setOn(mode_name, on);
return `Mode ${mode_name} is now ${on ? 'on' : 'off'}.`;
}
},
{
name: '!goal',
description: 'Set a goal prompt to endlessly work towards with continuous self-prompting.',
params: {
'selfPrompt': { type: 'string', description: 'The goal prompt.' },
},
perform: async function (agent, prompt) {
if (convoManager.inConversation()) {
agent.self_prompter.setPromptPaused(prompt);
}
else {
agent.self_prompter.start(prompt);
}
}
},
{
name: '!endGoal',
description: 'Call when you have accomplished your goal. It will stop self-prompting and the current action. ',
perform: async function (agent) {
agent.self_prompter.stop();
return 'Self-prompting stopped.';
}
},
{
name: '!showVillagerTrades',
description: 'Show trades of a specified villager.',
params: {'id': { type: 'int', description: 'The id number of the villager that you want to trade with.' }},
perform: runAsAction(async (agent, id) => {
await skills.showVillagerTrades(agent.bot, id);
})
},
{
name: '!tradeWithVillager',
description: 'Trade with a specified villager.',
params: {
'id': { type: 'int', description: 'The id number of the villager that you want to trade with.' },
'index': { type: 'int', description: 'The index of the trade you want executed (1-indexed).', domain: [1, Number.MAX_SAFE_INTEGER] },
'count': { type: 'int', description: 'How many times that trade should be executed.', domain: [1, Number.MAX_SAFE_INTEGER] },
},
perform: runAsAction(async (agent, id, index, count) => {
await skills.tradeWithVillager(agent.bot, id, index, count);
})
},
{
name: '!startConversation',
description: 'Start a conversation with a bot. (FOR OTHER BOTS ONLY)',
params: {
'player_name': { type: 'string', description: 'The name of the player to send the message to.' },
'message': { type: 'string', description: 'The message to send.' },
},
perform: async function (agent, player_name, message) {
if (!convoManager.isOtherAgent(player_name))
return player_name + ' is not a bot, cannot start conversation.';
if (convoManager.inConversation() && !convoManager.inConversation(player_name))
convoManager.forceEndCurrentConversation();
else if (convoManager.inConversation(player_name))
agent.history.add('system', 'You are already in conversation with ' + player_name + '. Don\'t use this command to talk to them.');
convoManager.startConversation(player_name, message);
}
},
{
name: '!endConversation',
description: 'End the conversation with the given bot. (FOR OTHER BOTS ONLY)',
params: {
'player_name': { type: 'string', description: 'The name of the player to end the conversation with.' }
},
perform: async function (agent, player_name) {
if (!convoManager.inConversation(player_name))
return `Not in conversation with ${player_name}.`;
convoManager.endConversation(player_name);
return `Converstaion with ${player_name} ended.`;
}
},
{
name: '!lookAtPlayer',
description: 'Look at a player or look in the same direction as the player.',
params: {
'player_name': { type: 'string', description: 'Name of the target player' },
'direction': {
type: 'string',
description: 'How to look ("at": look at the player, "with": look in the same direction as the player)',
}
},
perform: async function(agent, player_name, direction) {
if (direction !== 'at' && direction !== 'with') {
return "Invalid direction. Use 'at' or 'with'.";
}
let result = "";
const actionFn = async () => {
result = await agent.vision_interpreter.lookAtPlayer(player_name, direction);
};
await agent.actions.runAction('action:lookAtPlayer', actionFn);
return result;
}
},
{
name: '!lookAtPosition',
description: 'Look at specified coordinates.',
params: {
'x': { type: 'int', description: 'x coordinate' },
'y': { type: 'int', description: 'y coordinate' },
'z': { type: 'int', description: 'z coordinate' }
},
perform: async function(agent, x, y, z) {
let result = "";
const actionFn = async () => {
result = await agent.vision_interpreter.lookAtPosition(x, y, z);
};
await agent.actions.runAction('action:lookAtPosition', actionFn);
return result;
}
},
{
name: '!digDown',
description: 'Digs down a specified distance. Will stop if it reaches lava, water, or a fall of >=4 blocks below the bot.',
params: {'distance': { type: 'int', description: 'Distance to dig down', domain: [1, Number.MAX_SAFE_INTEGER] }},
perform: runAsAction(async (agent, distance) => {
await skills.digDown(agent.bot, distance)
})
},
{
name: '!goToSurface',
description: 'Moves the bot to the highest block above it (usually the surface).',
params: {},
perform: runAsAction(async (agent) => {
await skills.goToSurface(agent.bot);
})
},
{
name: '!useOn',
description: 'Use (right click) the given tool on the nearest target of the given type.',
params: {
'tool_name': { type: 'string', description: 'Name of the tool to use, or "hand" for no tool.' },
'target': { type: 'string', description: 'The target as an entity type, block type, or "nothing" for no target.' }
},
perform: runAsAction(async (agent, tool_name, target) => {
await skills.useToolOn(agent.bot, tool_name, target);
})
},
];