@crewai-ts/core 0.1.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/LICENSE +21 -0
- package/README.md +692 -0
- package/dist/chunk-3PVW4JKT.js +9749 -0
- package/dist/chunk-BE4JYKSG.js +4677 -0
- package/dist/chunk-LWD4QED4.js +656 -0
- package/dist/index.cjs +71303 -0
- package/dist/index.d.cts +13068 -0
- package/dist/index.d.ts +13068 -0
- package/dist/index.js +54839 -0
- package/dist/llms-hooks-transport-ChGiFBiU.d.ts +233 -0
- package/dist/llms-hooks-transport-DZlurMUQ.d.cts +233 -0
- package/dist/llms-hooks-transport.cjs +70 -0
- package/dist/llms-hooks-transport.d.cts +2 -0
- package/dist/llms-hooks-transport.d.ts +2 -0
- package/dist/llms-hooks-transport.js +10 -0
- package/dist/mcp-DS7UMYAM.js +62 -0
- package/dist/state-provider-core-Be9RKRAm.d.cts +4876 -0
- package/dist/state-provider-core-Be9RKRAm.d.ts +4876 -0
- package/dist/state-provider-core.cjs +55 -0
- package/dist/state-provider-core.d.cts +1 -0
- package/dist/state-provider-core.d.ts +1 -0
- package/dist/state-provider-core.js +7 -0
- package/package.json +5049 -0
|
@@ -0,0 +1,656 @@
|
|
|
1
|
+
// src/hooks.ts
|
|
2
|
+
var LLMCallHookContext = class {
|
|
3
|
+
executor;
|
|
4
|
+
agent;
|
|
5
|
+
task;
|
|
6
|
+
crew;
|
|
7
|
+
llm;
|
|
8
|
+
iterations;
|
|
9
|
+
messages;
|
|
10
|
+
response;
|
|
11
|
+
constructor(options = {}) {
|
|
12
|
+
this.executor = options.executor ?? null;
|
|
13
|
+
const executor = readHookExecutor(options.executor);
|
|
14
|
+
this.messages = options.messages ?? executor.messages ?? [];
|
|
15
|
+
this.llm = options.llm ?? executor.llm ?? null;
|
|
16
|
+
this.agent = options.agent ?? executor.agent ?? null;
|
|
17
|
+
this.task = options.task ?? executor.task ?? null;
|
|
18
|
+
this.crew = options.crew ?? executor.crew ?? null;
|
|
19
|
+
this.iterations = options.iterations ?? executor.iterations ?? 0;
|
|
20
|
+
this.response = options.response ?? null;
|
|
21
|
+
}
|
|
22
|
+
requestHumanInput(prompt, defaultMessage) {
|
|
23
|
+
return requestHookHumanInput(this, prompt, defaultMessage);
|
|
24
|
+
}
|
|
25
|
+
request_human_input(prompt, defaultMessage = "Press Enter to continue, or provide feedback:") {
|
|
26
|
+
return this.requestHumanInput(prompt, defaultMessage);
|
|
27
|
+
}
|
|
28
|
+
};
|
|
29
|
+
var ToolCallHookContext = class {
|
|
30
|
+
toolName;
|
|
31
|
+
tool_name;
|
|
32
|
+
tool;
|
|
33
|
+
agent;
|
|
34
|
+
task;
|
|
35
|
+
crew;
|
|
36
|
+
toolInput;
|
|
37
|
+
tool_input;
|
|
38
|
+
toolResult;
|
|
39
|
+
tool_result;
|
|
40
|
+
constructor(options) {
|
|
41
|
+
this.toolName = options.toolName ?? options.tool_name ?? options.tool.name;
|
|
42
|
+
this.tool_name = this.toolName;
|
|
43
|
+
this.toolInput = options.toolInput ?? options.tool_input ?? {};
|
|
44
|
+
this.tool_input = this.toolInput;
|
|
45
|
+
this.tool = options.tool;
|
|
46
|
+
this.agent = options.agent ?? null;
|
|
47
|
+
this.task = options.task ?? null;
|
|
48
|
+
this.crew = options.crew ?? null;
|
|
49
|
+
this.toolResult = options.toolResult ?? options.tool_result ?? null;
|
|
50
|
+
this.tool_result = this.toolResult;
|
|
51
|
+
}
|
|
52
|
+
requestHumanInput(prompt, defaultMessage) {
|
|
53
|
+
return requestHookHumanInput(this, prompt, defaultMessage);
|
|
54
|
+
}
|
|
55
|
+
request_human_input(prompt, defaultMessage = "Press Enter to continue, or provide feedback:") {
|
|
56
|
+
return this.requestHumanInput(prompt, defaultMessage);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
var ContextT = Object.freeze({ kind: "TypeVar" });
|
|
60
|
+
var ReturnT = Object.freeze({ kind: "TypeVar" });
|
|
61
|
+
var P = Object.freeze({ kind: "TypeVar" });
|
|
62
|
+
var R = Object.freeze({ kind: "TypeVar" });
|
|
63
|
+
var U = Object.freeze({ kind: "TypeVar" });
|
|
64
|
+
var Hook = Object.freeze({ kind: "Hook" });
|
|
65
|
+
var HTTPTransportKwargs = Object.freeze({ kind: "HTTPTransportKwargs" });
|
|
66
|
+
var BaseInterceptor = class _BaseInterceptor {
|
|
67
|
+
static __get_pydantic_core_schema__() {
|
|
68
|
+
return {
|
|
69
|
+
type: "plain-validator",
|
|
70
|
+
validator: (value) => this.validate_interceptor(value),
|
|
71
|
+
serialization: { type: "identity" }
|
|
72
|
+
};
|
|
73
|
+
}
|
|
74
|
+
static validateInterceptor(value) {
|
|
75
|
+
if (!(value instanceof _BaseInterceptor)) {
|
|
76
|
+
const typeName = value === null ? "null" : Array.isArray(value) ? "Array" : typeof value === "object" ? value.constructor.name : typeof value;
|
|
77
|
+
throw new Error(`Expected BaseInterceptor instance, got ${typeName}`);
|
|
78
|
+
}
|
|
79
|
+
return value;
|
|
80
|
+
}
|
|
81
|
+
static validate_interceptor(value) {
|
|
82
|
+
return this.validateInterceptor(value);
|
|
83
|
+
}
|
|
84
|
+
async aon_outbound(message) {
|
|
85
|
+
await Promise.resolve();
|
|
86
|
+
return this.on_outbound(message);
|
|
87
|
+
}
|
|
88
|
+
async aon_inbound(message) {
|
|
89
|
+
await Promise.resolve();
|
|
90
|
+
return this.on_inbound(message);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
function _validate_interceptor(value) {
|
|
94
|
+
return BaseInterceptor.validateInterceptor(value);
|
|
95
|
+
}
|
|
96
|
+
var AsyncHTTPTransport = class {
|
|
97
|
+
interceptor;
|
|
98
|
+
kwargs;
|
|
99
|
+
constructor(interceptor, kwargs = {}) {
|
|
100
|
+
this.interceptor = interceptor;
|
|
101
|
+
this.kwargs = { ...kwargs };
|
|
102
|
+
}
|
|
103
|
+
async handle_async_request(request) {
|
|
104
|
+
const outbound = await this.interceptor.aon_outbound(request);
|
|
105
|
+
return await this.interceptor.aon_inbound(outbound);
|
|
106
|
+
}
|
|
107
|
+
};
|
|
108
|
+
var HTTPTransport = class {
|
|
109
|
+
interceptor;
|
|
110
|
+
kwargs;
|
|
111
|
+
constructor(interceptor, kwargs = {}) {
|
|
112
|
+
this.interceptor = interceptor;
|
|
113
|
+
this.kwargs = { ...kwargs };
|
|
114
|
+
}
|
|
115
|
+
handle_request(request) {
|
|
116
|
+
const outbound = this.interceptor.on_outbound(request);
|
|
117
|
+
return this.interceptor.on_inbound(outbound);
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
var BeforeLLMCallHook = Object.freeze({ kind: "BeforeLLMCallHook" });
|
|
121
|
+
var AfterLLMCallHook = Object.freeze({ kind: "AfterLLMCallHook" });
|
|
122
|
+
var BeforeToolCallHook = Object.freeze({ kind: "BeforeToolCallHook" });
|
|
123
|
+
var AfterToolCallHook = Object.freeze({ kind: "AfterToolCallHook" });
|
|
124
|
+
var BeforeLLMCallHookType = BeforeLLMCallHook;
|
|
125
|
+
var AfterLLMCallHookType = AfterLLMCallHook;
|
|
126
|
+
var BeforeToolCallHookType = BeforeToolCallHook;
|
|
127
|
+
var AfterToolCallHookType = AfterToolCallHook;
|
|
128
|
+
var BeforeLLMCallHookCallable = BeforeLLMCallHook;
|
|
129
|
+
var AfterLLMCallHookCallable = AfterLLMCallHook;
|
|
130
|
+
var BeforeToolCallHookCallable = BeforeToolCallHook;
|
|
131
|
+
var AfterToolCallHookCallable = AfterToolCallHook;
|
|
132
|
+
var HookMethod = class {
|
|
133
|
+
_meth;
|
|
134
|
+
agents;
|
|
135
|
+
tools;
|
|
136
|
+
constructor(method, options = {}) {
|
|
137
|
+
this._meth = method;
|
|
138
|
+
this.agents = options.agents ?? null;
|
|
139
|
+
this.tools = options.tools ?? null;
|
|
140
|
+
Object.defineProperties(this, {
|
|
141
|
+
__name__: { value: method.name, enumerable: false },
|
|
142
|
+
__doc__: { value: null, enumerable: false }
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
call(...args) {
|
|
146
|
+
return this._meth(...args);
|
|
147
|
+
}
|
|
148
|
+
__call__(...args) {
|
|
149
|
+
return this.call(...args);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
var BeforeLLMCallHookMethod = class extends HookMethod {
|
|
153
|
+
is_before_llm_call_hook = true;
|
|
154
|
+
constructor(method, agents = null) {
|
|
155
|
+
super(method, { agents });
|
|
156
|
+
}
|
|
157
|
+
};
|
|
158
|
+
var AfterLLMCallHookMethod = class extends HookMethod {
|
|
159
|
+
is_after_llm_call_hook = true;
|
|
160
|
+
constructor(method, agents = null) {
|
|
161
|
+
super(method, { agents });
|
|
162
|
+
}
|
|
163
|
+
};
|
|
164
|
+
var BeforeToolCallHookMethod = class extends HookMethod {
|
|
165
|
+
is_before_tool_call_hook = true;
|
|
166
|
+
constructor(method, tools = null, agents = null) {
|
|
167
|
+
super(method, { agents, tools });
|
|
168
|
+
}
|
|
169
|
+
};
|
|
170
|
+
var AfterToolCallHookMethod = class extends HookMethod {
|
|
171
|
+
is_after_tool_call_hook = true;
|
|
172
|
+
constructor(method, tools = null, agents = null) {
|
|
173
|
+
super(method, { agents, tools });
|
|
174
|
+
}
|
|
175
|
+
};
|
|
176
|
+
var beforeLlmCallHooks = [];
|
|
177
|
+
var afterLlmCallHooks = [];
|
|
178
|
+
var beforeToolCallHooks = [];
|
|
179
|
+
var afterToolCallHooks = [];
|
|
180
|
+
function registerBeforeLlmCallHook(hook) {
|
|
181
|
+
beforeLlmCallHooks.push(hook);
|
|
182
|
+
}
|
|
183
|
+
var register_before_llm_call_hook = registerBeforeLlmCallHook;
|
|
184
|
+
function registerAfterLlmCallHook(hook) {
|
|
185
|
+
afterLlmCallHooks.push(hook);
|
|
186
|
+
}
|
|
187
|
+
var register_after_llm_call_hook = registerAfterLlmCallHook;
|
|
188
|
+
function getBeforeLlmCallHooks() {
|
|
189
|
+
return [...beforeLlmCallHooks];
|
|
190
|
+
}
|
|
191
|
+
var get_before_llm_call_hooks = getBeforeLlmCallHooks;
|
|
192
|
+
function getAfterLlmCallHooks() {
|
|
193
|
+
return [...afterLlmCallHooks];
|
|
194
|
+
}
|
|
195
|
+
var get_after_llm_call_hooks = getAfterLlmCallHooks;
|
|
196
|
+
function unregisterBeforeLlmCallHook(hook) {
|
|
197
|
+
return removeHook(beforeLlmCallHooks, hook);
|
|
198
|
+
}
|
|
199
|
+
var unregister_before_llm_call_hook = unregisterBeforeLlmCallHook;
|
|
200
|
+
function unregisterAfterLlmCallHook(hook) {
|
|
201
|
+
return removeHook(afterLlmCallHooks, hook);
|
|
202
|
+
}
|
|
203
|
+
var unregister_after_llm_call_hook = unregisterAfterLlmCallHook;
|
|
204
|
+
function clearBeforeLlmCallHooks() {
|
|
205
|
+
return clearHooks(beforeLlmCallHooks);
|
|
206
|
+
}
|
|
207
|
+
var clear_before_llm_call_hooks = clearBeforeLlmCallHooks;
|
|
208
|
+
function clearAfterLlmCallHooks() {
|
|
209
|
+
return clearHooks(afterLlmCallHooks);
|
|
210
|
+
}
|
|
211
|
+
var clear_after_llm_call_hooks = clearAfterLlmCallHooks;
|
|
212
|
+
function clearAllLlmCallHooks() {
|
|
213
|
+
return [clearBeforeLlmCallHooks(), clearAfterLlmCallHooks()];
|
|
214
|
+
}
|
|
215
|
+
var clear_all_llm_call_hooks = clearAllLlmCallHooks;
|
|
216
|
+
function registerBeforeToolCallHook(hook) {
|
|
217
|
+
beforeToolCallHooks.push(hook);
|
|
218
|
+
}
|
|
219
|
+
var register_before_tool_call_hook = registerBeforeToolCallHook;
|
|
220
|
+
function registerAfterToolCallHook(hook) {
|
|
221
|
+
afterToolCallHooks.push(hook);
|
|
222
|
+
}
|
|
223
|
+
var register_after_tool_call_hook = registerAfterToolCallHook;
|
|
224
|
+
function getBeforeToolCallHooks() {
|
|
225
|
+
return [...beforeToolCallHooks];
|
|
226
|
+
}
|
|
227
|
+
var get_before_tool_call_hooks = getBeforeToolCallHooks;
|
|
228
|
+
function getAfterToolCallHooks() {
|
|
229
|
+
return [...afterToolCallHooks];
|
|
230
|
+
}
|
|
231
|
+
var get_after_tool_call_hooks = getAfterToolCallHooks;
|
|
232
|
+
function unregisterBeforeToolCallHook(hook) {
|
|
233
|
+
return removeHook(beforeToolCallHooks, hook);
|
|
234
|
+
}
|
|
235
|
+
var unregister_before_tool_call_hook = unregisterBeforeToolCallHook;
|
|
236
|
+
function unregisterAfterToolCallHook(hook) {
|
|
237
|
+
return removeHook(afterToolCallHooks, hook);
|
|
238
|
+
}
|
|
239
|
+
var unregister_after_tool_call_hook = unregisterAfterToolCallHook;
|
|
240
|
+
function clearBeforeToolCallHooks() {
|
|
241
|
+
return clearHooks(beforeToolCallHooks);
|
|
242
|
+
}
|
|
243
|
+
var clear_before_tool_call_hooks = clearBeforeToolCallHooks;
|
|
244
|
+
function clearAfterToolCallHooks() {
|
|
245
|
+
return clearHooks(afterToolCallHooks);
|
|
246
|
+
}
|
|
247
|
+
var clear_after_tool_call_hooks = clearAfterToolCallHooks;
|
|
248
|
+
function clearAllToolCallHooks() {
|
|
249
|
+
return [clearBeforeToolCallHooks(), clearAfterToolCallHooks()];
|
|
250
|
+
}
|
|
251
|
+
var clear_all_tool_call_hooks = clearAllToolCallHooks;
|
|
252
|
+
function clearAllGlobalHooks() {
|
|
253
|
+
const llmHooks = clearAllLlmCallHooks();
|
|
254
|
+
const toolHooks = clearAllToolCallHooks();
|
|
255
|
+
return {
|
|
256
|
+
llm_hooks: llmHooks,
|
|
257
|
+
tool_hooks: toolHooks,
|
|
258
|
+
total: [llmHooks[0] + toolHooks[0], llmHooks[1] + toolHooks[1]]
|
|
259
|
+
};
|
|
260
|
+
}
|
|
261
|
+
var clear_all_global_hooks = clearAllGlobalHooks;
|
|
262
|
+
function registerCrewScopedHooks(instance) {
|
|
263
|
+
const registered = [];
|
|
264
|
+
const seen = /* @__PURE__ */ new Set();
|
|
265
|
+
let prototype = Object.getPrototypeOf(instance);
|
|
266
|
+
while (prototype && prototype !== Object.prototype) {
|
|
267
|
+
for (const name of Reflect.ownKeys(prototype)) {
|
|
268
|
+
if (name === "constructor" || seen.has(name)) {
|
|
269
|
+
continue;
|
|
270
|
+
}
|
|
271
|
+
seen.add(name);
|
|
272
|
+
const descriptor = Object.getOwnPropertyDescriptor(prototype, name);
|
|
273
|
+
const method = getObjectProperty(descriptor, "value");
|
|
274
|
+
if (typeof method !== "function") {
|
|
275
|
+
continue;
|
|
276
|
+
}
|
|
277
|
+
registerCrewScopedHook(instance, method, registered);
|
|
278
|
+
}
|
|
279
|
+
prototype = Object.getPrototypeOf(prototype);
|
|
280
|
+
}
|
|
281
|
+
Object.defineProperty(instance, "_registered_hook_functions", {
|
|
282
|
+
configurable: true,
|
|
283
|
+
enumerable: false,
|
|
284
|
+
writable: true,
|
|
285
|
+
value: registered
|
|
286
|
+
});
|
|
287
|
+
return registered;
|
|
288
|
+
}
|
|
289
|
+
function beforeLlmCall(value, context) {
|
|
290
|
+
if (typeof value === "function") {
|
|
291
|
+
if (context) {
|
|
292
|
+
markHook(value, "is_before_llm_call_hook", {});
|
|
293
|
+
return value;
|
|
294
|
+
}
|
|
295
|
+
return registerDecoratedHook(value, registerBeforeLlmCallHook, "is_before_llm_call_hook", {});
|
|
296
|
+
}
|
|
297
|
+
return createHookDecorator(registerBeforeLlmCallHook, "is_before_llm_call_hook", hookFilterOptions(value));
|
|
298
|
+
}
|
|
299
|
+
var before_llm_call = beforeLlmCall;
|
|
300
|
+
function afterLlmCall(value, context) {
|
|
301
|
+
if (typeof value === "function") {
|
|
302
|
+
if (context) {
|
|
303
|
+
markHook(value, "is_after_llm_call_hook", {});
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
306
|
+
return registerDecoratedHook(value, registerAfterLlmCallHook, "is_after_llm_call_hook", {});
|
|
307
|
+
}
|
|
308
|
+
return createHookDecorator(registerAfterLlmCallHook, "is_after_llm_call_hook", hookFilterOptions(value));
|
|
309
|
+
}
|
|
310
|
+
var after_llm_call = afterLlmCall;
|
|
311
|
+
function beforeToolCall(value, context) {
|
|
312
|
+
if (typeof value === "function") {
|
|
313
|
+
if (context) {
|
|
314
|
+
markHook(value, "is_before_tool_call_hook", {});
|
|
315
|
+
return value;
|
|
316
|
+
}
|
|
317
|
+
return registerDecoratedHook(value, registerBeforeToolCallHook, "is_before_tool_call_hook", {});
|
|
318
|
+
}
|
|
319
|
+
return createHookDecorator(registerBeforeToolCallHook, "is_before_tool_call_hook", hookFilterOptions(value));
|
|
320
|
+
}
|
|
321
|
+
var before_tool_call = beforeToolCall;
|
|
322
|
+
function afterToolCall(value, context) {
|
|
323
|
+
if (typeof value === "function") {
|
|
324
|
+
if (context) {
|
|
325
|
+
markHook(value, "is_after_tool_call_hook", {});
|
|
326
|
+
return value;
|
|
327
|
+
}
|
|
328
|
+
return registerDecoratedHook(value, registerAfterToolCallHook, "is_after_tool_call_hook", {});
|
|
329
|
+
}
|
|
330
|
+
return createHookDecorator(registerAfterToolCallHook, "is_after_tool_call_hook", hookFilterOptions(value));
|
|
331
|
+
}
|
|
332
|
+
var after_tool_call = afterToolCall;
|
|
333
|
+
async function runBeforeLlmCallHooks(context) {
|
|
334
|
+
for (const hook of beforeLlmCallHooks) {
|
|
335
|
+
if (await hook(context) === false) {
|
|
336
|
+
throw new Error("LLM call blocked by before_llm_call hook.");
|
|
337
|
+
}
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
async function runAfterLlmCallHooks(context) {
|
|
341
|
+
let response = context.response ?? "";
|
|
342
|
+
for (const hook of afterLlmCallHooks) {
|
|
343
|
+
context.response = response;
|
|
344
|
+
const replacement = await hook(context);
|
|
345
|
+
if (replacement !== null && replacement !== void 0) {
|
|
346
|
+
response = replacement;
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
return response;
|
|
350
|
+
}
|
|
351
|
+
async function runBeforeToolCallHooks(context) {
|
|
352
|
+
for (const hook of beforeToolCallHooks) {
|
|
353
|
+
if (await hook(context) === false) {
|
|
354
|
+
throw new Error(`Tool '${context.toolName}' execution blocked by before_tool_call hook.`);
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
}
|
|
358
|
+
async function runAfterToolCallHooks(context) {
|
|
359
|
+
let result = context.toolResult;
|
|
360
|
+
for (const hook of afterToolCallHooks) {
|
|
361
|
+
context.toolResult = result;
|
|
362
|
+
context.tool_result = result;
|
|
363
|
+
const replacement = await hook(context);
|
|
364
|
+
if (replacement !== null && replacement !== void 0) {
|
|
365
|
+
result = replacement;
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return result;
|
|
369
|
+
}
|
|
370
|
+
function removeHook(hooks, hook) {
|
|
371
|
+
const index = hooks.indexOf(hook);
|
|
372
|
+
if (index < 0) {
|
|
373
|
+
return false;
|
|
374
|
+
}
|
|
375
|
+
hooks.splice(index, 1);
|
|
376
|
+
return true;
|
|
377
|
+
}
|
|
378
|
+
function clearHooks(hooks) {
|
|
379
|
+
const count = hooks.length;
|
|
380
|
+
hooks.length = 0;
|
|
381
|
+
return count;
|
|
382
|
+
}
|
|
383
|
+
function createHookDecorator(register, marker, options) {
|
|
384
|
+
const normalizedOptions = normalizeHookFilterOptions(options);
|
|
385
|
+
return ((targetOrHook, propertyKeyOrContext) => {
|
|
386
|
+
if (typeof targetOrHook === "function" && (propertyKeyOrContext === void 0 || isStandardDecoratorContext(propertyKeyOrContext))) {
|
|
387
|
+
markHook(targetOrHook, marker, normalizedOptions);
|
|
388
|
+
if (propertyKeyOrContext === void 0) {
|
|
389
|
+
registerFilteredHook(targetOrHook, register, normalizedOptions);
|
|
390
|
+
}
|
|
391
|
+
return targetOrHook;
|
|
392
|
+
}
|
|
393
|
+
if (propertyKeyOrContext === void 0 || typeof propertyKeyOrContext === "object") {
|
|
394
|
+
return void 0;
|
|
395
|
+
}
|
|
396
|
+
const target = targetOrHook;
|
|
397
|
+
const method = target[propertyKeyOrContext];
|
|
398
|
+
if (typeof method === "function") {
|
|
399
|
+
markHook(method, marker, normalizedOptions);
|
|
400
|
+
}
|
|
401
|
+
return void 0;
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
function registerDecoratedHook(hook, register, marker, options) {
|
|
405
|
+
const normalizedOptions = normalizeHookFilterOptions(options);
|
|
406
|
+
markHook(hook, marker, normalizedOptions);
|
|
407
|
+
registerFilteredHook(hook, register, normalizedOptions);
|
|
408
|
+
return hook;
|
|
409
|
+
}
|
|
410
|
+
function registerFilteredHook(hook, register, options) {
|
|
411
|
+
if (!options.tools?.length && !options.agents?.length) {
|
|
412
|
+
register(hook);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
register(((context) => {
|
|
416
|
+
if (!hookContextMatchesFilters(context, options)) {
|
|
417
|
+
return null;
|
|
418
|
+
}
|
|
419
|
+
return hook(context);
|
|
420
|
+
}));
|
|
421
|
+
}
|
|
422
|
+
function markHook(hook, marker, options) {
|
|
423
|
+
Object.assign(hook, {
|
|
424
|
+
[marker]: true,
|
|
425
|
+
...options.agents?.length ? { agents: [...options.agents], _filter_agents: [...options.agents] } : {},
|
|
426
|
+
...options.tools?.length ? { tools: [...options.tools], _filter_tools: [...options.tools] } : {}
|
|
427
|
+
});
|
|
428
|
+
}
|
|
429
|
+
function normalizeHookFilterOptions(options) {
|
|
430
|
+
return {
|
|
431
|
+
...options.agents ? { agents: [...options.agents] } : {},
|
|
432
|
+
...options.tools ? { tools: options.tools.map(sanitizeHookToolName) } : {}
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
function hookFilterOptions(options) {
|
|
436
|
+
return {
|
|
437
|
+
...options.agents ? { agents: options.agents } : {},
|
|
438
|
+
...options.tools ? { tools: options.tools } : {}
|
|
439
|
+
};
|
|
440
|
+
}
|
|
441
|
+
function hookContextMatchesFilters(context, options) {
|
|
442
|
+
if (options.tools?.length && hasStringProperty(context, "tool_name") && !options.tools.includes(context.tool_name)) {
|
|
443
|
+
return false;
|
|
444
|
+
}
|
|
445
|
+
const agent = getObjectProperty(context, "agent");
|
|
446
|
+
if (options.agents?.length && agent && hasStringProperty(agent, "role") && !options.agents.includes(agent.role)) {
|
|
447
|
+
return false;
|
|
448
|
+
}
|
|
449
|
+
return true;
|
|
450
|
+
}
|
|
451
|
+
function registerCrewScopedHook(instance, method, registered) {
|
|
452
|
+
const options = readHookFilterOptions(method);
|
|
453
|
+
if (hasBooleanProperty(method, "is_before_llm_call_hook")) {
|
|
454
|
+
const hook = ((context) => method.call(instance, context));
|
|
455
|
+
registerFilteredHook(hook, registerBeforeLlmCallHook, options);
|
|
456
|
+
registered.push(["before_llm_call", hook]);
|
|
457
|
+
}
|
|
458
|
+
if (hasBooleanProperty(method, "is_after_llm_call_hook")) {
|
|
459
|
+
const hook = ((context) => method.call(instance, context));
|
|
460
|
+
registerFilteredHook(hook, registerAfterLlmCallHook, options);
|
|
461
|
+
registered.push(["after_llm_call", hook]);
|
|
462
|
+
}
|
|
463
|
+
if (hasBooleanProperty(method, "is_before_tool_call_hook")) {
|
|
464
|
+
const hook = ((context) => method.call(instance, context));
|
|
465
|
+
registerFilteredHook(hook, registerBeforeToolCallHook, options);
|
|
466
|
+
registered.push(["before_tool_call", hook]);
|
|
467
|
+
}
|
|
468
|
+
if (hasBooleanProperty(method, "is_after_tool_call_hook")) {
|
|
469
|
+
const hook = ((context) => method.call(instance, context));
|
|
470
|
+
registerFilteredHook(hook, registerAfterToolCallHook, options);
|
|
471
|
+
registered.push(["after_tool_call", hook]);
|
|
472
|
+
}
|
|
473
|
+
}
|
|
474
|
+
function readHookFilterOptions(method) {
|
|
475
|
+
const agents = getStringArrayProperty(method, "_filter_agents") ?? getStringArrayProperty(method, "agents");
|
|
476
|
+
const tools = getStringArrayProperty(method, "_filter_tools") ?? getStringArrayProperty(method, "tools");
|
|
477
|
+
return {
|
|
478
|
+
...agents ? { agents } : {},
|
|
479
|
+
...tools ? { tools } : {}
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function isStandardDecoratorContext(value) {
|
|
483
|
+
return typeof value === "object" && value !== null && "kind" in value;
|
|
484
|
+
}
|
|
485
|
+
function getObjectProperty(value, key) {
|
|
486
|
+
return (typeof value === "object" || typeof value === "function") && value !== null && key in value ? value[key] : null;
|
|
487
|
+
}
|
|
488
|
+
function hasStringProperty(value, key) {
|
|
489
|
+
return (typeof value === "object" || typeof value === "function") && value !== null && key in value && typeof value[key] === "string";
|
|
490
|
+
}
|
|
491
|
+
function hasBooleanProperty(value, key) {
|
|
492
|
+
return (typeof value === "object" || typeof value === "function") && value !== null && key in value && typeof value[key] === "boolean";
|
|
493
|
+
}
|
|
494
|
+
function getStringArrayProperty(value, key) {
|
|
495
|
+
const property = getObjectProperty(value, key);
|
|
496
|
+
return Array.isArray(property) && property.every((item) => typeof item === "string") ? property : null;
|
|
497
|
+
}
|
|
498
|
+
function sanitizeHookToolName(name) {
|
|
499
|
+
return name.trim().replace(/[^\w-]/g, "_");
|
|
500
|
+
}
|
|
501
|
+
function requestHookHumanInput(source, prompt, defaultMessage) {
|
|
502
|
+
const formatter = resolveHookFormatter(source);
|
|
503
|
+
pauseHookFormatter(formatter);
|
|
504
|
+
try {
|
|
505
|
+
const promptFn = getObjectProperty(globalThis, "prompt");
|
|
506
|
+
const response = isHookPromptFunction(promptFn) ? promptFn(defaultMessage ?? prompt) : "";
|
|
507
|
+
if (response == null) {
|
|
508
|
+
return "";
|
|
509
|
+
}
|
|
510
|
+
if (typeof response === "string") {
|
|
511
|
+
return response.trim();
|
|
512
|
+
}
|
|
513
|
+
if (typeof response === "number" || typeof response === "boolean" || typeof response === "bigint") {
|
|
514
|
+
return response.toString().trim();
|
|
515
|
+
}
|
|
516
|
+
return "";
|
|
517
|
+
} finally {
|
|
518
|
+
resumeHookFormatter(formatter);
|
|
519
|
+
}
|
|
520
|
+
}
|
|
521
|
+
function isHookPromptFunction(value) {
|
|
522
|
+
return typeof value === "function";
|
|
523
|
+
}
|
|
524
|
+
function resolveHookFormatter(source) {
|
|
525
|
+
for (const candidate of hookContextCandidates(source)) {
|
|
526
|
+
const formatter = getObjectProperty(candidate, "formatter");
|
|
527
|
+
if (formatter) {
|
|
528
|
+
return formatter;
|
|
529
|
+
}
|
|
530
|
+
const eventListener = getObjectProperty(candidate, "event_listener") ?? getObjectProperty(candidate, "eventListener");
|
|
531
|
+
const listenerFormatter = getObjectProperty(eventListener, "formatter");
|
|
532
|
+
if (listenerFormatter) {
|
|
533
|
+
return listenerFormatter;
|
|
534
|
+
}
|
|
535
|
+
}
|
|
536
|
+
return null;
|
|
537
|
+
}
|
|
538
|
+
function hookContextCandidates(source) {
|
|
539
|
+
const candidates = [source];
|
|
540
|
+
for (const key of ["executor", "tool", "agent", "task", "crew"]) {
|
|
541
|
+
const value = getObjectProperty(source, key);
|
|
542
|
+
if (value) {
|
|
543
|
+
candidates.push(value);
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
return candidates;
|
|
547
|
+
}
|
|
548
|
+
function pauseHookFormatter(formatter) {
|
|
549
|
+
const pause = getObjectProperty(formatter, "pause_live_updates") ?? getObjectProperty(formatter, "pauseLiveUpdates");
|
|
550
|
+
if (typeof pause === "function") {
|
|
551
|
+
pause.call(formatter);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
function resumeHookFormatter(formatter) {
|
|
555
|
+
const resume = getObjectProperty(formatter, "resume_live_updates") ?? getObjectProperty(formatter, "resumeLiveUpdates");
|
|
556
|
+
if (typeof resume === "function") {
|
|
557
|
+
resume.call(formatter);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
function readHookExecutor(executor) {
|
|
561
|
+
if (!executor || typeof executor !== "object") {
|
|
562
|
+
return {};
|
|
563
|
+
}
|
|
564
|
+
const record = executor;
|
|
565
|
+
return {
|
|
566
|
+
...Array.isArray(record.messages) ? { messages: record.messages } : {},
|
|
567
|
+
..."llm" in record ? { llm: record.llm } : {},
|
|
568
|
+
..."agent" in record ? { agent: record.agent } : {},
|
|
569
|
+
..."task" in record ? { task: record.task } : {},
|
|
570
|
+
..."crew" in record ? { crew: record.crew } : {},
|
|
571
|
+
...typeof record.iterations === "number" ? { iterations: record.iterations } : {}
|
|
572
|
+
};
|
|
573
|
+
}
|
|
574
|
+
|
|
575
|
+
export {
|
|
576
|
+
LLMCallHookContext,
|
|
577
|
+
ToolCallHookContext,
|
|
578
|
+
ContextT,
|
|
579
|
+
ReturnT,
|
|
580
|
+
P,
|
|
581
|
+
R,
|
|
582
|
+
U,
|
|
583
|
+
Hook,
|
|
584
|
+
HTTPTransportKwargs,
|
|
585
|
+
BaseInterceptor,
|
|
586
|
+
_validate_interceptor,
|
|
587
|
+
AsyncHTTPTransport,
|
|
588
|
+
HTTPTransport,
|
|
589
|
+
BeforeLLMCallHook,
|
|
590
|
+
AfterLLMCallHook,
|
|
591
|
+
BeforeToolCallHook,
|
|
592
|
+
AfterToolCallHook,
|
|
593
|
+
BeforeLLMCallHookType,
|
|
594
|
+
AfterLLMCallHookType,
|
|
595
|
+
BeforeToolCallHookType,
|
|
596
|
+
AfterToolCallHookType,
|
|
597
|
+
BeforeLLMCallHookCallable,
|
|
598
|
+
AfterLLMCallHookCallable,
|
|
599
|
+
BeforeToolCallHookCallable,
|
|
600
|
+
AfterToolCallHookCallable,
|
|
601
|
+
BeforeLLMCallHookMethod,
|
|
602
|
+
AfterLLMCallHookMethod,
|
|
603
|
+
BeforeToolCallHookMethod,
|
|
604
|
+
AfterToolCallHookMethod,
|
|
605
|
+
registerBeforeLlmCallHook,
|
|
606
|
+
register_before_llm_call_hook,
|
|
607
|
+
registerAfterLlmCallHook,
|
|
608
|
+
register_after_llm_call_hook,
|
|
609
|
+
getBeforeLlmCallHooks,
|
|
610
|
+
get_before_llm_call_hooks,
|
|
611
|
+
getAfterLlmCallHooks,
|
|
612
|
+
get_after_llm_call_hooks,
|
|
613
|
+
unregisterBeforeLlmCallHook,
|
|
614
|
+
unregister_before_llm_call_hook,
|
|
615
|
+
unregisterAfterLlmCallHook,
|
|
616
|
+
unregister_after_llm_call_hook,
|
|
617
|
+
clearBeforeLlmCallHooks,
|
|
618
|
+
clear_before_llm_call_hooks,
|
|
619
|
+
clearAfterLlmCallHooks,
|
|
620
|
+
clear_after_llm_call_hooks,
|
|
621
|
+
clearAllLlmCallHooks,
|
|
622
|
+
clear_all_llm_call_hooks,
|
|
623
|
+
registerBeforeToolCallHook,
|
|
624
|
+
register_before_tool_call_hook,
|
|
625
|
+
registerAfterToolCallHook,
|
|
626
|
+
register_after_tool_call_hook,
|
|
627
|
+
getBeforeToolCallHooks,
|
|
628
|
+
get_before_tool_call_hooks,
|
|
629
|
+
getAfterToolCallHooks,
|
|
630
|
+
get_after_tool_call_hooks,
|
|
631
|
+
unregisterBeforeToolCallHook,
|
|
632
|
+
unregister_before_tool_call_hook,
|
|
633
|
+
unregisterAfterToolCallHook,
|
|
634
|
+
unregister_after_tool_call_hook,
|
|
635
|
+
clearBeforeToolCallHooks,
|
|
636
|
+
clear_before_tool_call_hooks,
|
|
637
|
+
clearAfterToolCallHooks,
|
|
638
|
+
clear_after_tool_call_hooks,
|
|
639
|
+
clearAllToolCallHooks,
|
|
640
|
+
clear_all_tool_call_hooks,
|
|
641
|
+
clearAllGlobalHooks,
|
|
642
|
+
clear_all_global_hooks,
|
|
643
|
+
registerCrewScopedHooks,
|
|
644
|
+
beforeLlmCall,
|
|
645
|
+
before_llm_call,
|
|
646
|
+
afterLlmCall,
|
|
647
|
+
after_llm_call,
|
|
648
|
+
beforeToolCall,
|
|
649
|
+
before_tool_call,
|
|
650
|
+
afterToolCall,
|
|
651
|
+
after_tool_call,
|
|
652
|
+
runBeforeLlmCallHooks,
|
|
653
|
+
runAfterLlmCallHooks,
|
|
654
|
+
runBeforeToolCallHooks,
|
|
655
|
+
runAfterToolCallHooks
|
|
656
|
+
};
|