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: Let the Chat Contract Own Settings and Transport (#16592)
Marco Berettab8cbc56f4591
diff --git a/client/src/Providers/ChatSettingsContext.tsx b/client/src/Providers/ChatSettingsContext.tsxindex 9034f78789..c16e07d8cc 100644--- a/client/src/Providers/ChatSettingsContext.tsx+++ b/client/src/Providers/ChatSettingsContext.tsx@@ -1,23 +1,5 @@import { createContext, useContext } from 'react';--/** Composer action while a run is in flight: fold the text into the run, or queue a new turn. */-export type DuringRunAction = 'steer' | 'queue';--/**- * App-global preferences the chat reads but does not own. The host supplies them, so the chat- * hooks never reach into the app's state store for shell settings. A preference belongs here only- * once every chat reader of it takes it from here: a reader left on the store would act on a- * different value than a host that supplies its own.- */-export type ChatSettings = {- /** Default composer action while a run is in flight. */- duringRunDefaultAction: DuringRunAction;- setDuringRunDefaultAction: (action: DuringRunAction) => void;- /** Whether a steer interrupts the running step instead of waiting for the next one. */- steerInterruptsByDefault: boolean;- /** Closes the artifacts panel, called when the active conversation changes. */- resetVisibleArtifacts: () => void;-};+import type { ChatSettings } from '~/hooks/Chat/contract';/** Stock values, used when no host supplies settings (isolated renders and tests). */export const defaultChatSettings: ChatSettings = {diff --git a/client/src/Providers/ChatTransportContext.tsx b/client/src/Providers/ChatTransportContext.tsxnew file mode 100644index 0000000000..4eb5ba02bf--- /dev/null+++ b/client/src/Providers/ChatTransportContext.tsx@@ -0,0 +1,37 @@+import { createContext, useContext } from 'react';+import { EndpointURLs } from 'librechat-data-provider';+import type { Transport } from '~/hooks/Chat/contract';+import { createSSETransport } from '~/hooks/SSE/transport';+import * as requests from '~/data-provider';++/**+ * The stock transport: SSE for streams, the generation routes for control requests. Members+ * resolve their request functions when called, not when this module loads, because the request+ * hooks in `~/data-provider` read this context and so import this module back.+ */+export const defaultChatTransport: Transport = {+ stream: ({ token }) => createSSETransport({ token }),+ start: ({ server, payload }, options) =>+ requests.postGenerationRequest<unknown>(server, payload, options),+ abort: (params) => requests.abortStream(params),+ abortRun: ({ endpoint, abortKey }, { token }) =>+ fetch(`${EndpointURLs[endpoint]}/abort`, {+ method: 'POST',+ headers: {+ 'Content-Type': 'application/json',+ Authorization: `Bearer ${token}`,+ },+ body: JSON.stringify({ abortKey, endpoint }),+ }),+ steer: (params) => requests.steerMessage(params),+ cancelSteer: (params) => requests.cancelSteerMessage(params),+ armSteer: (params) => requests.armSteerMessage(params),+ listQueued: (conversationId, clientRequestIds) =>+ requests.fetchAgentQueuedTurns(conversationId, clientRequestIds),+ enqueue: (input) => requests.enqueueAgentQueuedTurn(input),+ cancelQueued: (input) => requests.cancelAgentQueuedTurn(input),+};++export const ChatTransportContext = createContext<Transport>(defaultChatTransport);++export const useChatTransport = () => useContext(ChatTransportContext);diff --git a/client/src/hooks/Chat/contract.check.ts b/client/src/hooks/Chat/contract.check.tsindex 99e20ed9dd..f16e32dc5d 100644--- a/client/src/hooks/Chat/contract.check.ts+++ b/client/src/hooks/Chat/contract.check.ts@@ -9,8 +9,10 @@* types consumers read.*/import type { ContextType } from 'react';+import type { ChatContract, AddedChatContract, ChatSettings, Transport } from './contract';+import type { ChatTransportContext } from '~/Providers/ChatTransportContext';+import type { ChatSettingsContext } from '~/Providers/ChatSettingsContext';import type { AddedChatContext } from '~/Providers/AddedChatContext';-import type { ChatContract, AddedChatContract } from './contract';import type { ChatContext } from '~/Providers/ChatContext';import type useAddedResponse from './useAddedResponse';import type useChatFunctions from './useChatFunctions';@@ -35,3 +37,9 @@ export type AddedChatContractChecks = [Expect<Equal<ReturnType<typeof useAddedResponse>, AddedChatContract>>,Expect<Equal<ContextType<typeof AddedChatContext>, AddedChatContract>>,];++/** The host supplies settings and transport through the contract's types, nothing wider. */+export type ChatHostContractChecks = [+ Expect<Equal<ContextType<typeof ChatSettingsContext>, ChatSettings>>,+ Expect<Equal<ContextType<typeof ChatTransportContext>, Transport>>,+];diff --git a/client/src/hooks/Chat/contract.ts b/client/src/hooks/Chat/contract.tsindex f5d7e6b674..64bb0111c5 100644--- a/client/src/hooks/Chat/contract.ts+++ b/client/src/hooks/Chat/contract.ts@@ -1,5 +1,23 @@-import type { TConversation, TMessage, TPreset } from 'librechat-data-provider';+import type {+ TPreset,+ TMessage,+ TConversation,+ ChatTransport,+ ChatTransportRequest,+} from 'librechat-data-provider';import type { SetStateAction, Dispatch, MouseEvent } from 'react';+import type {+ ArmSteerParams,+ ArmSteerResponse,+ AbortStreamParams,+ CancelSteerParams,+ SteerMessageParams,+ CancelSteerResponse,+ AbortStreamResponse,+ SteerMessageResponse,+ AgentQueuedTurnReceipt,+ EnqueueAgentQueuedTurnRequest,+} from '~/data-provider';import type { NewConversationParams, TOptionSettings, ExtendedFile, TAskFunction } from '~/common';/** Options accepted by {@link ChatConversationContract.newConversation}: the shared params plus@@ -138,3 +156,73 @@ export type AddedChatContract = {/** Builds and stores a conversation for the added pane from a template and/or preset. */generateConversation: (params?: NewConversationParams) => TConversation;};++/** Composer action while a run is in flight: fold the text into the run, or queue a new turn. */+export type DuringRunAction = 'steer' | 'queue';++/**+ * App-global preferences the chat reads but does not own. The host supplies them, so the chat+ * hooks never reach into the app's state store for shell settings. A preference belongs here only+ * once every chat reader of it takes it from here: a reader left on the store would act on a+ * different value than a host that supplies its own.+ */+export type ChatSettings = {+ /** Default composer action while a run is in flight. */+ duringRunDefaultAction: DuringRunAction;+ setDuringRunDefaultAction: (action: DuringRunAction) => void;+ /** Whether a steer interrupts the running step instead of waiting for the next one. */+ steerInterruptsByDefault: boolean;+ /** Closes the artifacts panel, called when the active conversation changes. */+ resetVisibleArtifacts: () => void;+};++/** The assistants abort route and the run it stops. */+export type AbortRunRequest = {+ /** The assistants endpoint whose `/abort` route owns the run. */+ endpoint: string;+ /** `conversationId:responseMessageId` of the run to stop. */+ abortKey: string;+};++/**+ * The wire the chat runs over, supplied by the host: starting a turn, streaming it, stopping+ * it, and steering or queueing behind it. A host that substitutes its own (a test, another+ * backend) changes no chat hook for those. Answers that resume a paused run (tool approvals,+ * ask-user replies) still post to the stock resume route directly.+ *+ * AI SDK: `ChatTransport`, widened by the control requests a resumable, steerable run needs.+ */+export interface Transport {+ /**+ * The stream side for one bearer token: `send` streams a turn that carries its own response+ * (Assistants), `reconnectToStream` attaches to a generation running on the server.+ */+ stream: (auth: { token?: string }) => ChatTransport;+ /**+ * POSTs a turn to a resumable generation route. Resolves to the start response, which names+ * the stream to attach to; rejects with the HTTP failure, its body on `response.data`.+ */+ start: (request: ChatTransportRequest, options?: { signal?: AbortSignal }) => Promise<unknown>;+ /** Stops a resumable generation; the stream then reports the abort. AI SDK: `stop`. */+ abort: (params: AbortStreamParams) => Promise<AbortStreamResponse>;+ /** Stops an Assistants run, which has no resumable generation to address. */+ abortRun: (request: AbortRunRequest, auth: { token?: string }) => Promise<Response>;+ /** Queues a message for injection into the running generation. */+ steer: (params: SteerMessageParams) => Promise<SteerMessageResponse>;+ /** Withdraws a steer that has not been injected yet. */+ cancelSteer: (params: CancelSteerParams) => Promise<CancelSteerResponse>;+ /** Escalates a queued steer to interrupt the running step. */+ armSteer: (params: ArmSteerParams) => Promise<ArmSteerResponse>;+ /** Reads the server's queued turns for a conversation, narrowed to known request ids. */+ listQueued: (+ conversationId: string,+ clientRequestIds?: string[],+ ) => Promise<AgentQueuedTurnReceipt[]>;+ /** Adds a turn to the server's queue behind the running generation. */+ enqueue: (input: EnqueueAgentQueuedTurnRequest) => Promise<AgentQueuedTurnReceipt>;+ /** Withdraws a queued turn the server has not admitted yet. */+ cancelQueued: (input: {+ conversationId: string;+ queuedTurnId: string;+ }) => Promise<AgentQueuedTurnReceipt>;+}diff --git a/client/src/hooks/Chat/useSteering.ts b/client/src/hooks/Chat/useSteering.tsindex bbd919f17b..eda745b02f 100644--- a/client/src/hooks/Chat/useSteering.ts+++ b/client/src/hooks/Chat/useSteering.ts@@ -26,9 +26,9 @@ import type {SettledQueuedTurnReceipt,} from '~/store/families';import type { AgentQueuedTurnReceipt, GenerationProtocolVersion } from '~/data-provider';-import type { DuringRunAction } from '~/Providers/ChatSettingsContext';import type { QueueSendLock } from '~/utils/queueIntent';import type { ExtendedFile, FileSetter } from '~/common';+import type { DuringRunAction } from './contract';import {useGetStartupConfig,useCancelSteerMutation,diff --git a/client/src/routes/ChatSettings.tsx b/client/src/routes/ChatSettings.tsxindex 7a62220cb0..c1f0bd88a7 100644--- a/client/src/routes/ChatSettings.tsx+++ b/client/src/routes/ChatSettings.tsx@@ -1,11 +1,13 @@import { useMemo } from 'react';import { useRecoilState, useRecoilValue, useResetRecoilState } from 'recoil';import type { ReactNode } from 'react';-import type { ChatSettings } from '~/Providers/ChatSettingsContext';+import type { ChatSettings } from '~/hooks/Chat/contract';+import { ChatTransportContext, defaultChatTransport } from '~/Providers/ChatTransportContext';import { ChatSettingsContext } from '~/Providers/ChatSettingsContext';import store from '~/store';-/** Supplies the chat's app-global preferences from the app's own settings store. */+/** Supplies the chat's app-global preferences from the app's own settings store, and the+ * transport its turns run over. */export default function ChatSettingsProvider({ children }: { children: ReactNode }) {const [duringRunDefaultAction, setDuringRunDefaultAction] = useRecoilState(store.duringRunDefaultAction,@@ -28,5 +30,9 @@ export default function ChatSettingsProvider({ children }: { children: ReactNode],);- return <ChatSettingsContext.Provider value={settings}>{children}</ChatSettingsContext.Provider>;+ return (+ <ChatTransportContext.Provider value={defaultChatTransport}>+ <ChatSettingsContext.Provider value={settings}>{children}</ChatSettingsContext.Provider>+ </ChatTransportContext.Provider>+ );}