micro-models-agent 0.24.12 → 0.25.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.
Files changed (167) hide show
  1. package/dist/cli/commands.js +220 -0
  2. package/dist/cli/completer.js +168 -0
  3. package/dist/cli/index.js +2 -0
  4. package/dist/cli/main.js +113 -0
  5. package/dist/cli/repl.js +987 -0
  6. package/dist/cli/security-commands.js +166 -0
  7. package/dist/cli/setup.js +229 -0
  8. package/dist/config/config.js +186 -0
  9. package/dist/config/defaults.js +91 -0
  10. package/dist/config/experts.js +15 -0
  11. package/dist/config/index.js +3 -0
  12. package/dist/config/security.js +193 -0
  13. package/dist/config/types.js +1 -0
  14. package/dist/core/agent-moe.js +98 -0
  15. package/dist/core/agent.js +461 -0
  16. package/dist/core/bootstrap.js +321 -0
  17. package/dist/core/index.js +2 -0
  18. package/dist/core/prompt-builder.js +55 -0
  19. package/dist/core/session-logger.js +122 -0
  20. package/dist/core/types.js +1 -0
  21. package/dist/i18n/en.json +461 -0
  22. package/dist/i18n/index.js +43 -0
  23. package/dist/i18n/ru.json +461 -0
  24. package/dist/index.js +22 -0
  25. package/dist/llm/image-utils.js +144 -0
  26. package/dist/llm/index.js +4 -0
  27. package/dist/llm/model-loader.js +78 -0
  28. package/dist/llm/openai-compat.js +324 -0
  29. package/dist/llm/orchestrator.js +194 -0
  30. package/dist/llm/provider.js +10 -0
  31. package/dist/llm/response.js +39 -0
  32. package/dist/llm/token-counter.js +39 -0
  33. package/dist/llm/types.js +1 -0
  34. package/dist/logger/app-logger.js +76 -0
  35. package/dist/logger/index.js +1 -0
  36. package/dist/main.js +733 -551
  37. package/dist/migration/backup.js +45 -0
  38. package/dist/migration/detect.js +50 -0
  39. package/dist/migration/index.js +2 -0
  40. package/dist/modules/browser/actions.js +46 -0
  41. package/dist/modules/browser/cookie-store.js +24 -0
  42. package/dist/modules/browser/index.js +5 -0
  43. package/dist/modules/browser/module.js +28 -0
  44. package/dist/modules/browser/session.js +287 -0
  45. package/dist/modules/browser/snapshot.js +114 -0
  46. package/dist/modules/browser/types.js +9 -0
  47. package/dist/modules/context/history.js +15 -0
  48. package/dist/modules/context/index.js +1 -0
  49. package/dist/modules/context/manager.js +240 -0
  50. package/dist/modules/execution/auditor.js +72 -0
  51. package/dist/modules/execution/index.js +6 -0
  52. package/dist/modules/execution/module.js +337 -0
  53. package/dist/modules/execution/moe-executor.js +209 -0
  54. package/dist/modules/execution/plan-validator.js +153 -0
  55. package/dist/modules/execution/planner.js +35 -0
  56. package/dist/modules/execution/stuck-detector.js +134 -0
  57. package/dist/modules/execution/tracker.js +53 -0
  58. package/dist/modules/execution/types.js +1 -0
  59. package/dist/modules/execution/verifier.js +149 -0
  60. package/dist/modules/hallucination/confidence.js +54 -0
  61. package/dist/modules/hallucination/consistency.js +60 -0
  62. package/dist/modules/hallucination/detector.js +41 -0
  63. package/dist/modules/hallucination/factual.js +170 -0
  64. package/dist/modules/hallucination/index.js +4 -0
  65. package/dist/modules/index.js +5 -0
  66. package/dist/modules/indexer/cache.js +38 -0
  67. package/dist/modules/indexer/index.js +3 -0
  68. package/dist/modules/indexer/module.js +192 -0
  69. package/dist/modules/indexer/walker.js +101 -0
  70. package/dist/modules/mcp/client.js +393 -0
  71. package/dist/modules/mcp/index.js +3 -0
  72. package/dist/modules/mcp/module.js +146 -0
  73. package/dist/modules/mcp/registry.js +15 -0
  74. package/dist/modules/memory/index.js +1 -0
  75. package/dist/modules/memory/module.js +48 -0
  76. package/dist/modules/memory/search.js +40 -0
  77. package/dist/modules/memory/store.js +65 -0
  78. package/dist/modules/pipelines/engine.js +60 -0
  79. package/dist/modules/pipelines/index.js +3 -0
  80. package/dist/modules/pipelines/parser.js +53 -0
  81. package/dist/modules/pipelines/template.js +14 -0
  82. package/dist/modules/plugins/builtin/lint-on-write.js +121 -0
  83. package/dist/modules/plugins/builtin/notify.js +8 -0
  84. package/dist/modules/plugins/index.js +1 -0
  85. package/dist/modules/plugins/loader.js +28 -0
  86. package/dist/modules/plugins/manager.js +161 -0
  87. package/dist/modules/plugins/types.js +1 -0
  88. package/dist/modules/processes/detect.js +34 -0
  89. package/dist/modules/processes/index.js +3 -0
  90. package/dist/modules/processes/registry.js +148 -0
  91. package/dist/modules/processes/runner.js +124 -0
  92. package/dist/modules/registry.js +45 -0
  93. package/dist/modules/security/audit-log.js +116 -0
  94. package/dist/modules/security/audit-notifier.js +292 -0
  95. package/dist/modules/security/command-validator.js +185 -0
  96. package/dist/modules/security/content-scanner.js +52 -0
  97. package/dist/modules/security/data-sanitizer.js +97 -0
  98. package/dist/modules/security/encryption.js +240 -0
  99. package/dist/modules/security/index.js +14 -0
  100. package/dist/modules/security/network-validator.js +79 -0
  101. package/dist/modules/security/path-validator.js +155 -0
  102. package/dist/modules/security/rate-limiter.js +119 -0
  103. package/dist/modules/security/security-policies.js +393 -0
  104. package/dist/modules/security/session-encryption.js +193 -0
  105. package/dist/modules/security/session-isolation.js +95 -0
  106. package/dist/modules/session/index.js +3 -0
  107. package/dist/modules/session/manager.js +167 -0
  108. package/dist/modules/session/module.js +24 -0
  109. package/dist/modules/session/store.js +174 -0
  110. package/dist/modules/session/types.js +1 -0
  111. package/dist/modules/skills/index.js +3 -0
  112. package/dist/modules/skills/loader.js +72 -0
  113. package/dist/modules/skills/matcher.js +27 -0
  114. package/dist/modules/skills/module.js +143 -0
  115. package/dist/modules/types.js +1 -0
  116. package/dist/modules/updater/checker.js +32 -0
  117. package/dist/modules/updater/index.js +1 -0
  118. package/dist/modules/user-profile/compressor.js +16 -0
  119. package/dist/modules/user-profile/index.js +1 -0
  120. package/dist/modules/user-profile/profile.js +68 -0
  121. package/dist/tools/approve.js +32 -0
  122. package/dist/tools/attach-image.js +89 -0
  123. package/dist/tools/bash.js +140 -0
  124. package/dist/tools/browser.js +97 -0
  125. package/dist/tools/create-dir.js +56 -0
  126. package/dist/tools/delete-file.js +63 -0
  127. package/dist/tools/edit-file.js +77 -0
  128. package/dist/tools/executor.js +95 -0
  129. package/dist/tools/file-info.js +45 -0
  130. package/dist/tools/filter-tools.js +10 -0
  131. package/dist/tools/glob-tool.js +26 -0
  132. package/dist/tools/grep-tool.js +64 -0
  133. package/dist/tools/index.js +52 -0
  134. package/dist/tools/list-dir.js +47 -0
  135. package/dist/tools/load-skill.js +48 -0
  136. package/dist/tools/mcp-call.js +68 -0
  137. package/dist/tools/move-file.js +84 -0
  138. package/dist/tools/path-utils.js +51 -0
  139. package/dist/tools/pipeline-run.js +144 -0
  140. package/dist/tools/preview.js +2 -0
  141. package/dist/tools/process-kill.js +29 -0
  142. package/dist/tools/process-list.js +38 -0
  143. package/dist/tools/process-log.js +41 -0
  144. package/dist/tools/question.js +142 -0
  145. package/dist/tools/read-file.js +73 -0
  146. package/dist/tools/recall.js +110 -0
  147. package/dist/tools/registry.js +36 -0
  148. package/dist/tools/remember.js +67 -0
  149. package/dist/tools/scope-check.js +30 -0
  150. package/dist/tools/search-history.js +64 -0
  151. package/dist/tools/subagent.js +142 -0
  152. package/dist/tools/types.js +1 -0
  153. package/dist/tools/user-input.js +123 -0
  154. package/dist/tools/web-browse.js +57 -0
  155. package/dist/tools/web-fetch.js +72 -0
  156. package/dist/tools/web-search.js +59 -0
  157. package/dist/tools/write-file.js +80 -0
  158. package/dist/ui/box.js +81 -0
  159. package/dist/ui/colors.js +4 -0
  160. package/dist/ui/diff.js +185 -0
  161. package/dist/ui/index.js +6 -0
  162. package/dist/ui/md-formatter.js +212 -0
  163. package/dist/ui/output.js +13 -0
  164. package/dist/ui/renderer.js +141 -0
  165. package/dist/ui/spinner.js +70 -0
  166. package/dist/ui/table.js +144 -0
  167. package/package.json +1 -1
@@ -0,0 +1,393 @@
1
+ import { mergeSecurityConfig } from '../../config/security';
2
+ /**
3
+ * Strict security policy - maximum security, minimum flexibility
4
+ * Recommended for: production environments, sensitive data, multi-user systems
5
+ */
6
+ export const STRICT_POLICY = {
7
+ name: 'Strict',
8
+ description: 'Maximum security with minimal flexibility. Blocks most dangerous operations.',
9
+ preset: 'strict',
10
+ recommendedFor: ['production', 'sensitive-data', 'multi-user', 'enterprise'],
11
+ config: {
12
+ bash: {
13
+ blacklist: [
14
+ 'rm', 'dd', 'chmod', 'wget', 'curl', 'scp', 'ssh', 'nc', 'netcat',
15
+ 'mknod', 'mkfifo', 'ln', 'chown', 'chgrp', 'useradd', 'usermod',
16
+ 'passwd', 'sudo', 'su', 'exec', 'eval', 'source', 'kill', 'pkill',
17
+ 'killall', 'shutdown', 'reboot', 'halt', 'poweroff', 'format',
18
+ 'fdisk', 'parted', 'mkfs', 'mount', 'umount', 'mv', 'cp',
19
+ ],
20
+ whitelist: ['ls', 'cat', 'echo', 'grep', 'find', 'pwd', 'whoami', 'date'],
21
+ blockDangerousFlags: true,
22
+ dangerousFlags: [
23
+ '--force', '-rf', '--no-preserve-root', '--recursive', '--all',
24
+ '-x', '-e', '--delete', '--remove', '--dangerous', '--yes', '-y',
25
+ '--no-confirm', '-R', '--no-clobber',
26
+ ],
27
+ dangerousOperators: ['>', '>>', '2>', '2>>', '|', '&&', '||', ';', '&', '`'],
28
+ logCommands: true,
29
+ },
30
+ paths: {
31
+ denied: [
32
+ '.git/', 'node_modules/', '.env', '*.key', '*.pem', '*.crt',
33
+ '*.p12', '*.pfx', '*.secret', '*.token', 'package-lock.json',
34
+ 'bun.lock', 'yarn.lock', 'pnpm-lock.yaml', '.ssh/', '.config/',
35
+ '.bashrc', '.bash_profile', '.zshrc', '.profile', '/etc/',
36
+ '/usr/', '/var/', '/bin/', '/sbin/', '/root/', '/home/',
37
+ ],
38
+ allowed: [],
39
+ },
40
+ network: {
41
+ deniedDomains: [
42
+ 'localhost', '127.0.0.1', '0.0.0.0', '::1',
43
+ '*.internal', '*.local', '192.168.*', '10.*', '172.16.*',
44
+ ],
45
+ allowedDomains: [],
46
+ requestTimeout: 10000,
47
+ maxResponseSize: 10000,
48
+ },
49
+ maxRecursionDepth: 2,
50
+ maxFileOperations: 50,
51
+ rateLimits: {
52
+ maxRequestsPerMinute: 30,
53
+ maxParallelTasks: 3,
54
+ },
55
+ contentScan: {
56
+ enabled: true,
57
+ dangerousPatterns: [
58
+ /eval\(/,
59
+ /new Function\(/,
60
+ /child_process\.exec\(/,
61
+ /child_process\.spawn\(/,
62
+ /require\('child_process'\)/,
63
+ /require\('http'\)\.createServer/,
64
+ /require\('net'\)\.createServer/,
65
+ /process\.exit\(/,
66
+ /while\(true\)/,
67
+ /for\(\;\;\)/,
68
+ /rm -rf/,
69
+ /dd if=/,
70
+ /chmod 777/,
71
+ /wget /,
72
+ /curl /,
73
+ /ssh /,
74
+ /scp /,
75
+ ],
76
+ },
77
+ sessionEncryption: {
78
+ enabled: true,
79
+ encryptHistory: true,
80
+ encryptSessionLog: true,
81
+ },
82
+ auditNotifier: {
83
+ enabled: true,
84
+ minSeverity: 'low',
85
+ eventTypes: [
86
+ 'security_block', 'bash_command', 'file_operation',
87
+ 'network_request', 'authentication', 'configuration_change',
88
+ 'session_start', 'session_end',
89
+ ],
90
+ maxRetries: 3,
91
+ },
92
+ },
93
+ };
94
+ /**
95
+ * Balanced security policy - good security with reasonable flexibility
96
+ * Recommended for: development, single-user systems, general use
97
+ */
98
+ export const BALANCED_POLICY = {
99
+ name: 'Balanced',
100
+ description: 'Good security with reasonable flexibility. Default policy.',
101
+ preset: 'balanced',
102
+ recommendedFor: ['development', 'single-user', 'general-use', 'testing'],
103
+ config: {
104
+ bash: {
105
+ blacklist: [
106
+ 'rm', 'dd', 'chmod', 'wget', 'curl', 'scp', 'ssh', 'nc', 'netcat',
107
+ 'mknod', 'mkfifo', 'useradd', 'usermod', 'passwd', 'sudo', 'su',
108
+ 'shutdown', 'reboot', 'halt', 'poweroff', 'format',
109
+ 'fdisk', 'parted', 'mkfs',
110
+ ],
111
+ whitelist: [],
112
+ blockDangerousFlags: true,
113
+ dangerousFlags: [
114
+ '--force', '-rf', '--no-preserve-root', '--recursive', '--all',
115
+ '--delete', '--remove', '--dangerous', '--yes', '-y',
116
+ '--no-confirm',
117
+ ],
118
+ dangerousOperators: ['|', '&&', '||', ';', '&', '`'],
119
+ logCommands: true,
120
+ },
121
+ paths: {
122
+ denied: [
123
+ '.git/', 'node_modules/', '.env', '*.key', '*.pem', '*.crt',
124
+ '*.p12', '*.pfx', '*.secret', '*.token', 'package-lock.json',
125
+ 'bun.lock', 'yarn.lock', 'pnpm-lock.yaml', '.ssh/',
126
+ ],
127
+ allowed: [],
128
+ },
129
+ network: {
130
+ deniedDomains: [],
131
+ allowedDomains: [],
132
+ requestTimeout: 15000,
133
+ maxResponseSize: 15000,
134
+ },
135
+ maxRecursionDepth: 3,
136
+ maxFileOperations: 100,
137
+ rateLimits: {
138
+ maxRequestsPerMinute: 60,
139
+ maxParallelTasks: 5,
140
+ },
141
+ contentScan: {
142
+ enabled: true,
143
+ dangerousPatterns: [
144
+ /eval\(/,
145
+ /new Function\(/,
146
+ /child_process\.exec\(/,
147
+ /child_process\.spawn\(/,
148
+ /require\('child_process'\)/,
149
+ /process\.exit\(/,
150
+ /while\(true\)/,
151
+ /for\(\;\;\)/,
152
+ /rm -rf/,
153
+ /dd if=/,
154
+ /chmod 777/,
155
+ ],
156
+ },
157
+ sessionEncryption: {
158
+ enabled: false,
159
+ encryptHistory: true,
160
+ encryptSessionLog: true,
161
+ },
162
+ auditNotifier: {
163
+ enabled: false,
164
+ minSeverity: 'medium',
165
+ eventTypes: ['security_block', 'bash_command', 'file_operation', 'network_request'],
166
+ maxRetries: 3,
167
+ },
168
+ },
169
+ };
170
+ /**
171
+ * Permissive security policy - minimal security, maximum flexibility
172
+ * Recommended for: trusted environments, local development, testing
173
+ */
174
+ export const PERMISSIVE_POLICY = {
175
+ name: 'Permissive',
176
+ description: 'Minimal security with maximum flexibility. Use with caution.',
177
+ preset: 'permissive',
178
+ recommendedFor: ['trusted-environment', 'local-dev', 'testing', 'sandbox'],
179
+ config: {
180
+ bash: {
181
+ blacklist: [
182
+ 'rm', 'dd', 'shutdown', 'reboot', 'halt', 'poweroff',
183
+ 'format', 'fdisk', 'parted', 'mkfs',
184
+ ],
185
+ whitelist: [],
186
+ blockDangerousFlags: false,
187
+ dangerousFlags: ['--force', '-rf', '--no-preserve-root'],
188
+ dangerousOperators: [],
189
+ logCommands: true,
190
+ },
191
+ paths: {
192
+ denied: ['.env', '*.key', '*.pem', '*.crt', '*.p12', '*.pfx'],
193
+ allowed: [],
194
+ },
195
+ network: {
196
+ deniedDomains: [],
197
+ allowedDomains: [],
198
+ requestTimeout: 30000,
199
+ maxResponseSize: 30000,
200
+ },
201
+ maxRecursionDepth: 5,
202
+ maxFileOperations: 200,
203
+ rateLimits: {
204
+ maxRequestsPerMinute: 120,
205
+ maxParallelTasks: 10,
206
+ },
207
+ contentScan: {
208
+ enabled: true,
209
+ dangerousPatterns: [
210
+ /eval\(/,
211
+ /new Function\(/,
212
+ /while\(true\)/,
213
+ /for\(\;\;\)/,
214
+ /rm -rf/,
215
+ /dd if=/,
216
+ ],
217
+ },
218
+ sessionEncryption: {
219
+ enabled: false,
220
+ encryptHistory: false,
221
+ encryptSessionLog: false,
222
+ },
223
+ auditNotifier: {
224
+ enabled: false,
225
+ minSeverity: 'high',
226
+ eventTypes: ['security_block'],
227
+ maxRetries: 2,
228
+ },
229
+ },
230
+ };
231
+ /**
232
+ * All available security policies
233
+ */
234
+ export const SECURITY_POLICIES = {
235
+ strict: STRICT_POLICY,
236
+ balanced: BALANCED_POLICY,
237
+ permissive: PERMISSIVE_POLICY,
238
+ custom: {
239
+ name: 'Custom',
240
+ description: 'Custom security configuration',
241
+ preset: 'custom',
242
+ recommendedFor: [],
243
+ config: {},
244
+ },
245
+ };
246
+ /**
247
+ * Security policy manager
248
+ */
249
+ export class SecurityPolicyManager {
250
+ currentPolicy = 'balanced';
251
+ customConfig = {};
252
+ /**
253
+ * Get all available policies
254
+ */
255
+ getPolicies() {
256
+ return Object.values(SECURITY_POLICIES);
257
+ }
258
+ /**
259
+ * Get a specific policy by name
260
+ */
261
+ getPolicy(preset) {
262
+ return SECURITY_POLICIES[preset] || SECURITY_POLICIES.balanced;
263
+ }
264
+ /**
265
+ * Get the current policy
266
+ */
267
+ getCurrentPolicy() {
268
+ if (this.currentPolicy === 'custom') {
269
+ return {
270
+ ...SECURITY_POLICIES.custom,
271
+ config: this.customConfig,
272
+ };
273
+ }
274
+ return this.getPolicy(this.currentPolicy);
275
+ }
276
+ /**
277
+ * Set the current policy
278
+ */
279
+ setPolicy(preset) {
280
+ this.currentPolicy = preset;
281
+ }
282
+ /**
283
+ * Set custom configuration (for 'custom' preset)
284
+ */
285
+ setCustomConfig(config) {
286
+ this.customConfig = config;
287
+ this.currentPolicy = 'custom';
288
+ }
289
+ /**
290
+ * Get the current security configuration
291
+ */
292
+ getConfig() {
293
+ const policy = this.getCurrentPolicy();
294
+ return mergeSecurityConfig({
295
+ ...policy.config,
296
+ ...(this.currentPolicy === 'custom' ? this.customConfig : {}),
297
+ });
298
+ }
299
+ /**
300
+ * Apply a policy and return the merged configuration
301
+ */
302
+ applyPolicy(preset, customOverrides) {
303
+ this.setPolicy(preset);
304
+ if (customOverrides) {
305
+ this.customConfig = customOverrides;
306
+ this.currentPolicy = 'custom';
307
+ }
308
+ return this.getConfig();
309
+ }
310
+ /**
311
+ * Get policy recommendation for a use case
312
+ */
313
+ getRecommendation(useCase) {
314
+ for (const policy of this.getPolicies()) {
315
+ if (policy.recommendedFor.includes(useCase)) {
316
+ return policy;
317
+ }
318
+ }
319
+ return BALANCED_POLICY;
320
+ }
321
+ /**
322
+ * Check if current policy allows a specific action
323
+ */
324
+ allowsAction(action, context) {
325
+ const config = this.getConfig();
326
+ switch (action) {
327
+ case 'bash':
328
+ if (context?.command) {
329
+ const cmd = context.command.split(' ')[0];
330
+ return !config.bash.blacklist.includes(cmd);
331
+ }
332
+ return true;
333
+ case 'file_access':
334
+ if (context?.path) {
335
+ for (const denied of config.paths.denied) {
336
+ if (context.path.includes(denied.replace('*', ''))) {
337
+ return false;
338
+ }
339
+ }
340
+ }
341
+ return true;
342
+ case 'network':
343
+ if (context?.path) {
344
+ for (const denied of config.network.deniedDomains) {
345
+ if (context.path.includes(denied.replace('*', ''))) {
346
+ return false;
347
+ }
348
+ }
349
+ }
350
+ return true;
351
+ default:
352
+ return true;
353
+ }
354
+ }
355
+ /**
356
+ * Get policy comparison
357
+ */
358
+ comparePolicies(policy1, policy2) {
359
+ const config1 = this.getPolicy(policy1).config;
360
+ const config2 = this.getPolicy(policy2).config;
361
+ const comparison = {};
362
+ const keys = new Set([...Object.keys(config1), ...Object.keys(config2)]);
363
+ for (const key of keys) {
364
+ comparison[key] = {
365
+ policy1: config1[key],
366
+ policy2: config2[key],
367
+ };
368
+ }
369
+ return comparison;
370
+ }
371
+ }
372
+ /**
373
+ * Global security policy manager instance
374
+ */
375
+ export const globalSecurityPolicyManager = new SecurityPolicyManager();
376
+ /**
377
+ * Create a security policy manager with custom configuration
378
+ */
379
+ export function createSecurityPolicyManager() {
380
+ return new SecurityPolicyManager();
381
+ }
382
+ /**
383
+ * Get a policy by name
384
+ */
385
+ export function getSecurityPolicy(preset) {
386
+ return globalSecurityPolicyManager.getPolicy(preset);
387
+ }
388
+ /**
389
+ * Apply a security policy
390
+ */
391
+ export function applySecurityPolicy(preset, customOverrides) {
392
+ return globalSecurityPolicyManager.applyPolicy(preset, customOverrides);
393
+ }
@@ -0,0 +1,193 @@
1
+ import { readFileSync, writeFileSync, existsSync, readdirSync, unlinkSync } from 'fs';
2
+ import { join } from 'path';
3
+ import { homedir } from 'os';
4
+ import { encryptString, decryptString, isEncryptedString, ConfigEncryptor, } from './encryption';
5
+ /**
6
+ * Default session encryption configuration
7
+ */
8
+ export const DEFAULT_SESSION_ENCRYPTION = {
9
+ enabled: false,
10
+ encryptHistory: true,
11
+ encryptSessionLog: true,
12
+ };
13
+ /**
14
+ * Session file encryptor/decryptor
15
+ */
16
+ export class SessionFileEncryptor {
17
+ config;
18
+ encryptor;
19
+ constructor(config) {
20
+ this.config = { ...DEFAULT_SESSION_ENCRYPTION, ...config };
21
+ this.encryptor = new ConfigEncryptor({
22
+ keyPath: config?.keyPath || join(homedir(), '.mma', '.session-encryption-key')
23
+ });
24
+ }
25
+ /**
26
+ * Check if session file encryption is enabled
27
+ */
28
+ isEnabled() {
29
+ return this.config.enabled;
30
+ }
31
+ /**
32
+ * Encrypt a session file content
33
+ */
34
+ encryptFileContent(content) {
35
+ if (!this.config.enabled)
36
+ return content;
37
+ return encryptString(content, this.encryptor.getKey());
38
+ }
39
+ /**
40
+ * Decrypt a session file content
41
+ */
42
+ decryptFileContent(content) {
43
+ if (!this.config.enabled)
44
+ return content;
45
+ if (!isEncryptedString(content))
46
+ return content;
47
+ return decryptString(content, this.encryptor.getKey());
48
+ }
49
+ /**
50
+ * Encrypt a JSON object (for structured files)
51
+ */
52
+ encryptJSON(obj) {
53
+ if (!this.config.enabled)
54
+ return JSON.stringify(obj);
55
+ return this.encryptFileContent(JSON.stringify(obj));
56
+ }
57
+ /**
58
+ * Decrypt a JSON string
59
+ */
60
+ decryptJSON(content) {
61
+ if (!this.config.enabled)
62
+ return JSON.parse(content);
63
+ const decrypted = this.decryptFileContent(content);
64
+ return JSON.parse(decrypted);
65
+ }
66
+ /**
67
+ * Encrypt a JSONL file (one JSON object per line)
68
+ */
69
+ encryptJSONL(lines) {
70
+ if (!this.config.enabled)
71
+ return lines;
72
+ return lines.map(line => this.encryptFileContent(line));
73
+ }
74
+ /**
75
+ * Decrypt a JSONL file
76
+ */
77
+ decryptJSONL(lines) {
78
+ if (!this.config.enabled)
79
+ return lines;
80
+ return lines.map(line => this.decryptFileContent(line));
81
+ }
82
+ /**
83
+ * Read and decrypt a session file
84
+ */
85
+ readSessionFile(filePath) {
86
+ const content = readFileSync(filePath, 'utf8');
87
+ return this.decryptFileContent(content);
88
+ }
89
+ /**
90
+ * Write and encrypt a session file
91
+ */
92
+ writeSessionFile(filePath, content) {
93
+ const encrypted = this.encryptFileContent(content);
94
+ writeFileSync(filePath, encrypted, 'utf8');
95
+ }
96
+ /**
97
+ * Read and decrypt a JSON session file
98
+ */
99
+ readSessionJSON(filePath) {
100
+ const content = readFileSync(filePath, 'utf8');
101
+ return this.decryptJSON(content);
102
+ }
103
+ /**
104
+ * Write and encrypt a JSON session file
105
+ */
106
+ writeSessionJSON(filePath, obj) {
107
+ const content = this.encryptJSON(obj);
108
+ writeFileSync(filePath, content, 'utf8');
109
+ }
110
+ /**
111
+ * Read and decrypt a JSONL session file
112
+ */
113
+ readSessionJSONL(filePath) {
114
+ const content = readFileSync(filePath, 'utf8');
115
+ const lines = content.split('\n').filter(line => line.trim());
116
+ const decryptedLines = this.decryptJSONL(lines);
117
+ return decryptedLines.map(line => JSON.parse(line));
118
+ }
119
+ /**
120
+ * Append an encrypted entry to a JSONL file
121
+ */
122
+ appendToSessionJSONL(filePath, obj) {
123
+ const encryptedLine = this.encryptFileContent(JSON.stringify(obj));
124
+ writeFileSync(filePath, encryptedLine + '\n', { flag: 'a', encoding: 'utf8' });
125
+ }
126
+ /**
127
+ * Encrypt all session files in a directory
128
+ */
129
+ encryptSessionDirectory(sessionDir) {
130
+ if (!this.config.enabled)
131
+ return;
132
+ const files = readdirSync(sessionDir);
133
+ for (const file of files) {
134
+ const filePath = join(sessionDir, file);
135
+ if (existsSync(filePath) && !file.endsWith('.enc')) {
136
+ try {
137
+ const content = readFileSync(filePath, 'utf8');
138
+ const encrypted = this.encryptFileContent(content);
139
+ writeFileSync(filePath + '.enc', encrypted, 'utf8');
140
+ unlinkSync(filePath);
141
+ }
142
+ catch {
143
+ // Skip binary files or files we can't read
144
+ }
145
+ }
146
+ }
147
+ }
148
+ /**
149
+ * Decrypt all session files in a directory
150
+ */
151
+ decryptSessionDirectory(sessionDir) {
152
+ if (!this.config.enabled)
153
+ return;
154
+ const files = readdirSync(sessionDir);
155
+ for (const file of files) {
156
+ if (file.endsWith('.enc')) {
157
+ const encFilePath = join(sessionDir, file);
158
+ const decFilePath = encFilePath.slice(0, -4); // Remove .enc
159
+ try {
160
+ const content = readFileSync(encFilePath, 'utf8');
161
+ const decrypted = this.decryptFileContent(content);
162
+ writeFileSync(decFilePath, decrypted, 'utf8');
163
+ unlinkSync(encFilePath);
164
+ }
165
+ catch {
166
+ // Skip files we can't decrypt
167
+ }
168
+ }
169
+ }
170
+ }
171
+ }
172
+ /**
173
+ * Global session file encryptor instance
174
+ */
175
+ export const globalSessionEncryptor = new SessionFileEncryptor();
176
+ /**
177
+ * Create a session file encryptor with custom configuration
178
+ */
179
+ export function createSessionFileEncryptor(config) {
180
+ return new SessionFileEncryptor(config);
181
+ }
182
+ /**
183
+ * Helper to encrypt a message object (for SessionMessage)
184
+ */
185
+ export function encryptSessionMessage(msg) {
186
+ return globalSessionEncryptor.encryptJSON(msg);
187
+ }
188
+ /**
189
+ * Helper to decrypt a session message
190
+ */
191
+ export function decryptSessionMessage(encrypted) {
192
+ return globalSessionEncryptor.decryptJSON(encrypted);
193
+ }
@@ -0,0 +1,95 @@
1
+ import { join, resolve } from 'path';
2
+ import { homedir } from 'os';
3
+ import { mkdirSync, existsSync } from 'fs';
4
+ import { DEFAULT_SECURITY_CONFIG } from '../../config/security';
5
+ /**
6
+ * Default session isolation configuration
7
+ */
8
+ export const DEFAULT_SESSION_ISOLATION = {
9
+ enabled: false,
10
+ isolatePlugins: true,
11
+ isolateTempFiles: true,
12
+ };
13
+ /**
14
+ * Create a session context with isolation
15
+ */
16
+ export function createSessionContext(sessionId, projectDir, isolationConfig, securityOverrides) {
17
+ const config = { ...DEFAULT_SESSION_ISOLATION, ...isolationConfig };
18
+ // Create session-specific temporary directory
19
+ const baseDir = config.baseDir || join(homedir(), '.mma', 'sessions', sessionId);
20
+ const tempDir = join(baseDir, 'temp');
21
+ if (config.isolateTempFiles && !existsSync(tempDir)) {
22
+ try {
23
+ mkdirSync(tempDir, { recursive: true, mode: 0o700 });
24
+ }
25
+ catch {
26
+ // If we can't create the temp dir, continue without isolation
27
+ }
28
+ }
29
+ return {
30
+ sessionId,
31
+ tempDir,
32
+ securityOverrides,
33
+ workingDir: projectDir,
34
+ };
35
+ }
36
+ /**
37
+ * Get session-specific security configuration
38
+ * Merges global security config with session overrides
39
+ */
40
+ export function getSessionSecurityConfig(globalConfig, sessionContext) {
41
+ const globalSecurity = globalConfig.security || DEFAULT_SECURITY_CONFIG;
42
+ const sessionOverrides = sessionContext?.securityOverrides;
43
+ if (!sessionOverrides) {
44
+ return globalSecurity;
45
+ }
46
+ return {
47
+ ...globalSecurity,
48
+ ...sessionOverrides,
49
+ // Merge nested objects
50
+ bash: { ...globalSecurity.bash, ...sessionOverrides.bash },
51
+ paths: { ...globalSecurity.paths, ...sessionOverrides.paths },
52
+ network: { ...globalSecurity.network, ...sessionOverrides.network },
53
+ rateLimits: { ...globalSecurity.rateLimits, ...sessionOverrides.rateLimits },
54
+ contentScan: { ...globalSecurity.contentScan, ...sessionOverrides.contentScan },
55
+ };
56
+ }
57
+ /**
58
+ * Get session-specific temporary directory
59
+ */
60
+ export function getSessionTempDir(sessionContext) {
61
+ return sessionContext.tempDir;
62
+ }
63
+ /**
64
+ * Clean up session-specific temporary data
65
+ */
66
+ export function cleanupSessionTempDir(sessionContext) {
67
+ // Note: In a real implementation, we would recursively delete the temp dir
68
+ // For now, we just leave it as the OS will clean up eventually
69
+ // In production, you might want to implement proper cleanup
70
+ }
71
+ /**
72
+ * Create a session-specific file path
73
+ * Ensures the path is within the session's working directory or temp directory
74
+ */
75
+ export function createSessionFilePath(sessionContext, relativePath, useTempDir = false) {
76
+ const baseDir = useTempDir ? sessionContext.tempDir : sessionContext.workingDir;
77
+ return resolve(baseDir, relativePath);
78
+ }
79
+ /**
80
+ * Check if a path is within the session's allowed directories
81
+ */
82
+ export function isPathInSessionScope(sessionContext, path) {
83
+ const resolvedPath = resolve(path);
84
+ const workingDir = resolve(sessionContext.workingDir);
85
+ const tempDir = resolve(sessionContext.tempDir);
86
+ // Check if path is within working directory
87
+ if (resolvedPath.startsWith(workingDir + '/') || resolvedPath.startsWith(workingDir + '\\')) {
88
+ return true;
89
+ }
90
+ // Check if path is within temp directory
91
+ if (resolvedPath.startsWith(tempDir + '/') || resolvedPath.startsWith(tempDir + '\\')) {
92
+ return true;
93
+ }
94
+ return false;
95
+ }
@@ -0,0 +1,3 @@
1
+ export { SessionStore } from './store';
2
+ export { SessionManager } from './manager';
3
+ export { SessionModule } from './module';