Skip to content

🚎 feat: Connect the Chat Transport to Live Generations - #18188

Merged
danny-avila merged 2 commits into
devfrom
danny-avila/chat-reattach-transport
Nov 17, 2026
Merged

danny-avila merged 2 commits into
devfrom
danny-avila/chat-reattach-transport

Conversation

@danny-avila

Copy link
Copy Markdown
Collaborator

Summary

The Chat class in @librechat/chat has no transport that talks to LibreChat. Its ChatTransport contract (sendMessages, reconnectToStream, abort) is implemented only by in-memory fakes in tests, so nothing outside a spec can construct a Chat and have it follow a real generation. The app's own chat runs on the hooks session instead, which is tied to the URL, the sidebar and the pane index and cannot be mounted a second time for a different conversation.

This adds createGenerationTransport, a ChatTransport over the generation routes. reconnectToStream asks the status route what is running for the chat, and when something is, opens that generation's stream with resume=true and the generation's createdAt, so the stream replays from its start and is fenced on that generation. A Chat that joins late therefore ends up holding what one that watched from the first frame holds. When nothing is running, or a different generation than the one named is running, it resolves null and opens no connection.

Sending and stopping are supplied by the app as optional members. Without them the transport is view-only and refuses both with ERR_VIEW_ONLY, which is the shape a subagent child thread needs: its stream is observed, never controlled.

Second in a stack: it sits on #18187 (child thread read routes) and is the transport the child-thread pane will construct its Chat with. Related to #18169, which gave each background child its own generation job to attach to.

How it works

The wire transport (createSSETransport) reports events through a callback; Chat reads an async iterable. openStream bridges the two and decides what the first event means:

reconnectToStream({ chatId })
  getStatus(chatId)
    not active, or a different generation than the one named   -> null, no connection
  wire.reconnectToStream(/api/agents/chat/stream/:id?resume=true&generationCreatedAt=…)
    open                      -> resolve the stream
    error 404 | 409           -> null   (the generation ended or was replaced)
    error, any other status   -> reject ChatTransportError (ERR_NETWORK | ERR_BAD_RESPONSE)
    abort                     -> null
  after open: frames queue in wire order; final | abort | error ends the iteration
  stream.close() or the caller's abortSignal closes the connection

The stream URL was assembled inline in StreamSubscription.url(). It moves to generationStreamUrl in core/protocol.ts, next to the protocol headers, and both the hooks session and the new transport build it there.

Type of change

  • Feature
  • Tests / tooling / CI

Testing

Tested environments/configuration:

Unit level only in this PR. The transport is exercised against a hand-driven wire, and once through the real Chat class: chat.resumeStream() reattaches, reads a step and a final frame, merges the final frame's messages and settles ready. Nothing in the app constructs it yet; the pane that does is the next PR in the stack and is where it is verified against a running server.

Automated tests:

  • Added packages/chat/src/core/__tests__/generation.spec.ts (15 cases): inactive status, URL and headers of the attachment, frame order, dropped connection, 404/409 before attaching, other failures before attaching, a mismatched generation, close(), the caller's abort signal, view-only refusals, pass-through of a supplied send and stop, and the two Chat cases.
  • subscription.spec.ts and chat.spec.ts still pass (273 tests across the three files).
  • npx tsc --noEmit in packages/chat; npm run static-checks -- --against origin/dev.

Screenshots / recordings

No user-facing change.

Risk / compatibility

The only behavior touched on an existing path is where the stream URL is built; subscription.spec.ts pins its path and query and is unchanged. The transport itself is new and unreferenced by the app.

@danny-avila
danny-avila added this pull request to stack #18259 November 17, 2026 14:36
@danny-avila
danny-avila force-pushed the danny-avila/chat-reattach-transport branch from c803582 to d8803d1 Compare November 17, 2026 14:41
@danny-avila
danny-avila marked this pull request as ready for review November 17, 2026 14:42
Base automatically changed from danny-avila/child-thread-read-routes to dev November 17, 2026 15:05
@danny-avila
danny-avila force-pushed the danny-avila/chat-reattach-transport branch from d8803d1 to 944b6c1 Compare November 17, 2026 15:05
@lia-by-librechat lia-by-librechat Bot changed the title 🔌 feat: Follow a Running Generation Through the Chat Class's Transport 🚎 feat: Connect the Chat Transport to Live Generations Nov 17, 2026
@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-17T15:21:08.987873Z fe2472c Manual request
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 944b6c10dc

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +69 to +72
const close = (): void => {
controller.abort();
finish();
};

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Settle the opening promise when cancellation closes the stream

If the caller aborts while the SSE transport is refreshing a token after an initial 401, close() marks the iterable done but never settles the pending openStream() promise. In this scenario, createSSETransport has an already-closed XHR, and its refresh handler returns without emitting an event once the signal is aborted, so the onEvent branches that resolve this promise never run. Consequently, callers awaiting reconnectToStream() can remain pending indefinitely after cancellation. Resolve the unopened attachment to null directly on cancellation rather than relying on the wire to emit an abort event.

Useful? React with 👍 / 👎.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Valid. Fixed in fe2472c: closing the stream now resolves an attachment that has not opened to null itself instead of waiting for the wire to report an abort. Pinned by resolves null when the caller aborts an attachment whose wire reports nothing more in generation.spec.ts, which uses a wire that emits nothing on abort; it fails with the line removed (1 failed, 15 passed) and passes with it (16 passed).

@danny-avila

Copy link
Copy Markdown
Collaborator Author

@codex review

Head fe2472c.

@chatgpt-codex-connector

Copy link
Copy Markdown

Codex Review: Didn't find any major issues. Another round soon, please!

Reviewed commit: fe2472c55b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@danny-avila
danny-avila merged commit cca2455 into dev Nov 17, 2026
59 of 60 checks passed
@danny-avila
danny-avila deleted the danny-avila/chat-reattach-transport branch November 17, 2026 15:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant