@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.
- package/dist/config/index.d.ts +1250 -0
- package/dist/config/index.js +32 -0
- package/dist/env/agent/index.d.ts +2279 -0
- package/dist/env/agent/index.js +24 -0
- package/dist/env/commands/index.d.ts +1131 -0
- package/dist/env/commands/index.js +14 -0
- package/dist/env/debug/formatters/index.d.ts +236 -0
- package/dist/env/debug/formatters/index.js +11 -0
- package/dist/env/debug/index.d.ts +1652 -0
- package/dist/env/debug/index.js +26 -0
- package/dist/env/hook/index.d.ts +279 -0
- package/dist/env/hook/index.js +29 -0
- package/dist/env/index.d.ts +3481 -0
- package/dist/env/index.js +82 -0
- package/dist/env/llm/index.d.ts +1760 -0
- package/dist/env/llm/index.js +40 -0
- package/dist/env/log-trace/index.d.ts +1574 -0
- package/dist/env/log-trace/index.js +83 -0
- package/dist/env/mcp/index.d.ts +1331 -0
- package/dist/env/mcp/index.js +39 -0
- package/dist/env/mcp/tool/index.d.ts +183 -0
- package/dist/env/mcp/tool/index.js +14 -0
- package/dist/env/memory/built-in/index.d.ts +232 -0
- package/dist/env/memory/built-in/index.js +11 -0
- package/dist/env/memory/index.d.ts +1799 -0
- package/dist/env/memory/index.js +56 -0
- package/dist/env/memory/plugin/index.d.ts +747 -0
- package/dist/env/memory/plugin/index.js +36 -0
- package/dist/env/prompt/index.d.ts +1164 -0
- package/dist/env/prompt/index.js +20 -0
- package/dist/env/session/index.d.ts +1908 -0
- package/dist/env/session/index.js +25 -0
- package/dist/env/session/storage/index.d.ts +564 -0
- package/dist/env/session/storage/index.js +18 -0
- package/dist/env/skill/index.d.ts +1266 -0
- package/dist/env/skill/index.js +34 -0
- package/dist/env/skill/tool/index.d.ts +193 -0
- package/dist/env/skill/tool/index.js +9 -0
- package/dist/env/task/delegate/index.d.ts +1612 -0
- package/dist/env/task/delegate/index.js +18 -0
- package/dist/env/task/events/index.d.ts +171 -0
- package/dist/env/task/events/index.js +7 -0
- package/dist/env/task/hooks/index.d.ts +624 -0
- package/dist/env/task/hooks/index.js +7 -0
- package/dist/env/task/index.d.ts +1553 -0
- package/dist/env/task/index.js +34 -0
- package/dist/env/task/plugins/index.d.ts +466 -0
- package/dist/env/task/plugins/index.js +23 -0
- package/dist/env/task/storage/index.d.ts +241 -0
- package/dist/env/task/storage/index.js +14 -0
- package/dist/env/task/tools/index.d.ts +1485 -0
- package/dist/env/task/tools/index.js +17 -0
- package/dist/env/task/tools/operation/index.d.ts +1484 -0
- package/dist/env/task/tools/operation/index.js +15 -0
- package/dist/env/tool/built-in/index.d.ts +218 -0
- package/dist/env/tool/built-in/index.js +25 -0
- package/dist/env/tool/index.d.ts +1396 -0
- package/dist/env/tool/index.js +39 -0
- package/dist/env/workflow/decorators/index.d.ts +2161 -0
- package/dist/env/workflow/decorators/index.js +27 -0
- package/dist/env/workflow/engine/index.d.ts +3453 -0
- package/dist/env/workflow/engine/index.js +28 -0
- package/dist/env/workflow/index.d.ts +3546 -0
- package/dist/env/workflow/index.js +136 -0
- package/dist/env/workflow/nodes/index.d.ts +2092 -0
- package/dist/env/workflow/nodes/index.js +19 -0
- package/dist/env/workflow/service/index.d.ts +227 -0
- package/dist/env/workflow/service/index.js +13 -0
- package/dist/env/workflow/storage/index.d.ts +165 -0
- package/dist/env/workflow/storage/index.js +27 -0
- package/dist/env/workflow/tools/index.d.ts +416 -0
- package/dist/env/workflow/tools/index.js +159 -0
- package/dist/env/workflow/types/index.d.ts +2255 -0
- package/dist/env/workflow/types/index.js +98 -0
- package/dist/env/workflow/utils/index.d.ts +2031 -0
- package/dist/env/workflow/utils/index.js +637 -0
- package/dist/index.d.ts +7858 -0
- package/dist/index.js +399 -0
- package/dist/shared/@ai-setting/roy-agent-core-0rtxwr28.js +258 -0
- package/dist/shared/@ai-setting/roy-agent-core-0vbdz0x7.js +36 -0
- package/dist/shared/@ai-setting/roy-agent-core-1akcqxj9.js +349 -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-3jywqmdd.js +393 -0
- package/dist/shared/@ai-setting/roy-agent-core-3rr5k71j.js +200 -0
- package/dist/shared/@ai-setting/roy-agent-core-44hnfb02.js +299 -0
- package/dist/shared/@ai-setting/roy-agent-core-4t40mkpv.js +206 -0
- package/dist/shared/@ai-setting/roy-agent-core-4txzpsbt.js +393 -0
- package/dist/shared/@ai-setting/roy-agent-core-5x94xmt6.js +350 -0
- package/dist/shared/@ai-setting/roy-agent-core-69jskqjg.js +180 -0
- package/dist/shared/@ai-setting/roy-agent-core-6kvtahqv.js +408 -0
- package/dist/shared/@ai-setting/roy-agent-core-7fgf85wc.js +284 -0
- package/dist/shared/@ai-setting/roy-agent-core-81w1963m.js +762 -0
- package/dist/shared/@ai-setting/roy-agent-core-8gxth0eh.js +10 -0
- package/dist/shared/@ai-setting/roy-agent-core-92z6t4he.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-93zfb3r1.js +922 -0
- package/dist/shared/@ai-setting/roy-agent-core-9yxb3ty9.js +15 -0
- package/dist/shared/@ai-setting/roy-agent-core-b0x5dda6.js +1130 -0
- package/dist/shared/@ai-setting/roy-agent-core-bcbqy27c.js +14 -0
- package/dist/shared/@ai-setting/roy-agent-core-bvr1761x.js +653 -0
- package/dist/shared/@ai-setting/roy-agent-core-ctdhjv68.js +93 -0
- package/dist/shared/@ai-setting/roy-agent-core-d7cyjkf7.js +872 -0
- package/dist/shared/@ai-setting/roy-agent-core-dh9d7a3m.js +11 -0
- package/dist/shared/@ai-setting/roy-agent-core-e25xkv53.js +64 -0
- package/dist/shared/@ai-setting/roy-agent-core-eajcvp4e.js +378 -0
- package/dist/shared/@ai-setting/roy-agent-core-f7q2x5z6.js +492 -0
- package/dist/shared/@ai-setting/roy-agent-core-fs0mn2jk.js +52 -0
- package/dist/shared/@ai-setting/roy-agent-core-g1s2h0e5.js +171 -0
- package/dist/shared/@ai-setting/roy-agent-core-g99pxzn5.js +862 -0
- package/dist/shared/@ai-setting/roy-agent-core-gbqcyegm.js +1387 -0
- package/dist/shared/@ai-setting/roy-agent-core-gjq1yk68.js +208 -0
- package/dist/shared/@ai-setting/roy-agent-core-gq20wsgv.js +139 -0
- package/dist/shared/@ai-setting/roy-agent-core-gwc4h96n.js +534 -0
- package/dist/shared/@ai-setting/roy-agent-core-jfh9q2qh.js +204 -0
- package/dist/shared/@ai-setting/roy-agent-core-jvatggbb.js +603 -0
- package/dist/shared/@ai-setting/roy-agent-core-kkbwepqb.js +97 -0
- package/dist/shared/@ai-setting/roy-agent-core-pjr12nnd.js +587 -0
- package/dist/shared/@ai-setting/roy-agent-core-psv4v63c.js +176 -0
- package/dist/shared/@ai-setting/roy-agent-core-psvxt4c9.js +60 -0
- package/dist/shared/@ai-setting/roy-agent-core-qqceba6k.js +442 -0
- 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-r9ezzemr.js +10 -0
- package/dist/shared/@ai-setting/roy-agent-core-rhmtwnw1.js +267 -0
- package/dist/shared/@ai-setting/roy-agent-core-rvv6ydff.js +584 -0
- package/dist/shared/@ai-setting/roy-agent-core-rvxg1wps.js +102 -0
- package/dist/shared/@ai-setting/roy-agent-core-satmq6sh.js +549 -0
- package/dist/shared/@ai-setting/roy-agent-core-sx7wsvnn.js +15 -0
- package/dist/shared/@ai-setting/roy-agent-core-t94ktchq.js +213 -0
- package/dist/shared/@ai-setting/roy-agent-core-vf215qfv.js +812 -0
- package/dist/shared/@ai-setting/roy-agent-core-vkz81f7v.js +1316 -0
- package/dist/shared/@ai-setting/roy-agent-core-vn2bc59q.js +1205 -0
- package/dist/shared/@ai-setting/roy-agent-core-wa1kzqky.js +328 -0
- package/dist/shared/@ai-setting/roy-agent-core-wft9ra24.js +20 -0
- package/dist/shared/@ai-setting/roy-agent-core-wrcy0h6z.js +2098 -0
- package/dist/shared/@ai-setting/roy-agent-core-xq8hhqb8.js +419 -0
- package/dist/shared/@ai-setting/roy-agent-core-xs5rsgat.js +368 -0
- package/dist/shared/@ai-setting/roy-agent-core-zbkpc41z.js +377 -0
- package/dist/shared/@ai-setting/roy-agent-core-zgypchmt.js +172 -0
- package/dist/shared/@ai-setting/roy-agent-core-zpn0bqa8.js +103 -0
- package/package.json +1 -1
|
@@ -0,0 +1,393 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createMemoryTools
|
|
3
|
+
} from "./roy-agent-core-g1s2h0e5.js";
|
|
4
|
+
import {
|
|
5
|
+
envKeyToConfigKey
|
|
6
|
+
} from "./roy-agent-core-qxhq8ven.js";
|
|
7
|
+
import {
|
|
8
|
+
BaseComponent
|
|
9
|
+
} from "./roy-agent-core-kkbwepqb.js";
|
|
10
|
+
import {
|
|
11
|
+
createLogger,
|
|
12
|
+
init_logger
|
|
13
|
+
} from "./roy-agent-core-44hnfb02.js";
|
|
14
|
+
|
|
15
|
+
// src/env/memory/types.ts
|
|
16
|
+
import { z } from "zod";
|
|
17
|
+
var RecordMemorySchema = z.object({
|
|
18
|
+
scope: z.enum(["project", "global"]).describe("作用域: project/global"),
|
|
19
|
+
mode: z.enum(["append", "prepend", "overwrite", "delete"]).describe("模式: append(追加)/prepend(插入)/overwrite(覆盖)/delete(删除)"),
|
|
20
|
+
content: z.string().optional().describe("记忆内容"),
|
|
21
|
+
title: z.string().optional().describe("章节标题")
|
|
22
|
+
});
|
|
23
|
+
var RecallMemorySchema = z.object({
|
|
24
|
+
scope: z.enum(["project", "global"]).optional().describe("作用域(可选,不传则读取所有)")
|
|
25
|
+
});
|
|
26
|
+
// src/env/memory/memory-config.ts
|
|
27
|
+
import path from "path";
|
|
28
|
+
import os from "os";
|
|
29
|
+
var DEFAULT_MEMORY_CONFIG = {
|
|
30
|
+
memoryPaths: [
|
|
31
|
+
{
|
|
32
|
+
type: "global",
|
|
33
|
+
path: path.join(os.homedir(), ".config", "roy-agent", "memory")
|
|
34
|
+
},
|
|
35
|
+
{
|
|
36
|
+
type: "project",
|
|
37
|
+
path: ".roy/memory"
|
|
38
|
+
}
|
|
39
|
+
],
|
|
40
|
+
recursive: true,
|
|
41
|
+
cacheEnabled: true,
|
|
42
|
+
cacheTTL: 5 * 60 * 1000
|
|
43
|
+
};
|
|
44
|
+
var SOURCE_PRIORITY = {
|
|
45
|
+
global: 0,
|
|
46
|
+
project: 1
|
|
47
|
+
};
|
|
48
|
+
function getSourcePriority(source) {
|
|
49
|
+
return SOURCE_PRIORITY[source] ?? 0;
|
|
50
|
+
}
|
|
51
|
+
// src/env/memory/memory-component.ts
|
|
52
|
+
init_logger();
|
|
53
|
+
|
|
54
|
+
// src/env/memory/memory-config-registration.ts
|
|
55
|
+
import path2 from "path";
|
|
56
|
+
import os2 from "os";
|
|
57
|
+
var MEMORY_DEFAULTS = {
|
|
58
|
+
"memory.recursive": true,
|
|
59
|
+
"memory.cacheEnabled": true,
|
|
60
|
+
"memory.cacheTTL": 300000,
|
|
61
|
+
"memory.memoryFile": "memory.md",
|
|
62
|
+
"memory.memoryPaths": [
|
|
63
|
+
{ type: "global", path: path2.join(os2.homedir(), ".config", "roy-agent", "memory") },
|
|
64
|
+
{ type: "project", path: ".roy/memory" }
|
|
65
|
+
]
|
|
66
|
+
};
|
|
67
|
+
var MEMORY_CONFIG_REGISTRATION = {
|
|
68
|
+
name: "memory",
|
|
69
|
+
sources: [
|
|
70
|
+
{ type: "env", envPrefix: "MEMORY", priority: 20, watch: false }
|
|
71
|
+
],
|
|
72
|
+
keys: [
|
|
73
|
+
{ key: "memory.recursive", sources: ["env", "file"] },
|
|
74
|
+
{ key: "memory.cacheEnabled", sources: ["env", "file"] },
|
|
75
|
+
{ key: "memory.cacheTTL", sources: ["env", "file"] },
|
|
76
|
+
{ key: "memory.memoryPaths", sources: ["env", "file"] },
|
|
77
|
+
{ key: "memory.memoryFile", sources: ["env", "file"] }
|
|
78
|
+
]
|
|
79
|
+
};
|
|
80
|
+
|
|
81
|
+
// src/env/memory/memory-component.ts
|
|
82
|
+
import path3 from "path";
|
|
83
|
+
import fs from "fs/promises";
|
|
84
|
+
var logger = createLogger("memory");
|
|
85
|
+
|
|
86
|
+
class MemoryComponent extends BaseComponent {
|
|
87
|
+
name = "memory";
|
|
88
|
+
version = "2.0.0";
|
|
89
|
+
config;
|
|
90
|
+
toolComponent;
|
|
91
|
+
configComponent;
|
|
92
|
+
cache = new Map;
|
|
93
|
+
configWatcher;
|
|
94
|
+
async init(config) {
|
|
95
|
+
await super.init(config);
|
|
96
|
+
const options = config?.options;
|
|
97
|
+
if (!options?.configComponent) {
|
|
98
|
+
throw new Error("ConfigComponent is required for MemoryComponent initialization");
|
|
99
|
+
}
|
|
100
|
+
this.configComponent = options.configComponent;
|
|
101
|
+
await this.registerConfig(options);
|
|
102
|
+
logger.info("[MemoryComponent] Initialized (simplified)");
|
|
103
|
+
}
|
|
104
|
+
async registerConfig(options) {
|
|
105
|
+
const configComponent = this.configComponent;
|
|
106
|
+
if (!configComponent)
|
|
107
|
+
return;
|
|
108
|
+
const { configPath, envPrefix, config } = options;
|
|
109
|
+
const prefix = envPrefix ?? "MEMORY";
|
|
110
|
+
configComponent.registerComponent(MEMORY_CONFIG_REGISTRATION);
|
|
111
|
+
if (configPath) {
|
|
112
|
+
configComponent.registerSource({
|
|
113
|
+
type: "file",
|
|
114
|
+
relativePath: configPath,
|
|
115
|
+
optional: true,
|
|
116
|
+
watch: false
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
configComponent.registerSource({
|
|
120
|
+
type: "env",
|
|
121
|
+
envPrefix: prefix,
|
|
122
|
+
priority: 20,
|
|
123
|
+
watch: false
|
|
124
|
+
});
|
|
125
|
+
await configComponent.load("memory");
|
|
126
|
+
for (const envKey of Object.keys(process.env)) {
|
|
127
|
+
const configKey = envKeyToConfigKey(envKey, prefix, "memory");
|
|
128
|
+
if (!configKey)
|
|
129
|
+
continue;
|
|
130
|
+
const value = process.env[envKey];
|
|
131
|
+
if (value !== undefined) {
|
|
132
|
+
await configComponent.set(configKey, value);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
for (const [key, value] of Object.entries(MEMORY_DEFAULTS)) {
|
|
136
|
+
if (configComponent.get(key) === undefined) {
|
|
137
|
+
await configComponent.set(key, value);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (config) {
|
|
141
|
+
const flatConfig = this.flattenConfig(config);
|
|
142
|
+
for (const [key, value] of Object.entries(flatConfig)) {
|
|
143
|
+
await configComponent.set(key, value);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
this.registerConfigWatcher(configComponent);
|
|
147
|
+
this.buildConfig(configComponent);
|
|
148
|
+
}
|
|
149
|
+
buildConfig(configComponent) {
|
|
150
|
+
const recursive = configComponent.get("memory.recursive") ?? true;
|
|
151
|
+
const cacheEnabled = configComponent.get("memory.cacheEnabled") ?? true;
|
|
152
|
+
const cacheTTL = configComponent.get("memory.cacheTTL") ?? 300000;
|
|
153
|
+
const memoryFile = configComponent.get("memory.memoryFile") ?? "memory.md";
|
|
154
|
+
const memoryPathsConfig = configComponent.get("memory.memoryPaths");
|
|
155
|
+
let memoryPaths = [];
|
|
156
|
+
if (Array.isArray(memoryPathsConfig)) {
|
|
157
|
+
memoryPaths = memoryPathsConfig;
|
|
158
|
+
} else if (typeof memoryPathsConfig === "string") {
|
|
159
|
+
try {
|
|
160
|
+
memoryPaths = JSON.parse(memoryPathsConfig);
|
|
161
|
+
} catch {
|
|
162
|
+
memoryPaths = DEFAULT_MEMORY_CONFIG.memoryPaths;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
if (memoryPaths.length === 0) {
|
|
166
|
+
memoryPaths = DEFAULT_MEMORY_CONFIG.memoryPaths;
|
|
167
|
+
}
|
|
168
|
+
this.config = {
|
|
169
|
+
memoryPaths,
|
|
170
|
+
recursive,
|
|
171
|
+
cacheEnabled,
|
|
172
|
+
cacheTTL,
|
|
173
|
+
memoryFile
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
flattenConfig(obj, prefix = "memory") {
|
|
177
|
+
const result = {};
|
|
178
|
+
for (const [key, value] of Object.entries(obj)) {
|
|
179
|
+
const fullKey = `${prefix}.${key}`;
|
|
180
|
+
if (value && typeof value === "object" && !Array.isArray(value)) {
|
|
181
|
+
Object.assign(result, this.flattenConfig(value, fullKey));
|
|
182
|
+
} else {
|
|
183
|
+
result[fullKey] = value;
|
|
184
|
+
}
|
|
185
|
+
}
|
|
186
|
+
return result;
|
|
187
|
+
}
|
|
188
|
+
registerConfigWatcher(configComponent) {
|
|
189
|
+
if (typeof configComponent.watch !== "function") {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
this.configWatcher = configComponent.watch("memory.*", (event) => {
|
|
193
|
+
this.onConfigChange(event);
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
onConfigChange(event) {
|
|
197
|
+
logger.info(`[MemoryComponent] Config changed: ${event.key}`, {
|
|
198
|
+
oldValue: event.oldValue,
|
|
199
|
+
newValue: event.newValue
|
|
200
|
+
});
|
|
201
|
+
if (this.configComponent) {
|
|
202
|
+
this.buildConfig(this.configComponent);
|
|
203
|
+
}
|
|
204
|
+
this.cache.clear();
|
|
205
|
+
}
|
|
206
|
+
async start() {
|
|
207
|
+
await super.start();
|
|
208
|
+
this.toolComponent = this.env?.getComponent("tool");
|
|
209
|
+
if (!this.toolComponent) {
|
|
210
|
+
logger.warn("[MemoryComponent] ToolComponent not found, skipping tool registration");
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
await this.registerTools();
|
|
214
|
+
this.setStatus("running");
|
|
215
|
+
logger.info(`[MemoryComponent] Started with ${this.toolComponent.getToolCount()} tools`);
|
|
216
|
+
}
|
|
217
|
+
async stop() {
|
|
218
|
+
if (this.configWatcher) {
|
|
219
|
+
this.configWatcher();
|
|
220
|
+
this.configWatcher = undefined;
|
|
221
|
+
}
|
|
222
|
+
if (this.toolComponent) {
|
|
223
|
+
this.toolComponent.unregister("record_memory");
|
|
224
|
+
this.toolComponent.unregister("recall_memory");
|
|
225
|
+
}
|
|
226
|
+
this.cache.clear();
|
|
227
|
+
await super.stop();
|
|
228
|
+
logger.info("[MemoryComponent] Stopped");
|
|
229
|
+
}
|
|
230
|
+
async registerTools() {
|
|
231
|
+
if (!this.toolComponent)
|
|
232
|
+
return;
|
|
233
|
+
const tools = createMemoryTools((args) => this.recordMemory(args), (scope) => this.recallMemory(scope), {
|
|
234
|
+
getMemoryPaths: () => this.getMemoryPaths(),
|
|
235
|
+
getMemoryFile: () => this.getMemoryFile()
|
|
236
|
+
});
|
|
237
|
+
for (const tool of tools) {
|
|
238
|
+
this.toolComponent.register(tool);
|
|
239
|
+
}
|
|
240
|
+
logger.info("[MemoryComponent] Registered memory tools");
|
|
241
|
+
}
|
|
242
|
+
getMemoryPaths() {
|
|
243
|
+
return this.config.memoryPaths;
|
|
244
|
+
}
|
|
245
|
+
updateMemoryPaths(paths) {
|
|
246
|
+
this.config.memoryPaths = paths;
|
|
247
|
+
this.clearCache();
|
|
248
|
+
}
|
|
249
|
+
getMemoryFile() {
|
|
250
|
+
return this.config.memoryFile ?? "memory.md";
|
|
251
|
+
}
|
|
252
|
+
async recordMemory(args) {
|
|
253
|
+
const { mode, content, title, filename, scope } = args;
|
|
254
|
+
const memoryFile = filename ?? this.getMemoryFile();
|
|
255
|
+
const paths = this.config.memoryPaths;
|
|
256
|
+
if (paths.length === 0) {
|
|
257
|
+
return null;
|
|
258
|
+
}
|
|
259
|
+
let basePath;
|
|
260
|
+
if (scope) {
|
|
261
|
+
const targetPath = paths.find((p) => p.type === scope);
|
|
262
|
+
if (!targetPath) {
|
|
263
|
+
logger.warn(`[recordMemory] No ${scope} path configured, using first available path`);
|
|
264
|
+
basePath = paths[0].path;
|
|
265
|
+
} else {
|
|
266
|
+
basePath = targetPath.path;
|
|
267
|
+
}
|
|
268
|
+
} else {
|
|
269
|
+
basePath = paths[0].path;
|
|
270
|
+
}
|
|
271
|
+
const filePath = path3.join(basePath, memoryFile);
|
|
272
|
+
try {
|
|
273
|
+
let existingContent = "";
|
|
274
|
+
let fileExists = false;
|
|
275
|
+
try {
|
|
276
|
+
existingContent = await fs.readFile(filePath, "utf-8");
|
|
277
|
+
fileExists = true;
|
|
278
|
+
} catch {}
|
|
279
|
+
let newContent;
|
|
280
|
+
let action;
|
|
281
|
+
switch (mode) {
|
|
282
|
+
case "delete":
|
|
283
|
+
if (fileExists) {
|
|
284
|
+
await fs.unlink(filePath);
|
|
285
|
+
this.clearCache();
|
|
286
|
+
return { path: filePath, action: "delete" };
|
|
287
|
+
}
|
|
288
|
+
return null;
|
|
289
|
+
case "overwrite":
|
|
290
|
+
if (title && content) {
|
|
291
|
+
newContent = `## ${title}
|
|
292
|
+
|
|
293
|
+
${content}
|
|
294
|
+
`;
|
|
295
|
+
} else {
|
|
296
|
+
newContent = content ?? "";
|
|
297
|
+
}
|
|
298
|
+
action = fileExists ? "overwrite" : "created";
|
|
299
|
+
break;
|
|
300
|
+
case "append":
|
|
301
|
+
if (!content) {
|
|
302
|
+
throw new Error("content is required for append mode");
|
|
303
|
+
}
|
|
304
|
+
if (title) {
|
|
305
|
+
newContent = existingContent + `
|
|
306
|
+
## ${title}
|
|
307
|
+
|
|
308
|
+
${content}
|
|
309
|
+
`;
|
|
310
|
+
} else {
|
|
311
|
+
newContent = existingContent + `
|
|
312
|
+
${content}
|
|
313
|
+
`;
|
|
314
|
+
}
|
|
315
|
+
action = fileExists ? "append" : "created";
|
|
316
|
+
break;
|
|
317
|
+
case "prepend":
|
|
318
|
+
if (!content) {
|
|
319
|
+
throw new Error("content is required for prepend mode");
|
|
320
|
+
}
|
|
321
|
+
if (title) {
|
|
322
|
+
newContent = `## ${title}
|
|
323
|
+
|
|
324
|
+
${content}
|
|
325
|
+
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
${existingContent}`;
|
|
329
|
+
} else {
|
|
330
|
+
newContent = `${content}
|
|
331
|
+
|
|
332
|
+
---
|
|
333
|
+
|
|
334
|
+
${existingContent}`;
|
|
335
|
+
}
|
|
336
|
+
action = fileExists ? "prepend" : "created";
|
|
337
|
+
break;
|
|
338
|
+
default:
|
|
339
|
+
throw new Error(`Unknown mode: ${mode}`);
|
|
340
|
+
}
|
|
341
|
+
await fs.mkdir(basePath, { recursive: true });
|
|
342
|
+
await fs.writeFile(filePath, newContent, "utf-8");
|
|
343
|
+
this.clearCache();
|
|
344
|
+
return { path: filePath, action };
|
|
345
|
+
} catch (error) {
|
|
346
|
+
logger.error(`[recordMemory] Failed: ${error}`);
|
|
347
|
+
throw error;
|
|
348
|
+
}
|
|
349
|
+
}
|
|
350
|
+
async recallMemory(scope) {
|
|
351
|
+
const memoryFile = this.getMemoryFile();
|
|
352
|
+
const paths = this.config.memoryPaths;
|
|
353
|
+
const results = [];
|
|
354
|
+
for (const memoryPath of paths) {
|
|
355
|
+
if (scope && memoryPath.type !== scope) {
|
|
356
|
+
continue;
|
|
357
|
+
}
|
|
358
|
+
const filePath = path3.join(memoryPath.path, memoryFile);
|
|
359
|
+
try {
|
|
360
|
+
const content = await fs.readFile(filePath, "utf-8");
|
|
361
|
+
if (content.trim()) {
|
|
362
|
+
results.push(`## [${memoryPath.type}] ${memoryFile}
|
|
363
|
+
${content}`);
|
|
364
|
+
}
|
|
365
|
+
} catch {}
|
|
366
|
+
}
|
|
367
|
+
return results.join(`
|
|
368
|
+
|
|
369
|
+
`);
|
|
370
|
+
}
|
|
371
|
+
clearCache() {
|
|
372
|
+
this.cache.clear();
|
|
373
|
+
}
|
|
374
|
+
getCache(key) {
|
|
375
|
+
if (!this.config.cacheEnabled)
|
|
376
|
+
return;
|
|
377
|
+
const entry = this.cache.get(key);
|
|
378
|
+
if (!entry)
|
|
379
|
+
return;
|
|
380
|
+
const age = Date.now() - entry.timestamp;
|
|
381
|
+
if (age > (this.config.cacheTTL ?? 300000)) {
|
|
382
|
+
this.cache.delete(key);
|
|
383
|
+
return;
|
|
384
|
+
}
|
|
385
|
+
return entry.data;
|
|
386
|
+
}
|
|
387
|
+
setCache(key, data) {
|
|
388
|
+
if (!this.config.cacheEnabled)
|
|
389
|
+
return;
|
|
390
|
+
this.cache.set(key, { data, timestamp: Date.now() });
|
|
391
|
+
}
|
|
392
|
+
}
|
|
393
|
+
export { DEFAULT_MEMORY_CONFIG, SOURCE_PRIORITY, getSourcePriority, MemoryComponent };
|