koishi-plugin-chatluna 1.3.6 → 1.3.7

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/lib/index.cjs CHANGED
@@ -3269,27 +3269,16 @@ function apply25(ctx, config, chain) {
3269
3269
  const newBatch = {
3270
3270
  messages: [inputMessage],
3271
3271
  userName,
3272
- resolveWaiters: []
3272
+ resolveWaiters: [],
3273
+ collectWaiters: [],
3274
+ state: config.messageQueueDelay > 0 ? "collecting" : "processing"
3273
3275
  };
3274
3276
  batches.set(conversationId, newBatch);
3275
3277
  if (config.messageQueueDelay > 0) {
3276
- return await new Promise(
3277
- (resolve) => {
3278
- newBatch.processorResolve = resolve;
3279
- newBatch.timeout = ctx.setTimeout(() => {
3280
- if (batches.get(conversationId) === newBatch) {
3281
- logger6.debug(
3282
- // eslint-disable-next-line max-len
3283
- `Delay timeout (${config.messageQueueDelay}s) for ${conversationId}, processing batch with ${newBatch.messages.length} messages`
3284
- );
3285
- context.options.inputMessage = mergeMessages(newBatch.messages);
3286
- batches.delete(conversationId);
3287
- resolve(2 /* CONTINUE */);
3288
- }
3289
- }, config.messageQueueDelay * 1e3);
3290
- }
3291
- );
3278
+ resetBatchTimeout(ctx, config, newBatch, conversationId);
3279
+ return await awaitCollectingBatch(newBatch, context);
3292
3280
  }
3281
+ newBatch.messages = [];
3293
3282
  return 2 /* CONTINUE */;
3294
3283
  }
3295
3284
  if (batch.userName !== userName) {
@@ -3304,34 +3293,27 @@ function apply25(ctx, config, chain) {
3304
3293
  context
3305
3294
  );
3306
3295
  }
3307
- if (batch.resolveWaiters.length === 0) {
3296
+ if (batch.state === "collecting") {
3308
3297
  logger6.debug(
3309
3298
  `Adding message to batch for ${conversationId}, messageId: ${messageId}, total: ${batch.messages.length + 1}`
3310
3299
  );
3311
3300
  batch.messages.push(inputMessage);
3312
- if (config.messageQueueDelay > 0 && batch.timeout && batch.processorResolve) {
3313
- batch.timeout();
3314
- batch.timeout = ctx.setTimeout(() => {
3315
- if (batches.get(conversationId) === batch && batch.processorResolve) {
3316
- logger6.debug(
3317
- `Delay timeout (${config.messageQueueDelay}s) for ${conversationId}, processing batch with ${batch.messages.length} messages`
3318
- );
3319
- context.options.inputMessage = mergeMessages(
3320
- batch.messages
3321
- );
3322
- batches.delete(conversationId);
3323
- batch.processorResolve(
3324
- 2 /* CONTINUE */
3325
- );
3326
- }
3327
- }, config.messageQueueDelay * 1e3);
3301
+ if (config.messageQueueDelay > 0) {
3302
+ resetBatchTimeout(ctx, config, batch, conversationId);
3328
3303
  }
3329
- return 1 /* STOP */;
3304
+ return await awaitCollectingBatch(batch, context);
3305
+ }
3306
+ logger6.debug(
3307
+ `Waiting for batch completion for ${conversationId}, messageId: ${messageId}`
3308
+ );
3309
+ const status = await awaitBatchCompletion(batch, inputMessage);
3310
+ if (status === 1 /* STOP */) {
3311
+ return status;
3330
3312
  }
3331
3313
  logger6.debug(
3332
3314
  `Interrupting and merging for ${conversationId}, messageId: ${messageId}`
3333
3315
  );
3334
- return await interruptAndMerge(batch, inputMessage, context);
3316
+ return interruptAndMerge(batch, context);
3335
3317
  }).after("check_room").after("read_chat_message").before("lifecycle-handle_command");
3336
3318
  const completeBatch = /* @__PURE__ */ __name(async (conversationId) => {
3337
3319
  const batch = batches.get(conversationId);
@@ -3342,16 +3324,16 @@ function apply25(ctx, config, chain) {
3342
3324
  if (batch.timeout) {
3343
3325
  batch.timeout();
3344
3326
  }
3345
- batch.resolveWaiters.forEach((resolve) => resolve());
3346
- batches.delete(conversationId);
3327
+ const waiters = batch.resolveWaiters;
3328
+ batch.resolveWaiters = [];
3329
+ waiters.forEach(
3330
+ (resolve) => resolve(2 /* CONTINUE */)
3331
+ );
3347
3332
  } else if (batch) {
3348
3333
  logger6.debug(`Cleaning up batch for ${conversationId}`);
3349
3334
  if (batch.timeout) {
3350
3335
  batch.timeout();
3351
3336
  }
3352
- if (batch.processorResolve) {
3353
- batch.processorResolve(1 /* STOP */);
3354
- }
3355
3337
  batches.delete(conversationId);
3356
3338
  }
3357
3339
  }, "completeBatch");
@@ -3375,52 +3357,102 @@ function apply25(ctx, config, chain) {
3375
3357
  batch.resolveWaiters.forEach(
3376
3358
  (resolve) => resolve(1 /* STOP */)
3377
3359
  );
3378
- if (batch.processorResolve) {
3379
- batch.processorResolve(1 /* STOP */);
3380
- }
3360
+ batch.collectWaiters.forEach(
3361
+ (resolve) => resolve(1 /* STOP */)
3362
+ );
3381
3363
  batches.delete(conversationId);
3382
3364
  }
3383
3365
  });
3384
3366
  }
3385
3367
  __name(apply25, "apply");
3386
- async function interruptAndMerge(batch, message, context) {
3387
- const oldWaiters = batch.resolveWaiters;
3388
- const oldProcessor = batch.processorResolve;
3389
- batch.resolveWaiters = [];
3390
- batch.processorResolve = void 0;
3391
- batch.messages.push(message);
3368
+ function interruptAndMerge(batch, context) {
3369
+ if (batch.messages.length === 0) {
3370
+ return 1 /* STOP */;
3371
+ }
3372
+ context.options.inputMessage = mergeMessages(batch.messages);
3373
+ batch.messages = [];
3374
+ batch.state = "processing";
3375
+ return 2 /* CONTINUE */;
3376
+ }
3377
+ __name(interruptAndMerge, "interruptAndMerge");
3378
+ async function awaitCollectingBatch(batch, context) {
3379
+ resolveCollectWaiters(batch, 1 /* STOP */);
3380
+ return await new Promise((resolve) => {
3381
+ batch.collectWaiters.push((status) => {
3382
+ if (status === 1 /* STOP */) {
3383
+ resolve(1 /* STOP */);
3384
+ return;
3385
+ }
3386
+ context.options.inputMessage = mergeMessages(batch.messages);
3387
+ batch.messages = [];
3388
+ batch.state = "processing";
3389
+ resolve(2 /* CONTINUE */);
3390
+ });
3391
+ });
3392
+ }
3393
+ __name(awaitCollectingBatch, "awaitCollectingBatch");
3394
+ function resolveCollectWaiters(batch, status) {
3395
+ const waiters = batch.collectWaiters;
3396
+ batch.collectWaiters = [];
3397
+ waiters.forEach((resolve) => resolve(status));
3398
+ }
3399
+ __name(resolveCollectWaiters, "resolveCollectWaiters");
3400
+ function resetBatchTimeout(ctx, config, batch, conversationId) {
3392
3401
  if (batch.timeout) {
3393
3402
  batch.timeout();
3394
- batch.timeout = void 0;
3395
3403
  }
3396
- oldWaiters.forEach((resolve) => resolve(1 /* STOP */));
3397
- if (oldProcessor) {
3398
- oldProcessor(1 /* STOP */);
3404
+ batch.timeout = ctx.setTimeout(() => {
3405
+ if (batches.get(conversationId) === batch) {
3406
+ logger6.debug(
3407
+ // eslint-disable-next-line max-len
3408
+ `Delay timeout (${config.messageQueueDelay}s) for ${conversationId}, processing batch with ${batch.messages.length} messages`
3409
+ );
3410
+ batch.timeout = void 0;
3411
+ resolveCollectWaiters(batch, 2 /* CONTINUE */);
3412
+ }
3413
+ }, config.messageQueueDelay * 1e3);
3414
+ }
3415
+ __name(resetBatchTimeout, "resetBatchTimeout");
3416
+ async function awaitBatchCompletion(batch, message) {
3417
+ if (batch.resolveWaiters.length === 0) {
3418
+ batch.messages = [message];
3419
+ } else {
3420
+ batch.messages.push(message);
3399
3421
  }
3400
- return new Promise((resolve) => {
3401
- batch.resolveWaiters.push(() => {
3402
- context.options.inputMessage = mergeMessages(batch.messages);
3403
- resolve(2 /* CONTINUE */);
3422
+ return await new Promise((resolve) => {
3423
+ batch.resolveWaiters.push((status) => {
3424
+ resolve(status ?? 2 /* CONTINUE */);
3404
3425
  });
3405
3426
  });
3406
3427
  }
3407
- __name(interruptAndMerge, "interruptAndMerge");
3428
+ __name(awaitBatchCompletion, "awaitBatchCompletion");
3408
3429
  async function waitForBatchCompletion(batch, conversationId, message, userName, context) {
3409
3430
  return new Promise((resolve) => {
3410
- batch.resolveWaiters.push(() => {
3431
+ batch.resolveWaiters.push((status) => {
3432
+ if (status === 1 /* STOP */) {
3433
+ resolve(1 /* STOP */);
3434
+ return;
3435
+ }
3411
3436
  batches.set(conversationId, {
3412
3437
  messages: [message],
3413
3438
  userName,
3414
3439
  resolveWaiters: [
3415
- () => {
3440
+ (nextStatus) => {
3441
+ if (nextStatus === 1 /* STOP */) {
3442
+ resolve(1 /* STOP */);
3443
+ return;
3444
+ }
3416
3445
  const newBatch = batches.get(conversationId);
3417
3446
  context.options.inputMessage = mergeMessages(
3418
3447
  newBatch.messages
3419
3448
  );
3449
+ newBatch.messages = [];
3420
3450
  batches.delete(conversationId);
3421
3451
  resolve(2 /* CONTINUE */);
3422
3452
  }
3423
- ]
3453
+ ],
3454
+ collectWaiters: [],
3455
+ state: "processing"
3424
3456
  });
3425
3457
  });
3426
3458
  });
package/lib/index.mjs CHANGED
@@ -3264,27 +3264,16 @@ function apply25(ctx, config, chain) {
3264
3264
  const newBatch = {
3265
3265
  messages: [inputMessage],
3266
3266
  userName,
3267
- resolveWaiters: []
3267
+ resolveWaiters: [],
3268
+ collectWaiters: [],
3269
+ state: config.messageQueueDelay > 0 ? "collecting" : "processing"
3268
3270
  };
3269
3271
  batches.set(conversationId, newBatch);
3270
3272
  if (config.messageQueueDelay > 0) {
3271
- return await new Promise(
3272
- (resolve) => {
3273
- newBatch.processorResolve = resolve;
3274
- newBatch.timeout = ctx.setTimeout(() => {
3275
- if (batches.get(conversationId) === newBatch) {
3276
- logger6.debug(
3277
- // eslint-disable-next-line max-len
3278
- `Delay timeout (${config.messageQueueDelay}s) for ${conversationId}, processing batch with ${newBatch.messages.length} messages`
3279
- );
3280
- context.options.inputMessage = mergeMessages(newBatch.messages);
3281
- batches.delete(conversationId);
3282
- resolve(2 /* CONTINUE */);
3283
- }
3284
- }, config.messageQueueDelay * 1e3);
3285
- }
3286
- );
3273
+ resetBatchTimeout(ctx, config, newBatch, conversationId);
3274
+ return await awaitCollectingBatch(newBatch, context);
3287
3275
  }
3276
+ newBatch.messages = [];
3288
3277
  return 2 /* CONTINUE */;
3289
3278
  }
3290
3279
  if (batch.userName !== userName) {
@@ -3299,34 +3288,27 @@ function apply25(ctx, config, chain) {
3299
3288
  context
3300
3289
  );
3301
3290
  }
3302
- if (batch.resolveWaiters.length === 0) {
3291
+ if (batch.state === "collecting") {
3303
3292
  logger6.debug(
3304
3293
  `Adding message to batch for ${conversationId}, messageId: ${messageId}, total: ${batch.messages.length + 1}`
3305
3294
  );
3306
3295
  batch.messages.push(inputMessage);
3307
- if (config.messageQueueDelay > 0 && batch.timeout && batch.processorResolve) {
3308
- batch.timeout();
3309
- batch.timeout = ctx.setTimeout(() => {
3310
- if (batches.get(conversationId) === batch && batch.processorResolve) {
3311
- logger6.debug(
3312
- `Delay timeout (${config.messageQueueDelay}s) for ${conversationId}, processing batch with ${batch.messages.length} messages`
3313
- );
3314
- context.options.inputMessage = mergeMessages(
3315
- batch.messages
3316
- );
3317
- batches.delete(conversationId);
3318
- batch.processorResolve(
3319
- 2 /* CONTINUE */
3320
- );
3321
- }
3322
- }, config.messageQueueDelay * 1e3);
3296
+ if (config.messageQueueDelay > 0) {
3297
+ resetBatchTimeout(ctx, config, batch, conversationId);
3323
3298
  }
3324
- return 1 /* STOP */;
3299
+ return await awaitCollectingBatch(batch, context);
3300
+ }
3301
+ logger6.debug(
3302
+ `Waiting for batch completion for ${conversationId}, messageId: ${messageId}`
3303
+ );
3304
+ const status = await awaitBatchCompletion(batch, inputMessage);
3305
+ if (status === 1 /* STOP */) {
3306
+ return status;
3325
3307
  }
3326
3308
  logger6.debug(
3327
3309
  `Interrupting and merging for ${conversationId}, messageId: ${messageId}`
3328
3310
  );
3329
- return await interruptAndMerge(batch, inputMessage, context);
3311
+ return interruptAndMerge(batch, context);
3330
3312
  }).after("check_room").after("read_chat_message").before("lifecycle-handle_command");
3331
3313
  const completeBatch = /* @__PURE__ */ __name(async (conversationId) => {
3332
3314
  const batch = batches.get(conversationId);
@@ -3337,16 +3319,16 @@ function apply25(ctx, config, chain) {
3337
3319
  if (batch.timeout) {
3338
3320
  batch.timeout();
3339
3321
  }
3340
- batch.resolveWaiters.forEach((resolve) => resolve());
3341
- batches.delete(conversationId);
3322
+ const waiters = batch.resolveWaiters;
3323
+ batch.resolveWaiters = [];
3324
+ waiters.forEach(
3325
+ (resolve) => resolve(2 /* CONTINUE */)
3326
+ );
3342
3327
  } else if (batch) {
3343
3328
  logger6.debug(`Cleaning up batch for ${conversationId}`);
3344
3329
  if (batch.timeout) {
3345
3330
  batch.timeout();
3346
3331
  }
3347
- if (batch.processorResolve) {
3348
- batch.processorResolve(1 /* STOP */);
3349
- }
3350
3332
  batches.delete(conversationId);
3351
3333
  }
3352
3334
  }, "completeBatch");
@@ -3370,52 +3352,102 @@ function apply25(ctx, config, chain) {
3370
3352
  batch.resolveWaiters.forEach(
3371
3353
  (resolve) => resolve(1 /* STOP */)
3372
3354
  );
3373
- if (batch.processorResolve) {
3374
- batch.processorResolve(1 /* STOP */);
3375
- }
3355
+ batch.collectWaiters.forEach(
3356
+ (resolve) => resolve(1 /* STOP */)
3357
+ );
3376
3358
  batches.delete(conversationId);
3377
3359
  }
3378
3360
  });
3379
3361
  }
3380
3362
  __name(apply25, "apply");
3381
- async function interruptAndMerge(batch, message, context) {
3382
- const oldWaiters = batch.resolveWaiters;
3383
- const oldProcessor = batch.processorResolve;
3384
- batch.resolveWaiters = [];
3385
- batch.processorResolve = void 0;
3386
- batch.messages.push(message);
3363
+ function interruptAndMerge(batch, context) {
3364
+ if (batch.messages.length === 0) {
3365
+ return 1 /* STOP */;
3366
+ }
3367
+ context.options.inputMessage = mergeMessages(batch.messages);
3368
+ batch.messages = [];
3369
+ batch.state = "processing";
3370
+ return 2 /* CONTINUE */;
3371
+ }
3372
+ __name(interruptAndMerge, "interruptAndMerge");
3373
+ async function awaitCollectingBatch(batch, context) {
3374
+ resolveCollectWaiters(batch, 1 /* STOP */);
3375
+ return await new Promise((resolve) => {
3376
+ batch.collectWaiters.push((status) => {
3377
+ if (status === 1 /* STOP */) {
3378
+ resolve(1 /* STOP */);
3379
+ return;
3380
+ }
3381
+ context.options.inputMessage = mergeMessages(batch.messages);
3382
+ batch.messages = [];
3383
+ batch.state = "processing";
3384
+ resolve(2 /* CONTINUE */);
3385
+ });
3386
+ });
3387
+ }
3388
+ __name(awaitCollectingBatch, "awaitCollectingBatch");
3389
+ function resolveCollectWaiters(batch, status) {
3390
+ const waiters = batch.collectWaiters;
3391
+ batch.collectWaiters = [];
3392
+ waiters.forEach((resolve) => resolve(status));
3393
+ }
3394
+ __name(resolveCollectWaiters, "resolveCollectWaiters");
3395
+ function resetBatchTimeout(ctx, config, batch, conversationId) {
3387
3396
  if (batch.timeout) {
3388
3397
  batch.timeout();
3389
- batch.timeout = void 0;
3390
3398
  }
3391
- oldWaiters.forEach((resolve) => resolve(1 /* STOP */));
3392
- if (oldProcessor) {
3393
- oldProcessor(1 /* STOP */);
3399
+ batch.timeout = ctx.setTimeout(() => {
3400
+ if (batches.get(conversationId) === batch) {
3401
+ logger6.debug(
3402
+ // eslint-disable-next-line max-len
3403
+ `Delay timeout (${config.messageQueueDelay}s) for ${conversationId}, processing batch with ${batch.messages.length} messages`
3404
+ );
3405
+ batch.timeout = void 0;
3406
+ resolveCollectWaiters(batch, 2 /* CONTINUE */);
3407
+ }
3408
+ }, config.messageQueueDelay * 1e3);
3409
+ }
3410
+ __name(resetBatchTimeout, "resetBatchTimeout");
3411
+ async function awaitBatchCompletion(batch, message) {
3412
+ if (batch.resolveWaiters.length === 0) {
3413
+ batch.messages = [message];
3414
+ } else {
3415
+ batch.messages.push(message);
3394
3416
  }
3395
- return new Promise((resolve) => {
3396
- batch.resolveWaiters.push(() => {
3397
- context.options.inputMessage = mergeMessages(batch.messages);
3398
- resolve(2 /* CONTINUE */);
3417
+ return await new Promise((resolve) => {
3418
+ batch.resolveWaiters.push((status) => {
3419
+ resolve(status ?? 2 /* CONTINUE */);
3399
3420
  });
3400
3421
  });
3401
3422
  }
3402
- __name(interruptAndMerge, "interruptAndMerge");
3423
+ __name(awaitBatchCompletion, "awaitBatchCompletion");
3403
3424
  async function waitForBatchCompletion(batch, conversationId, message, userName, context) {
3404
3425
  return new Promise((resolve) => {
3405
- batch.resolveWaiters.push(() => {
3426
+ batch.resolveWaiters.push((status) => {
3427
+ if (status === 1 /* STOP */) {
3428
+ resolve(1 /* STOP */);
3429
+ return;
3430
+ }
3406
3431
  batches.set(conversationId, {
3407
3432
  messages: [message],
3408
3433
  userName,
3409
3434
  resolveWaiters: [
3410
- () => {
3435
+ (nextStatus) => {
3436
+ if (nextStatus === 1 /* STOP */) {
3437
+ resolve(1 /* STOP */);
3438
+ return;
3439
+ }
3411
3440
  const newBatch = batches.get(conversationId);
3412
3441
  context.options.inputMessage = mergeMessages(
3413
3442
  newBatch.messages
3414
3443
  );
3444
+ newBatch.messages = [];
3415
3445
  batches.delete(conversationId);
3416
3446
  resolve(2 /* CONTINUE */);
3417
3447
  }
3418
- ]
3448
+ ],
3449
+ collectWaiters: [],
3450
+ state: "processing"
3419
3451
  });
3420
3452
  });
3421
3453
  });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "koishi-plugin-chatluna",
3
3
  "description": "chatluna for koishi",
4
- "version": "1.3.6",
4
+ "version": "1.3.7",
5
5
  "main": "lib/index.cjs",
6
6
  "module": "lib/index.mjs",
7
7
  "typings": "lib/index.d.ts",