agentlang 0.0.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.
Files changed (240) hide show
  1. package/LICENSE +83 -0
  2. package/README.md +120 -0
  3. package/bin/cli.js +4 -0
  4. package/out/api/http.d.ts +3 -0
  5. package/out/api/http.d.ts.map +1 -0
  6. package/out/api/http.js +290 -0
  7. package/out/api/http.js.map +1 -0
  8. package/out/cli/cli-util.d.ts +7 -0
  9. package/out/cli/cli-util.d.ts.map +1 -0
  10. package/out/cli/cli-util.js +9 -0
  11. package/out/cli/cli-util.js.map +1 -0
  12. package/out/cli/docs.d.ts +2 -0
  13. package/out/cli/docs.d.ts.map +1 -0
  14. package/out/cli/docs.js +236 -0
  15. package/out/cli/docs.js.map +1 -0
  16. package/out/cli/main.d.ts +288 -0
  17. package/out/cli/main.d.ts.map +1 -0
  18. package/out/cli/main.js +119 -0
  19. package/out/cli/main.js.map +1 -0
  20. package/out/cli/openapi-docs.yml +695 -0
  21. package/out/extension/main.cjs +18093 -0
  22. package/out/extension/main.cjs.map +7 -0
  23. package/out/extension/main.d.ts +4 -0
  24. package/out/extension/main.d.ts.map +1 -0
  25. package/out/extension/main.js +42 -0
  26. package/out/extension/main.js.map +1 -0
  27. package/out/language/agentlang-module.d.ts +42 -0
  28. package/out/language/agentlang-module.d.ts.map +1 -0
  29. package/out/language/agentlang-module.js +42 -0
  30. package/out/language/agentlang-module.js.map +1 -0
  31. package/out/language/agentlang-validator.d.ts +15 -0
  32. package/out/language/agentlang-validator.d.ts.map +1 -0
  33. package/out/language/agentlang-validator.js +50 -0
  34. package/out/language/agentlang-validator.js.map +1 -0
  35. package/out/language/generated/ast.d.ts +491 -0
  36. package/out/language/generated/ast.d.ts.map +1 -0
  37. package/out/language/generated/ast.js +934 -0
  38. package/out/language/generated/ast.js.map +1 -0
  39. package/out/language/generated/grammar.d.ts +7 -0
  40. package/out/language/generated/grammar.d.ts.map +1 -0
  41. package/out/language/generated/grammar.js +4475 -0
  42. package/out/language/generated/grammar.js.map +1 -0
  43. package/out/language/generated/module.d.ts +14 -0
  44. package/out/language/generated/module.d.ts.map +1 -0
  45. package/out/language/generated/module.js +21 -0
  46. package/out/language/generated/module.js.map +1 -0
  47. package/out/language/main-browser.d.ts +2 -0
  48. package/out/language/main-browser.d.ts.map +1 -0
  49. package/out/language/main-browser.js +10 -0
  50. package/out/language/main-browser.js.map +1 -0
  51. package/out/language/main.cjs +36229 -0
  52. package/out/language/main.cjs.map +7 -0
  53. package/out/language/main.d.ts +2 -0
  54. package/out/language/main.d.ts.map +1 -0
  55. package/out/language/main.js +11 -0
  56. package/out/language/main.js.map +1 -0
  57. package/out/language/parser.d.ts +9 -0
  58. package/out/language/parser.d.ts.map +1 -0
  59. package/out/language/parser.js +273 -0
  60. package/out/language/parser.js.map +1 -0
  61. package/out/language/syntax.d.ts +155 -0
  62. package/out/language/syntax.d.ts.map +1 -0
  63. package/out/language/syntax.js +527 -0
  64. package/out/language/syntax.js.map +1 -0
  65. package/out/runtime/agents/common.d.ts +2 -0
  66. package/out/runtime/agents/common.d.ts.map +1 -0
  67. package/out/runtime/agents/common.js +178 -0
  68. package/out/runtime/agents/common.js.map +1 -0
  69. package/out/runtime/agents/impl/openai.d.ts +8 -0
  70. package/out/runtime/agents/impl/openai.d.ts.map +1 -0
  71. package/out/runtime/agents/impl/openai.js +15 -0
  72. package/out/runtime/agents/impl/openai.js.map +1 -0
  73. package/out/runtime/agents/provider.d.ts +21 -0
  74. package/out/runtime/agents/provider.d.ts.map +1 -0
  75. package/out/runtime/agents/provider.js +32 -0
  76. package/out/runtime/agents/provider.js.map +1 -0
  77. package/out/runtime/agents/registry.d.ts +2 -0
  78. package/out/runtime/agents/registry.d.ts.map +1 -0
  79. package/out/runtime/agents/registry.js +10 -0
  80. package/out/runtime/agents/registry.js.map +1 -0
  81. package/out/runtime/auth/cognito.d.ts +16 -0
  82. package/out/runtime/auth/cognito.d.ts.map +1 -0
  83. package/out/runtime/auth/cognito.js +186 -0
  84. package/out/runtime/auth/cognito.js.map +1 -0
  85. package/out/runtime/auth/defs.d.ts +11 -0
  86. package/out/runtime/auth/defs.d.ts.map +1 -0
  87. package/out/runtime/auth/defs.js +24 -0
  88. package/out/runtime/auth/defs.js.map +1 -0
  89. package/out/runtime/auth/interface.d.ts +22 -0
  90. package/out/runtime/auth/interface.d.ts.map +1 -0
  91. package/out/runtime/auth/interface.js +2 -0
  92. package/out/runtime/auth/interface.js.map +1 -0
  93. package/out/runtime/defs.js +24 -0
  94. package/out/runtime/defs.js.map +1 -0
  95. package/out/runtime/interpreter.d.ts +69 -0
  96. package/out/runtime/interpreter.d.ts.map +1 -0
  97. package/out/runtime/interpreter.js +1163 -0
  98. package/out/runtime/interpreter.js.map +1 -0
  99. package/out/runtime/loader.d.ts +25 -0
  100. package/out/runtime/loader.d.ts.map +1 -0
  101. package/out/runtime/loader.js +346 -0
  102. package/out/runtime/loader.js.map +1 -0
  103. package/out/runtime/logger.d.ts +2 -0
  104. package/out/runtime/logger.d.ts.map +1 -0
  105. package/out/runtime/logger.js +44 -0
  106. package/out/runtime/logger.js.map +1 -0
  107. package/out/runtime/module.d.ts +273 -0
  108. package/out/runtime/module.d.ts.map +1 -0
  109. package/out/runtime/module.js +1786 -0
  110. package/out/runtime/module.js.map +1 -0
  111. package/out/runtime/modules/ai.d.ts +26 -0
  112. package/out/runtime/modules/ai.d.ts.map +1 -0
  113. package/out/runtime/modules/ai.js +211 -0
  114. package/out/runtime/modules/ai.js.map +1 -0
  115. package/out/runtime/modules/auth.d.ts +39 -0
  116. package/out/runtime/modules/auth.d.ts.map +1 -0
  117. package/out/runtime/modules/auth.js +359 -0
  118. package/out/runtime/modules/auth.js.map +1 -0
  119. package/out/runtime/modules/core.d.ts +2 -0
  120. package/out/runtime/modules/core.d.ts.map +1 -0
  121. package/out/runtime/modules/core.js +67 -0
  122. package/out/runtime/modules/core.js.map +1 -0
  123. package/out/runtime/relgraph.d.ts +21 -0
  124. package/out/runtime/relgraph.d.ts.map +1 -0
  125. package/out/runtime/relgraph.js +156 -0
  126. package/out/runtime/relgraph.js.map +1 -0
  127. package/out/runtime/resolvers/interface.d.ts +59 -0
  128. package/out/runtime/resolvers/interface.d.ts.map +1 -0
  129. package/out/runtime/resolvers/interface.js +111 -0
  130. package/out/runtime/resolvers/interface.js.map +1 -0
  131. package/out/runtime/resolvers/registry.d.ts +8 -0
  132. package/out/runtime/resolvers/registry.d.ts.map +1 -0
  133. package/out/runtime/resolvers/registry.js +26 -0
  134. package/out/runtime/resolvers/registry.js.map +1 -0
  135. package/out/runtime/resolvers/sqldb/database.d.ts +50 -0
  136. package/out/runtime/resolvers/sqldb/database.d.ts.map +1 -0
  137. package/out/runtime/resolvers/sqldb/database.js +618 -0
  138. package/out/runtime/resolvers/sqldb/database.js.map +1 -0
  139. package/out/runtime/resolvers/sqldb/dbutil.d.ts +18 -0
  140. package/out/runtime/resolvers/sqldb/dbutil.d.ts.map +1 -0
  141. package/out/runtime/resolvers/sqldb/dbutil.js +221 -0
  142. package/out/runtime/resolvers/sqldb/dbutil.js.map +1 -0
  143. package/out/runtime/resolvers/sqldb/impl.d.ts +26 -0
  144. package/out/runtime/resolvers/sqldb/impl.d.ts.map +1 -0
  145. package/out/runtime/resolvers/sqldb/impl.js +300 -0
  146. package/out/runtime/resolvers/sqldb/impl.js.map +1 -0
  147. package/out/runtime/state.js +83 -0
  148. package/out/runtime/state.js.map +1 -0
  149. package/out/runtime/util.d.ts +43 -0
  150. package/out/runtime/util.d.ts.map +1 -0
  151. package/out/runtime/util.js +447 -0
  152. package/out/runtime/util.js.map +1 -0
  153. package/out/setupClassic.d.ts +98 -0
  154. package/out/setupClassic.d.ts.map +1 -0
  155. package/out/setupClassic.js +38 -0
  156. package/out/setupClassic.js.map +1 -0
  157. package/out/setupCommon.d.ts +2 -0
  158. package/out/setupCommon.d.ts.map +1 -0
  159. package/out/setupCommon.js +33 -0
  160. package/out/setupCommon.js.map +1 -0
  161. package/out/setupExtended.d.ts +40 -0
  162. package/out/setupExtended.d.ts.map +1 -0
  163. package/out/setupExtended.js +67 -0
  164. package/out/setupExtended.js.map +1 -0
  165. package/out/syntaxes/agentlang.monarch.d.ts +77 -0
  166. package/out/syntaxes/agentlang.monarch.d.ts.map +1 -0
  167. package/out/syntaxes/agentlang.monarch.js +31 -0
  168. package/out/syntaxes/agentlang.monarch.js.map +1 -0
  169. package/out/utils/fs/index.d.ts +14 -0
  170. package/out/utils/fs/index.d.ts.map +1 -0
  171. package/out/utils/fs/index.js +26 -0
  172. package/out/utils/fs/index.js.map +1 -0
  173. package/out/utils/fs/interfaces.d.ts +105 -0
  174. package/out/utils/fs/interfaces.d.ts.map +1 -0
  175. package/out/utils/fs/interfaces.js +5 -0
  176. package/out/utils/fs/interfaces.js.map +1 -0
  177. package/out/utils/fs/lightning-fs.d.ts +116 -0
  178. package/out/utils/fs/lightning-fs.d.ts.map +1 -0
  179. package/out/utils/fs/lightning-fs.js +243 -0
  180. package/out/utils/fs/lightning-fs.js.map +1 -0
  181. package/out/utils/fs/node-fs.d.ts +93 -0
  182. package/out/utils/fs/node-fs.d.ts.map +1 -0
  183. package/out/utils/fs/node-fs.js +169 -0
  184. package/out/utils/fs/node-fs.js.map +1 -0
  185. package/out/utils/fs-utils.d.ts +153 -0
  186. package/out/utils/fs-utils.d.ts.map +1 -0
  187. package/out/utils/fs-utils.js +271 -0
  188. package/out/utils/fs-utils.js.map +1 -0
  189. package/out/utils/runtime.d.ts +36 -0
  190. package/out/utils/runtime.d.ts.map +1 -0
  191. package/out/utils/runtime.js +39 -0
  192. package/out/utils/runtime.js.map +1 -0
  193. package/package.json +155 -0
  194. package/src/api/http.ts +361 -0
  195. package/src/cli/cli-util.ts +18 -0
  196. package/src/cli/main.ts +146 -0
  197. package/src/extension/main.ts +51 -0
  198. package/src/language/agentlang-module.ts +75 -0
  199. package/src/language/agentlang-validator.ts +60 -0
  200. package/src/language/agentlang.langium +178 -0
  201. package/src/language/generated/ast.ts +1698 -0
  202. package/src/language/generated/grammar.ts +4477 -0
  203. package/src/language/generated/module.ts +25 -0
  204. package/src/language/main-browser.ts +19 -0
  205. package/src/language/main.ts +13 -0
  206. package/src/language/parser.ts +329 -0
  207. package/src/language/syntax.ts +646 -0
  208. package/src/runtime/agents/common.ts +177 -0
  209. package/src/runtime/agents/impl/openai.ts +19 -0
  210. package/src/runtime/agents/provider.ts +58 -0
  211. package/src/runtime/agents/registry.ts +9 -0
  212. package/src/runtime/auth/cognito.ts +225 -0
  213. package/src/runtime/auth/defs.ts +33 -0
  214. package/src/runtime/auth/interface.ts +31 -0
  215. package/src/runtime/defs.ts +33 -0
  216. package/src/runtime/interpreter.ts +1352 -0
  217. package/src/runtime/loader.ts +450 -0
  218. package/src/runtime/logger.ts +51 -0
  219. package/src/runtime/module.ts +2188 -0
  220. package/src/runtime/modules/ai.ts +257 -0
  221. package/src/runtime/modules/auth.ts +489 -0
  222. package/src/runtime/modules/core.ts +95 -0
  223. package/src/runtime/relgraph.ts +195 -0
  224. package/src/runtime/resolvers/interface.ts +160 -0
  225. package/src/runtime/resolvers/registry.ts +30 -0
  226. package/src/runtime/resolvers/sqldb/database.ts +823 -0
  227. package/src/runtime/resolvers/sqldb/dbutil.ts +257 -0
  228. package/src/runtime/resolvers/sqldb/impl.ts +471 -0
  229. package/src/runtime/state.ts +87 -0
  230. package/src/runtime/util.ts +513 -0
  231. package/src/setupClassic.ts +43 -0
  232. package/src/setupCommon.ts +33 -0
  233. package/src/setupExtended.ts +79 -0
  234. package/src/syntaxes/agentlang.monarch.ts +31 -0
  235. package/src/utils/fs/index.ts +28 -0
  236. package/src/utils/fs/interfaces.ts +118 -0
  237. package/src/utils/fs/lightning-fs.ts +284 -0
  238. package/src/utils/fs/node-fs.ts +185 -0
  239. package/src/utils/fs-utils.ts +304 -0
  240. package/src/utils/runtime.ts +43 -0
@@ -0,0 +1,28 @@
1
+ /**
2
+ * Filesystem module exports
3
+ */
4
+
5
+ export * from './interfaces.js';
6
+ import { ExtendedFileSystem } from './interfaces.js';
7
+ import { createNodeFS } from './node-fs.js';
8
+ import { createLightningFS } from './lightning-fs.js';
9
+
10
+ /**
11
+ * Create the appropriate filesystem implementation based on environment
12
+ * @returns Promise resolving to appropriate filesystem implementation
13
+ */
14
+ export async function createFS(options?: any): Promise<ExtendedFileSystem> {
15
+ // Check if we're in a browser or Node environment
16
+ if (typeof window === 'undefined') {
17
+ // Node.js environment
18
+ return createNodeFS();
19
+ } else {
20
+ // Browser environment - use Lightning FS
21
+ return createLightningFS(options);
22
+ }
23
+ }
24
+
25
+ // Export the specific filesystem implementations
26
+ export { createNodeFS } from './node-fs.js';
27
+ export { createLightningFS } from './lightning-fs.js';
28
+ export * from './interfaces.js';
@@ -0,0 +1,118 @@
1
+ /**
2
+ * Interfaces for filesystem operations
3
+ */
4
+
5
+ /**
6
+ * Basic filesystem interface that abstracts common operations
7
+ */
8
+ export interface FileSystem {
9
+ /**
10
+ * Read a file as text
11
+ * @param path Path to the file
12
+ * @returns Promise resolving to file content as string
13
+ */
14
+ readFile(path: string): Promise<string>;
15
+
16
+ /**
17
+ * Read a file as binary
18
+ * @param path Path to the file
19
+ * @returns Promise resolving to file content as Buffer
20
+ */
21
+ readFileBuffer(path: string): Promise<Buffer>;
22
+
23
+ /**
24
+ * Write content to a file
25
+ * @param path Path to the file
26
+ * @param data Content to write (string or Buffer)
27
+ * @returns Promise that resolves when write is complete
28
+ */
29
+ writeFile(path: string, data: string | Buffer): Promise<void>;
30
+
31
+ /**
32
+ * Check if a file or directory exists
33
+ * @param path Path to check
34
+ * @returns Promise resolving to boolean which indicates existence
35
+ */
36
+ exists(path: string): Promise<boolean>;
37
+
38
+ /**
39
+ * Create a directory
40
+ * @param path Directory path to create
41
+ * @returns Promise that resolves when directory is created
42
+ */
43
+ mkdir(path: string): Promise<void>;
44
+
45
+ /**
46
+ * List files in a directory
47
+ * @param path Directory path
48
+ * @returns Promise resolving to array of file names
49
+ */
50
+ readdir(path: string): Promise<string[]>;
51
+
52
+ /**
53
+ * Get stats for a file or directory
54
+ * @param path Path to check
55
+ * @returns Promise resolving to stats object
56
+ */
57
+ stat(path: string): Promise<FileStat>;
58
+
59
+ /**
60
+ * Remove a file
61
+ * @param path Path to the file
62
+ * @returns Promise that resolves when file is removed
63
+ */
64
+ unlink(path: string): Promise<void>;
65
+
66
+ /**
67
+ * Remove a directory
68
+ * @param path Path to the directory
69
+ * @returns Promise that resolves when directory is removed
70
+ */
71
+ rmdir(path: string): Promise<void>;
72
+ }
73
+
74
+ /**
75
+ * File stats interface
76
+ */
77
+ export interface FileStat {
78
+ isFile: () => boolean;
79
+ isDirectory: () => boolean;
80
+ isSymbolicLink: () => boolean;
81
+ size: number;
82
+ mtime: Date;
83
+ }
84
+
85
+ /**
86
+ * Extended filesystem interface with additional operations
87
+ */
88
+ export interface ExtendedFileSystem extends FileSystem {
89
+ /**
90
+ * Copy a file
91
+ * @param src Source path
92
+ * @param dest Destination path
93
+ * @returns Promise that resolves when copy is complete
94
+ */
95
+ copyFile(src: string, dest: string): Promise<void>;
96
+
97
+ /**
98
+ * Move a file
99
+ * @param src Source path
100
+ * @param dest Destination path
101
+ * @returns Promise that resolves when move is complete
102
+ */
103
+ moveFile(src: string, dest: string): Promise<void>;
104
+
105
+ /**
106
+ * Ensure a directory exists, creating it and any parent directories if needed
107
+ * @param path Directory path
108
+ * @returns Promise that resolves when directory exists
109
+ */
110
+ ensureDir(path: string): Promise<void>;
111
+
112
+ /**
113
+ * Remove a directory and all its contents recursively
114
+ * @param path Directory path
115
+ * @returns Promise that resolves when directory is removed
116
+ */
117
+ removeDir(path: string): Promise<void>;
118
+ }
@@ -0,0 +1,284 @@
1
+ /**
2
+ * Browser filesystem implementation using Lightning FS
3
+ */
4
+ import { ExtendedFileSystem, FileStat } from './interfaces.js';
5
+ import LightningFS from '@isomorphic-git/lightning-fs';
6
+
7
+ /**
8
+ * Convert Lightning FS stats to our FileStat interface
9
+ */
10
+ function convertStats(stats: any): FileStat {
11
+ return {
12
+ isFile: () => stats.isFile(),
13
+ isDirectory: () => stats.isDirectory(),
14
+ isSymbolicLink: () => false,
15
+ size: stats.size,
16
+ mtime: stats.mtime,
17
+ };
18
+ }
19
+
20
+ /**
21
+ * Browser filesystem implementation using Lightning FS
22
+ */
23
+ export class LightningFileSystem implements ExtendedFileSystem {
24
+ private fs: any;
25
+ private initialized: boolean = false;
26
+
27
+ /**
28
+ * Initialize Lightning FS
29
+ * @param options Lightning FS options
30
+ * @returns Promise that resolves when Lightning FS is initialized
31
+ */
32
+ async initialize(
33
+ options: {
34
+ name?: string;
35
+ wipe?: boolean;
36
+ persistentStorage?: boolean;
37
+ } = {}
38
+ ): Promise<void> {
39
+ if (this.initialized) {
40
+ return;
41
+ }
42
+
43
+ const name = options.name || 'fs';
44
+ const lfs = new LightningFS(name, { wipe: options.wipe });
45
+ this.fs = lfs.promises;
46
+ this.initialized = true;
47
+ }
48
+
49
+ /**
50
+ * Ensure Lightning FS is initialized
51
+ */
52
+ private ensureInitialized(): void {
53
+ if (!this.initialized) {
54
+ throw new Error('LightningFileSystem is not initialized. Call initialize() first.');
55
+ }
56
+ }
57
+
58
+ /**
59
+ * Read a file as text
60
+ * @param filePath path to the file
61
+ * @returns Promise resolving to file content as string
62
+ */
63
+ async readFile(filePath: string): Promise<string> {
64
+ this.ensureInitialized();
65
+ const buffer = await this.fs.readFile(filePath, { encoding: 'utf8' });
66
+ return buffer.toString();
67
+ }
68
+
69
+ /**
70
+ * Read a file as binary
71
+ * @param filePath Path to the file
72
+ * @returns Promise resolving to file content as Buffer
73
+ */
74
+ async readFileBuffer(filePath: string): Promise<Buffer> {
75
+ this.ensureInitialized();
76
+ return this.fs.readFile(filePath);
77
+ }
78
+
79
+ /**
80
+ * Write content to a file
81
+ * @param filePath Path to the file
82
+ * @param data Content to write (string or Buffer)
83
+ * @returns Promise that resolves when write is complete
84
+ */
85
+ async writeFile(filePath: string, data: string | Buffer): Promise<void> {
86
+ this.ensureInitialized();
87
+
88
+ // Ensure the directory exists
89
+ const dir = filePath.substring(0, filePath.lastIndexOf('/'));
90
+ if (dir) {
91
+ await this.ensureDir(dir);
92
+ }
93
+
94
+ return this.fs.writeFile(filePath, data);
95
+ }
96
+
97
+ /**
98
+ * Check if a file or directory exists
99
+ * @param filePath Path to check
100
+ * @returns Promise resolving to boolean indicate existence
101
+ */
102
+ async exists(filePath: string): Promise<boolean> {
103
+ this.ensureInitialized();
104
+ try {
105
+ await this.fs.stat(filePath);
106
+ return true;
107
+ } catch {
108
+ // Just catch any error without binding it to a variable
109
+ return false;
110
+ }
111
+ }
112
+
113
+ /**
114
+ * Create a directory
115
+ * @param dirPath Directory path to create
116
+ * @returns Promise that resolves when directory is created
117
+ */
118
+ async mkdir(dirPath: string): Promise<void> {
119
+ this.ensureInitialized();
120
+ try {
121
+ return await this.fs.mkdir(dirPath);
122
+ } catch (err: any) {
123
+ // Ignore if the directory already exists
124
+ if (err.code !== 'EEXIST') {
125
+ throw err;
126
+ }
127
+ }
128
+ }
129
+
130
+ /**
131
+ * List files in a directory
132
+ * @param dirPath Directory path
133
+ * @returns Promise resolving to an array of file names
134
+ */
135
+ async readdir(dirPath: string): Promise<string[]> {
136
+ this.ensureInitialized();
137
+ return this.fs.readdir(dirPath);
138
+ }
139
+
140
+ /**
141
+ * Get stats for a file or directory
142
+ * @param filePath Path to check
143
+ * @returns Promise resolving to stats object
144
+ */
145
+ async stat(filePath: string): Promise<FileStat> {
146
+ this.ensureInitialized();
147
+ const stats = await this.fs.stat(filePath);
148
+ return convertStats(stats);
149
+ }
150
+
151
+ /**
152
+ * Remove a file
153
+ * @param filePath Path to the file
154
+ * @returns Promise that resolves when a file is removed
155
+ */
156
+ async unlink(filePath: string): Promise<void> {
157
+ this.ensureInitialized();
158
+ return this.fs.unlink(filePath);
159
+ }
160
+
161
+ /**
162
+ * Remove a directory
163
+ * @param dirPath Path to the directory
164
+ * @returns Promise that resolves when directory is removed
165
+ */
166
+ async rmdir(dirPath: string): Promise<void> {
167
+ this.ensureInitialized();
168
+ return this.fs.rmdir(dirPath);
169
+ }
170
+
171
+ /**
172
+ * Copy a file
173
+ * @param src Source path
174
+ * @param dest Destination path
175
+ * @returns Promise that resolves when copy is complete
176
+ */
177
+ async copyFile(src: string, dest: string): Promise<void> {
178
+ this.ensureInitialized();
179
+
180
+ // Ensure destination directory exists
181
+ const destDir = dest.substring(0, dest.lastIndexOf('/'));
182
+ if (destDir) {
183
+ await this.ensureDir(destDir);
184
+ }
185
+
186
+ // Manual implementation
187
+ const content = await this.readFileBuffer(src);
188
+ return this.writeFile(dest, content);
189
+ }
190
+
191
+ /**
192
+ * Move a file
193
+ * @param src Source path
194
+ * @param dest Destination path
195
+ * @returns Promise that resolves when move is complete
196
+ */
197
+ async moveFile(src: string, dest: string): Promise<void> {
198
+ this.ensureInitialized();
199
+
200
+ // Ensure destination directory exists
201
+ const destDir = dest.substring(0, dest.lastIndexOf('/'));
202
+ if (destDir) {
203
+ await this.ensureDir(destDir);
204
+ }
205
+
206
+ // Copy then delete the original
207
+ await this.copyFile(src, dest);
208
+ await this.unlink(src);
209
+ }
210
+
211
+ /**
212
+ * Ensure a directory exists, creating it and any parent directories if needed
213
+ * @param dirPath Directory path
214
+ * @returns Promise that resolves when directory exists
215
+ */
216
+ async ensureDir(dirPath: string): Promise<void> {
217
+ this.ensureInitialized();
218
+
219
+ // Check if the directory already exists
220
+ const exists = await this.exists(dirPath);
221
+ if (exists) {
222
+ return;
223
+ }
224
+
225
+ // Create with a recursive option (Lightning FS supports this)
226
+ try {
227
+ await this.fs.mkdir(dirPath, { recursive: true });
228
+ } catch (err: any) {
229
+ // Ignore if the directory already exists (race condition)
230
+ if (err.code !== 'EEXIST') {
231
+ throw err;
232
+ }
233
+ }
234
+ }
235
+
236
+ /**
237
+ * Remove a directory and all its contents recursively
238
+ * @param dirPath Directory path
239
+ * @returns Promise that resolves when directory is removed
240
+ */
241
+ async removeDir(dirPath: string): Promise<void> {
242
+ this.ensureInitialized();
243
+
244
+ // Check if path exists
245
+ const exists = await this.exists(dirPath);
246
+ if (!exists) {
247
+ return;
248
+ }
249
+
250
+ // Get stats to check if it's a file or directory
251
+ const stats = await this.stat(dirPath);
252
+
253
+ if (stats.isFile()) {
254
+ // If it's a file, just unlink it
255
+ return this.unlink(dirPath);
256
+ }
257
+
258
+ // If it's a directory, remove all contents first
259
+ const files = await this.readdir(dirPath);
260
+
261
+ // Remove each file/directory in the directory
262
+ for (const file of files) {
263
+ const filePath = `${dirPath}/${file}`;
264
+ await this.removeDir(filePath);
265
+ }
266
+
267
+ // Finally, remove the empty directory
268
+ return this.rmdir(dirPath);
269
+ }
270
+ }
271
+
272
+ /**
273
+ * Create a new Lightning FS instance
274
+ * @param options Configuration options
275
+ * @returns Promise resolving to LightningFileSystem instance
276
+ */
277
+ export async function createLightningFS(options?: {
278
+ name?: string;
279
+ wipe?: boolean;
280
+ }): Promise<ExtendedFileSystem> {
281
+ const fs = new LightningFileSystem();
282
+ await fs.initialize(options);
283
+ return fs;
284
+ }
@@ -0,0 +1,185 @@
1
+ /**
2
+ * Node.js filesystem implementation
3
+ */
4
+ import * as fs from 'node:fs/promises';
5
+ import { Stats } from 'node:fs';
6
+ import * as path from 'node:path';
7
+ import { ExtendedFileSystem, FileStat } from './interfaces.js';
8
+
9
+ /**
10
+ * Convert Node.js fs.Stats to our FileStat interface
11
+ */
12
+ function convertStats(stats: Stats): FileStat {
13
+ return {
14
+ isFile: () => stats.isFile(),
15
+ isDirectory: () => stats.isDirectory(),
16
+ isSymbolicLink: () => stats.isSymbolicLink(),
17
+ size: stats.size,
18
+ mtime: stats.mtime,
19
+ };
20
+ }
21
+
22
+ /**
23
+ * Node.js filesystem implementation
24
+ */
25
+ export class NodeFileSystem implements ExtendedFileSystem {
26
+ /**
27
+ * Read a file as text
28
+ * @param filePath Path to the file
29
+ * @returns Promise resolving to file content as string
30
+ */
31
+ async readFile(filePath: string): Promise<string> {
32
+ return fs.readFile(filePath, 'utf8');
33
+ }
34
+
35
+ /**
36
+ * Read a file as binary
37
+ * @param filePath Path to the file
38
+ * @returns Promise resolving to file content as Buffer
39
+ */
40
+ async readFileBuffer(filePath: string): Promise<Buffer> {
41
+ return fs.readFile(filePath);
42
+ }
43
+
44
+ /**
45
+ * Write content to a file
46
+ * @param filePath Path to the file
47
+ * @param data Content to write (string or Buffer)
48
+ * @returns Promise that resolves when write is complete
49
+ */
50
+ async writeFile(filePath: string, data: string | Buffer): Promise<void> {
51
+ // Ensure the directory exists
52
+ const dir = path.dirname(filePath);
53
+ await this.ensureDir(dir);
54
+
55
+ return fs.writeFile(filePath, data);
56
+ }
57
+
58
+ /**
59
+ * Check if a file or directory exists
60
+ * @param filePath Path to check
61
+ * @returns Promise resolving to boolean which indicates existence
62
+ */
63
+ async exists(filePath: string): Promise<boolean> {
64
+ try {
65
+ await fs.access(filePath);
66
+ return true;
67
+ } catch {
68
+ return false;
69
+ }
70
+ }
71
+
72
+ /**
73
+ * Create a directory
74
+ * @param dirPath Directory path to create
75
+ * @returns Promise that resolves when directory is created
76
+ */
77
+ async mkdir(dirPath: string): Promise<void> {
78
+ try {
79
+ await fs.mkdir(dirPath);
80
+ } catch (err: any) {
81
+ // Ignore if the directory already exists
82
+ if (err.code !== 'EEXIST') {
83
+ throw err;
84
+ }
85
+ }
86
+ }
87
+
88
+ /**
89
+ * List files in a directory
90
+ * @param dirPath Directory path
91
+ * @returns Promise resolving to an array of file names
92
+ */
93
+ async readdir(dirPath: string): Promise<string[]> {
94
+ return fs.readdir(dirPath);
95
+ }
96
+
97
+ /**
98
+ * Get stats for a file or directory
99
+ * @param filePath Path to check
100
+ * @returns Promise resolving to stats object
101
+ */
102
+ async stat(filePath: string): Promise<FileStat> {
103
+ const stats = await fs.stat(filePath);
104
+ return convertStats(stats);
105
+ }
106
+
107
+ /**
108
+ * Remove a file
109
+ * @param filePath Path to the file
110
+ * @returns Promise that resolves when a file is removed
111
+ */
112
+ async unlink(filePath: string): Promise<void> {
113
+ return fs.unlink(filePath);
114
+ }
115
+
116
+ /**
117
+ * Remove a directory
118
+ * @param dirPath Path to the directory
119
+ * @returns Promise that resolves when directory is removed
120
+ */
121
+ async rmdir(dirPath: string): Promise<void> {
122
+ return fs.rmdir(dirPath);
123
+ }
124
+
125
+ /**
126
+ * Copy a file
127
+ * @param src Source path
128
+ * @param dest Destination path
129
+ * @returns Promise that resolves when copy is complete
130
+ */
131
+ async copyFile(src: string, dest: string): Promise<void> {
132
+ // Ensure destination directory exists
133
+ const destDir = path.dirname(dest);
134
+ await this.ensureDir(destDir);
135
+
136
+ return fs.copyFile(src, dest);
137
+ }
138
+
139
+ /**
140
+ * Move a file
141
+ * @param src Source path
142
+ * @param dest Destination path
143
+ * @returns Promise that resolves when move is complete
144
+ */
145
+ async moveFile(src: string, dest: string): Promise<void> {
146
+ // Ensure destination directory exists
147
+ const destDir = path.dirname(dest);
148
+ await this.ensureDir(destDir);
149
+
150
+ return fs.rename(src, dest);
151
+ }
152
+
153
+ /**
154
+ * Ensure a directory exists, creating it and any parent directories if needed
155
+ * @param dirPath Directory path
156
+ * @returns Promise that resolves when directory exists
157
+ */
158
+ async ensureDir(dirPath: string): Promise<void> {
159
+ try {
160
+ await fs.mkdir(dirPath, { recursive: true });
161
+ } catch (err: any) {
162
+ // Ignore if the directory already exists
163
+ if (err.code !== 'EEXIST') {
164
+ throw err;
165
+ }
166
+ }
167
+ }
168
+
169
+ /**
170
+ * Remove a directory and all its contents recursively
171
+ * @param dirPath Directory path
172
+ * @returns Promise that resolves when directory is removed
173
+ */
174
+ async removeDir(dirPath: string): Promise<void> {
175
+ return fs.rm(dirPath, { recursive: true, force: true });
176
+ }
177
+ }
178
+
179
+ /**
180
+ * Create a new Node.js filesystem instance
181
+ * @returns NodeFileSystem instance
182
+ */
183
+ export function createNodeFS(): ExtendedFileSystem {
184
+ return new NodeFileSystem();
185
+ }