stigmergy 1.0.95 → 1.0.97

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.
@@ -0,0 +1,440 @@
1
+ /**
2
+ * Data Structures for the Stigmergy CLI
3
+ */
4
+
5
+ /**
6
+ * HashTable implementation with collision handling using chaining and open addressing
7
+ */
8
+ class HashTable {
9
+ /**
10
+ * Create a new HashTable
11
+ * @param {number} size - Initial size of the hash table
12
+ * @param {string} collisionStrategy - Collision handling strategy ('chaining' or 'openAddressing')
13
+ */
14
+ constructor(size = 53, collisionStrategy = "chaining") {
15
+ this.size = size;
16
+ this.collisionStrategy = collisionStrategy;
17
+
18
+ if (collisionStrategy === "chaining") {
19
+ this.buckets = new Array(size).fill(null).map(() => []);
20
+ } else if (collisionStrategy === "openAddressing") {
21
+ this.buckets = new Array(size).fill(null);
22
+ this.deleted = new Array(size).fill(false);
23
+ } else {
24
+ throw new Error(
25
+ 'Invalid collision strategy. Use "chaining" or "openAddressing"',
26
+ );
27
+ }
28
+
29
+ this.count = 0;
30
+ this.loadFactorThreshold = 0.75;
31
+ }
32
+
33
+ /**
34
+ * Hash function to convert a key to an index
35
+ * @param {string|number} key - Key to hash
36
+ * @returns {number} Index in the hash table
37
+ */
38
+ _hash(key) {
39
+ if (typeof key === "number") {
40
+ return key % this.size;
41
+ }
42
+
43
+ let hash = 0;
44
+ const PRIME = 31;
45
+
46
+ for (let i = 0; i < key.length; i++) {
47
+ hash = (hash * PRIME + key.charCodeAt(i)) % this.size;
48
+ }
49
+
50
+ return hash;
51
+ }
52
+
53
+ /**
54
+ * Secondary hash function for double hashing in open addressing
55
+ * @param {string|number} key - Key to hash
56
+ * @returns {number} Secondary hash value
57
+ */
58
+ _hash2(key) {
59
+ if (typeof key === "number") {
60
+ return 7 - (key % 7);
61
+ }
62
+
63
+ let hash = 0;
64
+ for (let i = 0; i < key.length; i++) {
65
+ hash = (hash * 3 + key.charCodeAt(i)) % this.size;
66
+ }
67
+
68
+ return 7 - (hash % 7);
69
+ }
70
+
71
+ /**
72
+ * Resize the hash table when load factor exceeds threshold
73
+ */
74
+ _resize() {
75
+ const oldBuckets = this.buckets;
76
+ const oldDeleted = this.deleted;
77
+ const oldSize = this.size;
78
+
79
+ // Double the size
80
+ this.size *= 2;
81
+ this.count = 0;
82
+
83
+ if (this.collisionStrategy === "chaining") {
84
+ this.buckets = new Array(this.size).fill(null).map(() => []);
85
+ } else {
86
+ this.buckets = new Array(this.size).fill(null);
87
+ this.deleted = new Array(this.size).fill(false);
88
+ }
89
+
90
+ // Rehash all existing elements
91
+ if (this.collisionStrategy === "chaining") {
92
+ for (let i = 0; i < oldSize; i++) {
93
+ const bucket = oldBuckets[i];
94
+ if (bucket) {
95
+ for (const [key, value] of bucket) {
96
+ this.set(key, value);
97
+ }
98
+ }
99
+ }
100
+ } else {
101
+ for (let i = 0; i < oldSize; i++) {
102
+ if (oldBuckets[i] !== null && !oldDeleted[i]) {
103
+ this.set(oldBuckets[i][0], oldBuckets[i][1]);
104
+ }
105
+ }
106
+ }
107
+ }
108
+
109
+ /**
110
+ * Set a key-value pair in the hash table
111
+ * @param {string|number} key - Key to store
112
+ * @param {*} value - Value to store
113
+ * @returns {HashTable} The hash table instance
114
+ */
115
+ set(key, value) {
116
+ // Check if resize is needed
117
+ if (this.count >= this.size * this.loadFactorThreshold) {
118
+ this._resize();
119
+ }
120
+
121
+ if (this.collisionStrategy === "chaining") {
122
+ return this._setChaining(key, value);
123
+ } else {
124
+ return this._setOpenAddressing(key, value);
125
+ }
126
+ }
127
+
128
+ /**
129
+ * Set a key-value pair using chaining
130
+ * @private
131
+ */
132
+ _setChaining(key, value) {
133
+ const index = this._hash(key);
134
+ const bucket = this.buckets[index];
135
+
136
+ // Check if key already exists
137
+ for (let i = 0; i < bucket.length; i++) {
138
+ if (bucket[i][0] === key) {
139
+ bucket[i][1] = value; // Update existing value
140
+ return this;
141
+ }
142
+ }
143
+
144
+ // Add new key-value pair
145
+ bucket.push([key, value]);
146
+ this.count++;
147
+ return this;
148
+ }
149
+
150
+ /**
151
+ * Set a key-value pair using open addressing with double hashing
152
+ * @private
153
+ */
154
+ _setOpenAddressing(key, value) {
155
+ let index = this._hash(key);
156
+ let originalIndex = index;
157
+ let i = 0;
158
+
159
+ // Probe until we find an empty slot or deleted slot
160
+ while (
161
+ this.buckets[index] !== null &&
162
+ !this.deleted[index] &&
163
+ this.buckets[index][0] !== key
164
+ ) {
165
+ i++;
166
+ index = (originalIndex + i * this._hash2(key)) % this.size;
167
+
168
+ // If we've checked all slots, the table is full
169
+ if (i >= this.size) {
170
+ // Instead of throwing an error, we'll resize and try again
171
+ this._resize();
172
+ return this.set(key, value);
173
+ }
174
+ }
175
+
176
+ // If key already exists, update the value
177
+ if (
178
+ this.buckets[index] !== null &&
179
+ !this.deleted[index] &&
180
+ this.buckets[index][0] === key
181
+ ) {
182
+ this.buckets[index][1] = value;
183
+ } else {
184
+ // Insert new key-value pair
185
+ this.buckets[index] = [key, value];
186
+ this.deleted[index] = false;
187
+ this.count++;
188
+ }
189
+
190
+ return this;
191
+ }
192
+
193
+ /**
194
+ * Get a value by its key
195
+ * @param {string|number} key - Key to look up
196
+ * @returns {*} The value associated with the key, or undefined if not found
197
+ */
198
+ get(key) {
199
+ if (this.collisionStrategy === "chaining") {
200
+ return this._getChaining(key);
201
+ } else {
202
+ return this._getOpenAddressing(key);
203
+ }
204
+ }
205
+
206
+ /**
207
+ * Get a value using chaining
208
+ * @private
209
+ */
210
+ _getChaining(key) {
211
+ const index = this._hash(key);
212
+ const bucket = this.buckets[index];
213
+
214
+ for (let i = 0; i < bucket.length; i++) {
215
+ if (bucket[i][0] === key) {
216
+ return bucket[i][1];
217
+ }
218
+ }
219
+
220
+ return undefined;
221
+ }
222
+
223
+ /**
224
+ * Get a value using open addressing
225
+ * @private
226
+ */
227
+ _getOpenAddressing(key) {
228
+ let index = this._hash(key);
229
+ let i = 0;
230
+
231
+ // Probe until we find the key or an empty slot
232
+ while (this.buckets[index] !== null) {
233
+ if (!this.deleted[index] && this.buckets[index][0] === key) {
234
+ return this.buckets[index][1];
235
+ }
236
+
237
+ i++;
238
+ index = (this._hash(key) + i * this._hash2(key)) % this.size;
239
+
240
+ // Prevent infinite loop
241
+ if (i >= this.size) {
242
+ break;
243
+ }
244
+ }
245
+
246
+ return undefined;
247
+ }
248
+
249
+ /**
250
+ * Delete a key-value pair from the hash table
251
+ * @param {string|number} key - Key to delete
252
+ * @returns {boolean} True if the key was found and deleted, false otherwise
253
+ */
254
+ delete(key) {
255
+ if (this.collisionStrategy === "chaining") {
256
+ return this._deleteChaining(key);
257
+ } else {
258
+ return this._deleteOpenAddressing(key);
259
+ }
260
+ }
261
+
262
+ /**
263
+ * Delete a key-value pair using chaining
264
+ * @private
265
+ */
266
+ _deleteChaining(key) {
267
+ const index = this._hash(key);
268
+ const bucket = this.buckets[index];
269
+
270
+ for (let i = 0; i < bucket.length; i++) {
271
+ if (bucket[i][0] === key) {
272
+ bucket.splice(i, 1);
273
+ this.count--;
274
+ return true;
275
+ }
276
+ }
277
+
278
+ return false;
279
+ }
280
+
281
+ /**
282
+ * Delete a key-value pair using open addressing
283
+ * @private
284
+ */
285
+ _deleteOpenAddressing(key) {
286
+ let index = this._hash(key);
287
+ let i = 0;
288
+
289
+ // Probe until we find the key or an empty slot
290
+ while (this.buckets[index] !== null) {
291
+ if (!this.deleted[index] && this.buckets[index][0] === key) {
292
+ this.deleted[index] = true;
293
+ this.count--;
294
+ return true;
295
+ }
296
+
297
+ i++;
298
+ index = (this._hash(key) + i * this._hash2(key)) % this.size;
299
+
300
+ // Prevent infinite loop
301
+ if (i >= this.size) {
302
+ break;
303
+ }
304
+ }
305
+
306
+ return false;
307
+ }
308
+
309
+ /**
310
+ * Check if a key exists in the hash table
311
+ * @param {string|number} key - Key to check
312
+ * @returns {boolean} True if the key exists, false otherwise
313
+ */
314
+ has(key) {
315
+ return this.get(key) !== undefined;
316
+ }
317
+
318
+ /**
319
+ * Get all keys in the hash table
320
+ * @returns {Array} Array of all keys
321
+ */
322
+ keys() {
323
+ const keysArr = [];
324
+
325
+ if (this.collisionStrategy === "chaining") {
326
+ for (let i = 0; i < this.size; i++) {
327
+ const bucket = this.buckets[i];
328
+ if (bucket) {
329
+ for (const [key, _] of bucket) {
330
+ keysArr.push(key);
331
+ }
332
+ }
333
+ }
334
+ } else {
335
+ for (let i = 0; i < this.size; i++) {
336
+ if (this.buckets[i] !== null && !this.deleted[i]) {
337
+ keysArr.push(this.buckets[i][0]);
338
+ }
339
+ }
340
+ }
341
+
342
+ return keysArr;
343
+ }
344
+
345
+ /**
346
+ * Get all values in the hash table
347
+ * @returns {Array} Array of all values
348
+ */
349
+ values() {
350
+ const valuesArr = [];
351
+
352
+ if (this.collisionStrategy === "chaining") {
353
+ for (let i = 0; i < this.size; i++) {
354
+ const bucket = this.buckets[i];
355
+ if (bucket) {
356
+ for (const [_, value] of bucket) {
357
+ valuesArr.push(value);
358
+ }
359
+ }
360
+ }
361
+ } else {
362
+ for (let i = 0; i < this.size; i++) {
363
+ if (this.buckets[i] !== null && !this.deleted[i]) {
364
+ valuesArr.push(this.buckets[i][1]);
365
+ }
366
+ }
367
+ }
368
+
369
+ return valuesArr;
370
+ }
371
+
372
+ /**
373
+ * Get all key-value pairs in the hash table
374
+ * @returns {Array} Array of all key-value pairs
375
+ */
376
+ entries() {
377
+ const entriesArr = [];
378
+
379
+ if (this.collisionStrategy === "chaining") {
380
+ for (let i = 0; i < this.size; i++) {
381
+ const bucket = this.buckets[i];
382
+ if (bucket) {
383
+ for (const [key, value] of bucket) {
384
+ entriesArr.push([key, value]);
385
+ }
386
+ }
387
+ }
388
+ } else {
389
+ for (let i = 0; i < this.size; i++) {
390
+ if (this.buckets[i] !== null && !this.deleted[i]) {
391
+ entriesArr.push([this.buckets[i][0], this.buckets[i][1]]);
392
+ }
393
+ }
394
+ }
395
+
396
+ return entriesArr;
397
+ }
398
+
399
+ /**
400
+ * Clear the hash table
401
+ */
402
+ clear() {
403
+ if (this.collisionStrategy === "chaining") {
404
+ this.buckets = new Array(this.size).fill(null).map(() => []);
405
+ } else {
406
+ this.buckets = new Array(this.size).fill(null);
407
+ this.deleted = new Array(this.size).fill(false);
408
+ }
409
+
410
+ this.count = 0;
411
+ }
412
+
413
+ /**
414
+ * Get the size of the hash table
415
+ * @returns {number} Number of key-value pairs in the hash table
416
+ */
417
+ getSize() {
418
+ return this.count;
419
+ }
420
+
421
+ /**
422
+ * Check if the hash table is empty
423
+ * @returns {boolean} True if the hash table is empty, false otherwise
424
+ */
425
+ isEmpty() {
426
+ return this.count === 0;
427
+ }
428
+
429
+ /**
430
+ * Get the load factor of the hash table
431
+ * @returns {number} Load factor (number of elements / table size)
432
+ */
433
+ getLoadFactor() {
434
+ return this.count / this.size;
435
+ }
436
+ }
437
+
438
+ module.exports = {
439
+ HashTable,
440
+ };
package/src/deploy.js CHANGED
@@ -5,46 +5,52 @@
5
5
  * This script deploys hooks and integrations for all available CLI tools
6
6
  */
7
7
 
8
- const fs = require('fs').promises;
9
- const path = require('path');
10
- const os = require('os');
8
+ const fs = require("fs").promises;
9
+ const path = require("path");
10
+ const os = require("os");
11
11
 
12
12
  // Import the main Stigmergy installer
13
- const { StigmergyInstaller } = require('./main_english.js');
13
+ const { StigmergyInstaller } = require("./main_english.js");
14
+
15
+ // Set up global error handlers using our error handler module
16
+ const { setupGlobalErrorHandlers } = require("./core/error_handler");
17
+ setupGlobalErrorHandlers();
14
18
 
15
19
  async function deploy() {
16
- console.log('Stigmergy Deployment Script');
17
- console.log('==========================');
18
-
19
- try {
20
- // Create installer instance
21
- const installer = new StigmergyInstaller();
22
-
23
- // Scan for available tools
24
- console.log('[SCAN] Scanning for available CLI tools...');
25
- const scanResult = await installer.scanCLI();
26
- const available = scanResult.available;
27
-
28
- // Deploy hooks for all available tools
29
- console.log('[DEPLOY] Deploying hooks for all available tools...');
30
- await installer.deployHooks(available);
31
-
32
- console.log('\n[SUCCESS] Deployment completed successfully!');
33
- return true;
34
- } catch (error) {
35
- console.error('[ERROR] Deployment failed:', error.message);
36
- return false;
37
- }
20
+ console.log("Stigmergy Deployment Script");
21
+ console.log("==========================");
22
+
23
+ try {
24
+ // Create installer instance
25
+ const installer = new StigmergyInstaller();
26
+
27
+ // Scan for available tools
28
+ console.log("[SCAN] Scanning for available CLI tools...");
29
+ const scanResult = await installer.scanCLI();
30
+ const available = scanResult.available;
31
+
32
+ // Deploy hooks for all available tools
33
+ console.log("[DEPLOY] Deploying hooks for all available tools...");
34
+ await installer.deployHooks(available);
35
+
36
+ console.log("\n[SUCCESS] Deployment completed successfully!");
37
+ return true;
38
+ } catch (error) {
39
+ console.error("[ERROR] Deployment failed:", error.message);
40
+ return false;
41
+ }
38
42
  }
39
43
 
40
44
  // Run deployment if called directly
41
45
  if (require.main === module) {
42
- deploy().then(success => {
43
- process.exit(success ? 0 : 1);
44
- }).catch(error => {
45
- console.error('[FATAL ERROR]:', error.message);
46
- process.exit(1);
46
+ deploy()
47
+ .then((success) => {
48
+ process.exit(success ? 0 : 1);
49
+ })
50
+ .catch((error) => {
51
+ console.error("[FATAL ERROR]:", error.message);
52
+ process.exit(1);
47
53
  });
48
54
  }
49
55
 
50
- module.exports = { deploy };
56
+ module.exports = { deploy };
package/src/index.js CHANGED
@@ -1,9 +1,9 @@
1
- #!/usr/bin/env node
2
-
3
- /**
4
- * Stigmergy CLI - Entry Point
5
- * International Version - Pure ANSI Characters Only
6
- */
7
-
8
- // Import and export the main functionality from main.js
9
- require('./main.js');
1
+ #!/usr/bin/env node
2
+
3
+ /**
4
+ * Stigmergy CLI - Entry Point
5
+ * International Version - Pure ANSI Characters Only
6
+ */
7
+
8
+ // Import and export the main functionality from main.js
9
+ require("./main.js");