Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ jest.mock('@librechat/api', () => ({
jest.requireActual('@librechat/api').getTrustedNewConversationId(...args),
buildGenerationStartedResponse: (...args) =>
jest.requireActual('@librechat/api').buildGenerationStartedResponse(...args),
describeCreatedTurn: (...args) =>
jest.requireActual('@librechat/api').describeCreatedTurn(...args),
savePrivateTextMessage: (save, _req, ...args) => save(...args),
savePrivateTextErrorTurn: (...args) =>
jest.requireActual('@librechat/api').savePrivateTextErrorTurn(...args),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,8 @@ jest.mock('@librechat/api', () => ({
jest.requireActual('@librechat/api').getTrustedNewConversationId(...args),
buildGenerationStartedResponse: (...args) =>
jest.requireActual('@librechat/api').buildGenerationStartedResponse(...args),
describeCreatedTurn: (...args) =>
jest.requireActual('@librechat/api').describeCreatedTurn(...args),
getPreliminaryResponseMessageId: jest.requireActual(
'../../../../../packages/api/src/utils/message',
).getPreliminaryResponseMessageId,
Expand Down Expand Up @@ -4473,7 +4475,12 @@ describe('ResumableAgentController resume metadata', () => {

expect(mockGenerationJobManager.emitChunk).toHaveBeenCalledWith(
conversationId,
expect.objectContaining({ created: true, message: expect.objectContaining(skills) }),
expect.objectContaining({
created: true,
message: expect.objectContaining(skills),
/** A plain new turn states only the id its reply is stored under. */
turn: { responseMessageId: 'server-response' },
}),
expect.any(Object),
);
expect(mockGenerationJobManager.updateMetadata).toHaveBeenCalledWith(
Expand Down
9 changes: 9 additions & 0 deletions api/server/controllers/agents/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const {
createDisconnectSnapshotQueue,
isTemporaryTurn,
getPreliminaryResponseMessageId,
describeCreatedTurn,
} = require('@librechat/api');
const { disposeClient } = require('~/server/cleanup');
const {
Expand Down Expand Up @@ -2452,6 +2453,14 @@ const ResumableAgentController = async (req, res, next, initializeClient, addTit
req.body.files.length > 0 && { files: req.body.files }),
},
streamId,
turn: describeCreatedTurn({
responseMessageId: respMsgId,
sourceMessageId: editedResponseMessageId,
isRegenerate,
isCompaction,
isContinued,
editedContent,
}),
},
{ expectedCreatedAt: jobCreatedAt },
).catch((err) => {
Expand Down
1 change: 1 addition & 0 deletions packages/api/src/agents/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,3 +108,4 @@ export { captureAuthoredSkills, restoreAuthoredSkills } from './authored';

export { createSdkModelHost } from './remoteSdk/builder';
export type { SdkModelHost } from './remoteSdk/builder';
export * from './turn';
2 changes: 2 additions & 0 deletions packages/api/src/agents/subagentGeneration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { Agents, SubagentContentFrame, TMessageContentParts } from 'librech
import type { GenerationJobManagerClass, TerminalJobClaim } from '~/stream/GenerationJobManager';
import type { FinalMessageFields, ServerSentEvent } from '~/types';
import { createSubagentContentFold } from './subagentContent';
import { describeCreatedTurn } from './turn';

/** Frames waiting on a slow store before the stream is given up, matching the manager's own
* early-event ceiling. Merged deltas do not count twice, so only distinct steps reach it. */
Expand Down Expand Up @@ -456,6 +457,7 @@ export function createChildGenerationHost(
created: true,
message: { ...userMessage, sender: 'User', isCreatedByUser: true },
streamId: input.threadId,
turn: describeCreatedTurn({ responseMessageId }),
});
return generation;
} catch (error) {
Expand Down
71 changes: 71 additions & 0 deletions packages/api/src/agents/turn.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { ContentTypes } from 'librechat-data-provider';
import { describeCreatedTurn } from './turn';

describe('describeCreatedTurn', () => {
it('names only the reply for a plain new turn', () => {
expect(describeCreatedTurn({ responseMessageId: 'resp-1' })).toEqual({
responseMessageId: 'resp-1',
});
expect(
describeCreatedTurn({
responseMessageId: 'resp-1',
isRegenerate: false,
isCompaction: false,
isContinued: false,
editedContent: null,
}),
).toEqual({ responseMessageId: 'resp-1' });
});

it('says a regeneration is one', () => {
expect(describeCreatedTurn({ responseMessageId: 'resp-2', isRegenerate: true })).toEqual({
responseMessageId: 'resp-2',
isRegenerate: true,
});
});

it('says a compaction is regenerate-shaped as well, which its request does not', () => {
expect(describeCreatedTurn({ responseMessageId: 'resp-2', isCompaction: true })).toEqual({
responseMessageId: 'resp-2',
compact: true,
isRegenerate: true,
});
});

it('names the reply a continued turn starts from, which keeps its id', () => {
expect(
describeCreatedTurn({
responseMessageId: 'resp-1',
sourceMessageId: 'resp-1',
isContinued: true,
}),
).toEqual({ responseMessageId: 'resp-1', sourceMessageId: 'resp-1', isContinued: true });
});

it('names the reply an edited rerun starts from beside the new id it is stored under', () => {
const editedContent = { index: 1, type: ContentTypes.TEXT, text: 'Edited' } as const;
expect(
describeCreatedTurn({
responseMessageId: 'resp-2',
sourceMessageId: 'resp-1',
editedContent,
}),
).toEqual({ responseMessageId: 'resp-2', sourceMessageId: 'resp-1', editedContent });
});

it('does not name a source for a turn that starts from no reply', () => {
expect(
describeCreatedTurn({
responseMessageId: 'resp-2',
sourceMessageId: 'resp-1',
isRegenerate: true,
}),
).toEqual({ responseMessageId: 'resp-2', isRegenerate: true });
});

it('leaves out a reply id it was not given', () => {
expect(describeCreatedTurn({ responseMessageId: null, isRegenerate: true })).toEqual({
isRegenerate: true,
});
});
});
37 changes: 37 additions & 0 deletions packages/api/src/agents/turn.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { TEditedContent, ChatCreatedTurn } from 'librechat-data-provider';

/** What the request that started a turn says about it. */
export type TurnRequest = {
responseMessageId?: string | null;
/** The reply the request named to continue or edit, which an edited rerun does not keep. */
sourceMessageId?: string | null;
isRegenerate?: boolean;
isCompaction?: boolean;
isContinued?: boolean;
editedContent?: TEditedContent | null;
};

/**
* The shape of a turn as its `created` frame states it, so a client that did not start the turn
* applies it the way the one that did applies it. Only what holds is written: a plain new turn
* carries the id of its reply and nothing else.
*/
export function describeCreatedTurn({
responseMessageId,
sourceMessageId,
isRegenerate,
isCompaction,
isContinued,
editedContent,
}: TurnRequest): ChatCreatedTurn {
const startsFromReply = isContinued === true || editedContent != null;
return {
...(responseMessageId ? { responseMessageId } : {}),
...(isCompaction === true ? { compact: true } : {}),
/** A compaction is regenerate-shaped for every consumer; the request sets only `compact`. */
...(isRegenerate === true || isCompaction === true ? { isRegenerate: true } : {}),
...(startsFromReply && sourceMessageId ? { sourceMessageId } : {}),
...(isContinued === true ? { isContinued: true } : {}),
...(editedContent != null ? { editedContent } : {}),
};
}
4 changes: 3 additions & 1 deletion packages/api/src/types/events.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { TPendingSteer, TPromotedSteer } from 'librechat-data-provider';
import type { TPendingSteer, TPromotedSteer, ChatCreatedTurn } from 'librechat-data-provider';

/** SSE streaming event (on_run_step, on_message_delta, etc.) */
export type StreamEvent = {
Expand All @@ -22,6 +22,8 @@ export type CreatedEvent = {
quotes?: string[];
};
streamId: string;
/** The turn's shape, for a client that watches the turn without having started it. */
turn?: ChatCreatedTurn;
};

export type FinalMessageFields = {
Expand Down
132 changes: 127 additions & 5 deletions packages/chat/src/core/__tests__/apply.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -919,6 +919,127 @@ describe('side frames', () => {
});
});

describe('a turn whose shape the server states', () => {
const reply = (id: string, texts: string[]): TMessage => ({
messageId: id,
conversationId: 'thread-1',
parentMessageId: 'user-1',
isCreatedByUser: false,
text: '',
content: texts.map((text) => ({ type: ContentTypes.TEXT, text })),
});
const createdAs = (turn: object): ChatEvent =>
({ type: 'created', data: { created: true, message: user, turn } }) as ChatEvent;
const texts = (messages: TMessage[], id: string) =>
(messages.find((message) => message.messageId === id)?.content ?? []).map(
(part) => (part as { text?: string } | undefined)?.text,
);

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'])]);

applyEvent(
createdAs({ responseMessageId: 'resp-1', sourceMessageId: 'resp-1', isContinued: true }),
context,
);
expect(read().map((message) => message.messageId)).toEqual(['user-1', 'resp-1']);
expect(texts(read(), 'resp-1')).toEqual([]);

applyEvent(opened('s0', 0), context);
applyEvent(token('s0', 'Stopped here, then carried on'), context);

expect(read().map((message) => message.messageId)).toEqual(['user-1', 'resp-1']);
expect(texts(read(), 'resp-1')).toEqual(['Stopped here, then carried on']);
});

it('continues the reply it was told to, not the answer that happens to be last in the list', () => {
const sibling = reply('resp-9', ['Another branch']);
const { applyEvent } = createEventApplier();
const { context, read } = createList([user, reply('resp-1', ['Stopped here']), sibling]);

applyEvent(
createdAs({ responseMessageId: 'resp-1', sourceMessageId: 'resp-1', isContinued: true }),
context,
);
applyEvent(opened('s0', 0), context);
applyEvent(token('s0', 'Carried on'), context);

expect(texts(read(), 'resp-1')).toEqual(['Carried on']);
expect(read().find((message) => message.messageId === 'resp-9')).toBe(sibling);
});

it('reruns an edited reply from its own parts under the new id the server stores it at', () => {
const saved = reply('resp-0', ['Kept', 'Before the edit']);
const sibling = reply('resp-9', ['Another branch']);
const { applyEvent } = createEventApplier();
const { context, read } = createList([user, saved, sibling]);

applyEvent(
createdAs({
/** An edited rerun gets a fresh id; the reply it starts from is named beside it. */
responseMessageId: 'resp-1',
sourceMessageId: 'resp-0',
editedContent: { index: 1, type: ContentTypes.TEXT, text: 'After the edit' },
}),
context,
);
applyEvent(opened('s0', 0), context);
applyEvent(token('s0', ' and on'), context);

expect(read().map((message) => message.messageId)).toEqual(['user-1', 'resp-9', 'resp-1']);
const drawn = texts(read(), 'resp-1').join('|');
expect(drawn).toContain('Kept');
expect(drawn).toContain('After the edit');
expect(drawn).toContain(' and on');
expect(drawn).not.toContain('Before the edit');
expect(drawn).not.toContain('Another branch');
/** The saved row's own parts are not the ones edited, and the other branch is untouched. */
expect(texts([saved], 'resp-0')).toEqual(['Kept', 'Before the edit']);
expect(read().find((message) => message.messageId === 'resp-9')).toBe(sibling);
});

it('builds a plain turn on its own reply, not on an abandoned one under the same message', () => {
const abandoned = reply('resp-0', ['Abandoned attempt']);
const { applyEvent } = createEventApplier();
const { context, read } = createList([user, abandoned]);

applyEvent(createdAs({ responseMessageId: 'resp-1' }), context);
applyEvent(opened('s0', 0), context);
applyEvent(token('s0', 'Hi'), context);

expect(texts(read(), 'resp-1')).toEqual(['Hi']);
expect(read().find((message) => message.messageId === 'resp-0')).toBe(abandoned);
});

it("takes a regeneration at the server's word where the list holds no earlier answer", () => {
const { applyEvent } = createEventApplier();
const { context, read } = createList([
user,
{ ...reply('resp-0', ['Other branch']), parentMessageId: 'user-0' },
]);

applyEvent(createdAs({ responseMessageId: 'resp-1', isRegenerate: true }), context);
applyEvent(opened('s0', 0), context);
applyEvent(token('s0', 'Again'), context);

/** Not seeded from the reply at the tail, which a plain turn would carry on from. */
expect(texts(read(), 'resp-1')).toEqual(['Again']);
expect(texts(read(), 'resp-0')).toEqual(['Other branch']);
});

it('reads a frame without a stated shape by what the list implies, as before', () => {
const { applyEvent } = createEventApplier();
const { context, read } = createList([user, reply('resp-0', ['Earlier answer'])]);

applyEvent(created, context);
applyEvent(opened('s0', 0), context);
applyEvent(token('s0', 'Hi'), context);

expect(read().map((message) => message.messageId)).toEqual(['user-1', 'resp-0', 'resp-1']);
});
});

describe('state beside the message list', () => {
/** Main chat's handlers for that state, recording what they are called with and in what order. */
const record = () => {
Expand Down Expand Up @@ -1075,6 +1196,7 @@ describe('state beside the message list', () => {
const taken = {
type: 'steer_applied',
data: {
conversationId: 'thread-1',
steerId: 'steer-1',
clientSteerId: 'client-1',
index: 1,
Expand All @@ -1083,18 +1205,18 @@ describe('state beside the message list', () => {
},
} as ChatEvent;

applyEvent(created, ctx);
/** Its response is not drawn yet, so the part waits; the chip does not. */
/** No frame has named its turn yet, so the part has nowhere to go; the chip does not wait. */
applyEvent(taken, ctx);
expect(names()).toEqual(['bindResponse', 'resolve']);
expect(calls[1]).toEqual(['resolve', 'thread-1', 'steer-1', 'client-1', undefined]);
expect(names()).toEqual(['resolve']);
expect(calls[0]).toEqual(['resolve', 'thread-1', 'steer-1', 'client-1', undefined]);

applyEvent(created, ctx);
applyEvent(opened('s0', 0), ctx);
applyEvent(token('s0', 'Hi'), ctx);

expect(names().filter((name) => name !== 'tap')).toEqual([
'bindResponse',
'resolve',
'bindResponse',
'markLive',
]);
expect(calls.find(([name]) => name === 'markLive')).toEqual(['markLive', 'steer-1']);
Expand Down
Loading
Loading