@librechat/agents 3.1.67 → 3.1.68-dev.1
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/agents/AgentContext.cjs +23 -3
- package/dist/cjs/agents/AgentContext.cjs.map +1 -1
- package/dist/cjs/common/enum.cjs +16 -1
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +91 -0
- package/dist/cjs/graphs/Graph.cjs.map +1 -1
- package/dist/cjs/graphs/MultiAgentGraph.cjs +36 -0
- package/dist/cjs/graphs/MultiAgentGraph.cjs.map +1 -1
- package/dist/cjs/hooks/HookRegistry.cjs +162 -0
- package/dist/cjs/hooks/HookRegistry.cjs.map +1 -0
- package/dist/cjs/hooks/executeHooks.cjs +276 -0
- package/dist/cjs/hooks/executeHooks.cjs.map +1 -0
- package/dist/cjs/hooks/matchers.cjs +256 -0
- package/dist/cjs/hooks/matchers.cjs.map +1 -0
- package/dist/cjs/hooks/types.cjs +27 -0
- package/dist/cjs/hooks/types.cjs.map +1 -0
- package/dist/cjs/main.cjs +54 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/format.cjs +74 -12
- package/dist/cjs/messages/format.cjs.map +1 -1
- package/dist/cjs/run.cjs +111 -0
- package/dist/cjs/run.cjs.map +1 -1
- package/dist/cjs/summarization/index.cjs +41 -0
- package/dist/cjs/summarization/index.cjs.map +1 -1
- package/dist/cjs/summarization/node.cjs +165 -19
- package/dist/cjs/summarization/node.cjs.map +1 -1
- package/dist/cjs/tools/BashExecutor.cjs +165 -0
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +287 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
- package/dist/cjs/tools/CodeExecutor.cjs +0 -9
- package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs +7 -23
- package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
- package/dist/cjs/tools/ReadFile.cjs +43 -0
- package/dist/cjs/tools/ReadFile.cjs.map +1 -0
- package/dist/cjs/tools/SkillTool.cjs +50 -0
- package/dist/cjs/tools/SkillTool.cjs.map +1 -0
- package/dist/cjs/tools/SubagentTool.cjs +92 -0
- package/dist/cjs/tools/SubagentTool.cjs.map +1 -0
- package/dist/cjs/tools/ToolNode.cjs +304 -140
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/ToolSearch.cjs +2 -13
- package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +84 -0
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs +511 -0
- package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -0
- package/dist/esm/agents/AgentContext.mjs +23 -3
- package/dist/esm/agents/AgentContext.mjs.map +1 -1
- package/dist/esm/common/enum.mjs +15 -2
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +91 -0
- package/dist/esm/graphs/Graph.mjs.map +1 -1
- package/dist/esm/graphs/MultiAgentGraph.mjs +36 -0
- package/dist/esm/graphs/MultiAgentGraph.mjs.map +1 -1
- package/dist/esm/hooks/HookRegistry.mjs +160 -0
- package/dist/esm/hooks/HookRegistry.mjs.map +1 -0
- package/dist/esm/hooks/executeHooks.mjs +273 -0
- package/dist/esm/hooks/executeHooks.mjs.map +1 -0
- package/dist/esm/hooks/matchers.mjs +251 -0
- package/dist/esm/hooks/matchers.mjs.map +1 -0
- package/dist/esm/hooks/types.mjs +25 -0
- package/dist/esm/hooks/types.mjs.map +1 -0
- package/dist/esm/main.mjs +13 -2
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/format.mjs +66 -4
- package/dist/esm/messages/format.mjs.map +1 -1
- package/dist/esm/run.mjs +111 -0
- package/dist/esm/run.mjs.map +1 -1
- package/dist/esm/summarization/index.mjs +41 -1
- package/dist/esm/summarization/index.mjs.map +1 -1
- package/dist/esm/summarization/node.mjs +165 -19
- package/dist/esm/summarization/node.mjs.map +1 -1
- package/dist/esm/tools/BashExecutor.mjs +159 -0
- package/dist/esm/tools/BashExecutor.mjs.map +1 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +278 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
- package/dist/esm/tools/CodeExecutor.mjs +0 -9
- package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
- package/dist/esm/tools/ProgrammaticToolCalling.mjs +8 -24
- package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
- package/dist/esm/tools/ReadFile.mjs +38 -0
- package/dist/esm/tools/ReadFile.mjs.map +1 -0
- package/dist/esm/tools/SkillTool.mjs +45 -0
- package/dist/esm/tools/SkillTool.mjs.map +1 -0
- package/dist/esm/tools/SubagentTool.mjs +85 -0
- package/dist/esm/tools/SubagentTool.mjs.map +1 -0
- package/dist/esm/tools/ToolNode.mjs +306 -142
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/ToolSearch.mjs +3 -14
- package/dist/esm/tools/ToolSearch.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +82 -0
- package/dist/esm/tools/skillCatalog.mjs.map +1 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs +505 -0
- package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -0
- package/dist/types/agents/AgentContext.d.ts +6 -0
- package/dist/types/common/enum.d.ts +10 -2
- package/dist/types/graphs/Graph.d.ts +2 -0
- package/dist/types/graphs/MultiAgentGraph.d.ts +12 -0
- package/dist/types/hooks/HookRegistry.d.ts +56 -0
- package/dist/types/hooks/executeHooks.d.ts +79 -0
- package/dist/types/hooks/index.d.ts +6 -0
- package/dist/types/hooks/matchers.d.ts +95 -0
- package/dist/types/hooks/types.d.ts +320 -0
- package/dist/types/index.d.ts +8 -0
- package/dist/types/messages/format.d.ts +2 -1
- package/dist/types/run.d.ts +1 -0
- package/dist/types/summarization/index.d.ts +2 -0
- package/dist/types/summarization/node.d.ts +2 -0
- package/dist/types/tools/BashExecutor.d.ts +45 -0
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
- package/dist/types/tools/ProgrammaticToolCalling.d.ts +4 -9
- package/dist/types/tools/ReadFile.d.ts +28 -0
- package/dist/types/tools/SkillTool.d.ts +40 -0
- package/dist/types/tools/SubagentTool.d.ts +36 -0
- package/dist/types/tools/ToolNode.d.ts +24 -2
- package/dist/types/tools/ToolSearch.d.ts +2 -2
- package/dist/types/tools/skillCatalog.d.ts +19 -0
- package/dist/types/tools/subagent/SubagentExecutor.d.ts +137 -0
- package/dist/types/tools/subagent/index.d.ts +2 -0
- package/dist/types/types/graph.d.ts +61 -2
- package/dist/types/types/index.d.ts +1 -0
- package/dist/types/types/run.d.ts +20 -0
- package/dist/types/types/skill.d.ts +9 -0
- package/dist/types/types/tools.d.ts +38 -10
- package/package.json +5 -1
- package/src/agents/AgentContext.ts +26 -2
- package/src/common/enum.ts +15 -1
- package/src/graphs/Graph.ts +113 -0
- package/src/graphs/MultiAgentGraph.ts +39 -0
- package/src/graphs/__tests__/MultiAgentGraph.test.ts +91 -0
- package/src/hooks/HookRegistry.ts +208 -0
- package/src/hooks/__tests__/HookRegistry.test.ts +190 -0
- package/src/hooks/__tests__/compactHooks.test.ts +214 -0
- package/src/hooks/__tests__/executeHooks.test.ts +1013 -0
- package/src/hooks/__tests__/integration.test.ts +337 -0
- package/src/hooks/__tests__/matchers.test.ts +238 -0
- package/src/hooks/__tests__/toolHooks.test.ts +669 -0
- package/src/hooks/executeHooks.ts +375 -0
- package/src/hooks/index.ts +57 -0
- package/src/hooks/matchers.ts +280 -0
- package/src/hooks/types.ts +404 -0
- package/src/index.ts +10 -0
- package/src/messages/format.ts +74 -4
- package/src/messages/formatAgentMessages.skills.test.ts +334 -0
- package/src/run.ts +126 -0
- package/src/scripts/multi-agent-subagent.ts +246 -0
- package/src/scripts/programmatic_exec.ts +1 -10
- package/src/scripts/subagent-event-driven-debug.ts +190 -0
- package/src/scripts/subagent-tools-debug.ts +160 -0
- package/src/scripts/test_code_api.ts +0 -7
- package/src/scripts/tool_search.ts +1 -10
- package/src/specs/subagent.test.ts +305 -0
- package/src/summarization/__tests__/node.test.ts +42 -0
- package/src/summarization/__tests__/trigger.test.ts +100 -1
- package/src/summarization/index.ts +47 -0
- package/src/summarization/node.ts +202 -24
- package/src/tools/BashExecutor.ts +193 -0
- package/src/tools/BashProgrammaticToolCalling.ts +381 -0
- package/src/tools/CodeExecutor.ts +0 -11
- package/src/tools/ProgrammaticToolCalling.ts +4 -29
- package/src/tools/ReadFile.ts +39 -0
- package/src/tools/SkillTool.ts +46 -0
- package/src/tools/SubagentTool.ts +100 -0
- package/src/tools/ToolNode.ts +391 -169
- package/src/tools/ToolSearch.ts +3 -19
- package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +7 -8
- package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -1
- package/src/tools/__tests__/ReadFile.test.ts +44 -0
- package/src/tools/__tests__/SkillTool.test.ts +442 -0
- package/src/tools/__tests__/SubagentExecutor.test.ts +1148 -0
- package/src/tools/__tests__/SubagentTool.test.ts +149 -0
- package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
- package/src/tools/__tests__/ToolSearch.integration.test.ts +7 -8
- package/src/tools/__tests__/skillCatalog.test.ts +161 -0
- package/src/tools/__tests__/subagentHooks.test.ts +215 -0
- package/src/tools/skillCatalog.ts +126 -0
- package/src/tools/subagent/SubagentExecutor.ts +676 -0
- package/src/tools/subagent/index.ts +13 -0
- package/src/types/graph.ts +80 -1
- package/src/types/index.ts +1 -0
- package/src/types/run.ts +20 -0
- package/src/types/skill.ts +11 -0
- package/src/types/tools.ts +41 -10
|
@@ -0,0 +1,190 @@
|
|
|
1
|
+
// src/hooks/__tests__/HookRegistry.test.ts
|
|
2
|
+
import { HookRegistry } from '../HookRegistry';
|
|
3
|
+
import type {
|
|
4
|
+
HookMatcher,
|
|
5
|
+
HookCallback,
|
|
6
|
+
PreToolUseHookOutput,
|
|
7
|
+
PostToolUseHookOutput,
|
|
8
|
+
} from '../types';
|
|
9
|
+
|
|
10
|
+
const noop: HookCallback<
|
|
11
|
+
'PreToolUse'
|
|
12
|
+
> = async (): Promise<PreToolUseHookOutput> => ({});
|
|
13
|
+
const noopPost: HookCallback<
|
|
14
|
+
'PostToolUse'
|
|
15
|
+
> = async (): Promise<PostToolUseHookOutput> => ({});
|
|
16
|
+
|
|
17
|
+
function makePreToolUseMatcher(pattern?: string): HookMatcher<'PreToolUse'> {
|
|
18
|
+
return {
|
|
19
|
+
pattern,
|
|
20
|
+
hooks: [noop],
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function makePostToolUseMatcher(): HookMatcher<'PostToolUse'> {
|
|
25
|
+
return {
|
|
26
|
+
hooks: [noopPost],
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
describe('HookRegistry', () => {
|
|
31
|
+
describe('global registration', () => {
|
|
32
|
+
it('stores a matcher and returns it via getMatchers', () => {
|
|
33
|
+
const registry = new HookRegistry();
|
|
34
|
+
const matcher = makePreToolUseMatcher('Bash');
|
|
35
|
+
registry.register('PreToolUse', matcher);
|
|
36
|
+
const matchers = registry.getMatchers('PreToolUse');
|
|
37
|
+
expect(matchers).toHaveLength(1);
|
|
38
|
+
expect(matchers[0]).toBe(matcher);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it('keeps registrations isolated across events', () => {
|
|
42
|
+
const registry = new HookRegistry();
|
|
43
|
+
const pre = makePreToolUseMatcher('Bash');
|
|
44
|
+
const post = makePostToolUseMatcher();
|
|
45
|
+
registry.register('PreToolUse', pre);
|
|
46
|
+
registry.register('PostToolUse', post);
|
|
47
|
+
expect(registry.getMatchers('PreToolUse')).toEqual([pre]);
|
|
48
|
+
expect(registry.getMatchers('PostToolUse')).toEqual([post]);
|
|
49
|
+
expect(registry.getMatchers('Stop')).toEqual([]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('unregister removes the matcher from the registry', () => {
|
|
53
|
+
const registry = new HookRegistry();
|
|
54
|
+
const matcher = makePreToolUseMatcher();
|
|
55
|
+
const unregister = registry.register('PreToolUse', matcher);
|
|
56
|
+
expect(registry.getMatchers('PreToolUse')).toHaveLength(1);
|
|
57
|
+
unregister();
|
|
58
|
+
expect(registry.getMatchers('PreToolUse')).toHaveLength(0);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('hasHookFor reflects registration state', () => {
|
|
62
|
+
const registry = new HookRegistry();
|
|
63
|
+
expect(registry.hasHookFor('PreToolUse')).toBe(false);
|
|
64
|
+
registry.register('PreToolUse', makePreToolUseMatcher());
|
|
65
|
+
expect(registry.hasHookFor('PreToolUse')).toBe(true);
|
|
66
|
+
expect(registry.hasHookFor('PostToolUse')).toBe(false);
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
it('supports multiple matchers on the same event', () => {
|
|
70
|
+
const registry = new HookRegistry();
|
|
71
|
+
const a = makePreToolUseMatcher('Bash');
|
|
72
|
+
const b = makePreToolUseMatcher('Edit');
|
|
73
|
+
registry.register('PreToolUse', a);
|
|
74
|
+
registry.register('PreToolUse', b);
|
|
75
|
+
const matchers = registry.getMatchers('PreToolUse');
|
|
76
|
+
expect(matchers).toHaveLength(2);
|
|
77
|
+
expect(matchers).toContain(a);
|
|
78
|
+
expect(matchers).toContain(b);
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
it('returns a fresh array on each getMatchers call', () => {
|
|
82
|
+
const registry = new HookRegistry();
|
|
83
|
+
registry.register('PreToolUse', makePreToolUseMatcher());
|
|
84
|
+
const first = registry.getMatchers('PreToolUse');
|
|
85
|
+
const second = registry.getMatchers('PreToolUse');
|
|
86
|
+
expect(first).not.toBe(second);
|
|
87
|
+
first.length = 0;
|
|
88
|
+
expect(registry.getMatchers('PreToolUse')).toHaveLength(1);
|
|
89
|
+
});
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
describe('session registration', () => {
|
|
93
|
+
it('scopes matchers to a single session', () => {
|
|
94
|
+
const registry = new HookRegistry();
|
|
95
|
+
const sessionA = makePreToolUseMatcher('Bash');
|
|
96
|
+
const sessionB = makePreToolUseMatcher('Edit');
|
|
97
|
+
registry.registerSession('run-a', 'PreToolUse', sessionA);
|
|
98
|
+
registry.registerSession('run-b', 'PreToolUse', sessionB);
|
|
99
|
+
|
|
100
|
+
expect(registry.getMatchers('PreToolUse', 'run-a')).toEqual([sessionA]);
|
|
101
|
+
expect(registry.getMatchers('PreToolUse', 'run-b')).toEqual([sessionB]);
|
|
102
|
+
expect(registry.getMatchers('PreToolUse')).toEqual([]);
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
it('merges global matchers in front of session matchers', () => {
|
|
106
|
+
const registry = new HookRegistry();
|
|
107
|
+
const global = makePreToolUseMatcher('global');
|
|
108
|
+
const session = makePreToolUseMatcher('session');
|
|
109
|
+
registry.register('PreToolUse', global);
|
|
110
|
+
registry.registerSession('run-a', 'PreToolUse', session);
|
|
111
|
+
|
|
112
|
+
const matchers = registry.getMatchers('PreToolUse', 'run-a');
|
|
113
|
+
expect(matchers).toEqual([global, session]);
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('clearSession drops only the given session', () => {
|
|
117
|
+
const registry = new HookRegistry();
|
|
118
|
+
const a = makePreToolUseMatcher('a');
|
|
119
|
+
const b = makePreToolUseMatcher('b');
|
|
120
|
+
registry.registerSession('run-a', 'PreToolUse', a);
|
|
121
|
+
registry.registerSession('run-b', 'PreToolUse', b);
|
|
122
|
+
|
|
123
|
+
registry.clearSession('run-a');
|
|
124
|
+
expect(registry.getMatchers('PreToolUse', 'run-a')).toEqual([]);
|
|
125
|
+
expect(registry.getMatchers('PreToolUse', 'run-b')).toEqual([b]);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('removeMatcher removes from the correct scope', () => {
|
|
129
|
+
const registry = new HookRegistry();
|
|
130
|
+
const global = makePreToolUseMatcher('global');
|
|
131
|
+
const session = makePreToolUseMatcher('session');
|
|
132
|
+
registry.register('PreToolUse', global);
|
|
133
|
+
registry.registerSession('run-a', 'PreToolUse', session);
|
|
134
|
+
|
|
135
|
+
expect(registry.removeMatcher('PreToolUse', session, 'run-a')).toBe(true);
|
|
136
|
+
expect(registry.getMatchers('PreToolUse', 'run-a')).toEqual([global]);
|
|
137
|
+
|
|
138
|
+
expect(registry.removeMatcher('PreToolUse', global)).toBe(true);
|
|
139
|
+
expect(registry.getMatchers('PreToolUse', 'run-a')).toEqual([]);
|
|
140
|
+
});
|
|
141
|
+
|
|
142
|
+
it('removeMatcher returns false when the matcher is not found', () => {
|
|
143
|
+
const registry = new HookRegistry();
|
|
144
|
+
const orphan = makePreToolUseMatcher('orphan');
|
|
145
|
+
expect(registry.removeMatcher('PreToolUse', orphan)).toBe(false);
|
|
146
|
+
expect(registry.removeMatcher('PreToolUse', orphan, 'run-a')).toBe(false);
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('session unregister function removes only that matcher', () => {
|
|
150
|
+
const registry = new HookRegistry();
|
|
151
|
+
const a = makePreToolUseMatcher('a');
|
|
152
|
+
const b = makePreToolUseMatcher('b');
|
|
153
|
+
const unregisterA = registry.registerSession('run-a', 'PreToolUse', a);
|
|
154
|
+
registry.registerSession('run-a', 'PreToolUse', b);
|
|
155
|
+
|
|
156
|
+
unregisterA();
|
|
157
|
+
expect(registry.getMatchers('PreToolUse', 'run-a')).toEqual([b]);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('hasHookFor honours the sessionId parameter', () => {
|
|
161
|
+
const registry = new HookRegistry();
|
|
162
|
+
registry.registerSession('run-a', 'PreToolUse', makePreToolUseMatcher());
|
|
163
|
+
expect(registry.hasHookFor('PreToolUse')).toBe(false);
|
|
164
|
+
expect(registry.hasHookFor('PreToolUse', 'run-a')).toBe(true);
|
|
165
|
+
expect(registry.hasHookFor('PreToolUse', 'run-b')).toBe(false);
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
describe('session isolation under parallel registration', () => {
|
|
170
|
+
it('does not leak matchers between sessions registered in parallel', async () => {
|
|
171
|
+
const registry = new HookRegistry();
|
|
172
|
+
const sessions = Array.from({ length: 50 }, (_, i) => `run-${i}`);
|
|
173
|
+
await Promise.all(
|
|
174
|
+
sessions.map(async (sid): Promise<void> => {
|
|
175
|
+
registry.registerSession(
|
|
176
|
+
sid,
|
|
177
|
+
'PreToolUse',
|
|
178
|
+
makePreToolUseMatcher(sid)
|
|
179
|
+
);
|
|
180
|
+
})
|
|
181
|
+
);
|
|
182
|
+
|
|
183
|
+
for (const sid of sessions) {
|
|
184
|
+
const matchers = registry.getMatchers('PreToolUse', sid);
|
|
185
|
+
expect(matchers).toHaveLength(1);
|
|
186
|
+
expect(matchers[0]?.pattern).toBe(sid);
|
|
187
|
+
}
|
|
188
|
+
});
|
|
189
|
+
});
|
|
190
|
+
});
|
|
@@ -0,0 +1,214 @@
|
|
|
1
|
+
// src/hooks/__tests__/compactHooks.test.ts
|
|
2
|
+
import { HumanMessage, AIMessage } from '@langchain/core/messages';
|
|
3
|
+
import { FakeListChatModel } from '@langchain/core/utils/testing';
|
|
4
|
+
import { HookRegistry } from '../HookRegistry';
|
|
5
|
+
import { Run } from '@/run';
|
|
6
|
+
import * as providers from '@/llm/providers';
|
|
7
|
+
import { ToolEndHandler, ModelEndHandler } from '@/events';
|
|
8
|
+
import { createTokenCounter } from '@/utils/tokens';
|
|
9
|
+
import { Providers, GraphEvents } from '@/common';
|
|
10
|
+
import type * as t from '@/types';
|
|
11
|
+
import type {
|
|
12
|
+
HookCallback,
|
|
13
|
+
PreCompactHookInput,
|
|
14
|
+
PreCompactHookOutput,
|
|
15
|
+
PostCompactHookInput,
|
|
16
|
+
PostCompactHookOutput,
|
|
17
|
+
} from '../types';
|
|
18
|
+
|
|
19
|
+
const SUMMARY_RESPONSE = '## Summary\nUser asked a question and got an answer.';
|
|
20
|
+
|
|
21
|
+
const callerConfig = {
|
|
22
|
+
configurable: { thread_id: 'compact-test' },
|
|
23
|
+
streamMode: 'values' as const,
|
|
24
|
+
version: 'v2' as const,
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
let getChatModelClassSpy: jest.SpyInstance;
|
|
28
|
+
const originalGetChatModelClass = providers.getChatModelClass;
|
|
29
|
+
|
|
30
|
+
function mockSummarizationModel(): void {
|
|
31
|
+
getChatModelClassSpy = jest
|
|
32
|
+
.spyOn(providers, 'getChatModelClass')
|
|
33
|
+
.mockImplementation(((provider: Providers) => {
|
|
34
|
+
if (provider === Providers.OPENAI) {
|
|
35
|
+
return class extends FakeListChatModel {
|
|
36
|
+
constructor(
|
|
37
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
38
|
+
_options: any
|
|
39
|
+
) {
|
|
40
|
+
super({ responses: [SUMMARY_RESPONSE] });
|
|
41
|
+
}
|
|
42
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
43
|
+
} as any;
|
|
44
|
+
}
|
|
45
|
+
return originalGetChatModelClass(provider);
|
|
46
|
+
}) as typeof providers.getChatModelClass);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function buildConversation(): t.IState {
|
|
50
|
+
const messages: import('@langchain/core/messages').BaseMessage[] = [];
|
|
51
|
+
for (let i = 0; i < 20; i++) {
|
|
52
|
+
messages.push(new HumanMessage(`Question ${i}: ` + 'padding '.repeat(50)));
|
|
53
|
+
messages.push(new AIMessage(`Answer ${i}: ` + 'response '.repeat(50)));
|
|
54
|
+
}
|
|
55
|
+
return { messages };
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
async function createCompactingRun(
|
|
59
|
+
tokenCounter: t.TokenCounter,
|
|
60
|
+
hooks?: HookRegistry,
|
|
61
|
+
runId = 'compact-run'
|
|
62
|
+
): Promise<Run<t.IState>> {
|
|
63
|
+
const conversation = buildConversation();
|
|
64
|
+
const indexTokenCountMap: Record<string, number> = {};
|
|
65
|
+
for (let i = 0; i < conversation.messages.length; i++) {
|
|
66
|
+
indexTokenCountMap[String(i)] = tokenCounter(conversation.messages[i]);
|
|
67
|
+
}
|
|
68
|
+
return Run.create<t.IState>({
|
|
69
|
+
runId,
|
|
70
|
+
graphConfig: {
|
|
71
|
+
type: 'standard',
|
|
72
|
+
llmConfig: {
|
|
73
|
+
provider: Providers.OPENAI,
|
|
74
|
+
streaming: true,
|
|
75
|
+
streamUsage: false,
|
|
76
|
+
},
|
|
77
|
+
instructions: 'Be concise.',
|
|
78
|
+
maxContextTokens: 200,
|
|
79
|
+
summarizationEnabled: true,
|
|
80
|
+
summarizationConfig: {
|
|
81
|
+
provider: Providers.OPENAI,
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
returnContent: true,
|
|
85
|
+
skipCleanup: true,
|
|
86
|
+
customHandlers: {
|
|
87
|
+
[GraphEvents.TOOL_END]: new ToolEndHandler(),
|
|
88
|
+
[GraphEvents.CHAT_MODEL_END]: new ModelEndHandler(),
|
|
89
|
+
},
|
|
90
|
+
hooks,
|
|
91
|
+
tokenCounter,
|
|
92
|
+
indexTokenCountMap,
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
describe('Compaction hook integration', () => {
|
|
97
|
+
jest.setTimeout(30_000);
|
|
98
|
+
|
|
99
|
+
let tokenCounter: t.TokenCounter;
|
|
100
|
+
|
|
101
|
+
beforeAll(async () => {
|
|
102
|
+
tokenCounter = await createTokenCounter();
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
beforeEach(() => {
|
|
106
|
+
mockSummarizationModel();
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
afterEach(() => {
|
|
110
|
+
getChatModelClassSpy.mockRestore();
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
describe('PreCompact', () => {
|
|
114
|
+
it('fires with messagesBeforeCount and trigger', async () => {
|
|
115
|
+
const registry = new HookRegistry();
|
|
116
|
+
let captured: PreCompactHookInput | undefined;
|
|
117
|
+
const hook: HookCallback<'PreCompact'> = async (
|
|
118
|
+
input
|
|
119
|
+
): Promise<PreCompactHookOutput> => {
|
|
120
|
+
captured = input;
|
|
121
|
+
return {};
|
|
122
|
+
};
|
|
123
|
+
registry.register('PreCompact', { hooks: [hook] });
|
|
124
|
+
|
|
125
|
+
const run = await createCompactingRun(tokenCounter, registry);
|
|
126
|
+
run.Graph!.overrideTestModel(['Final answer after compaction.']);
|
|
127
|
+
const inputs = buildConversation();
|
|
128
|
+
await run.processStream(inputs, callerConfig);
|
|
129
|
+
|
|
130
|
+
expect(captured).toBeDefined();
|
|
131
|
+
expect(captured!.hook_event_name).toBe('PreCompact');
|
|
132
|
+
expect(captured!.messagesBeforeCount).toBeGreaterThan(0);
|
|
133
|
+
expect(captured!.runId).toBe('compact-run');
|
|
134
|
+
expect(captured!.threadId).toBe('compact-test');
|
|
135
|
+
expect(captured!.trigger).toBe('default');
|
|
136
|
+
expect(captured!.agentId).toBeDefined();
|
|
137
|
+
});
|
|
138
|
+
});
|
|
139
|
+
|
|
140
|
+
describe('PostCompact', () => {
|
|
141
|
+
it('fires with summary text after compaction', async () => {
|
|
142
|
+
const registry = new HookRegistry();
|
|
143
|
+
let captured: PostCompactHookInput | undefined;
|
|
144
|
+
const hook: HookCallback<'PostCompact'> = async (
|
|
145
|
+
input
|
|
146
|
+
): Promise<PostCompactHookOutput> => {
|
|
147
|
+
captured = input;
|
|
148
|
+
return {};
|
|
149
|
+
};
|
|
150
|
+
registry.register('PostCompact', { hooks: [hook] });
|
|
151
|
+
|
|
152
|
+
const run = await createCompactingRun(tokenCounter, registry);
|
|
153
|
+
run.Graph!.overrideTestModel(['Final answer after compaction.']);
|
|
154
|
+
const inputs = buildConversation();
|
|
155
|
+
await run.processStream(inputs, callerConfig);
|
|
156
|
+
|
|
157
|
+
expect(captured).toBeDefined();
|
|
158
|
+
expect(captured!.hook_event_name).toBe('PostCompact');
|
|
159
|
+
expect(captured!.threadId).toBe('compact-test');
|
|
160
|
+
expect(captured!.summary).toBe(SUMMARY_RESPONSE);
|
|
161
|
+
expect(captured!.messagesAfterCount).toBe(0);
|
|
162
|
+
expect(captured!.agentId).toBeDefined();
|
|
163
|
+
});
|
|
164
|
+
});
|
|
165
|
+
|
|
166
|
+
describe('error resilience', () => {
|
|
167
|
+
it('throwing PreCompact hook does not crash compaction', async () => {
|
|
168
|
+
const registry = new HookRegistry();
|
|
169
|
+
const throwingHook: HookCallback<
|
|
170
|
+
'PreCompact'
|
|
171
|
+
> = async (): Promise<PreCompactHookOutput> => {
|
|
172
|
+
throw new Error('pre hook crash');
|
|
173
|
+
};
|
|
174
|
+
registry.register('PreCompact', { hooks: [throwingHook] });
|
|
175
|
+
|
|
176
|
+
const run = await createCompactingRun(tokenCounter, registry);
|
|
177
|
+
run.Graph!.overrideTestModel(['Answer after compaction.']);
|
|
178
|
+
const inputs = buildConversation();
|
|
179
|
+
|
|
180
|
+
await expect(
|
|
181
|
+
run.processStream(inputs, callerConfig)
|
|
182
|
+
).resolves.not.toThrow();
|
|
183
|
+
});
|
|
184
|
+
|
|
185
|
+
it('throwing PostCompact hook does not crash compaction', async () => {
|
|
186
|
+
const registry = new HookRegistry();
|
|
187
|
+
const throwingHook: HookCallback<
|
|
188
|
+
'PostCompact'
|
|
189
|
+
> = async (): Promise<PostCompactHookOutput> => {
|
|
190
|
+
throw new Error('post hook crash');
|
|
191
|
+
};
|
|
192
|
+
registry.register('PostCompact', { hooks: [throwingHook] });
|
|
193
|
+
|
|
194
|
+
const run = await createCompactingRun(tokenCounter, registry);
|
|
195
|
+
run.Graph!.overrideTestModel(['Answer after compaction.']);
|
|
196
|
+
const inputs = buildConversation();
|
|
197
|
+
|
|
198
|
+
await expect(
|
|
199
|
+
run.processStream(inputs, callerConfig)
|
|
200
|
+
).resolves.not.toThrow();
|
|
201
|
+
});
|
|
202
|
+
});
|
|
203
|
+
|
|
204
|
+
describe('no-hooks baseline', () => {
|
|
205
|
+
it('summarization works identically without hooks', async () => {
|
|
206
|
+
const run = await createCompactingRun(tokenCounter);
|
|
207
|
+
run.Graph!.overrideTestModel(['Answer.']);
|
|
208
|
+
const inputs = buildConversation();
|
|
209
|
+
const result = await run.processStream(inputs, callerConfig);
|
|
210
|
+
|
|
211
|
+
expect(result).toBeDefined();
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
});
|