Repository navigation
Expand file tree
/
Copy pathprompter.js
More file actions
509 lines (455 loc) · 22.5 KB
/
Copy pathprompter.js
File metadata and controls
509 lines (455 loc) · 22.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
503
504
505
506
507
508
509
import { readFileSync, mkdirSync, writeFileSync} from 'fs';
import { Examples } from '../utils/examples.js';
import { getCommandDocs } from '../agent/commands/index.js';
import { SkillLibrary } from "../agent/library/skill_library.js";
import { stringifyTurns } from '../utils/text.js';
import { getCommand } from '../agent/commands/index.js';
import settings from '../agent/settings.js';
import { promises as fs } from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
import { selectAPI, createModel } from './_model_map.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
export class Prompter {
constructor(agent, profile) {
this.agent = agent;
this.profile = profile;
const defaults_dir = path.join(__dirname, '../../profiles/defaults');
let default_profile = JSON.parse(readFileSync(path.join(defaults_dir, '_default.json'), 'utf8'));
let base_fp = '';
if (settings.base_profile.includes('survival')) {
base_fp = path.join(defaults_dir, 'survival.json');
} else if (settings.base_profile.includes('assistant')) {
base_fp = path.join(defaults_dir, 'assistant.json');
} else if (settings.base_profile.includes('creative')) {
base_fp = path.join(defaults_dir, 'creative.json');
} else if (settings.base_profile.includes('god_mode')) {
base_fp = path.join(defaults_dir, 'god_mode.json');
}
let base_profile = JSON.parse(readFileSync(base_fp, 'utf8'));
// first use defaults to fill in missing values in the base profile
for (let key in default_profile) {
if (base_profile[key] === undefined)
base_profile[key] = default_profile[key];
}
// then use base profile to fill in missing values in the individual profile
for (let key in base_profile) {
if (this.profile[key] === undefined)
this.profile[key] = base_profile[key];
}
// base overrides default, individual overrides base
this.convo_examples = null;
this.coding_examples = null;
// Phase A3 telemetry: lifetime usage totals across all chat/code/memory calls.
// Per-turn lines logged from _recordUsage. Resets only on process restart.
this._usage_totals = { input: 0, output: 0, cache_read: 0, cache_creation: 0, turns: 0 };
let name = this.profile.name;
this.cooldown = this.profile.cooldown ? this.profile.cooldown : 0;
this.last_prompt_time = 0;
this.awaiting_coding = false;
// for backwards compatibility, move max_tokens to params
let max_tokens = null;
if (this.profile.max_tokens)
max_tokens = this.profile.max_tokens;
let chat_model_profile = selectAPI(this.profile.model);
this.chat_model = createModel(chat_model_profile);
if (this.profile.code_model) {
let code_model_profile = selectAPI(this.profile.code_model);
this.code_model = createModel(code_model_profile);
}
else {
this.code_model = this.chat_model;
}
// Phase G3: per-subagent model cache. Lazy creates a model instance
// per role profile's `model` field so the planner turn during a
// subagent's lifetime uses the stronger action model (typically
// Sonnet) without having to swap chat_model. Keys are model-id
// strings; createModel results are cached for process lifetime.
this._modelCache = new Map();
this._modelCache.set(this.profile.model, this.chat_model);
if (this.profile.vision_model) {
let vision_model_profile = selectAPI(this.profile.vision_model);
this.vision_model = createModel(vision_model_profile);
}
else {
this.vision_model = this.chat_model;
}
let embedding_model_profile = null;
if (this.profile.embedding) {
try {
embedding_model_profile = selectAPI(this.profile.embedding);
} catch (e) {
embedding_model_profile = null;
}
}
if (embedding_model_profile) {
this.embedding_model = createModel(embedding_model_profile);
}
else {
this.embedding_model = createModel({api: chat_model_profile.api});
}
this.skill_libary = new SkillLibrary(agent, this.embedding_model);
mkdirSync(`./bots/${name}`, { recursive: true });
writeFileSync(`./bots/${name}/last_profile.json`, JSON.stringify(this.profile, null, 4), (err) => {
if (err) {
throw new Error('Failed to save profile:', err);
}
console.log("Copy profile saved.");
});
}
// Phase G3: returns the model instance to use for the *current* planner
// turn. Defaults to chat_model; if a subagent is active and its role
// profile specified a model, lazy-creates + caches that instance and
// returns it so the subagent's turns get the stronger action model.
_modelForActiveTurn() {
const sub = this.agent?.activeSubagent;
if (!sub || !sub.model || sub.model === this.profile.model) return this.chat_model;
if (!this._modelCache.has(sub.model)) {
try {
const cfg = selectAPI(sub.model);
this._modelCache.set(sub.model, createModel(cfg));
} catch (e) {
console.warn(`[G3] failed to create subagent model ${sub.model}, falling back to chat_model:`, e?.message || e);
this._modelCache.set(sub.model, this.chat_model);
}
}
return this._modelCache.get(sub.model);
}
getName() {
return this.profile.name;
}
// Phase A3: read out-of-band usage from the last sendRequest on the given model,
// log one [cost] line, accumulate into this._usage_totals. No-op if the model
// wrapper doesn't expose last_usage (non-Claude wrappers).
_recordUsage(kind, model) {
const u = model?.last_usage;
if (!u) return;
const input = u.input_tokens || 0;
const output = u.output_tokens || 0;
const cache_read = u.cache_read_input_tokens || 0;
const cache_creation = u.cache_creation_input_tokens || 0;
this._usage_totals.input += input;
this._usage_totals.output += output;
this._usage_totals.cache_read += cache_read;
this._usage_totals.cache_creation += cache_creation;
this._usage_totals.turns += 1;
console.log(`[cost] kind=${kind} input=${input} output=${output} cache_read=${cache_read} cache_creation=${cache_creation} | total_turns=${this._usage_totals.turns} total_input=${this._usage_totals.input} total_output=${this._usage_totals.output} total_cache_read=${this._usage_totals.cache_read}`);
}
getInitModes() {
return this.profile.modes;
}
async initExamples() {
try {
this.convo_examples = new Examples(this.embedding_model, settings.num_examples);
this.coding_examples = new Examples(this.embedding_model, settings.num_examples);
// Wait for both examples to load before proceeding
await Promise.all([
this.convo_examples.load(this.profile.conversation_examples),
this.coding_examples.load(this.profile.coding_examples),
this.skill_libary.initSkillLibrary()
]).catch(error => {
// Preserve error details
console.error('Failed to initialize examples. Error details:', error);
console.error('Stack trace:', error.stack);
throw error;
});
console.log('Examples initialized.');
} catch (error) {
console.error('Failed to initialize examples:', error);
console.error('Stack trace:', error.stack);
throw error; // Re-throw with preserved details
}
}
async replaceStrings(prompt, messages, examples=null, to_summarize=[], last_goals=null) {
prompt = prompt.replaceAll('$NAME', this.agent.name);
if (prompt.includes('$TASKQUEUE')) {
const tq = this.agent.task_queue
? this.agent.task_queue.serialize({ planMode: this.agent.planMode === true })
: '';
prompt = prompt.replaceAll('$TASKQUEUE', tq);
}
if (prompt.includes('$MEMORY')) {
// Phase F4: $MEMORY now renders the layered hierarchy (server +
// bot + active-player). Falls back to the bot-only index if the
// layered builder isn't available for any reason.
let mem = '';
try {
const { buildLayeredMemoryIndex } = await import('../agent/memory_store.js');
mem = buildLayeredMemoryIndex(this.agent);
} catch (e) {
mem = this.agent.memory_store ? this.agent.memory_store.serializeIndex() : '';
}
prompt = prompt.replaceAll('$MEMORY', mem);
}
if (prompt.includes('$COC')) {
// Staple CLAUDE.md + player-editable house rules (see coc.js —
// the lectern writes house_rules.md, never CLAUDE.md).
const { composeCoC } = await import('../agent/coc.js');
prompt = prompt.replaceAll('$COC', composeCoC(this.agent.name));
}
// Static Minecraft knowledge — recipe prerequisites, tool tiers, block
// family aliases, dimension hazards. Edit bots/knowledge.md to add
// facts the bot keeps re-discovering by trial and error. Loaded fresh
// every turn so a hot edit propagates without restart.
if (prompt.includes('$WORLD_KNOWLEDGE')) {
let knowledge = '';
try {
knowledge = readFileSync(path.join(__dirname, '../../bots/knowledge.md'), 'utf8').trim();
} catch (e) {
knowledge = '(no knowledge file)';
}
prompt = prompt.replaceAll('$WORLD_KNOWLEDGE', knowledge);
}
if (prompt.includes('$STATS')) {
let stats = await getCommand('!stats').perform(this.agent) + '\n';
stats += await getCommand('!entities').perform(this.agent) + '\n';
stats += await getCommand('!nearbyBlocks').perform(this.agent);
prompt = prompt.replaceAll('$STATS', stats);
}
if (prompt.includes('$INVENTORY')) {
let inventory = await getCommand('!inventory').perform(this.agent);
prompt = prompt.replaceAll('$INVENTORY', inventory);
}
if (prompt.includes('$ACTION')) {
prompt = prompt.replaceAll('$ACTION', this.agent.actions.currentActionLabel);
}
if (prompt.includes('$COMMAND_DOCS'))
prompt = prompt.replaceAll('$COMMAND_DOCS', getCommandDocs(this.agent));
if (prompt.includes('$CODE_DOCS')) {
let code_task_content = messages.slice().reverse().find(msg =>
msg.role !== 'system' && typeof msg.content === 'string' && msg.content.includes('!newAction(')
)?.content?.match(/!newAction\((.*?)\)/)?.[1];
// The v2 orchestrator generates code without the legacy !newAction(...)
// wrapper, so the capture above is empty — which left skill-doc selection
// (incl. the gather-doc trigger in skill_library) matching an empty string
// and always returning the same generic docs. Fall back to the active
// task's description so doc selection sees the real intent ("chop oak logs").
if (!code_task_content) {
const active = this.agent.task_queue?.tasks?.find(t => t.status === 'in_progress');
code_task_content = active?.description || '';
}
// Free-chat commands ("get items from your chest") have no queued task
// and no !newAction wrapper, so both captures above stay empty and doc
// selection falls back to the generic mining set. Use the latest player
// message as the intent so conversational commands surface the right docs.
if (!code_task_content) {
code_task_content = messages.slice().reverse().find(msg =>
msg.role !== 'system' && typeof msg.content === 'string' && msg.content.trim()
)?.content || '';
}
prompt = prompt.replaceAll(
'$CODE_DOCS',
await this.skill_libary.getRelevantSkillDocs(code_task_content, settings.relevant_docs_count)
);
}
if (prompt.includes('$EXAMPLES') && examples !== null)
prompt = prompt.replaceAll('$EXAMPLES', await examples.createExampleMessage(messages));
// Phase F1: the legacy `$MEMORY → history.memory` replacement that
// used to live here is gone — the earlier replaceStrings($MEMORY ↔
// memory_store) wins for every prompt, and the saving_memory
// pipeline is dormant. Removed to avoid the appearance of two
// sources of truth for $MEMORY.
if (prompt.includes('$TO_SUMMARIZE'))
prompt = prompt.replaceAll('$TO_SUMMARIZE', stringifyTurns(to_summarize));
if (prompt.includes('$CONVO'))
prompt = prompt.replaceAll('$CONVO', 'Recent conversation:\n' + stringifyTurns(messages));
if (prompt.includes('$SELF_PROMPT')) {
// if active or paused, show the current goal
let self_prompt = !this.agent.self_prompter.isStopped() ? `YOUR CURRENT ASSIGNED GOAL: "${this.agent.self_prompter.prompt}"\n` : '';
prompt = prompt.replaceAll('$SELF_PROMPT', self_prompt);
}
if (prompt.includes('$LAST_GOALS')) {
let goal_text = '';
for (let goal in last_goals) {
if (last_goals[goal])
goal_text += `You recently successfully completed the goal ${goal}.\n`
else
goal_text += `You recently failed to complete the goal ${goal}.\n`
}
prompt = prompt.replaceAll('$LAST_GOALS', goal_text.trim());
}
if (prompt.includes('$BLUEPRINTS')) {
if (this.agent.npc.constructions) {
let blueprints = '';
for (let blueprint in this.agent.npc.constructions) {
blueprints += blueprint + ', ';
}
prompt = prompt.replaceAll('$BLUEPRINTS', blueprints.slice(0, -2));
}
}
// check if there are any remaining placeholders with syntax $<word>
let remaining = prompt.match(/\$[A-Z_]+/g);
if (remaining !== null) {
console.warn('Unknown prompt placeholders:', remaining.join(', '));
}
return prompt;
}
async checkCooldown() {
let elapsed = Date.now() - this.last_prompt_time;
if (elapsed < this.cooldown && this.cooldown > 0) {
await new Promise(r => setTimeout(r, this.cooldown - elapsed));
}
this.last_prompt_time = Date.now();
}
async promptConvo(messages) {
this.most_recent_msg_time = Date.now();
let current_msg_time = this.most_recent_msg_time;
for (let i = 0; i < 3; i++) { // try 3 times to avoid hallucinations
await this.checkCooldown();
if (current_msg_time !== this.most_recent_msg_time) {
return '';
}
let prompt = this.profile.conversing;
prompt = await this.replaceStrings(prompt, messages, this.convo_examples);
let generation;
try {
const activeModel = this._modelForActiveTurn();
generation = await activeModel.sendRequest(messages, prompt);
this._recordUsage('convo', activeModel);
if (typeof generation !== 'string') {
console.error('Error: Generated response is not a string', generation);
throw new Error('Generated response is not a string');
}
console.log("Generated response:", generation);
await this._saveLog(prompt, messages, generation, 'conversation');
} catch (error) {
console.error('Error during message generation or file writing:', error);
continue;
}
// Check for hallucination or invalid output
if (generation?.includes('(FROM OTHER BOT)')) {
console.warn('LLM hallucinated message as another bot. Trying again...');
continue;
}
if (current_msg_time !== this.most_recent_msg_time) {
console.warn(`${this.agent.name} received new message while generating, discarding old response.`);
return '';
}
if (generation?.includes('</think>')) {
const [_, afterThink] = generation.split('</think>')
generation = afterThink
}
return generation;
}
return '';
}
async promptCoding(messages) {
if (this.awaiting_coding) {
console.warn('Already awaiting coding response, returning no response.');
return '```//no response```';
}
this.awaiting_coding = true;
await this.checkCooldown();
let prompt = this.profile.coding;
prompt = await this.replaceStrings(prompt, messages, this.coding_examples);
let resp = await this.code_model.sendRequest(messages, prompt);
this._recordUsage('coding', this.code_model);
this.awaiting_coding = false;
await this._saveLog(prompt, messages, resp, 'coding');
return resp;
}
async promptMemSaving(to_summarize) {
await this.checkCooldown();
let prompt = this.profile.saving_memory;
prompt = await this.replaceStrings(prompt, null, null, to_summarize);
let resp = await this.chat_model.sendRequest([], prompt);
this._recordUsage('memsave', this.chat_model);
await this._saveLog(prompt, to_summarize, resp, 'memSaving');
if (resp?.includes('</think>')) {
const [_, afterThink] = resp.split('</think>')
resp = afterThink;
}
return resp;
}
// Phase A4: compact older turns into one chronological summary string.
// Returns the summary text; caller is responsible for splicing it back into history.
// Uses a dedicated `compaction` prompt — different from `saving_memory` which
// produces a 500-char facts buffer for the legacy $MEMORY field (Phase F kills that).
async promptCompact(to_summarize) {
if (!to_summarize || to_summarize.length === 0) return '';
await this.checkCooldown();
let prompt = this.profile.compaction;
if (!prompt) {
console.warn('[compact] no compaction prompt in profile; skipping');
return '';
}
prompt = await this.replaceStrings(prompt, null, null, to_summarize);
let resp = await this.chat_model.sendRequest([], prompt);
this._recordUsage('compact', this.chat_model);
await this._saveLog(prompt, to_summarize, resp, 'compact');
if (resp?.includes('</think>')) {
const [_, afterThink] = resp.split('</think>');
resp = afterThink;
}
// Hard cap so a runaway summary can't re-bloat the context.
const MAX = 2000;
if (resp.length > MAX) resp = resp.slice(0, MAX) + '...(truncated)';
return resp.trim();
}
async promptShouldRespondToBot(new_message) {
await this.checkCooldown();
let prompt = this.profile.bot_responder;
let messages = this.agent.history.getHistory();
messages.push({role: 'user', content: new_message});
prompt = await this.replaceStrings(prompt, null, null, messages);
let res = await this.chat_model.sendRequest([], prompt);
this._recordUsage('respond_check', this.chat_model);
return res.trim().toLowerCase() === 'respond';
}
async promptVision(messages, imageBuffer) {
await this.checkCooldown();
let prompt = this.profile.image_analysis;
prompt = await this.replaceStrings(prompt, messages, null, null, null);
return await this.vision_model.sendVisionRequest(messages, prompt, imageBuffer);
}
async promptGoalSetting(messages, last_goals) {
// deprecated
let system_message = this.profile.goal_setting;
system_message = await this.replaceStrings(system_message, messages);
let user_message = 'Use the below info to determine what goal to target next\n\n';
user_message += '$LAST_GOALS\n$STATS\n$INVENTORY\n$CONVO'
user_message = await this.replaceStrings(user_message, messages, null, null, last_goals);
let user_messages = [{role: 'user', content: user_message}];
let res = await this.chat_model.sendRequest(user_messages, system_message);
let goal = null;
try {
let data = res.split('```')[1].replace('json', '').trim();
goal = JSON.parse(data);
} catch (err) {
console.log('Failed to parse goal:', res, err);
}
if (!goal || !goal.name || !goal.quantity || isNaN(parseInt(goal.quantity))) {
console.log('Failed to set goal:', res);
return null;
}
goal.quantity = parseInt(goal.quantity);
return goal;
}
async _saveLog(prompt, messages, generation, tag) {
if (!settings.log_all_prompts)
return;
const timestamp = new Date().toISOString().replace(/[:.]/g, '-');
let logEntry;
let task_id = this.agent.task.task_id;
if (task_id == null) {
logEntry = `[${timestamp}] \nPrompt:\n${prompt}\n\nConversation:\n${JSON.stringify(messages, null, 2)}\n\nResponse:\n${generation}\n\n`;
} else {
logEntry = `[${timestamp}] Task ID: ${task_id}\nPrompt:\n${prompt}\n\nConversation:\n${JSON.stringify(messages, null, 2)}\n\nResponse:\n${generation}\n\n`;
}
const logFile = `${tag}_${timestamp}.txt`;
await this._saveToFile(logFile, logEntry);
}
async _saveToFile(logFile, logEntry) {
let task_id = this.agent.task.task_id;
let logDir;
if (task_id == null) {
logDir = path.join(__dirname, `../../bots/${this.agent.name}/logs`);
} else {
logDir = path.join(__dirname, `../../bots/${this.agent.name}/logs/${task_id}`);
}
await fs.mkdir(logDir, { recursive: true });
logFile = path.join(logDir, logFile);
await fs.appendFile(logFile, String(logEntry), 'utf-8');
}
}