@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.
Files changed (160) hide show
  1. package/README.md +1277 -0
  2. package/dist/agent.d.ts +1272 -0
  3. package/dist/agent.js +1912 -0
  4. package/dist/anchors/builtin.d.ts +24 -0
  5. package/dist/anchors/builtin.js +61 -0
  6. package/dist/anchors/index.d.ts +6 -0
  7. package/dist/anchors/index.js +5 -0
  8. package/dist/anchors/manager.d.ts +115 -0
  9. package/dist/anchors/manager.js +412 -0
  10. package/dist/anchors/types.d.ts +168 -0
  11. package/dist/anchors/types.js +10 -0
  12. package/dist/context/index.d.ts +12 -0
  13. package/dist/context/index.js +10 -0
  14. package/dist/context/manager.d.ts +224 -0
  15. package/dist/context/manager.js +770 -0
  16. package/dist/context/types.d.ts +377 -0
  17. package/dist/context/types.js +7 -0
  18. package/dist/costs/index.d.ts +8 -0
  19. package/dist/costs/index.js +7 -0
  20. package/dist/costs/tracker.d.ts +121 -0
  21. package/dist/costs/tracker.js +295 -0
  22. package/dist/costs/types.d.ts +157 -0
  23. package/dist/costs/types.js +8 -0
  24. package/dist/errors.d.ts +178 -0
  25. package/dist/errors.js +249 -0
  26. package/dist/guardrails/builtin.d.ts +27 -0
  27. package/dist/guardrails/builtin.js +223 -0
  28. package/dist/guardrails/index.d.ts +6 -0
  29. package/dist/guardrails/index.js +5 -0
  30. package/dist/guardrails/manager.d.ts +117 -0
  31. package/dist/guardrails/manager.js +288 -0
  32. package/dist/guardrails/types.d.ts +159 -0
  33. package/dist/guardrails/types.js +7 -0
  34. package/dist/hooks/index.d.ts +31 -0
  35. package/dist/hooks/index.js +29 -0
  36. package/dist/hooks/manager.d.ts +147 -0
  37. package/dist/hooks/manager.js +600 -0
  38. package/dist/hooks/types.d.ts +368 -0
  39. package/dist/hooks/types.js +12 -0
  40. package/dist/index.d.ts +45 -0
  41. package/dist/index.js +73 -0
  42. package/dist/mcp/client.d.ts +93 -0
  43. package/dist/mcp/client.js +287 -0
  44. package/dist/mcp/errors.d.ts +60 -0
  45. package/dist/mcp/errors.js +78 -0
  46. package/dist/mcp/index.d.ts +43 -0
  47. package/dist/mcp/index.js +45 -0
  48. package/dist/mcp/manager.d.ts +120 -0
  49. package/dist/mcp/manager.js +276 -0
  50. package/dist/mcp/tools.d.ts +54 -0
  51. package/dist/mcp/tools.js +99 -0
  52. package/dist/mcp/types.d.ts +150 -0
  53. package/dist/mcp/types.js +40 -0
  54. package/dist/memory/index.d.ts +8 -0
  55. package/dist/memory/index.js +7 -0
  56. package/dist/memory/loader.d.ts +114 -0
  57. package/dist/memory/loader.js +463 -0
  58. package/dist/memory/types.d.ts +182 -0
  59. package/dist/memory/types.js +8 -0
  60. package/dist/messages/index.d.ts +82 -0
  61. package/dist/messages/index.js +155 -0
  62. package/dist/permissions/index.d.ts +5 -0
  63. package/dist/permissions/index.js +4 -0
  64. package/dist/permissions/manager.d.ts +125 -0
  65. package/dist/permissions/manager.js +379 -0
  66. package/dist/permissions/types.d.ts +162 -0
  67. package/dist/permissions/types.js +7 -0
  68. package/dist/providers/claude.d.ts +90 -0
  69. package/dist/providers/claude.js +348 -0
  70. package/dist/providers/index.d.ts +8 -0
  71. package/dist/providers/index.js +11 -0
  72. package/dist/providers/mock.d.ts +133 -0
  73. package/dist/providers/mock.js +204 -0
  74. package/dist/providers/types.d.ts +168 -0
  75. package/dist/providers/types.js +4 -0
  76. package/dist/rate-limit/index.d.ts +45 -0
  77. package/dist/rate-limit/index.js +47 -0
  78. package/dist/rate-limit/limiter.d.ts +104 -0
  79. package/dist/rate-limit/limiter.js +326 -0
  80. package/dist/rate-limit/provider-wrapper.d.ts +112 -0
  81. package/dist/rate-limit/provider-wrapper.js +201 -0
  82. package/dist/rate-limit/retry.d.ts +108 -0
  83. package/dist/rate-limit/retry.js +287 -0
  84. package/dist/rate-limit/types.d.ts +181 -0
  85. package/dist/rate-limit/types.js +22 -0
  86. package/dist/rehearsal/file-analyzer.d.ts +22 -0
  87. package/dist/rehearsal/file-analyzer.js +351 -0
  88. package/dist/rehearsal/git-analyzer.d.ts +22 -0
  89. package/dist/rehearsal/git-analyzer.js +472 -0
  90. package/dist/rehearsal/index.d.ts +35 -0
  91. package/dist/rehearsal/index.js +36 -0
  92. package/dist/rehearsal/manager.d.ts +100 -0
  93. package/dist/rehearsal/manager.js +290 -0
  94. package/dist/rehearsal/types.d.ts +235 -0
  95. package/dist/rehearsal/types.js +8 -0
  96. package/dist/skills/index.d.ts +160 -0
  97. package/dist/skills/index.js +282 -0
  98. package/dist/state/agent-state.d.ts +41 -0
  99. package/dist/state/agent-state.js +88 -0
  100. package/dist/state/checkpointer.d.ts +110 -0
  101. package/dist/state/checkpointer.js +362 -0
  102. package/dist/state/errors.d.ts +66 -0
  103. package/dist/state/errors.js +88 -0
  104. package/dist/state/index.d.ts +35 -0
  105. package/dist/state/index.js +37 -0
  106. package/dist/state/serializer.d.ts +55 -0
  107. package/dist/state/serializer.js +172 -0
  108. package/dist/state/types.d.ts +312 -0
  109. package/dist/state/types.js +14 -0
  110. package/dist/tools/builtin/bash-output.d.ts +61 -0
  111. package/dist/tools/builtin/bash-output.js +90 -0
  112. package/dist/tools/builtin/bash.d.ts +150 -0
  113. package/dist/tools/builtin/bash.js +354 -0
  114. package/dist/tools/builtin/edit.d.ts +50 -0
  115. package/dist/tools/builtin/edit.js +215 -0
  116. package/dist/tools/builtin/glob.d.ts +62 -0
  117. package/dist/tools/builtin/glob.js +244 -0
  118. package/dist/tools/builtin/grep.d.ts +74 -0
  119. package/dist/tools/builtin/grep.js +363 -0
  120. package/dist/tools/builtin/index.d.ts +44 -0
  121. package/dist/tools/builtin/index.js +69 -0
  122. package/dist/tools/builtin/kill-shell.d.ts +44 -0
  123. package/dist/tools/builtin/kill-shell.js +80 -0
  124. package/dist/tools/builtin/read-file.d.ts +57 -0
  125. package/dist/tools/builtin/read-file.js +184 -0
  126. package/dist/tools/builtin/shell-manager.d.ts +176 -0
  127. package/dist/tools/builtin/shell-manager.js +337 -0
  128. package/dist/tools/builtin/task.d.ts +202 -0
  129. package/dist/tools/builtin/task.js +350 -0
  130. package/dist/tools/builtin/todo.d.ts +207 -0
  131. package/dist/tools/builtin/todo.js +453 -0
  132. package/dist/tools/builtin/utils.d.ts +27 -0
  133. package/dist/tools/builtin/utils.js +70 -0
  134. package/dist/tools/builtin/web-fetch.d.ts +96 -0
  135. package/dist/tools/builtin/web-fetch.js +290 -0
  136. package/dist/tools/builtin/write-file.d.ts +54 -0
  137. package/dist/tools/builtin/write-file.js +147 -0
  138. package/dist/tools/define.d.ts +60 -0
  139. package/dist/tools/define.js +65 -0
  140. package/dist/tools/index.d.ts +10 -0
  141. package/dist/tools/index.js +37 -0
  142. package/dist/tools/registry.d.ts +79 -0
  143. package/dist/tools/registry.js +151 -0
  144. package/dist/tools/types.d.ts +59 -0
  145. package/dist/tools/types.js +4 -0
  146. package/dist/tracing/hooks.d.ts +58 -0
  147. package/dist/tracing/hooks.js +377 -0
  148. package/dist/tracing/index.d.ts +51 -0
  149. package/dist/tracing/index.js +55 -0
  150. package/dist/tracing/logging.d.ts +78 -0
  151. package/dist/tracing/logging.js +310 -0
  152. package/dist/tracing/manager.d.ts +160 -0
  153. package/dist/tracing/manager.js +468 -0
  154. package/dist/tracing/otel.d.ts +102 -0
  155. package/dist/tracing/otel.js +246 -0
  156. package/dist/tracing/types.d.ts +346 -0
  157. package/dist/tracing/types.js +38 -0
  158. package/dist/utils/index.d.ts +23 -0
  159. package/dist/utils/index.js +44 -0
  160. package/package.json +79 -0
@@ -0,0 +1,288 @@
1
+ /**
2
+ * GuardrailManager - Pattern-based safety checks for tool execution
3
+ */
4
+ import { getBuiltinGuardrails } from './builtin.js';
5
+ /**
6
+ * Default options for GuardrailManager
7
+ */
8
+ const DEFAULT_OPTIONS = {
9
+ enabled: true,
10
+ includeDefaults: true,
11
+ };
12
+ /**
13
+ * GuardrailManager - Manages pattern-based safety checks for tool execution
14
+ *
15
+ * @example
16
+ * ```typescript
17
+ * const manager = new GuardrailManager({
18
+ * enabled: true,
19
+ * includeDefaults: true,
20
+ * custom: [
21
+ * {
22
+ * id: 'no-prod-db',
23
+ * name: 'Production Database Protection',
24
+ * description: 'Prevent operations on production database',
25
+ * patterns: [/prod.*db/i, /production.*database/i],
26
+ * action: 'block',
27
+ * message: 'Operations on production database are blocked',
28
+ * }
29
+ * ],
30
+ * onTriggered: async (result, context) => {
31
+ * if (result.action === 'confirm') {
32
+ * return await askUserConfirmation(result.guardrail.message);
33
+ * }
34
+ * return result.action !== 'block';
35
+ * }
36
+ * });
37
+ *
38
+ * // Check before tool execution
39
+ * const result = manager.check('bash', { command: 'rm -rf /' });
40
+ * if (result.triggered && result.action === 'block') {
41
+ * throw new Error(result.guardrail.message);
42
+ * }
43
+ * ```
44
+ */
45
+ export class GuardrailManager {
46
+ guardrails = new Map();
47
+ options;
48
+ eventHandler;
49
+ constructor(options = {}) {
50
+ this.options = {
51
+ ...DEFAULT_OPTIONS,
52
+ ...options,
53
+ };
54
+ // Add built-in guardrails
55
+ if (this.options.includeDefaults) {
56
+ for (const guardrail of getBuiltinGuardrails()) {
57
+ this.guardrails.set(guardrail.id, guardrail);
58
+ }
59
+ }
60
+ // Add custom guardrails
61
+ if (options.custom) {
62
+ for (const input of options.custom) {
63
+ this.add(input);
64
+ }
65
+ }
66
+ }
67
+ /**
68
+ * Set the event handler for guardrail events
69
+ */
70
+ onEvent(handler) {
71
+ this.eventHandler = handler;
72
+ }
73
+ /**
74
+ * Add a custom guardrail
75
+ */
76
+ add(input) {
77
+ const guardrail = {
78
+ ...input,
79
+ enabled: input.enabled ?? true,
80
+ };
81
+ this.guardrails.set(guardrail.id, guardrail);
82
+ return guardrail;
83
+ }
84
+ /**
85
+ * Get a guardrail by ID
86
+ */
87
+ get(id) {
88
+ return this.guardrails.get(id);
89
+ }
90
+ /**
91
+ * Get all guardrails
92
+ */
93
+ getAll() {
94
+ return Array.from(this.guardrails.values());
95
+ }
96
+ /**
97
+ * Get guardrails that apply to a specific tool
98
+ */
99
+ getForTool(toolName) {
100
+ return this.getAll().filter((g) => {
101
+ // enabled defaults to true if not set
102
+ if (g.enabled === false)
103
+ return false;
104
+ if (!g.scope || g.scope.length === 0)
105
+ return true;
106
+ return g.scope.includes(toolName);
107
+ });
108
+ }
109
+ /**
110
+ * Check if a guardrail exists
111
+ */
112
+ has(id) {
113
+ return this.guardrails.has(id);
114
+ }
115
+ /**
116
+ * Remove a guardrail by ID
117
+ */
118
+ remove(id) {
119
+ return this.guardrails.delete(id);
120
+ }
121
+ /**
122
+ * Enable a guardrail
123
+ */
124
+ enable(id) {
125
+ const guardrail = this.guardrails.get(id);
126
+ if (!guardrail)
127
+ return false;
128
+ guardrail.enabled = true;
129
+ return true;
130
+ }
131
+ /**
132
+ * Disable a guardrail
133
+ */
134
+ disable(id) {
135
+ const guardrail = this.guardrails.get(id);
136
+ if (!guardrail)
137
+ return false;
138
+ guardrail.enabled = false;
139
+ return true;
140
+ }
141
+ /**
142
+ * Get the number of guardrails
143
+ */
144
+ get size() {
145
+ return this.guardrails.size;
146
+ }
147
+ /**
148
+ * Check if guardrails are enabled
149
+ */
150
+ get enabled() {
151
+ return this.options.enabled;
152
+ }
153
+ /**
154
+ * Enable or disable all guardrails
155
+ */
156
+ setEnabled(enabled) {
157
+ this.options.enabled = enabled;
158
+ }
159
+ /**
160
+ * Check tool input against all applicable guardrails
161
+ *
162
+ * @param toolName - Name of the tool being called
163
+ * @param input - Tool input to check
164
+ * @returns GuardrailResult indicating if any guardrail was triggered
165
+ */
166
+ check(toolName, input) {
167
+ // If guardrails are disabled, allow everything
168
+ if (!this.options.enabled) {
169
+ return { triggered: false };
170
+ }
171
+ // Stringify the input for pattern matching
172
+ const inputString = this.stringifyInput(input);
173
+ // Get guardrails that apply to this tool
174
+ const applicableGuardrails = this.getForTool(toolName);
175
+ // Check each guardrail
176
+ for (const guardrail of applicableGuardrails) {
177
+ for (const pattern of guardrail.patterns) {
178
+ const match = inputString.match(pattern);
179
+ if (match) {
180
+ return {
181
+ triggered: true,
182
+ guardrail,
183
+ match: match[0],
184
+ action: guardrail.action,
185
+ toolName,
186
+ input,
187
+ };
188
+ }
189
+ }
190
+ }
191
+ return { triggered: false };
192
+ }
193
+ /**
194
+ * Check and handle guardrail triggering
195
+ *
196
+ * This method checks the input and calls the onTriggered handler if configured.
197
+ * Returns true if execution should proceed, false if blocked.
198
+ *
199
+ * @param toolName - Name of the tool being called
200
+ * @param input - Tool input to check
201
+ * @returns Promise<{ proceed: boolean; result: GuardrailResult }>
202
+ */
203
+ async checkAndHandle(toolName, input) {
204
+ const result = this.check(toolName, input);
205
+ if (!result.triggered) {
206
+ return { proceed: true, result };
207
+ }
208
+ const context = {
209
+ toolName,
210
+ input,
211
+ inputString: this.stringifyInput(input),
212
+ };
213
+ // Emit event
214
+ this.eventHandler?.({
215
+ type: 'guardrail:triggered',
216
+ result,
217
+ context,
218
+ });
219
+ // Handle based on action
220
+ switch (result.action) {
221
+ case 'block':
222
+ this.eventHandler?.({
223
+ type: 'guardrail:blocked',
224
+ result,
225
+ context,
226
+ });
227
+ return { proceed: false, result };
228
+ case 'confirm':
229
+ if (this.options.onTriggered) {
230
+ const confirmed = await this.options.onTriggered(result, context);
231
+ if (confirmed) {
232
+ this.eventHandler?.({
233
+ type: 'guardrail:confirmed',
234
+ result,
235
+ context,
236
+ });
237
+ return { proceed: true, result };
238
+ }
239
+ else {
240
+ this.eventHandler?.({
241
+ type: 'guardrail:cancelled',
242
+ result,
243
+ context,
244
+ });
245
+ return { proceed: false, result };
246
+ }
247
+ }
248
+ // No handler configured - block by default for confirm actions
249
+ return { proceed: false, result };
250
+ case 'warn':
251
+ this.eventHandler?.({
252
+ type: 'guardrail:warning',
253
+ result,
254
+ context,
255
+ });
256
+ return { proceed: true, result };
257
+ default:
258
+ return { proceed: true, result };
259
+ }
260
+ }
261
+ /**
262
+ * Stringify input for pattern matching
263
+ */
264
+ stringifyInput(input) {
265
+ if (typeof input === 'string') {
266
+ return input;
267
+ }
268
+ if (input === null || input === undefined) {
269
+ return '';
270
+ }
271
+ // For objects, extract common command fields
272
+ if (typeof input === 'object') {
273
+ const obj = input;
274
+ // Common command field names
275
+ const commandFields = ['command', 'cmd', 'script', 'query', 'sql', 'content'];
276
+ for (const field of commandFields) {
277
+ const value = obj[field];
278
+ if (typeof value === 'string') {
279
+ return value;
280
+ }
281
+ }
282
+ // Fallback to JSON
283
+ return JSON.stringify(input);
284
+ }
285
+ // For primitives (number, boolean, etc.)
286
+ return typeof input === 'string' ? input : JSON.stringify(input);
287
+ }
288
+ }
@@ -0,0 +1,159 @@
1
+ /**
2
+ * Guardrails - Pattern-based detection of risky operations
3
+ *
4
+ * Guardrails scan tool inputs/commands before execution, match against
5
+ * known dangerous patterns, and emit warnings or block execution.
6
+ */
7
+ /**
8
+ * Action to take when a guardrail is triggered
9
+ * - warn: Log warning but proceed with execution
10
+ * - confirm: Require confirmation before proceeding
11
+ * - block: Prevent execution entirely
12
+ */
13
+ export type GuardrailAction = 'warn' | 'confirm' | 'block';
14
+ /**
15
+ * A guardrail definition
16
+ */
17
+ export interface Guardrail {
18
+ /**
19
+ * Unique identifier for this guardrail
20
+ */
21
+ id: string;
22
+ /**
23
+ * Human-readable name
24
+ */
25
+ name: string;
26
+ /**
27
+ * Description of what this guardrail protects against
28
+ */
29
+ description: string;
30
+ /**
31
+ * Patterns to match against tool inputs
32
+ */
33
+ patterns: RegExp[];
34
+ /**
35
+ * Action to take when triggered
36
+ */
37
+ action: GuardrailAction;
38
+ /**
39
+ * Message to display when triggered
40
+ */
41
+ message: string;
42
+ /**
43
+ * Which tools this guardrail applies to (empty = all tools)
44
+ */
45
+ scope?: string[];
46
+ /**
47
+ * Whether this guardrail is enabled (default: true)
48
+ */
49
+ enabled?: boolean;
50
+ /**
51
+ * Tags for grouping/filtering guardrails
52
+ */
53
+ tags?: string[];
54
+ }
55
+ /**
56
+ * Input for adding a custom guardrail
57
+ */
58
+ export interface GuardrailInput {
59
+ id: string;
60
+ name: string;
61
+ description: string;
62
+ patterns: RegExp[];
63
+ action: GuardrailAction;
64
+ message: string;
65
+ scope?: string[];
66
+ enabled?: boolean;
67
+ tags?: string[];
68
+ }
69
+ /**
70
+ * Result of checking a guardrail
71
+ */
72
+ export interface GuardrailResult {
73
+ /**
74
+ * Whether any guardrail was triggered
75
+ */
76
+ triggered: boolean;
77
+ /**
78
+ * The guardrail that was triggered (if any)
79
+ */
80
+ guardrail?: Guardrail;
81
+ /**
82
+ * The matched pattern (if any)
83
+ */
84
+ match?: string;
85
+ /**
86
+ * The action to take
87
+ */
88
+ action?: GuardrailAction;
89
+ /**
90
+ * The tool that triggered the guardrail
91
+ */
92
+ toolName?: string;
93
+ /**
94
+ * The input that triggered the guardrail
95
+ */
96
+ input?: unknown;
97
+ }
98
+ /**
99
+ * Context passed to guardrail handlers
100
+ */
101
+ export interface GuardrailContext {
102
+ /**
103
+ * The tool being called
104
+ */
105
+ toolName: string;
106
+ /**
107
+ * The tool input
108
+ */
109
+ input: unknown;
110
+ /**
111
+ * The stringified input used for pattern matching
112
+ */
113
+ inputString: string;
114
+ }
115
+ /**
116
+ * Handler called when a guardrail is triggered
117
+ *
118
+ * @param result - The guardrail check result
119
+ * @param context - Context about the tool call
120
+ * @returns true to proceed with execution, false to block
121
+ */
122
+ export type GuardrailTriggeredHandler = (result: GuardrailResult, context: GuardrailContext) => boolean | Promise<boolean>;
123
+ /**
124
+ * Configuration for the GuardrailManager
125
+ */
126
+ export interface GuardrailManagerOptions {
127
+ /**
128
+ * Enable guardrails (default: true)
129
+ */
130
+ enabled?: boolean;
131
+ /**
132
+ * Custom guardrails to add
133
+ */
134
+ custom?: GuardrailInput[];
135
+ /**
136
+ * Include built-in guardrails (default: true)
137
+ */
138
+ includeDefaults?: boolean;
139
+ /**
140
+ * Handler called when a guardrail is triggered
141
+ */
142
+ onTriggered?: GuardrailTriggeredHandler;
143
+ }
144
+ /**
145
+ * Event types for guardrail operations
146
+ */
147
+ export type GuardrailEventType = 'guardrail:triggered' | 'guardrail:warning' | 'guardrail:blocked' | 'guardrail:confirmed' | 'guardrail:cancelled';
148
+ /**
149
+ * Guardrail event payload
150
+ */
151
+ export interface GuardrailEvent {
152
+ type: GuardrailEventType;
153
+ result: GuardrailResult;
154
+ context: GuardrailContext;
155
+ }
156
+ /**
157
+ * Event handler for guardrail events
158
+ */
159
+ export type GuardrailEventHandler = (event: GuardrailEvent) => void;
@@ -0,0 +1,7 @@
1
+ /**
2
+ * Guardrails - Pattern-based detection of risky operations
3
+ *
4
+ * Guardrails scan tool inputs/commands before execution, match against
5
+ * known dangerous patterns, and emit warnings or block execution.
6
+ */
7
+ export {};
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Hooks System - Lifecycle hooks for agent customization
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * import { HooksManager, HooksConfig } from '@compilr-dev/agents';
7
+ *
8
+ * // Configure hooks
9
+ * const hooks: HooksConfig = {
10
+ * beforeTool: [
11
+ * async (ctx) => {
12
+ * console.log(`Tool ${ctx.toolName} starting...`);
13
+ * }
14
+ * ],
15
+ * afterTool: [
16
+ * async (ctx) => {
17
+ * console.log(`Tool ${ctx.toolName} completed in ${ctx.durationMs}ms`);
18
+ * }
19
+ * ]
20
+ * };
21
+ *
22
+ * // Use with Agent
23
+ * const agent = new Agent({
24
+ * provider,
25
+ * hooks,
26
+ * });
27
+ * ```
28
+ */
29
+ export { HooksManager } from './manager.js';
30
+ export type { HooksManagerOptions } from './manager.js';
31
+ 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 './types.js';
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Hooks System - Lifecycle hooks for agent customization
3
+ *
4
+ * @example
5
+ * ```typescript
6
+ * import { HooksManager, HooksConfig } from '@compilr-dev/agents';
7
+ *
8
+ * // Configure hooks
9
+ * const hooks: HooksConfig = {
10
+ * beforeTool: [
11
+ * async (ctx) => {
12
+ * console.log(`Tool ${ctx.toolName} starting...`);
13
+ * }
14
+ * ],
15
+ * afterTool: [
16
+ * async (ctx) => {
17
+ * console.log(`Tool ${ctx.toolName} completed in ${ctx.durationMs}ms`);
18
+ * }
19
+ * ]
20
+ * };
21
+ *
22
+ * // Use with Agent
23
+ * const agent = new Agent({
24
+ * provider,
25
+ * hooks,
26
+ * });
27
+ * ```
28
+ */
29
+ export { HooksManager } from './manager.js';
@@ -0,0 +1,147 @@
1
+ /**
2
+ * HooksManager - Manages lifecycle hooks for agent customization
3
+ *
4
+ * Provides:
5
+ * - Hook registration with priorities
6
+ * - Sequential execution in priority order
7
+ * - Type-safe hook invocation
8
+ * - Error handling and recovery
9
+ */
10
+ import type { HooksConfig, IterationHookContext, LLMHookContext, AfterLLMHookContext, ToolHookContext, AfterToolHookContext, ErrorHookContext, BeforeIterationHook, AfterIterationHook, BeforeLLMHook, AfterLLMHook, BeforeToolHook, AfterToolHook, OnErrorHook, ErrorHookResult, HookRegistrationOptions, HookEventHandler } from './types.js';
11
+ import type { Message } from '../providers/types.js';
12
+ import type { ToolDefinition, ToolExecutionResult } from '../tools/types.js';
13
+ /**
14
+ * Options for creating a HooksManager
15
+ */
16
+ export interface HooksManagerOptions {
17
+ /**
18
+ * Initial hooks configuration
19
+ */
20
+ hooks?: HooksConfig;
21
+ /**
22
+ * Event handler for hook lifecycle events
23
+ */
24
+ onEvent?: HookEventHandler;
25
+ }
26
+ /**
27
+ * Manages lifecycle hooks for agent customization
28
+ */
29
+ export declare class HooksManager {
30
+ private beforeIterationHooks;
31
+ private afterIterationHooks;
32
+ private beforeLLMHooks;
33
+ private afterLLMHooks;
34
+ private beforeToolHooks;
35
+ private afterToolHooks;
36
+ private onErrorHooks;
37
+ private readonly onEvent?;
38
+ private hookIdCounter;
39
+ constructor(options?: HooksManagerOptions);
40
+ /**
41
+ * Register hooks from a configuration object
42
+ */
43
+ registerFromConfig(config: HooksConfig): void;
44
+ /**
45
+ * Register a before:iteration hook
46
+ */
47
+ registerBeforeIteration(hook: BeforeIterationHook, options?: HookRegistrationOptions): string;
48
+ /**
49
+ * Register an after:iteration hook
50
+ */
51
+ registerAfterIteration(hook: AfterIterationHook, options?: HookRegistrationOptions): string;
52
+ /**
53
+ * Register a before:llm hook
54
+ */
55
+ registerBeforeLLM(hook: BeforeLLMHook, options?: HookRegistrationOptions): string;
56
+ /**
57
+ * Register an after:llm hook
58
+ */
59
+ registerAfterLLM(hook: AfterLLMHook, options?: HookRegistrationOptions): string;
60
+ /**
61
+ * Register a before:tool hook
62
+ */
63
+ registerBeforeTool(hook: BeforeToolHook, options?: HookRegistrationOptions): string;
64
+ /**
65
+ * Register an after:tool hook
66
+ */
67
+ registerAfterTool(hook: AfterToolHook, options?: HookRegistrationOptions): string;
68
+ /**
69
+ * Register an onError hook
70
+ */
71
+ registerOnError(hook: OnErrorHook, options?: HookRegistrationOptions): string;
72
+ /**
73
+ * Unregister a hook by ID
74
+ */
75
+ unregister(hookId: string): boolean;
76
+ /**
77
+ * Clear all hooks
78
+ */
79
+ clear(): void;
80
+ /**
81
+ * Run before:iteration hooks
82
+ *
83
+ * @returns true to continue, false to skip iteration
84
+ */
85
+ runBeforeIteration(context: IterationHookContext): Promise<boolean>;
86
+ /**
87
+ * Run after:iteration hooks
88
+ */
89
+ runAfterIteration(context: IterationHookContext & {
90
+ toolCalls: Array<{
91
+ name: string;
92
+ input: Record<string, unknown>;
93
+ result: ToolExecutionResult;
94
+ }>;
95
+ completedWithText: boolean;
96
+ }): Promise<void>;
97
+ /**
98
+ * Run before:llm hooks
99
+ *
100
+ * @returns potentially modified messages and tools
101
+ */
102
+ runBeforeLLM(context: LLMHookContext): Promise<{
103
+ messages: Message[];
104
+ tools: ToolDefinition[];
105
+ }>;
106
+ /**
107
+ * Run after:llm hooks
108
+ */
109
+ runAfterLLM(context: AfterLLMHookContext): Promise<void>;
110
+ /**
111
+ * Run before:tool hooks
112
+ *
113
+ * @returns whether to proceed and potentially modified input or skip result
114
+ */
115
+ runBeforeTool(context: ToolHookContext): Promise<{
116
+ proceed: boolean;
117
+ input: Record<string, unknown>;
118
+ skipResult?: ToolExecutionResult;
119
+ }>;
120
+ /**
121
+ * Run after:tool hooks
122
+ *
123
+ * @returns potentially modified result
124
+ */
125
+ runAfterTool(context: AfterToolHookContext): Promise<ToolExecutionResult>;
126
+ /**
127
+ * Run onError hooks
128
+ *
129
+ * @returns error handling result
130
+ */
131
+ runOnError(context: ErrorHookContext): Promise<ErrorHookResult>;
132
+ /**
133
+ * Check if any hooks are registered
134
+ */
135
+ hasHooks(): boolean;
136
+ /**
137
+ * Get hook counts by type
138
+ */
139
+ getHookCounts(): Record<keyof HooksConfig, number>;
140
+ /**
141
+ * Get all registered hook IDs
142
+ */
143
+ getHookIds(): string[];
144
+ private generateId;
145
+ private sortByPriority;
146
+ private emitEvent;
147
+ }