mindcraft 0.1.4-0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/FAQ.md +38 -0
- package/LICENSE +21 -0
- package/README.md +255 -0
- package/andy.json +6 -0
- package/bin/mindcraft.js +80 -0
- package/keys.example.json +19 -0
- package/main.js +80 -0
- package/package.json +78 -0
- package/patches/minecraft-data+3.97.0.patch +13 -0
- package/patches/mineflayer+4.33.0.patch +54 -0
- package/patches/mineflayer-pathfinder+2.4.5.patch +265 -0
- package/patches/mineflayer-pvp+1.3.2.patch +13 -0
- package/patches/prismarine-viewer+1.33.0.patch +13 -0
- package/patches/protodef+1.19.0.patch +15 -0
- package/profiles/andy-4-reasoning.json +14 -0
- package/profiles/andy-4.json +7 -0
- package/profiles/azure.json +19 -0
- package/profiles/claude.json +7 -0
- package/profiles/claude_thinker.json +15 -0
- package/profiles/deepseek.json +7 -0
- package/profiles/defaults/_default.json +256 -0
- package/profiles/defaults/assistant.json +14 -0
- package/profiles/defaults/creative.json +14 -0
- package/profiles/defaults/god_mode.json +14 -0
- package/profiles/defaults/survival.json +14 -0
- package/profiles/freeguy.json +7 -0
- package/profiles/gemini.json +9 -0
- package/profiles/gpt.json +12 -0
- package/profiles/grok.json +7 -0
- package/profiles/llama.json +10 -0
- package/profiles/mercury.json +9 -0
- package/profiles/mistral.json +5 -0
- package/profiles/qwen.json +17 -0
- package/profiles/tasks/construction_profile.json +42 -0
- package/profiles/tasks/cooking_profile.json +11 -0
- package/profiles/tasks/crafting_profile.json +71 -0
- package/profiles/vllm.json +10 -0
- package/settings.js +64 -0
- package/src/agent/action_manager.js +177 -0
- package/src/agent/agent.js +561 -0
- package/src/agent/coder.js +229 -0
- package/src/agent/commands/actions.js +504 -0
- package/src/agent/commands/index.js +259 -0
- package/src/agent/commands/queries.js +347 -0
- package/src/agent/connection_handler.js +96 -0
- package/src/agent/conversation.js +353 -0
- package/src/agent/history.js +122 -0
- package/src/agent/library/full_state.js +89 -0
- package/src/agent/library/index.js +23 -0
- package/src/agent/library/lockdown.js +32 -0
- package/src/agent/library/skill_library.js +93 -0
- package/src/agent/library/skills.js +2093 -0
- package/src/agent/library/world.js +431 -0
- package/src/agent/memory_bank.js +25 -0
- package/src/agent/mindserver_proxy.js +136 -0
- package/src/agent/modes.js +446 -0
- package/src/agent/npc/build_goal.js +80 -0
- package/src/agent/npc/construction/dirt_shelter.json +38 -0
- package/src/agent/npc/construction/large_house.json +230 -0
- package/src/agent/npc/construction/small_stone_house.json +42 -0
- package/src/agent/npc/construction/small_wood_house.json +42 -0
- package/src/agent/npc/controller.js +261 -0
- package/src/agent/npc/data.js +50 -0
- package/src/agent/npc/item_goal.js +355 -0
- package/src/agent/npc/utils.js +126 -0
- package/src/agent/self_prompter.js +146 -0
- package/src/agent/settings.js +7 -0
- package/src/agent/speak.js +150 -0
- package/src/agent/tasks/construction_tasks.js +1104 -0
- package/src/agent/tasks/cooking_tasks.js +358 -0
- package/src/agent/tasks/tasks.js +594 -0
- package/src/agent/templates/execTemplate.js +6 -0
- package/src/agent/templates/lintTemplate.js +10 -0
- package/src/agent/vision/browser_viewer.js +8 -0
- package/src/agent/vision/camera.js +78 -0
- package/src/agent/vision/vision_interpreter.js +82 -0
- package/src/mindcraft/index.js +28 -0
- package/src/mindcraft/mcserver.js +154 -0
- package/src/mindcraft/mindcraft.js +111 -0
- package/src/mindcraft/mindserver.js +328 -0
- package/src/mindcraft/public/index.html +1253 -0
- package/src/mindcraft/public/settings_spec.json +145 -0
- package/src/mindcraft/userconfig.js +72 -0
- package/src/mindcraft-py/example.py +27 -0
- package/src/mindcraft-py/init-mindcraft.js +24 -0
- package/src/mindcraft-py/mindcraft.py +99 -0
- package/src/models/_model_map.js +89 -0
- package/src/models/azure.js +32 -0
- package/src/models/cerebras.js +61 -0
- package/src/models/claude.js +87 -0
- package/src/models/deepseek.js +59 -0
- package/src/models/gemini.js +176 -0
- package/src/models/glhf.js +71 -0
- package/src/models/gpt.js +147 -0
- package/src/models/grok.js +82 -0
- package/src/models/groq.js +95 -0
- package/src/models/huggingface.js +86 -0
- package/src/models/hyperbolic.js +114 -0
- package/src/models/lmstudio.js +74 -0
- package/src/models/mercury.js +95 -0
- package/src/models/mistral.js +94 -0
- package/src/models/novita.js +71 -0
- package/src/models/ollama.js +115 -0
- package/src/models/openrouter.js +77 -0
- package/src/models/prompter.js +366 -0
- package/src/models/qwen.js +80 -0
- package/src/models/replicate.js +60 -0
- package/src/models/vllm.js +81 -0
- package/src/process/agent_process.js +84 -0
- package/src/process/init_agent.js +54 -0
- package/src/utils/examples.js +83 -0
- package/src/utils/keys.js +34 -0
- package/src/utils/math.js +13 -0
- package/src/utils/mcdata.js +572 -0
- package/src/utils/text.js +78 -0
- package/src/utils/translator.js +30 -0
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { writeFile, readFile, mkdirSync } from 'fs';
|
|
2
|
+
import path from 'path';
|
|
3
|
+
import { fileURLToPath } from 'url';
|
|
4
|
+
import { makeCompartment, lockdown } from './library/lockdown.js';
|
|
5
|
+
import * as skills from './library/skills.js';
|
|
6
|
+
import * as world from './library/world.js';
|
|
7
|
+
import { Vec3 } from 'vec3';
|
|
8
|
+
import {ESLint} from "eslint";
|
|
9
|
+
import settings from './settings.js';
|
|
10
|
+
|
|
11
|
+
const __dirname = path.dirname(fileURLToPath(import.meta.url));
|
|
12
|
+
|
|
13
|
+
export class Coder {
|
|
14
|
+
constructor(agent) {
|
|
15
|
+
this.agent = agent;
|
|
16
|
+
this.file_counter = 0;
|
|
17
|
+
this.fp = path.join(settings.data_dir, agent.name, 'action-code') + path.sep;
|
|
18
|
+
this.code_template = '';
|
|
19
|
+
this.code_lint_template = '';
|
|
20
|
+
|
|
21
|
+
readFile(path.join(__dirname, 'templates/execTemplate.js'), 'utf8', (err, data) => {
|
|
22
|
+
if (err) throw err;
|
|
23
|
+
this.code_template = data;
|
|
24
|
+
});
|
|
25
|
+
readFile(path.join(__dirname, 'templates/lintTemplate.js'), 'utf8', (err, data) => {
|
|
26
|
+
if (err) throw err;
|
|
27
|
+
this.code_lint_template = data;
|
|
28
|
+
});
|
|
29
|
+
mkdirSync(this.fp, { recursive: true });
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
async generateCode(agent_history) {
|
|
33
|
+
this.agent.bot.modes.pause('unstuck');
|
|
34
|
+
lockdown();
|
|
35
|
+
// this message history is transient and only maintained in this function
|
|
36
|
+
let messages = agent_history.getHistory();
|
|
37
|
+
messages.push({role: 'system', content: 'Code generation started. Write code in codeblock in your response:'});
|
|
38
|
+
|
|
39
|
+
const MAX_ATTEMPTS = 5;
|
|
40
|
+
const MAX_NO_CODE = 3;
|
|
41
|
+
|
|
42
|
+
let code = null;
|
|
43
|
+
let no_code_failures = 0;
|
|
44
|
+
for (let i=0; i<MAX_ATTEMPTS; i++) {
|
|
45
|
+
if (this.agent.bot.interrupt_code)
|
|
46
|
+
return null;
|
|
47
|
+
const messages_copy = JSON.parse(JSON.stringify(messages));
|
|
48
|
+
let res = await this.agent.prompter.promptCoding(messages_copy);
|
|
49
|
+
if (this.agent.bot.interrupt_code)
|
|
50
|
+
return null;
|
|
51
|
+
let contains_code = res.indexOf('```') !== -1;
|
|
52
|
+
if (!contains_code) {
|
|
53
|
+
if (res.indexOf('!newAction') !== -1) {
|
|
54
|
+
messages.push({
|
|
55
|
+
role: 'assistant',
|
|
56
|
+
content: res.substring(0, res.indexOf('!newAction'))
|
|
57
|
+
});
|
|
58
|
+
continue; // using newaction will continue the loop
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
if (no_code_failures >= MAX_NO_CODE) {
|
|
62
|
+
console.warn("Action failed, agent would not write code.");
|
|
63
|
+
return 'Action failed, agent would not write code.';
|
|
64
|
+
}
|
|
65
|
+
messages.push({
|
|
66
|
+
role: 'system',
|
|
67
|
+
content: 'Error: no code provided. Write code in codeblock in your response. ``` // example ```'}
|
|
68
|
+
);
|
|
69
|
+
console.warn("No code block generated. Trying again.");
|
|
70
|
+
no_code_failures++;
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
code = res.substring(res.indexOf('```')+3, res.lastIndexOf('```'));
|
|
74
|
+
const result = await this._stageCode(code);
|
|
75
|
+
const executionModule = result.func;
|
|
76
|
+
const lintResult = await this._lintCode(result.src_lint_copy);
|
|
77
|
+
if (lintResult) {
|
|
78
|
+
const message = 'Error: Code lint error:'+'\n'+lintResult+'\nPlease try again.';
|
|
79
|
+
console.warn("Linting error:"+'\n'+lintResult+'\n');
|
|
80
|
+
messages.push({ role: 'system', content: message });
|
|
81
|
+
continue;
|
|
82
|
+
}
|
|
83
|
+
if (!executionModule) {
|
|
84
|
+
console.warn("Failed to stage code, something is wrong.");
|
|
85
|
+
return 'Failed to stage code, something is wrong.';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
try {
|
|
89
|
+
console.log('Executing code...');
|
|
90
|
+
await executionModule.main(this.agent.bot);
|
|
91
|
+
|
|
92
|
+
const code_output = this.agent.actions.getBotOutputSummary();
|
|
93
|
+
const summary = "Agent wrote this code: \n```" + this._sanitizeCode(code) + "```\nCode Output:\n" + code_output;
|
|
94
|
+
return summary;
|
|
95
|
+
} catch (e) {
|
|
96
|
+
if (this.agent.bot.interrupt_code)
|
|
97
|
+
return null;
|
|
98
|
+
|
|
99
|
+
console.warn('Generated code threw error: ' + e.toString());
|
|
100
|
+
console.warn('trying again...');
|
|
101
|
+
|
|
102
|
+
const code_output = this.agent.actions.getBotOutputSummary();
|
|
103
|
+
|
|
104
|
+
messages.push({
|
|
105
|
+
role: 'assistant',
|
|
106
|
+
content: res
|
|
107
|
+
});
|
|
108
|
+
messages.push({
|
|
109
|
+
role: 'system',
|
|
110
|
+
content: `Code Output:\n${code_output}\nCODE EXECUTION THREW ERROR: ${e.toString()}\n Please try again:`
|
|
111
|
+
});
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
return `Code generation failed after ${MAX_ATTEMPTS} attempts.`;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async _lintCode(code) {
|
|
118
|
+
let result = '#### CODE ERROR INFO ###\n';
|
|
119
|
+
const codeNoComments = code.replace(/\/\/.*$/gm, '').replace(/\/\*[\s\S]*?\*\//g, '');
|
|
120
|
+
const skillRegex = /((?:skills|world)\.(.*?))\(/g;
|
|
121
|
+
const skills = [];
|
|
122
|
+
let match;
|
|
123
|
+
while ((match = skillRegex.exec(codeNoComments)) !== null) {
|
|
124
|
+
skills.push(match[1]);
|
|
125
|
+
}
|
|
126
|
+
const allDocs = await this.agent.prompter.skill_libary.getAllSkillDocs();
|
|
127
|
+
const knownSkills = new Set(allDocs.map(doc => doc.split('\n')[0]));
|
|
128
|
+
const missingSkills = skills.filter(skill => !knownSkills.has(skill));
|
|
129
|
+
if (missingSkills.length > 0) {
|
|
130
|
+
result += 'These functions do not exist:\n';
|
|
131
|
+
result += missingSkills.join('\n');
|
|
132
|
+
console.log(result)
|
|
133
|
+
return result;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const eslint = new ESLint();
|
|
137
|
+
const results = await eslint.lintText(code);
|
|
138
|
+
const codeLines = code.split('\n');
|
|
139
|
+
const exceptions = results.map(r => r.messages).flat();
|
|
140
|
+
|
|
141
|
+
if (exceptions.length > 0) {
|
|
142
|
+
exceptions.forEach((exc, index) => {
|
|
143
|
+
if (exc.line && exc.column ) {
|
|
144
|
+
const errorLine = codeLines[exc.line - 1]?.trim() || 'Unable to retrieve error line content';
|
|
145
|
+
result += `#ERROR ${index + 1}\n`;
|
|
146
|
+
result += `Message: ${exc.message}\n`;
|
|
147
|
+
result += `Location: Line ${exc.line}, Column ${exc.column}\n`;
|
|
148
|
+
result += `Related Code Line: ${errorLine}\n`;
|
|
149
|
+
}
|
|
150
|
+
});
|
|
151
|
+
result += 'The code contains exceptions and cannot continue execution.';
|
|
152
|
+
} else {
|
|
153
|
+
return null;//no error
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
return result ;
|
|
157
|
+
}
|
|
158
|
+
// write custom code to file and import it
|
|
159
|
+
// write custom code to file and prepare for evaluation
|
|
160
|
+
async _stageCode(code) {
|
|
161
|
+
code = this._sanitizeCode(code);
|
|
162
|
+
let src = '';
|
|
163
|
+
code = code.replaceAll('console.log(', 'log(bot,');
|
|
164
|
+
code = code.replaceAll('log("', 'log(bot,"');
|
|
165
|
+
|
|
166
|
+
console.log(`Generated code: """${code}"""`);
|
|
167
|
+
|
|
168
|
+
// this may cause problems in callback functions
|
|
169
|
+
code = code.replaceAll(';\n', '; if(bot.interrupt_code) {log(bot, "Code interrupted.");return;}\n');
|
|
170
|
+
for (let line of code.split('\n')) {
|
|
171
|
+
src += ` ${line}\n`;
|
|
172
|
+
}
|
|
173
|
+
let src_lint_copy = this.code_lint_template.replace('/* CODE HERE */', src);
|
|
174
|
+
src = this.code_template.replace('/* CODE HERE */', src);
|
|
175
|
+
|
|
176
|
+
let filename = this.file_counter + '.js';
|
|
177
|
+
// if (this.file_counter > 0) {
|
|
178
|
+
// let prev_filename = this.fp + (this.file_counter-1) + '.js';
|
|
179
|
+
// unlink(prev_filename, (err) => {
|
|
180
|
+
// console.log("deleted file " + prev_filename);
|
|
181
|
+
// if (err) console.error(err);
|
|
182
|
+
// });
|
|
183
|
+
// } commented for now, useful to keep files for debugging
|
|
184
|
+
this.file_counter++;
|
|
185
|
+
|
|
186
|
+
let write_result = await this._writeFilePromise(this.fp + filename, src);
|
|
187
|
+
// This is where we determine the environment the agent's code should be exposed to.
|
|
188
|
+
// It will only have access to these things, (in addition to basic javascript objects like Array, Object, etc.)
|
|
189
|
+
// Note that the code may be able to modify the exposed objects.
|
|
190
|
+
const compartment = makeCompartment({
|
|
191
|
+
skills,
|
|
192
|
+
log: skills.log,
|
|
193
|
+
world,
|
|
194
|
+
Vec3,
|
|
195
|
+
});
|
|
196
|
+
const mainFn = compartment.evaluate(src);
|
|
197
|
+
|
|
198
|
+
if (write_result) {
|
|
199
|
+
console.error('Error writing code execution file: ' + write_result);
|
|
200
|
+
return null;
|
|
201
|
+
}
|
|
202
|
+
return { func:{main: mainFn}, src_lint_copy: src_lint_copy };
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
_sanitizeCode(code) {
|
|
206
|
+
code = code.trim();
|
|
207
|
+
const remove_strs = ['Javascript', 'javascript', 'js']
|
|
208
|
+
for (let r of remove_strs) {
|
|
209
|
+
if (code.startsWith(r)) {
|
|
210
|
+
code = code.slice(r.length);
|
|
211
|
+
return code;
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
return code;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
_writeFilePromise(filename, src) {
|
|
218
|
+
// makes it so we can await this function
|
|
219
|
+
return new Promise((resolve, reject) => {
|
|
220
|
+
writeFile(filename, src, (err) => {
|
|
221
|
+
if (err) {
|
|
222
|
+
reject(err);
|
|
223
|
+
} else {
|
|
224
|
+
resolve();
|
|
225
|
+
}
|
|
226
|
+
});
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
}
|