@compilr-dev/agents 0.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1277 -0
- package/dist/agent.d.ts +1272 -0
- package/dist/agent.js +1912 -0
- package/dist/anchors/builtin.d.ts +24 -0
- package/dist/anchors/builtin.js +61 -0
- package/dist/anchors/index.d.ts +6 -0
- package/dist/anchors/index.js +5 -0
- package/dist/anchors/manager.d.ts +115 -0
- package/dist/anchors/manager.js +412 -0
- package/dist/anchors/types.d.ts +168 -0
- package/dist/anchors/types.js +10 -0
- package/dist/context/index.d.ts +12 -0
- package/dist/context/index.js +10 -0
- package/dist/context/manager.d.ts +224 -0
- package/dist/context/manager.js +770 -0
- package/dist/context/types.d.ts +377 -0
- package/dist/context/types.js +7 -0
- package/dist/costs/index.d.ts +8 -0
- package/dist/costs/index.js +7 -0
- package/dist/costs/tracker.d.ts +121 -0
- package/dist/costs/tracker.js +295 -0
- package/dist/costs/types.d.ts +157 -0
- package/dist/costs/types.js +8 -0
- package/dist/errors.d.ts +178 -0
- package/dist/errors.js +249 -0
- package/dist/guardrails/builtin.d.ts +27 -0
- package/dist/guardrails/builtin.js +223 -0
- package/dist/guardrails/index.d.ts +6 -0
- package/dist/guardrails/index.js +5 -0
- package/dist/guardrails/manager.d.ts +117 -0
- package/dist/guardrails/manager.js +288 -0
- package/dist/guardrails/types.d.ts +159 -0
- package/dist/guardrails/types.js +7 -0
- package/dist/hooks/index.d.ts +31 -0
- package/dist/hooks/index.js +29 -0
- package/dist/hooks/manager.d.ts +147 -0
- package/dist/hooks/manager.js +600 -0
- package/dist/hooks/types.d.ts +368 -0
- package/dist/hooks/types.js +12 -0
- package/dist/index.d.ts +45 -0
- package/dist/index.js +73 -0
- package/dist/mcp/client.d.ts +93 -0
- package/dist/mcp/client.js +287 -0
- package/dist/mcp/errors.d.ts +60 -0
- package/dist/mcp/errors.js +78 -0
- package/dist/mcp/index.d.ts +43 -0
- package/dist/mcp/index.js +45 -0
- package/dist/mcp/manager.d.ts +120 -0
- package/dist/mcp/manager.js +276 -0
- package/dist/mcp/tools.d.ts +54 -0
- package/dist/mcp/tools.js +99 -0
- package/dist/mcp/types.d.ts +150 -0
- package/dist/mcp/types.js +40 -0
- package/dist/memory/index.d.ts +8 -0
- package/dist/memory/index.js +7 -0
- package/dist/memory/loader.d.ts +114 -0
- package/dist/memory/loader.js +463 -0
- package/dist/memory/types.d.ts +182 -0
- package/dist/memory/types.js +8 -0
- package/dist/messages/index.d.ts +82 -0
- package/dist/messages/index.js +155 -0
- package/dist/permissions/index.d.ts +5 -0
- package/dist/permissions/index.js +4 -0
- package/dist/permissions/manager.d.ts +125 -0
- package/dist/permissions/manager.js +379 -0
- package/dist/permissions/types.d.ts +162 -0
- package/dist/permissions/types.js +7 -0
- package/dist/providers/claude.d.ts +90 -0
- package/dist/providers/claude.js +348 -0
- package/dist/providers/index.d.ts +8 -0
- package/dist/providers/index.js +11 -0
- package/dist/providers/mock.d.ts +133 -0
- package/dist/providers/mock.js +204 -0
- package/dist/providers/types.d.ts +168 -0
- package/dist/providers/types.js +4 -0
- package/dist/rate-limit/index.d.ts +45 -0
- package/dist/rate-limit/index.js +47 -0
- package/dist/rate-limit/limiter.d.ts +104 -0
- package/dist/rate-limit/limiter.js +326 -0
- package/dist/rate-limit/provider-wrapper.d.ts +112 -0
- package/dist/rate-limit/provider-wrapper.js +201 -0
- package/dist/rate-limit/retry.d.ts +108 -0
- package/dist/rate-limit/retry.js +287 -0
- package/dist/rate-limit/types.d.ts +181 -0
- package/dist/rate-limit/types.js +22 -0
- package/dist/rehearsal/file-analyzer.d.ts +22 -0
- package/dist/rehearsal/file-analyzer.js +351 -0
- package/dist/rehearsal/git-analyzer.d.ts +22 -0
- package/dist/rehearsal/git-analyzer.js +472 -0
- package/dist/rehearsal/index.d.ts +35 -0
- package/dist/rehearsal/index.js +36 -0
- package/dist/rehearsal/manager.d.ts +100 -0
- package/dist/rehearsal/manager.js +290 -0
- package/dist/rehearsal/types.d.ts +235 -0
- package/dist/rehearsal/types.js +8 -0
- package/dist/skills/index.d.ts +160 -0
- package/dist/skills/index.js +282 -0
- package/dist/state/agent-state.d.ts +41 -0
- package/dist/state/agent-state.js +88 -0
- package/dist/state/checkpointer.d.ts +110 -0
- package/dist/state/checkpointer.js +362 -0
- package/dist/state/errors.d.ts +66 -0
- package/dist/state/errors.js +88 -0
- package/dist/state/index.d.ts +35 -0
- package/dist/state/index.js +37 -0
- package/dist/state/serializer.d.ts +55 -0
- package/dist/state/serializer.js +172 -0
- package/dist/state/types.d.ts +312 -0
- package/dist/state/types.js +14 -0
- package/dist/tools/builtin/bash-output.d.ts +61 -0
- package/dist/tools/builtin/bash-output.js +90 -0
- package/dist/tools/builtin/bash.d.ts +150 -0
- package/dist/tools/builtin/bash.js +354 -0
- package/dist/tools/builtin/edit.d.ts +50 -0
- package/dist/tools/builtin/edit.js +215 -0
- package/dist/tools/builtin/glob.d.ts +62 -0
- package/dist/tools/builtin/glob.js +244 -0
- package/dist/tools/builtin/grep.d.ts +74 -0
- package/dist/tools/builtin/grep.js +363 -0
- package/dist/tools/builtin/index.d.ts +44 -0
- package/dist/tools/builtin/index.js +69 -0
- package/dist/tools/builtin/kill-shell.d.ts +44 -0
- package/dist/tools/builtin/kill-shell.js +80 -0
- package/dist/tools/builtin/read-file.d.ts +57 -0
- package/dist/tools/builtin/read-file.js +184 -0
- package/dist/tools/builtin/shell-manager.d.ts +176 -0
- package/dist/tools/builtin/shell-manager.js +337 -0
- package/dist/tools/builtin/task.d.ts +202 -0
- package/dist/tools/builtin/task.js +350 -0
- package/dist/tools/builtin/todo.d.ts +207 -0
- package/dist/tools/builtin/todo.js +453 -0
- package/dist/tools/builtin/utils.d.ts +27 -0
- package/dist/tools/builtin/utils.js +70 -0
- package/dist/tools/builtin/web-fetch.d.ts +96 -0
- package/dist/tools/builtin/web-fetch.js +290 -0
- package/dist/tools/builtin/write-file.d.ts +54 -0
- package/dist/tools/builtin/write-file.js +147 -0
- package/dist/tools/define.d.ts +60 -0
- package/dist/tools/define.js +65 -0
- package/dist/tools/index.d.ts +10 -0
- package/dist/tools/index.js +37 -0
- package/dist/tools/registry.d.ts +79 -0
- package/dist/tools/registry.js +151 -0
- package/dist/tools/types.d.ts +59 -0
- package/dist/tools/types.js +4 -0
- package/dist/tracing/hooks.d.ts +58 -0
- package/dist/tracing/hooks.js +377 -0
- package/dist/tracing/index.d.ts +51 -0
- package/dist/tracing/index.js +55 -0
- package/dist/tracing/logging.d.ts +78 -0
- package/dist/tracing/logging.js +310 -0
- package/dist/tracing/manager.d.ts +160 -0
- package/dist/tracing/manager.js +468 -0
- package/dist/tracing/otel.d.ts +102 -0
- package/dist/tracing/otel.js +246 -0
- package/dist/tracing/types.d.ts +346 -0
- package/dist/tracing/types.js +38 -0
- package/dist/utils/index.d.ts +23 -0
- package/dist/utils/index.js +44 -0
- package/package.json +79 -0
|
@@ -0,0 +1,368 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hooks System - Lifecycle hooks for agent customization
|
|
3
|
+
*
|
|
4
|
+
* Hooks provide extension points for customizing agent behavior without
|
|
5
|
+
* subclassing. They enable middleware-like patterns for:
|
|
6
|
+
* - Logging and tracing
|
|
7
|
+
* - Input/output transformation
|
|
8
|
+
* - Custom validation
|
|
9
|
+
* - Metrics collection
|
|
10
|
+
* - Integration with external systems
|
|
11
|
+
*/
|
|
12
|
+
import type { Message } from '../providers/types.js';
|
|
13
|
+
import type { ToolDefinition, ToolExecutionResult } from '../tools/types.js';
|
|
14
|
+
/**
|
|
15
|
+
* Context available in all hook calls
|
|
16
|
+
*/
|
|
17
|
+
export interface HookContext {
|
|
18
|
+
/**
|
|
19
|
+
* Current session ID
|
|
20
|
+
*/
|
|
21
|
+
sessionId: string;
|
|
22
|
+
/**
|
|
23
|
+
* Current iteration number (1-indexed)
|
|
24
|
+
*/
|
|
25
|
+
iteration: number;
|
|
26
|
+
/**
|
|
27
|
+
* Abort signal for cancellation
|
|
28
|
+
*/
|
|
29
|
+
signal?: AbortSignal;
|
|
30
|
+
/**
|
|
31
|
+
* Custom metadata that can be passed between hooks
|
|
32
|
+
*/
|
|
33
|
+
metadata: Record<string, unknown>;
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Context for iteration hooks
|
|
37
|
+
*/
|
|
38
|
+
export interface IterationHookContext extends HookContext {
|
|
39
|
+
/**
|
|
40
|
+
* Maximum iterations allowed
|
|
41
|
+
*/
|
|
42
|
+
maxIterations: number;
|
|
43
|
+
/**
|
|
44
|
+
* Current message history
|
|
45
|
+
*/
|
|
46
|
+
messages: Message[];
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Hook called before each iteration starts.
|
|
50
|
+
*
|
|
51
|
+
* Can be used for:
|
|
52
|
+
* - Logging iteration boundaries
|
|
53
|
+
* - Custom iteration budget tracking
|
|
54
|
+
* - Early termination checks
|
|
55
|
+
*
|
|
56
|
+
* @returns void, or { skip: true } to skip this iteration
|
|
57
|
+
*/
|
|
58
|
+
export type BeforeIterationHook = (context: IterationHookContext) => undefined | Promise<undefined> | {
|
|
59
|
+
skip: true;
|
|
60
|
+
} | Promise<{
|
|
61
|
+
skip: true;
|
|
62
|
+
}>;
|
|
63
|
+
/**
|
|
64
|
+
* Hook called after each iteration completes.
|
|
65
|
+
*
|
|
66
|
+
* Can be used for:
|
|
67
|
+
* - Logging iteration results
|
|
68
|
+
* - Metrics collection
|
|
69
|
+
* - State snapshots
|
|
70
|
+
*/
|
|
71
|
+
export type AfterIterationHook = (context: IterationHookContext & {
|
|
72
|
+
/**
|
|
73
|
+
* Tool calls made in this iteration
|
|
74
|
+
*/
|
|
75
|
+
toolCalls: Array<{
|
|
76
|
+
name: string;
|
|
77
|
+
input: Record<string, unknown>;
|
|
78
|
+
result: ToolExecutionResult;
|
|
79
|
+
}>;
|
|
80
|
+
/**
|
|
81
|
+
* Whether LLM returned text without tool calls
|
|
82
|
+
*/
|
|
83
|
+
completedWithText: boolean;
|
|
84
|
+
}) => undefined | Promise<undefined>;
|
|
85
|
+
/**
|
|
86
|
+
* Context for LLM hooks
|
|
87
|
+
*/
|
|
88
|
+
export interface LLMHookContext extends HookContext {
|
|
89
|
+
/**
|
|
90
|
+
* Messages to be sent to LLM
|
|
91
|
+
*/
|
|
92
|
+
messages: Message[];
|
|
93
|
+
/**
|
|
94
|
+
* Tool definitions available to LLM
|
|
95
|
+
*/
|
|
96
|
+
tools: ToolDefinition[];
|
|
97
|
+
}
|
|
98
|
+
/**
|
|
99
|
+
* Result from before:llm hook that modifies messages or tools
|
|
100
|
+
*/
|
|
101
|
+
export interface BeforeLLMHookResult {
|
|
102
|
+
/**
|
|
103
|
+
* Modified messages (optional, original used if not provided)
|
|
104
|
+
*/
|
|
105
|
+
messages?: Message[];
|
|
106
|
+
/**
|
|
107
|
+
* Modified tools (optional, original used if not provided)
|
|
108
|
+
*/
|
|
109
|
+
tools?: ToolDefinition[];
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Hook called before LLM call.
|
|
113
|
+
*
|
|
114
|
+
* Can be used for:
|
|
115
|
+
* - Prompt transformation/injection
|
|
116
|
+
* - Tool filtering
|
|
117
|
+
* - Request logging
|
|
118
|
+
* - Caching checks
|
|
119
|
+
*
|
|
120
|
+
* @returns void to proceed unchanged, or modified messages/tools
|
|
121
|
+
*/
|
|
122
|
+
export type BeforeLLMHook = (context: LLMHookContext) => undefined | Promise<undefined> | BeforeLLMHookResult | Promise<BeforeLLMHookResult>;
|
|
123
|
+
/**
|
|
124
|
+
* Context for after:llm hook
|
|
125
|
+
*/
|
|
126
|
+
export interface AfterLLMHookContext extends LLMHookContext {
|
|
127
|
+
/**
|
|
128
|
+
* Text response from LLM
|
|
129
|
+
*/
|
|
130
|
+
text: string;
|
|
131
|
+
/**
|
|
132
|
+
* Tool uses requested by LLM
|
|
133
|
+
*/
|
|
134
|
+
toolUses: Array<{
|
|
135
|
+
id: string;
|
|
136
|
+
name: string;
|
|
137
|
+
input: Record<string, unknown>;
|
|
138
|
+
}>;
|
|
139
|
+
/**
|
|
140
|
+
* Token usage from the call
|
|
141
|
+
*/
|
|
142
|
+
usage?: {
|
|
143
|
+
inputTokens: number;
|
|
144
|
+
outputTokens: number;
|
|
145
|
+
};
|
|
146
|
+
/**
|
|
147
|
+
* Model that was used
|
|
148
|
+
*/
|
|
149
|
+
model?: string;
|
|
150
|
+
/**
|
|
151
|
+
* Duration of the LLM call in milliseconds
|
|
152
|
+
*/
|
|
153
|
+
durationMs: number;
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Hook called after LLM response is received.
|
|
157
|
+
*
|
|
158
|
+
* Can be used for:
|
|
159
|
+
* - Response logging
|
|
160
|
+
* - Token tracking
|
|
161
|
+
* - Response validation
|
|
162
|
+
* - Metrics collection
|
|
163
|
+
*/
|
|
164
|
+
export type AfterLLMHook = (context: AfterLLMHookContext) => undefined | Promise<undefined>;
|
|
165
|
+
/**
|
|
166
|
+
* Context for tool hooks
|
|
167
|
+
*/
|
|
168
|
+
export interface ToolHookContext extends HookContext {
|
|
169
|
+
/**
|
|
170
|
+
* Name of the tool being executed
|
|
171
|
+
*/
|
|
172
|
+
toolName: string;
|
|
173
|
+
/**
|
|
174
|
+
* Input arguments for the tool
|
|
175
|
+
*/
|
|
176
|
+
input: Record<string, unknown>;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* Result from before:tool hook that can skip or modify execution
|
|
180
|
+
*/
|
|
181
|
+
export interface BeforeToolHookResult {
|
|
182
|
+
/**
|
|
183
|
+
* Skip tool execution and use this result instead
|
|
184
|
+
*/
|
|
185
|
+
skip?: boolean;
|
|
186
|
+
/**
|
|
187
|
+
* Custom result to return (when skip is true)
|
|
188
|
+
*/
|
|
189
|
+
result?: ToolExecutionResult;
|
|
190
|
+
/**
|
|
191
|
+
* Modified input (when not skipping)
|
|
192
|
+
*/
|
|
193
|
+
input?: Record<string, unknown>;
|
|
194
|
+
}
|
|
195
|
+
/**
|
|
196
|
+
* Hook called before tool execution (after permissions and guardrails).
|
|
197
|
+
*
|
|
198
|
+
* Can be used for:
|
|
199
|
+
* - Custom validation
|
|
200
|
+
* - Input transformation
|
|
201
|
+
* - Execution mocking for tests
|
|
202
|
+
* - Rate limiting
|
|
203
|
+
*
|
|
204
|
+
* @returns void to proceed, or skip/modify options
|
|
205
|
+
*/
|
|
206
|
+
export type BeforeToolHook = (context: ToolHookContext) => undefined | Promise<undefined> | BeforeToolHookResult | Promise<BeforeToolHookResult>;
|
|
207
|
+
/**
|
|
208
|
+
* Context for after:tool hook
|
|
209
|
+
*/
|
|
210
|
+
export interface AfterToolHookContext extends ToolHookContext {
|
|
211
|
+
/**
|
|
212
|
+
* Result from tool execution
|
|
213
|
+
*/
|
|
214
|
+
result: ToolExecutionResult;
|
|
215
|
+
/**
|
|
216
|
+
* Duration of tool execution in milliseconds
|
|
217
|
+
*/
|
|
218
|
+
durationMs: number;
|
|
219
|
+
}
|
|
220
|
+
/**
|
|
221
|
+
* Result from after:tool hook that can modify the result
|
|
222
|
+
*/
|
|
223
|
+
export interface AfterToolHookResult {
|
|
224
|
+
/**
|
|
225
|
+
* Modified result (optional, original used if not provided)
|
|
226
|
+
*/
|
|
227
|
+
result?: ToolExecutionResult;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Hook called after tool execution.
|
|
231
|
+
*
|
|
232
|
+
* Can be used for:
|
|
233
|
+
* - Result transformation
|
|
234
|
+
* - Logging and metrics
|
|
235
|
+
* - Result validation
|
|
236
|
+
* - Error enrichment
|
|
237
|
+
*/
|
|
238
|
+
export type AfterToolHook = (context: AfterToolHookContext) => undefined | Promise<undefined> | AfterToolHookResult | Promise<AfterToolHookResult>;
|
|
239
|
+
/**
|
|
240
|
+
* Context for error hooks
|
|
241
|
+
*/
|
|
242
|
+
export interface ErrorHookContext extends HookContext {
|
|
243
|
+
/**
|
|
244
|
+
* The error that occurred
|
|
245
|
+
*/
|
|
246
|
+
error: Error;
|
|
247
|
+
/**
|
|
248
|
+
* Phase where the error occurred
|
|
249
|
+
*/
|
|
250
|
+
phase: 'llm' | 'tool' | 'iteration';
|
|
251
|
+
/**
|
|
252
|
+
* Tool name (if error occurred during tool execution)
|
|
253
|
+
*/
|
|
254
|
+
toolName?: string;
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Result from error hook that can recover or transform the error
|
|
258
|
+
*/
|
|
259
|
+
export interface ErrorHookResult {
|
|
260
|
+
/**
|
|
261
|
+
* Whether to suppress the error and continue
|
|
262
|
+
*/
|
|
263
|
+
handled?: boolean;
|
|
264
|
+
/**
|
|
265
|
+
* Transformed error to throw instead
|
|
266
|
+
*/
|
|
267
|
+
error?: Error;
|
|
268
|
+
/**
|
|
269
|
+
* Recovery result (for tool errors, replaces failed result)
|
|
270
|
+
*/
|
|
271
|
+
recovery?: ToolExecutionResult;
|
|
272
|
+
}
|
|
273
|
+
/**
|
|
274
|
+
* Hook called when an error occurs.
|
|
275
|
+
*
|
|
276
|
+
* Can be used for:
|
|
277
|
+
* - Error logging
|
|
278
|
+
* - Error transformation
|
|
279
|
+
* - Recovery strategies
|
|
280
|
+
* - Alerting
|
|
281
|
+
*/
|
|
282
|
+
export type OnErrorHook = (context: ErrorHookContext) => undefined | Promise<undefined> | ErrorHookResult | Promise<ErrorHookResult>;
|
|
283
|
+
/**
|
|
284
|
+
* Configuration for the hooks system
|
|
285
|
+
*/
|
|
286
|
+
export interface HooksConfig {
|
|
287
|
+
/**
|
|
288
|
+
* Hooks called before each iteration
|
|
289
|
+
*/
|
|
290
|
+
beforeIteration?: BeforeIterationHook[];
|
|
291
|
+
/**
|
|
292
|
+
* Hooks called after each iteration
|
|
293
|
+
*/
|
|
294
|
+
afterIteration?: AfterIterationHook[];
|
|
295
|
+
/**
|
|
296
|
+
* Hooks called before LLM calls
|
|
297
|
+
*/
|
|
298
|
+
beforeLLM?: BeforeLLMHook[];
|
|
299
|
+
/**
|
|
300
|
+
* Hooks called after LLM responses
|
|
301
|
+
*/
|
|
302
|
+
afterLLM?: AfterLLMHook[];
|
|
303
|
+
/**
|
|
304
|
+
* Hooks called before tool execution
|
|
305
|
+
*/
|
|
306
|
+
beforeTool?: BeforeToolHook[];
|
|
307
|
+
/**
|
|
308
|
+
* Hooks called after tool execution
|
|
309
|
+
*/
|
|
310
|
+
afterTool?: AfterToolHook[];
|
|
311
|
+
/**
|
|
312
|
+
* Hooks called when errors occur
|
|
313
|
+
*/
|
|
314
|
+
onError?: OnErrorHook[];
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Internal representation of a registered hook
|
|
318
|
+
*/
|
|
319
|
+
export interface RegisteredHook<T> {
|
|
320
|
+
/**
|
|
321
|
+
* Unique ID for this hook registration
|
|
322
|
+
*/
|
|
323
|
+
id: string;
|
|
324
|
+
/**
|
|
325
|
+
* The hook function
|
|
326
|
+
*/
|
|
327
|
+
hook: T;
|
|
328
|
+
/**
|
|
329
|
+
* Optional name for debugging
|
|
330
|
+
*/
|
|
331
|
+
name?: string;
|
|
332
|
+
/**
|
|
333
|
+
* Priority (lower = runs first, default: 0)
|
|
334
|
+
*/
|
|
335
|
+
priority?: number;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Options for hook registration
|
|
339
|
+
*/
|
|
340
|
+
export interface HookRegistrationOptions {
|
|
341
|
+
/**
|
|
342
|
+
* Name for debugging/logging
|
|
343
|
+
*/
|
|
344
|
+
name?: string;
|
|
345
|
+
/**
|
|
346
|
+
* Priority (lower = runs first, default: 0)
|
|
347
|
+
*/
|
|
348
|
+
priority?: number;
|
|
349
|
+
}
|
|
350
|
+
/**
|
|
351
|
+
* Event types for hook operations
|
|
352
|
+
*/
|
|
353
|
+
export type HookEventType = 'hook:registered' | 'hook:unregistered' | 'hook:started' | 'hook:completed' | 'hook:error';
|
|
354
|
+
/**
|
|
355
|
+
* Hook execution event payload
|
|
356
|
+
*/
|
|
357
|
+
export interface HookEvent {
|
|
358
|
+
type: HookEventType;
|
|
359
|
+
hookType: keyof HooksConfig;
|
|
360
|
+
hookId?: string;
|
|
361
|
+
hookName?: string;
|
|
362
|
+
durationMs?: number;
|
|
363
|
+
error?: Error;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* Event handler for hook events
|
|
367
|
+
*/
|
|
368
|
+
export type HookEventHandler = (event: HookEvent) => void;
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hooks System - Lifecycle hooks for agent customization
|
|
3
|
+
*
|
|
4
|
+
* Hooks provide extension points for customizing agent behavior without
|
|
5
|
+
* subclassing. They enable middleware-like patterns for:
|
|
6
|
+
* - Logging and tracing
|
|
7
|
+
* - Input/output transformation
|
|
8
|
+
* - Custom validation
|
|
9
|
+
* - Metrics collection
|
|
10
|
+
* - Integration with external systems
|
|
11
|
+
*/
|
|
12
|
+
export {};
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @compilr-dev/agents
|
|
3
|
+
*
|
|
4
|
+
* Lightweight multi-LLM agent library for building CLI AI assistants
|
|
5
|
+
*/
|
|
6
|
+
export { Agent } from './agent.js';
|
|
7
|
+
export type { AgentConfig, AgentRunResult, AgentEvent, AgentEventHandler, RunOptions, SubAgentConfig, SubAgentResult, StreamWriter, CustomEventConfig, } from './agent.js';
|
|
8
|
+
export type { LLMProvider, Message, MessageRole, ContentBlock, ContentBlockType, TextBlock, ToolUseBlock, ToolResultBlock, StreamChunk, LLMUsage, ChatOptions, ToolDefinition, ToolResult, MockProviderConfig, MockResponse, MockToolCall, } from './providers/index.js';
|
|
9
|
+
export { MockProvider, createMockProvider } from './providers/index.js';
|
|
10
|
+
export { ClaudeProvider, createClaudeProvider } from './providers/index.js';
|
|
11
|
+
export type { ClaudeProviderConfig } from './providers/index.js';
|
|
12
|
+
export type { Tool, ToolHandler, ToolRegistry, ToolInputSchema, ToolExecutionResult, ToolRegistryOptions, DefineToolOptions, ReadFileInput, WriteFileInput, BashInput, BashResult, FifoDetectionResult, GrepInput, GlobInput, EditInput, TodoWriteInput, TodoReadInput, TodoItem, TodoStatus, TodoContextCleanupOptions, TaskInput, TaskResult, AgentTypeConfig, TaskToolOptions, ContextMode, ThoroughnessLevel, SubAgentEventInfo, } from './tools/index.js';
|
|
13
|
+
export { defineTool, createSuccessResult, createErrorResult, wrapToolExecute, DefaultToolRegistry, createToolRegistry, } from './tools/index.js';
|
|
14
|
+
export { readFileTool, createReadFileTool, writeFileTool, createWriteFileTool, bashTool, createBashTool, execStream, detectFifoUsage, bashOutputTool, createBashOutputTool, killShellTool, createKillShellTool, ShellManager, getDefaultShellManager, setDefaultShellManager, grepTool, createGrepTool, globTool, createGlobTool, editTool, createEditTool, todoWriteTool, todoReadTool, createTodoTools, TodoStore, resetDefaultTodoStore, getDefaultTodoStore, createIsolatedTodoStore, cleanupTodoContextMessages, getTodoContextStats, webFetchTool, createWebFetchTool, createTaskTool, defaultAgentTypes, builtinTools, allBuiltinTools, } from './tools/index.js';
|
|
15
|
+
export { userMessage, assistantMessage, systemMessage, textBlock, toolUseBlock, toolResultBlock, getTextContent, getToolUses, getToolResults, hasToolUses, validateToolUseResultPairing, ensureMessageContent, normalizeMessages, } from './messages/index.js';
|
|
16
|
+
export type { ToolPairingValidation } from './messages/index.js';
|
|
17
|
+
export { generateId, sleep, retry, truncate } from './utils/index.js';
|
|
18
|
+
export { AgentError, ProviderError, ToolError, ToolTimeoutError, ToolLoopError, ValidationError, MaxIterationsError, AbortError, ContextOverflowError, isAgentError, isProviderError, isToolError, isToolTimeoutError, isToolLoopError, isContextOverflowError, wrapError, } from './errors.js';
|
|
19
|
+
export { ContextManager, DEFAULT_CONTEXT_CONFIG } from './context/index.js';
|
|
20
|
+
export type { ContextManagerOptions, ContextCategory, BudgetAllocation, CategoryBudgetInfo, PreflightResult, VerbosityLevel, VerbosityConfig, ContextConfig, FilteringConfig, CompactionConfig, SummarizationConfig, CompactionResult, SummarizationResult, FilteringResult, ContextEvent, ContextEventHandler, ContextStats, } from './context/index.js';
|
|
21
|
+
export { SkillRegistry, defineSkill, createSkillRegistry, builtinSkills, getDefaultSkillRegistry, resetDefaultSkillRegistry, } from './skills/index.js';
|
|
22
|
+
export type { Skill, SkillInvocationResult, SkillInvokeOptions } from './skills/index.js';
|
|
23
|
+
export { JsonSerializer, CompactJsonSerializer, defaultSerializer, MemoryCheckpointer, FileCheckpointer, StateError, StateErrorCode, CURRENT_STATE_VERSION, } from './state/index.js';
|
|
24
|
+
export type { AgentState, SessionMetadata, SessionInfo, StateSerializer, Checkpointer, CheckpointerWithPending, PendingWrite, ListSessionsOptions, ResumeOptions, FromStateOptions, FileCheckpointerOptions, } from './state/index.js';
|
|
25
|
+
export { AnchorManager, getDefaultAnchors, isBuiltinAnchor, getBuiltinAnchorIds, DEFAULT_SAFETY_ANCHORS, } from './anchors/index.js';
|
|
26
|
+
export type { Anchor, AnchorInput, AnchorPriority, AnchorScope, AnchorQueryOptions, AnchorClearOptions, AnchorManagerOptions, AnchorEventType, AnchorEvent, AnchorEventHandler, SerializedAnchor, } from './anchors/index.js';
|
|
27
|
+
export { GuardrailManager, getBuiltinGuardrails, isBuiltinGuardrail, getBuiltinGuardrailIds, getGuardrailsByTag, BUILTIN_GUARDRAILS, } from './guardrails/index.js';
|
|
28
|
+
export type { Guardrail, GuardrailInput, GuardrailAction, GuardrailResult, GuardrailContext, GuardrailManagerOptions, GuardrailTriggeredHandler, GuardrailEventType, GuardrailEvent, GuardrailEventHandler, } from './guardrails/index.js';
|
|
29
|
+
export { MCPClient, MCPManager, mcpToolToTool, mcpToolsToTools, convertMCPResult, contentBlocksToString, generateToolName, normalizeServerConfig, MCPError, MCPErrorCode, isMCPError, createSDKNotInstalledError, } from './mcp/index.js';
|
|
30
|
+
export type { MCPTransport, MCPConnectionStatus, MCPStdioOptions, MCPHttpOptions, MCPClientConfig, MCPServerConfig, MCPToolDefinition, MCPContentBlock, MCPToolResult, MCPClientEventType, MCPClientEvent, MCPClientEventHandler, MCPManagerOptions, MCPToolConversionOptions, } from './mcp/index.js';
|
|
31
|
+
export { PermissionManager } from './permissions/index.js';
|
|
32
|
+
export type { PermissionLevel, ToolPermission, PermissionRequest, PermissionCheckResult, PermissionHandler, PreviewGenerator, PermissionManagerOptions, PermissionEventType, PermissionEvent, PermissionEventHandler, } from './permissions/index.js';
|
|
33
|
+
export { ProjectMemoryLoader, createProjectMemoryLoader, loadProjectMemory, hasProjectMemory, getProviderPatterns, getSupportedProviders, getGenericPatterns, PROVIDER_PATTERNS, GENERIC_PATTERNS, } from './memory/index.js';
|
|
34
|
+
export type { LLMProviderName, MemoryFile, ProjectMemory, FilePattern, CombineStrategy, ProjectMemoryOptions, ProjectMemoryEventType, ProjectMemoryEvent, ProjectMemoryEventHandler, MemoryDiscoveryResult, ProviderPatterns, } from './memory/index.js';
|
|
35
|
+
export { UsageTracker, createUsageTracker } from './costs/index.js';
|
|
36
|
+
export type { TokenUsage, UsageRecord, UsageStats, TokenBudgetConfig, BudgetStatus, UsageEventType, UsageEvent, UsageEventHandler, UsageTrackerOptions, RecordUsageInput, } from './costs/index.js';
|
|
37
|
+
export { HooksManager } from './hooks/index.js';
|
|
38
|
+
export type { HooksManagerOptions } from './hooks/index.js';
|
|
39
|
+
export type { HooksConfig, HookRegistrationOptions, HookContext, IterationHookContext, LLMHookContext, AfterLLMHookContext, ToolHookContext, AfterToolHookContext, ErrorHookContext, BeforeIterationHook, AfterIterationHook, BeforeLLMHook, AfterLLMHook, BeforeToolHook, AfterToolHook, OnErrorHook, BeforeLLMHookResult, BeforeToolHookResult, AfterToolHookResult, ErrorHookResult, RegisteredHook, HookEventType, HookEvent, HookEventHandler, } from './hooks/index.js';
|
|
40
|
+
export { TracingManager, createTracingHooks, createLoggingHooks, mergeHooks, createStructuredLogger, createNoopLogger, createBufferedLogger, createTracingLogger, formatDuration, formatBytes, redactSensitive, createOTelExporter, createConsoleExporter, createBatchExporter, createMultiExporter, OTelNotInstalledError, isOTelNotInstalledError, SemanticAttributes, } from './tracing/index.js';
|
|
41
|
+
export type { Span, SpanStatus, SpanKind, SpanAttributes, SpanContext, SpanEvent, AttributeValue, Trace, TracingManagerOptions, StartSpanOptions, EndSpanOptions, TracingHooksConfig, TracingEvent, TracingEventHandler, OTelExporter, OTelSDK, OTelTracer, OTelSpan, LogLevel, LogEntry, StructuredLogger, StructuredLoggerOptions, TracingHookContext, TracingManagerInterface, } from './tracing/index.js';
|
|
42
|
+
export { TokenBucketRateLimiter, createRateLimiter, createNoopRateLimiter, RateLimitExceededError, isRateLimitExceededError, withRetry, createRetryWithRateLimit, RetryPresets, RetryStats, RateLimitedProvider, wrapWithRateLimit, ProviderRateLimits, } from './rate-limit/index.js';
|
|
43
|
+
export type { RateLimiterConfig, RateLimiterStats, RetryConfig, RateLimitRetryConfig, AcquireResult, RateLimiter, } from './rate-limit/index.js';
|
|
44
|
+
export { RehearsalManager, createRehearsalManager, GitRehearsalAnalyzer, createGitAnalyzer, FileRehearsalAnalyzer, createFileAnalyzer, } from './rehearsal/index.js';
|
|
45
|
+
export type { ImpactSeverity, RehearsalRecommendation, OperationCategory, AffectedFile, RehearsalImpact, RehearsalResult, RehearsalContext, RehearsalAnalyzer, RehearsalManagerOptions, RehearsalEventType, RehearsalEvent, RehearsalEventHandler, } from './rehearsal/index.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @compilr-dev/agents
|
|
3
|
+
*
|
|
4
|
+
* Lightweight multi-LLM agent library for building CLI AI assistants
|
|
5
|
+
*/
|
|
6
|
+
// Core agent
|
|
7
|
+
export { Agent } from './agent.js';
|
|
8
|
+
// Providers
|
|
9
|
+
export { MockProvider, createMockProvider } from './providers/index.js';
|
|
10
|
+
export { ClaudeProvider, createClaudeProvider } from './providers/index.js';
|
|
11
|
+
// Tool utilities
|
|
12
|
+
export { defineTool, createSuccessResult, createErrorResult, wrapToolExecute, DefaultToolRegistry, createToolRegistry, } from './tools/index.js';
|
|
13
|
+
// Built-in tools
|
|
14
|
+
export { readFileTool, createReadFileTool, writeFileTool, createWriteFileTool, bashTool, createBashTool, execStream, detectFifoUsage, bashOutputTool, createBashOutputTool, killShellTool, createKillShellTool, ShellManager, getDefaultShellManager, setDefaultShellManager, grepTool, createGrepTool, globTool, createGlobTool, editTool, createEditTool, todoWriteTool, todoReadTool, createTodoTools, TodoStore, resetDefaultTodoStore, getDefaultTodoStore, createIsolatedTodoStore, cleanupTodoContextMessages, getTodoContextStats, webFetchTool, createWebFetchTool,
|
|
15
|
+
// Task tool (sub-agent spawning)
|
|
16
|
+
createTaskTool, defaultAgentTypes, builtinTools, allBuiltinTools, } from './tools/index.js';
|
|
17
|
+
// Message utilities
|
|
18
|
+
export { userMessage, assistantMessage, systemMessage, textBlock, toolUseBlock, toolResultBlock, getTextContent, getToolUses, getToolResults, hasToolUses, validateToolUseResultPairing, ensureMessageContent, normalizeMessages, } from './messages/index.js';
|
|
19
|
+
// Utilities
|
|
20
|
+
export { generateId, sleep, retry, truncate } from './utils/index.js';
|
|
21
|
+
// Errors
|
|
22
|
+
export { AgentError, ProviderError, ToolError, ToolTimeoutError, ToolLoopError, ValidationError, MaxIterationsError, AbortError, ContextOverflowError, isAgentError, isProviderError, isToolError, isToolTimeoutError, isToolLoopError, isContextOverflowError, wrapError, } from './errors.js';
|
|
23
|
+
// Context management
|
|
24
|
+
export { ContextManager, DEFAULT_CONTEXT_CONFIG } from './context/index.js';
|
|
25
|
+
// Skills system
|
|
26
|
+
export { SkillRegistry, defineSkill, createSkillRegistry, builtinSkills, getDefaultSkillRegistry, resetDefaultSkillRegistry, } from './skills/index.js';
|
|
27
|
+
// State management
|
|
28
|
+
export {
|
|
29
|
+
// Serializers
|
|
30
|
+
JsonSerializer, CompactJsonSerializer, defaultSerializer,
|
|
31
|
+
// Checkpointers
|
|
32
|
+
MemoryCheckpointer, FileCheckpointer,
|
|
33
|
+
// Errors
|
|
34
|
+
StateError, StateErrorCode,
|
|
35
|
+
// Constants
|
|
36
|
+
CURRENT_STATE_VERSION, } from './state/index.js';
|
|
37
|
+
// Anchors - Critical information that survives context compaction
|
|
38
|
+
export { AnchorManager, getDefaultAnchors, isBuiltinAnchor, getBuiltinAnchorIds, DEFAULT_SAFETY_ANCHORS, } from './anchors/index.js';
|
|
39
|
+
// Guardrails - Pattern-based safety checks for tool execution
|
|
40
|
+
export { GuardrailManager, getBuiltinGuardrails, isBuiltinGuardrail, getBuiltinGuardrailIds, getGuardrailsByTag, BUILTIN_GUARDRAILS, } from './guardrails/index.js';
|
|
41
|
+
// MCP (Model Context Protocol) support
|
|
42
|
+
// Note: Requires optional peer dependency @modelcontextprotocol/sdk
|
|
43
|
+
export { MCPClient, MCPManager, mcpToolToTool, mcpToolsToTools, convertMCPResult, contentBlocksToString, generateToolName, normalizeServerConfig, MCPError, MCPErrorCode, isMCPError, createSDKNotInstalledError, } from './mcp/index.js';
|
|
44
|
+
// Permissions - Tool-level permission management
|
|
45
|
+
export { PermissionManager } from './permissions/index.js';
|
|
46
|
+
// Memory - Project-level instruction loading (CLAUDE.md, GEMINI.md, etc.)
|
|
47
|
+
export { ProjectMemoryLoader, createProjectMemoryLoader, loadProjectMemory, hasProjectMemory, getProviderPatterns, getSupportedProviders, getGenericPatterns, PROVIDER_PATTERNS, GENERIC_PATTERNS, } from './memory/index.js';
|
|
48
|
+
// Usage tracking - Token usage tracking (no dollar costs)
|
|
49
|
+
export { UsageTracker, createUsageTracker } from './costs/index.js';
|
|
50
|
+
// Hooks - Lifecycle hooks for agent customization
|
|
51
|
+
export { HooksManager } from './hooks/index.js';
|
|
52
|
+
// Tracing - Observability and distributed tracing
|
|
53
|
+
export {
|
|
54
|
+
// Manager
|
|
55
|
+
TracingManager,
|
|
56
|
+
// Built-in hooks
|
|
57
|
+
createTracingHooks, createLoggingHooks, mergeHooks,
|
|
58
|
+
// Structured logging
|
|
59
|
+
createStructuredLogger, createNoopLogger, createBufferedLogger, createTracingLogger, formatDuration, formatBytes, redactSensitive,
|
|
60
|
+
// OpenTelemetry integration
|
|
61
|
+
createOTelExporter, createConsoleExporter, createBatchExporter, createMultiExporter, OTelNotInstalledError, isOTelNotInstalledError,
|
|
62
|
+
// Semantic conventions
|
|
63
|
+
SemanticAttributes, } from './tracing/index.js';
|
|
64
|
+
// Rate Limiting & Retry
|
|
65
|
+
export {
|
|
66
|
+
// Rate Limiter
|
|
67
|
+
TokenBucketRateLimiter, createRateLimiter, createNoopRateLimiter, RateLimitExceededError, isRateLimitExceededError,
|
|
68
|
+
// Retry
|
|
69
|
+
withRetry, createRetryWithRateLimit, RetryPresets, RetryStats,
|
|
70
|
+
// Provider Wrapper
|
|
71
|
+
RateLimitedProvider, wrapWithRateLimit, ProviderRateLimits, } from './rate-limit/index.js';
|
|
72
|
+
// Rehearsal - Impact analysis for destructive operations
|
|
73
|
+
export { RehearsalManager, createRehearsalManager, GitRehearsalAnalyzer, createGitAnalyzer, FileRehearsalAnalyzer, createFileAnalyzer, } from './rehearsal/index.js';
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* MCP Client - Connects to MCP servers and discovers tools
|
|
3
|
+
*
|
|
4
|
+
* Uses the official @modelcontextprotocol/sdk as an optional peer dependency.
|
|
5
|
+
* If the SDK is not installed, attempting to use MCP features will throw
|
|
6
|
+
* a helpful error with installation instructions.
|
|
7
|
+
*/
|
|
8
|
+
import type { MCPClientConfig, MCPConnectionStatus, MCPToolDefinition, MCPToolResult, MCPClientEventHandler } from './types.js';
|
|
9
|
+
/**
|
|
10
|
+
* MCP Client for connecting to a single MCP server
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```typescript
|
|
14
|
+
* const client = new MCPClient({
|
|
15
|
+
* name: 'filesystem',
|
|
16
|
+
* transport: 'stdio',
|
|
17
|
+
* stdio: {
|
|
18
|
+
* command: 'npx',
|
|
19
|
+
* args: ['-y', '@modelcontextprotocol/server-filesystem', '/tmp'],
|
|
20
|
+
* },
|
|
21
|
+
* });
|
|
22
|
+
*
|
|
23
|
+
* await client.connect();
|
|
24
|
+
* const tools = await client.listTools();
|
|
25
|
+
* const result = await client.callTool('read_file', { path: '/tmp/test.txt' });
|
|
26
|
+
* await client.disconnect();
|
|
27
|
+
* ```
|
|
28
|
+
*/
|
|
29
|
+
export declare class MCPClient {
|
|
30
|
+
readonly name: string;
|
|
31
|
+
private readonly config;
|
|
32
|
+
private client;
|
|
33
|
+
private transport;
|
|
34
|
+
private _status;
|
|
35
|
+
private readonly eventHandlers;
|
|
36
|
+
private cachedTools;
|
|
37
|
+
constructor(config: MCPClientConfig);
|
|
38
|
+
/**
|
|
39
|
+
* Current connection status
|
|
40
|
+
*/
|
|
41
|
+
get status(): MCPConnectionStatus;
|
|
42
|
+
/**
|
|
43
|
+
* Whether the client is connected
|
|
44
|
+
*/
|
|
45
|
+
get isConnected(): boolean;
|
|
46
|
+
/**
|
|
47
|
+
* Connect to the MCP server
|
|
48
|
+
* @throws MCPError if connection fails
|
|
49
|
+
*/
|
|
50
|
+
connect(): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Disconnect from the MCP server
|
|
53
|
+
*/
|
|
54
|
+
disconnect(): Promise<void>;
|
|
55
|
+
/**
|
|
56
|
+
* List available tools from the server
|
|
57
|
+
* @throws MCPError if not connected or listing fails
|
|
58
|
+
*/
|
|
59
|
+
listTools(): Promise<MCPToolDefinition[]>;
|
|
60
|
+
/**
|
|
61
|
+
* Call a tool on the server
|
|
62
|
+
* @param toolName Name of the tool to call
|
|
63
|
+
* @param args Arguments to pass to the tool
|
|
64
|
+
* @throws MCPError if not connected, tool not found, or execution fails
|
|
65
|
+
*/
|
|
66
|
+
callTool(toolName: string, args?: Record<string, unknown>): Promise<MCPToolResult>;
|
|
67
|
+
/**
|
|
68
|
+
* Register an event handler
|
|
69
|
+
*/
|
|
70
|
+
onEvent(handler: MCPClientEventHandler): () => void;
|
|
71
|
+
/**
|
|
72
|
+
* Convenience method to listen for tools changed events
|
|
73
|
+
*/
|
|
74
|
+
onToolsChanged(callback: (tools: MCPToolDefinition[]) => void): () => void;
|
|
75
|
+
/**
|
|
76
|
+
* Convenience method to listen for errors
|
|
77
|
+
*/
|
|
78
|
+
onError(callback: (error: Error) => void): () => void;
|
|
79
|
+
/**
|
|
80
|
+
* Convenience method to listen for disconnect
|
|
81
|
+
*/
|
|
82
|
+
onDisconnect(callback: () => void): () => void;
|
|
83
|
+
/**
|
|
84
|
+
* Emit an event to all handlers
|
|
85
|
+
*/
|
|
86
|
+
private emit;
|
|
87
|
+
/**
|
|
88
|
+
* Get the connected client, throwing if not connected
|
|
89
|
+
* @throws MCPError if not connected
|
|
90
|
+
* @returns The connected SDK client
|
|
91
|
+
*/
|
|
92
|
+
private getConnectedClient;
|
|
93
|
+
}
|