@dudousxd/nestjs-agent 1.15.0 → 1.16.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/README.md CHANGED
@@ -311,6 +311,8 @@ starts under its own id, and the settling turn's stream says so in a final `queu
311
311
  (`started: { messageId, runId }`). `mode: 'interrupt'` cancels the running turn and runs the message
312
312
  next; `mode: 'queue'` always queues. A failed turn or a Stop pauses the queue (`POST
313
313
  /agent/threads/:id/queue/resume` lifts it); an exhausted quota pauses it as the next message starts.
314
+ `POST /agent/queue/:messageId/interrupt` runs a message that is already waiting now — it moves to
315
+ the head as an interrupt and the running turn is cancelled for it, in one request.
314
316
  `GET`/`DELETE /agent/threads/:id/queue`, `PATCH`/`DELETE /agent/queue/:messageId` list, clear,
315
317
  edit, move and remove what is waiting.
316
318
 
@@ -2035,6 +2035,59 @@ var AgentService = class {
2035
2035
  }
2036
2036
  return queue.publish(threadId);
2037
2037
  }
2038
+ /**
2039
+ * `POST <base>/queue/:messageId/interrupt` — run a message that is already waiting NOW: it moves
2040
+ * to the head of its queue marked as an interrupt, any pause is lifted, and the running turn is
2041
+ * cancelled (its settle starts this message — the same path a `mode: 'interrupt'` send takes).
2042
+ * With nothing running, the message starts at once.
2043
+ *
2044
+ * One request, and the message never leaves the queue: a client that removed it and sent it again
2045
+ * would lose it if the second call failed, and would run it twice if another tab's drain started
2046
+ * it in between. Answers the queue, plus `interrupting` (the run that was cancelled) or `runId`
2047
+ * (the run this message started).
2048
+ */
2049
+ async interruptQueuedMessage(actor, messageId) {
2050
+ const queue = this.requireQueue();
2051
+ const store = queue.queueStore();
2052
+ const threadId = await this.assertOwnsQueuedMessage(actor, messageId);
2053
+ const gone = /* @__PURE__ */ __name(() => new import_common7.GoneException(`queued message ${messageId} already started or was removed`), "gone");
2054
+ const marked = await store.updateQueuedMessage(messageId, {
2055
+ interrupt: true
2056
+ });
2057
+ if (marked === null) {
2058
+ throw gone();
2059
+ }
2060
+ if (marked.interrupt !== true) {
2061
+ throw new import_common7.NotImplementedException("Interrupting a queued message requires a ChatQueueStore whose updateQueuedMessage stores `interrupt`; the bound store ignored it.");
2062
+ }
2063
+ if (!await store.moveQueuedMessage(messageId, 0)) {
2064
+ throw gone();
2065
+ }
2066
+ await store.setQueuePause(threadId, null);
2067
+ const { live } = await queue.holder(threadId, this.runner);
2068
+ if (live !== null && live !== messageId && await store.getQueuedMessage(messageId) !== null) {
2069
+ const state2 = await queue.publish(threadId);
2070
+ await this.runner.cancel(live);
2071
+ return {
2072
+ ...state2,
2073
+ interrupting: live
2074
+ };
2075
+ }
2076
+ if (live !== null) {
2077
+ return {
2078
+ ...await queue.state(threadId),
2079
+ runId: messageId
2080
+ };
2081
+ }
2082
+ const runId = await queue.kick(threadId, this.runner);
2083
+ const state = runId === void 0 ? await queue.publish(threadId) : await queue.state(threadId);
2084
+ return {
2085
+ ...state,
2086
+ ...runId !== void 0 ? {
2087
+ runId
2088
+ } : {}
2089
+ };
2090
+ }
2038
2091
  /** `DELETE <base>/threads/:id/queue` — drop every waiting message (and any pause). */
2039
2092
  async clearQueue(actor, threadId) {
2040
2093
  await this.assertOwnsThread(actor, threadId);
@@ -3677,6 +3730,14 @@ var QueueController = class {
3677
3730
  } : {}
3678
3731
  });
3679
3732
  }
3733
+ /**
3734
+ * Run a waiting message now: it moves to the head as an interrupt and the running turn is
3735
+ * cancelled for it. Carries `interrupting` (the cancelled run), or `runId` when nothing was
3736
+ * running and it started at once.
3737
+ */
3738
+ async interrupt(req, messageId) {
3739
+ return this.agent.interruptQueuedMessage(await this.actorResolver.resolve(req), messageId);
3740
+ }
3680
3741
  async remove(req, messageId) {
3681
3742
  return this.agent.removeQueuedMessage(await this.actorResolver.resolve(req), messageId);
3682
3743
  }
@@ -3727,6 +3788,17 @@ _ts_decorate16([
3727
3788
  ]),
3728
3789
  _ts_metadata15("design:returntype", Promise)
3729
3790
  ], QueueController.prototype, "update", null);
3791
+ _ts_decorate16([
3792
+ (0, import_common16.Post)("queue/:messageId/interrupt"),
3793
+ _ts_param14(0, (0, import_common16.Req)()),
3794
+ _ts_param14(1, (0, import_common16.Param)("messageId")),
3795
+ _ts_metadata15("design:type", Function),
3796
+ _ts_metadata15("design:paramtypes", [
3797
+ typeof Request === "undefined" ? Object : Request,
3798
+ String
3799
+ ]),
3800
+ _ts_metadata15("design:returntype", Promise)
3801
+ ], QueueController.prototype, "interrupt", null);
3730
3802
  _ts_decorate16([
3731
3803
  (0, import_common16.Delete)("queue/:messageId"),
3732
3804
  _ts_param14(0, (0, import_common16.Req)()),