@microfox/ai-router 2.0.0 → 2.0.1-beta.2
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/CHANGELOG.md +18 -0
- package/README.md +40 -1
- package/package.json +1 -1
- package/dist/chunk-BJTO5JO5.mjs +0 -11
- package/dist/chunk-BJTO5JO5.mjs.map +0 -1
- package/dist/fs_store.d.mts +0 -5
- package/dist/fs_store.d.ts +0 -5
- package/dist/fs_store.js +0 -122
- package/dist/fs_store.js.map +0 -1
- package/dist/fs_store.mjs +0 -101
- package/dist/fs_store.mjs.map +0 -1
- package/dist/index.d.mts +0 -448
- package/dist/index.d.ts +0 -448
- package/dist/index.js +0 -1390
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -1347
- package/dist/index.mjs.map +0 -1
- package/dist/store-BBHh-uTh.d.mts +0 -27
- package/dist/store-BBHh-uTh.d.ts +0 -27
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,23 @@
|
|
|
1
1
|
# @microfox/ai-router
|
|
2
2
|
|
|
3
|
+
## 2.0.1-beta.2
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- 2a62227: added filter options to agentAsTool
|
|
8
|
+
|
|
9
|
+
## 2.0.1-beta.1
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- 07d4a18: beta release
|
|
14
|
+
|
|
15
|
+
## 2.0.1-beta.0
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- 07d4a18: beta release
|
|
20
|
+
|
|
3
21
|
## 2.0.0
|
|
4
22
|
|
|
5
23
|
### Major Changes
|
package/README.md
CHANGED
|
@@ -688,10 +688,49 @@ interface NextHandler<Metadata, Parts, Tools, State> {
|
|
|
688
688
|
callAgent(path: string, params?: Record<string, any>): Promise<Result>;
|
|
689
689
|
callTool(path: string, params: any): Promise<Result>;
|
|
690
690
|
attachTool(path: string): Tool<any, any>; // Use to expose a standard .tool() to an LLM.
|
|
691
|
-
agentAsTool(
|
|
691
|
+
agentAsTool(
|
|
692
|
+
path: string,
|
|
693
|
+
options?: Record<string, any> & { disableAllInputs?: boolean }
|
|
694
|
+
): Tool<any, any>; // Use to expose another .agent() as a tool to an LLM.
|
|
692
695
|
}
|
|
693
696
|
```
|
|
694
697
|
|
|
698
|
+
### Using Agents as Tools with Controlled Schema
|
|
699
|
+
|
|
700
|
+
The `next.agentAsTool()` method allows you to expose an agent as a tool to an LLM with fine-grained control over the input schema that the LLM sees. This is useful for simplifying the tool for the LLM, fixing certain parameters, or disabling all inputs.
|
|
701
|
+
|
|
702
|
+
#### Examples of `agentAsTool`
|
|
703
|
+
|
|
704
|
+
1. **Only expose certain inputs to the LLM:**
|
|
705
|
+
|
|
706
|
+
If an agent has an input schema with `query` and `otherParameter`, you can expose only the `query` field to the LLM.
|
|
707
|
+
|
|
708
|
+
```typescript
|
|
709
|
+
const researchTool = ctx.next.agentAsTool('/research', {
|
|
710
|
+
query: true, // Only 'query' will be in the schema for the LLM
|
|
711
|
+
});
|
|
712
|
+
```
|
|
713
|
+
|
|
714
|
+
2. **Fix a parameter value:**
|
|
715
|
+
|
|
716
|
+
You can hide a parameter from the LLM and provide a fixed value for it when the agent is executed.
|
|
717
|
+
|
|
718
|
+
```typescript
|
|
719
|
+
const specializedResearchTool = ctx.next.agentAsTool('/research', {
|
|
720
|
+
query: 'web development', // 'query' is not in the LLM's schema; it's always 'web development'
|
|
721
|
+
});
|
|
722
|
+
```
|
|
723
|
+
|
|
724
|
+
3. **Disable all inputs:**
|
|
725
|
+
|
|
726
|
+
You can expose the agent as a tool that takes no inputs from the LLM.
|
|
727
|
+
|
|
728
|
+
```typescript
|
|
729
|
+
const parameterlessTool = ctx.next.agentAsTool('/some-agent', {
|
|
730
|
+
disableAllInputs: true,
|
|
731
|
+
});
|
|
732
|
+
```
|
|
733
|
+
|
|
695
734
|
## Error Types
|
|
696
735
|
|
|
697
736
|
The router provides several error types for different scenarios:
|
package/package.json
CHANGED
package/dist/chunk-BJTO5JO5.mjs
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
2
|
-
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
3
|
-
}) : x)(function(x) {
|
|
4
|
-
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
5
|
-
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
6
|
-
});
|
|
7
|
-
|
|
8
|
-
export {
|
|
9
|
-
__require
|
|
10
|
-
};
|
|
11
|
-
//# sourceMappingURL=chunk-BJTO5JO5.mjs.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
|
package/dist/fs_store.d.mts
DELETED
package/dist/fs_store.d.ts
DELETED
package/dist/fs_store.js
DELETED
|
@@ -1,122 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
|
|
20
|
-
// src/fs_store.ts
|
|
21
|
-
var fs_store_exports = {};
|
|
22
|
-
__export(fs_store_exports, {
|
|
23
|
-
FileSystemStore: () => FileSystemStore
|
|
24
|
-
});
|
|
25
|
-
module.exports = __toCommonJS(fs_store_exports);
|
|
26
|
-
var NoOpStore = class {
|
|
27
|
-
constructor() {
|
|
28
|
-
console.warn(
|
|
29
|
-
"FileSystemStore is not available in the browser. Using a mock store."
|
|
30
|
-
);
|
|
31
|
-
}
|
|
32
|
-
async get(_key) {
|
|
33
|
-
return void 0;
|
|
34
|
-
}
|
|
35
|
-
async set(_key, _value) {
|
|
36
|
-
}
|
|
37
|
-
async delete(_key) {
|
|
38
|
-
}
|
|
39
|
-
async has(_key) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
43
|
-
var store;
|
|
44
|
-
if (typeof window !== "undefined") {
|
|
45
|
-
store = NoOpStore;
|
|
46
|
-
} else {
|
|
47
|
-
const fs = require("fs/promises");
|
|
48
|
-
const path = require("path");
|
|
49
|
-
const AsyncLock = require("async-lock");
|
|
50
|
-
class FileSystemStore2 {
|
|
51
|
-
constructor(storagePath = ".ai-router.store.json") {
|
|
52
|
-
this.storeFilePath = path.resolve(storagePath);
|
|
53
|
-
this.lock = new AsyncLock();
|
|
54
|
-
this.init();
|
|
55
|
-
}
|
|
56
|
-
async init() {
|
|
57
|
-
try {
|
|
58
|
-
await fs.access(this.storeFilePath);
|
|
59
|
-
} catch (error) {
|
|
60
|
-
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
61
|
-
await fs.writeFile(this.storeFilePath, "{}", "utf-8");
|
|
62
|
-
} else {
|
|
63
|
-
console.error("Failed to access or create storage file:", error);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
async readStore() {
|
|
68
|
-
try {
|
|
69
|
-
const fileContent = await fs.readFile(this.storeFilePath, "utf-8");
|
|
70
|
-
return JSON.parse(fileContent);
|
|
71
|
-
} catch (error) {
|
|
72
|
-
console.error(
|
|
73
|
-
"Failed to read store file, returning empty object:",
|
|
74
|
-
error
|
|
75
|
-
);
|
|
76
|
-
return {};
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
async writeStore(data) {
|
|
80
|
-
const fileContent = JSON.stringify(data, null, 2);
|
|
81
|
-
await fs.writeFile(this.storeFilePath, fileContent, "utf-8");
|
|
82
|
-
}
|
|
83
|
-
async get(key) {
|
|
84
|
-
return this.lock.acquire(this.storeFilePath, async () => {
|
|
85
|
-
const store2 = await this.readStore();
|
|
86
|
-
return store2[key];
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
async set(key, value) {
|
|
90
|
-
await this.lock.acquire(this.storeFilePath, async () => {
|
|
91
|
-
const store2 = await this.readStore();
|
|
92
|
-
store2[key] = value;
|
|
93
|
-
await this.writeStore(store2);
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
async delete(key) {
|
|
97
|
-
await this.lock.acquire(this.storeFilePath, async () => {
|
|
98
|
-
const store2 = await this.readStore();
|
|
99
|
-
delete store2[key];
|
|
100
|
-
await this.writeStore(store2);
|
|
101
|
-
});
|
|
102
|
-
}
|
|
103
|
-
async has(key) {
|
|
104
|
-
return this.lock.acquire(this.storeFilePath, async () => {
|
|
105
|
-
const store2 = await this.readStore();
|
|
106
|
-
return key in store2;
|
|
107
|
-
});
|
|
108
|
-
}
|
|
109
|
-
async clear() {
|
|
110
|
-
await this.lock.acquire(this.storeFilePath, async () => {
|
|
111
|
-
await this.writeStore({});
|
|
112
|
-
});
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
store = FileSystemStore2;
|
|
116
|
-
}
|
|
117
|
-
var FileSystemStore = store;
|
|
118
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
119
|
-
0 && (module.exports = {
|
|
120
|
-
FileSystemStore
|
|
121
|
-
});
|
|
122
|
-
//# sourceMappingURL=fs_store.js.map
|
package/dist/fs_store.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/fs_store.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { Store } from './store';\n\n// Type definition for AsyncLock since @types/async-lock might not be available\ntype AsyncLock = {\n acquire<T>(key: string, fn: () => Promise<T>): Promise<T>;\n};\n\nclass NoOpStore implements Store {\n constructor() {\n console.warn(\n 'FileSystemStore is not available in the browser. Using a mock store.'\n );\n }\n\n async get<T>(_key: string): Promise<T | undefined> {\n return undefined;\n }\n\n async set<T>(_key: string, _value: T): Promise<void> {\n // No-op\n }\n\n async delete(_key: string): Promise<void> {\n // No-op\n }\n\n async has(_key: string): Promise<boolean> {\n return false;\n }\n}\n\nlet store: new (...args: any[]) => Store;\nif (typeof window !== 'undefined') {\n store = NoOpStore;\n} else {\n const fs = require('fs/promises');\n const path = require('path');\n const AsyncLock = require('async-lock');\n\n class FileSystemStore implements Store {\n private storeFilePath: string;\n private lock: AsyncLock;\n\n constructor(storagePath: string = '.ai-router.store.json') {\n this.storeFilePath = path.resolve(storagePath);\n this.lock = new AsyncLock();\n this.init();\n }\n\n private async init() {\n try {\n // Check if file exists by trying to read it.\n await fs.access(this.storeFilePath);\n } catch (error: unknown) {\n // If not, create it with an empty object.\n if (\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n error.code === 'ENOENT'\n ) {\n await fs.writeFile(this.storeFilePath, '{}', 'utf-8');\n } else {\n console.error('Failed to access or create storage file:', error);\n }\n }\n }\n\n private async readStore(): Promise<Record<string, unknown>> {\n try {\n const fileContent = await fs.readFile(this.storeFilePath, 'utf-8');\n return JSON.parse(fileContent);\n } catch (error) {\n console.error(\n 'Failed to read store file, returning empty object:',\n error\n );\n return {};\n }\n }\n\n private async writeStore(data: Record<string, unknown>): Promise<void> {\n const fileContent = JSON.stringify(data, null, 2);\n await fs.writeFile(this.storeFilePath, fileContent, 'utf-8');\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return store[key] as T | undefined;\n });\n }\n\n async set<T>(key: string, value: T): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n store[key] = value;\n await this.writeStore(store);\n });\n }\n\n async delete(key: string): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n delete store[key];\n await this.writeStore(store);\n });\n }\n\n async has(key: string): Promise<boolean> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return key in store;\n });\n }\n\n async clear(): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n await this.writeStore({});\n });\n }\n }\n store = FileSystemStore;\n}\n\nexport const FileSystemStore = store;\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAQA,IAAM,YAAN,MAAiC;AAAA,EAC/B,cAAc;AACZ,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,IAAO,MAAsC;AACjD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,IAAO,MAAc,QAA0B;AAAA,EAErD;AAAA,EAEA,MAAM,OAAO,MAA6B;AAAA,EAE1C;AAAA,EAEA,MAAM,IAAI,MAAgC;AACxC,WAAO;AAAA,EACT;AACF;AAEA,IAAI;AACJ,IAAI,OAAO,WAAW,aAAa;AACjC,UAAQ;AACV,OAAO;AACL,QAAM,KAAK,QAAQ,aAAa;AAChC,QAAM,OAAO,QAAQ,MAAM;AAC3B,QAAM,YAAY,QAAQ,YAAY;AAAA,EAEtC,MAAMA,iBAAiC;AAAA,IAIrC,YAAY,cAAsB,yBAAyB;AACzD,WAAK,gBAAgB,KAAK,QAAQ,WAAW;AAC7C,WAAK,OAAO,IAAI,UAAU;AAC1B,WAAK,KAAK;AAAA,IACZ;AAAA,IAEA,MAAc,OAAO;AACnB,UAAI;AAEF,cA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M,IAAI,KAA+B;AACvC,aAAO,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACvD,cAAMA,SAAQ,MAAM,KAAK,UAAU;AACnC,eAAO,OAAOA;AAAA,MAChB,CAAC;AAAA,IACH;AAAA,IAEA,MAAM,QAAuB;AAC3B,YAAM,KAAK,KAAK,QAAQ,KAAK,eAAe,YAAY;AACtD,cAAM,KAAK,WAAW,CAAC,CAAC;AAAA,MAC1B,CAAC;AAAA,IACH;AAAA,EACF;AACA,UAAQD;AACV;AAEO,IAAM,kBAAkB;","names":["FileSystemStore","store"]}
|
package/dist/fs_store.mjs
DELETED
|
@@ -1,101 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
__require
|
|
3
|
-
} from "./chunk-BJTO5JO5.mjs";
|
|
4
|
-
|
|
5
|
-
// src/fs_store.ts
|
|
6
|
-
var NoOpStore = class {
|
|
7
|
-
constructor() {
|
|
8
|
-
console.warn(
|
|
9
|
-
"FileSystemStore is not available in the browser. Using a mock store."
|
|
10
|
-
);
|
|
11
|
-
}
|
|
12
|
-
async get(_key) {
|
|
13
|
-
return void 0;
|
|
14
|
-
}
|
|
15
|
-
async set(_key, _value) {
|
|
16
|
-
}
|
|
17
|
-
async delete(_key) {
|
|
18
|
-
}
|
|
19
|
-
async has(_key) {
|
|
20
|
-
return false;
|
|
21
|
-
}
|
|
22
|
-
};
|
|
23
|
-
var store;
|
|
24
|
-
if (typeof window !== "undefined") {
|
|
25
|
-
store = NoOpStore;
|
|
26
|
-
} else {
|
|
27
|
-
const fs = __require("fs/promises");
|
|
28
|
-
const path = __require("path");
|
|
29
|
-
const AsyncLock = __require("async-lock");
|
|
30
|
-
class FileSystemStore2 {
|
|
31
|
-
constructor(storagePath = ".ai-router.store.json") {
|
|
32
|
-
this.storeFilePath = path.resolve(storagePath);
|
|
33
|
-
this.lock = new AsyncLock();
|
|
34
|
-
this.init();
|
|
35
|
-
}
|
|
36
|
-
async init() {
|
|
37
|
-
try {
|
|
38
|
-
await fs.access(this.storeFilePath);
|
|
39
|
-
} catch (error) {
|
|
40
|
-
if (error && typeof error === "object" && "code" in error && error.code === "ENOENT") {
|
|
41
|
-
await fs.writeFile(this.storeFilePath, "{}", "utf-8");
|
|
42
|
-
} else {
|
|
43
|
-
console.error("Failed to access or create storage file:", error);
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
async readStore() {
|
|
48
|
-
try {
|
|
49
|
-
const fileContent = await fs.readFile(this.storeFilePath, "utf-8");
|
|
50
|
-
return JSON.parse(fileContent);
|
|
51
|
-
} catch (error) {
|
|
52
|
-
console.error(
|
|
53
|
-
"Failed to read store file, returning empty object:",
|
|
54
|
-
error
|
|
55
|
-
);
|
|
56
|
-
return {};
|
|
57
|
-
}
|
|
58
|
-
}
|
|
59
|
-
async writeStore(data) {
|
|
60
|
-
const fileContent = JSON.stringify(data, null, 2);
|
|
61
|
-
await fs.writeFile(this.storeFilePath, fileContent, "utf-8");
|
|
62
|
-
}
|
|
63
|
-
async get(key) {
|
|
64
|
-
return this.lock.acquire(this.storeFilePath, async () => {
|
|
65
|
-
const store2 = await this.readStore();
|
|
66
|
-
return store2[key];
|
|
67
|
-
});
|
|
68
|
-
}
|
|
69
|
-
async set(key, value) {
|
|
70
|
-
await this.lock.acquire(this.storeFilePath, async () => {
|
|
71
|
-
const store2 = await this.readStore();
|
|
72
|
-
store2[key] = value;
|
|
73
|
-
await this.writeStore(store2);
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
async delete(key) {
|
|
77
|
-
await this.lock.acquire(this.storeFilePath, async () => {
|
|
78
|
-
const store2 = await this.readStore();
|
|
79
|
-
delete store2[key];
|
|
80
|
-
await this.writeStore(store2);
|
|
81
|
-
});
|
|
82
|
-
}
|
|
83
|
-
async has(key) {
|
|
84
|
-
return this.lock.acquire(this.storeFilePath, async () => {
|
|
85
|
-
const store2 = await this.readStore();
|
|
86
|
-
return key in store2;
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
async clear() {
|
|
90
|
-
await this.lock.acquire(this.storeFilePath, async () => {
|
|
91
|
-
await this.writeStore({});
|
|
92
|
-
});
|
|
93
|
-
}
|
|
94
|
-
}
|
|
95
|
-
store = FileSystemStore2;
|
|
96
|
-
}
|
|
97
|
-
var FileSystemStore = store;
|
|
98
|
-
export {
|
|
99
|
-
FileSystemStore
|
|
100
|
-
};
|
|
101
|
-
//# sourceMappingURL=fs_store.mjs.map
|
package/dist/fs_store.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/fs_store.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-var-requires */\nimport { Store } from './store';\n\n// Type definition for AsyncLock since @types/async-lock might not be available\ntype AsyncLock = {\n acquire<T>(key: string, fn: () => Promise<T>): Promise<T>;\n};\n\nclass NoOpStore implements Store {\n constructor() {\n console.warn(\n 'FileSystemStore is not available in the browser. Using a mock store.'\n );\n }\n\n async get<T>(_key: string): Promise<T | undefined> {\n return undefined;\n }\n\n async set<T>(_key: string, _value: T): Promise<void> {\n // No-op\n }\n\n async delete(_key: string): Promise<void> {\n // No-op\n }\n\n async has(_key: string): Promise<boolean> {\n return false;\n }\n}\n\nlet store: new (...args: any[]) => Store;\nif (typeof window !== 'undefined') {\n store = NoOpStore;\n} else {\n const fs = require('fs/promises');\n const path = require('path');\n const AsyncLock = require('async-lock');\n\n class FileSystemStore implements Store {\n private storeFilePath: string;\n private lock: AsyncLock;\n\n constructor(storagePath: string = '.ai-router.store.json') {\n this.storeFilePath = path.resolve(storagePath);\n this.lock = new AsyncLock();\n this.init();\n }\n\n private async init() {\n try {\n // Check if file exists by trying to read it.\n await fs.access(this.storeFilePath);\n } catch (error: unknown) {\n // If not, create it with an empty object.\n if (\n error &&\n typeof error === 'object' &&\n 'code' in error &&\n error.code === 'ENOENT'\n ) {\n await fs.writeFile(this.storeFilePath, '{}', 'utf-8');\n } else {\n console.error('Failed to access or create storage file:', error);\n }\n }\n }\n\n private async readStore(): Promise<Record<string, unknown>> {\n try {\n const fileContent = await fs.readFile(this.storeFilePath, 'utf-8');\n return JSON.parse(fileContent);\n } catch (error) {\n console.error(\n 'Failed to read store file, returning empty object:',\n error\n );\n return {};\n }\n }\n\n private async writeStore(data: Record<string, unknown>): Promise<void> {\n const fileContent = JSON.stringify(data, null, 2);\n await fs.writeFile(this.storeFilePath, fileContent, 'utf-8');\n }\n\n async get<T>(key: string): Promise<T | undefined> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return store[key] as T | undefined;\n });\n }\n\n async set<T>(key: string, value: T): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n store[key] = value;\n await this.writeStore(store);\n });\n }\n\n async delete(key: string): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n delete store[key];\n await this.writeStore(store);\n });\n }\n\n async has(key: string): Promise<boolean> {\n return this.lock.acquire(this.storeFilePath, async () => {\n const store = await this.readStore();\n return key in store;\n });\n }\n\n async clear(): Promise<void> {\n await this.lock.acquire(this.storeFilePath, async () => {\n await this.writeStore({});\n });\n }\n }\n store = FileSystemStore;\n}\n\nexport const FileSystemStore = store;\n"],"mappings":";;;;;AAQA,IAAM,YAAN,MAAiC;AAAA,EAC/B,cAAc;AACZ,YAAQ;AAAA,MACN;AAAA,IACF;AAAA,EACF;AAAA,EAEA,MAAM,IAAO,MAAsC;AACjD,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,IAAO,MAAc,QAA0B;AAAA,EAErD;AAAA,EAEA,MAAM,OAAO,MAA6B;AAAA,EAE1C;AAAA,EAEA,MAAM,IAAI,MAAgC;AACxC,WAAO;AAAA,EACT;AACF;AAEA,IAAI;AACJ,IAAI,OAAO,WAAW,aAAa;AACjC,UAAQ;AACV,OAAO;AACL,QAAM,KAAK,UAAQ,aAAa;AAChC,QAAM,OAAO,UAAQ,MAAM;AAC3B,QAAM,YAAY,UAAQ,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