@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.js CHANGED
@@ -3413,6 +3413,144 @@ var init_WorkerPoolManager = __esm({
3413
3413
  }
3414
3414
  });
3415
3415
 
3416
+ // src/utils/WorkerTaskManager.ts
3417
+ function getWorkerTaskManager() {
3418
+ if (!DEFAULT_INSTANCE) DEFAULT_INSTANCE = new WorkerTaskManager();
3419
+ return DEFAULT_INSTANCE;
3420
+ }
3421
+ async function batchProcessViaWorkers(items, workerType, methodName, mapArgs, opts = {}) {
3422
+ if (items.length === 0) return [];
3423
+ const wtm = getWorkerTaskManager();
3424
+ const promises = items.map(
3425
+ (item, i) => wtm.submit(workerType, methodName, mapArgs(item, i), opts)
3426
+ );
3427
+ return Promise.all(promises);
3428
+ }
3429
+ var SUBMISSION_COUNTER, WorkerTaskManager, DEFAULT_INSTANCE;
3430
+ var init_WorkerTaskManager = __esm({
3431
+ "src/utils/WorkerTaskManager.ts"() {
3432
+ "use strict";
3433
+ init_esm_shims();
3434
+ init_taskScheduler();
3435
+ init_WorkerPoolManager();
3436
+ init_logger();
3437
+ SUBMISSION_COUNTER = 0;
3438
+ WorkerTaskManager = class {
3439
+ queue;
3440
+ poolManager;
3441
+ touchedPoolIds = /* @__PURE__ */ new Set();
3442
+ cancelledTaskIds = /* @__PURE__ */ new Set();
3443
+ handleStatus = /* @__PURE__ */ new Map();
3444
+ constructor(opts = {}) {
3445
+ this.queue = new TaskQueue({
3446
+ concurrency: opts.concurrency,
3447
+ timeout: opts.defaultTimeout,
3448
+ // We dispatch to WorkerPoolManager ourselves — the queue's
3449
+ // internal-worker-pool path would double-up.
3450
+ useWorkerPool: false
3451
+ });
3452
+ this.poolManager = getWorkerPoolManager();
3453
+ }
3454
+ /**
3455
+ * Submit a task and await its result. Throws on failure / cancellation.
3456
+ *
3457
+ * @param workerType — routing key. Maps to a named `WorkerPoolManager` pool.
3458
+ * @param methodName — function name exposed by the worker module.
3459
+ * @param args — positional arguments passed to the worker function.
3460
+ */
3461
+ async submit(workerType, methodName, args, opts = {}) {
3462
+ const handle = this.submitWithHandle(workerType, methodName, args, opts);
3463
+ return handle.result;
3464
+ }
3465
+ /**
3466
+ * Submit a task and return a handle for cancellation + status polling.
3467
+ */
3468
+ submitWithHandle(workerType, methodName, args, opts = {}) {
3469
+ const id = `wtm-${Date.now()}-${++SUBMISSION_COUNTER}`;
3470
+ this.handleStatus.set(id, "pending" /* PENDING */);
3471
+ const task = {
3472
+ id,
3473
+ priority: opts.priority ?? 1 /* NORMAL */,
3474
+ timeout: opts.timeout,
3475
+ input: { workerType, methodName, args },
3476
+ fn: async (input) => {
3477
+ if (this.cancelledTaskIds.has(id)) {
3478
+ throw new Error(`Task ${id} cancelled before dispatch`);
3479
+ }
3480
+ this.handleStatus.set(id, "running" /* RUNNING */);
3481
+ const pool = this.poolManager.getPool(input.workerType, opts.poolConfig);
3482
+ this.touchedPoolIds.add(input.workerType);
3483
+ return await pool.exec(input.methodName, input.args);
3484
+ }
3485
+ };
3486
+ const result = this.queue.enqueue(task).then(async (res) => {
3487
+ this.handleStatus.set(id, res.status);
3488
+ if (res.status === "completed" /* COMPLETED */) {
3489
+ return res.result;
3490
+ }
3491
+ if (res.status === "cancelled" /* CANCELLED */ || this.cancelledTaskIds.has(id)) {
3492
+ throw new Error(`Task ${id} cancelled`);
3493
+ }
3494
+ const err2 = res.error ?? new Error(`Task ${id} failed: ${res.status}`);
3495
+ throw err2;
3496
+ });
3497
+ return {
3498
+ id,
3499
+ result,
3500
+ cancel: () => {
3501
+ this.cancelledTaskIds.add(id);
3502
+ const evicted = this.queue.cancel(id);
3503
+ if (evicted) {
3504
+ this.handleStatus.set(id, "cancelled" /* CANCELLED */);
3505
+ }
3506
+ return evicted;
3507
+ },
3508
+ status: () => this.handleStatus.get(id) ?? "pending" /* PENDING */
3509
+ };
3510
+ }
3511
+ /**
3512
+ * Aggregated stats across the queue + touched pools.
3513
+ */
3514
+ getStats() {
3515
+ const pools = [];
3516
+ for (const poolId of this.touchedPoolIds) {
3517
+ const stats = this.poolManager.getPoolStats(poolId);
3518
+ if (stats) {
3519
+ pools.push({
3520
+ poolId,
3521
+ workers: stats.totalWorkers,
3522
+ activeTasks: stats.activeTasks,
3523
+ pendingTasks: stats.pendingTasks,
3524
+ totalTasksExecuted: stats.totalTasksExecuted
3525
+ });
3526
+ }
3527
+ }
3528
+ return { queue: this.queue.getStats(), pools };
3529
+ }
3530
+ /**
3531
+ * Drain the queue (wait for all pending + running tasks) and return their
3532
+ * results in completion order. Pool workers are NOT terminated — call
3533
+ * `shutdown()` for that.
3534
+ */
3535
+ async drain() {
3536
+ return this.queue.drain();
3537
+ }
3538
+ /**
3539
+ * Shut down the queue + the underlying WorkerPoolManager pools touched by
3540
+ * this instance. Idempotent. Returns after every owned worker has exited.
3541
+ */
3542
+ async shutdown() {
3543
+ try {
3544
+ await this.queue.shutdown();
3545
+ } catch (e) {
3546
+ logger.warn("[WorkerTaskManager.shutdown] queue.shutdown failed:", e);
3547
+ }
3548
+ }
3549
+ };
3550
+ DEFAULT_INSTANCE = null;
3551
+ }
3552
+ });
3553
+
3416
3554
  // src/utils/BatchProcessor.ts
3417
3555
  async function processBatch(items, processor, options = {}) {
3418
3556
  const batchProcessor = new BatchProcessor(options);
@@ -11267,6 +11405,7 @@ var init_utils = __esm({
11267
11405
  init_taskScheduler();
11268
11406
  init_operationUtils();
11269
11407
  init_WorkerPoolManager();
11408
+ init_WorkerTaskManager();
11270
11409
  init_BatchProcessor();
11271
11410
  init_MemoryMonitor();
11272
11411
  init_relationHelpers();
@@ -51761,6 +51900,7 @@ export {
51761
51900
  VisibilityResolver,
51762
51901
  WorkThreadManager,
51763
51902
  WorkerPoolManager,
51903
+ WorkerTaskManager,
51764
51904
  WorkingMemoryManager,
51765
51905
  WorldModelManager,
51766
51906
  WorldStateSnapshot,
@@ -51770,6 +51910,7 @@ export {
51770
51910
  applyPagination,
51771
51911
  asWarning,
51772
51912
  batchProcess,
51913
+ batchProcessViaWorkers,
51773
51914
  buildTFVector,
51774
51915
  calculateIDF,
51775
51916
  calculateIDFFromTokenSets,
@@ -51840,6 +51981,7 @@ export {
51840
51981
  getQuickHint,
51841
51982
  getRoleProfile,
51842
51983
  getWorkerPoolManager,
51984
+ getWorkerTaskManager,
51843
51985
  globalMemoryMonitor,
51844
51986
  groupEntitiesByType,
51845
51987
  hasAllTags,