@ai-setting/roy-agent-core 1.4.13 → 1.4.14

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 (103) hide show
  1. package/dist/packages/core/src/config/index.js +32 -0
  2. package/dist/packages/core/src/env/agent/index.js +24 -0
  3. package/dist/packages/core/src/env/commands/index.js +14 -0
  4. package/dist/packages/core/src/env/debug/formatters/index.js +11 -0
  5. package/dist/packages/core/src/env/debug/index.js +26 -0
  6. package/dist/packages/core/src/env/hook/index.js +29 -0
  7. package/dist/packages/core/src/env/index.js +81 -0
  8. package/dist/packages/core/src/env/llm/index.js +40 -0
  9. package/dist/packages/core/src/env/log-trace/index.js +83 -0
  10. package/dist/packages/core/src/env/mcp/index.js +39 -0
  11. package/dist/packages/core/src/env/mcp/tool/index.js +14 -0
  12. package/dist/packages/core/src/env/memory/built-in/index.js +11 -0
  13. package/dist/packages/core/src/env/memory/index.js +56 -0
  14. package/dist/packages/core/src/env/memory/plugin/index.js +36 -0
  15. package/dist/packages/core/src/env/prompt/index.js +20 -0
  16. package/dist/packages/core/src/env/session/index.js +25 -0
  17. package/dist/packages/core/src/env/session/storage/index.js +18 -0
  18. package/dist/packages/core/src/env/skill/index.js +34 -0
  19. package/dist/packages/core/src/env/skill/tool/index.js +9 -0
  20. package/dist/packages/core/src/env/task/delegate/index.js +18 -0
  21. package/dist/packages/core/src/env/task/hooks/index.js +7 -0
  22. package/dist/packages/core/src/env/task/index.js +30 -0
  23. package/dist/packages/core/src/env/task/plugins/index.js +23 -0
  24. package/dist/packages/core/src/env/task/storage/index.js +14 -0
  25. package/dist/packages/core/src/env/task/tools/index.js +17 -0
  26. package/dist/packages/core/src/env/task/tools/operation/index.js +15 -0
  27. package/dist/{shared/chunk-1d4rwms4.js → packages/core/src/env/tool/built-in/index.js} +4 -4
  28. package/dist/packages/core/src/env/tool/index.js +39 -0
  29. package/dist/packages/core/src/env/workflow/decorators/index.js +27 -0
  30. package/dist/packages/core/src/env/workflow/engine/index.js +28 -0
  31. package/dist/packages/core/src/env/workflow/index.js +132 -0
  32. package/dist/packages/core/src/env/workflow/nodes/index.js +19 -0
  33. package/dist/packages/core/src/env/workflow/service/index.js +13 -0
  34. package/dist/packages/core/src/env/workflow/storage/index.js +27 -0
  35. package/dist/packages/core/src/env/workflow/tools/index.js +159 -0
  36. package/dist/packages/core/src/env/workflow/types/index.js +94 -0
  37. package/dist/packages/core/src/env/workflow/utils/index.js +637 -0
  38. package/dist/packages/core/src/index.js +398 -0
  39. package/dist/shared/@ai-setting/roy-agent-core-04fm8177.js +393 -0
  40. package/dist/shared/@ai-setting/roy-agent-core-04qgbjbe.js +172 -0
  41. package/dist/shared/@ai-setting/roy-agent-core-084qqd7t.js +11 -0
  42. package/dist/shared/@ai-setting/roy-agent-core-0gekht4e.js +1130 -0
  43. package/dist/shared/@ai-setting/roy-agent-core-0hdry23r.js +419 -0
  44. package/dist/shared/{chunk-ze20rksg.js → @ai-setting/roy-agent-core-0sgn3de4.js} +2 -2
  45. package/dist/shared/@ai-setting/roy-agent-core-12x57kf1.js +286 -0
  46. package/dist/shared/@ai-setting/roy-agent-core-1f3xrrm6.js +393 -0
  47. package/dist/shared/{chunk-q9j99fsm.js → @ai-setting/roy-agent-core-1k28kg7h.js} +6 -6
  48. package/dist/shared/{chunk-2b5kbhx3.js → @ai-setting/roy-agent-core-1z1zv5g8.js} +9 -117
  49. package/dist/shared/@ai-setting/roy-agent-core-2hqxnaf3.js +851 -0
  50. package/dist/shared/@ai-setting/roy-agent-core-3dfq8awb.js +587 -0
  51. package/dist/shared/@ai-setting/roy-agent-core-3takar0s.js +93 -0
  52. package/dist/shared/@ai-setting/roy-agent-core-3tnb2005.js +117 -0
  53. package/dist/shared/@ai-setting/roy-agent-core-4vmcvkav.js +14 -0
  54. package/dist/shared/{chunk-25x2pdtp.js → @ai-setting/roy-agent-core-4ws8atva.js} +2 -2
  55. package/dist/shared/@ai-setting/roy-agent-core-5fbp24se.js +603 -0
  56. package/dist/shared/@ai-setting/roy-agent-core-5my94ywp.js +66 -0
  57. package/dist/shared/{chunk-rncy3rtd.js → @ai-setting/roy-agent-core-6j0zcmwk.js} +128 -1122
  58. package/dist/shared/@ai-setting/roy-agent-core-6w4pmxc7.js +266 -0
  59. package/dist/shared/{chunk-yqmx37vm.js → @ai-setting/roy-agent-core-7vrk3add.js} +2 -2
  60. package/dist/shared/@ai-setting/roy-agent-core-8dvbn7tw.js +64 -0
  61. package/dist/shared/@ai-setting/roy-agent-core-8mbmrwzs.js +171 -0
  62. package/dist/shared/@ai-setting/roy-agent-core-8wzz66qe.js +620 -0
  63. package/dist/shared/@ai-setting/roy-agent-core-9ykq91jc.js +762 -0
  64. package/dist/shared/@ai-setting/roy-agent-core-dde19zke.js +1305 -0
  65. package/dist/shared/@ai-setting/roy-agent-core-f7g67gce.js +913 -0
  66. package/dist/shared/@ai-setting/roy-agent-core-fq5mtxsy.js +341 -0
  67. package/dist/shared/@ai-setting/roy-agent-core-fvd9g6k8.js +1205 -0
  68. package/dist/shared/@ai-setting/roy-agent-core-gv1hrn3x.js +378 -0
  69. package/dist/shared/@ai-setting/roy-agent-core-gy0wp5h7.js +213 -0
  70. package/dist/shared/{chunk-mf5xqbdh.js → @ai-setting/roy-agent-core-hyza1gm7.js} +3 -2
  71. package/dist/shared/{chunk-1qwabsm0.js → @ai-setting/roy-agent-core-j8zx62zr.js} +2 -2
  72. package/dist/shared/@ai-setting/roy-agent-core-jb2exr0d.js +442 -0
  73. package/dist/shared/@ai-setting/roy-agent-core-jv3b7v9w.js +57 -0
  74. package/dist/shared/@ai-setting/roy-agent-core-k1rxf9ya.js +513 -0
  75. package/dist/shared/@ai-setting/roy-agent-core-kydc9nwb.js +60 -0
  76. package/dist/shared/@ai-setting/roy-agent-core-m2x48hw6.js +97 -0
  77. package/dist/shared/@ai-setting/roy-agent-core-m6y668cc.js +377 -0
  78. package/dist/shared/{chunk-1aakcfp1.js → @ai-setting/roy-agent-core-nczzf0ms.js} +3 -3
  79. package/dist/shared/@ai-setting/roy-agent-core-nfj6knp5.js +36 -0
  80. package/dist/shared/@ai-setting/roy-agent-core-ntrp979d.js +204 -0
  81. package/dist/shared/@ai-setting/roy-agent-core-pd7g8z5v.js +1387 -0
  82. package/dist/shared/@ai-setting/roy-agent-core-pzk1syce.js +14 -0
  83. package/dist/shared/@ai-setting/roy-agent-core-q50tg9m2.js +862 -0
  84. package/dist/shared/{chunk-9qzt1v1p.js → @ai-setting/roy-agent-core-qg9tcaph.js} +3 -2
  85. package/dist/shared/{chunk-91bas8w5.js → @ai-setting/roy-agent-core-qhyerewk.js} +5 -5
  86. package/dist/shared/{chunk-1pf5mfgd.js → @ai-setting/roy-agent-core-qxybm159.js} +3 -3
  87. package/dist/shared/{chunk-g6j5n3gv.js → @ai-setting/roy-agent-core-rh9dpkpw.js} +10 -10
  88. package/dist/shared/{chunk-0q6s9wm6.js → @ai-setting/roy-agent-core-rr9p1g43.js} +7 -51
  89. package/dist/shared/@ai-setting/roy-agent-core-sbzvpfn7.js +284 -0
  90. package/dist/shared/@ai-setting/roy-agent-core-t22nqt4d.js +788 -0
  91. package/dist/shared/{chunk-t1rh6jtm.js → @ai-setting/roy-agent-core-tkr5ynkh.js} +8 -13
  92. package/dist/shared/@ai-setting/roy-agent-core-v4aabsf0.js +303 -0
  93. package/dist/shared/@ai-setting/roy-agent-core-w75rafhy.js +202 -0
  94. package/dist/shared/@ai-setting/roy-agent-core-w76hqkmg.js +584 -0
  95. package/dist/shared/@ai-setting/roy-agent-core-yfbgwes2.js +408 -0
  96. package/dist/shared/{chunk-a9qmy3sc.js → @ai-setting/roy-agent-core-yn761yve.js} +7 -4
  97. package/dist/shared/@ai-setting/roy-agent-core-yrzmn4m1.js +492 -0
  98. package/dist/shared/@ai-setting/roy-agent-core-yt8wdh2w.js +206 -0
  99. package/package.json +8 -7
  100. package/dist/index.d.ts +0 -7825
  101. package/dist/index.js +0 -16551
  102. package/dist/shared/chunk-hs7tbmje.js +0 -24
  103. /package/dist/shared/{chunk-wbkh7wat.js → @ai-setting/roy-agent-core-fs0mn2jk.js} +0 -0
@@ -1,18 +1,15 @@
1
+ import {
2
+ getDatabase
3
+ } from "./roy-agent-core-qxybm159.js";
1
4
  import {
2
5
  exports_search_query_parser,
3
6
  init_search_query_parser
4
- } from "./chunk-ze20rksg.js";
5
- import {
6
- getDatabase
7
- } from "./chunk-1pf5mfgd.js";
8
- import"./chunk-a9qmy3sc.js";
9
- import"./chunk-q9j99fsm.js";
10
- import"./chunk-1qwabsm0.js";
7
+ } from "./roy-agent-core-0sgn3de4.js";
11
8
  import {
12
9
  __toCommonJS
13
- } from "./chunk-wbkh7wat.js";
10
+ } from "./roy-agent-core-fs0mn2jk.js";
14
11
 
15
- // src/env/workflow/storage/workflow-repo.ts
12
+ // packages/core/src/env/workflow/storage/workflow-repo.ts
16
13
  import { randomUUID } from "crypto";
17
14
  function rowToWorkflow(row) {
18
15
  const metadata = JSON.parse(row.metadata);
@@ -199,7 +196,5 @@ class WorkflowRepository {
199
196
  return rows.map(rowToWorkflow);
200
197
  }
201
198
  }
202
- export {
203
- rowToWorkflow,
204
- WorkflowRepository
205
- };
199
+
200
+ export { rowToWorkflow, WorkflowRepository };
@@ -0,0 +1,303 @@
1
+ import {
2
+ createLogger,
3
+ init_logger
4
+ } from "./roy-agent-core-yn761yve.js";
5
+
6
+ // packages/core/src/env/mcp/tool/adapter.ts
7
+ init_logger();
8
+ import { z } from "zod";
9
+ var logger = createLogger("mcp-adapter");
10
+ function adaptMcpTool(mcpTool, mcpClient, serverName) {
11
+ const toolName = `mcp_${serverName}_${mcpTool.name}`;
12
+ const description = `MCP tool [${serverName}/${mcpTool.name}]: ${mcpTool.description ?? ""}`;
13
+ const paramsSchema = convertInputSchemaToZod(mcpTool.inputSchema);
14
+ return {
15
+ name: toolName,
16
+ description,
17
+ parameters: paramsSchema,
18
+ execute: async (args, _ctx) => {
19
+ try {
20
+ const result = await mcpClient.callTool({
21
+ name: mcpTool.name,
22
+ arguments: args ?? {}
23
+ });
24
+ return convertMcpCallResult(result);
25
+ } catch (error) {
26
+ logger.error(`[McpAdapter] Tool call failed: ${toolName}`, {
27
+ error: error instanceof Error ? error.message : String(error)
28
+ });
29
+ return {
30
+ success: false,
31
+ output: "",
32
+ error: error instanceof Error ? error.message : String(error)
33
+ };
34
+ }
35
+ },
36
+ metadata: {
37
+ category: "mcp",
38
+ tags: ["mcp", serverName],
39
+ mcpTool: {
40
+ originalName: mcpTool.name,
41
+ originalDescription: mcpTool.description || "",
42
+ inputSchema: mcpTool.inputSchema
43
+ }
44
+ }
45
+ };
46
+ }
47
+ function convertInputSchemaToZod(inputSchema) {
48
+ if (!inputSchema) {
49
+ return z.unknown();
50
+ }
51
+ const schema = inputSchema;
52
+ const properties = schema.properties ?? {};
53
+ const required = schema.required ?? [];
54
+ if (schema.anyOf || schema.oneOf) {
55
+ const unions = schema.anyOf || schema.oneOf || [];
56
+ const zodUnions = unions.map((s) => jsonSchemaToZod(s, false)).filter((t) => t);
57
+ if (zodUnions.length > 0) {
58
+ return z.union(zodUnions);
59
+ }
60
+ }
61
+ if (schema.allOf) {
62
+ const schemas = [];
63
+ for (const subSchema of schema.allOf) {
64
+ const sub = jsonSchemaToZod(subSchema, false);
65
+ schemas.push(sub);
66
+ }
67
+ if (schemas.length === 1) {
68
+ return schemas[0];
69
+ }
70
+ let combined = schemas[0];
71
+ for (let i = 1;i < schemas.length; i++) {
72
+ combined = z.intersection(combined, schemas[i]);
73
+ }
74
+ return combined;
75
+ }
76
+ const zodFields = {};
77
+ const fieldDescriptions = {};
78
+ for (const [key, prop] of Object.entries(properties)) {
79
+ const isRequired = required.includes(key);
80
+ const fieldResult = jsonSchemaToZod(prop, !isRequired);
81
+ const propSchema = prop;
82
+ if (propSchema.description) {
83
+ fieldDescriptions[key] = propSchema.description;
84
+ }
85
+ if (!isRequired) {
86
+ zodFields[key] = fieldResult.optional();
87
+ } else {
88
+ zodFields[key] = fieldResult;
89
+ }
90
+ }
91
+ if (Object.keys(zodFields).length === 0) {
92
+ return schema.description ? z.unknown().describe(schema.description) : z.unknown();
93
+ }
94
+ const result = z.object(zodFields);
95
+ if (schema.description) {
96
+ return result.describe(schema.description);
97
+ }
98
+ return result;
99
+ }
100
+ function jsonSchemaToZod(prop, isOptional) {
101
+ const description = prop.description;
102
+ let baseType;
103
+ if (prop.nullable === true) {
104
+ const innerType = jsonSchemaToZod({ ...prop, nullable: undefined }, isOptional);
105
+ baseType = z.union([innerType, z.null()]);
106
+ if (description) {
107
+ return baseType.describe(description);
108
+ }
109
+ return baseType;
110
+ }
111
+ if (prop.anyOf || prop.oneOf) {
112
+ const unions = prop.anyOf || prop.oneOf || [];
113
+ const zodUnions = unions.map((s) => jsonSchemaToZod(s, false)).filter((t) => t);
114
+ if (zodUnions.length > 0) {
115
+ baseType = z.union(zodUnions);
116
+ if (description) {
117
+ return baseType.describe(description);
118
+ }
119
+ return baseType;
120
+ }
121
+ }
122
+ if (prop.allOf) {
123
+ const schemas = [];
124
+ for (const subSchema of prop.allOf) {
125
+ const sub = jsonSchemaToZod(subSchema, false);
126
+ schemas.push(sub);
127
+ }
128
+ if (schemas.length === 1) {
129
+ baseType = schemas[0];
130
+ } else {
131
+ baseType = z.intersection(schemas[0], schemas[1]);
132
+ for (let i = 2;i < schemas.length; i++) {
133
+ baseType = z.intersection(baseType, schemas[i]);
134
+ }
135
+ }
136
+ if (description) {
137
+ return baseType.describe(description);
138
+ }
139
+ return baseType;
140
+ }
141
+ const types = prop.types || (prop.type ? [prop.type] : []);
142
+ if (types.length > 1) {
143
+ const zodTypes = types.map((t) => getZodTypeForBasicType(t, prop)).filter((t) => t);
144
+ if (zodTypes.length > 0) {
145
+ baseType = z.union(zodTypes);
146
+ if (description) {
147
+ return baseType.describe(description);
148
+ }
149
+ return baseType;
150
+ }
151
+ }
152
+ const type = types[0];
153
+ if (!type) {
154
+ if (prop.enum) {
155
+ return handleEnum(prop, description);
156
+ }
157
+ baseType = z.unknown();
158
+ if (description) {
159
+ return baseType.describe(description);
160
+ }
161
+ return baseType;
162
+ }
163
+ baseType = getZodTypeForBasicType(type, prop);
164
+ if (description) {
165
+ const schemaDescription = baseType._def?.description;
166
+ if (schemaDescription) {
167
+ return baseType.describe(`${schemaDescription}. ${description}`);
168
+ }
169
+ return baseType.describe(description);
170
+ }
171
+ return baseType;
172
+ }
173
+ function getZodTypeForBasicType(type, prop) {
174
+ switch (type) {
175
+ case "string": {
176
+ if (prop.enum && prop.enum.length > 0 && prop.enum.every((v) => typeof v === "string")) {
177
+ return handleEnum(prop, prop.description);
178
+ }
179
+ let schema = z.string();
180
+ if (typeof prop.minLength === "number") {
181
+ schema = schema.min(prop.minLength);
182
+ }
183
+ if (typeof prop.maxLength === "number") {
184
+ schema = schema.max(prop.maxLength);
185
+ }
186
+ if (prop.pattern) {
187
+ try {
188
+ schema = schema.regex(new RegExp(prop.pattern));
189
+ } catch {}
190
+ }
191
+ if (prop.format) {
192
+ schema = schema.describe(`Format: ${prop.format}`);
193
+ }
194
+ if (prop.default !== undefined) {
195
+ schema = schema.default(prop.default);
196
+ }
197
+ return schema;
198
+ }
199
+ case "number":
200
+ case "integer": {
201
+ let schema = type === "integer" ? z.number().int() : z.number();
202
+ if (typeof prop.minimum === "number") {
203
+ schema = schema.min(prop.minimum);
204
+ }
205
+ if (typeof prop.maximum === "number") {
206
+ schema = schema.max(prop.maximum);
207
+ }
208
+ if (prop.default !== undefined) {
209
+ schema = schema.default(prop.default);
210
+ }
211
+ return schema;
212
+ }
213
+ case "boolean": {
214
+ let schema = z.boolean();
215
+ if (prop.default !== undefined) {
216
+ schema = schema.default(prop.default);
217
+ }
218
+ return schema;
219
+ }
220
+ case "array": {
221
+ let itemSchema = z.unknown();
222
+ if (prop.items) {
223
+ itemSchema = jsonSchemaToZod(prop.items, false);
224
+ }
225
+ let schema = z.array(itemSchema);
226
+ if (prop.additionalProperties && typeof prop.additionalProperties !== "boolean") {
227
+ const additionalItems = jsonSchemaToZod(prop.additionalProperties, false);
228
+ schema = z.array(z.union([itemSchema, additionalItems]));
229
+ }
230
+ return schema;
231
+ }
232
+ case "object": {
233
+ if (prop.properties) {
234
+ const fields = {};
235
+ const required = prop.required ?? [];
236
+ for (const [key, value] of Object.entries(prop.properties)) {
237
+ const isRequired = required.includes(key);
238
+ fields[key] = isRequired ? jsonSchemaToZod(value, false) : jsonSchemaToZod(value, true).optional();
239
+ }
240
+ return z.object(fields);
241
+ }
242
+ if (prop.additionalProperties) {
243
+ if (typeof prop.additionalProperties === "boolean") {
244
+ return z.record(z.string(), z.unknown());
245
+ }
246
+ return z.record(z.string(), jsonSchemaToZod(prop.additionalProperties, false));
247
+ }
248
+ return z.record(z.string(), z.unknown());
249
+ }
250
+ case "null":
251
+ return z.null();
252
+ case "null | string":
253
+ case "string | null":
254
+ return z.union([z.string(), z.null()]);
255
+ case "null | number":
256
+ case "number | null":
257
+ return z.union([z.number(), z.null()]);
258
+ default:
259
+ if (prop.enum) {
260
+ return handleEnum(prop);
261
+ }
262
+ return z.unknown();
263
+ }
264
+ }
265
+ function handleEnum(prop, existingDescription) {
266
+ if (!prop.enum || prop.enum.length === 0) {
267
+ return z.unknown();
268
+ }
269
+ const stringValues = prop.enum.filter((v) => typeof v === "string");
270
+ if (stringValues.length === 0) {
271
+ return z.any();
272
+ }
273
+ let schema;
274
+ if (stringValues.length === 1) {
275
+ schema = z.literal(stringValues[0]);
276
+ } else {
277
+ schema = z.enum([stringValues[0], ...stringValues.slice(1)]);
278
+ }
279
+ const valuesList = stringValues.map((v) => `"${v}"`).join(", ");
280
+ const valuesDescription = `Valid values: ${valuesList}`;
281
+ if (existingDescription) {
282
+ return schema.describe(`${existingDescription}. ${valuesDescription}`);
283
+ }
284
+ return schema.describe(valuesDescription);
285
+ }
286
+ function convertMcpCallResult(result) {
287
+ const content = result.content;
288
+ const textContent = content?.filter((c) => c.type === "text")?.map((c) => c.text)?.join("") ?? "";
289
+ return {
290
+ success: !result.isError,
291
+ output: textContent,
292
+ error: result.isError ? textContent : undefined
293
+ };
294
+ }
295
+ function createMcpToolsDescription(tools) {
296
+ if (tools.length === 0) {
297
+ return " No MCP tools currently available.";
298
+ }
299
+ return tools.map((t) => ` - ${t.name}: ${t.description}`).join(`
300
+ `);
301
+ }
302
+
303
+ export { adaptMcpTool, convertInputSchemaToZod, createMcpToolsDescription };
@@ -0,0 +1,202 @@
1
+ // packages/core/src/env/hook/hook-manager.ts
2
+ class HookManager {
3
+ _hooks = new Map;
4
+ componentName;
5
+ componentVersion;
6
+ defaultPriority;
7
+ constructor(options = {}) {
8
+ this.componentName = options.componentName ?? "unknown";
9
+ this.componentVersion = options.componentVersion ?? "0.0.0";
10
+ this.defaultPriority = options.defaultPriority ?? 0;
11
+ }
12
+ register(hookPoint, hook) {
13
+ const hooks = this.getOrCreateHooks(hookPoint);
14
+ hooks.push(hook);
15
+ }
16
+ registerMany(hookPoint, hooks) {
17
+ const hookList = this.getOrCreateHooks(hookPoint);
18
+ hookList.push(...hooks);
19
+ }
20
+ unregister(hookPoint, name) {
21
+ const hooks = this._hooks.get(hookPoint);
22
+ if (!hooks)
23
+ return false;
24
+ const index = hooks.findIndex((h) => h.name === name);
25
+ if (index === -1)
26
+ return false;
27
+ hooks.splice(index, 1);
28
+ return true;
29
+ }
30
+ unregisterAll(hookPoint) {
31
+ this._hooks.delete(hookPoint);
32
+ }
33
+ async execute(hookPoint, data, metadata = {}) {
34
+ const hooks = this._hooks.get(hookPoint);
35
+ if (!hooks || hooks.length === 0)
36
+ return;
37
+ const sortedHooks = this.sortHooks(hooks);
38
+ const ctx = this.createContext(hookPoint, data, metadata, "before");
39
+ for (const hook of sortedHooks) {
40
+ await this.safeExecute(hook, ctx);
41
+ }
42
+ }
43
+ async executeAndCollect(hookPoint, data, metadata = {}) {
44
+ const hooks = this._hooks.get(hookPoint);
45
+ if (!hooks || hooks.length === 0)
46
+ return [];
47
+ const sortedHooks = this.sortHooks(hooks);
48
+ const ctx = this.createContext(hookPoint, data, metadata, "before");
49
+ const results = [];
50
+ for (const hook of sortedHooks) {
51
+ const result = await this.safeExecuteAndReturn(hook, ctx);
52
+ if (result !== undefined) {
53
+ results.push(result);
54
+ }
55
+ }
56
+ return results;
57
+ }
58
+ count(hookPoint) {
59
+ return this._hooks.get(hookPoint)?.length ?? 0;
60
+ }
61
+ clear() {
62
+ this._hooks.clear();
63
+ }
64
+ getHookPoints() {
65
+ return Array.from(this._hooks.keys());
66
+ }
67
+ hasHooks(hookPoint) {
68
+ return this.count(hookPoint) > 0;
69
+ }
70
+ setComponentInfo(name, version) {
71
+ this.componentName = name;
72
+ this.componentVersion = version;
73
+ }
74
+ get hooks() {
75
+ return this._hooks;
76
+ }
77
+ async executeWithIntervention(hookPoint, data, metadata = {}) {
78
+ const hooks = this._hooks.get(hookPoint);
79
+ if (!hooks || hooks.length === 0) {
80
+ return { stopped: false, results: [] };
81
+ }
82
+ const sortedHooks = this.sortHooks(hooks);
83
+ const ctx = this.createContext(hookPoint, data, metadata, "before");
84
+ const results = [];
85
+ let stopped = false;
86
+ let action;
87
+ for (const hook of sortedHooks) {
88
+ if (stopped)
89
+ break;
90
+ const result = await this.safeExecuteAndReturn(hook, ctx);
91
+ results.push(result);
92
+ if (result && typeof result === "object" && "stopped" in result) {
93
+ const hookResult = result;
94
+ if (hookResult.stopped) {
95
+ stopped = true;
96
+ action = hookResult.action;
97
+ }
98
+ }
99
+ }
100
+ return { stopped, action, results };
101
+ }
102
+ getOrCreateHooks(hookPoint) {
103
+ let hooks = this._hooks.get(hookPoint);
104
+ if (!hooks) {
105
+ hooks = [];
106
+ this._hooks.set(hookPoint, hooks);
107
+ }
108
+ return hooks;
109
+ }
110
+ sortHooks(hooks) {
111
+ return [...hooks].sort((a, b) => {
112
+ const priorityA = a.priority ?? this.defaultPriority;
113
+ const priorityB = b.priority ?? this.defaultPriority;
114
+ return priorityA - priorityB;
115
+ });
116
+ }
117
+ createContext(hookPoint, data, metadata, phase) {
118
+ return {
119
+ component: {
120
+ name: this.componentName,
121
+ version: this.componentVersion
122
+ },
123
+ data,
124
+ metadata,
125
+ phase,
126
+ hookPoint
127
+ };
128
+ }
129
+ async safeExecute(hook, ctx) {
130
+ try {
131
+ await hook.execute(ctx);
132
+ } catch (error) {
133
+ console.error(`Hook "${hook.name}" failed:`, error);
134
+ }
135
+ }
136
+ async safeExecuteAndReturn(hook, ctx) {
137
+ try {
138
+ return await hook.execute(ctx);
139
+ } catch (error) {
140
+ console.error(`Hook "${hook.name}" failed:`, error);
141
+ return;
142
+ }
143
+ }
144
+ }
145
+
146
+ // packages/core/src/env/hook/global-hook-manager.ts
147
+ var globalHookManager = new HookManager;
148
+ var AgentHookPoints = {
149
+ BEFORE_START: "agent:before.start",
150
+ BEFORE_LLM: "agent:before.llm",
151
+ AFTER_LLM: "agent:after.llm",
152
+ BEFORE_TOOL: "agent:before.tool",
153
+ AFTER_TOOL: "agent:after.tool",
154
+ ON_ITERATION: "agent:on.iteration",
155
+ ON_THRESHOLD: "agent:on.threshold",
156
+ AFTER_COMPLETE: "agent:after.complete",
157
+ ON_ERROR: "agent:on.error"
158
+ };
159
+ var LLMHookPoints = {
160
+ BEFORE_INVOKE: "llm:before.invoke",
161
+ AFTER_INVOKE: "llm:after.invoke",
162
+ ON_STREAM: "llm:on.stream"
163
+ };
164
+ var ToolHookPoints = {
165
+ BEFORE_EXECUTE: "tool:before.execute",
166
+ AFTER_EXECUTE: "tool:after.execute",
167
+ BEFORE_REGISTER: "tool:before.register",
168
+ AFTER_REGISTER: "tool:after.register",
169
+ ON_ERROR: "tool:on.error"
170
+ };
171
+ var hookPointAliases = {
172
+ "before.tool": "tool:before.execute",
173
+ "after.tool": "tool:after.execute",
174
+ "llm.before-invoke": "llm:before.invoke",
175
+ "llm.after-invoke": "llm:after.invoke",
176
+ "llm.stream": "llm:on.stream"
177
+ };
178
+ function setupAliasHooks() {
179
+ const originalRegister = globalHookManager.register.bind(globalHookManager);
180
+ globalHookManager.register = function(hookPoint, hook) {
181
+ originalRegister(hookPoint, hook);
182
+ const alias = hookPointAliases[hookPoint];
183
+ if (alias) {
184
+ originalRegister(alias, hook);
185
+ }
186
+ };
187
+ }
188
+ setupAliasHooks();
189
+ async function executeAgentHook(hookPoint, data, metadata = {}) {
190
+ await globalHookManager.execute(hookPoint, data, metadata);
191
+ }
192
+ async function executeAgentHookWithIntervention(hookPoint, data, metadata = {}) {
193
+ return globalHookManager.executeWithIntervention(hookPoint, data, metadata);
194
+ }
195
+ async function executeLLMHook(hookPoint, data, metadata = {}) {
196
+ await globalHookManager.execute(hookPoint, data, metadata);
197
+ }
198
+ async function executeToolHook(hookPoint, data, metadata = {}) {
199
+ await globalHookManager.execute(hookPoint, data, metadata);
200
+ }
201
+
202
+ export { HookManager, globalHookManager, AgentHookPoints, LLMHookPoints, ToolHookPoints, executeAgentHook, executeAgentHookWithIntervention, executeLLMHook, executeToolHook };