heavylildude commited on
Commit
6c7818b
·
verified ·
1 Parent(s): 3282d7f

Upload 2 files

Browse files
Files changed (2) hide show
  1. config.js +11 -5
  2. magnus.js +135 -126
config.js CHANGED
@@ -1,11 +1,17 @@
1
  export const MAGNUS_VERSION = "251026";
2
 
 
 
3
  export const LLM_SERVER_BASE_URL = "http://127.0.0.1:8080";
4
  export const API_URL = `${LLM_SERVER_BASE_URL}/v1/chat/completions`;
5
  export const MEMORY_LIMIT = 10;
6
- export const CACHE_TTL = 10 * 60 * 1000;
7
 
8
- export const USER_ALIAS = "user";
9
- export const BRAVE_API_KEY = "ENTER_YOUR_KEY_HERE";
10
- export const GOOGLE_API_KEY = "ENTER_YOUR_KEY_HERE";
11
- export const GOOGLE_CX_ID = "ENTER_YOUR_KEY_HERE";
 
 
 
 
 
 
1
  export const MAGNUS_VERSION = "251026";
2
 
3
+ // --- LLM SERVER CONFIG ---
4
+ // Point this to your local llama-server instance
5
  export const LLM_SERVER_BASE_URL = "http://127.0.0.1:8080";
6
  export const API_URL = `${LLM_SERVER_BASE_URL}/v1/chat/completions`;
7
  export const MEMORY_LIMIT = 10;
 
8
 
9
+ export const USER_ALIAS = "chris, heavylildude, christo";
10
+
11
+ export const CACHE_TTL = 10 * 60 * 1000; // 10 minutes in milliseconds
12
+
13
+ export const BRAVE_API_KEY = "BSA03WdBwNgVWwMJHN6wYiZNTOyQQxh";
14
+ export const GOOGLE_API_KEY = "AIzaSyBBuXlCrsdqgsvID9dWgN3tLWKlvw_WDJg";
15
+ export const GOOGLE_CX_ID = "30c7bfc02547e42ca";
16
+
17
+ export const GGUF = "../GGUF/magnus-supernova-3.9B-Q4_K_M.gguf";
magnus.js CHANGED
@@ -1,127 +1,136 @@
1
- #!/usr/bin/env node
2
- import readline from "readline";
3
- import { spawn, execSync } from "child_process";
4
- import { initializeDatabase, db } from "./modules/database.js";
5
- import { bootUp, getPrompt, log, C, setReadline } from "./modules/ui.js";
6
- import { commandHandlers } from "./modules/commands.js";
7
- import { route } from "./modules/router.js";
8
-
9
- const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
10
- setReadline(rl);
11
-
12
- let multiline = false;
13
- let multiBuf = [];
14
-
15
- function refreshPrompt() {
16
- const newPrompt = getPrompt();
17
- rl.setPrompt(newPrompt);
18
- rl.prompt(true);
19
- }
20
-
21
- function handleMultiline(line) {
22
- if (line.trim() === "```") {
23
- if (!multiline) {
24
- multiline = true;
25
- multiBuf = [];
26
- log.gray("(multiline ON — type ``` to send)");
27
- rl.setPrompt('');
28
- rl.prompt();
29
- } else {
30
- multiline = false;
31
- return multiBuf.join("\n");
32
- }
33
- return null;
34
- }
35
- if (multiline) {
36
- multiBuf.push(line);
37
- return null;
38
- }
39
- return line.trim();
40
- }
41
-
42
- async function processInput(input, source = 'terminal') {
43
- if (!input) return false;
44
-
45
- try {
46
- const lowerInput = input.trim().toLowerCase();
47
-
48
- if (lowerInput === 'exit' || lowerInput === 'quit' || lowerInput === 'bye') {
49
- await commandHandlers.quit();
50
- return { isQuitting: true };
51
- } else if (input.trim().startsWith('/')) {
52
- const [command] = input.substring(1).split(/\s+/);
53
- const handler = commandHandlers[command];
54
- if (handler) {
55
- await handler(input);
56
- } else {
57
- log.warn(`Unknown command, brah. Sending to router anyway...`);
58
- await route(input);
59
- }
60
- } else {
61
- await route(input);
62
- }
63
- return { isQuitting: false };
64
- } catch (err) {
65
- log.error(`Magnus error: An unhandled exception occurred.`);
66
- console.error(err);
67
- return { isQuitting: false };
68
- }
69
- }
70
-
71
- let serverProcess = null;
72
-
73
- function startServer() {
74
- log.info('[SERVER] Firing up the llama-server engine...');
75
- serverProcess = spawn('llama-server', ['-m', 'magnus-one.gguf', '--jinja'], {
76
- detached: true,
77
- stdio: 'ignore'
78
- });
79
- serverProcess.unref();
80
- log.gray(`[SERVER] Llama-server is shredding on PID: ${serverProcess.pid}`);
81
- }
82
-
83
- function shutdown(exitCode = 0) {
84
- log.warn("\n[CLEANUP] Taking down the server... Catch ya later, legend!");
85
- if (serverProcess && serverProcess.pid) {
86
- try {
87
- if (process.platform === "win32") {
88
- execSync(`taskkill /pid ${serverProcess.pid} /f /t`);
89
- } else {
90
- process.kill(-serverProcess.pid, 'SIGTERM');
91
- }
92
- log.success("[CLEANUP] Server process terminated.");
93
- } catch (e) {
94
- log.error(`[CLEANUP] Bogus! Couldn't kill server process ${serverProcess.pid}: ${e.message}`);
95
- }
96
- }
97
- process.exit(exitCode);
98
- }
99
-
100
- async function main() {
101
- startServer();
102
- log.gray('[SYSTEM] Giving the server 5 seconds to warm up...');
103
- await new Promise(resolve => setTimeout(resolve, 5000));
104
- log.success('[SERVER] Aight, server should be ready. Let\'s rock!');
105
-
106
- initializeDatabase();
107
- await bootUp();
108
-
109
- rl.setPrompt(getPrompt());
110
- rl.prompt();
111
- rl.on("line", async (line) => {
112
- const processedLine = handleMultiline(line);
113
- if (processedLine === null) return;
114
- rl.pause();
115
- const { isQuitting } = await processInput(processedLine, 'terminal');
116
- if (isQuitting) {
117
- shutdown(0);
118
- return;
119
- }
120
- if (!multiline) refreshPrompt();
121
- rl.resume();
122
- });
123
- process.on('SIGINT', () => shutdown(0));
124
- process.on('SIGTERM', () => shutdown(0));
125
- }
126
-
 
 
 
 
 
 
 
 
 
127
  main().catch(err => { console.error(err); shutdown(1); });
 
1
+ #!/usr/bin/env node
2
+ import readline from "readline";
3
+ import { spawn, execSync } from "child_process";
4
+ import { initializeDatabase, db } from "./modules/database.js";
5
+ import { bootUp, getPrompt, log, C, setReadline } from "./modules/ui.js";
6
+ import { commandHandlers } from "./modules/commands.js";
7
+ import { route } from "./modules/router.js";
8
+ import { GGUF } from "./config.js";
9
+
10
+ const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
11
+ setReadline(rl); // Give the UI module access to the readline instance
12
+
13
+ let multiline = false;
14
+ let multiBuf = [];
15
+
16
+ function refreshPrompt() {
17
+ const newPrompt = getPrompt();
18
+ rl.setPrompt(newPrompt);
19
+ rl.prompt(true);
20
+ }
21
+
22
+ function handleMultiline(line) {
23
+ if (line.trim() === "```") {
24
+ if (!multiline) {
25
+ multiline = true;
26
+ multiBuf = [];
27
+ log.gray("(multiline ON — type ``` to send)");
28
+ rl.setPrompt(''); // Use a blank prompt for multiline input
29
+ rl.prompt();
30
+ } else {
31
+ multiline = false;
32
+ return multiBuf.join("\n");
33
+ }
34
+ return null;
35
+ }
36
+ if (multiline) {
37
+ multiBuf.push(line);
38
+ return null;
39
+ }
40
+ return line.trim();
41
+ }
42
+
43
+ async function processInput(input, source = 'terminal') {
44
+ if (!input) return false;
45
+
46
+ try {
47
+ const lowerInput = input.trim().toLowerCase();
48
+
49
+ if (lowerInput === 'exit' || lowerInput === 'quit' || lowerInput === 'bye') {
50
+ await commandHandlers.quit();
51
+ // Return a special value to signal that we are quitting and should not refresh the prompt.
52
+ return { isQuitting: true };
53
+ } else if (input.trim().startsWith('/')) {
54
+ const [command] = input.substring(1).split(/\s+/);
55
+ const handler = commandHandlers[command];
56
+ if (handler) {
57
+ await handler(input);
58
+ } else {
59
+ log.warn(`Unknown command, brah. Sending to router anyway...`);
60
+ await route(input);
61
+ }
62
+ } else {
63
+ await route(input);
64
+ }
65
+ // Return a default object if not quitting.
66
+ return { isQuitting: false };
67
+ } catch (err) {
68
+ log.error(`Magnus error: An unhandled exception occurred.`);
69
+ console.error(err);
70
+ return { isQuitting: false };
71
+ }
72
+ }
73
+
74
+ let serverProcess = null; // Keep the server process in a higher scope
75
+
76
+ function startServer() { // No longer returns the process
77
+ log.info('[SERVER] Firing up the llama-server engine...');
78
+ serverProcess = spawn('llama-server', ['-m', GGUF, '--jinja'], {
79
+ detached: true,
80
+ stdio: 'ignore'
81
+ });
82
+ serverProcess.unref(); // Let the main process exit independently
83
+ log.gray(`[SERVER] Llama-server is shredding on PID: ${serverProcess.pid}`);
84
+ }
85
+
86
+ function shutdown(exitCode = 0) {
87
+ log.warn("\n[CLEANUP] Taking down the server... Catch ya later, legend!");
88
+ if (serverProcess && serverProcess.pid) {
89
+ try {
90
+ if (process.platform === "win32") {
91
+ // Bitchin' Fix: Use execSync to block until the command finishes. No more zombies!
92
+ execSync(`taskkill /pid ${serverProcess.pid} /f /t`);
93
+ } else {
94
+ // Kill the entire process group. The `-` is the magic here.
95
+ process.kill(-serverProcess.pid, 'SIGTERM');
96
+ }
97
+ log.success("[CLEANUP] Server process terminated.");
98
+ } catch (e) {
99
+ log.error(`[CLEANUP] Bogus! Couldn't kill server process ${serverProcess.pid}: ${e.message}`);
100
+ }
101
+ }
102
+ process.exit(exitCode);
103
+ }
104
+
105
+ async function main() {
106
+ startServer();
107
+ log.gray('[SYSTEM] Giving the server 5 seconds to warm up...');
108
+ await new Promise(resolve => setTimeout(resolve, 5000)); // Simple 5-second delay
109
+ log.success('[SERVER] Aight, server should be ready. Let\'s rock!');
110
+
111
+ initializeDatabase();
112
+ await bootUp();
113
+
114
+ rl.setPrompt(getPrompt());
115
+ rl.prompt();
116
+ rl.on("line", async (line) => {
117
+ const processedLine = handleMultiline(line);
118
+
119
+ if (processedLine === null) return; // Multiline is active, just wait.
120
+
121
+ rl.pause(); // Pause the prompt while processing and streaming
122
+ const { isQuitting } = await processInput(processedLine, 'terminal');
123
+ if (isQuitting) {
124
+ shutdown(0); // Gracefully shutdown if command handler signals a quit
125
+ return;
126
+ }
127
+ if (!multiline) refreshPrompt();
128
+ rl.resume();
129
+ });
130
+
131
+ // Catch Ctrl+C and other termination signals for a graceful shutdown
132
+ process.on('SIGINT', () => shutdown(0));
133
+ process.on('SIGTERM', () => shutdown(0));
134
+ }
135
+
136
  main().catch(err => { console.error(err); shutdown(1); });