@danielsimonjr/memoryjs 2.6.0 → 2.7.0

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/dist/index.cjs CHANGED
@@ -3416,6 +3416,144 @@ var init_WorkerPoolManager = __esm({
3416
3416
  }
3417
3417
  });
3418
3418
 
3419
+ // src/utils/WorkerTaskManager.ts
3420
+ function getWorkerTaskManager() {
3421
+ if (!DEFAULT_INSTANCE) DEFAULT_INSTANCE = new WorkerTaskManager();
3422
+ return DEFAULT_INSTANCE;
3423
+ }
3424
+ async function batchProcessViaWorkers(items, workerType, methodName, mapArgs, opts = {}) {
3425
+ if (items.length === 0) return [];
3426
+ const wtm = getWorkerTaskManager();
3427
+ const promises = items.map(
3428
+ (item, i) => wtm.submit(workerType, methodName, mapArgs(item, i), opts)
3429
+ );
3430
+ return Promise.all(promises);
3431
+ }
3432
+ var SUBMISSION_COUNTER, WorkerTaskManager, DEFAULT_INSTANCE;
3433
+ var init_WorkerTaskManager = __esm({
3434
+ "src/utils/WorkerTaskManager.ts"() {
3435
+ "use strict";
3436
+ init_cjs_shims();
3437
+ init_taskScheduler();
3438
+ init_WorkerPoolManager();
3439
+ init_logger();
3440
+ SUBMISSION_COUNTER = 0;
3441
+ WorkerTaskManager = class {
3442
+ queue;
3443
+ poolManager;
3444
+ touchedPoolIds = /* @__PURE__ */ new Set();
3445
+ cancelledTaskIds = /* @__PURE__ */ new Set();
3446
+ handleStatus = /* @__PURE__ */ new Map();
3447
+ constructor(opts = {}) {
3448
+ this.queue = new TaskQueue({
3449
+ concurrency: opts.concurrency,
3450
+ timeout: opts.defaultTimeout,
3451
+ // We dispatch to WorkerPoolManager ourselves — the queue's
3452
+ // internal-worker-pool path would double-up.
3453
+ useWorkerPool: false
3454
+ });
3455
+ this.poolManager = getWorkerPoolManager();
3456
+ }
3457
+ /**
3458
+ * Submit a task and await its result. Throws on failure / cancellation.
3459
+ *
3460
+ * @param workerType — routing key. Maps to a named `WorkerPoolManager` pool.
3461
+ * @param methodName — function name exposed by the worker module.
3462
+ * @param args — positional arguments passed to the worker function.
3463
+ */
3464
+ async submit(workerType, methodName, args, opts = {}) {
3465
+ const handle = this.submitWithHandle(workerType, methodName, args, opts);
3466
+ return handle.result;
3467
+ }
3468
+ /**
3469
+ * Submit a task and return a handle for cancellation + status polling.
3470
+ */
3471
+ submitWithHandle(workerType, methodName, args, opts = {}) {
3472
+ const id = `wtm-${Date.now()}-${++SUBMISSION_COUNTER}`;
3473
+ this.handleStatus.set(id, "pending" /* PENDING */);
3474
+ const task = {
3475
+ id,
3476
+ priority: opts.priority ?? 1 /* NORMAL */,
3477
+ timeout: opts.timeout,
3478
+ input: { workerType, methodName, args },
3479
+ fn: async (input) => {
3480
+ if (this.cancelledTaskIds.has(id)) {
3481
+ throw new Error(`Task ${id} cancelled before dispatch`);
3482
+ }
3483
+ this.handleStatus.set(id, "running" /* RUNNING */);
3484
+ const pool = this.poolManager.getPool(input.workerType, opts.poolConfig);
3485
+ this.touchedPoolIds.add(input.workerType);
3486
+ return await pool.exec(input.methodName, input.args);
3487
+ }
3488
+ };
3489
+ const result = this.queue.enqueue(task).then(async (res) => {
3490
+ this.handleStatus.set(id, res.status);
3491
+ if (res.status === "completed" /* COMPLETED */) {
3492
+ return res.result;
3493
+ }
3494
+ if (res.status === "cancelled" /* CANCELLED */ || this.cancelledTaskIds.has(id)) {
3495
+ throw new Error(`Task ${id} cancelled`);
3496
+ }
3497
+ const err2 = res.error ?? new Error(`Task ${id} failed: ${res.status}`);
3498
+ throw err2;
3499
+ });
3500
+ return {
3501
+ id,
3502
+ result,
3503
+ cancel: () => {
3504
+ this.cancelledTaskIds.add(id);
3505
+ const evicted = this.queue.cancel(id);
3506
+ if (evicted) {
3507
+ this.handleStatus.set(id, "cancelled" /* CANCELLED */);
3508
+ }
3509
+ return evicted;
3510
+ },
3511
+ status: () => this.handleStatus.get(id) ?? "pending" /* PENDING */
3512
+ };
3513
+ }
3514
+ /**
3515
+ * Aggregated stats across the queue + touched pools.
3516
+ */
3517
+ getStats() {
3518
+ const pools = [];
3519
+ for (const poolId of this.touchedPoolIds) {
3520
+ const stats = this.poolManager.getPoolStats(poolId);
3521
+ if (stats) {
3522
+ pools.push({
3523
+ poolId,
3524
+ workers: stats.totalWorkers,
3525
+ activeTasks: stats.activeTasks,
3526
+ pendingTasks: stats.pendingTasks,
3527
+ totalTasksExecuted: stats.totalTasksExecuted
3528
+ });
3529
+ }
3530
+ }
3531
+ return { queue: this.queue.getStats(), pools };
3532
+ }
3533
+ /**
3534
+ * Drain the queue (wait for all pending + running tasks) and return their
3535
+ * results in completion order. Pool workers are NOT terminated — call
3536
+ * `shutdown()` for that.
3537
+ */
3538
+ async drain() {
3539
+ return this.queue.drain();
3540
+ }
3541
+ /**
3542
+ * Shut down the queue + the underlying WorkerPoolManager pools touched by
3543
+ * this instance. Idempotent. Returns after every owned worker has exited.
3544
+ */
3545
+ async shutdown() {
3546
+ try {
3547
+ await this.queue.shutdown();
3548
+ } catch (e) {
3549
+ logger.warn("[WorkerTaskManager.shutdown] queue.shutdown failed:", e);
3550
+ }
3551
+ }
3552
+ };
3553
+ DEFAULT_INSTANCE = null;
3554
+ }
3555
+ });
3556
+
3419
3557
  // src/utils/BatchProcessor.ts
3420
3558
  async function processBatch(items, processor, options = {}) {
3421
3559
  const batchProcessor = new BatchProcessor(options);
@@ -11270,6 +11408,7 @@ var init_utils = __esm({
11270
11408
  init_taskScheduler();
11271
11409
  init_operationUtils();
11272
11410
  init_WorkerPoolManager();
11411
+ init_WorkerTaskManager();
11273
11412
  init_BatchProcessor();
11274
11413
  init_MemoryMonitor();
11275
11414
  init_relationHelpers();
@@ -13775,6 +13914,7 @@ __export(index_exports, {
13775
13914
  VisibilityResolver: () => VisibilityResolver,
13776
13915
  WorkThreadManager: () => WorkThreadManager,
13777
13916
  WorkerPoolManager: () => WorkerPoolManager,
13917
+ WorkerTaskManager: () => WorkerTaskManager,
13778
13918
  WorkingMemoryManager: () => WorkingMemoryManager,
13779
13919
  WorldModelManager: () => WorldModelManager,
13780
13920
  WorldStateSnapshot: () => WorldStateSnapshot,
@@ -13784,6 +13924,7 @@ __export(index_exports, {
13784
13924
  applyPagination: () => applyPagination,
13785
13925
  asWarning: () => asWarning,
13786
13926
  batchProcess: () => batchProcess,
13927
+ batchProcessViaWorkers: () => batchProcessViaWorkers,
13787
13928
  buildTFVector: () => buildTFVector,
13788
13929
  calculateIDF: () => calculateIDF,
13789
13930
  calculateIDFFromTokenSets: () => calculateIDFFromTokenSets,
@@ -13854,6 +13995,7 @@ __export(index_exports, {
13854
13995
  getQuickHint: () => getQuickHint,
13855
13996
  getRoleProfile: () => getRoleProfile,
13856
13997
  getWorkerPoolManager: () => getWorkerPoolManager,
13998
+ getWorkerTaskManager: () => getWorkerTaskManager,
13857
13999
  globalMemoryMonitor: () => globalMemoryMonitor,
13858
14000
  groupEntitiesByType: () => groupEntitiesByType,
13859
14001
  hasAllTags: () => hasAllTags,
@@ -52171,6 +52313,7 @@ function extractRoleAndContent(entity) {
52171
52313
  VisibilityResolver,
52172
52314
  WorkThreadManager,
52173
52315
  WorkerPoolManager,
52316
+ WorkerTaskManager,
52174
52317
  WorkingMemoryManager,
52175
52318
  WorldModelManager,
52176
52319
  WorldStateSnapshot,
@@ -52180,6 +52323,7 @@ function extractRoleAndContent(entity) {
52180
52323
  applyPagination,
52181
52324
  asWarning,
52182
52325
  batchProcess,
52326
+ batchProcessViaWorkers,
52183
52327
  buildTFVector,
52184
52328
  calculateIDF,
52185
52329
  calculateIDFFromTokenSets,
@@ -52250,6 +52394,7 @@ function extractRoleAndContent(entity) {
52250
52394
  getQuickHint,
52251
52395
  getRoleProfile,
52252
52396
  getWorkerPoolManager,
52397
+ getWorkerTaskManager,
52253
52398
  globalMemoryMonitor,
52254
52399
  groupEntitiesByType,
52255
52400
  hasAllTags,