te.js 2.2.0 → 2.2.1
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/README.md +1 -12
- package/docs/README.md +1 -2
- package/docs/api-reference.md +124 -186
- package/docs/configuration.md +0 -13
- package/docs/getting-started.md +19 -21
- package/docs/rate-limiting.md +59 -58
- package/lib/llm/client.js +1 -1
- package/package.json +1 -1
- package/radar/index.js +191 -90
- package/rate-limit/base.js +12 -15
- package/rate-limit/index.js +12 -12
- package/rate-limit/index.test.js +45 -16
- package/rate-limit/storage/memory.js +13 -13
- package/rate-limit/storage/redis-install.js +70 -0
- package/rate-limit/storage/redis.js +94 -52
- package/server/ammo.js +3 -2
- package/te.js +64 -143
- package/utils/errors-llm-config.js +63 -0
- package/utils/startup.js +80 -0
- package/database/index.js +0 -167
- package/database/mongodb.js +0 -152
- package/database/redis.js +0 -210
- package/docs/database.md +0 -390
package/database/mongodb.js
DELETED
|
@@ -1,152 +0,0 @@
|
|
|
1
|
-
import { spawn } from 'node:child_process';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import { fileURLToPath } from 'node:url';
|
|
5
|
-
import TejLogger from 'tej-logger';
|
|
6
|
-
import TejError from '../server/error.js';
|
|
7
|
-
|
|
8
|
-
const __filename = fileURLToPath(import.meta.url);
|
|
9
|
-
const __dirname = path.dirname(__filename);
|
|
10
|
-
|
|
11
|
-
const logger = new TejLogger('MongoDBConnectionManager');
|
|
12
|
-
|
|
13
|
-
async function checkMongooseInstallation() {
|
|
14
|
-
const packageJsonPath = path.join(__dirname, '..', 'package.json');
|
|
15
|
-
const nodeModulesPath = path.join(
|
|
16
|
-
__dirname,
|
|
17
|
-
'..',
|
|
18
|
-
'node_modules',
|
|
19
|
-
'mongoose',
|
|
20
|
-
);
|
|
21
|
-
|
|
22
|
-
try {
|
|
23
|
-
const packageJson = JSON.parse(
|
|
24
|
-
await fs.promises.readFile(packageJsonPath, 'utf8'),
|
|
25
|
-
);
|
|
26
|
-
const inPackageJson = !!packageJson.dependencies?.mongoose;
|
|
27
|
-
|
|
28
|
-
let inNodeModules = false;
|
|
29
|
-
try {
|
|
30
|
-
await fs.promises.access(nodeModulesPath);
|
|
31
|
-
inNodeModules = true;
|
|
32
|
-
} catch {
|
|
33
|
-
inNodeModules = false;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return {
|
|
37
|
-
needsInstall: !inPackageJson || !inNodeModules,
|
|
38
|
-
reason: !inPackageJson
|
|
39
|
-
? 'not in package.json'
|
|
40
|
-
: !inNodeModules
|
|
41
|
-
? 'not in node_modules'
|
|
42
|
-
: null,
|
|
43
|
-
};
|
|
44
|
-
} catch (error) {
|
|
45
|
-
return { needsInstall: true, reason: 'error checking installation' };
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
function installMongooseSync() {
|
|
50
|
-
const spinner = ['|', '/', '-', '\\'];
|
|
51
|
-
let current = 0;
|
|
52
|
-
let intervalId;
|
|
53
|
-
|
|
54
|
-
try {
|
|
55
|
-
const { needsInstall, reason } = checkMongooseInstallation();
|
|
56
|
-
|
|
57
|
-
if (!needsInstall) {
|
|
58
|
-
return true;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
// Start the spinner
|
|
62
|
-
intervalId = setInterval(() => {
|
|
63
|
-
process.stdout.write(`\r${spinner[current]} Installing mongoose...`);
|
|
64
|
-
current = (current + 1) % spinner.length;
|
|
65
|
-
}, 100);
|
|
66
|
-
|
|
67
|
-
logger.info(`Tejas will install mongoose (${reason})...`);
|
|
68
|
-
|
|
69
|
-
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
70
|
-
const result = spawn.sync(command, ['install', 'mongoose'], {
|
|
71
|
-
stdio: 'inherit',
|
|
72
|
-
shell: true,
|
|
73
|
-
});
|
|
74
|
-
|
|
75
|
-
process.stdout.write('\r');
|
|
76
|
-
clearInterval(intervalId);
|
|
77
|
-
|
|
78
|
-
if (result.status === 0) {
|
|
79
|
-
logger.info('Mongoose installed successfully');
|
|
80
|
-
return true;
|
|
81
|
-
} else {
|
|
82
|
-
logger.error('Mongoose installation failed');
|
|
83
|
-
return false;
|
|
84
|
-
}
|
|
85
|
-
} catch (error) {
|
|
86
|
-
if (intervalId) {
|
|
87
|
-
process.stdout.write('\r');
|
|
88
|
-
clearInterval(intervalId);
|
|
89
|
-
}
|
|
90
|
-
logger.error('Error installing mongoose:', error);
|
|
91
|
-
return false;
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
|
|
95
|
-
/**
|
|
96
|
-
* Create a new MongoDB connection
|
|
97
|
-
* @param {Object} config - MongoDB configuration
|
|
98
|
-
* @param {string} config.uri - MongoDB connection URI
|
|
99
|
-
* @param {Object} [config.options={}] - Additional Mongoose options
|
|
100
|
-
* @returns {Promise<mongoose.Connection>} Mongoose connection instance
|
|
101
|
-
*/
|
|
102
|
-
async function createConnection(config) {
|
|
103
|
-
const { needsInstall } = await checkMongooseInstallation();
|
|
104
|
-
|
|
105
|
-
if (needsInstall) {
|
|
106
|
-
const installed = installMongooseSync();
|
|
107
|
-
if (!installed) {
|
|
108
|
-
throw new TejError(500, 'Failed to install required mongoose package');
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
|
|
112
|
-
const { uri, options = {} } = config;
|
|
113
|
-
|
|
114
|
-
try {
|
|
115
|
-
const { default: mongoose } = await import('mongoose');
|
|
116
|
-
const connection = await mongoose.createConnection(uri, options);
|
|
117
|
-
|
|
118
|
-
connection.on('error', (err) =>
|
|
119
|
-
logger.error(`MongoDB connection error:`, err),
|
|
120
|
-
);
|
|
121
|
-
connection.on('connected', () => {
|
|
122
|
-
logger.info(`MongoDB connected to ${uri}`);
|
|
123
|
-
});
|
|
124
|
-
connection.on('disconnected', () => {
|
|
125
|
-
logger.info(`MongoDB disconnected from ${uri}`);
|
|
126
|
-
});
|
|
127
|
-
|
|
128
|
-
return connection;
|
|
129
|
-
} catch (error) {
|
|
130
|
-
logger.error(`Failed to create MongoDB connection:`, error);
|
|
131
|
-
throw new TejError(
|
|
132
|
-
500,
|
|
133
|
-
`Failed to create MongoDB connection: ${error.message}`,
|
|
134
|
-
);
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Close a MongoDB connection
|
|
140
|
-
* @param {mongoose.Connection} connection - Mongoose connection to close
|
|
141
|
-
* @returns {Promise<void>}
|
|
142
|
-
*/
|
|
143
|
-
async function closeConnection(connection) {
|
|
144
|
-
if (connection) {
|
|
145
|
-
await connection.close();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
export default {
|
|
150
|
-
createConnection,
|
|
151
|
-
closeConnection,
|
|
152
|
-
};
|
package/database/redis.js
DELETED
|
@@ -1,210 +0,0 @@
|
|
|
1
|
-
import { spawnSync } from 'node:child_process';
|
|
2
|
-
import fs from 'node:fs';
|
|
3
|
-
import path from 'node:path';
|
|
4
|
-
import TejError from '../server/error.js';
|
|
5
|
-
import TejLogger from 'tej-logger';
|
|
6
|
-
import { pathToFileURL } from 'node:url';
|
|
7
|
-
|
|
8
|
-
const packageJsonPath = path.join(process.cwd(), 'package.json');
|
|
9
|
-
const packagePath = `${process.cwd()}/node_modules/redis/dist/index.js`;
|
|
10
|
-
|
|
11
|
-
const logger = new TejLogger('RedisConnectionManager');
|
|
12
|
-
|
|
13
|
-
async function checkRedisInstallation() {
|
|
14
|
-
try {
|
|
15
|
-
const packageJson = JSON.parse(
|
|
16
|
-
await fs.promises.readFile(packageJsonPath, 'utf8'),
|
|
17
|
-
);
|
|
18
|
-
const inPackageJson = !!packageJson.dependencies?.redis;
|
|
19
|
-
|
|
20
|
-
let inNodeModules = false;
|
|
21
|
-
try {
|
|
22
|
-
await fs.promises.access(packagePath);
|
|
23
|
-
inNodeModules = true;
|
|
24
|
-
} catch {
|
|
25
|
-
inNodeModules = false;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return {
|
|
29
|
-
needsInstall: !inPackageJson || !inNodeModules,
|
|
30
|
-
reason: !inPackageJson
|
|
31
|
-
? 'not in package.json'
|
|
32
|
-
: !inNodeModules
|
|
33
|
-
? 'not in node_modules'
|
|
34
|
-
: null,
|
|
35
|
-
};
|
|
36
|
-
} catch (error) {
|
|
37
|
-
logger.error(error, true);
|
|
38
|
-
return { needsInstall: true, reason: 'error checking installation' };
|
|
39
|
-
}
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
function installRedisSync() {
|
|
43
|
-
const spinner = ['|', '/', '-', '\\'];
|
|
44
|
-
let current = 0;
|
|
45
|
-
let intervalId;
|
|
46
|
-
|
|
47
|
-
try {
|
|
48
|
-
const { needsInstall, reason } = checkRedisInstallation();
|
|
49
|
-
|
|
50
|
-
if (!needsInstall) {
|
|
51
|
-
return true;
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
// Start the spinner
|
|
55
|
-
intervalId = setInterval(() => {
|
|
56
|
-
process.stdout.write(`\r${spinner[current]} Installing redis...`);
|
|
57
|
-
current = (current + 1) % spinner.length;
|
|
58
|
-
}, 100);
|
|
59
|
-
|
|
60
|
-
logger.info(`Tejas will install redis (${reason})...`);
|
|
61
|
-
|
|
62
|
-
const command = process.platform === 'win32' ? 'npm.cmd' : 'npm';
|
|
63
|
-
const result = spawnSync(command, ['install', 'redis'], {
|
|
64
|
-
stdio: 'inherit',
|
|
65
|
-
shell: true,
|
|
66
|
-
});
|
|
67
|
-
|
|
68
|
-
process.stdout.write('\r');
|
|
69
|
-
clearInterval(intervalId);
|
|
70
|
-
|
|
71
|
-
if (result.status === 0) {
|
|
72
|
-
logger.info('Redis installed successfully');
|
|
73
|
-
return true;
|
|
74
|
-
} else {
|
|
75
|
-
logger.error('Redis installation failed');
|
|
76
|
-
return false;
|
|
77
|
-
}
|
|
78
|
-
} catch (error) {
|
|
79
|
-
if (intervalId) {
|
|
80
|
-
process.stdout.write('\r');
|
|
81
|
-
clearInterval(intervalId);
|
|
82
|
-
}
|
|
83
|
-
logger.error(error, true);
|
|
84
|
-
return false;
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
/**
|
|
89
|
-
* Create a new Redis client or cluster
|
|
90
|
-
* @param {Object} config - Redis configuration
|
|
91
|
-
* @param {boolean} [config.isCluster=false] - Whether to use Redis Cluster
|
|
92
|
-
* @param {Object} [config.options={}] - Additional Redis options
|
|
93
|
-
* @returns {Promise<RedisClient|RedisCluster>} Redis client or cluster instance
|
|
94
|
-
*/
|
|
95
|
-
async function createConnection(config) {
|
|
96
|
-
const { needsInstall } = await checkRedisInstallation();
|
|
97
|
-
|
|
98
|
-
if (needsInstall) {
|
|
99
|
-
const installed = installRedisSync();
|
|
100
|
-
if (!installed) {
|
|
101
|
-
throw new TejError(500, 'Failed to install required redis package');
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const { isCluster = false, options = {} } = config;
|
|
106
|
-
let client;
|
|
107
|
-
|
|
108
|
-
try {
|
|
109
|
-
const { createClient, createCluster } = await import(
|
|
110
|
-
pathToFileURL(packagePath)
|
|
111
|
-
);
|
|
112
|
-
|
|
113
|
-
if (isCluster) {
|
|
114
|
-
client = createCluster({
|
|
115
|
-
...options,
|
|
116
|
-
});
|
|
117
|
-
} else {
|
|
118
|
-
client = createClient({
|
|
119
|
-
...options,
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
let connectionTimeout;
|
|
124
|
-
let hasConnected = false;
|
|
125
|
-
let connectionAttempts = 0;
|
|
126
|
-
const maxRetries = options.maxRetries || 3;
|
|
127
|
-
|
|
128
|
-
const {
|
|
129
|
-
promise: connectionPromise,
|
|
130
|
-
resolve: resolveConnection,
|
|
131
|
-
reject: rejectConnection,
|
|
132
|
-
} = Promise.withResolvers();
|
|
133
|
-
|
|
134
|
-
connectionTimeout = setTimeout(() => {
|
|
135
|
-
if (!hasConnected) {
|
|
136
|
-
client.quit().catch(() => {});
|
|
137
|
-
rejectConnection(new TejError(500, 'Redis connection timeout'));
|
|
138
|
-
}
|
|
139
|
-
}, options.connectTimeout || 10000);
|
|
140
|
-
|
|
141
|
-
client.on('error', (err) => {
|
|
142
|
-
logger.error(`Redis connection error: ${err}`, true);
|
|
143
|
-
if (!hasConnected && connectionAttempts >= maxRetries) {
|
|
144
|
-
clearTimeout(connectionTimeout);
|
|
145
|
-
client.quit().catch(() => {});
|
|
146
|
-
rejectConnection(
|
|
147
|
-
new TejError(
|
|
148
|
-
500,
|
|
149
|
-
`Redis connection failed after ${maxRetries} attempts: ${err.message}`,
|
|
150
|
-
),
|
|
151
|
-
);
|
|
152
|
-
}
|
|
153
|
-
connectionAttempts++;
|
|
154
|
-
});
|
|
155
|
-
|
|
156
|
-
client.on('connect', () => {
|
|
157
|
-
hasConnected = true;
|
|
158
|
-
clearTimeout(connectionTimeout);
|
|
159
|
-
logger.info(
|
|
160
|
-
`Redis connected on ${client?.options?.url ?? client?.options?.socket?.host}`,
|
|
161
|
-
);
|
|
162
|
-
});
|
|
163
|
-
|
|
164
|
-
client.on('ready', () => {
|
|
165
|
-
logger.info('Redis ready');
|
|
166
|
-
resolveConnection(client);
|
|
167
|
-
});
|
|
168
|
-
|
|
169
|
-
client.on('end', () => {
|
|
170
|
-
logger.info('Redis connection closed');
|
|
171
|
-
});
|
|
172
|
-
|
|
173
|
-
await client.connect();
|
|
174
|
-
await connectionPromise;
|
|
175
|
-
|
|
176
|
-
return client;
|
|
177
|
-
} catch (error) {
|
|
178
|
-
if (client) {
|
|
179
|
-
try {
|
|
180
|
-
await client.quit();
|
|
181
|
-
} catch (quitError) {
|
|
182
|
-
logger.error(
|
|
183
|
-
`Error while cleaning up Redis connection: ${quitError}`,
|
|
184
|
-
true,
|
|
185
|
-
);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
logger.error(`Failed to create Redis connection: ${error}`, true);
|
|
189
|
-
throw new TejError(
|
|
190
|
-
500,
|
|
191
|
-
`Failed to create Redis connection: ${error.message}`,
|
|
192
|
-
);
|
|
193
|
-
}
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/**
|
|
197
|
-
* Close a Redis connection
|
|
198
|
-
* @param {RedisClient|RedisCluster} client - Redis client to close
|
|
199
|
-
* @returns {Promise<void>}
|
|
200
|
-
*/
|
|
201
|
-
async function closeConnection(client) {
|
|
202
|
-
if (client) {
|
|
203
|
-
await client.quit();
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
|
|
207
|
-
export default {
|
|
208
|
-
createConnection,
|
|
209
|
-
closeConnection,
|
|
210
|
-
};
|