@librechat/agents 3.1.65 → 3.1.66-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.
- package/dist/cjs/common/enum.cjs +13 -0
- package/dist/cjs/common/enum.cjs.map +1 -1
- package/dist/cjs/graphs/Graph.cjs +3 -0
- package/dist/cjs/graphs/Graph.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 +40 -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/tools/BashExecutor.cjs +175 -0
- package/dist/cjs/tools/BashExecutor.cjs.map +1 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs +296 -0
- package/dist/cjs/tools/BashProgrammaticToolCalling.cjs.map +1 -0
- 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/ToolNode.cjs +304 -140
- package/dist/cjs/tools/ToolNode.cjs.map +1 -1
- package/dist/cjs/tools/skillCatalog.cjs +84 -0
- package/dist/cjs/tools/skillCatalog.cjs.map +1 -0
- package/dist/esm/common/enum.mjs +12 -1
- package/dist/esm/common/enum.mjs.map +1 -1
- package/dist/esm/graphs/Graph.mjs +3 -0
- package/dist/esm/graphs/Graph.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 +10 -1
- 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/tools/BashExecutor.mjs +169 -0
- package/dist/esm/tools/BashExecutor.mjs.map +1 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs +287 -0
- package/dist/esm/tools/BashProgrammaticToolCalling.mjs.map +1 -0
- 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/ToolNode.mjs +306 -142
- package/dist/esm/tools/ToolNode.mjs.map +1 -1
- package/dist/esm/tools/skillCatalog.mjs +82 -0
- package/dist/esm/tools/skillCatalog.mjs.map +1 -0
- package/dist/types/common/enum.d.ts +7 -1
- package/dist/types/graphs/Graph.d.ts +2 -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 +309 -0
- package/dist/types/index.d.ts +6 -0
- package/dist/types/messages/format.d.ts +2 -1
- package/dist/types/run.d.ts +1 -0
- package/dist/types/tools/BashExecutor.d.ts +45 -0
- package/dist/types/tools/BashProgrammaticToolCalling.d.ts +72 -0
- package/dist/types/tools/ReadFile.d.ts +28 -0
- package/dist/types/tools/SkillTool.d.ts +40 -0
- package/dist/types/tools/ToolNode.d.ts +24 -2
- package/dist/types/tools/skillCatalog.d.ts +19 -0
- 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 -1
- package/package.json +1 -1
- package/src/common/enum.ts +12 -0
- package/src/graphs/Graph.ts +4 -0
- package/src/hooks/HookRegistry.ts +208 -0
- package/src/hooks/__tests__/HookRegistry.test.ts +190 -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 +55 -0
- package/src/hooks/matchers.ts +280 -0
- package/src/hooks/types.ts +388 -0
- package/src/index.ts +8 -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/tools/BashExecutor.ts +205 -0
- package/src/tools/BashProgrammaticToolCalling.ts +397 -0
- package/src/tools/ReadFile.ts +39 -0
- package/src/tools/SkillTool.ts +46 -0
- package/src/tools/ToolNode.ts +391 -169
- package/src/tools/__tests__/ReadFile.test.ts +44 -0
- package/src/tools/__tests__/SkillTool.test.ts +442 -0
- package/src/tools/__tests__/ToolNode.session.test.ts +12 -12
- package/src/tools/__tests__/skillCatalog.test.ts +161 -0
- package/src/tools/skillCatalog.ts +126 -0
- 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 -1
|
@@ -0,0 +1,388 @@
|
|
|
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
|
+
trigger: 'threshold' | 'manual' | 'error';
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export interface PostCompactHookInput extends BaseHookInput {
|
|
146
|
+
hook_event_name: 'PostCompact';
|
|
147
|
+
summary: string;
|
|
148
|
+
messagesAfterCount: number;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/** Discriminated union of every hook input shape. */
|
|
152
|
+
export type HookInput =
|
|
153
|
+
| RunStartHookInput
|
|
154
|
+
| UserPromptSubmitHookInput
|
|
155
|
+
| PreToolUseHookInput
|
|
156
|
+
| PostToolUseHookInput
|
|
157
|
+
| PostToolUseFailureHookInput
|
|
158
|
+
| PermissionDeniedHookInput
|
|
159
|
+
| SubagentStartHookInput
|
|
160
|
+
| SubagentStopHookInput
|
|
161
|
+
| StopHookInput
|
|
162
|
+
| StopFailureHookInput
|
|
163
|
+
| PreCompactHookInput
|
|
164
|
+
| PostCompactHookInput;
|
|
165
|
+
|
|
166
|
+
/** Compile-time map from event name to its input shape. */
|
|
167
|
+
export type HookInputByEvent = {
|
|
168
|
+
RunStart: RunStartHookInput;
|
|
169
|
+
UserPromptSubmit: UserPromptSubmitHookInput;
|
|
170
|
+
PreToolUse: PreToolUseHookInput;
|
|
171
|
+
PostToolUse: PostToolUseHookInput;
|
|
172
|
+
PostToolUseFailure: PostToolUseFailureHookInput;
|
|
173
|
+
PermissionDenied: PermissionDeniedHookInput;
|
|
174
|
+
SubagentStart: SubagentStartHookInput;
|
|
175
|
+
SubagentStop: SubagentStopHookInput;
|
|
176
|
+
Stop: StopHookInput;
|
|
177
|
+
StopFailure: StopFailureHookInput;
|
|
178
|
+
PreCompact: PreCompactHookInput;
|
|
179
|
+
PostCompact: PostCompactHookInput;
|
|
180
|
+
};
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* Fields common to every hook output. Hooks that have nothing to say simply
|
|
184
|
+
* return `{}` (or omit the fields below).
|
|
185
|
+
*/
|
|
186
|
+
export interface BaseHookOutput {
|
|
187
|
+
/** Context string to inject into the conversation. Accumulated across hooks. */
|
|
188
|
+
additionalContext?: string;
|
|
189
|
+
/** True to prevent the next model turn. Any hook can set this. */
|
|
190
|
+
preventContinuation?: boolean;
|
|
191
|
+
/** Reason reported alongside `preventContinuation`. */
|
|
192
|
+
stopReason?: string;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
export type RunStartHookOutput = BaseHookOutput;
|
|
196
|
+
|
|
197
|
+
export interface UserPromptSubmitHookOutput extends BaseHookOutput {
|
|
198
|
+
decision?: ToolDecision;
|
|
199
|
+
reason?: string;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
export interface PreToolUseHookOutput extends BaseHookOutput {
|
|
203
|
+
decision?: ToolDecision;
|
|
204
|
+
reason?: string;
|
|
205
|
+
/**
|
|
206
|
+
* Replacement tool input. Merged into the pending tool call by the host.
|
|
207
|
+
*
|
|
208
|
+
* When multiple hooks set `updatedInput` within a single `executeHooks`
|
|
209
|
+
* call, the last writer in registration order wins (outer loop: matcher
|
|
210
|
+
* registration order; inner loop: hook position within the matcher). The
|
|
211
|
+
* winner is deterministic — `Promise.all` preserves input-array order.
|
|
212
|
+
* Consumers that need a single authoritative rewrite should still scope
|
|
213
|
+
* `updatedInput` to one hook per matcher to avoid confusing precedence.
|
|
214
|
+
*/
|
|
215
|
+
updatedInput?: Record<string, unknown>;
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export interface PostToolUseHookOutput extends BaseHookOutput {
|
|
219
|
+
/**
|
|
220
|
+
* Replacement tool output. Flows through the aggregated result so the
|
|
221
|
+
* host can substitute it before appending the tool result message.
|
|
222
|
+
* Ordering semantics match `PreToolUseHookOutput.updatedInput`:
|
|
223
|
+
* last-writer-wins in registration order.
|
|
224
|
+
*/
|
|
225
|
+
updatedOutput?: unknown;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
export type PostToolUseFailureHookOutput = BaseHookOutput;
|
|
229
|
+
|
|
230
|
+
export type PermissionDeniedHookOutput = BaseHookOutput;
|
|
231
|
+
|
|
232
|
+
export interface SubagentStartHookOutput extends BaseHookOutput {
|
|
233
|
+
decision?: ToolDecision;
|
|
234
|
+
reason?: string;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export type SubagentStopHookOutput = BaseHookOutput;
|
|
238
|
+
|
|
239
|
+
export interface StopHookOutput extends BaseHookOutput {
|
|
240
|
+
decision?: StopDecision;
|
|
241
|
+
reason?: string;
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
export type StopFailureHookOutput = BaseHookOutput;
|
|
245
|
+
|
|
246
|
+
export type PreCompactHookOutput = BaseHookOutput;
|
|
247
|
+
|
|
248
|
+
export type PostCompactHookOutput = BaseHookOutput;
|
|
249
|
+
|
|
250
|
+
/** Compile-time map from event name to its output shape. */
|
|
251
|
+
export type HookOutputByEvent = {
|
|
252
|
+
RunStart: RunStartHookOutput;
|
|
253
|
+
UserPromptSubmit: UserPromptSubmitHookOutput;
|
|
254
|
+
PreToolUse: PreToolUseHookOutput;
|
|
255
|
+
PostToolUse: PostToolUseHookOutput;
|
|
256
|
+
PostToolUseFailure: PostToolUseFailureHookOutput;
|
|
257
|
+
PermissionDenied: PermissionDeniedHookOutput;
|
|
258
|
+
SubagentStart: SubagentStartHookOutput;
|
|
259
|
+
SubagentStop: SubagentStopHookOutput;
|
|
260
|
+
Stop: StopHookOutput;
|
|
261
|
+
StopFailure: StopFailureHookOutput;
|
|
262
|
+
PreCompact: PreCompactHookOutput;
|
|
263
|
+
PostCompact: PostCompactHookOutput;
|
|
264
|
+
};
|
|
265
|
+
|
|
266
|
+
/** Superset output shape used by the executor's fold loop. */
|
|
267
|
+
export type HookOutput =
|
|
268
|
+
| RunStartHookOutput
|
|
269
|
+
| UserPromptSubmitHookOutput
|
|
270
|
+
| PreToolUseHookOutput
|
|
271
|
+
| PostToolUseHookOutput
|
|
272
|
+
| PostToolUseFailureHookOutput
|
|
273
|
+
| PermissionDeniedHookOutput
|
|
274
|
+
| SubagentStartHookOutput
|
|
275
|
+
| SubagentStopHookOutput
|
|
276
|
+
| StopHookOutput
|
|
277
|
+
| StopFailureHookOutput
|
|
278
|
+
| PreCompactHookOutput
|
|
279
|
+
| PostCompactHookOutput;
|
|
280
|
+
|
|
281
|
+
/**
|
|
282
|
+
* A hook callback is a plain async function registered against a specific
|
|
283
|
+
* event. The `signal` is always supplied by `executeHooks` and combines the
|
|
284
|
+
* batch's parent signal with the per-hook timeout — callbacks that perform
|
|
285
|
+
* long-running work should observe it.
|
|
286
|
+
*/
|
|
287
|
+
export type HookCallback<E extends HookEvent = HookEvent> = (
|
|
288
|
+
input: HookInputByEvent[E],
|
|
289
|
+
signal: AbortSignal
|
|
290
|
+
) => HookOutputByEvent[E] | Promise<HookOutputByEvent[E]>;
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A matcher groups one or more callbacks under a shared regex filter and
|
|
294
|
+
* shared timeout/once/internal flags. The generic `E` ties the callback
|
|
295
|
+
* types to the event the matcher is registered against.
|
|
296
|
+
*/
|
|
297
|
+
export interface HookMatcher<E extends HookEvent = HookEvent> {
|
|
298
|
+
/**
|
|
299
|
+
* Regex pattern matched against the event's primary query string (e.g.
|
|
300
|
+
* the tool name for `PreToolUse`, the agent type for `SubagentStart`).
|
|
301
|
+
*
|
|
302
|
+
* Omitted or empty means "always match". For events that do not supply a
|
|
303
|
+
* query string (`RunStart`, `Stop`, etc.), only wildcard matchers fire —
|
|
304
|
+
* a non-empty pattern on such events will never match.
|
|
305
|
+
*
|
|
306
|
+
* Patterns are treated as trusted input: `executeHooks` compiles them
|
|
307
|
+
* with `new RegExp(pattern)` without any sandbox, and a pathological
|
|
308
|
+
* pattern can block the event loop. Host registration code is expected
|
|
309
|
+
* to validate or length-bound patterns that originate from user input.
|
|
310
|
+
*/
|
|
311
|
+
pattern?: string;
|
|
312
|
+
/** Callbacks that fire when the matcher hits. Executed in parallel. */
|
|
313
|
+
hooks: HookCallback<E>[];
|
|
314
|
+
/** Per-matcher timeout in ms. Defaults to the executor's batch timeout. */
|
|
315
|
+
timeout?: number;
|
|
316
|
+
/**
|
|
317
|
+
* Atomically remove the matcher before its first dispatch.
|
|
318
|
+
*
|
|
319
|
+
* `executeHooks` claims `once: true` matchers synchronously — between
|
|
320
|
+
* `getMatchers` and its first `await` — so two concurrent calls cannot
|
|
321
|
+
* both dispatch the same matcher. Whichever call runs its sync prefix
|
|
322
|
+
* first wins the matcher; the other sees an empty bucket.
|
|
323
|
+
*
|
|
324
|
+
* Semantics are "at most one dispatch, ever" — if every hook in the
|
|
325
|
+
* matcher throws, the matcher is still gone. Use `once` for
|
|
326
|
+
* fire-and-forget bootstrapping (registration, telemetry, setup). Hosts
|
|
327
|
+
* that need retry semantics should register a normal matcher and
|
|
328
|
+
* self-unregister via the callback returned from `registry.register`.
|
|
329
|
+
*/
|
|
330
|
+
once?: boolean;
|
|
331
|
+
/** Internal hooks are excluded from telemetry and non-fatal error logging. */
|
|
332
|
+
internal?: boolean;
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
/**
|
|
336
|
+
* Storage shape for matchers keyed by event. Each event's matcher list is
|
|
337
|
+
* a generic array parameterized by that event type, so lookup via
|
|
338
|
+
* `HooksByEvent[E]` preserves type-safe callback signatures.
|
|
339
|
+
*/
|
|
340
|
+
export type HooksByEvent = {
|
|
341
|
+
[E in HookEvent]?: HookMatcher<E>[];
|
|
342
|
+
};
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Aggregated result of a single `executeHooks` call. Fields are populated
|
|
346
|
+
* according to the fold rules in `executeHooks.ts`.
|
|
347
|
+
*/
|
|
348
|
+
export interface AggregatedHookResult {
|
|
349
|
+
/** Folded tool-gating decision; `deny > ask > allow`. */
|
|
350
|
+
decision?: ToolDecision;
|
|
351
|
+
/** Folded stop decision; any `block` wins. */
|
|
352
|
+
stopDecision?: StopDecision;
|
|
353
|
+
/** Reason from the hook that set the winning decision. */
|
|
354
|
+
reason?: string;
|
|
355
|
+
/**
|
|
356
|
+
* Replacement tool input from a `PreToolUse` hook.
|
|
357
|
+
*
|
|
358
|
+
* Last-writer-wins in **registration order**: `executeHooks` uses
|
|
359
|
+
* `Promise.all`, which preserves input-array order, so the fold iterates
|
|
360
|
+
* outcomes in the same order they were pushed — outer loop over matchers
|
|
361
|
+
* as they sit in the registry, inner loop over each matcher's `hooks`
|
|
362
|
+
* array. The winner is therefore deterministic but may not match the
|
|
363
|
+
* order in which hooks actually completed. Consumers that want a single
|
|
364
|
+
* authoritative rewrite should still register one `updatedInput`-setting
|
|
365
|
+
* hook per matcher to avoid subtle precedence bugs.
|
|
366
|
+
*/
|
|
367
|
+
updatedInput?: Record<string, unknown>;
|
|
368
|
+
/**
|
|
369
|
+
* Replacement tool output from a `PostToolUse` hook.
|
|
370
|
+
*
|
|
371
|
+
* Same last-writer-wins-in-registration-order semantics as
|
|
372
|
+
* `updatedInput`. Present only when at least one hook set it; `undefined`
|
|
373
|
+
* means "use the original tool output".
|
|
374
|
+
*/
|
|
375
|
+
updatedOutput?: unknown;
|
|
376
|
+
/** Accumulated `additionalContext` strings from every hook, in order. */
|
|
377
|
+
additionalContexts: string[];
|
|
378
|
+
/** True if any hook returned `preventContinuation`. */
|
|
379
|
+
preventContinuation?: boolean;
|
|
380
|
+
/**
|
|
381
|
+
* Reason recorded alongside `preventContinuation`. First winner wins:
|
|
382
|
+
* once a hook sets both flags, later hooks that also set
|
|
383
|
+
* `preventContinuation` do not overwrite the reason.
|
|
384
|
+
*/
|
|
385
|
+
stopReason?: string;
|
|
386
|
+
/** Error messages from hooks that threw; always present (possibly empty). */
|
|
387
|
+
errors: string[];
|
|
388
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -14,7 +14,12 @@ 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/ReadFile';
|
|
22
|
+
export * from './tools/skillCatalog';
|
|
18
23
|
export * from './tools/ToolSearch';
|
|
19
24
|
export * from './tools/ToolNode';
|
|
20
25
|
export * from './tools/schema';
|
|
@@ -25,6 +30,9 @@ export * from './tools/search';
|
|
|
25
30
|
export * from './common';
|
|
26
31
|
export * from './utils';
|
|
27
32
|
|
|
33
|
+
/* Hooks */
|
|
34
|
+
export * from './hooks';
|
|
35
|
+
|
|
28
36
|
/* Types */
|
|
29
37
|
export type * from './types';
|
|
30
38
|
|
package/src/messages/format.ts
CHANGED
|
@@ -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
|
-
//
|
|
1039
|
-
//
|
|
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);
|