[PR #333] [CLOSED] feat(deepagents): add QueueLookaheadMiddleware #366

Closed
opened 2026-06-05 17:22:50 -04:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langchain-ai/deepagentsjs/pull/333
Author: @hntrl
Created: 3/20/2026
Status: Closed

Base: mainHead: hunter/js-queue-lookahead-middleware


📝 Commits (2)

  • bfda3f7 feat(deepagents): add QueueLookaheadMiddleware for consuming pending queued messages
  • fe4b17f format

📊 Changes

6 files changed (+668 additions, -3 deletions)

View changed files

📝 libs/deepagents/package.json (+1 -1)
📝 libs/deepagents/src/index.ts (+2 -0)
📝 libs/deepagents/src/middleware/index.ts (+6 -0)
libs/deepagents/src/middleware/queue_lookahead.test.ts (+400 -0)
libs/deepagents/src/middleware/queue_lookahead.ts (+228 -0)
📝 pnpm-lock.yaml (+31 -2)

📄 Description

Summary

Port of https://github.com/langchain-ai/deepagents/pull/2104 to JS deepagents.

Adds QueueLookaheadMiddleware -- middleware that consumes pending queued messages before each model call. When deployed on LangGraph Platform with multitask_strategy="enqueue", user messages sent during an active run are queued as pending runs. This middleware drains those pending runs via the beforeModel hook, injects the user messages into state, and cancels the consumed runs so the model sees follow-up messages in-context rather than as separate runs.

Changes

libs/deepagents

  • New queue_lookahead.ts -- Core middleware using the beforeModel hook. Calls client.runs.list() with select: ["run_id", "kwargs"] to fetch pending runs, extracts user messages from kwargs.input.messages, cancels the runs, and returns { messages: [...] } as a state update so they're checkpointed.
  • New queue_lookahead.test.ts -- 24 unit tests covering extraction helpers, message conversion, the beforeModel lifecycle (no thread_id, no pending runs, single/multiple pending runs, error resilience, cancel failures, non-user message filtering, custom cancel action).
  • Exports added to middleware/index.ts and src/index.ts.
  • @langchain/langgraph-sdk added as a dependency for the Client and Run types.
  • RunWithKwargs type augments the SDK's Run with kwargs since the SDK type doesn't include it despite the server supporting it via select. Annotated with a TODO(hntrl) to fix upstream.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langchain-ai/deepagentsjs/pull/333 **Author:** [@hntrl](https://github.com/hntrl) **Created:** 3/20/2026 **Status:** ❌ Closed **Base:** `main` ← **Head:** `hunter/js-queue-lookahead-middleware` --- ### 📝 Commits (2) - [`bfda3f7`](https://github.com/langchain-ai/deepagentsjs/commit/bfda3f7695a5c42ab9f60e7703746afeaa97990e) feat(deepagents): add QueueLookaheadMiddleware for consuming pending queued messages - [`fe4b17f`](https://github.com/langchain-ai/deepagentsjs/commit/fe4b17f356026012e9e710c12fa1dff4502905c1) format ### 📊 Changes **6 files changed** (+668 additions, -3 deletions) <details> <summary>View changed files</summary> 📝 `libs/deepagents/package.json` (+1 -1) 📝 `libs/deepagents/src/index.ts` (+2 -0) 📝 `libs/deepagents/src/middleware/index.ts` (+6 -0) ➕ `libs/deepagents/src/middleware/queue_lookahead.test.ts` (+400 -0) ➕ `libs/deepagents/src/middleware/queue_lookahead.ts` (+228 -0) 📝 `pnpm-lock.yaml` (+31 -2) </details> ### 📄 Description ## Summary Port of https://github.com/langchain-ai/deepagents/pull/2104 to JS deepagents. Adds `QueueLookaheadMiddleware` -- middleware that consumes pending queued messages before each model call. When deployed on LangGraph Platform with `multitask_strategy="enqueue"`, user messages sent during an active run are queued as pending runs. This middleware drains those pending runs via the `beforeModel` hook, injects the user messages into state, and cancels the consumed runs so the model sees follow-up messages in-context rather than as separate runs. ## Changes ### `libs/deepagents` - **New `queue_lookahead.ts`** -- Core middleware using the `beforeModel` hook. Calls `client.runs.list()` with `select: ["run_id", "kwargs"]` to fetch pending runs, extracts user messages from `kwargs.input.messages`, cancels the runs, and returns `{ messages: [...] }` as a state update so they're checkpointed. - **New `queue_lookahead.test.ts`** -- 24 unit tests covering extraction helpers, message conversion, the beforeModel lifecycle (no thread_id, no pending runs, single/multiple pending runs, error resilience, cancel failures, non-user message filtering, custom cancel action). - **Exports** added to `middleware/index.ts` and `src/index.ts`. - **`@langchain/langgraph-sdk`** added as a dependency for the `Client` and `Run` types. - **`RunWithKwargs` type** augments the SDK's `Run` with `kwargs` since the SDK type doesn't include it despite the server supporting it via `select`. Annotated with a `TODO(hntrl)` to fix upstream. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-06-05 17:22:50 -04:00
yindo closed this issue 2026-06-05 17:22:50 -04:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langchain-ai/deepagentsjs#366