diff --git a/packages/chat/src/components/subagents/__tests__/followed.spec.tsx b/packages/chat/src/components/subagents/__tests__/followed.spec.tsx index f798ae0c51..31a14e7926 100644 --- a/packages/chat/src/components/subagents/__tests__/followed.spec.tsx +++ b/packages/chat/src/components/subagents/__tests__/followed.spec.tsx @@ -60,6 +60,9 @@ const followedChat = (overrides: Partial = {}): FollowedChat => ({ isLoadingEarlier: false, isEarlierError: false, loadEarlier: () => undefined, + canSend: () => false, + send: () => false, + stop: () => Promise.resolve(), ...overrides, }); diff --git a/packages/chat/src/components/subagents/__tests__/panel.spec.tsx b/packages/chat/src/components/subagents/__tests__/panel.spec.tsx index 5cd400a1e7..b4345ccdf7 100644 --- a/packages/chat/src/components/subagents/__tests__/panel.spec.tsx +++ b/packages/chat/src/components/subagents/__tests__/panel.spec.tsx @@ -4711,6 +4711,9 @@ describe('SubagentThreadPanel', () => { isLoadingEarlier: false, isEarlierError: false, loadEarlier: () => undefined, + canSend: () => false, + send: () => false, + stop: () => Promise.resolve(), ...overrides, }); diff --git a/packages/chat/src/core/__tests__/apply.spec.ts b/packages/chat/src/core/__tests__/apply.spec.ts index 89123ab30d..d8375c3f44 100644 --- a/packages/chat/src/core/__tests__/apply.spec.ts +++ b/packages/chat/src/core/__tests__/apply.spec.ts @@ -935,6 +935,33 @@ describe('a turn whose shape the server states', () => { (part) => (part as { text?: string } | undefined)?.text, ); + it('settles a turn that streamed nothing onto the reply it was drawn with, not beside it', () => { + const { applyEvent } = createEventApplier(); + /** As a chat that sent the turn holds it: the message, and its reply under a padded id. */ + const drawn: TMessage = { ...reply('user-1_', []), parentMessageId: 'user-1' }; + const { context, read } = createList([user, drawn]); + + applyEvent(createdAs({ responseMessageId: 'resp-1' }), context); + applyEvent(final('Done at once'), context); + + expect(read().map((message) => message.messageId)).toEqual(['user-1', 'resp-1']); + expect(texts(read(), 'resp-1')).toEqual(['Done at once']); + }); + + it('leaves a reply the final frame does not replace where it is', () => { + const { applyEvent } = createEventApplier(); + const { context, read } = createList([user, reply('resp-0', ['An earlier answer'])]); + + /** A regeneration answers beside the saved reply; nothing of that reply is this turn's. */ + applyEvent(createdAs({ responseMessageId: 'resp-1', isRegenerate: true }), context); + applyEvent(opened('s0', 0), context); + applyEvent(token('s0', 'Again'), context); + applyEvent(final('Again'), context); + + expect(read().map((message) => message.messageId)).toEqual(['user-1', 'resp-0', 'resp-1']); + expect(texts(read(), 'resp-0')).toEqual(['An earlier answer']); + }); + it('draws a continued reply again from empty under its own id, not as a second answer', () => { const { applyEvent } = createEventApplier(); const { context, read } = createList([user, reply('resp-1', ['Stopped here'])]); diff --git a/packages/chat/src/core/__tests__/turns.spec.ts b/packages/chat/src/core/__tests__/turns.spec.ts index 9c0ce827cc..bdca666376 100644 --- a/packages/chat/src/core/__tests__/turns.spec.ts +++ b/packages/chat/src/core/__tests__/turns.spec.ts @@ -1,8 +1,8 @@ -import { Constants, ContentTypes, EModelEndpoint } from 'librechat-data-provider'; +import { Constants, ContentTypes, createPayload, EModelEndpoint } from 'librechat-data-provider'; import type { TMessage, TConversation } from 'librechat-data-provider'; -import type { BuildTurnInput } from '../turns'; +import type { SubmittedTurn, BuildTurnInput } from '../turns'; import type { ExtendedFile } from '../files'; -import { buildTurn, createRegenerate } from '../turns'; +import { buildTurn, createRegenerate, describeSubmittedTurn, prepareSubmittedTurn } from '../turns'; import { formatPastedText } from '../files'; const conversationId = 'conversation-1'; @@ -410,3 +410,71 @@ describe('createRegenerate', () => { expect(error).toHaveBeenCalled(); }); }); + +describe('a turn ask built, as a Chat takes it', () => { + /** The rows `ask` writes before its stream starts, as `useTurns` assembles them. */ + const submitted = (turn: ReturnType): SubmittedTurn => ({ + submission: turn.submission, + messages: turn.regenerateShaped + ? [...turn.submissionMessages, turn.initialResponse] + : [...turn.submissionMessages, turn.currentMsg, turn.initialResponse], + }); + + it('posts the body main chat posts for the submission, and shows the rows ask would have', () => { + const turn = submitted(buildTurn(input())); + + const prepared = prepareSubmittedTurn(turn); + + expect(prepared.payload).toEqual(createPayload(turn.submission).payload); + expect(prepared.payload).toMatchObject({ + endpoint: EModelEndpoint.agents, + text: 'Next question', + }); + expect(prepared.messages).toBe(turn.messages); + /** The reply the chat reports if the turn ends before the server names one. */ + expect(prepared.responseMessageId).toBe(turn.submission.initialResponse?.messageId); + expect(turn.messages[turn.messages.length - 1].messageId).toBe(prepared.responseMessageId); + }); + + it('is a submitted message with its text and the parent it answers', () => { + const turn = submitted(buildTurn(input())); + + expect(describeSubmittedTurn(turn)).toEqual({ + trigger: 'submit-message', + input: { + parts: [{ type: 'text', text: 'Next question' }], + metadata: { parentMessageId: 'response-1' }, + }, + }); + }); + + it('is a regeneration, or a continuation, of the reply the turn produces', () => { + const regenerated = submitted( + buildTurn( + input({ + params: { ...user }, + options: { isRegenerate: true, targetResponseMessageId: 'response-1' }, + }), + ), + ); + expect(describeSubmittedTurn(regenerated)).toEqual({ + trigger: 'regenerate-message', + messageId: regenerated.submission.initialResponse?.messageId, + }); + + /** A continuation is regenerate-shaped too; what it is called is decided first. */ + const continued = submitted( + buildTurn( + input({ + params: { ...user }, + options: { isContinued: true, isRegenerate: true, isEdited: true }, + latestMessage: response, + }), + ), + ); + expect(describeSubmittedTurn(continued)).toEqual({ + trigger: 'continue-message', + messageId: continued.submission.initialResponse?.messageId, + }); + }); +}); diff --git a/packages/chat/src/core/apply.ts b/packages/chat/src/core/apply.ts index 1812074188..fe30af9572 100644 --- a/packages/chat/src/core/apply.ts +++ b/packages/chat/src/core/apply.ts @@ -626,6 +626,19 @@ export function createEventApplier(init: EventApplierInit = {}): EventApplier { } else if (event.type === 'final') { /** The final frame is authoritative; a held copy of the stream must not land on top of it. */ drop(); + /** + * A turn that streamed nothing still holds the empty reply it was drawn with, under the + * id it had before the server named one. The frame's reply is that reply, so it takes its + * place instead of landing beside it. + */ + const drawn = submission?.initialResponse.messageId; + const settled = event.data.responseMessage?.messageId; + if (drawn != null && settled != null && drawn !== settled) { + const kept = ctx.messages.filter((message) => message.messageId !== drawn); + if (kept.length !== ctx.messages.length) { + ctx.write(kept); + } + } mergeFinalFrame(settledFinal(event.data, submission, ctx.messages), ctx); if (submission != null) { init.usage?.finalizeUsage(event.data, submission); diff --git a/packages/chat/src/core/turns/index.ts b/packages/chat/src/core/turns/index.ts index 5dd881368e..85c95d8922 100644 --- a/packages/chat/src/core/turns/index.ts +++ b/packages/chat/src/core/turns/index.ts @@ -7,3 +7,4 @@ export * from './title'; export * from './reset'; export * from './sender'; export * from './reasoning'; +export * from './prepare'; diff --git a/packages/chat/src/core/turns/prepare.ts b/packages/chat/src/core/turns/prepare.ts new file mode 100644 index 0000000000..95f0b34998 --- /dev/null +++ b/packages/chat/src/core/turns/prepare.ts @@ -0,0 +1,141 @@ +import { createPayload } from 'librechat-data-provider'; +import type { TMessage, TSubmission, EventSubmission } from 'librechat-data-provider'; +import type { ChatPrepareRequest, ChatPreparedRequest } from '../chat'; +import type { TResData } from './types'; +import { + mergeErrorMessages, + toStartGenerationError, + getStreamStartFailureText, + mergeRegenerateFinalMessages, + getExistingConversationAbortMessages, +} from '../resumable'; +import { STREAM_START_FAILED_METADATA_KEY } from '../regenerate'; +import { resolveErrorTurn } from './errors'; + +/** + * A turn as main chat's `ask` leaves it: the submission the turn builder made, and the rows + * `ask` shows while the request is in flight (the turn's user message and an empty reply, or + * the empty reply alone for a turn that reuses a saved user message). + */ +export type SubmittedTurn = { + submission: TSubmission; + messages: TMessage[]; +}; + +/** How a `Chat` is asked for a turn: the trigger it runs under, with what that trigger names. */ +export type SubmittedTurnRequest = Pick; + +/** + * Which of a `Chat`'s three requests a built turn is. A continuation and a regeneration name + * the reply they produce; a submitted message carries its text and the parent it answers. + */ +export function describeSubmittedTurn({ submission }: SubmittedTurn): SubmittedTurnRequest { + const { userMessage, initialResponse } = submission; + if (submission.isContinued === true) { + return { trigger: 'continue-message', messageId: initialResponse?.messageId }; + } + if (submission.isRegenerate === true) { + return { trigger: 'regenerate-message', messageId: initialResponse?.messageId }; + } + return { + trigger: 'submit-message', + input: { + parts: [{ type: 'text', text: userMessage.text ?? '' }], + metadata: { parentMessageId: userMessage.parentMessageId ?? undefined }, + }, + }; +} + +/** + * What a `Chat` posts and shows for a turn `ask` built: the body main chat's own start posts + * for that submission, so the server reads the same request whichever sent it, and the rows + * `ask` would have written, which the `Chat` writes itself so it can take them back when the + * server answers the start with a generation that is not this turn. + */ +export function prepareSubmittedTurn({ submission, messages }: SubmittedTurn): ChatPreparedRequest { + return { + payload: createPayload(submission).payload, + messages, + responseMessageId: submission.initialResponse?.messageId, + }; +} + +/** The turn as the handlers that settle it take it: with the reply `ask` always builds. */ +const settling = ({ submission }: SubmittedTurn): EventSubmission | undefined => + submission.initialResponse == null + ? undefined + : { ...submission, initialResponse: submission.initialResponse }; + +/** + * The list a turn leaves when it failed for good: the rows main chat's error handler writes. + * A start the server refused leaves the turn's message with the refusal as its reply, marked as + * a start that failed so the next message answers past it; a failure the stream reported keeps + * what had streamed and takes the failure as its last part. `current` is the list as the + * failure left it. Nothing when the turn has no reply to carry the failure. + */ +export function failedTurnMessages( + turn: SubmittedTurn, + current: TMessage[], + error: Error | undefined, +): TMessage[] | undefined { + const submission = settling(turn); + if (submission == null) { + return undefined; + } + const refused = toStartGenerationError(error)?.response; + const data = + refused == null + ? { text: error?.message ?? '' } + : { + text: getStreamStartFailureText(refused.data), + metadata: { [STREAM_START_FAILED_METADATA_KEY]: true }, + }; + const { errorResponse } = resolveErrorTurn({ + data: data as unknown as TResData, + submission, + getMessages: () => current, + isNewConversationRoute: false, + }); + return mergeErrorMessages({ ...submission, errorMessage: errorResponse }); +} + +/** + * The list a rerun leaves when its own stream ended it. While it ran, the list held only the + * branch being rerun: the reply it replaces and what followed that reply were set aside, as + * `ask` shows it. They are other branches of the same conversation, so they come back beside + * the new reply, as main chat's final handler puts them. Nothing for a turn that set none aside. + */ +export function rerunTurnMessages( + { submission }: SubmittedTurn, + response: TMessage | undefined, +): TMessage[] | undefined { + const { regenerateMessages, initialResponse } = submission; + if (submission.isRegenerate !== true || regenerateMessages == null) { + return undefined; + } + if (response == null) { + return regenerateMessages.filter((message) => message.messageId !== initialResponse?.messageId); + } + return mergeRegenerateFinalMessages({ + messages: regenerateMessages, + responseMessage: response, + initialResponseId: initialResponse?.messageId, + }); +} + +/** + * The list a turn leaves when it was stopped before the server kept anything of it: the rows + * from before the turn, as main chat's final handler restores them. Its message was never + * saved, so neither it nor the empty reply stays, and the words are the reader's to send again. + */ +export function withdrawnTurnMessages( + { submission }: SubmittedTurn, + current: TMessage[], +): TMessage[] { + return getExistingConversationAbortMessages({ + messages: submission.messages, + isRegenerate: submission.isRegenerate, + regenerateMessages: submission.regenerateMessages, + currentMessages: current, + }); +} diff --git a/packages/chat/src/react.ts b/packages/chat/src/react.ts index 0d0b6b5781..5904f3acfa 100644 --- a/packages/chat/src/react.ts +++ b/packages/chat/src/react.ts @@ -130,6 +130,8 @@ export { useFollowChat } from './react/follow'; export type { FollowChatOptions, FollowedChat } from './react/follow'; export { useFollowedContract } from './react/chat/followed'; export type { FollowedContractOptions } from './react/chat/followed'; +export { useDrivenContract } from './react/chat/driven'; +export type { DrivenContractOptions } from './react/chat/driven'; export { useSnapshot } from './react/snapshot'; export { ReactQueryChatState } from './react/state'; export type { Subscribe, SnapshotOptions } from './react/snapshot'; diff --git a/packages/chat/src/react/chat/__tests__/driven.spec.tsx b/packages/chat/src/react/chat/__tests__/driven.spec.tsx new file mode 100644 index 0000000000..7591c35631 --- /dev/null +++ b/packages/chat/src/react/chat/__tests__/driven.spec.tsx @@ -0,0 +1,746 @@ +/** + * @jest-environment jsdom + */ +import React, { useState } from 'react'; +import { createStore, Provider } from 'jotai'; +import { act, renderHook, waitFor } from '@testing-library/react'; +import { QueryClient, QueryClientProvider } from '@tanstack/react-query'; +import { + Constants, + StepTypes, + StepEvents, + ContentTypes, + EModelEndpoint, +} from 'librechat-data-provider'; +import type { TMessage, ChatEvent, TConversation } from 'librechat-data-provider'; +import type { ChatStream, ChatTransport, ChatSendRequest, ChatStreamStatus } from '../../../core'; +import type { ChatHost, TurnServices } from '../host'; +import type { ExtendedFile } from '../../../core'; +import { createTurnServices } from '../__fixtures__/services'; +import { showStopButtonByIndex } from '../../generation'; +import { isSubmittingFamily } from '../../submission'; +import { useDrivenContract } from '../driven'; +import { useFollowChat } from '../../follow'; +import { ChatHostProvider } from '../host'; +import { filesFamily } from '../../pane'; + +const conversationId = 'conversation-1'; +const SIDE_PANE = 7; + +const user: TMessage = { + messageId: 'user-1', + parentMessageId: Constants.NO_PARENT as string, + conversationId, + text: 'Hello', + sender: 'User', + isCreatedByUser: true, + manualSkills: ['skill-a'], +}; + +const reply = (messageId: string, text: string, parentMessageId = 'user-1'): TMessage => ({ + messageId, + parentMessageId, + conversationId, + text: '', + sender: 'Assistant', + isCreatedByUser: false, + content: [{ type: ContentTypes.TEXT, text }], +}); + +const conversation: TConversation = { + conversationId, + endpoint: EModelEndpoint.agents, + agent_id: 'agent-1', + title: null, + createdAt: '2026-10-28T00:00:00.000Z', + updatedAt: '2026-10-28T00:00:00.000Z', +}; + +type TestStream = ChatStream & { push(event: ChatEvent): void; closed: boolean }; + +const createStream = (): TestStream => { + const queued: ChatEvent[] = []; + let wake: (() => void) | undefined; + const stream: TestStream = { + streamId: conversationId, + generationCreatedAt: 3000, + closed: false, + push(event) { + queued.push(event); + wake?.(); + }, + close() { + stream.closed = true; + wake?.(); + }, + async *[Symbol.asyncIterator]() { + while (true) { + while (queued.length > 0) { + yield queued.shift() as ChatEvent; + } + if (stream.closed) { + return; + } + await new Promise((resolve) => { + wake = resolve; + }); + } + }, + }; + return stream; +}; + +/** A transport that can start a generation: every send and every attachment opens a stream. */ +const createTransport = () => { + const streams: TestStream[] = []; + let status: ChatStreamStatus = { active: false }; + const open = (): TestStream => { + const stream = createStream(); + streams.push(stream); + return stream; + }; + const sendMessages = jest.fn( + async (_request: ChatSendRequest): ReturnType => ({ + status: 'stream', + stream: open(), + }), + ); + const abort = jest.fn(async () => ({ success: true })); + const reconnectToStream = jest.fn(async () => (status.active ? open() : null)); + const transport: ChatTransport = { + sendMessages, + abort, + reconnectToStream, + getStatus: async () => status, + }; + return { + transport, + streams, + sendMessages, + abort, + reconnectToStream, + setStatus: (next: ChatStreamStatus) => { + status = next; + }, + }; +}; + +const runStep = (id: string, runId: string) => ({ + id, + runId, + index: 0, + type: StepTypes.MESSAGE_CREATION, + stepDetails: { type: StepTypes.MESSAGE_CREATION, message_creation: { message_id: id } }, + usage: null, +}); + +const created = (message: TMessage, responseMessageId: string, turn: object = {}): ChatEvent => + ({ + type: 'created', + data: { created: true, message, turn: { responseMessageId, ...turn } }, + }) as ChatEvent; + +const opened = (runId: string): ChatEvent => + ({ + type: 'step', + data: { event: StepEvents.ON_RUN_STEP, data: runStep('s0', runId) }, + }) as ChatEvent; + +const token = (text: string): ChatEvent => + ({ + type: 'step', + data: { + event: StepEvents.ON_MESSAGE_DELTA, + data: { id: 's0', delta: { content: [{ type: ContentTypes.TEXT, text }] } }, + }, + }) as ChatEvent; + +const final = (requestMessage: TMessage, responseMessage: TMessage): ChatEvent => + ({ + type: 'final', + data: { final: true, conversation: { conversationId }, requestMessage, responseMessage }, + }) as ChatEvent; + +const ids = (messages: TMessage[] | undefined) => + (messages ?? []).map((message) => message.messageId); +const textOf = (messages: TMessage[] | undefined, messageId: string) => + (messages?.find((message) => message.messageId === messageId)?.content ?? []).map( + (part) => (part as { text?: string } | undefined)?.text, + ); + +/** Lets promises settle and the frame that writes streamed tokens run. */ +const settle = () => + act(async () => { + await new Promise((resolve) => setTimeout(resolve, 40)); + }); + +function setup({ + stored = [user, reply('response-1', 'Hi')], + services = createTurnServices(), + canSend, +}: { stored?: TMessage[]; services?: TurnServices; canSend?: () => boolean } = {}) { + const queryClient = new QueryClient(); + const store = createStore(); + const harness = createTransport(); + const loadMessages = jest.fn(async () => stored); + const onWithdrawn = jest.fn(); + const host: ChatHost = { + usePaneConversation: () => { + const [held, setConversation] = useState(conversation); + return { conversation: held, setConversation }; + }, + useNewConversation: () => () => undefined, + useTurnServices: () => services, + useAbortStream: () => () => Promise.reject(new Error('unused')), + useMessageCache: () => { + throw new Error('A driven chat reads its own messages'); + }, + useFeedbackEnabled: () => false, + useMessages: () => ({ messages: null, isLoading: false, isFetching: false }), + }; + const wrapper = ({ children }: { children: React.ReactNode }) => ( + + + {children} + + + ); + const view = renderHook( + ({ live }: { live: boolean }) => { + const followed = useFollowChat({ + id: conversationId, + transport: harness.transport, + loadMessages, + live, + endpoint: conversation.endpoint, + onWithdrawn, + }); + const contract = useDrivenContract({ + id: conversationId, + followed: canSend == null ? followed : { ...followed, canSend }, + index: SIDE_PANE, + }); + return { followed, contract }; + }, + { wrapper, initialProps: { live: false } }, + ); + const ready = () => waitFor(() => expect(view.result.current.followed.isLoading).toBe(false)); + return { ...view, ...harness, store, services, loadMessages, onWithdrawn, ready }; +} + +describe('useDrivenContract', () => { + it('sends the turn ask builds through the chat, and shows the generation it starts', async () => { + const { result, sendMessages, streams, store, loadMessages, ready } = setup(); + await ready(); + + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + + /** The body main chat posts for this turn: a new message under the tail of the branch. */ + const { trigger, payload } = sendMessages.mock.calls[0][0]; + expect(trigger).toBe('submit-message'); + expect(payload).toMatchObject({ + endpoint: EModelEndpoint.agents, + conversationId, + text: 'Next question', + parentMessageId: 'response-1', + }); + /** The rows ask shows meanwhile, written by the chat: the message and its empty reply. */ + const shown = result.current.contract.getMessages() ?? []; + expect(ids(shown).slice(0, 2)).toEqual(['user-1', 'response-1']); + expect(shown[2]).toMatchObject({ isCreatedByUser: true, text: 'Next question' }); + expect(shown[3]).toMatchObject({ isCreatedByUser: false, parentMessageId: shown[2].messageId }); + expect(result.current.contract.isSubmitting).toBe(true); + expect(store.get(isSubmittingFamily(SIDE_PANE))).toBe(true); + + const sent = shown[2]; + act(() => { + streams[0].push(created(sent, 'response-2')); + streams[0].push(opened('response-2')); + streams[0].push(token('Answer')); + }); + await settle(); + expect(ids(result.current.contract.getMessages())).toEqual([ + 'user-1', + 'response-1', + sent.messageId, + 'response-2', + ]); + expect(textOf(result.current.contract.getMessages(), 'response-2')).toEqual(['Answer']); + + act(() => streams[0].push(final(sent, reply('response-2', 'Answer', sent.messageId)))); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + + /** The turn's own final frame is its result: nothing is read back over it. */ + expect(loadMessages).toHaveBeenCalledTimes(1); + expect(store.get(isSubmittingFamily(SIDE_PANE))).toBe(false); + expect(store.get(showStopButtonByIndex(SIDE_PANE))).toBe(false); + expect(result.current.contract.readOnly).toBeUndefined(); + }); + + it('regenerates and continues a reply as main chat does', async () => { + const { result, sendMessages, streams, ready } = setup(); + await ready(); + + act(() => { + result.current.contract.regenerate({ parentMessageId: 'user-1' }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + expect(sendMessages.mock.calls[0][0]).toMatchObject({ + trigger: 'regenerate-message', + payload: { isRegenerate: true, text: 'Hello', manualSkills: ['skill-a'] }, + }); + /** No second user message: the empty reply is drawn under the one replayed. */ + const rerun = result.current.contract.getMessages() ?? []; + expect(rerun.filter((message) => message.isCreatedByUser)).toHaveLength(1); + expect(rerun[rerun.length - 1]).toMatchObject({ parentMessageId: 'user-1', content: [] }); + + act(() => streams[0].push(final(user, reply('response-1b', 'Again')))); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + + act(() => { + result.current.contract.handleContinue({ + preventDefault: () => undefined, + } as React.MouseEvent); + }); + await waitFor(() => expect(streams).toHaveLength(2)); + expect(sendMessages.mock.calls[1][0]).toMatchObject({ + trigger: 'continue-message', + payload: { isContinued: true, text: 'Hello' }, + }); + }); + + it('stops the generation the chat is running', async () => { + const { result, abort, streams, ready } = setup(); + await ready(); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + + await act(async () => { + void result.current.contract.stopGenerating(); + await Promise.resolve(); + }); + + await waitFor(() => expect(abort).toHaveBeenCalledTimes(1)); + expect(abort.mock.calls[0]).toMatchObject([ + { endpoint: EModelEndpoint.agents, streamId: conversationId, generationCreatedAt: 3000 }, + ]); + }); + + it('attaches again to a turn whose stream dropped, then takes the stored result', async () => { + const { result, streams, reconnectToStream, setStatus, loadMessages, ready } = setup(); + await ready(); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + const sent = (result.current.contract.getMessages() ?? [])[2]; + act(() => { + streams[0].push(created(sent, 'response-2')); + streams[0].push(opened('response-2')); + streams[0].push(token('Half')); + }); + await settle(); + + /** The stream ends with no final frame while the generation runs on. */ + setStatus({ active: true, streamId: conversationId, createdAt: 3000 }); + const { chat, canSend } = result.current.followed; + const free: boolean[] = []; + const unsubscribe = chat?.subscribe(() => free.push(canSend())); + act(() => streams[0].close()); + await waitFor(() => expect(reconnectToStream).toHaveBeenCalledTimes(1), { timeout: 4000 }); + /** + * Still the reader's turn in flight, so nothing else may be sent over it: not once the page + * shows it attached again, and not in the moments between, when the chat reads as stopped. + */ + unsubscribe?.(); + expect(free.length).toBeGreaterThan(0); + expect(free).not.toContain(true); + expect(result.current.contract.isSubmitting).toBe(true); + expect(textOf(result.current.contract.getMessages(), 'response-2')).toEqual(['Half']); + + const stored = [ + user, + reply('response-1', 'Hi'), + sent, + reply('response-2', 'Whole', sent.messageId), + ]; + loadMessages.mockResolvedValue(stored); + setStatus({ active: false }); + act(() => streams[1].push(final(sent, reply('response-2', 'Whole', sent.messageId)))); + await waitFor(() => expect(loadMessages).toHaveBeenCalledTimes(2)); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + expect(textOf(result.current.contract.getMessages(), 'response-2')).toEqual(['Whole']); + }); + + it('reads the stored messages when the server had already run the turn', async () => { + const { result, sendMessages, loadMessages, ready } = setup(); + await ready(); + const stored = [user, reply('response-1', 'Hi'), reply('response-2', 'Already answered')]; + loadMessages.mockResolvedValue(stored); + sendMessages.mockResolvedValueOnce({ + status: 'settled', + conversationId, + generationProtocolVersion: 2, + }); + + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + + await waitFor(() => expect(loadMessages).toHaveBeenCalledTimes(2)); + await waitFor(() => expect(ids(result.current.contract.getMessages())).toEqual(ids(stored))); + expect(result.current.contract.isSubmitting).toBe(false); + }); + + it('refuses a turn while a generation it did not start is running, leaving the composer as it was', async () => { + const { result, sendMessages, streams, setStatus, store, services, rerender, ready } = setup(); + await ready(); + const staged = new Map([ + ['file-1', { file_id: 'file-1', progress: 1, size: 1 }], + ]); + act(() => store.set(filesFamily(SIDE_PANE), staged)); + + setStatus({ active: true, streamId: conversationId, createdAt: 2000 }); + rerender({ live: true }); + await waitFor(() => expect(streams).toHaveLength(1)); + expect(result.current.contract.isSubmitting).toBe(true); + + let asked: unknown; + act(() => { + asked = result.current.contract.ask({ text: 'Next question' }); + }); + + expect(asked).toBe(false); + expect(sendMessages).not.toHaveBeenCalled(); + expect(services.releaseFiles).not.toHaveBeenCalled(); + expect(store.get(filesFamily(SIDE_PANE))).toBe(staged); + }); + + it('asks the chat, not the last render, whether a turn can be taken', async () => { + /** The render says the conversation is free; the chat, asked at that instant, says it is not. */ + const { result, sendMessages, store, services, ready } = setup({ canSend: () => false }); + await ready(); + const staged = new Map([ + ['file-1', { file_id: 'file-1', progress: 1, size: 1 }], + ]); + act(() => store.set(filesFamily(SIDE_PANE), staged)); + expect(result.current.contract.isSubmitting).toBe(false); + + let asked: unknown; + act(() => { + asked = result.current.contract.ask({ text: 'Next question' }); + }); + + expect(asked).toBe(false); + expect(sendMessages).not.toHaveBeenCalled(); + expect(services.releaseFiles).not.toHaveBeenCalled(); + expect(store.get(filesFamily(SIDE_PANE))).toBe(staged); + expect(store.get(isSubmittingFamily(SIDE_PANE))).toBe(false); + }); + + it('takes no turn while the stored messages are being read back', async () => { + const { result, sendMessages, loadMessages, ready } = setup(); + await ready(); + const stored = [user, reply('response-1', 'Hi'), reply('response-2', 'Already answered')]; + let land: (messages: TMessage[]) => void = () => undefined; + loadMessages.mockReturnValue( + new Promise((resolve) => { + land = resolve; + }), + ); + sendMessages.mockResolvedValueOnce({ + status: 'settled', + conversationId, + generationProtocolVersion: 2, + }); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(loadMessages).toHaveBeenCalledTimes(2)); + await settle(); + + /** The turn is over and nothing streams, but the list is about to be replaced. */ + expect(result.current.followed.status).toBe('ready'); + expect(result.current.followed.canSend()).toBe(false); + let asked: unknown; + act(() => { + asked = result.current.contract.ask({ text: 'Too soon' }); + }); + expect(asked).toBe(false); + expect(sendMessages).toHaveBeenCalledTimes(1); + + await act(async () => land(stored)); + await waitFor(() => expect(result.current.followed.canSend()).toBe(true)); + expect(ids(result.current.contract.getMessages())).toEqual(ids(stored)); + }); + + it('leaves a refused start as the reply to its message, and takes the next turn', async () => { + const { result, sendMessages, streams, ready } = setup(); + await ready(); + sendMessages.mockRejectedValueOnce( + Object.assign(new Error('Request failed with status code 403'), { + response: { status: 403, data: { message: 'Not allowed' }, headers: {} }, + }), + ); + + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + await settle(); + + /** Main chat's rows for a start the server refused: the message, and the refusal under it. */ + const failed = result.current.contract.getMessages() ?? []; + expect(ids(failed).slice(0, 2)).toEqual(['user-1', 'response-1']); + expect(failed).toHaveLength(4); + expect(failed[2]).toMatchObject({ isCreatedByUser: true, text: 'Next question' }); + expect(failed[3]).toMatchObject({ + error: true, + parentMessageId: failed[2].messageId, + metadata: { streamStartFailed: true }, + }); + expect(failed[3].text).toContain('Not allowed'); + + /** Not a reply still on its way, so the conversation takes another message. */ + let asked: unknown; + act(() => { + asked = result.current.contract.ask({ text: 'Again' }); + }); + expect(asked).not.toBe(false); + await waitFor(() => expect(streams).toHaveLength(1)); + expect(sendMessages.mock.calls[1][0].payload).toMatchObject({ text: 'Again' }); + }); + + it('gives a rerun back the branches it set aside once its reply is in', async () => { + const later: TMessage = { + ...user, + messageId: 'user-2', + parentMessageId: 'response-1', + text: 'And then?', + }; + const stored = [user, reply('response-1', 'Hi'), later, reply('response-2', 'Then', 'user-2')]; + const { result, streams, loadMessages, ready } = setup({ stored }); + await ready(); + + act(() => { + result.current.contract.regenerate({ + messageId: 'response-1', + parentMessageId: 'user-1', + isCreatedByUser: false, + }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + /** While it runs, only the branch being rerun is on screen, as ask shows it. */ + expect(ids(result.current.contract.getMessages())).not.toContain('response-2'); + + act(() => streams[0].push(final(user, reply('response-1b', 'Again')))); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + await settle(); + + const settled = ids(result.current.contract.getMessages()); + expect(settled).toEqual( + expect.arrayContaining(['user-1', 'response-1', 'user-2', 'response-2']), + ); + expect(settled).toContain('response-1b'); + expect(settled.some((id) => id.endsWith('_'))).toBe(false); + /** Restored from what the turn set aside, not by reading the conversation again. */ + expect(loadMessages).toHaveBeenCalledTimes(1); + }); + + it('stops a generation it did not start, on the endpoint the conversation runs on', async () => { + const { result, abort, streams, setStatus, rerender, ready } = setup(); + await ready(); + setStatus({ active: true, streamId: conversationId, createdAt: 2000 }); + rerender({ live: true }); + await waitFor(() => expect(streams).toHaveLength(1)); + await settle(); + + let refusal: unknown; + await act(async () => { + result.current.contract.stopGenerating().catch((error: unknown) => { + refusal = error; + }); + await Promise.resolve(); + }); + + await waitFor(() => expect(abort).toHaveBeenCalledTimes(1)); + expect(refusal).toBeUndefined(); + expect(abort.mock.calls[0]).toMatchObject([{ endpoint: EModelEndpoint.agents }]); + }); + + it('reads the conversation back when the server answered a rerun with a newer generation', async () => { + const { result, sendMessages, streams, reconnectToStream, setStatus, loadMessages, ready } = + setup(); + await ready(); + /** Someone else's turn got there first: the start names the generation that replaced it. */ + setStatus({ active: true, streamId: conversationId, createdAt: 4000 }); + sendMessages.mockResolvedValueOnce({ + status: 'replaced', + conversationId, + streamId: conversationId, + generationCreatedAt: 4000, + generationProtocolVersion: 2, + }); + + act(() => { + result.current.contract.regenerate({ + messageId: 'response-1', + parentMessageId: 'user-1', + isCreatedByUser: false, + }); + }); + await waitFor(() => expect(reconnectToStream).toHaveBeenCalledTimes(1)); + await waitFor(() => expect(streams).toHaveLength(1)); + + const theirs: TMessage = { + ...user, + messageId: 'user-2', + parentMessageId: 'response-1', + text: 'Theirs', + }; + const stored = [user, reply('response-1', 'Hi'), theirs, reply('response-2', 'Done', 'user-2')]; + loadMessages.mockResolvedValue(stored); + setStatus({ active: false }); + act(() => streams[0].push(final(theirs, reply('response-2', 'Done', 'user-2')))); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + await waitFor(() => expect(loadMessages).toHaveBeenCalledTimes(2)); + await settle(); + + /** Their turn whole, and the reply the rerun never replaced: not this turn's snapshot. */ + expect(ids(result.current.contract.getMessages())).toEqual(ids(stored)); + }); + + it('stops a turn the server has not answered yet by its conversation, and does not follow it', async () => { + const { result, sendMessages, abort, reconnectToStream, loadMessages, ready } = setup(); + await ready(); + /** The start never answers; cancelling it leaves a generation that may be running unnamed. */ + sendMessages.mockImplementationOnce( + ({ abortSignal }) => + new Promise((_resolve, reject) => { + abortSignal?.addEventListener('abort', () => reject(abortSignal.reason), { once: true }); + }), + ); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(sendMessages).toHaveBeenCalledTimes(1)); + + await act(async () => { + await result.current.contract.stopGenerating(); + }); + + expect(abort).toHaveBeenCalledWith({ chatId: conversationId, endpoint: EModelEndpoint.agents }); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + await waitFor(() => expect(loadMessages).toHaveBeenCalledTimes(2)); + /** The reader's stop is not a dropped stream: nothing is attached to again. */ + expect(reconnectToStream).not.toHaveBeenCalled(); + expect(ids(result.current.contract.getMessages())).toEqual(['user-1', 'response-1']); + }); + + it('stops by name a generation that appears only after the stop by conversation', async () => { + const { result, sendMessages, abort, setStatus, ready } = setup(); + await ready(); + sendMessages.mockImplementationOnce( + ({ abortSignal }) => + new Promise((_resolve, reject) => { + abortSignal?.addEventListener('abort', () => reject(abortSignal.reason), { once: true }); + }), + ); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(sendMessages).toHaveBeenCalledTimes(1)); + setStatus({ active: true, streamId: conversationId, createdAt: 5000 }); + + await act(async () => { + await result.current.contract.stopGenerating(); + }); + + expect(abort.mock.calls).toEqual([ + [{ chatId: conversationId, endpoint: EModelEndpoint.agents }], + [ + { + chatId: conversationId, + endpoint: EModelEndpoint.agents, + streamId: conversationId, + generationCreatedAt: 5000, + }, + ], + ]); + }); + + it('takes back a turn stopped before the server kept anything of it', async () => { + const { result, sendMessages, streams, onWithdrawn, loadMessages, ready } = setup(); + await ready(); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + expect(result.current.contract.getMessages()).toHaveLength(4); + + /** What the server sends for a stop that landed before it had saved the turn. */ + act(() => + streams[0].push({ + type: 'final', + data: { + final: true, + earlyAbort: true, + conversation: { conversationId }, + requestMessage: null, + responseMessage: null, + }, + } as unknown as ChatEvent), + ); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + await settle(); + + /** The rows from before the turn: no message that was never saved, no reply still to come. */ + expect(ids(result.current.contract.getMessages())).toEqual(['user-1', 'response-1']); + expect(loadMessages).toHaveBeenCalledTimes(1); + /** The words were the reader's, and are offered back. */ + expect(onWithdrawn).toHaveBeenCalledTimes(1); + expect(onWithdrawn.mock.calls[0][0].submission.userMessage.text).toBe('Next question'); + + let asked: unknown; + act(() => { + asked = result.current.contract.ask({ text: 'Again' }); + }); + expect(asked).not.toBe(false); + await waitFor(() => expect(sendMessages).toHaveBeenCalledTimes(2)); + expect(sendMessages.mock.calls[1][0].payload).toMatchObject({ + text: 'Again', + parentMessageId: 'response-1', + }); + }); + + it('reads the conversation back when a turn ends with its reply still unanswered', async () => { + const { result, sendMessages, streams, loadMessages, ready } = setup(); + await ready(); + act(() => { + result.current.contract.ask({ text: 'Next question' }); + }); + await waitFor(() => expect(streams).toHaveLength(1)); + const sent = (result.current.contract.getMessages() ?? [])[2]; + const stored = [user, reply('response-1', 'Hi'), sent]; + loadMessages.mockResolvedValue(stored); + + /** A stop the stream reports with no rows of its own: nothing fills the empty reply. */ + act(() => streams[0].push({ type: 'abort' } as ChatEvent)); + await waitFor(() => expect(result.current.contract.isSubmitting).toBe(false)); + await waitFor(() => expect(loadMessages).toHaveBeenCalledTimes(2)); + await settle(); + + expect(ids(result.current.contract.getMessages())).toEqual(ids(stored)); + let asked: unknown; + act(() => { + asked = result.current.contract.ask({ text: 'Again' }); + }); + expect(asked).not.toBe(false); + await waitFor(() => expect(sendMessages).toHaveBeenCalledTimes(2)); + }); +}); diff --git a/packages/chat/src/react/chat/driven.ts b/packages/chat/src/react/chat/driven.ts new file mode 100644 index 0000000000..92bfdc421b --- /dev/null +++ b/packages/chat/src/react/chat/driven.ts @@ -0,0 +1,224 @@ +import { useRef, useMemo, useState, useEffect, useCallback } from 'react'; +import { useAtom, useSetAtom } from 'jotai'; +import type { TMessage, TSubmission } from 'librechat-data-provider'; +import type { MouseEvent, SetStateAction } from 'react'; +import type { ChatTurnsParams, TurnServices } from './host'; +import type { ChatContract } from './contract'; +import type { FollowedChat } from '../follow'; +import { useBranchTailId, useBranchTailGetter, useBranchTailSelect } from '../latest'; +import { presetFamily, filesFamily, optionSettingsFamily } from '../pane'; +import { abortScrollFamily, showStopButtonByIndex } from '../generation'; +import { siblingIdxFamily, siblingKey } from '../thread'; +import { useLiveAttachments } from '../stream/atoms'; +import { isSubmittingFamily } from '../submission'; +import { getMappingOptions } from '../../core'; +import { useTurnHandlers } from './handlers'; +import { useChatHost } from './host'; +import { useTurns } from './turns'; + +export type DrivenContractOptions = { + /** The conversation driven, which keys its messages in the cache. */ + id: string; + /** The conversation as `useFollowChat` holds it; its `send` is where this chat's turns go. */ + followed: FollowedChat; + /** + * The pane the host keys this chat's state by: its conversation, composer files and settings. + * One no other chat renders into, or the two would share that state. + */ + index: number; +}; + +const ignore = (): void => undefined; + +/** Where the tail sits in its thread, which a streamed delta does not change. */ +const pickTailPlace = (tail: TMessage | null): Pick | null => + tail && { depth: tail.depth, parentMessageId: tail.parentMessageId }; + +/** + * The chat contract of a conversation the reader drives through a core `Chat`. It is the + * contract `useChatHelpers` builds for a pane, from the same host and with the same `ask`, with + * the three things a pane's stream session owns taken from the `Chat` instead: the messages are + * the chat's, a turn `ask` built is sent by the chat, and a stop is the chat's. + * + * So a turn is refused, built and shown as main chat's is (the composer's files, skills, quotes + * and reasoning, an edit, a rerun, a continuation), and differs only in what carries it. + */ +export function useDrivenContract({ id, followed, index }: DrivenContractOptions): ChatContract { + const host = useChatHost(); + const { chat, status, isLoading, isFollowing, canSend, send, stop } = followed; + const [files, setFiles] = useAtom(filesFamily(index)); + const [filesLoading, setFilesLoading] = useState(false); + const newConversation = host.useNewConversation(index); + const { conversation, setConversation } = host.usePaneConversation(index); + + /** A generation started here or anywhere else occupies the conversation alike. */ + const isSubmitting = status === 'submitted' || status === 'streaming' || isFollowing; + const setIsSubmitting = useSetAtom(isSubmittingFamily(index)); + const setShowStopButton = useSetAtom(showStopButtonByIndex(index)); + /** + * The pane's own flags, which `ask` raises and the composer reads, follow the chat: nothing + * else lowers them here, where no stream session runs. + */ + useEffect(() => { + setIsSubmitting(isSubmitting); + setShowStopButton(isSubmitting); + }, [isSubmitting, setIsSubmitting, setShowStopButton]); + + const tailPlace = useBranchTailSelect(id, id, pickTailPlace); + const getLatestMessage = useBranchTailGetter(id, id); + const latestMessageId = useBranchTailId(id, id) ?? undefined; + const latestMessageDepth = tailPlace?.depth; + const setSiblingIdx = useSetAtom(siblingIdxFamily(siblingKey(tailPlace?.parentMessageId))); + + const getMessages = useCallback( + (targetConversationId?: string | null) => + targetConversationId == null || targetConversationId === id ? chat?.messages : undefined, + [chat, id], + ); + const setMessages = useCallback( + (messages: TMessage[]) => void chat?.setMessages(messages), + [chat], + ); + const subscribeMessages = useCallback( + (onChange: () => void) => chat?.subscribe(onChange) ?? ignore, + [chat], + ); + const getMessagesRevision = useCallback(() => chat?.version ?? 0, [chat]); + + /** + * `ask` writes the rows a turn shows and then publishes its submission. Here the chat writes + * them, so it can take them back, and so the rows are held until the submission they belong + * to arrives in the same call. + */ + const shown = useRef(); + const holdRows = useCallback((messages: TMessage[]) => { + shown.current = messages; + }, []); + const sendSubmission = useCallback( + (next: SetStateAction) => { + const submission = typeof next === 'function' ? next(null) : next; + const messages = shown.current; + shown.current = undefined; + if (submission == null) { + return; + } + if (messages == null || !send({ submission, messages })) { + /** The chat took nothing, so the flags `ask` raised for the turn come back down. */ + setIsSubmitting(false); + setShowStopButton(false); + } + }, + [send, setIsSubmitting, setShowStopButton], + ); + + const turnsParams: ChatTurnsParams = { + index, + files, + setFiles, + getMessages, + setMessages: holdRows, + isSubmitting: isSubmitting || isLoading, + conversation, + getLatestMessage, + setSubmission: sendSubmission, + setConversation, + }; + const appServices = host.useTurnServices(turnsParams); + /** Asked first, before `ask` drains the composer for a turn the chat would not take. */ + const services: TurnServices = { + ...appServices, + canSubmit: (submitted) => canSend() && appServices.canSubmit?.(submitted) !== false, + }; + const turns = useTurns(turnsParams, services); + const { ask, regenerate, handleRegenerate, handleContinue } = useTurnHandlers({ + ask: turns.ask, + regenerate: turns.regenerate, + getMessages, + getLatestMessage, + setSiblingIdx, + }); + + const stopGenerating = stop; + const handleStopGenerating = useCallback( + (e: MouseEvent) => { + e.preventDefault(); + void stopGenerating(); + }, + [stopGenerating], + ); + + const [preset, setPreset] = useAtom(presetFamily(index)); + const [abortScroll, setAbortScroll] = useAtom(abortScrollFamily(index)); + const [optionSettings, setOptionSettings] = useAtom(optionSettingsFamily(index)); + const feedbackEnabled = host.useFeedbackEnabled(); + + return useMemo( + () => ({ + index, + conversation, + setConversation, + newConversation, + preset, + setPreset, + optionSettings, + setOptionSettings, + getMessages, + messagesKey: id, + setMessages, + subscribeMessages, + getMessagesRevision, + setSiblingIdx, + latestMessageId, + latestMessageDepth, + useLiveAttachments, + ask, + regenerate, + isSubmitting, + setIsSubmitting, + handleRegenerate, + handleContinue, + stopGenerating, + handleStopGenerating, + abortScroll, + setAbortScroll, + files, + setFiles, + filesLoading, + setFilesLoading, + feedbackEnabled, + getMappingOptions, + }), + [ + id, + index, + conversation, + setConversation, + newConversation, + preset, + setPreset, + optionSettings, + setOptionSettings, + getMessages, + setMessages, + subscribeMessages, + getMessagesRevision, + setSiblingIdx, + latestMessageId, + latestMessageDepth, + ask, + regenerate, + isSubmitting, + setIsSubmitting, + handleRegenerate, + handleContinue, + stopGenerating, + handleStopGenerating, + abortScroll, + setAbortScroll, + files, + setFiles, + filesLoading, + feedbackEnabled, + ], + ); +} diff --git a/packages/chat/src/react/chat/handlers.ts b/packages/chat/src/react/chat/handlers.ts new file mode 100644 index 0000000000..4ebabb2adc --- /dev/null +++ b/packages/chat/src/react/chat/handlers.ts @@ -0,0 +1,97 @@ +import { useRef, useCallback } from 'react'; +import type { MouseEvent } from 'react'; +import type { ChatContract } from './contract'; +import type { ChatTurnsParams } from './host'; + +export type TurnHandlersParams = Pick & + Pick; + +export type TurnHandlers = Pick< + ChatContract, + 'ask' | 'regenerate' | 'handleRegenerate' | 'handleContinue' +>; + +/** + * The turn members of a chat contract over one pane's `ask` and `regenerate`, whichever way the + * pane's turns reach the server. `ask` and `regenerate` are rebuilt on every render; the ones + * returned keep one identity and call the latest, so the rows that hold them sit out the stream. + */ +export function useTurnHandlers({ + ask: latestAsk, + regenerate: latestRegenerate, + getMessages, + getLatestMessage, + setSiblingIdx, +}: TurnHandlersParams): TurnHandlers { + const askRef = useRef(latestAsk); + askRef.current = latestAsk; + const ask: ChatContract['ask'] = useCallback((...args) => askRef.current(...args), []); + + const regenerateRef = useRef(latestRegenerate); + regenerateRef.current = latestRegenerate; + const regenerate: ChatContract['regenerate'] = useCallback( + (...args) => regenerateRef.current(...args), + [], + ); + + /** The setter is rebound whenever the tail's parent changes (every turn); the + * ref keeps `handleContinue` referentially stable so rows do not re-render. */ + const setSiblingIdxRef = useRef(setSiblingIdx); + setSiblingIdxRef.current = setSiblingIdx; + + const continueGeneration = useCallback(() => { + const currentLatest = getLatestMessage(); + if (!currentLatest) { + console.error('Failed to regenerate the message: latestMessage not found.'); + return; + } + + const messages = getMessages(); + + const parentMessage = messages?.find( + (element) => element.messageId == currentLatest.parentMessageId, + ); + + if (parentMessage && parentMessage.isCreatedByUser) { + ask( + { ...parentMessage }, + { + isContinued: true, + isRegenerate: true, + isEdited: true, + overrideManualSkills: parentMessage.manualSkills, + overrideQuotes: parentMessage.quotes, + overrideReasoning: parentMessage.reasoningOverride ?? null, + }, + ); + } else { + console.error( + 'Failed to regenerate the message: parentMessage not found, or not created by user.', + ); + } + }, [getLatestMessage, getMessages, ask]); + + const handleRegenerate = useCallback( + (e: MouseEvent) => { + e.preventDefault(); + const parentMessageId = getLatestMessage()?.parentMessageId ?? ''; + if (!parentMessageId) { + console.error('Failed to regenerate the message: parentMessageId not found.'); + return; + } + regenerate({ parentMessageId }); + }, + [getLatestMessage, regenerate], + ); + + const handleContinue = useCallback( + (e: MouseEvent) => { + e.preventDefault(); + continueGeneration(); + setSiblingIdxRef.current(0); + }, + [continueGeneration], + ); + + return { ask, regenerate, handleRegenerate, handleContinue }; +} diff --git a/packages/chat/src/react/chat/helpers.ts b/packages/chat/src/react/chat/helpers.ts index 8663d075e8..84fb7bb431 100644 --- a/packages/chat/src/react/chat/helpers.ts +++ b/packages/chat/src/react/chat/helpers.ts @@ -1,4 +1,4 @@ -import { useCallback, useMemo, useRef, useState } from 'react'; +import { useCallback, useMemo, useState } from 'react'; import { useAtom, useSetAtom } from 'jotai'; import type { TMessage } from 'librechat-data-provider'; import type { MouseEvent } from 'react'; @@ -11,6 +11,7 @@ import { useLiveAttachments } from '../stream/atoms'; import { abortScrollFamily } from '../generation'; import { getMappingOptions } from '../../core'; import { getMessageCacheIds } from './cache'; +import { useTurnHandlers } from './handlers'; import { useStopGenerating } from './stop'; import { useChatHost } from './host'; import { useTurns } from './turns'; @@ -52,10 +53,6 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac const latestMessageDepth = tailPlace?.depth; const setSiblingIdx = useSetAtom(siblingIdxFamily(siblingKey(tailPlace?.parentMessageId))); - /** The setter is rebound whenever the tail's parent changes (every turn); the - * ref keeps `handleContinue` referentially stable so rows do not re-render. */ - const setSiblingIdxRef = useRef(setSiblingIdx); - setSiblingIdxRef.current = setSiblingIdx; const setMessages = useCallback( (messages: TMessage[]) => { @@ -97,48 +94,13 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac host.useTurnServices(turnsParams), ); - const askRef = useRef(_ask); - askRef.current = _ask; - const ask: typeof _ask = useCallback((...args) => askRef.current(...args), []); - - const regenerateRef = useRef(_regenerate); - regenerateRef.current = _regenerate; - const regenerate: typeof _regenerate = useCallback( - (...args) => regenerateRef.current(...args), - [], - ); - - const continueGeneration = useCallback(() => { - const currentLatest = getLatestMessage(); - if (!currentLatest) { - console.error('Failed to regenerate the message: latestMessage not found.'); - return; - } - - const messages = getMessages(); - - const parentMessage = messages?.find( - (element) => element.messageId == currentLatest.parentMessageId, - ); - - if (parentMessage && parentMessage.isCreatedByUser) { - ask( - { ...parentMessage }, - { - isContinued: true, - isRegenerate: true, - isEdited: true, - overrideManualSkills: parentMessage.manualSkills, - overrideQuotes: parentMessage.quotes, - overrideReasoning: parentMessage.reasoningOverride ?? null, - }, - ); - } else { - console.error( - 'Failed to regenerate the message: parentMessage not found, or not created by user.', - ); - } - }, [getLatestMessage, getMessages, ask]); + const { ask, regenerate, handleRegenerate, handleContinue } = useTurnHandlers({ + ask: _ask, + regenerate: _regenerate, + getMessages, + getLatestMessage, + setSiblingIdx, + }); const stopGenerating = useStopGenerating(index, conversation); @@ -150,28 +112,6 @@ export default function useChatHelpers(index = 0, paramId?: string): ChatContrac [stopGenerating], ); - const handleRegenerate = useCallback( - (e: MouseEvent) => { - e.preventDefault(); - const parentMessageId = getLatestMessage()?.parentMessageId ?? ''; - if (!parentMessageId) { - console.error('Failed to regenerate the message: parentMessageId not found.'); - return; - } - regenerate({ parentMessageId }); - }, - [getLatestMessage, regenerate], - ); - - const handleContinue = useCallback( - (e: MouseEvent) => { - e.preventDefault(); - continueGeneration(); - setSiblingIdxRef.current(0); - }, - [continueGeneration], - ); - const [preset, setPreset] = useAtom(presetFamily(index)); const [abortScroll, setAbortScroll] = useAtom(abortScrollFamily(index)); const [optionSettings, setOptionSettings] = useAtom(optionSettingsFamily(index)); diff --git a/packages/chat/src/react/follow.ts b/packages/chat/src/react/follow.ts index e505d189a0..635df9e1a9 100644 --- a/packages/chat/src/react/follow.ts +++ b/packages/chat/src/react/follow.ts @@ -4,12 +4,24 @@ import type { TMessage, ChatEvent } from 'librechat-data-provider'; import type { ChatStatus, ChatTransport, + SubmittedTurn, ChatConnection, ChatFinishResult, EventApplierInit, EventApplierEffect, } from '../core'; -import { Chat, MAX_RETRIES, getReconnectDelay, createEventApplier } from '../core'; +import { + Chat, + MAX_RETRIES, + getReconnectDelay, + rerunTurnMessages, + createEventApplier, + failedTurnMessages, + withdrawnTurnMessages, + hasPendingAssistantParent, + prepareSubmittedTurn, + describeSubmittedTurn, +} from '../core'; import { ReactQueryChatState } from './state'; import { useSnapshot } from './snapshot'; @@ -34,6 +46,11 @@ export type FollowChatOptions = { live?: boolean; /** False follows nothing: no read, no stream, and an idle result. */ enabled?: boolean; + /** + * The endpoint the conversation runs on, which a stop of a generation this chat did not start + * is addressed to. A chat that only follows needs none. + */ + endpoint?: string | null; /** Live status a step reports beside the message list. */ onEffect?: (effect: EventApplierEffect) => void; /** @@ -45,6 +62,11 @@ export type FollowChatOptions = { /** Every stream event after it was applied. */ onEvent?: (event: ChatEvent) => void; onFinish?: (result: ChatFinishResult) => void; + /** + * A turn sent from here was stopped before the server kept anything of it. Its rows are gone + * from the list; its words were the reader's and can be offered back to them. + */ + onWithdrawn?: (turn: SubmittedTurn) => void; }; /** One page of stored messages, oldest first. */ @@ -113,6 +135,25 @@ export type FollowedChat = { isEarlierError: boolean; /** Reads the page before the oldest row held, and puts it ahead of the list. */ loadEarlier: () => void; + /** + * Whether the conversation can take a turn at this instant: its stored messages are in, and no + * generation is being started, followed, looked for or read back. Read when a turn is about to + * be built, since the answer a render holds can be a commit old. + */ + canSend: () => boolean; + /** + * Sends a turn main chat's `ask` built, over the transport's send: the chat shows its rows, + * posts its body and applies the generation it starts. False when the chat did not take it, + * in which case nothing was shown or posted. A turn whose stream drops is attached to again, + * as a followed one is. + */ + send: (turn: SubmittedTurn) => boolean; + /** + * Stops the generation running under the conversation, whoever started it. A turn sent from + * here that the server has not answered yet has no generation to name, so it is stopped by its + * conversation, and is not attached to again. + */ + stop: () => Promise; }; type Entry = { @@ -121,15 +162,26 @@ type Entry = { attach: () => Promise; settle: () => Promise; loadEarlier: () => Promise; + canSend: FollowedChat['canSend']; + send: FollowedChat['send']; + stop: FollowedChat['stop']; }; /** What is known of the rows before the ones held, for the conversation `id`. */ type History = { id: string; hasEarlier: boolean; isLoading: boolean; isError: boolean }; type Load = { id: string; attempt: number; state: 'loading' | 'ready' | 'error' }; /** * One attachment: how it ended, unset when nothing was running, and whether it delivered any of - * the generation. The frame that reports a failure is not a delivery. + * the generation. The frame that reports a failure is not a delivery. For a turn sent from here, + * `foreign` says the server answered it with a generation that is not that turn: one already + * running, or a newer one that replaced it; `withdrawn` says it was stopped before the server + * saved anything of it. */ -type Attachment = { finished?: ChatFinishResult; received: boolean }; +type Attachment = { + finished?: ChatFinishResult; + received: boolean; + foreign?: boolean; + withdrawn?: boolean; +}; /** * How many more times a run the caller knows of is looked for after it was not found. Short: a @@ -143,6 +195,8 @@ const toPage = (read: TMessage[] | FollowedPage): Required => Array.isArray(read) ? { messages: read, earlier: null } : { messages: read.messages, earlier: read.earlier ?? null }; +const refuse = (): boolean => false; +const idle = (): Promise => Promise.resolve(); const unsubscribed = (): (() => void) => () => undefined; /** One write per frame for streamed tokens, as main chat's step handler coalesces them. */ @@ -156,14 +210,15 @@ const perFrame = (flush: () => void): (() => void) => { }; /** - * Follows one conversation the reader does not drive: shows its stored messages, attaches to the - * generation running under it and applies that stream the way main chat does, then reads the - * stored messages again once the generation ends. A stream that drops is not an end: what it + * Follows one conversation: shows its stored messages, attaches to the generation running under + * it and applies that stream the way main chat does, then reads the stored messages again once + * the generation ends. A stream that drops is not an end: what it * showed stays, and the attachment is made again on main chat's reconnect schedule. The * conversation is a core `Chat` over the message cache, so anything that reads * `[QueryKeys.messages, id]` sees what it writes. * - * A subagent's child thread is the first of these; a side chat is the same thing with a send. + * A subagent's child thread is one the reader only follows. One the reader drives is the same + * thing with `send`, over a transport that can start a generation. */ export function useFollowChat({ id, @@ -171,14 +226,36 @@ export function useFollowChat({ loadMessages, live = false, enabled = true, + endpoint, onEffect, beside, onEvent, onFinish, + onWithdrawn, }: FollowChatOptions): FollowedChat { const queryClient = useQueryClient(); - const host = useRef({ transport, loadMessages, live, onEffect, beside, onEvent, onFinish }); - host.current = { transport, loadMessages, live, onEffect, beside, onEvent, onFinish }; + const host = useRef({ + transport, + loadMessages, + live, + endpoint, + onEffect, + beside, + onEvent, + onFinish, + onWithdrawn, + }); + host.current = { + transport, + loadMessages, + live, + endpoint, + onEffect, + beside, + onEvent, + onFinish, + onWithdrawn, + }; const [attempt, setAttempt] = useState(0); const [entry, setEntry] = useState(); const [load, setLoad] = useState({ id, attempt, state: 'loading' }); @@ -206,6 +283,14 @@ export function useFollowChat({ let owed = false; let attachment: Attachment | undefined; let wake: (() => void) | undefined; + /** The turn being handed to the chat, which asks for it back inside the same call. */ + let handed: SubmittedTurn | undefined; + /** Whether a turn sent from here is in flight, which is not an attachment to follow or read over. */ + let driving = false; + /** Reads of the stored rows in flight, each of which replaces the list when it lands. */ + let reads = 0; + /** The reader's stop in flight, which the turn it ends waits for instead of reattaching. */ + let stopping: Promise | undefined; const applier = createEventApplier({ schedule: perFrame, onEffect: (effect) => host.current.onEffect?.(effect), @@ -216,20 +301,38 @@ export function useFollowChat({ state: new ReactQueryChatState({ queryClient, key: id }), /** Read on every call, so a transport rebuilt for a refreshed token is the one used. */ transport: { - sendMessages: (request) => host.current.transport.sendMessages(request), + sendMessages: async (request) => { + const turn = attachment; + const sent = await host.current.transport.sendMessages(request); + if (turn != null) { + turn.foreign = + sent.status === 'replaced' || + (sent.status === 'stream' && sent.stream.resumed === true); + } + return sent; + }, reconnectToStream: (request) => host.current.transport.reconnectToStream(request), abort: (request) => host.current.transport.abort(request), getStatus: (conversationId) => host.current.transport.getStatus?.(conversationId) ?? Promise.resolve({ active: false }), }, prepareRequest: () => { - throw new Error('A followed conversation takes no turns'); + const turn = handed; + handed = undefined; + if (turn == null) { + throw new Error('A followed conversation takes the turns `send` hands it, and no others'); + } + return prepareSubmittedTurn(turn); }, applyEvent: applier.applyEvent, + resumeSelection: () => ({ endpoint: host.current.endpoint }), onEvent: (event) => { if (attachment != null && event.type !== 'error') { attachment.received = true; } + if (attachment != null && event.type === 'final' && event.data.earlyAbort === true) { + attachment.withdrawn = true; + } host.current.onEvent?.(event); }, onFinish: (result) => { @@ -274,21 +377,26 @@ export function useFollowChat({ * does the first page this follower reads, since only then is the start of the list known. */ const read = async (): Promise => { - const page = toPage(await host.current.loadMessages()); - if (stale) { - return; + reads += 1; + try { + const page = toPage(await host.current.loadMessages()); + if (stale) { + return; + } + const held = chat.messages; + const first = page.messages[0]?.messageId; + const joinsAt = first == null ? -1 : held.findIndex((row) => row.messageId === first); + if (placed && joinsAt > 0) { + await chat.setMessages([...held.slice(0, joinsAt), ...page.messages]); + return; + } + await chat.setMessages(page.messages); + placed = true; + earlier = page.earlier; + setHistory({ id, hasEarlier: earlier != null, isLoading: false, isError: false }); + } finally { + reads -= 1; } - const held = chat.messages; - const first = page.messages[0]?.messageId; - const joinsAt = first == null ? -1 : held.findIndex((row) => row.messageId === first); - if (placed && joinsAt > 0) { - await chat.setMessages([...held.slice(0, joinsAt), ...page.messages]); - return; - } - await chat.setMessages(page.messages); - placed = true; - earlier = page.earlier; - setHistory({ id, hasEarlier: earlier != null, isLoading: false, isError: false }); }; /** Reads the page before the oldest row held and puts it ahead of the list. */ @@ -330,6 +438,11 @@ export function useFollowChat({ if (stale || !loaded) { return; } + if (driving) { + /** The turn in flight is this chat's own; whatever runs after it is asked for then. */ + owed = true; + return; + } if (attaching) { owed ||= reading; return; @@ -386,7 +499,7 @@ export function useFollowChat({ /** The caller saw the run end. A stream still open reads the stored rows itself when it ends. */ const settle = async (): Promise => { - if (stale || !loaded || attaching) { + if (stale || !loaded || attaching || driving) { return; } try { @@ -425,7 +538,145 @@ export function useFollowChat({ } }; - setEntry({ id, chat, attach, settle, loadEarlier }); + const canSend = (): boolean => + !stale && + loaded && + !attaching && + !driving && + reads === 0 && + (chat.status === 'ready' || chat.status === 'error'); + + /** + * How a turn sent from here ended decides what follows it. + * + * - Its stream dropped: the generation runs on, so it is attached to again like any other, + * unless the reader's own stop is what dropped it. + * - The server settled it without a stream, answered it with a generation that is not this + * turn (one already running, or a newer one that replaced it), or the reader stopped it + * before the server answered: the list is not this turn's to complete, so the stored rows + * are read. + * - Otherwise it ended on its own stream and the list is its result, completed the way main + * chat completes it: a turn stopped before the server kept anything of it leaves the rows + * from before it, a failure becomes the turn's reply, and a rerun gets back the branches + * it set aside. + */ + const conclude = async (turn: SubmittedTurn, current: Attachment): Promise => { + const { finished } = current; + const stopped = stopping; + const dropped = finished?.isDisconnect === true && stopped == null; + /** Whether the list is this turn's to complete: it ran, as itself, to an end of its own. */ + let own = + current.foreign !== true && finished?.isDisconnect !== true && finished?.isSettled !== true; + try { + if (stopped != null) { + await stopped.catch(() => undefined); + } + if (!stale && own) { + let completed: TMessage[] | undefined; + if (current.withdrawn === true) { + completed = withdrawnTurnMessages(turn, chat.messages); + } else if (finished?.isError === true) { + completed = failedTurnMessages(turn, chat.messages, chat.error); + } else { + completed = rerunTurnMessages(turn, finished?.message); + } + if (completed != null) { + await chat.setMessages(completed); + } + if (current.withdrawn === true) { + host.current.onWithdrawn?.(turn); + } + /** + * However the turn ended, it may not leave its empty reply behind as one still on its + * way: nothing more will fill it, and the next turn would be refused as answering a + * reply that has not arrived. What the server kept is then the result. + */ + const replyId = turn.submission.initialResponse?.messageId; + const reply = chat.messages.find((message) => message.messageId === replyId); + own = reply == null || !hasPendingAssistantParent(reply); + } + } finally { + /** Only now can another turn be built: on the list this one left, not the one before it. */ + driving = false; + } + if (stale) { + return; + } + if (dropped || owed) { + owed = false; + await attach(); + return; + } + if (!own) { + await settle(); + } + }; + + /** + * A stop the chat could not address is a turn the server has not answered: it may be running + * with nothing naming it yet. The server finds it by its conversation, and if it only + * appears afterwards, by the generation the status route then names. + */ + const stop = (): Promise => { + const asked = (async (): Promise => { + const result = await chat.stop(); + const endpoint = host.current.endpoint; + if (result.status !== 'unaddressable' || endpoint == null || endpoint === '') { + return; + } + await host.current.transport.abort({ chatId: id, endpoint }); + const status = await host.current.transport.getStatus?.(id); + if (status?.active === true) { + await host.current.transport.abort({ + chatId: id, + endpoint, + streamId: status.streamId, + generationCreatedAt: status.createdAt, + }); + } + })(); + stopping = asked; + const done = (): void => { + if (stopping === asked) { + stopping = undefined; + } + }; + asked.then(done, done); + return asked; + }; + + const send = (turn: SubmittedTurn): boolean => { + if (!canSend()) { + return false; + } + const request = describeSubmittedTurn(turn); + const current: Attachment = { received: false }; + handed = turn; + let sending: Promise; + if (request.trigger === 'submit-message' && request.input != null) { + sending = chat.sendMessage(request.input); + } else if (request.trigger === 'continue-message' && request.messageId != null) { + sending = chat.continue({ messageId: request.messageId }); + } else { + sending = chat.regenerate({ messageId: request.messageId }); + } + /** The chat asks for the turn before the call returns, unless it refused the request. */ + const taken = handed == null; + handed = undefined; + if (!taken) { + sending.catch(() => undefined); + return false; + } + driving = true; + attachment = current; + void sending.then( + () => conclude(turn, current), + () => conclude(turn, current), + ); + return true; + }; + + setEntry({ id, chat, attach, settle, loadEarlier, canSend, send, stop }); setHistory({ id, hasEarlier: false, isLoading: false, isError: false }); setLoad({ id, attempt, state: 'loading' }); setFollowing(false); @@ -466,6 +717,9 @@ export function useFollowChat({ const pageBack = current?.loadEarlier; const loadEarlier = useCallback(() => void pageBack?.(), [pageBack]); const older = current != null && history.id === id ? history : undefined; + const canSend = current?.canSend ?? refuse; + const send = current?.send ?? refuse; + const stop = current?.stop ?? idle; const tracked = load.id === id && load.attempt === attempt ? load.state : 'loading'; const state = enabled ? tracked : 'ready'; @@ -484,5 +738,8 @@ export function useFollowChat({ isLoadingEarlier: older?.isLoading ?? false, isEarlierError: older?.isError ?? false, loadEarlier: current == null ? nothing : loadEarlier, + canSend, + send, + stop, }; } diff --git a/packages/chat/src/react/subagents/__tests__/follows.spec.tsx b/packages/chat/src/react/subagents/__tests__/follows.spec.tsx index 9e57719bf9..7ba9e26e25 100644 --- a/packages/chat/src/react/subagents/__tests__/follows.spec.tsx +++ b/packages/chat/src/react/subagents/__tests__/follows.spec.tsx @@ -30,6 +30,9 @@ const followed = (overrides: Partial = {}): FollowedChat => ({ isLoadingEarlier: false, isEarlierError: false, loadEarlier: () => undefined, + canSend: () => false, + send: () => false, + stop: () => Promise.resolve(), ...overrides, }); diff --git a/packages/chat/src/react/subagents/follows.tsx b/packages/chat/src/react/subagents/follows.tsx index 1005f48ddd..b8edaae791 100644 --- a/packages/chat/src/react/subagents/follows.tsx +++ b/packages/chat/src/react/subagents/follows.tsx @@ -127,6 +127,9 @@ const opening: FollowedChat = { isLoadingEarlier: false, isEarlierError: false, loadEarlier: () => undefined, + canSend: () => false, + send: () => false, + stop: () => Promise.resolve(), }; const ThreadFollowsContext: Context = createContext<