-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfix-eliza-config.js
More file actions
executable file
·310 lines (264 loc) · 9.22 KB
/
Copy pathfix-eliza-config.js
File metadata and controls
executable file
·310 lines (264 loc) · 9.22 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
#!/usr/bin/env node
/**
* ElizaOS Configuration Fix
*
* This script:
* 1. Stops any running ElizaOS processes
* 2. Sets required environment variables for models
* 3. Creates runtime patches to override model settings
* 4. Establishes proper package links
* 5. Starts the agent with the correct configuration
*/
const { spawn, execSync, spawnSync } = require('child_process');
const fs = require('fs');
const path = require('path');
// Ensure we're in the project root
const projectRoot = process.cwd();
console.log(`Running in project root: ${projectRoot}`);
// Stop any running processes
console.log('Stopping any running ElizaOS processes...');
try {
// Kill any node processes that contain eliza in their command line
execSync('pkill -f "node.*eliza" || true');
// Specifically kill telegram bot processes
execSync('pkill -f "node-telegram-bot-api" || true');
execSync('pkill -f "telegraf" || true');
console.log('Processes stopped successfully.');
} catch (error) {
console.log('No processes needed to be stopped.');
}
// Wait for connections to fully close
console.log('Waiting for connections to close...');
spawnSync('sleep', ['2']);
// Set environment variables
process.env.LARGE_OPENAI_MODEL = 'gpt-4o';
process.env.MEDIUM_OPENAI_MODEL = 'gpt-4o';
process.env.SMALL_OPENAI_MODEL = 'gpt-4o-mini';
process.env.EMBEDDING_OPENAI_MODEL = 'text-embedding-3-small';
process.env.USE_OPENAI_EMBEDDING = 'true';
// Create .env file
const envPath = path.join(projectRoot, '.env');
const envContent = `
LARGE_OPENAI_MODEL=gpt-4o
MEDIUM_OPENAI_MODEL=gpt-4o
SMALL_OPENAI_MODEL=gpt-4o-mini
EMBEDDING_OPENAI_MODEL=text-embedding-3-small
USE_OPENAI_EMBEDDING=true
`;
fs.writeFileSync(envPath, envContent);
console.log(`Created .env file at ${envPath}`);
// Create a runtime patch script that will be required before loading the agent
const patchPath = path.join(projectRoot, 'runtime-model-patch.js');
const patchContent = `
// ElizaOS Runtime Model Patch
// This script fixes model configuration at runtime
// Set environment variables
process.env.LARGE_OPENAI_MODEL = 'gpt-4o';
process.env.MEDIUM_OPENAI_MODEL = 'gpt-4o';
process.env.SMALL_OPENAI_MODEL = 'gpt-4o-mini';
process.env.EMBEDDING_OPENAI_MODEL = 'text-embedding-3-small';
process.env.USE_OPENAI_EMBEDDING = 'true';
console.log('[MODEL-PATCH] Applying runtime model configuration patch');
// Patch the global models object that will be used by the core package
// This is a workaround for build issues in the core package
global.__modelSettings = {
openai: {
model: {
LARGE: {
name: 'gpt-4o',
maxInputTokens: 128000,
maxOutputTokens: 4096,
temperature: 0.7,
stop: [],
frequency_penalty: 0,
presence_penalty: 0,
},
MEDIUM: {
name: 'gpt-4o',
maxInputTokens: 128000,
maxOutputTokens: 4096,
temperature: 0.7,
stop: [],
frequency_penalty: 0,
presence_penalty: 0,
},
SMALL: {
name: 'gpt-4o-mini',
maxInputTokens: 16000,
maxOutputTokens: 2048,
temperature: 0.7,
stop: [],
frequency_penalty: 0,
presence_penalty: 0,
},
EMBEDDING: {
name: 'text-embedding-3-small',
dimensions: 1536,
},
IMAGE: {
name: 'dall-e-3',
steps: 50
},
}
}
};
// Override getModelSettings function when it's called
const originalRequire = module.require;
module.require = function(id) {
const exports = originalRequire.apply(this, arguments);
// Check if this is the models module from core
if (id.includes('models') && exports && typeof exports.getModelSettings === 'function') {
const originalGetModelSettings = exports.getModelSettings;
// Override the function
exports.getModelSettings = function(provider, type, settings) {
console.log(\`[PATCHED] getModelSettings called for \${provider}/\${type}\`);
// For openai provider, return our hardcoded settings
if (provider === 'openai' && global.__modelSettings?.openai?.model?.[type]) {
return { ...global.__modelSettings.openai.model[type] };
}
// Try the original function
try {
return originalGetModelSettings(provider, type, settings);
} catch (error) {
console.warn(\`[PATCHED] Original getModelSettings failed: \${error.message}\`);
// Fallback to hardcoded values
if (type === 'LARGE' || type === 'MEDIUM') {
return {
name: process.env.LARGE_OPENAI_MODEL || 'gpt-4o',
maxInputTokens: 128000,
maxOutputTokens: 4096,
temperature: 0.7,
stop: [],
frequency_penalty: 0,
presence_penalty: 0,
};
} else if (type === 'SMALL') {
return {
name: process.env.SMALL_OPENAI_MODEL || 'gpt-4o-mini',
maxInputTokens: 16000,
maxOutputTokens: 2048,
temperature: 0.7,
stop: [],
frequency_penalty: 0,
presence_penalty: 0,
};
} else if (type === 'EMBEDDING') {
return {
name: process.env.EMBEDDING_OPENAI_MODEL || 'text-embedding-3-small',
dimensions: 1536,
};
}
// Last resort fallback
return {
name: 'gpt-4o',
maxInputTokens: 128000,
maxOutputTokens: 4096,
temperature: 0.7,
};
}
};
}
return exports;
};
console.log('[MODEL-PATCH] Runtime patches applied successfully');
`;
fs.writeFileSync(patchPath, patchContent);
console.log(`Created runtime patch at ${patchPath}`);
// Create a script to direct-run the agent
const runScriptPath = path.join(projectRoot, 'run-fixed-agent.js');
const runScriptContent = `#!/usr/bin/env node
/**
* Run Fixed Agent
*
* This script directly loads the agent with runtime patches applied
* to bypass package resolution issues.
*/
const path = require('path');
const fs = require('fs');
const { spawn } = require('child_process');
// Load our runtime patch first
require('./runtime-model-patch.js');
// Extract character from arguments
let characterPath = 'characters/aengel.json';
const characterArg = process.argv.find(arg => arg.startsWith('--character='));
if (characterArg) {
characterPath = characterArg.split('=')[1];
}
console.log(\`Starting agent with character: \${characterPath}\`);
// Set NODE_PATH to include our local packages
process.env.NODE_PATH = [
path.join(__dirname, 'packages/core/dist'),
path.join(__dirname, 'node_modules'),
process.env.NODE_PATH
].filter(Boolean).join(':');
// Force Node.js to re-evaluate the module search paths
require('module').Module._initPaths();
// Start by running the agent script directly
let agentProcess;
try {
// We're bypassing the normal package resolution by directly running the agent's compiled code
// Since we can't require ESM modules from CommonJS, we spawn a new process
agentProcess = spawn('node', [
'--require', './runtime-model-patch.js',
path.join(__dirname, 'packages/agent/dist/index.js'),
'--',
\`--character=\${characterPath}\`
], {
env: process.env,
stdio: 'inherit'
});
agentProcess.on('error', (error) => {
console.error('Failed to start agent process:', error);
process.exit(1);
});
// Handle process exit
agentProcess.on('close', (code) => {
console.log(\`Agent process exited with code \${code}\`);
process.exit(code);
});
} catch (error) {
console.error('Failed to start agent:', error);
process.exit(1);
}
`;
fs.writeFileSync(runScriptPath, runScriptContent);
fs.chmodSync(runScriptPath, '755');
console.log(`Created agent runner script at ${runScriptPath}`);
// Create bash script for easy running
const bashScriptPath = path.join(projectRoot, 'start-fixed-agent.sh');
const bashScriptContent = `#!/bin/bash
# Stop any running ElizaOS instances
echo "Stopping any running ElizaOS instances..."
./cleanup-agents.sh
# Start the agent with patched model settings
echo "Starting agent with patched model settings..."
node run-fixed-agent.js \${@}
`;
fs.writeFileSync(bashScriptPath, bashScriptContent);
fs.chmodSync(bashScriptPath, '755');
console.log(`Created start script at ${bashScriptPath}`);
// Create a cleanup script
const cleanupScriptPath = path.join(projectRoot, 'cleanup-agents.sh');
const cleanupScriptContent = `#!/bin/bash
# Carefully stop all ElizaOS instances
echo "=== Carefully stopping all ElizaOS instances ==="
# Find and kill ElizaOS processes
pkill -f "node.*eliza" || echo "No ElizaOS processes found."
sleep 1
echo "Waiting for connections to close..."
sleep 2
# Check for telegram bots specifically
echo "=== Checking for telegram bots ==="
pkill -f "node-telegram-bot-api" || echo "No Telegram bot processes found."
pkill -f "telegraf" || echo "No Telegraf processes found."
sleep 1
echo "=== Cleanup complete ==="
echo "You can now start the agent with: ./start-fixed-agent.sh"
`;
fs.writeFileSync(cleanupScriptPath, cleanupScriptContent);
fs.chmodSync(cleanupScriptPath, '755');
console.log(`Created cleanup script at ${cleanupScriptPath}`);
console.log('\nSetup complete. You can now run the agent with:');
console.log(' ./start-fixed-agent.sh');
console.log('\nTo stop all running agents:');
console.log(' ./cleanup-agents.sh');