@librechat/agents 3.1.68 → 3.1.71-dev.0

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 (192) hide show
  1. package/dist/cjs/agents/AgentContext.cjs +23 -3
  2. package/dist/cjs/agents/AgentContext.cjs.map +1 -1
  3. package/dist/cjs/common/enum.cjs +16 -1
  4. package/dist/cjs/common/enum.cjs.map +1 -1
  5. package/dist/cjs/graphs/Graph.cjs +136 -0
  6. package/dist/cjs/graphs/Graph.cjs.map +1 -1
  7. package/dist/cjs/hooks/HookRegistry.cjs +162 -0
  8. package/dist/cjs/hooks/HookRegistry.cjs.map +1 -0
  9. package/dist/cjs/hooks/executeHooks.cjs +276 -0
  10. package/dist/cjs/hooks/executeHooks.cjs.map +1 -0
  11. package/dist/cjs/hooks/matchers.cjs +256 -0
  12. package/dist/cjs/hooks/matchers.cjs.map +1 -0
  13. package/dist/cjs/hooks/types.cjs +27 -0
  14. package/dist/cjs/hooks/types.cjs.map +1 -0
  15. package/dist/cjs/main.cjs +57 -0
  16. package/dist/cjs/main.cjs.map +1 -1
  17. package/dist/cjs/messages/format.cjs +74 -12
  18. package/dist/cjs/messages/format.cjs.map +1 -1
  19. package/dist/cjs/messages/prune.cjs +9 -2
  20. package/dist/cjs/messages/prune.cjs.map +1 -1
  21. package/dist/cjs/run.cjs +115 -0
  22. package/dist/cjs/run.cjs.map +1 -1
  23. package/dist/cjs/summarization/node.cjs +44 -0
  24. package/dist/cjs/summarization/node.cjs.map +1 -1
  25. package/dist/cjs/tools/BashExecutor.cjs +208 -0
  26. package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
  27. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +287 -0
  28. package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
  29. package/dist/cjs/tools/CodeExecutor.cjs +0 -9
  30. package/dist/cjs/tools/CodeExecutor.cjs.map +1 -1
  31. package/dist/cjs/tools/ProgrammaticToolCalling.cjs +7 -23
  32. package/dist/cjs/tools/ProgrammaticToolCalling.cjs.map +1 -1
  33. package/dist/cjs/tools/ReadFile.cjs +43 -0
  34. package/dist/cjs/tools/ReadFile.cjs.map +1 -0
  35. package/dist/cjs/tools/SkillTool.cjs +50 -0
  36. package/dist/cjs/tools/SkillTool.cjs.map +1 -0
  37. package/dist/cjs/tools/SubagentTool.cjs +92 -0
  38. package/dist/cjs/tools/SubagentTool.cjs.map +1 -0
  39. package/dist/cjs/tools/ToolNode.cjs +746 -174
  40. package/dist/cjs/tools/ToolNode.cjs.map +1 -1
  41. package/dist/cjs/tools/ToolSearch.cjs +2 -13
  42. package/dist/cjs/tools/ToolSearch.cjs.map +1 -1
  43. package/dist/cjs/tools/skillCatalog.cjs +84 -0
  44. package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
  45. package/dist/cjs/tools/subagent/SubagentExecutor.cjs +511 -0
  46. package/dist/cjs/tools/subagent/SubagentExecutor.cjs.map +1 -0
  47. package/dist/cjs/tools/toolOutputReferences.cjs +475 -0
  48. package/dist/cjs/tools/toolOutputReferences.cjs.map +1 -0
  49. package/dist/cjs/utils/truncation.cjs +28 -0
  50. package/dist/cjs/utils/truncation.cjs.map +1 -1
  51. package/dist/esm/agents/AgentContext.mjs +23 -3
  52. package/dist/esm/agents/AgentContext.mjs.map +1 -1
  53. package/dist/esm/common/enum.mjs +15 -2
  54. package/dist/esm/common/enum.mjs.map +1 -1
  55. package/dist/esm/graphs/Graph.mjs +136 -0
  56. package/dist/esm/graphs/Graph.mjs.map +1 -1
  57. package/dist/esm/hooks/HookRegistry.mjs +160 -0
  58. package/dist/esm/hooks/HookRegistry.mjs.map +1 -0
  59. package/dist/esm/hooks/executeHooks.mjs +273 -0
  60. package/dist/esm/hooks/executeHooks.mjs.map +1 -0
  61. package/dist/esm/hooks/matchers.mjs +251 -0
  62. package/dist/esm/hooks/matchers.mjs.map +1 -0
  63. package/dist/esm/hooks/types.mjs +25 -0
  64. package/dist/esm/hooks/types.mjs.map +1 -0
  65. package/dist/esm/main.mjs +13 -2
  66. package/dist/esm/main.mjs.map +1 -1
  67. package/dist/esm/messages/format.mjs +66 -4
  68. package/dist/esm/messages/format.mjs.map +1 -1
  69. package/dist/esm/messages/prune.mjs +9 -2
  70. package/dist/esm/messages/prune.mjs.map +1 -1
  71. package/dist/esm/run.mjs +115 -0
  72. package/dist/esm/run.mjs.map +1 -1
  73. package/dist/esm/summarization/node.mjs +44 -0
  74. package/dist/esm/summarization/node.mjs.map +1 -1
  75. package/dist/esm/tools/BashExecutor.mjs +200 -0
  76. package/dist/esm/tools/BashExecutor.mjs.map +1 -0
  77. package/dist/esm/tools/BashProgrammaticToolCalling.mjs +278 -0
  78. package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
  79. package/dist/esm/tools/CodeExecutor.mjs +0 -9
  80. package/dist/esm/tools/CodeExecutor.mjs.map +1 -1
  81. package/dist/esm/tools/ProgrammaticToolCalling.mjs +8 -24
  82. package/dist/esm/tools/ProgrammaticToolCalling.mjs.map +1 -1
  83. package/dist/esm/tools/ReadFile.mjs +38 -0
  84. package/dist/esm/tools/ReadFile.mjs.map +1 -0
  85. package/dist/esm/tools/SkillTool.mjs +45 -0
  86. package/dist/esm/tools/SkillTool.mjs.map +1 -0
  87. package/dist/esm/tools/SubagentTool.mjs +85 -0
  88. package/dist/esm/tools/SubagentTool.mjs.map +1 -0
  89. package/dist/esm/tools/ToolNode.mjs +748 -176
  90. package/dist/esm/tools/ToolNode.mjs.map +1 -1
  91. package/dist/esm/tools/ToolSearch.mjs +3 -14
  92. package/dist/esm/tools/ToolSearch.mjs.map +1 -1
  93. package/dist/esm/tools/skillCatalog.mjs +82 -0
  94. package/dist/esm/tools/skillCatalog.mjs.map +1 -0
  95. package/dist/esm/tools/subagent/SubagentExecutor.mjs +505 -0
  96. package/dist/esm/tools/subagent/SubagentExecutor.mjs.map +1 -0
  97. package/dist/esm/tools/toolOutputReferences.mjs +468 -0
  98. package/dist/esm/tools/toolOutputReferences.mjs.map +1 -0
  99. package/dist/esm/utils/truncation.mjs +27 -1
  100. package/dist/esm/utils/truncation.mjs.map +1 -1
  101. package/dist/types/agents/AgentContext.d.ts +6 -0
  102. package/dist/types/common/enum.d.ts +10 -2
  103. package/dist/types/graphs/Graph.d.ts +23 -0
  104. package/dist/types/hooks/HookRegistry.d.ts +56 -0
  105. package/dist/types/hooks/executeHooks.d.ts +79 -0
  106. package/dist/types/hooks/index.d.ts +6 -0
  107. package/dist/types/hooks/matchers.d.ts +95 -0
  108. package/dist/types/hooks/types.d.ts +320 -0
  109. package/dist/types/index.d.ts +8 -0
  110. package/dist/types/messages/format.d.ts +2 -1
  111. package/dist/types/run.d.ts +2 -0
  112. package/dist/types/summarization/node.d.ts +2 -0
  113. package/dist/types/tools/BashExecutor.d.ts +76 -0
  114. package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
  115. package/dist/types/tools/ProgrammaticToolCalling.d.ts +4 -9
  116. package/dist/types/tools/ReadFile.d.ts +28 -0
  117. package/dist/types/tools/SkillTool.d.ts +40 -0
  118. package/dist/types/tools/SubagentTool.d.ts +36 -0
  119. package/dist/types/tools/ToolNode.d.ts +109 -4
  120. package/dist/types/tools/ToolSearch.d.ts +2 -2
  121. package/dist/types/tools/skillCatalog.d.ts +19 -0
  122. package/dist/types/tools/subagent/SubagentExecutor.d.ts +137 -0
  123. package/dist/types/tools/subagent/index.d.ts +2 -0
  124. package/dist/types/tools/toolOutputReferences.d.ts +205 -0
  125. package/dist/types/types/graph.d.ts +61 -2
  126. package/dist/types/types/index.d.ts +1 -0
  127. package/dist/types/types/run.d.ts +28 -0
  128. package/dist/types/types/skill.d.ts +9 -0
  129. package/dist/types/types/tools.d.ts +108 -10
  130. package/dist/types/utils/truncation.d.ts +21 -0
  131. package/package.json +5 -1
  132. package/src/agents/AgentContext.ts +26 -2
  133. package/src/common/enum.ts +15 -1
  134. package/src/graphs/Graph.ts +161 -0
  135. package/src/hooks/HookRegistry.ts +208 -0
  136. package/src/hooks/__tests__/HookRegistry.test.ts +190 -0
  137. package/src/hooks/__tests__/compactHooks.test.ts +214 -0
  138. package/src/hooks/__tests__/executeHooks.test.ts +1013 -0
  139. package/src/hooks/__tests__/integration.test.ts +337 -0
  140. package/src/hooks/__tests__/matchers.test.ts +238 -0
  141. package/src/hooks/__tests__/toolHooks.test.ts +669 -0
  142. package/src/hooks/executeHooks.ts +375 -0
  143. package/src/hooks/index.ts +57 -0
  144. package/src/hooks/matchers.ts +280 -0
  145. package/src/hooks/types.ts +404 -0
  146. package/src/index.ts +10 -0
  147. package/src/messages/format.ts +74 -4
  148. package/src/messages/formatAgentMessages.skills.test.ts +334 -0
  149. package/src/messages/prune.ts +9 -2
  150. package/src/run.ts +130 -0
  151. package/src/scripts/multi-agent-subagent.ts +246 -0
  152. package/src/scripts/programmatic_exec.ts +1 -10
  153. package/src/scripts/subagent-event-driven-debug.ts +190 -0
  154. package/src/scripts/subagent-tools-debug.ts +160 -0
  155. package/src/scripts/test_code_api.ts +0 -7
  156. package/src/scripts/tool_search.ts +1 -10
  157. package/src/specs/prune.test.ts +413 -0
  158. package/src/specs/subagent.test.ts +305 -0
  159. package/src/summarization/node.ts +53 -0
  160. package/src/tools/BashExecutor.ts +238 -0
  161. package/src/tools/BashProgrammaticToolCalling.ts +381 -0
  162. package/src/tools/CodeExecutor.ts +0 -11
  163. package/src/tools/ProgrammaticToolCalling.ts +4 -29
  164. package/src/tools/ReadFile.ts +39 -0
  165. package/src/tools/SkillTool.ts +46 -0
  166. package/src/tools/SubagentTool.ts +100 -0
  167. package/src/tools/ToolNode.ts +999 -214
  168. package/src/tools/ToolSearch.ts +3 -19
  169. package/src/tools/__tests__/BashExecutor.test.ts +36 -0
  170. package/src/tools/__tests__/ProgrammaticToolCalling.integration.test.ts +7 -8
  171. package/src/tools/__tests__/ProgrammaticToolCalling.test.ts +0 -1
  172. package/src/tools/__tests__/ReadFile.test.ts +44 -0
  173. package/src/tools/__tests__/SkillTool.test.ts +442 -0
  174. package/src/tools/__tests__/SubagentExecutor.test.ts +1148 -0
  175. package/src/tools/__tests__/SubagentTool.test.ts +149 -0
  176. package/src/tools/__tests__/ToolNode.outputReferences.test.ts +1395 -0
  177. package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
  178. package/src/tools/__tests__/ToolSearch.integration.test.ts +7 -8
  179. package/src/tools/__tests__/skillCatalog.test.ts +161 -0
  180. package/src/tools/__tests__/subagentHooks.test.ts +215 -0
  181. package/src/tools/__tests__/toolOutputReferences.test.ts +415 -0
  182. package/src/tools/skillCatalog.ts +126 -0
  183. package/src/tools/subagent/SubagentExecutor.ts +676 -0
  184. package/src/tools/subagent/index.ts +13 -0
  185. package/src/tools/toolOutputReferences.ts +590 -0
  186. package/src/types/graph.ts +80 -1
  187. package/src/types/index.ts +1 -0
  188. package/src/types/run.ts +28 -0
  189. package/src/types/skill.ts +11 -0
  190. package/src/types/tools.ts +112 -10
  191. package/src/utils/__tests__/truncation.test.ts +66 -0
  192. package/src/utils/truncation.ts +30 -0
@@ -0,0 +1,404 @@
1
+ // src/hooks/types.ts
2
+ import type { BaseMessage } from '@langchain/core/messages';
3
+
4
+ /**
5
+ * Closed set of hook lifecycle events supported by the hooks system.
6
+ *
7
+ * These mirror the subset of Claude Code's event surface that makes sense
8
+ * for a library context (no filesystem/CLI-specific events). See
9
+ * `docs/hooks-design-report.md` §3.2 for the mapping to existing
10
+ * `@librechat/agents` emission points.
11
+ */
12
+ export const HOOK_EVENTS = [
13
+ 'RunStart',
14
+ 'UserPromptSubmit',
15
+ 'PreToolUse',
16
+ 'PostToolUse',
17
+ 'PostToolUseFailure',
18
+ 'PermissionDenied',
19
+ 'SubagentStart',
20
+ 'SubagentStop',
21
+ 'Stop',
22
+ 'StopFailure',
23
+ 'PreCompact',
24
+ 'PostCompact',
25
+ ] as const;
26
+
27
+ export type HookEvent = (typeof HOOK_EVENTS)[number];
28
+
29
+ /** Tool-gating decision; executeHooks folds with `deny > ask > allow` precedence. */
30
+ export type ToolDecision = 'allow' | 'deny' | 'ask';
31
+
32
+ /** Stop-loop decision; `block` means "do not stop, run another turn". Any `block` wins. */
33
+ export type StopDecision = 'continue' | 'block';
34
+
35
+ /**
36
+ * Fields shared by every `HookInput`. Discriminated by `hook_event_name`.
37
+ *
38
+ * - `runId` identifies the current agent run and is always present.
39
+ * - `threadId` identifies the conversation thread when the host has one.
40
+ * - `agentId` is only set when the hook fires inside a subagent scope.
41
+ */
42
+ export interface BaseHookInput {
43
+ runId: string;
44
+ threadId?: string;
45
+ agentId?: string;
46
+ }
47
+
48
+ export interface RunStartHookInput extends BaseHookInput {
49
+ hook_event_name: 'RunStart';
50
+ messages: BaseMessage[];
51
+ }
52
+
53
+ export interface UserPromptSubmitHookInput extends BaseHookInput {
54
+ hook_event_name: 'UserPromptSubmit';
55
+ prompt: string;
56
+ attachments?: BaseMessage[];
57
+ }
58
+
59
+ /**
60
+ * Fires before a tool is invoked. Hook may return `deny`/`ask`/`allow` and/or
61
+ * an `updatedInput` that replaces the tool arguments before invocation.
62
+ *
63
+ * `toolInput` is intentionally typed as `Record<string, unknown>` because the
64
+ * SDK is tool-agnostic — concrete tool argument shapes are only known at the
65
+ * call site and are narrowed by the host. This is the one escape hatch in
66
+ * the hook type system.
67
+ */
68
+ export interface PreToolUseHookInput extends BaseHookInput {
69
+ hook_event_name: 'PreToolUse';
70
+ toolName: string;
71
+ toolInput: Record<string, unknown>;
72
+ toolUseId: string;
73
+ stepId?: string;
74
+ /**
75
+ * Number of times this tool has been invoked in prior batches within the
76
+ * current run. Within a single batch of parallel calls, all calls to the
77
+ * same tool share the same turn value — per-call discrimination within a
78
+ * batch is not supported in v1.
79
+ */
80
+ turn?: number;
81
+ }
82
+
83
+ export interface PostToolUseHookInput extends BaseHookInput {
84
+ hook_event_name: 'PostToolUse';
85
+ toolName: string;
86
+ toolInput: Record<string, unknown>;
87
+ toolOutput: unknown;
88
+ toolUseId: string;
89
+ stepId?: string;
90
+ turn?: number;
91
+ }
92
+
93
+ export interface PostToolUseFailureHookInput extends BaseHookInput {
94
+ hook_event_name: 'PostToolUseFailure';
95
+ toolName: string;
96
+ toolInput: Record<string, unknown>;
97
+ toolUseId: string;
98
+ error: string;
99
+ stepId?: string;
100
+ turn?: number;
101
+ }
102
+
103
+ export interface PermissionDeniedHookInput extends BaseHookInput {
104
+ hook_event_name: 'PermissionDenied';
105
+ toolName: string;
106
+ toolInput: Record<string, unknown>;
107
+ toolUseId: string;
108
+ reason: string;
109
+ }
110
+
111
+ export interface SubagentStartHookInput extends BaseHookInput {
112
+ hook_event_name: 'SubagentStart';
113
+ parentAgentId?: string;
114
+ agentId: string;
115
+ agentType: string;
116
+ inputs: BaseMessage[];
117
+ }
118
+
119
+ export interface SubagentStopHookInput extends BaseHookInput {
120
+ hook_event_name: 'SubagentStop';
121
+ agentId: string;
122
+ agentType: string;
123
+ messages: BaseMessage[];
124
+ }
125
+
126
+ export interface StopHookInput extends BaseHookInput {
127
+ hook_event_name: 'Stop';
128
+ messages: BaseMessage[];
129
+ stopReason?: string;
130
+ stopHookActive: boolean;
131
+ }
132
+
133
+ export interface StopFailureHookInput extends BaseHookInput {
134
+ hook_event_name: 'StopFailure';
135
+ error: string;
136
+ lastAssistantMessage?: BaseMessage;
137
+ }
138
+
139
+ export interface PreCompactHookInput extends BaseHookInput {
140
+ hook_event_name: 'PreCompact';
141
+ messagesBeforeCount: number;
142
+ /**
143
+ * What triggered compaction. Matches `SummarizationTrigger.type` from the
144
+ * agent's summarization config. `'default'` means no trigger was
145
+ * configured and compaction fired because messages were pruned.
146
+ */
147
+ trigger:
148
+ | 'token_ratio'
149
+ | 'remaining_tokens'
150
+ | 'messages_to_refine'
151
+ | 'default'
152
+ | (string & {});
153
+ }
154
+
155
+ export interface PostCompactHookInput extends BaseHookInput {
156
+ hook_event_name: 'PostCompact';
157
+ summary: string;
158
+ /**
159
+ * Number of messages remaining after compaction. The summarize node
160
+ * returns a `removeAll` signal that clears all messages from state;
161
+ * the summary itself is injected into the system prompt, not as a
162
+ * message. This is `0` at the point of hook dispatch.
163
+ */
164
+ messagesAfterCount: number;
165
+ }
166
+
167
+ /** Discriminated union of every hook input shape. */
168
+ export type HookInput =
169
+ | RunStartHookInput
170
+ | UserPromptSubmitHookInput
171
+ | PreToolUseHookInput
172
+ | PostToolUseHookInput
173
+ | PostToolUseFailureHookInput
174
+ | PermissionDeniedHookInput
175
+ | SubagentStartHookInput
176
+ | SubagentStopHookInput
177
+ | StopHookInput
178
+ | StopFailureHookInput
179
+ | PreCompactHookInput
180
+ | PostCompactHookInput;
181
+
182
+ /** Compile-time map from event name to its input shape. */
183
+ export type HookInputByEvent = {
184
+ RunStart: RunStartHookInput;
185
+ UserPromptSubmit: UserPromptSubmitHookInput;
186
+ PreToolUse: PreToolUseHookInput;
187
+ PostToolUse: PostToolUseHookInput;
188
+ PostToolUseFailure: PostToolUseFailureHookInput;
189
+ PermissionDenied: PermissionDeniedHookInput;
190
+ SubagentStart: SubagentStartHookInput;
191
+ SubagentStop: SubagentStopHookInput;
192
+ Stop: StopHookInput;
193
+ StopFailure: StopFailureHookInput;
194
+ PreCompact: PreCompactHookInput;
195
+ PostCompact: PostCompactHookInput;
196
+ };
197
+
198
+ /**
199
+ * Fields common to every hook output. Hooks that have nothing to say simply
200
+ * return `{}` (or omit the fields below).
201
+ */
202
+ export interface BaseHookOutput {
203
+ /** Context string to inject into the conversation. Accumulated across hooks. */
204
+ additionalContext?: string;
205
+ /** True to prevent the next model turn. Any hook can set this. */
206
+ preventContinuation?: boolean;
207
+ /** Reason reported alongside `preventContinuation`. */
208
+ stopReason?: string;
209
+ }
210
+
211
+ export type RunStartHookOutput = BaseHookOutput;
212
+
213
+ export interface UserPromptSubmitHookOutput extends BaseHookOutput {
214
+ decision?: ToolDecision;
215
+ reason?: string;
216
+ }
217
+
218
+ export interface PreToolUseHookOutput extends BaseHookOutput {
219
+ decision?: ToolDecision;
220
+ reason?: string;
221
+ /**
222
+ * Replacement tool input. Merged into the pending tool call by the host.
223
+ *
224
+ * When multiple hooks set `updatedInput` within a single `executeHooks`
225
+ * call, the last writer in registration order wins (outer loop: matcher
226
+ * registration order; inner loop: hook position within the matcher). The
227
+ * winner is deterministic — `Promise.all` preserves input-array order.
228
+ * Consumers that need a single authoritative rewrite should still scope
229
+ * `updatedInput` to one hook per matcher to avoid confusing precedence.
230
+ */
231
+ updatedInput?: Record<string, unknown>;
232
+ }
233
+
234
+ export interface PostToolUseHookOutput extends BaseHookOutput {
235
+ /**
236
+ * Replacement tool output. Flows through the aggregated result so the
237
+ * host can substitute it before appending the tool result message.
238
+ * Ordering semantics match `PreToolUseHookOutput.updatedInput`:
239
+ * last-writer-wins in registration order.
240
+ */
241
+ updatedOutput?: unknown;
242
+ }
243
+
244
+ export type PostToolUseFailureHookOutput = BaseHookOutput;
245
+
246
+ export type PermissionDeniedHookOutput = BaseHookOutput;
247
+
248
+ export interface SubagentStartHookOutput extends BaseHookOutput {
249
+ decision?: ToolDecision;
250
+ reason?: string;
251
+ }
252
+
253
+ export type SubagentStopHookOutput = BaseHookOutput;
254
+
255
+ export interface StopHookOutput extends BaseHookOutput {
256
+ decision?: StopDecision;
257
+ reason?: string;
258
+ }
259
+
260
+ export type StopFailureHookOutput = BaseHookOutput;
261
+
262
+ export type PreCompactHookOutput = BaseHookOutput;
263
+
264
+ export type PostCompactHookOutput = BaseHookOutput;
265
+
266
+ /** Compile-time map from event name to its output shape. */
267
+ export type HookOutputByEvent = {
268
+ RunStart: RunStartHookOutput;
269
+ UserPromptSubmit: UserPromptSubmitHookOutput;
270
+ PreToolUse: PreToolUseHookOutput;
271
+ PostToolUse: PostToolUseHookOutput;
272
+ PostToolUseFailure: PostToolUseFailureHookOutput;
273
+ PermissionDenied: PermissionDeniedHookOutput;
274
+ SubagentStart: SubagentStartHookOutput;
275
+ SubagentStop: SubagentStopHookOutput;
276
+ Stop: StopHookOutput;
277
+ StopFailure: StopFailureHookOutput;
278
+ PreCompact: PreCompactHookOutput;
279
+ PostCompact: PostCompactHookOutput;
280
+ };
281
+
282
+ /** Superset output shape used by the executor's fold loop. */
283
+ export type HookOutput =
284
+ | RunStartHookOutput
285
+ | UserPromptSubmitHookOutput
286
+ | PreToolUseHookOutput
287
+ | PostToolUseHookOutput
288
+ | PostToolUseFailureHookOutput
289
+ | PermissionDeniedHookOutput
290
+ | SubagentStartHookOutput
291
+ | SubagentStopHookOutput
292
+ | StopHookOutput
293
+ | StopFailureHookOutput
294
+ | PreCompactHookOutput
295
+ | PostCompactHookOutput;
296
+
297
+ /**
298
+ * A hook callback is a plain async function registered against a specific
299
+ * event. The `signal` is always supplied by `executeHooks` and combines the
300
+ * batch's parent signal with the per-hook timeout — callbacks that perform
301
+ * long-running work should observe it.
302
+ */
303
+ export type HookCallback<E extends HookEvent = HookEvent> = (
304
+ input: HookInputByEvent[E],
305
+ signal: AbortSignal
306
+ ) => HookOutputByEvent[E] | Promise<HookOutputByEvent[E]>;
307
+
308
+ /**
309
+ * A matcher groups one or more callbacks under a shared regex filter and
310
+ * shared timeout/once/internal flags. The generic `E` ties the callback
311
+ * types to the event the matcher is registered against.
312
+ */
313
+ export interface HookMatcher<E extends HookEvent = HookEvent> {
314
+ /**
315
+ * Regex pattern matched against the event's primary query string (e.g.
316
+ * the tool name for `PreToolUse`, the agent type for `SubagentStart`).
317
+ *
318
+ * Omitted or empty means "always match". For events that do not supply a
319
+ * query string (`RunStart`, `Stop`, etc.), only wildcard matchers fire —
320
+ * a non-empty pattern on such events will never match.
321
+ *
322
+ * Patterns are treated as trusted input: `executeHooks` compiles them
323
+ * with `new RegExp(pattern)` without any sandbox, and a pathological
324
+ * pattern can block the event loop. Host registration code is expected
325
+ * to validate or length-bound patterns that originate from user input.
326
+ */
327
+ pattern?: string;
328
+ /** Callbacks that fire when the matcher hits. Executed in parallel. */
329
+ hooks: HookCallback<E>[];
330
+ /** Per-matcher timeout in ms. Defaults to the executor's batch timeout. */
331
+ timeout?: number;
332
+ /**
333
+ * Atomically remove the matcher before its first dispatch.
334
+ *
335
+ * `executeHooks` claims `once: true` matchers synchronously — between
336
+ * `getMatchers` and its first `await` — so two concurrent calls cannot
337
+ * both dispatch the same matcher. Whichever call runs its sync prefix
338
+ * first wins the matcher; the other sees an empty bucket.
339
+ *
340
+ * Semantics are "at most one dispatch, ever" — if every hook in the
341
+ * matcher throws, the matcher is still gone. Use `once` for
342
+ * fire-and-forget bootstrapping (registration, telemetry, setup). Hosts
343
+ * that need retry semantics should register a normal matcher and
344
+ * self-unregister via the callback returned from `registry.register`.
345
+ */
346
+ once?: boolean;
347
+ /** Internal hooks are excluded from telemetry and non-fatal error logging. */
348
+ internal?: boolean;
349
+ }
350
+
351
+ /**
352
+ * Storage shape for matchers keyed by event. Each event's matcher list is
353
+ * a generic array parameterized by that event type, so lookup via
354
+ * `HooksByEvent[E]` preserves type-safe callback signatures.
355
+ */
356
+ export type HooksByEvent = {
357
+ [E in HookEvent]?: HookMatcher<E>[];
358
+ };
359
+
360
+ /**
361
+ * Aggregated result of a single `executeHooks` call. Fields are populated
362
+ * according to the fold rules in `executeHooks.ts`.
363
+ */
364
+ export interface AggregatedHookResult {
365
+ /** Folded tool-gating decision; `deny > ask > allow`. */
366
+ decision?: ToolDecision;
367
+ /** Folded stop decision; any `block` wins. */
368
+ stopDecision?: StopDecision;
369
+ /** Reason from the hook that set the winning decision. */
370
+ reason?: string;
371
+ /**
372
+ * Replacement tool input from a `PreToolUse` hook.
373
+ *
374
+ * Last-writer-wins in **registration order**: `executeHooks` uses
375
+ * `Promise.all`, which preserves input-array order, so the fold iterates
376
+ * outcomes in the same order they were pushed — outer loop over matchers
377
+ * as they sit in the registry, inner loop over each matcher's `hooks`
378
+ * array. The winner is therefore deterministic but may not match the
379
+ * order in which hooks actually completed. Consumers that want a single
380
+ * authoritative rewrite should still register one `updatedInput`-setting
381
+ * hook per matcher to avoid subtle precedence bugs.
382
+ */
383
+ updatedInput?: Record<string, unknown>;
384
+ /**
385
+ * Replacement tool output from a `PostToolUse` hook.
386
+ *
387
+ * Same last-writer-wins-in-registration-order semantics as
388
+ * `updatedInput`. Present only when at least one hook set it; `undefined`
389
+ * means "use the original tool output".
390
+ */
391
+ updatedOutput?: unknown;
392
+ /** Accumulated `additionalContext` strings from every hook, in order. */
393
+ additionalContexts: string[];
394
+ /** True if any hook returned `preventContinuation`. */
395
+ preventContinuation?: boolean;
396
+ /**
397
+ * Reason recorded alongside `preventContinuation`. First winner wins:
398
+ * once a hook sets both flags, later hooks that also set
399
+ * `preventContinuation` do not overwrite the reason.
400
+ */
401
+ stopReason?: string;
402
+ /** Error messages from hooks that threw; always present (possibly empty). */
403
+ errors: string[];
404
+ }
package/src/index.ts CHANGED
@@ -14,7 +14,14 @@ export * from './summarization';
14
14
  /* Tools */
15
15
  export * from './tools/Calculator';
16
16
  export * from './tools/CodeExecutor';
17
+ export * from './tools/BashExecutor';
17
18
  export * from './tools/ProgrammaticToolCalling';
19
+ export * from './tools/BashProgrammaticToolCalling';
20
+ export * from './tools/SkillTool';
21
+ export * from './tools/SubagentTool';
22
+ export * from './tools/subagent';
23
+ export * from './tools/ReadFile';
24
+ export * from './tools/skillCatalog';
18
25
  export * from './tools/ToolSearch';
19
26
  export * from './tools/ToolNode';
20
27
  export * from './tools/schema';
@@ -25,6 +32,9 @@ export * from './tools/search';
25
32
  export * from './common';
26
33
  export * from './utils';
27
34
 
35
+ /* Hooks */
36
+ export * from './hooks';
37
+
28
38
  /* Types */
29
39
  export type * from './types';
30
40
 
@@ -797,18 +797,39 @@ function contentPartCharLength(part: MessageContentComplex): number {
797
797
  return len;
798
798
  }
799
799
 
800
+ /** Extracts the skillName from a skill tool_call's args (string or object). */
801
+ function extractSkillName(args: unknown): string | undefined {
802
+ let parsed: Record<string, unknown> | undefined;
803
+ if (typeof args === 'string') {
804
+ try {
805
+ parsed = JSON.parse(args) as Record<string, unknown>;
806
+ } catch {
807
+ /* malformed args — skip */
808
+ }
809
+ } else {
810
+ parsed = args as Record<string, unknown> | undefined;
811
+ }
812
+ const name = parsed?.skillName;
813
+ return typeof name === 'string' && name !== '' ? name : undefined;
814
+ }
815
+
800
816
  /**
801
817
  * Formats an array of messages for LangChain, handling tool calls and creating ToolMessage instances.
802
818
  *
803
819
  * @param payload - The array of messages to format.
804
820
  * @param indexTokenCountMap - Optional map of message indices to token counts.
805
821
  * @param tools - Optional set of tool names that are allowed in the request.
822
+ * @param skills - Optional map of skill name to body for reconstructing skill HumanMessages.
806
823
  * @returns - Object containing formatted messages and updated indexTokenCountMap if provided.
807
824
  */
808
825
  export const formatAgentMessages = (
809
826
  payload: TPayload,
810
827
  indexTokenCountMap?: Record<number, number | undefined>,
811
- tools?: Set<string>
828
+ tools?: Set<string>,
829
+ /** Pre-resolved skill bodies keyed by skill name. When present, HumanMessages
830
+ * are reconstructed after skill ToolMessages to restore skill instructions
831
+ * that were only in LangGraph state during the original run. */
832
+ skills?: Map<string, string>
812
833
  ): {
813
834
  messages: Array<HumanMessage | AIMessage | SystemMessage | ToolMessage>;
814
835
  indexTokenCountMap?: Record<number, number>;
@@ -902,6 +923,7 @@ export const formatAgentMessages = (
902
923
  * - Dynamically expand the set when tool_search results are encountered
903
924
  */
904
925
  let processedMessage = message;
926
+ let pendingSkillNames: Set<string> | undefined;
905
927
  if (discoveredTools) {
906
928
  const content = message.content;
907
929
  if (content != null && Array.isArray(content)) {
@@ -950,8 +972,17 @@ export const formatAgentMessages = (
950
972
  }
951
973
 
952
974
  if (discoveredTools.has(toolName)) {
953
- /** Valid tool - keep it */
954
975
  filteredContent.push(part);
976
+ if (
977
+ toolName === Constants.SKILL_TOOL &&
978
+ skills?.size != null &&
979
+ skills.size > 0
980
+ ) {
981
+ const skillName = extractSkillName(part.tool_call.args) ?? '';
982
+ if (skillName) {
983
+ (pendingSkillNames ??= new Set()).add(skillName);
984
+ }
985
+ }
955
986
  } else {
956
987
  /** Invalid tool - convert to string for context preservation */
957
988
  if (
@@ -1027,6 +1058,25 @@ export const formatAgentMessages = (
1027
1058
  }
1028
1059
  }
1029
1060
 
1061
+ /** When tools filtering is off, still detect skill tool_calls for body reconstruction */
1062
+ if (!discoveredTools && skills?.size != null && skills.size > 0) {
1063
+ const content = processedMessage.content;
1064
+ if (Array.isArray(content)) {
1065
+ for (const part of content) {
1066
+ if (
1067
+ part.type !== ContentTypes.TOOL_CALL ||
1068
+ part.tool_call?.name !== Constants.SKILL_TOOL
1069
+ ) {
1070
+ continue;
1071
+ }
1072
+ const skillName = extractSkillName(part.tool_call.args) ?? '';
1073
+ if (skillName) {
1074
+ (pendingSkillNames ??= new Set()).add(skillName);
1075
+ }
1076
+ }
1077
+ }
1078
+ }
1079
+
1030
1080
  const formattedMessages = formatAssistantMessage(processedMessage);
1031
1081
  if (sourceMessageId != null && sourceMessageId !== '') {
1032
1082
  for (const formattedMessage of formattedMessages) {
@@ -1035,9 +1085,29 @@ export const formatAgentMessages = (
1035
1085
  }
1036
1086
  messages.push(...formattedMessages);
1037
1087
 
1038
- // Update the index mapping for this assistant message
1039
- // Store all indices that were created from this original message
1088
+ // Capture index range BEFORE skill body injection so injected
1089
+ // HumanMessages are excluded from the assistant's token distribution.
1040
1090
  const endMessageIndex = messages.length;
1091
+
1092
+ if (pendingSkillNames?.size != null && pendingSkillNames.size > 0) {
1093
+ for (const skillName of pendingSkillNames) {
1094
+ const body = skills?.get(skillName) ?? '';
1095
+ if (body) {
1096
+ messages.push(
1097
+ new HumanMessage({
1098
+ content: body,
1099
+ additional_kwargs: {
1100
+ role: 'user',
1101
+ isMeta: true,
1102
+ source: 'skill',
1103
+ skillName,
1104
+ },
1105
+ })
1106
+ );
1107
+ }
1108
+ }
1109
+ }
1110
+
1041
1111
  const resultIndices = [];
1042
1112
  for (let j = startMessageIndex; j < endMessageIndex; j++) {
1043
1113
  resultIndices.push(j);