agents-library 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/base-agent.d.ts +172 -0
- package/dist/base-agent.d.ts.map +1 -0
- package/dist/base-agent.js +255 -0
- package/dist/base-agent.js.map +1 -0
- package/dist/base-bot.d.ts +282 -0
- package/dist/base-bot.d.ts.map +1 -0
- package/dist/base-bot.js +375 -0
- package/dist/base-bot.js.map +1 -0
- package/dist/common/result.d.ts +51 -0
- package/dist/common/result.d.ts.map +1 -0
- package/dist/common/result.js +45 -0
- package/dist/common/result.js.map +1 -0
- package/dist/common/types.d.ts +57 -0
- package/dist/common/types.d.ts.map +1 -0
- package/dist/common/types.js +42 -0
- package/dist/common/types.js.map +1 -0
- package/dist/index.d.ts +94 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +108 -0
- package/dist/index.js.map +1 -0
- package/dist/kadi-event-publisher.d.ts +163 -0
- package/dist/kadi-event-publisher.d.ts.map +1 -0
- package/dist/kadi-event-publisher.js +286 -0
- package/dist/kadi-event-publisher.js.map +1 -0
- package/dist/memory/arcadedb-adapter.d.ts +159 -0
- package/dist/memory/arcadedb-adapter.d.ts.map +1 -0
- package/dist/memory/arcadedb-adapter.js +314 -0
- package/dist/memory/arcadedb-adapter.js.map +1 -0
- package/dist/memory/file-storage-adapter.d.ts +122 -0
- package/dist/memory/file-storage-adapter.d.ts.map +1 -0
- package/dist/memory/file-storage-adapter.js +352 -0
- package/dist/memory/file-storage-adapter.js.map +1 -0
- package/dist/memory/memory-service.d.ts +208 -0
- package/dist/memory/memory-service.d.ts.map +1 -0
- package/dist/memory/memory-service.js +410 -0
- package/dist/memory/memory-service.js.map +1 -0
- package/dist/memory/types.d.ts +126 -0
- package/dist/memory/types.d.ts.map +1 -0
- package/dist/memory/types.js +41 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/producer-tool-utils.d.ts +474 -0
- package/dist/producer-tool-utils.d.ts.map +1 -0
- package/dist/producer-tool-utils.js +664 -0
- package/dist/producer-tool-utils.js.map +1 -0
- package/dist/providers/anthropic-provider.d.ts +160 -0
- package/dist/providers/anthropic-provider.d.ts.map +1 -0
- package/dist/providers/anthropic-provider.js +527 -0
- package/dist/providers/anthropic-provider.js.map +1 -0
- package/dist/providers/model-manager-provider.d.ts +91 -0
- package/dist/providers/model-manager-provider.d.ts.map +1 -0
- package/dist/providers/model-manager-provider.js +355 -0
- package/dist/providers/model-manager-provider.js.map +1 -0
- package/dist/providers/provider-manager.d.ts +111 -0
- package/dist/providers/provider-manager.d.ts.map +1 -0
- package/dist/providers/provider-manager.js +337 -0
- package/dist/providers/provider-manager.js.map +1 -0
- package/dist/providers/types.d.ts +145 -0
- package/dist/providers/types.d.ts.map +1 -0
- package/dist/providers/types.js +23 -0
- package/dist/providers/types.js.map +1 -0
- package/dist/shadow-agent-factory.d.ts +623 -0
- package/dist/shadow-agent-factory.d.ts.map +1 -0
- package/dist/shadow-agent-factory.js +1117 -0
- package/dist/shadow-agent-factory.js.map +1 -0
- package/dist/types/agent-config.d.ts +307 -0
- package/dist/types/agent-config.d.ts.map +1 -0
- package/dist/types/agent-config.js +15 -0
- package/dist/types/agent-config.js.map +1 -0
- package/dist/types/event-schemas.d.ts +358 -0
- package/dist/types/event-schemas.d.ts.map +1 -0
- package/dist/types/event-schemas.js +188 -0
- package/dist/types/event-schemas.js.map +1 -0
- package/dist/types/tool-schemas.d.ts +498 -0
- package/dist/types/tool-schemas.d.ts.map +1 -0
- package/dist/types/tool-schemas.js +457 -0
- package/dist/types/tool-schemas.js.map +1 -0
- package/dist/utils/logger.d.ts +135 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/logger.js +205 -0
- package/dist/utils/logger.js.map +1 -0
- package/dist/utils/timer.d.ts +186 -0
- package/dist/utils/timer.d.ts.map +1 -0
- package/dist/utils/timer.js +211 -0
- package/dist/utils/timer.js.map +1 -0
- package/dist/worker-agent-factory.d.ts +688 -0
- package/dist/worker-agent-factory.d.ts.map +1 -0
- package/dist/worker-agent-factory.js +1517 -0
- package/dist/worker-agent-factory.js.map +1 -0
- package/package.json +38 -0
|
@@ -0,0 +1,314 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ArcadeDB Adapter
|
|
3
|
+
*
|
|
4
|
+
* Provides database operations for long-term memory storage using ArcadeDB.
|
|
5
|
+
* Supports vertex/edge operations and Cypher queries with connection pooling.
|
|
6
|
+
*/
|
|
7
|
+
import { ok, err } from '../common/result.js';
|
|
8
|
+
import { DatabaseErrorType } from '../common/types.js';
|
|
9
|
+
/**
|
|
10
|
+
* ArcadeDB Client
|
|
11
|
+
*
|
|
12
|
+
* Low-level HTTP client for ArcadeDB REST API
|
|
13
|
+
* Handles connection, authentication, and query execution
|
|
14
|
+
*/
|
|
15
|
+
export class ArcadeDBClient {
|
|
16
|
+
config;
|
|
17
|
+
connected = false;
|
|
18
|
+
baseUrl;
|
|
19
|
+
auth;
|
|
20
|
+
timeout;
|
|
21
|
+
/**
|
|
22
|
+
* Create ArcadeDB Client
|
|
23
|
+
*
|
|
24
|
+
* @param config - Client configuration
|
|
25
|
+
*/
|
|
26
|
+
constructor(config) {
|
|
27
|
+
this.config = config;
|
|
28
|
+
this.baseUrl = `http://${config.host}:${config.port}`;
|
|
29
|
+
this.auth = Buffer.from(`${config.username}:${config.password}`).toString('base64');
|
|
30
|
+
this.timeout = config.timeout || 30000;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Connect to ArcadeDB
|
|
34
|
+
*
|
|
35
|
+
* Verifies connectivity by executing a test query
|
|
36
|
+
*
|
|
37
|
+
* @returns Result indicating connection success or error
|
|
38
|
+
*/
|
|
39
|
+
async connect() {
|
|
40
|
+
try {
|
|
41
|
+
// Temporarily mark as connected to allow test query
|
|
42
|
+
this.connected = true;
|
|
43
|
+
// Test connection with simple query
|
|
44
|
+
const result = await this.query('SELECT 1 as test');
|
|
45
|
+
if (!result.success) {
|
|
46
|
+
this.connected = false;
|
|
47
|
+
return result;
|
|
48
|
+
}
|
|
49
|
+
return ok(undefined);
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
this.connected = false;
|
|
53
|
+
return err({
|
|
54
|
+
type: DatabaseErrorType.CONNECTION_ERROR,
|
|
55
|
+
message: `Failed to connect to ArcadeDB: ${error.message}`,
|
|
56
|
+
originalError: error,
|
|
57
|
+
});
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* Disconnect from ArcadeDB
|
|
62
|
+
*
|
|
63
|
+
* Marks client as disconnected (no persistent connection to close)
|
|
64
|
+
*
|
|
65
|
+
* @returns Result indicating disconnect success
|
|
66
|
+
*/
|
|
67
|
+
async disconnect() {
|
|
68
|
+
this.connected = false;
|
|
69
|
+
return ok(undefined);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Check if client is connected
|
|
73
|
+
*
|
|
74
|
+
* @returns True if connected
|
|
75
|
+
*/
|
|
76
|
+
isConnected() {
|
|
77
|
+
return this.connected;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Execute query
|
|
81
|
+
*
|
|
82
|
+
* Supports both SQL and Cypher languages
|
|
83
|
+
*
|
|
84
|
+
* @param command - Query command
|
|
85
|
+
* @param params - Optional query parameters
|
|
86
|
+
* @param language - Query language ('sql' or 'cypher'), defaults to 'sql'
|
|
87
|
+
* @returns Result with query results or error
|
|
88
|
+
*/
|
|
89
|
+
async query(command, params, language = 'sql') {
|
|
90
|
+
if (!this.connected) {
|
|
91
|
+
return err({
|
|
92
|
+
type: DatabaseErrorType.NOT_CONNECTED,
|
|
93
|
+
message: 'Client not connected. Call connect() first.',
|
|
94
|
+
query: command,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
try {
|
|
98
|
+
const controller = new AbortController();
|
|
99
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
|
100
|
+
const response = await fetch(`${this.baseUrl}/api/v1/query/${this.config.database}`, {
|
|
101
|
+
method: 'POST',
|
|
102
|
+
headers: {
|
|
103
|
+
'Authorization': `Basic ${this.auth}`,
|
|
104
|
+
'Content-Type': 'application/json',
|
|
105
|
+
},
|
|
106
|
+
body: JSON.stringify({
|
|
107
|
+
language,
|
|
108
|
+
command,
|
|
109
|
+
...(params && { params }),
|
|
110
|
+
}),
|
|
111
|
+
signal: controller.signal,
|
|
112
|
+
});
|
|
113
|
+
clearTimeout(timeoutId);
|
|
114
|
+
if (!response.ok) {
|
|
115
|
+
const errorText = await response.text();
|
|
116
|
+
return err({
|
|
117
|
+
type: DatabaseErrorType.QUERY_ERROR,
|
|
118
|
+
message: `Query failed with status ${response.status}: ${errorText}`,
|
|
119
|
+
query: command,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
const data = await response.json();
|
|
123
|
+
if (data.error) {
|
|
124
|
+
return err({
|
|
125
|
+
type: DatabaseErrorType.QUERY_ERROR,
|
|
126
|
+
message: `Query error: ${data.error}`,
|
|
127
|
+
query: command,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
return ok(data.result || []);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (error.name === 'AbortError') {
|
|
134
|
+
return err({
|
|
135
|
+
type: DatabaseErrorType.TIMEOUT,
|
|
136
|
+
message: `Query timeout after ${this.timeout}ms`,
|
|
137
|
+
query: command,
|
|
138
|
+
originalError: error,
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
return err({
|
|
142
|
+
type: DatabaseErrorType.QUERY_ERROR,
|
|
143
|
+
message: `Query execution failed: ${error.message}`,
|
|
144
|
+
query: command,
|
|
145
|
+
originalError: error,
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* ArcadeDB Adapter
|
|
152
|
+
*
|
|
153
|
+
* High-level adapter for graph database operations
|
|
154
|
+
* Provides vertex/edge operations using Cypher queries
|
|
155
|
+
*/
|
|
156
|
+
export class ArcadeDBAdapter {
|
|
157
|
+
dbUrl;
|
|
158
|
+
username;
|
|
159
|
+
password;
|
|
160
|
+
timeout;
|
|
161
|
+
client = null;
|
|
162
|
+
/**
|
|
163
|
+
* Create ArcadeDB Adapter
|
|
164
|
+
*
|
|
165
|
+
* @param dbUrl - Database connection URL (format: http://host:port/database)
|
|
166
|
+
* @param username - Database username
|
|
167
|
+
* @param password - Database password
|
|
168
|
+
* @param timeout - Query timeout in milliseconds
|
|
169
|
+
*/
|
|
170
|
+
constructor(dbUrl, username = 'root', password = 'root', timeout = 30000) {
|
|
171
|
+
this.dbUrl = dbUrl;
|
|
172
|
+
this.username = username;
|
|
173
|
+
this.password = password;
|
|
174
|
+
this.timeout = timeout;
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Parse database URL into config
|
|
178
|
+
*
|
|
179
|
+
* @returns Client configuration
|
|
180
|
+
*/
|
|
181
|
+
parseDbUrl() {
|
|
182
|
+
const url = new URL(this.dbUrl);
|
|
183
|
+
const pathParts = url.pathname.split('/').filter(Boolean);
|
|
184
|
+
const database = pathParts[pathParts.length - 1];
|
|
185
|
+
return {
|
|
186
|
+
host: url.hostname,
|
|
187
|
+
port: parseInt(url.port) || 2480,
|
|
188
|
+
database,
|
|
189
|
+
username: this.username,
|
|
190
|
+
password: this.password,
|
|
191
|
+
timeout: this.timeout,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
/**
|
|
195
|
+
* Connect to database
|
|
196
|
+
*
|
|
197
|
+
* Creates client instance and establishes connection
|
|
198
|
+
*
|
|
199
|
+
* @returns Result indicating success or error
|
|
200
|
+
*/
|
|
201
|
+
async connect() {
|
|
202
|
+
if (this.client?.isConnected()) {
|
|
203
|
+
return ok(undefined); // Already connected, reuse connection
|
|
204
|
+
}
|
|
205
|
+
try {
|
|
206
|
+
const config = this.parseDbUrl();
|
|
207
|
+
this.client = new ArcadeDBClient(config);
|
|
208
|
+
return await this.client.connect();
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
return err({
|
|
212
|
+
type: DatabaseErrorType.CONNECTION_ERROR,
|
|
213
|
+
message: `Failed to parse database URL or connect: ${error.message}`,
|
|
214
|
+
originalError: error,
|
|
215
|
+
});
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
/**
|
|
219
|
+
* Disconnect from database
|
|
220
|
+
*
|
|
221
|
+
* @returns Result indicating success or error
|
|
222
|
+
*/
|
|
223
|
+
async disconnect() {
|
|
224
|
+
if (!this.client) {
|
|
225
|
+
return ok(undefined);
|
|
226
|
+
}
|
|
227
|
+
const result = await this.client.disconnect();
|
|
228
|
+
this.client = null;
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Execute Cypher query
|
|
233
|
+
*
|
|
234
|
+
* @param cypher - Cypher query command
|
|
235
|
+
* @param params - Optional query parameters
|
|
236
|
+
* @returns Result with query results or error
|
|
237
|
+
*/
|
|
238
|
+
async query(cypher, params) {
|
|
239
|
+
if (!this.client || !this.client.isConnected()) {
|
|
240
|
+
return err({
|
|
241
|
+
type: DatabaseErrorType.NOT_CONNECTED,
|
|
242
|
+
message: 'Not connected to database. Call connect() first.',
|
|
243
|
+
query: cypher,
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
return this.client.query(cypher, params, 'cypher');
|
|
247
|
+
}
|
|
248
|
+
/**
|
|
249
|
+
* Create vertex
|
|
250
|
+
*
|
|
251
|
+
* @param type - Vertex type (label)
|
|
252
|
+
* @param properties - Vertex properties
|
|
253
|
+
* @returns Result with vertex @rid or error
|
|
254
|
+
*/
|
|
255
|
+
async createVertex(type, properties) {
|
|
256
|
+
// Build property map with parameterization for Cypher
|
|
257
|
+
const propKeys = Object.keys(properties);
|
|
258
|
+
const propAssignments = propKeys.map(key => `${key}: $${key}`).join(', ');
|
|
259
|
+
// Cypher syntax: CREATE (n:Type {prop1: value1, prop2: value2}) RETURN n
|
|
260
|
+
const cypher = `CREATE (n:${type} {${propAssignments}}) RETURN n`;
|
|
261
|
+
const params = { ...properties };
|
|
262
|
+
const result = await this.query(cypher, params);
|
|
263
|
+
if (!result.success) {
|
|
264
|
+
return result;
|
|
265
|
+
}
|
|
266
|
+
if (result.data.length === 0 || !result.data[0]['@rid']) {
|
|
267
|
+
return err({
|
|
268
|
+
type: DatabaseErrorType.QUERY_ERROR,
|
|
269
|
+
message: 'Vertex created but @rid not returned',
|
|
270
|
+
query: cypher,
|
|
271
|
+
});
|
|
272
|
+
}
|
|
273
|
+
return ok(result.data[0]['@rid']);
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* Create edge
|
|
277
|
+
*
|
|
278
|
+
* @param fromRid - Source vertex @rid
|
|
279
|
+
* @param toRid - Target vertex @rid
|
|
280
|
+
* @param type - Edge type (label)
|
|
281
|
+
* @param properties - Optional edge properties
|
|
282
|
+
* @returns Result with edge @rid or error
|
|
283
|
+
*/
|
|
284
|
+
async createEdge(fromRid, toRid, type, properties) {
|
|
285
|
+
// Build Cypher query with parameterization
|
|
286
|
+
// Cypher syntax: MATCH (a), (b) WHERE id(a) = '@rid1' AND id(b) = '@rid2'
|
|
287
|
+
// CREATE (a)-[r:TYPE {props}]->(b) RETURN r
|
|
288
|
+
let cypher;
|
|
289
|
+
let params;
|
|
290
|
+
if (properties && Object.keys(properties).length > 0) {
|
|
291
|
+
const propKeys = Object.keys(properties);
|
|
292
|
+
const propAssignments = propKeys.map(key => `${key}: $${key}`).join(', ');
|
|
293
|
+
cypher = `MATCH (a), (b) WHERE id(a) = $fromRid AND id(b) = $toRid CREATE (a)-[r:${type} {${propAssignments}}]->(b) RETURN r`;
|
|
294
|
+
params = { fromRid, toRid, ...properties };
|
|
295
|
+
}
|
|
296
|
+
else {
|
|
297
|
+
cypher = `MATCH (a), (b) WHERE id(a) = $fromRid AND id(b) = $toRid CREATE (a)-[r:${type}]->(b) RETURN r`;
|
|
298
|
+
params = { fromRid, toRid };
|
|
299
|
+
}
|
|
300
|
+
const result = await this.query(cypher, params);
|
|
301
|
+
if (!result.success) {
|
|
302
|
+
return result;
|
|
303
|
+
}
|
|
304
|
+
if (result.data.length === 0 || !result.data[0]['@rid']) {
|
|
305
|
+
return err({
|
|
306
|
+
type: DatabaseErrorType.QUERY_ERROR,
|
|
307
|
+
message: 'Edge created but @rid not returned',
|
|
308
|
+
query: cypher,
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
return ok(result.data[0]['@rid']);
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
//# sourceMappingURL=arcadedb-adapter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"arcadedb-adapter.js","sourceRoot":"","sources":["../../src/memory/arcadedb-adapter.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,EAAE,EAAE,GAAG,EAAE,MAAM,qBAAqB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAsBvD;;;;;GAKG;AACH,MAAM,OAAO,cAAc;IAWI;IAVrB,SAAS,GAAY,KAAK,CAAC;IAClB,OAAO,CAAS;IAChB,IAAI,CAAS;IACb,OAAO,CAAS;IAEjC;;;;OAIG;IACH,YAA6B,MAA4B;QAA5B,WAAM,GAAN,MAAM,CAAsB;QACvD,IAAI,CAAC,OAAO,GAAG,UAAU,MAAM,CAAC,IAAI,IAAI,MAAM,CAAC,IAAI,EAAE,CAAC;QACtD,IAAI,CAAC,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,MAAM,CAAC,QAAQ,IAAI,MAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;QACpF,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC,OAAO,IAAI,KAAK,CAAC;IACzC,CAAC;IAED;;;;;;OAMG;IACH,KAAK,CAAC,OAAO;QACX,IAAI,CAAC;YACH,oDAAoD;Y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Provides low-level file operations for JSON and Markdown files with:
|
|
5
|
+
* - Atomic writes (write to temp → rename)
|
|
6
|
+
* - File locking with exponential backoff
|
|
7
|
+
* - Error handling with Result<T, FileError> pattern
|
|
8
|
+
* - Graceful ENOENT handling (returns null instead of error)
|
|
9
|
+
*/
|
|
10
|
+
import type { Result } from '../common/result.js';
|
|
11
|
+
import type { FileError } from '../common/types.js';
|
|
12
|
+
/**
|
|
13
|
+
* File Storage Adapter
|
|
14
|
+
*
|
|
15
|
+
* Manages low-level file operations with atomic writes and error handling
|
|
16
|
+
*/
|
|
17
|
+
export declare class FileStorageAdapter {
|
|
18
|
+
private readonly dataPath;
|
|
19
|
+
/**
|
|
20
|
+
* In-memory locks for file operations to prevent race conditions
|
|
21
|
+
*/
|
|
22
|
+
private fileLocks;
|
|
23
|
+
/**
|
|
24
|
+
* Create File Storage Adapter
|
|
25
|
+
*
|
|
26
|
+
* @param dataPath - Base directory for file storage
|
|
27
|
+
*/
|
|
28
|
+
constructor(dataPath: string);
|
|
29
|
+
/**
|
|
30
|
+
* Read JSON file
|
|
31
|
+
*
|
|
32
|
+
* @param filePath - Path to JSON file (relative to dataPath)
|
|
33
|
+
* @returns Result with parsed JSON data or null if file doesn't exist
|
|
34
|
+
*/
|
|
35
|
+
readJSON<T>(filePath: string): Promise<Result<T | null, FileError>>;
|
|
36
|
+
/**
|
|
37
|
+
* Write JSON file with atomic write pattern
|
|
38
|
+
*
|
|
39
|
+
* Atomic pattern: write to temp file → rename to final file
|
|
40
|
+
* This prevents partial writes and corruption
|
|
41
|
+
*
|
|
42
|
+
* @param filePath - Path to JSON file (relative to dataPath)
|
|
43
|
+
* @param data - Data to serialize and write
|
|
44
|
+
* @returns Result indicating success or error
|
|
45
|
+
*/
|
|
46
|
+
writeJSON<T>(filePath: string, data: T): Promise<Result<void, FileError>>;
|
|
47
|
+
/**
|
|
48
|
+
* Acquire lock for a file path and execute operation
|
|
49
|
+
*
|
|
50
|
+
* Ensures only one operation at a time per file path
|
|
51
|
+
*
|
|
52
|
+
* @param filePath - Path to lock
|
|
53
|
+
* @param operation - Async operation to execute while locked
|
|
54
|
+
* @returns Result of the operation
|
|
55
|
+
*/
|
|
56
|
+
private withFileLock;
|
|
57
|
+
/**
|
|
58
|
+
* Append item to JSON array file
|
|
59
|
+
*
|
|
60
|
+
* Reads existing array, appends item, writes back atomically with file locking
|
|
61
|
+
*
|
|
62
|
+
* @param filePath - Path to JSON array file
|
|
63
|
+
* @param item - Item to append
|
|
64
|
+
* @returns Result indicating success or error
|
|
65
|
+
*/
|
|
66
|
+
appendToJSONArray<T>(filePath: string, item: T): Promise<Result<void, FileError>>;
|
|
67
|
+
/**
|
|
68
|
+
* Trim JSON array to keep only last N items
|
|
69
|
+
*
|
|
70
|
+
* Reads array, keeps last N items, returns removed items
|
|
71
|
+
*
|
|
72
|
+
* @param filePath - Path to JSON array file
|
|
73
|
+
* @param keepLast - Number of items to keep
|
|
74
|
+
* @returns Result with removed items or error
|
|
75
|
+
*/
|
|
76
|
+
trimJSONArray<T>(filePath: string, keepLast: number): Promise<Result<T[], FileError>>;
|
|
77
|
+
/**
|
|
78
|
+
* Ensure directory exists (create if needed)
|
|
79
|
+
*
|
|
80
|
+
* @param dirPath - Directory path (relative to dataPath)
|
|
81
|
+
* @returns Result indicating success or error
|
|
82
|
+
*/
|
|
83
|
+
ensureDirectory(dirPath: string): Promise<Result<void, FileError>>;
|
|
84
|
+
/**
|
|
85
|
+
* List files in directory
|
|
86
|
+
*
|
|
87
|
+
* @param dirPath - Directory path (relative to dataPath)
|
|
88
|
+
* @returns Result with array of filenames or error
|
|
89
|
+
*/
|
|
90
|
+
listFiles(dirPath: string): Promise<Result<string[], FileError>>;
|
|
91
|
+
/**
|
|
92
|
+
* Delete file
|
|
93
|
+
*
|
|
94
|
+
* @param filePath - File path (relative to dataPath)
|
|
95
|
+
* @returns Result indicating success or error
|
|
96
|
+
*/
|
|
97
|
+
deleteFile(filePath: string): Promise<Result<void, FileError>>;
|
|
98
|
+
/**
|
|
99
|
+
* Read Markdown file
|
|
100
|
+
*
|
|
101
|
+
* @param filePath - Path to Markdown file (relative to dataPath)
|
|
102
|
+
* @returns Result with file content or null if file doesn't exist
|
|
103
|
+
*/
|
|
104
|
+
readMarkdown(filePath: string): Promise<Result<string | null, FileError>>;
|
|
105
|
+
/**
|
|
106
|
+
* Write Markdown file with atomic write pattern
|
|
107
|
+
*
|
|
108
|
+
* @param filePath - Path to Markdown file (relative to dataPath)
|
|
109
|
+
* @param content - Markdown content to write
|
|
110
|
+
* @returns Result indicating success or error
|
|
111
|
+
*/
|
|
112
|
+
writeMarkdown(filePath: string, content: string): Promise<Result<void, FileError>>;
|
|
113
|
+
/**
|
|
114
|
+
* Append content to Markdown file
|
|
115
|
+
*
|
|
116
|
+
* @param filePath - Path to Markdown file
|
|
117
|
+
* @param content - Content to append
|
|
118
|
+
* @returns Result indicating success or error
|
|
119
|
+
*/
|
|
120
|
+
appendToMarkdown(filePath: string, content: string): Promise<Result<void, FileError>>;
|
|
121
|
+
}
|
|
122
|
+
//# sourceMappingURL=file-storage-adapter.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"file-storage-adapter.d.ts","sourceRoot":"","sources":["../../src/memory/file-storage-adapter.ts"],"names":[],"mappings":"AAAA;;;;;;;;GAQG;AAIH,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,qBAAqB,CAAC;AAElD,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,oBAAoB,CAAC;AAGpD;;;;GAIG;AACH,qBAAa,kBAAkB;IAWjB,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAVrC;;OAEG;IACH,OAAO,CAAC,SAAS,CAAyC;IAE1D;;;;OAIG;gBAC0B,QAAQ,EAAE,MAAM;IAE7C;;;;;OAKG;IACG,QAAQ,CAAC,CAAC,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC,GAAG,IAAI,EAAE,SAAS,CA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