@ai-setting/roy-agent-core 1.4.13 → 1.4.15
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/config/index.js +32 -0
- package/dist/env/agent/index.js +24 -0
- package/dist/env/commands/index.js +14 -0
- package/dist/env/debug/formatters/index.js +11 -0
- package/dist/env/debug/index.js +26 -0
- package/dist/env/hook/index.js +29 -0
- package/dist/env/index.js +81 -0
- package/dist/env/llm/index.js +40 -0
- package/dist/env/log-trace/index.js +83 -0
- package/dist/env/mcp/index.js +39 -0
- package/dist/env/mcp/tool/index.js +14 -0
- package/dist/env/memory/built-in/index.js +11 -0
- package/dist/env/memory/index.js +56 -0
- package/dist/env/memory/plugin/index.js +36 -0
- package/dist/env/prompt/index.js +20 -0
- package/dist/env/session/index.js +25 -0
- package/dist/env/session/storage/index.js +18 -0
- package/dist/env/skill/index.js +34 -0
- package/dist/env/skill/tool/index.js +9 -0
- package/dist/env/task/delegate/index.js +18 -0
- package/dist/env/task/hooks/index.js +7 -0
- package/dist/env/task/index.js +30 -0
- package/dist/env/task/plugins/index.js +23 -0
- package/dist/env/task/storage/index.js +14 -0
- package/dist/env/task/tools/index.js +17 -0
- package/dist/env/task/tools/operation/index.js +15 -0
- package/dist/{shared/chunk-1d4rwms4.js → env/tool/built-in/index.js} +4 -4
- package/dist/env/tool/index.js +39 -0
- package/dist/env/workflow/decorators/index.js +27 -0
- package/dist/env/workflow/engine/index.js +28 -0
- package/dist/env/workflow/index.js +132 -0
- package/dist/env/workflow/nodes/index.js +19 -0
- package/dist/env/workflow/service/index.js +13 -0
- package/dist/env/workflow/storage/index.js +27 -0
- package/dist/env/workflow/tools/index.js +159 -0
- package/dist/env/workflow/types/index.js +94 -0
- package/dist/env/workflow/utils/index.js +637 -0
- package/dist/index.js +233 -16386
- package/dist/shared/{chunk-2b5kbhx3.js → @ai-setting/roy-agent-core-0rtxwr28.js} +6 -114
- package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +36 -0
- package/dist/shared/@ai-setting/roy-agent-core-12zkpda2.js +393 -0
- package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +117 -0
- package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +11 -0
- package/dist/shared/@ai-setting/roy-agent-core-2kg2wma8.js +620 -0
- package/dist/shared/@ai-setting/roy-agent-core-2x0m2p66.js +851 -0
- package/dist/shared/@ai-setting/roy-agent-core-35x0wrtt.js +172 -0
- package/dist/shared/{chunk-1pf5mfgd.js → @ai-setting/roy-agent-core-37e4tep3.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-3agad0d9.js +603 -0
- package/dist/shared/@ai-setting/roy-agent-core-4arba14a.js +419 -0
- package/dist/shared/@ai-setting/roy-agent-core-4rqmfr7t.js +266 -0
- package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +206 -0
- package/dist/shared/@ai-setting/roy-agent-core-561b1c4p.js +377 -0
- package/dist/shared/@ai-setting/roy-agent-core-5xf65pz6.js +1305 -0
- package/dist/shared/@ai-setting/roy-agent-core-6a72jfdy.js +303 -0
- package/dist/shared/{chunk-1aakcfp1.js → @ai-setting/roy-agent-core-7f303ffd.js} +3 -3
- package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +284 -0
- package/dist/shared/{chunk-t1rh6jtm.js → @ai-setting/roy-agent-core-7n436rb4.js} +7 -12
- package/dist/shared/@ai-setting/roy-agent-core-7r85t0qn.js +492 -0
- package/dist/shared/@ai-setting/roy-agent-core-7rewcey6.js +862 -0
- package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-9qwp5qkz.js +1387 -0
- package/dist/shared/{chunk-mf5xqbdh.js → @ai-setting/roy-agent-core-9yxb3ty9.js} +3 -2
- package/dist/shared/@ai-setting/roy-agent-core-anwsxdds.js +1205 -0
- package/dist/shared/{chunk-1qwabsm0.js → @ai-setting/roy-agent-core-bncgx3gb.js} +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-cd00w5mb.js +762 -0
- package/dist/shared/@ai-setting/roy-agent-core-cgs0j60t.js +442 -0
- package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +93 -0
- package/dist/shared/@ai-setting/roy-agent-core-dbsk841j.js +286 -0
- package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +64 -0
- package/dist/shared/{chunk-yqmx37vm.js → @ai-setting/roy-agent-core-e5jcp24a.js} +2 -2
- package/dist/shared/@ai-setting/roy-agent-core-e62e2a5a.js +204 -0
- package/dist/shared/{chunk-g6j5n3gv.js → @ai-setting/roy-agent-core-ewrj1c4k.js} +2 -2
- package/dist/shared/{chunk-rncy3rtd.js → @ai-setting/roy-agent-core-fdb6m4e4.js} +119 -1113
- package/dist/shared/{chunk-q9j99fsm.js → @ai-setting/roy-agent-core-fv32jaa8.js} +3 -3
- package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +171 -0
- package/dist/shared/@ai-setting/roy-agent-core-gmnkza34.js +202 -0
- package/dist/shared/@ai-setting/roy-agent-core-hz7rr4yx.js +513 -0
- package/dist/shared/@ai-setting/roy-agent-core-j3bbr2n0.js +378 -0
- package/dist/shared/{chunk-a9qmy3sc.js → @ai-setting/roy-agent-core-j3wc4465.js} +6 -3
- package/dist/shared/@ai-setting/roy-agent-core-jj79gszx.js +1130 -0
- package/dist/shared/@ai-setting/roy-agent-core-mwwk6req.js +913 -0
- package/dist/shared/{chunk-0q6s9wm6.js → @ai-setting/roy-agent-core-pc9g3962.js} +6 -50
- package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +60 -0
- package/dist/shared/@ai-setting/roy-agent-core-pzsg9pvf.js +393 -0
- package/dist/shared/{chunk-91bas8w5.js → @ai-setting/roy-agent-core-q779wnwm.js} +5 -5
- package/dist/shared/{chunk-25x2pdtp.js → @ai-setting/roy-agent-core-qw0ebh1d.js} +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +57 -0
- package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +66 -0
- package/dist/shared/@ai-setting/roy-agent-core-qya7seh6.js +408 -0
- package/dist/shared/@ai-setting/roy-agent-core-rbetrphj.js +97 -0
- package/dist/shared/@ai-setting/roy-agent-core-re1wjfw7.js +587 -0
- package/dist/shared/@ai-setting/roy-agent-core-rft3fmp0.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +584 -0
- package/dist/shared/{chunk-ze20rksg.js → @ai-setting/roy-agent-core-rvxg1wps.js} +1 -1
- package/dist/shared/@ai-setting/roy-agent-core-rzp9kxne.js +341 -0
- package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +213 -0
- package/dist/shared/@ai-setting/roy-agent-core-w78syn7w.js +788 -0
- package/dist/shared/{chunk-9qzt1v1p.js → @ai-setting/roy-agent-core-z2t8hse8.js} +3 -2
- package/package.json +8 -7
- package/dist/index.d.ts +0 -7825
- package/dist/shared/chunk-hs7tbmje.js +0 -24
- /package/dist/shared/{chunk-wbkh7wat.js → @ai-setting/roy-agent-core-fs0mn2jk.js} +0 -0
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
import {
|
|
2
2
|
SQLiteSpanStorage,
|
|
3
3
|
init_span_storage
|
|
4
|
-
} from "./
|
|
4
|
+
} from "./roy-agent-core-bncgx3gb.js";
|
|
5
5
|
import {
|
|
6
6
|
__esm,
|
|
7
7
|
__export,
|
|
8
8
|
__require
|
|
9
|
-
} from "./
|
|
9
|
+
} from "./roy-agent-core-fs0mn2jk.js";
|
|
10
10
|
|
|
11
11
|
// src/env/log-trace/opentelemetry/propagation.ts
|
|
12
12
|
function padLeft(str, length) {
|
|
@@ -365,4 +365,4 @@ var init_tracer_provider = __esm(() => {
|
|
|
365
365
|
init_types();
|
|
366
366
|
});
|
|
367
367
|
|
|
368
|
-
export { propagation, init_propagation, SpanKind, SpanStatus, init_types, OTelTracerProvider, getTracerProvider, resetTracerProvider, exports_tracer_provider, init_tracer_provider };
|
|
368
|
+
export { serialize, parse, propagation, init_propagation, SpanKind, SpanStatus, init_types, OTelTracerProvider, getTracerProvider, resetTracerProvider, exports_tracer_provider, init_tracer_provider };
|
|
@@ -0,0 +1,171 @@
|
|
|
1
|
+
// src/env/memory/built-in/record-memory.ts
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
var RecordMemorySchema = z.object({
|
|
4
|
+
scope: z.enum(["project", "global"]).describe("作用域: project/global"),
|
|
5
|
+
mode: z.enum(["append", "prepend", "overwrite", "delete"]).describe("模式: append(追加)/prepend(插入)/overwrite(覆盖)/delete(删除)"),
|
|
6
|
+
content: z.string().optional().describe("记忆内容"),
|
|
7
|
+
title: z.string().optional().describe("章节标题")
|
|
8
|
+
});
|
|
9
|
+
function createRecordMemoryTool(recordMemory, getMemoryPaths, getMemoryFile) {
|
|
10
|
+
return {
|
|
11
|
+
name: "record_memory",
|
|
12
|
+
description: buildDescription(getMemoryPaths, getMemoryFile),
|
|
13
|
+
parameters: RecordMemorySchema,
|
|
14
|
+
execute: async (args, _ctx) => {
|
|
15
|
+
const params = RecordMemorySchema.parse(args);
|
|
16
|
+
const startTime = Date.now();
|
|
17
|
+
try {
|
|
18
|
+
const result = await recordMemory({
|
|
19
|
+
scope: params.scope,
|
|
20
|
+
mode: params.mode,
|
|
21
|
+
content: params.content,
|
|
22
|
+
title: params.title
|
|
23
|
+
});
|
|
24
|
+
if (!result) {
|
|
25
|
+
return {
|
|
26
|
+
success: false,
|
|
27
|
+
output: "",
|
|
28
|
+
error: "未配置 memory 路径,无法记录记忆",
|
|
29
|
+
metadata: { execution_time_ms: Date.now() - startTime }
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
const actionMessages = {
|
|
33
|
+
created: "已创建记忆文件",
|
|
34
|
+
overwrite: "已覆盖记忆文件",
|
|
35
|
+
append: "已追加内容到记忆文件",
|
|
36
|
+
prepend: "已插入内容到记忆文件开头",
|
|
37
|
+
delete: "已删除记忆文件"
|
|
38
|
+
};
|
|
39
|
+
return {
|
|
40
|
+
success: true,
|
|
41
|
+
output: `${actionMessages[result.action]}: ${result.path}`,
|
|
42
|
+
metadata: {
|
|
43
|
+
execution_time_ms: Date.now() - startTime,
|
|
44
|
+
path: result.path,
|
|
45
|
+
action: result.action
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
} catch (error) {
|
|
49
|
+
return {
|
|
50
|
+
success: false,
|
|
51
|
+
output: "",
|
|
52
|
+
error: error instanceof Error ? error.message : String(error),
|
|
53
|
+
metadata: { execution_time_ms: Date.now() - startTime }
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
function buildDescription(getMemoryPaths, getMemoryFile) {
|
|
60
|
+
const paths = getMemoryPaths();
|
|
61
|
+
const memoryFile = getMemoryFile();
|
|
62
|
+
const lines = [
|
|
63
|
+
`# Record Memory Tool`,
|
|
64
|
+
"",
|
|
65
|
+
`Record memory to file. Writes to the memory.md file in the specified scope.`,
|
|
66
|
+
"",
|
|
67
|
+
`## Modes`,
|
|
68
|
+
"",
|
|
69
|
+
"| Mode | Description |",
|
|
70
|
+
"|------|-------------|",
|
|
71
|
+
"| **append** | 追加内容到 ${memoryFile} 尾部 |",
|
|
72
|
+
"| **prepend** | 插入内容到 ${memoryFile} 开头 |",
|
|
73
|
+
"| **overwrite** | 覆盖 ${memoryFile} |",
|
|
74
|
+
"| **delete** | 删除 ${memoryFile} |",
|
|
75
|
+
"",
|
|
76
|
+
`## Parameters`,
|
|
77
|
+
"",
|
|
78
|
+
"- **scope** (required): 作用域 - project 或 global",
|
|
79
|
+
"- **mode** (required): 操作模式",
|
|
80
|
+
"- **content** (optional): 记忆内容",
|
|
81
|
+
"- **title** (optional): 章节标题",
|
|
82
|
+
"",
|
|
83
|
+
`## Memory Paths`,
|
|
84
|
+
""
|
|
85
|
+
];
|
|
86
|
+
if (paths.length === 0) {
|
|
87
|
+
lines.push("No memory paths configured.");
|
|
88
|
+
} else {
|
|
89
|
+
lines.push("Files will be written to:");
|
|
90
|
+
for (const p of paths) {
|
|
91
|
+
lines.push(`- **[${p.type}]** ${p.path}/${memoryFile}`);
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return lines.join(`
|
|
95
|
+
`);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// src/env/memory/built-in/recall-memory.ts
|
|
99
|
+
import { z as z2 } from "zod";
|
|
100
|
+
var RecallMemorySchema = z2.object({
|
|
101
|
+
scope: z2.enum(["project", "global"]).optional().describe("作用域(可选,不传则读取所有)")
|
|
102
|
+
});
|
|
103
|
+
function createRecallMemoryTool(recallMemory, getMemoryPaths, getMemoryFile) {
|
|
104
|
+
return {
|
|
105
|
+
name: "recall_memory",
|
|
106
|
+
description: buildDescription2(getMemoryPaths, getMemoryFile),
|
|
107
|
+
parameters: RecallMemorySchema,
|
|
108
|
+
execute: async (args, _ctx) => {
|
|
109
|
+
const params = RecallMemorySchema.parse(args);
|
|
110
|
+
const startTime = Date.now();
|
|
111
|
+
try {
|
|
112
|
+
const content = await recallMemory(params.scope);
|
|
113
|
+
if (!content) {
|
|
114
|
+
return {
|
|
115
|
+
success: true,
|
|
116
|
+
output: "(No memory files found)",
|
|
117
|
+
metadata: { execution_time_ms: Date.now() - startTime }
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
return {
|
|
121
|
+
success: true,
|
|
122
|
+
output: content,
|
|
123
|
+
metadata: { execution_time_ms: Date.now() - startTime }
|
|
124
|
+
};
|
|
125
|
+
} catch (error) {
|
|
126
|
+
return {
|
|
127
|
+
success: false,
|
|
128
|
+
output: "",
|
|
129
|
+
error: error instanceof Error ? error.message : String(error),
|
|
130
|
+
metadata: { execution_time_ms: Date.now() - startTime }
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
function buildDescription2(getMemoryPaths, getMemoryFile) {
|
|
137
|
+
const paths = getMemoryPaths();
|
|
138
|
+
const memoryFile = getMemoryFile();
|
|
139
|
+
const lines = [
|
|
140
|
+
`# Recall Memory Tool`,
|
|
141
|
+
"",
|
|
142
|
+
`Recall memory from memory files. Reads from the memory.md file in the specified scope.`,
|
|
143
|
+
"",
|
|
144
|
+
`## Parameters`,
|
|
145
|
+
"",
|
|
146
|
+
"- **scope** (optional): 作用域 - project 或 global(不传则读取所有)",
|
|
147
|
+
"",
|
|
148
|
+
`## Memory Files`,
|
|
149
|
+
""
|
|
150
|
+
];
|
|
151
|
+
if (paths.length === 0) {
|
|
152
|
+
lines.push("No memory paths configured.");
|
|
153
|
+
} else {
|
|
154
|
+
lines.push("Files will be read from:");
|
|
155
|
+
for (const p of paths) {
|
|
156
|
+
lines.push(`- **[${p.type}]** ${p.path}/${memoryFile}`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
return lines.join(`
|
|
160
|
+
`);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// src/env/memory/built-in/index.ts
|
|
164
|
+
function createMemoryTools(recordMemory, recallMemory, config) {
|
|
165
|
+
return [
|
|
166
|
+
createRecordMemoryTool(recordMemory, config.getMemoryPaths, config.getMemoryFile),
|
|
167
|
+
createRecallMemoryTool(recallMemory, config.getMemoryPaths, config.getMemoryFile)
|
|
168
|
+
];
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export { RecordMemorySchema, RecallMemorySchema, createMemoryTools };
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// 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
|
+
// 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 };
|