@librechat/agents 3.1.66-dev.0 → 3.1.67

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.
Files changed (120) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +24 -15
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +0 -13
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +0 -3
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/main.cjs +0 -40
  8. package/dist/cjs/main.cjs.map +1 -1
  9. package/dist/cjs/messages/format.cjs +12 -74
  10. package/dist/cjs/messages/format.cjs.map +1 -1
  11. package/dist/cjs/run.cjs +0 -111
  12. package/dist/cjs/run.cjs.map +1 -1
  13. package/dist/cjs/tools/ToolNode.cjs +140 -304
  14. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  15. package/dist/esm/agents/AgentContext.mjs +24 -15
  16. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  17. package/dist/esm/common/enum.mjs +1 -12
  18. package/dist/esm/common/enum.mjs.map +1 -1
  19. package/dist/esm/graphs/Graph.mjs +0 -3
  20. package/dist/esm/graphs/Graph.mjs.map +1 -1
  21. package/dist/esm/main.mjs +1 -10
  22. package/dist/esm/main.mjs.map +1 -1
  23. package/dist/esm/messages/format.mjs +4 -66
  24. package/dist/esm/messages/format.mjs.map +1 -1
  25. package/dist/esm/run.mjs +0 -111
  26. package/dist/esm/run.mjs.map +1 -1
  27. package/dist/esm/tools/ToolNode.mjs +142 -306
  28. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  29. package/dist/types/agents/AgentContext.d.ts +6 -0
  30. package/dist/types/common/enum.d.ts +1 -7
  31. package/dist/types/graphs/Graph.d.ts +0 -2
  32. package/dist/types/index.d.ts +0 -6
  33. package/dist/types/messages/format.d.ts +1 -2
  34. package/dist/types/run.d.ts +0 -1
  35. package/dist/types/tools/ToolNode.d.ts +2 -24
  36. package/dist/types/types/index.d.ts +0 -1
  37. package/dist/types/types/llm.d.ts +14 -2
  38. package/dist/types/types/run.d.ts +0 -20
  39. package/dist/types/types/tools.d.ts +1 -38
  40. package/package.json +1 -1
  41. package/src/agents/AgentContext.ts +28 -15
  42. package/src/agents/__tests__/AgentContext.test.ts +110 -0
  43. package/src/common/enum.ts +0 -12
  44. package/src/graphs/Graph.ts +0 -4
  45. package/src/index.ts +0 -8
  46. package/src/messages/format.ts +4 -74
  47. package/src/run.ts +0 -126
  48. package/src/tools/ToolNode.ts +169 -391
  49. package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
  50. package/src/types/index.ts +0 -1
  51. package/src/types/llm.ts +16 -2
  52. package/src/types/run.ts +0 -20
  53. package/src/types/tools.ts +1 -41
  54. package/dist/cjs/hooks/HookRegistry.cjs +0 -162
  55. package/dist/cjs/hooks/HookRegistry.cjs.map +0 -1
  56. package/dist/cjs/hooks/executeHooks.cjs +0 -276
  57. package/dist/cjs/hooks/executeHooks.cjs.map +0 -1
  58. package/dist/cjs/hooks/matchers.cjs +0 -256
  59. package/dist/cjs/hooks/matchers.cjs.map +0 -1
  60. package/dist/cjs/hooks/types.cjs +0 -27
  61. package/dist/cjs/hooks/types.cjs.map +0 -1
  62. package/dist/cjs/tools/BashExecutor.cjs +0 -175
  63. package/dist/cjs/tools/BashExecutor.cjs.map +0 -1
  64. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +0 -296
  65. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +0 -1
  66. package/dist/cjs/tools/ReadFile.cjs +0 -43
  67. package/dist/cjs/tools/ReadFile.cjs.map +0 -1
  68. package/dist/cjs/tools/SkillTool.cjs +0 -50
  69. package/dist/cjs/tools/SkillTool.cjs.map +0 -1
  70. package/dist/cjs/tools/skillCatalog.cjs +0 -84
  71. package/dist/cjs/tools/skillCatalog.cjs.map +0 -1
  72. package/dist/esm/hooks/HookRegistry.mjs +0 -160
  73. package/dist/esm/hooks/HookRegistry.mjs.map +0 -1
  74. package/dist/esm/hooks/executeHooks.mjs +0 -273
  75. package/dist/esm/hooks/executeHooks.mjs.map +0 -1
  76. package/dist/esm/hooks/matchers.mjs +0 -251
  77. package/dist/esm/hooks/matchers.mjs.map +0 -1
  78. package/dist/esm/hooks/types.mjs +0 -25
  79. package/dist/esm/hooks/types.mjs.map +0 -1
  80. package/dist/esm/tools/BashExecutor.mjs +0 -169
  81. package/dist/esm/tools/BashExecutor.mjs.map +0 -1
  82. package/dist/esm/tools/BashProgrammaticToolCalling.mjs +0 -287
  83. package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +0 -1
  84. package/dist/esm/tools/ReadFile.mjs +0 -38
  85. package/dist/esm/tools/ReadFile.mjs.map +0 -1
  86. package/dist/esm/tools/SkillTool.mjs +0 -45
  87. package/dist/esm/tools/SkillTool.mjs.map +0 -1
  88. package/dist/esm/tools/skillCatalog.mjs +0 -82
  89. package/dist/esm/tools/skillCatalog.mjs.map +0 -1
  90. package/dist/types/hooks/HookRegistry.d.ts +0 -56
  91. package/dist/types/hooks/executeHooks.d.ts +0 -79
  92. package/dist/types/hooks/index.d.ts +0 -6
  93. package/dist/types/hooks/matchers.d.ts +0 -95
  94. package/dist/types/hooks/types.d.ts +0 -309
  95. package/dist/types/tools/BashExecutor.d.ts +0 -45
  96. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +0 -72
  97. package/dist/types/tools/ReadFile.d.ts +0 -28
  98. package/dist/types/tools/SkillTool.d.ts +0 -40
  99. package/dist/types/tools/skillCatalog.d.ts +0 -19
  100. package/dist/types/types/skill.d.ts +0 -9
  101. package/src/hooks/HookRegistry.ts +0 -208
  102. package/src/hooks/__tests__/HookRegistry.test.ts +0 -190
  103. package/src/hooks/__tests__/executeHooks.test.ts +0 -1013
  104. package/src/hooks/__tests__/integration.test.ts +0 -337
  105. package/src/hooks/__tests__/matchers.test.ts +0 -238
  106. package/src/hooks/__tests__/toolHooks.test.ts +0 -669
  107. package/src/hooks/executeHooks.ts +0 -375
  108. package/src/hooks/index.ts +0 -55
  109. package/src/hooks/matchers.ts +0 -280
  110. package/src/hooks/types.ts +0 -388
  111. package/src/messages/formatAgentMessages.skills.test.ts +0 -334
  112. package/src/tools/BashExecutor.ts +0 -205
  113. package/src/tools/BashProgrammaticToolCalling.ts +0 -397
  114. package/src/tools/ReadFile.ts +0 -39
  115. package/src/tools/SkillTool.ts +0 -46
  116. package/src/tools/__tests__/ReadFile.test.ts +0 -44
  117. package/src/tools/__tests__/SkillTool.test.ts +0 -442
  118. package/src/tools/__tests__/skillCatalog.test.ts +0 -161
  119. package/src/tools/skillCatalog.ts +0 -126
  120. package/src/types/skill.ts +0 -11
@@ -216,7 +216,7 @@ describe('ToolNode code execution session management', () => {
216
216
  toolNode as unknown as {
217
217
  storeCodeSessionFromResults: (
218
218
  results: t.ToolExecuteResult[],
219
- requestMap: Map<string, t.ToolCallRequest>
219
+ requests: t.ToolCallRequest[]
220
220
  ) => void;
221
221
  }
222
222
  ).storeCodeSessionFromResults.bind(toolNode);
@@ -233,7 +233,7 @@ describe('ToolNode code execution session management', () => {
233
233
  status: 'success',
234
234
  },
235
235
  ],
236
- new Map([['tc1', { id: 'tc1', name: Constants.EXECUTE_CODE, args: {} }]])
236
+ [{ id: 'tc1', name: Constants.EXECUTE_CODE, args: {} }]
237
237
  );
238
238
 
239
239
  const stored = sessions.get(
@@ -265,7 +265,7 @@ describe('ToolNode code execution session management', () => {
265
265
  toolNode as unknown as {
266
266
  storeCodeSessionFromResults: (
267
267
  results: t.ToolExecuteResult[],
268
- requestMap: Map<string, t.ToolCallRequest>
268
+ requests: t.ToolCallRequest[]
269
269
  ) => void;
270
270
  }
271
271
  ).storeCodeSessionFromResults.bind(toolNode);
@@ -279,7 +279,7 @@ describe('ToolNode code execution session management', () => {
279
279
  status: 'success',
280
280
  },
281
281
  ],
282
- new Map([['tc2', { id: 'tc2', name: Constants.EXECUTE_CODE, args: {} }]])
282
+ [{ id: 'tc2', name: Constants.EXECUTE_CODE, args: {} }]
283
283
  );
284
284
 
285
285
  const stored = sessions.get(
@@ -312,7 +312,7 @@ describe('ToolNode code execution session management', () => {
312
312
  toolNode as unknown as {
313
313
  storeCodeSessionFromResults: (
314
314
  results: t.ToolExecuteResult[],
315
- requestMap: Map<string, t.ToolCallRequest>
315
+ requests: t.ToolCallRequest[]
316
316
  ) => void;
317
317
  }
318
318
  ).storeCodeSessionFromResults.bind(toolNode);
@@ -329,7 +329,7 @@ describe('ToolNode code execution session management', () => {
329
329
  status: 'success',
330
330
  },
331
331
  ],
332
- new Map([['tc3', { id: 'tc3', name: Constants.EXECUTE_CODE, args: {} }]])
332
+ [{ id: 'tc3', name: Constants.EXECUTE_CODE, args: {} }]
333
333
  );
334
334
 
335
335
  const stored = sessions.get(
@@ -365,7 +365,7 @@ describe('ToolNode code execution session management', () => {
365
365
  toolNode as unknown as {
366
366
  storeCodeSessionFromResults: (
367
367
  results: t.ToolExecuteResult[],
368
- requestMap: Map<string, t.ToolCallRequest>
368
+ requests: t.ToolCallRequest[]
369
369
  ) => void;
370
370
  }
371
371
  ).storeCodeSessionFromResults.bind(toolNode);
@@ -379,7 +379,7 @@ describe('ToolNode code execution session management', () => {
379
379
  status: 'success',
380
380
  },
381
381
  ],
382
- new Map([['tc4', { id: 'tc4', name: Constants.EXECUTE_CODE, args: {} }]])
382
+ [{ id: 'tc4', name: Constants.EXECUTE_CODE, args: {} }]
383
383
  );
384
384
 
385
385
  const stored = sessions.get(
@@ -404,7 +404,7 @@ describe('ToolNode code execution session management', () => {
404
404
  toolNode as unknown as {
405
405
  storeCodeSessionFromResults: (
406
406
  results: t.ToolExecuteResult[],
407
- requestMap: Map<string, t.ToolCallRequest>
407
+ requests: t.ToolCallRequest[]
408
408
  ) => void;
409
409
  }
410
410
  ).storeCodeSessionFromResults.bind(toolNode);
@@ -418,7 +418,7 @@ describe('ToolNode code execution session management', () => {
418
418
  status: 'success',
419
419
  },
420
420
  ],
421
- new Map([['tc5', { id: 'tc5', name: 'web_search', args: {} }]])
421
+ [{ id: 'tc5', name: 'web_search', args: {} }]
422
422
  );
423
423
 
424
424
  expect(sessions.has(Constants.EXECUTE_CODE)).toBe(false);
@@ -438,7 +438,7 @@ describe('ToolNode code execution session management', () => {
438
438
  toolNode as unknown as {
439
439
  storeCodeSessionFromResults: (
440
440
  results: t.ToolExecuteResult[],
441
- requestMap: Map<string, t.ToolCallRequest>
441
+ requests: t.ToolCallRequest[]
442
442
  ) => void;
443
443
  }
444
444
  ).storeCodeSessionFromResults.bind(toolNode);
@@ -456,7 +456,7 @@ describe('ToolNode code execution session management', () => {
456
456
  errorMessage: 'execution failed',
457
457
  },
458
458
  ],
459
- new Map([['tc6', { id: 'tc6', name: Constants.EXECUTE_CODE, args: {} }]])
459
+ [{ id: 'tc6', name: Constants.EXECUTE_CODE, args: {} }]
460
460
  );
461
461
 
462
462
  expect(sessions.has(Constants.EXECUTE_CODE)).toBe(false);
@@ -2,7 +2,6 @@
2
2
  export * from './graph';
3
3
  export * from './llm';
4
4
  export * from './run';
5
- export * from './skill';
6
5
  export * from './stream';
7
6
  export * from './tools';
8
7
  export * from './summarize';
package/src/types/llm.ts CHANGED
@@ -45,7 +45,20 @@ export type AzureClientOptions = Partial<OpenAIChatInput> &
45
45
  } & BaseChatModelParams & {
46
46
  configuration?: OAIClientOptions;
47
47
  };
48
- export type ThinkingConfig = AnthropicInput['thinking'];
48
+ /**
49
+ * Controls whether Claude's reasoning content is returned in adaptive
50
+ * thinking responses. Added for Claude Opus 4.7, which omits thinking by
51
+ * default unless the caller opts in with `'summarized'`.
52
+ * @see https://platform.claude.com/docs/en/about-claude/models/whats-new-claude-4-7#thinking-content-omitted-by-default
53
+ */
54
+ export type ThinkingDisplay = 'summarized' | 'omitted';
55
+ export type ThinkingConfigAdaptive = {
56
+ type: 'adaptive';
57
+ display?: ThinkingDisplay;
58
+ };
59
+ export type ThinkingConfig =
60
+ | NonNullable<AnthropicInput['thinking']>
61
+ | ThinkingConfigAdaptive;
49
62
  export type ChatOpenAIToolType =
50
63
  | BindToolsInput
51
64
  | OpenAIClient.ChatCompletionTool;
@@ -60,7 +73,8 @@ export type GoogleThinkingConfig = {
60
73
  thinkingLevel?: string;
61
74
  };
62
75
  export type OpenAIClientOptions = ChatOpenAIFields;
63
- export type AnthropicClientOptions = AnthropicInput & {
76
+ export type AnthropicClientOptions = Omit<AnthropicInput, 'thinking'> & {
77
+ thinking?: ThinkingConfig;
64
78
  promptCache?: boolean;
65
79
  };
66
80
  export type MistralAIClientOptions = ChatMistralAIInput;
package/src/types/run.ts CHANGED
@@ -11,8 +11,6 @@ import type * as s from '@/types/stream';
11
11
  import type * as e from '@/common/enum';
12
12
  import type * as g from '@/types/graph';
13
13
  import type * as l from '@/types/llm';
14
- import type { ToolSessionMap } from '@/types/tools';
15
- import type { HookRegistry } from '@/hooks';
16
14
 
17
15
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
18
16
  export type ZodObjectAny = z.ZodObject<any, any, any, any>;
@@ -114,18 +112,6 @@ export type RunConfig = {
114
112
  runId: string;
115
113
  graphConfig: LegacyGraphConfig | StandardGraphConfig | MultiAgentGraphConfig;
116
114
  customHandlers?: Record<string, g.EventHandler>;
117
- /**
118
- * Pre-constructed hook registry for this run. Hooks fire at lifecycle
119
- * points in `processStream` (RunStart, UserPromptSubmit, Stop,
120
- * StopFailure) and around tool calls (PreToolUse, PostToolUse,
121
- * PostToolUseFailure, PermissionDenied).
122
- *
123
- * Pass `undefined` (the default) to skip all hook dispatch. When a
124
- * registry is provided, the run attaches it to the `Graph` so internal
125
- * nodes can fire hooks too, and clears the session in the `finally`
126
- * block to prevent leaks.
127
- */
128
- hooks?: HookRegistry;
129
115
  returnContent?: boolean;
130
116
  tokenCounter?: TokenCounter;
131
117
  indexTokenCountMap?: Record<string, number>;
@@ -140,12 +126,6 @@ export type RunConfig = {
140
126
  calibrationRatio?: number;
141
127
  /** Skip post-stream cleanup (clearHeavyState) — useful for tests that inspect graph state after processStream */
142
128
  skipCleanup?: boolean;
143
- /**
144
- * Initial session state to seed the Graph's ToolSessionMap.
145
- * Used to carry over code environment sessions from skill file priming
146
- * at run start, so ToolNode can inject session_id + files into tool calls.
147
- */
148
- initialSessions?: ToolSessionMap;
149
129
  };
150
130
 
151
131
  export type ProvidedCallbacks =
@@ -2,8 +2,7 @@
2
2
  import type { StructuredToolInterface } from '@langchain/core/tools';
3
3
  import type { RunnableToolLike } from '@langchain/core/runnables';
4
4
  import type { ToolCall } from '@langchain/core/messages/tool';
5
- import type { HookRegistry } from '@/hooks';
6
- import type { MessageContentComplex, ToolErrorData } from './stream';
5
+ import type { ToolErrorData } from './stream';
7
6
  import { EnvVar } from '@/common';
8
7
 
9
8
  /** Replacement type for `import type { ToolCall } from '@langchain/core/messages/tool'` in order to have stringified args typed */
@@ -50,12 +49,6 @@ export type ToolNodeOptions = {
50
49
  agentId?: string;
51
50
  /** Tool names that must be executed directly (via runTool) even in event-driven mode (e.g., graph-managed handoff tools) */
52
51
  directToolNames?: Set<string>;
53
- /**
54
- * Hook registry for PreToolUse/PostToolUse lifecycle hooks.
55
- * Only fires for event-driven tool calls (`dispatchToolEvents`). Tools
56
- * routed through `directToolNames` bypass hook dispatch entirely.
57
- */
58
- hookRegistry?: HookRegistry;
59
52
  /** Max context tokens for the agent — used to compute tool result truncation limits. */
60
53
  maxContextTokens?: number;
61
54
  /**
@@ -193,26 +186,6 @@ export type ToolExecuteBatchRequest = {
193
186
  reject: (error: Error) => void;
194
187
  };
195
188
 
196
- /**
197
- * A message injected into graph state by any tool execution handler.
198
- * Generic mechanism: any tool returning `injectedMessages` in its `ToolExecuteResult`
199
- * will have these appended to state after the ToolMessage for this call.
200
- */
201
- export type InjectedMessage = {
202
- /** 'user' for skill body injection, 'system' for context hints.
203
- * Both are converted to HumanMessage at runtime; the original role
204
- * is preserved in additional_kwargs.role. */
205
- role: 'user' | 'system';
206
- /** Message content: string for simple text, array for complex multi-part content */
207
- content: string | MessageContentComplex[];
208
- /** When true, the message is framework-internal: not shown in UI, not counted as a user turn */
209
- isMeta?: boolean;
210
- /** Origin tag for downstream consumers (UI, pruner, compaction) */
211
- source?: 'skill' | 'hook' | 'system';
212
- /** Only set when source is 'skill', for compaction preservation */
213
- skillName?: string;
214
- };
215
-
216
189
  /** Result for a single tool call in event-driven execution */
217
190
  export type ToolExecuteResult = {
218
191
  /** Matches ToolCallRequest.id */
@@ -225,13 +198,6 @@ export type ToolExecuteResult = {
225
198
  status: 'success' | 'error';
226
199
  /** Error message if status is 'error' */
227
200
  errorMessage?: string;
228
- /**
229
- * Messages to inject into graph state after the ToolMessage for this call.
230
- * Placed after tool results to respect provider message ordering (tool_call -> tool_result adjacency).
231
- * The host's message formatter may merge injected user messages with the preceding tool_result turn.
232
- * Generic mechanism: any tool execution handler can use this.
233
- */
234
- injectedMessages?: InjectedMessage[];
235
201
  };
236
202
 
237
203
  /** Map of tool names to tool definitions */
@@ -352,12 +318,6 @@ export type ProgrammaticExecutionArtifact = {
352
318
  files?: FileRefs;
353
319
  };
354
320
 
355
- /** Parameters for creating a bash execution tool (same API as CodeExecutor, bash-only) */
356
- export type BashExecutionToolParams = CodeExecutionToolParams;
357
-
358
- /** Parameters for creating a bash programmatic tool calling tool (same API as PTC, bash-only) */
359
- export type BashProgrammaticToolCallingParams = ProgrammaticToolCallingParams;
360
-
361
321
  /**
362
322
  * Initialization parameters for the PTC tool
363
323
  */
@@ -1,162 +0,0 @@
1
- 'use strict';
2
-
3
- /**
4
- * Run-scoped storage for hook matchers with an additional layer for
5
- * session-scoped matchers that should be cleaned up between sessions.
6
- *
7
- * Hosts construct one registry per `Run` (mirroring how `HandlerRegistry` is
8
- * scoped) and register global matchers + per-session matchers against it.
9
- * Registration is strictly additive — nothing in this class mutates a
10
- * matcher's callbacks or flags after insertion.
11
- *
12
- * ## Why `Map<sessionId, MatcherBucket>` and not `Record`
13
- *
14
- * LibreChat runs thousands of parallel sessions in one Node process, and
15
- * hook registration happens inside hot paths (tool loading, agent spawning).
16
- * A `Record<sessionId, ...>` has to be spread on every insertion, which is
17
- * O(n) per call and O(n²) total for a batch of parallel registrations. A
18
- * Map mutates in place, keeping insertions O(1). This mirrors the reasoning
19
- * Claude Code documents at `utils/hooks/sessionHooks.ts:62`.
20
- */
21
- class HookRegistry {
22
- global = {};
23
- sessions = new Map();
24
- /**
25
- * Register a matcher for the lifetime of this registry (= one Run).
26
- * Returns an unregister function that removes the matcher by reference.
27
- */
28
- register(event, matcher) {
29
- const list = ensureList(this.global, event);
30
- list.push(widen(matcher));
31
- return () => {
32
- removeFromList(list, matcher);
33
- };
34
- }
35
- /**
36
- * Register a matcher for a specific session. Cleared automatically when
37
- * `clearSession(sessionId)` is called, or can be removed directly via the
38
- * returned unregister function.
39
- */
40
- registerSession(sessionId, event, matcher) {
41
- const bucket = this.ensureSessionBucket(sessionId);
42
- const list = ensureList(bucket, event);
43
- list.push(widen(matcher));
44
- return () => {
45
- removeFromList(list, matcher);
46
- };
47
- }
48
- /**
49
- * Returns all matchers registered for `event`, concatenating global first
50
- * and then session-specific (when `sessionId` is supplied). The caller
51
- * receives a fresh array, so iterating it is safe even if a matcher is
52
- * removed mid-iteration (e.g. via `once: true`).
53
- */
54
- getMatchers(event, sessionId) {
55
- const globalList = readList(this.global, event);
56
- if (sessionId === undefined) {
57
- return snapshot(globalList);
58
- }
59
- const bucket = this.sessions.get(sessionId);
60
- if (bucket === undefined) {
61
- return snapshot(globalList);
62
- }
63
- const sessionList = readList(bucket, event);
64
- if (globalList.length === 0) {
65
- return snapshot(sessionList);
66
- }
67
- if (sessionList.length === 0) {
68
- return snapshot(globalList);
69
- }
70
- return snapshot([...globalList, ...sessionList]);
71
- }
72
- /**
73
- * Removes `matcher` by reference from global storage first, falling back
74
- * to the session bucket when `sessionId` is supplied. Used by
75
- * `executeHooks` to drop `once: true` matchers after they fire.
76
- */
77
- removeMatcher(event, matcher, sessionId) {
78
- if (removeFromList(readList(this.global, event), matcher)) {
79
- return true;
80
- }
81
- if (sessionId === undefined) {
82
- return false;
83
- }
84
- const bucket = this.sessions.get(sessionId);
85
- if (bucket === undefined) {
86
- return false;
87
- }
88
- return removeFromList(readList(bucket, event), matcher);
89
- }
90
- /**
91
- * Drops every session-scoped matcher for `sessionId`. Call this in the
92
- * `finally` block around a Run so a `once: true` hook that never fired
93
- * cannot leak into the next session on the same registry.
94
- */
95
- clearSession(sessionId) {
96
- this.sessions.delete(sessionId);
97
- }
98
- /** True if at least one matcher exists for `event` (global + session). */
99
- hasHookFor(event, sessionId) {
100
- if (readList(this.global, event).length > 0) {
101
- return true;
102
- }
103
- if (sessionId === undefined) {
104
- return false;
105
- }
106
- const bucket = this.sessions.get(sessionId);
107
- if (bucket === undefined) {
108
- return false;
109
- }
110
- return readList(bucket, event).length > 0;
111
- }
112
- ensureSessionBucket(sessionId) {
113
- const existing = this.sessions.get(sessionId);
114
- if (existing !== undefined) {
115
- return existing;
116
- }
117
- const fresh = {};
118
- this.sessions.set(sessionId, fresh);
119
- return fresh;
120
- }
121
- }
122
- function ensureList(bucket, event) {
123
- const existing = bucket[event];
124
- if (existing !== undefined) {
125
- return existing;
126
- }
127
- const fresh = [];
128
- bucket[event] = fresh;
129
- return fresh;
130
- }
131
- function readList(bucket, event) {
132
- return bucket[event] ?? [];
133
- }
134
- function removeFromList(list, matcher) {
135
- const idx = list.indexOf(widen(matcher));
136
- if (idx < 0) {
137
- return false;
138
- }
139
- list.splice(idx, 1);
140
- return true;
141
- }
142
- /**
143
- * Widen a per-event matcher to the storage's uniform slot type. Unsound at
144
- * the type level (function parameters are contravariant) but safe by
145
- * construction: `HookRegistry.register<E>` only ever puts matchers into the
146
- * bucket slot for their own event, and reads go through `snapshot<E>`
147
- * which is only called with the same `E`.
148
- */
149
- function widen(matcher) {
150
- return matcher;
151
- }
152
- /**
153
- * Narrow a storage list back to a per-event matcher list on the way out.
154
- * Sound counterpart to `widen`: the list only contains matchers that were
155
- * registered against `E`, because the public API enforces it on insert.
156
- */
157
- function snapshot(list) {
158
- return list.slice();
159
- }
160
-
161
- exports.HookRegistry = HookRegistry;
162
- //# sourceMappingURL=HookRegistry.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"HookRegistry.cjs","sources":["../../../src/hooks/HookRegistry.ts"],"sourcesContent":["// src/hooks/HookRegistry.ts\nimport type { HookEvent, HookMatcher } from './types';\n\n/**\n * Internal matcher storage type.\n *\n * Matchers registered via the public `register<E>` API are strictly typed\n * to a single `E`, but the storage needs one uniform slot type per event.\n * We store them as `HookMatcher<HookEvent>` and cast once at the variance\n * boundary — see `ensureList` and `snapshot` below. The invariant (every\n * matcher in `bucket[event]` was registered with that exact event) is\n * enforced by the public API; breaking it requires bypassing the types.\n */\ntype MatcherBucket = Partial<Record<HookEvent, HookMatcher<HookEvent>[]>>;\n\n/**\n * Run-scoped storage for hook matchers with an additional layer for\n * session-scoped matchers that should be cleaned up between sessions.\n *\n * Hosts construct one registry per `Run` (mirroring how `HandlerRegistry` is\n * scoped) and register global matchers + per-session matchers against it.\n * Registration is strictly additive — nothing in this class mutates a\n * matcher's callbacks or flags after insertion.\n *\n * ## Why `Map<sessionId, MatcherBucket>` and not `Record`\n *\n * LibreChat runs thousands of parallel sessions in one Node process, and\n * hook registration happens inside hot paths (tool loading, agent spawning).\n * A `Record<sessionId, ...>` has to be spread on every insertion, which is\n * O(n) per call and O(n²) total for a batch of parallel registrations. A\n * Map mutates in place, keeping insertions O(1). This mirrors the reasoning\n * Claude Code documents at `utils/hooks/sessionHooks.ts:62`.\n */\nexport class HookRegistry {\n private readonly global: MatcherBucket = {};\n private readonly sessions: Map<string, MatcherBucket> = new Map();\n\n /**\n * Register a matcher for the lifetime of this registry (= one Run).\n * Returns an unregister function that removes the matcher by reference.\n */\n register<E extends HookEvent>(event: E, matcher: HookMatcher<E>): () => void {\n const list = ensureList(this.global, event);\n list.push(widen(matcher));\n return () => {\n removeFromList(list, matcher);\n };\n }\n\n /**\n * Register a matcher for a specific session. Cleared automatically when\n * `clearSession(sessionId)` is called, or can be removed directly via the\n * returned unregister function.\n */\n registerSession<E extends HookEvent>(\n sessionId: string,\n event: E,\n matcher: HookMatcher<E>\n ): () => void {\n const bucket = this.ensureSessionBucket(sessionId);\n const list = ensureList(bucket, event);\n list.push(widen(matcher));\n return () => {\n removeFromList(list, matcher);\n };\n }\n\n /**\n * Returns all matchers registered for `event`, concatenating global first\n * and then session-specific (when `sessionId` is supplied). The caller\n * receives a fresh array, so iterating it is safe even if a matcher is\n * removed mid-iteration (e.g. via `once: true`).\n */\n getMatchers<E extends HookEvent>(\n event: E,\n sessionId?: string\n ): HookMatcher<E>[] {\n const globalList = readList(this.global, event);\n if (sessionId === undefined) {\n return snapshot<E>(globalList);\n }\n const bucket = this.sessions.get(sessionId);\n if (bucket === undefined) {\n return snapshot<E>(globalList);\n }\n const sessionList = readList(bucket, event);\n if (globalList.length === 0) {\n return snapshot<E>(sessionList);\n }\n if (sessionList.length === 0) {\n return snapshot<E>(globalList);\n }\n return snapshot<E>([...globalList, ...sessionList]);\n }\n\n /**\n * Removes `matcher` by reference from global storage first, falling back\n * to the session bucket when `sessionId` is supplied. Used by\n * `executeHooks` to drop `once: true` matchers after they fire.\n */\n removeMatcher<E extends HookEvent>(\n event: E,\n matcher: HookMatcher<E>,\n sessionId?: string\n ): boolean {\n if (removeFromList(readList(this.global, event), matcher)) {\n return true;\n }\n if (sessionId === undefined) {\n return false;\n }\n const bucket = this.sessions.get(sessionId);\n if (bucket === undefined) {\n return false;\n }\n return removeFromList(readList(bucket, event), matcher);\n }\n\n /**\n * Drops every session-scoped matcher for `sessionId`. Call this in the\n * `finally` block around a Run so a `once: true` hook that never fired\n * cannot leak into the next session on the same registry.\n */\n clearSession(sessionId: string): void {\n this.sessions.delete(sessionId);\n }\n\n /** True if at least one matcher exists for `event` (global + session). */\n hasHookFor(event: HookEvent, sessionId?: string): boolean {\n if (readList(this.global, event).length > 0) {\n return true;\n }\n if (sessionId === undefined) {\n return false;\n }\n const bucket = this.sessions.get(sessionId);\n if (bucket === undefined) {\n return false;\n }\n return readList(bucket, event).length > 0;\n }\n\n private ensureSessionBucket(sessionId: string): MatcherBucket {\n const existing = this.sessions.get(sessionId);\n if (existing !== undefined) {\n return existing;\n }\n const fresh: MatcherBucket = {};\n this.sessions.set(sessionId, fresh);\n return fresh;\n }\n}\n\nfunction ensureList(\n bucket: MatcherBucket,\n event: HookEvent\n): HookMatcher<HookEvent>[] {\n const existing = bucket[event];\n if (existing !== undefined) {\n return existing;\n }\n const fresh: HookMatcher<HookEvent>[] = [];\n bucket[event] = fresh;\n return fresh;\n}\n\nfunction readList(\n bucket: MatcherBucket,\n event: HookEvent\n): HookMatcher<HookEvent>[] {\n return bucket[event] ?? [];\n}\n\nfunction removeFromList<E extends HookEvent>(\n list: HookMatcher<HookEvent>[],\n matcher: HookMatcher<E>\n): boolean {\n const idx = list.indexOf(widen(matcher));\n if (idx < 0) {\n return false;\n }\n list.splice(idx, 1);\n return true;\n}\n\n/**\n * Widen a per-event matcher to the storage's uniform slot type. Unsound at\n * the type level (function parameters are contravariant) but safe by\n * construction: `HookRegistry.register<E>` only ever puts matchers into the\n * bucket slot for their own event, and reads go through `snapshot<E>`\n * which is only called with the same `E`.\n */\nfunction widen<E extends HookEvent>(\n matcher: HookMatcher<E>\n): HookMatcher<HookEvent> {\n return matcher as unknown as HookMatcher<HookEvent>;\n}\n\n/**\n * Narrow a storage list back to a per-event matcher list on the way out.\n * Sound counterpart to `widen`: the list only contains matchers that were\n * registered against `E`, because the public API enforces it on insert.\n */\nfunction snapshot<E extends HookEvent>(\n list: readonly HookMatcher<HookEvent>[]\n): HookMatcher<E>[] {\n return list.slice() as unknown as HookMatcher<E>[];\n}\n"],"names":[],"mappings":";;AAeA;;;;;;;;;;;;;;;;;AAiBG;MACU,YAAY,CAAA;IACN,MAAM,GAAkB,EAAE;AAC1B,IAAA,QAAQ,GAA+B,IAAI,GAAG,EAAE;AAEjE;;;AAGG;IACH,QA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