@librechat/agents 3.3.7 → 3.3.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/graphs/MultiAgentGraph.cjs +21 -4
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +124 -15
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/messages/injected.cjs +10 -1
- package/dist/cjs/messages/injected.cjs.map +1 -1
- package/dist/cjs/prompts/activityLabel.cjs +29 -1
- package/dist/cjs/prompts/activityLabel.cjs.map +1 -1
- package/dist/cjs/run.cjs +7 -2
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +55 -0
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +21 -4
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +124 -15
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/messages/injected.mjs +10 -1
- package/dist/esm/messages/injected.mjs.map +1 -1
- package/dist/esm/prompts/activityLabel.mjs +29 -1
- package/dist/esm/prompts/activityLabel.mjs.map +1 -1
- package/dist/esm/run.mjs +7 -2
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +55 -0
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/types/messages/format.d.ts +9 -8
- package/dist/types/prompts/activityLabel.d.ts +8 -1
- package/dist/types/run.d.ts +1 -1
- package/dist/types/types/activityLabel.d.ts +8 -0
- package/dist/types/types/stream.d.ts +19 -0
- package/package.json +1 -1
- package/src/graphs/MultiAgentGraph.ts +18 -4
- package/src/messages/format.ts +222 -50
- package/src/messages/formatAgentMessages.test.ts +308 -6
- package/src/messages/injected.test.ts +18 -1
- package/src/messages/injected.ts +8 -1
- package/src/prompts/activityLabel.ts +48 -0
- package/src/run.ts +10 -1
- package/src/specs/activity-label-prompt.test.ts +93 -0
- package/src/summarization/__tests__/node.test.ts +188 -0
- package/src/summarization/node.ts +67 -0
- package/src/types/activityLabel.ts +8 -0
- package/src/types/stream.ts +20 -0
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { AIMessage, HumanMessage, ToolMessage } from '@langchain/core/messages';
|
|
2
2
|
import type { RunnableConfig } from '@langchain/core/runnables';
|
|
3
|
+
import type { BaseMessage } from '@langchain/core/messages';
|
|
3
4
|
import type * as t from '@/types';
|
|
4
5
|
import {
|
|
5
6
|
createSummarizeNode,
|
|
6
7
|
DEFAULT_SUMMARIZATION_PROMPT,
|
|
7
8
|
DEFAULT_UPDATE_SUMMARIZATION_PROMPT,
|
|
8
9
|
} from '@/summarization/node';
|
|
10
|
+
import { convertInjectedMessages } from '@/messages/injected';
|
|
9
11
|
import { Constants, GraphEvents, Providers } from '@/common';
|
|
10
12
|
import { AgentContext } from '@/agents/AgentContext';
|
|
11
13
|
import * as providers from '@/llm/providers';
|
|
@@ -945,6 +947,192 @@ describe('recency window — first-turn protection', () => {
|
|
|
945
947
|
expect((result.messages![2] as AIMessage).content).toBe('turn 2 reply');
|
|
946
948
|
});
|
|
947
949
|
|
|
950
|
+
describe('summary coverage', () => {
|
|
951
|
+
const runCompaction = async (
|
|
952
|
+
messages: BaseMessage[],
|
|
953
|
+
turns = 1
|
|
954
|
+
): Promise<t.SummaryContentBlock | undefined> => {
|
|
955
|
+
captureEvents();
|
|
956
|
+
jest.spyOn(providers, 'getChatModelClass').mockReturnValue(
|
|
957
|
+
class {
|
|
958
|
+
constructor() {
|
|
959
|
+
return mockInvokeModel('Summary of older turns');
|
|
960
|
+
}
|
|
961
|
+
} as never
|
|
962
|
+
);
|
|
963
|
+
|
|
964
|
+
let summaryBlock: t.SummaryContentBlock | undefined;
|
|
965
|
+
const graph = mockGraph((_stepId, result) => {
|
|
966
|
+
if (result.type === 'summary') {
|
|
967
|
+
summaryBlock = result.summary;
|
|
968
|
+
}
|
|
969
|
+
});
|
|
970
|
+
const summarizeNode = createSummarizeNode({
|
|
971
|
+
agentContext: createAgentContext({
|
|
972
|
+
summarizationConfig: { retainRecent: { turns } },
|
|
973
|
+
} as never),
|
|
974
|
+
graph: graph as never,
|
|
975
|
+
generateStepId,
|
|
976
|
+
});
|
|
977
|
+
|
|
978
|
+
await summarizeNode(
|
|
979
|
+
{
|
|
980
|
+
messages,
|
|
981
|
+
summarizationRequest: {
|
|
982
|
+
remainingContextTokens: 0,
|
|
983
|
+
agentId: 'agent_0',
|
|
984
|
+
},
|
|
985
|
+
},
|
|
986
|
+
{} as RunnableConfig
|
|
987
|
+
);
|
|
988
|
+
|
|
989
|
+
return summaryBlock;
|
|
990
|
+
};
|
|
991
|
+
|
|
992
|
+
it('records the first retained message as the coverage anchor', async () => {
|
|
993
|
+
const summaryBlock = await runCompaction([
|
|
994
|
+
new HumanMessage({ content: 'turn 1 query', id: 'm1' }),
|
|
995
|
+
new AIMessage({ content: 'turn 1 reply', id: 'm2' }),
|
|
996
|
+
new HumanMessage({ content: 'turn 2 query', id: 'm3' }),
|
|
997
|
+
new AIMessage({ content: 'turn 2 reply', id: 'm4' }),
|
|
998
|
+
]);
|
|
999
|
+
|
|
1000
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm3' });
|
|
1001
|
+
});
|
|
1002
|
+
|
|
1003
|
+
it('skips a retained message that carries no source id', async () => {
|
|
1004
|
+
const summaryBlock = await runCompaction([
|
|
1005
|
+
new HumanMessage({ content: 'turn 1 query', id: 'm1' }),
|
|
1006
|
+
new AIMessage({ content: 'turn 1 reply', id: 'm2' }),
|
|
1007
|
+
new HumanMessage({ content: 'turn 2 query' }),
|
|
1008
|
+
new AIMessage({ content: 'turn 2 reply', id: 'm4' }),
|
|
1009
|
+
]);
|
|
1010
|
+
|
|
1011
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm4' });
|
|
1012
|
+
});
|
|
1013
|
+
|
|
1014
|
+
it('omits coverage when no retained message carries a source id', async () => {
|
|
1015
|
+
const summaryBlock = await runCompaction([
|
|
1016
|
+
new HumanMessage('turn 1 query'),
|
|
1017
|
+
new AIMessage('turn 1 reply'),
|
|
1018
|
+
new HumanMessage('turn 2 query'),
|
|
1019
|
+
new AIMessage('turn 2 reply'),
|
|
1020
|
+
]);
|
|
1021
|
+
|
|
1022
|
+
expect(summaryBlock?.coverage).toBeUndefined();
|
|
1023
|
+
});
|
|
1024
|
+
|
|
1025
|
+
/** A steer expands one source message into pre-steer, steer, and post-steer
|
|
1026
|
+
* messages sharing its ID, and the recency split lands on the steer. The
|
|
1027
|
+
* straddling message is the anchor, so it survives whole. */
|
|
1028
|
+
it('anchors on a source id that straddles the recency boundary', async () => {
|
|
1029
|
+
const summaryBlock = await runCompaction([
|
|
1030
|
+
new HumanMessage({ content: 'turn 1 query', id: 'm1' }),
|
|
1031
|
+
new AIMessage({ content: 'pre-steer reply', id: 'm2' }),
|
|
1032
|
+
new HumanMessage({
|
|
1033
|
+
content: 'steer',
|
|
1034
|
+
id: 'm2',
|
|
1035
|
+
additional_kwargs: { role: 'user', source: 'steer' },
|
|
1036
|
+
}),
|
|
1037
|
+
new AIMessage({ content: 'post-steer reply', id: 'm2' }),
|
|
1038
|
+
]);
|
|
1039
|
+
|
|
1040
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm2' });
|
|
1041
|
+
});
|
|
1042
|
+
|
|
1043
|
+
/** A steer carries `source: 'steer'` but is replayed from a payload entry
|
|
1044
|
+
* and stamped with its ID, so it is a valid anchor. When compaction lands
|
|
1045
|
+
* before any post-steer message exists it is the *only* retained entry —
|
|
1046
|
+
* treating every marked message as synthetic drops it. */
|
|
1047
|
+
it('anchors on a retained steer with no post-steer message', async () => {
|
|
1048
|
+
const summaryBlock = await runCompaction([
|
|
1049
|
+
new HumanMessage({ content: 'turn 1 query', id: 'm1' }),
|
|
1050
|
+
new AIMessage({ content: 'pre-steer reply', id: 'm2' }),
|
|
1051
|
+
new HumanMessage({
|
|
1052
|
+
content: 'steer',
|
|
1053
|
+
id: 'm2',
|
|
1054
|
+
additional_kwargs: { role: 'user', source: 'steer' },
|
|
1055
|
+
}),
|
|
1056
|
+
]);
|
|
1057
|
+
|
|
1058
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm2' });
|
|
1059
|
+
});
|
|
1060
|
+
|
|
1061
|
+
/** `formatAgentMessages` reconstructs skill bodies inside its payload loop
|
|
1062
|
+
* and keeps processing payload entries after, so this unstamped entry — a
|
|
1063
|
+
* reducer UUID by the time compaction sees it — precedes stamped messages.
|
|
1064
|
+
* Anchoring on it would resolve to nothing on the next run. */
|
|
1065
|
+
it('skips a reconstructed skill body to reach the stamped message behind it', async () => {
|
|
1066
|
+
const summaryBlock = await runCompaction(
|
|
1067
|
+
[
|
|
1068
|
+
new HumanMessage({ content: 'turn 1 query', id: 'm1' }),
|
|
1069
|
+
new AIMessage({ content: 'turn 1 reply', id: 'm2' }),
|
|
1070
|
+
new HumanMessage({
|
|
1071
|
+
content: 'skill body',
|
|
1072
|
+
id: 'reducer-uuid',
|
|
1073
|
+
additional_kwargs: {
|
|
1074
|
+
role: 'user',
|
|
1075
|
+
isMeta: true,
|
|
1076
|
+
source: 'skill',
|
|
1077
|
+
skillName: 'demo',
|
|
1078
|
+
},
|
|
1079
|
+
}),
|
|
1080
|
+
new HumanMessage({ content: 'turn 2 query', id: 'm3' }),
|
|
1081
|
+
new AIMessage({ content: 'turn 2 reply', id: 'm4' }),
|
|
1082
|
+
],
|
|
1083
|
+
2
|
|
1084
|
+
);
|
|
1085
|
+
|
|
1086
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm3' });
|
|
1087
|
+
});
|
|
1088
|
+
|
|
1089
|
+
/** `InjectedMessage` leaves both `isMeta` and `source` optional, so a bare
|
|
1090
|
+
* injected turn carries no marker of its own — and an injected `steer` is
|
|
1091
|
+
* otherwise indistinguishable from a replayed one. `convertInjectedMessages`
|
|
1092
|
+
* records `injected` on everything it builds, which decides both. */
|
|
1093
|
+
it.each([
|
|
1094
|
+
['a bare injected turn', { role: 'user' as const, content: 'injected' }],
|
|
1095
|
+
[
|
|
1096
|
+
'an injected steer',
|
|
1097
|
+
{
|
|
1098
|
+
role: 'user' as const,
|
|
1099
|
+
content: 'injected steer',
|
|
1100
|
+
source: 'steer' as const,
|
|
1101
|
+
},
|
|
1102
|
+
],
|
|
1103
|
+
])('skips %s when anchoring', async (_label, injected) => {
|
|
1104
|
+
const [converted] = convertInjectedMessages([injected]);
|
|
1105
|
+
converted.id = 'reducer-uuid';
|
|
1106
|
+
|
|
1107
|
+
const summaryBlock = await runCompaction(
|
|
1108
|
+
[
|
|
1109
|
+
new HumanMessage({ content: 'turn 1 query', id: 'm1' }),
|
|
1110
|
+
new AIMessage({ content: 'turn 1 reply', id: 'm2' }),
|
|
1111
|
+
converted,
|
|
1112
|
+
new HumanMessage({ content: 'turn 2 query', id: 'm3' }),
|
|
1113
|
+
new AIMessage({ content: 'turn 2 reply', id: 'm4' }),
|
|
1114
|
+
],
|
|
1115
|
+
2
|
|
1116
|
+
);
|
|
1117
|
+
|
|
1118
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm3' });
|
|
1119
|
+
});
|
|
1120
|
+
|
|
1121
|
+
it('anchors on the straddling id when it is the only source', async () => {
|
|
1122
|
+
const summaryBlock = await runCompaction([
|
|
1123
|
+
new AIMessage({ content: 'pre-steer reply', id: 'm1' }),
|
|
1124
|
+
new HumanMessage({
|
|
1125
|
+
content: 'steer',
|
|
1126
|
+
id: 'm1',
|
|
1127
|
+
additional_kwargs: { role: 'user', source: 'steer' },
|
|
1128
|
+
}),
|
|
1129
|
+
new AIMessage({ content: 'post-steer reply', id: 'm1' }),
|
|
1130
|
+
]);
|
|
1131
|
+
|
|
1132
|
+
expect(summaryBlock?.coverage).toEqual({ retainedFromMessageId: 'm1' });
|
|
1133
|
+
});
|
|
1134
|
+
});
|
|
1135
|
+
|
|
948
1136
|
it('keeps the masked tail content (does not re-inject restored tool payloads into state)', async () => {
|
|
949
1137
|
captureEvents();
|
|
950
1138
|
|
|
@@ -381,10 +381,75 @@ function computeSummaryTokenCount(
|
|
|
381
381
|
return 0;
|
|
382
382
|
}
|
|
383
383
|
|
|
384
|
+
/**
|
|
385
|
+
* Names the first retained message so the summary declares its own extent
|
|
386
|
+
* rather than leaving the next run to infer coverage from where the block
|
|
387
|
+
* happens to sit.
|
|
388
|
+
*
|
|
389
|
+
* Anchored to the retained side, not the covered side. One source message can
|
|
390
|
+
* expand into several messages — a steer splits an assistant entry into
|
|
391
|
+
* pre-steer, steer, and post-steer entries sharing its ID — and the recency
|
|
392
|
+
* split lands on any human-type message, including the steer. Naming the last
|
|
393
|
+
* *covered* message would then name a half-covered ID with no correct reading;
|
|
394
|
+
* naming the first *retained* message makes that same message the anchor, so it
|
|
395
|
+
* survives whole and everything before it is unambiguously covered.
|
|
396
|
+
*
|
|
397
|
+
* Synthetic entries are skipped, because they can sit *before* a resolvable
|
|
398
|
+
* one. `formatAgentMessages` reconstructs skill bodies inside its payload loop
|
|
399
|
+
* (see the `pendingSkillNames` block) and keeps processing payload entries
|
|
400
|
+
* afterwards, so an unstamped skill body — which `messagesStateReducer` then
|
|
401
|
+
* gives a UUID no payload entry carries — is followed by stamped messages.
|
|
402
|
+
* Anchoring on the UUID would look resolvable at write time and degrade to
|
|
403
|
+
* positional trimming on read, dropping the retained tail. Skipping it reaches
|
|
404
|
+
* the stamped message behind it.
|
|
405
|
+
*
|
|
406
|
+
* `convertInjectedMessages` records `injected` on everything it builds, which is
|
|
407
|
+
* what makes this decidable: `isMeta` and `source` are both optional on
|
|
408
|
+
* `InjectedMessage`, so a bare entry carries no marker of its own, and an
|
|
409
|
+
* injected `source: 'steer'` is otherwise indistinguishable from a replayed one.
|
|
410
|
+
* The remaining `isMeta`/`source` checks cover the constructors that build
|
|
411
|
+
* synthetic entries directly instead of going through that funnel — hook context
|
|
412
|
+
* in `ToolNode` and `StandardGraph`, handoff cues, reconstructed skill bodies.
|
|
413
|
+
*
|
|
414
|
+
* `steer` is exempt from the `source` check because a replayed steer *is*
|
|
415
|
+
* stamped from its payload entry; rejecting every marked `source` once dropped
|
|
416
|
+
* exactly those retained steers. Injected steers are still caught, by `injected`.
|
|
417
|
+
*
|
|
418
|
+
* Known limitation: a payload entry that omits `messageId` is never stamped, so
|
|
419
|
+
* the reducer's UUID is recorded and cannot resolve on the next run. There is no
|
|
420
|
+
* write-time fix — such an entry has no stable ID to name in the next payload
|
|
421
|
+
* either — and the reader's positional fallback is what `main` already does, so
|
|
422
|
+
* the anchor degrades rather than misleads.
|
|
423
|
+
*/
|
|
424
|
+
function isSyntheticContext(message: BaseMessage): boolean {
|
|
425
|
+
const { additional_kwargs: kwargs } = message;
|
|
426
|
+
if (kwargs.injected === true || kwargs.isMeta === true) {
|
|
427
|
+
return true;
|
|
428
|
+
}
|
|
429
|
+
return kwargs.source != null && kwargs.source !== 'steer';
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
function resolveSummaryCoverage(
|
|
433
|
+
messagesToRetain: BaseMessage[]
|
|
434
|
+
): t.SummaryCoverage | undefined {
|
|
435
|
+
for (let i = 0; i < messagesToRetain.length; i++) {
|
|
436
|
+
const message = messagesToRetain[i];
|
|
437
|
+
if (isSyntheticContext(message)) {
|
|
438
|
+
continue;
|
|
439
|
+
}
|
|
440
|
+
const id = message.id?.trim();
|
|
441
|
+
if (id != null && id !== '') {
|
|
442
|
+
return { retainedFromMessageId: id };
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
return undefined;
|
|
446
|
+
}
|
|
447
|
+
|
|
384
448
|
/** Constructs the SummaryContentBlock persisted in the run step and dispatched to events. */
|
|
385
449
|
function buildSummaryBlock(params: {
|
|
386
450
|
summaryText: string;
|
|
387
451
|
tokenCount: number;
|
|
452
|
+
coverage?: t.SummaryCoverage;
|
|
388
453
|
stepId: string;
|
|
389
454
|
stepIndex: number;
|
|
390
455
|
modelName?: string;
|
|
@@ -400,6 +465,7 @@ function buildSummaryBlock(params: {
|
|
|
400
465
|
} as t.MessageContentComplex,
|
|
401
466
|
],
|
|
402
467
|
tokenCount: params.tokenCount,
|
|
468
|
+
...(params.coverage != null ? { coverage: params.coverage } : {}),
|
|
403
469
|
summaryVersion: params.summaryVersion,
|
|
404
470
|
boundary: {
|
|
405
471
|
messageId: params.stepId,
|
|
@@ -1145,6 +1211,7 @@ export function createSummarizeNode({
|
|
|
1145
1211
|
const summaryBlock = buildSummaryBlock({
|
|
1146
1212
|
summaryText,
|
|
1147
1213
|
tokenCount,
|
|
1214
|
+
coverage: resolveSummaryCoverage(messagesToRetain),
|
|
1148
1215
|
stepId,
|
|
1149
1216
|
stepIndex: runStep.index,
|
|
1150
1217
|
modelName: clientConfig.modelName,
|
|
@@ -38,6 +38,14 @@ export type RunActivityLabelOptions = {
|
|
|
38
38
|
thinkingExcerpts?: string[];
|
|
39
39
|
/** Assistant's last text before the block (~200 chars), as intent context. */
|
|
40
40
|
lastAssistantText?: string;
|
|
41
|
+
/**
|
|
42
|
+
* Headers already committed for earlier batches in this run (run order,
|
|
43
|
+
* most recent last). Continuity context: the prompt shows them so the new
|
|
44
|
+
* header extends the run's story instead of restating a line already on
|
|
45
|
+
* screen. Hosts should pass only COMMITTED labels — a pending slot's text
|
|
46
|
+
* is empty and a dropped fill never surfaced to the user.
|
|
47
|
+
*/
|
|
48
|
+
previousLabels?: string[];
|
|
41
49
|
/** Override for the default label system prompt. */
|
|
42
50
|
prompt?: string;
|
|
43
51
|
/** Per-entry serialization cap for the prompt. Default 600. */
|
package/src/types/stream.ts
CHANGED
|
@@ -301,10 +301,30 @@ export type SummaryBoundary = {
|
|
|
301
301
|
contentIndex: number;
|
|
302
302
|
};
|
|
303
303
|
|
|
304
|
+
/**
|
|
305
|
+
* Semantic extent of a summary: the first source message compaction retained
|
|
306
|
+
* verbatim, meaning everything before it is covered. Distinct from `boundary`,
|
|
307
|
+
* which records where the block was emitted — a retained recency tail sits
|
|
308
|
+
* *before* the block's own position, so position alone cannot say what the
|
|
309
|
+
* summary replaced.
|
|
310
|
+
*
|
|
311
|
+
* Anchored to the retained side rather than the covered side so that a source
|
|
312
|
+
* message expanding into several messages (a steer splits an assistant entry
|
|
313
|
+
* into pre-steer, steer, and post-steer entries sharing one ID) stays whole:
|
|
314
|
+
* such a message is the retained anchor and survives intact.
|
|
315
|
+
*/
|
|
316
|
+
export type SummaryCoverage = {
|
|
317
|
+
retainedFromMessageId: string;
|
|
318
|
+
};
|
|
319
|
+
|
|
304
320
|
export type SummaryContentBlock = {
|
|
305
321
|
type: ContentTypes.SUMMARY;
|
|
306
322
|
content?: MessageContentComplex[];
|
|
323
|
+
/** Injection budget: provider output-token space when usage was reported, plus
|
|
324
|
+
* the wrapper added at injection time. Not comparable with per-message counts
|
|
325
|
+
* such as `indexTokenCountMap`, which are in the consumer's own tokenizer. */
|
|
307
326
|
tokenCount?: number;
|
|
327
|
+
coverage?: SummaryCoverage;
|
|
308
328
|
boundary?: SummaryBoundary;
|
|
309
329
|
summaryVersion?: number;
|
|
310
330
|
model?: string;
|