@ai-setting/roy-agent-core 1.5.15-test → 1.5.16-test

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 (141) hide show
  1. package/dist/config/index.d.ts +1250 -0
  2. package/dist/config/index.js +32 -0
  3. package/dist/env/agent/index.d.ts +2279 -0
  4. package/dist/env/agent/index.js +24 -0
  5. package/dist/env/commands/index.d.ts +1131 -0
  6. package/dist/env/commands/index.js +14 -0
  7. package/dist/env/debug/formatters/index.d.ts +236 -0
  8. package/dist/env/debug/formatters/index.js +11 -0
  9. package/dist/env/debug/index.d.ts +1652 -0
  10. package/dist/env/debug/index.js +26 -0
  11. package/dist/env/hook/index.d.ts +279 -0
  12. package/dist/env/hook/index.js +29 -0
  13. package/dist/env/index.d.ts +3481 -0
  14. package/dist/env/index.js +82 -0
  15. package/dist/env/llm/index.d.ts +1760 -0
  16. package/dist/env/llm/index.js +40 -0
  17. package/dist/env/log-trace/index.d.ts +1574 -0
  18. package/dist/env/log-trace/index.js +83 -0
  19. package/dist/env/mcp/index.d.ts +1331 -0
  20. package/dist/env/mcp/index.js +39 -0
  21. package/dist/env/mcp/tool/index.d.ts +183 -0
  22. package/dist/env/mcp/tool/index.js +14 -0
  23. package/dist/env/memory/built-in/index.d.ts +232 -0
  24. package/dist/env/memory/built-in/index.js +11 -0
  25. package/dist/env/memory/index.d.ts +1799 -0
  26. package/dist/env/memory/index.js +56 -0
  27. package/dist/env/memory/plugin/index.d.ts +747 -0
  28. package/dist/env/memory/plugin/index.js +36 -0
  29. package/dist/env/prompt/index.d.ts +1164 -0
  30. package/dist/env/prompt/index.js +20 -0
  31. package/dist/env/session/index.d.ts +1908 -0
  32. package/dist/env/session/index.js +25 -0
  33. package/dist/env/session/storage/index.d.ts +564 -0
  34. package/dist/env/session/storage/index.js +18 -0
  35. package/dist/env/skill/index.d.ts +1266 -0
  36. package/dist/env/skill/index.js +34 -0
  37. package/dist/env/skill/tool/index.d.ts +193 -0
  38. package/dist/env/skill/tool/index.js +9 -0
  39. package/dist/env/task/delegate/index.d.ts +1612 -0
  40. package/dist/env/task/delegate/index.js +18 -0
  41. package/dist/env/task/events/index.d.ts +171 -0
  42. package/dist/env/task/events/index.js +7 -0
  43. package/dist/env/task/hooks/index.d.ts +624 -0
  44. package/dist/env/task/hooks/index.js +7 -0
  45. package/dist/env/task/index.d.ts +1553 -0
  46. package/dist/env/task/index.js +34 -0
  47. package/dist/env/task/plugins/index.d.ts +466 -0
  48. package/dist/env/task/plugins/index.js +23 -0
  49. package/dist/env/task/storage/index.d.ts +241 -0
  50. package/dist/env/task/storage/index.js +14 -0
  51. package/dist/env/task/tools/index.d.ts +1485 -0
  52. package/dist/env/task/tools/index.js +17 -0
  53. package/dist/env/task/tools/operation/index.d.ts +1484 -0
  54. package/dist/env/task/tools/operation/index.js +15 -0
  55. package/dist/env/tool/built-in/index.d.ts +218 -0
  56. package/dist/env/tool/built-in/index.js +25 -0
  57. package/dist/env/tool/index.d.ts +1396 -0
  58. package/dist/env/tool/index.js +39 -0
  59. package/dist/env/workflow/decorators/index.d.ts +2161 -0
  60. package/dist/env/workflow/decorators/index.js +27 -0
  61. package/dist/env/workflow/engine/index.d.ts +3453 -0
  62. package/dist/env/workflow/engine/index.js +28 -0
  63. package/dist/env/workflow/index.d.ts +3546 -0
  64. package/dist/env/workflow/index.js +136 -0
  65. package/dist/env/workflow/nodes/index.d.ts +2092 -0
  66. package/dist/env/workflow/nodes/index.js +19 -0
  67. package/dist/env/workflow/service/index.d.ts +227 -0
  68. package/dist/env/workflow/service/index.js +13 -0
  69. package/dist/env/workflow/storage/index.d.ts +165 -0
  70. package/dist/env/workflow/storage/index.js +27 -0
  71. package/dist/env/workflow/tools/index.d.ts +416 -0
  72. package/dist/env/workflow/tools/index.js +159 -0
  73. package/dist/env/workflow/types/index.d.ts +2255 -0
  74. package/dist/env/workflow/types/index.js +98 -0
  75. package/dist/env/workflow/utils/index.d.ts +2031 -0
  76. package/dist/env/workflow/utils/index.js +637 -0
  77. package/dist/index.d.ts +7858 -0
  78. package/dist/index.js +399 -0
  79. package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +258 -0
  80. package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +36 -0
  81. package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +349 -0
  82. package/dist/shared/@ai-setting/roy-agent-core-1ce3fqrk.js +117 -0
  83. package/dist/shared/@ai-setting/roy-agent-core-2dhd60aw.js +11 -0
  84. package/dist/shared/@ai-setting/roy-agent-core-3jywqmdd.js +393 -0
  85. package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +200 -0
  86. package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +299 -0
  87. package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +206 -0
  88. package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +393 -0
  89. package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +350 -0
  90. package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +180 -0
  91. package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +408 -0
  92. package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +284 -0
  93. package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +762 -0
  94. package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +10 -0
  95. package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +14 -0
  96. package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +922 -0
  97. package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +15 -0
  98. package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +1130 -0
  99. package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +14 -0
  100. package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +653 -0
  101. package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +93 -0
  102. package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +872 -0
  103. package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +11 -0
  104. package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +64 -0
  105. package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +378 -0
  106. package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +492 -0
  107. package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +52 -0
  108. package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +171 -0
  109. package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +862 -0
  110. package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +1387 -0
  111. package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +208 -0
  112. package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +139 -0
  113. package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +534 -0
  114. package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +204 -0
  115. package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +603 -0
  116. package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +97 -0
  117. package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +587 -0
  118. package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +176 -0
  119. package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +60 -0
  120. package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +442 -0
  121. package/dist/shared/@ai-setting/roy-agent-core-qxhq8ven.js +57 -0
  122. package/dist/shared/@ai-setting/roy-agent-core-qxnbvgwe.js +66 -0
  123. package/dist/shared/@ai-setting/roy-agent-core-r9ezzemr.js +10 -0
  124. package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +267 -0
  125. package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +584 -0
  126. package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +102 -0
  127. package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +549 -0
  128. package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +15 -0
  129. package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +213 -0
  130. package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +812 -0
  131. package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +1316 -0
  132. package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +1205 -0
  133. package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +328 -0
  134. package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +20 -0
  135. package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +2098 -0
  136. package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +419 -0
  137. package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +368 -0
  138. package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +377 -0
  139. package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +172 -0
  140. package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +103 -0
  141. package/package.json +1 -1
@@ -0,0 +1,492 @@
1
+ import {
2
+ envKeyToConfigKey,
3
+ toEnvKey
4
+ } from "./roy-agent-core-qxhq8ven.js";
5
+ import {
6
+ BaseComponent
7
+ } from "./roy-agent-core-kkbwepqb.js";
8
+ import {
9
+ createLogger,
10
+ init_logger
11
+ } from "./roy-agent-core-44hnfb02.js";
12
+
13
+ // src/env/commands/commands-component.ts
14
+ import path2 from "path";
15
+ import os from "os";
16
+ init_logger();
17
+ import * as fs2 from "fs/promises";
18
+ import { exec } from "child_process";
19
+ import { promisify } from "util";
20
+
21
+ // src/env/commands/parser.ts
22
+ init_logger();
23
+ import fs from "fs/promises";
24
+ import path from "path";
25
+ var logger = createLogger("commands-parser");
26
+ function parseFrontmatter(text) {
27
+ const result = {};
28
+ const lines = text.split(`
29
+ `);
30
+ for (const line of lines) {
31
+ const match = line.match(/^(\w+):\s*"?(.+?)"?\s*$/);
32
+ if (match) {
33
+ const key = match[1];
34
+ let value = match[2];
35
+ if (value.startsWith('"') && value.endsWith('"')) {
36
+ value = value.slice(1, -1);
37
+ }
38
+ result[key] = value;
39
+ }
40
+ }
41
+ return result;
42
+ }
43
+ async function parseMetaFile(filePath, source) {
44
+ try {
45
+ const content = await fs.readFile(filePath, "utf-8");
46
+ const frontmatterMatch = content.match(/^---\n([\s\S]*?)\n---\n?/);
47
+ if (!frontmatterMatch) {
48
+ logger.warn(`[Parser] Missing frontmatter in: ${filePath}`);
49
+ return null;
50
+ }
51
+ const frontmatter = parseFrontmatter(frontmatterMatch[1]);
52
+ const name = frontmatter.name;
53
+ const description = frontmatter.description;
54
+ if (!name) {
55
+ logger.warn(`[Parser] Missing name in frontmatter: ${filePath}`);
56
+ return null;
57
+ }
58
+ if (!description) {
59
+ logger.warn(`[Parser] Missing description in frontmatter: ${filePath}`);
60
+ return null;
61
+ }
62
+ return {
63
+ name,
64
+ description,
65
+ tips: frontmatter.tips,
66
+ filePath,
67
+ source
68
+ };
69
+ } catch (error) {
70
+ logger.error(`[Parser] Failed to parse file: ${filePath}`, error);
71
+ return null;
72
+ }
73
+ }
74
+
75
+ // src/env/commands/commands-config-registration.ts
76
+ var COMMANDS_CONFIG_REGISTRATION = {
77
+ name: "command",
78
+ sources: [
79
+ { type: "env", envPrefix: "COMMAND", priority: 20, watch: false }
80
+ ],
81
+ keys: [
82
+ { key: "command.userCommandsDir", sources: ["env", "file"] },
83
+ { key: "command.projectCommandsDir", sources: ["env", "file"] },
84
+ { key: "command.cacheDir", sources: ["env", "file"] }
85
+ ]
86
+ };
87
+
88
+ // src/env/commands/commands-component.ts
89
+ var execAsync = promisify(exec);
90
+ var logger2 = createLogger("commands");
91
+ var DEFAULT_USER_DIR = ".roy-agent/commands";
92
+ var DEFAULT_PROJECT_DIR = ".roy/commands";
93
+ var DEFAULT_CACHE_DIR = ".cache/roy-agent/commands";
94
+ var DESCRIPTIONS_FILE = "descriptions.json";
95
+ var CONFIG_KEYS = [
96
+ "command.userCommandsDir",
97
+ "command.projectCommandsDir",
98
+ "command.cacheDir"
99
+ ];
100
+
101
+ class CommandsComponent extends BaseComponent {
102
+ name = "commands";
103
+ version = "2.0.0";
104
+ config;
105
+ configComponent;
106
+ userCommandsDir = "";
107
+ projectCommandsDir = "";
108
+ descriptionCache = new Map;
109
+ descriptionCacheFile = "";
110
+ constructor() {
111
+ super();
112
+ this.initDefaultDirs();
113
+ }
114
+ initDefaultDirs() {
115
+ const homeDir = os.homedir();
116
+ this.userCommandsDir = path2.join(homeDir, DEFAULT_USER_DIR);
117
+ this.projectCommandsDir = DEFAULT_PROJECT_DIR;
118
+ this.descriptionCacheFile = path2.join(homeDir, DEFAULT_CACHE_DIR, DESCRIPTIONS_FILE);
119
+ }
120
+ async init(config) {
121
+ await super.init(config);
122
+ const options = config?.options;
123
+ if (!options?.configComponent) {
124
+ throw new Error("ConfigComponent is required for CommandsComponent initialization");
125
+ }
126
+ this.configComponent = options.configComponent;
127
+ await this.registerConfig(options);
128
+ await fs2.mkdir(this.userCommandsDir, { recursive: true });
129
+ await this.loadDescriptionCache();
130
+ logger2.info(`CommandsComponent initialized: user=${this.userCommandsDir}, project=${this.projectCommandsDir}`);
131
+ }
132
+ async registerConfig(options) {
133
+ const configComponent = this.configComponent;
134
+ if (!configComponent)
135
+ return;
136
+ const { configPath, envPrefix, config } = options;
137
+ configComponent.registerComponent(COMMANDS_CONFIG_REGISTRATION);
138
+ if (configPath) {
139
+ configComponent.registerSource({
140
+ type: "file",
141
+ relativePath: configPath,
142
+ optional: true,
143
+ watch: false
144
+ });
145
+ }
146
+ await configComponent.load("command");
147
+ const prefix = envPrefix !== undefined ? envPrefix : "COMMAND";
148
+ for (const key of CONFIG_KEYS) {
149
+ const envKey = toEnvKey(key, prefix);
150
+ const value = process.env[envKey];
151
+ if (value !== undefined) {
152
+ await configComponent.set(key, value);
153
+ }
154
+ }
155
+ const loadedKeys = new Set(CONFIG_KEYS);
156
+ for (const envKey of Object.keys(process.env)) {
157
+ const configKey = envKeyToConfigKey(envKey, prefix, "command");
158
+ if (!configKey)
159
+ continue;
160
+ if (loadedKeys.has(configKey))
161
+ continue;
162
+ loadedKeys.add(configKey);
163
+ const value = process.env[envKey];
164
+ if (value !== undefined) {
165
+ await configComponent.set(configKey, value);
166
+ }
167
+ }
168
+ if (config) {
169
+ const flatConfig = this.flattenConfig(config);
170
+ for (const [key, value] of Object.entries(flatConfig)) {
171
+ if (value !== undefined) {
172
+ await configComponent.set(key, value);
173
+ }
174
+ }
175
+ }
176
+ const homeDir = os.homedir();
177
+ const defaultDirs = {
178
+ "command.userCommandsDir": path2.join(homeDir, DEFAULT_USER_DIR),
179
+ "command.projectCommandsDir": DEFAULT_PROJECT_DIR,
180
+ "command.cacheDir": path2.join(homeDir, DEFAULT_CACHE_DIR)
181
+ };
182
+ for (const [key, value] of Object.entries(defaultDirs)) {
183
+ if (configComponent.get(key) === undefined) {
184
+ await configComponent.set(key, value);
185
+ }
186
+ }
187
+ this.applyConfig(configComponent);
188
+ }
189
+ flattenConfig(config) {
190
+ const result = {};
191
+ if (config.userCommandsDir !== undefined) {
192
+ result["command.userCommandsDir"] = config.userCommandsDir;
193
+ }
194
+ if (config.projectCommandsDir !== undefined) {
195
+ result["command.projectCommandsDir"] = config.projectCommandsDir;
196
+ }
197
+ if (config.cacheDir !== undefined) {
198
+ result["command.cacheDir"] = config.cacheDir;
199
+ }
200
+ return result;
201
+ }
202
+ applyConfig(configComponent) {
203
+ const userDir = configComponent.get("command.userCommandsDir");
204
+ const projectDir = configComponent.get("command.projectCommandsDir");
205
+ const cacheDir = configComponent.get("command.cacheDir");
206
+ this.userCommandsDir = userDir || path2.join(os.homedir(), DEFAULT_USER_DIR);
207
+ this.projectCommandsDir = projectDir || DEFAULT_PROJECT_DIR;
208
+ this.descriptionCacheFile = path2.join(cacheDir || path2.join(os.homedir(), DEFAULT_CACHE_DIR), DESCRIPTIONS_FILE);
209
+ this.config = {
210
+ userCommandsDir: this.userCommandsDir,
211
+ projectCommandsDir: this.projectCommandsDir,
212
+ cacheDir
213
+ };
214
+ }
215
+ async start() {
216
+ await super.start();
217
+ logger2.debug("CommandsComponent started");
218
+ }
219
+ async stop() {
220
+ await this.saveDescriptionCache();
221
+ await super.stop();
222
+ logger2.debug("CommandsComponent stopped");
223
+ }
224
+ async getCommandDirs() {
225
+ return {
226
+ user: this.userCommandsDir,
227
+ project: this.projectCommandsDir
228
+ };
229
+ }
230
+ async discover(options) {
231
+ const commands = [];
232
+ let userCount = 0;
233
+ let projectCount = 0;
234
+ try {
235
+ const projectCommands = await this.scanDir(this.projectCommandsDir, "project", options?.pattern);
236
+ commands.push(...projectCommands);
237
+ projectCount = projectCommands.length;
238
+ } catch (error) {
239
+ logger2.debug(`Failed to scan project dir: ${error}`);
240
+ }
241
+ try {
242
+ const userCommands = await this.scanDir(this.userCommandsDir, "user", options?.pattern);
243
+ for (const cmd of userCommands) {
244
+ const exists = commands.find((c) => c.name === cmd.name);
245
+ if (!exists) {
246
+ commands.push(cmd);
247
+ userCount++;
248
+ }
249
+ }
250
+ } catch (error) {
251
+ logger2.debug(`Failed to scan user dir: ${error}`);
252
+ }
253
+ commands.sort((a, b) => a.name.localeCompare(b.name));
254
+ return {
255
+ commands,
256
+ stats: { userCount, projectCount }
257
+ };
258
+ }
259
+ async add(options) {
260
+ const { name, target, source = "user", description, tips } = options;
261
+ const dir = source === "user" ? this.userCommandsDir : this.projectCommandsDir;
262
+ const targetPath = path2.resolve(target);
263
+ await fs2.mkdir(dir, { recursive: true });
264
+ const commandPath = path2.join(dir, name);
265
+ try {
266
+ await fs2.access(commandPath);
267
+ throw new Error(`Command '${name}' already exists`);
268
+ } catch (error) {
269
+ if (error.code !== "ENOENT") {
270
+ throw error;
271
+ }
272
+ }
273
+ await fs2.symlink(targetPath, commandPath);
274
+ logger2.info(`Added command '${name}' -> ${targetPath} (${source})`);
275
+ const commandDir = path2.join(dir, name);
276
+ await this.createCommandMeta(commandDir, {
277
+ name,
278
+ target: targetPath,
279
+ description: description || `Command: ${name}`,
280
+ tips,
281
+ source
282
+ });
283
+ }
284
+ async createCommandMeta(commandDir, options) {
285
+ const parentDir = path2.dirname(commandDir);
286
+ const metaPath = path2.join(parentDir, `${options.name}.meta.md`);
287
+ const tipsContent = options.tips ? `
288
+ tips: "${options.tips}"` : "";
289
+ const content = `---
290
+ name: ${options.name}
291
+ description: ${options.description}${tipsContent}
292
+ source: ${options.source}
293
+ ---
294
+
295
+ # ${options.name} Command
296
+
297
+ ## Target
298
+ \`${options.target}\`
299
+
300
+ ## 功能
301
+ ${options.description}
302
+
303
+ ## 探索提示
304
+ Run \`${options.name} --help\` to explore full capabilities.
305
+ `;
306
+ await fs2.writeFile(metaPath, content, "utf-8");
307
+ logger2.info(`[CommandsComponent] Created meta.md for '${options.name}'`);
308
+ }
309
+ async getCommandsMeta() {
310
+ const metaList = [];
311
+ try {
312
+ const entries = await fs2.readdir(this.userCommandsDir, { withFileTypes: true });
313
+ for (const entry of entries) {
314
+ if (entry.isFile() && entry.name.endsWith(".meta.md")) {
315
+ const metaPath = path2.join(this.userCommandsDir, entry.name);
316
+ const meta = await parseMetaFile(metaPath, "user");
317
+ if (meta) {
318
+ metaList.push(meta);
319
+ }
320
+ }
321
+ }
322
+ } catch (error) {
323
+ if (error.code !== "ENOENT") {
324
+ logger2.debug(`Failed to scan user commands dir: ${error}`);
325
+ }
326
+ }
327
+ try {
328
+ const entries = await fs2.readdir(this.projectCommandsDir, { withFileTypes: true });
329
+ for (const entry of entries) {
330
+ if (entry.isFile() && entry.name.endsWith(".meta.md")) {
331
+ const metaPath = path2.join(this.projectCommandsDir, entry.name);
332
+ const meta = await parseMetaFile(metaPath, "project");
333
+ if (meta) {
334
+ metaList.push(meta);
335
+ }
336
+ }
337
+ }
338
+ } catch (error) {
339
+ if (error.code !== "ENOENT") {
340
+ logger2.debug(`Failed to scan project commands dir: ${error}`);
341
+ }
342
+ }
343
+ return metaList;
344
+ }
345
+ async remove(options) {
346
+ const { name, source } = options;
347
+ const dirsToCheck = source ? [source === "user" ? this.userCommandsDir : this.projectCommandsDir] : [this.userCommandsDir, this.projectCommandsDir];
348
+ for (const dir of dirsToCheck) {
349
+ const commandPath = path2.join(dir, name);
350
+ try {
351
+ await fs2.unlink(commandPath);
352
+ logger2.info(`Removed command '${name}' from ${dir}`);
353
+ return;
354
+ } catch (error) {
355
+ if (error.code !== "ENOENT") {
356
+ throw error;
357
+ }
358
+ }
359
+ }
360
+ throw new Error(`Command '${name}' not found`);
361
+ }
362
+ async getInfo(name) {
363
+ const dirsToCheck = [this.projectCommandsDir, this.userCommandsDir];
364
+ for (const dir of dirsToCheck) {
365
+ const commandPath = path2.join(dir, name);
366
+ try {
367
+ const stat = await fs2.lstat(commandPath);
368
+ const realPath = stat.isSymbolicLink() ? await fs2.readlink(commandPath) : commandPath;
369
+ const cmdSource = dir === this.projectCommandsDir ? "project" : "user";
370
+ const shortDescription = await this.getDescription(realPath);
371
+ return {
372
+ name,
373
+ path: realPath,
374
+ source: cmdSource,
375
+ shortDescription
376
+ };
377
+ } catch (error) {
378
+ if (error.code !== "ENOENT") {
379
+ throw error;
380
+ }
381
+ }
382
+ }
383
+ return null;
384
+ }
385
+ async exec(options) {
386
+ const { name, args = [], cwd } = options;
387
+ const info = await this.getInfo(name);
388
+ if (!info) {
389
+ return {
390
+ stdout: "",
391
+ stderr: `Command '${name}' not found`,
392
+ exitCode: 127,
393
+ success: false
394
+ };
395
+ }
396
+ const cmd = `${info.path} ${args.join(" ")}`;
397
+ try {
398
+ const { stdout, stderr } = await execAsync(cmd, {
399
+ cwd: cwd || process.cwd(),
400
+ timeout: 30000
401
+ });
402
+ return {
403
+ stdout,
404
+ stderr,
405
+ exitCode: 0,
406
+ success: true
407
+ };
408
+ } catch (error) {
409
+ return {
410
+ stdout: error.stdout || "",
411
+ stderr: error.stderr || error.message,
412
+ exitCode: error.code || 1,
413
+ success: false
414
+ };
415
+ }
416
+ }
417
+ async scanDir(dir, source, pattern) {
418
+ const commands = [];
419
+ try {
420
+ const entries = await fs2.readdir(dir);
421
+ for (const entry of entries) {
422
+ if (entry.startsWith("."))
423
+ continue;
424
+ if (pattern && !this.matchPattern(entry, pattern))
425
+ continue;
426
+ const fullPath = path2.join(dir, entry);
427
+ try {
428
+ const stat = await fs2.lstat(fullPath);
429
+ if (!stat.isSymbolicLink() && !stat.isFile())
430
+ continue;
431
+ if (stat.isFile()) {
432
+ try {
433
+ await fs2.access(fullPath, fs2.constants.X_OK);
434
+ } catch {
435
+ continue;
436
+ }
437
+ }
438
+ const realPath = stat.isSymbolicLink() ? await fs2.readlink(fullPath) : fullPath;
439
+ const shortDescription = await this.getDescription(realPath);
440
+ commands.push({
441
+ name: entry,
442
+ path: realPath,
443
+ source,
444
+ shortDescription
445
+ });
446
+ } catch (error) {
447
+ logger2.debug(`Failed to stat ${fullPath}: ${error}`);
448
+ }
449
+ }
450
+ } catch (error) {
451
+ logger2.debug(`Failed to scan dir ${dir}: ${error}`);
452
+ }
453
+ return commands;
454
+ }
455
+ matchPattern(name, pattern) {
456
+ const regex = new RegExp("^" + pattern.replace(/\*/g, ".*").replace(/\?/g, ".") + "$");
457
+ return regex.test(name);
458
+ }
459
+ async getDescription(commandPath) {
460
+ const cached = this.descriptionCache.get(commandPath);
461
+ if (cached !== undefined)
462
+ return cached;
463
+ try {
464
+ const { stdout } = await execAsync(`${commandPath} --help 2>/dev/null | head -1`, {
465
+ timeout: 2000
466
+ });
467
+ const description = stdout.trim().split(`
468
+ `)[0] || "";
469
+ this.descriptionCache.set(commandPath, description);
470
+ return description;
471
+ } catch {
472
+ return "";
473
+ }
474
+ }
475
+ async loadDescriptionCache() {
476
+ try {
477
+ const content = await fs2.readFile(this.descriptionCacheFile, "utf-8");
478
+ const data = JSON.parse(content);
479
+ this.descriptionCache = new Map(Object.entries(data));
480
+ } catch {}
481
+ }
482
+ async saveDescriptionCache() {
483
+ try {
484
+ await fs2.mkdir(path2.dirname(this.descriptionCacheFile), { recursive: true });
485
+ const data = Object.fromEntries(this.descriptionCache);
486
+ await fs2.writeFile(this.descriptionCacheFile, JSON.stringify(data, null, 2));
487
+ } catch (error) {
488
+ logger2.debug(`Failed to save description cache: ${error}`);
489
+ }
490
+ }
491
+ }
492
+ export { CommandsComponent };
@@ -0,0 +1,52 @@
1
+ import { createRequire } from "node:module";
2
+ var __defProp = Object.defineProperty;
3
+ var __getOwnPropNames = Object.getOwnPropertyNames;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
6
+ function __accessProp(key) {
7
+ return this[key];
8
+ }
9
+ var __toCommonJS = (from) => {
10
+ var entry = (__moduleCache ??= new WeakMap).get(from), desc;
11
+ if (entry)
12
+ return entry;
13
+ entry = __defProp({}, "__esModule", { value: true });
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (var key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(entry, key))
17
+ __defProp(entry, key, {
18
+ get: __accessProp.bind(from, key),
19
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
20
+ });
21
+ }
22
+ __moduleCache.set(from, entry);
23
+ return entry;
24
+ };
25
+ var __moduleCache;
26
+ var __returnValue = (v) => v;
27
+ function __exportSetter(name, newValue) {
28
+ this[name] = __returnValue.bind(null, newValue);
29
+ }
30
+ var __export = (target, all) => {
31
+ for (var name in all)
32
+ __defProp(target, name, {
33
+ get: all[name],
34
+ enumerable: true,
35
+ configurable: true,
36
+ set: __exportSetter.bind(all, name)
37
+ });
38
+ };
39
+ var __legacyDecorateClassTS = function(decorators, target, key, desc) {
40
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
41
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function")
42
+ r = Reflect.decorate(decorators, target, key, desc);
43
+ else
44
+ for (var i = decorators.length - 1;i >= 0; i--)
45
+ if (d = decorators[i])
46
+ r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
47
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
48
+ };
49
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
50
+ var __require = /* @__PURE__ */ createRequire(import.meta.url);
51
+
52
+ export { __toCommonJS, __export, __legacyDecorateClassTS, __esm, __require };
@@ -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 };