@librechat/agents 3.1.64 → 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/llm/anthropic/types.cjs.map +1 -1
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs +69 -54
- package/dist/cjs/llm/anthropic/utils/message_inputs.cjs.map +1 -1
- package/dist/cjs/main.cjs +40 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/messages/core.cjs +8 -1
- package/dist/cjs/messages/core.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/llm/anthropic/types.mjs.map +1 -1
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs +69 -54
- package/dist/esm/llm/anthropic/utils/message_inputs.mjs.map +1 -1
- package/dist/esm/main.mjs +10 -1
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/messages/core.mjs +8 -1
- package/dist/esm/messages/core.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/llm/anthropic/types.d.ts +1 -1
- 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 +2 -2
- 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/llm/anthropic/types.ts +1 -1
- package/src/llm/anthropic/utils/message_inputs.ts +93 -68
- package/src/llm/anthropic/utils/server-tool-inputs.test.ts +349 -0
- package/src/messages/core.ts +8 -1
- 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
|
|
|
@@ -6,7 +6,7 @@ export type AnthropicMessageDeltaEvent = Anthropic.MessageDeltaEvent;
|
|
|
6
6
|
export type AnthropicMessageStartEvent = Anthropic.MessageStartEvent;
|
|
7
7
|
|
|
8
8
|
export type AnthropicToolResponse = {
|
|
9
|
-
type: 'tool_use';
|
|
9
|
+
type: 'tool_use' | 'server_tool_use';
|
|
10
10
|
id: string;
|
|
11
11
|
name: string;
|
|
12
12
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -144,7 +144,9 @@ export function _convertLangChainToolCallToAnthropic(
|
|
|
144
144
|
throw new Error('Anthropic requires all tool calls to have an "id".');
|
|
145
145
|
}
|
|
146
146
|
return {
|
|
147
|
-
type:
|
|
147
|
+
type: toolCall.id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)
|
|
148
|
+
? 'server_tool_use'
|
|
149
|
+
: 'tool_use',
|
|
148
150
|
id: toolCall.id,
|
|
149
151
|
name: toolCall.name,
|
|
150
152
|
input: toolCall.args,
|
|
@@ -364,87 +366,90 @@ function _formatContent(message: BaseMessage) {
|
|
|
364
366
|
} else {
|
|
365
367
|
const contentBlocks = content.map((contentPart) => {
|
|
366
368
|
/**
|
|
367
|
-
*
|
|
368
|
-
* These
|
|
369
|
-
*
|
|
370
|
-
*
|
|
369
|
+
* Normalize server_tool_use blocks into a clean shape the API accepts.
|
|
370
|
+
* These blocks may arrive with the correct type (server_tool_use) or mislabeled
|
|
371
|
+
* as text/tool_use after chunk concatenation or state serialization.
|
|
372
|
+
* Regardless of current type, if the id starts with 'srvtoolu_' we rebuild
|
|
373
|
+
* a clean block with only the properties the API expects.
|
|
371
374
|
*/
|
|
372
375
|
if (
|
|
373
376
|
'id' in contentPart &&
|
|
374
|
-
'
|
|
375
|
-
|
|
376
|
-
|
|
377
|
+
typeof (contentPart as Record<string, unknown>).id === 'string' &&
|
|
378
|
+
((contentPart as Record<string, unknown>).id as string).startsWith(
|
|
379
|
+
Constants.ANTHROPIC_SERVER_TOOL_PREFIX
|
|
380
|
+
) &&
|
|
381
|
+
'name' in contentPart
|
|
377
382
|
) {
|
|
378
383
|
const rawPart = contentPart as Record<string, unknown>;
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
if (typeof input === 'string') {
|
|
386
|
-
try {
|
|
387
|
-
input = JSON.parse(input);
|
|
388
|
-
} catch {
|
|
389
|
-
input = {};
|
|
390
|
-
}
|
|
384
|
+
let input = rawPart.input;
|
|
385
|
+
if (typeof input === 'string') {
|
|
386
|
+
try {
|
|
387
|
+
input = JSON.parse(input);
|
|
388
|
+
} catch {
|
|
389
|
+
input = {};
|
|
391
390
|
}
|
|
391
|
+
}
|
|
392
|
+
const corrected: AnthropicServerToolUseBlockParam = {
|
|
393
|
+
type: 'server_tool_use',
|
|
394
|
+
id: rawPart.id as string,
|
|
395
|
+
name: (rawPart.name ?? 'web_search') as 'web_search',
|
|
396
|
+
input: (input ?? {}) as Record<string, unknown>,
|
|
397
|
+
};
|
|
398
|
+
return corrected;
|
|
399
|
+
}
|
|
392
400
|
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
401
|
+
/**
|
|
402
|
+
* Normalize web_search_tool_result blocks into a clean shape.
|
|
403
|
+
* Same rationale as above — the block may carry extra properties from
|
|
404
|
+
* streaming (input, index, etc.) that the API rejects. Rebuild cleanly.
|
|
405
|
+
*/
|
|
406
|
+
if (
|
|
407
|
+
'tool_use_id' in contentPart &&
|
|
408
|
+
typeof (contentPart as Record<string, unknown>).tool_use_id ===
|
|
409
|
+
'string' &&
|
|
410
|
+
(
|
|
411
|
+
(contentPart as Record<string, unknown>).tool_use_id as string
|
|
412
|
+
).startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) &&
|
|
413
|
+
'content' in contentPart
|
|
414
|
+
) {
|
|
415
|
+
const rawPart = contentPart as Record<string, unknown>;
|
|
416
|
+
const content = rawPart.content;
|
|
417
|
+
const isValidContent =
|
|
418
|
+
Array.isArray(content) ||
|
|
419
|
+
(content != null &&
|
|
420
|
+
typeof content === 'object' &&
|
|
421
|
+
'type' in content &&
|
|
422
|
+
(content as Record<string, unknown>).type ===
|
|
423
|
+
'web_search_tool_result_error');
|
|
424
|
+
|
|
425
|
+
if (isValidContent) {
|
|
426
|
+
const corrected: AnthropicWebSearchToolResultBlockParam = {
|
|
427
|
+
type: 'web_search_tool_result',
|
|
428
|
+
tool_use_id: rawPart.tool_use_id as string,
|
|
429
|
+
content:
|
|
430
|
+
content as AnthropicWebSearchToolResultBlockParam['content'],
|
|
398
431
|
};
|
|
399
|
-
|
|
400
432
|
return corrected;
|
|
401
433
|
}
|
|
402
|
-
|
|
403
|
-
// If it's not a server tool, skip it (return null to filter it out)
|
|
404
434
|
return null;
|
|
405
435
|
}
|
|
406
436
|
|
|
407
437
|
/**
|
|
408
|
-
*
|
|
409
|
-
* These have tool_use_id and nested content - fix their type instead of filtering.
|
|
410
|
-
* Only correct if we can confirm it's a web search result by checking the tool_use_id prefix.
|
|
411
|
-
*
|
|
412
|
-
* Handles both success results (array content) and error results (object with error_code).
|
|
438
|
+
* Skip non-server malformed blocks that have tool fields mixed with text type.
|
|
413
439
|
*/
|
|
440
|
+
if (
|
|
441
|
+
'id' in contentPart &&
|
|
442
|
+
'name' in contentPart &&
|
|
443
|
+
'input' in contentPart &&
|
|
444
|
+
contentPart.type === 'text'
|
|
445
|
+
) {
|
|
446
|
+
return null;
|
|
447
|
+
}
|
|
414
448
|
if (
|
|
415
449
|
'tool_use_id' in contentPart &&
|
|
416
450
|
'content' in contentPart &&
|
|
417
451
|
contentPart.type === 'text'
|
|
418
452
|
) {
|
|
419
|
-
const rawPart = contentPart as Record<string, unknown>;
|
|
420
|
-
const toolUseId = rawPart.tool_use_id as string;
|
|
421
|
-
const content = rawPart.content;
|
|
422
|
-
|
|
423
|
-
if (
|
|
424
|
-
toolUseId &&
|
|
425
|
-
toolUseId.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)
|
|
426
|
-
) {
|
|
427
|
-
// Verify content is either an array (success) or error object
|
|
428
|
-
const isValidContent =
|
|
429
|
-
Array.isArray(content) ||
|
|
430
|
-
(content != null &&
|
|
431
|
-
typeof content === 'object' &&
|
|
432
|
-
'type' in content &&
|
|
433
|
-
(content as Record<string, unknown>).type ===
|
|
434
|
-
'web_search_tool_result_error');
|
|
435
|
-
|
|
436
|
-
if (isValidContent) {
|
|
437
|
-
const corrected: AnthropicWebSearchToolResultBlockParam = {
|
|
438
|
-
type: 'web_search_tool_result',
|
|
439
|
-
tool_use_id: toolUseId,
|
|
440
|
-
content:
|
|
441
|
-
content as AnthropicWebSearchToolResultBlockParam['content'],
|
|
442
|
-
};
|
|
443
|
-
return corrected;
|
|
444
|
-
}
|
|
445
|
-
}
|
|
446
|
-
|
|
447
|
-
// If it's not a recognized server tool result format, skip it (return null to filter it out)
|
|
448
453
|
return null;
|
|
449
454
|
}
|
|
450
455
|
|
|
@@ -540,6 +545,15 @@ function _formatContent(message: BaseMessage) {
|
|
|
540
545
|
contentPartCopy.type = 'tool_use';
|
|
541
546
|
}
|
|
542
547
|
|
|
548
|
+
if (
|
|
549
|
+
contentPartCopy.type === 'tool_use' &&
|
|
550
|
+
'id' in contentPartCopy &&
|
|
551
|
+
typeof contentPartCopy.id === 'string' &&
|
|
552
|
+
contentPartCopy.id.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX)
|
|
553
|
+
) {
|
|
554
|
+
contentPartCopy.type = 'server_tool_use';
|
|
555
|
+
}
|
|
556
|
+
|
|
543
557
|
if ('input' in contentPartCopy) {
|
|
544
558
|
// Anthropic tool use inputs should be valid objects, when applicable.
|
|
545
559
|
if (typeof contentPartCopy.input === 'string') {
|
|
@@ -594,7 +608,11 @@ function _formatContent(message: BaseMessage) {
|
|
|
594
608
|
throw new Error('Unsupported message content format');
|
|
595
609
|
}
|
|
596
610
|
});
|
|
597
|
-
return contentBlocks.filter(
|
|
611
|
+
return contentBlocks.filter(
|
|
612
|
+
(block) =>
|
|
613
|
+
block !== null &&
|
|
614
|
+
!(block.type === 'text' && 'text' in block && block.text === '')
|
|
615
|
+
);
|
|
598
616
|
}
|
|
599
617
|
}
|
|
600
618
|
|
|
@@ -631,19 +649,26 @@ export function _convertMessagesToAnthropicPayload(
|
|
|
631
649
|
}
|
|
632
650
|
if (isAIMessage(message) && !!message.tool_calls?.length) {
|
|
633
651
|
if (typeof message.content === 'string') {
|
|
652
|
+
const clientToolCalls = message.tool_calls.filter(
|
|
653
|
+
(tc) =>
|
|
654
|
+
!(
|
|
655
|
+
tc.id?.startsWith(Constants.ANTHROPIC_SERVER_TOOL_PREFIX) ?? false
|
|
656
|
+
)
|
|
657
|
+
);
|
|
634
658
|
if (message.content === '') {
|
|
635
659
|
return {
|
|
636
660
|
role,
|
|
637
|
-
content:
|
|
638
|
-
|
|
639
|
-
|
|
661
|
+
content:
|
|
662
|
+
clientToolCalls.length > 0
|
|
663
|
+
? clientToolCalls.map(_convertLangChainToolCallToAnthropic)
|
|
664
|
+
: [{ type: 'text' as const, text: ' ' }],
|
|
640
665
|
};
|
|
641
666
|
} else {
|
|
642
667
|
return {
|
|
643
668
|
role,
|
|
644
669
|
content: [
|
|
645
|
-
{ type: 'text', text: message.content },
|
|
646
|
-
...
|
|
670
|
+
{ type: 'text' as const, text: message.content },
|
|
671
|
+
...clientToolCalls.map(_convertLangChainToolCallToAnthropic),
|
|
647
672
|
],
|
|
648
673
|
};
|
|
649
674
|
}
|