Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active
TypeScript aianthropicartifactsawsazurechatgptchatgpt-cloneclaudeclonedeepseekgeminigooglegpt-5librechatmcpo1openairesponses-apivisionwebui
🚦 refactor: Route Chat Send, Stop, Steer and Queue Through the Transport (#16593)
Marco Beretta379554a8cfb6
diff --git a/client/src/data-provider/SSE/__tests__/queuedTurns.test.ts b/client/src/data-provider/SSE/__tests__/queuedTurns.test.tsindex 4f7a435f89..2220fa7ce8 100644--- a/client/src/data-provider/SSE/__tests__/queuedTurns.test.ts+++ b/client/src/data-provider/SSE/__tests__/queuedTurns.test.ts@@ -15,6 +15,7 @@ jest.mock('@tanstack/react-query', () => ({}));jest.mock('librechat-data-provider', () => ({+ ...jest.requireActual('librechat-data-provider'),QueryKeys: { agentQueuedTurns: 'agentQueuedTurns' },MutationKeys: {enqueueAgentQueuedTurn: 'enqueueAgentQueuedTurn',diff --git a/client/src/data-provider/SSE/mutations.ts b/client/src/data-provider/SSE/mutations.tsindex 77331d9209..20622a08d0 100644--- a/client/src/data-provider/SSE/mutations.ts+++ b/client/src/data-provider/SSE/mutations.ts@@ -1,6 +1,7 @@import { useMutation } from '@tanstack/react-query';import { apiBaseUrl, EModelEndpoint } from 'librechat-data-provider';import type { Agents, TMessage, TEphemeralAgent, TPendingSteer } from 'librechat-data-provider';+import { useChatTransport } from '~/Providers/ChatTransportContext';import { postGenerationRequest } from './protocol';export interface AbortStreamParams {@@ -48,12 +49,13 @@ export const abortStream = async (params: AbortStreamParams): Promise<AbortStrea};/**- * React Query mutation hook for aborting a generation stream.+ * React Query mutation hook for aborting a generation stream, through the host's transport.* Use this when the user explicitly clicks the stop button.*/export function useAbortStreamMutation() {+ const transport = useChatTransport();return useMutation({- mutationFn: abortStream,+ mutationFn: transport.abort,});}@@ -220,10 +222,12 @@ export const steerMessage = async (params: SteerMessageParams): Promise<SteerMes);};-/** React Query mutation hook for steering; the injection arrives on the SSE. */+/** React Query mutation hook for steering, through the host's transport; the injection+ * arrives on the SSE. */export function useSteerMessageMutation() {+ const transport = useChatTransport();return useMutation({- mutationFn: steerMessage,+ mutationFn: transport.steer,});}@@ -255,8 +259,9 @@ export const cancelSteerMessage = async (};export function useCancelSteerMutation() {+ const transport = useChatTransport();return useMutation({- mutationFn: cancelSteerMessage,+ mutationFn: transport.cancelSteer,});}@@ -288,7 +293,8 @@ export const armSteerMessage = async (params: ArmSteerParams): Promise<ArmSteerR};export function useArmSteerMutation() {+ const transport = useChatTransport();return useMutation({- mutationFn: armSteerMessage,+ mutationFn: transport.armSteer,});}diff --git a/client/src/data-provider/SSE/queuedTurns.ts b/client/src/data-provider/SSE/queuedTurns.tsindex e87b9b7961..3b38557867 100644--- a/client/src/data-provider/SSE/queuedTurns.ts+++ b/client/src/data-provider/SSE/queuedTurns.ts@@ -5,6 +5,7 @@ import type {TAgentQueuedTurnReceipt,TEnqueueAgentQueuedTurnRequest,} from 'librechat-data-provider';+import { useChatTransport } from '~/Providers/ChatTransportContext';export type AgentQueuedTurnReceipt = TAgentQueuedTurnReceipt;export type EnqueueAgentQueuedTurnRequest = TEnqueueAgentQueuedTurnRequest;@@ -133,12 +134,13 @@ export function useAgentQueuedTurns(expectsReceipts = false,) {const queryClient = useQueryClient();+ const transport = useChatTransport();const knownIds = [...new Set(clientRequestIds)].sort();const knownIdsSignature = knownIds.join('\u0000');const previousRequest = useRef({ conversationId, knownIdsSignature });const query = useQuery({queryKey: agentQueuedTurnsQueryKey(conversationId),- queryFn: () => fetchAgentQueuedTurns(conversationId, knownIds),+ queryFn: () => transport.listQueued(conversationId, knownIds),enabled: enabled && conversationId.length > 0,staleTime: 1_000,/** A stopped indeterminate row can receive exact proof at any time. Its@@ -191,9 +193,10 @@ export function useAgentQueuedTurns(export function useEnqueueAgentQueuedTurnMutation() {const queryClient = useQueryClient();+ const transport = useChatTransport();return useMutation({mutationKey: [MutationKeys.enqueueAgentQueuedTurn],- mutationFn: enqueueAgentQueuedTurn,+ mutationFn: transport.enqueue,/** A retry can encounter admission middleware while the first request is* still committing. Reconcile ambiguous outcomes through the read-only* known-id projection instead of issuing a second mutating POST. */@@ -207,9 +210,10 @@ export function useEnqueueAgentQueuedTurnMutation() {export function useCancelAgentQueuedTurnMutation() {const queryClient = useQueryClient();+ const transport = useChatTransport();return useMutation({mutationKey: [MutationKeys.cancelAgentQueuedTurn],- mutationFn: cancelAgentQueuedTurn,+ mutationFn: transport.cancelQueued,onSuccess: (_receipt, input) =>queryClient.invalidateQueries({queryKey: [QueryKeys.agentQueuedTurns, input.conversationId],diff --git a/client/src/hooks/Chat/__tests__/transport.spec.tsx b/client/src/hooks/Chat/__tests__/transport.spec.tsxnew file mode 100644index 0000000000..5ca288581e--- /dev/null+++ b/client/src/hooks/Chat/__tests__/transport.spec.tsx@@ -0,0 +1,491 @@+import React from 'react';+import { MemoryRouter } from 'react-router-dom';+import { RecoilRoot, useRecoilValue } from 'recoil';+import { QueryKeys } from 'librechat-data-provider';+import { act, renderHook, waitFor } from '@testing-library/react';+import { QueryClient, QueryClientProvider } from '@tanstack/react-query';+import type {+ TEnqueueAgentQueuedTurnRequest,+ TSubmission,+ TConversation,+ ChatTransportOptions,+ ChatTransportRequest,+} from 'librechat-data-provider';+import type { MutableSnapshot } from 'recoil';+import type { Transport } from '~/hooks/Chat/contract';+import type { PendingSteer } from '~/store/families';+import { ChatTransportContext } from '~/Providers/ChatTransportContext';+import { useSteerReclaim } from '~/hooks/Chat/useSteerCancel';+import useSteerEscalate from '~/hooks/Chat/useSteerEscalate';+import useResumableSSE from '~/hooks/SSE/useResumableSSE';+import useChatHelpers from '~/hooks/Chat/useChatHelpers';+import useSteering from '~/hooks/Chat/useSteering';+import useSSE from '~/hooks/SSE/useSSE';+import store from '~/store';++jest.mock('~/hooks/AuthContext', () => {+ const { createContext } = jest.requireActual('react');+ return {+ AuthContext: createContext(undefined),+ useAuthContext: () => ({ token: 'test-token', isAuthenticated: true }),+ };+});++type StreamCall = { url: string; options: ChatTransportOptions };++/**+ * A transport that records every request and answers from the test, standing in for the+ * network at the contract boundary. `streams` and `sends` keep each connection's options, so a+ * test can push events through `onEvent` or watch its `signal`.+ */+function createFakeTransport(overrides: Partial<Transport> = {}) {+ const streams: StreamCall[] = [];+ const sends: (ChatTransportRequest & { options: ChatTransportOptions })[] = [];+ const transport: Transport = {+ stream: jest.fn(() => ({+ send: (request, options) => {+ sends.push({ ...request, options });+ },+ reconnectToStream: (request, options) => {+ streams.push({ url: request.url, options });+ return { closed: false };+ },+ })),+ start: jest.fn(async () => ({+ streamId: 'convo-1',+ conversationId: 'convo-1',+ generationCreatedAt: 1000,+ generationProtocolVersion: 2,+ })),+ abort: jest.fn(async () => ({ success: true })),+ abortRun: jest.fn(async () => new Response(null, { status: 204 })),+ steer: jest.fn(),+ cancelSteer: jest.fn(),+ armSteer: jest.fn(),+ listQueued: jest.fn(async () => []),+ enqueue: jest.fn(),+ cancelQueued: jest.fn(),+ ...overrides,+ };+ return { transport, streams, sends };+}++function createWrapper(+ transport: Transport,+ seed?: (state: MutableSnapshot) => void,+ seedCache?: (queryClient: QueryClient) => void,+) {+ const queryClient = new QueryClient({+ defaultOptions: { queries: { retry: false }, mutations: { retry: false } },+ });+ /** The composer's user-key check is not gated by `queriesEnabled`; answer it from cache. */+ queryClient.setQueryData([QueryKeys.name, 'agents'], { expiresAt: '' });+ seedCache?.(queryClient);+ return function Wrapper({ children }: { children: React.ReactNode }) {+ return (+ <MemoryRouter>+ <QueryClientProvider client={queryClient}>+ <RecoilRoot+ initializeState={(state) => {+ state.set(store.queriesEnabled, false);+ seed?.(state);+ }}+ >+ <ChatTransportContext.Provider value={transport}>+ {children}+ </ChatTransportContext.Provider>+ </RecoilRoot>+ </QueryClientProvider>+ </MemoryRouter>+ );+ };+}++const buildSubmission = (endpoint = 'agents') =>+ ({+ conversation: { conversationId: 'convo-1', endpoint },+ userMessage: {+ messageId: 'msg-1',+ conversationId: 'convo-1',+ text: 'Hello',+ isCreatedByUser: true,+ sender: 'User',+ parentMessageId: '00000000-0000-0000-0000-000000000000',+ },+ messages: [],+ isTemporary: false,+ initialResponse: {+ messageId: 'resp-1',+ conversationId: 'convo-1',+ parentMessageId: 'msg-1',+ text: '',+ isCreatedByUser: false,+ sender: 'Assistant',+ },+ endpointOption: { endpoint },+ }) as unknown as TSubmission;++const buildChatHelpers = () => ({+ setMessages: jest.fn(),+ getMessages: jest.fn(() => []),+ setConversation: jest.fn(),+ setIsSubmitting: jest.fn(),+ newConversation: jest.fn(),+});++const seedSteerableRun = ({ set }: MutableSnapshot) => {+ set(store.conversationByIndex(0), {+ conversationId: 'convo-1',+ endpoint: 'agents',+ } as TConversation);+ set(store.activeGenerationCreatedAtByConvoId('convo-1'), 1000);+ set(store.activeGenerationProtocolVersionByConvoId('convo-1'), 2);+};++/** The running turn: queued follow-ups chain off its in-flight response. */+const seedLiveBranch = (queryClient: QueryClient) =>+ queryClient.setQueryData(+ [QueryKeys.messages, 'convo-1'],+ [+ { ...buildSubmission().userMessage },+ {+ messageId: 'resp-1',+ conversationId: 'convo-1',+ parentMessageId: 'msg-1',+ text: '',+ isCreatedByUser: false,+ sender: 'Assistant',+ },+ ],+ );++const renderSteering = (transport: Transport) =>+ renderHook(+ () => ({+ steering: useSteering({+ consumeDraft: jest.fn(),+ index: 0,+ conversationId: 'convo-1',+ conversation: { conversationId: 'convo-1', endpoint: 'agents' } as TConversation,+ isSubmitting: true,+ answerModeActive: false,+ sendNow: jest.fn(),+ stopGenerating: jest.fn(),+ }),+ queue: useRecoilValue(store.queuedMessagesByConvoId('convo-1')),+ }),+ { wrapper: createWrapper(transport, seedSteerableRun, seedLiveBranch) },+ );++describe('chat transport boundary', () => {+ describe('send (agents)', () => {+ it('starts the turn and attaches to its stream through the host transport', async () => {+ const fake = createFakeTransport();+ const submission = buildSubmission();+ const helpers = buildChatHelpers();+ renderHook(() => useResumableSSE(submission, helpers), {+ wrapper: createWrapper(fake.transport),+ });++ await waitFor(() => expect(fake.streams).toHaveLength(1));+ expect(fake.transport.start).toHaveBeenCalledTimes(1);+ const [request, options] = (fake.transport.start as jest.Mock).mock.calls[0];+ expect(request.server).toBe('/api/agents/chat/agents');+ expect(request.payload).toEqual(expect.objectContaining({ text: 'Hello' }));+ expect(options.signal).toBeInstanceOf(AbortSignal);+ expect(fake.transport.stream).toHaveBeenCalledWith({ token: 'test-token' });+ expect(fake.streams[0].url).toContain('/api/agents/chat/stream/convo-1');+ expect(fake.streams[0].url).toContain('generationCreatedAt=1000');+ });++ it('reports a rejected start as an error message without attaching a stream', async () => {+ const failure = Object.assign(new Error('Bad request'), {+ response: { status: 400, data: { message: 'Bad request' }, headers: {} },+ });+ const fake = createFakeTransport({ start: jest.fn(async () => Promise.reject(failure)) });+ const submission = buildSubmission();+ const helpers = buildChatHelpers();+ renderHook(() => useResumableSSE(submission, helpers), {+ wrapper: createWrapper(fake.transport),+ });++ await waitFor(() => expect(helpers.setIsSubmitting).toHaveBeenCalledWith(false));+ expect(fake.transport.start).toHaveBeenCalledTimes(1);+ expect(fake.streams).toHaveLength(0);+ const written = helpers.setMessages.mock.calls.flatMap(([messages]) => messages);+ expect(written.some((message: { error?: boolean }) => message.error === true)).toBe(true);+ });+ });++ describe('send (assistants)', () => {+ it('streams the turn through the host transport and closes it on unmount', () => {+ const fake = createFakeTransport();+ const submission = buildSubmission('assistants');+ const helpers = buildChatHelpers();+ const { unmount } = renderHook(() => useSSE(submission, helpers), {+ wrapper: createWrapper(fake.transport),+ });++ expect(fake.transport.stream).toHaveBeenCalledWith({ token: 'test-token' });+ expect(fake.sends).toHaveLength(1);+ expect(fake.sends[0].server).toBe('/api/assistants/v2/chat');+ expect(fake.sends[0].payload).toEqual(expect.objectContaining({ text: 'Hello' }));+ expect(helpers.setIsSubmitting).toHaveBeenCalledWith(true);++ const { signal } = fake.sends[0].options;+ expect(signal.aborted).toBe(false);+ unmount();+ expect(signal.aborted).toBe(true);+ });++ it('writes a stream error from the transport as an error message', () => {+ const fake = createFakeTransport();+ const submission = buildSubmission('assistants');+ const helpers = buildChatHelpers();+ renderHook(() => useSSE(submission, helpers), { wrapper: createWrapper(fake.transport) });++ act(() => fake.sends[0].options.onEvent({ type: 'error', data: undefined }));++ expect(helpers.setIsSubmitting).toHaveBeenLastCalledWith(false);+ const written = helpers.setMessages.mock.calls.flatMap(([messages]) => messages);+ expect(written.some((message: { error?: boolean }) => message.error === true)).toBe(true);+ });+ });++ describe('abort', () => {+ const seedRun = ({ set }: MutableSnapshot) => {+ set(store.conversationByIndex(0), {+ conversationId: 'convo-1',+ endpoint: 'agents',+ } as TConversation);+ set(store.activeGenerationCreatedAtByConvoId('convo-1'), 1000);+ set(store.activeGenerationProtocolVersionByConvoId('convo-1'), 2);+ };++ it('stops a resumable generation through the host transport', async () => {+ const fake = createFakeTransport();+ const { result } = renderHook(() => useChatHelpers(0), {+ wrapper: createWrapper(fake.transport, seedRun),+ });++ await act(async () => {+ await result.current.stopGenerating();+ });++ expect(fake.transport.abort).toHaveBeenCalledTimes(1);+ expect((fake.transport.abort as jest.Mock).mock.calls[0][0]).toEqual({+ conversationId: 'convo-1',+ generationCreatedAt: 1000,+ });+ });++ it('settles a rejected stop without throwing to the caller', async () => {+ const failure = Object.assign(new Error('Not found'), { response: { status: 404 } });+ const fake = createFakeTransport({ abort: jest.fn(async () => Promise.reject(failure)) });+ const { result } = renderHook(() => useChatHelpers(0), {+ wrapper: createWrapper(fake.transport, seedRun),+ });++ await act(async () => {+ await expect(result.current.stopGenerating()).resolves.toBeUndefined();+ });+ expect(fake.transport.abort).toHaveBeenCalledTimes(1);+ });++ it('stops an Assistants run through the host transport when its stream is cancelled', async () => {+ const fake = createFakeTransport();+ const submission = buildSubmission('assistants');+ const helpers = buildChatHelpers();+ renderHook(() => useSSE(submission, helpers), { wrapper: createWrapper(fake.transport) });++ await act(async () => {+ fake.sends[0].options.onEvent({ type: 'abort' });+ });++ await waitFor(() => expect(fake.transport.abortRun).toHaveBeenCalledTimes(1));+ expect(fake.transport.abortRun).toHaveBeenCalledWith(+ { endpoint: 'assistants', abortKey: 'convo-1:' },+ { token: 'test-token' },+ );+ await waitFor(() => expect(helpers.setIsSubmitting).toHaveBeenLastCalledWith(false));+ });+ });++ describe('steer', () => {+ it('sends a steer to the running generation through the host transport', async () => {+ const fake = createFakeTransport({+ steer: jest.fn(async () => ({+ status: 'queued' as const,+ steerId: 'steer-1',+ position: 0,+ conversationId: 'convo-1',+ generationProtocolVersion: 2,+ })),+ });+ const { result } = renderSteering(fake.transport);++ act(() => {+ expect(result.current.steering.steerFromComposer('fold this in')).toBe(true);+ });++ await waitFor(() => expect(fake.transport.steer).toHaveBeenCalledTimes(1));+ expect((fake.transport.steer as jest.Mock).mock.calls[0][0]).toEqual(+ expect.objectContaining({+ conversationId: 'convo-1',+ generationCreatedAt: 1000,+ text: 'fold this in',+ }),+ );+ expect(result.current.queue).toEqual([]);+ });++ it('keeps a refused steer as a queued turn', async () => {+ const refusal = Object.assign(new Error('Steering unsupported'), {+ response: { status: 409, data: { code: 'STEER_UNSUPPORTED' } },+ });+ const fake = createFakeTransport({ steer: jest.fn(async () => Promise.reject(refusal)) });+ const { result } = renderSteering(fake.transport);++ act(() => {+ result.current.steering.steerFromComposer('do not lose me');+ });++ await waitFor(() =>+ expect(result.current.queue).toEqual([expect.objectContaining({ text: 'do not lose me' })]),+ );+ expect(fake.transport.steer).toHaveBeenCalledTimes(1);+ });+ });++ describe('steer controls', () => {+ const pendingSteer: PendingSteer = {+ steerId: 'steer-1',+ text: 'fold this in',+ status: 'pending',+ createdAt: 1,+ };+ const seedPendingSteer = (snapshot: MutableSnapshot) => {+ seedSteerableRun(snapshot);+ snapshot.set(store.pendingSteersByConvoId('convo-1'), [pendingSteer]);+ };++ it('withdraws a waiting steer through the host transport', async () => {+ const fake = createFakeTransport({ cancelSteer: jest.fn(async () => ({ removed: true })) });+ const { result } = renderHook(+ () => ({+ reclaim: useSteerReclaim('convo-1'),+ pending: useRecoilValue(store.pendingSteersByConvoId('convo-1')),+ }),+ { wrapper: createWrapper(fake.transport, seedPendingSteer) },+ );++ let outcome = '';+ await act(async () => {+ outcome = await result.current.reclaim(pendingSteer);+ });++ expect(outcome).toBe('reclaimed');+ expect((fake.transport.cancelSteer as jest.Mock).mock.calls[0][0]).toEqual({+ conversationId: 'convo-1',+ steerId: 'steer-1',+ generationCreatedAt: 1000,+ });+ expect(result.current.pending).toEqual([]);+ });++ it('escalates a waiting steer to an interrupt through the host transport', async () => {+ const fake = createFakeTransport({+ armSteer: jest.fn(async () => ({+ armed: true,+ preemptRevision: 1,+ generationProtocolVersion: 2,+ })),+ });+ const { result } = renderHook(+ () => ({+ escalate: useSteerEscalate('convo-1'),+ pending: useRecoilValue(store.pendingSteersByConvoId('convo-1')),+ }),+ { wrapper: createWrapper(fake.transport, seedPendingSteer) },+ );++ act(() => result.current.escalate({ steerId: 'steer-1' }));++ await waitFor(() =>+ expect(result.current.pending).toEqual([+ expect.objectContaining({ steerId: 'steer-1', preempt: true }),+ ]),+ );+ expect((fake.transport.armSteer as jest.Mock).mock.calls[0][0]).toEqual({+ conversationId: 'convo-1',+ steerId: 'steer-1',+ generationCreatedAt: 1000,+ });+ });+ });++ describe('queue', () => {+ const receiptFor = (input: TEnqueueAgentQueuedTurnRequest, status = 'queued' as const) => ({+ ...input,+ queuedTurnId: 'queued-turn-1',+ status,+ revision: 0,+ createdAt: '2026-10-01T00:00:00.000Z',+ updatedAt: '2026-10-01T00:00:00.000Z',+ });++ it('reads, adds and withdraws server queued turns through the host transport', async () => {+ const fake = createFakeTransport({+ enqueue: jest.fn(async (input: TEnqueueAgentQueuedTurnRequest) => receiptFor(input)),+ cancelQueued: jest.fn(async () => ({+ ...receiptFor({} as TEnqueueAgentQueuedTurnRequest),+ status: 'cancelled' as const,+ })),+ });+ const { result } = renderSteering(fake.transport);++ await waitFor(() => expect(fake.transport.listQueued).toHaveBeenCalled());+ expect((fake.transport.listQueued as jest.Mock).mock.calls[0][0]).toBe('convo-1');++ await act(async () => {+ expect(result.current.steering.queueFromComposer('after this run')).toBe(true);+ });++ await waitFor(() => expect(fake.transport.enqueue).toHaveBeenCalledTimes(1));+ expect((fake.transport.enqueue as jest.Mock).mock.calls[0][0]).toEqual(+ expect.objectContaining({ conversationId: 'convo-1', text: 'after this run' }),+ );+ await waitFor(() =>+ expect(result.current.queue[0].server).toEqual(+ expect.objectContaining({ id: 'queued-turn-1' }),+ ),+ );++ let discarded = false;+ await act(async () => {+ discarded = await result.current.steering.discardQueued(result.current.queue[0]);+ });+ expect(discarded).toBe(true);+ expect(fake.transport.cancelQueued).toHaveBeenCalledWith({+ conversationId: 'convo-1',+ queuedTurnId: 'queued-turn-1',+ });+ });++ it('keeps a turn queued locally when the server has no queue', async () => {+ const unsupported = Object.assign(new Error('Not found'), { response: { status: 404 } });+ const fake = createFakeTransport({+ enqueue: jest.fn(async () => Promise.reject(unsupported)),+ });+ const { result } = renderSteering(fake.transport);++ await act(async () => {+ result.current.steering.queueFromComposer('hold me here');+ });++ await waitFor(() => expect(fake.transport.enqueue).toHaveBeenCalledTimes(1));+ await waitFor(() => expect(result.current.queue[0]?.server).toBeUndefined());+ expect(result.current.queue).toEqual([expect.objectContaining({ text: 'hold me here' })]);+ });+ });+});diff --git a/client/src/hooks/SSE/useEventHandlers.ts b/client/src/hooks/SSE/useEventHandlers.tsindex f1fc248529..21a98dfb69 100644--- a/client/src/hooks/SSE/useEventHandlers.ts+++ b/client/src/hooks/SSE/useEventHandlers.ts@@ -7,7 +7,6 @@ import { useParams, useNavigate, useLocation } from 'react-router-dom';import {QueryKeys,Constants,- EndpointURLs,ContentTypes,tPresetSchema,tMessageSchema,@@ -64,6 +63,7 @@ import {import useFocusRegeneratedResponse from '~/hooks/Chat/useFocusRegeneratedResponse';import { shouldResetSubagentAtomsOnConversationChange } from './cleanup';import useAttachmentHandler from '~/hooks/SSE/useAttachmentHandler';+import { useChatTransport } from '~/Providers/ChatTransportContext';import useContentHandler from '~/hooks/SSE/useContentHandler';import useStepHandler from '~/hooks/SSE/useStepHandler';import { useApplyAgentTemplate } from '~/hooks/Agents';@@ -648,6 +648,7 @@ export default function useEventHandlers({const lastAnnouncementTimeRef = useRef(Date.now());const { conversationId: paramId } = useParams();const { token } = useAuthContext();+ const transport = useChatTransport();const { contentHandler, resetContentHandler } = useContentHandler({ setMessages, getMessages });/** `refetchType: 'all'` so cached-but-unmounted skill queries refresh too:@@ -1417,17 +1418,10 @@ export default function useEventHandlers({}try {- const response = await fetch(`${EndpointURLs[endpoint ?? '']}/abort`, {- method: 'POST',- headers: {- 'Content-Type': 'application/json',- Authorization: `Bearer ${token}`,- },- body: JSON.stringify({- abortKey: runAbortKey,- endpoint,- }),- });+ const response = await transport.abortRun(+ { endpoint: endpoint ?? '', abortKey: runAbortKey },+ { token },+ );// Check if the response is JSONconst contentType = response.headers.get('content-type');@@ -1473,6 +1467,7 @@ export default function useEventHandlers({},[token,+ transport,getMessages,setMessages,finalHandler,diff --git a/client/src/hooks/SSE/useResumableSSE.ts b/client/src/hooks/SSE/useResumableSSE.tsindex 723513b8e0..4a6852ef72 100644--- a/client/src/hooks/SSE/useResumableSSE.ts+++ b/client/src/hooks/SSE/useResumableSSE.ts@@ -82,7 +82,6 @@ import {streamStatusQueryKey,generationProtocolHeaders,getGenerationProtocolVersion,- postGenerationRequest,supportsGenerationProtocolV2,GENERATION_PROTOCOL_VERSION,} from '~/data-provider';@@ -96,10 +95,10 @@ import useEventHandlers, {keepLocalCodeApprovalMode,} from './useEventHandlers';import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';+import { useChatTransport } from '~/Providers/ChatTransportContext';import useSteerConvert from '~/hooks/Chat/useSteerConvert';import { liveAppliedSteerIdsAtom } from '~/store/steer';import { useAuthContext } from '~/hooks/AuthContext';-import { createSSETransport } from './transport';import { useFileMapContext } from '~/Providers';import useUsageHandler from './useUsageHandler';import useLocalize from '~/hooks/useLocalize';@@ -898,6 +897,7 @@ export default function useResumableSSE(const setActiveRunId = useSetRecoilState(store.activeRunFamily(runIndex));const { token, isAuthenticated } = useAuthContext();+ const transport = useChatTransport();const fileMap = useFileMapContext();const fileMapRef = useRef(fileMap);fileMapRef.current = fileMap;@@ -3906,7 +3906,7 @@ export default function useResumableSSE();};- connection = createSSETransport({ token }).reconnectToStream(+ connection = transport.stream({ token }).reconnectToStream({ url, headers: generationProtocolHeaders() },{signal: streamController.signal,@@ -3954,6 +3954,7 @@ export default function useResumableSSE(setConversation,runIndex,token,+ transport,setAbortScroll,setActiveRunId,setShowStopButton,@@ -4004,7 +4005,7 @@ export default function useResumableSSE(/*** Start generation (POST request that returns streamId)- * Uses the generation protocol request wrapper, including auth refresh.+ * Posts through the host transport, which owns auth refresh.* Retries transient network failures and startup readiness responses.* Readiness retries honor Retry-After until cleanup or the readiness window expires.*/@@ -4047,7 +4048,7 @@ export default function useResumableSSE(}requestAttempts += 1;try {- const data = await postGenerationRequest<unknown>(url, payload, { signal });+ const data = await transport.start({ server: url, payload }, { signal });if (signal?.aborted) {return null;}@@ -4248,6 +4249,7 @@ export default function useResumableSSE(setIsSubmitting,setShowStopButton,setSubmission,+ transport,],);diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.tsindex e7899b0fbc..0f7b5555cd 100644--- a/client/src/hooks/SSE/useSSE.ts+++ b/client/src/hooks/SSE/useSSE.ts@@ -20,9 +20,9 @@ import { clearComposerDrafts, applyPendingAction, findPendingActionMessageIndeximport { startedAsNewConversation, buildCreatedInitialResponse } from './useEventHandlers';import { pendingApprovalActionFamily } from '~/components/Chat/approval/state';import { useGetStartupConfig, useGetUserBalance } from '~/data-provider';+import { useChatTransport } from '~/Providers/ChatTransportContext';import { useAuthContext } from '~/hooks/AuthContext';import useEventHandlers from './useEventHandlers';-import { createSSETransport } from './transport';import useUsageHandler from './useUsageHandler';import store from '~/store';@@ -47,6 +47,7 @@ export default function useSSE(const setActiveRunId = useSetRecoilState(store.activeRunFamily(runIndex));const { token, isAuthenticated } = useAuthContext();+ const transport = useChatTransport();const [completed, setCompleted] = useState(new Set());const setAbortScroll = useSetRecoilState(store.abortScrollFamily(runIndex));const setShowStopButton = useSetRecoilState(store.showStopButtonByIndex(runIndex));@@ -308,10 +309,9 @@ export default function useSSE(const controller = new AbortController();setIsSubmitting(true);- createSSETransport({ token }).send(- { server: payloadData.server, payload },- { signal: controller.signal, onEvent },- );+ transport+ .stream({ token })+ .send({ server: payloadData.server, payload }, { signal: controller.signal, onEvent });return () => controller.abort();// eslint-disable-next-line react-hooks/exhaustive-deps